xref: /freebsd/sys/kern/subr_kdb.c (revision 7dfd9569a2f0637fb9a48157b1c1bfe5709faee3)
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 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kdb.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/pcpu.h>
38 #include <sys/proc.h>
39 #include <sys/smp.h>
40 #include <sys/sysctl.h>
41 
42 #include <machine/kdb.h>
43 #include <machine/pcb.h>
44 
45 #ifdef SMP
46 #include <machine/smp.h>
47 #endif
48 
49 int kdb_active = 0;
50 void *kdb_jmpbufp = NULL;
51 struct kdb_dbbe *kdb_dbbe = NULL;
52 struct pcb kdb_pcb;
53 struct pcb *kdb_thrctx = NULL;
54 struct thread *kdb_thread = NULL;
55 struct trapframe *kdb_frame = NULL;
56 
57 KDB_BACKEND(null, NULL, NULL, NULL);
58 SET_DECLARE(kdb_dbbe_set, struct kdb_dbbe);
59 
60 static int kdb_sysctl_available(SYSCTL_HANDLER_ARGS);
61 static int kdb_sysctl_current(SYSCTL_HANDLER_ARGS);
62 static int kdb_sysctl_enter(SYSCTL_HANDLER_ARGS);
63 static int kdb_sysctl_panic(SYSCTL_HANDLER_ARGS);
64 static int kdb_sysctl_trap(SYSCTL_HANDLER_ARGS);
65 
66 SYSCTL_NODE(_debug, OID_AUTO, kdb, CTLFLAG_RW, NULL, "KDB nodes");
67 
68 SYSCTL_PROC(_debug_kdb, OID_AUTO, available, CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
69     kdb_sysctl_available, "A", "list of available KDB backends");
70 
71 SYSCTL_PROC(_debug_kdb, OID_AUTO, current, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
72     kdb_sysctl_current, "A", "currently selected KDB backend");
73 
74 SYSCTL_PROC(_debug_kdb, OID_AUTO, enter, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
75     kdb_sysctl_enter, "I", "set to enter the debugger");
76 
77 SYSCTL_PROC(_debug_kdb, OID_AUTO, panic, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
78     kdb_sysctl_panic, "I", "set to panic the kernel");
79 
80 SYSCTL_PROC(_debug_kdb, OID_AUTO, trap, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
81     kdb_sysctl_trap, "I", "set cause a page fault");
82 
83 /*
84  * Flag indicating whether or not to IPI the other CPUs to stop them on
85  * entering the debugger.  Sometimes, this will result in a deadlock as
86  * stop_cpus() waits for the other cpus to stop, so we allow it to be
87  * disabled.
88  */
89 #ifdef SMP
90 static int kdb_stop_cpus = 1;
91 SYSCTL_INT(_debug_kdb, OID_AUTO, stop_cpus, CTLTYPE_INT | CTLFLAG_RW,
92     &kdb_stop_cpus, 0, "");
93 TUNABLE_INT("debug.kdb.stop_cpus", &kdb_stop_cpus);
94 #endif
95 
96 static int
97 kdb_sysctl_available(SYSCTL_HANDLER_ARGS)
98 {
99 	struct kdb_dbbe *be, **iter;
100 	char *avail, *p;
101 	ssize_t len, sz;
102 	int error;
103 
104 	sz = 0;
105 	SET_FOREACH(iter, kdb_dbbe_set) {
106 		be = *iter;
107 		if (be->dbbe_active == 0)
108 			sz += strlen(be->dbbe_name) + 1;
109 	}
110 	sz++;
111 	avail = malloc(sz, M_TEMP, M_WAITOK);
112 	p = avail;
113 	*p = '\0';
114 
115 	SET_FOREACH(iter, kdb_dbbe_set) {
116 		be = *iter;
117 		if (be->dbbe_active == 0) {
118 			len = snprintf(p, sz, "%s ", be->dbbe_name);
119 			p += len;
120 			sz -= len;
121 		}
122 	}
123 	KASSERT(sz >= 0, ("%s", __func__));
124 	error = sysctl_handle_string(oidp, avail, 0, req);
125 	free(avail, M_TEMP);
126 	return (error);
127 }
128 
129 static int
130 kdb_sysctl_current(SYSCTL_HANDLER_ARGS)
131 {
132 	char buf[16];
133 	int error;
134 
135 	if (kdb_dbbe != NULL) {
136 		strncpy(buf, kdb_dbbe->dbbe_name, sizeof(buf));
137 		buf[sizeof(buf) - 1] = '\0';
138 	} else
139 		*buf = '\0';
140 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
141 	if (error != 0 || req->newptr == NULL)
142 		return (error);
143 	if (kdb_active)
144 		return (EBUSY);
145 	return (kdb_dbbe_select(buf));
146 }
147 
148 static int
149 kdb_sysctl_enter(SYSCTL_HANDLER_ARGS)
150 {
151 	int error, i;
152 
153 	error = sysctl_wire_old_buffer(req, sizeof(int));
154 	if (error == 0) {
155 		i = 0;
156 		error = sysctl_handle_int(oidp, &i, 0, req);
157 	}
158 	if (error != 0 || req->newptr == NULL)
159 		return (error);
160 	if (kdb_active)
161 		return (EBUSY);
162 	kdb_enter("sysctl debug.kdb.enter");
163 	return (0);
164 }
165 
166 static int
167 kdb_sysctl_panic(SYSCTL_HANDLER_ARGS)
168 {
169 	int error, i;
170 
171 	error = sysctl_wire_old_buffer(req, sizeof(int));
172 	if (error == 0) {
173 		i = 0;
174 		error = sysctl_handle_int(oidp, &i, 0, req);
175 	}
176 	if (error != 0 || req->newptr == NULL)
177 		return (error);
178 	panic("kdb_sysctl_panic");
179 	return (0);
180 }
181 
182 static int
183 kdb_sysctl_trap(SYSCTL_HANDLER_ARGS)
184 {
185 	int error, i;
186 	int *addr = (int *)0x10;
187 
188 	error = sysctl_wire_old_buffer(req, sizeof(int));
189 	if (error == 0) {
190 		i = 0;
191 		error = sysctl_handle_int(oidp, &i, 0, req);
192 	}
193 	if (error != 0 || req->newptr == NULL)
194 		return (error);
195 	return (*addr);
196 }
197 
198 /*
199  * Solaris implements a new BREAK which is initiated by a character sequence
200  * CR ~ ^b which is similar to a familiar pattern used on Sun servers by the
201  * Remote Console.
202  *
203  * Note that this function may be called from almost anywhere, with interrupts
204  * disabled and with unknown locks held, so it must not access data other than
205  * its arguments.  Its up to the caller to ensure that the state variable is
206  * consistent.
207  */
208 
209 #define	KEY_CR		13	/* CR '\r' */
210 #define	KEY_TILDE	126	/* ~ */
211 #define	KEY_CRTLB	2	/* ^B */
212 
213 int
214 kdb_alt_break(int key, int *state)
215 {
216 	int brk;
217 
218 	brk = 0;
219 	switch (key) {
220 	case KEY_CR:
221 		*state = KEY_TILDE;
222 		break;
223 	case KEY_TILDE:
224 		*state = (*state == KEY_TILDE) ? KEY_CRTLB : 0;
225 		break;
226 	case KEY_CRTLB:
227 		if (*state == KEY_CRTLB)
228 			brk = 1;
229 		/* FALLTHROUGH */
230 	default:
231 		*state = 0;
232 		break;
233 	}
234 	return (brk);
235 }
236 
237 /*
238  * Print a backtrace of the calling thread. The backtrace is generated by
239  * the selected debugger, provided it supports backtraces. If no debugger
240  * is selected or the current debugger does not support backtraces, this
241  * function silently returns.
242  */
243 
244 void
245 kdb_backtrace()
246 {
247 
248 	if (kdb_dbbe != NULL && kdb_dbbe->dbbe_trace != NULL) {
249 		printf("KDB: stack backtrace:\n");
250 		kdb_dbbe->dbbe_trace();
251 	}
252 }
253 
254 /*
255  * Set/change the current backend.
256  */
257 
258 int
259 kdb_dbbe_select(const char *name)
260 {
261 	struct kdb_dbbe *be, **iter;
262 
263 	SET_FOREACH(iter, kdb_dbbe_set) {
264 		be = *iter;
265 		if (be->dbbe_active == 0 && strcmp(be->dbbe_name, name) == 0) {
266 			kdb_dbbe = be;
267 			return (0);
268 		}
269 	}
270 	return (EINVAL);
271 }
272 
273 /*
274  * Enter the currently selected debugger. If a message has been provided,
275  * it is printed first. If the debugger does not support the enter method,
276  * it is entered by using breakpoint(), which enters the debugger through
277  * kdb_trap().
278  */
279 
280 void
281 kdb_enter(const char *msg)
282 {
283 
284 	if (kdb_dbbe != NULL && kdb_active == 0) {
285 		if (msg != NULL)
286 			printf("KDB: enter: %s\n", msg);
287 		breakpoint();
288 	}
289 }
290 
291 /*
292  * Initialize the kernel debugger interface.
293  */
294 
295 void
296 kdb_init()
297 {
298 	struct kdb_dbbe *be, **iter;
299 	int cur_pri, pri;
300 
301 	kdb_active = 0;
302 	kdb_dbbe = NULL;
303 	cur_pri = -1;
304 	SET_FOREACH(iter, kdb_dbbe_set) {
305 		be = *iter;
306 		pri = (be->dbbe_init != NULL) ? be->dbbe_init() : -1;
307 		be->dbbe_active = (pri >= 0) ? 0 : -1;
308 		if (pri > cur_pri) {
309 			cur_pri = pri;
310 			kdb_dbbe = be;
311 		}
312 	}
313 	if (kdb_dbbe != NULL) {
314 		printf("KDB: debugger backends:");
315 		SET_FOREACH(iter, kdb_dbbe_set) {
316 			be = *iter;
317 			if (be->dbbe_active == 0)
318 				printf(" %s", be->dbbe_name);
319 		}
320 		printf("\n");
321 		printf("KDB: current backend: %s\n",
322 		    kdb_dbbe->dbbe_name);
323 	}
324 }
325 
326 /*
327  * Handle contexts.
328  */
329 
330 void *
331 kdb_jmpbuf(jmp_buf new)
332 {
333 	void *old;
334 
335 	old = kdb_jmpbufp;
336 	kdb_jmpbufp = new;
337 	return (old);
338 }
339 
340 void
341 kdb_reenter(void)
342 {
343 
344 	if (!kdb_active || kdb_jmpbufp == NULL)
345 		return;
346 
347 	longjmp(kdb_jmpbufp, 1);
348 	/* NOTREACHED */
349 }
350 
351 /*
352  * Thread related support functions.
353  */
354 
355 struct pcb *
356 kdb_thr_ctx(struct thread *thr)
357 {
358 #if defined(SMP) && defined(KDB_STOPPEDPCB)
359 	struct pcpu *pc;
360 #endif
361 
362 	if (thr == curthread)
363 		return (&kdb_pcb);
364 
365 #if defined(SMP) && defined(KDB_STOPPEDPCB)
366 	SLIST_FOREACH(pc, &cpuhead, pc_allcpu)  {
367 		if (pc->pc_curthread == thr && (stopped_cpus & pc->pc_cpumask))
368 			return (KDB_STOPPEDPCB(pc));
369 	}
370 #endif
371 	return (thr->td_pcb);
372 }
373 
374 struct thread *
375 kdb_thr_first(void)
376 {
377 	struct proc *p;
378 	struct thread *thr;
379 
380 	p = LIST_FIRST(&allproc);
381 	while (p != NULL) {
382 		if (p->p_sflag & PS_INMEM) {
383 			thr = FIRST_THREAD_IN_PROC(p);
384 			if (thr != NULL)
385 				return (thr);
386 		}
387 		p = LIST_NEXT(p, p_list);
388 	}
389 	return (NULL);
390 }
391 
392 struct thread *
393 kdb_thr_from_pid(pid_t pid)
394 {
395 	struct proc *p;
396 
397 	p = LIST_FIRST(&allproc);
398 	while (p != NULL) {
399 		if (p->p_sflag & PS_INMEM && p->p_pid == pid)
400 			return (FIRST_THREAD_IN_PROC(p));
401 		p = LIST_NEXT(p, p_list);
402 	}
403 	return (NULL);
404 }
405 
406 struct thread *
407 kdb_thr_lookup(lwpid_t tid)
408 {
409 	struct thread *thr;
410 
411 	thr = kdb_thr_first();
412 	while (thr != NULL && thr->td_tid != tid)
413 		thr = kdb_thr_next(thr);
414 	return (thr);
415 }
416 
417 struct thread *
418 kdb_thr_next(struct thread *thr)
419 {
420 	struct proc *p;
421 
422 	p = thr->td_proc;
423 	thr = TAILQ_NEXT(thr, td_plist);
424 	do {
425 		if (thr != NULL)
426 			return (thr);
427 		p = LIST_NEXT(p, p_list);
428 		if (p != NULL && (p->p_sflag & PS_INMEM))
429 			thr = FIRST_THREAD_IN_PROC(p);
430 	} while (p != NULL);
431 	return (NULL);
432 }
433 
434 int
435 kdb_thr_select(struct thread *thr)
436 {
437 	if (thr == NULL)
438 		return (EINVAL);
439 	kdb_thread = thr;
440 	kdb_thrctx = kdb_thr_ctx(thr);
441 	return (0);
442 }
443 
444 /*
445  * Enter the debugger due to a trap.
446  */
447 
448 int
449 kdb_trap(int type, int code, struct trapframe *tf)
450 {
451 	register_t intr;
452 #ifdef SMP
453 	int did_stop_cpus;
454 #endif
455 	int handled;
456 
457 	if (kdb_dbbe == NULL || kdb_dbbe->dbbe_trap == NULL)
458 		return (0);
459 
460 	/* We reenter the debugger through kdb_reenter(). */
461 	if (kdb_active)
462 		return (0);
463 
464 	intr = intr_disable();
465 
466 #ifdef SMP
467 	if ((did_stop_cpus = kdb_stop_cpus) != 0)
468 		stop_cpus(PCPU_GET(other_cpus));
469 #endif
470 
471 	kdb_active++;
472 
473 	kdb_frame = tf;
474 
475 	/* Let MD code do its thing first... */
476 	kdb_cpu_trap(type, code);
477 
478 	makectx(tf, &kdb_pcb);
479 	kdb_thr_select(curthread);
480 
481 	handled = kdb_dbbe->dbbe_trap(type, code);
482 
483 	kdb_active--;
484 
485 #ifdef SMP
486 	if (did_stop_cpus)
487 		restart_cpus(stopped_cpus);
488 #endif
489 
490 	intr_restore(intr);
491 
492 	return (handled);
493 }
494