]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
43866e37 A |
6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
7 | * | |
8 | * This file contains Original Code and/or Modifications of Original Code | |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
14 | * | |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
43866e37 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
1c79356b A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* | |
26 | * @OSF_COPYRIGHT@ | |
27 | */ | |
28 | /* | |
29 | * Mach Operating System | |
30 | * Copyright (c) 1991,1990 Carnegie Mellon University | |
31 | * All Rights Reserved. | |
32 | * | |
33 | * Permission to use, copy, modify and distribute this software and its | |
34 | * documentation is hereby granted, provided that both the copyright | |
35 | * notice and this permission notice appear in all copies of the | |
36 | * software, derivative works or modified versions, and any portions | |
37 | * thereof, and that both notices appear in supporting documentation. | |
38 | * | |
39 | * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" | |
40 | * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR | |
41 | * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
42 | * | |
43 | * Carnegie Mellon requests users of this software to return to | |
44 | * | |
45 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
46 | * School of Computer Science | |
47 | * Carnegie Mellon University | |
48 | * Pittsburgh PA 15213-3890 | |
49 | * | |
50 | * any improvements or extensions that they make and grant Carnegie Mellon | |
51 | * the rights to redistribute these changes. | |
52 | */ | |
53 | /* | |
54 | */ | |
55 | /* | |
56 | * Author: David B. Golub, Carnegie Mellon University | |
57 | * Date: 7/90 | |
58 | */ | |
59 | ||
60 | /* | |
61 | * Commands to run process. | |
62 | */ | |
63 | #include <mach/boolean.h> | |
64 | #include <machine/db_machdep.h> | |
65 | ||
66 | #include <ddb/db_lex.h> | |
67 | #include <ddb/db_break.h> | |
68 | #include <ddb/db_access.h> | |
69 | #include <ddb/db_run.h> | |
70 | #include <ddb/db_cond.h> | |
71 | #include <ddb/db_examine.h> | |
72 | #include <ddb/db_output.h> /* For db_printf() */ | |
73 | #include <ddb/db_watch.h> | |
74 | #include <kern/misc_protos.h> | |
75 | #include <kern/debug.h> | |
76 | ||
77 | boolean_t db_sstep_print; | |
78 | int db_loop_count; | |
79 | int db_call_depth; | |
80 | ||
81 | int db_inst_count; | |
82 | int db_last_inst_count; | |
83 | int db_load_count; | |
84 | int db_store_count; | |
85 | int db_max_inst_count = 1000; | |
86 | ||
87 | #ifndef db_set_single_step | |
88 | void db_set_task_single_step( | |
89 | register db_regs_t *regs, | |
90 | task_t task); | |
91 | #else | |
92 | #define db_set_task_single_step(regs,task) db_set_single_step(regs) | |
93 | #endif | |
94 | #ifndef db_clear_single_step | |
95 | void db_clear_task_single_step( | |
96 | db_regs_t *regs, | |
97 | task_t task); | |
98 | #else | |
99 | #define db_clear_task_single_step(regs,task) db_clear_single_step(regs) | |
100 | #endif | |
101 | ||
102 | extern jmp_buf_t *db_recover; | |
103 | boolean_t db_step_again(void); | |
104 | ||
105 | boolean_t | |
106 | db_stop_at_pc( | |
107 | boolean_t *is_breakpoint, | |
108 | task_t task, | |
109 | task_t space) | |
110 | { | |
111 | register db_addr_t pc; | |
112 | register db_thread_breakpoint_t bkpt; | |
113 | ||
114 | db_clear_task_single_step(DDB_REGS, space); | |
115 | db_clear_breakpoints(); | |
116 | db_clear_watchpoints(); | |
117 | pc = PC_REGS(DDB_REGS); | |
118 | ||
119 | #ifdef FIXUP_PC_AFTER_BREAK | |
120 | if (*is_breakpoint) { | |
121 | /* | |
122 | * Breakpoint trap. Fix up the PC if the | |
123 | * machine requires it. | |
124 | */ | |
125 | FIXUP_PC_AFTER_BREAK | |
126 | pc = PC_REGS(DDB_REGS); | |
127 | } | |
128 | #endif | |
129 | ||
130 | /* | |
131 | * Now check for a breakpoint at this address. | |
132 | */ | |
133 | bkpt = db_find_thread_breakpoint_here(space, pc); | |
134 | if (bkpt) { | |
135 | if (db_cond_check(bkpt)) { | |
136 | *is_breakpoint = TRUE; | |
137 | return (TRUE); /* stop here */ | |
138 | } | |
139 | } | |
140 | *is_breakpoint = FALSE; | |
141 | ||
142 | if (db_run_mode == STEP_INVISIBLE) { | |
143 | db_run_mode = STEP_CONTINUE; | |
144 | return (FALSE); /* continue */ | |
145 | } | |
146 | if (db_run_mode == STEP_COUNT) { | |
147 | return (FALSE); /* continue */ | |
148 | } | |
149 | if (db_run_mode == STEP_ONCE) { | |
150 | if (--db_loop_count > 0) { | |
151 | if (db_sstep_print) { | |
152 | db_print_loc_and_inst(pc, task); | |
153 | } | |
154 | return (FALSE); /* continue */ | |
155 | } | |
156 | } | |
157 | if (db_run_mode == STEP_RETURN) { | |
158 | jmp_buf_t *prev; | |
159 | jmp_buf_t db_jmpbuf; | |
160 | /* WARNING: the following assumes an instruction fits an int */ | |
161 | db_expr_t ins = db_get_task_value(pc, sizeof(int), FALSE, space); | |
162 | ||
163 | /* continue until matching return */ | |
164 | ||
165 | prev = db_recover; | |
166 | if (_setjmp(db_recover = &db_jmpbuf) == 0) { | |
167 | if (!inst_trap_return(ins) && | |
168 | (!inst_return(ins) || --db_call_depth != 0)) { | |
169 | if (db_sstep_print) { | |
170 | if (inst_call(ins) || inst_return(ins)) { | |
171 | register int i; | |
172 | ||
173 | db_printf("[after %6d /%4d] ", | |
174 | db_inst_count, | |
175 | db_inst_count - db_last_inst_count); | |
176 | db_last_inst_count = db_inst_count; | |
177 | for (i = db_call_depth; --i > 0; ) | |
178 | db_printf(" "); | |
179 | db_print_loc_and_inst(pc, task); | |
180 | db_printf("\n"); | |
181 | } | |
182 | } | |
183 | if (inst_call(ins)) | |
184 | db_call_depth++; | |
185 | db_recover = prev; | |
186 | if (db_step_again()) | |
187 | return (FALSE); /* continue */ | |
188 | } | |
189 | } | |
190 | db_recover = prev; | |
191 | } | |
192 | if (db_run_mode == STEP_CALLT) { | |
193 | /* WARNING: the following assumes an instruction fits an int */ | |
194 | db_expr_t ins = db_get_task_value(pc, sizeof(int), FALSE, space); | |
195 | ||
196 | /* continue until call or return */ | |
197 | ||
198 | if (!inst_call(ins) && | |
199 | !inst_return(ins) && | |
200 | !inst_trap_return(ins)) { | |
201 | if (db_step_again()) | |
202 | return (FALSE); /* continue */ | |
203 | } | |
204 | } | |
205 | if (db_find_breakpoint_here(space, pc)) | |
206 | return(FALSE); | |
207 | db_run_mode = STEP_NONE; | |
208 | return (TRUE); | |
209 | } | |
210 | ||
211 | void | |
212 | db_restart_at_pc( | |
213 | boolean_t watchpt, | |
214 | task_t task) | |
215 | { | |
216 | register db_addr_t pc = PC_REGS(DDB_REGS), brpc; | |
217 | ||
218 | if ((db_run_mode == STEP_COUNT) || | |
219 | (db_run_mode == STEP_RETURN) || | |
220 | (db_run_mode == STEP_CALLT)) { | |
221 | db_expr_t ins; | |
222 | ||
223 | /* | |
224 | * We are about to execute this instruction, | |
225 | * so count it now. | |
226 | */ | |
227 | ||
228 | ins = db_get_task_value(pc, sizeof(int), FALSE, task); | |
229 | db_inst_count++; | |
230 | db_load_count += db_inst_load(ins); | |
231 | db_store_count += db_inst_store(ins); | |
232 | #ifdef SOFTWARE_SSTEP | |
233 | /* Account for instructions in delay slots */ | |
234 | brpc = next_instr_address(pc,1,task); | |
235 | if ((brpc != pc) && (inst_branch(ins) || inst_call(ins))) { | |
236 | /* Note: this ~assumes an instruction <= sizeof(int) */ | |
237 | ins = db_get_task_value(brpc, sizeof(int), FALSE, task); | |
238 | db_inst_count++; | |
239 | db_load_count += db_inst_load(ins); | |
240 | db_store_count += db_inst_store(ins); | |
241 | } | |
242 | #endif /* SOFTWARE_SSTEP */ | |
243 | } | |
244 | ||
245 | if (db_run_mode == STEP_CONTINUE) { | |
246 | if (watchpt || db_find_breakpoint_here(task, pc)) { | |
247 | /* | |
248 | * Step over breakpoint/watchpoint. | |
249 | */ | |
250 | db_run_mode = STEP_INVISIBLE; | |
251 | db_set_task_single_step(DDB_REGS, task); | |
252 | } else { | |
253 | db_set_breakpoints(); | |
254 | db_set_watchpoints(); | |
255 | } | |
256 | } else { | |
257 | db_set_task_single_step(DDB_REGS, task); | |
258 | } | |
259 | } | |
260 | ||
261 | /* | |
262 | * 'n' and 'u' commands might never return. | |
263 | * Limit the maximum number of steps. | |
264 | */ | |
265 | ||
266 | boolean_t | |
267 | db_step_again(void) | |
268 | { | |
269 | if (db_inst_count && !(db_inst_count%db_max_inst_count)) { | |
270 | char c; | |
271 | db_printf("%d instructions, continue ? (y/n) ", | |
272 | db_inst_count); | |
273 | c = cngetc(); | |
274 | db_printf("\n"); | |
275 | if(c == 'n') | |
276 | return(FALSE); | |
277 | } | |
278 | return(TRUE); | |
279 | } | |
280 | ||
281 | void | |
282 | db_single_step( | |
283 | db_regs_t *regs, | |
284 | task_t task) | |
285 | { | |
286 | if (db_run_mode == STEP_CONTINUE) { | |
287 | db_run_mode = STEP_INVISIBLE; | |
288 | db_set_task_single_step(regs, task); | |
289 | } | |
290 | } | |
291 | ||
292 | #ifdef SOFTWARE_SSTEP | |
293 | /* | |
294 | * Software implementation of single-stepping. | |
295 | * If your machine does not have a trace mode | |
296 | * similar to the vax or sun ones you can use | |
297 | * this implementation, done for the mips. | |
298 | * Just define the above conditional and provide | |
299 | * the functions/macros defined below. | |
300 | * | |
301 | * extern boolean_t | |
302 | * inst_branch(), returns true if the instruction might branch | |
303 | * extern unsigned | |
304 | * branch_taken(), return the address the instruction might | |
305 | * branch to | |
306 | * db_getreg_val(); return the value of a user register, | |
307 | * as indicated in the hardware instruction | |
308 | * encoding, e.g. 8 for r8 | |
309 | * | |
310 | * next_instr_address(pc,bd,task) returns the address of the first | |
311 | * instruction following the one at "pc", | |
312 | * which is either in the taken path of | |
313 | * the branch (bd==1) or not. This is | |
314 | * for machines (mips) with branch delays. | |
315 | * | |
316 | * A single-step may involve at most 2 breakpoints - | |
317 | * one for branch-not-taken and one for branch taken. | |
318 | * If one of these addresses does not already have a breakpoint, | |
319 | * we allocate a breakpoint and save it here. | |
320 | * These breakpoints are deleted on return. | |
321 | */ | |
322 | db_breakpoint_t db_not_taken_bkpt = 0; | |
323 | db_breakpoint_t db_taken_bkpt = 0; | |
324 | ||
325 | db_breakpoint_t | |
326 | db_find_temp_breakpoint( | |
327 | task_t task, | |
328 | db_addr_t addr) | |
329 | { | |
330 | if (db_taken_bkpt && (db_taken_bkpt->address == addr) && | |
331 | db_taken_bkpt->task == task) | |
332 | return db_taken_bkpt; | |
333 | if (db_not_taken_bkpt && (db_not_taken_bkpt->address == addr) && | |
334 | db_not_taken_bkpt->task == task) | |
335 | return db_not_taken_bkpt; | |
336 | return 0; | |
337 | } | |
338 | ||
339 | void | |
340 | db_set_task_single_step( | |
341 | register db_regs_t *regs, | |
342 | task_t task) | |
343 | { | |
344 | db_addr_t pc = PC_REGS(regs), brpc; | |
345 | register unsigned int inst; | |
346 | register boolean_t unconditional; | |
347 | ||
348 | /* | |
349 | * User was stopped at pc, e.g. the instruction | |
350 | * at pc was not executed. | |
351 | */ | |
352 | inst = db_get_task_value(pc, sizeof(int), FALSE, task); | |
353 | if (inst_branch(inst) || inst_call(inst)) { | |
354 | extern db_expr_t getreg_val(); /* XXX -- need prototype! */ | |
355 | ||
356 | brpc = branch_taken(inst, pc, getreg_val, (unsigned char*)regs); | |
357 | if (brpc != pc) { /* self-branches are hopeless */ | |
358 | db_taken_bkpt = db_set_temp_breakpoint(task, brpc); | |
359 | } else | |
360 | db_taken_bkpt = 0; | |
361 | pc = next_instr_address(pc,1,task); | |
362 | } else | |
363 | pc = next_instr_address(pc,0,task); | |
364 | ||
365 | /* | |
366 | * check if this control flow instruction is an | |
367 | * unconditional transfer | |
368 | */ | |
369 | ||
370 | unconditional = inst_unconditional_flow_transfer(inst); | |
371 | ||
372 | /* | |
373 | We only set the sequential breakpoint if previous instruction was not | |
374 | an unconditional change of flow of control. If the previous instruction | |
375 | is an unconditional change of flow of control, setting a breakpoint in the | |
376 | next sequential location may set a breakpoint in data or in another routine, | |
377 | which could screw up either the program or the debugger. | |
378 | (Consider, for instance, that the next sequential instruction is the | |
379 | start of a routine needed by the debugger.) | |
380 | */ | |
381 | if (!unconditional && db_find_breakpoint_here(task, pc) == 0 && | |
382 | (db_taken_bkpt == 0 || db_taken_bkpt->address != pc)) { | |
383 | db_not_taken_bkpt = db_set_temp_breakpoint(task, pc); | |
384 | } else | |
385 | db_not_taken_bkpt = 0; | |
386 | } | |
387 | ||
388 | void | |
389 | db_clear_task_single_step( | |
390 | db_regs_t *regs, | |
391 | task_t task) | |
392 | { | |
393 | if (db_taken_bkpt != 0) { | |
394 | db_delete_temp_breakpoint(task, db_taken_bkpt); | |
395 | db_taken_bkpt = 0; | |
396 | } | |
397 | if (db_not_taken_bkpt != 0) { | |
398 | db_delete_temp_breakpoint(task, db_not_taken_bkpt); | |
399 | db_not_taken_bkpt = 0; | |
400 | } | |
401 | } | |
402 | ||
403 | #endif /* SOFTWARE_SSTEP */ | |
404 | ||
405 | extern int db_cmd_loop_done; | |
406 | ||
407 | /* single-step */ | |
408 | void | |
409 | db_single_step_cmd( | |
410 | db_expr_t addr, | |
411 | int have_addr, | |
412 | db_expr_t count, | |
413 | char * modif) | |
414 | { | |
415 | boolean_t print = FALSE; | |
416 | ||
417 | if (count == -1) | |
418 | count = 1; | |
419 | ||
420 | if (modif[0] == 'p') | |
421 | print = TRUE; | |
422 | ||
423 | db_run_mode = STEP_ONCE; | |
424 | db_loop_count = count; | |
425 | db_sstep_print = print; | |
426 | db_inst_count = 0; | |
427 | db_last_inst_count = 0; | |
428 | db_load_count = 0; | |
429 | db_store_count = 0; | |
430 | ||
431 | db_cmd_loop_done = 1; | |
432 | } | |
433 | ||
434 | /* trace and print until call/return */ | |
435 | void | |
436 | db_trace_until_call_cmd( | |
437 | db_expr_t addr, | |
438 | int have_addr, | |
439 | db_expr_t count, | |
440 | char * modif) | |
441 | { | |
442 | boolean_t print = FALSE; | |
443 | ||
444 | if (modif[0] == 'p') | |
445 | print = TRUE; | |
446 | ||
447 | db_run_mode = STEP_CALLT; | |
448 | db_sstep_print = print; | |
449 | db_inst_count = 0; | |
450 | db_last_inst_count = 0; | |
451 | db_load_count = 0; | |
452 | db_store_count = 0; | |
453 | ||
454 | db_cmd_loop_done = 1; | |
455 | } | |
456 | ||
457 | void | |
458 | db_trace_until_matching_cmd( | |
459 | db_expr_t addr, | |
460 | int have_addr, | |
461 | db_expr_t count, | |
462 | char * modif) | |
463 | { | |
464 | boolean_t print = FALSE; | |
465 | ||
466 | if (modif[0] == 'p') | |
467 | print = TRUE; | |
468 | ||
469 | db_run_mode = STEP_RETURN; | |
470 | db_call_depth = 1; | |
471 | db_sstep_print = print; | |
472 | db_inst_count = 0; | |
473 | db_last_inst_count = 0; | |
474 | db_load_count = 0; | |
475 | db_store_count = 0; | |
476 | ||
477 | db_cmd_loop_done = 1; | |
478 | } | |
479 | ||
480 | /* continue */ | |
481 | void | |
482 | db_continue_cmd( | |
483 | db_expr_t addr, | |
484 | int have_addr, | |
485 | db_expr_t count, | |
486 | char * modif) | |
487 | { | |
488 | /* | |
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 | |
492 | * it.) | |
493 | */ | |
494 | #if 0 | |
495 | if (modif[0] == 'c') | |
496 | db_run_mode = STEP_COUNT; | |
497 | else | |
498 | db_run_mode = STEP_CONTINUE; | |
499 | #else | |
500 | db_run_mode = STEP_CONTINUE; | |
501 | #endif | |
502 | db_inst_count = 0; | |
503 | db_last_inst_count = 0; | |
504 | db_load_count = 0; | |
505 | db_store_count = 0; | |
506 | ||
507 | db_cmd_loop_done = 1; | |
508 | } | |
509 | ||
510 | /* gdb */ | |
511 | void | |
512 | db_continue_gdb( | |
513 | db_expr_t addr, | |
514 | int have_addr, | |
515 | db_expr_t count, | |
516 | char * modif) | |
517 | { | |
518 | #if defined(__ppc__) | |
519 | db_to_gdb(); | |
520 | #endif | |
521 | db_run_mode = STEP_CONTINUE; | |
522 | db_inst_count = 0; | |
523 | db_last_inst_count = 0; | |
524 | db_load_count = 0; | |
525 | db_store_count = 0; | |
526 | ||
527 | db_cmd_loop_done = 1; | |
528 | } | |
529 | ||
530 | ||
531 | ||
532 | boolean_t | |
533 | db_in_single_step(void) | |
534 | { | |
535 | return(db_run_mode != STEP_NONE && db_run_mode != STEP_CONTINUE); | |
536 | } |