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