1 /*-
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: releng/6.2/sys/amd64/amd64/db_trace.c 156616 2006-03-13 03:03:51Z jeff $");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kdb.h>
33 #include <sys/proc.h>
34 #include <sys/stack.h>
35 #include <sys/sysent.h>
36
37 #include <machine/cpu.h>
38 #include <machine/md_var.h>
39 #include <machine/pcb.h>
40 #include <machine/reg.h>
41
42 #include <vm/vm.h>
43 #include <vm/vm_param.h>
44 #include <vm/pmap.h>
45
46 #include <ddb/ddb.h>
47 #include <ddb/db_access.h>
48 #include <ddb/db_sym.h>
49 #include <ddb/db_variables.h>
50
51 static db_varfcn_t db_dr0;
52 static db_varfcn_t db_dr1;
53 static db_varfcn_t db_dr2;
54 static db_varfcn_t db_dr3;
55 static db_varfcn_t db_dr4;
56 static db_varfcn_t db_dr5;
57 static db_varfcn_t db_dr6;
58 static db_varfcn_t db_dr7;
59 static db_varfcn_t db_frame;
60 static db_varfcn_t db_rsp;
61 static db_varfcn_t db_ss;
62
63 /*
64 * Machine register set.
65 */
66 #define DB_OFFSET(x) (db_expr_t *)offsetof(struct trapframe, x)
67 struct db_variable db_regs[] = {
68 { "cs", DB_OFFSET(tf_cs), db_frame },
69 #if 0
70 { "ds", DB_OFFSET(tf_ds), db_frame },
71 { "es", DB_OFFSET(tf_es), db_frame },
72 { "fs", DB_OFFSET(tf_fs), db_frame },
73 { "gs", DB_OFFSET(tf_gs), db_frame },
74 #endif
75 { "ss", NULL, db_ss },
76 { "rax", DB_OFFSET(tf_rax), db_frame },
77 { "rcx", DB_OFFSET(tf_rcx), db_frame },
78 { "rdx", DB_OFFSET(tf_rdx), db_frame },
79 { "rbx", DB_OFFSET(tf_rbx), db_frame },
80 { "rsp", NULL, db_rsp },
81 { "rbp", DB_OFFSET(tf_rbp), db_frame },
82 { "rsi", DB_OFFSET(tf_rsi), db_frame },
83 { "rdi", DB_OFFSET(tf_rdi), db_frame },
84 { "r8", DB_OFFSET(tf_r8), db_frame },
85 { "r9", DB_OFFSET(tf_r9), db_frame },
86 { "r10", DB_OFFSET(tf_r10), db_frame },
87 { "r11", DB_OFFSET(tf_r11), db_frame },
88 { "r12", DB_OFFSET(tf_r12), db_frame },
89 { "r13", DB_OFFSET(tf_r13), db_frame },
90 { "r14", DB_OFFSET(tf_r14), db_frame },
91 { "r15", DB_OFFSET(tf_r15), db_frame },
92 { "rip", DB_OFFSET(tf_rip), db_frame },
93 { "rflags", DB_OFFSET(tf_rflags), db_frame },
94 { "dr0", NULL, db_dr0 },
95 { "dr1", NULL, db_dr1 },
96 { "dr2", NULL, db_dr2 },
97 { "dr3", NULL, db_dr3 },
98 { "dr4", NULL, db_dr4 },
99 { "dr5", NULL, db_dr5 },
100 { "dr6", NULL, db_dr6 },
101 { "dr7", NULL, db_dr7 },
102 };
103 struct db_variable *db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
104
105 #define DB_DRX_FUNC(reg) \
106 static int \
107 db_ ## reg (vp, valuep, op) \
108 struct db_variable *vp; \
109 db_expr_t * valuep; \
110 int op; \
111 { \
112 if (op == DB_VAR_GET) \
113 *valuep = r ## reg (); \
114 else \
115 load_ ## reg (*valuep); \
116 return (1); \
117 }
118
119 DB_DRX_FUNC(dr0)
120 DB_DRX_FUNC(dr1)
121 DB_DRX_FUNC(dr2)
122 DB_DRX_FUNC(dr3)
123 DB_DRX_FUNC(dr4)
124 DB_DRX_FUNC(dr5)
125 DB_DRX_FUNC(dr6)
126 DB_DRX_FUNC(dr7)
127
128 static __inline long
129 get_rsp(struct trapframe *tf)
130 {
131 return ((ISPL(tf->tf_cs)) ? tf->tf_rsp :
132 (db_expr_t)tf + offsetof(struct trapframe, tf_rsp));
133 }
134
135 static int
136 db_frame(struct db_variable *vp, db_expr_t *valuep, int op)
137 {
138 long *reg;
139
140 if (kdb_frame == NULL)
141 return (0);
142
143 reg = (long *)((uintptr_t)kdb_frame + (db_expr_t)vp->valuep);
144 if (op == DB_VAR_GET)
145 *valuep = *reg;
146 else
147 *reg = *valuep;
148 return (1);
149 }
150
151 static int
152 db_rsp(struct db_variable *vp, db_expr_t *valuep, int op)
153 {
154
155 if (kdb_frame == NULL)
156 return (0);
157
158 if (op == DB_VAR_GET)
159 *valuep = get_rsp(kdb_frame);
160 else if (ISPL(kdb_frame->tf_cs))
161 kdb_frame->tf_rsp = *valuep;
162 return (1);
163 }
164
165 static int
166 db_ss(struct db_variable *vp, db_expr_t *valuep, int op)
167 {
168
169 if (kdb_frame == NULL)
170 return (0);
171
172 if (op == DB_VAR_GET)
173 *valuep = (ISPL(kdb_frame->tf_cs)) ? kdb_frame->tf_ss : rss();
174 else if (ISPL(kdb_frame->tf_cs))
175 kdb_frame->tf_ss = *valuep;
176 return (1);
177 }
178
179 /*
180 * Stack trace.
181 */
182 #define INKERNEL(va) (((va) >= DMAP_MIN_ADDRESS && (va) < DMAP_MAX_ADDRESS) \
183 || ((va) >= KERNBASE && (va) < VM_MAX_KERNEL_ADDRESS))
184
185 struct amd64_frame {
186 struct amd64_frame *f_frame;
187 long f_retaddr;
188 long f_arg0;
189 };
190
191 #define NORMAL 0
192 #define TRAP 1
193 #define INTERRUPT 2
194 #define SYSCALL 3
195
196 static void db_nextframe(struct amd64_frame **, db_addr_t *, struct thread *);
197 static int db_numargs(struct amd64_frame *);
198 static void db_print_stack_entry(const char *, int, char **, long *, db_addr_t);
199 static void decode_syscall(int, struct thread *);
200
201 static char * watchtype_str(int type);
202 int amd64_set_watch(int watchnum, unsigned int watchaddr, int size, int access,
203 struct dbreg * d);
204 int amd64_clr_watch(int watchnum, struct dbreg * d);
205 int db_md_set_watchpoint(db_expr_t addr, db_expr_t size);
206 int db_md_clr_watchpoint(db_expr_t addr, db_expr_t size);
207 void db_md_list_watchpoints(void);
208
209 /*
210 * Figure out how many arguments were passed into the frame at "fp".
211 */
212 static int
213 db_numargs(fp)
214 struct amd64_frame *fp;
215 {
216 #if 1
217 return (0); /* regparm, needs dwarf2 info */
218 #else
219 long *argp;
220 int inst;
221 int args;
222
223 argp = (long *)db_get_value((long)&fp->f_retaddr, 8, FALSE);
224 /*
225 * XXX etext is wrong for LKMs. We should attempt to interpret
226 * the instruction at the return address in all cases. This
227 * may require better fault handling.
228 */
229 if (argp < (long *)btext || argp >= (long *)etext) {
230 args = 5;
231 } else {
232 inst = db_get_value((long)argp, 4, FALSE);
233 if ((inst & 0xff) == 0x59) /* popl %ecx */
234 args = 1;
235 else if ((inst & 0xffff) == 0xc483) /* addl $Ibs, %esp */
236 args = ((inst >> 16) & 0xff) / 4;
237 else
238 args = 5;
239 }
240 return (args);
241 #endif
242 }
243
244 static void
245 db_print_stack_entry(name, narg, argnp, argp, callpc)
246 const char *name;
247 int narg;
248 char **argnp;
249 long *argp;
250 db_addr_t callpc;
251 {
252 db_printf("%s(", name);
253 #if 0
254 while (narg) {
255 if (argnp)
256 db_printf("%s=", *argnp++);
257 db_printf("%lr", (long)db_get_value((long)argp, 8, FALSE));
258 argp++;
259 if (--narg != 0)
260 db_printf(",");
261 }
262 #endif
263 db_printf(") at ");
264 db_printsym(callpc, DB_STGY_PROC);
265 db_printf("\n");
266 }
267
268 static void
269 decode_syscall(int number, struct thread *td)
270 {
271 struct proc *p;
272 c_db_sym_t sym;
273 db_expr_t diff;
274 sy_call_t *f;
275 const char *symname;
276
277 db_printf(" (%d", number);
278 p = (td != NULL) ? td->td_proc : NULL;
279 if (p != NULL && 0 <= number && number < p->p_sysent->sv_size) {
280 f = p->p_sysent->sv_table[number].sy_call;
281 sym = db_search_symbol((db_addr_t)f, DB_STGY_ANY, &diff);
282 if (sym != DB_SYM_NULL && diff == 0) {
283 db_symbol_values(sym, &symname, NULL);
284 db_printf(", %s, %s", p->p_sysent->sv_name, symname);
285 }
286 }
287 db_printf(")");
288 }
289
290 /*
291 * Figure out the next frame up in the call stack.
292 */
293 static void
294 db_nextframe(struct amd64_frame **fp, db_addr_t *ip, struct thread *td)
295 {
296 struct trapframe *tf;
297 int frame_type;
298 long rip, rsp, rbp;
299 db_expr_t offset;
300 c_db_sym_t sym;
301 const char *name;
302
303 rip = db_get_value((long) &(*fp)->f_retaddr, 8, FALSE);
304 rbp = db_get_value((long) &(*fp)->f_frame, 8, FALSE);
305
306 /*
307 * Figure out frame type. We look at the address just before
308 * the saved instruction pointer as the saved EIP is after the
309 * call function, and if the function being called is marked as
310 * dead (such as panic() at the end of dblfault_handler()), then
311 * the instruction at the saved EIP will be part of a different
312 * function (syscall() in this example) rather than the one that
313 * actually made the call.
314 */
315 frame_type = NORMAL;
316 sym = db_search_symbol(rip - 1, DB_STGY_ANY, &offset);
317 db_symbol_values(sym, &name, NULL);
318 if (name != NULL) {
319 if (strcmp(name, "calltrap") == 0 ||
320 strcmp(name, "fork_trampoline") == 0 ||
321 strcmp(name, "nmi_calltrap") == 0)
322 frame_type = TRAP;
323 else if (strncmp(name, "Xatpic_intr", 11) == 0 ||
324 strncmp(name, "Xatpic_fastintr", 15) == 0 ||
325 strncmp(name, "Xapic_isr", 9) == 0)
326 frame_type = INTERRUPT;
327 else if (strcmp(name, "Xfast_syscall") == 0)
328 frame_type = SYSCALL;
329 }
330
331 /*
332 * Normal frames need no special processing.
333 */
334 if (frame_type == NORMAL) {
335 *ip = (db_addr_t) rip;
336 *fp = (struct amd64_frame *) rbp;
337 return;
338 }
339
340 db_print_stack_entry(name, 0, 0, 0, rip);
341
342 /*
343 * Point to base of trapframe which is just above the
344 * current frame.
345 */
346 tf = (struct trapframe *)((long)*fp + 16);
347
348 if (INKERNEL((long) tf)) {
349 rsp = get_rsp(tf);
350 rip = tf->tf_rip;
351 rbp = tf->tf_rbp;
352 switch (frame_type) {
353 case TRAP:
354 db_printf("--- trap %#lr", tf->tf_trapno);
355 break;
356 case SYSCALL:
357 db_printf("--- syscall");
358 decode_syscall(tf->tf_rax, td);
359 break;
360 case INTERRUPT:
361 db_printf("--- interrupt");
362 break;
363 default:
364 panic("The moon has moved again.");
365 }
366 db_printf(", rip = %#lr, rsp = %#lr, rbp = %#lr ---\n", rip,
367 rsp, rbp);
368 }
369
370 *ip = (db_addr_t) rip;
371 *fp = (struct amd64_frame *) rbp;
372 }
373
374 static int
375 db_backtrace(struct thread *td, struct trapframe *tf,
376 struct amd64_frame *frame, db_addr_t pc, int count)
377 {
378 struct amd64_frame *actframe;
379 #define MAXNARG 16
380 char *argnames[MAXNARG], **argnp = NULL;
381 const char *name;
382 long *argp;
383 db_expr_t offset;
384 c_db_sym_t sym;
385 int narg, quit;
386 boolean_t first;
387
388 if (count == -1)
389 count = 1024;
390
391 first = TRUE;
392 quit = 0;
393 db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
394 while (count-- && !quit) {
395 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
396 db_symbol_values(sym, &name, NULL);
397
398 /*
399 * Attempt to determine a (possibly fake) frame that gives
400 * the caller's pc. It may differ from `frame' if the
401 * current function never sets up a standard frame or hasn't
402 * set one up yet or has just discarded one. The last two
403 * cases can be guessed fairly reliably for code generated
404 * by gcc. The first case is too much trouble to handle in
405 * general because the amount of junk on the stack depends
406 * on the pc (the special handling of "calltrap", etc. in
407 * db_nextframe() works because the `next' pc is special).
408 */
409 actframe = frame;
410 if (first) {
411 if (tf != NULL) {
412 int instr;
413
414 instr = db_get_value(pc, 4, FALSE);
415 if ((instr & 0xffffffff) == 0xe5894855) {
416 /* pushq %rbp; movq %rsp, %rbp */
417 actframe = (void *)(get_rsp(tf) - 8);
418 } else if ((instr & 0xffffff) == 0xe58948) {
419 /* movq %rsp, %rbp */
420 actframe = (void *)get_rsp(tf);
421 if (tf->tf_rbp == 0) {
422 /* Fake frame better. */
423 frame = actframe;
424 }
425 } else if ((instr & 0xff) == 0xc3) {
426 /* ret */
427 actframe = (void *)(get_rsp(tf) - 8);
428 } else if (offset == 0) {
429 /* Probably an assembler symbol. */
430 actframe = (void *)(get_rsp(tf) - 8);
431 }
432 } else if (strcmp(name, "fork_trampoline") == 0) {
433 /*
434 * Don't try to walk back on a stack for a
435 * process that hasn't actually been run yet.
436 */
437 db_print_stack_entry(name, 0, 0, 0, pc);
438 break;
439 }
440 first = FALSE;
441 }
442
443 argp = &actframe->f_arg0;
444 narg = MAXNARG;
445 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
446 argnp = argnames;
447 } else {
448 narg = db_numargs(frame);
449 }
450
451 db_print_stack_entry(name, narg, argnp, argp, pc);
452
453 if (actframe != frame) {
454 /* `frame' belongs to caller. */
455 pc = (db_addr_t)
456 db_get_value((long)&actframe->f_retaddr, 8, FALSE);
457 continue;
458 }
459
460 db_nextframe(&frame, &pc, td);
461
462 if (INKERNEL((long)pc) && !INKERNEL((long)frame)) {
463 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
464 db_symbol_values(sym, &name, NULL);
465 db_print_stack_entry(name, 0, 0, 0, pc);
466 break;
467 }
468 if (!INKERNEL((long) frame)) {
469 break;
470 }
471 }
472
473 return (0);
474 }
475
476 void
477 db_trace_self(void)
478 {
479 struct amd64_frame *frame;
480 db_addr_t callpc;
481 register_t rbp;
482
483 __asm __volatile("movq %%rbp,%0" : "=r" (rbp));
484 frame = (struct amd64_frame *)rbp;
485 callpc = (db_addr_t)db_get_value((long)&frame->f_retaddr, 8, FALSE);
486 frame = frame->f_frame;
487 db_backtrace(curthread, NULL, frame, callpc, -1);
488 }
489
490 int
491 db_trace_thread(struct thread *thr, int count)
492 {
493 struct pcb *ctx;
494
495 ctx = kdb_thr_ctx(thr);
496 return (db_backtrace(thr, NULL, (struct amd64_frame *)ctx->pcb_rbp,
497 ctx->pcb_rip, count));
498 }
499
500 void
501 stack_save(struct stack *st)
502 {
503 struct amd64_frame *frame;
504 vm_offset_t callpc;
505 register_t rbp;
506
507 stack_zero(st);
508 __asm __volatile("movq %%rbp,%0" : "=r" (rbp));
509 frame = (struct amd64_frame *)rbp;
510 while (1) {
511 if (!INKERNEL((long)frame))
512 break;
513 callpc = frame->f_retaddr;
514 if (!INKERNEL(callpc))
515 break;
516 if (stack_put(st, callpc) == -1)
517 break;
518 if (frame->f_frame <= frame ||
519 (vm_offset_t)frame->f_frame >=
520 (vm_offset_t)rbp + KSTACK_PAGES * PAGE_SIZE)
521 break;
522 frame = frame->f_frame;
523 }
524 }
525
526 int
527 amd64_set_watch(watchnum, watchaddr, size, access, d)
528 int watchnum;
529 unsigned int watchaddr;
530 int size;
531 int access;
532 struct dbreg * d;
533 {
534 int i;
535 unsigned int mask;
536
537 if (watchnum == -1) {
538 for (i = 0, mask = 0x3; i < 4; i++, mask <<= 2)
539 if ((d->dr[7] & mask) == 0)
540 break;
541 if (i < 4)
542 watchnum = i;
543 else
544 return (-1);
545 }
546
547 switch (access) {
548 case DBREG_DR7_EXEC:
549 size = 1; /* size must be 1 for an execution breakpoint */
550 /* fall through */
551 case DBREG_DR7_WRONLY:
552 case DBREG_DR7_RDWR:
553 break;
554 default : return (-1);
555 }
556
557 /*
558 * we can watch a 1, 2, or 4 byte sized location
559 */
560 switch (size) {
561 case 1 : mask = 0x00; break;
562 case 2 : mask = 0x01 << 2; break;
563 case 4 : mask = 0x03 << 2; break;
564 default : return (-1);
565 }
566
567 mask |= access;
568
569 /* clear the bits we are about to affect */
570 d->dr[7] &= ~((0x3 << (watchnum*2)) | (0x0f << (watchnum*4+16)));
571
572 /* set drN register to the address, N=watchnum */
573 DBREG_DRX(d,watchnum) = watchaddr;
574
575 /* enable the watchpoint */
576 d->dr[7] |= (0x2 << (watchnum*2)) | (mask << (watchnum*4+16));
577
578 return (watchnum);
579 }
580
581
582 int
583 amd64_clr_watch(watchnum, d)
584 int watchnum;
585 struct dbreg * d;
586 {
587
588 if (watchnum < 0 || watchnum >= 4)
589 return (-1);
590
591 d->dr[7] = d->dr[7] & ~((0x3 << (watchnum*2)) | (0x0f << (watchnum*4+16)));
592 DBREG_DRX(d,watchnum) = 0;
593
594 return (0);
595 }
596
597
598 int
599 db_md_set_watchpoint(addr, size)
600 db_expr_t addr;
601 db_expr_t size;
602 {
603 int avail, wsize;
604 int i;
605 struct dbreg d;
606
607 fill_dbregs(NULL, &d);
608
609 avail = 0;
610 for(i=0; i<4; i++) {
611 if ((d.dr[7] & (3 << (i*2))) == 0)
612 avail++;
613 }
614
615 if (avail*4 < size)
616 return (-1);
617
618 for (i=0; i<4 && (size != 0); i++) {
619 if ((d.dr[7] & (3<<(i*2))) == 0) {
620 if (size > 4)
621 wsize = 4;
622 else
623 wsize = size;
624 if (wsize == 3)
625 wsize++;
626 amd64_set_watch(i, addr, wsize,
627 DBREG_DR7_WRONLY, &d);
628 addr += wsize;
629 size -= wsize;
630 }
631 }
632
633 set_dbregs(NULL, &d);
634
635 return(0);
636 }
637
638
639 int
640 db_md_clr_watchpoint(addr, size)
641 db_expr_t addr;
642 db_expr_t size;
643 {
644 int i;
645 struct dbreg d;
646
647 fill_dbregs(NULL, &d);
648
649 for(i=0; i<4; i++) {
650 if (d.dr[7] & (3 << (i*2))) {
651 if ((DBREG_DRX((&d), i) >= addr) &&
652 (DBREG_DRX((&d), i) < addr+size))
653 amd64_clr_watch(i, &d);
654
655 }
656 }
657
658 set_dbregs(NULL, &d);
659
660 return(0);
661 }
662
663
664 static
665 char *
666 watchtype_str(type)
667 int type;
668 {
669 switch (type) {
670 case DBREG_DR7_EXEC : return "execute"; break;
671 case DBREG_DR7_RDWR : return "read/write"; break;
672 case DBREG_DR7_WRONLY : return "write"; break;
673 default : return "invalid"; break;
674 }
675 }
676
677
678 void
679 db_md_list_watchpoints()
680 {
681 int i;
682 struct dbreg d;
683
684 fill_dbregs(NULL, &d);
685
686 db_printf("\nhardware watchpoints:\n");
687 db_printf(" watch status type len address\n");
688 db_printf(" ----- -------- ---------- --- ----------\n");
689 for (i=0; i<4; i++) {
690 if (d.dr[7] & (0x03 << (i*2))) {
691 unsigned type, len;
692 type = (d.dr[7] >> (16+(i*4))) & 3;
693 len = (d.dr[7] >> (16+(i*4)+2)) & 3;
694 db_printf(" %-5d %-8s %10s %3d 0x%016lx\n",
695 i, "enabled", watchtype_str(type),
696 len + 1, DBREG_DRX((&d), i));
697 }
698 else {
699 db_printf(" %-5d disabled\n", i);
700 }
701 }
702
703 db_printf("\ndebug register values:\n");
704 for (i=0; i<8; i++) {
705 db_printf(" dr%d 0x%016lx\n", i, DBREG_DRX((&d), i));
706 }
707 db_printf("\n");
708 }
Cache object: 958ae499f0edd74eb6f21b0693314bdb
|