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