xref: /freebsd/sys/kern/subr_kdb.c (revision a02aba5f3c73d7ed377f88327fedd11f70f23353)
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$");
29 
30 #include "opt_kdb.h"
31 #include "opt_stack.h"
32 #include "opt_watchdog.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kdb.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/pcpu.h>
40 #include <sys/proc.h>
41 #include <sys/sbuf.h>
42 #include <sys/smp.h>
43 #include <sys/stack.h>
44 #include <sys/sysctl.h>
45 #ifdef SW_WATCHDOG
46 #include <sys/watchdog.h>
47 #endif
48 
49 #include <machine/kdb.h>
50 #include <machine/pcb.h>
51 
52 #ifdef SMP
53 #include <machine/smp.h>
54 #endif
55 
56 int kdb_active = 0;
57 static void *kdb_jmpbufp = NULL;
58 struct kdb_dbbe *kdb_dbbe = NULL;
59 static struct pcb kdb_pcb;
60 struct pcb *kdb_thrctx = NULL;
61 struct thread *kdb_thread = NULL;
62 struct trapframe *kdb_frame = NULL;
63 
64 #ifdef BREAK_TO_DEBUGGER
65 #define	KDB_BREAK_TO_DEBUGGER	1
66 #else
67 #define	KDB_BREAK_TO_DEBUGGER	0
68 #endif
69 
70 #ifdef ALT_BREAK_TO_DEBUGGER
71 #define	KDB_ALT_BREAK_TO_DEBUGGER	1
72 #else
73 #define	KDB_ALT_BREAK_TO_DEBUGGER	0
74 #endif
75 
76 static int	kdb_break_to_debugger = KDB_BREAK_TO_DEBUGGER;
77 static int	kdb_alt_break_to_debugger = KDB_ALT_BREAK_TO_DEBUGGER;
78 
79 KDB_BACKEND(null, NULL, NULL, NULL);
80 SET_DECLARE(kdb_dbbe_set, struct kdb_dbbe);
81 
82 static int kdb_sysctl_available(SYSCTL_HANDLER_ARGS);
83 static int kdb_sysctl_current(SYSCTL_HANDLER_ARGS);
84 static int kdb_sysctl_enter(SYSCTL_HANDLER_ARGS);
85 static int kdb_sysctl_panic(SYSCTL_HANDLER_ARGS);
86 static int kdb_sysctl_trap(SYSCTL_HANDLER_ARGS);
87 static int kdb_sysctl_trap_code(SYSCTL_HANDLER_ARGS);
88 
89 SYSCTL_NODE(_debug, OID_AUTO, kdb, CTLFLAG_RW, NULL, "KDB nodes");
90 
91 SYSCTL_PROC(_debug_kdb, OID_AUTO, available, CTLTYPE_STRING | CTLFLAG_RD, NULL,
92     0, kdb_sysctl_available, "A", "list of available KDB backends");
93 
94 SYSCTL_PROC(_debug_kdb, OID_AUTO, current, CTLTYPE_STRING | CTLFLAG_RW, NULL,
95     0, kdb_sysctl_current, "A", "currently selected KDB backend");
96 
97 SYSCTL_PROC(_debug_kdb, OID_AUTO, enter, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
98     kdb_sysctl_enter, "I", "set to enter the debugger");
99 
100 SYSCTL_PROC(_debug_kdb, OID_AUTO, panic, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
101     kdb_sysctl_panic, "I", "set to panic the kernel");
102 
103 SYSCTL_PROC(_debug_kdb, OID_AUTO, trap, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
104     kdb_sysctl_trap, "I", "set to cause a page fault via data access");
105 
106 SYSCTL_PROC(_debug_kdb, OID_AUTO, trap_code, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
107     kdb_sysctl_trap_code, "I", "set to cause a page fault via code access");
108 
109 SYSCTL_INT(_debug_kdb, OID_AUTO, break_to_debugger, CTLTYPE_INT | CTLFLAG_RW |
110     CTLFLAG_TUN, &kdb_break_to_debugger, 0, "Enable break to debugger");
111 TUNABLE_INT("debug.kdb.break_to_debugger", &kdb_break_to_debugger);
112 
113 SYSCTL_INT(_debug_kdb, OID_AUTO, alt_break_to_debugger, CTLTYPE_INT |
114     CTLFLAG_RW | CTLFLAG_TUN, &kdb_alt_break_to_debugger, 0,
115     "Enable alternative break to debugger");
116 TUNABLE_INT("debug.kdb.alt_break_to_debugger", &kdb_alt_break_to_debugger);
117 
118 /*
119  * Flag to indicate to debuggers why the debugger was entered.
120  */
121 const char * volatile kdb_why = KDB_WHY_UNSET;
122 
123 static int
124 kdb_sysctl_available(SYSCTL_HANDLER_ARGS)
125 {
126 	struct kdb_dbbe **iter;
127 	struct sbuf sbuf;
128 	int error;
129 
130 	sbuf_new_for_sysctl(&sbuf, NULL, 64, req);
131 	SET_FOREACH(iter, kdb_dbbe_set) {
132 		if ((*iter)->dbbe_active == 0)
133 			sbuf_printf(&sbuf, "%s ", (*iter)->dbbe_name);
134 	}
135 	error = sbuf_finish(&sbuf);
136 	sbuf_delete(&sbuf);
137 	return (error);
138 }
139 
140 static int
141 kdb_sysctl_current(SYSCTL_HANDLER_ARGS)
142 {
143 	char buf[16];
144 	int error;
145 
146 	if (kdb_dbbe != NULL)
147 		strlcpy(buf, kdb_dbbe->dbbe_name, sizeof(buf));
148 	else
149 		*buf = '\0';
150 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
151 	if (error != 0 || req->newptr == NULL)
152 		return (error);
153 	if (kdb_active)
154 		return (EBUSY);
155 	return (kdb_dbbe_select(buf));
156 }
157 
158 static int
159 kdb_sysctl_enter(SYSCTL_HANDLER_ARGS)
160 {
161 	int error, i;
162 
163 	error = sysctl_wire_old_buffer(req, sizeof(int));
164 	if (error == 0) {
165 		i = 0;
166 		error = sysctl_handle_int(oidp, &i, 0, req);
167 	}
168 	if (error != 0 || req->newptr == NULL)
169 		return (error);
170 	if (kdb_active)
171 		return (EBUSY);
172 	kdb_enter(KDB_WHY_SYSCTL, "sysctl debug.kdb.enter");
173 	return (0);
174 }
175 
176 static int
177 kdb_sysctl_panic(SYSCTL_HANDLER_ARGS)
178 {
179 	int error, i;
180 
181 	error = sysctl_wire_old_buffer(req, sizeof(int));
182 	if (error == 0) {
183 		i = 0;
184 		error = sysctl_handle_int(oidp, &i, 0, req);
185 	}
186 	if (error != 0 || req->newptr == NULL)
187 		return (error);
188 	panic("kdb_sysctl_panic");
189 	return (0);
190 }
191 
192 static int
193 kdb_sysctl_trap(SYSCTL_HANDLER_ARGS)
194 {
195 	int error, i;
196 	int *addr = (int *)0x10;
197 
198 	error = sysctl_wire_old_buffer(req, sizeof(int));
199 	if (error == 0) {
200 		i = 0;
201 		error = sysctl_handle_int(oidp, &i, 0, req);
202 	}
203 	if (error != 0 || req->newptr == NULL)
204 		return (error);
205 	return (*addr);
206 }
207 
208 static int
209 kdb_sysctl_trap_code(SYSCTL_HANDLER_ARGS)
210 {
211 	int error, i;
212 	void (*fp)(u_int, u_int, u_int) = (void *)0xdeadc0de;
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 	(*fp)(0x11111111, 0x22222222, 0x33333333);
222 	return (0);
223 }
224 
225 void
226 kdb_panic(const char *msg)
227 {
228 #ifdef SMP
229 	cpuset_t other_cpus;
230 
231 	other_cpus = all_cpus;
232 	CPU_CLR(PCPU_GET(cpuid), &other_cpus);
233 	stop_cpus_hard(other_cpus);
234 #endif
235 	printf("KDB: panic\n");
236 	panic("%s", msg);
237 }
238 
239 void
240 kdb_reboot(void)
241 {
242 
243 	printf("KDB: reboot requested\n");
244 	shutdown_nice(0);
245 }
246 
247 /*
248  * Solaris implements a new BREAK which is initiated by a character sequence
249  * CR ~ ^b which is similar to a familiar pattern used on Sun servers by the
250  * Remote Console.
251  *
252  * Note that this function may be called from almost anywhere, with interrupts
253  * disabled and with unknown locks held, so it must not access data other than
254  * its arguments.  Its up to the caller to ensure that the state variable is
255  * consistent.
256  */
257 
258 #define	KEY_CR		13	/* CR '\r' */
259 #define	KEY_TILDE	126	/* ~ */
260 #define	KEY_CRTLB	2	/* ^B */
261 #define	KEY_CRTLP	16	/* ^P */
262 #define	KEY_CRTLR	18	/* ^R */
263 
264 /* States of th KDB "alternate break sequence" detecting state machine. */
265 enum {
266 	KDB_ALT_BREAK_SEEN_NONE,
267 	KDB_ALT_BREAK_SEEN_CR,
268 	KDB_ALT_BREAK_SEEN_CR_TILDE,
269 };
270 
271 int
272 kdb_break(void)
273 {
274 
275 	if (!kdb_break_to_debugger)
276 		return (0);
277 	kdb_enter(KDB_WHY_BREAK, "Break to debugger");
278 	return (KDB_REQ_DEBUGGER);
279 }
280 
281 static int
282 kdb_alt_break_state(int key, int *state)
283 {
284 	int brk;
285 
286 	/* All states transition to KDB_ALT_BREAK_SEEN_CR on a CR. */
287 	if (key == KEY_CR) {
288 		*state = KDB_ALT_BREAK_SEEN_CR;
289 		return (0);
290 	}
291 
292 	brk = 0;
293 	switch (*state) {
294 	case KDB_ALT_BREAK_SEEN_CR:
295 		*state = KDB_ALT_BREAK_SEEN_NONE;
296 		if (key == KEY_TILDE)
297 			*state = KDB_ALT_BREAK_SEEN_CR_TILDE;
298 		break;
299 	case KDB_ALT_BREAK_SEEN_CR_TILDE:
300 		*state = KDB_ALT_BREAK_SEEN_NONE;
301 		if (key == KEY_CRTLB)
302 			brk = KDB_REQ_DEBUGGER;
303 		else if (key == KEY_CRTLP)
304 			brk = KDB_REQ_PANIC;
305 		else if (key == KEY_CRTLR)
306 			brk = KDB_REQ_REBOOT;
307 		break;
308 	case KDB_ALT_BREAK_SEEN_NONE:
309 	default:
310 		*state = KDB_ALT_BREAK_SEEN_NONE;
311 		break;
312 	}
313 	return (brk);
314 }
315 
316 static int
317 kdb_alt_break_internal(int key, int *state, int force_gdb)
318 {
319 	int brk;
320 
321 	if (!kdb_alt_break_to_debugger)
322 		return (0);
323 	brk = kdb_alt_break_state(key, state);
324 	switch (brk) {
325 	case KDB_REQ_DEBUGGER:
326 		if (force_gdb)
327 			kdb_dbbe_select("gdb");
328 		kdb_enter(KDB_WHY_BREAK, "Break to debugger");
329 		break;
330 
331 	case KDB_REQ_PANIC:
332 		if (force_gdb)
333 			kdb_dbbe_select("gdb");
334 		kdb_panic("Panic sequence on console");
335 		break;
336 
337 	case KDB_REQ_REBOOT:
338 		kdb_reboot();
339 		break;
340 	}
341 	return (0);
342 }
343 
344 int
345 kdb_alt_break(int key, int *state)
346 {
347 
348 	return (kdb_alt_break_internal(key, state, 0));
349 }
350 
351 /*
352  * This variation on kdb_alt_break() is used only by dcons, which has its own
353  * configuration flag to force GDB use regardless of the global KDB
354  * configuration.
355  */
356 int
357 kdb_alt_break_gdb(int key, int *state)
358 {
359 
360 	return (kdb_alt_break_internal(key, state, 1));
361 }
362 
363 /*
364  * Print a backtrace of the calling thread. The backtrace is generated by
365  * the selected debugger, provided it supports backtraces. If no debugger
366  * is selected or the current debugger does not support backtraces, this
367  * function silently returns.
368  */
369 
370 void
371 kdb_backtrace(void)
372 {
373 
374 	if (kdb_dbbe != NULL && kdb_dbbe->dbbe_trace != NULL) {
375 		printf("KDB: stack backtrace:\n");
376 		kdb_dbbe->dbbe_trace();
377 	}
378 #ifdef STACK
379 	else {
380 		struct stack st;
381 
382 		printf("KDB: stack backtrace:\n");
383 		stack_save(&st);
384 		stack_print_ddb(&st);
385 	}
386 #endif
387 }
388 
389 /*
390  * Set/change the current backend.
391  */
392 
393 int
394 kdb_dbbe_select(const char *name)
395 {
396 	struct kdb_dbbe *be, **iter;
397 
398 	SET_FOREACH(iter, kdb_dbbe_set) {
399 		be = *iter;
400 		if (be->dbbe_active == 0 && strcmp(be->dbbe_name, name) == 0) {
401 			kdb_dbbe = be;
402 			return (0);
403 		}
404 	}
405 	return (EINVAL);
406 }
407 
408 /*
409  * Enter the currently selected debugger. If a message has been provided,
410  * it is printed first. If the debugger does not support the enter method,
411  * it is entered by using breakpoint(), which enters the debugger through
412  * kdb_trap().  The 'why' argument will contain a more mechanically usable
413  * string than 'msg', and is relied upon by DDB scripting to identify the
414  * reason for entering the debugger so that the right script can be run.
415  */
416 void
417 kdb_enter(const char *why, const char *msg)
418 {
419 
420 	if (kdb_dbbe != NULL && kdb_active == 0) {
421 		if (msg != NULL)
422 			printf("KDB: enter: %s\n", msg);
423 		kdb_why = why;
424 		breakpoint();
425 		kdb_why = KDB_WHY_UNSET;
426 	}
427 }
428 
429 /*
430  * Initialize the kernel debugger interface.
431  */
432 
433 void
434 kdb_init(void)
435 {
436 	struct kdb_dbbe *be, **iter;
437 	int cur_pri, pri;
438 
439 	kdb_active = 0;
440 	kdb_dbbe = NULL;
441 	cur_pri = -1;
442 	SET_FOREACH(iter, kdb_dbbe_set) {
443 		be = *iter;
444 		pri = (be->dbbe_init != NULL) ? be->dbbe_init() : -1;
445 		be->dbbe_active = (pri >= 0) ? 0 : -1;
446 		if (pri > cur_pri) {
447 			cur_pri = pri;
448 			kdb_dbbe = be;
449 		}
450 	}
451 	if (kdb_dbbe != NULL) {
452 		printf("KDB: debugger backends:");
453 		SET_FOREACH(iter, kdb_dbbe_set) {
454 			be = *iter;
455 			if (be->dbbe_active == 0)
456 				printf(" %s", be->dbbe_name);
457 		}
458 		printf("\n");
459 		printf("KDB: current backend: %s\n",
460 		    kdb_dbbe->dbbe_name);
461 	}
462 }
463 
464 /*
465  * Handle contexts.
466  */
467 
468 void *
469 kdb_jmpbuf(jmp_buf new)
470 {
471 	void *old;
472 
473 	old = kdb_jmpbufp;
474 	kdb_jmpbufp = new;
475 	return (old);
476 }
477 
478 void
479 kdb_reenter(void)
480 {
481 
482 	if (!kdb_active || kdb_jmpbufp == NULL)
483 		return;
484 
485 	longjmp(kdb_jmpbufp, 1);
486 	/* NOTREACHED */
487 }
488 
489 /*
490  * Thread related support functions.
491  */
492 
493 struct pcb *
494 kdb_thr_ctx(struct thread *thr)
495 {
496 #if defined(SMP) && defined(KDB_STOPPEDPCB)
497 	struct pcpu *pc;
498 #endif
499 
500 	if (thr == curthread)
501 		return (&kdb_pcb);
502 
503 #if defined(SMP) && defined(KDB_STOPPEDPCB)
504 	STAILQ_FOREACH(pc, &cpuhead, pc_allcpu)  {
505 		if (pc->pc_curthread == thr &&
506 		    CPU_ISSET(pc->pc_cpuid, &stopped_cpus))
507 			return (KDB_STOPPEDPCB(pc));
508 	}
509 #endif
510 	return (thr->td_pcb);
511 }
512 
513 struct thread *
514 kdb_thr_first(void)
515 {
516 	struct proc *p;
517 	struct thread *thr;
518 
519 	p = LIST_FIRST(&allproc);
520 	while (p != NULL) {
521 		if (p->p_flag & P_INMEM) {
522 			thr = FIRST_THREAD_IN_PROC(p);
523 			if (thr != NULL)
524 				return (thr);
525 		}
526 		p = LIST_NEXT(p, p_list);
527 	}
528 	return (NULL);
529 }
530 
531 struct thread *
532 kdb_thr_from_pid(pid_t pid)
533 {
534 	struct proc *p;
535 
536 	p = LIST_FIRST(&allproc);
537 	while (p != NULL) {
538 		if (p->p_flag & P_INMEM && p->p_pid == pid)
539 			return (FIRST_THREAD_IN_PROC(p));
540 		p = LIST_NEXT(p, p_list);
541 	}
542 	return (NULL);
543 }
544 
545 struct thread *
546 kdb_thr_lookup(lwpid_t tid)
547 {
548 	struct thread *thr;
549 
550 	thr = kdb_thr_first();
551 	while (thr != NULL && thr->td_tid != tid)
552 		thr = kdb_thr_next(thr);
553 	return (thr);
554 }
555 
556 struct thread *
557 kdb_thr_next(struct thread *thr)
558 {
559 	struct proc *p;
560 
561 	p = thr->td_proc;
562 	thr = TAILQ_NEXT(thr, td_plist);
563 	do {
564 		if (thr != NULL)
565 			return (thr);
566 		p = LIST_NEXT(p, p_list);
567 		if (p != NULL && (p->p_flag & P_INMEM))
568 			thr = FIRST_THREAD_IN_PROC(p);
569 	} while (p != NULL);
570 	return (NULL);
571 }
572 
573 int
574 kdb_thr_select(struct thread *thr)
575 {
576 	if (thr == NULL)
577 		return (EINVAL);
578 	kdb_thread = thr;
579 	kdb_thrctx = kdb_thr_ctx(thr);
580 	return (0);
581 }
582 
583 /*
584  * Enter the debugger due to a trap.
585  */
586 
587 int
588 kdb_trap(int type, int code, struct trapframe *tf)
589 {
590 #ifdef SMP
591 	cpuset_t other_cpus;
592 #endif
593 	struct kdb_dbbe *be;
594 #ifdef SW_WATCHDOG
595 	u_int wdoglvt;
596 #endif
597 	register_t intr;
598 	int handled;
599 
600 	be = kdb_dbbe;
601 	if (be == NULL || be->dbbe_trap == NULL)
602 		return (0);
603 
604 	/* We reenter the debugger through kdb_reenter(). */
605 	if (kdb_active)
606 		return (0);
607 
608 	intr = intr_disable();
609 
610 #ifdef SW_WATCHDOG
611 	wdoglvt = wdog_kern_last_timeout();
612 	wdog_kern_pat(WD_TO_NEVER);
613 #endif
614 #ifdef SMP
615 	other_cpus = all_cpus;
616 	CPU_CLR(PCPU_GET(cpuid), &other_cpus);
617 	stop_cpus_hard(other_cpus);
618 #endif
619 
620 	kdb_active++;
621 
622 	kdb_frame = tf;
623 
624 	/* Let MD code do its thing first... */
625 	kdb_cpu_trap(type, code);
626 
627 	makectx(tf, &kdb_pcb);
628 	kdb_thr_select(curthread);
629 
630 	for (;;) {
631 		handled = be->dbbe_trap(type, code);
632 		if (be == kdb_dbbe)
633 			break;
634 		be = kdb_dbbe;
635 		if (be == NULL || be->dbbe_trap == NULL)
636 			break;
637 		printf("Switching to %s back-end\n", be->dbbe_name);
638 	}
639 
640 	kdb_active--;
641 
642 #ifdef SMP
643 	restart_cpus(stopped_cpus);
644 #endif
645 #ifdef SW_WATCHDOG
646 	wdog_kern_pat(wdoglvt);
647 #endif
648 
649 	intr_restore(intr);
650 
651 	return (handled);
652 }
653