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