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.
26 * $FreeBSD: src/sys/ddb/db_run.c,v 1.18 1999/08/28 00:41:10 peter Exp $
27 * $DragonFly: src/sys/ddb/db_run.c,v 1.6 2003/11/15 21:05:40 dillon Exp $
31 * Author: David B. Golub, Carnegie Mellon University
36 * Commands to run process.
38 #include <sys/param.h>
43 #include <ddb/db_break.h>
44 #include <ddb/db_access.h>
46 static int db_run_mode;
51 #define STEP_CONTINUE 4
52 #define STEP_INVISIBLE 5
55 static boolean_t db_sstep_print;
56 static int db_loop_count;
57 static int db_call_depth;
63 #ifndef db_set_single_step
64 extern void db_set_single_step (db_regs_t *regs);
66 #ifndef db_clear_single_step
67 extern void db_clear_single_step (db_regs_t *regs);
71 static void db_single_step (db_regs_t *regs);
75 db_stop_at_pc(is_breakpoint)
76 boolean_t *is_breakpoint;
81 db_clear_single_step(DDB_REGS);
82 db_clear_breakpoints();
83 db_clear_watchpoints();
84 pc = PC_REGS(DDB_REGS);
86 #ifdef FIXUP_PC_AFTER_BREAK
89 * Breakpoint trap. Fix up the PC if the
90 * machine requires it.
93 pc = PC_REGS(DDB_REGS);
98 * Now check for a breakpoint at this address.
100 bkpt = db_find_breakpoint_here(pc);
102 if (--bkpt->count == 0) {
103 bkpt->count = bkpt->init_count;
104 *is_breakpoint = TRUE;
105 return (TRUE); /* stop here */
107 } else if (*is_breakpoint) {
108 #ifdef __i386__ /* XXx */
109 ddb_regs.tf_eip += 1;
113 *is_breakpoint = FALSE;
115 if (db_run_mode == STEP_INVISIBLE) {
116 db_run_mode = STEP_CONTINUE;
117 return (FALSE); /* continue */
119 if (db_run_mode == STEP_COUNT) {
120 return (FALSE); /* continue */
122 if (db_run_mode == STEP_ONCE) {
123 if (--db_loop_count > 0) {
124 if (db_sstep_print) {
126 db_print_loc_and_inst(pc, DDB_REGS);
129 return (FALSE); /* continue */
132 if (db_run_mode == STEP_RETURN) {
133 db_expr_t ins = db_get_value(pc, sizeof(int), FALSE);
135 /* continue until matching return */
137 if (!inst_trap_return(ins) &&
138 (!inst_return(ins) || --db_call_depth != 0)) {
139 if (db_sstep_print) {
140 if (inst_call(ins) || inst_return(ins)) {
143 db_printf("[after %6d] ", db_inst_count);
144 for (i = db_call_depth; --i > 0; )
146 db_print_loc_and_inst(pc, DDB_REGS);
152 return (FALSE); /* continue */
155 if (db_run_mode == STEP_CALLT) {
156 db_expr_t ins = db_get_value(pc, sizeof(int), FALSE);
158 /* continue until call or return */
160 if (!inst_call(ins) &&
162 !inst_trap_return(ins)) {
163 return (FALSE); /* continue */
166 db_run_mode = STEP_NONE;
171 db_restart_at_pc(watchpt)
174 db_addr_t pc = PC_REGS(DDB_REGS);
176 if ((db_run_mode == STEP_COUNT) ||
177 (db_run_mode == STEP_RETURN) ||
178 (db_run_mode == STEP_CALLT)) {
182 * We are about to execute this instruction,
186 ins = db_get_value(pc, sizeof(int), FALSE);
188 db_load_count += inst_load(ins);
189 db_store_count += inst_store(ins);
190 #ifdef SOFTWARE_SSTEP
191 /* XXX works on mips, but... */
192 if (inst_branch(ins) || inst_call(ins)) {
193 ins = db_get_value(next_instr_address(pc,1),
196 db_load_count += inst_load(ins);
197 db_store_count += inst_store(ins);
199 #endif /* SOFTWARE_SSTEP */
202 if (db_run_mode == STEP_CONTINUE) {
203 if (watchpt || db_find_breakpoint_here(pc)) {
205 * Step over breakpoint/watchpoint.
207 db_run_mode = STEP_INVISIBLE;
208 db_set_single_step(DDB_REGS);
210 db_set_breakpoints();
211 db_set_watchpoints();
214 db_set_single_step(DDB_REGS);
223 if (db_run_mode == STEP_CONTINUE) {
224 db_run_mode = STEP_INVISIBLE;
225 db_set_single_step(regs);
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.
260 db_breakpoint_t db_not_taken_bkpt = 0;
261 db_breakpoint_t db_taken_bkpt = 0;
264 db_set_single_step(regs)
267 db_addr_t pc = PC_REGS(regs), brpc;
271 * User was stopped at pc, e.g. the instruction
272 * at pc was not executed.
274 inst = db_get_value(pc, sizeof(int), FALSE);
275 if (inst_branch(inst) || inst_call(inst)) {
276 brpc = branch_taken(inst, pc, regs);
277 if (brpc != pc) { /* self-branches are hopeless */
278 db_taken_bkpt = db_set_temp_breakpoint(brpc);
280 pc = next_instr_address(pc,1);
282 pc = next_instr_address(pc,0);
283 db_not_taken_bkpt = db_set_temp_breakpoint(pc);
287 db_clear_single_step(regs)
291 if (db_not_taken_bkpt != 0) {
292 db_delete_temp_breakpoint(db_not_taken_bkpt);
293 db_not_taken_bkpt = 0;
295 if (db_taken_bkpt != 0) {
296 db_delete_temp_breakpoint(db_taken_bkpt);
301 #endif /* SOFTWARE_SSTEP */
303 extern int db_cmd_loop_done;
308 db_single_step_cmd(addr, have_addr, count, modif)
314 boolean_t print = FALSE;
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(addr, have_addr, count, modif)
341 boolean_t print = FALSE;
346 db_run_mode = STEP_CALLT;
347 db_sstep_print = print;
352 db_cmd_loop_done = 1;
357 db_trace_until_matching_cmd(addr, have_addr, count, modif)
363 boolean_t print = FALSE;
368 db_run_mode = STEP_RETURN;
370 db_sstep_print = print;
375 db_cmd_loop_done = 1;
381 db_continue_cmd(addr, have_addr, count, modif)
388 db_run_mode = STEP_COUNT;
390 db_run_mode = STEP_CONTINUE;
395 db_cmd_loop_done = 1;