]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
8ad349bb | 4 | * @APPLE_LICENSE_OSREFERENCE_HEADER_START@ |
1c79356b | 5 | * |
8ad349bb A |
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 | |
14 | * agreement. | |
15 | * | |
16 | * Please obtain a copy of the License at | |
17 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
18 | * file. | |
19 | * | |
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. | |
27 | * | |
28 | * @APPLE_LICENSE_OSREFERENCE_HEADER_END@ | |
1c79356b A |
29 | */ |
30 | /* | |
31 | * @OSF_COPYRIGHT@ | |
32 | */ | |
33 | /* | |
34 | * Mach Operating System | |
35 | * Copyright (c) 1991,1990 Carnegie Mellon University | |
36 | * All Rights Reserved. | |
37 | * | |
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. | |
43 | * | |
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. | |
47 | * | |
48 | * Carnegie Mellon requests users of this software to return to | |
49 | * | |
50 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
51 | * School of Computer Science | |
52 | * Carnegie Mellon University | |
53 | * Pittsburgh PA 15213-3890 | |
54 | * | |
55 | * any improvements or extensions that they make and grant Carnegie Mellon | |
56 | * the rights to redistribute these changes. | |
57 | */ | |
58 | /* | |
59 | */ | |
60 | /* | |
61 | * Author: David B. Golub, Carnegie Mellon University | |
62 | * Date: 7/90 | |
63 | */ | |
64 | ||
65 | /* | |
66 | * Commands to run process. | |
67 | */ | |
68 | #include <mach/boolean.h> | |
69 | #include <machine/db_machdep.h> | |
70 | ||
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> | |
81 | ||
82 | boolean_t db_sstep_print; | |
83 | int db_loop_count; | |
84 | int db_call_depth; | |
85 | ||
86 | int db_inst_count; | |
87 | int db_last_inst_count; | |
88 | int db_load_count; | |
89 | int db_store_count; | |
90 | int db_max_inst_count = 1000; | |
91 | ||
92 | #ifndef db_set_single_step | |
93 | void db_set_task_single_step( | |
94 | register db_regs_t *regs, | |
95 | task_t task); | |
96 | #else | |
97 | #define db_set_task_single_step(regs,task) db_set_single_step(regs) | |
98 | #endif | |
99 | #ifndef db_clear_single_step | |
100 | void db_clear_task_single_step( | |
101 | db_regs_t *regs, | |
102 | task_t task); | |
103 | #else | |
104 | #define db_clear_task_single_step(regs,task) db_clear_single_step(regs) | |
105 | #endif | |
106 | ||
107 | extern jmp_buf_t *db_recover; | |
108 | boolean_t db_step_again(void); | |
109 | ||
110 | boolean_t | |
111 | db_stop_at_pc( | |
112 | boolean_t *is_breakpoint, | |
113 | task_t task, | |
114 | task_t space) | |
115 | { | |
116 | register db_addr_t pc; | |
117 | register db_thread_breakpoint_t bkpt; | |
118 | ||
119 | db_clear_task_single_step(DDB_REGS, space); | |
120 | db_clear_breakpoints(); | |
121 | db_clear_watchpoints(); | |
122 | pc = PC_REGS(DDB_REGS); | |
123 | ||
124 | #ifdef FIXUP_PC_AFTER_BREAK | |
125 | if (*is_breakpoint) { | |
126 | /* | |
127 | * Breakpoint trap. Fix up the PC if the | |
128 | * machine requires it. | |
129 | */ | |
130 | FIXUP_PC_AFTER_BREAK | |
131 | pc = PC_REGS(DDB_REGS); | |
132 | } | |
133 | #endif | |
134 | ||
135 | /* | |
136 | * Now check for a breakpoint at this address. | |
137 | */ | |
138 | bkpt = db_find_thread_breakpoint_here(space, pc); | |
139 | if (bkpt) { | |
140 | if (db_cond_check(bkpt)) { | |
141 | *is_breakpoint = TRUE; | |
142 | return (TRUE); /* stop here */ | |
143 | } | |
144 | } | |
145 | *is_breakpoint = FALSE; | |
146 | ||
147 | if (db_run_mode == STEP_INVISIBLE) { | |
148 | db_run_mode = STEP_CONTINUE; | |
149 | return (FALSE); /* continue */ | |
150 | } | |
151 | if (db_run_mode == STEP_COUNT) { | |
152 | return (FALSE); /* continue */ | |
153 | } | |
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); | |
158 | } | |
159 | return (FALSE); /* continue */ | |
160 | } | |
161 | } | |
162 | if (db_run_mode == STEP_RETURN) { | |
163 | jmp_buf_t *prev; | |
164 | jmp_buf_t db_jmpbuf; | |
165 | /* WARNING: the following assumes an instruction fits an int */ | |
166 | db_expr_t ins = db_get_task_value(pc, sizeof(int), FALSE, space); | |
167 | ||
168 | /* continue until matching return */ | |
169 | ||
170 | prev = db_recover; | |
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)) { | |
176 | register int i; | |
177 | ||
178 | db_printf("[after %6d /%4d] ", | |
179 | db_inst_count, | |
180 | db_inst_count - db_last_inst_count); | |
181 | db_last_inst_count = db_inst_count; | |
182 | for (i = db_call_depth; --i > 0; ) | |
183 | db_printf(" "); | |
184 | db_print_loc_and_inst(pc, task); | |
185 | db_printf("\n"); | |
186 | } | |
187 | } | |
188 | if (inst_call(ins)) | |
189 | db_call_depth++; | |
190 | db_recover = prev; | |
191 | if (db_step_again()) | |
192 | return (FALSE); /* continue */ | |
193 | } | |
194 | } | |
195 | db_recover = prev; | |
196 | } | |
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); | |
200 | ||
201 | /* continue until call or return */ | |
202 | ||
203 | if (!inst_call(ins) && | |
204 | !inst_return(ins) && | |
205 | !inst_trap_return(ins)) { | |
206 | if (db_step_again()) | |
207 | return (FALSE); /* continue */ | |
208 | } | |
209 | } | |
210 | if (db_find_breakpoint_here(space, pc)) | |
211 | return(FALSE); | |
212 | db_run_mode = STEP_NONE; | |
213 | return (TRUE); | |
214 | } | |
215 | ||
216 | void | |
217 | db_restart_at_pc( | |
218 | boolean_t watchpt, | |
219 | task_t task) | |
220 | { | |
221 | register db_addr_t pc = PC_REGS(DDB_REGS), brpc; | |
222 | ||
223 | if ((db_run_mode == STEP_COUNT) || | |
224 | (db_run_mode == STEP_RETURN) || | |
225 | (db_run_mode == STEP_CALLT)) { | |
226 | db_expr_t ins; | |
227 | ||
228 | /* | |
229 | * We are about to execute this instruction, | |
230 | * so count it now. | |
231 | */ | |
232 | ||
233 | ins = db_get_task_value(pc, sizeof(int), FALSE, task); | |
234 | db_inst_count++; | |
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); | |
243 | db_inst_count++; | |
244 | db_load_count += db_inst_load(ins); | |
245 | db_store_count += db_inst_store(ins); | |
246 | } | |
247 | #endif /* SOFTWARE_SSTEP */ | |
248 | } | |
249 | ||
250 | if (db_run_mode == STEP_CONTINUE) { | |
251 | if (watchpt || db_find_breakpoint_here(task, pc)) { | |
252 | /* | |
253 | * Step over breakpoint/watchpoint. | |
254 | */ | |
255 | db_run_mode = STEP_INVISIBLE; | |
256 | db_set_task_single_step(DDB_REGS, task); | |
257 | } else { | |
258 | db_set_breakpoints(); | |
259 | db_set_watchpoints(); | |
260 | } | |
261 | } else { | |
262 | db_set_task_single_step(DDB_REGS, task); | |
263 | } | |
264 | } | |
265 | ||
266 | /* | |
267 | * 'n' and 'u' commands might never return. | |
268 | * Limit the maximum number of steps. | |
269 | */ | |
270 | ||
271 | boolean_t | |
272 | db_step_again(void) | |
273 | { | |
274 | if (db_inst_count && !(db_inst_count%db_max_inst_count)) { | |
275 | char c; | |
276 | db_printf("%d instructions, continue ? (y/n) ", | |
277 | db_inst_count); | |
278 | c = cngetc(); | |
279 | db_printf("\n"); | |
280 | if(c == 'n') | |
281 | return(FALSE); | |
282 | } | |
283 | return(TRUE); | |
284 | } | |
285 | ||
286 | void | |
287 | db_single_step( | |
288 | db_regs_t *regs, | |
289 | task_t task) | |
290 | { | |
291 | if (db_run_mode == STEP_CONTINUE) { | |
292 | db_run_mode = STEP_INVISIBLE; | |
293 | db_set_task_single_step(regs, task); | |
294 | } | |
295 | } | |
296 | ||
297 | #ifdef SOFTWARE_SSTEP | |
298 | /* | |
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. | |
305 | * | |
306 | * extern boolean_t | |
307 | * inst_branch(), returns true if the instruction might branch | |
308 | * extern unsigned | |
309 | * branch_taken(), return the address the instruction might | |
310 | * branch to | |
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 | |
314 | * | |
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. | |
320 | * | |
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. | |
326 | */ | |
327 | db_breakpoint_t db_not_taken_bkpt = 0; | |
328 | db_breakpoint_t db_taken_bkpt = 0; | |
329 | ||
330 | db_breakpoint_t | |
331 | db_find_temp_breakpoint( | |
332 | task_t task, | |
333 | db_addr_t addr) | |
334 | { | |
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; | |
341 | return 0; | |
342 | } | |
343 | ||
344 | void | |
345 | db_set_task_single_step( | |
346 | register db_regs_t *regs, | |
347 | task_t task) | |
348 | { | |
349 | db_addr_t pc = PC_REGS(regs), brpc; | |
350 | register unsigned int inst; | |
351 | register boolean_t unconditional; | |
352 | ||
353 | /* | |
354 | * User was stopped at pc, e.g. the instruction | |
355 | * at pc was not executed. | |
356 | */ | |
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! */ | |
360 | ||
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); | |
364 | } else | |
365 | db_taken_bkpt = 0; | |
366 | pc = next_instr_address(pc,1,task); | |
367 | } else | |
368 | pc = next_instr_address(pc,0,task); | |
369 | ||
370 | /* | |
371 | * check if this control flow instruction is an | |
372 | * unconditional transfer | |
373 | */ | |
374 | ||
375 | unconditional = inst_unconditional_flow_transfer(inst); | |
376 | ||
377 | /* | |
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.) | |
385 | */ | |
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); | |
389 | } else | |
390 | db_not_taken_bkpt = 0; | |
391 | } | |
392 | ||
393 | void | |
394 | db_clear_task_single_step( | |
395 | db_regs_t *regs, | |
396 | task_t task) | |
397 | { | |
398 | if (db_taken_bkpt != 0) { | |
399 | db_delete_temp_breakpoint(task, db_taken_bkpt); | |
400 | db_taken_bkpt = 0; | |
401 | } | |
402 | if (db_not_taken_bkpt != 0) { | |
403 | db_delete_temp_breakpoint(task, db_not_taken_bkpt); | |
404 | db_not_taken_bkpt = 0; | |
405 | } | |
406 | } | |
407 | ||
408 | #endif /* SOFTWARE_SSTEP */ | |
409 | ||
410 | extern int db_cmd_loop_done; | |
411 | ||
412 | /* single-step */ | |
413 | void | |
414 | db_single_step_cmd( | |
415 | db_expr_t addr, | |
416 | int have_addr, | |
417 | db_expr_t count, | |
418 | char * modif) | |
419 | { | |
420 | boolean_t print = FALSE; | |
421 | ||
422 | if (count == -1) | |
423 | count = 1; | |
424 | ||
425 | if (modif[0] == 'p') | |
426 | print = TRUE; | |
427 | ||
428 | db_run_mode = STEP_ONCE; | |
429 | db_loop_count = count; | |
430 | db_sstep_print = print; | |
431 | db_inst_count = 0; | |
432 | db_last_inst_count = 0; | |
433 | db_load_count = 0; | |
434 | db_store_count = 0; | |
435 | ||
436 | db_cmd_loop_done = 1; | |
437 | } | |
438 | ||
439 | /* trace and print until call/return */ | |
440 | void | |
441 | db_trace_until_call_cmd( | |
442 | db_expr_t addr, | |
443 | int have_addr, | |
444 | db_expr_t count, | |
445 | char * modif) | |
446 | { | |
447 | boolean_t print = FALSE; | |
448 | ||
449 | if (modif[0] == 'p') | |
450 | print = TRUE; | |
451 | ||
452 | db_run_mode = STEP_CALLT; | |
453 | db_sstep_print = print; | |
454 | db_inst_count = 0; | |
455 | db_last_inst_count = 0; | |
456 | db_load_count = 0; | |
457 | db_store_count = 0; | |
458 | ||
459 | db_cmd_loop_done = 1; | |
460 | } | |
461 | ||
462 | void | |
463 | db_trace_until_matching_cmd( | |
464 | db_expr_t addr, | |
465 | int have_addr, | |
466 | db_expr_t count, | |
467 | char * modif) | |
468 | { | |
469 | boolean_t print = FALSE; | |
470 | ||
471 | if (modif[0] == 'p') | |
472 | print = TRUE; | |
473 | ||
474 | db_run_mode = STEP_RETURN; | |
475 | db_call_depth = 1; | |
476 | db_sstep_print = print; | |
477 | db_inst_count = 0; | |
478 | db_last_inst_count = 0; | |
479 | db_load_count = 0; | |
480 | db_store_count = 0; | |
481 | ||
482 | db_cmd_loop_done = 1; | |
483 | } | |
484 | ||
485 | /* continue */ | |
486 | void | |
487 | db_continue_cmd( | |
488 | db_expr_t addr, | |
489 | int have_addr, | |
490 | db_expr_t count, | |
491 | char * modif) | |
492 | { | |
493 | /* | |
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 | |
497 | * it.) | |
498 | */ | |
499 | #if 0 | |
500 | if (modif[0] == 'c') | |
501 | db_run_mode = STEP_COUNT; | |
502 | else | |
503 | db_run_mode = STEP_CONTINUE; | |
504 | #else | |
505 | db_run_mode = STEP_CONTINUE; | |
506 | #endif | |
507 | db_inst_count = 0; | |
508 | db_last_inst_count = 0; | |
509 | db_load_count = 0; | |
510 | db_store_count = 0; | |
511 | ||
512 | db_cmd_loop_done = 1; | |
513 | } | |
514 | ||
515 | /* gdb */ | |
516 | void | |
517 | db_continue_gdb( | |
518 | db_expr_t addr, | |
519 | int have_addr, | |
520 | db_expr_t count, | |
521 | char * modif) | |
522 | { | |
8ad349bb | 523 | #if defined(__ppc__) |
1c79356b | 524 | db_to_gdb(); |
8ad349bb | 525 | #endif |
1c79356b A |
526 | db_run_mode = STEP_CONTINUE; |
527 | db_inst_count = 0; | |
528 | db_last_inst_count = 0; | |
529 | db_load_count = 0; | |
530 | db_store_count = 0; | |
531 | ||
532 | db_cmd_loop_done = 1; | |
533 | } | |
534 | ||
535 | ||
8ad349bb | 536 | |
1c79356b A |
537 | boolean_t |
538 | db_in_single_step(void) | |
539 | { | |
540 | return(db_run_mode != STEP_NONE && db_run_mode != STEP_CONTINUE); | |
541 | } |