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