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.9 2005/12/23 21:35:44 swildner 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 db_addr_t db_dot; 58 jmp_buf db_jmpbuf; 59 db_addr_t db_last_addr; 60 db_addr_t db_prev; 61 db_addr_t db_next; 62 63 SET_DECLARE(db_cmd_set, struct command); 64 SET_DECLARE(db_show_cmd_set, struct command); 65 66 static db_cmdfcn_t db_fncall; 67 static db_cmdfcn_t db_gdb; 68 static db_cmdfcn_t db_reset; 69 70 static 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(void) 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 struct command **aux_tablep_end); 103 static int db_cmd_search (char *name, struct command *table, 104 struct command **aux_tablep, 105 struct command **aux_tablep_end, 106 struct command **cmdp); 107 static void db_command (struct command **last_cmdp, 108 struct command *cmd_table, 109 struct command **aux_cmd_tablep, 110 struct command **aux_cmd_tablep_end); 111 112 /* 113 * Search for command prefix. 114 * 115 * Parameters: 116 * cmdp: out 117 */ 118 static int 119 db_cmd_search(char *name, struct command *table, struct command **aux_tablep, 120 struct command **aux_tablep_end, struct command **cmdp) 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 char *lp; 128 char *rp; 129 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 char *lp; 160 char *rp; 161 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(struct command *table, struct command **aux_tablep, 199 struct command **aux_tablep_end) 200 { 201 struct command *cmd; 202 struct command **aux_cmdp; 203 204 for (cmd = table; cmd->name != 0; cmd++) { 205 db_printf("%-12s", cmd->name); 206 db_end_line(); 207 } 208 if (aux_tablep == 0) 209 return; 210 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) { 211 db_printf("%-12s", (*aux_cmdp)->name); 212 db_end_line(); 213 } 214 } 215 216 /* 217 * Parameters: 218 * last_cmdp: IN_OUT 219 */ 220 static void 221 db_command(struct command **last_cmdp, struct command *cmd_table, 222 struct command **aux_cmd_tablep, struct command **aux_cmd_tablep_end) 223 { 224 struct command *cmd; 225 int t; 226 char modif[TOK_STRING_SIZE]; 227 db_expr_t addr, count; 228 boolean_t have_addr = FALSE; 229 int result; 230 231 t = db_read_token(); 232 if (t == tEOL) { 233 /* empty line repeats last command, at 'next' */ 234 cmd = *last_cmdp; 235 addr = (db_expr_t)db_next; 236 have_addr = FALSE; 237 count = 1; 238 modif[0] = '\0'; 239 } 240 else if (t == tEXCL) { 241 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0); 242 return; 243 } 244 else if (t != tIDENT) { 245 db_printf("?\n"); 246 db_flush_lex(); 247 return; 248 } 249 else { 250 /* 251 * Search for command 252 */ 253 while (cmd_table) { 254 result = db_cmd_search(db_tok_string, 255 cmd_table, 256 aux_cmd_tablep, 257 aux_cmd_tablep_end, 258 &cmd); 259 switch (result) { 260 case CMD_NONE: 261 db_printf("No such command\n"); 262 db_flush_lex(); 263 return; 264 case CMD_AMBIGUOUS: 265 db_printf("Ambiguous\n"); 266 db_flush_lex(); 267 return; 268 case CMD_HELP: 269 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end); 270 db_flush_lex(); 271 return; 272 default: 273 break; 274 } 275 if ((cmd_table = cmd->more) != 0) { 276 /* XXX usually no more aux's. */ 277 aux_cmd_tablep = 0; 278 if (cmd_table == db_show_cmds) { 279 aux_cmd_tablep = SET_BEGIN(db_show_cmd_set); 280 aux_cmd_tablep_end = SET_LIMIT(db_show_cmd_set); 281 } 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 && t != tNUMBER) { 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 { "where", db_stack_trace_cmd, 0, 0 }, 417 { "call", db_fncall, CS_OWN, 0 }, 418 { "show", 0, 0, db_show_cmds }, 419 { "ps", db_ps, 0, 0 }, 420 { "gdb", db_gdb, 0, 0 }, 421 { "reset", db_reset, 0, 0 }, 422 { (char *)0, } 423 }; 424 425 static struct command *db_last_command = 0; 426 427 #if 0 428 void 429 db_help_cmd(void) 430 { 431 struct command *cmd = db_command_table; 432 433 while (cmd->name != 0) { 434 db_printf("%-12s", cmd->name); 435 db_end_line(); 436 cmd++; 437 } 438 } 439 #endif 440 441 /* 442 * At least one non-optional command must be implemented using 443 * DB_COMMAND() so that db_cmd_set gets created. Here is one. 444 */ 445 DB_COMMAND(panic, db_panic) 446 { 447 panic("from debugger"); 448 } 449 450 void 451 db_command_loop(void) 452 { 453 /* 454 * Initialize 'prev' and 'next' to dot. 455 */ 456 db_prev = db_dot; 457 db_next = db_dot; 458 459 db_cmd_loop_done = 0; 460 while (!db_cmd_loop_done) { 461 462 setjmp(db_jmpbuf); 463 if (db_print_position() != 0) 464 db_printf("\n"); 465 466 db_printf("db> "); 467 db_read_line(); 468 469 db_command(&db_last_command, db_command_table, 470 SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set)); 471 } 472 } 473 474 void 475 db_error(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(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4) 490 { 491 db_expr_t fn_addr; 492 #define MAXARGS 11 /* XXX only 10 are passed */ 493 db_expr_t args[MAXARGS]; 494 int nargs = 0; 495 db_expr_t retval; 496 typedef db_expr_t fcn_10args_t (db_expr_t, db_expr_t, db_expr_t, 497 db_expr_t, db_expr_t, db_expr_t, 498 db_expr_t, db_expr_t, db_expr_t, 499 db_expr_t); 500 fcn_10args_t *func; 501 int t; 502 503 if (!db_expression(&fn_addr)) { 504 db_printf("Bad function\n"); 505 db_flush_lex(); 506 return; 507 } 508 func = (fcn_10args_t *)fn_addr; /* XXX */ 509 510 t = db_read_token(); 511 if (t == tLPAREN) { 512 if (db_expression(&args[0])) { 513 nargs++; 514 while ((t = db_read_token()) == tCOMMA) { 515 if (nargs == MAXARGS) { 516 db_printf("Too many arguments\n"); 517 db_flush_lex(); 518 return; 519 } 520 if (!db_expression(&args[nargs])) { 521 db_printf("Argument missing\n"); 522 db_flush_lex(); 523 return; 524 } 525 nargs++; 526 } 527 db_unread_token(t); 528 } 529 if (db_read_token() != tRPAREN) { 530 db_printf("?\n"); 531 db_flush_lex(); 532 return; 533 } 534 } 535 db_skip_to_eol(); 536 537 while (nargs < MAXARGS) { 538 args[nargs++] = 0; 539 } 540 541 retval = (*func)(args[0], args[1], args[2], args[3], args[4], 542 args[5], args[6], args[7], args[8], args[9] ); 543 db_printf("%#lr\n", (long)retval); 544 } 545 546 /* Enter GDB remote protocol debugger on the next trap. */ 547 548 dev_t gdbdev = NODEV; 549 cn_getc_t *gdb_getc; 550 cn_putc_t *gdb_putc; 551 552 static void 553 db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4) 554 { 555 556 if (gdbdev == NODEV) { 557 db_printf("No gdb port enabled. Set flag 0x80 on desired port\n"); 558 db_printf("in your configuration file (currently sio only).\n"); 559 return; 560 } 561 boothowto ^= RB_GDB; 562 563 db_printf("Next trap will enter %s\n", 564 boothowto & RB_GDB ? "GDB remote protocol mode" 565 : "DDB debugger"); 566 } 567 568 static void 569 db_reset(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char * dummy4) 570 { 571 cpu_reset(); 572 } 573