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