1 /* $NetBSD: db_command.c,v 1.66 2002/08/26 11:34:28 scw 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.66 2002/08/26 11:34:28 scw 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, items, 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 items = 0; 313 314 columns = db_max_width / width; 315 if (columns == 0) 316 columns = 1; 317 lines = (numcmds + columns - 1) / columns; 318 for (i = 0; i < lines; i++) { 319 for (j = 0; j < columns; j++) { 320 p = table[j * lines + i].name; 321 if (p) 322 db_printf("%s", p); 323 if (j * lines + i + lines >= numcmds) { 324 db_putchar('\n'); 325 break; 326 } 327 w = strlen(p); 328 while (w < width) { 329 w = DB_NEXT_TAB(w); 330 db_putchar('\t'); 331 } 332 } 333 } 334 } 335 336 static void 337 db_command(const struct db_command **last_cmdp, 338 const struct db_command *cmd_table) 339 { 340 const struct db_command *cmd; 341 int t; 342 char modif[TOK_STRING_SIZE]; 343 db_expr_t addr, count; 344 boolean_t have_addr = FALSE; 345 int result; 346 347 static db_expr_t last_count = 0; 348 349 t = db_read_token(); 350 if ((t == tEOL) || (t == tCOMMA)) { 351 /* 352 * An empty line repeats last command, at 'next'. 353 * Only a count repeats the last command with the new count. 354 */ 355 cmd = *last_cmdp; 356 addr = (db_expr_t)db_next; 357 if (t == tCOMMA) { 358 if (!db_expression(&count)) { 359 db_printf("Count missing\n"); 360 db_flush_lex(); 361 return; 362 } 363 } else 364 count = last_count; 365 have_addr = FALSE; 366 modif[0] = '\0'; 367 db_skip_to_eol(); 368 } else if (t == tEXCL) { 369 db_fncall(0, 0, 0, NULL); 370 return; 371 } else if (t != tIDENT) { 372 db_printf("?\n"); 373 db_flush_lex(); 374 return; 375 } else { 376 /* 377 * Search for command 378 */ 379 while (cmd_table) { 380 result = db_cmd_search(db_tok_string, cmd_table, &cmd); 381 switch (result) { 382 case CMD_NONE: 383 db_printf("No such command\n"); 384 db_flush_lex(); 385 return; 386 case CMD_AMBIGUOUS: 387 db_printf("Ambiguous\n"); 388 db_flush_lex(); 389 return; 390 case CMD_HELP: 391 db_cmd_list(cmd_table); 392 db_flush_lex(); 393 return; 394 default: 395 break; 396 } 397 if ((cmd_table = cmd->more) != 0) { 398 t = db_read_token(); 399 if (t != tIDENT) { 400 db_cmd_list(cmd_table); 401 db_flush_lex(); 402 return; 403 } 404 } 405 } 406 407 if ((cmd->flag & CS_OWN) == 0) { 408 /* 409 * Standard syntax: 410 * command [/modifier] [addr] [,count] 411 */ 412 t = db_read_token(); 413 if (t == tSLASH) { 414 t = db_read_token(); 415 if (t != tIDENT) { 416 db_printf("Bad modifier\n"); 417 db_flush_lex(); 418 return; 419 } 420 db_strcpy(modif, db_tok_string); 421 } else { 422 db_unread_token(t); 423 modif[0] = '\0'; 424 } 425 426 if (db_expression(&addr)) { 427 db_dot = (db_addr_t) addr; 428 db_last_addr = db_dot; 429 have_addr = TRUE; 430 } else { 431 addr = (db_expr_t) db_dot; 432 have_addr = FALSE; 433 } 434 t = db_read_token(); 435 if (t == tCOMMA) { 436 if (!db_expression(&count)) { 437 db_printf("Count missing\n"); 438 db_flush_lex(); 439 return; 440 } 441 } else { 442 db_unread_token(t); 443 count = -1; 444 } 445 if ((cmd->flag & CS_MORE) == 0) { 446 db_skip_to_eol(); 447 } 448 } 449 } 450 *last_cmdp = cmd; 451 last_count = count; 452 if (cmd != 0) { 453 /* 454 * Execute the command. 455 */ 456 (*cmd->fcn)(addr, have_addr, count, modif); 457 458 if (cmd->flag & CS_SET_DOT) { 459 /* 460 * If command changes dot, set dot to 461 * previous address displayed (if 'ed' style). 462 */ 463 if (db_ed_style) 464 db_dot = db_prev; 465 else 466 db_dot = db_next; 467 } else { 468 /* 469 * If command does not change dot, 470 * set 'next' location to be the same. 471 */ 472 db_next = db_dot; 473 } 474 } 475 } 476 477 /*ARGSUSED*/ 478 static void 479 db_map_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 480 { 481 boolean_t full = FALSE; 482 483 if (modif[0] == 'f') 484 full = TRUE; 485 486 if (have_addr == FALSE) 487 addr = (db_expr_t)(intptr_t) kernel_map; 488 489 uvm_map_printit((struct vm_map *)(intptr_t) addr, full, db_printf); 490 } 491 492 /*ARGSUSED*/ 493 static void 494 db_malloc_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 495 { 496 497 #ifdef MALLOC_DEBUG 498 if (!have_addr) 499 addr = 0; 500 501 debug_malloc_printit(db_printf, (vaddr_t) addr); 502 #else 503 db_printf("The kernel is not built with the MALLOC_DEBUG option.\n"); 504 #endif /* MALLOC_DEBUG */ 505 } 506 507 /*ARGSUSED*/ 508 static void 509 db_object_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 510 { 511 boolean_t full = FALSE; 512 513 if (modif[0] == 'f') 514 full = TRUE; 515 516 uvm_object_printit((struct uvm_object *)(intptr_t) addr, full, 517 db_printf); 518 } 519 520 /*ARGSUSED*/ 521 static void 522 db_page_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 523 { 524 boolean_t full = FALSE; 525 526 if (modif[0] == 'f') 527 full = TRUE; 528 529 uvm_page_printit((struct vm_page *)(intptr_t) addr, full, db_printf); 530 } 531 532 /*ARGSUSED*/ 533 static void 534 db_buf_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 535 { 536 boolean_t full = FALSE; 537 538 if (modif[0] == 'f') 539 full = TRUE; 540 541 vfs_buf_print((struct buf *)(intptr_t) addr, full, db_printf); 542 } 543 544 /*ARGSUSED*/ 545 static void 546 db_event_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 547 { 548 boolean_t full = FALSE; 549 550 if (modif[0] == 'f') 551 full = TRUE; 552 553 event_print(full, db_printf); 554 } 555 556 /*ARGSUSED*/ 557 static void 558 db_vnode_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 559 { 560 boolean_t full = FALSE; 561 562 if (modif[0] == 'f') 563 full = TRUE; 564 565 vfs_vnode_print((struct vnode *)(intptr_t) addr, full, db_printf); 566 } 567 568 /*ARGSUSED*/ 569 static void 570 db_pool_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 571 { 572 573 pool_printit((struct pool *)(intptr_t) addr, modif, db_printf); 574 } 575 576 /*ARGSUSED*/ 577 static void 578 db_namecache_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, 579 char *modif) 580 { 581 582 namecache_print((struct vnode *)(intptr_t) addr, db_printf); 583 } 584 585 /*ARGSUSED*/ 586 static void 587 db_uvmexp_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 588 { 589 590 uvmexp_print(db_printf); 591 } 592 593 /* 594 * Call random function: 595 * !expr(arg,arg,arg) 596 */ 597 /*ARGSUSED*/ 598 static void 599 db_fncall(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 600 { 601 db_expr_t fn_addr; 602 #define MAXARGS 11 603 db_expr_t args[MAXARGS]; 604 int nargs = 0; 605 db_expr_t retval; 606 db_expr_t (*func)(db_expr_t, ...); 607 int t; 608 609 if (!db_expression(&fn_addr)) { 610 db_printf("Bad function\n"); 611 db_flush_lex(); 612 return; 613 } 614 func = (db_expr_t (*)(db_expr_t, ...))(intptr_t) fn_addr; 615 616 t = db_read_token(); 617 if (t == tLPAREN) { 618 if (db_expression(&args[0])) { 619 nargs++; 620 while ((t = db_read_token()) == tCOMMA) { 621 if (nargs == MAXARGS) { 622 db_printf("Too many arguments\n"); 623 db_flush_lex(); 624 return; 625 } 626 if (!db_expression(&args[nargs])) { 627 db_printf("Argument missing\n"); 628 db_flush_lex(); 629 return; 630 } 631 nargs++; 632 } 633 db_unread_token(t); 634 } 635 if (db_read_token() != tRPAREN) { 636 db_printf("?\n"); 637 db_flush_lex(); 638 return; 639 } 640 } 641 db_skip_to_eol(); 642 643 while (nargs < MAXARGS) { 644 args[nargs++] = 0; 645 } 646 647 retval = (*func)(args[0], args[1], args[2], args[3], args[4], 648 args[5], args[6], args[7], args[8], args[9]); 649 db_printf("%s\n", db_num_to_str(retval)); 650 } 651 652 static void 653 db_reboot_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 654 { 655 db_expr_t bootflags; 656 657 /* Flags, default to RB_AUTOBOOT */ 658 if (!db_expression(&bootflags)) 659 bootflags = (db_expr_t)RB_AUTOBOOT; 660 if (db_read_token() != tEOL) { 661 db_error("?\n"); 662 /*NOTREACHED*/ 663 } 664 /* 665 * We are leaving DDB, never to return upward. 666 * Clear db_recover so that we can debug faults in functions 667 * called from cpu_reboot. 668 */ 669 db_recover = 0; 670 cpu_reboot((int)bootflags, NULL); 671 } 672 673 static void 674 db_sifting_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 675 { 676 int mode, t; 677 678 t = db_read_token(); 679 if (t == tSLASH) { 680 t = db_read_token(); 681 if (t != tIDENT) { 682 bad_modifier: 683 db_printf("Bad modifier\n"); 684 db_flush_lex(); 685 return; 686 } 687 if (!strcmp(db_tok_string, "F")) 688 mode = 'F'; 689 else 690 goto bad_modifier; 691 t = db_read_token(); 692 } else 693 mode = 0; 694 695 if (t == tIDENT) 696 db_sifting(db_tok_string, mode); 697 else { 698 db_printf("Bad argument (non-string)\n"); 699 db_flush_lex(); 700 } 701 } 702 703 static void 704 db_stack_trace_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 705 { 706 707 if (count == -1) 708 count = 65535; 709 710 db_stack_trace_print(addr, have_addr, count, modif, db_printf); 711 } 712 713 static void 714 db_sync_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 715 { 716 717 /* 718 * We are leaving DDB, never to return upward. 719 * Clear db_recover so that we can debug faults in functions 720 * called from cpu_reboot. 721 */ 722 db_recover = 0; 723 cpu_reboot(RB_DUMP, NULL); 724 } 725