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