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/kern/subr_kdb.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  * Copyright (c) 2004 The FreeBSD Project
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  *
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
   16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   18  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/5.4/sys/kern/subr_kdb.c 145750 2005-05-01 05:38:14Z dwhite $");
   29 
   30 #include <sys/param.h>
   31 #include <sys/systm.h>
   32 #include <sys/kdb.h>
   33 #include <sys/kernel.h>
   34 #include <sys/malloc.h>
   35 #include <sys/pcpu.h>
   36 #include <sys/proc.h>
   37 #include <sys/smp.h>
   38 #include <sys/sysctl.h>
   39 
   40 #include <machine/kdb.h>
   41 #include <machine/pcb.h>
   42 
   43 #ifdef KDB_STOP_NMI
   44 #include <machine/smp.h>
   45 #endif
   46 
   47 /* 
   48  * KDB_STOP_NMI requires SMP to pick up the right dependencies
   49  * (And isn't useful on UP anyway) 
   50  */
   51 #if defined(KDB_STOP_NMI) && !defined(SMP)
   52 #error "options KDB_STOP_NMI" requires "options SMP"
   53 #endif
   54 
   55 int kdb_active = 0;
   56 void *kdb_jmpbufp = NULL;
   57 struct kdb_dbbe *kdb_dbbe = NULL;
   58 struct pcb kdb_pcb;
   59 struct pcb *kdb_thrctx = NULL;
   60 struct thread *kdb_thread = NULL;
   61 struct trapframe *kdb_frame = NULL;
   62 
   63 KDB_BACKEND(null, NULL, NULL, NULL);
   64 SET_DECLARE(kdb_dbbe_set, struct kdb_dbbe);
   65 
   66 static int kdb_sysctl_available(SYSCTL_HANDLER_ARGS);
   67 static int kdb_sysctl_current(SYSCTL_HANDLER_ARGS);
   68 static int kdb_sysctl_enter(SYSCTL_HANDLER_ARGS);
   69 
   70 SYSCTL_NODE(_debug, OID_AUTO, kdb, CTLFLAG_RW, NULL, "KDB nodes");
   71 
   72 SYSCTL_PROC(_debug_kdb, OID_AUTO, available, CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
   73     kdb_sysctl_available, "A", "list of available KDB backends");
   74 
   75 SYSCTL_PROC(_debug_kdb, OID_AUTO, current, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
   76     kdb_sysctl_current, "A", "currently selected KDB backend");
   77 
   78 SYSCTL_PROC(_debug_kdb, OID_AUTO, enter, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
   79     kdb_sysctl_enter, "I", "set to enter the debugger");
   80 
   81 /*
   82  * Flag indicating whether or not to IPI the other CPUs to stop them on
   83  * entering the debugger.  Sometimes, this will result in a deadlock as
   84  * stop_cpus() waits for the other cpus to stop, so we allow it to be
   85  * disabled.
   86  */
   87 #ifdef SMP
   88 static int kdb_stop_cpus = 1;
   89 SYSCTL_INT(_debug_kdb, OID_AUTO, stop_cpus, CTLTYPE_INT | CTLFLAG_RW,
   90     &kdb_stop_cpus, 0, "");
   91 TUNABLE_INT("debug.kdb.stop_cpus", &kdb_stop_cpus);
   92 
   93 #ifdef KDB_STOP_NMI
   94 /* 
   95  * Provide an alternate method of stopping other CPUs. If another CPU has
   96  * disabled interrupts the conventional STOP IPI will be blocked. This 
   97  * NMI-based stop should get through in that case.
   98  */
   99 static int kdb_stop_cpus_with_nmi = 0;
  100 SYSCTL_INT(_debug_kdb, OID_AUTO, stop_cpus_with_nmi, CTLTYPE_INT | CTLFLAG_RW,
  101     &kdb_stop_cpus_with_nmi, 0, "");
  102 TUNABLE_INT("debug.kdb.stop_cpus_with_nmi", &kdb_stop_cpus_with_nmi);
  103 #endif /* KDB_STOP_NMI */
  104 
  105 #endif
  106 
  107 static int
  108 kdb_sysctl_available(SYSCTL_HANDLER_ARGS)
  109 {
  110         struct kdb_dbbe *be, **iter;
  111         char *avail, *p;
  112         ssize_t len, sz;
  113         int error;
  114 
  115         sz = 0;
  116         SET_FOREACH(iter, kdb_dbbe_set) {
  117                 be = *iter;
  118                 if (be->dbbe_active == 0)
  119                         sz += strlen(be->dbbe_name) + 1;
  120         }
  121         sz++;
  122         avail = malloc(sz, M_TEMP, M_WAITOK);
  123         p = avail;
  124         SET_FOREACH(iter, kdb_dbbe_set) {
  125                 be = *iter;
  126                 if (be->dbbe_active == 0) {
  127                         len = snprintf(p, sz, "%s ", be->dbbe_name);
  128                         p += len;
  129                         sz -= len;
  130                 }
  131         }
  132         KASSERT(sz >= 0, ("%s", __func__));
  133         error = sysctl_handle_string(oidp, avail, 0, req);
  134         free(avail, M_TEMP);
  135         return (error);
  136 }
  137 
  138 static int
  139 kdb_sysctl_current(SYSCTL_HANDLER_ARGS)
  140 {
  141         char buf[16];
  142         int error;
  143 
  144         if (kdb_dbbe != NULL) {
  145                 strncpy(buf, kdb_dbbe->dbbe_name, sizeof(buf));
  146                 buf[sizeof(buf) - 1] = '\0';
  147         } else
  148                 *buf = '\0';
  149         error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
  150         if (error != 0 || req->newptr == NULL)
  151                 return (error);
  152         if (kdb_active)
  153                 return (EBUSY);
  154         return (kdb_dbbe_select(buf));
  155 }
  156 
  157 static int
  158 kdb_sysctl_enter(SYSCTL_HANDLER_ARGS)
  159 {
  160         int error, i;
  161 
  162         error = sysctl_wire_old_buffer(req, sizeof(int));
  163         if (error == 0) {
  164                 i = 0;
  165                 error = sysctl_handle_int(oidp, &i, 0, req);
  166         }
  167         if (error != 0 || req->newptr == NULL)
  168                 return (error);
  169         if (kdb_active)
  170                 return (EBUSY);
  171         kdb_enter("sysctl debug.kdb.enter");
  172         return (0);
  173 }
  174 
  175 /*
  176  * Solaris implements a new BREAK which is initiated by a character sequence
  177  * CR ~ ^b which is similar to a familiar pattern used on Sun servers by the
  178  * Remote Console.
  179  *
  180  * Note that this function may be called from almost anywhere, with interrupts
  181  * disabled and with unknown locks held, so it must not access data other than
  182  * its arguments.  Its up to the caller to ensure that the state variable is
  183  * consistent.
  184  */
  185 
  186 #define KEY_CR          13      /* CR '\r' */
  187 #define KEY_TILDE       126     /* ~ */
  188 #define KEY_CRTLB       2       /* ^B */
  189 
  190 int
  191 kdb_alt_break(int key, int *state)
  192 {
  193         int brk;
  194 
  195         brk = 0;
  196         switch (key) {
  197         case KEY_CR:
  198                 *state = KEY_TILDE;
  199                 break;
  200         case KEY_TILDE:
  201                 *state = (*state == KEY_TILDE) ? KEY_CRTLB : 0;
  202                 break;
  203         case KEY_CRTLB:
  204                 if (*state == KEY_CRTLB)
  205                         brk = 1;
  206                 /* FALLTHROUGH */
  207         default:
  208                 *state = 0;
  209                 break;
  210         }
  211         return (brk);
  212 }
  213 
  214 /*
  215  * Print a backtrace of the calling thread. The backtrace is generated by
  216  * the selected debugger, provided it supports backtraces. If no debugger
  217  * is selected or the current debugger does not support backtraces, this
  218  * function silently returns.
  219  */
  220 
  221 void
  222 kdb_backtrace()
  223 {
  224 
  225         if (kdb_dbbe != NULL && kdb_dbbe->dbbe_trace != NULL) {
  226                 printf("KDB: stack backtrace:\n");
  227                 kdb_dbbe->dbbe_trace();
  228         }
  229 }
  230 
  231 /*
  232  * Set/change the current backend.
  233  */
  234 
  235 int
  236 kdb_dbbe_select(const char *name)
  237 {
  238         struct kdb_dbbe *be, **iter;
  239 
  240         SET_FOREACH(iter, kdb_dbbe_set) {
  241                 be = *iter;
  242                 if (be->dbbe_active == 0 && strcmp(be->dbbe_name, name) == 0) {
  243                         kdb_dbbe = be;
  244                         return (0);
  245                 }
  246         }
  247         return (EINVAL);
  248 }
  249 
  250 /*
  251  * Enter the currently selected debugger. If a message has been provided,
  252  * it is printed first. If the debugger does not support the enter method,
  253  * it is entered by using breakpoint(), which enters the debugger through
  254  * kdb_trap().
  255  */
  256 
  257 void
  258 kdb_enter(const char *msg)
  259 {
  260 
  261         if (kdb_dbbe != NULL && kdb_active == 0) {
  262                 if (msg != NULL)
  263                         printf("KDB: enter: %s\n", msg);
  264                 breakpoint();
  265         }
  266 }
  267 
  268 /*
  269  * Initialize the kernel debugger interface.
  270  */
  271 
  272 void
  273 kdb_init()
  274 {
  275         struct kdb_dbbe *be, **iter;
  276         int cur_pri, pri;
  277 
  278         kdb_active = 0;
  279         kdb_dbbe = NULL;
  280         cur_pri = -1;
  281         SET_FOREACH(iter, kdb_dbbe_set) {
  282                 be = *iter;
  283                 pri = (be->dbbe_init != NULL) ? be->dbbe_init() : -1;
  284                 be->dbbe_active = (pri >= 0) ? 0 : -1;
  285                 if (pri > cur_pri) {
  286                         cur_pri = pri;
  287                         kdb_dbbe = be;
  288                 }
  289         }
  290         if (kdb_dbbe != NULL) {
  291                 printf("KDB: debugger backends:");
  292                 SET_FOREACH(iter, kdb_dbbe_set) {
  293                         be = *iter;
  294                         if (be->dbbe_active == 0)
  295                                 printf(" %s", be->dbbe_name);
  296                 }
  297                 printf("\n");
  298                 printf("KDB: current backend: %s\n",
  299                     kdb_dbbe->dbbe_name);
  300         }
  301 }
  302 
  303 /*
  304  * Handle contexts.
  305  */
  306 
  307 void *
  308 kdb_jmpbuf(jmp_buf new)
  309 {
  310         void *old;
  311 
  312         old = kdb_jmpbufp;
  313         kdb_jmpbufp = new;
  314         return (old);
  315 }
  316 
  317 void
  318 kdb_reenter(void)
  319 {
  320 
  321         if (!kdb_active || kdb_jmpbufp == NULL)
  322                 return;
  323 
  324         longjmp(kdb_jmpbufp, 1);
  325         /* NOTREACHED */
  326 }
  327 
  328 /*
  329  * Thread related support functions.
  330  */
  331 
  332 struct pcb *
  333 kdb_thr_ctx(struct thread *thr)
  334 #ifdef KDB_STOP_NMI
  335 {  
  336   u_int         cpuid;
  337   struct pcpu *pc;
  338   
  339   if (thr == curthread) 
  340     return &kdb_pcb;
  341 
  342   SLIST_FOREACH(pc, &cpuhead, pc_allcpu)  {
  343     cpuid = pc->pc_cpuid;
  344     if (pc->pc_curthread == thr && (atomic_load_acq_int(&stopped_cpus) & (1 << cpuid)))
  345       return &stoppcbs[cpuid];
  346   }
  347 
  348   return  thr->td_pcb;
  349 }
  350 #else
  351 {
  352         return ((thr == curthread) ? &kdb_pcb : thr->td_pcb);
  353 }
  354 #endif /* KDB_STOP_NMI */
  355 
  356 struct thread *
  357 kdb_thr_first(void)
  358 {
  359         struct proc *p;
  360         struct thread *thr;
  361 
  362         p = LIST_FIRST(&allproc);
  363         while (p != NULL) {
  364                 if (p->p_sflag & PS_INMEM) {
  365                         thr = FIRST_THREAD_IN_PROC(p);
  366                         if (thr != NULL)
  367                                 return (thr);
  368                 }
  369                 p = LIST_NEXT(p, p_list);
  370         }
  371         return (NULL);
  372 }
  373 
  374 struct thread *
  375 kdb_thr_from_pid(pid_t pid)
  376 {
  377         struct proc *p;
  378 
  379         p = LIST_FIRST(&allproc);
  380         while (p != NULL) {
  381                 if (p->p_sflag & PS_INMEM && p->p_pid == pid)
  382                         return (FIRST_THREAD_IN_PROC(p));
  383                 p = LIST_NEXT(p, p_list);
  384         }
  385         return (NULL);
  386 }
  387 
  388 struct thread *
  389 kdb_thr_lookup(lwpid_t tid)
  390 {
  391         struct thread *thr;
  392 
  393         thr = kdb_thr_first();
  394         while (thr != NULL && thr->td_tid != tid)
  395                 thr = kdb_thr_next(thr);
  396         return (thr);
  397 }
  398 
  399 struct thread *
  400 kdb_thr_next(struct thread *thr)
  401 {
  402         struct proc *p;
  403 
  404         p = thr->td_proc;
  405         thr = TAILQ_NEXT(thr, td_plist);
  406         do {
  407                 if (thr != NULL)
  408                         return (thr);
  409                 p = LIST_NEXT(p, p_list);
  410                 if (p != NULL && (p->p_sflag & PS_INMEM))
  411                         thr = FIRST_THREAD_IN_PROC(p);
  412         } while (p != NULL);
  413         return (NULL);
  414 }
  415 
  416 int
  417 kdb_thr_select(struct thread *thr)
  418 {
  419         if (thr == NULL)
  420                 return (EINVAL);
  421         kdb_thread = thr;
  422         kdb_thrctx = kdb_thr_ctx(thr);
  423         return (0);
  424 }
  425 
  426 /*
  427  * Enter the debugger due to a trap.
  428  */
  429 
  430 int
  431 kdb_trap(int type, int code, struct trapframe *tf)
  432 {
  433 #ifdef SMP
  434         int did_stop_cpus;
  435 #endif
  436         int handled;
  437 
  438         if (kdb_dbbe == NULL || kdb_dbbe->dbbe_trap == NULL)
  439                 return (0);
  440 
  441         /* We reenter the debugger through kdb_reenter(). */
  442         if (kdb_active)
  443                 return (0);
  444 
  445         makectx(tf, &kdb_pcb);
  446 
  447         critical_enter();
  448 
  449         kdb_active++;
  450         kdb_frame = tf;
  451         kdb_thr_select(curthread);
  452 
  453 #ifdef SMP
  454         if ((did_stop_cpus = kdb_stop_cpus) != 0)
  455           {
  456 #ifdef KDB_STOP_NMI
  457             if(kdb_stop_cpus_with_nmi)
  458               stop_cpus_nmi(PCPU_GET(other_cpus));
  459             else
  460 #endif /* KDB_STOP_NMI */
  461                 stop_cpus(PCPU_GET(other_cpus));
  462           }
  463 #endif
  464 
  465         /* Let MD code do its thing first... */
  466         kdb_cpu_trap(type, code);
  467 
  468         handled = kdb_dbbe->dbbe_trap(type, code);
  469 
  470 #ifdef SMP
  471         if (did_stop_cpus)
  472                 restart_cpus(stopped_cpus);
  473 #endif
  474 
  475         kdb_active--;
  476 
  477         critical_exit();
  478 
  479         return (handled);
  480 }

Cache object: fc9fdf21c53c1eb646d1c12b60630c9b


[ 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.