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