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

Cache object: 3d829e4f0ca8c5d59adcc6ce624ba274


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