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