Home | History | Annotate | Line # | Download | only in ddb
db_command.c revision 1.89
      1  1.89       uwe /*	$NetBSD: db_command.c,v 1.89 2006/09/05 21:56:44 uwe Exp $	*/
      2  1.12       cgd 
      3  1.64    simonb /*
      4   1.1       cgd  * Mach Operating System
      5   1.1       cgd  * Copyright (c) 1991,1990 Carnegie Mellon University
      6   1.1       cgd  * All Rights Reserved.
      7  1.64    simonb  *
      8   1.1       cgd  * Permission to use, copy, modify and distribute this software and its
      9   1.1       cgd  * documentation is hereby granted, provided that both the copyright
     10   1.1       cgd  * notice and this permission notice appear in all copies of the
     11   1.1       cgd  * software, derivative works or modified versions, and any portions
     12   1.1       cgd  * thereof, and that both notices appear in supporting documentation.
     13  1.64    simonb  *
     14  1.30        pk  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15   1.1       cgd  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     16   1.1       cgd  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  1.64    simonb  *
     18   1.1       cgd  * Carnegie Mellon requests users of this software to return to
     19  1.64    simonb  *
     20   1.1       cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21   1.1       cgd  *  School of Computer Science
     22   1.1       cgd  *  Carnegie Mellon University
     23   1.1       cgd  *  Pittsburgh PA 15213-3890
     24  1.64    simonb  *
     25   1.1       cgd  * any improvements or extensions that they make and grant Carnegie the
     26   1.1       cgd  * rights to redistribute these changes.
     27   1.1       cgd  */
     28  1.25       mrg 
     29  1.63     lukem /*
     30  1.63     lukem  * Command dispatcher.
     31  1.63     lukem  */
     32  1.63     lukem 
     33  1.63     lukem #include <sys/cdefs.h>
     34  1.89       uwe __KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.89 2006/09/05 21:56:44 uwe Exp $");
     35  1.63     lukem 
     36  1.27      tron #include "opt_ddb.h"
     37  1.69    briggs #include "opt_kgdb.h"
     38  1.40     jhawk #include "opt_inet.h"
     39  1.80      yamt #include "opt_ddbparam.h"
     40   1.5   mycroft 
     41   1.7   mycroft #include <sys/param.h>
     42  1.18  christos #include <sys/systm.h>
     43  1.23    scottr #include <sys/reboot.h>
     44  1.65    simonb #include <sys/device.h>
     45  1.65    simonb #include <sys/malloc.h>
     46  1.83      yamt #include <sys/mbuf.h>
     47  1.65    simonb #include <sys/namei.h>
     48  1.65    simonb #include <sys/pool.h>
     49   1.7   mycroft #include <sys/proc.h>
     50  1.36       chs #include <sys/vnode.h>
     51  1.15       gwr 
     52   1.1       cgd #include <machine/db_machdep.h>		/* type definitions */
     53   1.1       cgd 
     54  1.58       mrg #if defined(_KERNEL_OPT)
     55  1.34  sommerfe #include "opt_multiprocessor.h"
     56  1.34  sommerfe #endif
     57  1.34  sommerfe #ifdef MULTIPROCESSOR
     58  1.34  sommerfe #include <machine/cpu.h>
     59  1.34  sommerfe #endif
     60  1.34  sommerfe 
     61   1.1       cgd #include <ddb/db_lex.h>
     62   1.1       cgd #include <ddb/db_output.h>
     63  1.10        pk #include <ddb/db_command.h>
     64  1.16  christos #include <ddb/db_break.h>
     65  1.16  christos #include <ddb/db_watch.h>
     66  1.16  christos #include <ddb/db_run.h>
     67  1.16  christos #include <ddb/db_variables.h>
     68  1.16  christos #include <ddb/db_interface.h>
     69  1.16  christos #include <ddb/db_sym.h>
     70  1.16  christos #include <ddb/db_extern.h>
     71   1.1       cgd 
     72  1.24       mrg #include <uvm/uvm_extern.h>
     73  1.26  jonathan #include <uvm/uvm_ddb.h>
     74  1.24       mrg 
     75  1.46     jhawk #include "arp.h"
     76  1.46     jhawk 
     77   1.1       cgd /*
     78  1.64    simonb  * Results of command search.
     79  1.64    simonb  */
     80  1.64    simonb #define	CMD_UNIQUE	0
     81  1.64    simonb #define	CMD_FOUND	1
     82  1.64    simonb #define	CMD_NONE	2
     83  1.64    simonb #define	CMD_AMBIGUOUS	3
     84  1.64    simonb #define	CMD_HELP	4
     85  1.64    simonb 
     86  1.64    simonb /*
     87   1.1       cgd  * Exported global variables
     88   1.1       cgd  */
     89   1.1       cgd boolean_t	db_cmd_loop_done;
     90  1.17       gwr label_t		*db_recover;
     91  1.64    simonb db_addr_t	db_dot;
     92  1.64    simonb db_addr_t	db_last_addr;
     93  1.64    simonb db_addr_t	db_prev;
     94  1.64    simonb db_addr_t	db_next;
     95   1.1       cgd 
     96   1.1       cgd /*
     97   1.1       cgd  * if 'ed' style: 'dot' is set at start of last item printed,
     98   1.1       cgd  * and '+' points to next line.
     99   1.1       cgd  * Otherwise: 'dot' points to next item, '..' points to last.
    100   1.1       cgd  */
    101  1.64    simonb static boolean_t db_ed_style = TRUE;
    102  1.64    simonb 
    103  1.79  drochner static void	db_buf_print_cmd(db_expr_t, int, db_expr_t, const char *);
    104  1.65    simonb static void	db_cmd_list(const struct db_command *);
    105  1.64    simonb static int	db_cmd_search(const char *, const struct db_command *,
    106  1.64    simonb 		    const struct db_command **);
    107  1.64    simonb static void	db_command(const struct db_command **,
    108  1.64    simonb 		    const struct db_command *);
    109  1.79  drochner static void	db_event_print_cmd(db_expr_t, int, db_expr_t, const char *);
    110  1.79  drochner static void	db_fncall(db_expr_t, int, db_expr_t, const char *);
    111  1.79  drochner static void	db_malloc_print_cmd(db_expr_t, int, db_expr_t, const char *);
    112  1.79  drochner static void	db_map_print_cmd(db_expr_t, int, db_expr_t, const char *);
    113  1.79  drochner static void	db_namecache_print_cmd(db_expr_t, int, db_expr_t, const char *);
    114  1.79  drochner static void	db_object_print_cmd(db_expr_t, int, db_expr_t, const char *);
    115  1.79  drochner static void	db_page_print_cmd(db_expr_t, int, db_expr_t, const char *);
    116  1.84     bjh21 static void	db_show_all_pages(db_expr_t, int, db_expr_t, const char *);
    117  1.79  drochner static void	db_pool_print_cmd(db_expr_t, int, db_expr_t, const char *);
    118  1.79  drochner static void	db_reboot_cmd(db_expr_t, int, db_expr_t, const char *);
    119  1.79  drochner static void	db_sifting_cmd(db_expr_t, int, db_expr_t, const char *);
    120  1.79  drochner static void	db_stack_trace_cmd(db_expr_t, int, db_expr_t, const char *);
    121  1.79  drochner static void	db_sync_cmd(db_expr_t, int, db_expr_t, const char *);
    122  1.79  drochner static void	db_uvmexp_print_cmd(db_expr_t, int, db_expr_t, const char *);
    123  1.79  drochner static void	db_vnode_print_cmd(db_expr_t, int, db_expr_t, const char *);
    124  1.79  drochner static void	db_mount_print_cmd(db_expr_t, int, db_expr_t, const char *);
    125  1.83      yamt static void	db_mbuf_print_cmd(db_expr_t, int, db_expr_t, const char *);
    126  1.64    simonb 
    127  1.64    simonb /*
    128  1.64    simonb  * 'show' commands
    129  1.64    simonb  */
    130  1.64    simonb 
    131  1.64    simonb static const struct db_command db_show_all_cmds[] = {
    132  1.64    simonb 	{ "callout",	db_show_callout,	0, NULL },
    133  1.84     bjh21 	{ "pages",	db_show_all_pages,	0, NULL },
    134  1.64    simonb 	{ "procs",	db_show_all_procs,	0, NULL },
    135  1.82      yamt 	{ "pools",	db_show_all_pools,	0, NULL },
    136  1.64    simonb 	{ NULL, 	NULL, 			0, NULL }
    137  1.64    simonb };
    138  1.64    simonb 
    139  1.64    simonb static const struct db_command db_show_cmds[] = {
    140  1.64    simonb 	{ "all",	NULL,			0,	db_show_all_cmds },
    141  1.64    simonb #if defined(INET) && (NARP > 0)
    142  1.64    simonb 	{ "arptab",	db_show_arptab,		0,	NULL },
    143  1.64    simonb #endif
    144  1.64    simonb 	{ "breaks",	db_listbreak_cmd, 	0,	NULL },
    145  1.64    simonb 	{ "buf",	db_buf_print_cmd,	0,	NULL },
    146  1.65    simonb 	{ "event",	db_event_print_cmd,	0,	NULL },
    147  1.65    simonb 	{ "malloc",	db_malloc_print_cmd,	0,	NULL },
    148  1.64    simonb 	{ "map",	db_map_print_cmd,	0,	NULL },
    149  1.74       dbj 	{ "mount",	db_mount_print_cmd,	0,	NULL },
    150  1.83      yamt 	{ "mbuf",	db_mbuf_print_cmd,	0,	NULL },
    151  1.64    simonb 	{ "ncache",	db_namecache_print_cmd,	0,	NULL },
    152  1.64    simonb 	{ "object",	db_object_print_cmd,	0,	NULL },
    153  1.64    simonb 	{ "page",	db_page_print_cmd,	0,	NULL },
    154  1.64    simonb 	{ "pool",	db_pool_print_cmd,	0,	NULL },
    155  1.64    simonb 	{ "registers",	db_show_regs,		0,	NULL },
    156  1.72   thorpej 	{ "sched_qs",	db_show_sched_qs,	0,	NULL },
    157  1.64    simonb 	{ "uvmexp",	db_uvmexp_print_cmd,	0,	NULL },
    158  1.64    simonb 	{ "vnode",	db_vnode_print_cmd,	0,	NULL },
    159  1.64    simonb 	{ "watches",	db_listwatch_cmd, 	0,	NULL },
    160  1.64    simonb 	{ NULL,		NULL,			0,	NULL }
    161  1.64    simonb };
    162  1.64    simonb 
    163  1.77      yamt /* arch/<arch>/<arch>/db_interface.c */
    164  1.77      yamt #ifdef DB_MACHINE_COMMANDS
    165  1.77      yamt extern const struct db_command db_machine_command_table[];
    166  1.77      yamt #endif
    167  1.77      yamt 
    168  1.64    simonb static const struct db_command db_command_table[] = {
    169  1.73       chs 	{ "b",		db_breakpoint_cmd,	0,		NULL },
    170  1.64    simonb 	{ "break",	db_breakpoint_cmd,	0,		NULL },
    171  1.68       jmc 	{ "bt",		db_stack_trace_cmd,	0,		NULL },
    172  1.64    simonb 	{ "c",		db_continue_cmd,	0,		NULL },
    173  1.64    simonb 	{ "call",	db_fncall,		CS_OWN,		NULL },
    174  1.64    simonb 	{ "callout",	db_show_callout,	0,		NULL },
    175  1.64    simonb 	{ "continue",	db_continue_cmd,	0,		NULL },
    176  1.64    simonb 	{ "d",		db_delete_cmd,		0,		NULL },
    177  1.64    simonb 	{ "delete",	db_delete_cmd,		0,		NULL },
    178  1.64    simonb 	{ "dmesg",	db_dmesg,		0,		NULL },
    179  1.64    simonb 	{ "dwatch",	db_deletewatch_cmd,	0,		NULL },
    180  1.64    simonb 	{ "examine",	db_examine_cmd,		CS_SET_DOT, 	NULL },
    181  1.64    simonb 	{ "kill",	db_kill_proc,		CS_OWN,		NULL },
    182  1.69    briggs #ifdef KGDB
    183  1.69    briggs 	{ "kgdb",	db_kgdb_cmd,		0,		NULL },
    184  1.69    briggs #endif
    185  1.64    simonb #ifdef DB_MACHINE_COMMANDS
    186  1.64    simonb 	{ "machine",	NULL,			0, db_machine_command_table },
    187  1.64    simonb #endif
    188  1.64    simonb 	{ "match",	db_trace_until_matching_cmd,0,		NULL },
    189  1.64    simonb 	{ "next",	db_trace_until_matching_cmd,0,		NULL },
    190  1.64    simonb 	{ "p",		db_print_cmd,		0,		NULL },
    191  1.64    simonb 	{ "print",	db_print_cmd,		0,		NULL },
    192  1.64    simonb 	{ "ps",		db_show_all_procs,	0,		NULL },
    193  1.64    simonb 	{ "reboot",	db_reboot_cmd,		CS_OWN,		NULL },
    194  1.64    simonb 	{ "s",		db_single_step_cmd,	0,		NULL },
    195  1.64    simonb 	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, NULL },
    196  1.64    simonb 	{ "set",	db_set_cmd,		CS_OWN,		NULL },
    197  1.64    simonb 	{ "show",	NULL,			0,		db_show_cmds },
    198  1.64    simonb 	{ "sifting",	db_sifting_cmd,		CS_OWN,		NULL },
    199  1.64    simonb 	{ "step",	db_single_step_cmd,	0,		NULL },
    200  1.64    simonb 	{ "sync",	db_sync_cmd,		CS_OWN,		NULL },
    201  1.64    simonb 	{ "trace",	db_stack_trace_cmd,	0,		NULL },
    202  1.64    simonb 	{ "until",	db_trace_until_call_cmd,0,		NULL },
    203  1.64    simonb 	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
    204  1.64    simonb 	{ "watch",	db_watchpoint_cmd,	CS_MORE,	NULL },
    205  1.64    simonb 	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
    206  1.64    simonb 	{ "x",		db_examine_cmd,		CS_SET_DOT, 	NULL },
    207  1.64    simonb 	{ NULL, 	NULL,			0,		NULL }
    208  1.64    simonb };
    209  1.64    simonb 
    210  1.64    simonb static const struct db_command	*db_last_command = NULL;
    211  1.80      yamt #if defined(DDB_COMMANDONENTER)
    212  1.81      yamt char db_cmd_on_enter[DB_LINE_MAXLEN + 1] = ___STRING(DDB_COMMANDONENTER);
    213  1.81      yamt #else /* defined(DDB_COMMANDONENTER) */
    214  1.81      yamt char db_cmd_on_enter[DB_LINE_MAXLEN + 1] = "";
    215  1.80      yamt #endif /* defined(DDB_COMMANDONENTER) */
    216  1.80      yamt #define	DB_LINE_SEP	';'
    217   1.1       cgd 
    218   1.1       cgd /*
    219   1.1       cgd  * Utility routine - discard tokens through end-of-line.
    220   1.1       cgd  */
    221   1.1       cgd void
    222  1.64    simonb db_skip_to_eol(void)
    223   1.1       cgd {
    224  1.64    simonb 	int t;
    225  1.64    simonb 
    226   1.1       cgd 	do {
    227  1.64    simonb 		t = db_read_token();
    228   1.1       cgd 	} while (t != tEOL);
    229   1.1       cgd }
    230   1.1       cgd 
    231  1.64    simonb void
    232  1.89       uwe db_error(const char *s)
    233  1.64    simonb {
    234  1.64    simonb 
    235  1.64    simonb 	if (s)
    236  1.64    simonb 		db_printf("%s", s);
    237  1.64    simonb 	db_flush_lex();
    238  1.64    simonb 	longjmp(db_recover);
    239  1.64    simonb }
    240  1.64    simonb 
    241  1.81      yamt static void
    242  1.81      yamt db_execute_commandlist(const char *cmdlist)
    243  1.81      yamt {
    244  1.81      yamt 	const char *cmd = cmdlist;
    245  1.81      yamt 	const struct db_command	*dummy = NULL;
    246  1.81      yamt 
    247  1.81      yamt 	while (*cmd != '\0') {
    248  1.81      yamt 		const char *ep = cmd;
    249  1.81      yamt 
    250  1.81      yamt 		while (*ep != '\0' && *ep != DB_LINE_SEP) {
    251  1.81      yamt 			ep++;
    252  1.81      yamt 		}
    253  1.81      yamt 		db_set_line(cmd, ep);
    254  1.81      yamt 		db_command(&dummy, db_command_table);
    255  1.81      yamt 		cmd = ep;
    256  1.81      yamt 		if (*cmd == DB_LINE_SEP) {
    257  1.81      yamt 			cmd++;
    258  1.81      yamt 		}
    259  1.81      yamt 	}
    260  1.81      yamt }
    261  1.81      yamt 
    262  1.64    simonb void
    263  1.64    simonb db_command_loop(void)
    264  1.64    simonb {
    265  1.64    simonb 	label_t	db_jmpbuf;
    266  1.64    simonb 	label_t	*savejmp;
    267  1.64    simonb 
    268  1.64    simonb 	/*
    269  1.64    simonb 	 * Initialize 'prev' and 'next' to dot.
    270  1.64    simonb 	 */
    271  1.64    simonb 	db_prev = db_dot;
    272  1.64    simonb 	db_next = db_dot;
    273  1.64    simonb 
    274  1.64    simonb 	db_cmd_loop_done = 0;
    275  1.64    simonb 
    276  1.64    simonb 	savejmp = db_recover;
    277  1.64    simonb 	db_recover = &db_jmpbuf;
    278  1.64    simonb 	(void) setjmp(&db_jmpbuf);
    279  1.64    simonb 
    280  1.81      yamt 	db_execute_commandlist(db_cmd_on_enter);
    281  1.80      yamt 
    282  1.64    simonb 	while (!db_cmd_loop_done) {
    283  1.64    simonb 		if (db_print_position() != 0)
    284  1.64    simonb 			db_printf("\n");
    285  1.64    simonb 		db_output_line = 0;
    286  1.64    simonb 
    287  1.64    simonb 
    288  1.64    simonb #ifdef MULTIPROCESSOR
    289  1.64    simonb 		db_printf("db{%ld}> ", (long)cpu_number());
    290  1.64    simonb #else
    291  1.64    simonb 		db_printf("db> ");
    292  1.64    simonb #endif
    293  1.64    simonb 		(void) db_read_line();
    294  1.64    simonb 
    295  1.64    simonb 		db_command(&db_last_command, db_command_table);
    296  1.64    simonb 	}
    297  1.64    simonb 
    298  1.64    simonb 	db_recover = savejmp;
    299  1.64    simonb }
    300   1.1       cgd 
    301   1.1       cgd /*
    302   1.1       cgd  * Search for command prefix.
    303   1.1       cgd  */
    304  1.64    simonb static int
    305  1.64    simonb db_cmd_search(const char *name, const struct db_command *table,
    306  1.64    simonb     const struct db_command **cmdp)
    307   1.1       cgd {
    308  1.53  jdolecek 	const struct db_command	*cmd;
    309  1.10        pk 	int			result = CMD_NONE;
    310   1.1       cgd 
    311   1.1       cgd 	for (cmd = table; cmd->name != 0; cmd++) {
    312  1.64    simonb 		const char *lp;
    313  1.64    simonb 		const char *rp;
    314  1.64    simonb 		int  c;
    315  1.64    simonb 
    316  1.64    simonb 		lp = name;
    317  1.64    simonb 		rp = cmd->name;
    318  1.64    simonb 		while ((c = *lp) == *rp) {
    319  1.64    simonb 			if (c == 0) {
    320  1.64    simonb 				/* complete match */
    321  1.64    simonb 				*cmdp = cmd;
    322  1.64    simonb 				return (CMD_UNIQUE);
    323  1.64    simonb 			}
    324  1.64    simonb 			lp++;
    325  1.64    simonb 			rp++;
    326  1.64    simonb 		}
    327   1.1       cgd 		if (c == 0) {
    328  1.64    simonb 			/* end of name, not end of command -
    329  1.64    simonb 			   partial match */
    330  1.64    simonb 			if (result == CMD_FOUND) {
    331  1.64    simonb 				result = CMD_AMBIGUOUS;
    332  1.64    simonb 				/* but keep looking for a full match -
    333  1.64    simonb 				   this lets us match single letters */
    334  1.64    simonb 			} else {
    335  1.64    simonb 				*cmdp = cmd;
    336  1.64    simonb 				result = CMD_FOUND;
    337  1.64    simonb 			}
    338   1.1       cgd 		}
    339   1.1       cgd 	}
    340   1.1       cgd 	if (result == CMD_NONE) {
    341  1.64    simonb 		/* check for 'help' */
    342   1.1       cgd 		if (name[0] == 'h' && name[1] == 'e'
    343   1.1       cgd 		    && name[2] == 'l' && name[3] == 'p')
    344   1.1       cgd 			result = CMD_HELP;
    345   1.1       cgd 	}
    346   1.1       cgd 	return (result);
    347   1.1       cgd }
    348   1.1       cgd 
    349  1.64    simonb static void
    350  1.64    simonb db_cmd_list(const struct db_command *table)
    351   1.1       cgd {
    352  1.67    simonb 	int	 i, j, w, columns, lines, width=0, numcmds;
    353  1.53  jdolecek 	const char	*p;
    354   1.1       cgd 
    355  1.32     lukem 	for (numcmds = 0; table[numcmds].name != NULL; numcmds++) {
    356  1.32     lukem 		w = strlen(table[numcmds].name);
    357  1.32     lukem 		if (w > width)
    358  1.32     lukem 			width = w;
    359  1.32     lukem 	}
    360  1.32     lukem 	width = DB_NEXT_TAB(width);
    361  1.32     lukem 
    362  1.32     lukem 	columns = db_max_width / width;
    363  1.32     lukem 	if (columns == 0)
    364  1.32     lukem 		columns = 1;
    365  1.32     lukem 	lines = (numcmds + columns - 1) / columns;
    366  1.32     lukem 	for (i = 0; i < lines; i++) {
    367  1.32     lukem 		for (j = 0; j < columns; j++) {
    368  1.32     lukem 			p = table[j * lines + i].name;
    369  1.32     lukem 			if (p)
    370  1.32     lukem 				db_printf("%s", p);
    371  1.32     lukem 			if (j * lines + i + lines >= numcmds) {
    372  1.32     lukem 				db_putchar('\n');
    373  1.32     lukem 				break;
    374  1.32     lukem 			}
    375  1.85  christos 			if (p) {
    376  1.85  christos 				w = strlen(p);
    377  1.85  christos 				while (w < width) {
    378  1.85  christos 					w = DB_NEXT_TAB(w);
    379  1.85  christos 					db_putchar('\t');
    380  1.85  christos 				}
    381  1.32     lukem 			}
    382  1.32     lukem 		}
    383   1.1       cgd 	}
    384   1.1       cgd }
    385   1.1       cgd 
    386  1.64    simonb static void
    387  1.64    simonb db_command(const struct db_command **last_cmdp,
    388  1.64    simonb     const struct db_command *cmd_table)
    389   1.1       cgd {
    390  1.53  jdolecek 	const struct db_command	*cmd;
    391   1.1       cgd 	int		t;
    392   1.1       cgd 	char		modif[TOK_STRING_SIZE];
    393   1.1       cgd 	db_expr_t	addr, count;
    394  1.16  christos 	boolean_t	have_addr = FALSE;
    395   1.1       cgd 	int		result;
    396  1.87        he 	static db_expr_t last_count = 0;
    397   1.1       cgd 
    398  1.86       mrg 	cmd = NULL;	/* XXX gcc */
    399  1.86       mrg 
    400   1.1       cgd 	t = db_read_token();
    401  1.38     jhawk 	if ((t == tEOL) || (t == tCOMMA)) {
    402   1.1       cgd 		/*
    403  1.64    simonb 		 * An empty line repeats last command, at 'next'.
    404  1.64    simonb 		 * Only a count repeats the last command with the new count.
    405  1.64    simonb 		 */
    406  1.64    simonb 		cmd = *last_cmdp;
    407  1.64    simonb 		addr = (db_expr_t)db_next;
    408  1.64    simonb 		if (t == tCOMMA) {
    409  1.64    simonb 			if (!db_expression(&count)) {
    410  1.64    simonb 				db_printf("Count missing\n");
    411  1.64    simonb 				db_flush_lex();
    412  1.64    simonb 				return;
    413  1.64    simonb 			}
    414  1.64    simonb 		} else
    415  1.64    simonb 			count = last_count;
    416  1.64    simonb 		have_addr = FALSE;
    417  1.64    simonb 		modif[0] = '\0';
    418  1.64    simonb 		db_skip_to_eol();
    419  1.64    simonb 	} else if (t == tEXCL) {
    420  1.64    simonb 		db_fncall(0, 0, 0, NULL);
    421  1.64    simonb 		return;
    422  1.64    simonb 	} else if (t != tIDENT) {
    423  1.64    simonb 		db_printf("?\n");
    424  1.64    simonb 		db_flush_lex();
    425  1.64    simonb 		return;
    426  1.64    simonb 	} else {
    427  1.64    simonb 		/*
    428  1.64    simonb 		 * Search for command
    429   1.1       cgd 		 */
    430  1.64    simonb 		while (cmd_table) {
    431  1.64    simonb 			result = db_cmd_search(db_tok_string, cmd_table, &cmd);
    432  1.64    simonb 			switch (result) {
    433  1.64    simonb 			case CMD_NONE:
    434  1.64    simonb 				db_printf("No such command\n");
    435  1.64    simonb 				db_flush_lex();
    436  1.64    simonb 				return;
    437  1.64    simonb 			case CMD_AMBIGUOUS:
    438  1.64    simonb 				db_printf("Ambiguous\n");
    439  1.64    simonb 				db_flush_lex();
    440  1.64    simonb 				return;
    441  1.64    simonb 			case CMD_HELP:
    442  1.64    simonb 				db_cmd_list(cmd_table);
    443  1.64    simonb 				db_flush_lex();
    444  1.64    simonb 				return;
    445  1.64    simonb 			default:
    446  1.64    simonb 				break;
    447  1.64    simonb 			}
    448  1.64    simonb 			if ((cmd_table = cmd->more) != 0) {
    449  1.64    simonb 				t = db_read_token();
    450  1.64    simonb 				if (t != tIDENT) {
    451  1.64    simonb 					db_cmd_list(cmd_table);
    452  1.64    simonb 					db_flush_lex();
    453  1.64    simonb 					return;
    454  1.64    simonb 				}
    455  1.64    simonb 			}
    456   1.1       cgd 		}
    457   1.1       cgd 
    458  1.64    simonb 		if ((cmd->flag & CS_OWN) == 0) {
    459  1.64    simonb 			/*
    460  1.64    simonb 			 * Standard syntax:
    461  1.64    simonb 			 * command [/modifier] [addr] [,count]
    462  1.64    simonb 			 */
    463  1.64    simonb 			t = db_read_token();
    464  1.64    simonb 			if (t == tSLASH) {
    465  1.64    simonb 				t = db_read_token();
    466  1.64    simonb 				if (t != tIDENT) {
    467  1.64    simonb 					db_printf("Bad modifier\n");
    468  1.64    simonb 					db_flush_lex();
    469  1.64    simonb 					return;
    470  1.64    simonb 				}
    471  1.71    itojun 				strlcpy(modif, db_tok_string, sizeof(modif));
    472  1.64    simonb 			} else {
    473  1.64    simonb 				db_unread_token(t);
    474  1.64    simonb 				modif[0] = '\0';
    475  1.64    simonb 			}
    476  1.64    simonb 
    477  1.64    simonb 			if (db_expression(&addr)) {
    478  1.64    simonb 				db_dot = (db_addr_t) addr;
    479  1.64    simonb 				db_last_addr = db_dot;
    480  1.64    simonb 				have_addr = TRUE;
    481  1.64    simonb 			} else {
    482  1.64    simonb 				addr = (db_expr_t) db_dot;
    483  1.64    simonb 				have_addr = FALSE;
    484  1.64    simonb 			}
    485  1.64    simonb 			t = db_read_token();
    486  1.64    simonb 			if (t == tCOMMA) {
    487  1.64    simonb 				if (!db_expression(&count)) {
    488  1.64    simonb 					db_printf("Count missing\n");
    489  1.64    simonb 					db_flush_lex();
    490  1.64    simonb 					return;
    491  1.64    simonb 				}
    492  1.64    simonb 			} else {
    493  1.64    simonb 				db_unread_token(t);
    494  1.64    simonb 				count = -1;
    495  1.64    simonb 			}
    496  1.64    simonb 			if ((cmd->flag & CS_MORE) == 0) {
    497  1.64    simonb 				db_skip_to_eol();
    498  1.64    simonb 			}
    499   1.1       cgd 		}
    500   1.1       cgd 	}
    501   1.1       cgd 	*last_cmdp = cmd;
    502  1.38     jhawk 	last_count = count;
    503   1.1       cgd 	if (cmd != 0) {
    504   1.1       cgd 		/*
    505  1.64    simonb 		 * Execute the command.
    506   1.1       cgd 		 */
    507  1.64    simonb 		(*cmd->fcn)(addr, have_addr, count, modif);
    508  1.64    simonb 
    509  1.64    simonb 		if (cmd->flag & CS_SET_DOT) {
    510  1.64    simonb 			/*
    511  1.64    simonb 			 * If command changes dot, set dot to
    512  1.64    simonb 			 * previous address displayed (if 'ed' style).
    513  1.64    simonb 			 */
    514  1.64    simonb 			if (db_ed_style)
    515  1.64    simonb 				db_dot = db_prev;
    516  1.64    simonb 			else
    517  1.64    simonb 				db_dot = db_next;
    518  1.64    simonb 		} else {
    519  1.64    simonb 			/*
    520  1.64    simonb 			 * If command does not change dot,
    521  1.64    simonb 			 * set 'next' location to be the same.
    522  1.64    simonb 			 */
    523  1.64    simonb 			db_next = db_dot;
    524   1.1       cgd 		}
    525   1.1       cgd 	}
    526   1.1       cgd }
    527   1.1       cgd 
    528   1.4    brezak /*ARGSUSED*/
    529  1.64    simonb static void
    530  1.79  drochner db_map_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    531  1.64    simonb {
    532  1.64    simonb 	boolean_t full = FALSE;
    533  1.64    simonb 
    534  1.64    simonb 	if (modif[0] == 'f')
    535  1.64    simonb 		full = TRUE;
    536  1.60      matt 
    537  1.60      matt 	if (have_addr == FALSE)
    538  1.66       scw 		addr = (db_expr_t)(intptr_t) kernel_map;
    539   1.4    brezak 
    540  1.66       scw 	uvm_map_printit((struct vm_map *)(intptr_t) addr, full, db_printf);
    541   1.4    brezak }
    542   1.4    brezak 
    543   1.4    brezak /*ARGSUSED*/
    544  1.64    simonb static void
    545  1.79  drochner db_malloc_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    546  1.62   thorpej {
    547  1.64    simonb 
    548  1.62   thorpej #ifdef MALLOC_DEBUG
    549  1.62   thorpej 	if (!have_addr)
    550  1.62   thorpej 		addr = 0;
    551  1.62   thorpej 
    552  1.62   thorpej 	debug_malloc_printit(db_printf, (vaddr_t) addr);
    553  1.62   thorpej #else
    554  1.62   thorpej 	db_printf("The kernel is not built with the MALLOC_DEBUG option.\n");
    555  1.62   thorpej #endif /* MALLOC_DEBUG */
    556  1.62   thorpej }
    557  1.62   thorpej 
    558  1.62   thorpej /*ARGSUSED*/
    559  1.64    simonb static void
    560  1.79  drochner db_object_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    561  1.64    simonb {
    562  1.64    simonb 	boolean_t full = FALSE;
    563  1.64    simonb 
    564  1.64    simonb 	if (modif[0] == 'f')
    565  1.64    simonb 		full = TRUE;
    566   1.4    brezak 
    567  1.66       scw 	uvm_object_printit((struct uvm_object *)(intptr_t) addr, full,
    568  1.66       scw 	    db_printf);
    569  1.24       mrg }
    570  1.24       mrg 
    571  1.24       mrg /*ARGSUSED*/
    572  1.64    simonb static void
    573  1.79  drochner db_page_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    574  1.64    simonb {
    575  1.64    simonb 	boolean_t full = FALSE;
    576  1.64    simonb 
    577  1.64    simonb 	if (modif[0] == 'f')
    578  1.64    simonb 		full = TRUE;
    579  1.24       mrg 
    580  1.66       scw 	uvm_page_printit((struct vm_page *)(intptr_t) addr, full, db_printf);
    581  1.36       chs }
    582  1.36       chs 
    583  1.36       chs /*ARGSUSED*/
    584  1.64    simonb static void
    585  1.84     bjh21 db_show_all_pages(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    586  1.84     bjh21 {
    587  1.84     bjh21 
    588  1.84     bjh21 	uvm_page_printall(db_printf);
    589  1.84     bjh21 }
    590  1.84     bjh21 
    591  1.84     bjh21 /*ARGSUSED*/
    592  1.84     bjh21 static void
    593  1.79  drochner db_buf_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    594  1.36       chs {
    595  1.36       chs 	boolean_t full = FALSE;
    596  1.64    simonb 
    597  1.36       chs 	if (modif[0] == 'f')
    598  1.36       chs 		full = TRUE;
    599  1.36       chs 
    600  1.66       scw 	vfs_buf_print((struct buf *)(intptr_t) addr, full, db_printf);
    601  1.65    simonb }
    602  1.65    simonb 
    603  1.65    simonb /*ARGSUSED*/
    604  1.65    simonb static void
    605  1.79  drochner db_event_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    606  1.65    simonb {
    607  1.65    simonb 	boolean_t full = FALSE;
    608  1.65    simonb 
    609  1.65    simonb 	if (modif[0] == 'f')
    610  1.65    simonb 		full = TRUE;
    611  1.65    simonb 
    612  1.65    simonb 	event_print(full, db_printf);
    613  1.36       chs }
    614  1.36       chs 
    615  1.36       chs /*ARGSUSED*/
    616  1.64    simonb static void
    617  1.79  drochner db_vnode_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    618  1.36       chs {
    619  1.36       chs 	boolean_t full = FALSE;
    620  1.64    simonb 
    621  1.36       chs 	if (modif[0] == 'f')
    622  1.36       chs 		full = TRUE;
    623  1.36       chs 
    624  1.66       scw 	vfs_vnode_print((struct vnode *)(intptr_t) addr, full, db_printf);
    625  1.74       dbj }
    626  1.74       dbj 
    627  1.74       dbj static void
    628  1.79  drochner db_mount_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    629  1.74       dbj {
    630  1.74       dbj 	boolean_t full = FALSE;
    631  1.74       dbj 
    632  1.74       dbj 	if (modif[0] == 'f')
    633  1.74       dbj 		full = TRUE;
    634  1.74       dbj 
    635  1.74       dbj 	vfs_mount_print((struct mount *)(intptr_t) addr, full, db_printf);
    636   1.4    brezak }
    637   1.4    brezak 
    638  1.31   thorpej /*ARGSUSED*/
    639  1.64    simonb static void
    640  1.83      yamt db_mbuf_print_cmd(db_expr_t addr, int have_addr, db_expr_t count,
    641  1.83      yamt     const char *modif)
    642  1.83      yamt {
    643  1.83      yamt 
    644  1.83      yamt 	m_print((const struct mbuf *)(intptr_t) addr, modif, db_printf);
    645  1.83      yamt }
    646  1.83      yamt 
    647  1.83      yamt /*ARGSUSED*/
    648  1.83      yamt static void
    649  1.79  drochner db_pool_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    650  1.31   thorpej {
    651  1.64    simonb 
    652  1.66       scw 	pool_printit((struct pool *)(intptr_t) addr, modif, db_printf);
    653  1.51       chs }
    654  1.31   thorpej 
    655  1.51       chs /*ARGSUSED*/
    656  1.64    simonb static void
    657  1.64    simonb db_namecache_print_cmd(db_expr_t addr, int have_addr, db_expr_t count,
    658  1.79  drochner     const char *modif)
    659  1.51       chs {
    660  1.64    simonb 
    661  1.66       scw 	namecache_print((struct vnode *)(intptr_t) addr, db_printf);
    662  1.51       chs }
    663  1.51       chs 
    664  1.51       chs /*ARGSUSED*/
    665  1.64    simonb static void
    666  1.79  drochner db_uvmexp_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    667  1.51       chs {
    668  1.64    simonb 
    669  1.51       chs 	uvmexp_print(db_printf);
    670  1.31   thorpej }
    671  1.31   thorpej 
    672   1.1       cgd /*
    673   1.1       cgd  * Call random function:
    674   1.1       cgd  * !expr(arg,arg,arg)
    675   1.1       cgd  */
    676  1.16  christos /*ARGSUSED*/
    677  1.64    simonb static void
    678  1.79  drochner db_fncall(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    679   1.1       cgd {
    680   1.1       cgd 	db_expr_t	fn_addr;
    681   1.1       cgd #define	MAXARGS		11
    682   1.1       cgd 	db_expr_t	args[MAXARGS];
    683   1.1       cgd 	int		nargs = 0;
    684   1.1       cgd 	db_expr_t	retval;
    685  1.64    simonb 	db_expr_t	(*func)(db_expr_t, ...);
    686   1.1       cgd 	int		t;
    687   1.1       cgd 
    688   1.1       cgd 	if (!db_expression(&fn_addr)) {
    689  1.64    simonb 		db_printf("Bad function\n");
    690  1.64    simonb 		db_flush_lex();
    691  1.64    simonb 		return;
    692   1.1       cgd 	}
    693  1.66       scw 	func = (db_expr_t (*)(db_expr_t, ...))(intptr_t) fn_addr;
    694   1.1       cgd 
    695   1.1       cgd 	t = db_read_token();
    696   1.1       cgd 	if (t == tLPAREN) {
    697  1.64    simonb 		if (db_expression(&args[0])) {
    698  1.64    simonb 			nargs++;
    699  1.64    simonb 			while ((t = db_read_token()) == tCOMMA) {
    700  1.64    simonb 				if (nargs == MAXARGS) {
    701  1.64    simonb 					db_printf("Too many arguments\n");
    702  1.64    simonb 					db_flush_lex();
    703  1.64    simonb 					return;
    704  1.64    simonb 				}
    705  1.64    simonb 				if (!db_expression(&args[nargs])) {
    706  1.64    simonb 					db_printf("Argument missing\n");
    707  1.64    simonb 					db_flush_lex();
    708  1.64    simonb 					return;
    709  1.64    simonb 				}
    710  1.64    simonb 				nargs++;
    711  1.64    simonb 			}
    712  1.64    simonb 			db_unread_token(t);
    713  1.64    simonb 		}
    714  1.64    simonb 		if (db_read_token() != tRPAREN) {
    715  1.64    simonb 			db_printf("?\n");
    716   1.1       cgd 			db_flush_lex();
    717   1.1       cgd 			return;
    718   1.1       cgd 		}
    719   1.1       cgd 	}
    720   1.1       cgd 	db_skip_to_eol();
    721   1.1       cgd 
    722   1.1       cgd 	while (nargs < MAXARGS) {
    723  1.64    simonb 		args[nargs++] = 0;
    724   1.1       cgd 	}
    725   1.1       cgd 
    726   1.1       cgd 	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
    727  1.20  christos 			 args[5], args[6], args[7], args[8], args[9]);
    728  1.45     jhawk 	db_printf("%s\n", db_num_to_str(retval));
    729  1.23    scottr }
    730  1.23    scottr 
    731  1.64    simonb static void
    732  1.79  drochner db_reboot_cmd(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    733  1.23    scottr {
    734  1.23    scottr 	db_expr_t bootflags;
    735  1.23    scottr 
    736  1.23    scottr 	/* Flags, default to RB_AUTOBOOT */
    737  1.23    scottr 	if (!db_expression(&bootflags))
    738  1.23    scottr 		bootflags = (db_expr_t)RB_AUTOBOOT;
    739  1.23    scottr 	if (db_read_token() != tEOL) {
    740  1.64    simonb 		db_error("?\n");
    741  1.64    simonb 		/*NOTREACHED*/
    742  1.23    scottr 	}
    743  1.47  sommerfe 	/*
    744  1.47  sommerfe 	 * We are leaving DDB, never to return upward.
    745  1.47  sommerfe 	 * Clear db_recover so that we can debug faults in functions
    746  1.47  sommerfe 	 * called from cpu_reboot.
    747  1.47  sommerfe 	 */
    748  1.47  sommerfe 	db_recover = 0;
    749  1.23    scottr 	cpu_reboot((int)bootflags, NULL);
    750  1.41     jhawk }
    751  1.41     jhawk 
    752  1.64    simonb static void
    753  1.79  drochner db_sifting_cmd(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    754  1.41     jhawk {
    755  1.41     jhawk 	int	mode, t;
    756  1.41     jhawk 
    757  1.41     jhawk 	t = db_read_token();
    758  1.41     jhawk 	if (t == tSLASH) {
    759  1.41     jhawk 		t = db_read_token();
    760  1.41     jhawk 		if (t != tIDENT) {
    761  1.41     jhawk 			bad_modifier:
    762  1.41     jhawk 			db_printf("Bad modifier\n");
    763  1.41     jhawk 			db_flush_lex();
    764  1.41     jhawk 			return;
    765  1.41     jhawk 		}
    766  1.41     jhawk 		if (!strcmp(db_tok_string, "F"))
    767  1.41     jhawk 			mode = 'F';
    768  1.41     jhawk 		else
    769  1.41     jhawk 			goto bad_modifier;
    770  1.41     jhawk 		t = db_read_token();
    771  1.41     jhawk 	} else
    772  1.41     jhawk 		mode = 0;
    773  1.41     jhawk 
    774  1.51       chs 	if (t == tIDENT)
    775  1.41     jhawk 		db_sifting(db_tok_string, mode);
    776  1.41     jhawk 	else {
    777  1.41     jhawk 		db_printf("Bad argument (non-string)\n");
    778  1.41     jhawk 		db_flush_lex();
    779  1.41     jhawk 	}
    780  1.43     jhawk }
    781  1.43     jhawk 
    782  1.64    simonb static void
    783  1.79  drochner db_stack_trace_cmd(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    784  1.43     jhawk {
    785  1.79  drochner 	register const char *cp = modif;
    786  1.70    atatat 	register char c;
    787  1.70    atatat 	void (*pr)(const char *, ...);
    788  1.70    atatat 
    789  1.70    atatat 	pr = db_printf;
    790  1.70    atatat 	while ((c = *cp++) != 0)
    791  1.70    atatat 		if (c == 'l')
    792  1.70    atatat 			pr = printf;
    793  1.64    simonb 
    794  1.43     jhawk 	if (count == -1)
    795  1.43     jhawk 		count = 65535;
    796  1.43     jhawk 
    797  1.70    atatat 	db_stack_trace_print(addr, have_addr, count, modif, pr);
    798  1.32     lukem }
    799  1.32     lukem 
    800  1.64    simonb static void
    801  1.79  drochner db_sync_cmd(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    802  1.32     lukem {
    803  1.64    simonb 
    804  1.47  sommerfe 	/*
    805  1.47  sommerfe 	 * We are leaving DDB, never to return upward.
    806  1.47  sommerfe 	 * Clear db_recover so that we can debug faults in functions
    807  1.47  sommerfe 	 * called from cpu_reboot.
    808  1.47  sommerfe 	 */
    809  1.47  sommerfe 	db_recover = 0;
    810  1.32     lukem 	cpu_reboot(RB_DUMP, NULL);
    811   1.1       cgd }
    812