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_stop_at_pc(int type, int code, bool *is_breakpoint, bool *is_watchpoint)
79 *is_breakpoint = IS_BREAKPOINT_TRAP(type, code);
80 *is_watchpoint = IS_WATCHPOINT_TRAP(type, code);
83 db_clear_single_step();
84 db_clear_breakpoints();
85 db_clear_watchpoints();
87 #ifdef FIXUP_PC_AFTER_BREAK
90 * Breakpoint trap. Fix up the PC if the
91 * machine requires it.
99 * Now check for a breakpoint at this address.
101 bkpt = db_find_breakpoint_here(pc);
103 if (--bkpt->count == 0) {
104 bkpt->count = bkpt->init_count;
105 *is_breakpoint = true;
106 return (true); /* stop here */
108 return (false); /* continue the countdown */
109 } else if (*is_breakpoint) {
115 *is_breakpoint = false; /* might be a breakpoint, but not ours */
118 * If not stepping, then silently ignore single-step traps
119 * (except for clearing the single-step-flag above).
121 * If stepping, then abort if the trap type is unexpected.
122 * Breakpoints owned by us are expected and were handled above.
123 * Single-steps are expected and are handled below. All others
126 * Only do either of these if the MD layer claims to classify
127 * single-step traps unambiguously (by defining IS_SSTEP_TRAP).
128 * Otherwise, fall through to the bad historical behaviour
129 * given by turning unexpected traps into expected traps: if not
130 * stepping, then expect only breakpoints and stop, and if
131 * stepping, then expect only single-steps and step.
134 if (db_run_mode == STEP_CONTINUE && IS_SSTEP_TRAP(type, code))
136 if (db_run_mode != STEP_CONTINUE && !IS_SSTEP_TRAP(type, code)) {
137 printf("Stepping aborted\n");
142 if (db_run_mode == STEP_INVISIBLE) {
143 db_run_mode = STEP_CONTINUE;
144 return (false); /* continue */
146 if (db_run_mode == STEP_COUNT) {
147 return (false); /* continue */
149 if (db_run_mode == STEP_ONCE) {
150 if (--db_loop_count > 0) {
151 if (db_sstep_print) {
153 db_print_loc_and_inst(pc);
155 return (false); /* continue */
158 if (db_run_mode == STEP_RETURN) {
159 /* continue until matching return */
162 ins = db_get_value(pc, sizeof(int), false);
163 if (!inst_trap_return(ins) &&
164 (!inst_return(ins) || --db_call_depth != 0)) {
165 if (db_sstep_print) {
166 if (inst_call(ins) || inst_return(ins)) {
169 db_printf("[after %6d] ", db_inst_count);
170 for (i = db_call_depth; --i > 0; )
172 db_print_loc_and_inst(pc);
177 return (false); /* continue */
180 if (db_run_mode == STEP_CALLT) {
181 /* continue until call or return */
184 ins = db_get_value(pc, sizeof(int), false);
185 if (!inst_call(ins) &&
187 !inst_trap_return(ins)) {
188 return (false); /* continue */
195 db_restart_at_pc(bool watchpt)
197 db_addr_t pc = PC_REGS();
199 if ((db_run_mode == STEP_COUNT) ||
200 ((db_run_mode == STEP_ONCE) && db_sstep_multiple) ||
201 (db_run_mode == STEP_RETURN) ||
202 (db_run_mode == STEP_CALLT)) {
204 * We are about to execute this instruction,
207 db_get_value(pc, sizeof(int), false);
209 db_load_count += inst_load(ins);
210 db_store_count += inst_store(ins);
213 if (db_run_mode == STEP_CONTINUE) {
214 if (watchpt || db_find_breakpoint_here(pc)) {
216 * Step over breakpoint/watchpoint.
218 db_run_mode = STEP_INVISIBLE;
219 db_set_single_step();
221 db_set_breakpoints();
222 db_set_watchpoints();
225 db_set_single_step();
232 db_single_step_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
242 db_run_mode = STEP_ONCE;
243 db_loop_count = count;
244 db_sstep_multiple = (count != 1);
245 db_sstep_print = print;
250 db_cmd_loop_done = 1;
253 /* trace and print until call/return */
256 db_trace_until_call_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
264 db_run_mode = STEP_CALLT;
265 db_sstep_print = print;
270 db_cmd_loop_done = 1;
275 db_trace_until_matching_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
283 db_run_mode = STEP_RETURN;
285 db_sstep_print = print;
290 db_cmd_loop_done = 1;
296 db_continue_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
299 db_run_mode = STEP_COUNT;
301 db_run_mode = STEP_CONTINUE;
306 db_cmd_loop_done = 1;