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