1 /* $NetBSD: db_run.c,v 1.30 2007/02/22 06:41:01 thorpej Exp $ */
4 * Mach Operating System
5 * Copyright (c) 1993-1990 Carnegie Mellon University
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
28 * Author: David B. Golub, Carnegie Mellon University
33 * Commands to run process.
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: db_run.c,v 1.30 2007/02/22 06:41:01 thorpej Exp $");
41 #include <sys/param.h>
44 #include <machine/db_machdep.h>
46 #include <ddb/db_run.h>
47 #include <ddb/db_access.h>
48 #include <ddb/db_break.h>
55 static void db_set_temp_breakpoint(db_breakpoint_t
, db_addr_t
);
56 static void db_delete_temp_breakpoint(db_breakpoint_t
);
57 static struct db_breakpoint db_not_taken_bkpt
;
58 static struct db_breakpoint db_taken_bkpt
;
62 #include <ddb/db_lex.h>
63 #include <ddb/db_watch.h>
64 #include <ddb/db_output.h>
65 #include <ddb/db_sym.h>
66 #include <ddb/db_extern.h>
68 static int db_run_mode
;
73 #define STEP_CONTINUE 4
74 #define STEP_INVISIBLE 5
77 static bool db_sstep_print
;
78 static int db_loop_count
;
79 static int db_call_depth
;
82 db_stop_at_pc(db_regs_t
*regs
, bool *is_breakpoint
)
89 #ifdef FIXUP_PC_AFTER_BREAK
92 * Breakpoint trap. Regardless if we treat this as a
93 * real breakpoint (e.g. software single-step), fix up the PC.
95 FIXUP_PC_AFTER_BREAK(regs
);
100 #ifdef SOFTWARE_SSTEP
102 * If we stopped at one of the single-step breakpoints, say it's not
103 * really a breakpoint so that we don't skip over the real instruction.
105 if (db_taken_bkpt
.address
== pc
|| db_not_taken_bkpt
.address
== pc
)
106 *is_breakpoint
= false;
107 #endif /* SOFTWARE_SSTEP */
109 db_clear_single_step(regs
);
110 db_clear_breakpoints();
111 db_clear_watchpoints();
114 * Now check for a breakpoint at this address.
116 bkpt
= db_find_breakpoint_here(pc
);
118 if (--bkpt
->count
== 0) {
119 bkpt
->count
= bkpt
->init_count
;
120 *is_breakpoint
= true;
121 return (true); /* stop here */
123 } else if (*is_breakpoint
) {
127 PC_REGS(regs
) += BKPT_SIZE
;
131 *is_breakpoint
= false;
133 if (db_run_mode
== STEP_INVISIBLE
) {
134 db_run_mode
= STEP_CONTINUE
;
135 return (false); /* continue */
137 if (db_run_mode
== STEP_COUNT
) {
138 return (false); /* continue */
140 if (db_run_mode
== STEP_ONCE
) {
141 if (--db_loop_count
> 0) {
142 if (db_sstep_print
) {
144 db_print_loc_and_inst(pc
);
147 return (false); /* continue */
150 if (db_run_mode
== STEP_RETURN
) {
151 db_expr_t ins
= db_get_value(pc
, sizeof(int), false);
153 /* continue until matching return */
155 if (!inst_trap_return(ins
) &&
156 (!inst_return(ins
) || --db_call_depth
!= 0)) {
157 if (db_sstep_print
) {
158 if (inst_call(ins
) || inst_return(ins
)) {
161 db_printf("[after %6d] ",
163 for (i
= db_call_depth
; --i
> 0; )
165 db_print_loc_and_inst(pc
);
171 return (false); /* continue */
174 if (db_run_mode
== STEP_CALLT
) {
175 db_expr_t ins
= db_get_value(pc
, sizeof(int), false);
177 /* continue until call or return */
179 if (!inst_call(ins
) &&
181 !inst_trap_return(ins
)) {
182 return (false); /* continue */
185 db_run_mode
= STEP_NONE
;
190 db_restart_at_pc(db_regs_t
*regs
, bool watchpt
)
192 db_addr_t pc
= PC_REGS(regs
);
193 #ifdef SOFTWARE_SSTEP
197 if ((db_run_mode
== STEP_COUNT
) ||
198 (db_run_mode
== STEP_RETURN
) ||
199 (db_run_mode
== STEP_CALLT
)) {
203 * We are about to execute this instruction,
206 ins
= db_get_value(pc
, sizeof(int), false);
208 db_load_count
+= inst_load(ins
);
209 db_store_count
+= inst_store(ins
);
211 #ifdef SOFTWARE_SSTEP
213 * Account for instructions in delay slots.
215 brpc
= next_instr_address(pc
, true);
217 (inst_branch(ins
) || inst_call(ins
) || inst_return(ins
))) {
218 ins
= db_get_value(brpc
, sizeof(int), false);
220 db_load_count
+= inst_load(ins
);
221 db_store_count
+= inst_store(ins
);
226 if (db_run_mode
== STEP_CONTINUE
) {
227 if (watchpt
|| db_find_breakpoint_here(pc
)) {
229 * Step over breakpoint/watchpoint.
231 db_run_mode
= STEP_INVISIBLE
;
232 db_set_single_step(regs
);
234 db_set_breakpoints();
235 db_set_watchpoints();
238 db_set_single_step(regs
);
243 db_single_step(db_regs_t
*regs
)
246 if (db_run_mode
== STEP_CONTINUE
) {
247 db_run_mode
= STEP_INVISIBLE
;
248 db_set_single_step(regs
);
255 db_single_step_cmd(db_expr_t addr
, bool have_addr
,
256 db_expr_t count
, const char *modif
)
266 db_run_mode
= STEP_ONCE
;
267 db_loop_count
= count
;
268 db_sstep_print
= print
;
273 db_cmd_loop_done
= true;
276 /* trace and print until call/return */
279 db_trace_until_call_cmd(db_expr_t addr
, bool have_addr
,
280 db_expr_t count
, const char *modif
)
287 db_run_mode
= STEP_CALLT
;
288 db_sstep_print
= print
;
293 db_cmd_loop_done
= true;
298 db_trace_until_matching_cmd(db_expr_t addr
, bool have_addr
,
299 db_expr_t count
, const char *modif
)
306 db_run_mode
= STEP_RETURN
;
308 db_sstep_print
= print
;
313 db_cmd_loop_done
= true;
319 db_continue_cmd(db_expr_t addr
, bool have_addr
,
320 db_expr_t count
, const char *modif
)
324 db_run_mode
= STEP_COUNT
;
326 db_run_mode
= STEP_CONTINUE
;
331 db_cmd_loop_done
= true;
335 #ifdef SOFTWARE_SSTEP
337 * Software implementation of single-stepping.
338 * If your machine does not have a trace mode
339 * similar to the vax or sun ones you can use
340 * this implementation, done for the mips.
341 * Just define the above conditional and provide
342 * the functions/macros defined below.
344 * bool inst_branch(int inst)
345 * bool inst_call(int inst)
346 * returns true if the instruction might branch
348 * bool inst_unconditional_flow_transfer(int inst)
349 * returns true if the instruction is an unconditional
350 * transter of flow (i.e. unconditional branch)
352 * db_addr_t branch_taken(int inst, db_addr_t pc, db_regs_t *regs)
353 * returns the target address of the branch
355 * db_addr_t next_instr_address(db_addr_t pc, bool bd)
356 * returns the address of the first instruction following the
357 * one at "pc", which is either in the taken path of the branch
358 * (bd == true) or not. This is for machines (e.g. mips) with
361 * A single-step may involve at most 2 breakpoints -
362 * one for branch-not-taken and one for branch taken.
363 * If one of these addresses does not already have a breakpoint,
364 * we allocate a breakpoint and save it here.
365 * These breakpoints are deleted on return.
369 /* XXX - don't check for existing breakpoints in KGDB-only case */
370 #define db_find_breakpoint_here(pc) (0)
374 db_set_single_step(db_regs_t
*regs
)
376 db_addr_t pc
= PC_REGS(regs
), brpc
= pc
;
381 * User was stopped at pc, e.g. the instruction
382 * at pc was not executed.
384 inst
= db_get_value(pc
, sizeof(int), false);
385 if (inst_branch(inst
) || inst_call(inst
) || inst_return(inst
)) {
386 brpc
= branch_taken(inst
, pc
, regs
);
387 if (brpc
!= pc
) { /* self-branches are hopeless */
388 db_set_temp_breakpoint(&db_taken_bkpt
, brpc
);
390 db_taken_bkpt
.address
= 0;
391 pc
= next_instr_address(pc
, true);
395 * Check if this control flow instruction is an
396 * unconditional transfer.
398 unconditional
= inst_unconditional_flow_transfer(inst
);
400 pc
= next_instr_address(pc
, false);
403 * We only set the sequential breakpoint if previous
404 * instruction was not an unconditional change of flow
405 * control. If the previous instruction is an
406 * unconditional change of flow control, setting a
407 * breakpoint in the next sequential location may set
408 * a breakpoint in data or in another routine, which
409 * could screw up in either the program or the debugger.
410 * (Consider, for instance, that the next sequential
411 * instruction is the start of a routine needed by the
414 * Also, don't set both the taken and not-taken breakpoints
415 * in the same place even if the MD code would otherwise
418 if (unconditional
== false &&
419 db_find_breakpoint_here(pc
) == 0 &&
421 db_set_temp_breakpoint(&db_not_taken_bkpt
, pc
);
423 db_not_taken_bkpt
.address
= 0;
427 db_clear_single_step(db_regs_t
*regs
)
430 if (db_taken_bkpt
.address
!= 0)
431 db_delete_temp_breakpoint(&db_taken_bkpt
);
433 if (db_not_taken_bkpt
.address
!= 0)
434 db_delete_temp_breakpoint(&db_not_taken_bkpt
);
438 db_set_temp_breakpoint(db_breakpoint_t bkpt
, db_addr_t addr
)
442 bkpt
->address
= addr
;
443 /* bkpt->flags = BKPT_TEMP; - this is not used */
444 bkpt
->init_count
= 1;
447 bkpt
->bkpt_inst
= db_get_value(bkpt
->address
, BKPT_SIZE
, false);
448 db_put_value(bkpt
->address
, BKPT_SIZE
,
449 BKPT_SET(bkpt
->bkpt_inst
, bkpt
->address
));
453 db_delete_temp_breakpoint(db_breakpoint_t bkpt
)
456 db_put_value(bkpt
->address
, BKPT_SIZE
, bkpt
->bkpt_inst
);
459 #endif /* SOFTWARE_SSTEP */