Modify the trapframe sigcontext, ucontext, etc. Add %gs to the trapframe
[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.3 2007/01/08 03:33:43 dillon 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
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 db_varfcn_t db_dr0;
53 db_varfcn_t db_dr1;
54 db_varfcn_t db_dr2;
55 db_varfcn_t db_dr3;
56 db_varfcn_t db_dr4;
57 db_varfcn_t db_dr5;
58 db_varfcn_t db_dr6;
59 db_varfcn_t db_dr7;
60
61 /*
62  * Machine register set.
63  */
64 struct db_variable db_regs[] = {
65         { "cs",         &ddb_regs.tf_cs,     FCN_NULL },
66         { "ds",         &ddb_regs.tf_ds,     FCN_NULL },
67         { "es",         &ddb_regs.tf_es,     FCN_NULL },
68         { "fs",         &ddb_regs.tf_fs,     FCN_NULL },
69         { "gs",         &ddb_regs.tf_gs,     FCN_NULL },
70         { "ss",         &ddb_regs.tf_ss,     FCN_NULL },
71         { "eax",        &ddb_regs.tf_eax,    FCN_NULL },
72         { "ecx",        &ddb_regs.tf_ecx,    FCN_NULL },
73         { "edx",        &ddb_regs.tf_edx,    FCN_NULL },
74         { "ebx",        &ddb_regs.tf_ebx,    FCN_NULL },
75         { "esp",        &ddb_regs.tf_esp,    FCN_NULL },
76         { "ebp",        &ddb_regs.tf_ebp,    FCN_NULL },
77         { "esi",        &ddb_regs.tf_esi,    FCN_NULL },
78         { "edi",        &ddb_regs.tf_edi,    FCN_NULL },
79         { "eip",        &ddb_regs.tf_eip,    FCN_NULL },
80         { "efl",        &ddb_regs.tf_eflags, FCN_NULL },
81         { "dr0",        NULL,                db_dr0 },
82         { "dr1",        NULL,                db_dr1 },
83         { "dr2",        NULL,                db_dr2 },
84         { "dr3",        NULL,                db_dr3 },
85         { "dr4",        NULL,                db_dr4 },
86         { "dr5",        NULL,                db_dr5 },
87         { "dr6",        NULL,                db_dr6 },
88         { "dr7",        NULL,                db_dr7 },
89 };
90 struct db_variable *db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
91
92 /*
93  * Stack trace.
94  */
95 #define INKERNEL(va)    (((vm_offset_t)(va)) >= USRSTACK)
96
97 struct i386_frame {
98         struct i386_frame       *f_frame;
99         int                     f_retaddr;
100         int                     f_arg0;
101 };
102
103 #define NORMAL          0
104 #define TRAP            1
105 #define INTERRUPT       2
106 #define SYSCALL         3
107
108 static void     db_nextframe(struct i386_frame **, db_addr_t *);
109 static int      db_numargs(struct i386_frame *);
110 static void     db_print_stack_entry(const char *, int, char **, int *, db_addr_t);
111
112
113 static char     *watchtype_str(int type);
114 static int      ki386_set_watch(int watchnum, unsigned int watchaddr, 
115                                int size, int access, struct dbreg * d);
116 static int      ki386_clr_watch(int watchnum, struct dbreg * d);
117 int             db_md_set_watchpoint(db_expr_t addr, db_expr_t size);
118 int             db_md_clr_watchpoint(db_expr_t addr, db_expr_t size);
119 void            db_md_list_watchpoints(void);
120
121
122 /*
123  * Figure out how many arguments were passed into the frame at "fp".
124  */
125 static int
126 db_numargs(struct i386_frame *fp)
127 {
128         int     args;
129 #if 0
130         int     *argp;
131         int     inst;
132
133         argp = (int *)db_get_value((int)&fp->f_retaddr, 4, FALSE);
134         /*
135          * XXX etext is wrong for LKMs.  We should attempt to interpret
136          * the instruction at the return address in all cases.  This
137          * may require better fault handling.
138          */
139         if (argp < (int *)btext || argp >= (int *)etext) {
140                 args = 5;
141         } else {
142                 inst = db_get_value((int)argp, 4, FALSE);
143                 if ((inst & 0xff) == 0x59)      /* popl %ecx */
144                         args = 1;
145                 else if ((inst & 0xffff) == 0xc483)     /* addl $Ibs, %esp */
146                         args = ((inst >> 16) & 0xff) / 4;
147                 else
148                         args = 5;
149         }
150 #endif
151         args = 5;
152         return(args);
153 }
154
155 static void
156 db_print_stack_entry(const char *name, int narg, char **argnp, int *argp,
157                      db_addr_t callpc)
158 {
159         db_printf("%s(", name);
160         while (narg) {
161                 if (argnp)
162                         db_printf("%s=", *argnp++);
163                 db_printf("%r", db_get_value((int)argp, 4, FALSE));
164                 argp++;
165                 if (--narg != 0)
166                         db_printf(",");
167         }
168         db_printf(") at ");
169         db_printsym(callpc, DB_STGY_PROC);
170         db_printf("\n");
171 }
172
173 /*
174  * Figure out the next frame up in the call stack.
175  */
176 static void
177 db_nextframe(struct i386_frame **fp, db_addr_t *ip)
178 {
179         struct trapframe *tf;
180         int frame_type;
181         int eip, esp, ebp;
182         db_expr_t offset;
183         const char *sym, *name;
184
185         eip = db_get_value((int) &(*fp)->f_retaddr, 4, FALSE);
186         ebp = db_get_value((int) &(*fp)->f_frame, 4, FALSE);
187
188         /*
189          * Figure out frame type.
190          */
191
192         frame_type = NORMAL;
193
194         sym = db_search_symbol(eip, DB_STGY_ANY, &offset);
195         db_symbol_values(sym, &name, NULL);
196         if (name != NULL) {
197                 if (!strcmp(name, "calltrap")) {
198                         frame_type = TRAP;
199                 } else if (!strncmp(name, "Xresume", 7)) {
200                         frame_type = INTERRUPT;
201                 } else if (!strcmp(name, "_Xsyscall")) {
202                         frame_type = SYSCALL;
203                 }
204         }
205
206         /*
207          * Normal frames need no special processing.
208          */
209         if (frame_type == NORMAL) {
210                 *ip = (db_addr_t) eip;
211                 *fp = (struct i386_frame *) ebp;
212                 return;
213         }
214
215         db_print_stack_entry(name, 0, 0, 0, eip);
216
217         /*
218          * Point to base of trapframe which is just above the
219          * current frame.
220          */
221         tf = (struct trapframe *) ((int)*fp + 8);
222
223         esp = (ISPL(tf->tf_cs) == SEL_UPL) ?  tf->tf_esp : (int)&tf->tf_esp;
224         switch (frame_type) {
225         case TRAP:
226                 if (INKERNEL((int) tf)) {
227                         eip = tf->tf_eip;
228                         ebp = tf->tf_ebp;
229                         db_printf(
230                     "--- trap %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
231                             tf->tf_trapno, eip, esp, ebp);
232                 }
233                 break;
234         case SYSCALL:
235                 if (INKERNEL((int) tf)) {
236                         eip = tf->tf_eip;
237                         ebp = tf->tf_ebp;
238                         db_printf(
239                     "--- syscall %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
240                             tf->tf_eax, eip, esp, ebp);
241                 }
242                 break;
243         case INTERRUPT:
244                 tf = (struct trapframe *)((int)*fp + 16);
245                 if (INKERNEL((int) tf)) {
246                         eip = tf->tf_eip;
247                         ebp = tf->tf_ebp;
248                         db_printf(
249                     "--- interrupt, eip = %#r, esp = %#r, ebp = %#r ---\n",
250                             eip, esp, ebp);
251                 }
252                 break;
253         default:
254                 break;
255         }
256
257         *ip = (db_addr_t) eip;
258         *fp = (struct i386_frame *) ebp;
259 }
260
261 void
262 db_stack_trace_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
263                    char *modif)
264 {
265         struct i386_frame *frame;
266         int *argp;
267         db_addr_t callpc;
268         boolean_t first;
269         int i;
270
271         if (count == -1)
272                 count = 1024;
273
274         if (!have_addr) {
275                 frame = (struct i386_frame *)BP_REGS(&ddb_regs);
276                 if (frame == NULL)
277                         frame = (struct i386_frame *)(SP_REGS(&ddb_regs) - 4);
278                 callpc = PC_REGS(&ddb_regs);
279         } else if (!INKERNEL(addr)) {
280 #if needswork
281                 pid = (addr % 16) + ((addr >> 4) % 16) * 10 +
282                     ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 +
283                     ((addr >> 16) % 16) * 10000;
284                 /*
285                  * The pcb for curproc is not valid at this point,
286                  * so fall back to the default case.
287                  */
288                 if ((curproc != NULL) && (pid == curproc->p_pid)) {
289                         frame = (struct i386_frame *)BP_REGS(&ddb_regs);
290                         if (frame == NULL)
291                                 frame = (struct i386_frame *)
292                                     (SP_REGS(&ddb_regs) - 4);
293                         callpc = PC_REGS(&ddb_regs);
294                 } else {
295                         pid_t pid;
296                         struct proc *p;
297                         struct pcb *pcb;
298
299                         p = pfind(pid);
300                         if (p == NULL) {
301                                 db_printf("pid %d not found\n", pid);
302                                 return;
303                         }
304                         if ((p->p_flag & P_SWAPPEDOUT)) {
305                                 db_printf("pid %d swapped out\n", pid);
306                                 return;
307                         }
308                         pcb = p->p_thread->td_pcb;
309                         frame = (struct i386_frame *)pcb->pcb_ebp;
310                         if (frame == NULL)
311                                 frame = (struct i386_frame *)
312                                     (pcb->pcb_esp - 4);
313                         callpc = (db_addr_t)pcb->pcb_eip;
314                 }
315 #else
316                 /* XXX */
317                 db_printf("no kernel stack address\n");
318                 return;
319 #endif
320         } else {
321                 /*
322                  * Look for something that might be a frame pointer, just as
323                  * a convenience.
324                  */
325                 frame = (struct i386_frame *)addr;
326                 for (i = 0; i < 4096; i += 4) {
327                         struct i386_frame *check;
328
329                         check = (struct i386_frame *)db_get_value((int)((char *)&frame->f_frame + i), 4, FALSE);
330                         if ((char *)check - (char *)frame >= 0 &&
331                             (char *)check - (char *)frame < 4096
332                         ) {
333                                 break;
334                         }
335                         db_printf("%p does not look like a stack frame, skipping\n", (char *)&frame->f_frame + i);
336                 }
337                 if (i == 4096) {
338                         db_printf("Unable to find anything that looks like a stack frame\n");
339                         return;
340                 }
341                 frame = (void *)((char *)frame + i);
342                 db_printf("Trace beginning at frame %p\n", frame);
343                 callpc = (db_addr_t)db_get_value((int)&frame->f_retaddr, 4, FALSE);
344         }
345
346         first = TRUE;
347         while (count--) {
348                 struct i386_frame *actframe;
349                 int             narg;
350                 const char *    name;
351                 db_expr_t       offset;
352                 c_db_sym_t      sym;
353 #define MAXNARG 16
354                 char    *argnames[MAXNARG], **argnp = NULL;
355
356                 sym = db_search_symbol(callpc, DB_STGY_ANY, &offset);
357                 db_symbol_values(sym, &name, NULL);
358
359                 /*
360                  * Attempt to determine a (possibly fake) frame that gives
361                  * the caller's pc.  It may differ from `frame' if the
362                  * current function never sets up a standard frame or hasn't
363                  * set one up yet or has just discarded one.  The last two
364                  * cases can be guessed fairly reliably for code generated
365                  * by gcc.  The first case is too much trouble to handle in
366                  * general because the amount of junk on the stack depends
367                  * on the pc (the special handling of "calltrap", etc. in
368                  * db_nextframe() works because the `next' pc is special).
369                  */
370                 actframe = frame;
371                 if (first) {
372                         if (!have_addr) {
373                                 int instr;
374
375                                 instr = db_get_value(callpc, 4, FALSE);
376                                 if ((instr & 0x00ffffff) == 0x00e58955) {
377                                         /* pushl %ebp; movl %esp, %ebp */
378                                         actframe = (struct i386_frame *)
379                                             (SP_REGS(&ddb_regs) - 4);
380                                 } else if ((instr & 0x0000ffff) == 0x0000e589) {
381                                         /* movl %esp, %ebp */
382                                         actframe = (struct i386_frame *)
383                                             SP_REGS(&ddb_regs);
384                                         if (ddb_regs.tf_ebp == 0) {
385                                                 /* Fake caller's frame better. */
386                                                 frame = actframe;
387                                         }
388                                 } else if ((instr & 0x000000ff) == 0x000000c3) {
389                                         /* ret */
390                                         actframe = (struct i386_frame *)
391                                             (SP_REGS(&ddb_regs) - 4);
392                                 } else if (offset == 0) {
393                                         /* Probably a symbol in assembler code. */
394                                         actframe = (struct i386_frame *)
395                                             (SP_REGS(&ddb_regs) - 4);
396                                 }
397                         } else if (!strcmp(name, "fork_trampoline")) {
398                                 /*
399                                  * Don't try to walk back on a stack for a
400                                  * process that hasn't actually been run yet.
401                                  */
402                                 db_print_stack_entry(name, 0, 0, 0, callpc);
403                                 break;
404                         }
405                         first = FALSE;
406                 }
407
408                 argp = &actframe->f_arg0;
409                 narg = MAXNARG;
410                 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
411                         argnp = argnames;
412                 } else {
413                         narg = db_numargs(frame);
414                 }
415
416                 db_print_stack_entry(name, narg, argnp, argp, callpc);
417
418                 if (actframe != frame) {
419                         /* `frame' belongs to caller. */
420                         callpc = (db_addr_t)
421                             db_get_value((int)&actframe->f_retaddr, 4, FALSE);
422                         continue;
423                 }
424
425                 db_nextframe(&frame, &callpc);
426
427                 if (INKERNEL((int) callpc) && !INKERNEL((int) frame)) {
428                         sym = db_search_symbol(callpc, DB_STGY_ANY, &offset);
429                         db_symbol_values(sym, &name, NULL);
430                         db_print_stack_entry(name, 0, 0, 0, callpc);
431                         break;
432                 }
433                 if (!INKERNEL((int) frame)) {
434                         break;
435                 }
436         }
437 }
438
439 void
440 db_print_backtrace(void)
441 {
442         register_t  ebp;
443
444         __asm __volatile("movl %%ebp, %0" : "=r" (ebp));
445         db_stack_trace_cmd(ebp, 1, -1, NULL);
446 }
447
448 #define DB_DRX_FUNC(reg)                                                \
449 int                                                                     \
450 db_ ## reg (struct db_variable *vp, db_expr_t *valuep, int op)          \
451 {                                                                       \
452         if (op == DB_VAR_GET)                                           \
453                 *valuep = r ## reg ();                                  \
454         else                                                            \
455                 load_ ## reg (*valuep);                                 \
456                                                                         \
457         return(0);                                                      \
458
459
460 DB_DRX_FUNC(dr0)
461 DB_DRX_FUNC(dr1)
462 DB_DRX_FUNC(dr2)
463 DB_DRX_FUNC(dr3)
464 DB_DRX_FUNC(dr4)
465 DB_DRX_FUNC(dr5)
466 DB_DRX_FUNC(dr6)
467 DB_DRX_FUNC(dr7)
468
469 static int
470 ki386_set_watch(int watchnum, unsigned int watchaddr, int size, int access,
471                struct dbreg *d)
472 {
473         int i;
474         unsigned int mask;
475         
476         if (watchnum == -1) {
477                 for (i = 0, mask = 0x3; i < 4; i++, mask <<= 2)
478                         if ((d->dr7 & mask) == 0)
479                                 break;
480                 if (i < 4)
481                         watchnum = i;
482                 else
483                         return(-1);
484         }
485         
486         switch (access) {
487         case DBREG_DR7_EXEC:
488                 size = 1; /* size must be 1 for an execution breakpoint */
489                 /* fall through */
490         case DBREG_DR7_WRONLY:
491         case DBREG_DR7_RDWR:
492                 break;
493         default:
494                 return(-1);
495         }
496
497         /*
498          * we can watch a 1, 2, or 4 byte sized location
499          */
500         switch (size) {
501         case 1:
502                 mask = 0x00;
503                 break;
504         case 2:
505                 mask = 0x01 << 2;
506                 break;
507         case 4:
508                 mask = 0x03 << 2;
509                 break;
510         default:
511                 return(-1);
512         }
513
514         mask |= access;
515
516         /* clear the bits we are about to affect */
517         d->dr7 &= ~((0x3 << (watchnum * 2)) | (0x0f << (watchnum * 4 + 16)));
518
519         /* set drN register to the address, N=watchnum */
520         DBREG_DRX(d, watchnum) = watchaddr;
521
522         /* enable the watchpoint */
523         d->dr7 |= (0x2 << (watchnum * 2)) | (mask << (watchnum * 4 + 16));
524
525         return(watchnum);
526 }
527
528
529 int
530 ki386_clr_watch(int watchnum, struct dbreg *d)
531 {
532         if (watchnum < 0 || watchnum >= 4)
533                 return(-1);
534         
535         d->dr7 &= ~((0x3 << (watchnum * 2)) | (0x0f << (watchnum * 4 + 16)));
536         DBREG_DRX(d, watchnum) = 0;
537         
538         return(0);
539 }
540
541
542 int
543 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
544 {
545         int avail, wsize;
546         int i;
547         struct dbreg d;
548         
549         fill_dbregs(NULL, &d);
550         
551         avail = 0;
552         for(i=0; i < 4; i++) {
553                 if ((d.dr7 & (3 << (i * 2))) == 0)
554                         avail++;
555         }
556         
557         if (avail * 4 < size)
558                 return(-1);
559         
560         for (i=0; i < 4 && (size != 0); i++) {
561                 if ((d.dr7 & (3 << (i * 2))) == 0) {
562                         if (size > 4)
563                                 wsize = 4;
564                         else
565                                 wsize = size;
566                         if (wsize == 3)
567                                 wsize++;
568                         ki386_set_watch(i, addr, wsize, DBREG_DR7_WRONLY, &d);
569                         addr += wsize;
570                         size -= wsize;
571                 }
572         }
573
574         set_dbregs(NULL, &d);
575
576         return(0);
577 }
578
579 int
580 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
581 {
582         int i;
583         struct dbreg d;
584
585         fill_dbregs(NULL, &d);
586
587         for(i=0; i<4; i++) {
588                 if (d.dr7 & (3 << (i * 2))) {
589                         if ((DBREG_DRX((&d), i) >= addr) && 
590                             (DBREG_DRX((&d), i) < addr + size))
591                                 ki386_clr_watch(i, &d);
592                 }
593         }
594
595         set_dbregs(NULL, &d);
596
597         return(0);
598 }
599
600 static char *
601 watchtype_str(int type)
602 {
603         switch (type) {
604         case DBREG_DR7_EXEC:
605                 return "execute";
606         case DBREG_DR7_RDWR:
607                 return "read/write";
608         case DBREG_DR7_WRONLY:
609                 return "write";
610         default:
611                 return "invalid";
612         }
613 }
614
615 void
616 db_md_list_watchpoints(void)
617 {
618         int i;
619         struct dbreg d;
620
621         fill_dbregs(NULL, &d);
622
623         db_printf("\nhardware watchpoints:\n");
624         db_printf("  watch    status        type  len     address\n"
625                   "  -----  --------  ----------  ---  ----------\n");
626         for (i=0; i < 4; i++) {
627                 if (d.dr7 & (0x03 << (i * 2))) {
628                         unsigned type, len;
629                         type = (d.dr7 >> (16 + (i * 4))) & 3;
630                         len =  (d.dr7 >> (16 + (i * 4) + 2)) & 3;
631                         db_printf("  %-5d  %-8s  %10s  %3d  0x%08x\n",
632                                   i, "enabled", watchtype_str(type), 
633                                   len + 1, DBREG_DRX((&d), i));
634                 } else {
635                         db_printf("  %-5d  disabled\n", i);
636                 }
637         }
638
639         db_printf("\ndebug register values:\n");
640         for (i=0; i < 8; i++)
641                 db_printf("  dr%d 0x%08x\n", i, DBREG_DRX((&d),i));
642         db_printf("\n");
643 }