2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * Author: David B. Golub, Carnegie Mellon University
64 * Commands to run process.
66 #include <mach/boolean.h>
67 #include <machine/db_machdep.h>
69 #include <ddb/db_lex.h>
70 #include <ddb/db_break.h>
71 #include <ddb/db_access.h>
72 #include <ddb/db_run.h>
73 #include <ddb/db_cond.h>
74 #include <ddb/db_examine.h>
75 #include <ddb/db_output.h> /* For db_printf() */
76 #include <ddb/db_watch.h>
77 #include <kern/misc_protos.h>
78 #include <kern/debug.h>
80 #include <IOKit/IOPlatformExpert.h>
82 boolean_t db_sstep_print
;
87 int db_last_inst_count
;
90 int db_max_inst_count
= 1000;
92 #ifndef db_set_single_step
93 void db_set_task_single_step(
94 register db_regs_t
*regs
,
97 #define db_set_task_single_step(regs,task) db_set_single_step(regs)
99 #ifndef db_clear_single_step
100 void db_clear_task_single_step(
104 #define db_clear_task_single_step(regs,task) db_clear_single_step(regs)
107 extern jmp_buf_t
*db_recover
;
108 boolean_t
db_step_again(void);
110 static db_addr_t db_stop_pc
;
113 boolean_t
*is_breakpoint
,
117 register db_thread_breakpoint_t bkpt
;
119 db_clear_task_single_step(DDB_REGS
, space
);
120 db_clear_breakpoints();
121 db_clear_watchpoints();
122 db_stop_pc
= PC_REGS(DDB_REGS
);
124 #ifdef FIXUP_PC_AFTER_BREAK
125 if (*is_breakpoint
) {
127 * Breakpoint trap. Fix up the PC if the
128 * machine requires it.
131 db_stop_pc
= PC_REGS(DDB_REGS
);
136 * Now check for a breakpoint at this address.
138 bkpt
= db_find_thread_breakpoint_here(space
, db_stop_pc
);
140 if (db_cond_check(bkpt
)) {
141 *is_breakpoint
= TRUE
;
142 return (TRUE
); /* stop here */
145 *is_breakpoint
= FALSE
;
147 if (db_run_mode
== STEP_INVISIBLE
) {
148 db_run_mode
= STEP_CONTINUE
;
149 return (FALSE
); /* continue */
151 if (db_run_mode
== STEP_COUNT
) {
152 return (FALSE
); /* continue */
154 if (db_run_mode
== STEP_ONCE
) {
155 if (--db_loop_count
> 0) {
156 if (db_sstep_print
) {
157 db_print_loc_and_inst(db_stop_pc
, task
);
159 return (FALSE
); /* continue */
162 if (db_run_mode
== STEP_RETURN
) {
165 /* WARNING: the following assumes an instruction fits an int */
168 ins
= db_get_task_value(db_stop_pc
, sizeof(int), FALSE
, space
);
170 /* continue until matching return */
173 if (_setjmp(db_recover
= &db_jmpbuf
) == 0) {
174 if (!inst_trap_return(ins
) &&
175 (!inst_return(ins
) || --db_call_depth
!= 0)) {
176 if (db_sstep_print
) {
177 if (inst_call(ins
) || inst_return(ins
)) {
180 db_printf("[after %6d /%4d] ",
182 db_inst_count
- db_last_inst_count
);
183 db_last_inst_count
= db_inst_count
;
184 for (i
= db_call_depth
; --i
> 0; )
186 db_print_loc_and_inst(db_stop_pc
, task
);
194 return (FALSE
); /* continue */
199 if (db_run_mode
== STEP_CALLT
) {
200 /* WARNING: the following assumes an instruction fits an int */
202 ins
= db_get_task_value(db_stop_pc
, sizeof(int), FALSE
, space
);
204 /* continue until call or return */
206 if (!inst_call(ins
) &&
208 !inst_trap_return(ins
)) {
210 return (FALSE
); /* continue */
213 if (db_find_breakpoint_here(space
, db_stop_pc
))
215 db_run_mode
= STEP_NONE
;
224 db_addr_t pc
= PC_REGS(DDB_REGS
);
225 #ifdef SOFTWARE_SSTEP
230 if ((db_run_mode
== STEP_COUNT
) ||
231 (db_run_mode
== STEP_RETURN
) ||
232 (db_run_mode
== STEP_CALLT
)) {
236 * We are about to execute this instruction,
240 ins
= db_get_task_value(pc
, sizeof(int), FALSE
, task
);
242 db_load_count
+= db_inst_load((unsigned long)ins
);
243 db_store_count
+= db_inst_store((unsigned long)ins
);
244 #ifdef SOFTWARE_SSTEP
245 /* Account for instructions in delay slots */
246 brpc
= next_instr_address(pc
,1,task
);
247 if ((brpc
!= pc
) && (inst_branch(ins
) || inst_call(ins
))) {
248 /* Note: this ~assumes an instruction <= sizeof(int) */
249 ins
= db_get_task_value(brpc
, sizeof(int), FALSE
, task
);
251 db_load_count
+= db_inst_load(ins
);
252 db_store_count
+= db_inst_store(ins
);
254 #endif /* SOFTWARE_SSTEP */
257 if (db_run_mode
== STEP_CONTINUE
) {
258 if (watchpt
|| db_find_breakpoint_here(task
, pc
)) {
260 * Step over breakpoint/watchpoint.
262 db_run_mode
= STEP_INVISIBLE
;
263 db_set_task_single_step(DDB_REGS
, task
);
265 db_set_breakpoints();
266 db_set_watchpoints();
269 db_set_task_single_step(DDB_REGS
, task
);
274 * 'n' and 'u' commands might never return.
275 * Limit the maximum number of steps.
281 if (db_inst_count
&& !(db_inst_count%db_max_inst_count
)) {
283 db_printf("%d instructions, continue ? (y/n) ",
294 db_single_step(db_regs_t
*regs
, __unused task_t task
)
296 if (db_run_mode
== STEP_CONTINUE
) {
297 db_run_mode
= STEP_INVISIBLE
;
298 db_set_task_single_step(regs
, task
);
302 #ifdef SOFTWARE_SSTEP
304 * Software implementation of single-stepping.
305 * If your machine does not have a trace mode
306 * similar to the vax or sun ones you can use
307 * this implementation, done for the mips.
308 * Just define the above conditional and provide
309 * the functions/macros defined below.
312 * inst_branch(), returns true if the instruction might branch
314 * branch_taken(), return the address the instruction might
316 * db_getreg_val(); return the value of a user register,
317 * as indicated in the hardware instruction
318 * encoding, e.g. 8 for r8
320 * next_instr_address(pc,bd,task) returns the address of the first
321 * instruction following the one at "pc",
322 * which is either in the taken path of
323 * the branch (bd==1) or not. This is
324 * for machines (mips) with branch delays.
326 * A single-step may involve at most 2 breakpoints -
327 * one for branch-not-taken and one for branch taken.
328 * If one of these addresses does not already have a breakpoint,
329 * we allocate a breakpoint and save it here.
330 * These breakpoints are deleted on return.
332 db_breakpoint_t db_not_taken_bkpt
= 0;
333 db_breakpoint_t db_taken_bkpt
= 0;
336 db_find_temp_breakpoint(
340 if (db_taken_bkpt
&& (db_taken_bkpt
->address
== addr
) &&
341 db_taken_bkpt
->task
== task
)
342 return db_taken_bkpt
;
343 if (db_not_taken_bkpt
&& (db_not_taken_bkpt
->address
== addr
) &&
344 db_not_taken_bkpt
->task
== task
)
345 return db_not_taken_bkpt
;
350 db_set_task_single_step(
351 register db_regs_t
*regs
,
354 db_addr_t pc
= PC_REGS(regs
), brpc
;
355 register unsigned int inst
;
356 register boolean_t unconditional
;
359 * User was stopped at pc, e.g. the instruction
360 * at pc was not executed.
362 inst
= db_get_task_value(pc
, sizeof(int), FALSE
, task
);
363 if (inst_branch(inst
) || inst_call(inst
)) {
364 extern db_expr_t
getreg_val(); /* XXX -- need prototype! */
366 brpc
= branch_taken(inst
, pc
, getreg_val
, (unsigned char*)regs
);
367 if (brpc
!= pc
) { /* self-branches are hopeless */
368 db_taken_bkpt
= db_set_temp_breakpoint(task
, brpc
);
371 pc
= next_instr_address(pc
,1,task
);
373 pc
= next_instr_address(pc
,0,task
);
376 * check if this control flow instruction is an
377 * unconditional transfer
380 unconditional
= inst_unconditional_flow_transfer(inst
);
383 We only set the sequential breakpoint if previous instruction was not
384 an unconditional change of flow of control. If the previous instruction
385 is an unconditional change of flow of control, setting a breakpoint in the
386 next sequential location may set a breakpoint in data or in another routine,
387 which could screw up either the program or the debugger.
388 (Consider, for instance, that the next sequential instruction is the
389 start of a routine needed by the debugger.)
391 if (!unconditional
&& db_find_breakpoint_here(task
, pc
) == 0 &&
392 (db_taken_bkpt
== 0 || db_taken_bkpt
->address
!= pc
)) {
393 db_not_taken_bkpt
= db_set_temp_breakpoint(task
, pc
);
395 db_not_taken_bkpt
= 0;
399 db_clear_task_single_step(
403 if (db_taken_bkpt
!= 0) {
404 db_delete_temp_breakpoint(task
, db_taken_bkpt
);
407 if (db_not_taken_bkpt
!= 0) {
408 db_delete_temp_breakpoint(task
, db_not_taken_bkpt
);
409 db_not_taken_bkpt
= 0;
413 #endif /* SOFTWARE_SSTEP */
415 extern int db_cmd_loop_done
;
419 db_single_step_cmd(__unused db_expr_t addr
, __unused boolean_t have_addr
,
420 db_expr_t count
, char *modif
)
422 boolean_t print
= FALSE
;
424 if (count
== (db_expr_t
)-1)
430 db_run_mode
= STEP_ONCE
;
431 db_loop_count
= (typeof(db_loop_count
))count
;
432 db_sstep_print
= print
;
434 db_last_inst_count
= 0;
438 db_cmd_loop_done
= 1;
441 /* trace and print until call/return */
443 db_trace_until_call_cmd(__unused db_expr_t addr
, __unused boolean_t have_addr
,
444 __unused db_expr_t count
, char *modif
)
446 boolean_t print
= FALSE
;
451 db_run_mode
= STEP_CALLT
;
452 db_sstep_print
= print
;
454 db_last_inst_count
= 0;
458 db_cmd_loop_done
= 1;
462 db_trace_until_matching_cmd(__unused db_expr_t addr
,
463 __unused boolean_t have_addr
,
464 __unused db_expr_t count
,
467 boolean_t print
= FALSE
;
472 db_run_mode
= STEP_RETURN
;
474 db_sstep_print
= print
;
476 db_last_inst_count
= 0;
480 db_cmd_loop_done
= 1;
485 db_continue_cmd(__unused db_expr_t addr
, __unused boolean_t have_addr
,
486 __unused db_expr_t count
, __unused
char *modif
)
489 * Though "cont/c" works fairly well, it's not really robust
490 * enough to use in arbitrary situations, so disable it.
491 * (Doesn't seem cost-effective to debug and fix what ails
496 db_run_mode
= STEP_COUNT
;
498 db_run_mode
= STEP_CONTINUE
;
500 db_run_mode
= STEP_CONTINUE
;
503 db_last_inst_count
= 0;
507 db_cmd_loop_done
= 1;
522 db_continue_gdb(__unused db_expr_t addr
, __unused boolean_t have_addr
,
523 __unused db_expr_t count
, __unused
char *modif
)
526 db_run_mode
= STEP_CONTINUE
;
528 db_last_inst_count
= 0;
532 db_cmd_loop_done
= 1;
537 db_in_single_step(void)
539 return(db_run_mode
!= STEP_NONE
&& db_run_mode
!= STEP_CONTINUE
);