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