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