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