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 void *kdb_jmpbufp = NULL;
45 struct kdb_dbbe *kdb_dbbe = NULL;
47 struct pcb *kdb_thrctx = NULL;
48 struct thread *kdb_thread = NULL;
49 struct trapframe *kdb_frame = NULL;
51 KDB_BACKEND(null, NULL, NULL, NULL);
52 SET_DECLARE(kdb_dbbe_set, struct kdb_dbbe);
54 static int kdb_sysctl_available(SYSCTL_HANDLER_ARGS);
55 static int kdb_sysctl_current(SYSCTL_HANDLER_ARGS);
56 static int kdb_sysctl_enter(SYSCTL_HANDLER_ARGS);
58 SYSCTL_NODE(_debug, OID_AUTO, kdb, CTLFLAG_RW, NULL, "KDB nodes");
60 SYSCTL_PROC(_debug_kdb, OID_AUTO, available, CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
61 kdb_sysctl_available, "A", "list of available KDB backends");
63 SYSCTL_PROC(_debug_kdb, OID_AUTO, current, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
64 kdb_sysctl_current, "A", "currently selected KDB backend");
66 SYSCTL_PROC(_debug_kdb, OID_AUTO, enter, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
67 kdb_sysctl_enter, "I", "set to enter the debugger");
70 * Flag indicating whether or not to IPI the other CPUs to stop them on
71 * entering the debugger. Sometimes, this will result in a deadlock as
72 * stop_cpus() waits for the other cpus to stop, so we allow it to be
76 static int kdb_stop_cpus = 1;
77 SYSCTL_INT(_debug_kdb, OID_AUTO, stop_cpus, CTLTYPE_INT | CTLFLAG_RW,
78 &kdb_stop_cpus, 0, "");
79 TUNABLE_INT("debug.kdb.stop_cpus", &kdb_stop_cpus);
83 kdb_sysctl_available(SYSCTL_HANDLER_ARGS)
85 struct kdb_dbbe *be, **iter;
91 SET_FOREACH(iter, kdb_dbbe_set) {
93 if (be->dbbe_active == 0)
94 sz += strlen(be->dbbe_name) + 1;
97 avail = malloc(sz, M_TEMP, M_WAITOK);
101 SET_FOREACH(iter, kdb_dbbe_set) {
103 if (be->dbbe_active == 0) {
104 len = snprintf(p, sz, "%s ", be->dbbe_name);
109 KASSERT(sz >= 0, ("%s", __func__));
110 error = sysctl_handle_string(oidp, avail, 0, req);
116 kdb_sysctl_current(SYSCTL_HANDLER_ARGS)
121 if (kdb_dbbe != NULL) {
122 strncpy(buf, kdb_dbbe->dbbe_name, sizeof(buf));
123 buf[sizeof(buf) - 1] = '\0';
126 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
127 if (error != 0 || req->newptr == NULL)
131 return (kdb_dbbe_select(buf));
135 kdb_sysctl_enter(SYSCTL_HANDLER_ARGS)
139 error = sysctl_wire_old_buffer(req, sizeof(int));
142 error = sysctl_handle_int(oidp, &i, 0, req);
144 if (error != 0 || req->newptr == NULL)
148 kdb_enter("sysctl debug.kdb.enter");
153 * Solaris implements a new BREAK which is initiated by a character sequence
154 * CR ~ ^b which is similar to a familiar pattern used on Sun servers by the
157 * Note that this function may be called from almost anywhere, with interrupts
158 * disabled and with unknown locks held, so it must not access data other than
159 * its arguments. Its up to the caller to ensure that the state variable is
163 #define KEY_CR 13 /* CR '\r' */
164 #define KEY_TILDE 126 /* ~ */
165 #define KEY_CRTLB 2 /* ^B */
168 kdb_alt_break(int key, int *state)
178 *state = (*state == KEY_TILDE) ? KEY_CRTLB : 0;
181 if (*state == KEY_CRTLB)
192 * Print a backtrace of the calling thread. The backtrace is generated by
193 * the selected debugger, provided it supports backtraces. If no debugger
194 * is selected or the current debugger does not support backtraces, this
195 * function silently returns.
202 if (kdb_dbbe != NULL && kdb_dbbe->dbbe_trace != NULL) {
203 printf("KDB: stack backtrace:\n");
204 kdb_dbbe->dbbe_trace();
209 * Set/change the current backend.
213 kdb_dbbe_select(const char *name)
215 struct kdb_dbbe *be, **iter;
217 SET_FOREACH(iter, kdb_dbbe_set) {
219 if (be->dbbe_active == 0 && strcmp(be->dbbe_name, name) == 0) {
228 * Enter the currently selected debugger. If a message has been provided,
229 * it is printed first. If the debugger does not support the enter method,
230 * it is entered by using breakpoint(), which enters the debugger through
235 kdb_enter(const char *msg)
238 if (kdb_dbbe != NULL && kdb_active == 0) {
240 printf("KDB: enter: %s\n", msg);
246 * Initialize the kernel debugger interface.
252 struct kdb_dbbe *be, **iter;
258 SET_FOREACH(iter, kdb_dbbe_set) {
260 pri = (be->dbbe_init != NULL) ? be->dbbe_init() : -1;
261 be->dbbe_active = (pri >= 0) ? 0 : -1;
267 if (kdb_dbbe != NULL) {
268 printf("KDB: debugger backends:");
269 SET_FOREACH(iter, kdb_dbbe_set) {
271 if (be->dbbe_active == 0)
272 printf(" %s", be->dbbe_name);
275 printf("KDB: current backend: %s\n",
276 kdb_dbbe->dbbe_name);
285 kdb_jmpbuf(jmp_buf new)
298 if (!kdb_active || kdb_jmpbufp == NULL)
301 longjmp(kdb_jmpbufp, 1);
306 * Thread related support functions.
310 kdb_thr_ctx(struct thread *thr)
312 return ((thr == curthread) ? &kdb_pcb : thr->td_pcb);
321 p = LIST_FIRST(&allproc);
323 if (p->p_sflag & PS_INMEM) {
324 thr = FIRST_THREAD_IN_PROC(p);
328 p = LIST_NEXT(p, p_list);
334 kdb_thr_from_pid(pid_t pid)
338 p = LIST_FIRST(&allproc);
340 if (p->p_sflag & PS_INMEM && p->p_pid == pid)
341 return (FIRST_THREAD_IN_PROC(p));
342 p = LIST_NEXT(p, p_list);
348 kdb_thr_lookup(lwpid_t tid)
352 thr = kdb_thr_first();
353 while (thr != NULL && thr->td_tid != tid)
354 thr = kdb_thr_next(thr);
359 kdb_thr_next(struct thread *thr)
364 thr = TAILQ_NEXT(thr, td_plist);
368 p = LIST_NEXT(p, p_list);
369 if (p != NULL && (p->p_sflag & PS_INMEM))
370 thr = FIRST_THREAD_IN_PROC(p);
376 kdb_thr_select(struct thread *thr)
381 kdb_thrctx = kdb_thr_ctx(thr);
386 * Enter the debugger due to a trap.
390 kdb_trap(int type, int code, struct trapframe *tf)
397 if (kdb_dbbe == NULL || kdb_dbbe->dbbe_trap == NULL)
400 /* We reenter the debugger through kdb_reenter(). */
409 if ((did_stop_cpus = kdb_stop_cpus) != 0)
410 stop_cpus(PCPU_GET(other_cpus));
415 /* Let MD code do its thing first... */
416 kdb_cpu_trap(type, code);
418 makectx(tf, &kdb_pcb);
419 kdb_thr_select(curthread);
421 handled = kdb_dbbe->dbbe_trap(type, code);
425 restart_cpus(stopped_cpus);