Home | History | Annotate | Line # | Download | only in ddb
db_command.c revision 1.95
      1 /*	$NetBSD: db_command.c,v 1.95 2007/02/22 06:41:00 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 /*
     30  * Command dispatcher.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.95 2007/02/22 06:41:00 thorpej Exp $");
     35 
     36 #include "opt_ddb.h"
     37 #include "opt_kgdb.h"
     38 #include "opt_inet.h"
     39 #include "opt_ddbparam.h"
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/reboot.h>
     44 #include <sys/device.h>
     45 #include <sys/malloc.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/namei.h>
     48 #include <sys/pool.h>
     49 #include <sys/proc.h>
     50 #include <sys/vnode.h>
     51 #include <sys/lockdebug.h>
     52 #include <sys/sleepq.h>
     53 
     54 #include <machine/db_machdep.h>		/* type definitions */
     55 
     56 #if defined(_KERNEL_OPT)
     57 #include "opt_multiprocessor.h"
     58 #endif
     59 #ifdef MULTIPROCESSOR
     60 #include <machine/cpu.h>
     61 #endif
     62 
     63 #include <ddb/db_lex.h>
     64 #include <ddb/db_output.h>
     65 #include <ddb/db_command.h>
     66 #include <ddb/db_break.h>
     67 #include <ddb/db_watch.h>
     68 #include <ddb/db_run.h>
     69 #include <ddb/db_variables.h>
     70 #include <ddb/db_interface.h>
     71 #include <ddb/db_sym.h>
     72 #include <ddb/db_extern.h>
     73 
     74 #include <uvm/uvm_extern.h>
     75 #include <uvm/uvm_ddb.h>
     76 
     77 #include "arp.h"
     78 
     79 /*
     80  * Results of command search.
     81  */
     82 #define	CMD_UNIQUE	0
     83 #define	CMD_FOUND	1
     84 #define	CMD_NONE	2
     85 #define	CMD_AMBIGUOUS	3
     86 #define	CMD_HELP	4
     87 
     88 /*
     89  * Exported global variables
     90  */
     91 bool		db_cmd_loop_done;
     92 label_t		*db_recover;
     93 db_addr_t	db_dot;
     94 db_addr_t	db_last_addr;
     95 db_addr_t	db_prev;
     96 db_addr_t	db_next;
     97 
     98 /*
     99  * if 'ed' style: 'dot' is set at start of last item printed,
    100  * and '+' points to next line.
    101  * Otherwise: 'dot' points to next item, '..' points to last.
    102  */
    103 static bool	 db_ed_style = true;
    104 
    105 static void	db_buf_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    106 static void	db_cmd_list(const struct db_command *);
    107 static int	db_cmd_search(const char *, const struct db_command *,
    108 		    const struct db_command **);
    109 static void	db_command(const struct db_command **,
    110 		    const struct db_command *);
    111 static void	db_event_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    112 static void	db_fncall(db_expr_t, bool, db_expr_t, const char *);
    113 static void	db_malloc_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    114 static void	db_map_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    115 static void	db_namecache_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    116 static void	db_object_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    117 static void	db_page_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    118 static void	db_show_all_pages(db_expr_t, bool, db_expr_t, const char *);
    119 static void	db_pool_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    120 static void	db_reboot_cmd(db_expr_t, bool, db_expr_t, const char *);
    121 static void	db_sifting_cmd(db_expr_t, bool, db_expr_t, const char *);
    122 static void	db_stack_trace_cmd(db_expr_t, bool, db_expr_t, const char *);
    123 static void	db_sync_cmd(db_expr_t, bool, db_expr_t, const char *);
    124 static void	db_uvmexp_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    125 static void	db_vnode_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    126 static void	db_lock_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    127 static void	db_mount_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    128 static void	db_mbuf_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    129 
    130 /*
    131  * 'show' commands
    132  */
    133 
    134 static const struct db_command db_show_all_cmds[] = {
    135 	{ "callout",	db_show_callout,	0, NULL },
    136 	{ "pages",	db_show_all_pages,	0, NULL },
    137 	{ "procs",	db_show_all_procs,	0, NULL },
    138 	{ "pools",	db_show_all_pools,	0, NULL },
    139 	{ NULL, 	NULL, 			0, NULL }
    140 };
    141 
    142 static const struct db_command db_show_cmds[] = {
    143 	{ "all",	NULL,			0,	db_show_all_cmds },
    144 #if defined(INET) && (NARP > 0)
    145 	{ "arptab",	db_show_arptab,		0,	NULL },
    146 #endif
    147 	{ "breaks",	db_listbreak_cmd, 	0,	NULL },
    148 	{ "buf",	db_buf_print_cmd,	0,	NULL },
    149 	{ "event",	db_event_print_cmd,	0,	NULL },
    150 	{ "lock",	db_lock_print_cmd,	0,	NULL },
    151 	{ "malloc",	db_malloc_print_cmd,	0,	NULL },
    152 	{ "map",	db_map_print_cmd,	0,	NULL },
    153 	{ "mount",	db_mount_print_cmd,	0,	NULL },
    154 	{ "mbuf",	db_mbuf_print_cmd,	0,	NULL },
    155 	{ "ncache",	db_namecache_print_cmd,	0,	NULL },
    156 	{ "object",	db_object_print_cmd,	0,	NULL },
    157 	{ "page",	db_page_print_cmd,	0,	NULL },
    158 	{ "pool",	db_pool_print_cmd,	0,	NULL },
    159 	{ "registers",	db_show_regs,		0,	NULL },
    160 	{ "sched_qs",	db_show_sched_qs,	0,	NULL },
    161 	{ "uvmexp",	db_uvmexp_print_cmd,	0,	NULL },
    162 	{ "vnode",	db_vnode_print_cmd,	0,	NULL },
    163 	{ "watches",	db_listwatch_cmd, 	0,	NULL },
    164 	{ NULL,		NULL,			0,	NULL }
    165 };
    166 
    167 /* arch/<arch>/<arch>/db_interface.c */
    168 #ifdef DB_MACHINE_COMMANDS
    169 extern const struct db_command db_machine_command_table[];
    170 #endif
    171 
    172 static const struct db_command db_command_table[] = {
    173 	{ "b",		db_breakpoint_cmd,	0,		NULL },
    174 	{ "break",	db_breakpoint_cmd,	0,		NULL },
    175 	{ "bt",		db_stack_trace_cmd,	0,		NULL },
    176 	{ "c",		db_continue_cmd,	0,		NULL },
    177 	{ "call",	db_fncall,		CS_OWN,		NULL },
    178 	{ "callout",	db_show_callout,	0,		NULL },
    179 	{ "continue",	db_continue_cmd,	0,		NULL },
    180 	{ "d",		db_delete_cmd,		0,		NULL },
    181 	{ "delete",	db_delete_cmd,		0,		NULL },
    182 	{ "dmesg",	db_dmesg,		0,		NULL },
    183 	{ "dwatch",	db_deletewatch_cmd,	0,		NULL },
    184 	{ "examine",	db_examine_cmd,		CS_SET_DOT, 	NULL },
    185 	{ "kill",	db_kill_proc,		CS_OWN,		NULL },
    186 #ifdef KGDB
    187 	{ "kgdb",	db_kgdb_cmd,		0,		NULL },
    188 #endif
    189 #ifdef DB_MACHINE_COMMANDS
    190 	{ "machine",	NULL,			0, db_machine_command_table },
    191 #endif
    192 	{ "match",	db_trace_until_matching_cmd,0,		NULL },
    193 	{ "next",	db_trace_until_matching_cmd,0,		NULL },
    194 	{ "p",		db_print_cmd,		0,		NULL },
    195 	{ "print",	db_print_cmd,		0,		NULL },
    196 	{ "ps",		db_show_all_procs,	0,		NULL },
    197 	{ "reboot",	db_reboot_cmd,		CS_OWN,		NULL },
    198 	{ "s",		db_single_step_cmd,	0,		NULL },
    199 	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, NULL },
    200 	{ "set",	db_set_cmd,		CS_OWN,		NULL },
    201 	{ "show",	NULL,			0,		db_show_cmds },
    202 	{ "sifting",	db_sifting_cmd,		CS_OWN,		NULL },
    203 	{ "step",	db_single_step_cmd,	0,		NULL },
    204 	{ "sync",	db_sync_cmd,		CS_OWN,		NULL },
    205 	{ "trace",	db_stack_trace_cmd,	0,		NULL },
    206 	{ "until",	db_trace_until_call_cmd,0,		NULL },
    207 	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
    208 	{ "watch",	db_watchpoint_cmd,	CS_MORE,	NULL },
    209 	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
    210 	{ "x",		db_examine_cmd,		CS_SET_DOT, 	NULL },
    211 	{ NULL, 	NULL,			0,		NULL }
    212 };
    213 
    214 static const struct db_command	*db_last_command = NULL;
    215 #if defined(DDB_COMMANDONENTER)
    216 char db_cmd_on_enter[DB_LINE_MAXLEN + 1] = ___STRING(DDB_COMMANDONENTER);
    217 #else /* defined(DDB_COMMANDONENTER) */
    218 char db_cmd_on_enter[DB_LINE_MAXLEN + 1] = "";
    219 #endif /* defined(DDB_COMMANDONENTER) */
    220 #define	DB_LINE_SEP	';'
    221 
    222 /*
    223  * Utility routine - discard tokens through end-of-line.
    224  */
    225 void
    226 db_skip_to_eol(void)
    227 {
    228 	int t;
    229 
    230 	do {
    231 		t = db_read_token();
    232 	} while (t != tEOL);
    233 }
    234 
    235 void
    236 db_error(const char *s)
    237 {
    238 
    239 	if (s)
    240 		db_printf("%s", s);
    241 	db_flush_lex();
    242 	longjmp(db_recover);
    243 }
    244 
    245 static void
    246 db_execute_commandlist(const char *cmdlist)
    247 {
    248 	const char *cmd = cmdlist;
    249 	const struct db_command	*dummy = NULL;
    250 
    251 	while (*cmd != '\0') {
    252 		const char *ep = cmd;
    253 
    254 		while (*ep != '\0' && *ep != DB_LINE_SEP) {
    255 			ep++;
    256 		}
    257 		db_set_line(cmd, ep);
    258 		db_command(&dummy, db_command_table);
    259 		cmd = ep;
    260 		if (*cmd == DB_LINE_SEP) {
    261 			cmd++;
    262 		}
    263 	}
    264 }
    265 
    266 void
    267 db_command_loop(void)
    268 {
    269 	label_t	db_jmpbuf;
    270 	label_t	*savejmp;
    271 
    272 	/*
    273 	 * Initialize 'prev' and 'next' to dot.
    274 	 */
    275 	db_prev = db_dot;
    276 	db_next = db_dot;
    277 
    278 	db_cmd_loop_done = 0;
    279 
    280 	savejmp = db_recover;
    281 	db_recover = &db_jmpbuf;
    282 	(void) setjmp(&db_jmpbuf);
    283 
    284 	db_execute_commandlist(db_cmd_on_enter);
    285 
    286 	while (!db_cmd_loop_done) {
    287 		if (db_print_position() != 0)
    288 			db_printf("\n");
    289 		db_output_line = 0;
    290 
    291 
    292 #ifdef MULTIPROCESSOR
    293 		db_printf("db{%ld}> ", (long)cpu_number());
    294 #else
    295 		db_printf("db> ");
    296 #endif
    297 		(void) db_read_line();
    298 
    299 		db_command(&db_last_command, db_command_table);
    300 	}
    301 
    302 	db_recover = savejmp;
    303 }
    304 
    305 /*
    306  * Search for command prefix.
    307  */
    308 static int
    309 db_cmd_search(const char *name, const struct db_command *table,
    310     const struct db_command **cmdp)
    311 {
    312 	const struct db_command	*cmd;
    313 	int			result = CMD_NONE;
    314 
    315 	for (cmd = table; cmd->name != 0; cmd++) {
    316 		const char *lp;
    317 		const char *rp;
    318 		int  c;
    319 
    320 		lp = name;
    321 		rp = cmd->name;
    322 		while ((c = *lp) == *rp) {
    323 			if (c == 0) {
    324 				/* complete match */
    325 				*cmdp = cmd;
    326 				return (CMD_UNIQUE);
    327 			}
    328 			lp++;
    329 			rp++;
    330 		}
    331 		if (c == 0) {
    332 			/* end of name, not end of command -
    333 			   partial match */
    334 			if (result == CMD_FOUND) {
    335 				result = CMD_AMBIGUOUS;
    336 				/* but keep looking for a full match -
    337 				   this lets us match single letters */
    338 			} else {
    339 				*cmdp = cmd;
    340 				result = CMD_FOUND;
    341 			}
    342 		}
    343 	}
    344 	if (result == CMD_NONE) {
    345 		/* check for 'help' */
    346 		if (name[0] == 'h' && name[1] == 'e'
    347 		    && name[2] == 'l' && name[3] == 'p')
    348 			result = CMD_HELP;
    349 	}
    350 	return (result);
    351 }
    352 
    353 static void
    354 db_cmd_list(const struct db_command *table)
    355 {
    356 	int	 i, j, w, columns, lines, width=0, numcmds;
    357 	const char	*p;
    358 
    359 	for (numcmds = 0; table[numcmds].name != NULL; numcmds++) {
    360 		w = strlen(table[numcmds].name);
    361 		if (w > width)
    362 			width = w;
    363 	}
    364 	width = DB_NEXT_TAB(width);
    365 
    366 	columns = db_max_width / width;
    367 	if (columns == 0)
    368 		columns = 1;
    369 	lines = (numcmds + columns - 1) / columns;
    370 	for (i = 0; i < lines; i++) {
    371 		for (j = 0; j < columns; j++) {
    372 			p = table[j * lines + i].name;
    373 			if (p)
    374 				db_printf("%s", p);
    375 			if (j * lines + i + lines >= numcmds) {
    376 				db_putchar('\n');
    377 				break;
    378 			}
    379 			if (p) {
    380 				w = strlen(p);
    381 				while (w < width) {
    382 					w = DB_NEXT_TAB(w);
    383 					db_putchar('\t');
    384 				}
    385 			}
    386 		}
    387 	}
    388 }
    389 
    390 static void
    391 db_command(const struct db_command **last_cmdp,
    392     const struct db_command *cmd_table)
    393 {
    394 	const struct db_command	*cmd;
    395 	int		t;
    396 	char		modif[TOK_STRING_SIZE];
    397 	db_expr_t	addr, count;
    398 	bool		have_addr = false;
    399 	int		result;
    400 	static db_expr_t last_count = 0;
    401 
    402 	cmd = NULL;	/* XXX gcc */
    403 
    404 	t = db_read_token();
    405 	if ((t == tEOL) || (t == tCOMMA)) {
    406 		/*
    407 		 * An empty line repeats last command, at 'next'.
    408 		 * Only a count repeats the last command with the new count.
    409 		 */
    410 		cmd = *last_cmdp;
    411 		addr = (db_expr_t)db_next;
    412 		if (t == tCOMMA) {
    413 			if (!db_expression(&count)) {
    414 				db_printf("Count missing\n");
    415 				db_flush_lex();
    416 				return;
    417 			}
    418 		} else
    419 			count = last_count;
    420 		have_addr = false;
    421 		modif[0] = '\0';
    422 		db_skip_to_eol();
    423 	} else if (t == tEXCL) {
    424 		db_fncall(0, 0, 0, NULL);
    425 		return;
    426 	} else if (t != tIDENT) {
    427 		db_printf("?\n");
    428 		db_flush_lex();
    429 		return;
    430 	} else {
    431 		/*
    432 		 * Search for command
    433 		 */
    434 		while (cmd_table) {
    435 			result = db_cmd_search(db_tok_string, cmd_table, &cmd);
    436 			switch (result) {
    437 			case CMD_NONE:
    438 				db_printf("No such command\n");
    439 				db_flush_lex();
    440 				return;
    441 			case CMD_AMBIGUOUS:
    442 				db_printf("Ambiguous\n");
    443 				db_flush_lex();
    444 				return;
    445 			case CMD_HELP:
    446 				db_cmd_list(cmd_table);
    447 				db_flush_lex();
    448 				return;
    449 			default:
    450 				break;
    451 			}
    452 			if ((cmd_table = cmd->more) != 0) {
    453 				t = db_read_token();
    454 				if (t != tIDENT) {
    455 					db_cmd_list(cmd_table);
    456 					db_flush_lex();
    457 					return;
    458 				}
    459 			}
    460 		}
    461 
    462 		if ((cmd->flag & CS_OWN) == 0) {
    463 			/*
    464 			 * Standard syntax:
    465 			 * command [/modifier] [addr] [,count]
    466 			 */
    467 			t = db_read_token();
    468 			if (t == tSLASH) {
    469 				t = db_read_token();
    470 				if (t != tIDENT) {
    471 					db_printf("Bad modifier\n");
    472 					db_flush_lex();
    473 					return;
    474 				}
    475 				strlcpy(modif, db_tok_string, sizeof(modif));
    476 			} else {
    477 				db_unread_token(t);
    478 				modif[0] = '\0';
    479 			}
    480 
    481 			if (db_expression(&addr)) {
    482 				db_dot = (db_addr_t) addr;
    483 				db_last_addr = db_dot;
    484 				have_addr = true;
    485 			} else {
    486 				addr = (db_expr_t) db_dot;
    487 				have_addr = false;
    488 			}
    489 			t = db_read_token();
    490 			if (t == tCOMMA) {
    491 				if (!db_expression(&count)) {
    492 					db_printf("Count missing\n");
    493 					db_flush_lex();
    494 					return;
    495 				}
    496 			} else {
    497 				db_unread_token(t);
    498 				count = -1;
    499 			}
    500 			if ((cmd->flag & CS_MORE) == 0) {
    501 				db_skip_to_eol();
    502 			}
    503 		}
    504 	}
    505 	*last_cmdp = cmd;
    506 	last_count = count;
    507 	if (cmd != 0) {
    508 		/*
    509 		 * Execute the command.
    510 		 */
    511 		(*cmd->fcn)(addr, have_addr, count, modif);
    512 
    513 		if (cmd->flag & CS_SET_DOT) {
    514 			/*
    515 			 * If command changes dot, set dot to
    516 			 * previous address displayed (if 'ed' style).
    517 			 */
    518 			if (db_ed_style)
    519 				db_dot = db_prev;
    520 			else
    521 				db_dot = db_next;
    522 		} else {
    523 			/*
    524 			 * If command does not change dot,
    525 			 * set 'next' location to be the same.
    526 			 */
    527 			db_next = db_dot;
    528 		}
    529 	}
    530 }
    531 
    532 /*ARGSUSED*/
    533 static void
    534 db_map_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    535     const char *modif)
    536 {
    537 	bool full = false;
    538 
    539 	if (modif[0] == 'f')
    540 		full = true;
    541 
    542 	if (have_addr == false)
    543 		addr = (db_expr_t)(intptr_t) kernel_map;
    544 
    545 	uvm_map_printit((struct vm_map *)(intptr_t) addr, full, db_printf);
    546 }
    547 
    548 /*ARGSUSED*/
    549 static void
    550 db_malloc_print_cmd(db_expr_t addr, bool have_addr,
    551     db_expr_t count, const char *modif)
    552 {
    553 
    554 #ifdef MALLOC_DEBUG
    555 	if (!have_addr)
    556 		addr = 0;
    557 
    558 	debug_malloc_printit(db_printf, (vaddr_t) addr);
    559 #else
    560 	db_printf("The kernel is not built with the MALLOC_DEBUG option.\n");
    561 #endif /* MALLOC_DEBUG */
    562 }
    563 
    564 /*ARGSUSED*/
    565 static void
    566 db_object_print_cmd(db_expr_t addr, bool have_addr,
    567     db_expr_t count, const char *modif)
    568 {
    569 	bool full = false;
    570 
    571 	if (modif[0] == 'f')
    572 		full = true;
    573 
    574 	uvm_object_printit((struct uvm_object *)(intptr_t) addr, full,
    575 	    db_printf);
    576 }
    577 
    578 /*ARGSUSED*/
    579 static void
    580 db_page_print_cmd(db_expr_t addr, bool have_addr,
    581     db_expr_t count, const char *modif)
    582 {
    583 	bool full = false;
    584 
    585 	if (modif[0] == 'f')
    586 		full = true;
    587 
    588 	uvm_page_printit((struct vm_page *)(intptr_t) addr, full, db_printf);
    589 }
    590 
    591 /*ARGSUSED*/
    592 static void
    593 db_show_all_pages(db_expr_t addr, bool have_addr,
    594     db_expr_t count, const char *modif)
    595 {
    596 
    597 	uvm_page_printall(db_printf);
    598 }
    599 
    600 /*ARGSUSED*/
    601 static void
    602 db_buf_print_cmd(db_expr_t addr, bool have_addr,
    603     db_expr_t count, const char *modif)
    604 {
    605 	bool full = false;
    606 
    607 	if (modif[0] == 'f')
    608 		full = true;
    609 
    610 	vfs_buf_print((struct buf *)(intptr_t) addr, full, db_printf);
    611 }
    612 
    613 /*ARGSUSED*/
    614 static void
    615 db_event_print_cmd(db_expr_t addr, bool have_addr,
    616     db_expr_t count, const char *modif)
    617 {
    618 	bool full = false;
    619 
    620 	if (modif[0] == 'f')
    621 		full = true;
    622 
    623 	event_print(full, db_printf);
    624 }
    625 
    626 /*ARGSUSED*/
    627 static void
    628 db_vnode_print_cmd(db_expr_t addr, bool have_addr,
    629     db_expr_t count, const char *modif)
    630 {
    631 	bool full = false;
    632 
    633 	if (modif[0] == 'f')
    634 		full = true;
    635 
    636 	vfs_vnode_print((struct vnode *)(intptr_t) addr, full, db_printf);
    637 }
    638 
    639 static void
    640 db_mount_print_cmd(db_expr_t addr, bool have_addr,
    641     db_expr_t count, const char *modif)
    642 {
    643 	bool full = false;
    644 
    645 	if (modif[0] == 'f')
    646 		full = true;
    647 
    648 	vfs_mount_print((struct mount *)(intptr_t) addr, full, db_printf);
    649 }
    650 
    651 /*ARGSUSED*/
    652 static void
    653 db_mbuf_print_cmd(db_expr_t addr, bool have_addr,
    654     db_expr_t count, const char *modif)
    655 {
    656 
    657 	m_print((const struct mbuf *)(intptr_t) addr, modif, db_printf);
    658 }
    659 
    660 /*ARGSUSED*/
    661 static void
    662 db_pool_print_cmd(db_expr_t addr, bool have_addr,
    663     db_expr_t count, const char *modif)
    664 {
    665 
    666 	pool_printit((struct pool *)(intptr_t) addr, modif, db_printf);
    667 }
    668 
    669 /*ARGSUSED*/
    670 static void
    671 db_namecache_print_cmd(db_expr_t addr, bool have_addr,
    672     db_expr_t count, const char *modif)
    673 {
    674 
    675 	namecache_print((struct vnode *)(intptr_t) addr, db_printf);
    676 }
    677 
    678 /*ARGSUSED*/
    679 static void
    680 db_uvmexp_print_cmd(db_expr_t addr, bool have_addr,
    681     db_expr_t count, const char *modif)
    682 {
    683 
    684 	uvmexp_print(db_printf);
    685 }
    686 
    687 /*ARGSUSED*/
    688 static void
    689 db_lock_print_cmd(db_expr_t addr, bool have_addr,
    690     db_expr_t count, const char *modif)
    691 {
    692 
    693 	lockdebug_lock_print((void *)addr, db_printf);
    694 }
    695 
    696 /*
    697  * Call random function:
    698  * !expr(arg,arg,arg)
    699  */
    700 /*ARGSUSED*/
    701 static void
    702 db_fncall(db_expr_t addr, bool have_addr,
    703     db_expr_t count, const char *modif)
    704 {
    705 	db_expr_t	fn_addr;
    706 #define	MAXARGS		11
    707 	db_expr_t	args[MAXARGS];
    708 	int		nargs = 0;
    709 	db_expr_t	retval;
    710 	db_expr_t	(*func)(db_expr_t, ...);
    711 	int		t;
    712 
    713 	if (!db_expression(&fn_addr)) {
    714 		db_printf("Bad function\n");
    715 		db_flush_lex();
    716 		return;
    717 	}
    718 	func = (db_expr_t (*)(db_expr_t, ...))(intptr_t) fn_addr;
    719 
    720 	t = db_read_token();
    721 	if (t == tLPAREN) {
    722 		if (db_expression(&args[0])) {
    723 			nargs++;
    724 			while ((t = db_read_token()) == tCOMMA) {
    725 				if (nargs == MAXARGS) {
    726 					db_printf("Too many arguments\n");
    727 					db_flush_lex();
    728 					return;
    729 				}
    730 				if (!db_expression(&args[nargs])) {
    731 					db_printf("Argument missing\n");
    732 					db_flush_lex();
    733 					return;
    734 				}
    735 				nargs++;
    736 			}
    737 			db_unread_token(t);
    738 		}
    739 		if (db_read_token() != tRPAREN) {
    740 			db_printf("?\n");
    741 			db_flush_lex();
    742 			return;
    743 		}
    744 	}
    745 	db_skip_to_eol();
    746 
    747 	while (nargs < MAXARGS) {
    748 		args[nargs++] = 0;
    749 	}
    750 
    751 	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
    752 			 args[5], args[6], args[7], args[8], args[9]);
    753 	db_printf("%s\n", db_num_to_str(retval));
    754 }
    755 
    756 static void
    757 db_reboot_cmd(db_expr_t addr, bool have_addr,
    758     db_expr_t count, const char *modif)
    759 {
    760 	db_expr_t bootflags;
    761 
    762 	/* Flags, default to RB_AUTOBOOT */
    763 	if (!db_expression(&bootflags))
    764 		bootflags = (db_expr_t)RB_AUTOBOOT;
    765 	if (db_read_token() != tEOL) {
    766 		db_error("?\n");
    767 		/*NOTREACHED*/
    768 	}
    769 	/*
    770 	 * We are leaving DDB, never to return upward.
    771 	 * Clear db_recover so that we can debug faults in functions
    772 	 * called from cpu_reboot.
    773 	 */
    774 	db_recover = 0;
    775 	cpu_reboot((int)bootflags, NULL);
    776 }
    777 
    778 static void
    779 db_sifting_cmd(db_expr_t addr, bool have_addr,
    780     db_expr_t count, const char *modif)
    781 {
    782 	int	mode, t;
    783 
    784 	t = db_read_token();
    785 	if (t == tSLASH) {
    786 		t = db_read_token();
    787 		if (t != tIDENT) {
    788 			bad_modifier:
    789 			db_printf("Bad modifier\n");
    790 			db_flush_lex();
    791 			return;
    792 		}
    793 		if (!strcmp(db_tok_string, "F"))
    794 			mode = 'F';
    795 		else
    796 			goto bad_modifier;
    797 		t = db_read_token();
    798 	} else
    799 		mode = 0;
    800 
    801 	if (t == tIDENT)
    802 		db_sifting(db_tok_string, mode);
    803 	else {
    804 		db_printf("Bad argument (non-string)\n");
    805 		db_flush_lex();
    806 	}
    807 }
    808 
    809 static void
    810 db_stack_trace_cmd(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
    811 {
    812 	register const char *cp = modif;
    813 	register char c;
    814 	void (*pr)(const char *, ...);
    815 
    816 	pr = db_printf;
    817 	while ((c = *cp++) != 0)
    818 		if (c == 'l')
    819 			pr = printf;
    820 
    821 	if (count == -1)
    822 		count = 65535;
    823 
    824 	db_stack_trace_print(addr, have_addr, count, modif, pr);
    825 }
    826 
    827 static void
    828 db_sync_cmd(db_expr_t addr, bool have_addr,
    829     db_expr_t count, const char *modif)
    830 {
    831 
    832 	/*
    833 	 * We are leaving DDB, never to return upward.
    834 	 * Clear db_recover so that we can debug faults in functions
    835 	 * called from cpu_reboot.
    836 	 */
    837 	db_recover = 0;
    838 	cpu_reboot(RB_DUMP, NULL);
    839 }
    840