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