]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/db_trace.c
xnu-1699.26.8.tar.gz
[apple/xnu.git] / osfmk / i386 / db_trace.c
1 /*
2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58
59 #include <string.h>
60
61 #include <mach/boolean.h>
62 #include <vm/vm_map.h>
63 #include <kern/thread.h>
64 #include <kern/task.h>
65
66 #include <machine/asm.h>
67 #include <machine/db_machdep.h>
68 #include <machine/setjmp.h>
69 #include <mach/machine.h>
70 #include <mach/kmod.h>
71
72 #include <i386/mp.h>
73 #include <i386/pio.h>
74 #include <i386/cpuid.h>
75 #include <i386/proc_reg.h>
76 #include <i386/machine_routines.h>
77
78 #include <ddb/db_access.h>
79 #include <ddb/db_sym.h>
80 #include <ddb/db_variables.h>
81 #include <ddb/db_command.h>
82 #include <ddb/db_task_thread.h>
83 #include <ddb/db_output.h>
84
85 extern jmp_buf_t *db_recover;
86 struct x86_kernel_state ddb_null_kregs;
87 extern kmod_info_t *kmod;
88
89
90 /*
91 * Stack trace.
92 */
93
94 #define INKERNELSTACK(va, th) 1
95
96 #define DB_NUMARGS_MAX 5
97
98 struct i386_frame {
99 struct i386_frame *f_frame;
100 int f_retaddr;
101 int f_arg0;
102 };
103
104 #define TRAP 1
105 #define INTERRUPT 2
106 #define SYSCALL 3
107
108 db_addr_t db_user_trap_symbol_value = 0;
109 db_addr_t db_kernel_trap_symbol_value = 0;
110 db_addr_t db_interrupt_symbol_value = 0;
111 db_addr_t db_return_to_iret_symbol_value = 0;
112 db_addr_t db_syscall_symbol_value = 0;
113 boolean_t db_trace_symbols_found = FALSE;
114
115 struct i386_kregs {
116 char *name;
117 unsigned int offset;
118 } i386_kregs[] = {
119 { "ebx", (unsigned int)(&((struct x86_kernel_state *)0)->k_ebx) },
120 { "esp", (unsigned int)(&((struct x86_kernel_state *)0)->k_esp) },
121 { "ebp", (unsigned int)(&((struct x86_kernel_state *)0)->k_ebp) },
122 { "edi", (unsigned int)(&((struct x86_kernel_state *)0)->k_edi) },
123 { "esi", (unsigned int)(&((struct x86_kernel_state *)0)->k_esi) },
124 { "eip", (unsigned int)(&((struct x86_kernel_state *)0)->k_eip) },
125 { 0 }
126 };
127
128 /* Forward */
129
130 extern unsigned int * db_lookup_i386_kreg(
131 char *name,
132 int *kregp);
133 extern int db_i386_reg_value(
134 struct db_variable * vp,
135 db_expr_t * val,
136 int flag,
137 db_var_aux_param_t ap);
138 extern void db_find_trace_symbols(void);
139 extern int db_numargs(
140 struct i386_frame *fp,
141 task_t task);
142 extern void db_nextframe(
143 struct i386_frame **lfp,
144 struct i386_frame **fp,
145 db_addr_t *ip,
146 int frame_type,
147 thread_t thr_act);
148 extern int _setjmp(
149 jmp_buf_t * jb);
150
151 /*
152 * Machine register set.
153 */
154 struct db_variable db_regs[] = {
155 { "cs", (unsigned int *)&ddb_regs.cs, db_i386_reg_value, 0, 0, 0, 0, TRUE, 0, 0, (int *)0, 0 },
156 { "ds", (unsigned int *)&ddb_regs.ds, db_i386_reg_value, 0, 0, 0, 0, TRUE, 0, 0, (int *)0, 0 },
157 { "es", (unsigned int *)&ddb_regs.es, db_i386_reg_value, 0, 0, 0, 0, TRUE, 0, 0, (int *)0, 0 },
158 { "fs", (unsigned int *)&ddb_regs.fs, db_i386_reg_value, 0, 0, 0, 0, TRUE, 0, 0, (int *)0, 0 },
159 { "gs", (unsigned int *)&ddb_regs.gs, db_i386_reg_value, 0, 0, 0, 0, TRUE, 0, 0, (int *)0, 0 },
160 { "ss", (unsigned int *)&ddb_regs.ss, db_i386_reg_value, 0, 0, 0, 0, TRUE, 0, 0, (int *)0, 0 },
161 { "eax",(unsigned int *)&ddb_regs.eax, db_i386_reg_value, 0, 0, 0, 0, TRUE, 0, 0, (int *)0, 0 },
162 { "ecx",(unsigned int *)&ddb_regs.ecx, db_i386_reg_value, 0, 0, 0, 0, TRUE, 0, 0, (int *)0, 0 },
163 { "edx",(unsigned int *)&ddb_regs.edx, db_i386_reg_value, 0, 0, 0, 0, TRUE, 0, 0, (int *)0, 0 },
164 { "ebx",(unsigned int *)&ddb_regs.ebx, db_i386_reg_value, 0, 0, 0, 0, TRUE, 0, 0, (int *)0, 0 },
165 { "esp",(unsigned int *)&ddb_regs.uesp,db_i386_reg_value, 0, 0, 0, 0, TRUE, 0, 0, (int *)0, 0 },
166 { "ebp",(unsigned int *)&ddb_regs.ebp, db_i386_reg_value, 0, 0, 0, 0, TRUE, 0, 0, (int *)0, 0 },
167 { "esi",(unsigned int *)&ddb_regs.esi, db_i386_reg_value, 0, 0, 0, 0, TRUE, 0, 0, (int *)0, 0 },
168 { "edi",(unsigned int *)&ddb_regs.edi, db_i386_reg_value, 0, 0, 0, 0, TRUE, 0, 0, (int *)0, 0 },
169 { "eip",(unsigned int *)&ddb_regs.eip, db_i386_reg_value, 0, 0, 0, 0, TRUE, 0, 0, (int *)0, 0 },
170 { "efl",(unsigned int *)&ddb_regs.efl, db_i386_reg_value, 0, 0, 0, 0, TRUE, 0, 0, (int *)0, 0 }
171 };
172 struct db_variable *db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
173
174 unsigned int *
175 db_lookup_i386_kreg(
176 char *name,
177 int *kregp)
178 {
179 register struct i386_kregs *kp;
180
181 for (kp = i386_kregs; kp->name; kp++) {
182 if (strcmp(name, kp->name) == 0)
183 return((unsigned int *)((int)kregp + kp->offset));
184 }
185 return(0);
186 }
187
188 int
189 db_i386_reg_value(
190 struct db_variable *vp,
191 db_expr_t *valuep,
192 int flag,
193 db_var_aux_param_t ap)
194 {
195 extern char etext;
196 unsigned int *dp = 0;
197 db_expr_t null_reg = 0;
198 register thread_t thr_act = ap->thr_act;
199
200 if (db_option(ap->modif, 'u')) {
201 if (thr_act == THREAD_NULL) {
202 if ((thr_act = current_thread()) == THREAD_NULL)
203 db_error("no user registers\n");
204 }
205 if (thr_act == current_thread()) {
206 if (IS_USER_TRAP(&ddb_regs, &etext))
207 dp = vp->valuep;
208 }
209 } else {
210 if (thr_act == THREAD_NULL || thr_act == current_thread()) {
211 dp = vp->valuep;
212 } else {
213 if (thr_act &&
214 (thr_act->continuation != THREAD_CONTINUE_NULL) &&
215 thr_act->kernel_stack) {
216 int cpu;
217
218 for (cpu = 0; cpu < real_ncpus; cpu++) {
219 if (cpu_datap(cpu)->cpu_running == TRUE &&
220 cpu_datap(cpu)->cpu_active_thread == thr_act && cpu_datap(cpu)->cpu_kdb_saved_state) {
221 dp = (unsigned int *) (((unsigned int)cpu_datap(cpu)->cpu_kdb_saved_state) +
222 (((unsigned int) vp->valuep) -
223 (unsigned int) &ddb_regs));
224 break;
225 }
226 }
227 if (dp == 0 && thr_act)
228 dp = db_lookup_i386_kreg(vp->name,
229 (unsigned int *)(STACK_IKS(thr_act->kernel_stack)));
230 if (dp == 0)
231 dp = &null_reg;
232 } else if (thr_act &&
233 (thr_act->continuation != THREAD_CONTINUE_NULL)) {
234 /* only EIP is valid */
235 if (vp->valuep == (unsigned int *) &ddb_regs.eip) {
236 dp = (unsigned int *)(&thr_act->continuation);
237 } else {
238 dp = &null_reg;
239 }
240 }
241 }
242 }
243 if (dp == 0) {
244 int cpu;
245
246 if (!db_option(ap->modif, 'u')) {
247 for (cpu = 0; cpu < real_ncpus; cpu++) {
248 if (cpu_datap(cpu)->cpu_running == TRUE &&
249 cpu_datap(cpu)->cpu_active_thread == thr_act && cpu_datap(cpu)->cpu_kdb_saved_state) {
250 dp = (unsigned int *) (((unsigned int)cpu_datap(cpu)->cpu_kdb_saved_state) +
251 (((unsigned int) vp->valuep) -
252 (unsigned int) &ddb_regs));
253 break;
254 }
255 }
256 }
257 if (dp == 0) {
258 if (!thr_act)
259 db_error("no pcb\n");
260 dp = (unsigned int *)((unsigned int)(thr_act->machine.iss) +
261 ((unsigned int)vp->valuep - (unsigned int)&ddb_regs));
262 }
263 }
264 if (flag == DB_VAR_SET)
265 *dp = *valuep;
266 else
267 *valuep = *dp;
268 return(0);
269 }
270
271 void
272 db_find_trace_symbols(void)
273 {
274 db_expr_t value;
275 boolean_t found_some;
276
277 found_some = FALSE;
278 if (db_value_of_name(CC_SYM_PREFIX "user_trap", &value)) {
279 db_user_trap_symbol_value = (db_addr_t) value;
280 found_some = TRUE;
281 }
282 if (db_value_of_name(CC_SYM_PREFIX "kernel_trap", &value)) {
283 db_kernel_trap_symbol_value = (db_addr_t) value;
284 found_some = TRUE;
285 }
286 if (db_value_of_name(CC_SYM_PREFIX "interrupt", &value)) {
287 db_interrupt_symbol_value = (db_addr_t) value;
288 found_some = TRUE;
289 }
290 if (db_value_of_name(CC_SYM_PREFIX "return_to_iret", &value)) {
291 db_return_to_iret_symbol_value = (db_addr_t) value;
292 found_some = TRUE;
293 }
294 if (db_value_of_name(CC_SYM_PREFIX "syscall", &value)) {
295 db_syscall_symbol_value = (db_addr_t) value;
296 found_some = TRUE;
297 }
298 if (found_some)
299 db_trace_symbols_found = TRUE;
300 }
301
302 /*
303 * Figure out how many arguments were passed into the frame at "fp".
304 */
305 int db_numargs_default = 5;
306
307 int
308 db_numargs(
309 struct i386_frame *fp,
310 task_t task)
311 {
312 int *argp;
313 int inst;
314 int args;
315 extern char etext;
316
317 argp = (int *)db_get_task_value((int)&fp->f_retaddr, 4, FALSE, task);
318 if (argp < (int *)VM_MIN_KERNEL_ADDRESS || (char *)argp > &etext)
319 args = db_numargs_default;
320 else if (!DB_CHECK_ACCESS((int)argp, 4, task))
321 args = db_numargs_default;
322 else {
323 inst = db_get_task_value((int)argp, 4, FALSE, task);
324 if ((inst & 0xff) == 0x59) /* popl %ecx */
325 args = 1;
326 else if ((inst & 0xffff) == 0xc483) /* addl %n, %esp */
327 args = ((inst >> 16) & 0xff) / 4;
328 else
329 args = db_numargs_default;
330 }
331 return (args);
332 }
333
334 struct interrupt_frame {
335 struct i386_frame *if_frame; /* point to next frame */
336 int if_retaddr; /* return address to _interrupt */
337 int if_unit; /* unit number */
338 int if_spl; /* saved spl */
339 int if_iretaddr; /* _return_to_{iret,iret_i} */
340 int if_edx; /* old sp(iret) or saved edx(iret_i) */
341 int if_ecx; /* saved ecx(iret_i) */
342 int if_eax; /* saved eax(iret_i) */
343 int if_eip; /* saved eip(iret_i) */
344 int if_cs; /* saved cs(iret_i) */
345 int if_efl; /* saved efl(iret_i) */
346 };
347
348 extern const char *trap_type[];
349 extern int TRAP_TYPES;
350
351 /*
352 * Figure out the next frame up in the call stack.
353 * For trap(), we print the address of the faulting instruction and
354 * proceed with the calling frame. We return the ip that faulted.
355 * If the trap was caused by jumping through a bogus pointer, then
356 * the next line in the backtrace will list some random function as
357 * being called. It should get the argument list correct, though.
358 * It might be possible to dig out from the next frame up the name
359 * of the function that faulted, but that could get hairy.
360 */
361 void
362 db_nextframe(
363 struct i386_frame **lfp, /* in/out */
364 struct i386_frame **fp, /* in/out */
365 db_addr_t *ip, /* out */
366 int frame_type, /* in */
367 thread_t thr_act) /* in */
368 {
369 x86_saved_state32_t *iss32;
370 struct interrupt_frame *ifp;
371 task_t task = (thr_act != THREAD_NULL)? thr_act->task: TASK_NULL;
372
373 switch(frame_type) {
374 case TRAP:
375 /*
376 * We know that trap() has 1 argument and we know that
377 * it is an (strcut x86_saved_state32_t *).
378 */
379 iss32 = (x86_saved_state32_t *)
380 db_get_task_value((int)&((*fp)->f_arg0),4,FALSE,task);
381
382 if (iss32->trapno >= 0 && iss32->trapno < TRAP_TYPES) {
383 db_printf(">>>>> %s trap at ",
384 trap_type[iss32->trapno]);
385 } else {
386 db_printf(">>>>> trap (number %d) at ",
387 iss32->trapno & 0xffff);
388 }
389 db_task_printsym(iss32->eip, DB_STGY_PROC, task);
390 db_printf(" <<<<<\n");
391 *fp = (struct i386_frame *)iss32->ebp;
392 *ip = (db_addr_t)iss32->eip;
393 break;
394
395 case INTERRUPT:
396 if (*lfp == 0) {
397 db_printf(">>>>> interrupt <<<<<\n");
398 goto miss_frame;
399 }
400 db_printf(">>>>> interrupt at ");
401 ifp = (struct interrupt_frame *)(*lfp);
402 *fp = ifp->if_frame;
403 if (ifp->if_iretaddr == db_return_to_iret_symbol_value) {
404 *ip = ((x86_saved_state32_t *)ifp->if_edx)->eip;
405 } else
406 *ip = (db_addr_t)ifp->if_eip;
407 db_task_printsym(*ip, DB_STGY_PROC, task);
408 db_printf(" <<<<<\n");
409 break;
410
411 case SYSCALL:
412 if (thr_act != THREAD_NULL) {
413 iss32 = (x86_saved_state32_t *)thr_act->machine.iss;
414
415 *ip = (db_addr_t)(iss32->eip);
416 *fp = (struct i386_frame *)(iss32->ebp);
417 }
418 break;
419
420 default: /* falling down for unknown case */
421 miss_frame:
422 *ip = (db_addr_t)
423 db_get_task_value((int)&(*fp)->f_retaddr, 4, FALSE, task);
424 *lfp = *fp;
425 *fp = (struct i386_frame *)
426 db_get_task_value((int)&(*fp)->f_frame, 4, FALSE, task);
427 break;
428 }
429 }
430
431 void
432 db_stack_trace_cmd(
433 db_expr_t addr,
434 boolean_t have_addr,
435 db_expr_t count,
436 char *modif)
437 {
438 struct i386_frame *frame, *lastframe;
439 x86_saved_state32_t *iss32;
440 int *argp;
441 db_addr_t callpc, lastcallpc;
442 int frame_type;
443 boolean_t kernel_only = TRUE;
444 boolean_t trace_thread = FALSE;
445 boolean_t trace_all_threads = FALSE;
446 int thcount = 0;
447 char *filename;
448 int linenum;
449 task_t task;
450 thread_t th, top_act;
451 int user_frame;
452 int frame_count;
453 jmp_buf_t *prev;
454 jmp_buf_t db_jmp_buf;
455 queue_entry_t act_list;
456
457 if (!db_trace_symbols_found)
458 db_find_trace_symbols();
459
460 {
461 register char *cp = modif;
462 register char c;
463
464 while ((c = *cp++) != 0) {
465 if (c == 't')
466 trace_thread = TRUE;
467 if (c == 'T') {
468 trace_all_threads = TRUE;
469 trace_thread = TRUE;
470 }
471 if (c == 'u')
472 kernel_only = FALSE;
473 }
474 }
475
476 if (trace_all_threads) {
477 if (!have_addr && !trace_thread) {
478 have_addr = TRUE;
479 trace_thread = TRUE;
480 act_list = &(current_task()->threads);
481 addr = (db_expr_t) queue_first(act_list);
482 } else if (trace_thread) {
483 if (have_addr) {
484 if (!db_check_act_address_valid((thread_t)addr)) {
485 if (db_lookup_task((task_t)addr) == -1)
486 return;
487 act_list = &(((task_t)addr)->threads);
488 addr = (db_expr_t) queue_first(act_list);
489 } else {
490 act_list = &(((thread_t)addr)->task->threads);
491 thcount = db_lookup_task_act(((thread_t)addr)->task,
492 (thread_t)addr);
493 }
494 } else {
495 th = db_default_act;
496 if (th == THREAD_NULL)
497 th = current_thread();
498 if (th == THREAD_NULL) {
499 db_printf("no active thr_act\n");
500 return;
501 }
502 have_addr = TRUE;
503 act_list = &th->task->threads;
504 addr = (db_expr_t) queue_first(act_list);
505 }
506 }
507 }
508
509 if (count == -1)
510 count = 65535;
511
512 next_thread:
513 top_act = THREAD_NULL;
514
515 user_frame = 0;
516 frame_count = count;
517
518 if (!have_addr && !trace_thread) {
519 frame = (struct i386_frame *)ddb_regs.ebp;
520 callpc = (db_addr_t)ddb_regs.eip;
521 th = current_thread();
522 task = (th != THREAD_NULL)? th->task: TASK_NULL;
523 db_printf("thread 0x%x, current_thread() is 0x%x, ebp is 0x%x, eip is 0x%x\n", th, current_thread(), ddb_regs.ebp, ddb_regs.eip);
524 } else if (trace_thread) {
525 if (have_addr) {
526 th = (thread_t) addr;
527 if (!db_check_act_address_valid(th)) {
528 return;
529 }
530 } else {
531 th = db_default_act;
532 if (th == THREAD_NULL)
533 th = current_thread();
534 if (th == THREAD_NULL) {
535 db_printf("no active thread\n");
536 return;
537 }
538 }
539 if (trace_all_threads)
540 db_printf("---------- Thread 0x%x (#%d of %d) ----------\n",
541 addr, thcount, th->task->thread_count);
542
543 next_activation:
544 user_frame = 0;
545 // kprintf("th is %x, current_thread() is %x, ddb_regs.ebp is %x ddb_regs.eip is %x\n", th, current_thread(), ddb_regs.ebp, ddb_regs.eip);
546 task = th->task;
547 if (th == current_thread()) {
548 frame = (struct i386_frame *)ddb_regs.ebp;
549 callpc = (db_addr_t)ddb_regs.eip;
550 } else {
551 if (!th) {
552 db_printf("thread has no shuttle\n");
553
554 goto thread_done;
555 }
556 else if ( (th->continuation != THREAD_CONTINUE_NULL) ||
557 th->kernel_stack == 0) {
558
559 db_printf("Continuation ");
560 db_task_printsym((db_expr_t)th->continuation,
561 DB_STGY_PROC, task);
562 db_printf("\n");
563
564 iss32 = (x86_saved_state32_t *)th->machine.iss;
565
566 frame = (struct i386_frame *) (iss32->ebp);
567 callpc = (db_addr_t) (iss32->eip);
568
569 } else {
570 int cpu;
571
572 for (cpu = 0; cpu < real_ncpus; cpu++) {
573 if (cpu_datap(cpu)->cpu_running == TRUE &&
574 cpu_datap(cpu)->cpu_active_thread == th &&
575 cpu_datap(cpu)->cpu_kdb_saved_state) {
576 break;
577 }
578 }
579 if (top_act != THREAD_NULL) {
580 /*
581 * Trying to get the backtrace of an activation
582 * which is not the top_most one in the RPC chain:
583 * use the activation's pcb.
584 */
585 iss32 = (x86_saved_state32_t *)th->machine.iss;
586
587 frame = (struct i386_frame *) (iss32->ebp);
588 callpc = (db_addr_t) (iss32->eip);
589 } else {
590 if (cpu == real_ncpus) {
591 register struct x86_kernel_state *iks;
592 int r;
593
594 iks = STACK_IKS(th->kernel_stack);
595 prev = db_recover;
596 if ((r = _setjmp(db_recover = &db_jmp_buf)) == 0) {
597 frame = (struct i386_frame *) (iks->k_ebp);
598 callpc = (db_addr_t) (iks->k_eip);
599 } else {
600 /*
601 * The kernel stack has probably been
602 * paged out (swapped out activation).
603 */
604 db_recover = prev;
605 if (r == 2) /* 'q' from db_more() */
606 db_error(0);
607 db_printf("<kernel stack (0x%x) error "
608 "(probably swapped out)>\n",
609 iks);
610 goto thread_done;
611 }
612 db_recover = prev;
613 } else {
614 db_printf(">>>>> active on cpu %d <<<<<\n",
615 cpu);
616
617 iss32 = (x86_saved_state32_t *)cpu_datap(cpu)->cpu_kdb_saved_state;
618
619 frame = (struct i386_frame *) (iss32->ebp);
620 callpc = (db_addr_t) (iss32->eip);
621 }
622 }
623 }
624 }
625 } else {
626 frame = (struct i386_frame *)addr;
627 th = (db_default_act)? db_default_act: current_thread();
628 task = (th != THREAD_NULL)? th->task: TASK_NULL;
629 callpc = (db_addr_t)db_get_task_value((int)&frame->f_retaddr,
630 4,
631 FALSE,
632 (user_frame) ? task : 0);
633 }
634
635 if (!INKERNELSTACK((unsigned)frame, th)) {
636 db_printf(">>>>> user space <<<<<\n");
637 if (kernel_only)
638 goto thread_done;
639 user_frame++;
640 }
641
642 lastframe = 0;
643 lastcallpc = (db_addr_t) 0;
644 while (frame_count-- && frame != 0) {
645 int narg = DB_NUMARGS_MAX;
646 char * name;
647 db_expr_t offset;
648 db_addr_t call_func = 0;
649 int r;
650 db_addr_t off;
651
652 db_symbol_values(NULL,
653 db_search_task_symbol_and_line(
654 callpc,
655 DB_STGY_XTRN,
656 &offset,
657 &filename,
658 &linenum,
659 (user_frame) ? task : 0,
660 &narg),
661 &name, (db_expr_t *)&call_func);
662 if ( name == NULL) {
663 db_find_task_sym_and_offset(callpc,
664 &name, &off, (user_frame) ? task : 0);
665 offset = (db_expr_t) off;
666 }
667
668 if (user_frame == 0) {
669 if (call_func && call_func == db_user_trap_symbol_value ||
670 call_func == db_kernel_trap_symbol_value) {
671 frame_type = TRAP;
672 narg = 1;
673 } else if (call_func &&
674 call_func == db_interrupt_symbol_value) {
675 frame_type = INTERRUPT;
676 goto next_frame;
677 } else if (call_func && call_func == db_syscall_symbol_value) {
678 frame_type = SYSCALL;
679 goto next_frame;
680 } else {
681 frame_type = 0;
682 prev = db_recover;
683 if ((r = _setjmp(db_recover = &db_jmp_buf)) == 0) {
684 if (narg < 0)
685 narg = db_numargs(frame,
686 (user_frame) ? task : 0);
687 db_recover = prev;
688 } else {
689 db_recover = prev;
690 goto thread_done;
691 }
692 }
693 } else {
694 frame_type = 0;
695 prev = db_recover;
696 if ((r = _setjmp(db_recover = &db_jmp_buf)) == 0) {
697 if (narg < 0)
698 narg = db_numargs(frame,
699 (user_frame) ? task : 0);
700 db_recover = prev;
701 } else {
702 db_recover = prev;
703 goto thread_done;
704 }
705 }
706
707 if (name == 0 || offset > db_maxoff) {
708 db_printf("0x%x 0x%x(", frame, callpc);
709 offset = 0;
710 } else
711 db_printf("0x%x %s(", frame, name);
712
713 argp = &frame->f_arg0;
714 while (narg > 0) {
715 int value;
716
717 prev = db_recover;
718 if ((r = _setjmp(db_recover = &db_jmp_buf)) == 0) {
719 value = db_get_task_value((int)argp,
720 4,
721 FALSE,
722 (user_frame) ? task : 0);
723 } else {
724 db_recover = prev;
725 if (r == 2) /* 'q' from db_more() */
726 db_error(0);
727 db_printf("... <stack error>)");
728 if (offset)
729 db_printf("+%x", offset);
730 if (filename) {
731 db_printf(" [%s", filename);
732 if (linenum > 0)
733 db_printf(":%d", linenum);
734 db_printf("]");
735 }
736 db_printf("\n");
737 goto thread_done;
738 }
739 db_recover = prev;
740 db_printf("%x", value);
741 argp++;
742 if (--narg != 0)
743 db_printf(",");
744 }
745 if (narg < 0)
746 db_printf("...");
747 db_printf(")");
748 if (offset) {
749 db_printf("+%x", offset);
750 }
751 if (filename) {
752 db_printf(" [%s", filename);
753 if (linenum > 0)
754 db_printf(":%d", linenum);
755 db_printf("]");
756 }
757 db_printf("\n");
758
759 next_frame:
760 lastcallpc = callpc;
761 db_nextframe(&lastframe, &frame, &callpc, frame_type,
762 (user_frame) ? th : THREAD_NULL);
763
764 if (frame == 0) {
765 if (th->task_threads.prev != THREAD_NULL) {
766 if (top_act == THREAD_NULL)
767 top_act = th;
768 th = th->task_threads.prev;
769 db_printf(">>>>> next activation 0x%x ($task%d.%d) <<<<<\n",
770 th,
771 db_lookup_task(th->task),
772 db_lookup_task_act(th->task, th));
773 goto next_activation;
774 }
775 /* end of chain */
776 break;
777 }
778 if (!INKERNELSTACK(lastframe, th) ||
779 !INKERNELSTACK((unsigned)frame, th))
780 user_frame++;
781 if (user_frame == 1) {
782 db_printf(">>>>> user space <<<<<\n");
783 if (kernel_only)
784 break;
785 }
786 if (frame <= lastframe) {
787 if ((INKERNELSTACK(lastframe, th) &&
788 !INKERNELSTACK(frame, th)))
789 continue;
790 db_printf("Bad frame pointer: 0x%x\n", frame);
791 break;
792 }
793 }
794
795 thread_done:
796 if (trace_all_threads) {
797 if (top_act != THREAD_NULL)
798 th = top_act;
799 th = (thread_t) queue_next(&th->task_threads);
800 if (! queue_end(act_list, (queue_entry_t) th)) {
801 db_printf("\n");
802 addr = (db_expr_t) th;
803 thcount++;
804 goto next_thread;
805
806 }
807 }
808 }
809
810 extern mach_vm_size_t kdp_machine_vm_read(mach_vm_address_t, caddr_t, mach_vm_size_t);
811 extern boolean_t kdp_trans_off;
812 /*
813 * Print out 256 bytes of real storage
814 *
815 * dr [entaddr]
816 */
817 void
818 db_display_real(db_expr_t addr, boolean_t have_addr, db_expr_t count,
819 char *modif)
820 {
821 int i;
822 unsigned int xbuf[8];
823 unsigned read_result = 0;
824 /* Print 256 bytes */
825 for(i=0; i<8; i++) {
826
827 /*
828 * Do a physical read using kdp_machine_vm_read(), rather than replicating the same
829 * facility
830 */
831 kdp_trans_off = 1;
832 read_result = kdp_machine_vm_read(addr, &xbuf[0], 32);
833 kdp_trans_off = 0;
834
835 if (read_result != 32)
836 db_printf("Unable to read address\n");
837 else
838 db_printf("%016llX %08X %08X %08X %08X %08X %08X %08X %08X\n", addr, /* Print a line */
839 xbuf[0], xbuf[1], xbuf[2], xbuf[3],
840 xbuf[4], xbuf[5], xbuf[6], xbuf[7]);
841 addr = addr + 0x00000020; /* Point to next address */
842 }
843 db_next = addr;
844 }
845
846 /*
847 * Displays all of the kmods in the system.
848 *
849 * dk
850 */
851 void
852 db_display_kmod(__unused db_expr_t addr, __unused boolean_t have_addr,
853 __unused db_expr_t count, __unused char *modif)
854 {
855
856 kmod_info_t *kmd;
857 unsigned int strt, end;
858
859 kmd = kmod; /* Start at the start */
860
861 db_printf("info addr start - end name ver\n");
862
863 while (kmd) { /* Dump 'em all */
864 strt = (unsigned int) kmd->address + kmd->hdr_size;
865 end = (unsigned int) kmd->address + kmd->size;
866 db_printf("%08X %08X %08X - %08X: %s, %s\n",
867 kmd, kmd->address, strt, end, kmd->name, kmd->version);
868 kmd = kmd->next;
869 }
870 }
871
872 void
873 db_display_iokit(__unused db_expr_t addr, __unused boolean_t have_addr,
874 __unused db_expr_t count, __unused char *modif)
875 {
876 }