Deleted Added
full compact
subr_kdb.c (139804) subr_kdb.c (145727)
1/*-
2 * Copyright (c) 2004 The FreeBSD Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2004 The FreeBSD Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/kern/subr_kdb.c 139804 2005-01-06 23:35:40Z imp $");
28__FBSDID("$FreeBSD: head/sys/kern/subr_kdb.c 145727 2005-04-30 20:01:00Z dwhite $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kdb.h>
33#include <sys/kernel.h>
34#include <sys/malloc.h>
35#include <sys/pcpu.h>
36#include <sys/proc.h>
37#include <sys/smp.h>
38#include <sys/sysctl.h>
39
40#include <machine/kdb.h>
41#include <machine/pcb.h>
42
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kdb.h>
33#include <sys/kernel.h>
34#include <sys/malloc.h>
35#include <sys/pcpu.h>
36#include <sys/proc.h>
37#include <sys/smp.h>
38#include <sys/sysctl.h>
39
40#include <machine/kdb.h>
41#include <machine/pcb.h>
42
43#ifdef KDB_STOP_NMI
44#include <machine/smp.h>
45#endif
46
47/*
48 * KDB_STOP_NMI requires SMP to pick up the right dependencies
49 * (And isn't useful on UP anyway)
50 */
51#if defined(KDB_STOP_NMI) && !defined(SMP)
52#error "options KDB_STOP_NMI" requires "options SMP"
53#endif
54
43int kdb_active = 0;
44void *kdb_jmpbufp = NULL;
45struct kdb_dbbe *kdb_dbbe = NULL;
46struct pcb kdb_pcb;
47struct pcb *kdb_thrctx = NULL;
48struct thread *kdb_thread = NULL;
49struct trapframe *kdb_frame = NULL;
50
51KDB_BACKEND(null, NULL, NULL, NULL);
52SET_DECLARE(kdb_dbbe_set, struct kdb_dbbe);
53
54static int kdb_sysctl_available(SYSCTL_HANDLER_ARGS);
55static int kdb_sysctl_current(SYSCTL_HANDLER_ARGS);
56static int kdb_sysctl_enter(SYSCTL_HANDLER_ARGS);
57
58SYSCTL_NODE(_debug, OID_AUTO, kdb, CTLFLAG_RW, NULL, "KDB nodes");
59
60SYSCTL_PROC(_debug_kdb, OID_AUTO, available, CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
61 kdb_sysctl_available, "A", "list of available KDB backends");
62
63SYSCTL_PROC(_debug_kdb, OID_AUTO, current, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
64 kdb_sysctl_current, "A", "currently selected KDB backend");
65
66SYSCTL_PROC(_debug_kdb, OID_AUTO, enter, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
67 kdb_sysctl_enter, "I", "set to enter the debugger");
68
69/*
70 * Flag indicating whether or not to IPI the other CPUs to stop them on
71 * entering the debugger. Sometimes, this will result in a deadlock as
72 * stop_cpus() waits for the other cpus to stop, so we allow it to be
73 * disabled.
74 */
75#ifdef SMP
76static int kdb_stop_cpus = 1;
77SYSCTL_INT(_debug_kdb, OID_AUTO, stop_cpus, CTLTYPE_INT | CTLFLAG_RW,
78 &kdb_stop_cpus, 0, "");
79TUNABLE_INT("debug.kdb.stop_cpus", &kdb_stop_cpus);
55int kdb_active = 0;
56void *kdb_jmpbufp = NULL;
57struct kdb_dbbe *kdb_dbbe = NULL;
58struct pcb kdb_pcb;
59struct pcb *kdb_thrctx = NULL;
60struct thread *kdb_thread = NULL;
61struct trapframe *kdb_frame = NULL;
62
63KDB_BACKEND(null, NULL, NULL, NULL);
64SET_DECLARE(kdb_dbbe_set, struct kdb_dbbe);
65
66static int kdb_sysctl_available(SYSCTL_HANDLER_ARGS);
67static int kdb_sysctl_current(SYSCTL_HANDLER_ARGS);
68static int kdb_sysctl_enter(SYSCTL_HANDLER_ARGS);
69
70SYSCTL_NODE(_debug, OID_AUTO, kdb, CTLFLAG_RW, NULL, "KDB nodes");
71
72SYSCTL_PROC(_debug_kdb, OID_AUTO, available, CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
73 kdb_sysctl_available, "A", "list of available KDB backends");
74
75SYSCTL_PROC(_debug_kdb, OID_AUTO, current, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
76 kdb_sysctl_current, "A", "currently selected KDB backend");
77
78SYSCTL_PROC(_debug_kdb, OID_AUTO, enter, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
79 kdb_sysctl_enter, "I", "set to enter the debugger");
80
81/*
82 * Flag indicating whether or not to IPI the other CPUs to stop them on
83 * entering the debugger. Sometimes, this will result in a deadlock as
84 * stop_cpus() waits for the other cpus to stop, so we allow it to be
85 * disabled.
86 */
87#ifdef SMP
88static int kdb_stop_cpus = 1;
89SYSCTL_INT(_debug_kdb, OID_AUTO, stop_cpus, CTLTYPE_INT | CTLFLAG_RW,
90 &kdb_stop_cpus, 0, "");
91TUNABLE_INT("debug.kdb.stop_cpus", &kdb_stop_cpus);
92
93#ifdef KDB_STOP_NMI
94/*
95 * Provide an alternate method of stopping other CPUs. If another CPU has
96 * disabled interrupts the conventional STOP IPI will be blocked. This
97 * NMI-based stop should get through in that case.
98 */
99static int kdb_stop_cpus_with_nmi = 0;
100SYSCTL_INT(_debug_kdb, OID_AUTO, stop_cpus_with_nmi, CTLTYPE_INT | CTLFLAG_RW,
101 &kdb_stop_cpus_with_nmi, 0, "");
102TUNABLE_INT("debug.kdb.stop_cpus_with_nmi", &kdb_stop_cpus_with_nmi);
103#endif /* KDB_STOP_NMI */
104
80#endif
81
82static int
83kdb_sysctl_available(SYSCTL_HANDLER_ARGS)
84{
85 struct kdb_dbbe *be, **iter;
86 char *avail, *p;
87 ssize_t len, sz;
88 int error;
89
90 sz = 0;
91 SET_FOREACH(iter, kdb_dbbe_set) {
92 be = *iter;
93 if (be->dbbe_active == 0)
94 sz += strlen(be->dbbe_name) + 1;
95 }
96 sz++;
97 avail = malloc(sz, M_TEMP, M_WAITOK);
98 p = avail;
99 *p = '\0';
100
101 SET_FOREACH(iter, kdb_dbbe_set) {
102 be = *iter;
103 if (be->dbbe_active == 0) {
104 len = snprintf(p, sz, "%s ", be->dbbe_name);
105 p += len;
106 sz -= len;
107 }
108 }
109 KASSERT(sz >= 0, ("%s", __func__));
110 error = sysctl_handle_string(oidp, avail, 0, req);
111 free(avail, M_TEMP);
112 return (error);
113}
114
115static int
116kdb_sysctl_current(SYSCTL_HANDLER_ARGS)
117{
118 char buf[16];
119 int error;
120
121 if (kdb_dbbe != NULL) {
122 strncpy(buf, kdb_dbbe->dbbe_name, sizeof(buf));
123 buf[sizeof(buf) - 1] = '\0';
124 } else
125 *buf = '\0';
126 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
127 if (error != 0 || req->newptr == NULL)
128 return (error);
129 if (kdb_active)
130 return (EBUSY);
131 return (kdb_dbbe_select(buf));
132}
133
134static int
135kdb_sysctl_enter(SYSCTL_HANDLER_ARGS)
136{
137 int error, i;
138
139 error = sysctl_wire_old_buffer(req, sizeof(int));
140 if (error == 0) {
141 i = 0;
142 error = sysctl_handle_int(oidp, &i, 0, req);
143 }
144 if (error != 0 || req->newptr == NULL)
145 return (error);
146 if (kdb_active)
147 return (EBUSY);
148 kdb_enter("sysctl debug.kdb.enter");
149 return (0);
150}
151
152/*
153 * Solaris implements a new BREAK which is initiated by a character sequence
154 * CR ~ ^b which is similar to a familiar pattern used on Sun servers by the
155 * Remote Console.
156 *
157 * Note that this function may be called from almost anywhere, with interrupts
158 * disabled and with unknown locks held, so it must not access data other than
159 * its arguments. Its up to the caller to ensure that the state variable is
160 * consistent.
161 */
162
163#define KEY_CR 13 /* CR '\r' */
164#define KEY_TILDE 126 /* ~ */
165#define KEY_CRTLB 2 /* ^B */
166
167int
168kdb_alt_break(int key, int *state)
169{
170 int brk;
171
172 brk = 0;
173 switch (key) {
174 case KEY_CR:
175 *state = KEY_TILDE;
176 break;
177 case KEY_TILDE:
178 *state = (*state == KEY_TILDE) ? KEY_CRTLB : 0;
179 break;
180 case KEY_CRTLB:
181 if (*state == KEY_CRTLB)
182 brk = 1;
183 /* FALLTHROUGH */
184 default:
185 *state = 0;
186 break;
187 }
188 return (brk);
189}
190
191/*
192 * Print a backtrace of the calling thread. The backtrace is generated by
193 * the selected debugger, provided it supports backtraces. If no debugger
194 * is selected or the current debugger does not support backtraces, this
195 * function silently returns.
196 */
197
198void
199kdb_backtrace()
200{
201
202 if (kdb_dbbe != NULL && kdb_dbbe->dbbe_trace != NULL) {
203 printf("KDB: stack backtrace:\n");
204 kdb_dbbe->dbbe_trace();
205 }
206}
207
208/*
209 * Set/change the current backend.
210 */
211
212int
213kdb_dbbe_select(const char *name)
214{
215 struct kdb_dbbe *be, **iter;
216
217 SET_FOREACH(iter, kdb_dbbe_set) {
218 be = *iter;
219 if (be->dbbe_active == 0 && strcmp(be->dbbe_name, name) == 0) {
220 kdb_dbbe = be;
221 return (0);
222 }
223 }
224 return (EINVAL);
225}
226
227/*
228 * Enter the currently selected debugger. If a message has been provided,
229 * it is printed first. If the debugger does not support the enter method,
230 * it is entered by using breakpoint(), which enters the debugger through
231 * kdb_trap().
232 */
233
234void
235kdb_enter(const char *msg)
236{
237
238 if (kdb_dbbe != NULL && kdb_active == 0) {
239 if (msg != NULL)
240 printf("KDB: enter: %s\n", msg);
241 breakpoint();
242 }
243}
244
245/*
246 * Initialize the kernel debugger interface.
247 */
248
249void
250kdb_init()
251{
252 struct kdb_dbbe *be, **iter;
253 int cur_pri, pri;
254
255 kdb_active = 0;
256 kdb_dbbe = NULL;
257 cur_pri = -1;
258 SET_FOREACH(iter, kdb_dbbe_set) {
259 be = *iter;
260 pri = (be->dbbe_init != NULL) ? be->dbbe_init() : -1;
261 be->dbbe_active = (pri >= 0) ? 0 : -1;
262 if (pri > cur_pri) {
263 cur_pri = pri;
264 kdb_dbbe = be;
265 }
266 }
267 if (kdb_dbbe != NULL) {
268 printf("KDB: debugger backends:");
269 SET_FOREACH(iter, kdb_dbbe_set) {
270 be = *iter;
271 if (be->dbbe_active == 0)
272 printf(" %s", be->dbbe_name);
273 }
274 printf("\n");
275 printf("KDB: current backend: %s\n",
276 kdb_dbbe->dbbe_name);
277 }
278}
279
280/*
281 * Handle contexts.
282 */
283
284void *
285kdb_jmpbuf(jmp_buf new)
286{
287 void *old;
288
289 old = kdb_jmpbufp;
290 kdb_jmpbufp = new;
291 return (old);
292}
293
294void
295kdb_reenter(void)
296{
297
298 if (!kdb_active || kdb_jmpbufp == NULL)
299 return;
300
301 longjmp(kdb_jmpbufp, 1);
302 /* NOTREACHED */
303}
304
305/*
306 * Thread related support functions.
307 */
308
309struct pcb *
310kdb_thr_ctx(struct thread *thr)
105#endif
106
107static int
108kdb_sysctl_available(SYSCTL_HANDLER_ARGS)
109{
110 struct kdb_dbbe *be, **iter;
111 char *avail, *p;
112 ssize_t len, sz;
113 int error;
114
115 sz = 0;
116 SET_FOREACH(iter, kdb_dbbe_set) {
117 be = *iter;
118 if (be->dbbe_active == 0)
119 sz += strlen(be->dbbe_name) + 1;
120 }
121 sz++;
122 avail = malloc(sz, M_TEMP, M_WAITOK);
123 p = avail;
124 *p = '\0';
125
126 SET_FOREACH(iter, kdb_dbbe_set) {
127 be = *iter;
128 if (be->dbbe_active == 0) {
129 len = snprintf(p, sz, "%s ", be->dbbe_name);
130 p += len;
131 sz -= len;
132 }
133 }
134 KASSERT(sz >= 0, ("%s", __func__));
135 error = sysctl_handle_string(oidp, avail, 0, req);
136 free(avail, M_TEMP);
137 return (error);
138}
139
140static int
141kdb_sysctl_current(SYSCTL_HANDLER_ARGS)
142{
143 char buf[16];
144 int error;
145
146 if (kdb_dbbe != NULL) {
147 strncpy(buf, kdb_dbbe->dbbe_name, sizeof(buf));
148 buf[sizeof(buf) - 1] = '\0';
149 } else
150 *buf = '\0';
151 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
152 if (error != 0 || req->newptr == NULL)
153 return (error);
154 if (kdb_active)
155 return (EBUSY);
156 return (kdb_dbbe_select(buf));
157}
158
159static int
160kdb_sysctl_enter(SYSCTL_HANDLER_ARGS)
161{
162 int error, i;
163
164 error = sysctl_wire_old_buffer(req, sizeof(int));
165 if (error == 0) {
166 i = 0;
167 error = sysctl_handle_int(oidp, &i, 0, req);
168 }
169 if (error != 0 || req->newptr == NULL)
170 return (error);
171 if (kdb_active)
172 return (EBUSY);
173 kdb_enter("sysctl debug.kdb.enter");
174 return (0);
175}
176
177/*
178 * Solaris implements a new BREAK which is initiated by a character sequence
179 * CR ~ ^b which is similar to a familiar pattern used on Sun servers by the
180 * Remote Console.
181 *
182 * Note that this function may be called from almost anywhere, with interrupts
183 * disabled and with unknown locks held, so it must not access data other than
184 * its arguments. Its up to the caller to ensure that the state variable is
185 * consistent.
186 */
187
188#define KEY_CR 13 /* CR '\r' */
189#define KEY_TILDE 126 /* ~ */
190#define KEY_CRTLB 2 /* ^B */
191
192int
193kdb_alt_break(int key, int *state)
194{
195 int brk;
196
197 brk = 0;
198 switch (key) {
199 case KEY_CR:
200 *state = KEY_TILDE;
201 break;
202 case KEY_TILDE:
203 *state = (*state == KEY_TILDE) ? KEY_CRTLB : 0;
204 break;
205 case KEY_CRTLB:
206 if (*state == KEY_CRTLB)
207 brk = 1;
208 /* FALLTHROUGH */
209 default:
210 *state = 0;
211 break;
212 }
213 return (brk);
214}
215
216/*
217 * Print a backtrace of the calling thread. The backtrace is generated by
218 * the selected debugger, provided it supports backtraces. If no debugger
219 * is selected or the current debugger does not support backtraces, this
220 * function silently returns.
221 */
222
223void
224kdb_backtrace()
225{
226
227 if (kdb_dbbe != NULL && kdb_dbbe->dbbe_trace != NULL) {
228 printf("KDB: stack backtrace:\n");
229 kdb_dbbe->dbbe_trace();
230 }
231}
232
233/*
234 * Set/change the current backend.
235 */
236
237int
238kdb_dbbe_select(const char *name)
239{
240 struct kdb_dbbe *be, **iter;
241
242 SET_FOREACH(iter, kdb_dbbe_set) {
243 be = *iter;
244 if (be->dbbe_active == 0 && strcmp(be->dbbe_name, name) == 0) {
245 kdb_dbbe = be;
246 return (0);
247 }
248 }
249 return (EINVAL);
250}
251
252/*
253 * Enter the currently selected debugger. If a message has been provided,
254 * it is printed first. If the debugger does not support the enter method,
255 * it is entered by using breakpoint(), which enters the debugger through
256 * kdb_trap().
257 */
258
259void
260kdb_enter(const char *msg)
261{
262
263 if (kdb_dbbe != NULL && kdb_active == 0) {
264 if (msg != NULL)
265 printf("KDB: enter: %s\n", msg);
266 breakpoint();
267 }
268}
269
270/*
271 * Initialize the kernel debugger interface.
272 */
273
274void
275kdb_init()
276{
277 struct kdb_dbbe *be, **iter;
278 int cur_pri, pri;
279
280 kdb_active = 0;
281 kdb_dbbe = NULL;
282 cur_pri = -1;
283 SET_FOREACH(iter, kdb_dbbe_set) {
284 be = *iter;
285 pri = (be->dbbe_init != NULL) ? be->dbbe_init() : -1;
286 be->dbbe_active = (pri >= 0) ? 0 : -1;
287 if (pri > cur_pri) {
288 cur_pri = pri;
289 kdb_dbbe = be;
290 }
291 }
292 if (kdb_dbbe != NULL) {
293 printf("KDB: debugger backends:");
294 SET_FOREACH(iter, kdb_dbbe_set) {
295 be = *iter;
296 if (be->dbbe_active == 0)
297 printf(" %s", be->dbbe_name);
298 }
299 printf("\n");
300 printf("KDB: current backend: %s\n",
301 kdb_dbbe->dbbe_name);
302 }
303}
304
305/*
306 * Handle contexts.
307 */
308
309void *
310kdb_jmpbuf(jmp_buf new)
311{
312 void *old;
313
314 old = kdb_jmpbufp;
315 kdb_jmpbufp = new;
316 return (old);
317}
318
319void
320kdb_reenter(void)
321{
322
323 if (!kdb_active || kdb_jmpbufp == NULL)
324 return;
325
326 longjmp(kdb_jmpbufp, 1);
327 /* NOTREACHED */
328}
329
330/*
331 * Thread related support functions.
332 */
333
334struct pcb *
335kdb_thr_ctx(struct thread *thr)
336#ifdef KDB_STOP_NMI
337{
338 u_int cpuid;
339 struct pcpu *pc;
340
341 if (thr == curthread)
342 return &kdb_pcb;
343
344 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
345 cpuid = pc->pc_cpuid;
346 if (pc->pc_curthread == thr && (atomic_load_acq_int(&stopped_cpus) & (1 << cpuid)))
347 return &stoppcbs[cpuid];
348 }
349
350 return thr->td_pcb;
351}
352#else
311{
312 return ((thr == curthread) ? &kdb_pcb : thr->td_pcb);
313}
353{
354 return ((thr == curthread) ? &kdb_pcb : thr->td_pcb);
355}
356#endif /* KDB_STOP_NMI */
314
315struct thread *
316kdb_thr_first(void)
317{
318 struct proc *p;
319 struct thread *thr;
320
321 p = LIST_FIRST(&allproc);
322 while (p != NULL) {
323 if (p->p_sflag & PS_INMEM) {
324 thr = FIRST_THREAD_IN_PROC(p);
325 if (thr != NULL)
326 return (thr);
327 }
328 p = LIST_NEXT(p, p_list);
329 }
330 return (NULL);
331}
332
333struct thread *
334kdb_thr_from_pid(pid_t pid)
335{
336 struct proc *p;
337
338 p = LIST_FIRST(&allproc);
339 while (p != NULL) {
340 if (p->p_sflag & PS_INMEM && p->p_pid == pid)
341 return (FIRST_THREAD_IN_PROC(p));
342 p = LIST_NEXT(p, p_list);
343 }
344 return (NULL);
345}
346
347struct thread *
348kdb_thr_lookup(lwpid_t tid)
349{
350 struct thread *thr;
351
352 thr = kdb_thr_first();
353 while (thr != NULL && thr->td_tid != tid)
354 thr = kdb_thr_next(thr);
355 return (thr);
356}
357
358struct thread *
359kdb_thr_next(struct thread *thr)
360{
361 struct proc *p;
362
363 p = thr->td_proc;
364 thr = TAILQ_NEXT(thr, td_plist);
365 do {
366 if (thr != NULL)
367 return (thr);
368 p = LIST_NEXT(p, p_list);
369 if (p != NULL && (p->p_sflag & PS_INMEM))
370 thr = FIRST_THREAD_IN_PROC(p);
371 } while (p != NULL);
372 return (NULL);
373}
374
375int
376kdb_thr_select(struct thread *thr)
377{
378 if (thr == NULL)
379 return (EINVAL);
380 kdb_thread = thr;
381 kdb_thrctx = kdb_thr_ctx(thr);
382 return (0);
383}
384
385/*
386 * Enter the debugger due to a trap.
387 */
388
389int
390kdb_trap(int type, int code, struct trapframe *tf)
391{
392#ifdef SMP
393 int did_stop_cpus;
394#endif
395 int handled;
396
397 if (kdb_dbbe == NULL || kdb_dbbe->dbbe_trap == NULL)
398 return (0);
399
400 /* We reenter the debugger through kdb_reenter(). */
401 if (kdb_active)
402 return (0);
403
404 critical_enter();
405
406 kdb_active++;
407
408#ifdef SMP
409 if ((did_stop_cpus = kdb_stop_cpus) != 0)
357
358struct thread *
359kdb_thr_first(void)
360{
361 struct proc *p;
362 struct thread *thr;
363
364 p = LIST_FIRST(&allproc);
365 while (p != NULL) {
366 if (p->p_sflag & PS_INMEM) {
367 thr = FIRST_THREAD_IN_PROC(p);
368 if (thr != NULL)
369 return (thr);
370 }
371 p = LIST_NEXT(p, p_list);
372 }
373 return (NULL);
374}
375
376struct thread *
377kdb_thr_from_pid(pid_t pid)
378{
379 struct proc *p;
380
381 p = LIST_FIRST(&allproc);
382 while (p != NULL) {
383 if (p->p_sflag & PS_INMEM && p->p_pid == pid)
384 return (FIRST_THREAD_IN_PROC(p));
385 p = LIST_NEXT(p, p_list);
386 }
387 return (NULL);
388}
389
390struct thread *
391kdb_thr_lookup(lwpid_t tid)
392{
393 struct thread *thr;
394
395 thr = kdb_thr_first();
396 while (thr != NULL && thr->td_tid != tid)
397 thr = kdb_thr_next(thr);
398 return (thr);
399}
400
401struct thread *
402kdb_thr_next(struct thread *thr)
403{
404 struct proc *p;
405
406 p = thr->td_proc;
407 thr = TAILQ_NEXT(thr, td_plist);
408 do {
409 if (thr != NULL)
410 return (thr);
411 p = LIST_NEXT(p, p_list);
412 if (p != NULL && (p->p_sflag & PS_INMEM))
413 thr = FIRST_THREAD_IN_PROC(p);
414 } while (p != NULL);
415 return (NULL);
416}
417
418int
419kdb_thr_select(struct thread *thr)
420{
421 if (thr == NULL)
422 return (EINVAL);
423 kdb_thread = thr;
424 kdb_thrctx = kdb_thr_ctx(thr);
425 return (0);
426}
427
428/*
429 * Enter the debugger due to a trap.
430 */
431
432int
433kdb_trap(int type, int code, struct trapframe *tf)
434{
435#ifdef SMP
436 int did_stop_cpus;
437#endif
438 int handled;
439
440 if (kdb_dbbe == NULL || kdb_dbbe->dbbe_trap == NULL)
441 return (0);
442
443 /* We reenter the debugger through kdb_reenter(). */
444 if (kdb_active)
445 return (0);
446
447 critical_enter();
448
449 kdb_active++;
450
451#ifdef SMP
452 if ((did_stop_cpus = kdb_stop_cpus) != 0)
453 {
454#ifdef KDB_STOP_NMI
455 if(kdb_stop_cpus_with_nmi)
456 stop_cpus_nmi(PCPU_GET(other_cpus));
457 else
458#endif /* KDB_STOP_NMI */
410 stop_cpus(PCPU_GET(other_cpus));
459 stop_cpus(PCPU_GET(other_cpus));
460 }
411#endif
412
413 kdb_frame = tf;
414
415 /* Let MD code do its thing first... */
416 kdb_cpu_trap(type, code);
417
418 makectx(tf, &kdb_pcb);
419 kdb_thr_select(curthread);
420
421 handled = kdb_dbbe->dbbe_trap(type, code);
422
423#ifdef SMP
424 if (did_stop_cpus)
425 restart_cpus(stopped_cpus);
426#endif
427
428 kdb_active--;
429
430 critical_exit();
431
432 return (handled);
433}
461#endif
462
463 kdb_frame = tf;
464
465 /* Let MD code do its thing first... */
466 kdb_cpu_trap(type, code);
467
468 makectx(tf, &kdb_pcb);
469 kdb_thr_select(curthread);
470
471 handled = kdb_dbbe->dbbe_trap(type, code);
472
473#ifdef SMP
474 if (did_stop_cpus)
475 restart_cpus(stopped_cpus);
476#endif
477
478 kdb_active--;
479
480 critical_exit();
481
482 return (handled);
483}