Home | History | Annotate | Line # | Download | only in ddb
db_command.c revision 1.43
      1 /*	$NetBSD: db_command.c,v 1.43 2000/05/26 03:34:31 jhawk 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 #include "opt_inet.h"
     31 
     32 /*
     33  * Command dispatcher.
     34  */
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/reboot.h>
     38 #include <sys/proc.h>
     39 #include <sys/vnode.h>
     40 #include <sys/pool.h>
     41 
     42 #include <machine/db_machdep.h>		/* type definitions */
     43 
     44 #if defined(_KERNEL) && !defined(_LKM)
     45 #include "opt_multiprocessor.h"
     46 #endif
     47 #ifdef MULTIPROCESSOR
     48 #include <machine/cpu.h>
     49 #endif
     50 
     51 #include <ddb/db_lex.h>
     52 #include <ddb/db_output.h>
     53 #include <ddb/db_command.h>
     54 #include <ddb/db_break.h>
     55 #include <ddb/db_watch.h>
     56 #include <ddb/db_run.h>
     57 #include <ddb/db_variables.h>
     58 #include <ddb/db_interface.h>
     59 #include <ddb/db_sym.h>
     60 #include <ddb/db_extern.h>
     61 
     62 #include <vm/vm.h>
     63 
     64 #include <uvm/uvm_extern.h>
     65 #include <uvm/uvm_ddb.h>
     66 
     67 /*
     68  * Exported global variables
     69  */
     70 boolean_t	db_cmd_loop_done;
     71 label_t		*db_recover;
     72 
     73 /*
     74  * if 'ed' style: 'dot' is set at start of last item printed,
     75  * and '+' points to next line.
     76  * Otherwise: 'dot' points to next item, '..' points to last.
     77  */
     78 boolean_t	db_ed_style = TRUE;
     79 
     80 /*
     81  * Utility routine - discard tokens through end-of-line.
     82  */
     83 void
     84 db_skip_to_eol()
     85 {
     86 	int	t;
     87 	do {
     88 	    t = db_read_token();
     89 	} while (t != tEOL);
     90 }
     91 
     92 /*
     93  * Results of command search.
     94  */
     95 #define	CMD_UNIQUE	0
     96 #define	CMD_FOUND	1
     97 #define	CMD_NONE	2
     98 #define	CMD_AMBIGUOUS	3
     99 #define	CMD_HELP	4
    100 
    101 /*
    102  * Search for command prefix.
    103  */
    104 int
    105 db_cmd_search(name, table, cmdp)
    106 	char			*name;
    107 	struct db_command	*table;
    108 	struct db_command	**cmdp;	/* out */
    109 {
    110 	struct db_command	*cmd;
    111 	int			result = CMD_NONE;
    112 
    113 	for (cmd = table; cmd->name != 0; cmd++) {
    114 	    char *lp;
    115 	    char *rp;
    116 	    int  c;
    117 
    118 	    lp = name;
    119 	    rp = cmd->name;
    120 	    while ((c = *lp) == *rp) {
    121 		if (c == 0) {
    122 		    /* complete match */
    123 		    *cmdp = cmd;
    124 		    return (CMD_UNIQUE);
    125 		}
    126 		lp++;
    127 		rp++;
    128 	    }
    129 	    if (c == 0) {
    130 		/* end of name, not end of command -
    131 		   partial match */
    132 		if (result == CMD_FOUND) {
    133 		    result = CMD_AMBIGUOUS;
    134 		    /* but keep looking for a full match -
    135 		       this lets us match single letters */
    136 		}
    137 		else {
    138 		    *cmdp = cmd;
    139 		    result = CMD_FOUND;
    140 		}
    141 	    }
    142 	}
    143 	if (result == CMD_NONE) {
    144 	    /* check for 'help' */
    145 		if (name[0] == 'h' && name[1] == 'e'
    146 		    && name[2] == 'l' && name[3] == 'p')
    147 			result = CMD_HELP;
    148 	}
    149 	return (result);
    150 }
    151 
    152 void
    153 db_cmd_list(table)
    154 	struct db_command *table;
    155 {
    156 	int	 i, j, w, columns, lines, width=0, items, numcmds;
    157 	char	*p;
    158 
    159 	for (numcmds = 0; table[numcmds].name != NULL; numcmds++) {
    160 		w = strlen(table[numcmds].name);
    161 		if (w > width)
    162 			width = w;
    163 	}
    164 	width = DB_NEXT_TAB(width);
    165 	items = 0;
    166 
    167 	columns = db_max_width / width;
    168 	if (columns == 0)
    169 		columns = 1;
    170 	lines = (numcmds + columns - 1) / columns;
    171 	for (i = 0; i < lines; i++) {
    172 		for (j = 0; j < columns; j++) {
    173 			p = table[j * lines + i].name;
    174 			if (p)
    175 				db_printf("%s", p);
    176 			if (j * lines + i + lines >= numcmds) {
    177 				db_putchar('\n');
    178 				break;
    179 			}
    180 			w = strlen(p);
    181 			while (w < width) {
    182 				w = DB_NEXT_TAB(w);
    183 				db_putchar('\t');
    184 			}
    185 		}
    186 	}
    187 }
    188 
    189 void
    190 db_command(last_cmdp, cmd_table)
    191 	struct db_command	**last_cmdp;	/* IN_OUT */
    192 	struct db_command	*cmd_table;
    193 {
    194 	struct db_command	*cmd;
    195 	int		t;
    196 	char		modif[TOK_STRING_SIZE];
    197 	db_expr_t	addr, count;
    198 	boolean_t	have_addr = FALSE;
    199 	int		result;
    200 
    201 	static db_expr_t               last_count = 0;
    202 
    203 	t = db_read_token();
    204 	if ((t == tEOL) || (t == tCOMMA)) {
    205 	    /*
    206 	     * An empty line repeats last command, at 'next'.
    207 	     * Only a count repeats the last command with the new count.
    208 	     */
    209 	    cmd = *last_cmdp;
    210 	    addr = (db_expr_t)db_next;
    211 	    if (t == tCOMMA) {
    212 	            if (!db_expression(&count)) {
    213 		    db_printf("Count missing\n");
    214 		    db_flush_lex();
    215 		    return;
    216 	        }
    217 	    } else
    218 	        count = last_count;
    219 	    have_addr = FALSE;
    220 	    modif[0] = '\0';
    221 	}
    222 	else if (t == tEXCL) {
    223 	    db_fncall(0, 0, 0, NULL);
    224 	    return;
    225 	}
    226 	else if (t != tIDENT) {
    227 	    db_printf("?\n");
    228 	    db_flush_lex();
    229 	    return;
    230 	}
    231 	else {
    232 	    /*
    233 	     * Search for command
    234 	     */
    235 	    while (cmd_table) {
    236 		result = db_cmd_search(db_tok_string,
    237 				       cmd_table,
    238 				       &cmd);
    239 		switch (result) {
    240 		    case CMD_NONE:
    241 			db_printf("No such command\n");
    242 			db_flush_lex();
    243 			return;
    244 		    case CMD_AMBIGUOUS:
    245 			db_printf("Ambiguous\n");
    246 			db_flush_lex();
    247 			return;
    248 		    case CMD_HELP:
    249 			db_cmd_list(cmd_table);
    250 			db_flush_lex();
    251 			return;
    252 		    default:
    253 			break;
    254 		}
    255 		if ((cmd_table = cmd->more) != 0) {
    256 		    t = db_read_token();
    257 		    if (t != tIDENT) {
    258 			db_cmd_list(cmd_table);
    259 			db_flush_lex();
    260 			return;
    261 		    }
    262 		}
    263 	    }
    264 
    265 	    if ((cmd->flag & CS_OWN) == 0) {
    266 		/*
    267 		 * Standard syntax:
    268 		 * command [/modifier] [addr] [,count]
    269 		 */
    270 		t = db_read_token();
    271 		if (t == tSLASH) {
    272 		    t = db_read_token();
    273 		    if (t != tIDENT) {
    274 			db_printf("Bad modifier\n");
    275 			db_flush_lex();
    276 			return;
    277 		    }
    278 		    db_strcpy(modif, db_tok_string);
    279 		}
    280 		else {
    281 		    db_unread_token(t);
    282 		    modif[0] = '\0';
    283 		}
    284 
    285 		if (db_expression(&addr)) {
    286 		    db_dot = (db_addr_t) addr;
    287 		    db_last_addr = db_dot;
    288 		    have_addr = TRUE;
    289 		}
    290 		else {
    291 		    addr = (db_expr_t) db_dot;
    292 		    have_addr = FALSE;
    293 		}
    294 		t = db_read_token();
    295 		if (t == tCOMMA) {
    296 		    if (!db_expression(&count)) {
    297 			db_printf("Count missing\n");
    298 			db_flush_lex();
    299 			return;
    300 		    }
    301 		}
    302 		else {
    303 		    db_unread_token(t);
    304 		    count = -1;
    305 		}
    306 		if ((cmd->flag & CS_MORE) == 0) {
    307 		    db_skip_to_eol();
    308 		}
    309 	    }
    310 	}
    311 	*last_cmdp = cmd;
    312 	last_count = count;
    313 	if (cmd != 0) {
    314 	    /*
    315 	     * Execute the command.
    316 	     */
    317 	    (*cmd->fcn)(addr, have_addr, count, modif);
    318 
    319 	    if (cmd->flag & CS_SET_DOT) {
    320 		/*
    321 		 * If command changes dot, set dot to
    322 		 * previous address displayed (if 'ed' style).
    323 		 */
    324 		if (db_ed_style) {
    325 		    db_dot = db_prev;
    326 		}
    327 		else {
    328 		    db_dot = db_next;
    329 		}
    330 	    }
    331 	    else {
    332 		/*
    333 		 * If command does not change dot,
    334 		 * set 'next' location to be the same.
    335 		 */
    336 		db_next = db_dot;
    337 	    }
    338 	}
    339 }
    340 
    341 /*ARGSUSED*/
    342 void
    343 db_map_print_cmd(addr, have_addr, count, modif)
    344 	db_expr_t	addr;
    345 	int		have_addr;
    346 	db_expr_t	count;
    347 	char *		modif;
    348 {
    349         boolean_t full = FALSE;
    350 
    351         if (modif[0] == 'f')
    352                 full = TRUE;
    353 
    354         uvm_map_printit((vm_map_t) addr, full, db_printf);
    355 }
    356 
    357 /*ARGSUSED*/
    358 void
    359 db_object_print_cmd(addr, have_addr, count, modif)
    360 	db_expr_t	addr;
    361 	int		have_addr;
    362 	db_expr_t	count;
    363 	char *		modif;
    364 {
    365         boolean_t full = FALSE;
    366 
    367         if (modif[0] == 'f')
    368                 full = TRUE;
    369 
    370 	uvm_object_printit((struct uvm_object *) addr, full, db_printf);
    371 }
    372 
    373 /*ARGSUSED*/
    374 void
    375 db_page_print_cmd(addr, have_addr, count, modif)
    376 	db_expr_t	addr;
    377 	int		have_addr;
    378 	db_expr_t	count;
    379 	char *		modif;
    380 {
    381         boolean_t full = FALSE;
    382 
    383         if (modif[0] == 'f')
    384                 full = TRUE;
    385 
    386 	uvm_page_printit((struct vm_page *) addr, full, db_printf);
    387 }
    388 
    389 /*ARGSUSED*/
    390 void
    391 db_buf_print_cmd(addr, have_addr, count, modif)
    392 	db_expr_t	addr;
    393 	int		have_addr;
    394 	db_expr_t	count;
    395 	char *		modif;
    396 {
    397 	boolean_t full = FALSE;
    398 
    399 	if (modif[0] == 'f')
    400 		full = TRUE;
    401 
    402 	vfs_buf_print((struct buf *)addr, full, db_printf);
    403 }
    404 
    405 /*ARGSUSED*/
    406 void
    407 db_vnode_print_cmd(addr, have_addr, count, modif)
    408 	db_expr_t	addr;
    409 	int		have_addr;
    410 	db_expr_t	count;
    411 	char *		modif;
    412 {
    413 	boolean_t full = FALSE;
    414 
    415 	if (modif[0] == 'f')
    416 		full = TRUE;
    417 
    418 	vfs_vnode_print((struct vnode *)addr, full, db_printf);
    419 }
    420 
    421 /*ARGSUSED*/
    422 void
    423 db_pool_print_cmd(addr, have_addr, count, modif)
    424 	db_expr_t	addr;
    425 	int		have_addr;
    426 	db_expr_t	count;
    427 	char *		modif;
    428 {
    429 
    430 	pool_printit((struct pool *) addr, modif, db_printf);
    431 }
    432 
    433 /*
    434  * 'show' commands
    435  */
    436 
    437 struct db_command db_show_all_cmds[] = {
    438 	{ "callout",	db_show_callout,	0, NULL },
    439 	{ "procs",	db_show_all_procs,	0, NULL },
    440 	{ NULL, 	NULL, 			0, NULL }
    441 };
    442 
    443 struct db_command db_show_cmds[] = {
    444 	{ "all",	NULL,			0,	db_show_all_cmds },
    445 #ifdef INET
    446 	{ "arptab",	db_show_arptab,		0,	NULL },
    447 #endif
    448 	{ "breaks",	db_listbreak_cmd, 	0,	NULL },
    449 	{ "map",	db_map_print_cmd,	0,	NULL },
    450 	{ "object",	db_object_print_cmd,	0,	NULL },
    451 	{ "page",	db_page_print_cmd,	0,	NULL },
    452 	{ "buf",	db_buf_print_cmd,	0,	NULL },
    453 	{ "vnode",	db_vnode_print_cmd,	0,	NULL },
    454 	{ "pool",	db_pool_print_cmd,	0,	NULL },
    455 	{ "registers",	db_show_regs,		0,	NULL },
    456 	{ "watches",	db_listwatch_cmd, 	0,	NULL },
    457 	{ NULL,		NULL,			0,	NULL }
    458 };
    459 
    460 struct db_command db_command_table[] = {
    461 	{ "break",	db_breakpoint_cmd,	0,		NULL },
    462 	{ "c",		db_continue_cmd,	0,		NULL },
    463 	{ "call",	db_fncall,		CS_OWN,		NULL },
    464 	{ "callout",	db_show_callout,	0,		NULL },
    465 	{ "continue",	db_continue_cmd,	0,		NULL },
    466 	{ "d",		db_delete_cmd,		0,		NULL },
    467 	{ "delete",	db_delete_cmd,		0,		NULL },
    468 	{ "dwatch",	db_deletewatch_cmd,	0,		NULL },
    469 	{ "examine",	db_examine_cmd,		CS_SET_DOT, 	NULL },
    470 	{ "kill",	db_kill_proc,		CS_OWN,		NULL },
    471 #ifdef DB_MACHINE_COMMANDS
    472 	{ "machine",    NULL,                   0,     		NULL },
    473 #endif
    474 	{ "match",	db_trace_until_matching_cmd,0,		NULL },
    475 	{ "next",	db_trace_until_matching_cmd,0,		NULL },
    476 	{ "p",		db_print_cmd,		0,		NULL },
    477 	{ "print",	db_print_cmd,		0,		NULL },
    478 	{ "ps",		db_show_all_procs,	0,		NULL },
    479 	{ "reboot",	db_reboot_cmd,		CS_OWN,		NULL },
    480 	{ "s",		db_single_step_cmd,	0,		NULL },
    481 	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, NULL },
    482 	{ "set",	db_set_cmd,		CS_OWN,		NULL },
    483 	{ "show",	NULL,			0,		db_show_cmds },
    484 	{ "sifting",    db_sifting_cmd,		CS_OWN,		NULL },
    485 	{ "step",	db_single_step_cmd,	0,		NULL },
    486 	{ "sync",	db_sync_cmd,		CS_OWN,		NULL },
    487 	{ "trace",	db_stack_trace_cmd,	0,		NULL },
    488 	{ "until",	db_trace_until_call_cmd,0,		NULL },
    489 	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
    490 	{ "watch",	db_watchpoint_cmd,	CS_MORE,	NULL },
    491 	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
    492 	{ "x",		db_examine_cmd,		CS_SET_DOT, 	NULL },
    493 	{ NULL, 	NULL,			0,		NULL }
    494 };
    495 
    496 #ifdef DB_MACHINE_COMMANDS
    497 
    498 /*
    499  * this function should be called to install the machine dependent
    500  * commands. It should be called before the debugger is enabled
    501  */
    502 void
    503 db_machine_commands_install(ptr)
    504 	struct db_command *ptr;
    505 {
    506 	struct db_command *cmd;
    507 
    508 	for (cmd = db_command_table; cmd != 0; cmd++) {
    509 		if (strcmp(cmd->name, "machine") == 0) {
    510 			cmd->more = ptr;
    511 			break;
    512 		}
    513 	}
    514 }
    515 
    516 #endif
    517 
    518 struct db_command	*db_last_command = 0;
    519 
    520 void
    521 db_command_loop()
    522 {
    523 	label_t		db_jmpbuf;
    524 	label_t		*savejmp;
    525 	extern int	db_output_line;
    526 
    527 	/*
    528 	 * Initialize 'prev' and 'next' to dot.
    529 	 */
    530 	db_prev = db_dot;
    531 	db_next = db_dot;
    532 
    533 	db_cmd_loop_done = 0;
    534 
    535 	savejmp = db_recover;
    536 	db_recover = &db_jmpbuf;
    537 	(void) setjmp(&db_jmpbuf);
    538 
    539 	while (!db_cmd_loop_done) {
    540 		if (db_print_position() != 0)
    541 			db_printf("\n");
    542 		db_output_line = 0;
    543 
    544 #ifdef MULTIPROCESSOR
    545 		db_printf("(cpu %ld)", (long)cpu_number());
    546 #endif
    547 		db_printf("db> ");
    548 		(void) db_read_line();
    549 
    550 		db_command(&db_last_command, db_command_table);
    551 	}
    552 
    553 	db_recover = savejmp;
    554 }
    555 
    556 void
    557 db_error(s)
    558 	char *s;
    559 {
    560 	if (s)
    561 	    db_printf(s);
    562 	db_flush_lex();
    563 	longjmp(db_recover);
    564 }
    565 
    566 
    567 /*
    568  * Call random function:
    569  * !expr(arg,arg,arg)
    570  */
    571 /*ARGSUSED*/
    572 void
    573 db_fncall(addr, have_addr, count, modif)
    574 	db_expr_t	addr;
    575 	int		have_addr;
    576 	db_expr_t	count;
    577 	char *		modif;
    578 {
    579 	db_expr_t	fn_addr;
    580 #define	MAXARGS		11
    581 	db_expr_t	args[MAXARGS];
    582 	int		nargs = 0;
    583 	db_expr_t	retval;
    584 	db_expr_t	(*func) __P((db_expr_t, ...));
    585 	int		t;
    586 
    587 	if (!db_expression(&fn_addr)) {
    588 	    db_printf("Bad function\n");
    589 	    db_flush_lex();
    590 	    return;
    591 	}
    592 	func = (db_expr_t (*) __P((db_expr_t, ...))) fn_addr;
    593 
    594 	t = db_read_token();
    595 	if (t == tLPAREN) {
    596 	    if (db_expression(&args[0])) {
    597 		nargs++;
    598 		while ((t = db_read_token()) == tCOMMA) {
    599 		    if (nargs == MAXARGS) {
    600 			db_printf("Too many arguments\n");
    601 			db_flush_lex();
    602 			return;
    603 		    }
    604 		    if (!db_expression(&args[nargs])) {
    605 			db_printf("Argument missing\n");
    606 			db_flush_lex();
    607 			return;
    608 		    }
    609 		    nargs++;
    610 		}
    611 		db_unread_token(t);
    612 	    }
    613 	    if (db_read_token() != tRPAREN) {
    614 		db_printf("?\n");
    615 		db_flush_lex();
    616 		return;
    617 	    }
    618 	}
    619 	db_skip_to_eol();
    620 
    621 	while (nargs < MAXARGS) {
    622 	    args[nargs++] = 0;
    623 	}
    624 
    625 	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
    626 			 args[5], args[6], args[7], args[8], args[9]);
    627 	db_printf("%#ln\n", retval);
    628 }
    629 
    630 void
    631 db_reboot_cmd(addr, have_addr, count, modif)
    632 	db_expr_t	addr;
    633 	int		have_addr;
    634 	db_expr_t	count;
    635 	char *		modif;
    636 {
    637 	db_expr_t bootflags;
    638 
    639 	/* Flags, default to RB_AUTOBOOT */
    640 	if (!db_expression(&bootflags))
    641 		bootflags = (db_expr_t)RB_AUTOBOOT;
    642 	if (db_read_token() != tEOL) {
    643 	    db_error("?\n");
    644 	    /*NOTREACHED*/
    645 	}
    646 	cpu_reboot((int)bootflags, NULL);
    647 }
    648 
    649 void
    650 db_sifting_cmd(addr, have_addr, count, omodif)
    651 	db_expr_t	addr;
    652 	int		have_addr;
    653 	db_expr_t	count;
    654 	char *		omodif;
    655 {
    656 	int	mode, t;
    657 
    658 	t = db_read_token();
    659 	if (t == tSLASH) {
    660 		t = db_read_token();
    661 		if (t != tIDENT) {
    662 			bad_modifier:
    663 			db_printf("Bad modifier\n");
    664 			db_flush_lex();
    665 			return;
    666 		}
    667 		if (!strcmp(db_tok_string, "F"))
    668 			mode = 'F';
    669 		else
    670 			goto bad_modifier;
    671 		t = db_read_token();
    672 	} else
    673 		mode = 0;
    674 
    675 	if (t==tIDENT)
    676 		db_sifting(db_tok_string, mode);
    677 	else {
    678 		db_printf("Bad argument (non-string)\n");
    679 		db_flush_lex();
    680 	}
    681 }
    682 
    683 void
    684 db_stack_trace_cmd(addr, have_addr, count, modif)
    685 	db_expr_t	addr;
    686 	boolean_t	have_addr;
    687 	db_expr_t	count;
    688 	char		*modif;
    689 {
    690 	if (count == -1)
    691 		count = 65535;
    692 
    693 	db_stack_trace_print(addr, have_addr, count, modif, db_printf);
    694 }
    695 
    696 void
    697 db_sync_cmd(addr, have_addr, count, modif)
    698 	db_expr_t	addr;
    699 	int		have_addr;
    700 	db_expr_t	count;
    701 	char *		modif;
    702 {
    703 
    704 	cpu_reboot(RB_DUMP, NULL);
    705 }
    706