2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
14 * 3. Neither the name of The DragonFly Project nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific, prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Mach Operating System
34 * Copyright (c) 1991,1990 Carnegie Mellon University
35 * All Rights Reserved.
37 * Permission to use, copy, modify and distribute this software and its
38 * documentation is hereby granted, provided that both the copyright
39 * notice and this permission notice appear in all copies of the
40 * software, derivative works or modified versions, and any portions
41 * thereof, and that both notices appear in supporting documentation.
43 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
44 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
45 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47 * Carnegie Mellon requests users of this software to return to
49 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
50 * School of Computer Science
51 * Carnegie Mellon University
52 * Pittsburgh PA 15213-3890
54 * any improvements or extensions that they make and grant Carnegie the
55 * rights to redistribute these changes.
57 * $FreeBSD: src/sys/i386/i386/db_trace.c,v 1.35.2.3 2002/02/21 22:31:25 silby Exp $
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/linker_set.h>
67 #include <machine/cpu.h>
68 #include <machine/md_var.h>
71 #include <vm/vm_param.h>
73 #include <vm/vm_map.h>
76 #include <ddb/db_access.h>
77 #include <ddb/db_sym.h>
78 #include <ddb/db_variables.h>
90 * Machine register set.
92 struct db_variable db_regs[] = {
93 { "cs", &ddb_regs.tf_cs, NULL },
94 /* { "ds", &ddb_regs.tf_ds, NULL },
95 { "es", &ddb_regs.tf_es, NULL },
96 { "fs", &ddb_regs.tf_fs, NULL },
97 { "gs", &ddb_regs.tf_gs, NULL }, */
98 { "ss", &ddb_regs.tf_ss, NULL },
99 { "rax", &ddb_regs.tf_rax, NULL },
100 { "rcx", &ddb_regs.tf_rcx, NULL },
101 { "rdx", &ddb_regs.tf_rdx, NULL },
102 { "rbx", &ddb_regs.tf_rbx, NULL },
103 { "rsp", &ddb_regs.tf_rsp, NULL },
104 { "rbp", &ddb_regs.tf_rbp, NULL },
105 { "rsi", &ddb_regs.tf_rsi, NULL },
106 { "rdi", &ddb_regs.tf_rdi, NULL },
107 { "rip", &ddb_regs.tf_rip, NULL },
108 { "rfl", &ddb_regs.tf_rflags, NULL },
109 { "r8", &ddb_regs.tf_r8, NULL },
110 { "r9", &ddb_regs.tf_r9, NULL },
111 { "r10", &ddb_regs.tf_r10, NULL },
112 { "r11", &ddb_regs.tf_r11, NULL },
113 { "r12", &ddb_regs.tf_r12, NULL },
114 { "r13", &ddb_regs.tf_r13, NULL },
115 { "r14", &ddb_regs.tf_r14, NULL },
116 { "r15", &ddb_regs.tf_r15, NULL },
117 { "dr0", NULL, db_dr0 },
118 { "dr1", NULL, db_dr1 },
119 { "dr2", NULL, db_dr2 },
120 { "dr3", NULL, db_dr3 },
121 { "dr4", NULL, db_dr4 },
122 { "dr5", NULL, db_dr5 },
123 { "dr6", NULL, db_dr6 },
124 { "dr7", NULL, db_dr7 },
126 struct db_variable *db_eregs = db_regs + NELEM(db_regs);
131 #define INKERNEL(va) (((vm_offset_t)(va)) >= USRSTACK)
133 struct x86_64_frame {
134 struct x86_64_frame *f_frame;
144 static void db_nextframe(struct x86_64_frame **, db_addr_t *);
145 static int db_numargs(struct x86_64_frame *);
146 static void db_print_stack_entry(const char *, int, char **, long *, db_addr_t);
147 static void dl_symbol_values(long callpc, const char **name);
150 static char *watchtype_str(int type);
151 static int kx86_64_set_watch(int watchnum, unsigned int watchaddr,
152 int size, int access, struct dbreg * d);
153 static int kx86_64_clr_watch(int watchnum, struct dbreg * d);
154 int db_md_set_watchpoint(db_expr_t addr, db_expr_t size);
155 int db_md_clr_watchpoint(db_expr_t addr, db_expr_t size);
156 void db_md_list_watchpoints(void);
160 * Figure out how many arguments were passed into the frame at "fp".
163 db_numargs(struct x86_64_frame *fp)
166 return (0); /* regparm, needs dwarf2 info */
173 argp = (int *)db_get_value((int)&fp->f_retaddr, 4, FALSE);
175 * XXX etext is wrong for LKMs. We should attempt to interpret
176 * the instruction at the return address in all cases. This
177 * may require better fault handling.
179 if (argp < (int *)btext || argp >= (int *)etext) {
182 inst = db_get_value((int)argp, 4, FALSE);
183 if ((inst & 0xff) == 0x59) /* popl %ecx */
185 else if ((inst & 0xffff) == 0xc483) /* addl $Ibs, %esp */
186 args = ((inst >> 16) & 0xff) / 4;
197 db_print_stack_entry(const char *name, int narg, char **argnp, long *argp,
200 db_printf("%s(", name);
203 db_printf("%s=", *argnp++);
204 db_printf("%ld", (long)db_get_value((long)argp, 8, FALSE));
210 db_printsym(callpc, DB_STGY_PROC);
211 db_printf(" %p ", (void*) callpc);
216 * Figure out the next frame up in the call stack.
219 db_nextframe(struct x86_64_frame **fp, db_addr_t *ip)
221 struct trapframe *tf;
225 const char *sym, *name;
227 if ((unsigned long)*fp < PAGE_SIZE) {
231 rip = db_get_value((long) &(*fp)->f_retaddr, 8, FALSE);
232 rbp = db_get_value((long) &(*fp)->f_frame, 8, FALSE);
235 * Figure out frame type.
240 sym = db_search_symbol(rip, DB_STGY_ANY, &offset);
241 db_symbol_values(sym, &name, NULL);
242 dl_symbol_values(rip, &name);
244 if (!strcmp(name, "calltrap")) {
246 } else if (!strncmp(name, "Xresume", 7)) {
247 frame_type = INTERRUPT;
248 } else if (!strcmp(name, "_Xsyscall")) {
249 frame_type = SYSCALL;
254 * Normal frames need no special processing.
256 if (frame_type == NORMAL) {
257 *ip = (db_addr_t) rip;
258 *fp = (struct x86_64_frame *) rbp;
262 db_print_stack_entry(name, 0, 0, 0, rip);
265 * Point to base of trapframe which is just above the
268 tf = (struct trapframe *)((long)*fp + 16);
271 rsp = (ISPL(tf->tf_cs) == SEL_UPL) ? tf->tf_rsp : (long)&tf->tf_rsp;
273 rsp = (long)&tf->tf_rsp;
275 switch (frame_type) {
281 "--- trap %016lx, rip = %016lx, rsp = %016lx, rbp = %016lx ---\n",
282 tf->tf_trapno, rip, rsp, rbp);
290 "--- syscall %016lx, rip = %016lx, rsp = %016lx, rbp = %016lx ---\n",
291 tf->tf_rax, rip, rsp, rbp);
295 tf = (struct trapframe *)((long)*fp + 16);
300 "--- interrupt, rip = %016lx, rsp = %016lx, rbp = %016lx ---\n",
308 *ip = (db_addr_t) rip;
309 *fp = (struct x86_64_frame *) rbp;
313 db_stack_trace_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
316 struct x86_64_frame *frame;
326 frame = (struct x86_64_frame *)BP_REGS(&ddb_regs);
328 frame = (struct x86_64_frame *)(SP_REGS(&ddb_regs) - 8);
329 callpc = PC_REGS(&ddb_regs);
332 * Look for something that might be a frame pointer, just as
335 frame = (struct x86_64_frame *)addr;
336 for (i = 0; i < 4096; i += 8) {
337 struct x86_64_frame *check;
339 check = (struct x86_64_frame *)db_get_value((long)((char *)&frame->f_frame + i), 8, FALSE);
340 if ((char *)check - (char *)frame >= 0 &&
341 (char *)check - (char *)frame < 4096
345 db_printf("%p does not look like a stack frame, skipping\n", (char *)&frame->f_frame + i);
348 db_printf("Unable to find anything that looks like a stack frame\n");
351 frame = (void *)((char *)frame + i);
352 db_printf("Trace beginning at frame %p\n", frame);
353 callpc = (db_addr_t)db_get_value((long)&frame->f_retaddr, 8, FALSE);
358 struct x86_64_frame *actframe;
364 char *argnames[MAXNARG], **argnp = NULL;
366 sym = db_search_symbol(callpc, DB_STGY_ANY, &offset);
367 db_symbol_values(sym, &name, NULL);
368 dl_symbol_values(callpc, &name);
371 * Attempt to determine a (possibly fake) frame that gives
372 * the caller's pc. It may differ from `frame' if the
373 * current function never sets up a standard frame or hasn't
374 * set one up yet or has just discarded one. The last two
375 * cases can be guessed fairly reliably for code generated
376 * by gcc. The first case is too much trouble to handle in
377 * general because the amount of junk on the stack depends
378 * on the pc (the special handling of "calltrap", etc. in
379 * db_nextframe() works because the `next' pc is special).
386 instr = db_get_value(callpc, 4, FALSE);
387 if ((instr & 0xffffffff) == 0xe5894855) {
388 /* pushq %rbp; movq %rsp, %rbp */
389 actframe = (struct x86_64_frame *)
390 (SP_REGS(&ddb_regs) - 8);
391 } else if ((instr & 0xffffff) == 0xe58948) {
392 /* movq %rsp, %rbp */
393 actframe = (struct x86_64_frame *)
395 if (ddb_regs.tf_rbp == 0) {
396 /* Fake caller's frame better. */
399 } else if ((instr & 0xff) == 0xc3) {
401 actframe = (struct x86_64_frame *)
402 (SP_REGS(&ddb_regs) - 8);
403 } else if (offset == 0) {
404 /* Probably a symbol in assembler code. */
405 actframe = (struct x86_64_frame *)
406 (SP_REGS(&ddb_regs) - 8);
408 } else if (name != NULL &&
409 strcmp(name, "fork_trampoline") == 0) {
411 * Don't try to walk back on a stack for a
412 * process that hasn't actually been run yet.
414 db_print_stack_entry(name, 0, 0, 0, callpc);
420 argp = &actframe->f_arg0;
422 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
425 narg = db_numargs(frame);
428 db_print_stack_entry(name, narg, argnp, argp, callpc);
431 * Stop at the system call boundary (else we risk
432 * double-faulting on junk).
434 if (name && strcmp(name, "Xfast_syscall") == 0)
437 if (actframe != frame) {
438 /* `frame' belongs to caller. */
440 db_get_value((long)&actframe->f_retaddr, 8, FALSE);
444 db_nextframe(&frame, &callpc);
451 print_backtrace(int count)
455 __asm __volatile("movq %%rbp, %0" : "=r" (rbp));
456 db_stack_trace_cmd(rbp, 1, count, NULL);
459 #define DB_DRX_FUNC(reg) \
461 db_ ## reg (struct db_variable *vp, db_expr_t *valuep, int op) \
463 if (op == DB_VAR_GET) \
464 *valuep = r ## reg (); \
466 load_ ## reg (*valuep); \
481 kx86_64_set_watch(int watchnum, unsigned int watchaddr, int size, int access,
487 if (watchnum == -1) {
488 for (i = 0, mask = 0x3; i < 4; i++, mask <<= 2)
489 if ((d->dr[7] & mask) == 0)
499 size = 1; /* size must be 1 for an execution breakpoint */
501 case DBREG_DR7_WRONLY:
509 * we can watch a 1, 2, 4, or 8 byte sized location
530 /* clear the bits we are about to affect */
531 d->dr[7] &= ~((0x3 << (watchnum * 2)) | (0x0f << (watchnum * 4 + 16)));
533 /* set drN register to the address, N=watchnum */
534 DBREG_DRX(d, watchnum) = watchaddr;
536 /* enable the watchpoint */
537 d->dr[7] |= (0x2 << (watchnum * 2)) | (mask << (watchnum * 4 + 16));
544 kx86_64_clr_watch(int watchnum, struct dbreg *d)
546 if (watchnum < 0 || watchnum >= 4)
549 d->dr[7] &= ~((0x3 << (watchnum * 2)) | (0x0f << (watchnum * 4 + 16)));
550 DBREG_DRX(d, watchnum) = 0;
557 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
563 fill_dbregs(NULL, &d);
566 for (i = 0; i < 4; i++) {
567 if ((d.dr[7] & (3 << (i * 2))) == 0)
571 if (avail * 8 < size)
574 for (i=0; i < 4 && (size != 0); i++) {
575 if ((d.dr[7] & (3 << (i * 2))) == 0) {
576 if (size >= 8 || (avail == 1 && size > 4))
584 kx86_64_set_watch(i, addr, wsize, DBREG_DR7_WRONLY, &d);
590 set_dbregs(NULL, &d);
596 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
601 fill_dbregs(NULL, &d);
603 for(i = 0; i < 4; i++) {
604 if (d.dr[7] & (3 << (i * 2))) {
605 if ((DBREG_DRX((&d), i) >= addr) &&
606 (DBREG_DRX((&d), i) < addr + size))
607 kx86_64_clr_watch(i, &d);
611 set_dbregs(NULL, &d);
617 watchtype_str(int type)
624 case DBREG_DR7_WRONLY:
632 db_md_list_watchpoints(void)
637 fill_dbregs(NULL, &d);
639 db_printf("\nhardware watchpoints:\n");
640 db_printf(" watch status type len address\n"
641 " ----- -------- ---------- --- ----------\n");
642 for (i = 0; i < 4; i++) {
643 if (d.dr[7] & (0x03 << (i * 2))) {
645 type = (d.dr[7] >> (16 + (i * 4))) & 3;
646 len = (d.dr[7] >> (16 + (i * 4) + 2)) & 3;
647 db_printf(" %-5d %-8s %10s %3d 0x%08lx\n",
648 i, "enabled", watchtype_str(type),
649 len + 1, DBREG_DRX((&d), i));
651 db_printf(" %-5d disabled\n", i);
655 db_printf("\ndebug register values:\n");
656 for (i = 0; i < 8; i++)
657 db_printf(" dr%d 0x%08lx\n", i, DBREG_DRX((&d),i));
662 * See if dladdr() can get the symbol name via the standard dynamic loader.
666 dl_symbol_values(long callpc, const char **name)
671 if (dladdr((const void *)callpc, &info) != 0) {
672 if (info.dli_saddr <= (const void *)callpc)
673 *name = info.dli_sname;