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