xref: /freebsd/sys/amd64/amd64/db_trace.c (revision 4b9d6057)
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/param.h>
28 #include <sys/systm.h>
29 #include <sys/kdb.h>
30 #include <sys/proc.h>
31 #include <sys/reg.h>
32 #include <sys/smp.h>
33 #include <sys/stack.h>
34 
35 #include <machine/cpu.h>
36 #include <machine/md_var.h>
37 #include <machine/pcb.h>
38 #include <machine/stack.h>
39 
40 #include <vm/vm.h>
41 #include <vm/vm_param.h>
42 #include <vm/pmap.h>
43 
44 #include <ddb/ddb.h>
45 #include <ddb/db_access.h>
46 #include <ddb/db_sym.h>
47 #include <ddb/db_variables.h>
48 
49 static db_varfcn_t db_frame;
50 static db_varfcn_t db_frame_seg;
51 
52 CTASSERT(sizeof(struct dbreg) == sizeof(((struct pcpu *)NULL)->pc_dbreg));
53 
54 /*
55  * Machine register set.
56  */
57 #define	DB_OFFSET(x)	(db_expr_t *)offsetof(struct trapframe, x)
58 struct db_variable db_regs[] = {
59 	{ "cs",		DB_OFFSET(tf_cs),	db_frame_seg },
60 	{ "ds",		DB_OFFSET(tf_ds),	db_frame_seg },
61 	{ "es",		DB_OFFSET(tf_es),	db_frame_seg },
62 	{ "fs",		DB_OFFSET(tf_fs),	db_frame_seg },
63 	{ "gs",		DB_OFFSET(tf_gs),	db_frame_seg },
64 	{ "ss",		DB_OFFSET(tf_ss),	db_frame_seg },
65 	{ "rax",	DB_OFFSET(tf_rax),	db_frame },
66 	{ "rcx",        DB_OFFSET(tf_rcx),	db_frame },
67 	{ "rdx",	DB_OFFSET(tf_rdx),	db_frame },
68 	{ "rbx",	DB_OFFSET(tf_rbx),	db_frame },
69 	{ "rsp",	DB_OFFSET(tf_rsp),	db_frame },
70 	{ "rbp",	DB_OFFSET(tf_rbp),	db_frame },
71 	{ "rsi",	DB_OFFSET(tf_rsi),	db_frame },
72 	{ "rdi",	DB_OFFSET(tf_rdi),	db_frame },
73 	{ "r8",		DB_OFFSET(tf_r8),	db_frame },
74 	{ "r9",		DB_OFFSET(tf_r9),	db_frame },
75 	{ "r10",	DB_OFFSET(tf_r10),	db_frame },
76 	{ "r11",	DB_OFFSET(tf_r11),	db_frame },
77 	{ "r12",	DB_OFFSET(tf_r12),	db_frame },
78 	{ "r13",	DB_OFFSET(tf_r13),	db_frame },
79 	{ "r14",	DB_OFFSET(tf_r14),	db_frame },
80 	{ "r15",	DB_OFFSET(tf_r15),	db_frame },
81 	{ "rip",	DB_OFFSET(tf_rip),	db_frame },
82 	{ "rflags",	DB_OFFSET(tf_rflags),	db_frame },
83 };
84 struct db_variable *db_eregs = db_regs + nitems(db_regs);
85 
86 static int
87 db_frame_seg(struct db_variable *vp, db_expr_t *valuep, int op)
88 {
89 	uint16_t *reg;
90 
91 	if (kdb_frame == NULL)
92 		return (0);
93 
94 	reg = (uint16_t *)((uintptr_t)kdb_frame + (db_expr_t)vp->valuep);
95 	if (op == DB_VAR_GET)
96 		*valuep = *reg;
97 	else
98 		*reg = *valuep;
99 	return (1);
100 }
101 
102 static int
103 db_frame(struct db_variable *vp, db_expr_t *valuep, int op)
104 {
105 	long *reg;
106 
107 	if (kdb_frame == NULL)
108 		return (0);
109 
110 	reg = (long *)((uintptr_t)kdb_frame + (db_expr_t)vp->valuep);
111 	if (op == DB_VAR_GET)
112 		*valuep = *reg;
113 	else
114 		*reg = *valuep;
115 	return (1);
116 }
117 
118 #define NORMAL		0
119 #define	TRAP		1
120 #define	INTERRUPT	2
121 #define	SYSCALL		3
122 
123 static void
124 db_print_stack_entry(const char *name, db_addr_t callpc, db_addr_t frame)
125 {
126 
127 	db_printf("%s() at ", name != NULL ? name : "??");
128 	db_printsym(callpc, DB_STGY_PROC);
129 	if (frame != 0)
130 		db_printf("/frame %#lx", frame);
131 	db_printf("\n");
132 }
133 
134 /*
135  * Figure out the next frame up in the call stack.
136  */
137 static void
138 db_nextframe(db_addr_t *fp, db_addr_t *ip, struct thread *td)
139 {
140 	struct trapframe *tf;
141 	db_addr_t tf_addr;
142 	int frame_type;
143 	long rip, rsp, rbp;
144 	db_expr_t offset;
145 	c_db_sym_t sym;
146 	const char *name;
147 
148 	rip = db_get_value(*fp + offsetof(struct amd64_frame, f_retaddr), 8,
149 	    FALSE);
150 	rbp = db_get_value(*fp + offsetof(struct amd64_frame, f_frame), 8,
151 	    FALSE);
152 
153 	/*
154 	 * Figure out frame type.  We look at the address just before
155 	 * the saved instruction pointer as the saved EIP is after the
156 	 * call function, and if the function being called is marked as
157 	 * dead (such as panic() at the end of dblfault_handler()), then
158 	 * the instruction at the saved EIP will be part of a different
159 	 * function (syscall() in this example) rather than the one that
160 	 * actually made the call.
161 	 */
162 	frame_type = NORMAL;
163 	sym = db_search_symbol(rip - 1, DB_STGY_ANY, &offset);
164 	db_symbol_values(sym, &name, NULL);
165 	if (name != NULL) {
166 		if (strcmp(name, "calltrap") == 0 ||
167 		    strcmp(name, "fork_trampoline") == 0 ||
168 		    strcmp(name, "mchk_calltrap") == 0 ||
169 		    strcmp(name, "nmi_calltrap") == 0 ||
170 		    strcmp(name, "Xdblfault") == 0)
171 			frame_type = TRAP;
172 		else if (strncmp(name, "Xatpic_intr", 11) == 0 ||
173 		    strncmp(name, "Xapic_isr", 9) == 0 ||
174 		    strcmp(name, "Xxen_intr_upcall") == 0 ||
175 		    strcmp(name, "Xtimerint") == 0 ||
176 		    strcmp(name, "Xipi_intr_bitmap_handler") == 0 ||
177 		    strcmp(name, "Xcpustop") == 0 ||
178 		    strcmp(name, "Xcpususpend") == 0 ||
179 		    strcmp(name, "Xrendezvous") == 0)
180 			frame_type = INTERRUPT;
181 		else if (strcmp(name, "Xfast_syscall") == 0 ||
182 		    strcmp(name, "Xfast_syscall_pti") == 0 ||
183 		    strcmp(name, "fast_syscall_common") == 0)
184 			frame_type = SYSCALL;
185 #ifdef COMPAT_FREEBSD32
186 		else if (strcmp(name, "Xint0x80_syscall") == 0)
187 			frame_type = SYSCALL;
188 #endif
189 	}
190 
191 	/*
192 	 * Normal frames need no special processing.
193 	 */
194 	if (frame_type == NORMAL) {
195 		*ip = rip;
196 		*fp = rbp;
197 		return;
198 	}
199 
200 	db_print_stack_entry(name, rip, *fp);
201 
202 	/*
203 	 * Point to base of trapframe which is just above the
204 	 * current frame.
205 	 */
206 	tf_addr = *fp + 16;
207 
208 	if (!__is_aligned(tf_addr, _Alignof(*tf)) || !INKERNEL(tf_addr)) {
209 		db_printf("--- invalid trapframe %p\n", (void *)tf_addr);
210 		*ip = 0;
211 		*fp = 0;
212 		return;
213 	}
214 	tf = (struct trapframe *)tf_addr;
215 
216 	rsp = tf->tf_rsp;
217 	rip = tf->tf_rip;
218 	rbp = tf->tf_rbp;
219 	switch (frame_type) {
220 	case TRAP:
221 		db_printf("--- trap %#r", tf->tf_trapno);
222 		break;
223 	case SYSCALL:
224 		db_printf("--- syscall");
225 		db_decode_syscall(td, tf->tf_rax);
226 		break;
227 	case INTERRUPT:
228 		db_printf("--- interrupt");
229 		break;
230 	default:
231 		__assert_unreachable();
232 	}
233 	db_printf(", rip = %#lr, rsp = %#lr, rbp = %#lr ---\n", rip, rsp, rbp);
234 
235 	*ip = rip;
236 	*fp = rbp;
237 }
238 
239 static int __nosanitizeaddress __nosanitizememory
240 db_backtrace(struct thread *td, struct trapframe *tf, db_addr_t frame,
241     db_addr_t pc, register_t sp, int count)
242 {
243 	db_addr_t actframe;
244 	const char *name;
245 	db_expr_t offset;
246 	c_db_sym_t sym;
247 	boolean_t first;
248 
249 	if (count == -1)
250 		count = 1024;
251 
252 	first = TRUE;
253 	while (count-- && !db_pager_quit) {
254 		sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
255 		db_symbol_values(sym, &name, NULL);
256 
257 		/*
258 		 * Attempt to determine a (possibly fake) frame that gives
259 		 * the caller's pc.  It may differ from `frame' if the
260 		 * current function never sets up a standard frame or hasn't
261 		 * set one up yet or has just discarded one.  The last two
262 		 * cases can be guessed fairly reliably for code generated
263 		 * by gcc.  The first case is too much trouble to handle in
264 		 * general because the amount of junk on the stack depends
265 		 * on the pc (the special handling of "calltrap", etc. in
266 		 * db_nextframe() works because the `next' pc is special).
267 		 */
268 		actframe = frame;
269 		if (first) {
270 			first = FALSE;
271 			if (sym == C_DB_SYM_NULL && sp != 0) {
272 				/*
273 				 * If a symbol couldn't be found, we've probably
274 				 * jumped to a bogus location, so try and use
275 				 * the return address to find our caller.
276 				 */
277 				db_print_stack_entry(name, pc, 0);
278 				pc = db_get_value(sp, 8, FALSE);
279 				if (db_search_symbol(pc, DB_STGY_PROC,
280 				    &offset) == C_DB_SYM_NULL)
281 					break;
282 				continue;
283 			} else if (tf != NULL) {
284 				int instr;
285 
286 				instr = db_get_value(pc, 4, FALSE);
287 				if ((instr & 0xffffffff) == 0xe5894855) {
288 					/* pushq %rbp; movq %rsp, %rbp */
289 					actframe = tf->tf_rsp - 8;
290 				} else if ((instr & 0xffffff) == 0xe58948) {
291 					/* movq %rsp, %rbp */
292 					actframe = tf->tf_rsp;
293 					if (tf->tf_rbp == 0) {
294 						/* Fake frame better. */
295 						frame = actframe;
296 					}
297 				} else if ((instr & 0xff) == 0xc3) {
298 					/* ret */
299 					actframe = tf->tf_rsp - 8;
300 				} else if (offset == 0) {
301 					/* Probably an assembler symbol. */
302 					actframe = tf->tf_rsp - 8;
303 				}
304 			} else if (name != NULL &&
305 			    strcmp(name, "fork_trampoline") == 0) {
306 				/*
307 				 * Don't try to walk back on a stack for a
308 				 * process that hasn't actually been run yet.
309 				 */
310 				db_print_stack_entry(name, pc, actframe);
311 				break;
312 			}
313 		}
314 
315 		db_print_stack_entry(name, pc, actframe);
316 
317 		if (actframe != frame) {
318 			/* `frame' belongs to caller. */
319 			pc = db_get_value(actframe +
320 			    offsetof(struct amd64_frame, f_retaddr), 8, FALSE);
321 			continue;
322 		}
323 
324 		db_nextframe(&frame, &pc, td);
325 		if (frame == 0)
326 			break;
327 
328 		if (!__is_aligned(frame, _Alignof(struct amd64_frame)) ||
329 		    !INKERNEL(frame)) {
330 			if (INKERNEL(pc)) {
331 				sym = db_search_symbol(pc, DB_STGY_ANY,
332 				    &offset);
333 				db_symbol_values(sym, &name, NULL);
334 				db_print_stack_entry(name, pc, frame);
335 				break;
336 			}
337 			break;
338 		}
339 	}
340 
341 	return (0);
342 }
343 
344 void
345 db_trace_self(void)
346 {
347 	db_addr_t callpc, frame;
348 	register_t rbp;
349 
350 	__asm __volatile("movq %%rbp,%0" : "=r" (rbp));
351 	callpc = db_get_value(rbp + offsetof(struct amd64_frame, f_retaddr), 8,
352 	    FALSE);
353 	frame = db_get_value(rbp + offsetof(struct amd64_frame, f_frame), 8,
354 	    FALSE);
355 	db_backtrace(curthread, NULL, frame, callpc, 0, -1);
356 }
357 
358 int
359 db_trace_thread(struct thread *thr, int count)
360 {
361 	struct pcb *ctx;
362 	struct trapframe *tf;
363 
364 	ctx = kdb_thr_ctx(thr);
365 	tf = thr == kdb_thread ? kdb_frame : NULL;
366 	return (db_backtrace(thr, tf, ctx->pcb_rbp, ctx->pcb_rip, ctx->pcb_rsp,
367 	    count));
368 }
369 
370 void
371 db_md_list_watchpoints(void)
372 {
373 
374 	dbreg_list_watchpoints();
375 }
376