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