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.3/sys/amd64/amd64/db_trace.c 173886 2007-11-24 19:45:58Z cvs2svn $");
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 strcmp(name, "Xdblfault") == 0)
323 frame_type = TRAP;
324 else if (strncmp(name, "Xatpic_intr", 11) == 0 ||
325 strncmp(name, "Xatpic_fastintr", 15) == 0 ||
326 strncmp(name, "Xapic_isr", 9) == 0)
327 frame_type = INTERRUPT;
328 else if (strcmp(name, "Xfast_syscall") == 0)
329 frame_type = SYSCALL;
330 }
331
332 /*
333 * Normal frames need no special processing.
334 */
335 if (frame_type == NORMAL) {
336 *ip = (db_addr_t) rip;
337 *fp = (struct amd64_frame *) rbp;
338 return;
339 }
340
341 db_print_stack_entry(name, 0, 0, 0, rip);
342
343 /*
344 * Point to base of trapframe which is just above the
345 * current frame.
346 */
347 tf = (struct trapframe *)((long)*fp + 16);
348
349 if (INKERNEL((long) tf)) {
350 rsp = get_rsp(tf);
351 rip = tf->tf_rip;
352 rbp = tf->tf_rbp;
353 switch (frame_type) {
354 case TRAP:
355 db_printf("--- trap %#lr", tf->tf_trapno);
356 break;
357 case SYSCALL:
358 db_printf("--- syscall");
359 decode_syscall(tf->tf_rax, td);
360 break;
361 case INTERRUPT:
362 db_printf("--- interrupt");
363 break;
364 default:
365 panic("The moon has moved again.");
366 }
367 db_printf(", rip = %#lr, rsp = %#lr, rbp = %#lr ---\n", rip,
368 rsp, rbp);
369 }
370
371 *ip = (db_addr_t) rip;
372 *fp = (struct amd64_frame *) rbp;
373 }
374
375 static int
376 db_backtrace(struct thread *td, struct trapframe *tf,
377 struct amd64_frame *frame, db_addr_t pc, int count)
378 {
379 struct amd64_frame *actframe;
380 #define MAXNARG 16
381 char *argnames[MAXNARG], **argnp = NULL;
382 const char *name;
383 long *argp;
384 db_expr_t offset;
385 c_db_sym_t sym;
386 int narg, quit;
387 boolean_t first;
388
389 if (count == -1)
390 count = 1024;
391
392 first = TRUE;
393 quit = 0;
394 db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
395 while (count-- && !quit) {
396 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
397 db_symbol_values(sym, &name, NULL);
398
399 /*
400 * Attempt to determine a (possibly fake) frame that gives
401 * the caller's pc. It may differ from `frame' if the
402 * current function never sets up a standard frame or hasn't
403 * set one up yet or has just discarded one. The last two
404 * cases can be guessed fairly reliably for code generated
405 * by gcc. The first case is too much trouble to handle in
406 * general because the amount of junk on the stack depends
407 * on the pc (the special handling of "calltrap", etc. in
408 * db_nextframe() works because the `next' pc is special).
409 */
410 actframe = frame;
411 if (first) {
412 if (tf != NULL) {
413 int instr;
414
415 instr = db_get_value(pc, 4, FALSE);
416 if ((instr & 0xffffffff) == 0xe5894855) {
417 /* pushq %rbp; movq %rsp, %rbp */
418 actframe = (void *)(get_rsp(tf) - 8);
419 } else if ((instr & 0xffffff) == 0xe58948) {
420 /* movq %rsp, %rbp */
421 actframe = (void *)get_rsp(tf);
422 if (tf->tf_rbp == 0) {
423 /* Fake frame better. */
424 frame = actframe;
425 }
426 } else if ((instr & 0xff) == 0xc3) {
427 /* ret */
428 actframe = (void *)(get_rsp(tf) - 8);
429 } else if (offset == 0) {
430 /* Probably an assembler symbol. */
431 actframe = (void *)(get_rsp(tf) - 8);
432 }
433 } else if (strcmp(name, "fork_trampoline") == 0) {
434 /*
435 * Don't try to walk back on a stack for a
436 * process that hasn't actually been run yet.
437 */
438 db_print_stack_entry(name, 0, 0, 0, pc);
439 break;
440 }
441 first = FALSE;
442 }
443
444 argp = &actframe->f_arg0;
445 narg = MAXNARG;
446 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
447 argnp = argnames;
448 } else {
449 narg = db_numargs(frame);
450 }
451
452 db_print_stack_entry(name, narg, argnp, argp, pc);
453
454 if (actframe != frame) {
455 /* `frame' belongs to caller. */
456 pc = (db_addr_t)
457 db_get_value((long)&actframe->f_retaddr, 8, FALSE);
458 continue;
459 }
460
461 db_nextframe(&frame, &pc, td);
462
463 if (INKERNEL((long)pc) && !INKERNEL((long)frame)) {
464 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
465 db_symbol_values(sym, &name, NULL);
466 db_print_stack_entry(name, 0, 0, 0, pc);
467 break;
468 }
469 if (!INKERNEL((long) frame)) {
470 break;
471 }
472 }
473
474 return (0);
475 }
476
477 void
478 db_trace_self(void)
479 {
480 struct amd64_frame *frame;
481 db_addr_t callpc;
482 register_t rbp;
483
484 __asm __volatile("movq %%rbp,%0" : "=r" (rbp));
485 frame = (struct amd64_frame *)rbp;
486 callpc = (db_addr_t)db_get_value((long)&frame->f_retaddr, 8, FALSE);
487 frame = frame->f_frame;
488 db_backtrace(curthread, NULL, frame, callpc, -1);
489 }
490
491 int
492 db_trace_thread(struct thread *thr, int count)
493 {
494 struct pcb *ctx;
495
496 ctx = kdb_thr_ctx(thr);
497 return (db_backtrace(thr, NULL, (struct amd64_frame *)ctx->pcb_rbp,
498 ctx->pcb_rip, count));
499 }
500
501 void
502 stack_save(struct stack *st)
503 {
504 struct amd64_frame *frame;
505 vm_offset_t callpc;
506 register_t rbp;
507
508 stack_zero(st);
509 __asm __volatile("movq %%rbp,%0" : "=r" (rbp));
510 frame = (struct amd64_frame *)rbp;
511 while (1) {
512 if (!INKERNEL((long)frame))
513 break;
514 callpc = frame->f_retaddr;
515 if (!INKERNEL(callpc))
516 break;
517 if (stack_put(st, callpc) == -1)
518 break;
519 if (frame->f_frame <= frame ||
520 (vm_offset_t)frame->f_frame >=
521 (vm_offset_t)rbp + KSTACK_PAGES * PAGE_SIZE)
522 break;
523 frame = frame->f_frame;
524 }
525 }
526
527 int
528 amd64_set_watch(watchnum, watchaddr, size, access, d)
529 int watchnum;
530 unsigned int watchaddr;
531 int size;
532 int access;
533 struct dbreg * d;
534 {
535 int i;
536 unsigned int mask;
537
538 if (watchnum == -1) {
539 for (i = 0, mask = 0x3; i < 4; i++, mask <<= 2)
540 if ((d->dr[7] & mask) == 0)
541 break;
542 if (i < 4)
543 watchnum = i;
544 else
545 return (-1);
546 }
547
548 switch (access) {
549 case DBREG_DR7_EXEC:
550 size = 1; /* size must be 1 for an execution breakpoint */
551 /* fall through */
552 case DBREG_DR7_WRONLY:
553 case DBREG_DR7_RDWR:
554 break;
555 default : return (-1);
556 }
557
558 /*
559 * we can watch a 1, 2, or 4 byte sized location
560 */
561 switch (size) {
562 case 1 : mask = 0x00; break;
563 case 2 : mask = 0x01 << 2; break;
564 case 4 : mask = 0x03 << 2; break;
565 default : return (-1);
566 }
567
568 mask |= access;
569
570 /* clear the bits we are about to affect */
571 d->dr[7] &= ~((0x3 << (watchnum*2)) | (0x0f << (watchnum*4+16)));
572
573 /* set drN register to the address, N=watchnum */
574 DBREG_DRX(d,watchnum) = watchaddr;
575
576 /* enable the watchpoint */
577 d->dr[7] |= (0x2 << (watchnum*2)) | (mask << (watchnum*4+16));
578
579 return (watchnum);
580 }
581
582
583 int
584 amd64_clr_watch(watchnum, d)
585 int watchnum;
586 struct dbreg * d;
587 {
588
589 if (watchnum < 0 || watchnum >= 4)
590 return (-1);
591
592 d->dr[7] = d->dr[7] & ~((0x3 << (watchnum*2)) | (0x0f << (watchnum*4+16)));
593 DBREG_DRX(d,watchnum) = 0;
594
595 return (0);
596 }
597
598
599 int
600 db_md_set_watchpoint(addr, size)
601 db_expr_t addr;
602 db_expr_t size;
603 {
604 int avail, wsize;
605 int i;
606 struct dbreg d;
607
608 fill_dbregs(NULL, &d);
609
610 avail = 0;
611 for(i=0; i<4; i++) {
612 if ((d.dr[7] & (3 << (i*2))) == 0)
613 avail++;
614 }
615
616 if (avail*4 < size)
617 return (-1);
618
619 for (i=0; i<4 && (size != 0); i++) {
620 if ((d.dr[7] & (3<<(i*2))) == 0) {
621 if (size > 4)
622 wsize = 4;
623 else
624 wsize = size;
625 if (wsize == 3)
626 wsize++;
627 amd64_set_watch(i, addr, wsize,
628 DBREG_DR7_WRONLY, &d);
629 addr += wsize;
630 size -= wsize;
631 }
632 }
633
634 set_dbregs(NULL, &d);
635
636 return(0);
637 }
638
639
640 int
641 db_md_clr_watchpoint(addr, size)
642 db_expr_t addr;
643 db_expr_t size;
644 {
645 int i;
646 struct dbreg d;
647
648 fill_dbregs(NULL, &d);
649
650 for(i=0; i<4; i++) {
651 if (d.dr[7] & (3 << (i*2))) {
652 if ((DBREG_DRX((&d), i) >= addr) &&
653 (DBREG_DRX((&d), i) < addr+size))
654 amd64_clr_watch(i, &d);
655
656 }
657 }
658
659 set_dbregs(NULL, &d);
660
661 return(0);
662 }
663
664
665 static
666 char *
667 watchtype_str(type)
668 int type;
669 {
670 switch (type) {
671 case DBREG_DR7_EXEC : return "execute"; break;
672 case DBREG_DR7_RDWR : return "read/write"; break;
673 case DBREG_DR7_WRONLY : return "write"; break;
674 default : return "invalid"; break;
675 }
676 }
677
678
679 void
680 db_md_list_watchpoints()
681 {
682 int i;
683 struct dbreg d;
684
685 fill_dbregs(NULL, &d);
686
687 db_printf("\nhardware watchpoints:\n");
688 db_printf(" watch status type len address\n");
689 db_printf(" ----- -------- ---------- --- ----------\n");
690 for (i=0; i<4; i++) {
691 if (d.dr[7] & (0x03 << (i*2))) {
692 unsigned type, len;
693 type = (d.dr[7] >> (16+(i*4))) & 3;
694 len = (d.dr[7] >> (16+(i*4)+2)) & 3;
695 db_printf(" %-5d %-8s %10s %3d 0x%016lx\n",
696 i, "enabled", watchtype_str(type),
697 len + 1, DBREG_DRX((&d), i));
698 }
699 else {
700 db_printf(" %-5d disabled\n", i);
701 }
702 }
703
704 db_printf("\ndebug register values:\n");
705 for (i=0; i<8; i++) {
706 db_printf(" dr%d 0x%016lx\n", i, DBREG_DRX((&d), i));
707 }
708 db_printf("\n");
709 }
Cache object: ed4f3b634b5310c0e274f5e01b68af98
|