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