xref: /freebsd-13-stable/sys/kern/subr_kdb.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2004 The FreeBSD Project
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include "opt_kdb.h"
31 #include "opt_stack.h"
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/cons.h>
36 #include <sys/kdb.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/lock.h>
40 #include <sys/pcpu.h>
41 #include <sys/proc.h>
42 #include <sys/sbuf.h>
43 #include <sys/smp.h>
44 #include <sys/stack.h>
45 #include <sys/sysctl.h>
46 
47 #include <machine/kdb.h>
48 #include <machine/pcb.h>
49 
50 #ifdef SMP
51 #include <machine/smp.h>
52 #endif
53 
54 u_char __read_frequently kdb_active = 0;
55 static void *kdb_jmpbufp = NULL;
56 struct kdb_dbbe *kdb_dbbe = NULL;
57 static struct pcb kdb_pcb;
58 struct pcb *kdb_thrctx = NULL;
59 struct thread *kdb_thread = NULL;
60 struct trapframe *kdb_frame = NULL;
61 
62 #ifdef BREAK_TO_DEBUGGER
63 #define	KDB_BREAK_TO_DEBUGGER	1
64 #else
65 #define	KDB_BREAK_TO_DEBUGGER	0
66 #endif
67 
68 #ifdef ALT_BREAK_TO_DEBUGGER
69 #define	KDB_ALT_BREAK_TO_DEBUGGER	1
70 #else
71 #define	KDB_ALT_BREAK_TO_DEBUGGER	0
72 #endif
73 
74 static int	kdb_break_to_debugger = KDB_BREAK_TO_DEBUGGER;
75 static int	kdb_alt_break_to_debugger = KDB_ALT_BREAK_TO_DEBUGGER;
76 
77 KDB_BACKEND(null, NULL, NULL, NULL, NULL);
78 
79 static int kdb_sysctl_available(SYSCTL_HANDLER_ARGS);
80 static int kdb_sysctl_current(SYSCTL_HANDLER_ARGS);
81 static int kdb_sysctl_enter(SYSCTL_HANDLER_ARGS);
82 static int kdb_sysctl_panic(SYSCTL_HANDLER_ARGS);
83 static int kdb_sysctl_panic_str(SYSCTL_HANDLER_ARGS);
84 static int kdb_sysctl_trap(SYSCTL_HANDLER_ARGS);
85 static int kdb_sysctl_trap_code(SYSCTL_HANDLER_ARGS);
86 static int kdb_sysctl_stack_overflow(SYSCTL_HANDLER_ARGS);
87 
88 static SYSCTL_NODE(_debug, OID_AUTO, kdb, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
89     "KDB nodes");
90 
91 SYSCTL_PROC(_debug_kdb, OID_AUTO, available,
92     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
93     kdb_sysctl_available, "A",
94     "list of available KDB backends");
95 
96 SYSCTL_PROC(_debug_kdb, OID_AUTO, current,
97     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
98     kdb_sysctl_current, "A",
99     "currently selected KDB backend");
100 
101 SYSCTL_PROC(_debug_kdb, OID_AUTO, enter,
102     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE, NULL, 0,
103     kdb_sysctl_enter, "I",
104     "set to enter the debugger");
105 
106 SYSCTL_PROC(_debug_kdb, OID_AUTO, panic,
107     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE, NULL, 0,
108     kdb_sysctl_panic, "I",
109     "set to panic the kernel");
110 
111 SYSCTL_PROC(_debug_kdb, OID_AUTO, panic_str,
112     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE, NULL, 0,
113     kdb_sysctl_panic_str, "A",
114     "trigger a kernel panic, using the provided string as the panic message");
115 
116 SYSCTL_PROC(_debug_kdb, OID_AUTO, trap,
117     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE, NULL, 0,
118     kdb_sysctl_trap, "I",
119     "set to cause a page fault via data access");
120 
121 SYSCTL_PROC(_debug_kdb, OID_AUTO, trap_code,
122     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE, NULL, 0,
123     kdb_sysctl_trap_code, "I",
124     "set to cause a page fault via code access");
125 
126 SYSCTL_PROC(_debug_kdb, OID_AUTO, stack_overflow,
127     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE, NULL, 0,
128     kdb_sysctl_stack_overflow, "I",
129     "set to cause a stack overflow");
130 
131 SYSCTL_INT(_debug_kdb, OID_AUTO, break_to_debugger,
132     CTLFLAG_RWTUN | CTLFLAG_SECURE,
133     &kdb_break_to_debugger, 0, "Enable break to debugger");
134 
135 SYSCTL_INT(_debug_kdb, OID_AUTO, alt_break_to_debugger,
136     CTLFLAG_RWTUN | CTLFLAG_SECURE,
137     &kdb_alt_break_to_debugger, 0, "Enable alternative break to debugger");
138 
139 /*
140  * Flag to indicate to debuggers why the debugger was entered.
141  */
142 const char * volatile kdb_why = KDB_WHY_UNSET;
143 
144 static int
kdb_sysctl_available(SYSCTL_HANDLER_ARGS)145 kdb_sysctl_available(SYSCTL_HANDLER_ARGS)
146 {
147 	struct kdb_dbbe **iter;
148 	struct sbuf sbuf;
149 	int error;
150 
151 	sbuf_new_for_sysctl(&sbuf, NULL, 64, req);
152 	SET_FOREACH(iter, kdb_dbbe_set) {
153 		if ((*iter)->dbbe_active == 0)
154 			sbuf_printf(&sbuf, "%s ", (*iter)->dbbe_name);
155 	}
156 	error = sbuf_finish(&sbuf);
157 	sbuf_delete(&sbuf);
158 	return (error);
159 }
160 
161 static int
kdb_sysctl_current(SYSCTL_HANDLER_ARGS)162 kdb_sysctl_current(SYSCTL_HANDLER_ARGS)
163 {
164 	char buf[16];
165 	int error;
166 
167 	if (kdb_dbbe != NULL)
168 		strlcpy(buf, kdb_dbbe->dbbe_name, sizeof(buf));
169 	else
170 		*buf = '\0';
171 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
172 	if (error != 0 || req->newptr == NULL)
173 		return (error);
174 	if (kdb_active)
175 		return (EBUSY);
176 	return (kdb_dbbe_select(buf));
177 }
178 
179 static int
kdb_sysctl_enter(SYSCTL_HANDLER_ARGS)180 kdb_sysctl_enter(SYSCTL_HANDLER_ARGS)
181 {
182 	int error, i;
183 
184 	error = sysctl_wire_old_buffer(req, sizeof(int));
185 	if (error == 0) {
186 		i = 0;
187 		error = sysctl_handle_int(oidp, &i, 0, req);
188 	}
189 	if (error != 0 || req->newptr == NULL)
190 		return (error);
191 	if (kdb_active)
192 		return (EBUSY);
193 	kdb_enter(KDB_WHY_SYSCTL, "sysctl debug.kdb.enter");
194 	return (0);
195 }
196 
197 static int
kdb_sysctl_panic(SYSCTL_HANDLER_ARGS)198 kdb_sysctl_panic(SYSCTL_HANDLER_ARGS)
199 {
200 	int error, i;
201 
202 	error = sysctl_wire_old_buffer(req, sizeof(int));
203 	if (error == 0) {
204 		i = 0;
205 		error = sysctl_handle_int(oidp, &i, 0, req);
206 	}
207 	if (error != 0 || req->newptr == NULL)
208 		return (error);
209 	panic("kdb_sysctl_panic");
210 	return (0);
211 }
212 
213 static int
kdb_sysctl_panic_str(SYSCTL_HANDLER_ARGS)214 kdb_sysctl_panic_str(SYSCTL_HANDLER_ARGS)
215 {
216 	int error;
217 	static char buf[256]; /* static buffer to limit mallocs when panicing */
218 
219 	*buf = '\0';
220 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
221 	if (error != 0 || req->newptr == NULL)
222 		return (error);
223 	panic("kdb_sysctl_panic: %s", buf);
224 	return (0);
225 }
226 
227 static int
kdb_sysctl_trap(SYSCTL_HANDLER_ARGS)228 kdb_sysctl_trap(SYSCTL_HANDLER_ARGS)
229 {
230 	int error, i;
231 	int *addr = (int *)0x10;
232 
233 	error = sysctl_wire_old_buffer(req, sizeof(int));
234 	if (error == 0) {
235 		i = 0;
236 		error = sysctl_handle_int(oidp, &i, 0, req);
237 	}
238 	if (error != 0 || req->newptr == NULL)
239 		return (error);
240 	return (*addr);
241 }
242 
243 static int
kdb_sysctl_trap_code(SYSCTL_HANDLER_ARGS)244 kdb_sysctl_trap_code(SYSCTL_HANDLER_ARGS)
245 {
246 	int error, i;
247 	void (*fp)(u_int, u_int, u_int) = (void *)0xdeadc0de;
248 
249 	error = sysctl_wire_old_buffer(req, sizeof(int));
250 	if (error == 0) {
251 		i = 0;
252 		error = sysctl_handle_int(oidp, &i, 0, req);
253 	}
254 	if (error != 0 || req->newptr == NULL)
255 		return (error);
256 	(*fp)(0x11111111, 0x22222222, 0x33333333);
257 	return (0);
258 }
259 
260 static void kdb_stack_overflow(volatile int *x)  __noinline;
261 static void
kdb_stack_overflow(volatile int * x)262 kdb_stack_overflow(volatile int *x)
263 {
264 
265 	if (*x > 10000000)
266 		return;
267 	kdb_stack_overflow(x);
268 	*x += PCPU_GET(cpuid) / 1000000;
269 }
270 
271 static int
kdb_sysctl_stack_overflow(SYSCTL_HANDLER_ARGS)272 kdb_sysctl_stack_overflow(SYSCTL_HANDLER_ARGS)
273 {
274 	int error, i;
275 	volatile int x;
276 
277 	error = sysctl_wire_old_buffer(req, sizeof(int));
278 	if (error == 0) {
279 		i = 0;
280 		error = sysctl_handle_int(oidp, &i, 0, req);
281 	}
282 	if (error != 0 || req->newptr == NULL)
283 		return (error);
284 	x = 0;
285 	kdb_stack_overflow(&x);
286 	return (0);
287 }
288 
289 void
kdb_panic(const char * msg)290 kdb_panic(const char *msg)
291 {
292 
293 	printf("KDB: panic\n");
294 	panic("%s", msg);
295 }
296 
297 void
kdb_reboot(void)298 kdb_reboot(void)
299 {
300 
301 	printf("KDB: reboot requested\n");
302 	shutdown_nice(0);
303 }
304 
305 /*
306  * Solaris implements a new BREAK which is initiated by a character sequence
307  * CR ~ ^b which is similar to a familiar pattern used on Sun servers by the
308  * Remote Console.
309  *
310  * Note that this function may be called from almost anywhere, with interrupts
311  * disabled and with unknown locks held, so it must not access data other than
312  * its arguments.  Its up to the caller to ensure that the state variable is
313  * consistent.
314  */
315 #define	KEY_CR		13	/* CR '\r' */
316 #define	KEY_TILDE	126	/* ~ */
317 #define	KEY_CRTLB	2	/* ^B */
318 #define	KEY_CRTLP	16	/* ^P */
319 #define	KEY_CRTLR	18	/* ^R */
320 
321 /* States of th KDB "alternate break sequence" detecting state machine. */
322 enum {
323 	KDB_ALT_BREAK_SEEN_NONE,
324 	KDB_ALT_BREAK_SEEN_CR,
325 	KDB_ALT_BREAK_SEEN_CR_TILDE,
326 };
327 
328 int
kdb_break(void)329 kdb_break(void)
330 {
331 
332 	if (!kdb_break_to_debugger)
333 		return (0);
334 	kdb_enter(KDB_WHY_BREAK, "Break to debugger");
335 	return (KDB_REQ_DEBUGGER);
336 }
337 
338 static int
kdb_alt_break_state(int key,int * state)339 kdb_alt_break_state(int key, int *state)
340 {
341 	int brk;
342 
343 	/* All states transition to KDB_ALT_BREAK_SEEN_CR on a CR. */
344 	if (key == KEY_CR) {
345 		*state = KDB_ALT_BREAK_SEEN_CR;
346 		return (0);
347 	}
348 
349 	brk = 0;
350 	switch (*state) {
351 	case KDB_ALT_BREAK_SEEN_CR:
352 		*state = KDB_ALT_BREAK_SEEN_NONE;
353 		if (key == KEY_TILDE)
354 			*state = KDB_ALT_BREAK_SEEN_CR_TILDE;
355 		break;
356 	case KDB_ALT_BREAK_SEEN_CR_TILDE:
357 		*state = KDB_ALT_BREAK_SEEN_NONE;
358 		if (key == KEY_CRTLB)
359 			brk = KDB_REQ_DEBUGGER;
360 		else if (key == KEY_CRTLP)
361 			brk = KDB_REQ_PANIC;
362 		else if (key == KEY_CRTLR)
363 			brk = KDB_REQ_REBOOT;
364 		break;
365 	case KDB_ALT_BREAK_SEEN_NONE:
366 	default:
367 		*state = KDB_ALT_BREAK_SEEN_NONE;
368 		break;
369 	}
370 	return (brk);
371 }
372 
373 static int
kdb_alt_break_internal(int key,int * state,int force_gdb)374 kdb_alt_break_internal(int key, int *state, int force_gdb)
375 {
376 	int brk;
377 
378 	if (!kdb_alt_break_to_debugger)
379 		return (0);
380 	brk = kdb_alt_break_state(key, state);
381 	switch (brk) {
382 	case KDB_REQ_DEBUGGER:
383 		if (force_gdb)
384 			kdb_dbbe_select("gdb");
385 		kdb_enter(KDB_WHY_BREAK, "Break to debugger");
386 		break;
387 
388 	case KDB_REQ_PANIC:
389 		if (force_gdb)
390 			kdb_dbbe_select("gdb");
391 		kdb_panic("Panic sequence on console");
392 		break;
393 
394 	case KDB_REQ_REBOOT:
395 		kdb_reboot();
396 		break;
397 	}
398 	return (0);
399 }
400 
401 int
kdb_alt_break(int key,int * state)402 kdb_alt_break(int key, int *state)
403 {
404 
405 	return (kdb_alt_break_internal(key, state, 0));
406 }
407 
408 /*
409  * This variation on kdb_alt_break() is used only by dcons, which has its own
410  * configuration flag to force GDB use regardless of the global KDB
411  * configuration.
412  */
413 int
kdb_alt_break_gdb(int key,int * state)414 kdb_alt_break_gdb(int key, int *state)
415 {
416 
417 	return (kdb_alt_break_internal(key, state, 1));
418 }
419 
420 /*
421  * Print a backtrace of the calling thread. The backtrace is generated by
422  * the selected debugger, provided it supports backtraces. If no debugger
423  * is selected or the current debugger does not support backtraces, this
424  * function silently returns.
425  */
426 void
kdb_backtrace(void)427 kdb_backtrace(void)
428 {
429 
430 	if (kdb_dbbe != NULL && kdb_dbbe->dbbe_trace != NULL) {
431 		printf("KDB: stack backtrace:\n");
432 		kdb_dbbe->dbbe_trace();
433 	}
434 #ifdef STACK
435 	else {
436 		struct stack st;
437 
438 		printf("KDB: stack backtrace:\n");
439 		stack_zero(&st);
440 		stack_save(&st);
441 		stack_print_ddb(&st);
442 	}
443 #endif
444 }
445 
446 /*
447  * Similar to kdb_backtrace() except that it prints a backtrace of an
448  * arbitrary thread rather than the calling thread.
449  */
450 void
kdb_backtrace_thread(struct thread * td)451 kdb_backtrace_thread(struct thread *td)
452 {
453 
454 	if (kdb_dbbe != NULL && kdb_dbbe->dbbe_trace_thread != NULL) {
455 		printf("KDB: stack backtrace of thread %d:\n", td->td_tid);
456 		kdb_dbbe->dbbe_trace_thread(td);
457 	}
458 #ifdef STACK
459 	else {
460 		struct stack st;
461 
462 		printf("KDB: stack backtrace of thread %d:\n", td->td_tid);
463 		if (stack_save_td(&st, td) == 0)
464 			stack_print_ddb(&st);
465 	}
466 #endif
467 }
468 
469 /*
470  * Set/change the current backend.
471  */
472 int
kdb_dbbe_select(const char * name)473 kdb_dbbe_select(const char *name)
474 {
475 	struct kdb_dbbe *be, **iter;
476 
477 	SET_FOREACH(iter, kdb_dbbe_set) {
478 		be = *iter;
479 		if (be->dbbe_active == 0 && strcmp(be->dbbe_name, name) == 0) {
480 			kdb_dbbe = be;
481 			return (0);
482 		}
483 	}
484 	return (EINVAL);
485 }
486 
487 /*
488  * Enter the currently selected debugger. If a message has been provided,
489  * it is printed first. If the debugger does not support the enter method,
490  * it is entered by using breakpoint(), which enters the debugger through
491  * kdb_trap().  The 'why' argument will contain a more mechanically usable
492  * string than 'msg', and is relied upon by DDB scripting to identify the
493  * reason for entering the debugger so that the right script can be run.
494  */
495 void
kdb_enter(const char * why,const char * msg)496 kdb_enter(const char *why, const char *msg)
497 {
498 
499 	if (kdb_dbbe != NULL && kdb_active == 0) {
500 		if (msg != NULL)
501 			printf("KDB: enter: %s\n", msg);
502 		kdb_why = why;
503 		breakpoint();
504 		kdb_why = KDB_WHY_UNSET;
505 	}
506 }
507 
508 /*
509  * Initialize the kernel debugger interface.
510  */
511 void
kdb_init(void)512 kdb_init(void)
513 {
514 	struct kdb_dbbe *be, **iter;
515 	int cur_pri, pri;
516 
517 	kdb_active = 0;
518 	kdb_dbbe = NULL;
519 	cur_pri = -1;
520 	SET_FOREACH(iter, kdb_dbbe_set) {
521 		be = *iter;
522 		pri = (be->dbbe_init != NULL) ? be->dbbe_init() : -1;
523 		be->dbbe_active = (pri >= 0) ? 0 : -1;
524 		if (pri > cur_pri) {
525 			cur_pri = pri;
526 			kdb_dbbe = be;
527 		}
528 	}
529 	if (kdb_dbbe != NULL) {
530 		printf("KDB: debugger backends:");
531 		SET_FOREACH(iter, kdb_dbbe_set) {
532 			be = *iter;
533 			if (be->dbbe_active == 0)
534 				printf(" %s", be->dbbe_name);
535 		}
536 		printf("\n");
537 		printf("KDB: current backend: %s\n",
538 		    kdb_dbbe->dbbe_name);
539 	}
540 }
541 
542 /*
543  * Handle contexts.
544  */
545 void *
kdb_jmpbuf(jmp_buf new)546 kdb_jmpbuf(jmp_buf new)
547 {
548 	void *old;
549 
550 	old = kdb_jmpbufp;
551 	kdb_jmpbufp = new;
552 	return (old);
553 }
554 
555 void
kdb_reenter(void)556 kdb_reenter(void)
557 {
558 
559 	if (!kdb_active || kdb_jmpbufp == NULL)
560 		return;
561 
562 	printf("KDB: reentering\n");
563 	kdb_backtrace();
564 	longjmp(kdb_jmpbufp, 1);
565 	/* NOTREACHED */
566 }
567 
568 void
kdb_reenter_silent(void)569 kdb_reenter_silent(void)
570 {
571 
572 	if (!kdb_active || kdb_jmpbufp == NULL)
573 		return;
574 
575 	longjmp(kdb_jmpbufp, 1);
576 	/* NOTREACHED */
577 }
578 
579 /*
580  * Thread-related support functions.
581  */
582 struct pcb *
kdb_thr_ctx(struct thread * thr)583 kdb_thr_ctx(struct thread *thr)
584 {
585 #if defined(SMP) && defined(KDB_STOPPEDPCB)
586 	struct pcpu *pc;
587 #endif
588 
589 	if (thr == curthread)
590 		return (&kdb_pcb);
591 
592 #if defined(SMP) && defined(KDB_STOPPEDPCB)
593 	STAILQ_FOREACH(pc, &cpuhead, pc_allcpu)  {
594 		if (pc->pc_curthread == thr &&
595 		    CPU_ISSET(pc->pc_cpuid, &stopped_cpus))
596 			return (KDB_STOPPEDPCB(pc));
597 	}
598 #endif
599 	return (thr->td_pcb);
600 }
601 
602 struct thread *
kdb_thr_first(void)603 kdb_thr_first(void)
604 {
605 	struct proc *p;
606 	struct thread *thr;
607 	u_int i;
608 
609 	/* This function may be called early. */
610 	if (pidhashtbl == NULL)
611 		return (&thread0);
612 
613 	for (i = 0; i <= pidhash; i++) {
614 		LIST_FOREACH(p, &pidhashtbl[i], p_hash) {
615 			thr = FIRST_THREAD_IN_PROC(p);
616 			if (thr != NULL)
617 				return (thr);
618 		}
619 	}
620 	return (NULL);
621 }
622 
623 struct thread *
kdb_thr_from_pid(pid_t pid)624 kdb_thr_from_pid(pid_t pid)
625 {
626 	struct proc *p;
627 
628 	LIST_FOREACH(p, PIDHASH(pid), p_hash) {
629 		if (p->p_pid == pid)
630 			return (FIRST_THREAD_IN_PROC(p));
631 	}
632 	return (NULL);
633 }
634 
635 struct thread *
kdb_thr_lookup(lwpid_t tid)636 kdb_thr_lookup(lwpid_t tid)
637 {
638 	struct thread *thr;
639 
640 	thr = kdb_thr_first();
641 	while (thr != NULL && thr->td_tid != tid)
642 		thr = kdb_thr_next(thr);
643 	return (thr);
644 }
645 
646 struct thread *
kdb_thr_next(struct thread * thr)647 kdb_thr_next(struct thread *thr)
648 {
649 	struct proc *p;
650 	u_int hash;
651 
652 	p = thr->td_proc;
653 	thr = TAILQ_NEXT(thr, td_plist);
654 	if (thr != NULL)
655 		return (thr);
656 	if (pidhashtbl == NULL)
657 		return (NULL);
658 	hash = p->p_pid & pidhash;
659 	for (;;) {
660 		p = LIST_NEXT(p, p_hash);
661 		while (p == NULL) {
662 			if (++hash > pidhash)
663 				return (NULL);
664 			p = LIST_FIRST(&pidhashtbl[hash]);
665 		}
666 		thr = FIRST_THREAD_IN_PROC(p);
667 		if (thr != NULL)
668 			return (thr);
669 	}
670 }
671 
672 int
kdb_thr_select(struct thread * thr)673 kdb_thr_select(struct thread *thr)
674 {
675 	if (thr == NULL)
676 		return (EINVAL);
677 	kdb_thread = thr;
678 	kdb_thrctx = kdb_thr_ctx(thr);
679 	return (0);
680 }
681 
682 /*
683  * Enter the debugger due to a trap.
684  */
685 int
kdb_trap(int type,int code,struct trapframe * tf)686 kdb_trap(int type, int code, struct trapframe *tf)
687 {
688 #ifdef SMP
689 	cpuset_t other_cpus;
690 #endif
691 	struct kdb_dbbe *be;
692 	register_t intr;
693 	int handled;
694 	int did_stop_cpus;
695 
696 	be = kdb_dbbe;
697 	if (be == NULL || be->dbbe_trap == NULL)
698 		return (0);
699 
700 	/* We reenter the debugger through kdb_reenter(). */
701 	if (kdb_active)
702 		return (0);
703 
704 	intr = intr_disable();
705 
706 	if (!SCHEDULER_STOPPED()) {
707 #ifdef SMP
708 		other_cpus = all_cpus;
709 		CPU_ANDNOT(&other_cpus, &other_cpus, &stopped_cpus);
710 		CPU_CLR(PCPU_GET(cpuid), &other_cpus);
711 		stop_cpus_hard(other_cpus);
712 #endif
713 		curthread->td_stopsched = 1;
714 		did_stop_cpus = 1;
715 	} else
716 		did_stop_cpus = 0;
717 
718 	kdb_active++;
719 
720 	kdb_frame = tf;
721 
722 	/* Let MD code do its thing first... */
723 	kdb_cpu_trap(type, code);
724 
725 	makectx(tf, &kdb_pcb);
726 	kdb_thr_select(curthread);
727 
728 	cngrab();
729 
730 	for (;;) {
731 		handled = be->dbbe_trap(type, code);
732 		if (be == kdb_dbbe)
733 			break;
734 		be = kdb_dbbe;
735 		if (be == NULL || be->dbbe_trap == NULL)
736 			break;
737 		printf("Switching to %s back-end\n", be->dbbe_name);
738 	}
739 
740 	cnungrab();
741 
742 	kdb_active--;
743 
744 	if (did_stop_cpus) {
745 		curthread->td_stopsched = 0;
746 #ifdef SMP
747 		CPU_AND(&other_cpus, &other_cpus, &stopped_cpus);
748 		restart_cpus(other_cpus);
749 #endif
750 	}
751 
752 	intr_restore(intr);
753 
754 	return (handled);
755 }
756