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;
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(bool *is_breakpoint)
100 if (db_pc_is_singlestep(pc))
101 *is_breakpoint = false;
103 db_clear_single_step();
104 db_clear_breakpoints();
105 db_clear_watchpoints();
107 #ifdef FIXUP_PC_AFTER_BREAK
108 if (*is_breakpoint) {
110 * Breakpoint trap. Fix up the PC if the
111 * machine requires it.
119 * Now check for a breakpoint at this address.
121 bkpt = db_find_breakpoint_here(pc);
123 if (--bkpt->count == 0) {
124 bkpt->count = bkpt->init_count;
125 *is_breakpoint = true;
126 return (true); /* stop here */
128 } else if (*is_breakpoint) {
134 *is_breakpoint = false;
136 if (db_run_mode == STEP_INVISIBLE) {
137 db_run_mode = STEP_CONTINUE;
138 return (false); /* continue */
140 if (db_run_mode == STEP_COUNT) {
141 return (false); /* continue */
143 if (db_run_mode == STEP_ONCE) {
144 if (--db_loop_count > 0) {
145 if (db_sstep_print) {
147 db_print_loc_and_inst(pc);
150 return (false); /* continue */
153 if (db_run_mode == STEP_RETURN) {
154 /* continue until matching return */
157 ins = db_get_value(pc, sizeof(int), false);
158 if (!inst_trap_return(ins) &&
159 (!inst_return(ins) || --db_call_depth != 0)) {
160 if (db_sstep_print) {
161 if (inst_call(ins) || inst_return(ins)) {
164 db_printf("[after %6d] ", db_inst_count);
165 for (i = db_call_depth; --i > 0; )
167 db_print_loc_and_inst(pc);
173 return (false); /* continue */
176 if (db_run_mode == STEP_CALLT) {
177 /* continue until call or return */
180 ins = db_get_value(pc, sizeof(int), false);
181 if (!inst_call(ins) &&
183 !inst_trap_return(ins)) {
184 return (false); /* continue */
187 db_run_mode = STEP_NONE;
192 db_restart_at_pc(bool watchpt)
194 db_addr_t pc = PC_REGS();
196 if ((db_run_mode == STEP_COUNT) ||
197 (db_run_mode == STEP_RETURN) ||
198 (db_run_mode == STEP_CALLT)) {
200 * We are about to execute this instruction,
203 #ifdef SOFTWARE_SSTEP
206 db_get_value(pc, sizeof(int), false);
208 db_load_count += inst_load(ins);
209 db_store_count += inst_store(ins);
210 #ifdef SOFTWARE_SSTEP
211 /* XXX works on mips, but... */
212 if (inst_branch(ins) || inst_call(ins)) {
213 ins = db_get_value(next_instr_address(pc,1),
216 db_load_count += inst_load(ins);
217 db_store_count += inst_store(ins);
219 #endif /* SOFTWARE_SSTEP */
222 if (db_run_mode == STEP_CONTINUE) {
223 if (watchpt || db_find_breakpoint_here(pc)) {
225 * Step over breakpoint/watchpoint.
227 db_run_mode = STEP_INVISIBLE;
228 db_set_single_step();
230 db_set_breakpoints();
231 db_set_watchpoints();
234 db_set_single_step();
238 #ifdef SOFTWARE_SSTEP
240 * Software implementation of single-stepping.
241 * If your machine does not have a trace mode
242 * similar to the vax or sun ones you can use
243 * this implementation, done for the mips.
244 * Just define the above conditional and provide
245 * the functions/macros defined below.
248 * inst_branch(), returns true if the instruction might branch
250 * branch_taken(), return the address the instruction might
252 * db_getreg_val(); return the value of a user register,
253 * as indicated in the hardware instruction
254 * encoding, e.g. 8 for r8
256 * next_instr_address(pc,bd) returns the address of the first
257 * instruction following the one at "pc",
258 * which is either in the taken path of
259 * the branch (bd==1) or not. This is
260 * for machines (mips) with branch delays.
262 * A single-step may involve at most 2 breakpoints -
263 * one for branch-not-taken and one for branch taken.
264 * If one of these addresses does not already have a breakpoint,
265 * we allocate a breakpoint and save it here.
266 * These breakpoints are deleted on return.
270 db_set_single_step(void)
272 db_addr_t pc = PC_REGS(), brpc;
276 * User was stopped at pc, e.g. the instruction
277 * at pc was not executed.
279 inst = db_get_value(pc, sizeof(int), false);
280 if (inst_branch(inst) || inst_call(inst) || inst_return(inst)) {
281 brpc = branch_taken(inst, pc);
282 if (brpc != pc) { /* self-branches are hopeless */
283 db_taken_bkpt = db_set_temp_breakpoint(brpc);
285 pc = next_instr_address(pc, 1);
287 pc = next_instr_address(pc, 0);
288 db_not_taken_bkpt = db_set_temp_breakpoint(pc);
292 db_clear_single_step(void)
295 if (db_not_taken_bkpt != 0) {
296 db_delete_temp_breakpoint(db_not_taken_bkpt);
297 db_not_taken_bkpt = 0;
299 if (db_taken_bkpt != 0) {
300 db_delete_temp_breakpoint(db_taken_bkpt);
305 #endif /* SOFTWARE_SSTEP */
307 extern int db_cmd_loop_done;
312 db_single_step_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
322 db_run_mode = STEP_ONCE;
323 db_loop_count = count;
324 db_sstep_print = print;
329 db_cmd_loop_done = 1;
332 /* trace and print until call/return */
335 db_trace_until_call_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
343 db_run_mode = STEP_CALLT;
344 db_sstep_print = print;
349 db_cmd_loop_done = 1;
354 db_trace_until_matching_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
362 db_run_mode = STEP_RETURN;
364 db_sstep_print = print;
369 db_cmd_loop_done = 1;
375 db_continue_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
378 db_run_mode = STEP_COUNT;
380 db_run_mode = STEP_CONTINUE;
385 db_cmd_loop_done = 1;