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