2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
61 * Author: David B. Golub, Carnegie Mellon University
66 * Commands to run process.
68 #include <mach/boolean.h>
69 #include <machine/db_machdep.h>
71 #include <ddb/db_lex.h>
72 #include <ddb/db_break.h>
73 #include <ddb/db_access.h>
74 #include <ddb/db_run.h>
75 #include <ddb/db_cond.h>
76 #include <ddb/db_examine.h>
77 #include <ddb/db_output.h> /* For db_printf() */
78 #include <ddb/db_watch.h>
79 #include <kern/misc_protos.h>
80 #include <kern/debug.h>
82 #include <IOKit/IOPlatformExpert.h>
84 boolean_t db_sstep_print
;
89 int db_last_inst_count
;
92 int db_max_inst_count
= 1000;
94 #ifndef db_set_single_step
95 void db_set_task_single_step(
96 register db_regs_t
*regs
,
99 #define db_set_task_single_step(regs,task) db_set_single_step(regs)
101 #ifndef db_clear_single_step
102 void db_clear_task_single_step(
106 #define db_clear_task_single_step(regs,task) db_clear_single_step(regs)
109 extern jmp_buf_t
*db_recover
;
110 boolean_t
db_step_again(void);
114 boolean_t
*is_breakpoint
,
118 register db_addr_t pc
;
119 register db_thread_breakpoint_t bkpt
;
121 db_clear_task_single_step(DDB_REGS
, space
);
122 db_clear_breakpoints();
123 db_clear_watchpoints();
124 pc
= PC_REGS(DDB_REGS
);
126 #ifdef FIXUP_PC_AFTER_BREAK
127 if (*is_breakpoint
) {
129 * Breakpoint trap. Fix up the PC if the
130 * machine requires it.
133 pc
= PC_REGS(DDB_REGS
);
138 * Now check for a breakpoint at this address.
140 bkpt
= db_find_thread_breakpoint_here(space
, pc
);
142 if (db_cond_check(bkpt
)) {
143 *is_breakpoint
= TRUE
;
144 return (TRUE
); /* stop here */
147 *is_breakpoint
= FALSE
;
149 if (db_run_mode
== STEP_INVISIBLE
) {
150 db_run_mode
= STEP_CONTINUE
;
151 return (FALSE
); /* continue */
153 if (db_run_mode
== STEP_COUNT
) {
154 return (FALSE
); /* continue */
156 if (db_run_mode
== STEP_ONCE
) {
157 if (--db_loop_count
> 0) {
158 if (db_sstep_print
) {
159 db_print_loc_and_inst(pc
, task
);
161 return (FALSE
); /* continue */
164 if (db_run_mode
== STEP_RETURN
) {
167 /* WARNING: the following assumes an instruction fits an int */
168 db_expr_t ins
= db_get_task_value(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(pc
, task
);
194 return (FALSE
); /* continue */
199 if (db_run_mode
== STEP_CALLT
) {
200 /* WARNING: the following assumes an instruction fits an int */
201 db_expr_t ins
= db_get_task_value(pc
, sizeof(int), FALSE
, space
);
203 /* continue until call or return */
205 if (!inst_call(ins
) &&
207 !inst_trap_return(ins
)) {
209 return (FALSE
); /* continue */
212 if (db_find_breakpoint_here(space
, pc
))
214 db_run_mode
= STEP_NONE
;
223 register db_addr_t pc
= PC_REGS(DDB_REGS
), brpc
;
225 if ((db_run_mode
== STEP_COUNT
) ||
226 (db_run_mode
== STEP_RETURN
) ||
227 (db_run_mode
== STEP_CALLT
)) {
231 * We are about to execute this instruction,
235 ins
= db_get_task_value(pc
, sizeof(int), FALSE
, task
);
237 db_load_count
+= db_inst_load(ins
);
238 db_store_count
+= db_inst_store(ins
);
239 #ifdef SOFTWARE_SSTEP
240 /* Account for instructions in delay slots */
241 brpc
= next_instr_address(pc
,1,task
);
242 if ((brpc
!= pc
) && (inst_branch(ins
) || inst_call(ins
))) {
243 /* Note: this ~assumes an instruction <= sizeof(int) */
244 ins
= db_get_task_value(brpc
, sizeof(int), FALSE
, task
);
246 db_load_count
+= db_inst_load(ins
);
247 db_store_count
+= db_inst_store(ins
);
249 #endif /* SOFTWARE_SSTEP */
252 if (db_run_mode
== STEP_CONTINUE
) {
253 if (watchpt
|| db_find_breakpoint_here(task
, pc
)) {
255 * Step over breakpoint/watchpoint.
257 db_run_mode
= STEP_INVISIBLE
;
258 db_set_task_single_step(DDB_REGS
, task
);
260 db_set_breakpoints();
261 db_set_watchpoints();
264 db_set_task_single_step(DDB_REGS
, task
);
269 * 'n' and 'u' commands might never return.
270 * Limit the maximum number of steps.
276 if (db_inst_count
&& !(db_inst_count%db_max_inst_count
)) {
278 db_printf("%d instructions, continue ? (y/n) ",
293 if (db_run_mode
== STEP_CONTINUE
) {
294 db_run_mode
= STEP_INVISIBLE
;
295 db_set_task_single_step(regs
, task
);
299 #ifdef SOFTWARE_SSTEP
301 * Software implementation of single-stepping.
302 * If your machine does not have a trace mode
303 * similar to the vax or sun ones you can use
304 * this implementation, done for the mips.
305 * Just define the above conditional and provide
306 * the functions/macros defined below.
309 * inst_branch(), returns true if the instruction might branch
311 * branch_taken(), return the address the instruction might
313 * db_getreg_val(); return the value of a user register,
314 * as indicated in the hardware instruction
315 * encoding, e.g. 8 for r8
317 * next_instr_address(pc,bd,task) returns the address of the first
318 * instruction following the one at "pc",
319 * which is either in the taken path of
320 * the branch (bd==1) or not. This is
321 * for machines (mips) with branch delays.
323 * A single-step may involve at most 2 breakpoints -
324 * one for branch-not-taken and one for branch taken.
325 * If one of these addresses does not already have a breakpoint,
326 * we allocate a breakpoint and save it here.
327 * These breakpoints are deleted on return.
329 db_breakpoint_t db_not_taken_bkpt
= 0;
330 db_breakpoint_t db_taken_bkpt
= 0;
333 db_find_temp_breakpoint(
337 if (db_taken_bkpt
&& (db_taken_bkpt
->address
== addr
) &&
338 db_taken_bkpt
->task
== task
)
339 return db_taken_bkpt
;
340 if (db_not_taken_bkpt
&& (db_not_taken_bkpt
->address
== addr
) &&
341 db_not_taken_bkpt
->task
== task
)
342 return db_not_taken_bkpt
;
347 db_set_task_single_step(
348 register db_regs_t
*regs
,
351 db_addr_t pc
= PC_REGS(regs
), brpc
;
352 register unsigned int inst
;
353 register boolean_t unconditional
;
356 * User was stopped at pc, e.g. the instruction
357 * at pc was not executed.
359 inst
= db_get_task_value(pc
, sizeof(int), FALSE
, task
);
360 if (inst_branch(inst
) || inst_call(inst
)) {
361 extern db_expr_t
getreg_val(); /* XXX -- need prototype! */
363 brpc
= branch_taken(inst
, pc
, getreg_val
, (unsigned char*)regs
);
364 if (brpc
!= pc
) { /* self-branches are hopeless */
365 db_taken_bkpt
= db_set_temp_breakpoint(task
, brpc
);
368 pc
= next_instr_address(pc
,1,task
);
370 pc
= next_instr_address(pc
,0,task
);
373 * check if this control flow instruction is an
374 * unconditional transfer
377 unconditional
= inst_unconditional_flow_transfer(inst
);
380 We only set the sequential breakpoint if previous instruction was not
381 an unconditional change of flow of control. If the previous instruction
382 is an unconditional change of flow of control, setting a breakpoint in the
383 next sequential location may set a breakpoint in data or in another routine,
384 which could screw up either the program or the debugger.
385 (Consider, for instance, that the next sequential instruction is the
386 start of a routine needed by the debugger.)
388 if (!unconditional
&& db_find_breakpoint_here(task
, pc
) == 0 &&
389 (db_taken_bkpt
== 0 || db_taken_bkpt
->address
!= pc
)) {
390 db_not_taken_bkpt
= db_set_temp_breakpoint(task
, pc
);
392 db_not_taken_bkpt
= 0;
396 db_clear_task_single_step(
400 if (db_taken_bkpt
!= 0) {
401 db_delete_temp_breakpoint(task
, db_taken_bkpt
);
404 if (db_not_taken_bkpt
!= 0) {
405 db_delete_temp_breakpoint(task
, db_not_taken_bkpt
);
406 db_not_taken_bkpt
= 0;
410 #endif /* SOFTWARE_SSTEP */
412 extern int db_cmd_loop_done
;
422 boolean_t print
= FALSE
;
430 db_run_mode
= STEP_ONCE
;
431 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(
449 boolean_t print
= FALSE
;
454 db_run_mode
= STEP_CALLT
;
455 db_sstep_print
= print
;
457 db_last_inst_count
= 0;
461 db_cmd_loop_done
= 1;
465 db_trace_until_matching_cmd(
471 boolean_t print
= FALSE
;
476 db_run_mode
= STEP_RETURN
;
478 db_sstep_print
= print
;
480 db_last_inst_count
= 0;
484 db_cmd_loop_done
= 1;
496 * Though "cont/c" works fairly well, it's not really robust
497 * enough to use in arbitrary situations, so disable it.
498 * (Doesn't seem cost-effective to debug and fix what ails
503 db_run_mode
= STEP_COUNT
;
505 db_run_mode
= STEP_CONTINUE
;
507 db_run_mode
= STEP_CONTINUE
;
510 db_last_inst_count
= 0;
514 db_cmd_loop_done
= 1;
525 extern unsigned int switch_debugger
;
539 db_run_mode
= STEP_CONTINUE
;
541 db_last_inst_count
= 0;
545 db_cmd_loop_done
= 1;
550 db_in_single_step(void)
552 return(db_run_mode
!= STEP_NONE
&& db_run_mode
!= STEP_CONTINUE
);