]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/db_trace.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / 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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kdb.h>
33 #include <sys/proc.h>
34 #include <sys/stack.h>
35 #include <sys/sysent.h>
36
37 #include <machine/cpu.h>
38 #include <machine/md_var.h>
39 #include <machine/pcb.h>
40 #include <machine/reg.h>
41 #include <machine/stack.h>
42
43 #include <vm/vm.h>
44 #include <vm/vm_param.h>
45 #include <vm/pmap.h>
46
47 #include <ddb/ddb.h>
48 #include <ddb/db_access.h>
49 #include <ddb/db_sym.h>
50 #include <ddb/db_variables.h>
51
52 static db_varfcn_t db_dr0;
53 static db_varfcn_t db_dr1;
54 static db_varfcn_t db_dr2;
55 static db_varfcn_t db_dr3;
56 static db_varfcn_t db_dr4;
57 static db_varfcn_t db_dr5;
58 static db_varfcn_t db_dr6;
59 static db_varfcn_t db_dr7;
60 static db_varfcn_t db_frame;
61 static db_varfcn_t db_rsp;
62 static db_varfcn_t db_ss;
63
64 /*
65  * Machine register set.
66  */
67 #define DB_OFFSET(x)    (db_expr_t *)offsetof(struct trapframe, x)
68 struct db_variable db_regs[] = {
69         { "cs",         DB_OFFSET(tf_cs),       db_frame },
70 #if 0
71         { "ds",         DB_OFFSET(tf_ds),       db_frame },
72         { "es",         DB_OFFSET(tf_es),       db_frame },
73         { "fs",         DB_OFFSET(tf_fs),       db_frame },
74         { "gs",         DB_OFFSET(tf_gs),       db_frame },
75 #endif
76         { "ss",         NULL,                   db_ss },
77         { "rax",        DB_OFFSET(tf_rax),      db_frame },
78         { "rcx",        DB_OFFSET(tf_rcx),      db_frame },
79         { "rdx",        DB_OFFSET(tf_rdx),      db_frame },
80         { "rbx",        DB_OFFSET(tf_rbx),      db_frame },
81         { "rsp",        NULL,                   db_rsp },
82         { "rbp",        DB_OFFSET(tf_rbp),      db_frame },
83         { "rsi",        DB_OFFSET(tf_rsi),      db_frame },
84         { "rdi",        DB_OFFSET(tf_rdi),      db_frame },
85         { "r8",         DB_OFFSET(tf_r8),       db_frame },
86         { "r9",         DB_OFFSET(tf_r9),       db_frame },
87         { "r10",        DB_OFFSET(tf_r10),      db_frame },
88         { "r11",        DB_OFFSET(tf_r11),      db_frame },
89         { "r12",        DB_OFFSET(tf_r12),      db_frame },
90         { "r13",        DB_OFFSET(tf_r13),      db_frame },
91         { "r14",        DB_OFFSET(tf_r14),      db_frame },
92         { "r15",        DB_OFFSET(tf_r15),      db_frame },
93         { "rip",        DB_OFFSET(tf_rip),      db_frame },
94         { "rflags",     DB_OFFSET(tf_rflags),   db_frame },
95 #define DB_N_SHOW_REGS  20      /* Don't show registers after here. */
96         { "dr0",        NULL,                   db_dr0 },
97         { "dr1",        NULL,                   db_dr1 },
98         { "dr2",        NULL,                   db_dr2 },
99         { "dr3",        NULL,                   db_dr3 },
100         { "dr4",        NULL,                   db_dr4 },
101         { "dr5",        NULL,                   db_dr5 },
102         { "dr6",        NULL,                   db_dr6 },
103         { "dr7",        NULL,                   db_dr7 },
104 };
105 struct db_variable *db_eregs = db_regs + DB_N_SHOW_REGS;
106
107 #define DB_DRX_FUNC(reg)                \
108 static int                              \
109 db_ ## reg (vp, valuep, op)             \
110         struct db_variable *vp;         \
111         db_expr_t * valuep;             \
112         int op;                         \
113 {                                       \
114         if (op == DB_VAR_GET)           \
115                 *valuep = r ## reg ();  \
116         else                            \
117                 load_ ## reg (*valuep); \
118         return (1);                     \
119 }
120
121 DB_DRX_FUNC(dr0)
122 DB_DRX_FUNC(dr1)
123 DB_DRX_FUNC(dr2)
124 DB_DRX_FUNC(dr3)
125 DB_DRX_FUNC(dr4)
126 DB_DRX_FUNC(dr5)
127 DB_DRX_FUNC(dr6)
128 DB_DRX_FUNC(dr7)
129
130 static __inline long
131 get_rsp(struct trapframe *tf)
132 {
133         return ((ISPL(tf->tf_cs)) ? tf->tf_rsp :
134             (db_expr_t)tf + offsetof(struct trapframe, tf_rsp));
135 }
136
137 static int
138 db_frame(struct db_variable *vp, db_expr_t *valuep, int op)
139 {
140         long *reg;
141
142         if (kdb_frame == NULL)
143                 return (0);
144
145         reg = (long *)((uintptr_t)kdb_frame + (db_expr_t)vp->valuep);
146         if (op == DB_VAR_GET)
147                 *valuep = *reg;
148         else
149                 *reg = *valuep;
150         return (1);
151 }
152
153 static int
154 db_rsp(struct db_variable *vp, db_expr_t *valuep, int op)
155 {
156
157         if (kdb_frame == NULL)
158                 return (0);
159
160         if (op == DB_VAR_GET)
161                 *valuep = get_rsp(kdb_frame);
162         else if (ISPL(kdb_frame->tf_cs))
163                 kdb_frame->tf_rsp = *valuep;
164         return (1);
165 }
166
167 static int
168 db_ss(struct db_variable *vp, db_expr_t *valuep, int op)
169 {
170
171         if (kdb_frame == NULL)
172                 return (0);
173
174         if (op == DB_VAR_GET)
175                 *valuep = (ISPL(kdb_frame->tf_cs)) ? kdb_frame->tf_ss : rss();
176         else if (ISPL(kdb_frame->tf_cs))
177                 kdb_frame->tf_ss = *valuep;
178         return (1);
179 }
180
181 #define NORMAL          0
182 #define TRAP            1
183 #define INTERRUPT       2
184 #define SYSCALL         3
185 #define TRAP_INTERRUPT  5
186
187 static void db_nextframe(struct amd64_frame **, db_addr_t *, struct thread *);
188 static int db_numargs(struct amd64_frame *);
189 static void db_print_stack_entry(const char *, int, char **, long *, db_addr_t);
190 static void decode_syscall(int, struct thread *);
191
192 static const char * watchtype_str(int type);
193 int  amd64_set_watch(int watchnum, unsigned long watchaddr, int size,
194                     int access, struct dbreg *d);
195 int  amd64_clr_watch(int watchnum, struct dbreg *d);
196
197 /*
198  * Figure out how many arguments were passed into the frame at "fp".
199  */
200 static int
201 db_numargs(fp)
202         struct amd64_frame *fp;
203 {
204 #if 1
205         return (0);     /* regparm, needs dwarf2 info */
206 #else
207         long    *argp;
208         int     inst;
209         int     args;
210
211         argp = (long *)db_get_value((long)&fp->f_retaddr, 8, FALSE);
212         /*
213          * XXX etext is wrong for LKMs.  We should attempt to interpret
214          * the instruction at the return address in all cases.  This
215          * may require better fault handling.
216          */
217         if (argp < (long *)btext || argp >= (long *)etext) {
218                 args = 5;
219         } else {
220                 inst = db_get_value((long)argp, 4, FALSE);
221                 if ((inst & 0xff) == 0x59)      /* popl %ecx */
222                         args = 1;
223                 else if ((inst & 0xffff) == 0xc483)     /* addl $Ibs, %esp */
224                         args = ((inst >> 16) & 0xff) / 4;
225                 else
226                         args = 5;
227         }
228         return (args);
229 #endif
230 }
231
232 static void
233 db_print_stack_entry(name, narg, argnp, argp, callpc)
234         const char *name;
235         int narg;
236         char **argnp;
237         long *argp;
238         db_addr_t callpc;
239 {
240         db_printf("%s(", name);
241 #if 0
242         while (narg) {
243                 if (argnp)
244                         db_printf("%s=", *argnp++);
245                 db_printf("%lr", (long)db_get_value((long)argp, 8, FALSE));
246                 argp++;
247                 if (--narg != 0)
248                         db_printf(",");
249         }
250 #endif
251         db_printf(") at ");
252         db_printsym(callpc, DB_STGY_PROC);
253         db_printf("\n");
254 }
255
256 static void
257 decode_syscall(int number, struct thread *td)
258 {
259         struct proc *p;
260         c_db_sym_t sym;
261         db_expr_t diff;
262         sy_call_t *f;
263         const char *symname;
264
265         db_printf(" (%d", number);
266         p = (td != NULL) ? td->td_proc : NULL;
267         if (p != NULL && 0 <= number && number < p->p_sysent->sv_size) {
268                 f = p->p_sysent->sv_table[number].sy_call;
269                 sym = db_search_symbol((db_addr_t)f, DB_STGY_ANY, &diff);
270                 if (sym != DB_SYM_NULL && diff == 0) {
271                         db_symbol_values(sym, &symname, NULL);
272                         db_printf(", %s, %s", p->p_sysent->sv_name, symname);
273                 }
274         }
275         db_printf(")");
276 }
277
278 /*
279  * Figure out the next frame up in the call stack.
280  */
281 static void
282 db_nextframe(struct amd64_frame **fp, db_addr_t *ip, struct thread *td)
283 {
284         struct trapframe *tf;
285         int frame_type;
286         long rip, rsp, rbp;
287         db_expr_t offset;
288         c_db_sym_t sym;
289         const char *name;
290
291         rip = db_get_value((long) &(*fp)->f_retaddr, 8, FALSE);
292         rbp = db_get_value((long) &(*fp)->f_frame, 8, FALSE);
293
294         /*
295          * Figure out frame type.  We look at the address just before
296          * the saved instruction pointer as the saved EIP is after the
297          * call function, and if the function being called is marked as
298          * dead (such as panic() at the end of dblfault_handler()), then
299          * the instruction at the saved EIP will be part of a different
300          * function (syscall() in this example) rather than the one that
301          * actually made the call.
302          */
303         frame_type = NORMAL;
304         sym = db_search_symbol(rip - 1, DB_STGY_ANY, &offset);
305         db_symbol_values(sym, &name, NULL);
306         if (name != NULL) {
307                 if (strcmp(name, "calltrap") == 0 ||
308                     strcmp(name, "fork_trampoline") == 0 ||
309                     strcmp(name, "nmi_calltrap") == 0 ||
310                     strcmp(name, "Xdblfault") == 0)
311                         frame_type = TRAP;
312                 else if (strncmp(name, "Xatpic_intr", 11) == 0 ||
313                     strncmp(name, "Xapic_isr", 9) == 0 ||
314                     strcmp(name, "Xtimerint") == 0 ||
315                     strcmp(name, "Xipi_intr_bitmap_handler") == 0 ||
316                     strcmp(name, "Xcpustop") == 0 ||
317                     strcmp(name, "Xrendezvous") == 0)
318                         frame_type = INTERRUPT;
319                 else if (strcmp(name, "Xfast_syscall") == 0)
320                         frame_type = SYSCALL;
321                 /* XXX: These are interrupts with trap frames. */
322                 else if (strcmp(name, "Xtimerint") == 0 ||
323                     strcmp(name, "Xcpustop") == 0 ||
324                     strcmp(name, "Xrendezvous") == 0 ||
325                     strcmp(name, "Xipi_intr_bitmap_handler") == 0)
326                         frame_type = TRAP_INTERRUPT;
327         }
328
329         /*
330          * Normal frames need no special processing.
331          */
332         if (frame_type == NORMAL) {
333                 *ip = (db_addr_t) rip;
334                 *fp = (struct amd64_frame *) rbp;
335                 return;
336         }
337
338         db_print_stack_entry(name, 0, 0, 0, rip);
339
340         /*
341          * Point to base of trapframe which is just above the
342          * current frame.
343          */
344         tf = (struct trapframe *)((long)*fp + 16);
345
346         if (INKERNEL((long) tf)) {
347                 rsp = get_rsp(tf);
348                 rip = tf->tf_rip;
349                 rbp = tf->tf_rbp;
350                 switch (frame_type) {
351                 case TRAP:
352                         db_printf("--- trap %#lr", tf->tf_trapno);
353                         break;
354                 case SYSCALL:
355                         db_printf("--- syscall");
356                         decode_syscall(tf->tf_rax, td);
357                         break;
358                 case TRAP_INTERRUPT:
359                 case INTERRUPT:
360                         db_printf("--- interrupt");
361                         break;
362                 default:
363                         panic("The moon has moved again.");
364                 }
365                 db_printf(", rip = %#lr, rsp = %#lr, rbp = %#lr ---\n", rip,
366                     rsp, rbp);
367         }
368
369         *ip = (db_addr_t) rip;
370         *fp = (struct amd64_frame *) rbp;
371 }
372
373 static int
374 db_backtrace(struct thread *td, struct trapframe *tf,
375     struct amd64_frame *frame, db_addr_t pc, int count)
376 {
377         struct amd64_frame *actframe;
378 #define MAXNARG 16
379         char *argnames[MAXNARG], **argnp = NULL;
380         const char *name;
381         long *argp;
382         db_expr_t offset;
383         c_db_sym_t sym;
384         int narg;
385         boolean_t first;
386
387         if (count == -1)
388                 count = 1024;
389
390         first = TRUE;
391         while (count-- && !db_pager_quit) {
392                 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
393                 db_symbol_values(sym, &name, NULL);
394
395                 /*
396                  * Attempt to determine a (possibly fake) frame that gives
397                  * the caller's pc.  It may differ from `frame' if the
398                  * current function never sets up a standard frame or hasn't
399                  * set one up yet or has just discarded one.  The last two
400                  * cases can be guessed fairly reliably for code generated
401                  * by gcc.  The first case is too much trouble to handle in
402                  * general because the amount of junk on the stack depends
403                  * on the pc (the special handling of "calltrap", etc. in
404                  * db_nextframe() works because the `next' pc is special).
405                  */
406                 actframe = frame;
407                 if (first) {
408                         if (tf != NULL) {
409                                 int instr;
410
411                                 instr = db_get_value(pc, 4, FALSE);
412                                 if ((instr & 0xffffffff) == 0xe5894855) {
413                                         /* pushq %rbp; movq %rsp, %rbp */
414                                         actframe = (void *)(get_rsp(tf) - 8);
415                                 } else if ((instr & 0xffffff) == 0xe58948) {
416                                         /* movq %rsp, %rbp */
417                                         actframe = (void *)get_rsp(tf);
418                                         if (tf->tf_rbp == 0) {
419                                                 /* Fake frame better. */
420                                                 frame = actframe;
421                                         }
422                                 } else if ((instr & 0xff) == 0xc3) {
423                                         /* ret */
424                                         actframe = (void *)(get_rsp(tf) - 8);
425                                 } else if (offset == 0) {
426                                         /* Probably an assembler symbol. */
427                                         actframe = (void *)(get_rsp(tf) - 8);
428                                 }
429                         } else if (strcmp(name, "fork_trampoline") == 0) {
430                                 /*
431                                  * Don't try to walk back on a stack for a
432                                  * process that hasn't actually been run yet.
433                                  */
434                                 db_print_stack_entry(name, 0, 0, 0, pc);
435                                 break;
436                         }
437                         first = FALSE;
438                 }
439
440                 argp = &actframe->f_arg0;
441                 narg = MAXNARG;
442                 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
443                         argnp = argnames;
444                 } else {
445                         narg = db_numargs(frame);
446                 }
447
448                 db_print_stack_entry(name, narg, argnp, argp, pc);
449
450                 if (actframe != frame) {
451                         /* `frame' belongs to caller. */
452                         pc = (db_addr_t)
453                             db_get_value((long)&actframe->f_retaddr, 8, FALSE);
454                         continue;
455                 }
456
457                 db_nextframe(&frame, &pc, td);
458
459                 if (INKERNEL((long)pc) && !INKERNEL((long)frame)) {
460                         sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
461                         db_symbol_values(sym, &name, NULL);
462                         db_print_stack_entry(name, 0, 0, 0, pc);
463                         break;
464                 }
465                 if (!INKERNEL((long) frame)) {
466                         break;
467                 }
468         }
469
470         return (0);
471 }
472
473 void
474 db_trace_self(void)
475 {
476         struct amd64_frame *frame;
477         db_addr_t callpc;
478         register_t rbp;
479
480         __asm __volatile("movq %%rbp,%0" : "=r" (rbp));
481         frame = (struct amd64_frame *)rbp;
482         callpc = (db_addr_t)db_get_value((long)&frame->f_retaddr, 8, FALSE);
483         frame = frame->f_frame;
484         db_backtrace(curthread, NULL, frame, callpc, -1);
485 }
486
487 int
488 db_trace_thread(struct thread *thr, int count)
489 {
490         struct pcb *ctx;
491
492         ctx = kdb_thr_ctx(thr);
493         return (db_backtrace(thr, NULL, (struct amd64_frame *)ctx->pcb_rbp,
494                     ctx->pcb_rip, count));
495 }
496
497 int
498 amd64_set_watch(watchnum, watchaddr, size, access, d)
499         int watchnum;
500         unsigned long watchaddr;
501         int size;
502         int access;
503         struct dbreg *d;
504 {
505         int i, len;
506
507         if (watchnum == -1) {
508                 for (i = 0; i < 4; i++)
509                         if (!DBREG_DR7_ENABLED(d->dr[7], i))
510                                 break;
511                 if (i < 4)
512                         watchnum = i;
513                 else
514                         return (-1);
515         }
516
517         switch (access) {
518         case DBREG_DR7_EXEC:
519                 size = 1; /* size must be 1 for an execution breakpoint */
520                 /* fall through */
521         case DBREG_DR7_WRONLY:
522         case DBREG_DR7_RDWR:
523                 break;
524         default:
525                 return (-1);
526         }
527
528         /*
529          * we can watch a 1, 2, 4, or 8 byte sized location
530          */
531         switch (size) {
532         case 1:
533                 len = DBREG_DR7_LEN_1;
534                 break;
535         case 2:
536                 len = DBREG_DR7_LEN_2;
537                 break;
538         case 4:
539                 len = DBREG_DR7_LEN_4;
540                 break;
541         case 8:
542                 len = DBREG_DR7_LEN_8;
543                 break;
544         default:
545                 return (-1);
546         }
547
548         /* clear the bits we are about to affect */
549         d->dr[7] &= ~DBREG_DR7_MASK(watchnum);
550
551         /* set drN register to the address, N=watchnum */
552         DBREG_DRX(d, watchnum) = watchaddr;
553
554         /* enable the watchpoint */
555         d->dr[7] |= DBREG_DR7_SET(watchnum, len, access,
556             DBREG_DR7_GLOBAL_ENABLE);
557
558         return (watchnum);
559 }
560
561
562 int
563 amd64_clr_watch(watchnum, d)
564         int watchnum;
565         struct dbreg *d;
566 {
567
568         if (watchnum < 0 || watchnum >= 4)
569                 return (-1);
570
571         d->dr[7] &= ~DBREG_DR7_MASK(watchnum);
572         DBREG_DRX(d, watchnum) = 0;
573
574         return (0);
575 }
576
577
578 int
579 db_md_set_watchpoint(addr, size)
580         db_expr_t addr;
581         db_expr_t size;
582 {
583         struct dbreg d;
584         int avail, i, wsize;
585
586         fill_dbregs(NULL, &d);
587
588         avail = 0;
589         for(i = 0; i < 4; i++) {
590                 if (!DBREG_DR7_ENABLED(d.dr[7], i))
591                         avail++;
592         }
593
594         if (avail * 8 < size)
595                 return (-1);
596
597         for (i = 0; i < 4 && (size > 0); i++) {
598                 if (!DBREG_DR7_ENABLED(d.dr[7], i)) {
599                         if (size >= 8 || (avail == 1 && size > 4))
600                                 wsize = 8;
601                         else if (size > 2)
602                                 wsize = 4;
603                         else
604                                 wsize = size;
605                         amd64_set_watch(i, addr, wsize,
606                                        DBREG_DR7_WRONLY, &d);
607                         addr += wsize;
608                         size -= wsize;
609                         avail--;
610                 }
611         }
612
613         set_dbregs(NULL, &d);
614
615         return(0);
616 }
617
618
619 int
620 db_md_clr_watchpoint(addr, size)
621         db_expr_t addr;
622         db_expr_t size;
623 {
624         struct dbreg d;
625         int i;
626
627         fill_dbregs(NULL, &d);
628
629         for(i = 0; i < 4; i++) {
630                 if (DBREG_DR7_ENABLED(d.dr[7], i)) {
631                         if ((DBREG_DRX((&d), i) >= addr) &&
632                             (DBREG_DRX((&d), i) < addr+size))
633                                 amd64_clr_watch(i, &d);
634
635                 }
636         }
637
638         set_dbregs(NULL, &d);
639
640         return(0);
641 }
642
643
644 static const char *
645 watchtype_str(type)
646         int type;
647 {
648         switch (type) {
649                 case DBREG_DR7_EXEC   : return "execute";    break;
650                 case DBREG_DR7_RDWR   : return "read/write"; break;
651                 case DBREG_DR7_WRONLY : return "write";      break;
652                 default               : return "invalid";    break;
653         }
654 }
655
656
657 void
658 db_md_list_watchpoints()
659 {
660         struct dbreg d;
661         int i, len, type;
662
663         fill_dbregs(NULL, &d);
664
665         db_printf("\nhardware watchpoints:\n");
666         db_printf("  watch    status        type  len             address\n");
667         db_printf("  -----  --------  ----------  ---  ------------------\n");
668         for (i = 0; i < 4; i++) {
669                 if (DBREG_DR7_ENABLED(d.dr[7], i)) {
670                         type = DBREG_DR7_ACCESS(d.dr[7], i);
671                         len = DBREG_DR7_LEN(d.dr[7], i);
672                         if (len == DBREG_DR7_LEN_8)
673                                 len = 8;
674                         else
675                                 len++;
676                         db_printf("  %-5d  %-8s  %10s  %3d  ",
677                             i, "enabled", watchtype_str(type), len);
678                         db_printsym((db_addr_t)DBREG_DRX((&d), i), DB_STGY_ANY);
679                         db_printf("\n");
680                 } else {
681                         db_printf("  %-5d  disabled\n", i);
682                 }
683         }
684
685         db_printf("\ndebug register values:\n");
686         for (i = 0; i < 8; i++) {
687                 db_printf("  dr%d 0x%016lx\n", i, DBREG_DRX((&d), i));
688         }
689         db_printf("\n");
690 }