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