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