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>
44 #include <sys/systm.h>
46 #include <machine/kdb.h>
47 #include <machine/pcb.h>
52 #include <ddb/db_access.h>
53 #include <ddb/db_break.h>
54 #include <ddb/db_command.h>
59 #define STEP_CONTINUE 4
60 #define STEP_INVISIBLE 5
62 static int db_run_mode = STEP_CONTINUE;
64 static bool db_sstep_multiple;
65 static bool db_sstep_print;
66 static int db_loop_count;
67 static int db_call_depth;
74 db_breakpoint_t db_not_taken_bkpt = 0;
75 db_breakpoint_t db_taken_bkpt = 0;
78 #ifndef db_set_single_step
79 void db_set_single_step(void);
81 #ifndef db_clear_single_step
82 void db_clear_single_step(void);
84 #ifndef db_pc_is_singlestep
86 db_pc_is_singlestep(db_addr_t pc)
89 if ((db_not_taken_bkpt != 0 && pc == db_not_taken_bkpt->address)
90 || (db_taken_bkpt != 0 && pc == db_taken_bkpt->address))
98 db_stop_at_pc(int type, int code, bool *is_breakpoint, bool *is_watchpoint)
101 db_breakpoint_t bkpt;
103 *is_breakpoint = IS_BREAKPOINT_TRAP(type, code);
104 *is_watchpoint = IS_WATCHPOINT_TRAP(type, code);
106 if (db_pc_is_singlestep(pc))
107 *is_breakpoint = false;
109 db_clear_single_step();
110 db_clear_breakpoints();
111 db_clear_watchpoints();
113 #ifdef FIXUP_PC_AFTER_BREAK
114 if (*is_breakpoint) {
116 * Breakpoint trap. Fix up the PC if the
117 * machine requires it.
125 * Now check for a breakpoint at this address.
127 bkpt = db_find_breakpoint_here(pc);
129 if (--bkpt->count == 0) {
130 bkpt->count = bkpt->init_count;
131 *is_breakpoint = true;
132 return (true); /* stop here */
134 return (false); /* continue the countdown */
135 } else if (*is_breakpoint) {
141 *is_breakpoint = false; /* might be a breakpoint, but not ours */
144 * If not stepping, then silently ignore single-step traps
145 * (except for clearing the single-step-flag above).
147 * If stepping, then abort if the trap type is unexpected.
148 * Breakpoints owned by us are expected and were handled above.
149 * Single-steps are expected and are handled below. All others
152 * Only do either of these if the MD layer claims to classify
153 * single-step traps unambiguously (by defining IS_SSTEP_TRAP).
154 * Otherwise, fall through to the bad historical behaviour
155 * given by turning unexpected traps into expected traps: if not
156 * stepping, then expect only breakpoints and stop, and if
157 * stepping, then expect only single-steps and step.
160 if (db_run_mode == STEP_CONTINUE && IS_SSTEP_TRAP(type, code))
162 if (db_run_mode != STEP_CONTINUE && !IS_SSTEP_TRAP(type, code)) {
163 printf("Stepping aborted\n");
168 if (db_run_mode == STEP_INVISIBLE) {
169 db_run_mode = STEP_CONTINUE;
170 return (false); /* continue */
172 if (db_run_mode == STEP_COUNT) {
173 return (false); /* continue */
175 if (db_run_mode == STEP_ONCE) {
176 if (--db_loop_count > 0) {
177 if (db_sstep_print) {
179 db_print_loc_and_inst(pc);
181 return (false); /* continue */
184 if (db_run_mode == STEP_RETURN) {
185 /* continue until matching return */
188 ins = db_get_value(pc, sizeof(int), false);
189 if (!inst_trap_return(ins) &&
190 (!inst_return(ins) || --db_call_depth != 0)) {
191 if (db_sstep_print) {
192 if (inst_call(ins) || inst_return(ins)) {
195 db_printf("[after %6d] ", db_inst_count);
196 for (i = db_call_depth; --i > 0; )
198 db_print_loc_and_inst(pc);
203 return (false); /* continue */
206 if (db_run_mode == STEP_CALLT) {
207 /* continue until call or return */
210 ins = db_get_value(pc, sizeof(int), false);
211 if (!inst_call(ins) &&
213 !inst_trap_return(ins)) {
214 return (false); /* continue */
221 db_restart_at_pc(bool watchpt)
223 db_addr_t pc = PC_REGS();
225 if ((db_run_mode == STEP_COUNT) ||
226 ((db_run_mode == STEP_ONCE) && db_sstep_multiple) ||
227 (db_run_mode == STEP_RETURN) ||
228 (db_run_mode == STEP_CALLT)) {
230 * We are about to execute this instruction,
233 #ifdef SOFTWARE_SSTEP
236 db_get_value(pc, sizeof(int), false);
238 db_load_count += inst_load(ins);
239 db_store_count += inst_store(ins);
240 #ifdef SOFTWARE_SSTEP
241 /* XXX works on mips, but... */
242 if (inst_branch(ins) || inst_call(ins)) {
243 ins = db_get_value(next_instr_address(pc,1),
246 db_load_count += inst_load(ins);
247 db_store_count += inst_store(ins);
249 #endif /* SOFTWARE_SSTEP */
252 if (db_run_mode == STEP_CONTINUE) {
253 if (watchpt || db_find_breakpoint_here(pc)) {
255 * Step over breakpoint/watchpoint.
257 db_run_mode = STEP_INVISIBLE;
258 db_set_single_step();
260 db_set_breakpoints();
261 db_set_watchpoints();
264 db_set_single_step();
268 #ifdef SOFTWARE_SSTEP
270 * Software implementation of single-stepping.
271 * If your machine does not have a trace mode
272 * similar to the vax or sun ones you can use
273 * this implementation, done for the mips.
274 * Just define the above conditional and provide
275 * the functions/macros defined below.
278 * inst_branch(), returns true if the instruction might branch
280 * branch_taken(), return the address the instruction might
282 * db_getreg_val(); return the value of a user register,
283 * as indicated in the hardware instruction
284 * encoding, e.g. 8 for r8
286 * next_instr_address(pc,bd) returns the address of the first
287 * instruction following the one at "pc",
288 * which is either in the taken path of
289 * the branch (bd==1) or not. This is
290 * for machines (mips) with branch delays.
292 * A single-step may involve at most 2 breakpoints -
293 * one for branch-not-taken and one for branch taken.
294 * If one of these addresses does not already have a breakpoint,
295 * we allocate a breakpoint and save it here.
296 * These breakpoints are deleted on return.
300 db_set_single_step(void)
302 db_addr_t pc = PC_REGS(), brpc;
306 * User was stopped at pc, e.g. the instruction
307 * at pc was not executed.
309 inst = db_get_value(pc, sizeof(int), false);
310 if (inst_branch(inst) || inst_call(inst) || inst_return(inst)) {
311 brpc = branch_taken(inst, pc);
312 if (brpc != pc) { /* self-branches are hopeless */
313 db_taken_bkpt = db_set_temp_breakpoint(brpc);
315 pc = next_instr_address(pc, 1);
317 pc = next_instr_address(pc, 0);
318 db_not_taken_bkpt = db_set_temp_breakpoint(pc);
322 db_clear_single_step(void)
325 if (db_not_taken_bkpt != 0) {
326 db_delete_temp_breakpoint(db_not_taken_bkpt);
327 db_not_taken_bkpt = 0;
329 if (db_taken_bkpt != 0) {
330 db_delete_temp_breakpoint(db_taken_bkpt);
335 #endif /* SOFTWARE_SSTEP */
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;