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