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