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