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$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
40 #include <sys/sysctl.h>
42 #include <machine/kdb.h>
43 #include <machine/pcb.h>
46 #include <machine/smp.h>
50 * KDB_STOP_NMI requires SMP to pick up the right dependencies
51 * (And isn't useful on UP anyway)
53 #if defined(KDB_STOP_NMI) && !defined(SMP)
54 #error "options KDB_STOP_NMI" requires "options SMP"
58 void *kdb_jmpbufp = NULL;
59 struct kdb_dbbe *kdb_dbbe = NULL;
61 struct pcb *kdb_thrctx = NULL;
62 struct thread *kdb_thread = NULL;
63 struct trapframe *kdb_frame = NULL;
65 KDB_BACKEND(null, NULL, NULL, NULL);
66 SET_DECLARE(kdb_dbbe_set, struct kdb_dbbe);
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);
72 SYSCTL_NODE(_debug, OID_AUTO, kdb, CTLFLAG_RW, NULL, "KDB nodes");
74 SYSCTL_PROC(_debug_kdb, OID_AUTO, available, CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
75 kdb_sysctl_available, "A", "list of available KDB backends");
77 SYSCTL_PROC(_debug_kdb, OID_AUTO, current, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
78 kdb_sysctl_current, "A", "currently selected KDB backend");
80 SYSCTL_PROC(_debug_kdb, OID_AUTO, enter, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
81 kdb_sysctl_enter, "I", "set to enter the debugger");
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
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);
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.
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 */
110 kdb_sysctl_available(SYSCTL_HANDLER_ARGS)
112 struct kdb_dbbe *be, **iter;
118 SET_FOREACH(iter, kdb_dbbe_set) {
120 if (be->dbbe_active == 0)
121 sz += strlen(be->dbbe_name) + 1;
124 avail = malloc(sz, M_TEMP, M_WAITOK);
128 SET_FOREACH(iter, kdb_dbbe_set) {
130 if (be->dbbe_active == 0) {
131 len = snprintf(p, sz, "%s ", be->dbbe_name);
136 KASSERT(sz >= 0, ("%s", __func__));
137 error = sysctl_handle_string(oidp, avail, 0, req);
143 kdb_sysctl_current(SYSCTL_HANDLER_ARGS)
148 if (kdb_dbbe != NULL) {
149 strncpy(buf, kdb_dbbe->dbbe_name, sizeof(buf));
150 buf[sizeof(buf) - 1] = '\0';
153 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
154 if (error != 0 || req->newptr == NULL)
158 return (kdb_dbbe_select(buf));
162 kdb_sysctl_enter(SYSCTL_HANDLER_ARGS)
166 error = sysctl_wire_old_buffer(req, sizeof(int));
169 error = sysctl_handle_int(oidp, &i, 0, req);
171 if (error != 0 || req->newptr == NULL)
175 kdb_enter("sysctl debug.kdb.enter");
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
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
190 #define KEY_CR 13 /* CR '\r' */
191 #define KEY_TILDE 126 /* ~ */
192 #define KEY_CRTLB 2 /* ^B */
195 kdb_alt_break(int key, int *state)
205 *state = (*state == KEY_TILDE) ? KEY_CRTLB : 0;
208 if (*state == KEY_CRTLB)
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.
229 if (kdb_dbbe != NULL && kdb_dbbe->dbbe_trace != NULL) {
230 printf("KDB: stack backtrace:\n");
231 kdb_dbbe->dbbe_trace();
236 * Set/change the current backend.
240 kdb_dbbe_select(const char *name)
242 struct kdb_dbbe *be, **iter;
244 SET_FOREACH(iter, kdb_dbbe_set) {
246 if (be->dbbe_active == 0 && strcmp(be->dbbe_name, name) == 0) {
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
262 kdb_enter(const char *msg)
265 if (kdb_dbbe != NULL && kdb_active == 0) {
267 printf("KDB: enter: %s\n", msg);
273 * Initialize the kernel debugger interface.
279 struct kdb_dbbe *be, **iter;
285 SET_FOREACH(iter, kdb_dbbe_set) {
287 pri = (be->dbbe_init != NULL) ? be->dbbe_init() : -1;
288 be->dbbe_active = (pri >= 0) ? 0 : -1;
294 if (kdb_dbbe != NULL) {
295 printf("KDB: debugger backends:");
296 SET_FOREACH(iter, kdb_dbbe_set) {
298 if (be->dbbe_active == 0)
299 printf(" %s", be->dbbe_name);
302 printf("KDB: current backend: %s\n",
303 kdb_dbbe->dbbe_name);
312 kdb_jmpbuf(jmp_buf new)
325 if (!kdb_active || kdb_jmpbufp == NULL)
328 longjmp(kdb_jmpbufp, 1);
333 * Thread related support functions.
337 kdb_thr_ctx(struct thread *thr)
343 if (thr == curthread)
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];
356 return ((thr == curthread) ? &kdb_pcb : thr->td_pcb);
358 #endif /* KDB_STOP_NMI */
366 p = LIST_FIRST(&allproc);
368 if (p->p_sflag & PS_INMEM) {
369 thr = FIRST_THREAD_IN_PROC(p);
373 p = LIST_NEXT(p, p_list);
379 kdb_thr_from_pid(pid_t pid)
383 p = LIST_FIRST(&allproc);
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);
393 kdb_thr_lookup(lwpid_t tid)
397 thr = kdb_thr_first();
398 while (thr != NULL && thr->td_tid != tid)
399 thr = kdb_thr_next(thr);
404 kdb_thr_next(struct thread *thr)
409 thr = TAILQ_NEXT(thr, td_plist);
413 p = LIST_NEXT(p, p_list);
414 if (p != NULL && (p->p_sflag & PS_INMEM))
415 thr = FIRST_THREAD_IN_PROC(p);
421 kdb_thr_select(struct thread *thr)
426 kdb_thrctx = kdb_thr_ctx(thr);
431 * Enter the debugger due to a trap.
435 kdb_trap(int type, int code, struct trapframe *tf)
442 if (kdb_dbbe == NULL || kdb_dbbe->dbbe_trap == NULL)
445 /* We reenter the debugger through kdb_reenter(). */
454 if ((did_stop_cpus = kdb_stop_cpus) != 0)
457 if(kdb_stop_cpus_with_nmi)
458 stop_cpus_nmi(PCPU_GET(other_cpus));
460 #endif /* KDB_STOP_NMI */
461 stop_cpus(PCPU_GET(other_cpus));
467 /* Let MD code do its thing first... */
468 kdb_cpu_trap(type, code);
470 makectx(tf, &kdb_pcb);
471 kdb_thr_select(curthread);
473 handled = kdb_dbbe->dbbe_trap(type, code);
477 restart_cpus(stopped_cpus);