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