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