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