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