xref: /freebsd/sys/kern/subr_kdb.c (revision ff1c63671b7372ef7d52da11d02e082876c22654)
19454b2d8SWarner Losh /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
4cbc17435SMarcel Moolenaar  * Copyright (c) 2004 The FreeBSD Project
5cbc17435SMarcel Moolenaar  * All rights reserved.
6cbc17435SMarcel Moolenaar  *
7cbc17435SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
8cbc17435SMarcel Moolenaar  * modification, are permitted provided that the following conditions
9cbc17435SMarcel Moolenaar  * are met:
10cbc17435SMarcel Moolenaar  *
11cbc17435SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
12cbc17435SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
13cbc17435SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
14cbc17435SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
15cbc17435SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
16cbc17435SMarcel Moolenaar  *
17cbc17435SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18cbc17435SMarcel Moolenaar  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19cbc17435SMarcel Moolenaar  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20cbc17435SMarcel Moolenaar  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21cbc17435SMarcel Moolenaar  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22cbc17435SMarcel Moolenaar  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23cbc17435SMarcel Moolenaar  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24cbc17435SMarcel Moolenaar  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25cbc17435SMarcel Moolenaar  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26cbc17435SMarcel Moolenaar  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27cbc17435SMarcel Moolenaar  */
28cbc17435SMarcel Moolenaar 
29cbc17435SMarcel Moolenaar #include <sys/cdefs.h>
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>
35bf8696b4SAndriy Gapon #include <sys/cons.h>
36cbc17435SMarcel Moolenaar #include <sys/kdb.h>
37cbc17435SMarcel Moolenaar #include <sys/kernel.h>
38cbc17435SMarcel Moolenaar #include <sys/malloc.h>
393f289c3fSJeff Roberson #include <sys/lock.h>
40cbc17435SMarcel Moolenaar #include <sys/pcpu.h>
414819e5aeSStephen J. Kiernan #include <sys/priv.h>
42cbc17435SMarcel Moolenaar #include <sys/proc.h>
433a5d3671SMatthew D Fleming #include <sys/sbuf.h>
44cbc17435SMarcel Moolenaar #include <sys/smp.h>
45088acbb3SAndriy Gapon #include <sys/stack.h>
46cbc17435SMarcel Moolenaar #include <sys/sysctl.h>
479d6ae1e3SColin Percival #include <sys/tslog.h>
48cbc17435SMarcel Moolenaar 
49cbc17435SMarcel Moolenaar #include <machine/kdb.h>
50cbc17435SMarcel Moolenaar #include <machine/pcb.h>
51cbc17435SMarcel Moolenaar 
5258553b99SJohn Baldwin #ifdef SMP
53fdc9713bSDoug White #include <machine/smp.h>
54fdc9713bSDoug White #endif
55fdc9713bSDoug White 
562449b9e5SMitchell Horne #include <security/mac/mac_framework.h>
572449b9e5SMitchell Horne 
58614e1868SMateusz Guzik u_char __read_frequently kdb_active = 0;
597cddab63SWarner Losh static void *kdb_jmpbufp = NULL;
60cbc17435SMarcel Moolenaar struct kdb_dbbe *kdb_dbbe = NULL;
617cddab63SWarner Losh static struct pcb kdb_pcb;
62cbc17435SMarcel Moolenaar struct pcb *kdb_thrctx = NULL;
63cbc17435SMarcel Moolenaar struct thread *kdb_thread = NULL;
64cbc17435SMarcel Moolenaar struct trapframe *kdb_frame = NULL;
65cbc17435SMarcel Moolenaar 
664cf75455SRobert Watson #ifdef BREAK_TO_DEBUGGER
674cf75455SRobert Watson #define	KDB_BREAK_TO_DEBUGGER	1
684cf75455SRobert Watson #else
694cf75455SRobert Watson #define	KDB_BREAK_TO_DEBUGGER	0
704cf75455SRobert Watson #endif
714cf75455SRobert Watson 
724cf75455SRobert Watson #ifdef ALT_BREAK_TO_DEBUGGER
734cf75455SRobert Watson #define	KDB_ALT_BREAK_TO_DEBUGGER	1
744cf75455SRobert Watson #else
754cf75455SRobert Watson #define	KDB_ALT_BREAK_TO_DEBUGGER	0
764cf75455SRobert Watson #endif
774cf75455SRobert Watson 
784cf75455SRobert Watson static int	kdb_break_to_debugger = KDB_BREAK_TO_DEBUGGER;
794cf75455SRobert Watson static int	kdb_alt_break_to_debugger = KDB_ALT_BREAK_TO_DEBUGGER;
80cab10561SMark Johnston static int	kdb_enter_securelevel = 0;
814cf75455SRobert Watson 
820cc457b0SJohn Baldwin KDB_BACKEND(null, NULL, NULL, NULL, NULL);
83cbc17435SMarcel Moolenaar 
84cbc17435SMarcel Moolenaar static int kdb_sysctl_available(SYSCTL_HANDLER_ARGS);
85cbc17435SMarcel Moolenaar static int kdb_sysctl_current(SYSCTL_HANDLER_ARGS);
86cbc17435SMarcel Moolenaar static int kdb_sysctl_enter(SYSCTL_HANDLER_ARGS);
8740c9966aSPeter Wemm static int kdb_sysctl_panic(SYSCTL_HANDLER_ARGS);
8893644056SWarner Losh static int kdb_sysctl_panic_str(SYSCTL_HANDLER_ARGS);
8940c9966aSPeter Wemm static int kdb_sysctl_trap(SYSCTL_HANDLER_ARGS);
9042ccd54fSYaroslav Tykhiy static int kdb_sysctl_trap_code(SYSCTL_HANDLER_ARGS);
91fd94177cSKonstantin Belousov static int kdb_sysctl_stack_overflow(SYSCTL_HANDLER_ARGS);
92cbc17435SMarcel Moolenaar 
937029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, kdb, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
947029da5cSPawel Biernacki     "KDB nodes");
95cbc17435SMarcel Moolenaar 
967029da5cSPawel Biernacki SYSCTL_PROC(_debug_kdb, OID_AUTO, available,
977029da5cSPawel Biernacki     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
987029da5cSPawel Biernacki     kdb_sysctl_available, "A",
997029da5cSPawel Biernacki     "list of available KDB backends");
100cbc17435SMarcel Moolenaar 
1017029da5cSPawel Biernacki SYSCTL_PROC(_debug_kdb, OID_AUTO, current,
1027029da5cSPawel Biernacki     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
1037029da5cSPawel Biernacki     kdb_sysctl_current, "A",
1047029da5cSPawel Biernacki     "currently selected KDB backend");
105cbc17435SMarcel Moolenaar 
106ef522f95SDavid E. O'Brien SYSCTL_PROC(_debug_kdb, OID_AUTO, enter,
107cab10561SMark Johnston     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
1087029da5cSPawel Biernacki     kdb_sysctl_enter, "I",
1097029da5cSPawel Biernacki     "set to enter the debugger");
110cbc17435SMarcel Moolenaar 
111ef522f95SDavid E. O'Brien SYSCTL_PROC(_debug_kdb, OID_AUTO, panic,
1127029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE, NULL, 0,
1137029da5cSPawel Biernacki     kdb_sysctl_panic, "I",
1147029da5cSPawel Biernacki     "set to panic the kernel");
11540c9966aSPeter Wemm 
11693644056SWarner Losh SYSCTL_PROC(_debug_kdb, OID_AUTO, panic_str,
11793644056SWarner Losh     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE, NULL, 0,
11893644056SWarner Losh     kdb_sysctl_panic_str, "A",
11940e6e2c2SWarner Losh     "trigger a kernel panic, using the provided string as the panic message");
12093644056SWarner Losh 
121ef522f95SDavid E. O'Brien SYSCTL_PROC(_debug_kdb, OID_AUTO, trap,
1227029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE, NULL, 0,
1237029da5cSPawel Biernacki     kdb_sysctl_trap, "I",
1247029da5cSPawel Biernacki     "set to cause a page fault via data access");
12542ccd54fSYaroslav Tykhiy 
126ef522f95SDavid E. O'Brien SYSCTL_PROC(_debug_kdb, OID_AUTO, trap_code,
1277029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE, NULL, 0,
1287029da5cSPawel Biernacki     kdb_sysctl_trap_code, "I",
1297029da5cSPawel Biernacki     "set to cause a page fault via code access");
13040c9966aSPeter Wemm 
131fd94177cSKonstantin Belousov SYSCTL_PROC(_debug_kdb, OID_AUTO, stack_overflow,
1327029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE, NULL, 0,
1337029da5cSPawel Biernacki     kdb_sysctl_stack_overflow, "I",
1347029da5cSPawel Biernacki     "set to cause a stack overflow");
135fd94177cSKonstantin Belousov 
136ef522f95SDavid E. O'Brien SYSCTL_INT(_debug_kdb, OID_AUTO, break_to_debugger,
137cab10561SMark Johnston     CTLFLAG_RWTUN,
138ef522f95SDavid E. O'Brien     &kdb_break_to_debugger, 0, "Enable break to debugger");
1394cf75455SRobert Watson 
140ef522f95SDavid E. O'Brien SYSCTL_INT(_debug_kdb, OID_AUTO, alt_break_to_debugger,
141cab10561SMark Johnston     CTLFLAG_RWTUN,
142ef522f95SDavid E. O'Brien     &kdb_alt_break_to_debugger, 0, "Enable alternative break to debugger");
1434cf75455SRobert Watson 
144cab10561SMark Johnston SYSCTL_INT(_debug_kdb, OID_AUTO, enter_securelevel,
145cab10561SMark Johnston     CTLFLAG_RWTUN | CTLFLAG_SECURE,
146cab10561SMark Johnston     &kdb_enter_securelevel, 0,
147cab10561SMark Johnston     "Maximum securelevel to enter a KDB backend");
148cab10561SMark Johnston 
149d8939d82SRobert Watson /*
1503de213ccSRobert Watson  * Flag to indicate to debuggers why the debugger was entered.
1513de213ccSRobert Watson  */
1523de213ccSRobert Watson const char * volatile kdb_why = KDB_WHY_UNSET;
1533de213ccSRobert Watson 
154cbc17435SMarcel Moolenaar static int
155cbc17435SMarcel Moolenaar kdb_sysctl_available(SYSCTL_HANDLER_ARGS)
156cbc17435SMarcel Moolenaar {
1573a5d3671SMatthew D Fleming 	struct kdb_dbbe **iter;
1583a5d3671SMatthew D Fleming 	struct sbuf sbuf;
159cbc17435SMarcel Moolenaar 	int error;
160cbc17435SMarcel Moolenaar 
1613a5d3671SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 64, req);
162cbc17435SMarcel Moolenaar 	SET_FOREACH(iter, kdb_dbbe_set) {
1633a5d3671SMatthew D Fleming 		if ((*iter)->dbbe_active == 0)
1643a5d3671SMatthew D Fleming 			sbuf_printf(&sbuf, "%s ", (*iter)->dbbe_name);
165cbc17435SMarcel Moolenaar 	}
1663a5d3671SMatthew D Fleming 	error = sbuf_finish(&sbuf);
1673a5d3671SMatthew D Fleming 	sbuf_delete(&sbuf);
168cbc17435SMarcel Moolenaar 	return (error);
169cbc17435SMarcel Moolenaar }
170cbc17435SMarcel Moolenaar 
171cbc17435SMarcel Moolenaar static int
172cbc17435SMarcel Moolenaar kdb_sysctl_current(SYSCTL_HANDLER_ARGS)
173cbc17435SMarcel Moolenaar {
174cbc17435SMarcel Moolenaar 	char buf[16];
175cbc17435SMarcel Moolenaar 	int error;
176cbc17435SMarcel Moolenaar 
1773a5d3671SMatthew D Fleming 	if (kdb_dbbe != NULL)
1783a5d3671SMatthew D Fleming 		strlcpy(buf, kdb_dbbe->dbbe_name, sizeof(buf));
1793a5d3671SMatthew D Fleming 	else
180a8bfba1aSMarcel Moolenaar 		*buf = '\0';
181cbc17435SMarcel Moolenaar 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
182cbc17435SMarcel Moolenaar 	if (error != 0 || req->newptr == NULL)
183cbc17435SMarcel Moolenaar 		return (error);
184cbc17435SMarcel Moolenaar 	if (kdb_active)
185cbc17435SMarcel Moolenaar 		return (EBUSY);
1863bcd2440SMarcel Moolenaar 	return (kdb_dbbe_select(buf));
187cbc17435SMarcel Moolenaar }
188cbc17435SMarcel Moolenaar 
189cbc17435SMarcel Moolenaar static int
190cbc17435SMarcel Moolenaar kdb_sysctl_enter(SYSCTL_HANDLER_ARGS)
191cbc17435SMarcel Moolenaar {
192cbc17435SMarcel Moolenaar 	int error, i;
193cbc17435SMarcel Moolenaar 
194cbc17435SMarcel Moolenaar 	error = sysctl_wire_old_buffer(req, sizeof(int));
195cbc17435SMarcel Moolenaar 	if (error == 0) {
196cbc17435SMarcel Moolenaar 		i = 0;
197cbc17435SMarcel Moolenaar 		error = sysctl_handle_int(oidp, &i, 0, req);
198cbc17435SMarcel Moolenaar 	}
199cbc17435SMarcel Moolenaar 	if (error != 0 || req->newptr == NULL)
200cbc17435SMarcel Moolenaar 		return (error);
201cbc17435SMarcel Moolenaar 	if (kdb_active)
202cbc17435SMarcel Moolenaar 		return (EBUSY);
2033de213ccSRobert Watson 	kdb_enter(KDB_WHY_SYSCTL, "sysctl debug.kdb.enter");
204cbc17435SMarcel Moolenaar 	return (0);
205cbc17435SMarcel Moolenaar }
206cbc17435SMarcel Moolenaar 
20740c9966aSPeter Wemm static int
20840c9966aSPeter Wemm kdb_sysctl_panic(SYSCTL_HANDLER_ARGS)
20940c9966aSPeter Wemm {
21040c9966aSPeter Wemm 	int error, i;
21140c9966aSPeter Wemm 
21240c9966aSPeter Wemm 	error = sysctl_wire_old_buffer(req, sizeof(int));
21340c9966aSPeter Wemm 	if (error == 0) {
21440c9966aSPeter Wemm 		i = 0;
21540c9966aSPeter Wemm 		error = sysctl_handle_int(oidp, &i, 0, req);
21640c9966aSPeter Wemm 	}
21740c9966aSPeter Wemm 	if (error != 0 || req->newptr == NULL)
21840c9966aSPeter Wemm 		return (error);
21940c9966aSPeter Wemm 	panic("kdb_sysctl_panic");
22040c9966aSPeter Wemm 	return (0);
22140c9966aSPeter Wemm }
22240c9966aSPeter Wemm 
22340c9966aSPeter Wemm static int
22493644056SWarner Losh kdb_sysctl_panic_str(SYSCTL_HANDLER_ARGS)
22593644056SWarner Losh {
22693644056SWarner Losh 	int error;
22793644056SWarner Losh 	static char buf[256]; /* static buffer to limit mallocs when panicing */
22893644056SWarner Losh 
22993644056SWarner Losh 	*buf = '\0';
23093644056SWarner Losh 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
23193644056SWarner Losh 	if (error != 0 || req->newptr == NULL)
23293644056SWarner Losh 		return (error);
23393644056SWarner Losh 	panic("kdb_sysctl_panic: %s", buf);
23493644056SWarner Losh 	return (0);
23593644056SWarner Losh }
23693644056SWarner Losh 
23793644056SWarner Losh static int
23840c9966aSPeter Wemm kdb_sysctl_trap(SYSCTL_HANDLER_ARGS)
23940c9966aSPeter Wemm {
24040c9966aSPeter Wemm 	int error, i;
241*ff1c6367SRyan Libby 	int *volatile addr = (int *)0x10;
24240c9966aSPeter Wemm 
24340c9966aSPeter Wemm 	error = sysctl_wire_old_buffer(req, sizeof(int));
24440c9966aSPeter Wemm 	if (error == 0) {
24540c9966aSPeter Wemm 		i = 0;
24640c9966aSPeter Wemm 		error = sysctl_handle_int(oidp, &i, 0, req);
24740c9966aSPeter Wemm 	}
24840c9966aSPeter Wemm 	if (error != 0 || req->newptr == NULL)
24940c9966aSPeter Wemm 		return (error);
25040c9966aSPeter Wemm 	return (*addr);
25140c9966aSPeter Wemm }
25240c9966aSPeter Wemm 
25342ccd54fSYaroslav Tykhiy static int
25442ccd54fSYaroslav Tykhiy kdb_sysctl_trap_code(SYSCTL_HANDLER_ARGS)
25542ccd54fSYaroslav Tykhiy {
25642ccd54fSYaroslav Tykhiy 	int error, i;
25742ccd54fSYaroslav Tykhiy 	void (*fp)(u_int, u_int, u_int) = (void *)0xdeadc0de;
25842ccd54fSYaroslav Tykhiy 
25942ccd54fSYaroslav Tykhiy 	error = sysctl_wire_old_buffer(req, sizeof(int));
26042ccd54fSYaroslav Tykhiy 	if (error == 0) {
26142ccd54fSYaroslav Tykhiy 		i = 0;
26242ccd54fSYaroslav Tykhiy 		error = sysctl_handle_int(oidp, &i, 0, req);
26342ccd54fSYaroslav Tykhiy 	}
26442ccd54fSYaroslav Tykhiy 	if (error != 0 || req->newptr == NULL)
26542ccd54fSYaroslav Tykhiy 		return (error);
26642ccd54fSYaroslav Tykhiy 	(*fp)(0x11111111, 0x22222222, 0x33333333);
26742ccd54fSYaroslav Tykhiy 	return (0);
26842ccd54fSYaroslav Tykhiy }
26942ccd54fSYaroslav Tykhiy 
270fd94177cSKonstantin Belousov static void kdb_stack_overflow(volatile int *x)  __noinline;
271fd94177cSKonstantin Belousov static void
272fd94177cSKonstantin Belousov kdb_stack_overflow(volatile int *x)
273fd94177cSKonstantin Belousov {
274fd94177cSKonstantin Belousov 
275fd94177cSKonstantin Belousov 	if (*x > 10000000)
276fd94177cSKonstantin Belousov 		return;
277fd94177cSKonstantin Belousov 	kdb_stack_overflow(x);
278fd94177cSKonstantin Belousov 	*x += PCPU_GET(cpuid) / 1000000;
279fd94177cSKonstantin Belousov }
280fd94177cSKonstantin Belousov 
281fd94177cSKonstantin Belousov static int
282fd94177cSKonstantin Belousov kdb_sysctl_stack_overflow(SYSCTL_HANDLER_ARGS)
283fd94177cSKonstantin Belousov {
284fd94177cSKonstantin Belousov 	int error, i;
285fd94177cSKonstantin Belousov 	volatile int x;
286fd94177cSKonstantin Belousov 
287fd94177cSKonstantin Belousov 	error = sysctl_wire_old_buffer(req, sizeof(int));
288fd94177cSKonstantin Belousov 	if (error == 0) {
289fd94177cSKonstantin Belousov 		i = 0;
290fd94177cSKonstantin Belousov 		error = sysctl_handle_int(oidp, &i, 0, req);
291fd94177cSKonstantin Belousov 	}
292fd94177cSKonstantin Belousov 	if (error != 0 || req->newptr == NULL)
293fd94177cSKonstantin Belousov 		return (error);
294fd94177cSKonstantin Belousov 	x = 0;
295fd94177cSKonstantin Belousov 	kdb_stack_overflow(&x);
296fd94177cSKonstantin Belousov 	return (0);
297fd94177cSKonstantin Belousov }
298fd94177cSKonstantin Belousov 
29943d7128cSPeter Wemm void
30043d7128cSPeter Wemm kdb_panic(const char *msg)
30143d7128cSPeter Wemm {
302a38f1f26SAttilio Rao 
3031ea833a5STom Jones 	kdb_why = KDB_WHY_PANIC;
30443d7128cSPeter Wemm 	printf("KDB: panic\n");
3051bdfff22SAndriy Gapon 	panic("%s", msg);
30643d7128cSPeter Wemm }
30743d7128cSPeter Wemm 
30843d7128cSPeter Wemm void
30943d7128cSPeter Wemm kdb_reboot(void)
31043d7128cSPeter Wemm {
31143d7128cSPeter Wemm 
3121ea833a5STom Jones 	kdb_why = KDB_WHY_REBOOT;
31343d7128cSPeter Wemm 	printf("KDB: reboot requested\n");
31443d7128cSPeter Wemm 	shutdown_nice(0);
31543d7128cSPeter Wemm }
31643d7128cSPeter Wemm 
317cbc17435SMarcel Moolenaar /*
318cbc17435SMarcel Moolenaar  * Solaris implements a new BREAK which is initiated by a character sequence
319cbc17435SMarcel Moolenaar  * CR ~ ^b which is similar to a familiar pattern used on Sun servers by the
320cbc17435SMarcel Moolenaar  * Remote Console.
321cbc17435SMarcel Moolenaar  *
322cbc17435SMarcel Moolenaar  * Note that this function may be called from almost anywhere, with interrupts
323cbc17435SMarcel Moolenaar  * disabled and with unknown locks held, so it must not access data other than
324cbc17435SMarcel Moolenaar  * its arguments.  Its up to the caller to ensure that the state variable is
325cbc17435SMarcel Moolenaar  * consistent.
326cbc17435SMarcel Moolenaar  */
327cbc17435SMarcel Moolenaar #define	KEY_CR		13	/* CR '\r' */
328cbc17435SMarcel Moolenaar #define	KEY_TILDE	126	/* ~ */
329cbc17435SMarcel Moolenaar #define	KEY_CRTLB	2	/* ^B */
33043d7128cSPeter Wemm #define	KEY_CRTLP	16	/* ^P */
33143d7128cSPeter Wemm #define	KEY_CRTLR	18	/* ^R */
332cbc17435SMarcel Moolenaar 
33349f5aeafSAttilio Rao /* States of th KDB "alternate break sequence" detecting state machine. */
33449f5aeafSAttilio Rao enum {
33549f5aeafSAttilio Rao 	KDB_ALT_BREAK_SEEN_NONE,
33649f5aeafSAttilio Rao 	KDB_ALT_BREAK_SEEN_CR,
33749f5aeafSAttilio Rao 	KDB_ALT_BREAK_SEEN_CR_TILDE,
33849f5aeafSAttilio Rao };
33949f5aeafSAttilio Rao 
340cbc17435SMarcel Moolenaar int
3414cf75455SRobert Watson kdb_break(void)
3424cf75455SRobert Watson {
3434cf75455SRobert Watson 
3444cf75455SRobert Watson 	if (!kdb_break_to_debugger)
3454cf75455SRobert Watson 		return (0);
3464cf75455SRobert Watson 	kdb_enter(KDB_WHY_BREAK, "Break to debugger");
3474cf75455SRobert Watson 	return (KDB_REQ_DEBUGGER);
3484cf75455SRobert Watson }
3494cf75455SRobert Watson 
3504cf75455SRobert Watson static int
3514cf75455SRobert Watson kdb_alt_break_state(int key, int *state)
352cbc17435SMarcel Moolenaar {
353cbc17435SMarcel Moolenaar 	int brk;
354cbc17435SMarcel Moolenaar 
35549f5aeafSAttilio Rao 	/* All states transition to KDB_ALT_BREAK_SEEN_CR on a CR. */
35649f5aeafSAttilio Rao 	if (key == KEY_CR) {
35749f5aeafSAttilio Rao 		*state = KDB_ALT_BREAK_SEEN_CR;
35849f5aeafSAttilio Rao 		return (0);
35949f5aeafSAttilio Rao 	}
36049f5aeafSAttilio Rao 
361cbc17435SMarcel Moolenaar 	brk = 0;
36243d7128cSPeter Wemm 	switch (*state) {
36349f5aeafSAttilio Rao 	case KDB_ALT_BREAK_SEEN_CR:
36449f5aeafSAttilio Rao 		*state = KDB_ALT_BREAK_SEEN_NONE;
36543d7128cSPeter Wemm 		if (key == KEY_TILDE)
36649f5aeafSAttilio Rao 			*state = KDB_ALT_BREAK_SEEN_CR_TILDE;
367cbc17435SMarcel Moolenaar 		break;
36849f5aeafSAttilio Rao 	case KDB_ALT_BREAK_SEEN_CR_TILDE:
36949f5aeafSAttilio Rao 		*state = KDB_ALT_BREAK_SEEN_NONE;
37043d7128cSPeter Wemm 		if (key == KEY_CRTLB)
37143d7128cSPeter Wemm 			brk = KDB_REQ_DEBUGGER;
37243d7128cSPeter Wemm 		else if (key == KEY_CRTLP)
37343d7128cSPeter Wemm 			brk = KDB_REQ_PANIC;
37443d7128cSPeter Wemm 		else if (key == KEY_CRTLR)
37543d7128cSPeter Wemm 			brk = KDB_REQ_REBOOT;
37649f5aeafSAttilio Rao 		break;
37749f5aeafSAttilio Rao 	case KDB_ALT_BREAK_SEEN_NONE:
37849f5aeafSAttilio Rao 	default:
37949f5aeafSAttilio Rao 		*state = KDB_ALT_BREAK_SEEN_NONE;
38049f5aeafSAttilio Rao 		break;
381cbc17435SMarcel Moolenaar 	}
382cbc17435SMarcel Moolenaar 	return (brk);
383cbc17435SMarcel Moolenaar }
384cbc17435SMarcel Moolenaar 
3854cf75455SRobert Watson static int
3864cf75455SRobert Watson kdb_alt_break_internal(int key, int *state, int force_gdb)
3874cf75455SRobert Watson {
3884cf75455SRobert Watson 	int brk;
3894cf75455SRobert Watson 
3904cf75455SRobert Watson 	if (!kdb_alt_break_to_debugger)
3914cf75455SRobert Watson 		return (0);
3924cf75455SRobert Watson 	brk = kdb_alt_break_state(key, state);
3934cf75455SRobert Watson 	switch (brk) {
3944cf75455SRobert Watson 	case KDB_REQ_DEBUGGER:
3954cf75455SRobert Watson 		if (force_gdb)
3964cf75455SRobert Watson 			kdb_dbbe_select("gdb");
3974cf75455SRobert Watson 		kdb_enter(KDB_WHY_BREAK, "Break to debugger");
3984cf75455SRobert Watson 		break;
3994cf75455SRobert Watson 
4004cf75455SRobert Watson 	case KDB_REQ_PANIC:
4014cf75455SRobert Watson 		if (force_gdb)
4024cf75455SRobert Watson 			kdb_dbbe_select("gdb");
4034cf75455SRobert Watson 		kdb_panic("Panic sequence on console");
4044cf75455SRobert Watson 		break;
4054cf75455SRobert Watson 
4064cf75455SRobert Watson 	case KDB_REQ_REBOOT:
4074cf75455SRobert Watson 		kdb_reboot();
4084cf75455SRobert Watson 		break;
4094cf75455SRobert Watson 	}
4104cf75455SRobert Watson 	return (0);
4114cf75455SRobert Watson }
4124cf75455SRobert Watson 
4134cf75455SRobert Watson int
4144cf75455SRobert Watson kdb_alt_break(int key, int *state)
4154cf75455SRobert Watson {
4164cf75455SRobert Watson 
4174cf75455SRobert Watson 	return (kdb_alt_break_internal(key, state, 0));
4184cf75455SRobert Watson }
4194cf75455SRobert Watson 
4204cf75455SRobert Watson /*
4214cf75455SRobert Watson  * This variation on kdb_alt_break() is used only by dcons, which has its own
4224cf75455SRobert Watson  * configuration flag to force GDB use regardless of the global KDB
4234cf75455SRobert Watson  * configuration.
4244cf75455SRobert Watson  */
4254cf75455SRobert Watson int
4264cf75455SRobert Watson kdb_alt_break_gdb(int key, int *state)
4274cf75455SRobert Watson {
4284cf75455SRobert Watson 
4294cf75455SRobert Watson 	return (kdb_alt_break_internal(key, state, 1));
4304cf75455SRobert Watson }
4314cf75455SRobert Watson 
432cbc17435SMarcel Moolenaar /*
433cbc17435SMarcel Moolenaar  * Print a backtrace of the calling thread. The backtrace is generated by
434cbc17435SMarcel Moolenaar  * the selected debugger, provided it supports backtraces. If no debugger
435cbc17435SMarcel Moolenaar  * is selected or the current debugger does not support backtraces, this
436cbc17435SMarcel Moolenaar  * function silently returns.
437cbc17435SMarcel Moolenaar  */
438cbc17435SMarcel Moolenaar void
4397cddab63SWarner Losh kdb_backtrace(void)
440cbc17435SMarcel Moolenaar {
441cbc17435SMarcel Moolenaar 
442cbc17435SMarcel Moolenaar 	if (kdb_dbbe != NULL && kdb_dbbe->dbbe_trace != NULL) {
443cbc17435SMarcel Moolenaar 		printf("KDB: stack backtrace:\n");
444cbc17435SMarcel Moolenaar 		kdb_dbbe->dbbe_trace();
445cbc17435SMarcel Moolenaar 	}
446088acbb3SAndriy Gapon #ifdef STACK
447088acbb3SAndriy Gapon 	else {
448088acbb3SAndriy Gapon 		struct stack st;
449088acbb3SAndriy Gapon 
450088acbb3SAndriy Gapon 		printf("KDB: stack backtrace:\n");
451088acbb3SAndriy Gapon 		stack_save(&st);
45261548876SAndriy Gapon 		stack_print_ddb(&st);
453088acbb3SAndriy Gapon 	}
454088acbb3SAndriy Gapon #endif
455cbc17435SMarcel Moolenaar }
456cbc17435SMarcel Moolenaar 
457cbc17435SMarcel Moolenaar /*
4580cc457b0SJohn Baldwin  * Similar to kdb_backtrace() except that it prints a backtrace of an
4590cc457b0SJohn Baldwin  * arbitrary thread rather than the calling thread.
4600cc457b0SJohn Baldwin  */
4610cc457b0SJohn Baldwin void
4620cc457b0SJohn Baldwin kdb_backtrace_thread(struct thread *td)
4630cc457b0SJohn Baldwin {
4640cc457b0SJohn Baldwin 
4650cc457b0SJohn Baldwin 	if (kdb_dbbe != NULL && kdb_dbbe->dbbe_trace_thread != NULL) {
4660cc457b0SJohn Baldwin 		printf("KDB: stack backtrace of thread %d:\n", td->td_tid);
4670cc457b0SJohn Baldwin 		kdb_dbbe->dbbe_trace_thread(td);
4680cc457b0SJohn Baldwin 	}
4690cc457b0SJohn Baldwin #ifdef STACK
4700cc457b0SJohn Baldwin 	else {
4710cc457b0SJohn Baldwin 		struct stack st;
4720cc457b0SJohn Baldwin 
4730cc457b0SJohn Baldwin 		printf("KDB: stack backtrace of thread %d:\n", td->td_tid);
4741c29da02SMark Johnston 		if (stack_save_td(&st, td) == 0)
4750cc457b0SJohn Baldwin 			stack_print_ddb(&st);
4760cc457b0SJohn Baldwin 	}
4770cc457b0SJohn Baldwin #endif
4780cc457b0SJohn Baldwin }
4790cc457b0SJohn Baldwin 
4800cc457b0SJohn Baldwin /*
4813bcd2440SMarcel Moolenaar  * Set/change the current backend.
4823bcd2440SMarcel Moolenaar  */
4833bcd2440SMarcel Moolenaar int
4843bcd2440SMarcel Moolenaar kdb_dbbe_select(const char *name)
4853bcd2440SMarcel Moolenaar {
4863bcd2440SMarcel Moolenaar 	struct kdb_dbbe *be, **iter;
4874819e5aeSStephen J. Kiernan 	int error;
4884819e5aeSStephen J. Kiernan 
4894819e5aeSStephen J. Kiernan 	error = priv_check(curthread, PRIV_KDB_SET_BACKEND);
4904819e5aeSStephen J. Kiernan 	if (error)
4914819e5aeSStephen J. Kiernan 		return (error);
4923bcd2440SMarcel Moolenaar 
4933bcd2440SMarcel Moolenaar 	SET_FOREACH(iter, kdb_dbbe_set) {
4943bcd2440SMarcel Moolenaar 		be = *iter;
4953bcd2440SMarcel Moolenaar 		if (be->dbbe_active == 0 && strcmp(be->dbbe_name, name) == 0) {
4963bcd2440SMarcel Moolenaar 			kdb_dbbe = be;
4973bcd2440SMarcel Moolenaar 			return (0);
4983bcd2440SMarcel Moolenaar 		}
4993bcd2440SMarcel Moolenaar 	}
5003bcd2440SMarcel Moolenaar 	return (EINVAL);
5013bcd2440SMarcel Moolenaar }
5023bcd2440SMarcel Moolenaar 
503cab10561SMark Johnston static bool
504bd16c274SMark Johnston kdb_backend_permitted(struct kdb_dbbe *be, struct thread *td)
505cab10561SMark Johnston {
506bd16c274SMark Johnston 	struct ucred *cred;
507cab10561SMark Johnston 	int error;
508cab10561SMark Johnston 
509bd16c274SMark Johnston 	cred = td->td_ucred;
510bd16c274SMark Johnston 	if (cred == NULL) {
511bd16c274SMark Johnston 		KASSERT(td == &thread0 && cold,
512bd16c274SMark Johnston 		    ("%s: missing cred for %p", __func__, td));
513bd16c274SMark Johnston 		error = 0;
514bd16c274SMark Johnston 	} else {
515cab10561SMark Johnston 		error = securelevel_gt(cred, kdb_enter_securelevel);
516bd16c274SMark Johnston 	}
517cab10561SMark Johnston #ifdef MAC
518cab10561SMark Johnston 	/*
519cab10561SMark Johnston 	 * Give MAC a chance to weigh in on the policy: if the securelevel is
520cab10561SMark Johnston 	 * not raised, then MAC may veto the backend, otherwise MAC may
521cab10561SMark Johnston 	 * explicitly grant access.
522cab10561SMark Johnston 	 */
523cab10561SMark Johnston 	if (error == 0) {
524cab10561SMark Johnston 		error = mac_kdb_check_backend(be);
525cab10561SMark Johnston 		if (error != 0) {
526cab10561SMark Johnston 			printf("MAC prevented execution of KDB backend: %s\n",
527cab10561SMark Johnston 			    be->dbbe_name);
528cab10561SMark Johnston 			return (false);
529cab10561SMark Johnston 		}
530cab10561SMark Johnston 	} else if (mac_kdb_grant_backend(be) == 0) {
531cab10561SMark Johnston 		error = 0;
532cab10561SMark Johnston 	}
533cab10561SMark Johnston #endif
534cab10561SMark Johnston 	if (error != 0)
535cab10561SMark Johnston 		printf("refusing to enter KDB with elevated securelevel\n");
536cab10561SMark Johnston 	return (error == 0);
537cab10561SMark Johnston }
538cab10561SMark Johnston 
5393bcd2440SMarcel Moolenaar /*
540cbc17435SMarcel Moolenaar  * Enter the currently selected debugger. If a message has been provided,
541cbc17435SMarcel Moolenaar  * it is printed first. If the debugger does not support the enter method,
542cbc17435SMarcel Moolenaar  * it is entered by using breakpoint(), which enters the debugger through
5433de213ccSRobert Watson  * kdb_trap().  The 'why' argument will contain a more mechanically usable
5443de213ccSRobert Watson  * string than 'msg', and is relied upon by DDB scripting to identify the
5453de213ccSRobert Watson  * reason for entering the debugger so that the right script can be run.
546cbc17435SMarcel Moolenaar  */
547cbc17435SMarcel Moolenaar void
5483de213ccSRobert Watson kdb_enter(const char *why, const char *msg)
549cbc17435SMarcel Moolenaar {
550cbc17435SMarcel Moolenaar 
551cbc17435SMarcel Moolenaar 	if (kdb_dbbe != NULL && kdb_active == 0) {
5521ea833a5STom Jones 		kdb_why = why;
553cbc17435SMarcel Moolenaar 		if (msg != NULL)
554cbc17435SMarcel Moolenaar 			printf("KDB: enter: %s\n", msg);
555cbc17435SMarcel Moolenaar 		breakpoint();
5563de213ccSRobert Watson 		kdb_why = KDB_WHY_UNSET;
557cbc17435SMarcel Moolenaar 	}
558cbc17435SMarcel Moolenaar }
559cbc17435SMarcel Moolenaar 
560cbc17435SMarcel Moolenaar /*
561cbc17435SMarcel Moolenaar  * Initialize the kernel debugger interface.
562cbc17435SMarcel Moolenaar  */
563cbc17435SMarcel Moolenaar void
5647cddab63SWarner Losh kdb_init(void)
565cbc17435SMarcel Moolenaar {
566cbc17435SMarcel Moolenaar 	struct kdb_dbbe *be, **iter;
567cbc17435SMarcel Moolenaar 	int cur_pri, pri;
568cbc17435SMarcel Moolenaar 
5699d6ae1e3SColin Percival 	TSENTER();
570cbc17435SMarcel Moolenaar 	kdb_active = 0;
571cbc17435SMarcel Moolenaar 	kdb_dbbe = NULL;
572cbc17435SMarcel Moolenaar 	cur_pri = -1;
573cbc17435SMarcel Moolenaar 	SET_FOREACH(iter, kdb_dbbe_set) {
574cbc17435SMarcel Moolenaar 		be = *iter;
575cbc17435SMarcel Moolenaar 		pri = (be->dbbe_init != NULL) ? be->dbbe_init() : -1;
576cbc17435SMarcel Moolenaar 		be->dbbe_active = (pri >= 0) ? 0 : -1;
577cbc17435SMarcel Moolenaar 		if (pri > cur_pri) {
578cbc17435SMarcel Moolenaar 			cur_pri = pri;
579cbc17435SMarcel Moolenaar 			kdb_dbbe = be;
580cbc17435SMarcel Moolenaar 		}
581cbc17435SMarcel Moolenaar 	}
582cbc17435SMarcel Moolenaar 	if (kdb_dbbe != NULL) {
583cbc17435SMarcel Moolenaar 		printf("KDB: debugger backends:");
584cbc17435SMarcel Moolenaar 		SET_FOREACH(iter, kdb_dbbe_set) {
585cbc17435SMarcel Moolenaar 			be = *iter;
586cbc17435SMarcel Moolenaar 			if (be->dbbe_active == 0)
587cbc17435SMarcel Moolenaar 				printf(" %s", be->dbbe_name);
588cbc17435SMarcel Moolenaar 		}
589cbc17435SMarcel Moolenaar 		printf("\n");
590cbc17435SMarcel Moolenaar 		printf("KDB: current backend: %s\n",
591cbc17435SMarcel Moolenaar 		    kdb_dbbe->dbbe_name);
592cbc17435SMarcel Moolenaar 	}
5939d6ae1e3SColin Percival 	TSEXIT();
594cbc17435SMarcel Moolenaar }
595cbc17435SMarcel Moolenaar 
596cbc17435SMarcel Moolenaar /*
597cbc17435SMarcel Moolenaar  * Handle contexts.
598cbc17435SMarcel Moolenaar  */
599cbc17435SMarcel Moolenaar void *
600cbc17435SMarcel Moolenaar kdb_jmpbuf(jmp_buf new)
601cbc17435SMarcel Moolenaar {
602cbc17435SMarcel Moolenaar 	void *old;
603cbc17435SMarcel Moolenaar 
604cbc17435SMarcel Moolenaar 	old = kdb_jmpbufp;
605cbc17435SMarcel Moolenaar 	kdb_jmpbufp = new;
606cbc17435SMarcel Moolenaar 	return (old);
607cbc17435SMarcel Moolenaar }
608cbc17435SMarcel Moolenaar 
609cbc17435SMarcel Moolenaar void
610cbc17435SMarcel Moolenaar kdb_reenter(void)
611cbc17435SMarcel Moolenaar {
612cbc17435SMarcel Moolenaar 
613cbc17435SMarcel Moolenaar 	if (!kdb_active || kdb_jmpbufp == NULL)
614cbc17435SMarcel Moolenaar 		return;
615cbc17435SMarcel Moolenaar 
616c2a44591SKonstantin Belousov 	printf("KDB: reentering\n");
617c2a44591SKonstantin Belousov 	kdb_backtrace();
618cbc17435SMarcel Moolenaar 	longjmp(kdb_jmpbufp, 1);
619cbc17435SMarcel Moolenaar 	/* NOTREACHED */
620cbc17435SMarcel Moolenaar }
621cbc17435SMarcel Moolenaar 
622212ff84fSEdward Tomasz Napierala void
623212ff84fSEdward Tomasz Napierala kdb_reenter_silent(void)
624212ff84fSEdward Tomasz Napierala {
625212ff84fSEdward Tomasz Napierala 
626212ff84fSEdward Tomasz Napierala 	if (!kdb_active || kdb_jmpbufp == NULL)
627212ff84fSEdward Tomasz Napierala 		return;
628212ff84fSEdward Tomasz Napierala 
629212ff84fSEdward Tomasz Napierala 	longjmp(kdb_jmpbufp, 1);
630212ff84fSEdward Tomasz Napierala 	/* NOTREACHED */
631212ff84fSEdward Tomasz Napierala }
632212ff84fSEdward Tomasz Napierala 
633cbc17435SMarcel Moolenaar /*
6342c9ec075SAlexey Dokuchaev  * Thread-related support functions.
635cbc17435SMarcel Moolenaar  */
636cbc17435SMarcel Moolenaar struct pcb *
637cbc17435SMarcel Moolenaar kdb_thr_ctx(struct thread *thr)
638fdc9713bSDoug White {
6399fb6718dSMark Johnston #ifdef SMP
640fdc9713bSDoug White 	struct pcpu *pc;
64158553b99SJohn Baldwin #endif
642fdc9713bSDoug White 
643fdc9713bSDoug White 	if (thr == curthread)
6446b1e0d75SJohn Baldwin 		return (&kdb_pcb);
645fdc9713bSDoug White 
6469fb6718dSMark Johnston #ifdef SMP
647d098f930SNathan Whitehorn 	STAILQ_FOREACH(pc, &cpuhead, pc_allcpu)  {
64871a19bdcSAttilio Rao 		if (pc->pc_curthread == thr &&
649a38f1f26SAttilio Rao 		    CPU_ISSET(pc->pc_cpuid, &stopped_cpus))
6509fb6718dSMark Johnston 			return (&stoppcbs[pc->pc_cpuid]);
651fdc9713bSDoug White 	}
65258553b99SJohn Baldwin #endif
6536b1e0d75SJohn Baldwin 	return (thr->td_pcb);
654fdc9713bSDoug White }
655cbc17435SMarcel Moolenaar 
656cbc17435SMarcel Moolenaar struct thread *
657cbc17435SMarcel Moolenaar kdb_thr_first(void)
658cbc17435SMarcel Moolenaar {
659cbc17435SMarcel Moolenaar 	struct proc *p;
660cbc17435SMarcel Moolenaar 	struct thread *thr;
6619acce1c9SJohn Baldwin 	u_int i;
662cbc17435SMarcel Moolenaar 
6634ccaa87fSMitchell Horne 	/* This function may be called early. */
6644ccaa87fSMitchell Horne 	if (pidhashtbl == NULL)
6654ccaa87fSMitchell Horne 		return (&thread0);
6664ccaa87fSMitchell Horne 
6679acce1c9SJohn Baldwin 	for (i = 0; i <= pidhash; i++) {
6689acce1c9SJohn Baldwin 		LIST_FOREACH(p, &pidhashtbl[i], p_hash) {
669cbc17435SMarcel Moolenaar 			thr = FIRST_THREAD_IN_PROC(p);
670cbc17435SMarcel Moolenaar 			if (thr != NULL)
671cbc17435SMarcel Moolenaar 				return (thr);
672cbc17435SMarcel Moolenaar 		}
673cbc17435SMarcel Moolenaar 	}
674cbc17435SMarcel Moolenaar 	return (NULL);
675cbc17435SMarcel Moolenaar }
676cbc17435SMarcel Moolenaar 
677cbc17435SMarcel Moolenaar struct thread *
6783d4f3136SMarcel Moolenaar kdb_thr_from_pid(pid_t pid)
6793d4f3136SMarcel Moolenaar {
6803d4f3136SMarcel Moolenaar 	struct proc *p;
6813d4f3136SMarcel Moolenaar 
6829acce1c9SJohn Baldwin 	LIST_FOREACH(p, PIDHASH(pid), p_hash) {
683825d2341SJohn Baldwin 		if (p->p_pid == pid)
6843d4f3136SMarcel Moolenaar 			return (FIRST_THREAD_IN_PROC(p));
6853d4f3136SMarcel Moolenaar 	}
6863d4f3136SMarcel Moolenaar 	return (NULL);
6873d4f3136SMarcel Moolenaar }
6883d4f3136SMarcel Moolenaar 
6893d4f3136SMarcel Moolenaar struct thread *
6903d4f3136SMarcel Moolenaar kdb_thr_lookup(lwpid_t tid)
691cbc17435SMarcel Moolenaar {
692cbc17435SMarcel Moolenaar 	struct thread *thr;
693cbc17435SMarcel Moolenaar 
694cbc17435SMarcel Moolenaar 	thr = kdb_thr_first();
695cbc17435SMarcel Moolenaar 	while (thr != NULL && thr->td_tid != tid)
696cbc17435SMarcel Moolenaar 		thr = kdb_thr_next(thr);
697cbc17435SMarcel Moolenaar 	return (thr);
698cbc17435SMarcel Moolenaar }
699cbc17435SMarcel Moolenaar 
700cbc17435SMarcel Moolenaar struct thread *
701cbc17435SMarcel Moolenaar kdb_thr_next(struct thread *thr)
702cbc17435SMarcel Moolenaar {
703cbc17435SMarcel Moolenaar 	struct proc *p;
7049acce1c9SJohn Baldwin 	u_int hash;
705cbc17435SMarcel Moolenaar 
706cbc17435SMarcel Moolenaar 	p = thr->td_proc;
707cbc17435SMarcel Moolenaar 	thr = TAILQ_NEXT(thr, td_plist);
708cbc17435SMarcel Moolenaar 	if (thr != NULL)
709cbc17435SMarcel Moolenaar 		return (thr);
7104ccaa87fSMitchell Horne 	if (pidhashtbl == NULL)
7114ccaa87fSMitchell Horne 		return (NULL);
7129acce1c9SJohn Baldwin 	hash = p->p_pid & pidhash;
7139acce1c9SJohn Baldwin 	for (;;) {
7149acce1c9SJohn Baldwin 		p = LIST_NEXT(p, p_hash);
7159acce1c9SJohn Baldwin 		while (p == NULL) {
7169acce1c9SJohn Baldwin 			if (++hash > pidhash)
717cbc17435SMarcel Moolenaar 				return (NULL);
7189acce1c9SJohn Baldwin 			p = LIST_FIRST(&pidhashtbl[hash]);
7199acce1c9SJohn Baldwin 		}
720825d2341SJohn Baldwin 		thr = FIRST_THREAD_IN_PROC(p);
721825d2341SJohn Baldwin 		if (thr != NULL)
722825d2341SJohn Baldwin 			return (thr);
7239acce1c9SJohn Baldwin 	}
724cbc17435SMarcel Moolenaar }
725cbc17435SMarcel Moolenaar 
726cbc17435SMarcel Moolenaar int
727cbc17435SMarcel Moolenaar kdb_thr_select(struct thread *thr)
728cbc17435SMarcel Moolenaar {
729cbc17435SMarcel Moolenaar 	if (thr == NULL)
730cbc17435SMarcel Moolenaar 		return (EINVAL);
731cbc17435SMarcel Moolenaar 	kdb_thread = thr;
732cbc17435SMarcel Moolenaar 	kdb_thrctx = kdb_thr_ctx(thr);
733cbc17435SMarcel Moolenaar 	return (0);
734cbc17435SMarcel Moolenaar }
735cbc17435SMarcel Moolenaar 
736cbc17435SMarcel Moolenaar /*
737cbc17435SMarcel Moolenaar  * Enter the debugger due to a trap.
738cbc17435SMarcel Moolenaar  */
739cbc17435SMarcel Moolenaar int
740cbc17435SMarcel Moolenaar kdb_trap(int type, int code, struct trapframe *tf)
741cbc17435SMarcel Moolenaar {
742d8939d82SRobert Watson #ifdef SMP
743a38f1f26SAttilio Rao 	cpuset_t other_cpus;
744d8939d82SRobert Watson #endif
745c0757dafSAttilio Rao 	struct kdb_dbbe *be;
746c0757dafSAttilio Rao 	register_t intr;
7475991a4f8SMarcel Moolenaar 	int handled;
74835370593SAndriy Gapon 	int did_stop_cpus;
749cbc17435SMarcel Moolenaar 
7503a5d3671SMatthew D Fleming 	be = kdb_dbbe;
7513a5d3671SMatthew D Fleming 	if (be == NULL || be->dbbe_trap == NULL)
752cbc17435SMarcel Moolenaar 		return (0);
753cbc17435SMarcel Moolenaar 
754cbc17435SMarcel Moolenaar 	/* We reenter the debugger through kdb_reenter(). */
755cbc17435SMarcel Moolenaar 	if (kdb_active)
756cbc17435SMarcel Moolenaar 		return (0);
757cbc17435SMarcel Moolenaar 
7582fae8f5aSMarcel Moolenaar 	intr = intr_disable();
759cbc17435SMarcel Moolenaar 
76035370593SAndriy Gapon 	if (!SCHEDULER_STOPPED()) {
761fcbb889fSEric van Gyzen #ifdef SMP
762a19bd8e3SStefan Eßer 		other_cpus = all_cpus;
763a19bd8e3SStefan Eßer 		CPU_ANDNOT(&other_cpus, &other_cpus, &stopped_cpus);
764a38f1f26SAttilio Rao 		CPU_CLR(PCPU_GET(cpuid), &other_cpus);
765a38f1f26SAttilio Rao 		stop_cpus_hard(other_cpus);
766fcbb889fSEric van Gyzen #endif
7676b353101SOlivier Certner 		scheduler_stopped = true;
76835370593SAndriy Gapon 		did_stop_cpus = 1;
76935370593SAndriy Gapon 	} else
77035370593SAndriy Gapon 		did_stop_cpus = 0;
771cbc17435SMarcel Moolenaar 
77239eb1d12SMarcel Moolenaar 	kdb_active++;
77339eb1d12SMarcel Moolenaar 
774e6aa7232SMarcel Moolenaar 	kdb_frame = tf;
775e6aa7232SMarcel Moolenaar 
776cbc17435SMarcel Moolenaar 	/* Let MD code do its thing first... */
777cbc17435SMarcel Moolenaar 	kdb_cpu_trap(type, code);
778cbc17435SMarcel Moolenaar 
779ddf41225SMarcel Moolenaar 	makectx(tf, &kdb_pcb);
780ddf41225SMarcel Moolenaar 	kdb_thr_select(curthread);
781ddf41225SMarcel Moolenaar 
782bf8696b4SAndriy Gapon 	cngrab();
783bf8696b4SAndriy Gapon 
7843a5d3671SMatthew D Fleming 	for (;;) {
785bd16c274SMark Johnston 		if (!kdb_backend_permitted(be, curthread)) {
7862449b9e5SMitchell Horne 			/* Unhandled breakpoint traps are fatal. */
7872449b9e5SMitchell Horne 			handled = 1;
7882449b9e5SMitchell Horne 			break;
7892449b9e5SMitchell Horne 		}
7903a5d3671SMatthew D Fleming 		handled = be->dbbe_trap(type, code);
7913a5d3671SMatthew D Fleming 		if (be == kdb_dbbe)
7923a5d3671SMatthew D Fleming 			break;
7933a5d3671SMatthew D Fleming 		be = kdb_dbbe;
7943a5d3671SMatthew D Fleming 		if (be == NULL || be->dbbe_trap == NULL)
7953a5d3671SMatthew D Fleming 			break;
7963a5d3671SMatthew D Fleming 		printf("Switching to %s back-end\n", be->dbbe_name);
7973a5d3671SMatthew D Fleming 	}
798cbc17435SMarcel Moolenaar 
799bf8696b4SAndriy Gapon 	cnungrab();
800bf8696b4SAndriy Gapon 
80139eb1d12SMarcel Moolenaar 	kdb_active--;
80239eb1d12SMarcel Moolenaar 
80316b51429SEric van Gyzen 	if (did_stop_cpus) {
8046b353101SOlivier Certner 		scheduler_stopped = false;
805fcbb889fSEric van Gyzen #ifdef SMP
806e2650af1SStefan Eßer 		CPU_AND(&other_cpus, &other_cpus, &stopped_cpus);
8075b3e8b07SWojciech Macek 		restart_cpus(other_cpus);
808cbc17435SMarcel Moolenaar #endif
809fcbb889fSEric van Gyzen 	}
810cbc17435SMarcel Moolenaar 
8112fae8f5aSMarcel Moolenaar 	intr_restore(intr);
812cbc17435SMarcel Moolenaar 
813cbc17435SMarcel Moolenaar 	return (handled);
814cbc17435SMarcel Moolenaar }
815