subr_kdb.c revision 218825
1193323Sed/*-
2193323Sed * Copyright (c) 2004 The FreeBSD Project
3193323Sed * All rights reserved.
4193323Sed *
5193323Sed * Redistribution and use in source and binary forms, with or without
6193323Sed * modification, are permitted provided that the following conditions
7193323Sed * are met:
8193323Sed *
9193323Sed * 1. Redistributions of source code must retain the above copyright
10193323Sed *    notice, this list of conditions and the following disclaimer.
11193323Sed * 2. Redistributions in binary form must reproduce the above copyright
12193323Sed *    notice, this list of conditions and the following disclaimer in the
13193323Sed *    documentation and/or other materials provided with the distribution.
14193323Sed *
15193323Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16193323Sed * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17193323Sed * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18193323Sed * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19193323Sed * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20193323Sed * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21193323Sed * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22193323Sed * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23193323Sed * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24193323Sed * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25193323Sed */
26193323Sed
27193323Sed#include <sys/cdefs.h>
28193323Sed__FBSDID("$FreeBSD: head/sys/kern/subr_kdb.c 218825 2011-02-18 22:25:11Z mdf $");
29193323Sed
30193323Sed#include "opt_kdb.h"
31193323Sed#include "opt_stack.h"
32195098Sed
33195098Sed#include <sys/param.h>
34195098Sed#include <sys/systm.h>
35193323Sed#include <sys/kdb.h>
36193323Sed#include <sys/kernel.h>
37195098Sed#include <sys/malloc.h>
38193323Sed#include <sys/pcpu.h>
39193323Sed#include <sys/proc.h>
40193323Sed#include <sys/sbuf.h>
41193323Sed#include <sys/smp.h>
42193323Sed#include <sys/stack.h>
43193323Sed#include <sys/sysctl.h>
44193323Sed
45193323Sed#include <machine/kdb.h>
46193323Sed#include <machine/pcb.h>
47193323Sed
48193323Sed#ifdef SMP
49193323Sed#include <machine/smp.h>
50193323Sed#endif
51193323Sed
52193323Sedint kdb_active = 0;
53193323Sedstatic void *kdb_jmpbufp = NULL;
54193323Sedstruct kdb_dbbe *kdb_dbbe = NULL;
55193323Sedstatic struct pcb kdb_pcb;
56193323Sedstruct pcb *kdb_thrctx = NULL;
57193323Sedstruct thread *kdb_thread = NULL;
58193323Sedstruct trapframe *kdb_frame = NULL;
59193323Sed
60193323SedKDB_BACKEND(null, NULL, NULL, NULL);
61193323SedSET_DECLARE(kdb_dbbe_set, struct kdb_dbbe);
62193323Sed
63193323Sedstatic int kdb_sysctl_available(SYSCTL_HANDLER_ARGS);
64193323Sedstatic int kdb_sysctl_current(SYSCTL_HANDLER_ARGS);
65193323Sedstatic int kdb_sysctl_enter(SYSCTL_HANDLER_ARGS);
66193323Sedstatic int kdb_sysctl_panic(SYSCTL_HANDLER_ARGS);
67193323Sedstatic int kdb_sysctl_trap(SYSCTL_HANDLER_ARGS);
68193323Sedstatic int kdb_sysctl_trap_code(SYSCTL_HANDLER_ARGS);
69193323Sed
70193323SedSYSCTL_NODE(_debug, OID_AUTO, kdb, CTLFLAG_RW, NULL, "KDB nodes");
71193323Sed
72193323SedSYSCTL_PROC(_debug_kdb, OID_AUTO, available, CTLTYPE_STRING | CTLFLAG_RD, NULL,
73193323Sed    0, kdb_sysctl_available, "A", "list of available KDB backends");
74193323Sed
75193323SedSYSCTL_PROC(_debug_kdb, OID_AUTO, current, CTLTYPE_STRING | CTLFLAG_RW, NULL,
76193323Sed    0, kdb_sysctl_current, "A", "currently selected KDB backend");
77193323Sed
78193323SedSYSCTL_PROC(_debug_kdb, OID_AUTO, enter, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
79193323Sed    kdb_sysctl_enter, "I", "set to enter the debugger");
80193323Sed
81193323SedSYSCTL_PROC(_debug_kdb, OID_AUTO, panic, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
82193323Sed    kdb_sysctl_panic, "I", "set to panic the kernel");
83193323Sed
84193323SedSYSCTL_PROC(_debug_kdb, OID_AUTO, trap, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
85193323Sed    kdb_sysctl_trap, "I", "set to cause a page fault via data access");
86193323Sed
87193323SedSYSCTL_PROC(_debug_kdb, OID_AUTO, trap_code, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
88193323Sed    kdb_sysctl_trap_code, "I", "set to cause a page fault via code access");
89193323Sed
90193323Sed/*
91193323Sed * Flag indicating whether or not to IPI the other CPUs to stop them on
92193323Sed * entering the debugger.  Sometimes, this will result in a deadlock as
93193323Sed * stop_cpus() waits for the other cpus to stop, so we allow it to be
94193323Sed * disabled.  In order to maximize the chances of success, use a hard
95193323Sed * stop for that.
96193323Sed */
97193323Sed#ifdef SMP
98193323Sedstatic int kdb_stop_cpus = 1;
99193323SedSYSCTL_INT(_debug_kdb, OID_AUTO, stop_cpus, CTLFLAG_RW | CTLFLAG_TUN,
100193323Sed    &kdb_stop_cpus, 0, "stop other CPUs when entering the debugger");
101193323SedTUNABLE_INT("debug.kdb.stop_cpus", &kdb_stop_cpus);
102193323Sed#endif
103193323Sed
104193323Sed/*
105193323Sed * Flag to indicate to debuggers why the debugger was entered.
106193323Sed */
107193323Sedconst char * volatile kdb_why = KDB_WHY_UNSET;
108193323Sed
109193323Sedstatic int
110193323Sedkdb_sysctl_available(SYSCTL_HANDLER_ARGS)
111193323Sed{
112193323Sed	struct kdb_dbbe **iter;
113193323Sed	struct sbuf sbuf;
114193323Sed	int error;
115193323Sed
116193323Sed	sbuf_new_for_sysctl(&sbuf, NULL, 64, req);
117193323Sed	SET_FOREACH(iter, kdb_dbbe_set) {
118193323Sed		if ((*iter)->dbbe_active == 0)
119193323Sed			sbuf_printf(&sbuf, "%s ", (*iter)->dbbe_name);
120193323Sed	}
121193323Sed	error = sbuf_finish(&sbuf);
122193323Sed	sbuf_delete(&sbuf);
123193323Sed	return (error);
124193323Sed}
125193323Sed
126193323Sedstatic int
127193323Sedkdb_sysctl_current(SYSCTL_HANDLER_ARGS)
128193323Sed{
129193323Sed	char buf[16];
130193323Sed	int error;
131193323Sed
132193323Sed	if (kdb_dbbe != NULL)
133193323Sed		strlcpy(buf, kdb_dbbe->dbbe_name, sizeof(buf));
134193323Sed	else
135193323Sed		*buf = '\0';
136193323Sed	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
137193323Sed	if (error != 0 || req->newptr == NULL)
138193323Sed		return (error);
139193323Sed	if (kdb_active)
140193323Sed		return (EBUSY);
141193323Sed	return (kdb_dbbe_select(buf));
142193323Sed}
143193323Sed
144193323Sedstatic int
145193323Sedkdb_sysctl_enter(SYSCTL_HANDLER_ARGS)
146193323Sed{
147193323Sed	int error, i;
148193323Sed
149193323Sed	error = sysctl_wire_old_buffer(req, sizeof(int));
150193323Sed	if (error == 0) {
151193323Sed		i = 0;
152193323Sed		error = sysctl_handle_int(oidp, &i, 0, req);
153193323Sed	}
154193323Sed	if (error != 0 || req->newptr == NULL)
155193323Sed		return (error);
156193323Sed	if (kdb_active)
157193323Sed		return (EBUSY);
158193323Sed	kdb_enter(KDB_WHY_SYSCTL, "sysctl debug.kdb.enter");
159193323Sed	return (0);
160193323Sed}
161193323Sed
162193323Sedstatic int
163193323Sedkdb_sysctl_panic(SYSCTL_HANDLER_ARGS)
164193323Sed{
165193323Sed	int error, i;
166193323Sed
167193323Sed	error = sysctl_wire_old_buffer(req, sizeof(int));
168193323Sed	if (error == 0) {
169193323Sed		i = 0;
170193323Sed		error = sysctl_handle_int(oidp, &i, 0, req);
171193323Sed	}
172193323Sed	if (error != 0 || req->newptr == NULL)
173193323Sed		return (error);
174193323Sed	panic("kdb_sysctl_panic");
175193323Sed	return (0);
176193323Sed}
177193323Sed
178193323Sedstatic int
179193323Sedkdb_sysctl_trap(SYSCTL_HANDLER_ARGS)
180193323Sed{
181193323Sed	int error, i;
182193323Sed	int *addr = (int *)0x10;
183193323Sed
184193323Sed	error = sysctl_wire_old_buffer(req, sizeof(int));
185193323Sed	if (error == 0) {
186193323Sed		i = 0;
187193323Sed		error = sysctl_handle_int(oidp, &i, 0, req);
188193323Sed	}
189193323Sed	if (error != 0 || req->newptr == NULL)
190193323Sed		return (error);
191193323Sed	return (*addr);
192193323Sed}
193193323Sed
194193323Sedstatic int
195193323Sedkdb_sysctl_trap_code(SYSCTL_HANDLER_ARGS)
196193323Sed{
197193323Sed	int error, i;
198193323Sed	void (*fp)(u_int, u_int, u_int) = (void *)0xdeadc0de;
199193323Sed
200193323Sed	error = sysctl_wire_old_buffer(req, sizeof(int));
201193323Sed	if (error == 0) {
202193323Sed		i = 0;
203193323Sed		error = sysctl_handle_int(oidp, &i, 0, req);
204193323Sed	}
205193323Sed	if (error != 0 || req->newptr == NULL)
206193323Sed		return (error);
207193323Sed	(*fp)(0x11111111, 0x22222222, 0x33333333);
208193323Sed	return (0);
209193323Sed}
210193323Sed
211193323Sedvoid
212193323Sedkdb_panic(const char *msg)
213193323Sed{
214193323Sed
215193323Sed#ifdef SMP
216193323Sed	stop_cpus_hard(PCPU_GET(other_cpus));
217193323Sed#endif
218193323Sed	printf("KDB: panic\n");
219193323Sed	panic("%s", msg);
220193323Sed}
221193323Sed
222193323Sedvoid
223193323Sedkdb_reboot(void)
224193323Sed{
225193323Sed
226193323Sed	printf("KDB: reboot requested\n");
227193323Sed	shutdown_nice(0);
228193323Sed}
229193323Sed
230193323Sed/*
231193323Sed * Solaris implements a new BREAK which is initiated by a character sequence
232193323Sed * CR ~ ^b which is similar to a familiar pattern used on Sun servers by the
233193323Sed * Remote Console.
234193323Sed *
235193323Sed * Note that this function may be called from almost anywhere, with interrupts
236193323Sed * disabled and with unknown locks held, so it must not access data other than
237193323Sed * its arguments.  Its up to the caller to ensure that the state variable is
238193323Sed * consistent.
239193323Sed */
240193323Sed
241193323Sed#define	KEY_CR		13	/* CR '\r' */
242193323Sed#define	KEY_TILDE	126	/* ~ */
243193323Sed#define	KEY_CRTLB	2	/* ^B */
244193323Sed#define	KEY_CRTLP	16	/* ^P */
245193323Sed#define	KEY_CRTLR	18	/* ^R */
246193323Sed
247194178Sedint
248193323Sedkdb_alt_break(int key, int *state)
249193323Sed{
250193323Sed	int brk;
251193323Sed
252193323Sed	brk = 0;
253193323Sed	switch (*state) {
254193323Sed	case 0:
255193323Sed		if (key == KEY_CR)
256193323Sed			*state = 1;
257193323Sed		break;
258193323Sed	case 1:
259193323Sed		if (key == KEY_TILDE)
260193323Sed			*state = 2;
261193323Sed		break;
262193323Sed	case 2:
263193323Sed		if (key == KEY_CRTLB)
264193323Sed			brk = KDB_REQ_DEBUGGER;
265193323Sed		else if (key == KEY_CRTLP)
266193323Sed			brk = KDB_REQ_PANIC;
267193323Sed		else if (key == KEY_CRTLR)
268193323Sed			brk = KDB_REQ_REBOOT;
269193323Sed		*state = 0;
270193323Sed	}
271193323Sed	return (brk);
272193323Sed}
273193323Sed
274193323Sed/*
275193323Sed * Print a backtrace of the calling thread. The backtrace is generated by
276193323Sed * the selected debugger, provided it supports backtraces. If no debugger
277193323Sed * is selected or the current debugger does not support backtraces, this
278193323Sed * function silently returns.
279193323Sed */
280195098Sed
281195098Sedvoid
282195098Sedkdb_backtrace(void)
283195098Sed{
284195098Sed
285195098Sed	if (kdb_dbbe != NULL && kdb_dbbe->dbbe_trace != NULL) {
286195098Sed		printf("KDB: stack backtrace:\n");
287195098Sed		kdb_dbbe->dbbe_trace();
288193323Sed	}
289193323Sed#ifdef STACK
290193323Sed	else {
291193323Sed		struct stack st;
292193323Sed
293193323Sed		printf("KDB: stack backtrace:\n");
294193323Sed		stack_save(&st);
295193323Sed		stack_print_ddb(&st);
296193323Sed	}
297193323Sed#endif
298193323Sed}
299193323Sed
300193323Sed/*
301193323Sed * Set/change the current backend.
302193323Sed */
303193323Sed
304193323Sedint
305193323Sedkdb_dbbe_select(const char *name)
306193323Sed{
307193323Sed	struct kdb_dbbe *be, **iter;
308193323Sed
309193323Sed	SET_FOREACH(iter, kdb_dbbe_set) {
310193323Sed		be = *iter;
311193323Sed		if (be->dbbe_active == 0 && strcmp(be->dbbe_name, name) == 0) {
312193323Sed			kdb_dbbe = be;
313193323Sed			return (0);
314193323Sed		}
315193323Sed	}
316193323Sed	return (EINVAL);
317193323Sed}
318193323Sed
319193323Sed/*
320193323Sed * Enter the currently selected debugger. If a message has been provided,
321193323Sed * it is printed first. If the debugger does not support the enter method,
322193323Sed * it is entered by using breakpoint(), which enters the debugger through
323193323Sed * kdb_trap().  The 'why' argument will contain a more mechanically usable
324193323Sed * string than 'msg', and is relied upon by DDB scripting to identify the
325193323Sed * reason for entering the debugger so that the right script can be run.
326193323Sed */
327193323Sedvoid
328193323Sedkdb_enter(const char *why, const char *msg)
329193323Sed{
330193323Sed
331193323Sed	if (kdb_dbbe != NULL && kdb_active == 0) {
332193323Sed		if (msg != NULL)
333193323Sed			printf("KDB: enter: %s\n", msg);
334193323Sed		kdb_why = why;
335193323Sed		breakpoint();
336193323Sed		kdb_why = KDB_WHY_UNSET;
337193323Sed	}
338193323Sed}
339193323Sed
340193323Sed/*
341193323Sed * Initialize the kernel debugger interface.
342193323Sed */
343193323Sed
344193323Sedvoid
345193323Sedkdb_init(void)
346193323Sed{
347193323Sed	struct kdb_dbbe *be, **iter;
348193323Sed	int cur_pri, pri;
349193323Sed
350193323Sed	kdb_active = 0;
351193323Sed	kdb_dbbe = NULL;
352193323Sed	cur_pri = -1;
353193323Sed	SET_FOREACH(iter, kdb_dbbe_set) {
354193323Sed		be = *iter;
355193323Sed		pri = (be->dbbe_init != NULL) ? be->dbbe_init() : -1;
356193323Sed		be->dbbe_active = (pri >= 0) ? 0 : -1;
357193323Sed		if (pri > cur_pri) {
358193323Sed			cur_pri = pri;
359193323Sed			kdb_dbbe = be;
360193323Sed		}
361193323Sed	}
362193323Sed	if (kdb_dbbe != NULL) {
363		printf("KDB: debugger backends:");
364		SET_FOREACH(iter, kdb_dbbe_set) {
365			be = *iter;
366			if (be->dbbe_active == 0)
367				printf(" %s", be->dbbe_name);
368		}
369		printf("\n");
370		printf("KDB: current backend: %s\n",
371		    kdb_dbbe->dbbe_name);
372	}
373}
374
375/*
376 * Handle contexts.
377 */
378
379void *
380kdb_jmpbuf(jmp_buf new)
381{
382	void *old;
383
384	old = kdb_jmpbufp;
385	kdb_jmpbufp = new;
386	return (old);
387}
388
389void
390kdb_reenter(void)
391{
392
393	if (!kdb_active || kdb_jmpbufp == NULL)
394		return;
395
396	longjmp(kdb_jmpbufp, 1);
397	/* NOTREACHED */
398}
399
400/*
401 * Thread related support functions.
402 */
403
404struct pcb *
405kdb_thr_ctx(struct thread *thr)
406{
407#if defined(SMP) && defined(KDB_STOPPEDPCB)
408	struct pcpu *pc;
409#endif
410
411	if (thr == curthread)
412		return (&kdb_pcb);
413
414#if defined(SMP) && defined(KDB_STOPPEDPCB)
415	SLIST_FOREACH(pc, &cpuhead, pc_allcpu)  {
416		if (pc->pc_curthread == thr && (stopped_cpus & pc->pc_cpumask))
417			return (KDB_STOPPEDPCB(pc));
418	}
419#endif
420	return (thr->td_pcb);
421}
422
423struct thread *
424kdb_thr_first(void)
425{
426	struct proc *p;
427	struct thread *thr;
428
429	p = LIST_FIRST(&allproc);
430	while (p != NULL) {
431		if (p->p_flag & P_INMEM) {
432			thr = FIRST_THREAD_IN_PROC(p);
433			if (thr != NULL)
434				return (thr);
435		}
436		p = LIST_NEXT(p, p_list);
437	}
438	return (NULL);
439}
440
441struct thread *
442kdb_thr_from_pid(pid_t pid)
443{
444	struct proc *p;
445
446	p = LIST_FIRST(&allproc);
447	while (p != NULL) {
448		if (p->p_flag & P_INMEM && p->p_pid == pid)
449			return (FIRST_THREAD_IN_PROC(p));
450		p = LIST_NEXT(p, p_list);
451	}
452	return (NULL);
453}
454
455struct thread *
456kdb_thr_lookup(lwpid_t tid)
457{
458	struct thread *thr;
459
460	thr = kdb_thr_first();
461	while (thr != NULL && thr->td_tid != tid)
462		thr = kdb_thr_next(thr);
463	return (thr);
464}
465
466struct thread *
467kdb_thr_next(struct thread *thr)
468{
469	struct proc *p;
470
471	p = thr->td_proc;
472	thr = TAILQ_NEXT(thr, td_plist);
473	do {
474		if (thr != NULL)
475			return (thr);
476		p = LIST_NEXT(p, p_list);
477		if (p != NULL && (p->p_flag & P_INMEM))
478			thr = FIRST_THREAD_IN_PROC(p);
479	} while (p != NULL);
480	return (NULL);
481}
482
483int
484kdb_thr_select(struct thread *thr)
485{
486	if (thr == NULL)
487		return (EINVAL);
488	kdb_thread = thr;
489	kdb_thrctx = kdb_thr_ctx(thr);
490	return (0);
491}
492
493/*
494 * Enter the debugger due to a trap.
495 */
496
497int
498kdb_trap(int type, int code, struct trapframe *tf)
499{
500	struct kdb_dbbe *be;
501	register_t intr;
502#ifdef SMP
503	int did_stop_cpus;
504#endif
505	int handled;
506
507	be = kdb_dbbe;
508	if (be == NULL || be->dbbe_trap == NULL)
509		return (0);
510
511	/* We reenter the debugger through kdb_reenter(). */
512	if (kdb_active)
513		return (0);
514
515	intr = intr_disable();
516
517#ifdef SMP
518	if ((did_stop_cpus = kdb_stop_cpus) != 0)
519		stop_cpus_hard(PCPU_GET(other_cpus));
520#endif
521
522	kdb_active++;
523
524	kdb_frame = tf;
525
526	/* Let MD code do its thing first... */
527	kdb_cpu_trap(type, code);
528
529	makectx(tf, &kdb_pcb);
530	kdb_thr_select(curthread);
531
532	for (;;) {
533		handled = be->dbbe_trap(type, code);
534		if (be == kdb_dbbe)
535			break;
536		be = kdb_dbbe;
537		if (be == NULL || be->dbbe_trap == NULL)
538			break;
539		printf("Switching to %s back-end\n", be->dbbe_name);
540	}
541
542	kdb_active--;
543
544#ifdef SMP
545	if (did_stop_cpus)
546		restart_cpus(stopped_cpus);
547#endif
548
549	intr_restore(intr);
550
551	return (handled);
552}
553