1 /* 2 * Mach Operating System 3 * Copyright (c) 1991,1990 Carnegie Mellon University 4 * All Rights Reserved. 5 * 6 * Permission to use, copy, modify and distribute this software and its 7 * documentation is hereby granted, provided that both the copyright 8 * notice and this permission notice appear in all copies of the 9 * software, derivative works or modified versions, and any portions 10 * thereof, and that both notices appear in supporting documentation. 11 * 12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 15 * 16 * Carnegie Mellon requests users of this software to return to 17 * 18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 19 * School of Computer Science 20 * Carnegie Mellon University 21 * Pittsburgh PA 15213-3890 22 * 23 * any improvements or extensions that they make and grant Carnegie the 24 * rights to redistribute these changes. 25 * 26 * $FreeBSD$ 27 */ 28 29 /* 30 * Author: David B. Golub, Carnegie Mellon University 31 * Date: 7/90 32 */ 33 34 /* 35 * Command dispatcher. 36 */ 37 #include <sys/param.h> 38 #include <sys/linker_set.h> 39 #include <sys/reboot.h> 40 #include <sys/systm.h> 41 #include <sys/cons.h> 42 43 #include <ddb/ddb.h> 44 #include <ddb/db_command.h> 45 #include <ddb/db_lex.h> 46 #include <ddb/db_output.h> 47 48 #include <machine/md_var.h> 49 #include <machine/setjmp.h> 50 51 /* 52 * Exported global variables 53 */ 54 boolean_t db_cmd_loop_done; 55 db_addr_t db_dot; 56 jmp_buf db_jmpbuf; 57 db_addr_t db_last_addr; 58 db_addr_t db_prev; 59 db_addr_t db_next; 60 61 SET_DECLARE(db_cmd_set, struct command); 62 SET_DECLARE(db_show_cmd_set, struct command); 63 64 static db_cmdfcn_t db_fncall; 65 static db_cmdfcn_t db_gdb; 66 static db_cmdfcn_t db_reset; 67 68 /* XXX this is actually forward-static. */ 69 extern struct command db_show_cmds[]; 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 static 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 static void db_cmd_list __P((struct command *table, 100 struct command **aux_tablep, 101 struct command **aux_tablep_end)); 102 static int db_cmd_search __P((char *name, struct command *table, 103 struct command **aux_tablep, 104 struct command **aux_tablep_end, 105 struct command **cmdp)); 106 static void db_command __P((struct command **last_cmdp, 107 struct command *cmd_table, 108 struct command **aux_cmd_tablep, 109 struct command **aux_cmd_tablep_end)); 110 111 /* 112 * Search for command prefix. 113 */ 114 static int 115 db_cmd_search(name, table, aux_tablep, aux_tablep_end, cmdp) 116 char * name; 117 struct command *table; 118 struct command **aux_tablep; 119 struct command **aux_tablep_end; 120 struct command **cmdp; /* out */ 121 { 122 struct command *cmd; 123 struct command **aux_cmdp; 124 int result = CMD_NONE; 125 126 for (cmd = table; cmd->name != 0; cmd++) { 127 register char *lp; 128 register char *rp; 129 register int c; 130 131 lp = name; 132 rp = cmd->name; 133 while ((c = *lp) == *rp) { 134 if (c == 0) { 135 /* complete match */ 136 *cmdp = cmd; 137 return (CMD_UNIQUE); 138 } 139 lp++; 140 rp++; 141 } 142 if (c == 0) { 143 /* end of name, not end of command - 144 partial match */ 145 if (result == CMD_FOUND) { 146 result = CMD_AMBIGUOUS; 147 /* but keep looking for a full match - 148 this lets us match single letters */ 149 } 150 else { 151 *cmdp = cmd; 152 result = CMD_FOUND; 153 } 154 } 155 } 156 if (result == CMD_NONE && aux_tablep != 0) 157 /* XXX repeat too much code. */ 158 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) { 159 register char *lp; 160 register char *rp; 161 register int c; 162 163 lp = name; 164 rp = (*aux_cmdp)->name; 165 while ((c = *lp) == *rp) { 166 if (c == 0) { 167 /* complete match */ 168 *cmdp = *aux_cmdp; 169 return (CMD_UNIQUE); 170 } 171 lp++; 172 rp++; 173 } 174 if (c == 0) { 175 /* end of name, not end of command - 176 partial match */ 177 if (result == CMD_FOUND) { 178 result = CMD_AMBIGUOUS; 179 /* but keep looking for a full match - 180 this lets us match single letters */ 181 } 182 else { 183 *cmdp = *aux_cmdp; 184 result = CMD_FOUND; 185 } 186 } 187 } 188 if (result == CMD_NONE) { 189 /* check for 'help' */ 190 if (name[0] == 'h' && name[1] == 'e' 191 && name[2] == 'l' && name[3] == 'p') 192 result = CMD_HELP; 193 } 194 return (result); 195 } 196 197 static void 198 db_cmd_list(table, aux_tablep, aux_tablep_end) 199 struct command *table; 200 struct command **aux_tablep; 201 struct command **aux_tablep_end; 202 { 203 register struct command *cmd; 204 register struct command **aux_cmdp; 205 206 for (cmd = table; cmd->name != 0; cmd++) { 207 db_printf("%-12s", cmd->name); 208 db_end_line(); 209 } 210 if (aux_tablep == 0) 211 return; 212 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) { 213 db_printf("%-12s", (*aux_cmdp)->name); 214 db_end_line(); 215 } 216 } 217 218 static void 219 db_command(last_cmdp, cmd_table, aux_cmd_tablep, aux_cmd_tablep_end) 220 struct command **last_cmdp; /* IN_OUT */ 221 struct command *cmd_table; 222 struct command **aux_cmd_tablep; 223 struct command **aux_cmd_tablep_end; 224 { 225 struct command *cmd; 226 int t; 227 char modif[TOK_STRING_SIZE]; 228 db_expr_t addr, count; 229 boolean_t have_addr = FALSE; 230 int result; 231 232 t = db_read_token(); 233 if (t == tEOL) { 234 /* empty line repeats last command, at 'next' */ 235 cmd = *last_cmdp; 236 addr = (db_expr_t)db_next; 237 have_addr = FALSE; 238 count = 1; 239 modif[0] = '\0'; 240 } 241 else if (t == tEXCL) { 242 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0); 243 return; 244 } 245 else if (t != tIDENT) { 246 db_printf("?\n"); 247 db_flush_lex(); 248 return; 249 } 250 else { 251 /* 252 * Search for command 253 */ 254 while (cmd_table) { 255 result = db_cmd_search(db_tok_string, 256 cmd_table, 257 aux_cmd_tablep, 258 aux_cmd_tablep_end, 259 &cmd); 260 switch (result) { 261 case CMD_NONE: 262 db_printf("No such command\n"); 263 db_flush_lex(); 264 return; 265 case CMD_AMBIGUOUS: 266 db_printf("Ambiguous\n"); 267 db_flush_lex(); 268 return; 269 case CMD_HELP: 270 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end); 271 db_flush_lex(); 272 return; 273 default: 274 break; 275 } 276 if ((cmd_table = cmd->more) != 0) { 277 /* XXX usually no more aux's. */ 278 aux_cmd_tablep = 0; 279 if (cmd_table == db_show_cmds) 280 aux_cmd_tablep = SET_BEGIN(db_show_cmd_set); 281 aux_cmd_tablep_end = SET_LIMIT(db_show_cmd_set); 282 283 t = db_read_token(); 284 if (t != tIDENT) { 285 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end); 286 db_flush_lex(); 287 return; 288 } 289 } 290 } 291 292 if ((cmd->flag & CS_OWN) == 0) { 293 /* 294 * Standard syntax: 295 * command [/modifier] [addr] [,count] 296 */ 297 t = db_read_token(); 298 if (t == tSLASH) { 299 t = db_read_token(); 300 if (t != tIDENT) { 301 db_printf("Bad modifier\n"); 302 db_flush_lex(); 303 return; 304 } 305 db_strcpy(modif, db_tok_string); 306 } 307 else { 308 db_unread_token(t); 309 modif[0] = '\0'; 310 } 311 312 if (db_expression(&addr)) { 313 db_dot = (db_addr_t) addr; 314 db_last_addr = db_dot; 315 have_addr = TRUE; 316 } 317 else { 318 addr = (db_expr_t) db_dot; 319 have_addr = FALSE; 320 } 321 t = db_read_token(); 322 if (t == tCOMMA) { 323 if (!db_expression(&count)) { 324 db_printf("Count missing\n"); 325 db_flush_lex(); 326 return; 327 } 328 } 329 else { 330 db_unread_token(t); 331 count = -1; 332 } 333 if ((cmd->flag & CS_MORE) == 0) { 334 db_skip_to_eol(); 335 } 336 } 337 } 338 *last_cmdp = cmd; 339 if (cmd != 0) { 340 /* 341 * Execute the command. 342 */ 343 (*cmd->fcn)(addr, have_addr, count, modif); 344 345 if (cmd->flag & CS_SET_DOT) { 346 /* 347 * If command changes dot, set dot to 348 * previous address displayed (if 'ed' style). 349 */ 350 if (db_ed_style) { 351 db_dot = db_prev; 352 } 353 else { 354 db_dot = db_next; 355 } 356 } 357 else { 358 /* 359 * If command does not change dot, 360 * set 'next' location to be the same. 361 */ 362 db_next = db_dot; 363 } 364 } 365 } 366 367 /* 368 * 'show' commands 369 */ 370 371 static struct command db_show_all_cmds[] = { 372 #if 0 373 { "threads", db_show_all_threads, 0, 0 }, 374 #endif 375 { "procs", db_ps, 0, 0 }, 376 { (char *)0 } 377 }; 378 379 static struct command db_show_cmds[] = { 380 { "all", 0, 0, db_show_all_cmds }, 381 { "registers", db_show_regs, 0, 0 }, 382 { "breaks", db_listbreak_cmd, 0, 0 }, 383 #if 0 384 { "thread", db_show_one_thread, 0, 0 }, 385 #endif 386 #if 0 387 { "port", ipc_port_print, 0, 0 }, 388 #endif 389 { (char *)0, } 390 }; 391 392 static struct command db_command_table[] = { 393 { "print", db_print_cmd, 0, 0 }, 394 { "p", db_print_cmd, 0, 0 }, 395 { "examine", db_examine_cmd, CS_SET_DOT, 0 }, 396 { "x", db_examine_cmd, CS_SET_DOT, 0 }, 397 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 }, 398 { "set", db_set_cmd, CS_OWN, 0 }, 399 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 }, 400 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 }, 401 { "delete", db_delete_cmd, 0, 0 }, 402 { "d", db_delete_cmd, 0, 0 }, 403 { "break", db_breakpoint_cmd, 0, 0 }, 404 { "dwatch", db_deletewatch_cmd, 0, 0 }, 405 { "watch", db_watchpoint_cmd, CS_MORE,0 }, 406 { "dhwatch", db_deletehwatch_cmd, 0, 0 }, 407 { "hwatch", db_hwatchpoint_cmd, 0, 0 }, 408 { "step", db_single_step_cmd, 0, 0 }, 409 { "s", db_single_step_cmd, 0, 0 }, 410 { "continue", db_continue_cmd, 0, 0 }, 411 { "c", db_continue_cmd, 0, 0 }, 412 { "until", db_trace_until_call_cmd,0, 0 }, 413 { "next", db_trace_until_matching_cmd,0, 0 }, 414 { "match", db_trace_until_matching_cmd,0, 0 }, 415 { "trace", db_stack_trace_cmd, 0, 0 }, 416 { "call", db_fncall, CS_OWN, 0 }, 417 { "show", 0, 0, db_show_cmds }, 418 { "ps", db_ps, 0, 0 }, 419 { "gdb", db_gdb, 0, 0 }, 420 { "reset", db_reset, 0, 0 }, 421 { (char *)0, } 422 }; 423 424 static struct command *db_last_command = 0; 425 426 #if 0 427 void 428 db_help_cmd() 429 { 430 struct command *cmd = db_command_table; 431 432 while (cmd->name != 0) { 433 db_printf("%-12s", cmd->name); 434 db_end_line(); 435 cmd++; 436 } 437 } 438 #endif 439 440 /* 441 * At least one non-optional command must be implemented using 442 * DB_COMMAND() so that db_cmd_set gets created. Here is one. 443 */ 444 DB_COMMAND(panic, db_panic) 445 { 446 panic("from debugger"); 447 } 448 449 void 450 db_command_loop() 451 { 452 /* 453 * Initialize 'prev' and 'next' to dot. 454 */ 455 db_prev = db_dot; 456 db_next = db_dot; 457 458 db_cmd_loop_done = 0; 459 while (!db_cmd_loop_done) { 460 461 (void) setjmp(db_jmpbuf); 462 if (db_print_position() != 0) 463 db_printf("\n"); 464 465 db_printf("db> "); 466 (void) db_read_line(); 467 468 db_command(&db_last_command, db_command_table, 469 SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set)); 470 } 471 } 472 473 void 474 db_error(s) 475 char *s; 476 { 477 if (s) 478 db_printf("%s", s); 479 db_flush_lex(); 480 longjmp(db_jmpbuf, 1); 481 } 482 483 484 /* 485 * Call random function: 486 * !expr(arg,arg,arg) 487 */ 488 static void 489 db_fncall(dummy1, dummy2, dummy3, dummy4) 490 db_expr_t dummy1; 491 boolean_t dummy2; 492 db_expr_t dummy3; 493 char * dummy4; 494 { 495 db_expr_t fn_addr; 496 #define MAXARGS 11 /* XXX only 10 are passed */ 497 db_expr_t args[MAXARGS]; 498 int nargs = 0; 499 db_expr_t retval; 500 typedef db_expr_t fcn_10args_t __P((db_expr_t, db_expr_t, db_expr_t, 501 db_expr_t, db_expr_t, db_expr_t, 502 db_expr_t, db_expr_t, db_expr_t, 503 db_expr_t)); 504 fcn_10args_t *func; 505 int t; 506 507 if (!db_expression(&fn_addr)) { 508 db_printf("Bad function\n"); 509 db_flush_lex(); 510 return; 511 } 512 func = (fcn_10args_t *)fn_addr; /* XXX */ 513 514 t = db_read_token(); 515 if (t == tLPAREN) { 516 if (db_expression(&args[0])) { 517 nargs++; 518 while ((t = db_read_token()) == tCOMMA) { 519 if (nargs == MAXARGS) { 520 db_printf("Too many arguments\n"); 521 db_flush_lex(); 522 return; 523 } 524 if (!db_expression(&args[nargs])) { 525 db_printf("Argument missing\n"); 526 db_flush_lex(); 527 return; 528 } 529 nargs++; 530 } 531 db_unread_token(t); 532 } 533 if (db_read_token() != tRPAREN) { 534 db_printf("?\n"); 535 db_flush_lex(); 536 return; 537 } 538 } 539 db_skip_to_eol(); 540 541 while (nargs < MAXARGS) { 542 args[nargs++] = 0; 543 } 544 545 retval = (*func)(args[0], args[1], args[2], args[3], args[4], 546 args[5], args[6], args[7], args[8], args[9] ); 547 db_printf("%#lr\n", (long)retval); 548 } 549 550 /* Enter GDB remote protocol debugger on the next trap. */ 551 552 dev_t gdbdev = NODEV; 553 cn_getc_t *gdb_getc; 554 cn_putc_t *gdb_putc; 555 556 static void 557 db_gdb (dummy1, dummy2, dummy3, dummy4) 558 db_expr_t dummy1; 559 boolean_t dummy2; 560 db_expr_t dummy3; 561 char * dummy4; 562 { 563 564 if (gdbdev == NODEV) { 565 db_printf("No gdb port enabled. Set flag 0x80 on desired port\n"); 566 db_printf("in your configuration file (currently sio only).\n"); 567 return; 568 } 569 boothowto ^= RB_GDB; 570 571 db_printf("Next trap will enter %s\n", 572 boothowto & RB_GDB ? "GDB remote protocol mode" 573 : "DDB debugger"); 574 } 575 576 static void 577 db_reset(dummy1, dummy2, dummy3, dummy4) 578 db_expr_t dummy1; 579 boolean_t dummy2; 580 db_expr_t dummy3; 581 char * dummy4; 582 { 583 584 cpu_reset(); 585 } 586