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.17 1995/12/07 12:44:48 davidg 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/systm.h> 39 40 #include <vm/vm.h> 41 #include <vm/vm_extern.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 <setjmp.h> 49 50 /* 51 * Exported global variables 52 */ 53 boolean_t db_cmd_loop_done; 54 jmp_buf db_jmpbuf; 55 db_addr_t db_dot; 56 db_addr_t db_last_addr; 57 db_addr_t db_prev; 58 db_addr_t db_next; 59 60 static db_cmdfcn_t db_fncall; 61 static db_cmdfcn_t db_panic; 62 /* 63 * if 'ed' style: 'dot' is set at start of last item printed, 64 * and '+' points to next line. 65 * Otherwise: 'dot' points to next item, '..' points to last. 66 */ 67 static boolean_t db_ed_style = TRUE; 68 69 /* 70 * Utility routine - discard tokens through end-of-line. 71 */ 72 void 73 db_skip_to_eol() 74 { 75 int t; 76 do { 77 t = db_read_token(); 78 } while (t != tEOL); 79 } 80 81 /* 82 * Command table 83 */ 84 struct command { 85 char * name; /* command name */ 86 db_cmdfcn_t *fcn; /* function to call */ 87 int flag; /* extra info: */ 88 #define CS_OWN 0x1 /* non-standard syntax */ 89 #define CS_MORE 0x2 /* standard syntax, but may have other 90 words at end */ 91 #define CS_SET_DOT 0x100 /* set dot after command */ 92 struct command *more; /* another level of command */ 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 __P((struct command *table)); 105 static int db_cmd_search __P((char *name, struct command *table, 106 struct command **cmdp)); 107 static void db_command __P((struct command **last_cmdp, 108 struct command *cmd_table)); 109 110 /* 111 * Search for command prefix. 112 */ 113 static int 114 db_cmd_search(name, table, cmdp) 115 char * name; 116 struct command *table; 117 struct command **cmdp; /* out */ 118 { 119 struct command *cmd; 120 int result = CMD_NONE; 121 122 for (cmd = table; cmd->name != 0; cmd++) { 123 register char *lp; 124 register char *rp; 125 register int c; 126 127 lp = name; 128 rp = cmd->name; 129 while ((c = *lp) == *rp) { 130 if (c == 0) { 131 /* complete match */ 132 *cmdp = cmd; 133 return (CMD_UNIQUE); 134 } 135 lp++; 136 rp++; 137 } 138 if (c == 0) { 139 /* end of name, not end of command - 140 partial match */ 141 if (result == CMD_FOUND) { 142 result = CMD_AMBIGUOUS; 143 /* but keep looking for a full match - 144 this lets us match single letters */ 145 } 146 else { 147 *cmdp = cmd; 148 result = CMD_FOUND; 149 } 150 } 151 } 152 if (result == CMD_NONE) { 153 /* check for 'help' */ 154 if (name[0] == 'h' && name[1] == 'e' 155 && name[2] == 'l' && name[3] == 'p') 156 result = CMD_HELP; 157 } 158 return (result); 159 } 160 161 static void 162 db_cmd_list(table) 163 struct command *table; 164 { 165 register struct command *cmd; 166 167 for (cmd = table; cmd->name != 0; cmd++) { 168 db_printf("%-12s", cmd->name); 169 db_end_line(); 170 } 171 } 172 173 static void 174 db_command(last_cmdp, cmd_table) 175 struct command **last_cmdp; /* IN_OUT */ 176 struct command *cmd_table; 177 { 178 struct command *cmd; 179 int t; 180 char modif[TOK_STRING_SIZE]; 181 db_expr_t addr, count; 182 boolean_t have_addr = FALSE; 183 int result; 184 185 t = db_read_token(); 186 if (t == tEOL) { 187 /* empty line repeats last command, at 'next' */ 188 cmd = *last_cmdp; 189 addr = (db_expr_t)db_next; 190 have_addr = FALSE; 191 count = 1; 192 modif[0] = '\0'; 193 } 194 else if (t == tEXCL) { 195 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0); 196 return; 197 } 198 else if (t != tIDENT) { 199 db_printf("?\n"); 200 db_flush_lex(); 201 return; 202 } 203 else { 204 /* 205 * Search for command 206 */ 207 while (cmd_table) { 208 result = db_cmd_search(db_tok_string, 209 cmd_table, 210 &cmd); 211 switch (result) { 212 case CMD_NONE: 213 db_printf("No such command\n"); 214 db_flush_lex(); 215 return; 216 case CMD_AMBIGUOUS: 217 db_printf("Ambiguous\n"); 218 db_flush_lex(); 219 return; 220 case CMD_HELP: 221 db_cmd_list(cmd_table); 222 db_flush_lex(); 223 return; 224 default: 225 break; 226 } 227 if ((cmd_table = cmd->more) != 0) { 228 t = db_read_token(); 229 if (t != tIDENT) { 230 db_cmd_list(cmd_table); 231 db_flush_lex(); 232 return; 233 } 234 } 235 } 236 237 if ((cmd->flag & CS_OWN) == 0) { 238 /* 239 * Standard syntax: 240 * command [/modifier] [addr] [,count] 241 */ 242 t = db_read_token(); 243 if (t == tSLASH) { 244 t = db_read_token(); 245 if (t != tIDENT) { 246 db_printf("Bad modifier\n"); 247 db_flush_lex(); 248 return; 249 } 250 db_strcpy(modif, db_tok_string); 251 } 252 else { 253 db_unread_token(t); 254 modif[0] = '\0'; 255 } 256 257 if (db_expression(&addr)) { 258 db_dot = (db_addr_t) addr; 259 db_last_addr = db_dot; 260 have_addr = TRUE; 261 } 262 else { 263 addr = (db_expr_t) db_dot; 264 have_addr = FALSE; 265 } 266 t = db_read_token(); 267 if (t == tCOMMA) { 268 if (!db_expression(&count)) { 269 db_printf("Count missing\n"); 270 db_flush_lex(); 271 return; 272 } 273 } 274 else { 275 db_unread_token(t); 276 count = -1; 277 } 278 if ((cmd->flag & CS_MORE) == 0) { 279 db_skip_to_eol(); 280 } 281 } 282 } 283 *last_cmdp = cmd; 284 if (cmd != 0) { 285 /* 286 * Execute the command. 287 */ 288 (*cmd->fcn)(addr, have_addr, count, modif); 289 290 if (cmd->flag & CS_SET_DOT) { 291 /* 292 * If command changes dot, set dot to 293 * previous address displayed (if 'ed' style). 294 */ 295 if (db_ed_style) { 296 db_dot = db_prev; 297 } 298 else { 299 db_dot = db_next; 300 } 301 } 302 else { 303 /* 304 * If command does not change dot, 305 * set 'next' location to be the same. 306 */ 307 db_next = db_dot; 308 } 309 } 310 } 311 312 /* 313 * 'show' commands 314 */ 315 316 static struct command db_show_all_cmds[] = { 317 #if 0 318 { "threads", db_show_all_threads, 0, 0 }, 319 #endif 320 { "procs", db_ps, 0, 0 }, 321 { (char *)0 } 322 }; 323 324 static struct command db_show_cmds[] = { 325 { "all", 0, 0, db_show_all_cmds }, 326 { "registers", db_show_regs, 0, 0 }, 327 { "breaks", db_listbreak_cmd, 0, 0 }, 328 { "watches", db_listwatch_cmd, 0, 0 }, 329 #if 0 330 { "thread", db_show_one_thread, 0, 0 }, 331 #endif 332 { "map", vm_map_print, 0, 0 }, 333 { "object", vm_object_print, 0, 0 }, 334 #if 0 335 { "page", vm_page_print, 0, 0 }, 336 #endif 337 #if 0 338 { "port", ipc_port_print, 0, 0 }, 339 #endif 340 { (char *)0, } 341 }; 342 343 static struct command db_command_table[] = { 344 { "print", db_print_cmd, 0, 0 }, 345 { "p", db_print_cmd, 0, 0 }, 346 { "examine", db_examine_cmd, CS_SET_DOT, 0 }, 347 { "x", db_examine_cmd, CS_SET_DOT, 0 }, 348 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 }, 349 { "set", db_set_cmd, CS_OWN, 0 }, 350 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 }, 351 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 }, 352 { "delete", db_delete_cmd, 0, 0 }, 353 { "d", db_delete_cmd, 0, 0 }, 354 { "break", db_breakpoint_cmd, 0, 0 }, 355 { "dwatch", db_deletewatch_cmd, 0, 0 }, 356 { "watch", db_watchpoint_cmd, CS_MORE,0 }, 357 { "step", db_single_step_cmd, 0, 0 }, 358 { "s", db_single_step_cmd, 0, 0 }, 359 { "continue", db_continue_cmd, 0, 0 }, 360 { "c", db_continue_cmd, 0, 0 }, 361 { "until", db_trace_until_call_cmd,0, 0 }, 362 { "next", db_trace_until_matching_cmd,0, 0 }, 363 { "match", db_trace_until_matching_cmd,0, 0 }, 364 { "trace", db_stack_trace_cmd, 0, 0 }, 365 { "call", db_fncall, CS_OWN, 0 }, 366 { "show", 0, 0, db_show_cmds }, 367 { "ps", db_ps, 0, 0 }, 368 { "panic", db_panic, 0, 0 }, 369 { (char *)0, } 370 }; 371 372 static struct command *db_last_command = 0; 373 374 #if 0 375 void 376 db_help_cmd() 377 { 378 struct command *cmd = db_command_table; 379 380 while (cmd->name != 0) { 381 db_printf("%-12s", cmd->name); 382 db_end_line(); 383 cmd++; 384 } 385 } 386 #endif 387 388 static void 389 db_panic(dummy1, dummy2, dummy3, dummy4) 390 db_expr_t dummy1; 391 boolean_t dummy2; 392 db_expr_t dummy3; 393 char * dummy4; 394 { 395 panic("from debugger"); 396 } 397 398 void 399 db_command_loop() 400 { 401 /* 402 * Initialize 'prev' and 'next' to dot. 403 */ 404 db_prev = db_dot; 405 db_next = db_dot; 406 407 db_cmd_loop_done = 0; 408 while (!db_cmd_loop_done) { 409 410 (void) setjmp(db_jmpbuf); 411 if (db_print_position() != 0) 412 db_printf("\n"); 413 414 db_printf("db> "); 415 (void) db_read_line(); 416 417 db_command(&db_last_command, db_command_table); 418 } 419 } 420 421 void 422 db_error(s) 423 char *s; 424 { 425 if (s) 426 db_printf(s); 427 db_flush_lex(); 428 longjmp(db_jmpbuf, 1); 429 } 430 431 432 /* 433 * Call random function: 434 * !expr(arg,arg,arg) 435 */ 436 static void 437 db_fncall(dummy1, dummy2, dummy3, dummy4) 438 db_expr_t dummy1; 439 boolean_t dummy2; 440 db_expr_t dummy3; 441 char * dummy4; 442 { 443 db_expr_t fn_addr; 444 #define MAXARGS 11 /* XXX only 10 are passed */ 445 db_expr_t args[MAXARGS]; 446 int nargs = 0; 447 db_expr_t retval; 448 typedef db_expr_t fcn_10args_t __P((db_expr_t, db_expr_t, db_expr_t, 449 db_expr_t, db_expr_t, db_expr_t, 450 db_expr_t, db_expr_t, db_expr_t, 451 db_expr_t)); 452 fcn_10args_t *func; 453 int t; 454 455 if (!db_expression(&fn_addr)) { 456 db_printf("Bad function\n"); 457 db_flush_lex(); 458 return; 459 } 460 func = (fcn_10args_t *)fn_addr; /* XXX */ 461 462 t = db_read_token(); 463 if (t == tLPAREN) { 464 if (db_expression(&args[0])) { 465 nargs++; 466 while ((t = db_read_token()) == tCOMMA) { 467 if (nargs == MAXARGS) { 468 db_printf("Too many arguments\n"); 469 db_flush_lex(); 470 return; 471 } 472 if (!db_expression(&args[nargs])) { 473 db_printf("Argument missing\n"); 474 db_flush_lex(); 475 return; 476 } 477 nargs++; 478 } 479 db_unread_token(t); 480 } 481 if (db_read_token() != tRPAREN) { 482 db_printf("?\n"); 483 db_flush_lex(); 484 return; 485 } 486 } 487 db_skip_to_eol(); 488 489 while (nargs < MAXARGS) { 490 args[nargs++] = 0; 491 } 492 493 retval = (*func)(args[0], args[1], args[2], args[3], args[4], 494 args[5], args[6], args[7], args[8], args[9] ); 495 db_printf("%#n\n", retval); 496 } 497