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