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 $
58 * $DragonFly: src/sys/platform/pc64/amd64/db_trace.c,v 1.3 2008/08/29 17:07:10 dillon Exp $
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/linker_set.h>
68 #include <machine/cpu.h>
69 #include <machine/md_var.h>
72 #include <vm/vm_param.h>
74 #include <vm/vm_map.h>
76 #include <dlfcn.h> /* DLL */
80 #include <ddb/db_access.h>
81 #include <ddb/db_sym.h>
82 #include <ddb/db_variables.h>
94 * Machine register set.
96 struct db_variable db_regs[] = {
97 { "cs", &ddb_regs.tf_cs, NULL },
98 /* { "ds", &ddb_regs.tf_ds, NULL },
99 { "es", &ddb_regs.tf_es, NULL },
100 { "fs", &ddb_regs.tf_fs, NULL },
101 { "gs", &ddb_regs.tf_gs, NULL }, */
102 { "ss", &ddb_regs.tf_ss, NULL },
103 { "rax", &ddb_regs.tf_rax, NULL },
104 { "rcx", &ddb_regs.tf_rcx, NULL },
105 { "rdx", &ddb_regs.tf_rdx, NULL },
106 { "rbx", &ddb_regs.tf_rbx, NULL },
107 { "rsp", &ddb_regs.tf_rsp, NULL },
108 { "rbp", &ddb_regs.tf_rbp, NULL },
109 { "rsi", &ddb_regs.tf_rsi, NULL },
110 { "rdi", &ddb_regs.tf_rdi, NULL },
111 { "rip", &ddb_regs.tf_rip, NULL },
112 { "rfl", &ddb_regs.tf_rflags, NULL },
113 { "r8", &ddb_regs.tf_r8, NULL },
114 { "r9", &ddb_regs.tf_r9, NULL },
115 { "r10", &ddb_regs.tf_r10, NULL },
116 { "r11", &ddb_regs.tf_r11, NULL },
117 { "r12", &ddb_regs.tf_r12, NULL },
118 { "r13", &ddb_regs.tf_r13, NULL },
119 { "r14", &ddb_regs.tf_r14, NULL },
120 { "r15", &ddb_regs.tf_r15, NULL },
121 { "dr0", NULL, db_dr0 },
122 { "dr1", NULL, db_dr1 },
123 { "dr2", NULL, db_dr2 },
124 { "dr3", NULL, db_dr3 },
125 { "dr4", NULL, db_dr4 },
126 { "dr5", NULL, db_dr5 },
127 { "dr6", NULL, db_dr6 },
128 { "dr7", NULL, db_dr7 },
130 struct db_variable *db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
135 #define INKERNEL(va) (((vm_offset_t)(va)) >= USRSTACK)
138 struct amd64_frame *f_frame;
148 static void db_nextframe(struct amd64_frame **, db_addr_t *);
149 static int db_numargs(struct amd64_frame *);
150 static void db_print_stack_entry(const char *, int, char **, long *, db_addr_t);
151 static void dl_symbol_values(long callpc, const char **name);
154 static char *watchtype_str(int type);
155 static int kamd64_set_watch(int watchnum, unsigned int watchaddr,
156 int size, int access, struct dbreg * d);
157 static int kamd64_clr_watch(int watchnum, struct dbreg * d);
158 int db_md_set_watchpoint(db_expr_t addr, db_expr_t size);
159 int db_md_clr_watchpoint(db_expr_t addr, db_expr_t size);
160 void db_md_list_watchpoints(void);
164 * Figure out how many arguments were passed into the frame at "fp".
167 db_numargs(struct amd64_frame *fp)
170 return (0); /* regparm, needs dwarf2 info */
177 argp = (int *)db_get_value((int)&fp->f_retaddr, 4, FALSE);
179 * XXX etext is wrong for LKMs. We should attempt to interpret
180 * the instruction at the return address in all cases. This
181 * may require better fault handling.
183 if (argp < (int *)btext || argp >= (int *)etext) {
186 inst = db_get_value((int)argp, 4, FALSE);
187 if ((inst & 0xff) == 0x59) /* popl %ecx */
189 else if ((inst & 0xffff) == 0xc483) /* addl $Ibs, %esp */
190 args = ((inst >> 16) & 0xff) / 4;
201 db_print_stack_entry(const char *name, int narg, char **argnp, long *argp,
204 db_printf("%s(", name);
207 db_printf("%s=", *argnp++);
208 db_printf("%r", db_get_value((long)argp, 8, FALSE));
214 db_printsym(callpc, DB_STGY_PROC);
219 * Figure out the next frame up in the call stack.
222 db_nextframe(struct amd64_frame **fp, db_addr_t *ip)
224 struct trapframe *tf;
228 const char *sym, *name;
230 rip = db_get_value((long) &(*fp)->f_retaddr, 8, FALSE);
231 rbp = db_get_value((long) &(*fp)->f_frame, 8, FALSE);
234 * Figure out frame type.
239 sym = db_search_symbol(rip, DB_STGY_ANY, &offset);
240 db_symbol_values(sym, &name, NULL);
241 dl_symbol_values(rip, &name);
243 if (!strcmp(name, "calltrap")) {
245 } else if (!strncmp(name, "Xresume", 7)) {
246 frame_type = INTERRUPT;
247 } else if (!strcmp(name, "_Xsyscall")) {
248 frame_type = SYSCALL;
253 * Normal frames need no special processing.
255 if (frame_type == NORMAL) {
256 *ip = (db_addr_t) rip;
257 *fp = (struct amd64_frame *) rbp;
261 db_print_stack_entry(name, 0, 0, 0, rip);
264 * Point to base of trapframe which is just above the
267 tf = (struct trapframe *)((long)*fp + 16);
270 rsp = (ISPL(tf->tf_cs) == SEL_UPL) ? tf->tf_rsp : (long)&tf->tf_rsp;
272 rsp = (long)&tf->tf_rsp;
274 switch (frame_type) {
280 "--- trap %#r, rip = %#r, rsp = %#r, rbp = %#r ---\n",
281 tf->tf_trapno, rip, rsp, rbp);
289 "--- syscall %#r, rip = %#r, rsp = %#r, rbp = %#r ---\n",
290 tf->tf_rax, rip, rsp, rbp);
294 tf = (struct trapframe *)((long)*fp + 16);
299 "--- interrupt, rip = %#r, rsp = %#r, rbp = %#r ---\n",
307 *ip = (db_addr_t) rip;
308 *fp = (struct amd64_frame *) rbp;
312 db_stack_trace_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
315 struct amd64_frame *frame;
325 frame = (struct amd64_frame *)BP_REGS(&ddb_regs);
327 frame = (struct amd64_frame *)(SP_REGS(&ddb_regs) - 8);
328 callpc = PC_REGS(&ddb_regs);
331 * Look for something that might be a frame pointer, just as
334 frame = (struct amd64_frame *)addr;
335 for (i = 0; i < 4096; i += 8) {
336 struct amd64_frame *check;
338 check = (struct amd64_frame *)db_get_value((long)((char *)&frame->f_frame + i), 8, FALSE);
339 if ((char *)check - (char *)frame >= 0 &&
340 (char *)check - (char *)frame < 4096
344 db_printf("%p does not look like a stack frame, skipping\n", (char *)&frame->f_frame + i);
347 db_printf("Unable to find anything that looks like a stack frame\n");
350 frame = (void *)((char *)frame + i);
351 db_printf("Trace beginning at frame %p\n", frame);
352 callpc = (db_addr_t)db_get_value((long)&frame->f_retaddr, 8, FALSE);
357 struct amd64_frame *actframe;
363 char *argnames[MAXNARG], **argnp = NULL;
365 sym = db_search_symbol(callpc, DB_STGY_ANY, &offset);
366 db_symbol_values(sym, &name, NULL);
367 dl_symbol_values(callpc, &name);
370 * Attempt to determine a (possibly fake) frame that gives
371 * the caller's pc. It may differ from `frame' if the
372 * current function never sets up a standard frame or hasn't
373 * set one up yet or has just discarded one. The last two
374 * cases can be guessed fairly reliably for code generated
375 * by gcc. The first case is too much trouble to handle in
376 * general because the amount of junk on the stack depends
377 * on the pc (the special handling of "calltrap", etc. in
378 * db_nextframe() works because the `next' pc is special).
385 instr = db_get_value(callpc, 4, FALSE);
386 if ((instr & 0xffffffff) == 0xe5894855) {
387 /* pushq %rbp; movq %rsp, %rbp */
388 actframe = (struct amd64_frame *)
389 (SP_REGS(&ddb_regs) - 8);
390 } else if ((instr & 0xffffff) == 0xe58948) {
391 /* movq %rsp, %rbp */
392 actframe = (struct amd64_frame *)
394 if (ddb_regs.tf_rbp == 0) {
395 /* Fake caller's frame better. */
398 } else if ((instr & 0xff) == 0xc3) {
400 actframe = (struct amd64_frame *)
401 (SP_REGS(&ddb_regs) - 8);
402 } else if (offset == 0) {
403 /* Probably a symbol in assembler code. */
404 actframe = (struct amd64_frame *)
405 (SP_REGS(&ddb_regs) - 8);
407 } else if (name != NULL &&
408 strcmp(name, "fork_trampoline") == 0) {
410 * Don't try to walk back on a stack for a
411 * process that hasn't actually been run yet.
413 db_print_stack_entry(name, 0, 0, 0, callpc);
419 argp = &actframe->f_arg0;
421 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
424 narg = db_numargs(frame);
427 db_print_stack_entry(name, narg, argnp, argp, callpc);
429 if (actframe != frame) {
430 /* `frame' belongs to caller. */
432 db_get_value((long)&actframe->f_retaddr, 8, FALSE);
436 db_nextframe(&frame, &callpc);
443 print_backtrace(void)
447 __asm __volatile("movq %%rbp, %0" : "=r" (rbp));
448 db_stack_trace_cmd(rbp, 1, -1, NULL);
451 #define DB_DRX_FUNC(reg) \
453 db_ ## reg (struct db_variable *vp, db_expr_t *valuep, int op) \
455 if (op == DB_VAR_GET) \
456 *valuep = r ## reg (); \
458 load_ ## reg (*valuep); \
473 kamd64_set_watch(int watchnum, unsigned int watchaddr, int size, int access,
479 if (watchnum == -1) {
480 for (i = 0, mask = 0x3; i < 4; i++, mask <<= 2)
481 if ((d->dr[7] & mask) == 0)
491 size = 1; /* size must be 1 for an execution breakpoint */
493 case DBREG_DR7_WRONLY:
501 * we can watch a 1, 2, 4, or 8 byte sized location
521 /* clear the bits we are about to affect */
522 d->dr[7] &= ~((0x3 << (watchnum * 2)) | (0x0f << (watchnum * 4 + 16)));
524 /* set drN register to the address, N=watchnum */
525 DBREG_DRX(d, watchnum) = watchaddr;
527 /* enable the watchpoint */
528 d->dr[7] |= (0x2 << (watchnum * 2)) | (mask << (watchnum * 4 + 16));
535 kamd64_clr_watch(int watchnum, struct dbreg *d)
537 if (watchnum < 0 || watchnum >= 4)
540 d->dr[7] &= ~((0x3 << (watchnum * 2)) | (0x0f << (watchnum * 4 + 16)));
541 DBREG_DRX(d, watchnum) = 0;
548 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
554 fill_dbregs(NULL, &d);
557 for (i = 0; i < 4; i++) {
558 if ((d.dr[7] & (3 << (i * 2))) == 0)
562 if (avail * 8 < size)
565 for (i=0; i < 4 && (size != 0); i++) {
566 if ((d.dr[7] & (3 << (i * 2))) == 0) {
567 if (size >= 8 || (avail == 1 && size > 4))
575 kamd64_set_watch(i, addr, wsize, DBREG_DR7_WRONLY, &d);
581 set_dbregs(NULL, &d);
587 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
592 fill_dbregs(NULL, &d);
594 for(i = 0; i < 4; i++) {
595 if (d.dr[7] & (3 << (i * 2))) {
596 if ((DBREG_DRX((&d), i) >= addr) &&
597 (DBREG_DRX((&d), i) < addr + size))
598 kamd64_clr_watch(i, &d);
602 set_dbregs(NULL, &d);
608 watchtype_str(int type)
615 case DBREG_DR7_WRONLY:
623 db_md_list_watchpoints(void)
628 fill_dbregs(NULL, &d);
630 db_printf("\nhardware watchpoints:\n");
631 db_printf(" watch status type len address\n"
632 " ----- -------- ---------- --- ----------\n");
633 for (i = 0; i < 4; i++) {
634 if (d.dr[7] & (0x03 << (i * 2))) {
636 type = (d.dr[7] >> (16 + (i * 4))) & 3;
637 len = (d.dr[7] >> (16 + (i * 4) + 2)) & 3;
638 db_printf(" %-5d %-8s %10s %3d 0x%08x\n",
639 i, "enabled", watchtype_str(type),
640 len + 1, DBREG_DRX((&d), i));
642 db_printf(" %-5d disabled\n", i);
646 db_printf("\ndebug register values:\n");
647 for (i = 0; i < 8; i++)
648 db_printf(" dr%d 0x%08x\n", i, DBREG_DRX((&d),i));
653 * See if dladdr() can get the symbol name via the standard dynamic loader.
657 dl_symbol_values(long callpc, const char **name)
663 if (dladdr((const void *)callpc, &info) != 0) {
664 if (info.dli_saddr <= (const void *)callpc)
665 *name = info.dli_sname;