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