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