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