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 <machine/kdb.h>
45 #include <machine/pcb.h>
50 #include <ddb/db_break.h>
51 #include <ddb/db_access.h>
56 #define STEP_CONTINUE 4
57 #define STEP_INVISIBLE 5
59 static int db_run_mode = STEP_CONTINUE;
61 static bool db_sstep_multiple;
62 static bool db_sstep_print;
63 static int db_loop_count;
64 static int db_call_depth;
71 db_breakpoint_t db_not_taken_bkpt = 0;
72 db_breakpoint_t db_taken_bkpt = 0;
75 #ifndef db_set_single_step
76 void db_set_single_step(void);
78 #ifndef db_clear_single_step
79 void db_clear_single_step(void);
81 #ifndef db_pc_is_singlestep
83 db_pc_is_singlestep(db_addr_t pc)
86 if ((db_not_taken_bkpt != 0 && pc == db_not_taken_bkpt->address)
87 || (db_taken_bkpt != 0 && pc == db_taken_bkpt->address))
95 db_stop_at_pc(int type, int code, bool *is_breakpoint, bool *is_watchpoint)
100 *is_breakpoint = IS_BREAKPOINT_TRAP(type, code);
101 *is_watchpoint = IS_WATCHPOINT_TRAP(type, code);
103 if (db_pc_is_singlestep(pc))
104 *is_breakpoint = false;
106 db_clear_single_step();
107 db_clear_breakpoints();
108 db_clear_watchpoints();
110 #ifdef FIXUP_PC_AFTER_BREAK
111 if (*is_breakpoint) {
113 * Breakpoint trap. Fix up the PC if the
114 * machine requires it.
122 * Now check for a breakpoint at this address.
124 bkpt = db_find_breakpoint_here(pc);
126 if (--bkpt->count == 0) {
127 bkpt->count = bkpt->init_count;
128 *is_breakpoint = true;
129 return (true); /* stop here */
131 return (false); /* continue the countdown */
132 } else if (*is_breakpoint) {
138 *is_breakpoint = false; /* might be a breakpoint, but not ours */
141 * If not stepping, then silently ignore single-step traps
142 * (except for clearing the single-step-flag above).
144 * If stepping, then abort if the trap type is unexpected.
145 * Breakpoints owned by us are expected and were handled above.
146 * Single-steps are expected and are handled below. All others
149 * Only do either of these if the MD layer claims to classify
150 * single-step traps unambiguously (by defining IS_SSTEP_TRAP).
151 * Otherwise, fall through to the bad historical behaviour
152 * given by turning unexpected traps into expected traps: if not
153 * stepping, then expect only breakpoints and stop, and if
154 * stepping, then expect only single-steps and step.
157 if (db_run_mode == STEP_CONTINUE && IS_SSTEP_TRAP(type, code))
159 if (db_run_mode != STEP_CONTINUE && !IS_SSTEP_TRAP(type, code)) {
160 printf("Stepping aborted\n");
165 if (db_run_mode == STEP_INVISIBLE) {
166 db_run_mode = STEP_CONTINUE;
167 return (false); /* continue */
169 if (db_run_mode == STEP_COUNT) {
170 return (false); /* continue */
172 if (db_run_mode == STEP_ONCE) {
173 if (--db_loop_count > 0) {
174 if (db_sstep_print) {
176 db_print_loc_and_inst(pc);
178 return (false); /* continue */
181 if (db_run_mode == STEP_RETURN) {
182 /* continue until matching return */
185 ins = db_get_value(pc, sizeof(int), false);
186 if (!inst_trap_return(ins) &&
187 (!inst_return(ins) || --db_call_depth != 0)) {
188 if (db_sstep_print) {
189 if (inst_call(ins) || inst_return(ins)) {
192 db_printf("[after %6d] ", db_inst_count);
193 for (i = db_call_depth; --i > 0; )
195 db_print_loc_and_inst(pc);
200 return (false); /* continue */
203 if (db_run_mode == STEP_CALLT) {
204 /* continue until call or return */
207 ins = db_get_value(pc, sizeof(int), false);
208 if (!inst_call(ins) &&
210 !inst_trap_return(ins)) {
211 return (false); /* continue */
218 db_restart_at_pc(bool watchpt)
220 db_addr_t pc = PC_REGS();
222 if ((db_run_mode == STEP_COUNT) ||
223 ((db_run_mode == STEP_ONCE) && db_sstep_multiple) ||
224 (db_run_mode == STEP_RETURN) ||
225 (db_run_mode == STEP_CALLT)) {
227 * We are about to execute this instruction,
230 #ifdef SOFTWARE_SSTEP
233 db_get_value(pc, sizeof(int), false);
235 db_load_count += inst_load(ins);
236 db_store_count += inst_store(ins);
237 #ifdef SOFTWARE_SSTEP
238 /* XXX works on mips, but... */
239 if (inst_branch(ins) || inst_call(ins)) {
240 ins = db_get_value(next_instr_address(pc,1),
243 db_load_count += inst_load(ins);
244 db_store_count += inst_store(ins);
246 #endif /* SOFTWARE_SSTEP */
249 if (db_run_mode == STEP_CONTINUE) {
250 if (watchpt || db_find_breakpoint_here(pc)) {
252 * Step over breakpoint/watchpoint.
254 db_run_mode = STEP_INVISIBLE;
255 db_set_single_step();
257 db_set_breakpoints();
258 db_set_watchpoints();
261 db_set_single_step();
265 #ifdef SOFTWARE_SSTEP
267 * Software implementation of single-stepping.
268 * If your machine does not have a trace mode
269 * similar to the vax or sun ones you can use
270 * this implementation, done for the mips.
271 * Just define the above conditional and provide
272 * the functions/macros defined below.
275 * inst_branch(), returns true if the instruction might branch
277 * branch_taken(), return the address the instruction might
279 * db_getreg_val(); return the value of a user register,
280 * as indicated in the hardware instruction
281 * encoding, e.g. 8 for r8
283 * next_instr_address(pc,bd) returns the address of the first
284 * instruction following the one at "pc",
285 * which is either in the taken path of
286 * the branch (bd==1) or not. This is
287 * for machines (mips) with branch delays.
289 * A single-step may involve at most 2 breakpoints -
290 * one for branch-not-taken and one for branch taken.
291 * If one of these addresses does not already have a breakpoint,
292 * we allocate a breakpoint and save it here.
293 * These breakpoints are deleted on return.
297 db_set_single_step(void)
299 db_addr_t pc = PC_REGS(), brpc;
303 * User was stopped at pc, e.g. the instruction
304 * at pc was not executed.
306 inst = db_get_value(pc, sizeof(int), false);
307 if (inst_branch(inst) || inst_call(inst) || inst_return(inst)) {
308 brpc = branch_taken(inst, pc);
309 if (brpc != pc) { /* self-branches are hopeless */
310 db_taken_bkpt = db_set_temp_breakpoint(brpc);
312 pc = next_instr_address(pc, 1);
314 pc = next_instr_address(pc, 0);
315 db_not_taken_bkpt = db_set_temp_breakpoint(pc);
319 db_clear_single_step(void)
322 if (db_not_taken_bkpt != 0) {
323 db_delete_temp_breakpoint(db_not_taken_bkpt);
324 db_not_taken_bkpt = 0;
326 if (db_taken_bkpt != 0) {
327 db_delete_temp_breakpoint(db_taken_bkpt);
332 #endif /* SOFTWARE_SSTEP */
334 extern int db_cmd_loop_done;
339 db_single_step_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
349 db_run_mode = STEP_ONCE;
350 db_loop_count = count;
351 db_sstep_multiple = (count != 1);
352 db_sstep_print = print;
357 db_cmd_loop_done = 1;
360 /* trace and print until call/return */
363 db_trace_until_call_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
371 db_run_mode = STEP_CALLT;
372 db_sstep_print = print;
377 db_cmd_loop_done = 1;
382 db_trace_until_matching_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
390 db_run_mode = STEP_RETURN;
392 db_sstep_print = print;
397 db_cmd_loop_done = 1;
403 db_continue_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
406 db_run_mode = STEP_COUNT;
408 db_run_mode = STEP_CONTINUE;
413 db_cmd_loop_done = 1;