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