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