]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_run.c
xnu-792.10.96.tar.gz
[apple/xnu.git] / osfmk / ddb / db_run.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990 Carnegie Mellon University
28 * All Rights Reserved.
29 *
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.
35 *
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.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52 /*
53 * Author: David B. Golub, Carnegie Mellon University
54 * Date: 7/90
55 */
56
57 /*
58 * Commands to run process.
59 */
60 #include <mach/boolean.h>
61 #include <machine/db_machdep.h>
62
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>
73
74 #include <IOKit/IOPlatformExpert.h>
75
76 boolean_t db_sstep_print;
77 int db_loop_count;
78 int db_call_depth;
79
80 int db_inst_count;
81 int db_last_inst_count;
82 int db_load_count;
83 int db_store_count;
84 int db_max_inst_count = 1000;
85
86 #ifndef db_set_single_step
87 void db_set_task_single_step(
88 register db_regs_t *regs,
89 task_t task);
90 #else
91 #define db_set_task_single_step(regs,task) db_set_single_step(regs)
92 #endif
93 #ifndef db_clear_single_step
94 void db_clear_task_single_step(
95 db_regs_t *regs,
96 task_t task);
97 #else
98 #define db_clear_task_single_step(regs,task) db_clear_single_step(regs)
99 #endif
100
101 extern jmp_buf_t *db_recover;
102 boolean_t db_step_again(void);
103
104 boolean_t
105 db_stop_at_pc(
106 boolean_t *is_breakpoint,
107 task_t task,
108 task_t space)
109 {
110 register db_addr_t pc;
111 register db_thread_breakpoint_t bkpt;
112
113 db_clear_task_single_step(DDB_REGS, space);
114 db_clear_breakpoints();
115 db_clear_watchpoints();
116 pc = PC_REGS(DDB_REGS);
117
118 #ifdef FIXUP_PC_AFTER_BREAK
119 if (*is_breakpoint) {
120 /*
121 * Breakpoint trap. Fix up the PC if the
122 * machine requires it.
123 */
124 FIXUP_PC_AFTER_BREAK
125 pc = PC_REGS(DDB_REGS);
126 }
127 #endif
128
129 /*
130 * Now check for a breakpoint at this address.
131 */
132 bkpt = db_find_thread_breakpoint_here(space, pc);
133 if (bkpt) {
134 if (db_cond_check(bkpt)) {
135 *is_breakpoint = TRUE;
136 return (TRUE); /* stop here */
137 }
138 }
139 *is_breakpoint = FALSE;
140
141 if (db_run_mode == STEP_INVISIBLE) {
142 db_run_mode = STEP_CONTINUE;
143 return (FALSE); /* continue */
144 }
145 if (db_run_mode == STEP_COUNT) {
146 return (FALSE); /* continue */
147 }
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);
152 }
153 return (FALSE); /* continue */
154 }
155 }
156 if (db_run_mode == STEP_RETURN) {
157 jmp_buf_t *prev;
158 jmp_buf_t db_jmpbuf;
159 /* WARNING: the following assumes an instruction fits an int */
160 db_expr_t ins = db_get_task_value(pc, sizeof(int), FALSE, space);
161
162 /* continue until matching return */
163
164 prev = db_recover;
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)) {
170 register int i;
171
172 db_printf("[after %6d /%4d] ",
173 db_inst_count,
174 db_inst_count - db_last_inst_count);
175 db_last_inst_count = db_inst_count;
176 for (i = db_call_depth; --i > 0; )
177 db_printf(" ");
178 db_print_loc_and_inst(pc, task);
179 db_printf("\n");
180 }
181 }
182 if (inst_call(ins))
183 db_call_depth++;
184 db_recover = prev;
185 if (db_step_again())
186 return (FALSE); /* continue */
187 }
188 }
189 db_recover = prev;
190 }
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);
194
195 /* continue until call or return */
196
197 if (!inst_call(ins) &&
198 !inst_return(ins) &&
199 !inst_trap_return(ins)) {
200 if (db_step_again())
201 return (FALSE); /* continue */
202 }
203 }
204 if (db_find_breakpoint_here(space, pc))
205 return(FALSE);
206 db_run_mode = STEP_NONE;
207 return (TRUE);
208 }
209
210 void
211 db_restart_at_pc(
212 boolean_t watchpt,
213 task_t task)
214 {
215 register db_addr_t pc = PC_REGS(DDB_REGS), brpc;
216
217 if ((db_run_mode == STEP_COUNT) ||
218 (db_run_mode == STEP_RETURN) ||
219 (db_run_mode == STEP_CALLT)) {
220 db_expr_t ins;
221
222 /*
223 * We are about to execute this instruction,
224 * so count it now.
225 */
226
227 ins = db_get_task_value(pc, sizeof(int), FALSE, task);
228 db_inst_count++;
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);
237 db_inst_count++;
238 db_load_count += db_inst_load(ins);
239 db_store_count += db_inst_store(ins);
240 }
241 #endif /* SOFTWARE_SSTEP */
242 }
243
244 if (db_run_mode == STEP_CONTINUE) {
245 if (watchpt || db_find_breakpoint_here(task, pc)) {
246 /*
247 * Step over breakpoint/watchpoint.
248 */
249 db_run_mode = STEP_INVISIBLE;
250 db_set_task_single_step(DDB_REGS, task);
251 } else {
252 db_set_breakpoints();
253 db_set_watchpoints();
254 }
255 } else {
256 db_set_task_single_step(DDB_REGS, task);
257 }
258 }
259
260 /*
261 * 'n' and 'u' commands might never return.
262 * Limit the maximum number of steps.
263 */
264
265 boolean_t
266 db_step_again(void)
267 {
268 if (db_inst_count && !(db_inst_count%db_max_inst_count)) {
269 char c;
270 db_printf("%d instructions, continue ? (y/n) ",
271 db_inst_count);
272 c = cngetc();
273 db_printf("\n");
274 if(c == 'n')
275 return(FALSE);
276 }
277 return(TRUE);
278 }
279
280 void
281 db_single_step(
282 db_regs_t *regs,
283 task_t task)
284 {
285 if (db_run_mode == STEP_CONTINUE) {
286 db_run_mode = STEP_INVISIBLE;
287 db_set_task_single_step(regs, task);
288 }
289 }
290
291 #ifdef SOFTWARE_SSTEP
292 /*
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.
299 *
300 * extern boolean_t
301 * inst_branch(), returns true if the instruction might branch
302 * extern unsigned
303 * branch_taken(), return the address the instruction might
304 * branch to
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
308 *
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.
314 *
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.
320 */
321 db_breakpoint_t db_not_taken_bkpt = 0;
322 db_breakpoint_t db_taken_bkpt = 0;
323
324 db_breakpoint_t
325 db_find_temp_breakpoint(
326 task_t task,
327 db_addr_t addr)
328 {
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;
335 return 0;
336 }
337
338 void
339 db_set_task_single_step(
340 register db_regs_t *regs,
341 task_t task)
342 {
343 db_addr_t pc = PC_REGS(regs), brpc;
344 register unsigned int inst;
345 register boolean_t unconditional;
346
347 /*
348 * User was stopped at pc, e.g. the instruction
349 * at pc was not executed.
350 */
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! */
354
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);
358 } else
359 db_taken_bkpt = 0;
360 pc = next_instr_address(pc,1,task);
361 } else
362 pc = next_instr_address(pc,0,task);
363
364 /*
365 * check if this control flow instruction is an
366 * unconditional transfer
367 */
368
369 unconditional = inst_unconditional_flow_transfer(inst);
370
371 /*
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.)
379 */
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);
383 } else
384 db_not_taken_bkpt = 0;
385 }
386
387 void
388 db_clear_task_single_step(
389 db_regs_t *regs,
390 task_t task)
391 {
392 if (db_taken_bkpt != 0) {
393 db_delete_temp_breakpoint(task, db_taken_bkpt);
394 db_taken_bkpt = 0;
395 }
396 if (db_not_taken_bkpt != 0) {
397 db_delete_temp_breakpoint(task, db_not_taken_bkpt);
398 db_not_taken_bkpt = 0;
399 }
400 }
401
402 #endif /* SOFTWARE_SSTEP */
403
404 extern int db_cmd_loop_done;
405
406 /* single-step */
407 void
408 db_single_step_cmd(
409 db_expr_t addr,
410 int have_addr,
411 db_expr_t count,
412 char * modif)
413 {
414 boolean_t print = FALSE;
415
416 if (count == -1)
417 count = 1;
418
419 if (modif[0] == 'p')
420 print = TRUE;
421
422 db_run_mode = STEP_ONCE;
423 db_loop_count = count;
424 db_sstep_print = print;
425 db_inst_count = 0;
426 db_last_inst_count = 0;
427 db_load_count = 0;
428 db_store_count = 0;
429
430 db_cmd_loop_done = 1;
431 }
432
433 /* trace and print until call/return */
434 void
435 db_trace_until_call_cmd(
436 db_expr_t addr,
437 int have_addr,
438 db_expr_t count,
439 char * modif)
440 {
441 boolean_t print = FALSE;
442
443 if (modif[0] == 'p')
444 print = TRUE;
445
446 db_run_mode = STEP_CALLT;
447 db_sstep_print = print;
448 db_inst_count = 0;
449 db_last_inst_count = 0;
450 db_load_count = 0;
451 db_store_count = 0;
452
453 db_cmd_loop_done = 1;
454 }
455
456 void
457 db_trace_until_matching_cmd(
458 db_expr_t addr,
459 int have_addr,
460 db_expr_t count,
461 char * modif)
462 {
463 boolean_t print = FALSE;
464
465 if (modif[0] == 'p')
466 print = TRUE;
467
468 db_run_mode = STEP_RETURN;
469 db_call_depth = 1;
470 db_sstep_print = print;
471 db_inst_count = 0;
472 db_last_inst_count = 0;
473 db_load_count = 0;
474 db_store_count = 0;
475
476 db_cmd_loop_done = 1;
477 }
478
479 /* continue */
480 void
481 db_continue_cmd(
482 db_expr_t addr,
483 int have_addr,
484 db_expr_t count,
485 char * modif)
486 {
487 /*
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
491 * it.)
492 */
493 #if 0
494 if (modif[0] == 'c')
495 db_run_mode = STEP_COUNT;
496 else
497 db_run_mode = STEP_CONTINUE;
498 #else
499 db_run_mode = STEP_CONTINUE;
500 #endif
501 db_inst_count = 0;
502 db_last_inst_count = 0;
503 db_load_count = 0;
504 db_store_count = 0;
505
506 db_cmd_loop_done = 1;
507 }
508
509
510 /*
511 * Switch to gdb
512 */
513 void
514 db_to_gdb(
515 void)
516 {
517 extern unsigned int switch_debugger;
518
519 switch_debugger=1;
520 }
521
522 /* gdb */
523 void
524 db_continue_gdb(
525 db_expr_t addr,
526 int have_addr,
527 db_expr_t count,
528 char * modif)
529 {
530 db_to_gdb();
531 db_run_mode = STEP_CONTINUE;
532 db_inst_count = 0;
533 db_last_inst_count = 0;
534 db_load_count = 0;
535 db_store_count = 0;
536
537 db_cmd_loop_done = 1;
538 }
539
540
541 boolean_t
542 db_in_single_step(void)
543 {
544 return(db_run_mode != STEP_NONE && db_run_mode != STEP_CONTINUE);
545 }
546