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