]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/subr_kdb.c
zfs: merge openzfs/zfs@10e36e176
[FreeBSD/FreeBSD.git] / sys / kern / subr_kdb.c
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 ucred *cred)
507 {
508         int error;
509
510         error = securelevel_gt(cred, kdb_enter_securelevel);
511 #ifdef MAC
512         /*
513          * Give MAC a chance to weigh in on the policy: if the securelevel is
514          * not raised, then MAC may veto the backend, otherwise MAC may
515          * explicitly grant access.
516          */
517         if (error == 0) {
518                 error = mac_kdb_check_backend(be);
519                 if (error != 0) {
520                         printf("MAC prevented execution of KDB backend: %s\n",
521                             be->dbbe_name);
522                         return (false);
523                 }
524         } else if (mac_kdb_grant_backend(be) == 0) {
525                 error = 0;
526         }
527 #endif
528         if (error != 0)
529                 printf("refusing to enter KDB with elevated securelevel\n");
530         return (error == 0);
531 }
532
533 /*
534  * Enter the currently selected debugger. If a message has been provided,
535  * it is printed first. If the debugger does not support the enter method,
536  * it is entered by using breakpoint(), which enters the debugger through
537  * kdb_trap().  The 'why' argument will contain a more mechanically usable
538  * string than 'msg', and is relied upon by DDB scripting to identify the
539  * reason for entering the debugger so that the right script can be run.
540  */
541 void
542 kdb_enter(const char *why, const char *msg)
543 {
544
545         if (kdb_dbbe != NULL && kdb_active == 0) {
546                 kdb_why = why;
547                 if (msg != NULL)
548                         printf("KDB: enter: %s\n", msg);
549                 breakpoint();
550                 kdb_why = KDB_WHY_UNSET;
551         }
552 }
553
554 /*
555  * Initialize the kernel debugger interface.
556  */
557 void
558 kdb_init(void)
559 {
560         struct kdb_dbbe *be, **iter;
561         int cur_pri, pri;
562
563         TSENTER();
564         kdb_active = 0;
565         kdb_dbbe = NULL;
566         cur_pri = -1;
567         SET_FOREACH(iter, kdb_dbbe_set) {
568                 be = *iter;
569                 pri = (be->dbbe_init != NULL) ? be->dbbe_init() : -1;
570                 be->dbbe_active = (pri >= 0) ? 0 : -1;
571                 if (pri > cur_pri) {
572                         cur_pri = pri;
573                         kdb_dbbe = be;
574                 }
575         }
576         if (kdb_dbbe != NULL) {
577                 printf("KDB: debugger backends:");
578                 SET_FOREACH(iter, kdb_dbbe_set) {
579                         be = *iter;
580                         if (be->dbbe_active == 0)
581                                 printf(" %s", be->dbbe_name);
582                 }
583                 printf("\n");
584                 printf("KDB: current backend: %s\n",
585                     kdb_dbbe->dbbe_name);
586         }
587         TSEXIT();
588 }
589
590 /*
591  * Handle contexts.
592  */
593 void *
594 kdb_jmpbuf(jmp_buf new)
595 {
596         void *old;
597
598         old = kdb_jmpbufp;
599         kdb_jmpbufp = new;
600         return (old);
601 }
602
603 void
604 kdb_reenter(void)
605 {
606
607         if (!kdb_active || kdb_jmpbufp == NULL)
608                 return;
609
610         printf("KDB: reentering\n");
611         kdb_backtrace();
612         longjmp(kdb_jmpbufp, 1);
613         /* NOTREACHED */
614 }
615
616 void
617 kdb_reenter_silent(void)
618 {
619
620         if (!kdb_active || kdb_jmpbufp == NULL)
621                 return;
622
623         longjmp(kdb_jmpbufp, 1);
624         /* NOTREACHED */
625 }
626
627 /*
628  * Thread-related support functions.
629  */
630 struct pcb *
631 kdb_thr_ctx(struct thread *thr)
632 {
633 #ifdef SMP
634         struct pcpu *pc;
635 #endif
636
637         if (thr == curthread)
638                 return (&kdb_pcb);
639
640 #ifdef SMP
641         STAILQ_FOREACH(pc, &cpuhead, pc_allcpu)  {
642                 if (pc->pc_curthread == thr &&
643                     CPU_ISSET(pc->pc_cpuid, &stopped_cpus))
644                         return (&stoppcbs[pc->pc_cpuid]);
645         }
646 #endif
647         return (thr->td_pcb);
648 }
649
650 struct thread *
651 kdb_thr_first(void)
652 {
653         struct proc *p;
654         struct thread *thr;
655         u_int i;
656
657         /* This function may be called early. */
658         if (pidhashtbl == NULL)
659                 return (&thread0);
660
661         for (i = 0; i <= pidhash; i++) {
662                 LIST_FOREACH(p, &pidhashtbl[i], p_hash) {
663                         thr = FIRST_THREAD_IN_PROC(p);
664                         if (thr != NULL)
665                                 return (thr);
666                 }
667         }
668         return (NULL);
669 }
670
671 struct thread *
672 kdb_thr_from_pid(pid_t pid)
673 {
674         struct proc *p;
675
676         LIST_FOREACH(p, PIDHASH(pid), p_hash) {
677                 if (p->p_pid == pid)
678                         return (FIRST_THREAD_IN_PROC(p));
679         }
680         return (NULL);
681 }
682
683 struct thread *
684 kdb_thr_lookup(lwpid_t tid)
685 {
686         struct thread *thr;
687
688         thr = kdb_thr_first();
689         while (thr != NULL && thr->td_tid != tid)
690                 thr = kdb_thr_next(thr);
691         return (thr);
692 }
693
694 struct thread *
695 kdb_thr_next(struct thread *thr)
696 {
697         struct proc *p;
698         u_int hash;
699
700         p = thr->td_proc;
701         thr = TAILQ_NEXT(thr, td_plist);
702         if (thr != NULL)
703                 return (thr);
704         if (pidhashtbl == NULL)
705                 return (NULL);
706         hash = p->p_pid & pidhash;
707         for (;;) {
708                 p = LIST_NEXT(p, p_hash);
709                 while (p == NULL) {
710                         if (++hash > pidhash)
711                                 return (NULL);
712                         p = LIST_FIRST(&pidhashtbl[hash]);
713                 }
714                 thr = FIRST_THREAD_IN_PROC(p);
715                 if (thr != NULL)
716                         return (thr);
717         }
718 }
719
720 int
721 kdb_thr_select(struct thread *thr)
722 {
723         if (thr == NULL)
724                 return (EINVAL);
725         kdb_thread = thr;
726         kdb_thrctx = kdb_thr_ctx(thr);
727         return (0);
728 }
729
730 /*
731  * Enter the debugger due to a trap.
732  */
733 int
734 kdb_trap(int type, int code, struct trapframe *tf)
735 {
736 #ifdef SMP
737         cpuset_t other_cpus;
738 #endif
739         struct kdb_dbbe *be;
740         register_t intr;
741         int handled;
742         int did_stop_cpus;
743
744         be = kdb_dbbe;
745         if (be == NULL || be->dbbe_trap == NULL)
746                 return (0);
747
748         /* We reenter the debugger through kdb_reenter(). */
749         if (kdb_active)
750                 return (0);
751
752         intr = intr_disable();
753
754         if (!SCHEDULER_STOPPED()) {
755 #ifdef SMP
756                 other_cpus = all_cpus;
757                 CPU_ANDNOT(&other_cpus, &other_cpus, &stopped_cpus);
758                 CPU_CLR(PCPU_GET(cpuid), &other_cpus);
759                 stop_cpus_hard(other_cpus);
760 #endif
761                 curthread->td_stopsched = 1;
762                 did_stop_cpus = 1;
763         } else
764                 did_stop_cpus = 0;
765
766         kdb_active++;
767
768         kdb_frame = tf;
769
770         /* Let MD code do its thing first... */
771         kdb_cpu_trap(type, code);
772
773         makectx(tf, &kdb_pcb);
774         kdb_thr_select(curthread);
775
776         cngrab();
777
778         for (;;) {
779                 if (!kdb_backend_permitted(be, curthread->td_ucred)) {
780                         /* Unhandled breakpoint traps are fatal. */
781                         handled = 1;
782                         break;
783                 }
784                 handled = be->dbbe_trap(type, code);
785                 if (be == kdb_dbbe)
786                         break;
787                 be = kdb_dbbe;
788                 if (be == NULL || be->dbbe_trap == NULL)
789                         break;
790                 printf("Switching to %s back-end\n", be->dbbe_name);
791         }
792
793         cnungrab();
794
795         kdb_active--;
796
797         if (did_stop_cpus) {
798                 curthread->td_stopsched = 0;
799 #ifdef SMP
800                 CPU_AND(&other_cpus, &other_cpus, &stopped_cpus);
801                 restart_cpus(other_cpus);
802 #endif
803         }
804
805         intr_restore(intr);
806
807         return (handled);
808 }