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