1 /*- 2 * SPDX-License-Identifier: MIT-CMU 3 * 4 * Mach Operating System 5 * Copyright (c) 1991,1990 Carnegie Mellon University 6 * All Rights Reserved. 7 * 8 * Permission to use, copy, modify and distribute this software and its 9 * documentation is hereby granted, provided that both the copyright 10 * notice and this permission notice appear in all copies of the 11 * software, derivative works or modified versions, and any portions 12 * thereof, and that both notices appear in supporting documentation. 13 * 14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 17 * 18 * Carnegie Mellon requests users of this software to return to 19 * 20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 21 * School of Computer Science 22 * Carnegie Mellon University 23 * Pittsburgh PA 15213-3890 24 * 25 * any improvements or extensions that they make and grant Carnegie the 26 * rights to redistribute these changes. 27 */ 28 /* 29 * Author: David B. Golub, Carnegie Mellon University 30 * Date: 7/90 31 */ 32 /* 33 * Command dispatcher. 34 */ 35 36 #include <sys/param.h> 37 #include <sys/conf.h> 38 #include <sys/cons.h> 39 #include <sys/eventhandler.h> 40 #include <sys/kdb.h> 41 #include <sys/kernel.h> 42 #include <sys/linker_set.h> 43 #include <sys/lock.h> 44 #include <sys/mutex.h> 45 #include <sys/proc.h> 46 #include <sys/reboot.h> 47 #include <sys/signalvar.h> 48 #include <sys/systm.h> 49 #include <sys/watchdog.h> 50 51 #include <ddb/ddb.h> 52 #include <ddb/db_command.h> 53 #include <ddb/db_lex.h> 54 #include <ddb/db_output.h> 55 56 #include <machine/cpu.h> 57 #include <machine/setjmp.h> 58 59 #include <security/mac/mac_framework.h> 60 61 /* 62 * Exported global variables 63 */ 64 int db_cmd_loop_done; 65 db_addr_t db_dot; 66 db_addr_t db_last_addr; 67 db_addr_t db_prev; 68 db_addr_t db_next; 69 70 static db_cmdfcn_t db_dump; 71 static db_cmdfcn_t db_fncall; 72 static db_cmdfcn_t db_gdb; 73 static db_cmdfcn_t db_halt; 74 static db_cmdfcn_t db_kill; 75 static db_cmdfcn_t db_reset; 76 static db_cmdfcn_t db_stack_trace; 77 static db_cmdfcn_t db_stack_trace_active; 78 static db_cmdfcn_t db_stack_trace_all; 79 static db_cmdfcn_t db_watchdog; 80 81 #define DB_CMD(_name, _func, _flags) \ 82 { \ 83 .name = (_name), \ 84 .fcn = (_func), \ 85 .flag = (_flags), \ 86 .more = NULL, \ 87 } 88 #define DB_TABLE(_name, _more) \ 89 { \ 90 .name = (_name), \ 91 .fcn = NULL, \ 92 .more = (_more), \ 93 } 94 95 static struct db_command db_show_active_cmds[] = { 96 DB_CMD("trace", db_stack_trace_active, DB_CMD_MEMSAFE), 97 }; 98 struct db_command_table db_show_active_table = 99 LIST_HEAD_INITIALIZER(db_show_active_table); 100 101 static struct db_command db_show_all_cmds[] = { 102 DB_CMD("trace", db_stack_trace_all, DB_CMD_MEMSAFE), 103 }; 104 struct db_command_table db_show_all_table = 105 LIST_HEAD_INITIALIZER(db_show_all_table); 106 107 static struct db_command db_show_cmds[] = { 108 DB_TABLE("active", &db_show_active_table), 109 DB_TABLE("all", &db_show_all_table), 110 DB_CMD("registers", db_show_regs, DB_CMD_MEMSAFE), 111 DB_CMD("breaks", db_listbreak_cmd, DB_CMD_MEMSAFE), 112 DB_CMD("threads", db_show_threads, DB_CMD_MEMSAFE), 113 }; 114 struct db_command_table db_show_table = LIST_HEAD_INITIALIZER(db_show_table); 115 116 static struct db_command db_cmds[] = { 117 DB_TABLE("show", &db_show_table), 118 DB_CMD("print", db_print_cmd, 0), 119 DB_CMD("p", db_print_cmd, 0), 120 DB_CMD("examine", db_examine_cmd, CS_SET_DOT), 121 DB_CMD("x", db_examine_cmd, CS_SET_DOT), 122 DB_CMD("search", db_search_cmd, CS_OWN|CS_SET_DOT), 123 DB_CMD("set", db_set_cmd, CS_OWN|DB_CMD_MEMSAFE), 124 DB_CMD("write", db_write_cmd, CS_MORE|CS_SET_DOT), 125 DB_CMD("w", db_write_cmd, CS_MORE|CS_SET_DOT), 126 DB_CMD("delete", db_delete_cmd, 0), 127 DB_CMD("d", db_delete_cmd, 0), 128 DB_CMD("dump", db_dump, DB_CMD_MEMSAFE), 129 #ifdef HAS_HW_BREAKPOINT 130 DB_CMD("dhbreak", db_deletehbreak_cmd, 0), 131 DB_CMD("hbreak", db_hbreakpoint_cmd, 0), 132 #endif 133 DB_CMD("break", db_breakpoint_cmd, 0), 134 DB_CMD("b", db_breakpoint_cmd, 0), 135 DB_CMD("dwatch", db_deletewatch_cmd, 0), 136 DB_CMD("watch", db_watchpoint_cmd, CS_MORE), 137 DB_CMD("dhwatch", db_deletehwatch_cmd, 0), 138 DB_CMD("hwatch", db_hwatchpoint_cmd, 0), 139 DB_CMD("step", db_single_step_cmd, DB_CMD_MEMSAFE), 140 DB_CMD("s", db_single_step_cmd, DB_CMD_MEMSAFE), 141 DB_CMD("continue", db_continue_cmd, DB_CMD_MEMSAFE), 142 DB_CMD("c", db_continue_cmd, DB_CMD_MEMSAFE), 143 DB_CMD("until", db_trace_until_call_cmd, DB_CMD_MEMSAFE), 144 DB_CMD("next", db_trace_until_matching_cmd, DB_CMD_MEMSAFE), 145 DB_CMD("match", db_trace_until_matching_cmd, 0), 146 DB_CMD("trace", db_stack_trace, CS_OWN|DB_CMD_MEMSAFE), 147 DB_CMD("t", db_stack_trace, CS_OWN|DB_CMD_MEMSAFE), 148 /* XXX alias for active trace */ 149 DB_CMD("acttrace", db_stack_trace_active, DB_CMD_MEMSAFE), 150 /* XXX alias for all trace */ 151 DB_CMD("alltrace", db_stack_trace_all, DB_CMD_MEMSAFE), 152 DB_CMD("where", db_stack_trace, CS_OWN|DB_CMD_MEMSAFE), 153 DB_CMD("bt", db_stack_trace, CS_OWN|DB_CMD_MEMSAFE), 154 DB_CMD("call", db_fncall, CS_OWN), 155 DB_CMD("ps", db_ps, DB_CMD_MEMSAFE), 156 DB_CMD("gdb", db_gdb, 0), 157 DB_CMD("halt", db_halt, DB_CMD_MEMSAFE), 158 DB_CMD("reboot", db_reset, DB_CMD_MEMSAFE), 159 DB_CMD("reset", db_reset, DB_CMD_MEMSAFE), 160 DB_CMD("kill", db_kill, CS_OWN|DB_CMD_MEMSAFE), 161 DB_CMD("watchdog", db_watchdog, CS_OWN|DB_CMD_MEMSAFE), 162 DB_CMD("thread", db_set_thread, 0), 163 DB_CMD("run", db_run_cmd, CS_OWN|DB_CMD_MEMSAFE), 164 DB_CMD("script", db_script_cmd, CS_OWN|DB_CMD_MEMSAFE), 165 DB_CMD("scripts", db_scripts_cmd, DB_CMD_MEMSAFE), 166 DB_CMD("unscript", db_unscript_cmd, CS_OWN|DB_CMD_MEMSAFE), 167 DB_CMD("capture", db_capture_cmd, CS_OWN|DB_CMD_MEMSAFE), 168 DB_CMD("textdump", db_textdump_cmd, CS_OWN|DB_CMD_MEMSAFE), 169 DB_CMD("findstack", db_findstack_cmd, 0), 170 DB_CMD("pprint", db_pprint_cmd, CS_OWN), 171 }; 172 struct db_command_table db_cmd_table = LIST_HEAD_INITIALIZER(db_cmd_table); 173 174 #undef DB_CMD 175 #undef DB_TABLE 176 177 static struct db_command *db_last_command = NULL; 178 179 /* 180 * if 'ed' style: 'dot' is set at start of last item printed, 181 * and '+' points to next line. 182 * Otherwise: 'dot' points to next item, '..' points to last. 183 */ 184 static bool db_ed_style = true; 185 186 /* 187 * Utility routine - discard tokens through end-of-line. 188 */ 189 void 190 db_skip_to_eol(void) 191 { 192 int t; 193 194 do { 195 t = db_read_token(); 196 } while (t != tEOL); 197 } 198 199 /* 200 * Results of command search. 201 */ 202 #define CMD_UNIQUE 0 203 #define CMD_FOUND 1 204 #define CMD_NONE 2 205 #define CMD_AMBIGUOUS 3 206 #define CMD_HELP 4 207 208 static void db_cmd_match(char *name, struct db_command *cmd, 209 struct db_command **cmdp, int *resultp); 210 static void db_cmd_list(struct db_command_table *table); 211 static int db_cmd_search(char *name, struct db_command_table *table, 212 struct db_command **cmdp); 213 static void db_command(struct db_command **last_cmdp, 214 struct db_command_table *cmd_table, bool dopager); 215 216 /* 217 * Initialize the command lists from the static tables. 218 */ 219 void 220 db_command_init(void) 221 { 222 int i; 223 224 for (i = 0; i < nitems(db_cmds); i++) 225 db_command_register(&db_cmd_table, &db_cmds[i]); 226 for (i = 0; i < nitems(db_show_cmds); i++) 227 db_command_register(&db_show_table, &db_show_cmds[i]); 228 for (i = 0; i < nitems(db_show_active_cmds); i++) 229 db_command_register(&db_show_active_table, 230 &db_show_active_cmds[i]); 231 for (i = 0; i < nitems(db_show_all_cmds); i++) 232 db_command_register(&db_show_all_table, &db_show_all_cmds[i]); 233 } 234 235 /* 236 * Register a command. 237 */ 238 void 239 db_command_register(struct db_command_table *list, struct db_command *cmd) 240 { 241 struct db_command *c, *last; 242 243 #ifdef MAC 244 if (mac_ddb_command_register(list, cmd)) { 245 printf("%s: MAC policy refused registration of command %s\n", 246 __func__, cmd->name); 247 return; 248 } 249 #endif 250 last = NULL; 251 LIST_FOREACH(c, list, next) { 252 int n = strcmp(cmd->name, c->name); 253 254 /* Check that the command is not already present. */ 255 if (n == 0) { 256 printf("%s: Warning, the command \"%s\" already exists;" 257 " ignoring request\n", __func__, cmd->name); 258 return; 259 } 260 if (n < 0) { 261 /* NB: keep list sorted lexicographically */ 262 LIST_INSERT_BEFORE(c, cmd, next); 263 return; 264 } 265 last = c; 266 } 267 if (last == NULL) 268 LIST_INSERT_HEAD(list, cmd, next); 269 else 270 LIST_INSERT_AFTER(last, cmd, next); 271 } 272 273 /* 274 * Remove a command previously registered with db_command_register. 275 */ 276 void 277 db_command_unregister(struct db_command_table *list, struct db_command *cmd) 278 { 279 struct db_command *c; 280 281 LIST_FOREACH(c, list, next) { 282 if (cmd == c) { 283 LIST_REMOVE(cmd, next); 284 return; 285 } 286 } 287 /* NB: intentionally quiet */ 288 } 289 290 /* 291 * Helper function to match a single command. 292 */ 293 static void 294 db_cmd_match(char *name, struct db_command *cmd, struct db_command **cmdp, 295 int *resultp) 296 { 297 char *lp, *rp; 298 int c; 299 300 lp = name; 301 rp = cmd->name; 302 while ((c = *lp) == *rp) { 303 if (c == 0) { 304 /* complete match */ 305 *cmdp = cmd; 306 *resultp = CMD_UNIQUE; 307 return; 308 } 309 lp++; 310 rp++; 311 } 312 if (c == 0) { 313 /* end of name, not end of command - 314 partial match */ 315 if (*resultp == CMD_FOUND) { 316 *resultp = CMD_AMBIGUOUS; 317 /* but keep looking for a full match - 318 this lets us match single letters */ 319 } else if (*resultp == CMD_NONE) { 320 *cmdp = cmd; 321 *resultp = CMD_FOUND; 322 } 323 } 324 } 325 326 /* 327 * Search for command prefix. 328 */ 329 static int 330 db_cmd_search(char *name, struct db_command_table *table, 331 struct db_command **cmdp) 332 { 333 struct db_command *cmd; 334 int result = CMD_NONE; 335 336 LIST_FOREACH(cmd, table, next) { 337 db_cmd_match(name,cmd,cmdp,&result); 338 if (result == CMD_UNIQUE) 339 break; 340 } 341 342 if (result == CMD_NONE) { 343 /* check for 'help' */ 344 if (name[0] == 'h' && name[1] == 'e' 345 && name[2] == 'l' && name[3] == 'p') 346 result = CMD_HELP; 347 } 348 return (result); 349 } 350 351 static void 352 db_cmd_list(struct db_command_table *table) 353 { 354 struct db_command *cmd; 355 int have_subcommands; 356 357 have_subcommands = 0; 358 LIST_FOREACH(cmd, table, next) { 359 if (cmd->more != NULL) 360 have_subcommands++; 361 db_printf("%-16s", cmd->name); 362 db_end_line(16); 363 } 364 365 if (have_subcommands > 0) { 366 db_printf("\nThe following have subcommands; append \"help\" " 367 "to list (e.g. \"show help\"):\n"); 368 LIST_FOREACH(cmd, table, next) { 369 if (cmd->more == NULL) 370 continue; 371 db_printf("%-16s", cmd->name); 372 db_end_line(16); 373 } 374 } 375 } 376 377 static void 378 db_command(struct db_command **last_cmdp, struct db_command_table *cmd_table, 379 bool dopager) 380 { 381 char modif[TOK_STRING_SIZE]; 382 struct db_command *cmd = NULL; 383 db_expr_t addr, count; 384 int t, result; 385 bool have_addr = false; 386 387 t = db_read_token(); 388 if (t == tEOL) { 389 /* empty line repeats last command, at 'next' */ 390 cmd = *last_cmdp; 391 addr = (db_expr_t)db_next; 392 have_addr = false; 393 count = 1; 394 modif[0] = '\0'; 395 } else if (t == tEXCL) { 396 db_fncall((db_expr_t)0, false, (db_expr_t)0, NULL); 397 return; 398 } else if (t != tIDENT) { 399 db_printf("Unrecognized input; use \"help\" " 400 "to list available commands\n"); 401 db_flush_lex(); 402 return; 403 } else { 404 /* 405 * Search for command 406 */ 407 while (cmd_table != NULL) { 408 result = db_cmd_search(db_tok_string, cmd_table, &cmd); 409 switch (result) { 410 case CMD_NONE: 411 db_printf("No such command; use \"help\" " 412 "to list available commands\n"); 413 db_flush_lex(); 414 return; 415 case CMD_AMBIGUOUS: 416 db_printf("Ambiguous\n"); 417 db_flush_lex(); 418 return; 419 case CMD_HELP: 420 if (cmd_table == &db_cmd_table) { 421 db_printf("This is ddb(4), the kernel debugger; " 422 "see https://man.FreeBSD.org/ddb/4 for help.\n"); 423 db_printf("Use \"bt\" for backtrace, \"dump\" for " 424 "kernel core dump, \"reset\" to reboot.\n"); 425 db_printf("Available commands:\n"); 426 } 427 db_cmd_list(cmd_table); 428 db_flush_lex(); 429 return; 430 case CMD_UNIQUE: 431 case CMD_FOUND: 432 break; 433 } 434 if ((cmd_table = cmd->more) != NULL) { 435 t = db_read_token(); 436 if (t != tIDENT) { 437 db_printf("Subcommand required; " 438 "available subcommands:\n"); 439 db_cmd_list(cmd_table); 440 db_flush_lex(); 441 return; 442 } 443 } 444 } 445 446 if ((cmd->flag & CS_OWN) == 0) { 447 /* 448 * Standard syntax: 449 * command [/modifier] [addr] [,count] 450 */ 451 t = db_read_token(); 452 if (t == tSLASH) { 453 t = db_read_token(); 454 if (t != tIDENT) { 455 db_printf("Bad modifier\n"); 456 db_flush_lex(); 457 return; 458 } 459 db_strcpy(modif, db_tok_string); 460 } else { 461 db_unread_token(t); 462 modif[0] = '\0'; 463 } 464 465 if (db_expression(&addr)) { 466 db_dot = (db_addr_t) addr; 467 db_last_addr = db_dot; 468 have_addr = true; 469 } else { 470 addr = (db_expr_t) db_dot; 471 have_addr = false; 472 } 473 474 t = db_read_token(); 475 if (t == tCOMMA) { 476 if (!db_expression(&count)) { 477 db_printf("Count missing\n"); 478 db_flush_lex(); 479 return; 480 } 481 } else { 482 db_unread_token(t); 483 count = -1; 484 } 485 486 if ((cmd->flag & CS_MORE) == 0) { 487 db_skip_to_eol(); 488 } 489 } 490 } 491 492 *last_cmdp = cmd; 493 if (cmd != NULL) { 494 #ifdef MAC 495 if (mac_ddb_command_exec(cmd, addr, have_addr, count, modif)) { 496 db_printf("MAC prevented execution of command %s\n", 497 cmd->name); 498 return; 499 } 500 #endif 501 /* 502 * Execute the command. 503 */ 504 if (dopager) 505 db_enable_pager(); 506 else 507 db_disable_pager(); 508 (*cmd->fcn)(addr, have_addr, count, modif); 509 if (dopager) 510 db_disable_pager(); 511 512 if (cmd->flag & CS_SET_DOT) { 513 /* 514 * If command changes dot, set dot to previous address 515 * displayed (if 'ed' style). 516 */ 517 db_dot = db_ed_style ? db_prev : db_next; 518 } else { 519 /* 520 * If command does not change dot, set 'next' location 521 * to be the same. 522 */ 523 db_next = db_dot; 524 } 525 } 526 } 527 528 /* 529 * At least one non-optional command must be implemented using 530 * DB_COMMAND() so that db_cmd_set gets created. Here is one. 531 */ 532 DB_COMMAND_FLAGS(panic, db_panic, DB_CMD_MEMSAFE) 533 { 534 db_disable_pager(); 535 panic("from debugger"); 536 } 537 538 void 539 db_command_loop(void) 540 { 541 /* 542 * Initialize 'prev' and 'next' to dot. 543 */ 544 db_prev = db_dot; 545 db_next = db_dot; 546 547 db_cmd_loop_done = 0; 548 while (!db_cmd_loop_done) { 549 if (db_print_position() != 0) 550 db_printf("\n"); 551 552 db_printf("db> "); 553 (void)db_read_line(); 554 555 db_command(&db_last_command, &db_cmd_table, /* dopager */ true); 556 } 557 } 558 559 /* 560 * Execute a command on behalf of a script. The caller is responsible for 561 * making sure that the command string is < DB_MAXLINE or it will be 562 * truncated. 563 * 564 * XXXRW: Runs by injecting faked input into DDB input stream; it would be 565 * nicer to use an alternative approach that didn't mess with the previous 566 * command buffer. 567 */ 568 void 569 db_command_script(const char *command) 570 { 571 db_prev = db_next = db_dot; 572 db_inject_line(command); 573 db_command(&db_last_command, &db_cmd_table, /* dopager */ false); 574 } 575 576 void 577 db_error(const char *s) 578 { 579 if (s) 580 db_printf("%s", s); 581 db_flush_lex(); 582 kdb_reenter_silent(); 583 panic("%s: did not reenter debugger", __func__); 584 } 585 586 static void 587 db_dump(db_expr_t dummy, bool dummy2, db_expr_t dummy3, char *dummy4) 588 { 589 int error; 590 591 if (textdump_pending) { 592 db_printf("textdump_pending set.\n" 593 "run \"textdump unset\" first or \"textdump dump\" for a textdump.\n"); 594 return; 595 } 596 error = doadump(false); 597 if (error) { 598 db_printf("Cannot dump: "); 599 switch (error) { 600 case EBUSY: 601 db_printf("debugger got invoked while dumping.\n"); 602 break; 603 case ENXIO: 604 db_printf("no dump device specified.\n"); 605 break; 606 default: 607 db_printf("unknown error (error=%d).\n", error); 608 break; 609 } 610 } 611 } 612 613 /* 614 * Call random function: 615 * !expr(arg,arg,arg) 616 */ 617 618 /* The generic implementation supports a maximum of 10 arguments. */ 619 typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t, 620 db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t); 621 622 static __inline int 623 db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[]) 624 { 625 __db_f *f = (__db_f *)addr; 626 627 if (nargs > 10) { 628 db_printf("Too many arguments (max 10)\n"); 629 return (0); 630 } 631 *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5], 632 args[6], args[7], args[8], args[9]); 633 return (1); 634 } 635 636 static void 637 db_fncall(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 638 { 639 db_expr_t fn_addr; 640 db_expr_t args[DB_MAXARGS]; 641 int nargs = 0; 642 db_expr_t retval; 643 int t; 644 645 if (!db_expression(&fn_addr)) { 646 db_printf("Bad function\n"); 647 db_flush_lex(); 648 return; 649 } 650 651 t = db_read_token(); 652 if (t == tLPAREN) { 653 if (db_expression(&args[0])) { 654 nargs++; 655 while ((t = db_read_token()) == tCOMMA) { 656 if (nargs == DB_MAXARGS) { 657 db_printf("Too many arguments (max %d)\n", DB_MAXARGS); 658 db_flush_lex(); 659 return; 660 } 661 if (!db_expression(&args[nargs])) { 662 db_printf("Argument missing\n"); 663 db_flush_lex(); 664 return; 665 } 666 nargs++; 667 } 668 db_unread_token(t); 669 } 670 if (db_read_token() != tRPAREN) { 671 db_printf("Mismatched parens\n"); 672 db_flush_lex(); 673 return; 674 } 675 } 676 db_skip_to_eol(); 677 db_disable_pager(); 678 679 if (DB_CALL(fn_addr, &retval, nargs, args)) 680 db_printf("= %#lr\n", (long)retval); 681 } 682 683 static void 684 db_halt(db_expr_t dummy, bool dummy2, db_expr_t dummy3, char *dummy4) 685 { 686 687 cpu_halt(); 688 } 689 690 static void 691 db_kill(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 692 { 693 db_expr_t old_radix, pid, sig; 694 struct proc *p; 695 696 #define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0) 697 698 /* 699 * PIDs and signal numbers are typically represented in base 700 * 10, so make that the default here. It can, of course, be 701 * overridden by specifying a prefix. 702 */ 703 old_radix = db_radix; 704 db_radix = 10; 705 /* Retrieve arguments. */ 706 if (!db_expression(&sig)) 707 DB_ERROR(("Missing signal number\n")); 708 if (!db_expression(&pid)) 709 DB_ERROR(("Missing process ID\n")); 710 db_skip_to_eol(); 711 if (!_SIG_VALID(sig)) 712 DB_ERROR(("Signal number out of range\n")); 713 714 /* 715 * Find the process in question. allproc_lock is not needed 716 * since we're in DDB. 717 */ 718 /* sx_slock(&allproc_lock); */ 719 FOREACH_PROC_IN_SYSTEM(p) 720 if (p->p_pid == pid) 721 break; 722 /* sx_sunlock(&allproc_lock); */ 723 if (p == NULL) 724 DB_ERROR(("Can't find process with pid %ld\n", (long) pid)); 725 726 /* If it's already locked, bail; otherwise, do the deed. */ 727 if (PROC_TRYLOCK(p) == 0) 728 DB_ERROR(("Can't lock process with pid %ld\n", (long) pid)); 729 else { 730 pksignal(p, sig, NULL); 731 PROC_UNLOCK(p); 732 } 733 734 out: 735 db_radix = old_radix; 736 #undef DB_ERROR 737 } 738 739 /* 740 * Reboot. In case there is an additional argument, take it as delay in 741 * seconds. Default to 15s if we cannot parse it and make sure we will 742 * never wait longer than 1 week. Some code is similar to 743 * kern_shutdown.c:shutdown_panic(). 744 */ 745 #ifndef DB_RESET_MAXDELAY 746 #define DB_RESET_MAXDELAY (3600 * 24 * 7) 747 #endif 748 749 static void 750 db_reset(db_expr_t addr, bool have_addr, db_expr_t count __unused, 751 char *modif) 752 { 753 int delay, loop; 754 755 if (have_addr) { 756 delay = (int)db_hex2dec(addr); 757 758 /* If we parse to fail, use 15s. */ 759 if (delay == -1) 760 delay = 15; 761 762 /* Cap at one week. */ 763 if ((uintmax_t)delay > (uintmax_t)DB_RESET_MAXDELAY) 764 delay = DB_RESET_MAXDELAY; 765 766 db_printf("Automatic reboot in %d seconds - " 767 "press a key on the console to abort\n", delay); 768 for (loop = delay * 10; loop > 0; --loop) { 769 DELAY(1000 * 100); /* 1/10th second */ 770 /* Did user type a key? */ 771 if (cncheckc() != -1) 772 return; 773 } 774 } 775 776 /* 777 * Conditionally try the standard reboot path, so any registered 778 * shutdown/reset handlers have a chance to run first. Some platforms 779 * may not support the machine-dependent mechanism used by cpu_reset() 780 * and rely on some other non-standard mechanism to perform the reset. 781 * For example, the BCM2835 watchdog driver or gpio-poweroff driver. 782 */ 783 if (modif[0] != 's') { 784 kern_reboot(RB_NOSYNC); 785 /* NOTREACHED */ 786 } 787 788 cpu_reset(); 789 } 790 791 static void 792 db_watchdog(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 793 { 794 db_expr_t old_radix, tout; 795 int err, i; 796 797 old_radix = db_radix; 798 db_radix = 10; 799 err = db_expression(&tout); 800 db_skip_to_eol(); 801 db_radix = old_radix; 802 803 /* If no argument is provided the watchdog will just be disabled. */ 804 if (err == 0) { 805 db_printf("No argument provided, disabling watchdog\n"); 806 tout = 0; 807 } else if ((tout & WD_INTERVAL) == WD_TO_NEVER) { 808 db_error("Out of range watchdog interval\n"); 809 return; 810 } 811 EVENTHANDLER_INVOKE(watchdog_list, tout, &i); 812 } 813 814 static void 815 db_gdb(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 816 { 817 818 if (kdb_dbbe_select("gdb") != 0) { 819 db_printf("The remote GDB backend could not be selected.\n"); 820 return; 821 } 822 /* 823 * Mark that we are done in the debugger. kdb_trap() 824 * should re-enter with the new backend. 825 */ 826 db_cmd_loop_done = 1; 827 db_printf("(ctrl-c will return control to ddb)\n"); 828 } 829 830 static void 831 db_stack_trace(db_expr_t tid, bool hastid, db_expr_t count, char *modif) 832 { 833 struct thread *td; 834 db_expr_t radix; 835 pid_t pid; 836 int t; 837 838 /* 839 * We parse our own arguments. We don't like the default radix. 840 */ 841 radix = db_radix; 842 db_radix = 10; 843 hastid = db_expression(&tid); 844 t = db_read_token(); 845 if (t == tCOMMA) { 846 if (!db_expression(&count)) { 847 db_printf("Count missing\n"); 848 db_flush_lex(); 849 db_radix = radix; 850 return; 851 } 852 } else { 853 db_unread_token(t); 854 count = -1; 855 } 856 db_skip_to_eol(); 857 db_radix = radix; 858 859 if (hastid) { 860 td = kdb_thr_lookup((lwpid_t)tid); 861 if (td == NULL) 862 td = kdb_thr_from_pid((pid_t)tid); 863 if (td == NULL) { 864 db_printf("Thread %d not found\n", (int)tid); 865 return; 866 } 867 } else 868 td = kdb_thread; 869 if (td->td_proc != NULL) 870 pid = td->td_proc->p_pid; 871 else 872 pid = -1; 873 db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td); 874 if (td->td_proc != NULL && (td->td_proc->p_flag & P_INMEM) == 0) 875 db_printf("--- swapped out\n"); 876 else 877 db_trace_thread(td, count); 878 } 879 880 static void 881 _db_stack_trace_all(bool active_only) 882 { 883 struct thread *td; 884 jmp_buf jb; 885 void *prev_jb; 886 887 for (td = kdb_thr_first(); td != NULL; td = kdb_thr_next(td)) { 888 prev_jb = kdb_jmpbuf(jb); 889 if (setjmp(jb) == 0) { 890 if (TD_IS_RUNNING(td)) 891 db_printf("\nTracing command %s pid %d" 892 " tid %ld td %p (CPU %d)\n", 893 td->td_proc->p_comm, td->td_proc->p_pid, 894 (long)td->td_tid, td, td->td_oncpu); 895 else if (active_only) 896 continue; 897 else 898 db_printf("\nTracing command %s pid %d" 899 " tid %ld td %p\n", td->td_proc->p_comm, 900 td->td_proc->p_pid, (long)td->td_tid, td); 901 if (td->td_proc->p_flag & P_INMEM) 902 db_trace_thread(td, -1); 903 else 904 db_printf("--- swapped out\n"); 905 if (db_pager_quit) { 906 kdb_jmpbuf(prev_jb); 907 return; 908 } 909 } 910 kdb_jmpbuf(prev_jb); 911 } 912 } 913 914 static void 915 db_stack_trace_active(db_expr_t dummy, bool dummy2, db_expr_t dummy3, 916 char *dummy4) 917 { 918 919 _db_stack_trace_all(true); 920 } 921 922 static void 923 db_stack_trace_all(db_expr_t dummy, bool dummy2, db_expr_t dummy3, 924 char *dummy4) 925 { 926 927 _db_stack_trace_all(false); 928 } 929 930 /* 931 * Take the parsed expression value from the command line that was parsed 932 * as a hexadecimal value and convert it as if the expression was parsed 933 * as a decimal value. Returns -1 if the expression was not a valid 934 * decimal value. 935 */ 936 db_expr_t 937 db_hex2dec(db_expr_t expr) 938 { 939 uintptr_t x, y; 940 db_expr_t val; 941 942 y = 1; 943 val = 0; 944 x = expr; 945 while (x != 0) { 946 if (x % 16 > 9) 947 return (-1); 948 val += (x % 16) * (y); 949 x >>= 4; 950 y *= 10; 951 } 952 return (val); 953 } 954