Home | History | Annotate | Line # | Download | only in ddb
db_command.c revision 1.26
      1 /*	$NetBSD: db_command.c,v 1.26 1998/07/04 22:18:48 jonathan 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 #include "opt_uvm.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 
     39 #include <machine/db_machdep.h>		/* type definitions */
     40 
     41 #include <ddb/db_lex.h>
     42 #include <ddb/db_output.h>
     43 #include <ddb/db_command.h>
     44 #include <ddb/db_break.h>
     45 #include <ddb/db_watch.h>
     46 #include <ddb/db_run.h>
     47 #include <ddb/db_variables.h>
     48 #include <ddb/db_interface.h>
     49 #include <ddb/db_sym.h>
     50 #include <ddb/db_extern.h>
     51 
     52 #include <vm/vm.h>
     53 
     54 #if defined(UVM)
     55 #include <uvm/uvm_extern.h>
     56 #include <uvm/uvm_ddb.h>
     57 #endif
     58 
     59 /*
     60  * Exported global variables
     61  */
     62 boolean_t	db_cmd_loop_done;
     63 label_t		*db_recover;
     64 
     65 /*
     66  * if 'ed' style: 'dot' is set at start of last item printed,
     67  * and '+' points to next line.
     68  * Otherwise: 'dot' points to next item, '..' points to last.
     69  */
     70 boolean_t	db_ed_style = TRUE;
     71 
     72 /*
     73  * Utility routine - discard tokens through end-of-line.
     74  */
     75 void
     76 db_skip_to_eol()
     77 {
     78 	int	t;
     79 	do {
     80 	    t = db_read_token();
     81 	} while (t != tEOL);
     82 }
     83 
     84 /*
     85  * Results of command search.
     86  */
     87 #define	CMD_UNIQUE	0
     88 #define	CMD_FOUND	1
     89 #define	CMD_NONE	2
     90 #define	CMD_AMBIGUOUS	3
     91 #define	CMD_HELP	4
     92 
     93 /*
     94  * Search for command prefix.
     95  */
     96 int
     97 db_cmd_search(name, table, cmdp)
     98 	char			*name;
     99 	struct db_command	*table;
    100 	struct db_command	**cmdp;	/* out */
    101 {
    102 	struct db_command	*cmd;
    103 	int			result = CMD_NONE;
    104 
    105 	for (cmd = table; cmd->name != 0; cmd++) {
    106 	    register char *lp;
    107 	    register char *rp;
    108 	    register int  c;
    109 
    110 	    lp = name;
    111 	    rp = cmd->name;
    112 	    while ((c = *lp) == *rp) {
    113 		if (c == 0) {
    114 		    /* complete match */
    115 		    *cmdp = cmd;
    116 		    return (CMD_UNIQUE);
    117 		}
    118 		lp++;
    119 		rp++;
    120 	    }
    121 	    if (c == 0) {
    122 		/* end of name, not end of command -
    123 		   partial match */
    124 		if (result == CMD_FOUND) {
    125 		    result = CMD_AMBIGUOUS;
    126 		    /* but keep looking for a full match -
    127 		       this lets us match single letters */
    128 		}
    129 		else {
    130 		    *cmdp = cmd;
    131 		    result = CMD_FOUND;
    132 		}
    133 	    }
    134 	}
    135 	if (result == CMD_NONE) {
    136 	    /* check for 'help' */
    137 		if (name[0] == 'h' && name[1] == 'e'
    138 		    && name[2] == 'l' && name[3] == 'p')
    139 			result = CMD_HELP;
    140 	}
    141 	return (result);
    142 }
    143 
    144 void
    145 db_cmd_list(table)
    146 	struct db_command *table;
    147 {
    148 	register struct db_command *cmd;
    149 
    150 	for (cmd = table; cmd->name != 0; cmd++) {
    151 	    db_printf("%-12s", cmd->name);
    152 	    db_end_line();
    153 	}
    154 }
    155 
    156 void
    157 db_command(last_cmdp, cmd_table)
    158 	struct db_command	**last_cmdp;	/* IN_OUT */
    159 	struct db_command	*cmd_table;
    160 {
    161 	struct db_command	*cmd;
    162 	int		t;
    163 	char		modif[TOK_STRING_SIZE];
    164 	db_expr_t	addr, count;
    165 	boolean_t	have_addr = FALSE;
    166 	int		result;
    167 
    168 	t = db_read_token();
    169 	if (t == tEOL) {
    170 	    /* empty line repeats last command, at 'next' */
    171 	    cmd = *last_cmdp;
    172 	    addr = (db_expr_t)db_next;
    173 	    have_addr = FALSE;
    174 	    count = 1;
    175 	    modif[0] = '\0';
    176 	}
    177 	else if (t == tEXCL) {
    178 	    db_fncall(0, 0, 0, NULL);
    179 	    return;
    180 	}
    181 	else if (t != tIDENT) {
    182 	    db_printf("?\n");
    183 	    db_flush_lex();
    184 	    return;
    185 	}
    186 	else {
    187 	    /*
    188 	     * Search for command
    189 	     */
    190 	    while (cmd_table) {
    191 		result = db_cmd_search(db_tok_string,
    192 				       cmd_table,
    193 				       &cmd);
    194 		switch (result) {
    195 		    case CMD_NONE:
    196 			db_printf("No such command\n");
    197 			db_flush_lex();
    198 			return;
    199 		    case CMD_AMBIGUOUS:
    200 			db_printf("Ambiguous\n");
    201 			db_flush_lex();
    202 			return;
    203 		    case CMD_HELP:
    204 			db_cmd_list(cmd_table);
    205 			db_flush_lex();
    206 			return;
    207 		    default:
    208 			break;
    209 		}
    210 		if ((cmd_table = cmd->more) != 0) {
    211 		    t = db_read_token();
    212 		    if (t != tIDENT) {
    213 			db_cmd_list(cmd_table);
    214 			db_flush_lex();
    215 			return;
    216 		    }
    217 		}
    218 	    }
    219 
    220 	    if ((cmd->flag & CS_OWN) == 0) {
    221 		/*
    222 		 * Standard syntax:
    223 		 * command [/modifier] [addr] [,count]
    224 		 */
    225 		t = db_read_token();
    226 		if (t == tSLASH) {
    227 		    t = db_read_token();
    228 		    if (t != tIDENT) {
    229 			db_printf("Bad modifier\n");
    230 			db_flush_lex();
    231 			return;
    232 		    }
    233 		    db_strcpy(modif, db_tok_string);
    234 		}
    235 		else {
    236 		    db_unread_token(t);
    237 		    modif[0] = '\0';
    238 		}
    239 
    240 		if (db_expression(&addr)) {
    241 		    db_dot = (db_addr_t) addr;
    242 		    db_last_addr = db_dot;
    243 		    have_addr = TRUE;
    244 		}
    245 		else {
    246 		    addr = (db_expr_t) db_dot;
    247 		    have_addr = FALSE;
    248 		}
    249 		t = db_read_token();
    250 		if (t == tCOMMA) {
    251 		    if (!db_expression(&count)) {
    252 			db_printf("Count missing\n");
    253 			db_flush_lex();
    254 			return;
    255 		    }
    256 		}
    257 		else {
    258 		    db_unread_token(t);
    259 		    count = -1;
    260 		}
    261 		if ((cmd->flag & CS_MORE) == 0) {
    262 		    db_skip_to_eol();
    263 		}
    264 	    }
    265 	}
    266 	*last_cmdp = cmd;
    267 	if (cmd != 0) {
    268 	    /*
    269 	     * Execute the command.
    270 	     */
    271 	    (*cmd->fcn)(addr, have_addr, count, modif);
    272 
    273 	    if (cmd->flag & CS_SET_DOT) {
    274 		/*
    275 		 * If command changes dot, set dot to
    276 		 * previous address displayed (if 'ed' style).
    277 		 */
    278 		if (db_ed_style) {
    279 		    db_dot = db_prev;
    280 		}
    281 		else {
    282 		    db_dot = db_next;
    283 		}
    284 	    }
    285 	    else {
    286 		/*
    287 		 * If command does not change dot,
    288 		 * set 'next' location to be the same.
    289 		 */
    290 		db_next = db_dot;
    291 	    }
    292 	}
    293 }
    294 
    295 /*ARGSUSED*/
    296 void
    297 db_map_print_cmd(addr, have_addr, count, modif)
    298 	db_expr_t	addr;
    299 	int		have_addr;
    300 	db_expr_t	count;
    301 	char *		modif;
    302 {
    303         boolean_t full = FALSE;
    304 
    305         if (modif[0] == 'f')
    306                 full = TRUE;
    307 
    308 #if defined(UVM)
    309         uvm_map_printit((vm_map_t) addr, full, db_printf);
    310 #else
    311         _vm_map_print((vm_map_t) addr, full, db_printf);
    312 #endif
    313 }
    314 
    315 /*ARGSUSED*/
    316 void
    317 db_object_print_cmd(addr, have_addr, count, modif)
    318 	db_expr_t	addr;
    319 	int		have_addr;
    320 	db_expr_t	count;
    321 	char *		modif;
    322 {
    323         boolean_t full = FALSE;
    324 
    325         if (modif[0] == 'f')
    326                 full = TRUE;
    327 
    328 #if defined(UVM)
    329 	uvm_object_printit((struct uvm_object *) addr, full, db_printf);
    330 #else
    331         _vm_object_print((vm_object_t) addr, full, db_printf);
    332 #endif
    333 }
    334 
    335 /*ARGSUSED*/
    336 void
    337 db_page_print_cmd(addr, have_addr, count, modif)
    338 	db_expr_t	addr;
    339 	int		have_addr;
    340 	db_expr_t	count;
    341 	char *		modif;
    342 {
    343         boolean_t full = FALSE;
    344 
    345         if (modif[0] == 'f')
    346                 full = TRUE;
    347 
    348 #if defined(UVM)
    349 	uvm_page_printit((struct vm_page *) addr, full, db_printf);
    350 #else
    351 	printf("only supported by UVM\n");
    352 #endif
    353 }
    354 
    355 /*
    356  * 'show' commands
    357  */
    358 
    359 struct db_command db_show_all_cmds[] = {
    360 	{ "procs",	db_show_all_procs,	0, NULL },
    361 	{ "callout",	db_show_callout,	0, NULL },
    362 	{ NULL, 	NULL, 			0, NULL }
    363 };
    364 
    365 struct db_command db_show_cmds[] = {
    366 	{ "all",	NULL,			0,	db_show_all_cmds },
    367 	{ "registers",	db_show_regs,		0,	NULL },
    368 	{ "breaks",	db_listbreak_cmd, 	0,	NULL },
    369 	{ "watches",	db_listwatch_cmd, 	0,	NULL },
    370 	{ "map",	db_map_print_cmd,	0,	NULL },
    371 	{ "object",	db_object_print_cmd,	0,	NULL },
    372 	{ "page",	db_page_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