xref: /freebsd/sys/kern/subr_kdb.c (revision c0757daf1fdc2e8c7de4177d57de7dc8e6ff577b)
19454b2d8SWarner Losh /*-
2cbc17435SMarcel Moolenaar  * Copyright (c) 2004 The FreeBSD Project
3cbc17435SMarcel Moolenaar  * All rights reserved.
4cbc17435SMarcel Moolenaar  *
5cbc17435SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
6cbc17435SMarcel Moolenaar  * modification, are permitted provided that the following conditions
7cbc17435SMarcel Moolenaar  * are met:
8cbc17435SMarcel Moolenaar  *
9cbc17435SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
10cbc17435SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
11cbc17435SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
12cbc17435SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
13cbc17435SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
14cbc17435SMarcel Moolenaar  *
15cbc17435SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16cbc17435SMarcel Moolenaar  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17cbc17435SMarcel Moolenaar  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18cbc17435SMarcel Moolenaar  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19cbc17435SMarcel Moolenaar  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20cbc17435SMarcel Moolenaar  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21cbc17435SMarcel Moolenaar  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22cbc17435SMarcel Moolenaar  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23cbc17435SMarcel Moolenaar  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24cbc17435SMarcel Moolenaar  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25cbc17435SMarcel Moolenaar  */
26cbc17435SMarcel Moolenaar 
27cbc17435SMarcel Moolenaar #include <sys/cdefs.h>
28cbc17435SMarcel Moolenaar __FBSDID("$FreeBSD$");
29cbc17435SMarcel Moolenaar 
30f0c6706dSPeter Wemm #include "opt_kdb.h"
31088acbb3SAndriy Gapon #include "opt_stack.h"
32f0c6706dSPeter Wemm 
33cbc17435SMarcel Moolenaar #include <sys/param.h>
34cbc17435SMarcel Moolenaar #include <sys/systm.h>
35cbc17435SMarcel Moolenaar #include <sys/kdb.h>
36cbc17435SMarcel Moolenaar #include <sys/kernel.h>
37cbc17435SMarcel Moolenaar #include <sys/malloc.h>
38cbc17435SMarcel Moolenaar #include <sys/pcpu.h>
39cbc17435SMarcel Moolenaar #include <sys/proc.h>
403a5d3671SMatthew D Fleming #include <sys/sbuf.h>
41cbc17435SMarcel Moolenaar #include <sys/smp.h>
42088acbb3SAndriy Gapon #include <sys/stack.h>
43cbc17435SMarcel Moolenaar #include <sys/sysctl.h>
44cbc17435SMarcel Moolenaar 
45cbc17435SMarcel Moolenaar #include <machine/kdb.h>
46cbc17435SMarcel Moolenaar #include <machine/pcb.h>
47cbc17435SMarcel Moolenaar 
4858553b99SJohn Baldwin #ifdef SMP
49fdc9713bSDoug White #include <machine/smp.h>
50fdc9713bSDoug White #endif
51fdc9713bSDoug White 
52cbc17435SMarcel Moolenaar int kdb_active = 0;
537cddab63SWarner Losh static void *kdb_jmpbufp = NULL;
54cbc17435SMarcel Moolenaar struct kdb_dbbe *kdb_dbbe = NULL;
557cddab63SWarner Losh static struct pcb kdb_pcb;
56cbc17435SMarcel Moolenaar struct pcb *kdb_thrctx = NULL;
57cbc17435SMarcel Moolenaar struct thread *kdb_thread = NULL;
58cbc17435SMarcel Moolenaar struct trapframe *kdb_frame = NULL;
59cbc17435SMarcel Moolenaar 
60cbc17435SMarcel Moolenaar KDB_BACKEND(null, NULL, NULL, NULL);
61cbc17435SMarcel Moolenaar SET_DECLARE(kdb_dbbe_set, struct kdb_dbbe);
62cbc17435SMarcel Moolenaar 
63cbc17435SMarcel Moolenaar static int kdb_sysctl_available(SYSCTL_HANDLER_ARGS);
64cbc17435SMarcel Moolenaar static int kdb_sysctl_current(SYSCTL_HANDLER_ARGS);
65cbc17435SMarcel Moolenaar static int kdb_sysctl_enter(SYSCTL_HANDLER_ARGS);
6640c9966aSPeter Wemm static int kdb_sysctl_panic(SYSCTL_HANDLER_ARGS);
6740c9966aSPeter Wemm static int kdb_sysctl_trap(SYSCTL_HANDLER_ARGS);
6842ccd54fSYaroslav Tykhiy static int kdb_sysctl_trap_code(SYSCTL_HANDLER_ARGS);
69cbc17435SMarcel Moolenaar 
70cbc17435SMarcel Moolenaar SYSCTL_NODE(_debug, OID_AUTO, kdb, CTLFLAG_RW, NULL, "KDB nodes");
71cbc17435SMarcel Moolenaar 
727cddab63SWarner Losh SYSCTL_PROC(_debug_kdb, OID_AUTO, available, CTLTYPE_STRING | CTLFLAG_RD, NULL,
737cddab63SWarner Losh     0, kdb_sysctl_available, "A", "list of available KDB backends");
74cbc17435SMarcel Moolenaar 
757cddab63SWarner Losh SYSCTL_PROC(_debug_kdb, OID_AUTO, current, CTLTYPE_STRING | CTLFLAG_RW, NULL,
767cddab63SWarner Losh     0, kdb_sysctl_current, "A", "currently selected KDB backend");
77cbc17435SMarcel Moolenaar 
787cddab63SWarner Losh SYSCTL_PROC(_debug_kdb, OID_AUTO, enter, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
79cbc17435SMarcel Moolenaar     kdb_sysctl_enter, "I", "set to enter the debugger");
80cbc17435SMarcel Moolenaar 
817cddab63SWarner Losh SYSCTL_PROC(_debug_kdb, OID_AUTO, panic, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
8240c9966aSPeter Wemm     kdb_sysctl_panic, "I", "set to panic the kernel");
8340c9966aSPeter Wemm 
847cddab63SWarner Losh SYSCTL_PROC(_debug_kdb, OID_AUTO, trap, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
8542ccd54fSYaroslav Tykhiy     kdb_sysctl_trap, "I", "set to cause a page fault via data access");
8642ccd54fSYaroslav Tykhiy 
877cddab63SWarner Losh SYSCTL_PROC(_debug_kdb, OID_AUTO, trap_code, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
8842ccd54fSYaroslav Tykhiy     kdb_sysctl_trap_code, "I", "set to cause a page fault via code access");
8940c9966aSPeter Wemm 
90d8939d82SRobert Watson /*
913de213ccSRobert Watson  * Flag to indicate to debuggers why the debugger was entered.
923de213ccSRobert Watson  */
933de213ccSRobert Watson const char * volatile kdb_why = KDB_WHY_UNSET;
943de213ccSRobert Watson 
95cbc17435SMarcel Moolenaar static int
96cbc17435SMarcel Moolenaar kdb_sysctl_available(SYSCTL_HANDLER_ARGS)
97cbc17435SMarcel Moolenaar {
983a5d3671SMatthew D Fleming 	struct kdb_dbbe **iter;
993a5d3671SMatthew D Fleming 	struct sbuf sbuf;
100cbc17435SMarcel Moolenaar 	int error;
101cbc17435SMarcel Moolenaar 
1023a5d3671SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 64, req);
103cbc17435SMarcel Moolenaar 	SET_FOREACH(iter, kdb_dbbe_set) {
1043a5d3671SMatthew D Fleming 		if ((*iter)->dbbe_active == 0)
1053a5d3671SMatthew D Fleming 			sbuf_printf(&sbuf, "%s ", (*iter)->dbbe_name);
106cbc17435SMarcel Moolenaar 	}
1073a5d3671SMatthew D Fleming 	error = sbuf_finish(&sbuf);
1083a5d3671SMatthew D Fleming 	sbuf_delete(&sbuf);
109cbc17435SMarcel Moolenaar 	return (error);
110cbc17435SMarcel Moolenaar }
111cbc17435SMarcel Moolenaar 
112cbc17435SMarcel Moolenaar static int
113cbc17435SMarcel Moolenaar kdb_sysctl_current(SYSCTL_HANDLER_ARGS)
114cbc17435SMarcel Moolenaar {
115cbc17435SMarcel Moolenaar 	char buf[16];
116cbc17435SMarcel Moolenaar 	int error;
117cbc17435SMarcel Moolenaar 
1183a5d3671SMatthew D Fleming 	if (kdb_dbbe != NULL)
1193a5d3671SMatthew D Fleming 		strlcpy(buf, kdb_dbbe->dbbe_name, sizeof(buf));
1203a5d3671SMatthew D Fleming 	else
121a8bfba1aSMarcel Moolenaar 		*buf = '\0';
122cbc17435SMarcel Moolenaar 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
123cbc17435SMarcel Moolenaar 	if (error != 0 || req->newptr == NULL)
124cbc17435SMarcel Moolenaar 		return (error);
125cbc17435SMarcel Moolenaar 	if (kdb_active)
126cbc17435SMarcel Moolenaar 		return (EBUSY);
1273bcd2440SMarcel Moolenaar 	return (kdb_dbbe_select(buf));
128cbc17435SMarcel Moolenaar }
129cbc17435SMarcel Moolenaar 
130cbc17435SMarcel Moolenaar static int
131cbc17435SMarcel Moolenaar kdb_sysctl_enter(SYSCTL_HANDLER_ARGS)
132cbc17435SMarcel Moolenaar {
133cbc17435SMarcel Moolenaar 	int error, i;
134cbc17435SMarcel Moolenaar 
135cbc17435SMarcel Moolenaar 	error = sysctl_wire_old_buffer(req, sizeof(int));
136cbc17435SMarcel Moolenaar 	if (error == 0) {
137cbc17435SMarcel Moolenaar 		i = 0;
138cbc17435SMarcel Moolenaar 		error = sysctl_handle_int(oidp, &i, 0, req);
139cbc17435SMarcel Moolenaar 	}
140cbc17435SMarcel Moolenaar 	if (error != 0 || req->newptr == NULL)
141cbc17435SMarcel Moolenaar 		return (error);
142cbc17435SMarcel Moolenaar 	if (kdb_active)
143cbc17435SMarcel Moolenaar 		return (EBUSY);
1443de213ccSRobert Watson 	kdb_enter(KDB_WHY_SYSCTL, "sysctl debug.kdb.enter");
145cbc17435SMarcel Moolenaar 	return (0);
146cbc17435SMarcel Moolenaar }
147cbc17435SMarcel Moolenaar 
14840c9966aSPeter Wemm static int
14940c9966aSPeter Wemm kdb_sysctl_panic(SYSCTL_HANDLER_ARGS)
15040c9966aSPeter Wemm {
15140c9966aSPeter Wemm 	int error, i;
15240c9966aSPeter Wemm 
15340c9966aSPeter Wemm 	error = sysctl_wire_old_buffer(req, sizeof(int));
15440c9966aSPeter Wemm 	if (error == 0) {
15540c9966aSPeter Wemm 		i = 0;
15640c9966aSPeter Wemm 		error = sysctl_handle_int(oidp, &i, 0, req);
15740c9966aSPeter Wemm 	}
15840c9966aSPeter Wemm 	if (error != 0 || req->newptr == NULL)
15940c9966aSPeter Wemm 		return (error);
16040c9966aSPeter Wemm 	panic("kdb_sysctl_panic");
16140c9966aSPeter Wemm 	return (0);
16240c9966aSPeter Wemm }
16340c9966aSPeter Wemm 
16440c9966aSPeter Wemm static int
16540c9966aSPeter Wemm kdb_sysctl_trap(SYSCTL_HANDLER_ARGS)
16640c9966aSPeter Wemm {
16740c9966aSPeter Wemm 	int error, i;
16840c9966aSPeter Wemm 	int *addr = (int *)0x10;
16940c9966aSPeter Wemm 
17040c9966aSPeter Wemm 	error = sysctl_wire_old_buffer(req, sizeof(int));
17140c9966aSPeter Wemm 	if (error == 0) {
17240c9966aSPeter Wemm 		i = 0;
17340c9966aSPeter Wemm 		error = sysctl_handle_int(oidp, &i, 0, req);
17440c9966aSPeter Wemm 	}
17540c9966aSPeter Wemm 	if (error != 0 || req->newptr == NULL)
17640c9966aSPeter Wemm 		return (error);
17740c9966aSPeter Wemm 	return (*addr);
17840c9966aSPeter Wemm }
17940c9966aSPeter Wemm 
18042ccd54fSYaroslav Tykhiy static int
18142ccd54fSYaroslav Tykhiy kdb_sysctl_trap_code(SYSCTL_HANDLER_ARGS)
18242ccd54fSYaroslav Tykhiy {
18342ccd54fSYaroslav Tykhiy 	int error, i;
18442ccd54fSYaroslav Tykhiy 	void (*fp)(u_int, u_int, u_int) = (void *)0xdeadc0de;
18542ccd54fSYaroslav Tykhiy 
18642ccd54fSYaroslav Tykhiy 	error = sysctl_wire_old_buffer(req, sizeof(int));
18742ccd54fSYaroslav Tykhiy 	if (error == 0) {
18842ccd54fSYaroslav Tykhiy 		i = 0;
18942ccd54fSYaroslav Tykhiy 		error = sysctl_handle_int(oidp, &i, 0, req);
19042ccd54fSYaroslav Tykhiy 	}
19142ccd54fSYaroslav Tykhiy 	if (error != 0 || req->newptr == NULL)
19242ccd54fSYaroslav Tykhiy 		return (error);
19342ccd54fSYaroslav Tykhiy 	(*fp)(0x11111111, 0x22222222, 0x33333333);
19442ccd54fSYaroslav Tykhiy 	return (0);
19542ccd54fSYaroslav Tykhiy }
19642ccd54fSYaroslav Tykhiy 
19743d7128cSPeter Wemm void
19843d7128cSPeter Wemm kdb_panic(const char *msg)
19943d7128cSPeter Wemm {
20043d7128cSPeter Wemm #ifdef SMP
201a38f1f26SAttilio Rao 	cpuset_t other_cpus;
202a38f1f26SAttilio Rao 
203a38f1f26SAttilio Rao 	other_cpus = all_cpus;
204a38f1f26SAttilio Rao 	CPU_CLR(PCPU_GET(cpuid), &other_cpus);
205a38f1f26SAttilio Rao 	stop_cpus_hard(other_cpus);
20643d7128cSPeter Wemm #endif
20743d7128cSPeter Wemm 	printf("KDB: panic\n");
2081bdfff22SAndriy Gapon 	panic("%s", msg);
20943d7128cSPeter Wemm }
21043d7128cSPeter Wemm 
21143d7128cSPeter Wemm void
21243d7128cSPeter Wemm kdb_reboot(void)
21343d7128cSPeter Wemm {
21443d7128cSPeter Wemm 
21543d7128cSPeter Wemm 	printf("KDB: reboot requested\n");
21643d7128cSPeter Wemm 	shutdown_nice(0);
21743d7128cSPeter Wemm }
21843d7128cSPeter Wemm 
219cbc17435SMarcel Moolenaar /*
220cbc17435SMarcel Moolenaar  * Solaris implements a new BREAK which is initiated by a character sequence
221cbc17435SMarcel Moolenaar  * CR ~ ^b which is similar to a familiar pattern used on Sun servers by the
222cbc17435SMarcel Moolenaar  * Remote Console.
223cbc17435SMarcel Moolenaar  *
224cbc17435SMarcel Moolenaar  * Note that this function may be called from almost anywhere, with interrupts
225cbc17435SMarcel Moolenaar  * disabled and with unknown locks held, so it must not access data other than
226cbc17435SMarcel Moolenaar  * its arguments.  Its up to the caller to ensure that the state variable is
227cbc17435SMarcel Moolenaar  * consistent.
228cbc17435SMarcel Moolenaar  */
229cbc17435SMarcel Moolenaar 
230cbc17435SMarcel Moolenaar #define	KEY_CR		13	/* CR '\r' */
231cbc17435SMarcel Moolenaar #define	KEY_TILDE	126	/* ~ */
232cbc17435SMarcel Moolenaar #define	KEY_CRTLB	2	/* ^B */
23343d7128cSPeter Wemm #define	KEY_CRTLP	16	/* ^P */
23443d7128cSPeter Wemm #define	KEY_CRTLR	18	/* ^R */
235cbc17435SMarcel Moolenaar 
23649f5aeafSAttilio Rao /* States of th KDB "alternate break sequence" detecting state machine. */
23749f5aeafSAttilio Rao enum {
23849f5aeafSAttilio Rao 	KDB_ALT_BREAK_SEEN_NONE,
23949f5aeafSAttilio Rao 	KDB_ALT_BREAK_SEEN_CR,
24049f5aeafSAttilio Rao 	KDB_ALT_BREAK_SEEN_CR_TILDE,
24149f5aeafSAttilio Rao };
24249f5aeafSAttilio Rao 
243cbc17435SMarcel Moolenaar int
244cbc17435SMarcel Moolenaar kdb_alt_break(int key, int *state)
245cbc17435SMarcel Moolenaar {
246cbc17435SMarcel Moolenaar 	int brk;
247cbc17435SMarcel Moolenaar 
24849f5aeafSAttilio Rao 	/* All states transition to KDB_ALT_BREAK_SEEN_CR on a CR. */
24949f5aeafSAttilio Rao 	if (key == KEY_CR) {
25049f5aeafSAttilio Rao 		*state = KDB_ALT_BREAK_SEEN_CR;
25149f5aeafSAttilio Rao 		return (0);
25249f5aeafSAttilio Rao 	}
25349f5aeafSAttilio Rao 
254cbc17435SMarcel Moolenaar 	brk = 0;
25543d7128cSPeter Wemm 	switch (*state) {
25649f5aeafSAttilio Rao 	case KDB_ALT_BREAK_SEEN_CR:
25749f5aeafSAttilio Rao 		*state = KDB_ALT_BREAK_SEEN_NONE;
25843d7128cSPeter Wemm 		if (key == KEY_TILDE)
25949f5aeafSAttilio Rao 			*state = KDB_ALT_BREAK_SEEN_CR_TILDE;
260cbc17435SMarcel Moolenaar 		break;
26149f5aeafSAttilio Rao 	case KDB_ALT_BREAK_SEEN_CR_TILDE:
26249f5aeafSAttilio Rao 		*state = KDB_ALT_BREAK_SEEN_NONE;
26343d7128cSPeter Wemm 		if (key == KEY_CRTLB)
26443d7128cSPeter Wemm 			brk = KDB_REQ_DEBUGGER;
26543d7128cSPeter Wemm 		else if (key == KEY_CRTLP)
26643d7128cSPeter Wemm 			brk = KDB_REQ_PANIC;
26743d7128cSPeter Wemm 		else if (key == KEY_CRTLR)
26843d7128cSPeter Wemm 			brk = KDB_REQ_REBOOT;
26949f5aeafSAttilio Rao 		break;
27049f5aeafSAttilio Rao 	case KDB_ALT_BREAK_SEEN_NONE:
27149f5aeafSAttilio Rao 	default:
27249f5aeafSAttilio Rao 		*state = KDB_ALT_BREAK_SEEN_NONE;
27349f5aeafSAttilio Rao 		break;
274cbc17435SMarcel Moolenaar 	}
275cbc17435SMarcel Moolenaar 	return (brk);
276cbc17435SMarcel Moolenaar }
277cbc17435SMarcel Moolenaar 
278cbc17435SMarcel Moolenaar /*
279cbc17435SMarcel Moolenaar  * Print a backtrace of the calling thread. The backtrace is generated by
280cbc17435SMarcel Moolenaar  * the selected debugger, provided it supports backtraces. If no debugger
281cbc17435SMarcel Moolenaar  * is selected or the current debugger does not support backtraces, this
282cbc17435SMarcel Moolenaar  * function silently returns.
283cbc17435SMarcel Moolenaar  */
284cbc17435SMarcel Moolenaar 
285cbc17435SMarcel Moolenaar void
2867cddab63SWarner Losh kdb_backtrace(void)
287cbc17435SMarcel Moolenaar {
288cbc17435SMarcel Moolenaar 
289cbc17435SMarcel Moolenaar 	if (kdb_dbbe != NULL && kdb_dbbe->dbbe_trace != NULL) {
290cbc17435SMarcel Moolenaar 		printf("KDB: stack backtrace:\n");
291cbc17435SMarcel Moolenaar 		kdb_dbbe->dbbe_trace();
292cbc17435SMarcel Moolenaar 	}
293088acbb3SAndriy Gapon #ifdef STACK
294088acbb3SAndriy Gapon 	else {
295088acbb3SAndriy Gapon 		struct stack st;
296088acbb3SAndriy Gapon 
297088acbb3SAndriy Gapon 		printf("KDB: stack backtrace:\n");
298088acbb3SAndriy Gapon 		stack_save(&st);
29961548876SAndriy Gapon 		stack_print_ddb(&st);
300088acbb3SAndriy Gapon 	}
301088acbb3SAndriy Gapon #endif
302cbc17435SMarcel Moolenaar }
303cbc17435SMarcel Moolenaar 
304cbc17435SMarcel Moolenaar /*
3053bcd2440SMarcel Moolenaar  * Set/change the current backend.
3063bcd2440SMarcel Moolenaar  */
3073bcd2440SMarcel Moolenaar 
3083bcd2440SMarcel Moolenaar int
3093bcd2440SMarcel Moolenaar kdb_dbbe_select(const char *name)
3103bcd2440SMarcel Moolenaar {
3113bcd2440SMarcel Moolenaar 	struct kdb_dbbe *be, **iter;
3123bcd2440SMarcel Moolenaar 
3133bcd2440SMarcel Moolenaar 	SET_FOREACH(iter, kdb_dbbe_set) {
3143bcd2440SMarcel Moolenaar 		be = *iter;
3153bcd2440SMarcel Moolenaar 		if (be->dbbe_active == 0 && strcmp(be->dbbe_name, name) == 0) {
3163bcd2440SMarcel Moolenaar 			kdb_dbbe = be;
3173bcd2440SMarcel Moolenaar 			return (0);
3183bcd2440SMarcel Moolenaar 		}
3193bcd2440SMarcel Moolenaar 	}
3203bcd2440SMarcel Moolenaar 	return (EINVAL);
3213bcd2440SMarcel Moolenaar }
3223bcd2440SMarcel Moolenaar 
3233bcd2440SMarcel Moolenaar /*
324cbc17435SMarcel Moolenaar  * Enter the currently selected debugger. If a message has been provided,
325cbc17435SMarcel Moolenaar  * it is printed first. If the debugger does not support the enter method,
326cbc17435SMarcel Moolenaar  * it is entered by using breakpoint(), which enters the debugger through
3273de213ccSRobert Watson  * kdb_trap().  The 'why' argument will contain a more mechanically usable
3283de213ccSRobert Watson  * string than 'msg', and is relied upon by DDB scripting to identify the
3293de213ccSRobert Watson  * reason for entering the debugger so that the right script can be run.
330cbc17435SMarcel Moolenaar  */
331cbc17435SMarcel Moolenaar void
3323de213ccSRobert Watson kdb_enter(const char *why, const char *msg)
333cbc17435SMarcel Moolenaar {
334cbc17435SMarcel Moolenaar 
335cbc17435SMarcel Moolenaar 	if (kdb_dbbe != NULL && kdb_active == 0) {
336cbc17435SMarcel Moolenaar 		if (msg != NULL)
337cbc17435SMarcel Moolenaar 			printf("KDB: enter: %s\n", msg);
3383de213ccSRobert Watson 		kdb_why = why;
339cbc17435SMarcel Moolenaar 		breakpoint();
3403de213ccSRobert Watson 		kdb_why = KDB_WHY_UNSET;
341cbc17435SMarcel Moolenaar 	}
342cbc17435SMarcel Moolenaar }
343cbc17435SMarcel Moolenaar 
344cbc17435SMarcel Moolenaar /*
345cbc17435SMarcel Moolenaar  * Initialize the kernel debugger interface.
346cbc17435SMarcel Moolenaar  */
347cbc17435SMarcel Moolenaar 
348cbc17435SMarcel Moolenaar void
3497cddab63SWarner Losh kdb_init(void)
350cbc17435SMarcel Moolenaar {
351cbc17435SMarcel Moolenaar 	struct kdb_dbbe *be, **iter;
352cbc17435SMarcel Moolenaar 	int cur_pri, pri;
353cbc17435SMarcel Moolenaar 
354cbc17435SMarcel Moolenaar 	kdb_active = 0;
355cbc17435SMarcel Moolenaar 	kdb_dbbe = NULL;
356cbc17435SMarcel Moolenaar 	cur_pri = -1;
357cbc17435SMarcel Moolenaar 	SET_FOREACH(iter, kdb_dbbe_set) {
358cbc17435SMarcel Moolenaar 		be = *iter;
359cbc17435SMarcel Moolenaar 		pri = (be->dbbe_init != NULL) ? be->dbbe_init() : -1;
360cbc17435SMarcel Moolenaar 		be->dbbe_active = (pri >= 0) ? 0 : -1;
361cbc17435SMarcel Moolenaar 		if (pri > cur_pri) {
362cbc17435SMarcel Moolenaar 			cur_pri = pri;
363cbc17435SMarcel Moolenaar 			kdb_dbbe = be;
364cbc17435SMarcel Moolenaar 		}
365cbc17435SMarcel Moolenaar 	}
366cbc17435SMarcel Moolenaar 	if (kdb_dbbe != NULL) {
367cbc17435SMarcel Moolenaar 		printf("KDB: debugger backends:");
368cbc17435SMarcel Moolenaar 		SET_FOREACH(iter, kdb_dbbe_set) {
369cbc17435SMarcel Moolenaar 			be = *iter;
370cbc17435SMarcel Moolenaar 			if (be->dbbe_active == 0)
371cbc17435SMarcel Moolenaar 				printf(" %s", be->dbbe_name);
372cbc17435SMarcel Moolenaar 		}
373cbc17435SMarcel Moolenaar 		printf("\n");
374cbc17435SMarcel Moolenaar 		printf("KDB: current backend: %s\n",
375cbc17435SMarcel Moolenaar 		    kdb_dbbe->dbbe_name);
376cbc17435SMarcel Moolenaar 	}
377cbc17435SMarcel Moolenaar }
378cbc17435SMarcel Moolenaar 
379cbc17435SMarcel Moolenaar /*
380cbc17435SMarcel Moolenaar  * Handle contexts.
381cbc17435SMarcel Moolenaar  */
382cbc17435SMarcel Moolenaar 
383cbc17435SMarcel Moolenaar void *
384cbc17435SMarcel Moolenaar kdb_jmpbuf(jmp_buf new)
385cbc17435SMarcel Moolenaar {
386cbc17435SMarcel Moolenaar 	void *old;
387cbc17435SMarcel Moolenaar 
388cbc17435SMarcel Moolenaar 	old = kdb_jmpbufp;
389cbc17435SMarcel Moolenaar 	kdb_jmpbufp = new;
390cbc17435SMarcel Moolenaar 	return (old);
391cbc17435SMarcel Moolenaar }
392cbc17435SMarcel Moolenaar 
393cbc17435SMarcel Moolenaar void
394cbc17435SMarcel Moolenaar kdb_reenter(void)
395cbc17435SMarcel Moolenaar {
396cbc17435SMarcel Moolenaar 
397cbc17435SMarcel Moolenaar 	if (!kdb_active || kdb_jmpbufp == NULL)
398cbc17435SMarcel Moolenaar 		return;
399cbc17435SMarcel Moolenaar 
400cbc17435SMarcel Moolenaar 	longjmp(kdb_jmpbufp, 1);
401cbc17435SMarcel Moolenaar 	/* NOTREACHED */
402cbc17435SMarcel Moolenaar }
403cbc17435SMarcel Moolenaar 
404cbc17435SMarcel Moolenaar /*
405cbc17435SMarcel Moolenaar  * Thread related support functions.
406cbc17435SMarcel Moolenaar  */
407cbc17435SMarcel Moolenaar 
408cbc17435SMarcel Moolenaar struct pcb *
409cbc17435SMarcel Moolenaar kdb_thr_ctx(struct thread *thr)
410fdc9713bSDoug White {
411bfcdefd8SMarcel Moolenaar #if defined(SMP) && defined(KDB_STOPPEDPCB)
412fdc9713bSDoug White 	struct pcpu *pc;
41358553b99SJohn Baldwin #endif
414fdc9713bSDoug White 
415fdc9713bSDoug White 	if (thr == curthread)
4166b1e0d75SJohn Baldwin 		return (&kdb_pcb);
417fdc9713bSDoug White 
418bfcdefd8SMarcel Moolenaar #if defined(SMP) && defined(KDB_STOPPEDPCB)
419d098f930SNathan Whitehorn 	STAILQ_FOREACH(pc, &cpuhead, pc_allcpu)  {
42071a19bdcSAttilio Rao 		if (pc->pc_curthread == thr &&
421a38f1f26SAttilio Rao 		    CPU_ISSET(pc->pc_cpuid, &stopped_cpus))
422bfcdefd8SMarcel Moolenaar 			return (KDB_STOPPEDPCB(pc));
423fdc9713bSDoug White 	}
42458553b99SJohn Baldwin #endif
4256b1e0d75SJohn Baldwin 	return (thr->td_pcb);
426fdc9713bSDoug White }
427cbc17435SMarcel Moolenaar 
428cbc17435SMarcel Moolenaar struct thread *
429cbc17435SMarcel Moolenaar kdb_thr_first(void)
430cbc17435SMarcel Moolenaar {
431cbc17435SMarcel Moolenaar 	struct proc *p;
432cbc17435SMarcel Moolenaar 	struct thread *thr;
433cbc17435SMarcel Moolenaar 
434cbc17435SMarcel Moolenaar 	p = LIST_FIRST(&allproc);
435cbc17435SMarcel Moolenaar 	while (p != NULL) {
436b61ce5b0SJeff Roberson 		if (p->p_flag & P_INMEM) {
437cbc17435SMarcel Moolenaar 			thr = FIRST_THREAD_IN_PROC(p);
438cbc17435SMarcel Moolenaar 			if (thr != NULL)
439cbc17435SMarcel Moolenaar 				return (thr);
440cbc17435SMarcel Moolenaar 		}
441cbc17435SMarcel Moolenaar 		p = LIST_NEXT(p, p_list);
442cbc17435SMarcel Moolenaar 	}
443cbc17435SMarcel Moolenaar 	return (NULL);
444cbc17435SMarcel Moolenaar }
445cbc17435SMarcel Moolenaar 
446cbc17435SMarcel Moolenaar struct thread *
4473d4f3136SMarcel Moolenaar kdb_thr_from_pid(pid_t pid)
4483d4f3136SMarcel Moolenaar {
4493d4f3136SMarcel Moolenaar 	struct proc *p;
4503d4f3136SMarcel Moolenaar 
4513d4f3136SMarcel Moolenaar 	p = LIST_FIRST(&allproc);
4523d4f3136SMarcel Moolenaar 	while (p != NULL) {
453b61ce5b0SJeff Roberson 		if (p->p_flag & P_INMEM && p->p_pid == pid)
4543d4f3136SMarcel Moolenaar 			return (FIRST_THREAD_IN_PROC(p));
4553d4f3136SMarcel Moolenaar 		p = LIST_NEXT(p, p_list);
4563d4f3136SMarcel Moolenaar 	}
4573d4f3136SMarcel Moolenaar 	return (NULL);
4583d4f3136SMarcel Moolenaar }
4593d4f3136SMarcel Moolenaar 
4603d4f3136SMarcel Moolenaar struct thread *
4613d4f3136SMarcel Moolenaar kdb_thr_lookup(lwpid_t tid)
462cbc17435SMarcel Moolenaar {
463cbc17435SMarcel Moolenaar 	struct thread *thr;
464cbc17435SMarcel Moolenaar 
465cbc17435SMarcel Moolenaar 	thr = kdb_thr_first();
466cbc17435SMarcel Moolenaar 	while (thr != NULL && thr->td_tid != tid)
467cbc17435SMarcel Moolenaar 		thr = kdb_thr_next(thr);
468cbc17435SMarcel Moolenaar 	return (thr);
469cbc17435SMarcel Moolenaar }
470cbc17435SMarcel Moolenaar 
471cbc17435SMarcel Moolenaar struct thread *
472cbc17435SMarcel Moolenaar kdb_thr_next(struct thread *thr)
473cbc17435SMarcel Moolenaar {
474cbc17435SMarcel Moolenaar 	struct proc *p;
475cbc17435SMarcel Moolenaar 
476cbc17435SMarcel Moolenaar 	p = thr->td_proc;
477cbc17435SMarcel Moolenaar 	thr = TAILQ_NEXT(thr, td_plist);
478cbc17435SMarcel Moolenaar 	do {
479cbc17435SMarcel Moolenaar 		if (thr != NULL)
480cbc17435SMarcel Moolenaar 			return (thr);
481cbc17435SMarcel Moolenaar 		p = LIST_NEXT(p, p_list);
482b61ce5b0SJeff Roberson 		if (p != NULL && (p->p_flag & P_INMEM))
483cbc17435SMarcel Moolenaar 			thr = FIRST_THREAD_IN_PROC(p);
484cbc17435SMarcel Moolenaar 	} while (p != NULL);
485cbc17435SMarcel Moolenaar 	return (NULL);
486cbc17435SMarcel Moolenaar }
487cbc17435SMarcel Moolenaar 
488cbc17435SMarcel Moolenaar int
489cbc17435SMarcel Moolenaar kdb_thr_select(struct thread *thr)
490cbc17435SMarcel Moolenaar {
491cbc17435SMarcel Moolenaar 	if (thr == NULL)
492cbc17435SMarcel Moolenaar 		return (EINVAL);
493cbc17435SMarcel Moolenaar 	kdb_thread = thr;
494cbc17435SMarcel Moolenaar 	kdb_thrctx = kdb_thr_ctx(thr);
495cbc17435SMarcel Moolenaar 	return (0);
496cbc17435SMarcel Moolenaar }
497cbc17435SMarcel Moolenaar 
498cbc17435SMarcel Moolenaar /*
499cbc17435SMarcel Moolenaar  * Enter the debugger due to a trap.
500cbc17435SMarcel Moolenaar  */
501cbc17435SMarcel Moolenaar 
502cbc17435SMarcel Moolenaar int
503cbc17435SMarcel Moolenaar kdb_trap(int type, int code, struct trapframe *tf)
504cbc17435SMarcel Moolenaar {
505d8939d82SRobert Watson #ifdef SMP
506a38f1f26SAttilio Rao 	cpuset_t other_cpus;
507d8939d82SRobert Watson #endif
508*c0757dafSAttilio Rao 	struct kdb_dbbe *be;
509*c0757dafSAttilio Rao 	register_t intr;
5105991a4f8SMarcel Moolenaar 	int handled;
511cbc17435SMarcel Moolenaar 
5123a5d3671SMatthew D Fleming 	be = kdb_dbbe;
5133a5d3671SMatthew D Fleming 	if (be == NULL || be->dbbe_trap == NULL)
514cbc17435SMarcel Moolenaar 		return (0);
515cbc17435SMarcel Moolenaar 
516cbc17435SMarcel Moolenaar 	/* We reenter the debugger through kdb_reenter(). */
517cbc17435SMarcel Moolenaar 	if (kdb_active)
518cbc17435SMarcel Moolenaar 		return (0);
519cbc17435SMarcel Moolenaar 
5202fae8f5aSMarcel Moolenaar 	intr = intr_disable();
521cbc17435SMarcel Moolenaar 
522cbc17435SMarcel Moolenaar #ifdef SMP
523a38f1f26SAttilio Rao 	other_cpus = all_cpus;
524a38f1f26SAttilio Rao 	CPU_CLR(PCPU_GET(cpuid), &other_cpus);
525a38f1f26SAttilio Rao 	stop_cpus_hard(other_cpus);
526cbc17435SMarcel Moolenaar #endif
527cbc17435SMarcel Moolenaar 
52839eb1d12SMarcel Moolenaar 	kdb_active++;
52939eb1d12SMarcel Moolenaar 
530e6aa7232SMarcel Moolenaar 	kdb_frame = tf;
531e6aa7232SMarcel Moolenaar 
532cbc17435SMarcel Moolenaar 	/* Let MD code do its thing first... */
533cbc17435SMarcel Moolenaar 	kdb_cpu_trap(type, code);
534cbc17435SMarcel Moolenaar 
535ddf41225SMarcel Moolenaar 	makectx(tf, &kdb_pcb);
536ddf41225SMarcel Moolenaar 	kdb_thr_select(curthread);
537ddf41225SMarcel Moolenaar 
5383a5d3671SMatthew D Fleming 	for (;;) {
5393a5d3671SMatthew D Fleming 		handled = be->dbbe_trap(type, code);
5403a5d3671SMatthew D Fleming 		if (be == kdb_dbbe)
5413a5d3671SMatthew D Fleming 			break;
5423a5d3671SMatthew D Fleming 		be = kdb_dbbe;
5433a5d3671SMatthew D Fleming 		if (be == NULL || be->dbbe_trap == NULL)
5443a5d3671SMatthew D Fleming 			break;
5453a5d3671SMatthew D Fleming 		printf("Switching to %s back-end\n", be->dbbe_name);
5463a5d3671SMatthew D Fleming 	}
547cbc17435SMarcel Moolenaar 
54839eb1d12SMarcel Moolenaar 	kdb_active--;
54939eb1d12SMarcel Moolenaar 
550cbc17435SMarcel Moolenaar #ifdef SMP
551cbc17435SMarcel Moolenaar 	restart_cpus(stopped_cpus);
552cbc17435SMarcel Moolenaar #endif
553cbc17435SMarcel Moolenaar 
5542fae8f5aSMarcel Moolenaar 	intr_restore(intr);
555cbc17435SMarcel Moolenaar 
556cbc17435SMarcel Moolenaar 	return (handled);
557cbc17435SMarcel Moolenaar }
558