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>
51 static int db_run_mode;
56 #define STEP_CONTINUE 4
57 #define STEP_INVISIBLE 5
60 static boolean_t db_sstep_print;
61 static int db_loop_count;
62 static int db_call_depth;
68 #ifndef db_set_single_step
69 void db_set_single_step(void);
71 #ifndef db_clear_single_step
72 void db_clear_single_step(void);
76 db_breakpoint_t db_not_taken_bkpt = 0;
77 db_breakpoint_t db_taken_bkpt = 0;
81 db_stop_at_pc(is_breakpoint)
82 boolean_t *is_breakpoint;
84 register db_addr_t pc;
85 register db_breakpoint_t bkpt;
89 if ((db_not_taken_bkpt != 0 && pc == db_not_taken_bkpt->address)
90 || (db_taken_bkpt != 0 && pc == db_taken_bkpt->address))
91 *is_breakpoint = FALSE;
94 db_clear_single_step();
95 db_clear_breakpoints();
96 db_clear_watchpoints();
98 #ifdef FIXUP_PC_AFTER_BREAK
101 * Breakpoint trap. Fix up the PC if the
102 * machine requires it.
110 * Now check for a breakpoint at this address.
112 bkpt = db_find_breakpoint_here(pc);
114 if (--bkpt->count == 0) {
115 bkpt->count = bkpt->init_count;
116 *is_breakpoint = TRUE;
117 return (TRUE); /* stop here */
119 } else if (*is_breakpoint) {
125 *is_breakpoint = FALSE;
127 if (db_run_mode == STEP_INVISIBLE) {
128 db_run_mode = STEP_CONTINUE;
129 return (FALSE); /* continue */
131 if (db_run_mode == STEP_COUNT) {
132 return (FALSE); /* continue */
134 if (db_run_mode == STEP_ONCE) {
135 if (--db_loop_count > 0) {
136 if (db_sstep_print) {
138 db_print_loc_and_inst(pc);
141 return (FALSE); /* continue */
144 if (db_run_mode == STEP_RETURN) {
145 /* continue until matching return */
148 ins = db_get_value(pc, sizeof(int), FALSE);
149 if (!inst_trap_return(ins) &&
150 (!inst_return(ins) || --db_call_depth != 0)) {
151 if (db_sstep_print) {
152 if (inst_call(ins) || inst_return(ins)) {
155 db_printf("[after %6d] ", db_inst_count);
156 for (i = db_call_depth; --i > 0; )
158 db_print_loc_and_inst(pc);
164 return (FALSE); /* continue */
167 if (db_run_mode == STEP_CALLT) {
168 /* continue until call or return */
171 ins = db_get_value(pc, sizeof(int), FALSE);
172 if (!inst_call(ins) &&
174 !inst_trap_return(ins)) {
175 return (FALSE); /* continue */
178 db_run_mode = STEP_NONE;
183 db_restart_at_pc(watchpt)
186 register db_addr_t pc = PC_REGS();
188 if ((db_run_mode == STEP_COUNT) ||
189 (db_run_mode == STEP_RETURN) ||
190 (db_run_mode == STEP_CALLT)) {
194 * We are about to execute this instruction,
198 ins = db_get_value(pc, sizeof(int), FALSE);
200 db_load_count += inst_load(ins);
201 db_store_count += inst_store(ins);
202 #ifdef SOFTWARE_SSTEP
203 /* XXX works on mips, but... */
204 if (inst_branch(ins) || inst_call(ins)) {
205 ins = db_get_value(next_instr_address(pc,1),
208 db_load_count += inst_load(ins);
209 db_store_count += inst_store(ins);
211 #endif /* SOFTWARE_SSTEP */
214 if (db_run_mode == STEP_CONTINUE) {
215 if (watchpt || db_find_breakpoint_here(pc)) {
217 * Step over breakpoint/watchpoint.
219 db_run_mode = STEP_INVISIBLE;
220 db_set_single_step();
222 db_set_breakpoints();
223 db_set_watchpoints();
226 db_set_single_step();
230 #ifdef SOFTWARE_SSTEP
232 * Software implementation of single-stepping.
233 * If your machine does not have a trace mode
234 * similar to the vax or sun ones you can use
235 * this implementation, done for the mips.
236 * Just define the above conditional and provide
237 * the functions/macros defined below.
240 * inst_branch(), returns true if the instruction might branch
242 * branch_taken(), return the address the instruction might
244 * db_getreg_val(); return the value of a user register,
245 * as indicated in the hardware instruction
246 * encoding, e.g. 8 for r8
248 * next_instr_address(pc,bd) returns the address of the first
249 * instruction following the one at "pc",
250 * which is either in the taken path of
251 * the branch (bd==1) or not. This is
252 * for machines (mips) with branch delays.
254 * A single-step may involve at most 2 breakpoints -
255 * one for branch-not-taken and one for branch taken.
256 * If one of these addresses does not already have a breakpoint,
257 * we allocate a breakpoint and save it here.
258 * These breakpoints are deleted on return.
262 db_set_single_step(void)
264 db_addr_t pc = PC_REGS(), brpc;
268 * User was stopped at pc, e.g. the instruction
269 * at pc was not executed.
271 inst = db_get_value(pc, sizeof(int), FALSE);
272 if (inst_branch(inst) || inst_call(inst) || inst_return(inst)) {
273 brpc = branch_taken(inst, pc);
274 if (brpc != pc) { /* self-branches are hopeless */
275 db_taken_bkpt = db_set_temp_breakpoint(brpc);
277 pc = next_instr_address(pc, 1);
279 pc = next_instr_address(pc, 0);
280 db_not_taken_bkpt = db_set_temp_breakpoint(pc);
284 db_clear_single_step(void)
287 if (db_not_taken_bkpt != 0) {
288 db_delete_temp_breakpoint(db_not_taken_bkpt);
289 db_not_taken_bkpt = 0;
291 if (db_taken_bkpt != 0) {
292 db_delete_temp_breakpoint(db_taken_bkpt);
297 #endif /* SOFTWARE_SSTEP */
299 extern int db_cmd_loop_done;
304 db_single_step_cmd(addr, have_addr, count, modif)
310 boolean_t print = FALSE;
318 db_run_mode = STEP_ONCE;
319 db_loop_count = count;
320 db_sstep_print = print;
325 db_cmd_loop_done = 1;
328 /* trace and print until call/return */
331 db_trace_until_call_cmd(addr, have_addr, count, modif)
337 boolean_t print = FALSE;
342 db_run_mode = STEP_CALLT;
343 db_sstep_print = print;
348 db_cmd_loop_done = 1;
353 db_trace_until_matching_cmd(addr, have_addr, count, modif)
359 boolean_t print = FALSE;
364 db_run_mode = STEP_RETURN;
366 db_sstep_print = print;
371 db_cmd_loop_done = 1;
377 db_continue_cmd(addr, have_addr, count, modif)
384 db_run_mode = STEP_COUNT;
386 db_run_mode = STEP_CONTINUE;
391 db_cmd_loop_done = 1;