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