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