print_backtrace - Take parameter count
[dragonfly.git] / sys / platform / vkernel / i386 / db_trace.c
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 $
27  * $DragonFly: src/sys/platform/vkernel/i386/db_trace.c,v 1.9 2008/02/03 15:10:26 nth Exp $
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>
45 #include <dlfcn.h>      /* DLL */
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
53 static int db_dr(struct db_variable *vp, db_expr_t *valuep, int op);
54
55 /*
56  * Machine register set.
57  */
58 struct db_variable db_regs[] = {
59         { "cs",         &ddb_regs.tf_cs,     NULL },
60         { "ds",         &ddb_regs.tf_ds,     NULL },
61         { "es",         &ddb_regs.tf_es,     NULL },
62         { "fs",         &ddb_regs.tf_fs,     NULL },
63         { "gs",         &ddb_regs.tf_gs,     NULL },
64         { "ss",         &ddb_regs.tf_ss,     NULL },
65         { "eax",        &ddb_regs.tf_eax,    NULL },
66         { "ecx",        &ddb_regs.tf_ecx,    NULL },
67         { "edx",        &ddb_regs.tf_edx,    NULL },
68         { "ebx",        &ddb_regs.tf_ebx,    NULL },
69         { "esp",        &ddb_regs.tf_esp,    NULL },
70         { "ebp",        &ddb_regs.tf_ebp,    NULL },
71         { "esi",        &ddb_regs.tf_esi,    NULL },
72         { "edi",        &ddb_regs.tf_edi,    NULL },
73         { "eip",        &ddb_regs.tf_eip,    NULL },
74         { "efl",        &ddb_regs.tf_eflags, NULL },
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 },
83 };
84 struct 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
91 struct 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
102 static void     db_nextframe(struct i386_frame **, db_addr_t *);
103 static int      db_numargs(struct i386_frame *);
104 static void     db_print_stack_entry(const char *, int, char **, int *, db_addr_t);
105 static void     dl_symbol_values(int callpc, const char **name);
106
107
108 int             db_md_set_watchpoint(db_expr_t addr, db_expr_t size);
109 int             db_md_clr_watchpoint(db_expr_t addr, db_expr_t size);
110 void            db_md_list_watchpoints(void);
111
112
113 /*
114  * Figure out how many arguments were passed into the frame at "fp".
115  */
116 static int
117 db_numargs(struct i386_frame *fp)
118 {
119         int     args;
120 #if 0
121         int     *argp;
122         int     inst;
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         }
141 #endif
142         args = 5;
143         return(args);
144 }
145
146 static void
147 db_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  */
167 static void
168 db_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);
187         dl_symbol_values(eip, &name);
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
215 #if 0
216         esp = (ISPL(tf->tf_cs) == SEL_UPL) ?  tf->tf_esp : (int)&tf->tf_esp;
217 #endif
218         esp = (int)&tf->tf_esp;
219
220         switch (frame_type) {
221         case TRAP:
222                 {
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:
231                 {
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);
241                 {
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
257 void
258 db_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);
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);
313                 dl_symbol_values(callpc, &name);
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                                 }
353                         } else if (name != NULL &&
354                                    strcmp(name, "fork_trampoline") == 0) {
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);
383                 if (frame == 0)
384                         break;
385         }
386 }
387
388 void
389 print_backtrace(int count)
390 {
391         register_t  ebp;
392
393         __asm __volatile("movl %%ebp, %0" : "=r" (ebp));
394         db_stack_trace_cmd(ebp, 1, count, NULL);
395 }
396
397 static int
398 db_dr(struct db_variable *vp, db_expr_t *valuep, int op)
399 {
400         if (op == DB_VAR_GET)
401                 *valuep = 0;
402         return(-1);
403 }
404
405 int
406 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
407 {
408         return(-1);
409 }
410
411 int
412 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
413 {
414         return(-1);
415 }
416
417 void
418 db_md_list_watchpoints(void)
419 {
420         /* no hardware watchpoints in vkernel */
421 }
422
423 /*
424  * See if dladdr() can get the symbol name via the standard dynamic loader.
425  */
426 static
427 void
428 dl_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) {
434                         if (info.dli_saddr <= (const void *)callpc)
435                                 *name = info.dli_sname;
436                 }
437         }
438 }
439