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