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