xref: /freebsd/sys/kern/subr_kdb.c (revision 6b1e0d75b06f6ac87729bec87284466e5c25ee60)
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"
31f0c6706dSPeter Wemm 
32cbc17435SMarcel Moolenaar #include <sys/param.h>
33cbc17435SMarcel Moolenaar #include <sys/systm.h>
34cbc17435SMarcel Moolenaar #include <sys/kdb.h>
35cbc17435SMarcel Moolenaar #include <sys/kernel.h>
36cbc17435SMarcel Moolenaar #include <sys/malloc.h>
37cbc17435SMarcel Moolenaar #include <sys/pcpu.h>
38cbc17435SMarcel Moolenaar #include <sys/proc.h>
39cbc17435SMarcel Moolenaar #include <sys/smp.h>
40cbc17435SMarcel Moolenaar #include <sys/sysctl.h>
41cbc17435SMarcel Moolenaar 
42cbc17435SMarcel Moolenaar #include <machine/kdb.h>
43cbc17435SMarcel Moolenaar #include <machine/pcb.h>
44cbc17435SMarcel Moolenaar 
45fdc9713bSDoug White #ifdef KDB_STOP_NMI
46fdc9713bSDoug White #include <machine/smp.h>
47fdc9713bSDoug White #endif
48fdc9713bSDoug White 
49fdc9713bSDoug White /*
50fdc9713bSDoug White  * KDB_STOP_NMI requires SMP to pick up the right dependencies
51fdc9713bSDoug White  * (And isn't useful on UP anyway)
52fdc9713bSDoug White  */
53fdc9713bSDoug White #if defined(KDB_STOP_NMI) && !defined(SMP)
54fdc9713bSDoug White #error "options KDB_STOP_NMI" requires "options SMP"
55fdc9713bSDoug White #endif
56fdc9713bSDoug White 
57cbc17435SMarcel Moolenaar int kdb_active = 0;
58cbc17435SMarcel Moolenaar void *kdb_jmpbufp = NULL;
59cbc17435SMarcel Moolenaar struct kdb_dbbe *kdb_dbbe = NULL;
60cbc17435SMarcel Moolenaar struct pcb kdb_pcb;
61cbc17435SMarcel Moolenaar struct pcb *kdb_thrctx = NULL;
62cbc17435SMarcel Moolenaar struct thread *kdb_thread = NULL;
63cbc17435SMarcel Moolenaar struct trapframe *kdb_frame = NULL;
64cbc17435SMarcel Moolenaar 
65cbc17435SMarcel Moolenaar KDB_BACKEND(null, NULL, NULL, NULL);
66cbc17435SMarcel Moolenaar SET_DECLARE(kdb_dbbe_set, struct kdb_dbbe);
67cbc17435SMarcel Moolenaar 
68cbc17435SMarcel Moolenaar static int kdb_sysctl_available(SYSCTL_HANDLER_ARGS);
69cbc17435SMarcel Moolenaar static int kdb_sysctl_current(SYSCTL_HANDLER_ARGS);
70cbc17435SMarcel Moolenaar static int kdb_sysctl_enter(SYSCTL_HANDLER_ARGS);
71cbc17435SMarcel Moolenaar 
72cbc17435SMarcel Moolenaar SYSCTL_NODE(_debug, OID_AUTO, kdb, CTLFLAG_RW, NULL, "KDB nodes");
73cbc17435SMarcel Moolenaar 
74cbc17435SMarcel Moolenaar SYSCTL_PROC(_debug_kdb, OID_AUTO, available, CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
75cbc17435SMarcel Moolenaar     kdb_sysctl_available, "A", "list of available KDB backends");
76cbc17435SMarcel Moolenaar 
77cbc17435SMarcel Moolenaar SYSCTL_PROC(_debug_kdb, OID_AUTO, current, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
78cbc17435SMarcel Moolenaar     kdb_sysctl_current, "A", "currently selected KDB backend");
79cbc17435SMarcel Moolenaar 
80cbc17435SMarcel Moolenaar SYSCTL_PROC(_debug_kdb, OID_AUTO, enter, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
81cbc17435SMarcel Moolenaar     kdb_sysctl_enter, "I", "set to enter the debugger");
82cbc17435SMarcel Moolenaar 
83d8939d82SRobert Watson /*
84d8939d82SRobert Watson  * Flag indicating whether or not to IPI the other CPUs to stop them on
85d8939d82SRobert Watson  * entering the debugger.  Sometimes, this will result in a deadlock as
86d8939d82SRobert Watson  * stop_cpus() waits for the other cpus to stop, so we allow it to be
87d8939d82SRobert Watson  * disabled.
88d8939d82SRobert Watson  */
89d8939d82SRobert Watson #ifdef SMP
90d8939d82SRobert Watson static int kdb_stop_cpus = 1;
91d8939d82SRobert Watson SYSCTL_INT(_debug_kdb, OID_AUTO, stop_cpus, CTLTYPE_INT | CTLFLAG_RW,
92d8939d82SRobert Watson     &kdb_stop_cpus, 0, "");
93d963815bSRobert Watson TUNABLE_INT("debug.kdb.stop_cpus", &kdb_stop_cpus);
94fdc9713bSDoug White 
95fdc9713bSDoug White #ifdef KDB_STOP_NMI
96fdc9713bSDoug White /*
97fdc9713bSDoug White  * Provide an alternate method of stopping other CPUs. If another CPU has
98fdc9713bSDoug White  * disabled interrupts the conventional STOP IPI will be blocked. This
99fdc9713bSDoug White  * NMI-based stop should get through in that case.
100fdc9713bSDoug White  */
101667285c4SRobert Watson static int kdb_stop_cpus_with_nmi = 1;
102fdc9713bSDoug White SYSCTL_INT(_debug_kdb, OID_AUTO, stop_cpus_with_nmi, CTLTYPE_INT | CTLFLAG_RW,
103fdc9713bSDoug White     &kdb_stop_cpus_with_nmi, 0, "");
104fdc9713bSDoug White TUNABLE_INT("debug.kdb.stop_cpus_with_nmi", &kdb_stop_cpus_with_nmi);
105fdc9713bSDoug White #endif /* KDB_STOP_NMI */
106fdc9713bSDoug White 
107d8939d82SRobert Watson #endif
108d8939d82SRobert Watson 
109cbc17435SMarcel Moolenaar static int
110cbc17435SMarcel Moolenaar kdb_sysctl_available(SYSCTL_HANDLER_ARGS)
111cbc17435SMarcel Moolenaar {
112cbc17435SMarcel Moolenaar 	struct kdb_dbbe *be, **iter;
113cbc17435SMarcel Moolenaar 	char *avail, *p;
114cbc17435SMarcel Moolenaar 	ssize_t len, sz;
115cbc17435SMarcel Moolenaar 	int error;
116cbc17435SMarcel Moolenaar 
117cbc17435SMarcel Moolenaar 	sz = 0;
118cbc17435SMarcel Moolenaar 	SET_FOREACH(iter, kdb_dbbe_set) {
119cbc17435SMarcel Moolenaar 		be = *iter;
120cbc17435SMarcel Moolenaar 		if (be->dbbe_active == 0)
121cbc17435SMarcel Moolenaar 			sz += strlen(be->dbbe_name) + 1;
122cbc17435SMarcel Moolenaar 	}
123cbc17435SMarcel Moolenaar 	sz++;
124cbc17435SMarcel Moolenaar 	avail = malloc(sz, M_TEMP, M_WAITOK);
125cbc17435SMarcel Moolenaar 	p = avail;
126f742a1edSStephan Uphoff 	*p = '\0';
127f742a1edSStephan Uphoff 
128cbc17435SMarcel Moolenaar 	SET_FOREACH(iter, kdb_dbbe_set) {
129cbc17435SMarcel Moolenaar 		be = *iter;
130cbc17435SMarcel Moolenaar 		if (be->dbbe_active == 0) {
131cbc17435SMarcel Moolenaar 			len = snprintf(p, sz, "%s ", be->dbbe_name);
132cbc17435SMarcel Moolenaar 			p += len;
133cbc17435SMarcel Moolenaar 			sz -= len;
134cbc17435SMarcel Moolenaar 		}
135cbc17435SMarcel Moolenaar 	}
136cbc17435SMarcel Moolenaar 	KASSERT(sz >= 0, ("%s", __func__));
137cbc17435SMarcel Moolenaar 	error = sysctl_handle_string(oidp, avail, 0, req);
138cbc17435SMarcel Moolenaar 	free(avail, M_TEMP);
139cbc17435SMarcel Moolenaar 	return (error);
140cbc17435SMarcel Moolenaar }
141cbc17435SMarcel Moolenaar 
142cbc17435SMarcel Moolenaar static int
143cbc17435SMarcel Moolenaar kdb_sysctl_current(SYSCTL_HANDLER_ARGS)
144cbc17435SMarcel Moolenaar {
145cbc17435SMarcel Moolenaar 	char buf[16];
146cbc17435SMarcel Moolenaar 	int error;
147cbc17435SMarcel Moolenaar 
148a8bfba1aSMarcel Moolenaar 	if (kdb_dbbe != NULL) {
149cbc17435SMarcel Moolenaar 		strncpy(buf, kdb_dbbe->dbbe_name, sizeof(buf));
150cbc17435SMarcel Moolenaar 		buf[sizeof(buf) - 1] = '\0';
151a8bfba1aSMarcel Moolenaar 	} else
152a8bfba1aSMarcel Moolenaar 		*buf = '\0';
153cbc17435SMarcel Moolenaar 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
154cbc17435SMarcel Moolenaar 	if (error != 0 || req->newptr == NULL)
155cbc17435SMarcel Moolenaar 		return (error);
156cbc17435SMarcel Moolenaar 	if (kdb_active)
157cbc17435SMarcel Moolenaar 		return (EBUSY);
1583bcd2440SMarcel Moolenaar 	return (kdb_dbbe_select(buf));
159cbc17435SMarcel Moolenaar }
160cbc17435SMarcel Moolenaar 
161cbc17435SMarcel Moolenaar static int
162cbc17435SMarcel Moolenaar kdb_sysctl_enter(SYSCTL_HANDLER_ARGS)
163cbc17435SMarcel Moolenaar {
164cbc17435SMarcel Moolenaar 	int error, i;
165cbc17435SMarcel Moolenaar 
166cbc17435SMarcel Moolenaar 	error = sysctl_wire_old_buffer(req, sizeof(int));
167cbc17435SMarcel Moolenaar 	if (error == 0) {
168cbc17435SMarcel Moolenaar 		i = 0;
169cbc17435SMarcel Moolenaar 		error = sysctl_handle_int(oidp, &i, 0, req);
170cbc17435SMarcel Moolenaar 	}
171cbc17435SMarcel Moolenaar 	if (error != 0 || req->newptr == NULL)
172cbc17435SMarcel Moolenaar 		return (error);
173cbc17435SMarcel Moolenaar 	if (kdb_active)
174cbc17435SMarcel Moolenaar 		return (EBUSY);
175cbc17435SMarcel Moolenaar 	kdb_enter("sysctl debug.kdb.enter");
176cbc17435SMarcel Moolenaar 	return (0);
177cbc17435SMarcel Moolenaar }
178cbc17435SMarcel Moolenaar 
179cbc17435SMarcel Moolenaar /*
180cbc17435SMarcel Moolenaar  * Solaris implements a new BREAK which is initiated by a character sequence
181cbc17435SMarcel Moolenaar  * CR ~ ^b which is similar to a familiar pattern used on Sun servers by the
182cbc17435SMarcel Moolenaar  * Remote Console.
183cbc17435SMarcel Moolenaar  *
184cbc17435SMarcel Moolenaar  * Note that this function may be called from almost anywhere, with interrupts
185cbc17435SMarcel Moolenaar  * disabled and with unknown locks held, so it must not access data other than
186cbc17435SMarcel Moolenaar  * its arguments.  Its up to the caller to ensure that the state variable is
187cbc17435SMarcel Moolenaar  * consistent.
188cbc17435SMarcel Moolenaar  */
189cbc17435SMarcel Moolenaar 
190cbc17435SMarcel Moolenaar #define	KEY_CR		13	/* CR '\r' */
191cbc17435SMarcel Moolenaar #define	KEY_TILDE	126	/* ~ */
192cbc17435SMarcel Moolenaar #define	KEY_CRTLB	2	/* ^B */
193cbc17435SMarcel Moolenaar 
194cbc17435SMarcel Moolenaar int
195cbc17435SMarcel Moolenaar kdb_alt_break(int key, int *state)
196cbc17435SMarcel Moolenaar {
197cbc17435SMarcel Moolenaar 	int brk;
198cbc17435SMarcel Moolenaar 
199cbc17435SMarcel Moolenaar 	brk = 0;
200cbc17435SMarcel Moolenaar 	switch (key) {
201cbc17435SMarcel Moolenaar 	case KEY_CR:
202cbc17435SMarcel Moolenaar 		*state = KEY_TILDE;
203cbc17435SMarcel Moolenaar 		break;
204cbc17435SMarcel Moolenaar 	case KEY_TILDE:
205cbc17435SMarcel Moolenaar 		*state = (*state == KEY_TILDE) ? KEY_CRTLB : 0;
206cbc17435SMarcel Moolenaar 		break;
207cbc17435SMarcel Moolenaar 	case KEY_CRTLB:
208cbc17435SMarcel Moolenaar 		if (*state == KEY_CRTLB)
209cbc17435SMarcel Moolenaar 			brk = 1;
210cbc17435SMarcel Moolenaar 		/* FALLTHROUGH */
211cbc17435SMarcel Moolenaar 	default:
212cbc17435SMarcel Moolenaar 		*state = 0;
213cbc17435SMarcel Moolenaar 		break;
214cbc17435SMarcel Moolenaar 	}
215cbc17435SMarcel Moolenaar 	return (brk);
216cbc17435SMarcel Moolenaar }
217cbc17435SMarcel Moolenaar 
218cbc17435SMarcel Moolenaar /*
219cbc17435SMarcel Moolenaar  * Print a backtrace of the calling thread. The backtrace is generated by
220cbc17435SMarcel Moolenaar  * the selected debugger, provided it supports backtraces. If no debugger
221cbc17435SMarcel Moolenaar  * is selected or the current debugger does not support backtraces, this
222cbc17435SMarcel Moolenaar  * function silently returns.
223cbc17435SMarcel Moolenaar  */
224cbc17435SMarcel Moolenaar 
225cbc17435SMarcel Moolenaar void
226cbc17435SMarcel Moolenaar kdb_backtrace()
227cbc17435SMarcel Moolenaar {
228cbc17435SMarcel Moolenaar 
229cbc17435SMarcel Moolenaar 	if (kdb_dbbe != NULL && kdb_dbbe->dbbe_trace != NULL) {
230cbc17435SMarcel Moolenaar 		printf("KDB: stack backtrace:\n");
231cbc17435SMarcel Moolenaar 		kdb_dbbe->dbbe_trace();
232cbc17435SMarcel Moolenaar 	}
233cbc17435SMarcel Moolenaar }
234cbc17435SMarcel Moolenaar 
235cbc17435SMarcel Moolenaar /*
2363bcd2440SMarcel Moolenaar  * Set/change the current backend.
2373bcd2440SMarcel Moolenaar  */
2383bcd2440SMarcel Moolenaar 
2393bcd2440SMarcel Moolenaar int
2403bcd2440SMarcel Moolenaar kdb_dbbe_select(const char *name)
2413bcd2440SMarcel Moolenaar {
2423bcd2440SMarcel Moolenaar 	struct kdb_dbbe *be, **iter;
2433bcd2440SMarcel Moolenaar 
2443bcd2440SMarcel Moolenaar 	SET_FOREACH(iter, kdb_dbbe_set) {
2453bcd2440SMarcel Moolenaar 		be = *iter;
2463bcd2440SMarcel Moolenaar 		if (be->dbbe_active == 0 && strcmp(be->dbbe_name, name) == 0) {
2473bcd2440SMarcel Moolenaar 			kdb_dbbe = be;
2483bcd2440SMarcel Moolenaar 			return (0);
2493bcd2440SMarcel Moolenaar 		}
2503bcd2440SMarcel Moolenaar 	}
2513bcd2440SMarcel Moolenaar 	return (EINVAL);
2523bcd2440SMarcel Moolenaar }
2533bcd2440SMarcel Moolenaar 
2543bcd2440SMarcel Moolenaar /*
255cbc17435SMarcel Moolenaar  * Enter the currently selected debugger. If a message has been provided,
256cbc17435SMarcel Moolenaar  * it is printed first. If the debugger does not support the enter method,
257cbc17435SMarcel Moolenaar  * it is entered by using breakpoint(), which enters the debugger through
258cbc17435SMarcel Moolenaar  * kdb_trap().
259cbc17435SMarcel Moolenaar  */
260cbc17435SMarcel Moolenaar 
261cbc17435SMarcel Moolenaar void
262cbc17435SMarcel Moolenaar kdb_enter(const char *msg)
263cbc17435SMarcel Moolenaar {
264cbc17435SMarcel Moolenaar 
265cbc17435SMarcel Moolenaar 	if (kdb_dbbe != NULL && kdb_active == 0) {
266cbc17435SMarcel Moolenaar 		if (msg != NULL)
267cbc17435SMarcel Moolenaar 			printf("KDB: enter: %s\n", msg);
268cbc17435SMarcel Moolenaar 		breakpoint();
269cbc17435SMarcel Moolenaar 	}
270cbc17435SMarcel Moolenaar }
271cbc17435SMarcel Moolenaar 
272cbc17435SMarcel Moolenaar /*
273cbc17435SMarcel Moolenaar  * Initialize the kernel debugger interface.
274cbc17435SMarcel Moolenaar  */
275cbc17435SMarcel Moolenaar 
276cbc17435SMarcel Moolenaar void
277cbc17435SMarcel Moolenaar kdb_init()
278cbc17435SMarcel Moolenaar {
279cbc17435SMarcel Moolenaar 	struct kdb_dbbe *be, **iter;
280cbc17435SMarcel Moolenaar 	int cur_pri, pri;
281cbc17435SMarcel Moolenaar 
282cbc17435SMarcel Moolenaar 	kdb_active = 0;
283cbc17435SMarcel Moolenaar 	kdb_dbbe = NULL;
284cbc17435SMarcel Moolenaar 	cur_pri = -1;
285cbc17435SMarcel Moolenaar 	SET_FOREACH(iter, kdb_dbbe_set) {
286cbc17435SMarcel Moolenaar 		be = *iter;
287cbc17435SMarcel Moolenaar 		pri = (be->dbbe_init != NULL) ? be->dbbe_init() : -1;
288cbc17435SMarcel Moolenaar 		be->dbbe_active = (pri >= 0) ? 0 : -1;
289cbc17435SMarcel Moolenaar 		if (pri > cur_pri) {
290cbc17435SMarcel Moolenaar 			cur_pri = pri;
291cbc17435SMarcel Moolenaar 			kdb_dbbe = be;
292cbc17435SMarcel Moolenaar 		}
293cbc17435SMarcel Moolenaar 	}
294cbc17435SMarcel Moolenaar 	if (kdb_dbbe != NULL) {
295cbc17435SMarcel Moolenaar 		printf("KDB: debugger backends:");
296cbc17435SMarcel Moolenaar 		SET_FOREACH(iter, kdb_dbbe_set) {
297cbc17435SMarcel Moolenaar 			be = *iter;
298cbc17435SMarcel Moolenaar 			if (be->dbbe_active == 0)
299cbc17435SMarcel Moolenaar 				printf(" %s", be->dbbe_name);
300cbc17435SMarcel Moolenaar 		}
301cbc17435SMarcel Moolenaar 		printf("\n");
302cbc17435SMarcel Moolenaar 		printf("KDB: current backend: %s\n",
303cbc17435SMarcel Moolenaar 		    kdb_dbbe->dbbe_name);
304cbc17435SMarcel Moolenaar 	}
305cbc17435SMarcel Moolenaar }
306cbc17435SMarcel Moolenaar 
307cbc17435SMarcel Moolenaar /*
308cbc17435SMarcel Moolenaar  * Handle contexts.
309cbc17435SMarcel Moolenaar  */
310cbc17435SMarcel Moolenaar 
311cbc17435SMarcel Moolenaar void *
312cbc17435SMarcel Moolenaar kdb_jmpbuf(jmp_buf new)
313cbc17435SMarcel Moolenaar {
314cbc17435SMarcel Moolenaar 	void *old;
315cbc17435SMarcel Moolenaar 
316cbc17435SMarcel Moolenaar 	old = kdb_jmpbufp;
317cbc17435SMarcel Moolenaar 	kdb_jmpbufp = new;
318cbc17435SMarcel Moolenaar 	return (old);
319cbc17435SMarcel Moolenaar }
320cbc17435SMarcel Moolenaar 
321cbc17435SMarcel Moolenaar void
322cbc17435SMarcel Moolenaar kdb_reenter(void)
323cbc17435SMarcel Moolenaar {
324cbc17435SMarcel Moolenaar 
325cbc17435SMarcel Moolenaar 	if (!kdb_active || kdb_jmpbufp == NULL)
326cbc17435SMarcel Moolenaar 		return;
327cbc17435SMarcel Moolenaar 
328cbc17435SMarcel Moolenaar 	longjmp(kdb_jmpbufp, 1);
329cbc17435SMarcel Moolenaar 	/* NOTREACHED */
330cbc17435SMarcel Moolenaar }
331cbc17435SMarcel Moolenaar 
332cbc17435SMarcel Moolenaar /*
333cbc17435SMarcel Moolenaar  * Thread related support functions.
334cbc17435SMarcel Moolenaar  */
335cbc17435SMarcel Moolenaar 
336cbc17435SMarcel Moolenaar struct pcb *
337cbc17435SMarcel Moolenaar kdb_thr_ctx(struct thread *thr)
338fdc9713bSDoug White #ifdef KDB_STOP_NMI
339fdc9713bSDoug White {
340fdc9713bSDoug White 	struct pcpu *pc;
3416b1e0d75SJohn Baldwin 	u_int cpuid;
342fdc9713bSDoug White 
343fdc9713bSDoug White 	if (thr == curthread)
3446b1e0d75SJohn Baldwin 		return (&kdb_pcb);
345fdc9713bSDoug White 
346fdc9713bSDoug White 	SLIST_FOREACH(pc, &cpuhead, pc_allcpu)  {
347fdc9713bSDoug White 		cpuid = pc->pc_cpuid;
348fdc9713bSDoug White 		if (pc->pc_curthread == thr && (atomic_load_acq_int(&stopped_cpus) & (1 << cpuid)))
3496b1e0d75SJohn Baldwin 			return (&stoppcbs[cpuid]);
350fdc9713bSDoug White 	}
3516b1e0d75SJohn Baldwin 	return (thr->td_pcb);
352fdc9713bSDoug White }
353fdc9713bSDoug White #else
354cbc17435SMarcel Moolenaar {
355cbc17435SMarcel Moolenaar 	return ((thr == curthread) ? &kdb_pcb : thr->td_pcb);
356cbc17435SMarcel Moolenaar }
357fdc9713bSDoug White #endif /* KDB_STOP_NMI */
358cbc17435SMarcel Moolenaar 
359cbc17435SMarcel Moolenaar struct thread *
360cbc17435SMarcel Moolenaar kdb_thr_first(void)
361cbc17435SMarcel Moolenaar {
362cbc17435SMarcel Moolenaar 	struct proc *p;
363cbc17435SMarcel Moolenaar 	struct thread *thr;
364cbc17435SMarcel Moolenaar 
365cbc17435SMarcel Moolenaar 	p = LIST_FIRST(&allproc);
366cbc17435SMarcel Moolenaar 	while (p != NULL) {
367cbc17435SMarcel Moolenaar 		if (p->p_sflag & PS_INMEM) {
368cbc17435SMarcel Moolenaar 			thr = FIRST_THREAD_IN_PROC(p);
369cbc17435SMarcel Moolenaar 			if (thr != NULL)
370cbc17435SMarcel Moolenaar 				return (thr);
371cbc17435SMarcel Moolenaar 		}
372cbc17435SMarcel Moolenaar 		p = LIST_NEXT(p, p_list);
373cbc17435SMarcel Moolenaar 	}
374cbc17435SMarcel Moolenaar 	return (NULL);
375cbc17435SMarcel Moolenaar }
376cbc17435SMarcel Moolenaar 
377cbc17435SMarcel Moolenaar struct thread *
3783d4f3136SMarcel Moolenaar kdb_thr_from_pid(pid_t pid)
3793d4f3136SMarcel Moolenaar {
3803d4f3136SMarcel Moolenaar 	struct proc *p;
3813d4f3136SMarcel Moolenaar 
3823d4f3136SMarcel Moolenaar 	p = LIST_FIRST(&allproc);
3833d4f3136SMarcel Moolenaar 	while (p != NULL) {
3843d4f3136SMarcel Moolenaar 		if (p->p_sflag & PS_INMEM && p->p_pid == pid)
3853d4f3136SMarcel Moolenaar 			return (FIRST_THREAD_IN_PROC(p));
3863d4f3136SMarcel Moolenaar 		p = LIST_NEXT(p, p_list);
3873d4f3136SMarcel Moolenaar 	}
3883d4f3136SMarcel Moolenaar 	return (NULL);
3893d4f3136SMarcel Moolenaar }
3903d4f3136SMarcel Moolenaar 
3913d4f3136SMarcel Moolenaar struct thread *
3923d4f3136SMarcel Moolenaar kdb_thr_lookup(lwpid_t tid)
393cbc17435SMarcel Moolenaar {
394cbc17435SMarcel Moolenaar 	struct thread *thr;
395cbc17435SMarcel Moolenaar 
396cbc17435SMarcel Moolenaar 	thr = kdb_thr_first();
397cbc17435SMarcel Moolenaar 	while (thr != NULL && thr->td_tid != tid)
398cbc17435SMarcel Moolenaar 		thr = kdb_thr_next(thr);
399cbc17435SMarcel Moolenaar 	return (thr);
400cbc17435SMarcel Moolenaar }
401cbc17435SMarcel Moolenaar 
402cbc17435SMarcel Moolenaar struct thread *
403cbc17435SMarcel Moolenaar kdb_thr_next(struct thread *thr)
404cbc17435SMarcel Moolenaar {
405cbc17435SMarcel Moolenaar 	struct proc *p;
406cbc17435SMarcel Moolenaar 
407cbc17435SMarcel Moolenaar 	p = thr->td_proc;
408cbc17435SMarcel Moolenaar 	thr = TAILQ_NEXT(thr, td_plist);
409cbc17435SMarcel Moolenaar 	do {
410cbc17435SMarcel Moolenaar 		if (thr != NULL)
411cbc17435SMarcel Moolenaar 			return (thr);
412cbc17435SMarcel Moolenaar 		p = LIST_NEXT(p, p_list);
413cbc17435SMarcel Moolenaar 		if (p != NULL && (p->p_sflag & PS_INMEM))
414cbc17435SMarcel Moolenaar 			thr = FIRST_THREAD_IN_PROC(p);
415cbc17435SMarcel Moolenaar 	} while (p != NULL);
416cbc17435SMarcel Moolenaar 	return (NULL);
417cbc17435SMarcel Moolenaar }
418cbc17435SMarcel Moolenaar 
419cbc17435SMarcel Moolenaar int
420cbc17435SMarcel Moolenaar kdb_thr_select(struct thread *thr)
421cbc17435SMarcel Moolenaar {
422cbc17435SMarcel Moolenaar 	if (thr == NULL)
423cbc17435SMarcel Moolenaar 		return (EINVAL);
424cbc17435SMarcel Moolenaar 	kdb_thread = thr;
425cbc17435SMarcel Moolenaar 	kdb_thrctx = kdb_thr_ctx(thr);
426cbc17435SMarcel Moolenaar 	return (0);
427cbc17435SMarcel Moolenaar }
428cbc17435SMarcel Moolenaar 
429cbc17435SMarcel Moolenaar /*
430cbc17435SMarcel Moolenaar  * Enter the debugger due to a trap.
431cbc17435SMarcel Moolenaar  */
432cbc17435SMarcel Moolenaar 
433cbc17435SMarcel Moolenaar int
434cbc17435SMarcel Moolenaar kdb_trap(int type, int code, struct trapframe *tf)
435cbc17435SMarcel Moolenaar {
436d8939d82SRobert Watson #ifdef SMP
437d8939d82SRobert Watson 	int did_stop_cpus;
438d8939d82SRobert Watson #endif
439cbc17435SMarcel Moolenaar 	int handled;
440cbc17435SMarcel Moolenaar 
441cbc17435SMarcel Moolenaar 	if (kdb_dbbe == NULL || kdb_dbbe->dbbe_trap == NULL)
442cbc17435SMarcel Moolenaar 		return (0);
443cbc17435SMarcel Moolenaar 
444cbc17435SMarcel Moolenaar 	/* We reenter the debugger through kdb_reenter(). */
445cbc17435SMarcel Moolenaar 	if (kdb_active)
446cbc17435SMarcel Moolenaar 		return (0);
447cbc17435SMarcel Moolenaar 
448cbc17435SMarcel Moolenaar 	critical_enter();
449cbc17435SMarcel Moolenaar 
450cbc17435SMarcel Moolenaar 	kdb_active++;
451cbc17435SMarcel Moolenaar 
452cbc17435SMarcel Moolenaar #ifdef SMP
453d8939d82SRobert Watson 	if ((did_stop_cpus = kdb_stop_cpus) != 0)
454fdc9713bSDoug White 	  {
455fdc9713bSDoug White #ifdef KDB_STOP_NMI
456fdc9713bSDoug White 	    if(kdb_stop_cpus_with_nmi)
457fdc9713bSDoug White 	      stop_cpus_nmi(PCPU_GET(other_cpus));
458fdc9713bSDoug White 	    else
459fdc9713bSDoug White #endif /* KDB_STOP_NMI */
460cbc17435SMarcel Moolenaar 		stop_cpus(PCPU_GET(other_cpus));
461fdc9713bSDoug White 	  }
462cbc17435SMarcel Moolenaar #endif
463cbc17435SMarcel Moolenaar 
464e6aa7232SMarcel Moolenaar 	kdb_frame = tf;
465e6aa7232SMarcel Moolenaar 
466cbc17435SMarcel Moolenaar 	/* Let MD code do its thing first... */
467cbc17435SMarcel Moolenaar 	kdb_cpu_trap(type, code);
468cbc17435SMarcel Moolenaar 
469ddf41225SMarcel Moolenaar 	makectx(tf, &kdb_pcb);
470ddf41225SMarcel Moolenaar 	kdb_thr_select(curthread);
471ddf41225SMarcel Moolenaar 
472cbc17435SMarcel Moolenaar 	handled = kdb_dbbe->dbbe_trap(type, code);
473cbc17435SMarcel Moolenaar 
474cbc17435SMarcel Moolenaar #ifdef SMP
475d8939d82SRobert Watson 	if (did_stop_cpus)
476cbc17435SMarcel Moolenaar 		restart_cpus(stopped_cpus);
477cbc17435SMarcel Moolenaar #endif
478cbc17435SMarcel Moolenaar 
479cbc17435SMarcel Moolenaar 	kdb_active--;
480cbc17435SMarcel Moolenaar 
481cbc17435SMarcel Moolenaar 	critical_exit();
482cbc17435SMarcel Moolenaar 
483cbc17435SMarcel Moolenaar 	return (handled);
484cbc17435SMarcel Moolenaar }
485