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 { (char *)0, } 145 }; 146 147 static struct command_table db_command_table = { 148 db_commands, 149 SET_BEGIN(db_cmd_set), 150 SET_LIMIT(db_cmd_set) 151 }; 152 153 static struct command *db_last_command = 0; 154 155 /* 156 * if 'ed' style: 'dot' is set at start of last item printed, 157 * and '+' points to next line. 158 * Otherwise: 'dot' points to next item, '..' points to last. 159 */ 160 static boolean_t db_ed_style = TRUE; 161 162 /* 163 * Utility routine - discard tokens through end-of-line. 164 */ 165 void 166 db_skip_to_eol() 167 { 168 int t; 169 do { 170 t = db_read_token(); 171 } while (t != tEOL); 172 } 173 174 /* 175 * Results of command search. 176 */ 177 #define CMD_UNIQUE 0 178 #define CMD_FOUND 1 179 #define CMD_NONE 2 180 #define CMD_AMBIGUOUS 3 181 #define CMD_HELP 4 182 183 static void db_cmd_match(char *name, struct command *cmd, 184 struct command **cmdp, int *resultp); 185 static void db_cmd_list(struct command_table *table); 186 static int db_cmd_search(char *name, struct command_table *table, 187 struct command **cmdp); 188 static void db_command(struct command **last_cmdp, 189 struct command_table *cmd_table); 190 191 /* 192 * Helper function to match a single command. 193 */ 194 static void 195 db_cmd_match(name, cmd, cmdp, resultp) 196 char * name; 197 struct command *cmd; 198 struct command **cmdp; /* out */ 199 int * resultp; 200 { 201 char *lp, *rp; 202 int c; 203 204 lp = name; 205 rp = cmd->name; 206 while ((c = *lp) == *rp) { 207 if (c == 0) { 208 /* complete match */ 209 *cmdp = cmd; 210 *resultp = CMD_UNIQUE; 211 return; 212 } 213 lp++; 214 rp++; 215 } 216 if (c == 0) { 217 /* end of name, not end of command - 218 partial match */ 219 if (*resultp == CMD_FOUND) { 220 *resultp = CMD_AMBIGUOUS; 221 /* but keep looking for a full match - 222 this lets us match single letters */ 223 } else { 224 *cmdp = cmd; 225 *resultp = CMD_FOUND; 226 } 227 } 228 } 229 230 /* 231 * Search for command prefix. 232 */ 233 static int 234 db_cmd_search(name, table, cmdp) 235 char * name; 236 struct command_table *table; 237 struct command **cmdp; /* out */ 238 { 239 struct command *cmd; 240 struct command **aux_cmdp; 241 int result = CMD_NONE; 242 243 for (cmd = table->table; cmd->name != 0; cmd++) { 244 db_cmd_match(name, cmd, cmdp, &result); 245 if (result == CMD_UNIQUE) 246 return (CMD_UNIQUE); 247 } 248 if (table->aux_tablep != NULL) 249 for (aux_cmdp = table->aux_tablep; 250 aux_cmdp < table->aux_tablep_end; 251 aux_cmdp++) { 252 db_cmd_match(name, *aux_cmdp, cmdp, &result); 253 if (result == CMD_UNIQUE) 254 return (CMD_UNIQUE); 255 } 256 if (result == CMD_NONE) { 257 /* check for 'help' */ 258 if (name[0] == 'h' && name[1] == 'e' 259 && name[2] == 'l' && name[3] == 'p') 260 result = CMD_HELP; 261 } 262 return (result); 263 } 264 265 static void 266 db_cmd_list(table) 267 struct command_table *table; 268 { 269 register struct command *cmd; 270 register struct command **aux_cmdp; 271 272 for (cmd = table->table; cmd->name != 0; cmd++) { 273 db_printf("%-12s", cmd->name); 274 db_end_line(12); 275 } 276 if (table->aux_tablep == NULL) 277 return; 278 for (aux_cmdp = table->aux_tablep; aux_cmdp < table->aux_tablep_end; 279 aux_cmdp++) { 280 db_printf("%-12s", (*aux_cmdp)->name); 281 db_end_line(12); 282 } 283 } 284 285 static void 286 db_command(last_cmdp, cmd_table) 287 struct command **last_cmdp; /* IN_OUT */ 288 struct command_table *cmd_table; 289 { 290 struct command *cmd; 291 int t; 292 char modif[TOK_STRING_SIZE]; 293 db_expr_t addr, count; 294 boolean_t have_addr = FALSE; 295 int result; 296 297 t = db_read_token(); 298 if (t == tEOL) { 299 /* empty line repeats last command, at 'next' */ 300 cmd = *last_cmdp; 301 addr = (db_expr_t)db_next; 302 have_addr = FALSE; 303 count = 1; 304 modif[0] = '\0'; 305 } 306 else if (t == tEXCL) { 307 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0); 308 return; 309 } 310 else if (t != tIDENT) { 311 db_printf("?\n"); 312 db_flush_lex(); 313 return; 314 } 315 else { 316 /* 317 * Search for command 318 */ 319 while (cmd_table) { 320 result = db_cmd_search(db_tok_string, 321 cmd_table, 322 &cmd); 323 switch (result) { 324 case CMD_NONE: 325 db_printf("No such command\n"); 326 db_flush_lex(); 327 return; 328 case CMD_AMBIGUOUS: 329 db_printf("Ambiguous\n"); 330 db_flush_lex(); 331 return; 332 case CMD_HELP: 333 db_cmd_list(cmd_table); 334 db_flush_lex(); 335 return; 336 default: 337 break; 338 } 339 if ((cmd_table = cmd->more) != NULL) { 340 t = db_read_token(); 341 if (t != tIDENT) { 342 db_cmd_list(cmd_table); 343 db_flush_lex(); 344 return; 345 } 346 } 347 } 348 349 if ((cmd->flag & CS_OWN) == 0) { 350 /* 351 * Standard syntax: 352 * command [/modifier] [addr] [,count] 353 */ 354 t = db_read_token(); 355 if (t == tSLASH) { 356 t = db_read_token(); 357 if (t != tIDENT) { 358 db_printf("Bad modifier\n"); 359 db_flush_lex(); 360 return; 361 } 362 db_strcpy(modif, db_tok_string); 363 } 364 else { 365 db_unread_token(t); 366 modif[0] = '\0'; 367 } 368 369 if (db_expression(&addr)) { 370 db_dot = (db_addr_t) addr; 371 db_last_addr = db_dot; 372 have_addr = TRUE; 373 } 374 else { 375 addr = (db_expr_t) db_dot; 376 have_addr = FALSE; 377 } 378 t = db_read_token(); 379 if (t == tCOMMA) { 380 if (!db_expression(&count)) { 381 db_printf("Count missing\n"); 382 db_flush_lex(); 383 return; 384 } 385 } 386 else { 387 db_unread_token(t); 388 count = -1; 389 } 390 if ((cmd->flag & CS_MORE) == 0) { 391 db_skip_to_eol(); 392 } 393 } 394 } 395 *last_cmdp = cmd; 396 if (cmd != 0) { 397 /* 398 * Execute the command. 399 */ 400 db_enable_pager(); 401 (*cmd->fcn)(addr, have_addr, count, modif); 402 db_disable_pager(); 403 404 if (cmd->flag & CS_SET_DOT) { 405 /* 406 * If command changes dot, set dot to 407 * previous address displayed (if 'ed' style). 408 */ 409 if (db_ed_style) { 410 db_dot = db_prev; 411 } 412 else { 413 db_dot = db_next; 414 } 415 } 416 else { 417 /* 418 * If command does not change dot, 419 * set 'next' location to be the same. 420 */ 421 db_next = db_dot; 422 } 423 } 424 } 425 426 /* 427 * At least one non-optional command must be implemented using 428 * DB_COMMAND() so that db_cmd_set gets created. Here is one. 429 */ 430 DB_COMMAND(panic, db_panic) 431 { 432 db_disable_pager(); 433 panic("from debugger"); 434 } 435 436 void 437 db_command_loop() 438 { 439 /* 440 * Initialize 'prev' and 'next' to dot. 441 */ 442 db_prev = db_dot; 443 db_next = db_dot; 444 445 db_cmd_loop_done = 0; 446 while (!db_cmd_loop_done) { 447 if (db_print_position() != 0) 448 db_printf("\n"); 449 450 db_printf("db> "); 451 (void) db_read_line(); 452 453 db_command(&db_last_command, &db_command_table); 454 } 455 } 456 457 void 458 db_error(s) 459 const char *s; 460 { 461 if (s) 462 db_printf("%s", s); 463 db_flush_lex(); 464 kdb_reenter(); 465 } 466 467 468 /* 469 * Call random function: 470 * !expr(arg,arg,arg) 471 */ 472 473 /* The generic implementation supports a maximum of 10 arguments. */ 474 typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t, 475 db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t); 476 477 static __inline int 478 db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[]) 479 { 480 __db_f *f = (__db_f *)addr; 481 482 if (nargs > 10) { 483 db_printf("Too many arguments (max 10)\n"); 484 return (0); 485 } 486 *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5], 487 args[6], args[7], args[8], args[9]); 488 return (1); 489 } 490 491 static void 492 db_fncall(dummy1, dummy2, dummy3, dummy4) 493 db_expr_t dummy1; 494 boolean_t dummy2; 495 db_expr_t dummy3; 496 char * dummy4; 497 { 498 db_expr_t fn_addr; 499 db_expr_t args[DB_MAXARGS]; 500 int nargs = 0; 501 db_expr_t retval; 502 int t; 503 504 if (!db_expression(&fn_addr)) { 505 db_printf("Bad function\n"); 506 db_flush_lex(); 507 return; 508 } 509 510 t = db_read_token(); 511 if (t == tLPAREN) { 512 if (db_expression(&args[0])) { 513 nargs++; 514 while ((t = db_read_token()) == tCOMMA) { 515 if (nargs == DB_MAXARGS) { 516 db_printf("Too many arguments (max %d)\n", DB_MAXARGS); 517 db_flush_lex(); 518 return; 519 } 520 if (!db_expression(&args[nargs])) { 521 db_printf("Argument missing\n"); 522 db_flush_lex(); 523 return; 524 } 525 nargs++; 526 } 527 db_unread_token(t); 528 } 529 if (db_read_token() != tRPAREN) { 530 db_printf("?\n"); 531 db_flush_lex(); 532 return; 533 } 534 } 535 db_skip_to_eol(); 536 db_disable_pager(); 537 538 if (DB_CALL(fn_addr, &retval, nargs, args)) 539 db_printf("= %#lr\n", (long)retval); 540 } 541 542 static void 543 db_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4) 544 { 545 546 cpu_halt(); 547 } 548 549 static void 550 db_kill(dummy1, dummy2, dummy3, dummy4) 551 db_expr_t dummy1; 552 boolean_t dummy2; 553 db_expr_t dummy3; 554 char * dummy4; 555 { 556 db_expr_t old_radix, pid, sig; 557 struct proc *p; 558 559 #define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0) 560 561 /* 562 * PIDs and signal numbers are typically represented in base 563 * 10, so make that the default here. It can, of course, be 564 * overridden by specifying a prefix. 565 */ 566 old_radix = db_radix; 567 db_radix = 10; 568 /* Retrieve arguments. */ 569 if (!db_expression(&sig)) 570 DB_ERROR(("Missing signal number\n")); 571 if (!db_expression(&pid)) 572 DB_ERROR(("Missing process ID\n")); 573 db_skip_to_eol(); 574 if (sig < 0 || sig > _SIG_MAXSIG) 575 DB_ERROR(("Signal number out of range\n")); 576 577 /* 578 * Find the process in question. allproc_lock is not needed 579 * since we're in DDB. 580 */ 581 /* sx_slock(&allproc_lock); */ 582 LIST_FOREACH(p, &allproc, p_list) 583 if (p->p_pid == pid) 584 break; 585 /* sx_sunlock(&allproc_lock); */ 586 if (p == NULL) 587 DB_ERROR(("Can't find process with pid %ld\n", (long) pid)); 588 589 /* If it's already locked, bail; otherwise, do the deed. */ 590 if (PROC_TRYLOCK(p) == 0) 591 DB_ERROR(("Can't lock process with pid %ld\n", (long) pid)); 592 else { 593 psignal(p, sig); 594 PROC_UNLOCK(p); 595 } 596 597 out: 598 db_radix = old_radix; 599 #undef DB_ERROR 600 } 601 602 static void 603 db_reset(dummy1, dummy2, dummy3, dummy4) 604 db_expr_t dummy1; 605 boolean_t dummy2; 606 db_expr_t dummy3; 607 char * dummy4; 608 { 609 610 cpu_reset(); 611 } 612 613 static void 614 db_watchdog(dummy1, dummy2, dummy3, dummy4) 615 db_expr_t dummy1; 616 boolean_t dummy2; 617 db_expr_t dummy3; 618 char * dummy4; 619 { 620 int i; 621 622 /* 623 * XXX: It might make sense to be able to set the watchdog to a 624 * XXX: timeout here so that failure or hang as a result of subsequent 625 * XXX: ddb commands could be recovered by a reset. 626 */ 627 628 EVENTHANDLER_INVOKE(watchdog_list, 0, &i); 629 } 630 631 static void 632 db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4) 633 { 634 635 if (kdb_dbbe_select("gdb") != 0) 636 db_printf("The remote GDB backend could not be selected.\n"); 637 else 638 db_printf("Step to enter the remote GDB backend.\n"); 639 } 640 641 static void 642 db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif) 643 { 644 struct thread *td; 645 db_expr_t radix; 646 pid_t pid; 647 int t; 648 649 /* 650 * We parse our own arguments. We don't like the default radix. 651 */ 652 radix = db_radix; 653 db_radix = 10; 654 hastid = db_expression(&tid); 655 t = db_read_token(); 656 if (t == tCOMMA) { 657 if (!db_expression(&count)) { 658 db_printf("Count missing\n"); 659 db_flush_lex(); 660 return; 661 } 662 } else { 663 db_unread_token(t); 664 count = -1; 665 } 666 db_skip_to_eol(); 667 db_radix = radix; 668 669 if (hastid) { 670 td = kdb_thr_lookup((lwpid_t)tid); 671 if (td == NULL) 672 td = kdb_thr_from_pid((pid_t)tid); 673 if (td == NULL) { 674 db_printf("Thread %d not found\n", (int)tid); 675 return; 676 } 677 } else 678 td = kdb_thread; 679 if (td->td_proc != NULL) 680 pid = td->td_proc->p_pid; 681 else 682 pid = -1; 683 db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td); 684 db_trace_thread(td, count); 685 } 686 687 static void 688 db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, 689 char *dummy4) 690 { 691 struct proc *p; 692 struct thread *td; 693 jmp_buf jb; 694 void *prev_jb; 695 696 LIST_FOREACH(p, &allproc, p_list) { 697 prev_jb = kdb_jmpbuf(jb); 698 if (setjmp(jb) == 0) { 699 FOREACH_THREAD_IN_PROC(p, td) { 700 db_printf("\nTracing command %s pid %d tid %ld td %p\n", 701 p->p_comm, p->p_pid, (long)td->td_tid, td); 702 db_trace_thread(td, -1); 703 if (db_pager_quit) { 704 kdb_jmpbuf(prev_jb); 705 return; 706 } 707 } 708 } 709 kdb_jmpbuf(prev_jb); 710 } 711 } 712