Home | History | Annotate | Line # | Download | only in ddb
db_command.c revision 1.131
      1 /*	$NetBSD: db_command.c,v 1.131 2009/03/21 13:06:39 ad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996, 1997, 1998, 1999, 2002, 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Adam Hamsik, and by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Mach Operating System
     34  * Copyright (c) 1991,1990 Carnegie Mellon University
     35  * All Rights Reserved.
     36  *
     37  * Permission to use, copy, modify and distribute this software and its
     38  * documentation is hereby granted, provided that both the copyright
     39  * notice and this permission notice appear in all copies of the
     40  * software, derivative works or modified versions, and any portions
     41  * thereof, and that both notices appear in supporting documentation.
     42  *
     43  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     44  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     45  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     46  *
     47  * Carnegie Mellon requests users of this software to return to
     48  *
     49  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     50  *  School of Computer Science
     51  *  Carnegie Mellon University
     52  *  Pittsburgh PA 15213-3890
     53  *
     54  * any improvements or extensions that they make and grant Carnegie the
     55  * rights to redistribute these changes.
     56  */
     57 
     58 /*
     59  * Command dispatcher.
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.131 2009/03/21 13:06:39 ad Exp $");
     64 
     65 #ifdef _KERNEL_OPT
     66 #include "opt_aio.h"
     67 #include "opt_ddb.h"
     68 #include "opt_kgdb.h"
     69 #include "opt_inet.h"
     70 #include "opt_uvmhist.h"
     71 #include "opt_ddbparam.h"
     72 #include "opt_multiprocessor.h"
     73 #include "arp.h"
     74 #endif
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/reboot.h>
     79 #include <sys/device.h>
     80 #include <sys/lwp.h>
     81 #include <sys/malloc.h>
     82 #include <sys/mbuf.h>
     83 #include <sys/namei.h>
     84 #include <sys/pool.h>
     85 #include <sys/proc.h>
     86 #include <sys/vnode.h>
     87 #include <sys/vmem.h>
     88 #include <sys/lockdebug.h>
     89 #include <sys/cpu.h>
     90 #include <sys/buf.h>
     91 #include <sys/module.h>
     92 
     93 /*include queue macros*/
     94 #include <sys/queue.h>
     95 
     96 #include <ddb/ddb.h>
     97 
     98 #include <uvm/uvm_extern.h>
     99 #include <uvm/uvm_ddb.h>
    100 
    101 /*
    102  * Results of command search.
    103  */
    104 #define	CMD_EXACT		0
    105 #define	CMD_PREFIX		1
    106 #define	CMD_NONE		2
    107 #define	CMD_AMBIGUOUS	3
    108 
    109 /*
    110  * Exported global variables
    111  */
    112 bool		db_cmd_loop_done;
    113 label_t		*db_recover;
    114 db_addr_t	db_dot;
    115 db_addr_t	db_last_addr;
    116 db_addr_t	db_prev;
    117 db_addr_t	db_next;
    118 
    119 
    120 /*
    121  * New DDB api for adding and removing commands uses three lists, because
    122  * we use two types of commands
    123  * a) standard commands without subcommands -> reboot
    124  * b) show commands which are subcommands of show command -> show aio_jobs
    125  * c) if defined machine specific commands
    126  *
    127  * ddb_add_cmd, ddb_rem_cmd use type (DDB_SHOW_CMD||DDB_BASE_CMD)argument to
    128  * add them to representativ lists.
    129  */
    130 
    131 static const struct db_command db_command_table[];
    132 static const struct db_command db_show_cmds[];
    133 
    134 #ifdef DB_MACHINE_COMMANDS
    135 static const struct db_command db_machine_command_table[];
    136 #endif
    137 
    138 /* the global queue of all command tables */
    139 TAILQ_HEAD(db_cmd_tbl_en_head, db_cmd_tbl_en);
    140 
    141 /* TAILQ entry used to register command tables */
    142 struct db_cmd_tbl_en {
    143 	const struct db_command *db_cmd;	/* cmd table */
    144 	TAILQ_ENTRY(db_cmd_tbl_en) db_cmd_next;
    145 };
    146 
    147 /* head of base commands list */
    148 static struct db_cmd_tbl_en_head db_base_cmd_list =
    149 	TAILQ_HEAD_INITIALIZER(db_base_cmd_list);
    150 static struct db_cmd_tbl_en db_base_cmd_builtins =
    151      { .db_cmd = db_command_table };
    152 
    153 /* head of show commands list */
    154 static struct db_cmd_tbl_en_head db_show_cmd_list =
    155 	TAILQ_HEAD_INITIALIZER(db_show_cmd_list);
    156 static struct db_cmd_tbl_en db_show_cmd_builtins =
    157      { .db_cmd = db_show_cmds };
    158 
    159 /* head of machine commands list */
    160 static struct db_cmd_tbl_en_head db_mach_cmd_list =
    161 	TAILQ_HEAD_INITIALIZER(db_mach_cmd_list);
    162 #ifdef DB_MACHINE_COMMANDS
    163 static struct db_cmd_tbl_en db_mach_cmd_builtins =
    164      { .db_cmd = db_machine_command_table };
    165 #endif
    166 
    167 /*
    168  * if 'ed' style: 'dot' is set at start of last item printed,
    169  * and '+' points to next line.
    170  * Otherwise: 'dot' points to next item, '..' points to last.
    171  */
    172 static bool	 db_ed_style = true;
    173 
    174 static void	db_init_commands(void);
    175 static int	db_register_tbl_entry(uint8_t type,
    176     struct db_cmd_tbl_en *list_ent);
    177 static void	db_cmd_list(const struct db_cmd_tbl_en_head *);
    178 static int	db_cmd_search(const char *, struct db_cmd_tbl_en_head *,
    179 			      const struct db_command **);
    180 static int	db_cmd_search_table(const char *, const struct db_command *,
    181 				    const struct db_command **);
    182 static void	db_cmd_search_failed(char *, int);
    183 static const struct db_command *db_read_command(void);
    184 static void	db_command(const struct db_command **);
    185 static void	db_buf_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    186 static void	db_event_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    187 static void	db_fncall(db_expr_t, bool, db_expr_t, const char *);
    188 static void     db_help_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    189 static void	db_lock_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    190 static void	db_mount_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    191 static void	db_mbuf_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    192 static void	db_malloc_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    193 static void	db_map_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    194 static void	db_namecache_print_cmd(db_expr_t, bool, db_expr_t,
    195 		    const char *);
    196 static void	db_object_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    197 static void	db_page_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    198 static void	db_show_all_pages(db_expr_t, bool, db_expr_t, const char *);
    199 static void	db_pool_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    200 static void	db_reboot_cmd(db_expr_t, bool, db_expr_t, const char *);
    201 static void	db_sifting_cmd(db_expr_t, bool, db_expr_t, const char *);
    202 static void	db_stack_trace_cmd(db_expr_t, bool, db_expr_t, const char *);
    203 static void	db_sync_cmd(db_expr_t, bool, db_expr_t, const char *);
    204 static void	db_whatis_cmd(db_expr_t, bool, db_expr_t, const char *);
    205 static void	db_uvmexp_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    206 #ifdef UVMHIST
    207 static void	db_uvmhist_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    208 #endif
    209 static void	db_vnode_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    210 static void	db_vmem_print_cmd(db_expr_t, bool, db_expr_t, const char *);
    211 
    212 static const struct db_command db_show_cmds[] = {
    213 	/*added from all sub cmds*/
    214 #ifdef _KERNEL	/* XXX CRASH(8) */
    215 	{ DDB_ADD_CMD("callout",  db_show_callout,
    216 	    0 ,"List all used callout functions.",NULL,NULL) },
    217 #endif
    218 	{ DDB_ADD_CMD("pages",	db_show_all_pages,
    219 	    0 ,"List all used memory pages.",NULL,NULL) },
    220 	{ DDB_ADD_CMD("procs",	db_show_all_procs,
    221 	    0 ,"List all processes.",NULL,NULL) },
    222 	{ DDB_ADD_CMD("pools",	db_show_all_pools,
    223 	    0 ,"Show all poolS",NULL,NULL) },
    224 #ifdef AIO
    225 	/*added from all sub cmds*/
    226 	{ DDB_ADD_CMD("aio_jobs",	db_show_aio_jobs,	0,
    227 	    "Show aio jobs",NULL,NULL) },
    228 #endif
    229 	{ DDB_ADD_CMD("all",	NULL,
    230 	    CS_COMPAT, NULL,NULL,NULL) },
    231 #if defined(INET) && (NARP > 0)
    232 	{ DDB_ADD_CMD("arptab",	db_show_arptab,		0,NULL,NULL,NULL) },
    233 #endif
    234 #ifdef _KERNEL
    235 	{ DDB_ADD_CMD("breaks",	db_listbreak_cmd, 	0,
    236 	    "Display all breaks.",NULL,NULL) },
    237 #endif
    238 	{ DDB_ADD_CMD("buf",	db_buf_print_cmd,	0,
    239 	    "Print the struct buf at address.", "[/f] address",NULL) },
    240 	{ DDB_ADD_CMD("event",	db_event_print_cmd,	0,
    241 	    "Print all the non-zero evcnt(9) event counters.", "[/f]",NULL) },
    242 	{ DDB_ADD_CMD("files", db_show_files_cmd,	0,
    243 	    "Print the files open by process at address",
    244 	    "[/f] address", NULL) },
    245 	{ DDB_ADD_CMD("lock",	db_lock_print_cmd,	0,NULL,NULL,NULL) },
    246 	{ DDB_ADD_CMD("malloc",	db_malloc_print_cmd,0,NULL,NULL,NULL) },
    247 	{ DDB_ADD_CMD("map",	db_map_print_cmd,	0,
    248 	    "Print the vm_map at address.", "[/f] address",NULL) },
    249 	{ DDB_ADD_CMD("module", db_show_module_cmd,	0,
    250 	    "Print kernel modules", NULL, NULL) },
    251 	{ DDB_ADD_CMD("mount",	db_mount_print_cmd,	0,
    252 	    "Print the mount structure at address.", "[/f] address",NULL) },
    253 	{ DDB_ADD_CMD("mqueue", db_show_mqueue_cmd,	0,
    254 	    "Print the message queues", NULL, NULL) },
    255 	{ DDB_ADD_CMD("mbuf",	db_mbuf_print_cmd,	0,NULL,NULL,
    256 	    "-c prints all mbuf chains") },
    257 	{ DDB_ADD_CMD("ncache",	db_namecache_print_cmd,	0,
    258 	    "Dump the namecache list.", "address",NULL) },
    259 	{ DDB_ADD_CMD("object",	db_object_print_cmd,	0,
    260 	    "Print the vm_object at address.", "[/f] address",NULL) },
    261 	{ DDB_ADD_CMD("page",	db_page_print_cmd,	0,
    262 	    "Print the vm_page at address.", "[/f] address",NULL) },
    263 	{ DDB_ADD_CMD("pool",	db_pool_print_cmd,	0,
    264 	    "Print the pool at address.", "[/clp] address",NULL) },
    265 	{ DDB_ADD_CMD("registers",	db_show_regs,		0,
    266 	    "Display the register set.", "[/u]",NULL) },
    267 	{ DDB_ADD_CMD("sched_qs",	db_show_sched_qs,	0,
    268 	    "Print the state of the scheduler's run queues.",
    269 	    NULL,NULL) },
    270 	{ DDB_ADD_CMD("uvmexp",	db_uvmexp_print_cmd, 0,
    271 	    "Print a selection of UVM counters and statistics.",
    272 	    NULL,NULL) },
    273 #ifdef UVMHIST
    274 	{ DDB_ADD_CMD("uvmhist", db_uvmhist_print_cmd, 0,
    275 	    "Print the UVM history logs.",
    276 	    NULL,NULL) },
    277 #endif
    278 	{ DDB_ADD_CMD("vnode",	db_vnode_print_cmd,	0,
    279 	    "Print the vnode at address.", "[/f] address",NULL) },
    280 	{ DDB_ADD_CMD("vmem", db_vmem_print_cmd,	0,
    281 	    "Print the vmem usage.", "[/a] address", NULL) },
    282 	{ DDB_ADD_CMD("vmems", db_show_all_vmems,	0,
    283 	    "Show all vmems.", NULL, NULL) },
    284 #ifdef _KERNEL
    285 	{ DDB_ADD_CMD("watches",	db_listwatch_cmd, 	0,
    286 	    "Display all watchpoints.", NULL,NULL) },
    287 #endif
    288 	{ DDB_ADD_CMD(NULL,		NULL,			0,NULL,NULL,NULL) }
    289 };
    290 
    291 /* arch/<arch>/<arch>/db_interface.c */
    292 #ifdef DB_MACHINE_COMMANDS
    293 extern const struct db_command db_machine_command_table[];
    294 #endif
    295 
    296 static const struct db_command db_command_table[] = {
    297 	{ DDB_ADD_CMD("b",		db_breakpoint_cmd,	0,
    298 	    "Set a breakpoint at address", "[/u] address[,count].",NULL) },
    299 	{ DDB_ADD_CMD("break",	db_breakpoint_cmd,	0,
    300 	    "Set a breakpoint at address", "[/u] address[,count].",NULL) },
    301 	{ DDB_ADD_CMD("bt",		db_stack_trace_cmd,	0,
    302 	    "Show backtrace.", "See help trace.",NULL) },
    303 	{ DDB_ADD_CMD("c",		db_continue_cmd,	0,
    304 	    "Continue execution.", "[/c]",NULL) },
    305 	{ DDB_ADD_CMD("call",	db_fncall,		CS_OWN,
    306 	    "Call the function", "address[(expression[,...])]",NULL) },
    307 #ifdef _KERNEL	/* XXX CRASH(8) */
    308 	{ DDB_ADD_CMD("callout",	db_show_callout,	0, NULL,
    309 	    NULL,NULL ) },
    310 #endif
    311 	{ DDB_ADD_CMD("continue",	db_continue_cmd,	0,
    312 	    "Continue execution.", "[/c]",NULL) },
    313 	{ DDB_ADD_CMD("d",		db_delete_cmd,		0,
    314 	    "Delete a breakpoint.", "address | #number",NULL) },
    315 	{ DDB_ADD_CMD("delete",	db_delete_cmd,		0,
    316 	    "Delete a breakpoint.", "address | #number",NULL) },
    317 	{ DDB_ADD_CMD("dmesg",	db_dmesg,		0,
    318 	    "Show kernel message buffer.", "[count]",NULL) },
    319 	{ DDB_ADD_CMD("dwatch",	db_deletewatch_cmd,	0,
    320 	    "Delete the watchpoint.", "address",NULL) },
    321 	{ DDB_ADD_CMD("examine",	db_examine_cmd,		CS_SET_DOT,
    322 	    "Display the address locations.",
    323 	    "[/modifier] address[,count]",NULL) },
    324 	{ DDB_ADD_CMD("exit",		db_continue_cmd,	0,
    325 	    "Continue execution.", "[/c]",NULL) },
    326 	{ DDB_ADD_CMD("help",   db_help_print_cmd, CS_OWN|CS_NOREPEAT,
    327 	    "Display help about commands",
    328 	    "Use other commands as arguments.",NULL) },
    329 	{ DDB_ADD_CMD("kill",	db_kill_proc,		CS_OWN,
    330 	    "Send a signal to the process","pid[,signal_number]",
    331 	    "   pid:\t\t\tthe process id (may need 0t prefix for decimal)\n"
    332 	    "   signal_number:\tthe signal to send") },
    333 #ifdef KGDB
    334 	{ DDB_ADD_CMD("kgdb",	db_kgdb_cmd,	0,	NULL,NULL,NULL) },
    335 #endif
    336 	{ DDB_ADD_CMD("machine",NULL,CS_MACH,
    337 	    "Architecture specific functions.",NULL,NULL) },
    338 	{ DDB_ADD_CMD("match",	db_trace_until_matching_cmd,0,
    339 	    "Stop at the matching return instruction.","See help next",NULL) },
    340 	{ DDB_ADD_CMD("next",	db_trace_until_matching_cmd,0,
    341 	    "Stop at the matching return instruction.","[/p]",NULL) },
    342 	{ DDB_ADD_CMD("p",		db_print_cmd,		0,
    343 	    "Print address according to the format.",
    344 	    "[/axzodurc] address [address ...]",NULL) },
    345 	{ DDB_ADD_CMD("print",	db_print_cmd,		0,
    346 	    "Print address according to the format.",
    347 	    "[/axzodurc] address [address ...]",NULL) },
    348 	{ DDB_ADD_CMD("ps",		db_show_all_procs,	0,
    349 	    "Print all processes.","See show all procs",NULL) },
    350 	{ DDB_ADD_CMD("quit",		db_continue_cmd,	0,
    351 	    "Continue execution.", "[/c]",NULL) },
    352 	{ DDB_ADD_CMD("reboot",	db_reboot_cmd,		CS_OWN,
    353 	    "Reboot","0x1  RB_ASKNAME, 0x2 RB_SINGLE, 0x4 RB_NOSYNC, 0x8 RB_HALT,"
    354 	    "0x40 RB_KDB, 0x100 RB_DUMP, 0x808 RB_POWERDOWN",NULL) },
    355 	{ DDB_ADD_CMD("s",		db_single_step_cmd,	0,
    356 	    "Single-step count times.","[/p] [,count]",NULL) },
    357 	{ DDB_ADD_CMD("search",	db_search_cmd,		CS_OWN|CS_SET_DOT,
    358 	    "Search memory from address for value.",
    359 	    "[/bhl] address value [mask] [,count]",NULL) },
    360 	{ DDB_ADD_CMD("set",	db_set_cmd,		CS_OWN,
    361 	    "Set the named variable","$variable [=] expression",NULL) },
    362 	{ DDB_ADD_CMD("show",	NULL, CS_SHOW,
    363 	    "Show kernel stats.", NULL,NULL) },
    364 	{ DDB_ADD_CMD("sifting",	db_sifting_cmd,		CS_OWN,
    365 	    "Search the symbol tables ","[/F] string",NULL) },
    366 	{ DDB_ADD_CMD("step",	db_single_step_cmd,	0,
    367 	    "Single-step count times.","[/p] [,count]",NULL) },
    368 	{ DDB_ADD_CMD("sync",	db_sync_cmd,		CS_OWN,
    369 	    "Force a crash dump, and then reboot.",NULL,NULL) },
    370 	{ DDB_ADD_CMD("trace",	db_stack_trace_cmd,	0,
    371 	    "Stack trace from frame-address.",
    372 	    "[/u[l]] [frame-address][,count]",NULL) },
    373 	{ DDB_ADD_CMD("until",	db_trace_until_call_cmd,0,
    374 	    "Stop at the next call or return instruction.","[/p]",NULL) },
    375 	{ DDB_ADD_CMD("w",		db_write_cmd,		CS_MORE|CS_SET_DOT,
    376 	    "Write the expressions at succeeding locations.",
    377 	    "[/bhl] address expression [expression ...]",NULL) },
    378 	{ DDB_ADD_CMD("watch",	db_watchpoint_cmd,	CS_MORE,
    379 	    "Set a watchpoint for a region. ","address[,size]",NULL) },
    380 	{ DDB_ADD_CMD("whatis",	db_whatis_cmd, 0,
    381 	    "Describe what an address is", "address", NULL) },
    382 	{ DDB_ADD_CMD("write",	db_write_cmd,		CS_MORE|CS_SET_DOT,
    383 	    "Write the expressions at succeeding locations.",
    384 	    "[/bhl] address expression [expression ...]",NULL) },
    385 	{ DDB_ADD_CMD("x",		db_examine_cmd,		CS_SET_DOT,
    386 	    "Display the address locations.",
    387 	    "[/modifier] address[,count]",NULL) },
    388 	{ DDB_ADD_CMD(NULL, 	NULL,		   0, NULL, NULL, NULL) }
    389 };
    390 
    391 static const struct db_command	*db_last_command = NULL;
    392 #if defined(DDB_COMMANDONENTER)
    393 char db_cmd_on_enter[DB_LINE_MAXLEN + 1] = ___STRING(DDB_COMMANDONENTER);
    394 #else /* defined(DDB_COMMANDONENTER) */
    395 char db_cmd_on_enter[DB_LINE_MAXLEN + 1] = "";
    396 #endif /* defined(DDB_COMMANDONENTER) */
    397 #define	DB_LINE_SEP	';'
    398 
    399 /*
    400  * Execute commandlist after ddb start
    401  * This function goes through the command list created from commands and ';'
    402  */
    403 static void
    404 db_execute_commandlist(const char *cmdlist)
    405 {
    406 	const char *cmd = cmdlist;
    407 	const struct db_command	*dummy = NULL;
    408 
    409 	while (*cmd != '\0') {
    410 		const char *ep = cmd;
    411 
    412 		while (*ep != '\0' && *ep != DB_LINE_SEP) {
    413 			ep++;
    414 		}
    415 		db_set_line(cmd, ep);
    416 		db_command(&dummy);
    417 		cmd = ep;
    418 		if (*cmd == DB_LINE_SEP) {
    419 			cmd++;
    420 		}
    421 	}
    422 }
    423 
    424 /* Initialize ddb command tables */
    425 void
    426 db_init_commands(void)
    427 {
    428 	static bool done = false;
    429 
    430 	if (done) return;
    431 	done = true;
    432 
    433 	/* register command tables */
    434 	(void)db_register_tbl_entry(DDB_BASE_CMD, &db_base_cmd_builtins);
    435 #ifdef DB_MACHINE_COMMANDS
    436 	(void)db_register_tbl_entry(DDB_MACH_CMD, &db_mach_cmd_builtins);
    437 #endif
    438 	(void)db_register_tbl_entry(DDB_SHOW_CMD, &db_show_cmd_builtins);
    439 }
    440 
    441 
    442 /*
    443  * Add command table to the specified list
    444  * Arg:
    445  * int type specifies type of command table DDB_SHOW_CMD|DDB_BASE_CMD|DDB_MAC_CMD
    446  * *cmd_tbl poiter to static allocated db_command table
    447  *
    448  * Command table must be NULL terminated array of struct db_command
    449  */
    450 int
    451 db_register_tbl(uint8_t type, const struct db_command *cmd_tbl)
    452 {
    453 	struct db_cmd_tbl_en *list_ent;
    454 
    455 	/* empty list - ignore */
    456 	if (cmd_tbl->name == 0)
    457 		return 0;
    458 
    459 	/* force builtin commands to be registered first */
    460 	db_init_commands();
    461 
    462 	/* now create a list entry for this table */
    463 	list_ent = db_zalloc(sizeof(*list_ent));
    464 	if (list_ent == NULL)
    465 		return ENOMEM;
    466 	list_ent->db_cmd=cmd_tbl;
    467 
    468 	/* and register it */
    469 	return db_register_tbl_entry(type, list_ent);
    470 }
    471 
    472 static int
    473 db_register_tbl_entry(uint8_t type, struct db_cmd_tbl_en *list_ent)
    474 {
    475 	struct db_cmd_tbl_en_head *list;
    476 
    477 	switch(type) {
    478 	case DDB_BASE_CMD:
    479 		list = &db_base_cmd_list;
    480 		break;
    481 	case DDB_SHOW_CMD:
    482 		list = &db_show_cmd_list;
    483 		break;
    484 	case DDB_MACH_CMD:
    485 		list = &db_mach_cmd_list;
    486 		break;
    487 	default:
    488 		return ENOENT;
    489 	}
    490 
    491 	TAILQ_INSERT_TAIL(list, list_ent, db_cmd_next);
    492 
    493 	return 0;
    494 }
    495 
    496 /*
    497  * Remove command table specified with db_cmd address == cmd_tbl
    498  */
    499 int
    500 db_unregister_tbl(uint8_t type,const struct db_command *cmd_tbl)
    501 {
    502 	struct db_cmd_tbl_en *list_ent;
    503 	struct db_cmd_tbl_en_head *list;
    504 
    505 	/* find list on which the entry should live */
    506 	switch (type) {
    507 	case DDB_BASE_CMD:
    508 		list=&db_base_cmd_list;
    509 		break;
    510 	case DDB_SHOW_CMD:
    511 		list=&db_show_cmd_list;
    512 		break;
    513 	case DDB_MACH_CMD:
    514 		list=&db_mach_cmd_list;
    515 		break;
    516 	default:
    517 		return EINVAL;
    518 	}
    519 
    520 	TAILQ_FOREACH (list_ent, list, db_cmd_next) {
    521 		if (list_ent->db_cmd == cmd_tbl){
    522 			TAILQ_REMOVE(list,
    523 			    list_ent, db_cmd_next);
    524 			db_free(list_ent, sizeof(*list_ent));
    525 			return 0;
    526 		}
    527 	}
    528 	return ENOENT;
    529 }
    530 
    531 /* This function is called from machine trap code. */
    532 void
    533 db_command_loop(void)
    534 {
    535 	label_t	db_jmpbuf;
    536 	label_t	*savejmp;
    537 
    538 	/*
    539 	 * Initialize 'prev' and 'next' to dot.
    540 	 */
    541 	db_prev = db_dot;
    542 	db_next = db_dot;
    543 
    544 	db_cmd_loop_done = false;
    545 
    546 	/* Init default command tables add machine, base,
    547 	   show command tables to the list */
    548 	db_init_commands();
    549 
    550 	/* save context for return from ddb */
    551 	savejmp = db_recover;
    552 	db_recover = &db_jmpbuf;
    553 	(void) setjmp(&db_jmpbuf);
    554 
    555 	/* Execute default ddb start commands */
    556 	db_execute_commandlist(db_cmd_on_enter);
    557 
    558 	(void) setjmp(&db_jmpbuf);
    559 	while (!db_cmd_loop_done) {
    560 		if (db_print_position() != 0)
    561 			db_printf("\n");
    562 		db_output_line = 0;
    563 		(void) db_read_line();
    564 		db_command(&db_last_command);
    565 	}
    566 
    567 	db_recover = savejmp;
    568 }
    569 
    570 /*
    571  * Search command table for command prefix
    572  */
    573 static int
    574 db_cmd_search_table(const char *name,
    575 		    const struct db_command *table,
    576 		    const struct db_command **cmdp)
    577 {
    578 
    579 	const struct db_command	*cmd;
    580 	int result;
    581 
    582 	result = CMD_NONE;
    583 	*cmdp = NULL;
    584 
    585 	for (cmd = table; cmd->name != 0; cmd++) {
    586 		const char *lp;
    587 		const char *rp;
    588 
    589 		lp = name;
    590 		rp = cmd->name;
    591 		while (*lp != '\0' && *lp == *rp) {
    592 			rp++;
    593 			lp++;
    594 		}
    595 
    596 		if (*lp != '\0') /* mismatch or extra chars in name */
    597 			continue;
    598 
    599 		if (*rp == '\0') { /* exact match */
    600 			*cmdp = cmd;
    601 			return (CMD_EXACT);
    602 		}
    603 
    604 		/* prefix match: end of name, not end of command */
    605 		if (result == CMD_NONE) {
    606 			result = CMD_PREFIX;
    607 			*cmdp = cmd;
    608 		}
    609 		else if (result == CMD_PREFIX) {
    610 			result = CMD_AMBIGUOUS;
    611 			*cmdp = NULL;
    612 		}
    613 	}
    614 
    615 	return (result);
    616 }
    617 
    618 
    619 /*
    620  * Search list of command tables for command
    621  */
    622 static int
    623 db_cmd_search(const char *name,
    624 	      struct db_cmd_tbl_en_head *list_head,
    625 	      const struct db_command **cmdp)
    626 {
    627 	struct db_cmd_tbl_en *list_ent;
    628 	const struct db_command *found_command;
    629 	bool accept_prefix_match;
    630 	int result;
    631 
    632 	result = CMD_NONE;
    633 	found_command = NULL;
    634 	accept_prefix_match = true;
    635 
    636 	TAILQ_FOREACH(list_ent, list_head, db_cmd_next) {
    637 		const struct db_command *cmd;
    638 		int found;
    639 
    640 		found = db_cmd_search_table(name, list_ent->db_cmd, &cmd);
    641 		if (found == CMD_EXACT) {
    642 			result = CMD_EXACT;
    643 			found_command = cmd;
    644 			break;
    645 		}
    646 
    647 		if (found == CMD_PREFIX) {
    648 			if (accept_prefix_match) {
    649 				/*
    650 				 * Continue search, but note current result
    651 				 * in case we won't find anything else.
    652 				 */
    653 				accept_prefix_match = false;
    654 				result = CMD_PREFIX;
    655 				found_command = cmd;
    656 			} else {
    657 				/*
    658 				 * Watch out for globally ambiguous
    659 				 * prefix match that is not locally
    660 				 * ambiguous - with one match in one
    661 				 * table and another match(es) in
    662 				 * another table.
    663 				 */
    664 				result = CMD_AMBIGUOUS;
    665 				found_command = NULL;
    666 			}
    667 		}
    668 		else if (found == CMD_AMBIGUOUS) {
    669 			accept_prefix_match = false;
    670 			result = CMD_AMBIGUOUS;
    671 			found_command = NULL;
    672 		}
    673 	}
    674 
    675 	*cmdp = found_command;
    676 	return result;
    677 }
    678 
    679 static void
    680 db_cmd_search_failed(char *name, int search_result)
    681 {
    682 	if (search_result == CMD_NONE)
    683 		db_printf("No such command: %s\n", name);
    684 	else
    685 		db_printf("Ambiguous command: %s\n", name);
    686 }
    687 
    688 
    689 /*
    690  * List commands to the console.
    691  */
    692 static void
    693 db_cmd_list(const struct db_cmd_tbl_en_head *list)
    694 {
    695 
    696 	struct db_cmd_tbl_en *list_ent;
    697 	const struct db_command *table;
    698 	size_t		i, j, w, columns, lines, numcmds, width=0;
    699 	const char	*p;
    700 
    701 	TAILQ_FOREACH(list_ent,list,db_cmd_next) {
    702 		table = list_ent->db_cmd;
    703 		for (i = 0; table[i].name != NULL; i++) {
    704 			w = strlen(table[i].name);
    705 			if (w > width)
    706 				width = w;
    707 		}
    708 	}
    709 
    710 	width = DB_NEXT_TAB(width);
    711 
    712 	columns = db_max_width / width;
    713 	if (columns == 0)
    714 		columns = 1;
    715 
    716 	TAILQ_FOREACH(list_ent,list,db_cmd_next) {
    717 		table = list_ent->db_cmd;
    718 
    719 		for (numcmds = 0; table[numcmds].name != NULL; numcmds++)
    720 			;
    721 		lines = (numcmds + columns - 1) / columns;
    722 
    723 		for (i = 0; i < lines; i++) {
    724 			for (j = 0; j < columns; j++) {
    725 				p = table[j * lines + i].name;
    726 				if (p)
    727 					db_printf("%s", p);
    728 				if (j * lines + i + lines >= numcmds) {
    729 					db_putchar('\n');
    730 					break;
    731 				}
    732 				if (p) {
    733 					w = strlen(p);
    734 					while (w < width) {
    735 						w = DB_NEXT_TAB(w);
    736 						db_putchar('\t');
    737 					}
    738 				}
    739 			}
    740 		}
    741 	}
    742 	return;
    743 }
    744 
    745 /*
    746  * Read complete command with all subcommands, starting with current
    747  * db_tok_string. If subcommand is missing, print the list of all
    748  * subcommands.  If command/subcommand is not found, print an error
    749  * message.  Returns pointer to "leaf" command or NULL.
    750  */
    751 static const struct db_command *
    752 db_read_command(void)
    753 {
    754 	const struct db_command *command;
    755 	struct db_cmd_tbl_en_head *list;
    756 	int found;
    757 	int t;
    758 
    759 	list = &db_base_cmd_list;
    760 	do {
    761 		found = db_cmd_search(db_tok_string, list, &command);
    762 		if (command == NULL) {
    763 			db_cmd_search_failed(db_tok_string, found);
    764 			db_flush_lex();
    765 			return NULL;
    766 		}
    767 
    768 		if (command->flag == CS_SHOW)
    769 			list = &db_show_cmd_list;
    770 		else if (command->flag == CS_MACH)
    771 			list = &db_mach_cmd_list;
    772 		else if (command->flag == CS_COMPAT)
    773 			/* same list */;
    774 		else
    775 			break; /* expect no more subcommands */
    776 
    777 		t = db_read_token(); /* read subcommand */
    778 		if (t != tIDENT) {
    779 			/* if none given - just print all of them */
    780 			db_cmd_list(list);
    781 			db_flush_lex();
    782 			return NULL;
    783 		}
    784 	} while (list != NULL);
    785 
    786 	return command;
    787 }
    788 
    789 /*
    790  * Parse command line and execute apropriate function.
    791  */
    792 static void
    793 db_command(const struct db_command **last_cmdp)
    794 {
    795 	const struct db_command *command;
    796 	static db_expr_t last_count = 0;
    797 	db_expr_t	addr, count;
    798 	char		modif[TOK_STRING_SIZE];
    799 
    800 	int			t;
    801 	bool		have_addr = false;
    802 
    803 	command = NULL;
    804 
    805 	t = db_read_token();
    806 	if ((t == tEOL) || (t == tCOMMA)) {
    807 		/*
    808 		 * An empty line repeats last command, at 'next'.
    809 		 * Only a count repeats the last command with the new count.
    810 		 */
    811 		command = *last_cmdp;
    812 
    813 		if (!command)
    814 			return;
    815 
    816 		addr = (db_expr_t)db_next;
    817 		if (t == tCOMMA) {
    818 			if (!db_expression(&count)) {
    819 				db_printf("Count missing\n");
    820 				db_flush_lex();
    821 				return;
    822 			}
    823 		} else
    824 			count = last_count;
    825 		have_addr = false;
    826 		modif[0] = '\0';
    827 		db_skip_to_eol();
    828 
    829 	} else if (t == tEXCL) {
    830 		db_fncall(0, 0, 0, NULL);
    831 		return;
    832 
    833 	} else if (t != tIDENT) {
    834 		db_printf("?\n");
    835 		db_flush_lex();
    836 		return;
    837 
    838 	} else {
    839 
    840 		command = db_read_command();
    841 		if (command == NULL)
    842 			return;
    843 
    844 		if ((command->flag & CS_OWN) == 0) {
    845 
    846 			/*
    847 			 * Standard syntax:
    848 			 * command [/modifier] [addr] [,count]
    849 			 */
    850 			t = db_read_token(); /* get modifier */
    851 			if (t == tSLASH) {
    852 				t = db_read_token();
    853 				if (t != tIDENT) {
    854 					db_printf("Bad modifier\n");
    855 					db_flush_lex();
    856 					return;
    857 				}
    858 				/* save modifier */
    859 				strlcpy(modif, db_tok_string, sizeof(modif));
    860 
    861 			} else {
    862 				db_unread_token(t);
    863 				modif[0] = '\0';
    864 			}
    865 
    866 			if (db_expression(&addr)) { /*get address*/
    867 				db_dot = (db_addr_t) addr;
    868 				db_last_addr = db_dot;
    869 				have_addr = true;
    870 			} else {
    871 				addr = (db_expr_t) db_dot;
    872 				have_addr = false;
    873 			}
    874 
    875 			t = db_read_token();
    876 			if (t == tCOMMA) { /*Get count*/
    877 				if (!db_expression(&count)) {
    878 					db_printf("Count missing\n");
    879 					db_flush_lex();
    880 					return;
    881 				}
    882 			} else {
    883 				db_unread_token(t);
    884 				count = -1;
    885 			}
    886 			if ((command->flag & CS_MORE) == 0) {
    887 				db_skip_to_eol();
    888 			}
    889 		}
    890 	}
    891 
    892 	if (command != NULL && command->flag & CS_NOREPEAT) {
    893 		*last_cmdp = NULL;
    894 		last_count = 0;
    895 	} else {
    896 		*last_cmdp = command;
    897 		last_count = count;
    898 	}
    899 
    900 
    901 	if (command != NULL) {
    902 		/*
    903 		 * Execute the command.
    904 		 */
    905 		if (command->fcn != NULL)
    906 			(*command->fcn)(addr, have_addr, count, modif);
    907 
    908 		if (command->flag & CS_SET_DOT) {
    909 			/*
    910 			 * If command changes dot, set dot to
    911 			 * previous address displayed (if 'ed' style).
    912 			 */
    913 			if (db_ed_style)
    914 				db_dot = db_prev;
    915 			else
    916 				db_dot = db_next;
    917 		} else {
    918 			/*
    919 			 * If command does not change dot,
    920 			 * set 'next' location to be the same.
    921 			 */
    922 			db_next = db_dot;
    923 		}
    924 	}
    925 }
    926 
    927 /*
    928  * Print help for commands
    929  */
    930 static void
    931 db_help_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    932 const char *modif)
    933 {
    934 	const struct db_command *command;
    935 	int t;
    936 
    937 	t = db_read_token();
    938 
    939 	/* is there another command after the "help"? */
    940 	if (t != tIDENT) {
    941 		/* print base commands */
    942 		db_cmd_list(&db_base_cmd_list);
    943 		return;
    944 	}
    945 
    946 	command = db_read_command();
    947 	if (command == NULL)
    948 		return;
    949 
    950 #ifdef DDB_VERBOSE_HELP
    951 	db_printf("Command: %s\n", command->name);
    952 	if (command->cmd_descr != NULL)
    953 		db_printf(" Description: %s\n", command->cmd_descr);
    954 	if (command->cmd_arg != NULL)
    955 		db_printf(" Arguments: %s\n", command->cmd_arg);
    956 	if (command->cmd_arg_help != NULL)
    957 		db_printf(" Arguments description:\n%s\n",
    958 			  command->cmd_arg_help);
    959 	if ((command->cmd_arg == NULL) && (command->cmd_descr == NULL))
    960 		db_printf(" No help message.\n");
    961 #endif
    962 
    963 	db_skip_to_eol();
    964 }
    965 
    966 /*ARGSUSED*/
    967 static void
    968 db_map_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    969     const char *modif)
    970 {
    971 	bool full = false;
    972 
    973 	if (modif[0] == 'f')
    974 		full = true;
    975 
    976 	if (have_addr == false)
    977 		addr = (db_expr_t)(uintptr_t)db_read_ptr("kernel_map");
    978 
    979 #ifdef _KERNEL
    980 	uvm_map_printit((struct vm_map *)(uintptr_t) addr, full, db_printf);
    981 #endif	/* XXX CRASH(8) */
    982 }
    983 
    984 /*ARGSUSED*/
    985 static void
    986 db_malloc_print_cmd(db_expr_t addr, bool have_addr,
    987     db_expr_t count, const char *modif)
    988 {
    989 
    990 #ifdef MALLOC_DEBUG
    991 	if (!have_addr)
    992 		addr = 0;
    993 
    994 	debug_malloc_printit(db_printf, (vaddr_t) addr);
    995 #else
    996 	db_printf("The kernel is not built with the MALLOC_DEBUG option.\n");
    997 #endif /* MALLOC_DEBUG */
    998 }
    999 
   1000 /*ARGSUSED*/
   1001 static void
   1002 db_object_print_cmd(db_expr_t addr, bool have_addr,
   1003     db_expr_t count, const char *modif)
   1004 {
   1005 	bool full = false;
   1006 
   1007 	if (modif[0] == 'f')
   1008 		full = true;
   1009 
   1010 #ifdef _KERNEL /* XXX CRASH(8) */
   1011 	uvm_object_printit((struct uvm_object *)(uintptr_t) addr, full,
   1012 	    db_printf);
   1013 #endif
   1014 }
   1015 
   1016 /*ARGSUSED*/
   1017 static void
   1018 db_page_print_cmd(db_expr_t addr, bool have_addr,
   1019     db_expr_t count, const char *modif)
   1020 {
   1021 	bool full = false;
   1022 
   1023 	if (modif[0] == 'f')
   1024 		full = true;
   1025 
   1026 #ifdef _KERNEL /* XXX CRASH(8) */
   1027 	uvm_page_printit((struct vm_page *)(uintptr_t) addr, full, db_printf);
   1028 #endif
   1029 }
   1030 
   1031 /*ARGSUSED*/
   1032 static void
   1033 db_show_all_pages(db_expr_t addr, bool have_addr,
   1034     db_expr_t count, const char *modif)
   1035 {
   1036 
   1037 #ifdef _KERNEL /* XXX CRASH(8) */
   1038 	uvm_page_printall(db_printf);
   1039 #endif
   1040 }
   1041 
   1042 /*ARGSUSED*/
   1043 static void
   1044 db_buf_print_cmd(db_expr_t addr, bool have_addr,
   1045     db_expr_t count, const char *modif)
   1046 {
   1047 	bool full = false;
   1048 
   1049 	if (modif[0] == 'f')
   1050 		full = true;
   1051 
   1052 #ifdef _KERNEL /* XXX CRASH(8) */
   1053 	vfs_buf_print((struct buf *)(uintptr_t) addr, full, db_printf);
   1054 #endif
   1055 }
   1056 
   1057 /*ARGSUSED*/
   1058 static void
   1059 db_event_print_cmd(db_expr_t addr, bool have_addr,
   1060     db_expr_t count, const char *modif)
   1061 {
   1062 	bool full = false;
   1063 	struct evcnt ev, *evp;
   1064 	char buf[80];
   1065 
   1066 	if (modif[0] == 'f')
   1067 		full = true;
   1068 
   1069 	evp = (struct evcnt *)db_read_ptr("allevents");
   1070 	while (evp != NULL) {
   1071 		db_read_bytes((db_addr_t)evp, sizeof(ev), (char *)&ev);
   1072 		evp = ev.ev_list.tqe_next;
   1073 		if (ev.ev_count == 0 && !full)
   1074 			continue;
   1075 		db_read_bytes((db_addr_t)ev.ev_group, ev.ev_grouplen + 1, buf);
   1076 		db_printf("evcnt type %d: %s ", ev.ev_type, buf);
   1077 		db_read_bytes((db_addr_t)ev.ev_name, ev.ev_namelen + 1, buf);
   1078 		db_printf("%s = %lld\n", buf, (long long)ev.ev_count);
   1079 	}
   1080 }
   1081 
   1082 /*ARGSUSED*/
   1083 static void
   1084 db_vnode_print_cmd(db_expr_t addr, bool have_addr,
   1085     db_expr_t count, const char *modif)
   1086 {
   1087 	bool full = false;
   1088 
   1089 	if (modif[0] == 'f')
   1090 		full = true;
   1091 
   1092 #ifdef _KERNEL /* XXX CRASH(8) */
   1093 	vfs_vnode_print((struct vnode *)(uintptr_t) addr, full, db_printf);
   1094 #endif
   1095 }
   1096 
   1097 /*ARGSUSED*/
   1098 static void
   1099 db_vmem_print_cmd(db_expr_t addr, bool have_addr,
   1100     db_expr_t count, const char *modif)
   1101 {
   1102 
   1103 #ifdef _KERNEL /* XXX CRASH(8) */
   1104 	vmem_print((uintptr_t) addr, modif, db_printf);
   1105 #endif
   1106 }
   1107 
   1108 static void
   1109 db_mount_print_cmd(db_expr_t addr, bool have_addr,
   1110     db_expr_t count, const char *modif)
   1111 {
   1112 	bool full = false;
   1113 
   1114 	if (modif[0] == 'f')
   1115 		full = true;
   1116 
   1117 #ifdef _KERNEL	/* XXX CRASH(8) */
   1118 	vfs_mount_print((struct mount *)(uintptr_t) addr, full, db_printf);
   1119 #endif
   1120 }
   1121 
   1122 /*ARGSUSED*/
   1123 static void
   1124 db_mbuf_print_cmd(db_expr_t addr, bool have_addr,
   1125     db_expr_t count, const char *modif)
   1126 {
   1127 
   1128 #ifdef _KERNEL /* XXX CRASH(8) */
   1129 	m_print((const struct mbuf *)(uintptr_t) addr, modif, db_printf);
   1130 #endif
   1131 }
   1132 
   1133 /*ARGSUSED*/
   1134 static void
   1135 db_pool_print_cmd(db_expr_t addr, bool have_addr,
   1136     db_expr_t count, const char *modif)
   1137 {
   1138 
   1139 #ifdef _KERNEL /* XXX CRASH(8) */
   1140 	pool_printit((struct pool *)(uintptr_t) addr, modif, db_printf);
   1141 #endif
   1142 }
   1143 
   1144 /*ARGSUSED*/
   1145 static void
   1146 db_namecache_print_cmd(db_expr_t addr, bool have_addr,
   1147     db_expr_t count, const char *modif)
   1148 {
   1149 
   1150 #ifdef _KERNEL /* XXX CRASH(8) */
   1151 	namecache_print((struct vnode *)(uintptr_t) addr, db_printf);
   1152 #endif
   1153 }
   1154 
   1155 /*ARGSUSED*/
   1156 static void
   1157 db_uvmexp_print_cmd(db_expr_t addr, bool have_addr,
   1158     db_expr_t count, const char *modif)
   1159 {
   1160 
   1161 #ifdef _KERNEL	/* XXX CRASH(8) */
   1162 	uvmexp_print(db_printf);
   1163 #endif
   1164 }
   1165 
   1166 #ifdef UVMHIST
   1167 /*ARGSUSED*/
   1168 static void
   1169 db_uvmhist_print_cmd(db_expr_t addr, bool have_addr,
   1170     db_expr_t count, const char *modif)
   1171 {
   1172 
   1173 	uvmhist_print(db_printf);
   1174 }
   1175 #endif
   1176 
   1177 /*ARGSUSED*/
   1178 static void
   1179 db_lock_print_cmd(db_expr_t addr, bool have_addr,
   1180     db_expr_t count, const char *modif)
   1181 {
   1182 
   1183 #ifdef _KERNEL	/* XXX CRASH(8) */
   1184 	lockdebug_lock_print((void *)(uintptr_t)addr, db_printf);
   1185 #endif
   1186 }
   1187 
   1188 /*
   1189  * Call random function:
   1190  * !expr(arg,arg,arg)
   1191  */
   1192 /*ARGSUSED*/
   1193 static void
   1194 db_fncall(db_expr_t addr, bool have_addr,
   1195     db_expr_t count, const char *modif)
   1196 {
   1197 #ifdef _KERNEL
   1198 	db_expr_t	fn_addr;
   1199 #define	MAXARGS		11
   1200 	db_expr_t	args[MAXARGS];
   1201 	int		nargs = 0;
   1202 	db_expr_t	retval;
   1203 	db_expr_t	(*func)(db_expr_t, ...);
   1204 	int		t;
   1205 
   1206 	if (!db_expression(&fn_addr)) {
   1207 		db_printf("Bad function\n");
   1208 		db_flush_lex();
   1209 		return;
   1210 	}
   1211 	func = (db_expr_t (*)(db_expr_t, ...))(uintptr_t) fn_addr;
   1212 
   1213 	t = db_read_token();
   1214 	if (t == tLPAREN) {
   1215 		if (db_expression(&args[0])) {
   1216 			nargs++;
   1217 			while ((t = db_read_token()) == tCOMMA) {
   1218 				if (nargs == MAXARGS) {
   1219 					db_printf("Too many arguments\n");
   1220 					db_flush_lex();
   1221 					return;
   1222 				}
   1223 				if (!db_expression(&args[nargs])) {
   1224 					db_printf("Argument missing\n");
   1225 					db_flush_lex();
   1226 					return;
   1227 				}
   1228 				nargs++;
   1229 			}
   1230 			db_unread_token(t);
   1231 		}
   1232 		if (db_read_token() != tRPAREN) {
   1233 			db_printf("?\n");
   1234 			db_flush_lex();
   1235 			return;
   1236 		}
   1237 	}
   1238 	db_skip_to_eol();
   1239 
   1240 	while (nargs < MAXARGS) {
   1241 		args[nargs++] = 0;
   1242 	}
   1243 
   1244 	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
   1245 			 args[5], args[6], args[7], args[8], args[9]);
   1246 	db_printf("%s\n", db_num_to_str(retval));
   1247 #else	/* _KERNEL */
   1248 	db_printf("This command can only be used in-kernel.\n");
   1249 #endif	/* _KERNEL */
   1250 }
   1251 
   1252 static void
   1253 db_reboot_cmd(db_expr_t addr, bool have_addr,
   1254     db_expr_t count, const char *modif)
   1255 {
   1256 #ifdef _KERNEL
   1257 	db_expr_t bootflags;
   1258 
   1259 	/* Flags, default to RB_AUTOBOOT */
   1260 	if (!db_expression(&bootflags))
   1261 		bootflags = (db_expr_t)RB_AUTOBOOT;
   1262 	if (db_read_token() != tEOL) {
   1263 		db_error("?\n");
   1264 		/*NOTREACHED*/
   1265 	}
   1266 	/*
   1267 	 * We are leaving DDB, never to return upward.
   1268 	 * Clear db_recover so that we can debug faults in functions
   1269 	 * called from cpu_reboot.
   1270 	 */
   1271 	db_recover = 0;
   1272 	cpu_reboot((int)bootflags, NULL);
   1273 #else	/* _KERNEL */
   1274 	db_printf("This command can only be used in-kernel.\n");
   1275 #endif	/* _KERNEL */
   1276 }
   1277 
   1278 static void
   1279 db_sifting_cmd(db_expr_t addr, bool have_addr,
   1280     db_expr_t count, const char *modif)
   1281 {
   1282 	int	mode, t;
   1283 
   1284 	t = db_read_token();
   1285 	if (t == tSLASH) {
   1286 		t = db_read_token();
   1287 		if (t != tIDENT) {
   1288 			bad_modifier:
   1289 			db_printf("Bad modifier\n");
   1290 			db_flush_lex();
   1291 			return;
   1292 		}
   1293 		if (!strcmp(db_tok_string, "F"))
   1294 			mode = 'F';
   1295 		else
   1296 			goto bad_modifier;
   1297 		t = db_read_token();
   1298 	} else
   1299 		mode = 0;
   1300 
   1301 	if (t == tIDENT)
   1302 		db_sifting(db_tok_string, mode);
   1303 	else {
   1304 		db_printf("Bad argument (non-string)\n");
   1305 		db_flush_lex();
   1306 	}
   1307 }
   1308 
   1309 static void
   1310 db_stack_trace_cmd(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
   1311 {
   1312 	register const char *cp = modif;
   1313 	register char c;
   1314 	void (*pr)(const char *, ...);
   1315 
   1316 	pr = db_printf;
   1317 	while ((c = *cp++) != 0)
   1318 		if (c == 'l')
   1319 			pr = (void (*)(const char *, ...))printf;
   1320 
   1321 	if (count == -1)
   1322 		count = 65535;
   1323 
   1324 	db_stack_trace_print(addr, have_addr, count, modif, pr);
   1325 }
   1326 
   1327 static void
   1328 db_sync_cmd(db_expr_t addr, bool have_addr,
   1329     db_expr_t count, const char *modif)
   1330 {
   1331 #ifdef _KERNEL
   1332 	/*
   1333 	 * We are leaving DDB, never to return upward.
   1334 	 * Clear db_recover so that we can debug faults in functions
   1335 	 * called from cpu_reboot.
   1336 	 */
   1337 	db_recover = 0;
   1338 	panicstr = "dump forced via kernel debugger";
   1339 	cpu_reboot(RB_DUMP, NULL);
   1340 #else	/* _KERNEL */
   1341 	db_printf("This command can only be used in-kernel.\n");
   1342 #endif	/* _KERNEL */
   1343 }
   1344 
   1345 /*
   1346  * Describe what an address is
   1347  */
   1348 void
   1349 db_whatis_cmd(db_expr_t address, bool have_addr,
   1350     db_expr_t count, const char *modif)
   1351 {
   1352 	const uintptr_t addr = (uintptr_t)address;
   1353 
   1354 	db_lwp_whatis(addr, db_printf);
   1355 #ifdef _KERNEL	/* XXX CRASH(8) */
   1356 	pool_whatis(addr, db_printf);
   1357 	vmem_whatis(addr, db_printf);
   1358 	uvm_whatis(addr, db_printf);
   1359 	module_whatis(addr, db_printf);
   1360 #endif
   1361 }
   1362