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