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