2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
27 * Author: David B. Golub, Carnegie Mellon University
32 * Commands to run process.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
42 #include <machine/kdb.h>
43 #include <machine/pcb.h>
48 #include <ddb/db_break.h>
49 #include <ddb/db_access.h>
54 #define STEP_CONTINUE 4
55 #define STEP_INVISIBLE 5
57 static int db_run_mode = STEP_CONTINUE;
59 static bool db_sstep_multiple;
60 static bool db_sstep_print;
61 static int db_loop_count;
62 static int db_call_depth;
69 db_breakpoint_t db_not_taken_bkpt = 0;
70 db_breakpoint_t db_taken_bkpt = 0;
73 #ifndef db_set_single_step
74 void db_set_single_step(void);
76 #ifndef db_clear_single_step
77 void db_clear_single_step(void);
79 #ifndef db_pc_is_singlestep
81 db_pc_is_singlestep(db_addr_t pc)
84 if ((db_not_taken_bkpt != 0 && pc == db_not_taken_bkpt->address)
85 || (db_taken_bkpt != 0 && pc == db_taken_bkpt->address))
93 db_stop_at_pc(int type, int code, bool *is_breakpoint, bool *is_watchpoint)
98 *is_breakpoint = IS_BREAKPOINT_TRAP(type, code);
99 *is_watchpoint = IS_WATCHPOINT_TRAP(type, code);
101 if (db_pc_is_singlestep(pc))
102 *is_breakpoint = false;
104 db_clear_single_step();
105 db_clear_breakpoints();
106 db_clear_watchpoints();
108 #ifdef FIXUP_PC_AFTER_BREAK
109 if (*is_breakpoint) {
111 * Breakpoint trap. Fix up the PC if the
112 * machine requires it.
120 * Now check for a breakpoint at this address.
122 bkpt = db_find_breakpoint_here(pc);
124 if (--bkpt->count == 0) {
125 bkpt->count = bkpt->init_count;
126 *is_breakpoint = true;
127 return (true); /* stop here */
129 return (false); /* continue the countdown */
130 } else if (*is_breakpoint) {
136 *is_breakpoint = false; /* might be a breakpoint, but not ours */
139 * If not stepping, then silently ignore single-step traps
140 * (except for clearing the single-step-flag above).
142 * If stepping, then abort if the trap type is unexpected.
143 * Breakpoints owned by us are expected and were handled above.
144 * Single-steps are expected and are handled below. All others
147 * Only do either of these if the MD layer claims to classify
148 * single-step traps unambiguously (by defining IS_SSTEP_TRAP).
149 * Otherwise, fall through to the bad historical behaviour
150 * given by turning unexpected traps into expected traps: if not
151 * stepping, then expect only breakpoints and stop, and if
152 * stepping, then expect only single-steps and step.
155 if (db_run_mode == STEP_CONTINUE && IS_SSTEP_TRAP(type, code))
157 if (db_run_mode != STEP_CONTINUE && !IS_SSTEP_TRAP(type, code)) {
158 printf("Stepping aborted\n");
163 if (db_run_mode == STEP_INVISIBLE) {
164 db_run_mode = STEP_CONTINUE;
165 return (false); /* continue */
167 if (db_run_mode == STEP_COUNT) {
168 return (false); /* continue */
170 if (db_run_mode == STEP_ONCE) {
171 if (--db_loop_count > 0) {
172 if (db_sstep_print) {
174 db_print_loc_and_inst(pc);
177 return (false); /* continue */
180 if (db_run_mode == STEP_RETURN) {
181 /* continue until matching return */
184 ins = db_get_value(pc, sizeof(int), false);
185 if (!inst_trap_return(ins) &&
186 (!inst_return(ins) || --db_call_depth != 0)) {
187 if (db_sstep_print) {
188 if (inst_call(ins) || inst_return(ins)) {
191 db_printf("[after %6d] ", db_inst_count);
192 for (i = db_call_depth; --i > 0; )
194 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;