kill db_print_backtrace()
[dragonfly.git] / sys / platform / vkernel / i386 / db_trace.c
CommitLineData
6f7b98e0
MD
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 * $FreeBSD: src/sys/i386/i386/db_trace.c,v 1.35.2.3 2002/02/21 22:31:25 silby Exp $
956a62ec 27 * $DragonFly: src/sys/platform/vkernel/i386/db_trace.c,v 1.9 2008/02/03 15:10:26 nth Exp $
6f7b98e0
MD
28 */
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/linker_set.h>
33#include <sys/lock.h>
34#include <sys/proc.h>
35#include <sys/reg.h>
36
37#include <machine/cpu.h>
38#include <machine/md_var.h>
39
40#include <vm/vm.h>
41#include <vm/vm_param.h>
42#include <vm/pmap.h>
43#include <vm/vm_map.h>
44#include <ddb/ddb.h>
35ae59b9 45#include <dlfcn.h> /* DLL */
6f7b98e0
MD
46
47#include <sys/user.h>
48
49#include <ddb/db_access.h>
50#include <ddb/db_sym.h>
51#include <ddb/db_variables.h>
52
956a62ec 53static int db_dr(struct db_variable *vp, db_expr_t *valuep, int op);
6f7b98e0
MD
54
55/*
56 * Machine register set.
57 */
58struct db_variable db_regs[] = {
59 { "cs", &ddb_regs.tf_cs, FCN_NULL },
60 { "ds", &ddb_regs.tf_ds, FCN_NULL },
61 { "es", &ddb_regs.tf_es, FCN_NULL },
62 { "fs", &ddb_regs.tf_fs, FCN_NULL },
6f7b98e0 63 { "gs", &ddb_regs.tf_gs, FCN_NULL },
6f7b98e0
MD
64 { "ss", &ddb_regs.tf_ss, FCN_NULL },
65 { "eax", &ddb_regs.tf_eax, FCN_NULL },
66 { "ecx", &ddb_regs.tf_ecx, FCN_NULL },
67 { "edx", &ddb_regs.tf_edx, FCN_NULL },
68 { "ebx", &ddb_regs.tf_ebx, FCN_NULL },
69 { "esp", &ddb_regs.tf_esp, FCN_NULL },
70 { "ebp", &ddb_regs.tf_ebp, FCN_NULL },
71 { "esi", &ddb_regs.tf_esi, FCN_NULL },
72 { "edi", &ddb_regs.tf_edi, FCN_NULL },
73 { "eip", &ddb_regs.tf_eip, FCN_NULL },
74 { "efl", &ddb_regs.tf_eflags, FCN_NULL },
956a62ec
NT
75 { "dr0", NULL, db_dr },
76 { "dr1", NULL, db_dr },
77 { "dr2", NULL, db_dr },
78 { "dr3", NULL, db_dr },
79 { "dr4", NULL, db_dr },
80 { "dr5", NULL, db_dr },
81 { "dr6", NULL, db_dr },
82 { "dr7", NULL, db_dr },
6f7b98e0
MD
83};
84struct db_variable *db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
85
86/*
87 * Stack trace.
88 */
89#define INKERNEL(va) (((vm_offset_t)(va)) >= USRSTACK)
90
91struct i386_frame {
92 struct i386_frame *f_frame;
93 int f_retaddr;
94 int f_arg0;
95};
96
97#define NORMAL 0
98#define TRAP 1
99#define INTERRUPT 2
100#define SYSCALL 3
101
102static void db_nextframe(struct i386_frame **, db_addr_t *);
103static int db_numargs(struct i386_frame *);
104static void db_print_stack_entry(const char *, int, char **, int *, db_addr_t);
35ae59b9 105static void dl_symbol_values(int callpc, const char **name);
6f7b98e0
MD
106
107
6f7b98e0
MD
108int db_md_set_watchpoint(db_expr_t addr, db_expr_t size);
109int db_md_clr_watchpoint(db_expr_t addr, db_expr_t size);
110void db_md_list_watchpoints(void);
111
112
113/*
114 * Figure out how many arguments were passed into the frame at "fp".
115 */
116static int
117db_numargs(struct i386_frame *fp)
118{
9ad680a3
MD
119 int args;
120#if 0
6f7b98e0
MD
121 int *argp;
122 int inst;
6f7b98e0
MD
123
124 argp = (int *)db_get_value((int)&fp->f_retaddr, 4, FALSE);
125 /*
126 * XXX etext is wrong for LKMs. We should attempt to interpret
127 * the instruction at the return address in all cases. This
128 * may require better fault handling.
129 */
130 if (argp < (int *)btext || argp >= (int *)etext) {
131 args = 5;
132 } else {
133 inst = db_get_value((int)argp, 4, FALSE);
134 if ((inst & 0xff) == 0x59) /* popl %ecx */
135 args = 1;
136 else if ((inst & 0xffff) == 0xc483) /* addl $Ibs, %esp */
137 args = ((inst >> 16) & 0xff) / 4;
138 else
139 args = 5;
140 }
9ad680a3
MD
141#endif
142 args = 5;
6f7b98e0
MD
143 return(args);
144}
145
146static void
147db_print_stack_entry(const char *name, int narg, char **argnp, int *argp,
148 db_addr_t callpc)
149{
150 db_printf("%s(", name);
151 while (narg) {
152 if (argnp)
153 db_printf("%s=", *argnp++);
154 db_printf("%r", db_get_value((int)argp, 4, FALSE));
155 argp++;
156 if (--narg != 0)
157 db_printf(",");
158 }
159 db_printf(") at ");
160 db_printsym(callpc, DB_STGY_PROC);
161 db_printf("\n");
162}
163
164/*
165 * Figure out the next frame up in the call stack.
166 */
167static void
168db_nextframe(struct i386_frame **fp, db_addr_t *ip)
169{
170 struct trapframe *tf;
171 int frame_type;
172 int eip, esp, ebp;
173 db_expr_t offset;
174 const char *sym, *name;
175
176 eip = db_get_value((int) &(*fp)->f_retaddr, 4, FALSE);
177 ebp = db_get_value((int) &(*fp)->f_frame, 4, FALSE);
178
179 /*
180 * Figure out frame type.
181 */
182
183 frame_type = NORMAL;
184
185 sym = db_search_symbol(eip, DB_STGY_ANY, &offset);
186 db_symbol_values(sym, &name, NULL);
35ae59b9 187 dl_symbol_values(eip, &name);
6f7b98e0
MD
188 if (name != NULL) {
189 if (!strcmp(name, "calltrap")) {
190 frame_type = TRAP;
191 } else if (!strncmp(name, "Xresume", 7)) {
192 frame_type = INTERRUPT;
193 } else if (!strcmp(name, "_Xsyscall")) {
194 frame_type = SYSCALL;
195 }
196 }
197
198 /*
199 * Normal frames need no special processing.
200 */
201 if (frame_type == NORMAL) {
202 *ip = (db_addr_t) eip;
203 *fp = (struct i386_frame *) ebp;
204 return;
205 }
206
207 db_print_stack_entry(name, 0, 0, 0, eip);
208
209 /*
210 * Point to base of trapframe which is just above the
211 * current frame.
212 */
213 tf = (struct trapframe *) ((int)*fp + 8);
214
9097944f 215#if 0
6f7b98e0 216 esp = (ISPL(tf->tf_cs) == SEL_UPL) ? tf->tf_esp : (int)&tf->tf_esp;
9097944f
MD
217#endif
218 esp = (int)&tf->tf_esp;
cced5af3 219
6f7b98e0
MD
220 switch (frame_type) {
221 case TRAP:
cced5af3 222 {
6f7b98e0
MD
223 eip = tf->tf_eip;
224 ebp = tf->tf_ebp;
225 db_printf(
226 "--- trap %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
227 tf->tf_trapno, eip, esp, ebp);
228 }
229 break;
230 case SYSCALL:
cced5af3 231 {
6f7b98e0
MD
232 eip = tf->tf_eip;
233 ebp = tf->tf_ebp;
234 db_printf(
235 "--- syscall %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
236 tf->tf_eax, eip, esp, ebp);
237 }
238 break;
239 case INTERRUPT:
240 tf = (struct trapframe *)((int)*fp + 16);
cced5af3 241 {
6f7b98e0
MD
242 eip = tf->tf_eip;
243 ebp = tf->tf_ebp;
244 db_printf(
245 "--- interrupt, eip = %#r, esp = %#r, ebp = %#r ---\n",
246 eip, esp, ebp);
247 }
248 break;
249 default:
250 break;
251 }
252
253 *ip = (db_addr_t) eip;
254 *fp = (struct i386_frame *) ebp;
255}
256
257void
258db_stack_trace_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
259 char *modif)
260{
261 struct i386_frame *frame;
262 int *argp;
263 db_addr_t callpc;
264 boolean_t first;
265 int i;
266
267 if (count == -1)
268 count = 1024;
269
270 if (!have_addr) {
271 frame = (struct i386_frame *)BP_REGS(&ddb_regs);
272 if (frame == NULL)
273 frame = (struct i386_frame *)(SP_REGS(&ddb_regs) - 4);
274 callpc = PC_REGS(&ddb_regs);
6f7b98e0
MD
275 } else {
276 /*
277 * Look for something that might be a frame pointer, just as
278 * a convenience.
279 */
280 frame = (struct i386_frame *)addr;
281 for (i = 0; i < 4096; i += 4) {
282 struct i386_frame *check;
283
284 check = (struct i386_frame *)db_get_value((int)((char *)&frame->f_frame + i), 4, FALSE);
285 if ((char *)check - (char *)frame >= 0 &&
286 (char *)check - (char *)frame < 4096
287 ) {
288 break;
289 }
290 db_printf("%p does not look like a stack frame, skipping\n", (char *)&frame->f_frame + i);
291 }
292 if (i == 4096) {
293 db_printf("Unable to find anything that looks like a stack frame\n");
294 return;
295 }
296 frame = (void *)((char *)frame + i);
297 db_printf("Trace beginning at frame %p\n", frame);
298 callpc = (db_addr_t)db_get_value((int)&frame->f_retaddr, 4, FALSE);
299 }
300
301 first = TRUE;
302 while (count--) {
303 struct i386_frame *actframe;
304 int narg;
305 const char * name;
306 db_expr_t offset;
307 c_db_sym_t sym;
308#define MAXNARG 16
309 char *argnames[MAXNARG], **argnp = NULL;
310
311 sym = db_search_symbol(callpc, DB_STGY_ANY, &offset);
312 db_symbol_values(sym, &name, NULL);
35ae59b9 313 dl_symbol_values(callpc, &name);
6f7b98e0
MD
314
315 /*
316 * Attempt to determine a (possibly fake) frame that gives
317 * the caller's pc. It may differ from `frame' if the
318 * current function never sets up a standard frame or hasn't
319 * set one up yet or has just discarded one. The last two
320 * cases can be guessed fairly reliably for code generated
321 * by gcc. The first case is too much trouble to handle in
322 * general because the amount of junk on the stack depends
323 * on the pc (the special handling of "calltrap", etc. in
324 * db_nextframe() works because the `next' pc is special).
325 */
326 actframe = frame;
327 if (first) {
328 if (!have_addr) {
329 int instr;
330
331 instr = db_get_value(callpc, 4, FALSE);
332 if ((instr & 0x00ffffff) == 0x00e58955) {
333 /* pushl %ebp; movl %esp, %ebp */
334 actframe = (struct i386_frame *)
335 (SP_REGS(&ddb_regs) - 4);
336 } else if ((instr & 0x0000ffff) == 0x0000e589) {
337 /* movl %esp, %ebp */
338 actframe = (struct i386_frame *)
339 SP_REGS(&ddb_regs);
340 if (ddb_regs.tf_ebp == 0) {
341 /* Fake caller's frame better. */
342 frame = actframe;
343 }
344 } else if ((instr & 0x000000ff) == 0x000000c3) {
345 /* ret */
346 actframe = (struct i386_frame *)
347 (SP_REGS(&ddb_regs) - 4);
348 } else if (offset == 0) {
349 /* Probably a symbol in assembler code. */
350 actframe = (struct i386_frame *)
351 (SP_REGS(&ddb_regs) - 4);
352 }
27735f7d
SS
353 } else if (name != NULL &&
354 strcmp(name, "fork_trampoline") == 0) {
6f7b98e0
MD
355 /*
356 * Don't try to walk back on a stack for a
357 * process that hasn't actually been run yet.
358 */
359 db_print_stack_entry(name, 0, 0, 0, callpc);
360 break;
361 }
362 first = FALSE;
363 }
364
365 argp = &actframe->f_arg0;
366 narg = MAXNARG;
367 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
368 argnp = argnames;
369 } else {
370 narg = db_numargs(frame);
371 }
372
373 db_print_stack_entry(name, narg, argnp, argp, callpc);
374
375 if (actframe != frame) {
376 /* `frame' belongs to caller. */
377 callpc = (db_addr_t)
378 db_get_value((int)&actframe->f_retaddr, 4, FALSE);
379 continue;
380 }
381
382 db_nextframe(&frame, &callpc);
cced5af3 383 if (frame == 0)
6f7b98e0 384 break;
6f7b98e0
MD
385 }
386}
387
388void
1e5fb84b 389print_backtrace(void)
6f7b98e0
MD
390{
391 register_t ebp;
392
393 __asm __volatile("movl %%ebp, %0" : "=r" (ebp));
394 db_stack_trace_cmd(ebp, 1, -1, NULL);
395}
396
6f7b98e0 397static int
956a62ec 398db_dr(struct db_variable *vp, db_expr_t *valuep, int op)
6f7b98e0 399{
956a62ec
NT
400 if (op == DB_VAR_GET)
401 *valuep = 0;
402 return(-1);
6f7b98e0
MD
403}
404
6f7b98e0
MD
405int
406db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
407{
956a62ec 408 return(-1);
6f7b98e0
MD
409}
410
411int
412db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
413{
956a62ec 414 return(-1);
6f7b98e0
MD
415}
416
417void
418db_md_list_watchpoints(void)
419{
956a62ec 420 /* no hardware watchpoints in vkernel */
6f7b98e0 421}
35ae59b9
MD
422
423/*
424 * See if dladdr() can get the symbol name via the standard dynamic loader.
425 */
426static
427void
428dl_symbol_values(int callpc, const char **name)
429{
430 Dl_info info;
431
432 if (*name == NULL) {
433 if (dladdr((const void *)callpc, &info) != 0) {
a50d3a2d
MD
434 if (info.dli_saddr <= (const void *)callpc)
435 *name = info.dli_sname;
35ae59b9
MD
436 }
437 }
438}
439