2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
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.
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.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
36 #include <sys/stack.h>
38 #include <machine/cpu.h>
39 #include <machine/md_var.h>
40 #include <machine/pcb.h>
41 #include <machine/stack.h>
44 #include <vm/vm_param.h>
48 #include <ddb/db_access.h>
49 #include <ddb/db_sym.h>
50 #include <ddb/db_variables.h>
52 static db_varfcn_t db_frame;
53 static db_varfcn_t db_frame_seg;
55 CTASSERT(sizeof(struct dbreg) == sizeof(((struct pcpu *)NULL)->pc_dbreg));
58 * Machine register set.
60 #define DB_OFFSET(x) (db_expr_t *)offsetof(struct trapframe, x)
61 struct db_variable db_regs[] = {
62 { "cs", DB_OFFSET(tf_cs), db_frame_seg },
63 { "ds", DB_OFFSET(tf_ds), db_frame_seg },
64 { "es", DB_OFFSET(tf_es), db_frame_seg },
65 { "fs", DB_OFFSET(tf_fs), db_frame_seg },
66 { "gs", DB_OFFSET(tf_gs), db_frame_seg },
67 { "ss", DB_OFFSET(tf_ss), db_frame_seg },
68 { "rax", DB_OFFSET(tf_rax), db_frame },
69 { "rcx", DB_OFFSET(tf_rcx), db_frame },
70 { "rdx", DB_OFFSET(tf_rdx), db_frame },
71 { "rbx", DB_OFFSET(tf_rbx), db_frame },
72 { "rsp", DB_OFFSET(tf_rsp), db_frame },
73 { "rbp", DB_OFFSET(tf_rbp), db_frame },
74 { "rsi", DB_OFFSET(tf_rsi), db_frame },
75 { "rdi", DB_OFFSET(tf_rdi), db_frame },
76 { "r8", DB_OFFSET(tf_r8), db_frame },
77 { "r9", DB_OFFSET(tf_r9), db_frame },
78 { "r10", DB_OFFSET(tf_r10), db_frame },
79 { "r11", DB_OFFSET(tf_r11), db_frame },
80 { "r12", DB_OFFSET(tf_r12), db_frame },
81 { "r13", DB_OFFSET(tf_r13), db_frame },
82 { "r14", DB_OFFSET(tf_r14), db_frame },
83 { "r15", DB_OFFSET(tf_r15), db_frame },
84 { "rip", DB_OFFSET(tf_rip), db_frame },
85 { "rflags", DB_OFFSET(tf_rflags), db_frame },
87 struct db_variable *db_eregs = db_regs + nitems(db_regs);
90 db_frame_seg(struct db_variable *vp, db_expr_t *valuep, int op)
94 if (kdb_frame == NULL)
97 reg = (uint16_t *)((uintptr_t)kdb_frame + (db_expr_t)vp->valuep);
106 db_frame(struct db_variable *vp, db_expr_t *valuep, int op)
110 if (kdb_frame == NULL)
113 reg = (long *)((uintptr_t)kdb_frame + (db_expr_t)vp->valuep);
114 if (op == DB_VAR_GET)
126 static void db_nextframe(struct amd64_frame **, db_addr_t *, struct thread *);
127 static void db_print_stack_entry(const char *, db_addr_t, void *);
128 static void decode_syscall(int, struct thread *);
131 db_print_stack_entry(const char *name, db_addr_t callpc, void *frame)
134 db_printf("%s() at ", name != NULL ? name : "??");
135 db_printsym(callpc, DB_STGY_PROC);
137 db_printf("/frame 0x%lx", (register_t)frame);
142 * Figure out the next frame up in the call stack.
145 db_nextframe(struct amd64_frame **fp, db_addr_t *ip, struct thread *td)
147 struct trapframe *tf;
154 rip = db_get_value((long) &(*fp)->f_retaddr, 8, FALSE);
155 rbp = db_get_value((long) &(*fp)->f_frame, 8, FALSE);
158 * Figure out frame type. We look at the address just before
159 * the saved instruction pointer as the saved EIP is after the
160 * call function, and if the function being called is marked as
161 * dead (such as panic() at the end of dblfault_handler()), then
162 * the instruction at the saved EIP will be part of a different
163 * function (syscall() in this example) rather than the one that
164 * actually made the call.
167 sym = db_search_symbol(rip - 1, DB_STGY_ANY, &offset);
168 db_symbol_values(sym, &name, NULL);
170 if (strcmp(name, "calltrap") == 0 ||
171 strcmp(name, "fork_trampoline") == 0 ||
172 strcmp(name, "mchk_calltrap") == 0 ||
173 strcmp(name, "nmi_calltrap") == 0 ||
174 strcmp(name, "Xdblfault") == 0)
176 else if (strncmp(name, "Xatpic_intr", 11) == 0 ||
177 strncmp(name, "Xapic_isr", 9) == 0 ||
178 strcmp(name, "Xxen_intr_upcall") == 0 ||
179 strcmp(name, "Xtimerint") == 0 ||
180 strcmp(name, "Xipi_intr_bitmap_handler") == 0 ||
181 strcmp(name, "Xcpustop") == 0 ||
182 strcmp(name, "Xcpususpend") == 0 ||
183 strcmp(name, "Xrendezvous") == 0)
184 frame_type = INTERRUPT;
185 else if (strcmp(name, "Xfast_syscall") == 0 ||
186 strcmp(name, "Xfast_syscall_pti") == 0 ||
187 strcmp(name, "fast_syscall_common") == 0)
188 frame_type = SYSCALL;
189 #ifdef COMPAT_FREEBSD32
190 else if (strcmp(name, "Xint0x80_syscall") == 0)
191 frame_type = SYSCALL;
196 * Normal frames need no special processing.
198 if (frame_type == NORMAL) {
199 *ip = (db_addr_t) rip;
200 *fp = (struct amd64_frame *) rbp;
204 db_print_stack_entry(name, rip, &(*fp)->f_frame);
207 * Point to base of trapframe which is just above the
210 tf = (struct trapframe *)((long)*fp + 16);
212 if (INKERNEL((long) tf)) {
216 switch (frame_type) {
218 db_printf("--- trap %#r", tf->tf_trapno);
221 db_printf("--- syscall");
222 db_decode_syscall(tf->tf_rax, td);
225 db_printf("--- interrupt");
228 panic("The moon has moved again.");
230 db_printf(", rip = %#lr, rsp = %#lr, rbp = %#lr ---\n", rip,
234 *ip = (db_addr_t) rip;
235 *fp = (struct amd64_frame *) rbp;
238 static int __nosanitizeaddress __nosanitizememory
239 db_backtrace(struct thread *td, struct trapframe *tf, struct amd64_frame *frame,
240 db_addr_t pc, register_t sp, int count)
242 struct amd64_frame *actframe;
252 while (count-- && !db_pager_quit) {
253 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
254 db_symbol_values(sym, &name, NULL);
257 * Attempt to determine a (possibly fake) frame that gives
258 * the caller's pc. It may differ from `frame' if the
259 * current function never sets up a standard frame or hasn't
260 * set one up yet or has just discarded one. The last two
261 * cases can be guessed fairly reliably for code generated
262 * by gcc. The first case is too much trouble to handle in
263 * general because the amount of junk on the stack depends
264 * on the pc (the special handling of "calltrap", etc. in
265 * db_nextframe() works because the `next' pc is special).
270 if (sym == C_DB_SYM_NULL && sp != 0) {
272 * If a symbol couldn't be found, we've probably
273 * jumped to a bogus location, so try and use
274 * the return address to find our caller.
276 db_print_stack_entry(name, pc, NULL);
277 pc = db_get_value(sp, 8, FALSE);
278 if (db_search_symbol(pc, DB_STGY_PROC,
279 &offset) == C_DB_SYM_NULL)
282 } else if (tf != NULL) {
285 instr = db_get_value(pc, 4, FALSE);
286 if ((instr & 0xffffffff) == 0xe5894855) {
287 /* pushq %rbp; movq %rsp, %rbp */
288 actframe = (void *)(tf->tf_rsp - 8);
289 } else if ((instr & 0xffffff) == 0xe58948) {
290 /* movq %rsp, %rbp */
291 actframe = (void *)tf->tf_rsp;
292 if (tf->tf_rbp == 0) {
293 /* Fake frame better. */
296 } else if ((instr & 0xff) == 0xc3) {
298 actframe = (void *)(tf->tf_rsp - 8);
299 } else if (offset == 0) {
300 /* Probably an assembler symbol. */
301 actframe = (void *)(tf->tf_rsp - 8);
303 } else if (name != NULL &&
304 strcmp(name, "fork_trampoline") == 0) {
306 * Don't try to walk back on a stack for a
307 * process that hasn't actually been run yet.
309 db_print_stack_entry(name, pc, actframe);
314 db_print_stack_entry(name, pc, actframe);
316 if (actframe != frame) {
317 /* `frame' belongs to caller. */
319 db_get_value((long)&actframe->f_retaddr, 8, FALSE);
323 db_nextframe(&frame, &pc, td);
325 if (INKERNEL((long)pc) && !INKERNEL((long)frame)) {
326 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
327 db_symbol_values(sym, &name, NULL);
328 db_print_stack_entry(name, pc, frame);
331 if (!INKERNEL((long) frame)) {
342 struct amd64_frame *frame;
346 __asm __volatile("movq %%rbp,%0" : "=r" (rbp));
347 frame = (struct amd64_frame *)rbp;
348 callpc = (db_addr_t)db_get_value((long)&frame->f_retaddr, 8, FALSE);
349 frame = frame->f_frame;
350 db_backtrace(curthread, NULL, frame, callpc, 0, -1);
354 db_trace_thread(struct thread *thr, int count)
357 struct trapframe *tf;
359 ctx = kdb_thr_ctx(thr);
360 tf = thr == kdb_thread ? kdb_frame : NULL;
361 return (db_backtrace(thr, tf, (struct amd64_frame *)ctx->pcb_rbp,
362 ctx->pcb_rip, ctx->pcb_rsp, count));
366 db_md_list_watchpoints(void)
369 dbreg_list_watchpoints();