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