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