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