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 #include <IOKit/IOPlatformExpert.h>
76 boolean_t db_sstep_print
;
81 int db_last_inst_count
;
84 int db_max_inst_count
= 1000;
86 #ifndef db_set_single_step
87 void db_set_task_single_step(
88 register db_regs_t
*regs
,
91 #define db_set_task_single_step(regs,task) db_set_single_step(regs)
93 #ifndef db_clear_single_step
94 void db_clear_task_single_step(
98 #define db_clear_task_single_step(regs,task) db_clear_single_step(regs)
101 extern jmp_buf_t
*db_recover
;
102 boolean_t
db_step_again(void);
106 boolean_t
*is_breakpoint
,
110 register db_addr_t pc
;
111 register db_thread_breakpoint_t bkpt
;
113 db_clear_task_single_step(DDB_REGS
, space
);
114 db_clear_breakpoints();
115 db_clear_watchpoints();
116 pc
= PC_REGS(DDB_REGS
);
118 #ifdef FIXUP_PC_AFTER_BREAK
119 if (*is_breakpoint
) {
121 * Breakpoint trap. Fix up the PC if the
122 * machine requires it.
125 pc
= PC_REGS(DDB_REGS
);
130 * Now check for a breakpoint at this address.
132 bkpt
= db_find_thread_breakpoint_here(space
, pc
);
134 if (db_cond_check(bkpt
)) {
135 *is_breakpoint
= TRUE
;
136 return (TRUE
); /* stop here */
139 *is_breakpoint
= FALSE
;
141 if (db_run_mode
== STEP_INVISIBLE
) {
142 db_run_mode
= STEP_CONTINUE
;
143 return (FALSE
); /* continue */
145 if (db_run_mode
== STEP_COUNT
) {
146 return (FALSE
); /* continue */
148 if (db_run_mode
== STEP_ONCE
) {
149 if (--db_loop_count
> 0) {
150 if (db_sstep_print
) {
151 db_print_loc_and_inst(pc
, task
);
153 return (FALSE
); /* continue */
156 if (db_run_mode
== STEP_RETURN
) {
159 /* WARNING: the following assumes an instruction fits an int */
160 db_expr_t ins
= db_get_task_value(pc
, sizeof(int), FALSE
, space
);
162 /* continue until matching return */
165 if (_setjmp(db_recover
= &db_jmpbuf
) == 0) {
166 if (!inst_trap_return(ins
) &&
167 (!inst_return(ins
) || --db_call_depth
!= 0)) {
168 if (db_sstep_print
) {
169 if (inst_call(ins
) || inst_return(ins
)) {
172 db_printf("[after %6d /%4d] ",
174 db_inst_count
- db_last_inst_count
);
175 db_last_inst_count
= db_inst_count
;
176 for (i
= db_call_depth
; --i
> 0; )
178 db_print_loc_and_inst(pc
, task
);
186 return (FALSE
); /* continue */
191 if (db_run_mode
== STEP_CALLT
) {
192 /* WARNING: the following assumes an instruction fits an int */
193 db_expr_t ins
= db_get_task_value(pc
, sizeof(int), FALSE
, space
);
195 /* continue until call or return */
197 if (!inst_call(ins
) &&
199 !inst_trap_return(ins
)) {
201 return (FALSE
); /* continue */
204 if (db_find_breakpoint_here(space
, pc
))
206 db_run_mode
= STEP_NONE
;
215 register db_addr_t pc
= PC_REGS(DDB_REGS
), brpc
;
217 if ((db_run_mode
== STEP_COUNT
) ||
218 (db_run_mode
== STEP_RETURN
) ||
219 (db_run_mode
== STEP_CALLT
)) {
223 * We are about to execute this instruction,
227 ins
= db_get_task_value(pc
, sizeof(int), FALSE
, task
);
229 db_load_count
+= db_inst_load(ins
);
230 db_store_count
+= db_inst_store(ins
);
231 #ifdef SOFTWARE_SSTEP
232 /* Account for instructions in delay slots */
233 brpc
= next_instr_address(pc
,1,task
);
234 if ((brpc
!= pc
) && (inst_branch(ins
) || inst_call(ins
))) {
235 /* Note: this ~assumes an instruction <= sizeof(int) */
236 ins
= db_get_task_value(brpc
, sizeof(int), FALSE
, task
);
238 db_load_count
+= db_inst_load(ins
);
239 db_store_count
+= db_inst_store(ins
);
241 #endif /* SOFTWARE_SSTEP */
244 if (db_run_mode
== STEP_CONTINUE
) {
245 if (watchpt
|| db_find_breakpoint_here(task
, pc
)) {
247 * Step over breakpoint/watchpoint.
249 db_run_mode
= STEP_INVISIBLE
;
250 db_set_task_single_step(DDB_REGS
, task
);
252 db_set_breakpoints();
253 db_set_watchpoints();
256 db_set_task_single_step(DDB_REGS
, task
);
261 * 'n' and 'u' commands might never return.
262 * Limit the maximum number of steps.
268 if (db_inst_count
&& !(db_inst_count%db_max_inst_count
)) {
270 db_printf("%d instructions, continue ? (y/n) ",
285 if (db_run_mode
== STEP_CONTINUE
) {
286 db_run_mode
= STEP_INVISIBLE
;
287 db_set_task_single_step(regs
, task
);
291 #ifdef SOFTWARE_SSTEP
293 * Software implementation of single-stepping.
294 * If your machine does not have a trace mode
295 * similar to the vax or sun ones you can use
296 * this implementation, done for the mips.
297 * Just define the above conditional and provide
298 * the functions/macros defined below.
301 * inst_branch(), returns true if the instruction might branch
303 * branch_taken(), return the address the instruction might
305 * db_getreg_val(); return the value of a user register,
306 * as indicated in the hardware instruction
307 * encoding, e.g. 8 for r8
309 * next_instr_address(pc,bd,task) returns the address of the first
310 * instruction following the one at "pc",
311 * which is either in the taken path of
312 * the branch (bd==1) or not. This is
313 * for machines (mips) with branch delays.
315 * A single-step may involve at most 2 breakpoints -
316 * one for branch-not-taken and one for branch taken.
317 * If one of these addresses does not already have a breakpoint,
318 * we allocate a breakpoint and save it here.
319 * These breakpoints are deleted on return.
321 db_breakpoint_t db_not_taken_bkpt
= 0;
322 db_breakpoint_t db_taken_bkpt
= 0;
325 db_find_temp_breakpoint(
329 if (db_taken_bkpt
&& (db_taken_bkpt
->address
== addr
) &&
330 db_taken_bkpt
->task
== task
)
331 return db_taken_bkpt
;
332 if (db_not_taken_bkpt
&& (db_not_taken_bkpt
->address
== addr
) &&
333 db_not_taken_bkpt
->task
== task
)
334 return db_not_taken_bkpt
;
339 db_set_task_single_step(
340 register db_regs_t
*regs
,
343 db_addr_t pc
= PC_REGS(regs
), brpc
;
344 register unsigned int inst
;
345 register boolean_t unconditional
;
348 * User was stopped at pc, e.g. the instruction
349 * at pc was not executed.
351 inst
= db_get_task_value(pc
, sizeof(int), FALSE
, task
);
352 if (inst_branch(inst
) || inst_call(inst
)) {
353 extern db_expr_t
getreg_val(); /* XXX -- need prototype! */
355 brpc
= branch_taken(inst
, pc
, getreg_val
, (unsigned char*)regs
);
356 if (brpc
!= pc
) { /* self-branches are hopeless */
357 db_taken_bkpt
= db_set_temp_breakpoint(task
, brpc
);
360 pc
= next_instr_address(pc
,1,task
);
362 pc
= next_instr_address(pc
,0,task
);
365 * check if this control flow instruction is an
366 * unconditional transfer
369 unconditional
= inst_unconditional_flow_transfer(inst
);
372 We only set the sequential breakpoint if previous instruction was not
373 an unconditional change of flow of control. If the previous instruction
374 is an unconditional change of flow of control, setting a breakpoint in the
375 next sequential location may set a breakpoint in data or in another routine,
376 which could screw up either the program or the debugger.
377 (Consider, for instance, that the next sequential instruction is the
378 start of a routine needed by the debugger.)
380 if (!unconditional
&& db_find_breakpoint_here(task
, pc
) == 0 &&
381 (db_taken_bkpt
== 0 || db_taken_bkpt
->address
!= pc
)) {
382 db_not_taken_bkpt
= db_set_temp_breakpoint(task
, pc
);
384 db_not_taken_bkpt
= 0;
388 db_clear_task_single_step(
392 if (db_taken_bkpt
!= 0) {
393 db_delete_temp_breakpoint(task
, db_taken_bkpt
);
396 if (db_not_taken_bkpt
!= 0) {
397 db_delete_temp_breakpoint(task
, db_not_taken_bkpt
);
398 db_not_taken_bkpt
= 0;
402 #endif /* SOFTWARE_SSTEP */
404 extern int db_cmd_loop_done
;
414 boolean_t print
= FALSE
;
422 db_run_mode
= STEP_ONCE
;
423 db_loop_count
= count
;
424 db_sstep_print
= print
;
426 db_last_inst_count
= 0;
430 db_cmd_loop_done
= 1;
433 /* trace and print until call/return */
435 db_trace_until_call_cmd(
441 boolean_t print
= FALSE
;
446 db_run_mode
= STEP_CALLT
;
447 db_sstep_print
= print
;
449 db_last_inst_count
= 0;
453 db_cmd_loop_done
= 1;
457 db_trace_until_matching_cmd(
463 boolean_t print
= FALSE
;
468 db_run_mode
= STEP_RETURN
;
470 db_sstep_print
= print
;
472 db_last_inst_count
= 0;
476 db_cmd_loop_done
= 1;
488 * Though "cont/c" works fairly well, it's not really robust
489 * enough to use in arbitrary situations, so disable it.
490 * (Doesn't seem cost-effective to debug and fix what ails
495 db_run_mode
= STEP_COUNT
;
497 db_run_mode
= STEP_CONTINUE
;
499 db_run_mode
= STEP_CONTINUE
;
502 db_last_inst_count
= 0;
506 db_cmd_loop_done
= 1;
517 extern unsigned int switch_debugger
;
531 db_run_mode
= STEP_CONTINUE
;
533 db_last_inst_count
= 0;
537 db_cmd_loop_done
= 1;
542 db_in_single_step(void)
544 return(db_run_mode
!= STEP_NONE
&& db_run_mode
!= STEP_CONTINUE
);