2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 * Author: David B. Golub, Carnegie Mellon University
58 * Commands to run process.
60 #include <mach/boolean.h>
61 #include <machine/db_machdep.h>
63 #include <ddb/db_lex.h>
64 #include <ddb/db_break.h>
65 #include <ddb/db_access.h>
66 #include <ddb/db_run.h>
67 #include <ddb/db_cond.h>
68 #include <ddb/db_examine.h>
69 #include <ddb/db_output.h> /* For db_printf() */
70 #include <ddb/db_watch.h>
71 #include <kern/misc_protos.h>
72 #include <kern/debug.h>
74 boolean_t db_sstep_print
;
79 int db_last_inst_count
;
82 int db_max_inst_count
= 1000;
84 #ifndef db_set_single_step
85 void db_set_task_single_step(
86 register db_regs_t
*regs
,
89 #define db_set_task_single_step(regs,task) db_set_single_step(regs)
91 #ifndef db_clear_single_step
92 void db_clear_task_single_step(
96 #define db_clear_task_single_step(regs,task) db_clear_single_step(regs)
99 extern jmp_buf_t
*db_recover
;
100 boolean_t
db_step_again(void);
104 boolean_t
*is_breakpoint
,
108 register db_addr_t pc
;
109 register db_thread_breakpoint_t bkpt
;
111 db_clear_task_single_step(DDB_REGS
, space
);
112 db_clear_breakpoints();
113 db_clear_watchpoints();
114 pc
= PC_REGS(DDB_REGS
);
116 #ifdef FIXUP_PC_AFTER_BREAK
117 if (*is_breakpoint
) {
119 * Breakpoint trap. Fix up the PC if the
120 * machine requires it.
123 pc
= PC_REGS(DDB_REGS
);
128 * Now check for a breakpoint at this address.
130 bkpt
= db_find_thread_breakpoint_here(space
, pc
);
132 if (db_cond_check(bkpt
)) {
133 *is_breakpoint
= TRUE
;
134 return (TRUE
); /* stop here */
137 *is_breakpoint
= FALSE
;
139 if (db_run_mode
== STEP_INVISIBLE
) {
140 db_run_mode
= STEP_CONTINUE
;
141 return (FALSE
); /* continue */
143 if (db_run_mode
== STEP_COUNT
) {
144 return (FALSE
); /* continue */
146 if (db_run_mode
== STEP_ONCE
) {
147 if (--db_loop_count
> 0) {
148 if (db_sstep_print
) {
149 db_print_loc_and_inst(pc
, task
);
151 return (FALSE
); /* continue */
154 if (db_run_mode
== STEP_RETURN
) {
157 /* WARNING: the following assumes an instruction fits an int */
158 db_expr_t ins
= db_get_task_value(pc
, sizeof(int), FALSE
, space
);
160 /* continue until matching return */
163 if (_setjmp(db_recover
= &db_jmpbuf
) == 0) {
164 if (!inst_trap_return(ins
) &&
165 (!inst_return(ins
) || --db_call_depth
!= 0)) {
166 if (db_sstep_print
) {
167 if (inst_call(ins
) || inst_return(ins
)) {
170 db_printf("[after %6d /%4d] ",
172 db_inst_count
- db_last_inst_count
);
173 db_last_inst_count
= db_inst_count
;
174 for (i
= db_call_depth
; --i
> 0; )
176 db_print_loc_and_inst(pc
, task
);
184 return (FALSE
); /* continue */
189 if (db_run_mode
== STEP_CALLT
) {
190 /* WARNING: the following assumes an instruction fits an int */
191 db_expr_t ins
= db_get_task_value(pc
, sizeof(int), FALSE
, space
);
193 /* continue until call or return */
195 if (!inst_call(ins
) &&
197 !inst_trap_return(ins
)) {
199 return (FALSE
); /* continue */
202 if (db_find_breakpoint_here(space
, pc
))
204 db_run_mode
= STEP_NONE
;
213 register db_addr_t pc
= PC_REGS(DDB_REGS
), brpc
;
215 if ((db_run_mode
== STEP_COUNT
) ||
216 (db_run_mode
== STEP_RETURN
) ||
217 (db_run_mode
== STEP_CALLT
)) {
221 * We are about to execute this instruction,
225 ins
= db_get_task_value(pc
, sizeof(int), FALSE
, task
);
227 db_load_count
+= db_inst_load(ins
);
228 db_store_count
+= db_inst_store(ins
);
229 #ifdef SOFTWARE_SSTEP
230 /* Account for instructions in delay slots */
231 brpc
= next_instr_address(pc
,1,task
);
232 if ((brpc
!= pc
) && (inst_branch(ins
) || inst_call(ins
))) {
233 /* Note: this ~assumes an instruction <= sizeof(int) */
234 ins
= db_get_task_value(brpc
, sizeof(int), FALSE
, task
);
236 db_load_count
+= db_inst_load(ins
);
237 db_store_count
+= db_inst_store(ins
);
239 #endif /* SOFTWARE_SSTEP */
242 if (db_run_mode
== STEP_CONTINUE
) {
243 if (watchpt
|| db_find_breakpoint_here(task
, pc
)) {
245 * Step over breakpoint/watchpoint.
247 db_run_mode
= STEP_INVISIBLE
;
248 db_set_task_single_step(DDB_REGS
, task
);
250 db_set_breakpoints();
251 db_set_watchpoints();
254 db_set_task_single_step(DDB_REGS
, task
);
259 * 'n' and 'u' commands might never return.
260 * Limit the maximum number of steps.
266 if (db_inst_count
&& !(db_inst_count%db_max_inst_count
)) {
268 db_printf("%d instructions, continue ? (y/n) ",
283 if (db_run_mode
== STEP_CONTINUE
) {
284 db_run_mode
= STEP_INVISIBLE
;
285 db_set_task_single_step(regs
, task
);
289 #ifdef SOFTWARE_SSTEP
291 * Software implementation of single-stepping.
292 * If your machine does not have a trace mode
293 * similar to the vax or sun ones you can use
294 * this implementation, done for the mips.
295 * Just define the above conditional and provide
296 * the functions/macros defined below.
299 * inst_branch(), returns true if the instruction might branch
301 * branch_taken(), return the address the instruction might
303 * db_getreg_val(); return the value of a user register,
304 * as indicated in the hardware instruction
305 * encoding, e.g. 8 for r8
307 * next_instr_address(pc,bd,task) returns the address of the first
308 * instruction following the one at "pc",
309 * which is either in the taken path of
310 * the branch (bd==1) or not. This is
311 * for machines (mips) with branch delays.
313 * A single-step may involve at most 2 breakpoints -
314 * one for branch-not-taken and one for branch taken.
315 * If one of these addresses does not already have a breakpoint,
316 * we allocate a breakpoint and save it here.
317 * These breakpoints are deleted on return.
319 db_breakpoint_t db_not_taken_bkpt
= 0;
320 db_breakpoint_t db_taken_bkpt
= 0;
323 db_find_temp_breakpoint(
327 if (db_taken_bkpt
&& (db_taken_bkpt
->address
== addr
) &&
328 db_taken_bkpt
->task
== task
)
329 return db_taken_bkpt
;
330 if (db_not_taken_bkpt
&& (db_not_taken_bkpt
->address
== addr
) &&
331 db_not_taken_bkpt
->task
== task
)
332 return db_not_taken_bkpt
;
337 db_set_task_single_step(
338 register db_regs_t
*regs
,
341 db_addr_t pc
= PC_REGS(regs
), brpc
;
342 register unsigned int inst
;
343 register boolean_t unconditional
;
346 * User was stopped at pc, e.g. the instruction
347 * at pc was not executed.
349 inst
= db_get_task_value(pc
, sizeof(int), FALSE
, task
);
350 if (inst_branch(inst
) || inst_call(inst
)) {
351 extern db_expr_t
getreg_val(); /* XXX -- need prototype! */
353 brpc
= branch_taken(inst
, pc
, getreg_val
, (unsigned char*)regs
);
354 if (brpc
!= pc
) { /* self-branches are hopeless */
355 db_taken_bkpt
= db_set_temp_breakpoint(task
, brpc
);
358 pc
= next_instr_address(pc
,1,task
);
360 pc
= next_instr_address(pc
,0,task
);
363 * check if this control flow instruction is an
364 * unconditional transfer
367 unconditional
= inst_unconditional_flow_transfer(inst
);
370 We only set the sequential breakpoint if previous instruction was not
371 an unconditional change of flow of control. If the previous instruction
372 is an unconditional change of flow of control, setting a breakpoint in the
373 next sequential location may set a breakpoint in data or in another routine,
374 which could screw up either the program or the debugger.
375 (Consider, for instance, that the next sequential instruction is the
376 start of a routine needed by the debugger.)
378 if (!unconditional
&& db_find_breakpoint_here(task
, pc
) == 0 &&
379 (db_taken_bkpt
== 0 || db_taken_bkpt
->address
!= pc
)) {
380 db_not_taken_bkpt
= db_set_temp_breakpoint(task
, pc
);
382 db_not_taken_bkpt
= 0;
386 db_clear_task_single_step(
390 if (db_taken_bkpt
!= 0) {
391 db_delete_temp_breakpoint(task
, db_taken_bkpt
);
394 if (db_not_taken_bkpt
!= 0) {
395 db_delete_temp_breakpoint(task
, db_not_taken_bkpt
);
396 db_not_taken_bkpt
= 0;
400 #endif /* SOFTWARE_SSTEP */
402 extern int db_cmd_loop_done
;
412 boolean_t print
= FALSE
;
420 db_run_mode
= STEP_ONCE
;
421 db_loop_count
= count
;
422 db_sstep_print
= print
;
424 db_last_inst_count
= 0;
428 db_cmd_loop_done
= 1;
431 /* trace and print until call/return */
433 db_trace_until_call_cmd(
439 boolean_t print
= FALSE
;
444 db_run_mode
= STEP_CALLT
;
445 db_sstep_print
= print
;
447 db_last_inst_count
= 0;
451 db_cmd_loop_done
= 1;
455 db_trace_until_matching_cmd(
461 boolean_t print
= FALSE
;
466 db_run_mode
= STEP_RETURN
;
468 db_sstep_print
= print
;
470 db_last_inst_count
= 0;
474 db_cmd_loop_done
= 1;
486 * Though "cont/c" works fairly well, it's not really robust
487 * enough to use in arbitrary situations, so disable it.
488 * (Doesn't seem cost-effective to debug and fix what ails
493 db_run_mode
= STEP_COUNT
;
495 db_run_mode
= STEP_CONTINUE
;
497 db_run_mode
= STEP_CONTINUE
;
500 db_last_inst_count
= 0;
504 db_cmd_loop_done
= 1;
518 db_run_mode
= STEP_CONTINUE
;
520 db_last_inst_count
= 0;
524 db_cmd_loop_done
= 1;
530 db_in_single_step(void)
532 return(db_run_mode
!= STEP_NONE
&& db_run_mode
!= STEP_CONTINUE
);