1 /* $NetBSD: db_command.c,v 1.67 2003/01/20 07:20:01 simonb Exp $ */ 2 3 /* 4 * Mach Operating System 5 * Copyright (c) 1991,1990 Carnegie Mellon University 6 * All Rights Reserved. 7 * 8 * Permission to use, copy, modify and distribute this software and its 9 * documentation is hereby granted, provided that both the copyright 10 * notice and this permission notice appear in all copies of the 11 * software, derivative works or modified versions, and any portions 12 * thereof, and that both notices appear in supporting documentation. 13 * 14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 17 * 18 * Carnegie Mellon requests users of this software to return to 19 * 20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 21 * School of Computer Science 22 * Carnegie Mellon University 23 * Pittsburgh PA 15213-3890 24 * 25 * any improvements or extensions that they make and grant Carnegie the 26 * rights to redistribute these changes. 27 */ 28 29 /* 30 * Command dispatcher. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.67 2003/01/20 07:20:01 simonb Exp $"); 35 36 #include "opt_ddb.h" 37 #include "opt_inet.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/reboot.h> 42 #include <sys/device.h> 43 #include <sys/malloc.h> 44 #include <sys/namei.h> 45 #include <sys/pool.h> 46 #include <sys/proc.h> 47 #include <sys/vnode.h> 48 49 #include <machine/db_machdep.h> /* type definitions */ 50 51 #if defined(_KERNEL_OPT) 52 #include "opt_multiprocessor.h" 53 #endif 54 #ifdef MULTIPROCESSOR 55 #include <machine/cpu.h> 56 #endif 57 58 #include <ddb/db_lex.h> 59 #include <ddb/db_output.h> 60 #include <ddb/db_command.h> 61 #include <ddb/db_break.h> 62 #include <ddb/db_watch.h> 63 #include <ddb/db_run.h> 64 #include <ddb/db_variables.h> 65 #include <ddb/db_interface.h> 66 #include <ddb/db_sym.h> 67 #include <ddb/db_extern.h> 68 69 #include <uvm/uvm_extern.h> 70 #include <uvm/uvm_ddb.h> 71 72 #include "arp.h" 73 74 /* 75 * Results of command search. 76 */ 77 #define CMD_UNIQUE 0 78 #define CMD_FOUND 1 79 #define CMD_NONE 2 80 #define CMD_AMBIGUOUS 3 81 #define CMD_HELP 4 82 83 /* 84 * Exported global variables 85 */ 86 boolean_t db_cmd_loop_done; 87 label_t *db_recover; 88 db_addr_t db_dot; 89 db_addr_t db_last_addr; 90 db_addr_t db_prev; 91 db_addr_t db_next; 92 93 /* 94 * if 'ed' style: 'dot' is set at start of last item printed, 95 * and '+' points to next line. 96 * Otherwise: 'dot' points to next item, '..' points to last. 97 */ 98 static boolean_t db_ed_style = TRUE; 99 100 static void db_buf_print_cmd(db_expr_t, int, db_expr_t, char *); 101 static void db_cmd_list(const struct db_command *); 102 static int db_cmd_search(const char *, const struct db_command *, 103 const struct db_command **); 104 static void db_command(const struct db_command **, 105 const struct db_command *); 106 static void db_event_print_cmd(db_expr_t, int, db_expr_t, char *); 107 static void db_fncall(db_expr_t, int, db_expr_t, char *); 108 static void db_malloc_print_cmd(db_expr_t, int, db_expr_t, char *); 109 static void db_map_print_cmd(db_expr_t, int, db_expr_t, char *); 110 static void db_namecache_print_cmd(db_expr_t, int, db_expr_t, char *); 111 static void db_object_print_cmd(db_expr_t, int, db_expr_t, char *); 112 static void db_page_print_cmd(db_expr_t, int, db_expr_t, char *); 113 static void db_pool_print_cmd(db_expr_t, int, db_expr_t, char *); 114 static void db_reboot_cmd(db_expr_t, int, db_expr_t, char *); 115 static void db_sifting_cmd(db_expr_t, int, db_expr_t, char *); 116 static void db_stack_trace_cmd(db_expr_t, int, db_expr_t, char *); 117 static void db_sync_cmd(db_expr_t, int, db_expr_t, char *); 118 static void db_uvmexp_print_cmd(db_expr_t, int, db_expr_t, char *); 119 static void db_vnode_print_cmd(db_expr_t, int, db_expr_t, char *); 120 121 /* 122 * 'show' commands 123 */ 124 125 static const struct db_command db_show_all_cmds[] = { 126 { "callout", db_show_callout, 0, NULL }, 127 { "procs", db_show_all_procs, 0, NULL }, 128 { NULL, NULL, 0, NULL } 129 }; 130 131 static const struct db_command db_show_cmds[] = { 132 { "all", NULL, 0, db_show_all_cmds }, 133 #if defined(INET) && (NARP > 0) 134 { "arptab", db_show_arptab, 0, NULL }, 135 #endif 136 { "breaks", db_listbreak_cmd, 0, NULL }, 137 { "buf", db_buf_print_cmd, 0, NULL }, 138 { "event", db_event_print_cmd, 0, NULL }, 139 { "malloc", db_malloc_print_cmd, 0, NULL }, 140 { "map", db_map_print_cmd, 0, NULL }, 141 { "ncache", db_namecache_print_cmd, 0, NULL }, 142 { "object", db_object_print_cmd, 0, NULL }, 143 { "page", db_page_print_cmd, 0, NULL }, 144 { "pool", db_pool_print_cmd, 0, NULL }, 145 { "registers", db_show_regs, 0, NULL }, 146 { "uvmexp", db_uvmexp_print_cmd, 0, NULL }, 147 { "vnode", db_vnode_print_cmd, 0, NULL }, 148 { "watches", db_listwatch_cmd, 0, NULL }, 149 { NULL, NULL, 0, NULL } 150 }; 151 152 static const struct db_command db_command_table[] = { 153 { "break", db_breakpoint_cmd, 0, NULL }, 154 { "c", db_continue_cmd, 0, NULL }, 155 { "call", db_fncall, CS_OWN, NULL }, 156 { "callout", db_show_callout, 0, NULL }, 157 { "continue", db_continue_cmd, 0, NULL }, 158 { "d", db_delete_cmd, 0, NULL }, 159 { "delete", db_delete_cmd, 0, NULL }, 160 { "dmesg", db_dmesg, 0, NULL }, 161 { "dwatch", db_deletewatch_cmd, 0, NULL }, 162 { "examine", db_examine_cmd, CS_SET_DOT, NULL }, 163 { "kill", db_kill_proc, CS_OWN, NULL }, 164 #ifdef DB_MACHINE_COMMANDS 165 { "machine", NULL, 0, db_machine_command_table }, 166 #endif 167 { "match", db_trace_until_matching_cmd,0, NULL }, 168 { "next", db_trace_until_matching_cmd,0, NULL }, 169 { "p", db_print_cmd, 0, NULL }, 170 { "print", db_print_cmd, 0, NULL }, 171 { "ps", db_show_all_procs, 0, NULL }, 172 { "reboot", db_reboot_cmd, CS_OWN, NULL }, 173 { "s", db_single_step_cmd, 0, NULL }, 174 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, NULL }, 175 { "set", db_set_cmd, CS_OWN, NULL }, 176 { "show", NULL, 0, db_show_cmds }, 177 { "sifting", db_sifting_cmd, CS_OWN, NULL }, 178 { "step", db_single_step_cmd, 0, NULL }, 179 { "sync", db_sync_cmd, CS_OWN, NULL }, 180 { "trace", db_stack_trace_cmd, 0, NULL }, 181 { "until", db_trace_until_call_cmd,0, NULL }, 182 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, NULL }, 183 { "watch", db_watchpoint_cmd, CS_MORE, NULL }, 184 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, NULL }, 185 { "x", db_examine_cmd, CS_SET_DOT, NULL }, 186 { NULL, NULL, 0, NULL } 187 }; 188 189 static const struct db_command *db_last_command = NULL; 190 191 /* 192 * Utility routine - discard tokens through end-of-line. 193 */ 194 void 195 db_skip_to_eol(void) 196 { 197 int t; 198 199 do { 200 t = db_read_token(); 201 } while (t != tEOL); 202 } 203 204 void 205 db_error(s) 206 char *s; 207 { 208 209 if (s) 210 db_printf("%s", s); 211 db_flush_lex(); 212 longjmp(db_recover); 213 } 214 215 void 216 db_command_loop(void) 217 { 218 label_t db_jmpbuf; 219 label_t *savejmp; 220 221 /* 222 * Initialize 'prev' and 'next' to dot. 223 */ 224 db_prev = db_dot; 225 db_next = db_dot; 226 227 db_cmd_loop_done = 0; 228 229 savejmp = db_recover; 230 db_recover = &db_jmpbuf; 231 (void) setjmp(&db_jmpbuf); 232 233 while (!db_cmd_loop_done) { 234 if (db_print_position() != 0) 235 db_printf("\n"); 236 db_output_line = 0; 237 238 239 #ifdef MULTIPROCESSOR 240 db_printf("db{%ld}> ", (long)cpu_number()); 241 #else 242 db_printf("db> "); 243 #endif 244 (void) db_read_line(); 245 246 db_command(&db_last_command, db_command_table); 247 } 248 249 db_recover = savejmp; 250 } 251 252 /* 253 * Search for command prefix. 254 */ 255 static int 256 db_cmd_search(const char *name, const struct db_command *table, 257 const struct db_command **cmdp) 258 { 259 const struct db_command *cmd; 260 int result = CMD_NONE; 261 262 for (cmd = table; cmd->name != 0; cmd++) { 263 const char *lp; 264 const char *rp; 265 int c; 266 267 lp = name; 268 rp = cmd->name; 269 while ((c = *lp) == *rp) { 270 if (c == 0) { 271 /* complete match */ 272 *cmdp = cmd; 273 return (CMD_UNIQUE); 274 } 275 lp++; 276 rp++; 277 } 278 if (c == 0) { 279 /* end of name, not end of command - 280 partial match */ 281 if (result == CMD_FOUND) { 282 result = CMD_AMBIGUOUS; 283 /* but keep looking for a full match - 284 this lets us match single letters */ 285 } else { 286 *cmdp = cmd; 287 result = CMD_FOUND; 288 } 289 } 290 } 291 if (result == CMD_NONE) { 292 /* check for 'help' */ 293 if (name[0] == 'h' && name[1] == 'e' 294 && name[2] == 'l' && name[3] == 'p') 295 result = CMD_HELP; 296 } 297 return (result); 298 } 299 300 static void 301 db_cmd_list(const struct db_command *table) 302 { 303 int i, j, w, columns, lines, width=0, numcmds; 304 const char *p; 305 306 for (numcmds = 0; table[numcmds].name != NULL; numcmds++) { 307 w = strlen(table[numcmds].name); 308 if (w > width) 309 width = w; 310 } 311 width = DB_NEXT_TAB(width); 312 313 columns = db_max_width / width; 314 if (columns == 0) 315 columns = 1; 316 lines = (numcmds + columns - 1) / columns; 317 for (i = 0; i < lines; i++) { 318 for (j = 0; j < columns; j++) { 319 p = table[j * lines + i].name; 320 if (p) 321 db_printf("%s", p); 322 if (j * lines + i + lines >= numcmds) { 323 db_putchar('\n'); 324 break; 325 } 326 w = strlen(p); 327 while (w < width) { 328 w = DB_NEXT_TAB(w); 329 db_putchar('\t'); 330 } 331 } 332 } 333 } 334 335 static void 336 db_command(const struct db_command **last_cmdp, 337 const struct db_command *cmd_table) 338 { 339 const struct db_command *cmd; 340 int t; 341 char modif[TOK_STRING_SIZE]; 342 db_expr_t addr, count; 343 boolean_t have_addr = FALSE; 344 int result; 345 346 static db_expr_t last_count = 0; 347 348 t = db_read_token(); 349 if ((t == tEOL) || (t == tCOMMA)) { 350 /* 351 * An empty line repeats last command, at 'next'. 352 * Only a count repeats the last command with the new count. 353 */ 354 cmd = *last_cmdp; 355 addr = (db_expr_t)db_next; 356 if (t == tCOMMA) { 357 if (!db_expression(&count)) { 358 db_printf("Count missing\n"); 359 db_flush_lex(); 360 return; 361 } 362 } else 363 count = last_count; 364 have_addr = FALSE; 365 modif[0] = '\0'; 366 db_skip_to_eol(); 367 } else if (t == tEXCL) { 368 db_fncall(0, 0, 0, NULL); 369 return; 370 } else if (t != tIDENT) { 371 db_printf("?\n"); 372 db_flush_lex(); 373 return; 374 } else { 375 /* 376 * Search for command 377 */ 378 while (cmd_table) { 379 result = db_cmd_search(db_tok_string, cmd_table, &cmd); 380 switch (result) { 381 case CMD_NONE: 382 db_printf("No such command\n"); 383 db_flush_lex(); 384 return; 385 case CMD_AMBIGUOUS: 386 db_printf("Ambiguous\n"); 387 db_flush_lex(); 388 return; 389 case CMD_HELP: 390 db_cmd_list(cmd_table); 391 db_flush_lex(); 392 return; 393 default: 394 break; 395 } 396 if ((cmd_table = cmd->more) != 0) { 397 t = db_read_token(); 398 if (t != tIDENT) { 399 db_cmd_list(cmd_table); 400 db_flush_lex(); 401 return; 402 } 403 } 404 } 405 406 if ((cmd->flag & CS_OWN) == 0) { 407 /* 408 * Standard syntax: 409 * command [/modifier] [addr] [,count] 410 */ 411 t = db_read_token(); 412 if (t == tSLASH) { 413 t = db_read_token(); 414 if (t != tIDENT) { 415 db_printf("Bad modifier\n"); 416 db_flush_lex(); 417 return; 418 } 419 db_strcpy(modif, db_tok_string); 420 } else { 421 db_unread_token(t); 422 modif[0] = '\0'; 423 } 424 425 if (db_expression(&addr)) { 426 db_dot = (db_addr_t) addr; 427 db_last_addr = db_dot; 428 have_addr = TRUE; 429 } else { 430 addr = (db_expr_t) db_dot; 431 have_addr = FALSE; 432 } 433 t = db_read_token(); 434 if (t == tCOMMA) { 435 if (!db_expression(&count)) { 436 db_printf("Count missing\n"); 437 db_flush_lex(); 438 return; 439 } 440 } else { 441 db_unread_token(t); 442 count = -1; 443 } 444 if ((cmd->flag & CS_MORE) == 0) { 445 db_skip_to_eol(); 446 } 447 } 448 } 449 *last_cmdp = cmd; 450 last_count = count; 451 if (cmd != 0) { 452 /* 453 * Execute the command. 454 */ 455 (*cmd->fcn)(addr, have_addr, count, modif); 456 457 if (cmd->flag & CS_SET_DOT) { 458 /* 459 * If command changes dot, set dot to 460 * previous address displayed (if 'ed' style). 461 */ 462 if (db_ed_style) 463 db_dot = db_prev; 464 else 465 db_dot = db_next; 466 } else { 467 /* 468 * If command does not change dot, 469 * set 'next' location to be the same. 470 */ 471 db_next = db_dot; 472 } 473 } 474 } 475 476 /*ARGSUSED*/ 477 static void 478 db_map_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 479 { 480 boolean_t full = FALSE; 481 482 if (modif[0] == 'f') 483 full = TRUE; 484 485 if (have_addr == FALSE) 486 addr = (db_expr_t)(intptr_t) kernel_map; 487 488 uvm_map_printit((struct vm_map *)(intptr_t) addr, full, db_printf); 489 } 490 491 /*ARGSUSED*/ 492 static void 493 db_malloc_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 494 { 495 496 #ifdef MALLOC_DEBUG 497 if (!have_addr) 498 addr = 0; 499 500 debug_malloc_printit(db_printf, (vaddr_t) addr); 501 #else 502 db_printf("The kernel is not built with the MALLOC_DEBUG option.\n"); 503 #endif /* MALLOC_DEBUG */ 504 } 505 506 /*ARGSUSED*/ 507 static void 508 db_object_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 509 { 510 boolean_t full = FALSE; 511 512 if (modif[0] == 'f') 513 full = TRUE; 514 515 uvm_object_printit((struct uvm_object *)(intptr_t) addr, full, 516 db_printf); 517 } 518 519 /*ARGSUSED*/ 520 static void 521 db_page_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 522 { 523 boolean_t full = FALSE; 524 525 if (modif[0] == 'f') 526 full = TRUE; 527 528 uvm_page_printit((struct vm_page *)(intptr_t) addr, full, db_printf); 529 } 530 531 /*ARGSUSED*/ 532 static void 533 db_buf_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 534 { 535 boolean_t full = FALSE; 536 537 if (modif[0] == 'f') 538 full = TRUE; 539 540 vfs_buf_print((struct buf *)(intptr_t) addr, full, db_printf); 541 } 542 543 /*ARGSUSED*/ 544 static void 545 db_event_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 546 { 547 boolean_t full = FALSE; 548 549 if (modif[0] == 'f') 550 full = TRUE; 551 552 event_print(full, db_printf); 553 } 554 555 /*ARGSUSED*/ 556 static void 557 db_vnode_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 558 { 559 boolean_t full = FALSE; 560 561 if (modif[0] == 'f') 562 full = TRUE; 563 564 vfs_vnode_print((struct vnode *)(intptr_t) addr, full, db_printf); 565 } 566 567 /*ARGSUSED*/ 568 static void 569 db_pool_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 570 { 571 572 pool_printit((struct pool *)(intptr_t) addr, modif, db_printf); 573 } 574 575 /*ARGSUSED*/ 576 static void 577 db_namecache_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, 578 char *modif) 579 { 580 581 namecache_print((struct vnode *)(intptr_t) addr, db_printf); 582 } 583 584 /*ARGSUSED*/ 585 static void 586 db_uvmexp_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 587 { 588 589 uvmexp_print(db_printf); 590 } 591 592 /* 593 * Call random function: 594 * !expr(arg,arg,arg) 595 */ 596 /*ARGSUSED*/ 597 static void 598 db_fncall(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 599 { 600 db_expr_t fn_addr; 601 #define MAXARGS 11 602 db_expr_t args[MAXARGS]; 603 int nargs = 0; 604 db_expr_t retval; 605 db_expr_t (*func)(db_expr_t, ...); 606 int t; 607 608 if (!db_expression(&fn_addr)) { 609 db_printf("Bad function\n"); 610 db_flush_lex(); 611 return; 612 } 613 func = (db_expr_t (*)(db_expr_t, ...))(intptr_t) fn_addr; 614 615 t = db_read_token(); 616 if (t == tLPAREN) { 617 if (db_expression(&args[0])) { 618 nargs++; 619 while ((t = db_read_token()) == tCOMMA) { 620 if (nargs == MAXARGS) { 621 db_printf("Too many arguments\n"); 622 db_flush_lex(); 623 return; 624 } 625 if (!db_expression(&args[nargs])) { 626 db_printf("Argument missing\n"); 627 db_flush_lex(); 628 return; 629 } 630 nargs++; 631 } 632 db_unread_token(t); 633 } 634 if (db_read_token() != tRPAREN) { 635 db_printf("?\n"); 636 db_flush_lex(); 637 return; 638 } 639 } 640 db_skip_to_eol(); 641 642 while (nargs < MAXARGS) { 643 args[nargs++] = 0; 644 } 645 646 retval = (*func)(args[0], args[1], args[2], args[3], args[4], 647 args[5], args[6], args[7], args[8], args[9]); 648 db_printf("%s\n", db_num_to_str(retval)); 649 } 650 651 static void 652 db_reboot_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 653 { 654 db_expr_t bootflags; 655 656 /* Flags, default to RB_AUTOBOOT */ 657 if (!db_expression(&bootflags)) 658 bootflags = (db_expr_t)RB_AUTOBOOT; 659 if (db_read_token() != tEOL) { 660 db_error("?\n"); 661 /*NOTREACHED*/ 662 } 663 /* 664 * We are leaving DDB, never to return upward. 665 * Clear db_recover so that we can debug faults in functions 666 * called from cpu_reboot. 667 */ 668 db_recover = 0; 669 cpu_reboot((int)bootflags, NULL); 670 } 671 672 static void 673 db_sifting_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 674 { 675 int mode, t; 676 677 t = db_read_token(); 678 if (t == tSLASH) { 679 t = db_read_token(); 680 if (t != tIDENT) { 681 bad_modifier: 682 db_printf("Bad modifier\n"); 683 db_flush_lex(); 684 return; 685 } 686 if (!strcmp(db_tok_string, "F")) 687 mode = 'F'; 688 else 689 goto bad_modifier; 690 t = db_read_token(); 691 } else 692 mode = 0; 693 694 if (t == tIDENT) 695 db_sifting(db_tok_string, mode); 696 else { 697 db_printf("Bad argument (non-string)\n"); 698 db_flush_lex(); 699 } 700 } 701 702 static void 703 db_stack_trace_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 704 { 705 706 if (count == -1) 707 count = 65535; 708 709 db_stack_trace_print(addr, have_addr, count, modif, db_printf); 710 } 711 712 static void 713 db_sync_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 714 { 715 716 /* 717 * We are leaving DDB, never to return upward. 718 * Clear db_recover so that we can debug faults in functions 719 * called from cpu_reboot. 720 */ 721 db_recover = 0; 722 cpu_reboot(RB_DUMP, NULL); 723 } 724