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