2 * Copyright (c) 2004 The FreeBSD Project
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
38 #include <sys/sysctl.h>
40 #include <machine/kdb.h>
41 #include <machine/pcb.h>
44 #include <machine/smp.h>
48 * KDB_STOP_NMI requires SMP to pick up the right dependencies
49 * (And isn't useful on UP anyway)
51 #if defined(KDB_STOP_NMI) && !defined(SMP)
52 #error "options KDB_STOP_NMI" requires "options SMP"
56 void *kdb_jmpbufp = NULL;
57 struct kdb_dbbe *kdb_dbbe = NULL;
59 struct pcb *kdb_thrctx = NULL;
60 struct thread *kdb_thread = NULL;
61 struct trapframe *kdb_frame = NULL;
63 KDB_BACKEND(null, NULL, NULL, NULL);
64 SET_DECLARE(kdb_dbbe_set, struct kdb_dbbe);
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);
70 SYSCTL_NODE(_debug, OID_AUTO, kdb, CTLFLAG_RW, NULL, "KDB nodes");
72 SYSCTL_PROC(_debug_kdb, OID_AUTO, available, CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
73 kdb_sysctl_available, "A", "list of available KDB backends");
75 SYSCTL_PROC(_debug_kdb, OID_AUTO, current, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
76 kdb_sysctl_current, "A", "currently selected KDB backend");
78 SYSCTL_PROC(_debug_kdb, OID_AUTO, enter, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
79 kdb_sysctl_enter, "I", "set to enter the debugger");
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
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);
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.
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 */
108 kdb_sysctl_available(SYSCTL_HANDLER_ARGS)
110 struct kdb_dbbe *be, **iter;
116 SET_FOREACH(iter, kdb_dbbe_set) {
118 if (be->dbbe_active == 0)
119 sz += strlen(be->dbbe_name) + 1;
122 avail = malloc(sz, M_TEMP, M_WAITOK);
126 SET_FOREACH(iter, kdb_dbbe_set) {
128 if (be->dbbe_active == 0) {
129 len = snprintf(p, sz, "%s ", be->dbbe_name);
134 KASSERT(sz >= 0, ("%s", __func__));
135 error = sysctl_handle_string(oidp, avail, 0, req);
141 kdb_sysctl_current(SYSCTL_HANDLER_ARGS)
146 if (kdb_dbbe != NULL) {
147 strncpy(buf, kdb_dbbe->dbbe_name, sizeof(buf));
148 buf[sizeof(buf) - 1] = '\0';
151 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
152 if (error != 0 || req->newptr == NULL)
156 return (kdb_dbbe_select(buf));
160 kdb_sysctl_enter(SYSCTL_HANDLER_ARGS)
164 error = sysctl_wire_old_buffer(req, sizeof(int));
167 error = sysctl_handle_int(oidp, &i, 0, req);
169 if (error != 0 || req->newptr == NULL)
173 kdb_enter("sysctl debug.kdb.enter");
178 * Solaris implements a new BREAK which is initiated by a character sequence
179 * CR ~ ^b which is similar to a familiar pattern used on Sun servers by the
182 * Note that this function may be called from almost anywhere, with interrupts
183 * disabled and with unknown locks held, so it must not access data other than
184 * its arguments. Its up to the caller to ensure that the state variable is
188 #define KEY_CR 13 /* CR '\r' */
189 #define KEY_TILDE 126 /* ~ */
190 #define KEY_CRTLB 2 /* ^B */
193 kdb_alt_break(int key, int *state)
203 *state = (*state == KEY_TILDE) ? KEY_CRTLB : 0;
206 if (*state == KEY_CRTLB)
217 * Print a backtrace of the calling thread. The backtrace is generated by
218 * the selected debugger, provided it supports backtraces. If no debugger
219 * is selected or the current debugger does not support backtraces, this
220 * function silently returns.
227 if (kdb_dbbe != NULL && kdb_dbbe->dbbe_trace != NULL) {
228 printf("KDB: stack backtrace:\n");
229 kdb_dbbe->dbbe_trace();
234 * Set/change the current backend.
238 kdb_dbbe_select(const char *name)
240 struct kdb_dbbe *be, **iter;
242 SET_FOREACH(iter, kdb_dbbe_set) {
244 if (be->dbbe_active == 0 && strcmp(be->dbbe_name, name) == 0) {
253 * Enter the currently selected debugger. If a message has been provided,
254 * it is printed first. If the debugger does not support the enter method,
255 * it is entered by using breakpoint(), which enters the debugger through
260 kdb_enter(const char *msg)
263 if (kdb_dbbe != NULL && kdb_active == 0) {
265 printf("KDB: enter: %s\n", msg);
271 * Initialize the kernel debugger interface.
277 struct kdb_dbbe *be, **iter;
283 SET_FOREACH(iter, kdb_dbbe_set) {
285 pri = (be->dbbe_init != NULL) ? be->dbbe_init() : -1;
286 be->dbbe_active = (pri >= 0) ? 0 : -1;
292 if (kdb_dbbe != NULL) {
293 printf("KDB: debugger backends:");
294 SET_FOREACH(iter, kdb_dbbe_set) {
296 if (be->dbbe_active == 0)
297 printf(" %s", be->dbbe_name);
300 printf("KDB: current backend: %s\n",
301 kdb_dbbe->dbbe_name);
310 kdb_jmpbuf(jmp_buf new)
323 if (!kdb_active || kdb_jmpbufp == NULL)
326 longjmp(kdb_jmpbufp, 1);
331 * Thread related support functions.
335 kdb_thr_ctx(struct thread *thr)
341 if (thr == curthread)
344 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
345 cpuid = pc->pc_cpuid;
346 if (pc->pc_curthread == thr && (atomic_load_acq_int(&stopped_cpus) & (1 << cpuid)))
347 return &stoppcbs[cpuid];
354 return ((thr == curthread) ? &kdb_pcb : thr->td_pcb);
356 #endif /* KDB_STOP_NMI */
364 p = LIST_FIRST(&allproc);
366 if (p->p_sflag & PS_INMEM) {
367 thr = FIRST_THREAD_IN_PROC(p);
371 p = LIST_NEXT(p, p_list);
377 kdb_thr_from_pid(pid_t pid)
381 p = LIST_FIRST(&allproc);
383 if (p->p_sflag & PS_INMEM && p->p_pid == pid)
384 return (FIRST_THREAD_IN_PROC(p));
385 p = LIST_NEXT(p, p_list);
391 kdb_thr_lookup(lwpid_t tid)
395 thr = kdb_thr_first();
396 while (thr != NULL && thr->td_tid != tid)
397 thr = kdb_thr_next(thr);
402 kdb_thr_next(struct thread *thr)
407 thr = TAILQ_NEXT(thr, td_plist);
411 p = LIST_NEXT(p, p_list);
412 if (p != NULL && (p->p_sflag & PS_INMEM))
413 thr = FIRST_THREAD_IN_PROC(p);
419 kdb_thr_select(struct thread *thr)
424 kdb_thrctx = kdb_thr_ctx(thr);
429 * Enter the debugger due to a trap.
433 kdb_trap(int type, int code, struct trapframe *tf)
440 if (kdb_dbbe == NULL || kdb_dbbe->dbbe_trap == NULL)
443 /* We reenter the debugger through kdb_reenter(). */
452 if ((did_stop_cpus = kdb_stop_cpus) != 0)
455 if(kdb_stop_cpus_with_nmi)
456 stop_cpus_nmi(PCPU_GET(other_cpus));
458 #endif /* KDB_STOP_NMI */
459 stop_cpus(PCPU_GET(other_cpus));
465 /* Let MD code do its thing first... */
466 kdb_cpu_trap(type, code);
468 makectx(tf, &kdb_pcb);
469 kdb_thr_select(curthread);
471 handled = kdb_dbbe->dbbe_trap(type, code);
475 restart_cpus(stopped_cpus);