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 bool 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(bool *is_breakpoint)
88 if ((db_not_taken_bkpt != 0 && pc == db_not_taken_bkpt->address)
89 || (db_taken_bkpt != 0 && pc == db_taken_bkpt->address))
90 *is_breakpoint = false;
93 db_clear_single_step();
94 db_clear_breakpoints();
95 db_clear_watchpoints();
97 #ifdef FIXUP_PC_AFTER_BREAK
100 * Breakpoint trap. Fix up the PC if the
101 * machine requires it.
109 * Now check for a breakpoint at this address.
111 bkpt = db_find_breakpoint_here(pc);
113 if (--bkpt->count == 0) {
114 bkpt->count = bkpt->init_count;
115 *is_breakpoint = true;
116 return (true); /* stop here */
118 } else if (*is_breakpoint) {
124 *is_breakpoint = false;
126 if (db_run_mode == STEP_INVISIBLE) {
127 db_run_mode = STEP_CONTINUE;
128 return (false); /* continue */
130 if (db_run_mode == STEP_COUNT) {
131 return (false); /* continue */
133 if (db_run_mode == STEP_ONCE) {
134 if (--db_loop_count > 0) {
135 if (db_sstep_print) {
137 db_print_loc_and_inst(pc);
140 return (false); /* continue */
143 if (db_run_mode == STEP_RETURN) {
144 /* continue until matching return */
147 ins = db_get_value(pc, sizeof(int), false);
148 if (!inst_trap_return(ins) &&
149 (!inst_return(ins) || --db_call_depth != 0)) {
150 if (db_sstep_print) {
151 if (inst_call(ins) || inst_return(ins)) {
154 db_printf("[after %6d] ", db_inst_count);
155 for (i = db_call_depth; --i > 0; )
157 db_print_loc_and_inst(pc);
163 return (false); /* continue */
166 if (db_run_mode == STEP_CALLT) {
167 /* continue until call or return */
170 ins = db_get_value(pc, sizeof(int), false);
171 if (!inst_call(ins) &&
173 !inst_trap_return(ins)) {
174 return (false); /* continue */
177 db_run_mode = STEP_NONE;
182 db_restart_at_pc(bool watchpt)
184 db_addr_t pc = PC_REGS();
186 if ((db_run_mode == STEP_COUNT) ||
187 (db_run_mode == STEP_RETURN) ||
188 (db_run_mode == STEP_CALLT)) {
190 * We are about to execute this instruction,
193 #ifdef SOFTWARE_SSTEP
196 db_get_value(pc, sizeof(int), false);
198 db_load_count += inst_load(ins);
199 db_store_count += inst_store(ins);
200 #ifdef SOFTWARE_SSTEP
201 /* XXX works on mips, but... */
202 if (inst_branch(ins) || inst_call(ins)) {
203 ins = db_get_value(next_instr_address(pc,1),
206 db_load_count += inst_load(ins);
207 db_store_count += inst_store(ins);
209 #endif /* SOFTWARE_SSTEP */
212 if (db_run_mode == STEP_CONTINUE) {
213 if (watchpt || db_find_breakpoint_here(pc)) {
215 * Step over breakpoint/watchpoint.
217 db_run_mode = STEP_INVISIBLE;
218 db_set_single_step();
220 db_set_breakpoints();
221 db_set_watchpoints();
224 db_set_single_step();
228 #ifdef SOFTWARE_SSTEP
230 * Software implementation of single-stepping.
231 * If your machine does not have a trace mode
232 * similar to the vax or sun ones you can use
233 * this implementation, done for the mips.
234 * Just define the above conditional and provide
235 * the functions/macros defined below.
238 * inst_branch(), returns true if the instruction might branch
240 * branch_taken(), return the address the instruction might
242 * db_getreg_val(); return the value of a user register,
243 * as indicated in the hardware instruction
244 * encoding, e.g. 8 for r8
246 * next_instr_address(pc,bd) returns the address of the first
247 * instruction following the one at "pc",
248 * which is either in the taken path of
249 * the branch (bd==1) or not. This is
250 * for machines (mips) with branch delays.
252 * A single-step may involve at most 2 breakpoints -
253 * one for branch-not-taken and one for branch taken.
254 * If one of these addresses does not already have a breakpoint,
255 * we allocate a breakpoint and save it here.
256 * These breakpoints are deleted on return.
260 db_set_single_step(void)
262 db_addr_t pc = PC_REGS(), brpc;
266 * User was stopped at pc, e.g. the instruction
267 * at pc was not executed.
269 inst = db_get_value(pc, sizeof(int), false);
270 if (inst_branch(inst) || inst_call(inst) || inst_return(inst)) {
271 brpc = branch_taken(inst, pc);
272 if (brpc != pc) { /* self-branches are hopeless */
273 db_taken_bkpt = db_set_temp_breakpoint(brpc);
275 pc = next_instr_address(pc, 1);
277 pc = next_instr_address(pc, 0);
278 db_not_taken_bkpt = db_set_temp_breakpoint(pc);
282 db_clear_single_step(void)
285 if (db_not_taken_bkpt != 0) {
286 db_delete_temp_breakpoint(db_not_taken_bkpt);
287 db_not_taken_bkpt = 0;
289 if (db_taken_bkpt != 0) {
290 db_delete_temp_breakpoint(db_taken_bkpt);
295 #endif /* SOFTWARE_SSTEP */
297 extern int db_cmd_loop_done;
302 db_single_step_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
312 db_run_mode = STEP_ONCE;
313 db_loop_count = count;
314 db_sstep_print = print;
319 db_cmd_loop_done = 1;
322 /* trace and print until call/return */
325 db_trace_until_call_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
333 db_run_mode = STEP_CALLT;
334 db_sstep_print = print;
339 db_cmd_loop_done = 1;
344 db_trace_until_matching_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
352 db_run_mode = STEP_RETURN;
354 db_sstep_print = print;
359 db_cmd_loop_done = 1;
365 db_continue_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
368 db_run_mode = STEP_COUNT;
370 db_run_mode = STEP_CONTINUE;
375 db_cmd_loop_done = 1;