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