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/8.3/sys/kern/subr_kdb.c 225796 2011-09-27 13:50:30Z attilio $");
   29 
   30 #include "opt_kdb.h"
   31 #include "opt_stack.h"
   32 
   33 #include <sys/param.h>
   34 #include <sys/systm.h>
   35 #include <sys/kdb.h>
   36 #include <sys/kernel.h>
   37 #include <sys/malloc.h>
   38 #include <sys/pcpu.h>
   39 #include <sys/proc.h>
   40 #include <sys/smp.h>
   41 #include <sys/stack.h>
   42 #include <sys/sysctl.h>
   43 
   44 #include <machine/kdb.h>
   45 #include <machine/pcb.h>
   46 
   47 #ifdef SMP
   48 #include <machine/smp.h>
   49 #endif
   50 
   51 int kdb_active = 0;
   52 static void *kdb_jmpbufp = NULL;
   53 struct kdb_dbbe *kdb_dbbe = NULL;
   54 static struct pcb kdb_pcb;
   55 struct pcb *kdb_thrctx = NULL;
   56 struct thread *kdb_thread = NULL;
   57 struct trapframe *kdb_frame = NULL;
   58 
   59 KDB_BACKEND(null, NULL, NULL, NULL);
   60 SET_DECLARE(kdb_dbbe_set, struct kdb_dbbe);
   61 
   62 static int kdb_sysctl_available(SYSCTL_HANDLER_ARGS);
   63 static int kdb_sysctl_current(SYSCTL_HANDLER_ARGS);
   64 static int kdb_sysctl_enter(SYSCTL_HANDLER_ARGS);
   65 static int kdb_sysctl_panic(SYSCTL_HANDLER_ARGS);
   66 static int kdb_sysctl_trap(SYSCTL_HANDLER_ARGS);
   67 static int kdb_sysctl_trap_code(SYSCTL_HANDLER_ARGS);
   68 
   69 SYSCTL_NODE(_debug, OID_AUTO, kdb, CTLFLAG_RW, NULL, "KDB nodes");
   70 
   71 SYSCTL_PROC(_debug_kdb, OID_AUTO, available, CTLTYPE_STRING | CTLFLAG_RD, NULL,
   72     0, kdb_sysctl_available, "A", "list of available KDB backends");
   73 
   74 SYSCTL_PROC(_debug_kdb, OID_AUTO, current, CTLTYPE_STRING | CTLFLAG_RW, NULL,
   75     0, kdb_sysctl_current, "A", "currently selected KDB backend");
   76 
   77 SYSCTL_PROC(_debug_kdb, OID_AUTO, enter, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
   78     kdb_sysctl_enter, "I", "set to enter the debugger");
   79 
   80 SYSCTL_PROC(_debug_kdb, OID_AUTO, panic, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
   81     kdb_sysctl_panic, "I", "set to panic the kernel");
   82 
   83 SYSCTL_PROC(_debug_kdb, OID_AUTO, trap, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
   84     kdb_sysctl_trap, "I", "set to cause a page fault via data access");
   85 
   86 SYSCTL_PROC(_debug_kdb, OID_AUTO, trap_code, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
   87     kdb_sysctl_trap_code, "I", "set to cause a page fault via code access");
   88 
   89 /*
   90  * Flag indicating whether or not to IPI the other CPUs to stop them on
   91  * entering the debugger.  Sometimes, this will result in a deadlock as
   92  * stop_cpus() waits for the other cpus to stop, so we allow it to be
   93  * disabled.  In order to maximize the chances of success, use a hard
   94  * stop for that.
   95  */
   96 #ifdef SMP
   97 static int kdb_stop_cpus = 1;
   98 SYSCTL_INT(_debug_kdb, OID_AUTO, stop_cpus, CTLFLAG_RW | CTLFLAG_TUN,
   99     &kdb_stop_cpus, 0, "stop other CPUs when entering the debugger");
  100 TUNABLE_INT("debug.kdb.stop_cpus", &kdb_stop_cpus);
  101 #endif
  102 
  103 /*
  104  * Flag to indicate to debuggers why the debugger was entered.
  105  */
  106 const char * volatile kdb_why = KDB_WHY_UNSET;
  107 
  108 static int
  109 kdb_sysctl_available(SYSCTL_HANDLER_ARGS)
  110 {
  111         struct kdb_dbbe *be, **iter;
  112         char *avail, *p;
  113         ssize_t len, sz;
  114         int error;
  115 
  116         sz = 0;
  117         SET_FOREACH(iter, kdb_dbbe_set) {
  118                 be = *iter;
  119                 if (be->dbbe_active == 0)
  120                         sz += strlen(be->dbbe_name) + 1;
  121         }
  122         sz++;
  123         avail = malloc(sz, M_TEMP, M_WAITOK);
  124         p = avail;
  125         *p = '\0';
  126 
  127         SET_FOREACH(iter, kdb_dbbe_set) {
  128                 be = *iter;
  129                 if (be->dbbe_active == 0) {
  130                         len = snprintf(p, sz, "%s ", be->dbbe_name);
  131                         p += len;
  132                         sz -= len;
  133                 }
  134         }
  135         KASSERT(sz >= 0, ("%s", __func__));
  136         error = sysctl_handle_string(oidp, avail, 0, req);
  137         free(avail, M_TEMP);
  138         return (error);
  139 }
  140 
  141 static int
  142 kdb_sysctl_current(SYSCTL_HANDLER_ARGS)
  143 {
  144         char buf[16];
  145         int error;
  146 
  147         if (kdb_dbbe != NULL)
  148                 strlcpy(buf, kdb_dbbe->dbbe_name, sizeof(buf));
  149         else
  150                 *buf = '\0';
  151         error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
  152         if (error != 0 || req->newptr == NULL)
  153                 return (error);
  154         if (kdb_active)
  155                 return (EBUSY);
  156         return (kdb_dbbe_select(buf));
  157 }
  158 
  159 static int
  160 kdb_sysctl_enter(SYSCTL_HANDLER_ARGS)
  161 {
  162         int error, i;
  163 
  164         error = sysctl_wire_old_buffer(req, sizeof(int));
  165         if (error == 0) {
  166                 i = 0;
  167                 error = sysctl_handle_int(oidp, &i, 0, req);
  168         }
  169         if (error != 0 || req->newptr == NULL)
  170                 return (error);
  171         if (kdb_active)
  172                 return (EBUSY);
  173         kdb_enter(KDB_WHY_SYSCTL, "sysctl debug.kdb.enter");
  174         return (0);
  175 }
  176 
  177 static int
  178 kdb_sysctl_panic(SYSCTL_HANDLER_ARGS)
  179 {
  180         int error, i;
  181 
  182         error = sysctl_wire_old_buffer(req, sizeof(int));
  183         if (error == 0) {
  184                 i = 0;
  185                 error = sysctl_handle_int(oidp, &i, 0, req);
  186         }
  187         if (error != 0 || req->newptr == NULL)
  188                 return (error);
  189         panic("kdb_sysctl_panic");
  190         return (0);
  191 }
  192 
  193 static int
  194 kdb_sysctl_trap(SYSCTL_HANDLER_ARGS)
  195 {
  196         int error, i;
  197         int *addr = (int *)0x10;
  198 
  199         error = sysctl_wire_old_buffer(req, sizeof(int));
  200         if (error == 0) {
  201                 i = 0;
  202                 error = sysctl_handle_int(oidp, &i, 0, req);
  203         }
  204         if (error != 0 || req->newptr == NULL)
  205                 return (error);
  206         return (*addr);
  207 }
  208 
  209 static int
  210 kdb_sysctl_trap_code(SYSCTL_HANDLER_ARGS)
  211 {
  212         int error, i;
  213         void (*fp)(u_int, u_int, u_int) = (void *)0xdeadc0de;
  214 
  215         error = sysctl_wire_old_buffer(req, sizeof(int));
  216         if (error == 0) {
  217                 i = 0;
  218                 error = sysctl_handle_int(oidp, &i, 0, req);
  219         }
  220         if (error != 0 || req->newptr == NULL)
  221                 return (error);
  222         (*fp)(0x11111111, 0x22222222, 0x33333333);
  223         return (0);
  224 }
  225 
  226 void
  227 kdb_panic(const char *msg)
  228 {
  229         
  230 #ifdef SMP
  231         stop_cpus_hard(PCPU_GET(other_cpus));
  232 #endif
  233         printf("KDB: panic\n");
  234         panic("%s", msg);
  235 }
  236 
  237 void
  238 kdb_reboot(void)
  239 {
  240 
  241         printf("KDB: reboot requested\n");
  242         shutdown_nice(0);
  243 }
  244 
  245 /*
  246  * Solaris implements a new BREAK which is initiated by a character sequence
  247  * CR ~ ^b which is similar to a familiar pattern used on Sun servers by the
  248  * Remote Console.
  249  *
  250  * Note that this function may be called from almost anywhere, with interrupts
  251  * disabled and with unknown locks held, so it must not access data other than
  252  * its arguments.  Its up to the caller to ensure that the state variable is
  253  * consistent.
  254  */
  255 
  256 #define KEY_CR          13      /* CR '\r' */
  257 #define KEY_TILDE       126     /* ~ */
  258 #define KEY_CRTLB       2       /* ^B */
  259 #define KEY_CRTLP       16      /* ^P */
  260 #define KEY_CRTLR       18      /* ^R */
  261 
  262 int
  263 kdb_alt_break(int key, int *state)
  264 {
  265         int brk;
  266 
  267         brk = 0;
  268         switch (*state) {
  269         case 0:
  270                 if (key == KEY_CR)
  271                         *state = 1;
  272                 break;
  273         case 1:
  274                 if (key == KEY_TILDE)
  275                         *state = 2;
  276                 break;
  277         case 2:
  278                 if (key == KEY_CRTLB)
  279                         brk = KDB_REQ_DEBUGGER;
  280                 else if (key == KEY_CRTLP)
  281                         brk = KDB_REQ_PANIC;
  282                 else if (key == KEY_CRTLR)
  283                         brk = KDB_REQ_REBOOT;
  284                 *state = 0;
  285         }
  286         return (brk);
  287 }
  288 
  289 /*
  290  * Print a backtrace of the calling thread. The backtrace is generated by
  291  * the selected debugger, provided it supports backtraces. If no debugger
  292  * is selected or the current debugger does not support backtraces, this
  293  * function silently returns.
  294  */
  295 
  296 void
  297 kdb_backtrace(void)
  298 {
  299 
  300         if (kdb_dbbe != NULL && kdb_dbbe->dbbe_trace != NULL) {
  301                 printf("KDB: stack backtrace:\n");
  302                 kdb_dbbe->dbbe_trace();
  303         }
  304 #ifdef STACK
  305         else {
  306                 struct stack st;
  307 
  308                 printf("KDB: stack backtrace:\n");
  309                 stack_save(&st);
  310                 stack_print_ddb(&st);
  311         }
  312 #endif
  313 }
  314 
  315 /*
  316  * Set/change the current backend.
  317  */
  318 
  319 int
  320 kdb_dbbe_select(const char *name)
  321 {
  322         struct kdb_dbbe *be, **iter;
  323 
  324         SET_FOREACH(iter, kdb_dbbe_set) {
  325                 be = *iter;
  326                 if (be->dbbe_active == 0 && strcmp(be->dbbe_name, name) == 0) {
  327                         kdb_dbbe = be;
  328                         return (0);
  329                 }
  330         }
  331         return (EINVAL);
  332 }
  333 
  334 /*
  335  * Enter the currently selected debugger. If a message has been provided,
  336  * it is printed first. If the debugger does not support the enter method,
  337  * it is entered by using breakpoint(), which enters the debugger through
  338  * kdb_trap().  The 'why' argument will contain a more mechanically usable
  339  * string than 'msg', and is relied upon by DDB scripting to identify the
  340  * reason for entering the debugger so that the right script can be run.
  341  */
  342 void
  343 kdb_enter(const char *why, const char *msg)
  344 {
  345 
  346         if (kdb_dbbe != NULL && kdb_active == 0) {
  347                 if (msg != NULL)
  348                         printf("KDB: enter: %s\n", msg);
  349                 kdb_why = why;
  350                 breakpoint();
  351                 kdb_why = KDB_WHY_UNSET;
  352         }
  353 }
  354 
  355 /*
  356  * Initialize the kernel debugger interface.
  357  */
  358 
  359 void
  360 kdb_init(void)
  361 {
  362         struct kdb_dbbe *be, **iter;
  363         int cur_pri, pri;
  364 
  365         kdb_active = 0;
  366         kdb_dbbe = NULL;
  367         cur_pri = -1;
  368         SET_FOREACH(iter, kdb_dbbe_set) {
  369                 be = *iter;
  370                 pri = (be->dbbe_init != NULL) ? be->dbbe_init() : -1;
  371                 be->dbbe_active = (pri >= 0) ? 0 : -1;
  372                 if (pri > cur_pri) {
  373                         cur_pri = pri;
  374                         kdb_dbbe = be;
  375                 }
  376         }
  377         if (kdb_dbbe != NULL) {
  378                 printf("KDB: debugger backends:");
  379                 SET_FOREACH(iter, kdb_dbbe_set) {
  380                         be = *iter;
  381                         if (be->dbbe_active == 0)
  382                                 printf(" %s", be->dbbe_name);
  383                 }
  384                 printf("\n");
  385                 printf("KDB: current backend: %s\n",
  386                     kdb_dbbe->dbbe_name);
  387         }
  388 }
  389 
  390 /*
  391  * Handle contexts.
  392  */
  393 
  394 void *
  395 kdb_jmpbuf(jmp_buf new)
  396 {
  397         void *old;
  398 
  399         old = kdb_jmpbufp;
  400         kdb_jmpbufp = new;
  401         return (old);
  402 }
  403 
  404 void
  405 kdb_reenter(void)
  406 {
  407 
  408         if (!kdb_active || kdb_jmpbufp == NULL)
  409                 return;
  410 
  411         longjmp(kdb_jmpbufp, 1);
  412         /* NOTREACHED */
  413 }
  414 
  415 /*
  416  * Thread related support functions.
  417  */
  418 
  419 struct pcb *
  420 kdb_thr_ctx(struct thread *thr)
  421 {  
  422 #if defined(SMP) && defined(KDB_STOPPEDPCB)
  423         struct pcpu *pc;
  424 #endif
  425  
  426         if (thr == curthread) 
  427                 return (&kdb_pcb);
  428 
  429 #if defined(SMP) && defined(KDB_STOPPEDPCB)
  430         SLIST_FOREACH(pc, &cpuhead, pc_allcpu)  {
  431                 if (pc->pc_curthread == thr && (stopped_cpus & pc->pc_cpumask))
  432                         return (KDB_STOPPEDPCB(pc));
  433         }
  434 #endif
  435         return (thr->td_pcb);
  436 }
  437 
  438 struct thread *
  439 kdb_thr_first(void)
  440 {
  441         struct proc *p;
  442         struct thread *thr;
  443 
  444         p = LIST_FIRST(&allproc);
  445         while (p != NULL) {
  446                 if (p->p_flag & P_INMEM) {
  447                         thr = FIRST_THREAD_IN_PROC(p);
  448                         if (thr != NULL)
  449                                 return (thr);
  450                 }
  451                 p = LIST_NEXT(p, p_list);
  452         }
  453         return (NULL);
  454 }
  455 
  456 struct thread *
  457 kdb_thr_from_pid(pid_t pid)
  458 {
  459         struct proc *p;
  460 
  461         p = LIST_FIRST(&allproc);
  462         while (p != NULL) {
  463                 if (p->p_flag & P_INMEM && p->p_pid == pid)
  464                         return (FIRST_THREAD_IN_PROC(p));
  465                 p = LIST_NEXT(p, p_list);
  466         }
  467         return (NULL);
  468 }
  469 
  470 struct thread *
  471 kdb_thr_lookup(lwpid_t tid)
  472 {
  473         struct thread *thr;
  474 
  475         thr = kdb_thr_first();
  476         while (thr != NULL && thr->td_tid != tid)
  477                 thr = kdb_thr_next(thr);
  478         return (thr);
  479 }
  480 
  481 struct thread *
  482 kdb_thr_next(struct thread *thr)
  483 {
  484         struct proc *p;
  485 
  486         p = thr->td_proc;
  487         thr = TAILQ_NEXT(thr, td_plist);
  488         do {
  489                 if (thr != NULL)
  490                         return (thr);
  491                 p = LIST_NEXT(p, p_list);
  492                 if (p != NULL && (p->p_flag & P_INMEM))
  493                         thr = FIRST_THREAD_IN_PROC(p);
  494         } while (p != NULL);
  495         return (NULL);
  496 }
  497 
  498 int
  499 kdb_thr_select(struct thread *thr)
  500 {
  501         if (thr == NULL)
  502                 return (EINVAL);
  503         kdb_thread = thr;
  504         kdb_thrctx = kdb_thr_ctx(thr);
  505         return (0);
  506 }
  507 
  508 /*
  509  * Enter the debugger due to a trap.
  510  */
  511 
  512 int
  513 kdb_trap(int type, int code, struct trapframe *tf)
  514 {
  515         struct kdb_dbbe *be;
  516         register_t intr;
  517 #ifdef SMP
  518         int did_stop_cpus;
  519 #endif
  520         int handled;
  521 
  522         be = kdb_dbbe;
  523         if (be == NULL || be->dbbe_trap == NULL)
  524                 return (0);
  525 
  526         /* We reenter the debugger through kdb_reenter(). */
  527         if (kdb_active)
  528                 return (0);
  529 
  530         intr = intr_disable();
  531 
  532 #ifdef SMP
  533         if ((did_stop_cpus = kdb_stop_cpus) != 0)
  534                 stop_cpus_hard(PCPU_GET(other_cpus));
  535 #endif
  536 
  537         kdb_active++;
  538 
  539         kdb_frame = tf;
  540 
  541         /* Let MD code do its thing first... */
  542         kdb_cpu_trap(type, code);
  543 
  544         makectx(tf, &kdb_pcb);
  545         kdb_thr_select(curthread);
  546 
  547         for (;;) {
  548                 handled = be->dbbe_trap(type, code);
  549                 if (be == kdb_dbbe)
  550                         break;
  551                 be = kdb_dbbe;
  552                 if (be == NULL || be->dbbe_trap == NULL)
  553                         break;
  554                 printf("Switching to %s back-end\n", be->dbbe_name);
  555         }
  556 
  557         kdb_active--;
  558 
  559 #ifdef SMP
  560         if (did_stop_cpus)
  561                 restart_cpus(stopped_cpus);
  562 #endif
  563 
  564         intr_restore(intr);
  565 
  566         return (handled);
  567 }

Cache object: d25bd3337f6843269680f37db98b0563


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