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 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);
112 boolean_t
*is_breakpoint
,
116 register db_addr_t pc
;
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 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 pc
= PC_REGS(DDB_REGS
);
136 * Now check for a breakpoint at this address.
138 bkpt
= db_find_thread_breakpoint_here(space
, 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(pc
, task
);
159 return (FALSE
); /* continue */
162 if (db_run_mode
== STEP_RETURN
) {
165 /* WARNING: the following assumes an instruction fits an int */
166 db_expr_t ins
= db_get_task_value(pc
, sizeof(int), FALSE
, space
);
168 /* continue until matching return */
171 if (_setjmp(db_recover
= &db_jmpbuf
) == 0) {
172 if (!inst_trap_return(ins
) &&
173 (!inst_return(ins
) || --db_call_depth
!= 0)) {
174 if (db_sstep_print
) {
175 if (inst_call(ins
) || inst_return(ins
)) {
178 db_printf("[after %6d /%4d] ",
180 db_inst_count
- db_last_inst_count
);
181 db_last_inst_count
= db_inst_count
;
182 for (i
= db_call_depth
; --i
> 0; )
184 db_print_loc_and_inst(pc
, task
);
192 return (FALSE
); /* continue */
197 if (db_run_mode
== STEP_CALLT
) {
198 /* WARNING: the following assumes an instruction fits an int */
199 db_expr_t ins
= db_get_task_value(pc
, sizeof(int), FALSE
, space
);
201 /* continue until call or return */
203 if (!inst_call(ins
) &&
205 !inst_trap_return(ins
)) {
207 return (FALSE
); /* continue */
210 if (db_find_breakpoint_here(space
, pc
))
212 db_run_mode
= STEP_NONE
;
221 register db_addr_t pc
= PC_REGS(DDB_REGS
), brpc
;
223 if ((db_run_mode
== STEP_COUNT
) ||
224 (db_run_mode
== STEP_RETURN
) ||
225 (db_run_mode
== STEP_CALLT
)) {
229 * We are about to execute this instruction,
233 ins
= db_get_task_value(pc
, sizeof(int), FALSE
, task
);
235 db_load_count
+= db_inst_load(ins
);
236 db_store_count
+= db_inst_store(ins
);
237 #ifdef SOFTWARE_SSTEP
238 /* Account for instructions in delay slots */
239 brpc
= next_instr_address(pc
,1,task
);
240 if ((brpc
!= pc
) && (inst_branch(ins
) || inst_call(ins
))) {
241 /* Note: this ~assumes an instruction <= sizeof(int) */
242 ins
= db_get_task_value(brpc
, sizeof(int), FALSE
, task
);
244 db_load_count
+= db_inst_load(ins
);
245 db_store_count
+= db_inst_store(ins
);
247 #endif /* SOFTWARE_SSTEP */
250 if (db_run_mode
== STEP_CONTINUE
) {
251 if (watchpt
|| db_find_breakpoint_here(task
, pc
)) {
253 * Step over breakpoint/watchpoint.
255 db_run_mode
= STEP_INVISIBLE
;
256 db_set_task_single_step(DDB_REGS
, task
);
258 db_set_breakpoints();
259 db_set_watchpoints();
262 db_set_task_single_step(DDB_REGS
, task
);
267 * 'n' and 'u' commands might never return.
268 * Limit the maximum number of steps.
274 if (db_inst_count
&& !(db_inst_count%db_max_inst_count
)) {
276 db_printf("%d instructions, continue ? (y/n) ",
291 if (db_run_mode
== STEP_CONTINUE
) {
292 db_run_mode
= STEP_INVISIBLE
;
293 db_set_task_single_step(regs
, task
);
297 #ifdef SOFTWARE_SSTEP
299 * Software implementation of single-stepping.
300 * If your machine does not have a trace mode
301 * similar to the vax or sun ones you can use
302 * this implementation, done for the mips.
303 * Just define the above conditional and provide
304 * the functions/macros defined below.
307 * inst_branch(), returns true if the instruction might branch
309 * branch_taken(), return the address the instruction might
311 * db_getreg_val(); return the value of a user register,
312 * as indicated in the hardware instruction
313 * encoding, e.g. 8 for r8
315 * next_instr_address(pc,bd,task) returns the address of the first
316 * instruction following the one at "pc",
317 * which is either in the taken path of
318 * the branch (bd==1) or not. This is
319 * for machines (mips) with branch delays.
321 * A single-step may involve at most 2 breakpoints -
322 * one for branch-not-taken and one for branch taken.
323 * If one of these addresses does not already have a breakpoint,
324 * we allocate a breakpoint and save it here.
325 * These breakpoints are deleted on return.
327 db_breakpoint_t db_not_taken_bkpt
= 0;
328 db_breakpoint_t db_taken_bkpt
= 0;
331 db_find_temp_breakpoint(
335 if (db_taken_bkpt
&& (db_taken_bkpt
->address
== addr
) &&
336 db_taken_bkpt
->task
== task
)
337 return db_taken_bkpt
;
338 if (db_not_taken_bkpt
&& (db_not_taken_bkpt
->address
== addr
) &&
339 db_not_taken_bkpt
->task
== task
)
340 return db_not_taken_bkpt
;
345 db_set_task_single_step(
346 register db_regs_t
*regs
,
349 db_addr_t pc
= PC_REGS(regs
), brpc
;
350 register unsigned int inst
;
351 register boolean_t unconditional
;
354 * User was stopped at pc, e.g. the instruction
355 * at pc was not executed.
357 inst
= db_get_task_value(pc
, sizeof(int), FALSE
, task
);
358 if (inst_branch(inst
) || inst_call(inst
)) {
359 extern db_expr_t
getreg_val(); /* XXX -- need prototype! */
361 brpc
= branch_taken(inst
, pc
, getreg_val
, (unsigned char*)regs
);
362 if (brpc
!= pc
) { /* self-branches are hopeless */
363 db_taken_bkpt
= db_set_temp_breakpoint(task
, brpc
);
366 pc
= next_instr_address(pc
,1,task
);
368 pc
= next_instr_address(pc
,0,task
);
371 * check if this control flow instruction is an
372 * unconditional transfer
375 unconditional
= inst_unconditional_flow_transfer(inst
);
378 We only set the sequential breakpoint if previous instruction was not
379 an unconditional change of flow of control. If the previous instruction
380 is an unconditional change of flow of control, setting a breakpoint in the
381 next sequential location may set a breakpoint in data or in another routine,
382 which could screw up either the program or the debugger.
383 (Consider, for instance, that the next sequential instruction is the
384 start of a routine needed by the debugger.)
386 if (!unconditional
&& db_find_breakpoint_here(task
, pc
) == 0 &&
387 (db_taken_bkpt
== 0 || db_taken_bkpt
->address
!= pc
)) {
388 db_not_taken_bkpt
= db_set_temp_breakpoint(task
, pc
);
390 db_not_taken_bkpt
= 0;
394 db_clear_task_single_step(
398 if (db_taken_bkpt
!= 0) {
399 db_delete_temp_breakpoint(task
, db_taken_bkpt
);
402 if (db_not_taken_bkpt
!= 0) {
403 db_delete_temp_breakpoint(task
, db_not_taken_bkpt
);
404 db_not_taken_bkpt
= 0;
408 #endif /* SOFTWARE_SSTEP */
410 extern int db_cmd_loop_done
;
420 boolean_t print
= FALSE
;
428 db_run_mode
= STEP_ONCE
;
429 db_loop_count
= count
;
430 db_sstep_print
= print
;
432 db_last_inst_count
= 0;
436 db_cmd_loop_done
= 1;
439 /* trace and print until call/return */
441 db_trace_until_call_cmd(
447 boolean_t print
= FALSE
;
452 db_run_mode
= STEP_CALLT
;
453 db_sstep_print
= print
;
455 db_last_inst_count
= 0;
459 db_cmd_loop_done
= 1;
463 db_trace_until_matching_cmd(
469 boolean_t print
= FALSE
;
474 db_run_mode
= STEP_RETURN
;
476 db_sstep_print
= print
;
478 db_last_inst_count
= 0;
482 db_cmd_loop_done
= 1;
494 * Though "cont/c" works fairly well, it's not really robust
495 * enough to use in arbitrary situations, so disable it.
496 * (Doesn't seem cost-effective to debug and fix what ails
501 db_run_mode
= STEP_COUNT
;
503 db_run_mode
= STEP_CONTINUE
;
505 db_run_mode
= STEP_CONTINUE
;
508 db_last_inst_count
= 0;
512 db_cmd_loop_done
= 1;
526 db_run_mode
= STEP_CONTINUE
;
528 db_last_inst_count
= 0;
532 db_cmd_loop_done
= 1;
538 db_in_single_step(void)
540 return(db_run_mode
!= STEP_NONE
&& db_run_mode
!= STEP_CONTINUE
);