1 /*- 2 * Mach Operating System 3 * Copyright (c) 1991,1990 Carnegie Mellon University 4 * All Rights Reserved. 5 * 6 * Permission to use, copy, modify and distribute this software and its 7 * documentation is hereby granted, provided that both the copyright 8 * notice and this permission notice appear in all copies of the 9 * software, derivative works or modified versions, and any portions 10 * thereof, and that both notices appear in supporting documentation. 11 * 12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 15 * 16 * Carnegie Mellon requests users of this software to return to 17 * 18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 19 * School of Computer Science 20 * Carnegie Mellon University 21 * Pittsburgh PA 15213-3890 22 * 23 * any improvements or extensions that they make and grant Carnegie the 24 * rights to redistribute these changes. 25 */ 26 /* 27 * Author: David B. Golub, Carnegie Mellon University 28 * Date: 7/90 29 */ 30 /* 31 * Command dispatcher. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <sys/param.h> 38 #include <sys/linker_set.h> 39 #include <sys/lock.h> 40 #include <sys/kdb.h> 41 #include <sys/mutex.h> 42 #include <sys/proc.h> 43 #include <sys/reboot.h> 44 #include <sys/signalvar.h> 45 #include <sys/systm.h> 46 #include <sys/cons.h> 47 #include <sys/watchdog.h> 48 49 #include <ddb/ddb.h> 50 #include <ddb/db_command.h> 51 #include <ddb/db_lex.h> 52 #include <ddb/db_output.h> 53 54 #include <machine/cpu.h> 55 #include <machine/setjmp.h> 56 57 /* 58 * Exported global variables 59 */ 60 boolean_t db_cmd_loop_done; 61 db_addr_t db_dot; 62 db_addr_t db_last_addr; 63 db_addr_t db_prev; 64 db_addr_t db_next; 65 66 SET_DECLARE(db_cmd_set, struct command); 67 SET_DECLARE(db_show_cmd_set, struct command); 68 69 static db_cmdfcn_t db_fncall; 70 static db_cmdfcn_t db_gdb; 71 static db_cmdfcn_t db_halt; 72 static db_cmdfcn_t db_kill; 73 static db_cmdfcn_t db_reset; 74 static db_cmdfcn_t db_stack_trace; 75 static db_cmdfcn_t db_stack_trace_all; 76 static db_cmdfcn_t db_watchdog; 77 78 /* 79 * 'show' commands 80 */ 81 82 static struct command db_show_all_cmds[] = { 83 { "procs", db_ps, 0, 0 }, 84 { (char *)0 } 85 }; 86 87 static struct command_table db_show_all_table = { 88 db_show_all_cmds 89 }; 90 91 static struct command db_show_cmds[] = { 92 { "all", 0, 0, &db_show_all_table }, 93 { "registers", db_show_regs, 0, 0 }, 94 { "breaks", db_listbreak_cmd, 0, 0 }, 95 { "threads", db_show_threads, 0, 0 }, 96 { (char *)0, } 97 }; 98 99 static struct command_table db_show_table = { 100 db_show_cmds, 101 SET_BEGIN(db_show_cmd_set), 102 SET_LIMIT(db_show_cmd_set) 103 }; 104 105 static struct command db_commands[] = { 106 { "print", db_print_cmd, 0, 0 }, 107 { "p", db_print_cmd, 0, 0 }, 108 { "examine", db_examine_cmd, CS_SET_DOT, 0 }, 109 { "x", db_examine_cmd, CS_SET_DOT, 0 }, 110 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 }, 111 { "set", db_set_cmd, CS_OWN, 0 }, 112 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 }, 113 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 }, 114 { "delete", db_delete_cmd, 0, 0 }, 115 { "d", db_delete_cmd, 0, 0 }, 116 { "break", db_breakpoint_cmd, 0, 0 }, 117 { "b", db_breakpoint_cmd, 0, 0 }, 118 { "dwatch", db_deletewatch_cmd, 0, 0 }, 119 { "watch", db_watchpoint_cmd, CS_MORE,0 }, 120 { "dhwatch", db_deletehwatch_cmd, 0, 0 }, 121 { "hwatch", db_hwatchpoint_cmd, 0, 0 }, 122 { "step", db_single_step_cmd, 0, 0 }, 123 { "s", db_single_step_cmd, 0, 0 }, 124 { "continue", db_continue_cmd, 0, 0 }, 125 { "c", db_continue_cmd, 0, 0 }, 126 { "until", db_trace_until_call_cmd,0, 0 }, 127 { "next", db_trace_until_matching_cmd,0, 0 }, 128 { "match", db_trace_until_matching_cmd,0, 0 }, 129 { "trace", db_stack_trace, CS_OWN, 0 }, 130 { "t", db_stack_trace, CS_OWN, 0 }, 131 { "alltrace", db_stack_trace_all, 0, 0 }, 132 { "where", db_stack_trace, CS_OWN, 0 }, 133 { "bt", db_stack_trace, CS_OWN, 0 }, 134 { "call", db_fncall, CS_OWN, 0 }, 135 { "show", 0, 0, &db_show_table }, 136 { "ps", db_ps, 0, 0 }, 137 { "gdb", db_gdb, 0, 0 }, 138 { "halt", db_halt, 0, 0 }, 139 { "reboot", db_reset, 0, 0 }, 140 { "reset", db_reset, 0, 0 }, 141 { "kill", db_kill, CS_OWN, 0 }, 142 { "watchdog", db_watchdog, 0, 0 }, 143 { "thread", db_set_thread, CS_OWN, 0 }, 144 { "run", db_run_cmd, CS_OWN, 0 }, 145 { "script", db_script_cmd, CS_OWN, 0 }, 146 { "scripts", db_scripts_cmd, 0, 0 }, 147 { "unscript", db_unscript_cmd, CS_OWN, 0 }, 148 { "capture", db_capture_cmd, CS_OWN, 0 }, 149 { "textdump", db_textdump_cmd, CS_OWN, 0 }, 150 { (char *)0, } 151 }; 152 153 static struct command_table db_command_table = { 154 db_commands, 155 SET_BEGIN(db_cmd_set), 156 SET_LIMIT(db_cmd_set) 157 }; 158 159 static struct command *db_last_command = 0; 160 161 /* 162 * if 'ed' style: 'dot' is set at start of last item printed, 163 * and '+' points to next line. 164 * Otherwise: 'dot' points to next item, '..' points to last. 165 */ 166 static boolean_t db_ed_style = TRUE; 167 168 /* 169 * Utility routine - discard tokens through end-of-line. 170 */ 171 void 172 db_skip_to_eol() 173 { 174 int t; 175 do { 176 t = db_read_token(); 177 } while (t != tEOL); 178 } 179 180 /* 181 * Results of command search. 182 */ 183 #define CMD_UNIQUE 0 184 #define CMD_FOUND 1 185 #define CMD_NONE 2 186 #define CMD_AMBIGUOUS 3 187 #define CMD_HELP 4 188 189 static void db_cmd_match(char *name, struct command *cmd, 190 struct command **cmdp, int *resultp); 191 static void db_cmd_list(struct command_table *table); 192 static int db_cmd_search(char *name, struct command_table *table, 193 struct command **cmdp); 194 static void db_command(struct command **last_cmdp, 195 struct command_table *cmd_table, int dopager); 196 197 /* 198 * Helper function to match a single command. 199 */ 200 static void 201 db_cmd_match(name, cmd, cmdp, resultp) 202 char * name; 203 struct command *cmd; 204 struct command **cmdp; /* out */ 205 int * resultp; 206 { 207 char *lp, *rp; 208 int c; 209 210 lp = name; 211 rp = cmd->name; 212 while ((c = *lp) == *rp) { 213 if (c == 0) { 214 /* complete match */ 215 *cmdp = cmd; 216 *resultp = CMD_UNIQUE; 217 return; 218 } 219 lp++; 220 rp++; 221 } 222 if (c == 0) { 223 /* end of name, not end of command - 224 partial match */ 225 if (*resultp == CMD_FOUND) { 226 *resultp = CMD_AMBIGUOUS; 227 /* but keep looking for a full match - 228 this lets us match single letters */ 229 } else { 230 *cmdp = cmd; 231 *resultp = CMD_FOUND; 232 } 233 } 234 } 235 236 /* 237 * Search for command prefix. 238 */ 239 static int 240 db_cmd_search(name, table, cmdp) 241 char * name; 242 struct command_table *table; 243 struct command **cmdp; /* out */ 244 { 245 struct command *cmd; 246 struct command **aux_cmdp; 247 int result = CMD_NONE; 248 249 for (cmd = table->table; cmd->name != 0; cmd++) { 250 db_cmd_match(name, cmd, cmdp, &result); 251 if (result == CMD_UNIQUE) 252 return (CMD_UNIQUE); 253 } 254 if (table->aux_tablep != NULL) 255 for (aux_cmdp = table->aux_tablep; 256 aux_cmdp < table->aux_tablep_end; 257 aux_cmdp++) { 258 db_cmd_match(name, *aux_cmdp, cmdp, &result); 259 if (result == CMD_UNIQUE) 260 return (CMD_UNIQUE); 261 } 262 if (result == CMD_NONE) { 263 /* check for 'help' */ 264 if (name[0] == 'h' && name[1] == 'e' 265 && name[2] == 'l' && name[3] == 'p') 266 result = CMD_HELP; 267 } 268 return (result); 269 } 270 271 static void 272 db_cmd_list(table) 273 struct command_table *table; 274 { 275 register struct command *cmd; 276 register struct command **aux_cmdp; 277 278 for (cmd = table->table; cmd->name != 0; cmd++) { 279 db_printf("%-12s", cmd->name); 280 db_end_line(12); 281 } 282 if (table->aux_tablep == NULL) 283 return; 284 for (aux_cmdp = table->aux_tablep; aux_cmdp < table->aux_tablep_end; 285 aux_cmdp++) { 286 db_printf("%-12s", (*aux_cmdp)->name); 287 db_end_line(12); 288 } 289 } 290 291 static void 292 db_command(last_cmdp, cmd_table, dopager) 293 struct command **last_cmdp; /* IN_OUT */ 294 struct command_table *cmd_table; 295 int dopager; 296 { 297 struct command *cmd; 298 int t; 299 char modif[TOK_STRING_SIZE]; 300 db_expr_t addr, count; 301 boolean_t have_addr = FALSE; 302 int result; 303 304 t = db_read_token(); 305 if (t == tEOL) { 306 /* empty line repeats last command, at 'next' */ 307 cmd = *last_cmdp; 308 addr = (db_expr_t)db_next; 309 have_addr = FALSE; 310 count = 1; 311 modif[0] = '\0'; 312 } 313 else if (t == tEXCL) { 314 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0); 315 return; 316 } 317 else if (t != tIDENT) { 318 db_printf("?\n"); 319 db_flush_lex(); 320 return; 321 } 322 else { 323 /* 324 * Search for command 325 */ 326 while (cmd_table) { 327 result = db_cmd_search(db_tok_string, 328 cmd_table, 329 &cmd); 330 switch (result) { 331 case CMD_NONE: 332 db_printf("No such command\n"); 333 db_flush_lex(); 334 return; 335 case CMD_AMBIGUOUS: 336 db_printf("Ambiguous\n"); 337 db_flush_lex(); 338 return; 339 case CMD_HELP: 340 db_cmd_list(cmd_table); 341 db_flush_lex(); 342 return; 343 default: 344 break; 345 } 346 if ((cmd_table = cmd->more) != NULL) { 347 t = db_read_token(); 348 if (t != tIDENT) { 349 db_cmd_list(cmd_table); 350 db_flush_lex(); 351 return; 352 } 353 } 354 } 355 356 if ((cmd->flag & CS_OWN) == 0) { 357 /* 358 * Standard syntax: 359 * command [/modifier] [addr] [,count] 360 */ 361 t = db_read_token(); 362 if (t == tSLASH) { 363 t = db_read_token(); 364 if (t != tIDENT) { 365 db_printf("Bad modifier\n"); 366 db_flush_lex(); 367 return; 368 } 369 db_strcpy(modif, db_tok_string); 370 } 371 else { 372 db_unread_token(t); 373 modif[0] = '\0'; 374 } 375 376 if (db_expression(&addr)) { 377 db_dot = (db_addr_t) addr; 378 db_last_addr = db_dot; 379 have_addr = TRUE; 380 } 381 else { 382 addr = (db_expr_t) db_dot; 383 have_addr = FALSE; 384 } 385 t = db_read_token(); 386 if (t == tCOMMA) { 387 if (!db_expression(&count)) { 388 db_printf("Count missing\n"); 389 db_flush_lex(); 390 return; 391 } 392 } 393 else { 394 db_unread_token(t); 395 count = -1; 396 } 397 if ((cmd->flag & CS_MORE) == 0) { 398 db_skip_to_eol(); 399 } 400 } 401 } 402 *last_cmdp = cmd; 403 if (cmd != 0) { 404 /* 405 * Execute the command. 406 */ 407 if (dopager) 408 db_enable_pager(); 409 else 410 db_disable_pager(); 411 (*cmd->fcn)(addr, have_addr, count, modif); 412 if (dopager) 413 db_disable_pager(); 414 415 if (cmd->flag & CS_SET_DOT) { 416 /* 417 * If command changes dot, set dot to 418 * previous address displayed (if 'ed' style). 419 */ 420 if (db_ed_style) { 421 db_dot = db_prev; 422 } 423 else { 424 db_dot = db_next; 425 } 426 } 427 else { 428 /* 429 * If command does not change dot, 430 * set 'next' location to be the same. 431 */ 432 db_next = db_dot; 433 } 434 } 435 } 436 437 /* 438 * At least one non-optional command must be implemented using 439 * DB_COMMAND() so that db_cmd_set gets created. Here is one. 440 */ 441 DB_COMMAND(panic, db_panic) 442 { 443 db_disable_pager(); 444 panic("from debugger"); 445 } 446 447 void 448 db_command_loop() 449 { 450 /* 451 * Initialize 'prev' and 'next' to dot. 452 */ 453 db_prev = db_dot; 454 db_next = db_dot; 455 456 db_cmd_loop_done = 0; 457 while (!db_cmd_loop_done) { 458 if (db_print_position() != 0) 459 db_printf("\n"); 460 461 db_printf("db> "); 462 (void) db_read_line(); 463 464 db_command(&db_last_command, &db_command_table, /* dopager */ 1); 465 } 466 } 467 468 /* 469 * Execute a command on behalf of a script. The caller is responsible for 470 * making sure that the command string is < DB_MAXLINE or it will be 471 * truncated. 472 * 473 * XXXRW: Runs by injecting faked input into DDB input stream; it would be 474 * nicer to use an alternative approach that didn't mess with the previous 475 * command buffer. 476 */ 477 void 478 db_command_script(const char *command) 479 { 480 db_prev = db_next = db_dot; 481 db_inject_line(command); 482 db_command(&db_last_command, &db_command_table, /* dopager */ 0); 483 } 484 485 void 486 db_error(s) 487 const char *s; 488 { 489 if (s) 490 db_printf("%s", s); 491 db_flush_lex(); 492 kdb_reenter(); 493 } 494 495 496 /* 497 * Call random function: 498 * !expr(arg,arg,arg) 499 */ 500 501 /* The generic implementation supports a maximum of 10 arguments. */ 502 typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t, 503 db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t); 504 505 static __inline int 506 db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[]) 507 { 508 __db_f *f = (__db_f *)addr; 509 510 if (nargs > 10) { 511 db_printf("Too many arguments (max 10)\n"); 512 return (0); 513 } 514 *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5], 515 args[6], args[7], args[8], args[9]); 516 return (1); 517 } 518 519 static void 520 db_fncall(dummy1, dummy2, dummy3, dummy4) 521 db_expr_t dummy1; 522 boolean_t dummy2; 523 db_expr_t dummy3; 524 char * dummy4; 525 { 526 db_expr_t fn_addr; 527 db_expr_t args[DB_MAXARGS]; 528 int nargs = 0; 529 db_expr_t retval; 530 int t; 531 532 if (!db_expression(&fn_addr)) { 533 db_printf("Bad function\n"); 534 db_flush_lex(); 535 return; 536 } 537 538 t = db_read_token(); 539 if (t == tLPAREN) { 540 if (db_expression(&args[0])) { 541 nargs++; 542 while ((t = db_read_token()) == tCOMMA) { 543 if (nargs == DB_MAXARGS) { 544 db_printf("Too many arguments (max %d)\n", DB_MAXARGS); 545 db_flush_lex(); 546 return; 547 } 548 if (!db_expression(&args[nargs])) { 549 db_printf("Argument missing\n"); 550 db_flush_lex(); 551 return; 552 } 553 nargs++; 554 } 555 db_unread_token(t); 556 } 557 if (db_read_token() != tRPAREN) { 558 db_printf("?\n"); 559 db_flush_lex(); 560 return; 561 } 562 } 563 db_skip_to_eol(); 564 db_disable_pager(); 565 566 if (DB_CALL(fn_addr, &retval, nargs, args)) 567 db_printf("= %#lr\n", (long)retval); 568 } 569 570 static void 571 db_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4) 572 { 573 574 cpu_halt(); 575 } 576 577 static void 578 db_kill(dummy1, dummy2, dummy3, dummy4) 579 db_expr_t dummy1; 580 boolean_t dummy2; 581 db_expr_t dummy3; 582 char * dummy4; 583 { 584 db_expr_t old_radix, pid, sig; 585 struct proc *p; 586 587 #define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0) 588 589 /* 590 * PIDs and signal numbers are typically represented in base 591 * 10, so make that the default here. It can, of course, be 592 * overridden by specifying a prefix. 593 */ 594 old_radix = db_radix; 595 db_radix = 10; 596 /* Retrieve arguments. */ 597 if (!db_expression(&sig)) 598 DB_ERROR(("Missing signal number\n")); 599 if (!db_expression(&pid)) 600 DB_ERROR(("Missing process ID\n")); 601 db_skip_to_eol(); 602 if (sig < 0 || sig > _SIG_MAXSIG) 603 DB_ERROR(("Signal number out of range\n")); 604 605 /* 606 * Find the process in question. allproc_lock is not needed 607 * since we're in DDB. 608 */ 609 /* sx_slock(&allproc_lock); */ 610 FOREACH_PROC_IN_SYSTEM(p) 611 if (p->p_pid == pid) 612 break; 613 /* sx_sunlock(&allproc_lock); */ 614 if (p == NULL) 615 DB_ERROR(("Can't find process with pid %ld\n", (long) pid)); 616 617 /* If it's already locked, bail; otherwise, do the deed. */ 618 if (PROC_TRYLOCK(p) == 0) 619 DB_ERROR(("Can't lock process with pid %ld\n", (long) pid)); 620 else { 621 psignal(p, sig); 622 PROC_UNLOCK(p); 623 } 624 625 out: 626 db_radix = old_radix; 627 #undef DB_ERROR 628 } 629 630 static void 631 db_reset(dummy1, dummy2, dummy3, dummy4) 632 db_expr_t dummy1; 633 boolean_t dummy2; 634 db_expr_t dummy3; 635 char * dummy4; 636 { 637 638 cpu_reset(); 639 } 640 641 static void 642 db_watchdog(dummy1, dummy2, dummy3, dummy4) 643 db_expr_t dummy1; 644 boolean_t dummy2; 645 db_expr_t dummy3; 646 char * dummy4; 647 { 648 int i; 649 650 /* 651 * XXX: It might make sense to be able to set the watchdog to a 652 * XXX: timeout here so that failure or hang as a result of subsequent 653 * XXX: ddb commands could be recovered by a reset. 654 */ 655 656 EVENTHANDLER_INVOKE(watchdog_list, 0, &i); 657 } 658 659 static void 660 db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4) 661 { 662 663 if (kdb_dbbe_select("gdb") != 0) 664 db_printf("The remote GDB backend could not be selected.\n"); 665 else 666 db_printf("Step to enter the remote GDB backend.\n"); 667 } 668 669 static void 670 db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif) 671 { 672 struct thread *td; 673 db_expr_t radix; 674 pid_t pid; 675 int t; 676 677 /* 678 * We parse our own arguments. We don't like the default radix. 679 */ 680 radix = db_radix; 681 db_radix = 10; 682 hastid = db_expression(&tid); 683 t = db_read_token(); 684 if (t == tCOMMA) { 685 if (!db_expression(&count)) { 686 db_printf("Count missing\n"); 687 db_flush_lex(); 688 return; 689 } 690 } else { 691 db_unread_token(t); 692 count = -1; 693 } 694 db_skip_to_eol(); 695 db_radix = radix; 696 697 if (hastid) { 698 td = kdb_thr_lookup((lwpid_t)tid); 699 if (td == NULL) 700 td = kdb_thr_from_pid((pid_t)tid); 701 if (td == NULL) { 702 db_printf("Thread %d not found\n", (int)tid); 703 return; 704 } 705 } else 706 td = kdb_thread; 707 if (td->td_proc != NULL) 708 pid = td->td_proc->p_pid; 709 else 710 pid = -1; 711 db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td); 712 db_trace_thread(td, count); 713 } 714 715 static void 716 db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, 717 char *dummy4) 718 { 719 struct proc *p; 720 struct thread *td; 721 jmp_buf jb; 722 void *prev_jb; 723 724 FOREACH_PROC_IN_SYSTEM(p) { 725 prev_jb = kdb_jmpbuf(jb); 726 if (setjmp(jb) == 0) { 727 FOREACH_THREAD_IN_PROC(p, td) { 728 db_printf("\nTracing command %s pid %d tid %ld td %p\n", 729 p->p_comm, p->p_pid, (long)td->td_tid, td); 730 db_trace_thread(td, -1); 731 if (db_pager_quit) { 732 kdb_jmpbuf(prev_jb); 733 return; 734 } 735 } 736 } 737 kdb_jmpbuf(prev_jb); 738 } 739 } 740