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