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