2 * SPDX-License-Identifier: MIT-CMU
4 * Mach Operating System
5 * Copyright (c) 1991,1990 Carnegie Mellon University
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
29 * Author: David B. Golub, Carnegie Mellon University
34 * Commands to run process.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <machine/kdb.h>
46 #include <machine/pcb.h>
51 #include <ddb/db_break.h>
52 #include <ddb/db_access.h>
57 #define STEP_CONTINUE 4
58 #define STEP_INVISIBLE 5
60 static int db_run_mode = STEP_CONTINUE;
62 static bool db_sstep_multiple;
63 static bool db_sstep_print;
64 static int db_loop_count;
65 static int db_call_depth;
72 db_breakpoint_t db_not_taken_bkpt = 0;
73 db_breakpoint_t db_taken_bkpt = 0;
76 #ifndef db_set_single_step
77 void db_set_single_step(void);
79 #ifndef db_clear_single_step
80 void db_clear_single_step(void);
82 #ifndef db_pc_is_singlestep
84 db_pc_is_singlestep(db_addr_t pc)
87 if ((db_not_taken_bkpt != 0 && pc == db_not_taken_bkpt->address)
88 || (db_taken_bkpt != 0 && pc == db_taken_bkpt->address))
96 db_stop_at_pc(int type, int code, bool *is_breakpoint, bool *is_watchpoint)
101 *is_breakpoint = IS_BREAKPOINT_TRAP(type, code);
102 *is_watchpoint = IS_WATCHPOINT_TRAP(type, code);
104 if (db_pc_is_singlestep(pc))
105 *is_breakpoint = false;
107 db_clear_single_step();
108 db_clear_breakpoints();
109 db_clear_watchpoints();
111 #ifdef FIXUP_PC_AFTER_BREAK
112 if (*is_breakpoint) {
114 * Breakpoint trap. Fix up the PC if the
115 * machine requires it.
123 * Now check for a breakpoint at this address.
125 bkpt = db_find_breakpoint_here(pc);
127 if (--bkpt->count == 0) {
128 bkpt->count = bkpt->init_count;
129 *is_breakpoint = true;
130 return (true); /* stop here */
132 return (false); /* continue the countdown */
133 } else if (*is_breakpoint) {
139 *is_breakpoint = false; /* might be a breakpoint, but not ours */
142 * If not stepping, then silently ignore single-step traps
143 * (except for clearing the single-step-flag above).
145 * If stepping, then abort if the trap type is unexpected.
146 * Breakpoints owned by us are expected and were handled above.
147 * Single-steps are expected and are handled below. All others
150 * Only do either of these if the MD layer claims to classify
151 * single-step traps unambiguously (by defining IS_SSTEP_TRAP).
152 * Otherwise, fall through to the bad historical behaviour
153 * given by turning unexpected traps into expected traps: if not
154 * stepping, then expect only breakpoints and stop, and if
155 * stepping, then expect only single-steps and step.
158 if (db_run_mode == STEP_CONTINUE && IS_SSTEP_TRAP(type, code))
160 if (db_run_mode != STEP_CONTINUE && !IS_SSTEP_TRAP(type, code)) {
161 printf("Stepping aborted\n");
166 if (db_run_mode == STEP_INVISIBLE) {
167 db_run_mode = STEP_CONTINUE;
168 return (false); /* continue */
170 if (db_run_mode == STEP_COUNT) {
171 return (false); /* continue */
173 if (db_run_mode == STEP_ONCE) {
174 if (--db_loop_count > 0) {
175 if (db_sstep_print) {
177 db_print_loc_and_inst(pc);
179 return (false); /* continue */
182 if (db_run_mode == STEP_RETURN) {
183 /* continue until matching return */
186 ins = db_get_value(pc, sizeof(int), false);
187 if (!inst_trap_return(ins) &&
188 (!inst_return(ins) || --db_call_depth != 0)) {
189 if (db_sstep_print) {
190 if (inst_call(ins) || inst_return(ins)) {
193 db_printf("[after %6d] ", db_inst_count);
194 for (i = db_call_depth; --i > 0; )
196 db_print_loc_and_inst(pc);
201 return (false); /* continue */
204 if (db_run_mode == STEP_CALLT) {
205 /* continue until call or return */
208 ins = db_get_value(pc, sizeof(int), false);
209 if (!inst_call(ins) &&
211 !inst_trap_return(ins)) {
212 return (false); /* continue */
219 db_restart_at_pc(bool watchpt)
221 db_addr_t pc = PC_REGS();
223 if ((db_run_mode == STEP_COUNT) ||
224 ((db_run_mode == STEP_ONCE) && db_sstep_multiple) ||
225 (db_run_mode == STEP_RETURN) ||
226 (db_run_mode == STEP_CALLT)) {
228 * We are about to execute this instruction,
231 #ifdef SOFTWARE_SSTEP
234 db_get_value(pc, sizeof(int), false);
236 db_load_count += inst_load(ins);
237 db_store_count += inst_store(ins);
238 #ifdef SOFTWARE_SSTEP
239 /* XXX works on mips, but... */
240 if (inst_branch(ins) || inst_call(ins)) {
241 ins = db_get_value(next_instr_address(pc,1),
244 db_load_count += inst_load(ins);
245 db_store_count += inst_store(ins);
247 #endif /* SOFTWARE_SSTEP */
250 if (db_run_mode == STEP_CONTINUE) {
251 if (watchpt || db_find_breakpoint_here(pc)) {
253 * Step over breakpoint/watchpoint.
255 db_run_mode = STEP_INVISIBLE;
256 db_set_single_step();
258 db_set_breakpoints();
259 db_set_watchpoints();
262 db_set_single_step();
266 #ifdef SOFTWARE_SSTEP
268 * Software implementation of single-stepping.
269 * If your machine does not have a trace mode
270 * similar to the vax or sun ones you can use
271 * this implementation, done for the mips.
272 * Just define the above conditional and provide
273 * the functions/macros defined below.
276 * inst_branch(), returns true if the instruction might branch
278 * branch_taken(), return the address the instruction might
280 * db_getreg_val(); return the value of a user register,
281 * as indicated in the hardware instruction
282 * encoding, e.g. 8 for r8
284 * next_instr_address(pc,bd) returns the address of the first
285 * instruction following the one at "pc",
286 * which is either in the taken path of
287 * the branch (bd==1) or not. This is
288 * for machines (mips) with branch delays.
290 * A single-step may involve at most 2 breakpoints -
291 * one for branch-not-taken and one for branch taken.
292 * If one of these addresses does not already have a breakpoint,
293 * we allocate a breakpoint and save it here.
294 * These breakpoints are deleted on return.
298 db_set_single_step(void)
300 db_addr_t pc = PC_REGS(), brpc;
304 * User was stopped at pc, e.g. the instruction
305 * at pc was not executed.
307 inst = db_get_value(pc, sizeof(int), false);
308 if (inst_branch(inst) || inst_call(inst) || inst_return(inst)) {
309 brpc = branch_taken(inst, pc);
310 if (brpc != pc) { /* self-branches are hopeless */
311 db_taken_bkpt = db_set_temp_breakpoint(brpc);
313 pc = next_instr_address(pc, 1);
315 pc = next_instr_address(pc, 0);
316 db_not_taken_bkpt = db_set_temp_breakpoint(pc);
320 db_clear_single_step(void)
323 if (db_not_taken_bkpt != 0) {
324 db_delete_temp_breakpoint(db_not_taken_bkpt);
325 db_not_taken_bkpt = 0;
327 if (db_taken_bkpt != 0) {
328 db_delete_temp_breakpoint(db_taken_bkpt);
333 #endif /* SOFTWARE_SSTEP */
335 extern int db_cmd_loop_done;
340 db_single_step_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
350 db_run_mode = STEP_ONCE;
351 db_loop_count = count;
352 db_sstep_multiple = (count != 1);
353 db_sstep_print = print;
358 db_cmd_loop_done = 1;
361 /* trace and print until call/return */
364 db_trace_until_call_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
372 db_run_mode = STEP_CALLT;
373 db_sstep_print = print;
378 db_cmd_loop_done = 1;
383 db_trace_until_matching_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
391 db_run_mode = STEP_RETURN;
393 db_sstep_print = print;
398 db_cmd_loop_done = 1;
404 db_continue_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
407 db_run_mode = STEP_COUNT;
409 db_run_mode = STEP_CONTINUE;
414 db_cmd_loop_done = 1;