db_command.c revision 163134
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 163134 2006-10-08 18:15:08Z bde $"); 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_kill; 72static db_cmdfcn_t db_reset; 73static db_cmdfcn_t db_stack_trace; 74static db_cmdfcn_t db_stack_trace_all; 75static db_cmdfcn_t db_watchdog; 76 77/* 78 * 'show' commands 79 */ 80 81static struct command db_show_all_cmds[] = { 82 { "procs", db_ps, 0, 0 }, 83 { (char *)0 } 84}; 85 86static struct command_table db_show_all_table = { 87 db_show_all_cmds 88}; 89 90static 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 98static 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 104static 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 142static struct command_table db_command_table = { 143 db_commands, 144 SET_BEGIN(db_cmd_set), 145 SET_LIMIT(db_cmd_set) 146}; 147 148static 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 */ 155static boolean_t db_ed_style = TRUE; 156 157/* 158 * Utility routine - discard tokens through end-of-line. 159 */ 160void 161db_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 178static void db_cmd_match(char *name, struct command *cmd, 179 struct command **cmdp, int *resultp); 180static void db_cmd_list(struct command_table *table); 181static int db_cmd_search(char *name, struct command_table *table, 182 struct command **cmdp); 183static void db_command(struct command **last_cmdp, 184 struct command_table *cmd_table); 185 186/* 187 * Helper function to match a single command. 188 */ 189static void 190db_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 */ 228static int 229db_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 260static void 261db_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(12); 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(12); 277 } 278} 279 280static void 281db_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 db_enable_pager(); 396 (*cmd->fcn)(addr, have_addr, count, modif); 397 db_disable_pager(); 398 399 if (cmd->flag & CS_SET_DOT) { 400 /* 401 * If command changes dot, set dot to 402 * previous address displayed (if 'ed' style). 403 */ 404 if (db_ed_style) { 405 db_dot = db_prev; 406 } 407 else { 408 db_dot = db_next; 409 } 410 } 411 else { 412 /* 413 * If command does not change dot, 414 * set 'next' location to be the same. 415 */ 416 db_next = db_dot; 417 } 418 } 419} 420 421/* 422 * At least one non-optional command must be implemented using 423 * DB_COMMAND() so that db_cmd_set gets created. Here is one. 424 */ 425DB_COMMAND(panic, db_panic) 426{ 427 db_disable_pager(); 428 panic("from debugger"); 429} 430 431void 432db_command_loop() 433{ 434 /* 435 * Initialize 'prev' and 'next' to dot. 436 */ 437 db_prev = db_dot; 438 db_next = db_dot; 439 440 db_cmd_loop_done = 0; 441 while (!db_cmd_loop_done) { 442 if (db_print_position() != 0) 443 db_printf("\n"); 444 445 db_printf("db> "); 446 (void) db_read_line(); 447 448 db_command(&db_last_command, &db_command_table); 449 } 450} 451 452void 453db_error(s) 454 const char *s; 455{ 456 if (s) 457 db_printf("%s", s); 458 db_flush_lex(); 459 kdb_reenter(); 460} 461 462 463/* 464 * Call random function: 465 * !expr(arg,arg,arg) 466 */ 467 468/* The generic implementation supports a maximum of 10 arguments. */ 469typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t, 470 db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t); 471 472static __inline int 473db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[]) 474{ 475 __db_f *f = (__db_f *)addr; 476 477 if (nargs > 10) { 478 db_printf("Too many arguments (max 10)\n"); 479 return (0); 480 } 481 *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5], 482 args[6], args[7], args[8], args[9]); 483 return (1); 484} 485 486static void 487db_fncall(dummy1, dummy2, dummy3, dummy4) 488 db_expr_t dummy1; 489 boolean_t dummy2; 490 db_expr_t dummy3; 491 char * dummy4; 492{ 493 db_expr_t fn_addr; 494 db_expr_t args[DB_MAXARGS]; 495 int nargs = 0; 496 db_expr_t retval; 497 int t; 498 499 if (!db_expression(&fn_addr)) { 500 db_printf("Bad function\n"); 501 db_flush_lex(); 502 return; 503 } 504 505 t = db_read_token(); 506 if (t == tLPAREN) { 507 if (db_expression(&args[0])) { 508 nargs++; 509 while ((t = db_read_token()) == tCOMMA) { 510 if (nargs == DB_MAXARGS) { 511 db_printf("Too many arguments (max %d)\n", DB_MAXARGS); 512 db_flush_lex(); 513 return; 514 } 515 if (!db_expression(&args[nargs])) { 516 db_printf("Argument missing\n"); 517 db_flush_lex(); 518 return; 519 } 520 nargs++; 521 } 522 db_unread_token(t); 523 } 524 if (db_read_token() != tRPAREN) { 525 db_printf("?\n"); 526 db_flush_lex(); 527 return; 528 } 529 } 530 db_skip_to_eol(); 531 db_disable_pager(); 532 533 if (DB_CALL(fn_addr, &retval, nargs, args)) 534 db_printf("= %#lr\n", (long)retval); 535} 536 537static void 538db_kill(dummy1, dummy2, dummy3, dummy4) 539 db_expr_t dummy1; 540 boolean_t dummy2; 541 db_expr_t dummy3; 542 char * dummy4; 543{ 544 db_expr_t old_radix, pid, sig; 545 struct proc *p; 546 547#define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0) 548 549 /* 550 * PIDs and signal numbers are typically represented in base 551 * 10, so make that the default here. It can, of course, be 552 * overridden by specifying a prefix. 553 */ 554 old_radix = db_radix; 555 db_radix = 10; 556 /* Retrieve arguments. */ 557 if (!db_expression(&sig)) 558 DB_ERROR(("Missing signal number\n")); 559 if (!db_expression(&pid)) 560 DB_ERROR(("Missing process ID\n")); 561 db_skip_to_eol(); 562 if (sig < 0 || sig > _SIG_MAXSIG) 563 DB_ERROR(("Signal number out of range\n")); 564 565 /* 566 * Find the process in question. allproc_lock is not needed 567 * since we're in DDB. 568 */ 569 /* sx_slock(&allproc_lock); */ 570 LIST_FOREACH(p, &allproc, p_list) 571 if (p->p_pid == pid) 572 break; 573 /* sx_sunlock(&allproc_lock); */ 574 if (p == NULL) 575 DB_ERROR(("Can't find process with pid %ld\n", (long) pid)); 576 577 /* If it's already locked, bail; otherwise, do the deed. */ 578 if (PROC_TRYLOCK(p) == 0) 579 DB_ERROR(("Can't lock process with pid %ld\n", (long) pid)); 580 else { 581 psignal(p, sig); 582 PROC_UNLOCK(p); 583 } 584 585out: 586 db_radix = old_radix; 587#undef DB_ERROR 588} 589 590static void 591db_reset(dummy1, dummy2, dummy3, dummy4) 592 db_expr_t dummy1; 593 boolean_t dummy2; 594 db_expr_t dummy3; 595 char * dummy4; 596{ 597 598 cpu_reset(); 599} 600 601static void 602db_watchdog(dummy1, dummy2, dummy3, dummy4) 603 db_expr_t dummy1; 604 boolean_t dummy2; 605 db_expr_t dummy3; 606 char * dummy4; 607{ 608 int i; 609 610 /* 611 * XXX: It might make sense to be able to set the watchdog to a 612 * XXX: timeout here so that failure or hang as a result of subsequent 613 * XXX: ddb commands could be recovered by a reset. 614 */ 615 616 EVENTHANDLER_INVOKE(watchdog_list, 0, &i); 617} 618 619static void 620db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4) 621{ 622 623 if (kdb_dbbe_select("gdb") != 0) 624 db_printf("The remote GDB backend could not be selected.\n"); 625 else 626 db_printf("Step to enter the remote GDB backend.\n"); 627} 628 629static void 630db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif) 631{ 632 struct thread *td; 633 db_expr_t radix; 634 pid_t pid; 635 int t; 636 637 /* 638 * We parse our own arguments. We don't like the default radix. 639 */ 640 radix = db_radix; 641 db_radix = 10; 642 hastid = db_expression(&tid); 643 t = db_read_token(); 644 if (t == tCOMMA) { 645 if (!db_expression(&count)) { 646 db_printf("Count missing\n"); 647 db_flush_lex(); 648 return; 649 } 650 } else { 651 db_unread_token(t); 652 count = -1; 653 } 654 db_skip_to_eol(); 655 db_radix = radix; 656 657 if (hastid) { 658 td = kdb_thr_lookup((lwpid_t)tid); 659 if (td == NULL) 660 td = kdb_thr_from_pid((pid_t)tid); 661 if (td == NULL) { 662 db_printf("Thread %d not found\n", (int)tid); 663 return; 664 } 665 } else 666 td = kdb_thread; 667 if (td->td_proc != NULL) 668 pid = td->td_proc->p_pid; 669 else 670 pid = -1; 671 db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td); 672 db_trace_thread(td, count); 673} 674 675static void 676db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, 677 char *dummy4) 678{ 679 struct proc *p; 680 struct thread *td; 681 682 LIST_FOREACH(p, &allproc, 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 (db_pager_quit) 688 return; 689 } 690 } 691} 692