Adjust pamp_growkernel(), elf_brand_inuse(), and ktrace() to use
[dragonfly.git] / sys / i386 / 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 $
0e5797fe 27 * $DragonFly: src/sys/i386/i386/Attic/db_trace.c,v 1.12 2006/05/25 04:17:07 dillon Exp $
984263bc
MD
28 */
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/linker_set.h>
33#include <sys/lock.h>
34#include <sys/proc.h>
35
36#include <machine/cpu.h>
37#include <machine/md_var.h>
38#include <machine/reg.h>
39
40#include <vm/vm.h>
41#include <vm/vm_param.h>
42#include <vm/pmap.h>
43#include <vm/vm_map.h>
44#include <ddb/ddb.h>
45
46#include <sys/user.h>
47
48#include <ddb/db_access.h>
49#include <ddb/db_sym.h>
50#include <ddb/db_variables.h>
51
52db_varfcn_t db_dr0;
53db_varfcn_t db_dr1;
54db_varfcn_t db_dr2;
55db_varfcn_t db_dr3;
56db_varfcn_t db_dr4;
57db_varfcn_t db_dr5;
58db_varfcn_t db_dr6;
59db_varfcn_t db_dr7;
60
61/*
62 * Machine register set.
63 */
64struct db_variable db_regs[] = {
65 { "cs", &ddb_regs.tf_cs, FCN_NULL },
66 { "ds", &ddb_regs.tf_ds, FCN_NULL },
67 { "es", &ddb_regs.tf_es, FCN_NULL },
68 { "fs", &ddb_regs.tf_fs, FCN_NULL },
69#if 0
70 { "gs", &ddb_regs.tf_gs, FCN_NULL },
71#endif
72 { "ss", &ddb_regs.tf_ss, FCN_NULL },
73 { "eax", &ddb_regs.tf_eax, FCN_NULL },
74 { "ecx", &ddb_regs.tf_ecx, FCN_NULL },
75 { "edx", &ddb_regs.tf_edx, FCN_NULL },
76 { "ebx", &ddb_regs.tf_ebx, FCN_NULL },
77 { "esp", &ddb_regs.tf_esp, FCN_NULL },
78 { "ebp", &ddb_regs.tf_ebp, FCN_NULL },
79 { "esi", &ddb_regs.tf_esi, FCN_NULL },
80 { "edi", &ddb_regs.tf_edi, FCN_NULL },
81 { "eip", &ddb_regs.tf_eip, FCN_NULL },
82 { "efl", &ddb_regs.tf_eflags, FCN_NULL },
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};
92struct db_variable *db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
93
94/*
95 * Stack trace.
96 */
97#define INKERNEL(va) (((vm_offset_t)(va)) >= USRSTACK)
98
99struct i386_frame {
100 struct i386_frame *f_frame;
101 int f_retaddr;
102 int f_arg0;
103};
104
105#define NORMAL 0
106#define TRAP 1
107#define INTERRUPT 2
108#define SYSCALL 3
109
30ef6427
JS
110static void db_nextframe(struct i386_frame **, db_addr_t *);
111static int db_numargs(struct i386_frame *);
112static void db_print_stack_entry(const char *, int, char **, int *, db_addr_t);
984263bc
MD
113
114
30ef6427
JS
115static char *watchtype_str(int type);
116int i386_set_watch(int watchnum, unsigned int watchaddr,
117 int size, int access, struct dbreg * d);
118int i386_clr_watch(int watchnum, struct dbreg * d);
119int db_md_set_watchpoint(db_expr_t addr, db_expr_t size);
120int db_md_clr_watchpoint(db_expr_t addr, db_expr_t size);
121void db_md_list_watchpoints(void);
984263bc
MD
122
123
124/*
125 * Figure out how many arguments were passed into the frame at "fp".
126 */
127static int
30ef6427 128db_numargs(struct i386_frame *fp)
984263bc
MD
129{
130 int *argp;
131 int inst;
132 int args;
133
134 argp = (int *)db_get_value((int)&fp->f_retaddr, 4, FALSE);
135 /*
136 * XXX etext is wrong for LKMs. We should attempt to interpret
137 * the instruction at the return address in all cases. This
138 * may require better fault handling.
139 */
140 if (argp < (int *)btext || argp >= (int *)etext) {
141 args = 5;
142 } else {
143 inst = db_get_value((int)argp, 4, FALSE);
144 if ((inst & 0xff) == 0x59) /* popl %ecx */
145 args = 1;
146 else if ((inst & 0xffff) == 0xc483) /* addl $Ibs, %esp */
147 args = ((inst >> 16) & 0xff) / 4;
148 else
149 args = 5;
150 }
30ef6427 151 return(args);
984263bc
MD
152}
153
154static void
30ef6427
JS
155db_print_stack_entry(const char *name, int narg, char **argnp, int *argp,
156 db_addr_t callpc)
984263bc
MD
157{
158 db_printf("%s(", name);
159 while (narg) {
160 if (argnp)
161 db_printf("%s=", *argnp++);
162 db_printf("%r", db_get_value((int)argp, 4, FALSE));
163 argp++;
164 if (--narg != 0)
165 db_printf(",");
166 }
167 db_printf(") at ");
168 db_printsym(callpc, DB_STGY_PROC);
169 db_printf("\n");
170}
171
172/*
173 * Figure out the next frame up in the call stack.
174 */
175static void
ff803bd8 176db_nextframe(struct i386_frame **fp, db_addr_t *ip)
984263bc
MD
177{
178 struct trapframe *tf;
179 int frame_type;
180 int eip, esp, ebp;
181 db_expr_t offset;
182 const char *sym, *name;
183
184 eip = db_get_value((int) &(*fp)->f_retaddr, 4, FALSE);
185 ebp = db_get_value((int) &(*fp)->f_frame, 4, FALSE);
186
187 /*
188 * Figure out frame type.
189 */
190
191 frame_type = NORMAL;
192
193 sym = db_search_symbol(eip, DB_STGY_ANY, &offset);
194 db_symbol_values(sym, &name, NULL);
195 if (name != NULL) {
196 if (!strcmp(name, "calltrap")) {
197 frame_type = TRAP;
198 } else if (!strncmp(name, "Xresume", 7)) {
199 frame_type = INTERRUPT;
200 } else if (!strcmp(name, "_Xsyscall")) {
201 frame_type = SYSCALL;
202 }
203 }
204
205 /*
206 * Normal frames need no special processing.
207 */
208 if (frame_type == NORMAL) {
209 *ip = (db_addr_t) eip;
210 *fp = (struct i386_frame *) ebp;
211 return;
212 }
213
214 db_print_stack_entry(name, 0, 0, 0, eip);
215
216 /*
217 * Point to base of trapframe which is just above the
218 * current frame.
219 */
220 tf = (struct trapframe *) ((int)*fp + 8);
221
222 esp = (ISPL(tf->tf_cs) == SEL_UPL) ? tf->tf_esp : (int)&tf->tf_esp;
223 switch (frame_type) {
224 case TRAP:
225 if (INKERNEL((int) tf)) {
226 eip = tf->tf_eip;
227 ebp = tf->tf_ebp;
228 db_printf(
229 "--- trap %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
230 tf->tf_trapno, eip, esp, ebp);
231 }
232 break;
233 case SYSCALL:
234 if (INKERNEL((int) tf)) {
235 eip = tf->tf_eip;
236 ebp = tf->tf_ebp;
237 db_printf(
238 "--- syscall %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
239 tf->tf_eax, eip, esp, ebp);
240 }
241 break;
242 case INTERRUPT:
243 tf = (struct trapframe *)((int)*fp + 16);
244 if (INKERNEL((int) tf)) {
245 eip = tf->tf_eip;
246 ebp = tf->tf_ebp;
247 db_printf(
248 "--- interrupt, eip = %#r, esp = %#r, ebp = %#r ---\n",
249 eip, esp, ebp);
250 }
251 break;
252 default:
253 break;
254 }
255
256 *ip = (db_addr_t) eip;
257 *fp = (struct i386_frame *) ebp;
258}
259
260void
30ef6427
JS
261db_stack_trace_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
262 char *modif)
984263bc
MD
263{
264 struct i386_frame *frame;
265 int *argp;
266 db_addr_t callpc;
267 boolean_t first;
268 struct pcb *pcb;
269 struct proc *p;
270 pid_t pid;
8ec5e934 271 int i;
984263bc
MD
272
273 if (count == -1)
274 count = 1024;
275
276 if (!have_addr) {
55a2e6d4 277 frame = (struct i386_frame *)BP_REGS(&ddb_regs);
984263bc 278 if (frame == NULL)
55a2e6d4
MD
279 frame = (struct i386_frame *)(SP_REGS(&ddb_regs) - 4);
280 callpc = PC_REGS(&ddb_regs);
984263bc
MD
281 } else if (!INKERNEL(addr)) {
282 pid = (addr % 16) + ((addr >> 4) % 16) * 10 +
283 ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 +
284 ((addr >> 16) % 16) * 10000;
285 /*
286 * The pcb for curproc is not valid at this point,
287 * so fall back to the default case.
288 */
289 if ((curproc != NULL) && (pid == curproc->p_pid)) {
55a2e6d4 290 frame = (struct i386_frame *)BP_REGS(&ddb_regs);
984263bc
MD
291 if (frame == NULL)
292 frame = (struct i386_frame *)
55a2e6d4
MD
293 (SP_REGS(&ddb_regs) - 4);
294 callpc = PC_REGS(&ddb_regs);
984263bc
MD
295 } else {
296
f62004ad 297 FOREACH_PROC_IN_SYSTEM(p) {
984263bc
MD
298 if (p->p_pid == pid)
299 break;
300 }
984263bc
MD
301 if (p == NULL) {
302 db_printf("pid %d not found\n", pid);
303 return;
304 }
344ad853 305 if ((p->p_flag & P_SWAPPEDOUT)) {
984263bc
MD
306 db_printf("pid %d swapped out\n", pid);
307 return;
308 }
b7c628e4 309 pcb = p->p_thread->td_pcb;
984263bc
MD
310 frame = (struct i386_frame *)pcb->pcb_ebp;
311 if (frame == NULL)
312 frame = (struct i386_frame *)
313 (pcb->pcb_esp - 4);
314 callpc = (db_addr_t)pcb->pcb_eip;
315 }
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
MD
392 }
393 } else if (!strcmp(name, "fork_trampoline")) {
394 /*
395 * Don't try to walk back on a stack for a
396 * process that hasn't actually been run yet.
397 */
398 db_print_stack_entry(name, 0, 0, 0, callpc);
399 break;
400 }
401 first = FALSE;
402 }
403
404 argp = &actframe->f_arg0;
405 narg = MAXNARG;
406 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
407 argnp = argnames;
408 } else {
409 narg = db_numargs(frame);
410 }
411
412 db_print_stack_entry(name, narg, argnp, argp, callpc);
413
414 if (actframe != frame) {
415 /* `frame' belongs to caller. */
416 callpc = (db_addr_t)
417 db_get_value((int)&actframe->f_retaddr, 4, FALSE);
418 continue;
419 }
420
421 db_nextframe(&frame, &callpc);
422
423 if (INKERNEL((int) callpc) && !INKERNEL((int) frame)) {
424 sym = db_search_symbol(callpc, DB_STGY_ANY, &offset);
425 db_symbol_values(sym, &name, NULL);
426 db_print_stack_entry(name, 0, 0, 0, callpc);
427 break;
428 }
429 if (!INKERNEL((int) frame)) {
430 break;
431 }
432 }
433}
434
6bd9bbba
HP
435void
436db_print_backtrace(void)
437{
30ef6427 438 register_t ebp;
6bd9bbba 439
30ef6427
JS
440 __asm __volatile("movl %%ebp, %0" : "=r" (ebp));
441 db_stack_trace_cmd(ebp, 1, -1, NULL);
6bd9bbba
HP
442}
443
30ef6427
JS
444#define DB_DRX_FUNC(reg) \
445int \
446db_ ## reg (struct db_variable *vp, db_expr_t *valuep, int op) \
447{ \
448 if (op == DB_VAR_GET) \
449 *valuep = r ## reg (); \
450 else \
451 load_ ## reg (*valuep); \
452 \
453 return(0); \
984263bc
MD
454}
455
456DB_DRX_FUNC(dr0)
457DB_DRX_FUNC(dr1)
458DB_DRX_FUNC(dr2)
459DB_DRX_FUNC(dr3)
460DB_DRX_FUNC(dr4)
461DB_DRX_FUNC(dr5)
462DB_DRX_FUNC(dr6)
463DB_DRX_FUNC(dr7)
464
465
984263bc 466int
30ef6427
JS
467i386_set_watch(int watchnum, unsigned int watchaddr, int size, int access,
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
30ef6427 527i386_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++;
30ef6427 565 i386_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))
984263bc 588 i386_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}