The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/amd64/amd64/db_trace.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: d919a70d153aae398699220294bba85c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.