xref: /freebsd/sys/ddb/db_command.c (revision 21d748a9573ee91dc2434ffd2688e4ea25fb5f06)
1dd3cb568SWarner Losh /*-
25b81b6b3SRodney W. Grimes  * Mach Operating System
35b81b6b3SRodney W. Grimes  * Copyright (c) 1991,1990 Carnegie Mellon University
45b81b6b3SRodney W. Grimes  * All Rights Reserved.
55b81b6b3SRodney W. Grimes  *
65b81b6b3SRodney W. Grimes  * Permission to use, copy, modify and distribute this software and its
75b81b6b3SRodney W. Grimes  * documentation is hereby granted, provided that both the copyright
85b81b6b3SRodney W. Grimes  * notice and this permission notice appear in all copies of the
95b81b6b3SRodney W. Grimes  * software, derivative works or modified versions, and any portions
105b81b6b3SRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
115b81b6b3SRodney W. Grimes  *
125b81b6b3SRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
135b81b6b3SRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
145b81b6b3SRodney W. Grimes  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
155b81b6b3SRodney W. Grimes  *
165b81b6b3SRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
175b81b6b3SRodney W. Grimes  *
185b81b6b3SRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
195b81b6b3SRodney W. Grimes  *  School of Computer Science
205b81b6b3SRodney W. Grimes  *  Carnegie Mellon University
215b81b6b3SRodney W. Grimes  *  Pittsburgh PA 15213-3890
225b81b6b3SRodney W. Grimes  *
235b81b6b3SRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
245b81b6b3SRodney W. Grimes  * rights to redistribute these changes.
255b81b6b3SRodney W. Grimes  */
265b81b6b3SRodney W. Grimes /*
275b81b6b3SRodney W. Grimes  *	Author: David B. Golub, Carnegie Mellon University
285b81b6b3SRodney W. Grimes  *	Date:	7/90
295b81b6b3SRodney W. Grimes  */
305b81b6b3SRodney W. Grimes /*
315b81b6b3SRodney W. Grimes  * Command dispatcher.
325b81b6b3SRodney W. Grimes  */
33753960f7SDavid E. O'Brien 
34753960f7SDavid E. O'Brien #include <sys/cdefs.h>
35753960f7SDavid E. O'Brien __FBSDID("$FreeBSD$");
36753960f7SDavid E. O'Brien 
37f540b106SGarrett Wollman #include <sys/param.h>
380ec81012SJohn Polstra #include <sys/linker_set.h>
3919d2c78fSDima Dorfman #include <sys/lock.h>
4037224cd3SMarcel Moolenaar #include <sys/kdb.h>
4119d2c78fSDima Dorfman #include <sys/mutex.h>
4219d2c78fSDima Dorfman #include <sys/proc.h>
43ad146781SPaul Traina #include <sys/reboot.h>
4419d2c78fSDima Dorfman #include <sys/signalvar.h>
45f540b106SGarrett Wollman #include <sys/systm.h>
46ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
47299cceefSMarcel Moolenaar #include <sys/conf.h>
4874cc032bSPoul-Henning Kamp #include <sys/watchdog.h>
4939297ba4SSam Leffler #include <sys/kernel.h>
503000820aSPoul-Henning Kamp 
515ccbc3ccSBruce Evans #include <ddb/ddb.h>
52058284fcSBruce Evans #include <ddb/db_command.h>
535b81b6b3SRodney W. Grimes #include <ddb/db_lex.h>
545b81b6b3SRodney W. Grimes #include <ddb/db_output.h>
555b81b6b3SRodney W. Grimes 
5626502503SMarcel Moolenaar #include <machine/cpu.h>
570b1ae809SJulian Elischer #include <machine/setjmp.h>
585b81b6b3SRodney W. Grimes 
595b81b6b3SRodney W. Grimes /*
605b81b6b3SRodney W. Grimes  * Exported global variables
615b81b6b3SRodney W. Grimes  */
625b81b6b3SRodney W. Grimes boolean_t	db_cmd_loop_done;
635b81b6b3SRodney W. Grimes db_addr_t	db_dot;
645b81b6b3SRodney W. Grimes db_addr_t	db_last_addr;
655b81b6b3SRodney W. Grimes db_addr_t	db_prev;
665b81b6b3SRodney W. Grimes db_addr_t	db_next;
67f41325dbSPeter Wemm 
68299cceefSMarcel Moolenaar static db_cmdfcn_t	db_dump;
69f73a856dSPoul-Henning Kamp static db_cmdfcn_t	db_fncall;
70f3be7cb3SMarcel Moolenaar static db_cmdfcn_t	db_gdb;
71e4b732cfSBruce Evans static db_cmdfcn_t	db_halt;
7219d2c78fSDima Dorfman static db_cmdfcn_t	db_kill;
735370c3b6SPeter Wemm static db_cmdfcn_t	db_reset;
74fd32d93bSMarcel Moolenaar static db_cmdfcn_t	db_stack_trace;
75a7ad956bSRobert Watson static db_cmdfcn_t	db_stack_trace_all;
7674cc032bSPoul-Henning Kamp static db_cmdfcn_t	db_watchdog;
77ad146781SPaul Traina 
78cec9a4bfSDavid E. O'Brien /*
79cec9a4bfSDavid E. O'Brien  * 'show' commands
80cec9a4bfSDavid E. O'Brien  */
81cec9a4bfSDavid E. O'Brien 
82cec9a4bfSDavid E. O'Brien static struct command db_show_all_cmds[] = {
8339297ba4SSam Leffler 	{ "trace",	db_stack_trace_all,	0,	0 },
84cec9a4bfSDavid E. O'Brien };
8539297ba4SSam Leffler struct command_table db_show_all_table =
8639297ba4SSam Leffler     LIST_HEAD_INITIALIZER(db_show_all_table);
87e1e31c0eSJohn Baldwin 
88cec9a4bfSDavid E. O'Brien static struct command db_show_cmds[] = {
89e1e31c0eSJohn Baldwin 	{ "all",	0,			0,	&db_show_all_table },
90cec9a4bfSDavid E. O'Brien 	{ "registers",	db_show_regs,		0,	0 },
91cec9a4bfSDavid E. O'Brien 	{ "breaks",	db_listbreak_cmd, 	0,	0 },
92cec9a4bfSDavid E. O'Brien 	{ "threads",	db_show_threads,	0,	0 },
93cec9a4bfSDavid E. O'Brien };
9439297ba4SSam Leffler struct command_table db_show_table = LIST_HEAD_INITIALIZER(db_show_table);
95cec9a4bfSDavid E. O'Brien 
9639297ba4SSam Leffler static struct command db_cmds[] = {
97cec9a4bfSDavid E. O'Brien 	{ "print",	db_print_cmd,		0,	0 },
98cec9a4bfSDavid E. O'Brien 	{ "p",		db_print_cmd,		0,	0 },
99cec9a4bfSDavid E. O'Brien 	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
100cec9a4bfSDavid E. O'Brien 	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
101cec9a4bfSDavid E. O'Brien 	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
102cec9a4bfSDavid E. O'Brien 	{ "set",	db_set_cmd,		CS_OWN,	0 },
103cec9a4bfSDavid E. O'Brien 	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
104cec9a4bfSDavid E. O'Brien 	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
105cec9a4bfSDavid E. O'Brien 	{ "delete",	db_delete_cmd,		0,	0 },
106cec9a4bfSDavid E. O'Brien 	{ "d",		db_delete_cmd,		0,	0 },
107299cceefSMarcel Moolenaar 	{ "dump",	db_dump,		0,	0 },
108cec9a4bfSDavid E. O'Brien 	{ "break",	db_breakpoint_cmd,	0,	0 },
10975149ab8SBruce Evans 	{ "b",		db_breakpoint_cmd,	0,	0 },
110cec9a4bfSDavid E. O'Brien 	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
111cec9a4bfSDavid E. O'Brien 	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
112cec9a4bfSDavid E. O'Brien 	{ "dhwatch",	db_deletehwatch_cmd,	0,      0 },
113cec9a4bfSDavid E. O'Brien 	{ "hwatch",	db_hwatchpoint_cmd,	0,      0 },
114cec9a4bfSDavid E. O'Brien 	{ "step",	db_single_step_cmd,	0,	0 },
115cec9a4bfSDavid E. O'Brien 	{ "s",		db_single_step_cmd,	0,	0 },
116cec9a4bfSDavid E. O'Brien 	{ "continue",	db_continue_cmd,	0,	0 },
117cec9a4bfSDavid E. O'Brien 	{ "c",		db_continue_cmd,	0,	0 },
118cec9a4bfSDavid E. O'Brien 	{ "until",	db_trace_until_call_cmd,0,	0 },
119cec9a4bfSDavid E. O'Brien 	{ "next",	db_trace_until_matching_cmd,0,	0 },
120cec9a4bfSDavid E. O'Brien 	{ "match",	db_trace_until_matching_cmd,0,	0 },
121cec9a4bfSDavid E. O'Brien 	{ "trace",	db_stack_trace,		CS_OWN,	0 },
12275149ab8SBruce Evans 	{ "t",		db_stack_trace,		CS_OWN,	0 },
12339297ba4SSam Leffler 	/* XXX alias for all trace */
1243924da21SJohn Baldwin 	{ "alltrace",	db_stack_trace_all,	0,	0 },
125cec9a4bfSDavid E. O'Brien 	{ "where",	db_stack_trace,		CS_OWN,	0 },
1263924da21SJohn Baldwin 	{ "bt",		db_stack_trace,		CS_OWN,	0 },
127cec9a4bfSDavid E. O'Brien 	{ "call",	db_fncall,		CS_OWN,	0 },
128e1e31c0eSJohn Baldwin 	{ "show",	0,			0,	&db_show_table },
129cec9a4bfSDavid E. O'Brien 	{ "ps",		db_ps,			0,	0 },
130cec9a4bfSDavid E. O'Brien 	{ "gdb",	db_gdb,			0,	0 },
131e4b732cfSBruce Evans 	{ "halt",	db_halt,		0,	0 },
132e4b732cfSBruce Evans 	{ "reboot",	db_reset,		0,	0 },
133cec9a4bfSDavid E. O'Brien 	{ "reset",	db_reset,		0,	0 },
134cec9a4bfSDavid E. O'Brien 	{ "kill",	db_kill,		CS_OWN,	0 },
1358b927d7bSAttilio Rao 	{ "watchdog",	db_watchdog,		CS_OWN,	0 },
136cec9a4bfSDavid E. O'Brien 	{ "thread",	db_set_thread,		CS_OWN,	0 },
137c9b0cc3bSRobert Watson 	{ "run",	db_run_cmd,		CS_OWN,	0 },
138c9b0cc3bSRobert Watson 	{ "script",	db_script_cmd,		CS_OWN,	0 },
139c9b0cc3bSRobert Watson 	{ "scripts",	db_scripts_cmd,		0,	0 },
140c9b0cc3bSRobert Watson 	{ "unscript",	db_unscript_cmd,	CS_OWN,	0 },
141086fec57SRobert Watson 	{ "capture",	db_capture_cmd,		CS_OWN,	0 },
142618c7db3SRobert Watson 	{ "textdump",	db_textdump_cmd,	CS_OWN, 0 },
1439ab83ecbSKonstantin Belousov 	{ "findstack",	db_findstack_cmd,	0,	0 },
144cec9a4bfSDavid E. O'Brien };
14539297ba4SSam Leffler struct command_table db_cmd_table = LIST_HEAD_INITIALIZER(db_cmd_table);
146e1e31c0eSJohn Baldwin 
147cec9a4bfSDavid E. O'Brien static struct command	*db_last_command = 0;
1486337f4efSBruce Evans 
1495b81b6b3SRodney W. Grimes /*
1505b81b6b3SRodney W. Grimes  * if 'ed' style: 'dot' is set at start of last item printed,
1515b81b6b3SRodney W. Grimes  * and '+' points to next line.
1525b81b6b3SRodney W. Grimes  * Otherwise: 'dot' points to next item, '..' points to last.
1535b81b6b3SRodney W. Grimes  */
154f73a856dSPoul-Henning Kamp static boolean_t	db_ed_style = TRUE;
1555b81b6b3SRodney W. Grimes 
1565b81b6b3SRodney W. Grimes /*
1575b81b6b3SRodney W. Grimes  * Utility routine - discard tokens through end-of-line.
1585b81b6b3SRodney W. Grimes  */
1595b81b6b3SRodney W. Grimes void
1605b81b6b3SRodney W. Grimes db_skip_to_eol()
1615b81b6b3SRodney W. Grimes {
1625b81b6b3SRodney W. Grimes 	int	t;
1635b81b6b3SRodney W. Grimes 	do {
1645b81b6b3SRodney W. Grimes 	    t = db_read_token();
1655b81b6b3SRodney W. Grimes 	} while (t != tEOL);
1665b81b6b3SRodney W. Grimes }
1675b81b6b3SRodney W. Grimes 
1685b81b6b3SRodney W. Grimes /*
1695b81b6b3SRodney W. Grimes  * Results of command search.
1705b81b6b3SRodney W. Grimes  */
1715b81b6b3SRodney W. Grimes #define	CMD_UNIQUE	0
1725b81b6b3SRodney W. Grimes #define	CMD_FOUND	1
1735b81b6b3SRodney W. Grimes #define	CMD_NONE	2
1745b81b6b3SRodney W. Grimes #define	CMD_AMBIGUOUS	3
1755b81b6b3SRodney W. Grimes #define	CMD_HELP	4
1765b81b6b3SRodney W. Grimes 
177e1e31c0eSJohn Baldwin static void	db_cmd_match(char *name, struct command *cmd,
178e1e31c0eSJohn Baldwin 		    struct command **cmdp, int *resultp);
179e1e31c0eSJohn Baldwin static void	db_cmd_list(struct command_table *table);
180e1e31c0eSJohn Baldwin static int	db_cmd_search(char *name, struct command_table *table,
181e1e31c0eSJohn Baldwin 		    struct command **cmdp);
18214e10f99SAlfred Perlstein static void	db_command(struct command **last_cmdp,
183c9b0cc3bSRobert Watson 		    struct command_table *cmd_table, int dopager);
184058284fcSBruce Evans 
1855b81b6b3SRodney W. Grimes /*
18639297ba4SSam Leffler  * Initialize the command lists from the static tables.
18739297ba4SSam Leffler  */
18893d4804bSJohn Baldwin void
18993d4804bSJohn Baldwin db_command_init(void)
19039297ba4SSam Leffler {
19139297ba4SSam Leffler #define	N(a)	(sizeof(a) / sizeof(a[0]))
19239297ba4SSam Leffler 	int i;
19339297ba4SSam Leffler 
19439297ba4SSam Leffler 	for (i = 0; i < N(db_cmds); i++)
19539297ba4SSam Leffler 		db_command_register(&db_cmd_table, &db_cmds[i]);
19639297ba4SSam Leffler 	for (i = 0; i < N(db_show_cmds); i++)
19739297ba4SSam Leffler 		db_command_register(&db_show_table, &db_show_cmds[i]);
19839297ba4SSam Leffler 	for (i = 0; i < N(db_show_all_cmds); i++)
19939297ba4SSam Leffler 		db_command_register(&db_show_all_table, &db_show_all_cmds[i]);
20039297ba4SSam Leffler #undef N
20139297ba4SSam Leffler }
20239297ba4SSam Leffler 
20339297ba4SSam Leffler /*
20439297ba4SSam Leffler  * Register a command.
20539297ba4SSam Leffler  */
20639297ba4SSam Leffler void
20739297ba4SSam Leffler db_command_register(struct command_table *list, struct command *cmd)
20839297ba4SSam Leffler {
20939297ba4SSam Leffler 	struct command *c, *last;
21039297ba4SSam Leffler 
21139297ba4SSam Leffler 	last = NULL;
21239297ba4SSam Leffler 	LIST_FOREACH(c, list, next) {
21339297ba4SSam Leffler 		int n = strcmp(cmd->name, c->name);
21439297ba4SSam Leffler 
21539297ba4SSam Leffler 		/* Check that the command is not already present. */
21639297ba4SSam Leffler 		if (n == 0) {
21739297ba4SSam Leffler 			printf("%s: Warning, the command \"%s\" already exists;"
21839297ba4SSam Leffler 			     " ignoring request\n", __func__, cmd->name);
21939297ba4SSam Leffler 			return;
22039297ba4SSam Leffler 		}
22139297ba4SSam Leffler 		if (n < 0) {
22239297ba4SSam Leffler 			/* NB: keep list sorted lexicographically */
22339297ba4SSam Leffler 			LIST_INSERT_BEFORE(c, cmd, next);
22439297ba4SSam Leffler 			return;
22539297ba4SSam Leffler 		}
22639297ba4SSam Leffler 		last = c;
22739297ba4SSam Leffler 	}
22839297ba4SSam Leffler 	if (last == NULL)
22939297ba4SSam Leffler 		LIST_INSERT_HEAD(list, cmd, next);
23039297ba4SSam Leffler 	else
23139297ba4SSam Leffler 		LIST_INSERT_AFTER(last, cmd, next);
23239297ba4SSam Leffler }
23339297ba4SSam Leffler 
23439297ba4SSam Leffler /*
23539297ba4SSam Leffler  * Remove a command previously registered with db_command_register.
23639297ba4SSam Leffler  */
23739297ba4SSam Leffler void
23839297ba4SSam Leffler db_command_unregister(struct command_table *list, struct command *cmd)
23939297ba4SSam Leffler {
24039297ba4SSam Leffler 	struct command *c;
24139297ba4SSam Leffler 
24239297ba4SSam Leffler 	LIST_FOREACH(c, list, next) {
24339297ba4SSam Leffler 		if (cmd == c) {
24439297ba4SSam Leffler 			LIST_REMOVE(cmd, next);
24539297ba4SSam Leffler 			return;
24639297ba4SSam Leffler 		}
24739297ba4SSam Leffler 	}
24839297ba4SSam Leffler 	/* NB: intentionally quiet */
24939297ba4SSam Leffler }
25039297ba4SSam Leffler 
25139297ba4SSam Leffler /*
252e1e31c0eSJohn Baldwin  * Helper function to match a single command.
2535b81b6b3SRodney W. Grimes  */
254e1e31c0eSJohn Baldwin static void
255e1e31c0eSJohn Baldwin db_cmd_match(name, cmd, cmdp, resultp)
2565b81b6b3SRodney W. Grimes 	char *		name;
2575b81b6b3SRodney W. Grimes 	struct command	*cmd;
258e1e31c0eSJohn Baldwin 	struct command	**cmdp;	/* out */
259e1e31c0eSJohn Baldwin 	int *		resultp;
260e1e31c0eSJohn Baldwin {
261e1e31c0eSJohn Baldwin 	char *lp, *rp;
262e1e31c0eSJohn Baldwin 	int c;
2635b81b6b3SRodney W. Grimes 
2645b81b6b3SRodney W. Grimes 	lp = name;
2655b81b6b3SRodney W. Grimes 	rp = cmd->name;
2665b81b6b3SRodney W. Grimes 	while ((c = *lp) == *rp) {
2675b81b6b3SRodney W. Grimes 		if (c == 0) {
2685b81b6b3SRodney W. Grimes 			/* complete match */
2695b81b6b3SRodney W. Grimes 			*cmdp = cmd;
270e1e31c0eSJohn Baldwin 			*resultp = CMD_UNIQUE;
271e1e31c0eSJohn Baldwin 			return;
2725b81b6b3SRodney W. Grimes 		}
2735b81b6b3SRodney W. Grimes 		lp++;
2745b81b6b3SRodney W. Grimes 		rp++;
2755b81b6b3SRodney W. Grimes 	}
2765b81b6b3SRodney W. Grimes 	if (c == 0) {
2775b81b6b3SRodney W. Grimes 		/* end of name, not end of command -
2785b81b6b3SRodney W. Grimes 		   partial match */
279e1e31c0eSJohn Baldwin 		if (*resultp == CMD_FOUND) {
280e1e31c0eSJohn Baldwin 			*resultp = CMD_AMBIGUOUS;
2815b81b6b3SRodney W. Grimes 			/* but keep looking for a full match -
2825b81b6b3SRodney W. Grimes 			   this lets us match single letters */
283e1e31c0eSJohn Baldwin 		} else {
2845b81b6b3SRodney W. Grimes 			*cmdp = cmd;
285e1e31c0eSJohn Baldwin 			*resultp = CMD_FOUND;
2865b81b6b3SRodney W. Grimes 		}
2875b81b6b3SRodney W. Grimes 	}
2885b81b6b3SRodney W. Grimes }
2896337f4efSBruce Evans 
290e1e31c0eSJohn Baldwin /*
291e1e31c0eSJohn Baldwin  * Search for command prefix.
292e1e31c0eSJohn Baldwin  */
293e1e31c0eSJohn Baldwin static int
294e1e31c0eSJohn Baldwin db_cmd_search(name, table, cmdp)
295e1e31c0eSJohn Baldwin 	char *		name;
296e1e31c0eSJohn Baldwin 	struct command_table *table;
297e1e31c0eSJohn Baldwin 	struct command	**cmdp;	/* out */
298e1e31c0eSJohn Baldwin {
299e1e31c0eSJohn Baldwin 	struct command	*cmd;
300e1e31c0eSJohn Baldwin 	int		result = CMD_NONE;
301e1e31c0eSJohn Baldwin 
30239297ba4SSam Leffler 	LIST_FOREACH(cmd, table, next) {
303e1e31c0eSJohn Baldwin 		db_cmd_match(name,cmd,cmdp,&result);
304e1e31c0eSJohn Baldwin 		if (result == CMD_UNIQUE)
30539297ba4SSam Leffler 			break;
3066337f4efSBruce Evans 	}
30739297ba4SSam Leffler 
3085b81b6b3SRodney W. Grimes 	if (result == CMD_NONE) {
3095b81b6b3SRodney W. Grimes 		/* check for 'help' */
3105b81b6b3SRodney W. Grimes 		if (name[0] == 'h' && name[1] == 'e'
3115b81b6b3SRodney W. Grimes 		    && name[2] == 'l' && name[3] == 'p')
3125b81b6b3SRodney W. Grimes 			result = CMD_HELP;
3135b81b6b3SRodney W. Grimes 	}
3145b81b6b3SRodney W. Grimes 	return (result);
3155b81b6b3SRodney W. Grimes }
3165b81b6b3SRodney W. Grimes 
317f73a856dSPoul-Henning Kamp static void
318e1e31c0eSJohn Baldwin db_cmd_list(table)
319e1e31c0eSJohn Baldwin 	struct command_table *table;
3205b81b6b3SRodney W. Grimes {
3215b81b6b3SRodney W. Grimes 	register struct command	*cmd;
3225b81b6b3SRodney W. Grimes 
32339297ba4SSam Leffler 	LIST_FOREACH(cmd, table, next) {
3245b81b6b3SRodney W. Grimes 		db_printf("%-12s", cmd->name);
3252481da74SBruce Evans 		db_end_line(12);
3265b81b6b3SRodney W. Grimes 	}
3275b81b6b3SRodney W. Grimes }
3285b81b6b3SRodney W. Grimes 
329f73a856dSPoul-Henning Kamp static void
330c9b0cc3bSRobert Watson db_command(last_cmdp, cmd_table, dopager)
3315b81b6b3SRodney W. Grimes 	struct command	**last_cmdp;	/* IN_OUT */
332e1e31c0eSJohn Baldwin 	struct command_table *cmd_table;
333c9b0cc3bSRobert Watson 	int dopager;
3345b81b6b3SRodney W. Grimes {
33539297ba4SSam Leffler 	struct command	*cmd = NULL;
3365b81b6b3SRodney W. Grimes 	int		t;
3375b81b6b3SRodney W. Grimes 	char		modif[TOK_STRING_SIZE];
3385b81b6b3SRodney W. Grimes 	db_expr_t	addr, count;
339381fe1aaSGarrett Wollman 	boolean_t	have_addr = FALSE;
3405b81b6b3SRodney W. Grimes 	int		result;
3415b81b6b3SRodney W. Grimes 
3425b81b6b3SRodney W. Grimes 	t = db_read_token();
3435b81b6b3SRodney W. Grimes 	if (t == tEOL) {
3445b81b6b3SRodney W. Grimes 	    /* empty line repeats last command, at 'next' */
3455b81b6b3SRodney W. Grimes 	    cmd = *last_cmdp;
3465b81b6b3SRodney W. Grimes 	    addr = (db_expr_t)db_next;
3475b81b6b3SRodney W. Grimes 	    have_addr = FALSE;
3485b81b6b3SRodney W. Grimes 	    count = 1;
3495b81b6b3SRodney W. Grimes 	    modif[0] = '\0';
3505b81b6b3SRodney W. Grimes 	}
3515b81b6b3SRodney W. Grimes 	else if (t == tEXCL) {
352976794d9SBruce Evans 	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
3535b81b6b3SRodney W. Grimes 	    return;
3545b81b6b3SRodney W. Grimes 	}
3555b81b6b3SRodney W. Grimes 	else if (t != tIDENT) {
3565b81b6b3SRodney W. Grimes 	    db_printf("?\n");
3575b81b6b3SRodney W. Grimes 	    db_flush_lex();
3585b81b6b3SRodney W. Grimes 	    return;
3595b81b6b3SRodney W. Grimes 	}
3605b81b6b3SRodney W. Grimes 	else {
3615b81b6b3SRodney W. Grimes 	    /*
3625b81b6b3SRodney W. Grimes 	     * Search for command
3635b81b6b3SRodney W. Grimes 	     */
3645b81b6b3SRodney W. Grimes 	    while (cmd_table) {
3655b81b6b3SRodney W. Grimes 		result = db_cmd_search(db_tok_string,
3665b81b6b3SRodney W. Grimes 				       cmd_table,
3675b81b6b3SRodney W. Grimes 				       &cmd);
3685b81b6b3SRodney W. Grimes 		switch (result) {
3695b81b6b3SRodney W. Grimes 		    case CMD_NONE:
3705b81b6b3SRodney W. Grimes 			db_printf("No such command\n");
3715b81b6b3SRodney W. Grimes 			db_flush_lex();
3725b81b6b3SRodney W. Grimes 			return;
3735b81b6b3SRodney W. Grimes 		    case CMD_AMBIGUOUS:
3745b81b6b3SRodney W. Grimes 			db_printf("Ambiguous\n");
3755b81b6b3SRodney W. Grimes 			db_flush_lex();
3765b81b6b3SRodney W. Grimes 			return;
3775b81b6b3SRodney W. Grimes 		    case CMD_HELP:
378e1e31c0eSJohn Baldwin 			db_cmd_list(cmd_table);
3795b81b6b3SRodney W. Grimes 			db_flush_lex();
3805b81b6b3SRodney W. Grimes 			return;
3815b81b6b3SRodney W. Grimes 		    default:
3825b81b6b3SRodney W. Grimes 			break;
3835b81b6b3SRodney W. Grimes 		}
384e1e31c0eSJohn Baldwin 		if ((cmd_table = cmd->more) != NULL) {
3855b81b6b3SRodney W. Grimes 		    t = db_read_token();
3865b81b6b3SRodney W. Grimes 		    if (t != tIDENT) {
387e1e31c0eSJohn Baldwin 			db_cmd_list(cmd_table);
3885b81b6b3SRodney W. Grimes 			db_flush_lex();
3895b81b6b3SRodney W. Grimes 			return;
3905b81b6b3SRodney W. Grimes 		    }
3915b81b6b3SRodney W. Grimes 		}
3925b81b6b3SRodney W. Grimes 	    }
3935b81b6b3SRodney W. Grimes 
3945b81b6b3SRodney W. Grimes 	    if ((cmd->flag & CS_OWN) == 0) {
3955b81b6b3SRodney W. Grimes 		/*
3965b81b6b3SRodney W. Grimes 		 * Standard syntax:
3975b81b6b3SRodney W. Grimes 		 * command [/modifier] [addr] [,count]
3985b81b6b3SRodney W. Grimes 		 */
3995b81b6b3SRodney W. Grimes 		t = db_read_token();
4005b81b6b3SRodney W. Grimes 		if (t == tSLASH) {
4015b81b6b3SRodney W. Grimes 		    t = db_read_token();
4025b81b6b3SRodney W. Grimes 		    if (t != tIDENT) {
4035b81b6b3SRodney W. Grimes 			db_printf("Bad modifier\n");
4045b81b6b3SRodney W. Grimes 			db_flush_lex();
4055b81b6b3SRodney W. Grimes 			return;
4065b81b6b3SRodney W. Grimes 		    }
4075b81b6b3SRodney W. Grimes 		    db_strcpy(modif, db_tok_string);
4085b81b6b3SRodney W. Grimes 		}
4095b81b6b3SRodney W. Grimes 		else {
4105b81b6b3SRodney W. Grimes 		    db_unread_token(t);
4115b81b6b3SRodney W. Grimes 		    modif[0] = '\0';
4125b81b6b3SRodney W. Grimes 		}
4135b81b6b3SRodney W. Grimes 
4145b81b6b3SRodney W. Grimes 		if (db_expression(&addr)) {
4155b81b6b3SRodney W. Grimes 		    db_dot = (db_addr_t) addr;
4165b81b6b3SRodney W. Grimes 		    db_last_addr = db_dot;
4175b81b6b3SRodney W. Grimes 		    have_addr = TRUE;
4185b81b6b3SRodney W. Grimes 		}
4195b81b6b3SRodney W. Grimes 		else {
4205b81b6b3SRodney W. Grimes 		    addr = (db_expr_t) db_dot;
4215b81b6b3SRodney W. Grimes 		    have_addr = FALSE;
4225b81b6b3SRodney W. Grimes 		}
4235b81b6b3SRodney W. Grimes 		t = db_read_token();
4245b81b6b3SRodney W. Grimes 		if (t == tCOMMA) {
4255b81b6b3SRodney W. Grimes 		    if (!db_expression(&count)) {
4265b81b6b3SRodney W. Grimes 			db_printf("Count missing\n");
4275b81b6b3SRodney W. Grimes 			db_flush_lex();
4285b81b6b3SRodney W. Grimes 			return;
4295b81b6b3SRodney W. Grimes 		    }
4305b81b6b3SRodney W. Grimes 		}
4315b81b6b3SRodney W. Grimes 		else {
4325b81b6b3SRodney W. Grimes 		    db_unread_token(t);
4335b81b6b3SRodney W. Grimes 		    count = -1;
4345b81b6b3SRodney W. Grimes 		}
4355b81b6b3SRodney W. Grimes 		if ((cmd->flag & CS_MORE) == 0) {
4365b81b6b3SRodney W. Grimes 		    db_skip_to_eol();
4375b81b6b3SRodney W. Grimes 		}
4385b81b6b3SRodney W. Grimes 	    }
4395b81b6b3SRodney W. Grimes 	}
4405b81b6b3SRodney W. Grimes 	*last_cmdp = cmd;
4415b81b6b3SRodney W. Grimes 	if (cmd != 0) {
4425b81b6b3SRodney W. Grimes 	    /*
4435b81b6b3SRodney W. Grimes 	     * Execute the command.
4445b81b6b3SRodney W. Grimes 	     */
445c9b0cc3bSRobert Watson 	    if (dopager)
44619e9205aSJohn Baldwin 		db_enable_pager();
447c9b0cc3bSRobert Watson 	    else
448c9b0cc3bSRobert Watson 		db_disable_pager();
4495b81b6b3SRodney W. Grimes 	    (*cmd->fcn)(addr, have_addr, count, modif);
450c9b0cc3bSRobert Watson 	    if (dopager)
45119e9205aSJohn Baldwin 		db_disable_pager();
4525b81b6b3SRodney W. Grimes 
4535b81b6b3SRodney W. Grimes 	    if (cmd->flag & CS_SET_DOT) {
4545b81b6b3SRodney W. Grimes 		/*
4555b81b6b3SRodney W. Grimes 		 * If command changes dot, set dot to
4565b81b6b3SRodney W. Grimes 		 * previous address displayed (if 'ed' style).
4575b81b6b3SRodney W. Grimes 		 */
4585b81b6b3SRodney W. Grimes 		if (db_ed_style) {
4595b81b6b3SRodney W. Grimes 		    db_dot = db_prev;
4605b81b6b3SRodney W. Grimes 		}
4615b81b6b3SRodney W. Grimes 		else {
4625b81b6b3SRodney W. Grimes 		    db_dot = db_next;
4635b81b6b3SRodney W. Grimes 		}
4645b81b6b3SRodney W. Grimes 	    }
4655b81b6b3SRodney W. Grimes 	    else {
4665b81b6b3SRodney W. Grimes 		/*
4675b81b6b3SRodney W. Grimes 		 * If command does not change dot,
4685b81b6b3SRodney W. Grimes 		 * set 'next' location to be the same.
4695b81b6b3SRodney W. Grimes 		 */
4705b81b6b3SRodney W. Grimes 		db_next = db_dot;
4715b81b6b3SRodney W. Grimes 	    }
4725b81b6b3SRodney W. Grimes 	}
4735b81b6b3SRodney W. Grimes }
4745b81b6b3SRodney W. Grimes 
4755b81b6b3SRodney W. Grimes /*
476b7aa38c1SBruce Evans  * At least one non-optional command must be implemented using
477b7aa38c1SBruce Evans  * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
478b7aa38c1SBruce Evans  */
479b7aa38c1SBruce Evans DB_COMMAND(panic, db_panic)
4803eac4884SPoul-Henning Kamp {
48115cc91d3SJohn Baldwin 	db_disable_pager();
482edf8a815SDavid Greenman 	panic("from debugger");
4833eac4884SPoul-Henning Kamp }
4843eac4884SPoul-Henning Kamp 
4853eac4884SPoul-Henning Kamp void
4865b81b6b3SRodney W. Grimes db_command_loop()
4875b81b6b3SRodney W. Grimes {
4885b81b6b3SRodney W. Grimes 	/*
4895b81b6b3SRodney W. Grimes 	 * Initialize 'prev' and 'next' to dot.
4905b81b6b3SRodney W. Grimes 	 */
4915b81b6b3SRodney W. Grimes 	db_prev = db_dot;
4925b81b6b3SRodney W. Grimes 	db_next = db_dot;
4935b81b6b3SRodney W. Grimes 
4945b81b6b3SRodney W. Grimes 	db_cmd_loop_done = 0;
4955b81b6b3SRodney W. Grimes 	while (!db_cmd_loop_done) {
4965b81b6b3SRodney W. Grimes 	    if (db_print_position() != 0)
4975b81b6b3SRodney W. Grimes 		db_printf("\n");
4985b81b6b3SRodney W. Grimes 
4995b81b6b3SRodney W. Grimes 	    db_printf("db> ");
5005b81b6b3SRodney W. Grimes 	    (void) db_read_line();
5015b81b6b3SRodney W. Grimes 
50239297ba4SSam Leffler 	    db_command(&db_last_command, &db_cmd_table, /* dopager */ 1);
5035b81b6b3SRodney W. Grimes 	}
5045b81b6b3SRodney W. Grimes }
5055b81b6b3SRodney W. Grimes 
506c9b0cc3bSRobert Watson /*
507c9b0cc3bSRobert Watson  * Execute a command on behalf of a script.  The caller is responsible for
508c9b0cc3bSRobert Watson  * making sure that the command string is < DB_MAXLINE or it will be
509c9b0cc3bSRobert Watson  * truncated.
510c9b0cc3bSRobert Watson  *
511c9b0cc3bSRobert Watson  * XXXRW: Runs by injecting faked input into DDB input stream; it would be
512c9b0cc3bSRobert Watson  * nicer to use an alternative approach that didn't mess with the previous
513c9b0cc3bSRobert Watson  * command buffer.
514c9b0cc3bSRobert Watson  */
515c9b0cc3bSRobert Watson void
516c9b0cc3bSRobert Watson db_command_script(const char *command)
517c9b0cc3bSRobert Watson {
518c9b0cc3bSRobert Watson 	db_prev = db_next = db_dot;
519c9b0cc3bSRobert Watson 	db_inject_line(command);
52039297ba4SSam Leffler 	db_command(&db_last_command, &db_cmd_table, /* dopager */ 0);
521c9b0cc3bSRobert Watson }
522c9b0cc3bSRobert Watson 
5235b81b6b3SRodney W. Grimes void
5245b81b6b3SRodney W. Grimes db_error(s)
525bda9921dSMark Murray 	const char *s;
5265b81b6b3SRodney W. Grimes {
5275b81b6b3SRodney W. Grimes 	if (s)
52858d9a059SKris Kennaway 	    db_printf("%s", s);
5295b81b6b3SRodney W. Grimes 	db_flush_lex();
53037224cd3SMarcel Moolenaar 	kdb_reenter();
5315b81b6b3SRodney W. Grimes }
5325b81b6b3SRodney W. Grimes 
533299cceefSMarcel Moolenaar static void
534299cceefSMarcel Moolenaar db_dump(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
535299cceefSMarcel Moolenaar {
536299cceefSMarcel Moolenaar 	int error;
537299cceefSMarcel Moolenaar 
538*21d748a9SAlfred Perlstein 	if (textdump_pending) {
539*21d748a9SAlfred Perlstein 		db_printf("textdump_pending set.\n"
540*21d748a9SAlfred Perlstein 		    "run \"textdump unset\" first or \"textdump dump\" for a textdump.\n");
541*21d748a9SAlfred Perlstein 		return;
542*21d748a9SAlfred Perlstein 	}
543299cceefSMarcel Moolenaar 	error = doadump(FALSE);
544299cceefSMarcel Moolenaar 	if (error) {
545299cceefSMarcel Moolenaar 		db_printf("Cannot dump: ");
546299cceefSMarcel Moolenaar 		switch (error) {
547299cceefSMarcel Moolenaar 		case EBUSY:
548299cceefSMarcel Moolenaar 			db_printf("debugger got invoked while dumping.\n");
549299cceefSMarcel Moolenaar 			break;
550299cceefSMarcel Moolenaar 		case ENXIO:
551299cceefSMarcel Moolenaar 			db_printf("no dump device specified.\n");
552299cceefSMarcel Moolenaar 			break;
553299cceefSMarcel Moolenaar 		default:
554299cceefSMarcel Moolenaar 			db_printf("unknown error (error=%d).\n", error);
555299cceefSMarcel Moolenaar 			break;
556299cceefSMarcel Moolenaar 		}
557299cceefSMarcel Moolenaar 	}
558299cceefSMarcel Moolenaar }
5595b81b6b3SRodney W. Grimes 
5605b81b6b3SRodney W. Grimes /*
5615b81b6b3SRodney W. Grimes  * Call random function:
5625b81b6b3SRodney W. Grimes  * !expr(arg,arg,arg)
5635b81b6b3SRodney W. Grimes  */
564a2aeb24eSMarcel Moolenaar 
565a2aeb24eSMarcel Moolenaar /* The generic implementation supports a maximum of 10 arguments. */
566a2aeb24eSMarcel Moolenaar typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
567a2aeb24eSMarcel Moolenaar     db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
568a2aeb24eSMarcel Moolenaar 
569a2aeb24eSMarcel Moolenaar static __inline int
570a2aeb24eSMarcel Moolenaar db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
571a2aeb24eSMarcel Moolenaar {
572a2aeb24eSMarcel Moolenaar 	__db_f *f = (__db_f *)addr;
573a2aeb24eSMarcel Moolenaar 
574a2aeb24eSMarcel Moolenaar 	if (nargs > 10) {
575a2aeb24eSMarcel Moolenaar 		db_printf("Too many arguments (max 10)\n");
576a2aeb24eSMarcel Moolenaar 		return (0);
577a2aeb24eSMarcel Moolenaar 	}
578a2aeb24eSMarcel Moolenaar 	*rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
579a2aeb24eSMarcel Moolenaar 	    args[6], args[7], args[8], args[9]);
580a2aeb24eSMarcel Moolenaar 	return (1);
581a2aeb24eSMarcel Moolenaar }
582a2aeb24eSMarcel Moolenaar 
583f73a856dSPoul-Henning Kamp static void
584976794d9SBruce Evans db_fncall(dummy1, dummy2, dummy3, dummy4)
585976794d9SBruce Evans 	db_expr_t	dummy1;
586976794d9SBruce Evans 	boolean_t	dummy2;
587976794d9SBruce Evans 	db_expr_t	dummy3;
588976794d9SBruce Evans 	char *		dummy4;
5895b81b6b3SRodney W. Grimes {
5905b81b6b3SRodney W. Grimes 	db_expr_t	fn_addr;
591a2aeb24eSMarcel Moolenaar 	db_expr_t	args[DB_MAXARGS];
5925b81b6b3SRodney W. Grimes 	int		nargs = 0;
5935b81b6b3SRodney W. Grimes 	db_expr_t	retval;
5945b81b6b3SRodney W. Grimes 	int		t;
5955b81b6b3SRodney W. Grimes 
5965b81b6b3SRodney W. Grimes 	if (!db_expression(&fn_addr)) {
5975b81b6b3SRodney W. Grimes 	    db_printf("Bad function\n");
5985b81b6b3SRodney W. Grimes 	    db_flush_lex();
5995b81b6b3SRodney W. Grimes 	    return;
6005b81b6b3SRodney W. Grimes 	}
6015b81b6b3SRodney W. Grimes 
6025b81b6b3SRodney W. Grimes 	t = db_read_token();
6035b81b6b3SRodney W. Grimes 	if (t == tLPAREN) {
6045b81b6b3SRodney W. Grimes 	    if (db_expression(&args[0])) {
6055b81b6b3SRodney W. Grimes 		nargs++;
6065b81b6b3SRodney W. Grimes 		while ((t = db_read_token()) == tCOMMA) {
607a2aeb24eSMarcel Moolenaar 		    if (nargs == DB_MAXARGS) {
608a2aeb24eSMarcel Moolenaar 			db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
6095b81b6b3SRodney W. Grimes 			db_flush_lex();
6105b81b6b3SRodney W. Grimes 			return;
6115b81b6b3SRodney W. Grimes 		    }
6125b81b6b3SRodney W. Grimes 		    if (!db_expression(&args[nargs])) {
6135b81b6b3SRodney W. Grimes 			db_printf("Argument missing\n");
6145b81b6b3SRodney W. Grimes 			db_flush_lex();
6155b81b6b3SRodney W. Grimes 			return;
6165b81b6b3SRodney W. Grimes 		    }
6175b81b6b3SRodney W. Grimes 		    nargs++;
6185b81b6b3SRodney W. Grimes 		}
6195b81b6b3SRodney W. Grimes 		db_unread_token(t);
6205b81b6b3SRodney W. Grimes 	    }
6215b81b6b3SRodney W. Grimes 	    if (db_read_token() != tRPAREN) {
6225b81b6b3SRodney W. Grimes 		db_printf("?\n");
6235b81b6b3SRodney W. Grimes 		db_flush_lex();
6245b81b6b3SRodney W. Grimes 		return;
6255b81b6b3SRodney W. Grimes 	    }
6265b81b6b3SRodney W. Grimes 	}
6275b81b6b3SRodney W. Grimes 	db_skip_to_eol();
62815cc91d3SJohn Baldwin 	db_disable_pager();
6295b81b6b3SRodney W. Grimes 
630a2aeb24eSMarcel Moolenaar 	if (DB_CALL(fn_addr, &retval, nargs, args))
631a2aeb24eSMarcel Moolenaar 		db_printf("= %#lr\n", (long)retval);
6325b81b6b3SRodney W. Grimes }
633ad146781SPaul Traina 
6345370c3b6SPeter Wemm static void
635e4b732cfSBruce Evans db_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
636e4b732cfSBruce Evans {
637e4b732cfSBruce Evans 
638e4b732cfSBruce Evans 	cpu_halt();
639e4b732cfSBruce Evans }
640e4b732cfSBruce Evans 
641e4b732cfSBruce Evans static void
64219d2c78fSDima Dorfman db_kill(dummy1, dummy2, dummy3, dummy4)
64319d2c78fSDima Dorfman 	db_expr_t	dummy1;
64419d2c78fSDima Dorfman 	boolean_t	dummy2;
64519d2c78fSDima Dorfman 	db_expr_t	dummy3;
64619d2c78fSDima Dorfman 	char *		dummy4;
64719d2c78fSDima Dorfman {
64819d2c78fSDima Dorfman 	db_expr_t old_radix, pid, sig;
64919d2c78fSDima Dorfman 	struct proc *p;
65019d2c78fSDima Dorfman 
65119d2c78fSDima Dorfman #define DB_ERROR(f)	do { db_printf f; db_flush_lex(); goto out; } while (0)
65219d2c78fSDima Dorfman 
65319d2c78fSDima Dorfman 	/*
65419d2c78fSDima Dorfman 	 * PIDs and signal numbers are typically represented in base
65519d2c78fSDima Dorfman 	 * 10, so make that the default here.  It can, of course, be
65619d2c78fSDima Dorfman 	 * overridden by specifying a prefix.
65719d2c78fSDima Dorfman 	 */
65819d2c78fSDima Dorfman 	old_radix = db_radix;
65919d2c78fSDima Dorfman 	db_radix = 10;
66019d2c78fSDima Dorfman 	/* Retrieve arguments. */
66119d2c78fSDima Dorfman 	if (!db_expression(&sig))
66219d2c78fSDima Dorfman 		DB_ERROR(("Missing signal number\n"));
66319d2c78fSDima Dorfman 	if (!db_expression(&pid))
66419d2c78fSDima Dorfman 		DB_ERROR(("Missing process ID\n"));
66519d2c78fSDima Dorfman 	db_skip_to_eol();
666a29af74bSKonstantin Belousov 	if (!_SIG_VALID(sig))
66719d2c78fSDima Dorfman 		DB_ERROR(("Signal number out of range\n"));
66819d2c78fSDima Dorfman 
66919d2c78fSDima Dorfman 	/*
67019d2c78fSDima Dorfman 	 * Find the process in question.  allproc_lock is not needed
67119d2c78fSDima Dorfman 	 * since we're in DDB.
67219d2c78fSDima Dorfman 	 */
67319d2c78fSDima Dorfman 	/* sx_slock(&allproc_lock); */
674f67af5c9SXin LI 	FOREACH_PROC_IN_SYSTEM(p)
67519d2c78fSDima Dorfman 	    if (p->p_pid == pid)
67619d2c78fSDima Dorfman 		    break;
67719d2c78fSDima Dorfman 	/* sx_sunlock(&allproc_lock); */
67819d2c78fSDima Dorfman 	if (p == NULL)
679e30e3e9eSMatt Jacob 		DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
68019d2c78fSDima Dorfman 
68119d2c78fSDima Dorfman 	/* If it's already locked, bail; otherwise, do the deed. */
68219d2c78fSDima Dorfman 	if (PROC_TRYLOCK(p) == 0)
683e30e3e9eSMatt Jacob 		DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
68419d2c78fSDima Dorfman 	else {
685a3de221dSKonstantin Belousov 		pksignal(p, sig, NULL);
68619d2c78fSDima Dorfman 		PROC_UNLOCK(p);
68719d2c78fSDima Dorfman 	}
68819d2c78fSDima Dorfman 
68919d2c78fSDima Dorfman out:
69019d2c78fSDima Dorfman 	db_radix = old_radix;
69119d2c78fSDima Dorfman #undef DB_ERROR
69219d2c78fSDima Dorfman }
69319d2c78fSDima Dorfman 
6940f59fbc3SBjoern A. Zeeb /*
6950f59fbc3SBjoern A. Zeeb  * Reboot.  In case there is an additional argument, take it as delay in
6960f59fbc3SBjoern A. Zeeb  * seconds.  Default to 15s if we cannot parse it and make sure we will
6970f59fbc3SBjoern A. Zeeb  * never wait longer than 1 week.  Some code is similar to
6980f59fbc3SBjoern A. Zeeb  * kern_shutdown.c:shutdown_panic().
6990f59fbc3SBjoern A. Zeeb  */
7000f59fbc3SBjoern A. Zeeb #ifndef	DB_RESET_MAXDELAY
7010f59fbc3SBjoern A. Zeeb #define	DB_RESET_MAXDELAY	(3600 * 24 * 7)
7020f59fbc3SBjoern A. Zeeb #endif
7030f59fbc3SBjoern A. Zeeb 
70419d2c78fSDima Dorfman static void
7050f59fbc3SBjoern A. Zeeb db_reset(db_expr_t addr, boolean_t have_addr, db_expr_t count __unused,
7060f59fbc3SBjoern A. Zeeb     char *modif __unused)
7075370c3b6SPeter Wemm {
7080f59fbc3SBjoern A. Zeeb 	int delay, loop;
7090f59fbc3SBjoern A. Zeeb 
7100f59fbc3SBjoern A. Zeeb 	if (have_addr) {
7110f59fbc3SBjoern A. Zeeb 		delay = (int)db_hex2dec(addr);
7120f59fbc3SBjoern A. Zeeb 
7130f59fbc3SBjoern A. Zeeb 		/* If we parse to fail, use 15s. */
7140f59fbc3SBjoern A. Zeeb 		if (delay == -1)
7150f59fbc3SBjoern A. Zeeb 			delay = 15;
7160f59fbc3SBjoern A. Zeeb 
7170f59fbc3SBjoern A. Zeeb 		/* Cap at one week. */
7180f59fbc3SBjoern A. Zeeb 		if ((uintmax_t)delay > (uintmax_t)DB_RESET_MAXDELAY)
7190f59fbc3SBjoern A. Zeeb 			delay = DB_RESET_MAXDELAY;
7200f59fbc3SBjoern A. Zeeb 
7210f59fbc3SBjoern A. Zeeb 		db_printf("Automatic reboot in %d seconds - "
7220f59fbc3SBjoern A. Zeeb 		    "press a key on the console to abort\n", delay);
7230f59fbc3SBjoern A. Zeeb 		for (loop = delay * 10; loop > 0; --loop) {
7240f59fbc3SBjoern A. Zeeb 			DELAY(1000 * 100); /* 1/10th second */
7250f59fbc3SBjoern A. Zeeb 			/* Did user type a key? */
7260f59fbc3SBjoern A. Zeeb 			if (cncheckc() != -1)
7270f59fbc3SBjoern A. Zeeb 				return;
7280f59fbc3SBjoern A. Zeeb 		}
7290f59fbc3SBjoern A. Zeeb 	}
7305370c3b6SPeter Wemm 
7315370c3b6SPeter Wemm 	cpu_reset();
7325370c3b6SPeter Wemm }
73374cc032bSPoul-Henning Kamp 
73474cc032bSPoul-Henning Kamp static void
73574cc032bSPoul-Henning Kamp db_watchdog(dummy1, dummy2, dummy3, dummy4)
73674cc032bSPoul-Henning Kamp 	db_expr_t	dummy1;
73774cc032bSPoul-Henning Kamp 	boolean_t	dummy2;
73874cc032bSPoul-Henning Kamp 	db_expr_t	dummy3;
73974cc032bSPoul-Henning Kamp 	char *		dummy4;
74074cc032bSPoul-Henning Kamp {
7418b927d7bSAttilio Rao 	db_expr_t old_radix, tout;
7428b927d7bSAttilio Rao 	int err, i;
7438b927d7bSAttilio Rao 
7448b927d7bSAttilio Rao 	old_radix = db_radix;
7458b927d7bSAttilio Rao 	db_radix = 10;
7468b927d7bSAttilio Rao 	err = db_expression(&tout);
7478b927d7bSAttilio Rao 	db_skip_to_eol();
7488b927d7bSAttilio Rao 	db_radix = old_radix;
7498b927d7bSAttilio Rao 
7508b927d7bSAttilio Rao 	/* If no argument is provided the watchdog will just be disabled. */
7518b927d7bSAttilio Rao 	if (err == 0) {
7528b927d7bSAttilio Rao 		db_printf("No argument provided, disabling watchdog\n");
7538b927d7bSAttilio Rao 		tout = 0;
7548b927d7bSAttilio Rao 	} else if ((tout & WD_INTERVAL) == WD_TO_NEVER) {
7558b927d7bSAttilio Rao 		db_error("Out of range watchdog interval\n");
7568b927d7bSAttilio Rao 		return;
7578b927d7bSAttilio Rao 	}
7588b927d7bSAttilio Rao 	EVENTHANDLER_INVOKE(watchdog_list, tout, &i);
75974cc032bSPoul-Henning Kamp }
760f3be7cb3SMarcel Moolenaar 
761f3be7cb3SMarcel Moolenaar static void
762f3be7cb3SMarcel Moolenaar db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
763f3be7cb3SMarcel Moolenaar {
764f3be7cb3SMarcel Moolenaar 
7653a5d3671SMatthew D Fleming 	if (kdb_dbbe_select("gdb") != 0) {
766f3be7cb3SMarcel Moolenaar 		db_printf("The remote GDB backend could not be selected.\n");
7673a5d3671SMatthew D Fleming 		return;
7683a5d3671SMatthew D Fleming 	}
7693a5d3671SMatthew D Fleming 	/*
7703a5d3671SMatthew D Fleming 	 * Mark that we are done in the debugger.  kdb_trap()
7713a5d3671SMatthew D Fleming 	 * should re-enter with the new backend.
7723a5d3671SMatthew D Fleming 	 */
7733a5d3671SMatthew D Fleming 	db_cmd_loop_done = 1;
7743a5d3671SMatthew D Fleming 	db_printf("(ctrl-c will return control to ddb)\n");
775f3be7cb3SMarcel Moolenaar }
776fd32d93bSMarcel Moolenaar 
777fd32d93bSMarcel Moolenaar static void
778fd32d93bSMarcel Moolenaar db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
779fd32d93bSMarcel Moolenaar {
780fd32d93bSMarcel Moolenaar 	struct thread *td;
781fd32d93bSMarcel Moolenaar 	db_expr_t radix;
782a13aca1aSRobert Watson 	pid_t pid;
783fd32d93bSMarcel Moolenaar 	int t;
784fd32d93bSMarcel Moolenaar 
785fd32d93bSMarcel Moolenaar 	/*
786fd32d93bSMarcel Moolenaar 	 * We parse our own arguments. We don't like the default radix.
787fd32d93bSMarcel Moolenaar 	 */
788fd32d93bSMarcel Moolenaar 	radix = db_radix;
789fd32d93bSMarcel Moolenaar 	db_radix = 10;
790fd32d93bSMarcel Moolenaar 	hastid = db_expression(&tid);
791fd32d93bSMarcel Moolenaar 	t = db_read_token();
792fd32d93bSMarcel Moolenaar 	if (t == tCOMMA) {
793fd32d93bSMarcel Moolenaar 		if (!db_expression(&count)) {
794fd32d93bSMarcel Moolenaar 			db_printf("Count missing\n");
795fd32d93bSMarcel Moolenaar 			db_flush_lex();
796fd32d93bSMarcel Moolenaar 			return;
797fd32d93bSMarcel Moolenaar 		}
798fd32d93bSMarcel Moolenaar 	} else {
799fd32d93bSMarcel Moolenaar 		db_unread_token(t);
800fd32d93bSMarcel Moolenaar 		count = -1;
801fd32d93bSMarcel Moolenaar 	}
802fd32d93bSMarcel Moolenaar 	db_skip_to_eol();
803fd32d93bSMarcel Moolenaar 	db_radix = radix;
804fd32d93bSMarcel Moolenaar 
805fd32d93bSMarcel Moolenaar 	if (hastid) {
806fd32d93bSMarcel Moolenaar 		td = kdb_thr_lookup((lwpid_t)tid);
807fd32d93bSMarcel Moolenaar 		if (td == NULL)
808fd32d93bSMarcel Moolenaar 			td = kdb_thr_from_pid((pid_t)tid);
809fd32d93bSMarcel Moolenaar 		if (td == NULL) {
810fd32d93bSMarcel Moolenaar 			db_printf("Thread %d not found\n", (int)tid);
811fd32d93bSMarcel Moolenaar 			return;
812fd32d93bSMarcel Moolenaar 		}
813fd32d93bSMarcel Moolenaar 	} else
814fd32d93bSMarcel Moolenaar 		td = kdb_thread;
815a13aca1aSRobert Watson 	if (td->td_proc != NULL)
816a13aca1aSRobert Watson 		pid = td->td_proc->p_pid;
817a13aca1aSRobert Watson 	else
818a13aca1aSRobert Watson 		pid = -1;
819a13aca1aSRobert Watson 	db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
820fd32d93bSMarcel Moolenaar 	db_trace_thread(td, count);
821fd32d93bSMarcel Moolenaar }
822a7ad956bSRobert Watson 
823a7ad956bSRobert Watson static void
824a7ad956bSRobert Watson db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
825a7ad956bSRobert Watson     char *dummy4)
826a7ad956bSRobert Watson {
827a7ad956bSRobert Watson 	struct proc *p;
828a7ad956bSRobert Watson 	struct thread *td;
8299641e389SKonstantin Belousov 	jmp_buf jb;
8309641e389SKonstantin Belousov 	void *prev_jb;
831a7ad956bSRobert Watson 
832f67af5c9SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
8339641e389SKonstantin Belousov 		prev_jb = kdb_jmpbuf(jb);
8349641e389SKonstantin Belousov 		if (setjmp(jb) == 0) {
835a7ad956bSRobert Watson 			FOREACH_THREAD_IN_PROC(p, td) {
836a7ad956bSRobert Watson 				db_printf("\nTracing command %s pid %d tid %ld td %p\n",
837a7ad956bSRobert Watson 					  p->p_comm, p->p_pid, (long)td->td_tid, td);
838a7ad956bSRobert Watson 				db_trace_thread(td, -1);
8399641e389SKonstantin Belousov 				if (db_pager_quit) {
8409641e389SKonstantin Belousov 					kdb_jmpbuf(prev_jb);
841da927f93SOlivier Houchard 					return;
842a7ad956bSRobert Watson 				}
843a7ad956bSRobert Watson 			}
844a7ad956bSRobert Watson 		}
8459641e389SKonstantin Belousov 		kdb_jmpbuf(prev_jb);
8469641e389SKonstantin Belousov 	}
8479641e389SKonstantin Belousov }
8480f59fbc3SBjoern A. Zeeb 
8490f59fbc3SBjoern A. Zeeb /*
8500f59fbc3SBjoern A. Zeeb  * Take the parsed expression value from the command line that was parsed
8510f59fbc3SBjoern A. Zeeb  * as a hexadecimal value and convert it as if the expression was parsed
8520f59fbc3SBjoern A. Zeeb  * as a decimal value.  Returns -1 if the expression was not a valid
8530f59fbc3SBjoern A. Zeeb  * decimal value.
8540f59fbc3SBjoern A. Zeeb  */
8550f59fbc3SBjoern A. Zeeb db_expr_t
8560f59fbc3SBjoern A. Zeeb db_hex2dec(db_expr_t expr)
8570f59fbc3SBjoern A. Zeeb {
8580f59fbc3SBjoern A. Zeeb 	uintptr_t x, y;
8590f59fbc3SBjoern A. Zeeb 	db_expr_t val;
8600f59fbc3SBjoern A. Zeeb 
8610f59fbc3SBjoern A. Zeeb 	y = 1;
8620f59fbc3SBjoern A. Zeeb 	val = 0;
8630f59fbc3SBjoern A. Zeeb 	x = expr;
8640f59fbc3SBjoern A. Zeeb 	while (x != 0) {
8650f59fbc3SBjoern A. Zeeb 		if (x % 16 > 9)
8660f59fbc3SBjoern A. Zeeb 			return (-1);
8670f59fbc3SBjoern A. Zeeb 		val += (x % 16) * (y);
8680f59fbc3SBjoern A. Zeeb 		x >>= 4;
8690f59fbc3SBjoern A. Zeeb 		y *= 10;
8700f59fbc3SBjoern A. Zeeb 	}
8710f59fbc3SBjoern A. Zeeb 	return (val);
8720f59fbc3SBjoern A. Zeeb }
873