xref: /freebsd/sys/ddb/db_command.c (revision c036339ddf0cf80164f41ea31f1d8d27f4a068a9)
1dd3cb568SWarner Losh /*-
2796df753SPedro F. Giffuni  * SPDX-License-Identifier: MIT-CMU
3796df753SPedro F. Giffuni  *
45b81b6b3SRodney W. Grimes  * Mach Operating System
55b81b6b3SRodney W. Grimes  * Copyright (c) 1991,1990 Carnegie Mellon University
65b81b6b3SRodney W. Grimes  * All Rights Reserved.
75b81b6b3SRodney W. Grimes  *
85b81b6b3SRodney W. Grimes  * Permission to use, copy, modify and distribute this software and its
95b81b6b3SRodney W. Grimes  * documentation is hereby granted, provided that both the copyright
105b81b6b3SRodney W. Grimes  * notice and this permission notice appear in all copies of the
115b81b6b3SRodney W. Grimes  * software, derivative works or modified versions, and any portions
125b81b6b3SRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
135b81b6b3SRodney W. Grimes  *
145b81b6b3SRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
155b81b6b3SRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
165b81b6b3SRodney W. Grimes  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
175b81b6b3SRodney W. Grimes  *
185b81b6b3SRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
195b81b6b3SRodney W. Grimes  *
205b81b6b3SRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
215b81b6b3SRodney W. Grimes  *  School of Computer Science
225b81b6b3SRodney W. Grimes  *  Carnegie Mellon University
235b81b6b3SRodney W. Grimes  *  Pittsburgh PA 15213-3890
245b81b6b3SRodney W. Grimes  *
255b81b6b3SRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
265b81b6b3SRodney W. Grimes  * rights to redistribute these changes.
275b81b6b3SRodney W. Grimes  */
285b81b6b3SRodney W. Grimes /*
295b81b6b3SRodney W. Grimes  *	Author: David B. Golub, Carnegie Mellon University
305b81b6b3SRodney W. Grimes  *	Date:	7/90
315b81b6b3SRodney W. Grimes  */
325b81b6b3SRodney W. Grimes /*
335b81b6b3SRodney W. Grimes  * Command dispatcher.
345b81b6b3SRodney W. Grimes  */
35753960f7SDavid E. O'Brien 
36753960f7SDavid E. O'Brien #include <sys/cdefs.h>
37753960f7SDavid E. O'Brien __FBSDID("$FreeBSD$");
38753960f7SDavid E. O'Brien 
39f540b106SGarrett Wollman #include <sys/param.h>
404f2ad624SMitchell Horne #include <sys/conf.h>
414f2ad624SMitchell Horne #include <sys/cons.h>
42e2e050c8SConrad Meyer #include <sys/eventhandler.h>
434f2ad624SMitchell Horne #include <sys/kdb.h>
444f2ad624SMitchell Horne #include <sys/kernel.h>
450ec81012SJohn Polstra #include <sys/linker_set.h>
4619d2c78fSDima Dorfman #include <sys/lock.h>
4719d2c78fSDima Dorfman #include <sys/mutex.h>
4819d2c78fSDima Dorfman #include <sys/proc.h>
49ad146781SPaul Traina #include <sys/reboot.h>
5019d2c78fSDima Dorfman #include <sys/signalvar.h>
51f540b106SGarrett Wollman #include <sys/systm.h>
5274cc032bSPoul-Henning Kamp #include <sys/watchdog.h>
533000820aSPoul-Henning Kamp 
545ccbc3ccSBruce Evans #include <ddb/ddb.h>
55058284fcSBruce Evans #include <ddb/db_command.h>
565b81b6b3SRodney W. Grimes #include <ddb/db_lex.h>
575b81b6b3SRodney W. Grimes #include <ddb/db_output.h>
585b81b6b3SRodney W. Grimes 
5926502503SMarcel Moolenaar #include <machine/cpu.h>
600b1ae809SJulian Elischer #include <machine/setjmp.h>
615b81b6b3SRodney W. Grimes 
622449b9e5SMitchell Horne #include <security/mac/mac_framework.h>
632449b9e5SMitchell Horne 
645b81b6b3SRodney W. Grimes /*
655b81b6b3SRodney W. Grimes  * Exported global variables
665b81b6b3SRodney W. Grimes  */
6751d025a5SJustin Hibbits int		db_cmd_loop_done;
685b81b6b3SRodney W. Grimes db_addr_t	db_dot;
695b81b6b3SRodney W. Grimes db_addr_t	db_last_addr;
705b81b6b3SRodney W. Grimes db_addr_t	db_prev;
715b81b6b3SRodney W. Grimes db_addr_t	db_next;
72f41325dbSPeter Wemm 
73299cceefSMarcel Moolenaar static db_cmdfcn_t	db_dump;
74f73a856dSPoul-Henning Kamp static db_cmdfcn_t	db_fncall;
75f3be7cb3SMarcel Moolenaar static db_cmdfcn_t	db_gdb;
76e4b732cfSBruce Evans static db_cmdfcn_t	db_halt;
7719d2c78fSDima Dorfman static db_cmdfcn_t	db_kill;
785370c3b6SPeter Wemm static db_cmdfcn_t	db_reset;
79fd32d93bSMarcel Moolenaar static db_cmdfcn_t	db_stack_trace;
807e89a322SConrad Meyer static db_cmdfcn_t	db_stack_trace_active;
81a7ad956bSRobert Watson static db_cmdfcn_t	db_stack_trace_all;
8274cc032bSPoul-Henning Kamp static db_cmdfcn_t	db_watchdog;
83ad146781SPaul Traina 
848a099482SMitchell Horne #define	DB_CMD(_name, _func, _flags)	\
858a099482SMitchell Horne {					\
868a099482SMitchell Horne 	.name =	(_name),		\
878a099482SMitchell Horne 	.fcn =	(_func),		\
888a099482SMitchell Horne 	.flag =	(_flags),		\
898a099482SMitchell Horne 	.more = NULL,			\
908a099482SMitchell Horne }
918a099482SMitchell Horne #define	DB_TABLE(_name, _more)		\
928a099482SMitchell Horne {					\
938a099482SMitchell Horne 	.name =	(_name),		\
948a099482SMitchell Horne 	.fcn =	NULL,			\
958a099482SMitchell Horne 	.more =	(_more),		\
968a099482SMitchell Horne }
97cec9a4bfSDavid E. O'Brien 
984ef7db5aSMitchell Horne static struct db_command db_show_active_cmds[] = {
99a305b20eSMitchell Horne 	DB_CMD("trace",		db_stack_trace_active,	DB_CMD_MEMSAFE),
1007e89a322SConrad Meyer };
1014ef7db5aSMitchell Horne struct db_command_table db_show_active_table =
1027e89a322SConrad Meyer     LIST_HEAD_INITIALIZER(db_show_active_table);
1037e89a322SConrad Meyer 
1044ef7db5aSMitchell Horne static struct db_command db_show_all_cmds[] = {
105a305b20eSMitchell Horne 	DB_CMD("trace",		db_stack_trace_all,	DB_CMD_MEMSAFE),
106cec9a4bfSDavid E. O'Brien };
1074ef7db5aSMitchell Horne struct db_command_table db_show_all_table =
10839297ba4SSam Leffler     LIST_HEAD_INITIALIZER(db_show_all_table);
109e1e31c0eSJohn Baldwin 
1104ef7db5aSMitchell Horne static struct db_command db_show_cmds[] = {
1118a099482SMitchell Horne 	DB_TABLE("active",	&db_show_active_table),
1128a099482SMitchell Horne 	DB_TABLE("all",		&db_show_all_table),
113a305b20eSMitchell Horne 	DB_CMD("registers",	db_show_regs,		DB_CMD_MEMSAFE),
114a305b20eSMitchell Horne 	DB_CMD("breaks",	db_listbreak_cmd,	DB_CMD_MEMSAFE),
115a305b20eSMitchell Horne 	DB_CMD("threads",	db_show_threads,	DB_CMD_MEMSAFE),
116cec9a4bfSDavid E. O'Brien };
1174ef7db5aSMitchell Horne struct db_command_table db_show_table = LIST_HEAD_INITIALIZER(db_show_table);
118cec9a4bfSDavid E. O'Brien 
1194ef7db5aSMitchell Horne static struct db_command db_cmds[] = {
1208a099482SMitchell Horne 	DB_TABLE("show",	&db_show_table),
1218a099482SMitchell Horne 	DB_CMD("print",		db_print_cmd,		0),
1228a099482SMitchell Horne 	DB_CMD("p",		db_print_cmd,		0),
1238a099482SMitchell Horne 	DB_CMD("examine",	db_examine_cmd,		CS_SET_DOT),
1248a099482SMitchell Horne 	DB_CMD("x",		db_examine_cmd,		CS_SET_DOT),
1258a099482SMitchell Horne 	DB_CMD("search",	db_search_cmd,		CS_OWN|CS_SET_DOT),
126a305b20eSMitchell Horne 	DB_CMD("set",		db_set_cmd,		CS_OWN|DB_CMD_MEMSAFE),
1278a099482SMitchell Horne 	DB_CMD("write",		db_write_cmd,		CS_MORE|CS_SET_DOT),
1288a099482SMitchell Horne 	DB_CMD("w",		db_write_cmd,		CS_MORE|CS_SET_DOT),
129*c036339dSMark Johnston 	DB_CMD("delete",	db_delete_cmd,		0),
130*c036339dSMark Johnston 	DB_CMD("d",		db_delete_cmd,		0),
131a305b20eSMitchell Horne 	DB_CMD("dump",		db_dump,		DB_CMD_MEMSAFE),
132*c036339dSMark Johnston 	DB_CMD("break",		db_breakpoint_cmd,	0),
133*c036339dSMark Johnston 	DB_CMD("b",		db_breakpoint_cmd,	0),
134*c036339dSMark Johnston 	DB_CMD("dwatch",	db_deletewatch_cmd,	0),
135*c036339dSMark Johnston 	DB_CMD("watch",		db_watchpoint_cmd,	CS_MORE),
136*c036339dSMark Johnston 	DB_CMD("dhwatch",	db_deletehwatch_cmd,	0),
137*c036339dSMark Johnston 	DB_CMD("hwatch",	db_hwatchpoint_cmd,	0),
138a305b20eSMitchell Horne 	DB_CMD("step",		db_single_step_cmd,	DB_CMD_MEMSAFE),
139a305b20eSMitchell Horne 	DB_CMD("s",		db_single_step_cmd,	DB_CMD_MEMSAFE),
140a305b20eSMitchell Horne 	DB_CMD("continue",	db_continue_cmd,	DB_CMD_MEMSAFE),
141a305b20eSMitchell Horne 	DB_CMD("c",		db_continue_cmd,	DB_CMD_MEMSAFE),
142a305b20eSMitchell Horne 	DB_CMD("until",		db_trace_until_call_cmd, DB_CMD_MEMSAFE),
143a305b20eSMitchell Horne 	DB_CMD("next",		db_trace_until_matching_cmd, DB_CMD_MEMSAFE),
1448a099482SMitchell Horne 	DB_CMD("match",		db_trace_until_matching_cmd, 0),
145a305b20eSMitchell Horne 	DB_CMD("trace",		db_stack_trace,		CS_OWN|DB_CMD_MEMSAFE),
146a305b20eSMitchell Horne 	DB_CMD("t",		db_stack_trace,		CS_OWN|DB_CMD_MEMSAFE),
1477e89a322SConrad Meyer 	/* XXX alias for active trace */
148a305b20eSMitchell Horne 	DB_CMD("acttrace",	db_stack_trace_active,	DB_CMD_MEMSAFE),
14939297ba4SSam Leffler 	/* XXX alias for all trace */
150a305b20eSMitchell Horne 	DB_CMD("alltrace",	db_stack_trace_all,	DB_CMD_MEMSAFE),
151a305b20eSMitchell Horne 	DB_CMD("where",		db_stack_trace,		CS_OWN|DB_CMD_MEMSAFE),
152a305b20eSMitchell Horne 	DB_CMD("bt",		db_stack_trace,		CS_OWN|DB_CMD_MEMSAFE),
1538a099482SMitchell Horne 	DB_CMD("call",		db_fncall,		CS_OWN),
154a305b20eSMitchell Horne 	DB_CMD("ps",		db_ps,			DB_CMD_MEMSAFE),
1558a099482SMitchell Horne 	DB_CMD("gdb",		db_gdb,			0),
156a305b20eSMitchell Horne 	DB_CMD("halt",		db_halt,		DB_CMD_MEMSAFE),
157a305b20eSMitchell Horne 	DB_CMD("reboot",	db_reset,		DB_CMD_MEMSAFE),
158a305b20eSMitchell Horne 	DB_CMD("reset",		db_reset,		DB_CMD_MEMSAFE),
159a305b20eSMitchell Horne 	DB_CMD("kill",		db_kill,		CS_OWN|DB_CMD_MEMSAFE),
160a305b20eSMitchell Horne 	DB_CMD("watchdog",	db_watchdog,		CS_OWN|DB_CMD_MEMSAFE),
1618a099482SMitchell Horne 	DB_CMD("thread",	db_set_thread,		0),
162a305b20eSMitchell Horne 	DB_CMD("run",		db_run_cmd,		CS_OWN|DB_CMD_MEMSAFE),
163a305b20eSMitchell Horne 	DB_CMD("script",	db_script_cmd,		CS_OWN|DB_CMD_MEMSAFE),
164a305b20eSMitchell Horne 	DB_CMD("scripts",	db_scripts_cmd,		DB_CMD_MEMSAFE),
165a305b20eSMitchell Horne 	DB_CMD("unscript",	db_unscript_cmd,	CS_OWN|DB_CMD_MEMSAFE),
166a305b20eSMitchell Horne 	DB_CMD("capture",	db_capture_cmd,		CS_OWN|DB_CMD_MEMSAFE),
167a305b20eSMitchell Horne 	DB_CMD("textdump",	db_textdump_cmd,	CS_OWN|DB_CMD_MEMSAFE),
1688a099482SMitchell Horne 	DB_CMD("findstack",	db_findstack_cmd,	0),
169cec9a4bfSDavid E. O'Brien };
1704ef7db5aSMitchell Horne struct db_command_table db_cmd_table = LIST_HEAD_INITIALIZER(db_cmd_table);
171e1e31c0eSJohn Baldwin 
1728a099482SMitchell Horne #undef DB_CMD
1738a099482SMitchell Horne #undef DB_TABLE
1748a099482SMitchell Horne 
1754ef7db5aSMitchell Horne static struct db_command *db_last_command = NULL;
1766337f4efSBruce Evans 
1775b81b6b3SRodney W. Grimes /*
1785b81b6b3SRodney W. Grimes  * if 'ed' style: 'dot' is set at start of last item printed,
1795b81b6b3SRodney W. Grimes  * and '+' points to next line.
1805b81b6b3SRodney W. Grimes  * Otherwise: 'dot' points to next item, '..' points to last.
1815b81b6b3SRodney W. Grimes  */
182cd508278SPedro F. Giffuni static bool	db_ed_style = true;
1835b81b6b3SRodney W. Grimes 
1845b81b6b3SRodney W. Grimes /*
1855b81b6b3SRodney W. Grimes  * Utility routine - discard tokens through end-of-line.
1865b81b6b3SRodney W. Grimes  */
1875b81b6b3SRodney W. Grimes void
188a41dd031SPedro F. Giffuni db_skip_to_eol(void)
1895b81b6b3SRodney W. Grimes {
1905b81b6b3SRodney W. Grimes 	int t;
1914f2ad624SMitchell Horne 
1925b81b6b3SRodney W. Grimes 	do {
1935b81b6b3SRodney W. Grimes 		t = db_read_token();
1945b81b6b3SRodney W. Grimes 	} while (t != tEOL);
1955b81b6b3SRodney W. Grimes }
1965b81b6b3SRodney W. Grimes 
1975b81b6b3SRodney W. Grimes /*
1985b81b6b3SRodney W. Grimes  * Results of command search.
1995b81b6b3SRodney W. Grimes  */
2005b81b6b3SRodney W. Grimes #define	CMD_UNIQUE	0
2015b81b6b3SRodney W. Grimes #define	CMD_FOUND	1
2025b81b6b3SRodney W. Grimes #define	CMD_NONE	2
2035b81b6b3SRodney W. Grimes #define	CMD_AMBIGUOUS	3
2045b81b6b3SRodney W. Grimes #define	CMD_HELP	4
2055b81b6b3SRodney W. Grimes 
2064ef7db5aSMitchell Horne static void	db_cmd_match(char *name, struct db_command *cmd,
2074ef7db5aSMitchell Horne 		    struct db_command **cmdp, int *resultp);
2084ef7db5aSMitchell Horne static void	db_cmd_list(struct db_command_table *table);
2094ef7db5aSMitchell Horne static int	db_cmd_search(char *name, struct db_command_table *table,
2104ef7db5aSMitchell Horne 		    struct db_command **cmdp);
2114ef7db5aSMitchell Horne static void	db_command(struct db_command **last_cmdp,
2124f2ad624SMitchell Horne 		    struct db_command_table *cmd_table, bool dopager);
213058284fcSBruce Evans 
2145b81b6b3SRodney W. Grimes /*
21539297ba4SSam Leffler  * Initialize the command lists from the static tables.
21639297ba4SSam Leffler  */
21793d4804bSJohn Baldwin void
21893d4804bSJohn Baldwin db_command_init(void)
21939297ba4SSam Leffler {
22039297ba4SSam Leffler 	int i;
22139297ba4SSam Leffler 
2224f2ad624SMitchell Horne 	for (i = 0; i < nitems(db_cmds); i++)
22339297ba4SSam Leffler 		db_command_register(&db_cmd_table, &db_cmds[i]);
2244f2ad624SMitchell Horne 	for (i = 0; i < nitems(db_show_cmds); i++)
22539297ba4SSam Leffler 		db_command_register(&db_show_table, &db_show_cmds[i]);
2264f2ad624SMitchell Horne 	for (i = 0; i < nitems(db_show_active_cmds); i++)
2277e89a322SConrad Meyer 		db_command_register(&db_show_active_table,
2287e89a322SConrad Meyer 		    &db_show_active_cmds[i]);
2294f2ad624SMitchell Horne 	for (i = 0; i < nitems(db_show_all_cmds); i++)
23039297ba4SSam Leffler 		db_command_register(&db_show_all_table, &db_show_all_cmds[i]);
23139297ba4SSam Leffler }
23239297ba4SSam Leffler 
23339297ba4SSam Leffler /*
23439297ba4SSam Leffler  * Register a command.
23539297ba4SSam Leffler  */
23639297ba4SSam Leffler void
2374ef7db5aSMitchell Horne db_command_register(struct db_command_table *list, struct db_command *cmd)
23839297ba4SSam Leffler {
2394ef7db5aSMitchell Horne 	struct db_command *c, *last;
24039297ba4SSam Leffler 
2412449b9e5SMitchell Horne #ifdef MAC
2422449b9e5SMitchell Horne 	if (mac_ddb_command_register(list, cmd)) {
2432449b9e5SMitchell Horne 		printf("%s: MAC policy refused registration of command %s\n",
2442449b9e5SMitchell Horne 		    __func__, cmd->name);
2452449b9e5SMitchell Horne 		return;
2462449b9e5SMitchell Horne 	}
2472449b9e5SMitchell Horne #endif
24839297ba4SSam Leffler 	last = NULL;
24939297ba4SSam Leffler 	LIST_FOREACH(c, list, next) {
25039297ba4SSam Leffler 		int n = strcmp(cmd->name, c->name);
25139297ba4SSam Leffler 
25239297ba4SSam Leffler 		/* Check that the command is not already present. */
25339297ba4SSam Leffler 		if (n == 0) {
25439297ba4SSam Leffler 			printf("%s: Warning, the command \"%s\" already exists;"
25539297ba4SSam Leffler 			     " ignoring request\n", __func__, cmd->name);
25639297ba4SSam Leffler 			return;
25739297ba4SSam Leffler 		}
25839297ba4SSam Leffler 		if (n < 0) {
25939297ba4SSam Leffler 			/* NB: keep list sorted lexicographically */
26039297ba4SSam Leffler 			LIST_INSERT_BEFORE(c, cmd, next);
26139297ba4SSam Leffler 			return;
26239297ba4SSam Leffler 		}
26339297ba4SSam Leffler 		last = c;
26439297ba4SSam Leffler 	}
26539297ba4SSam Leffler 	if (last == NULL)
26639297ba4SSam Leffler 		LIST_INSERT_HEAD(list, cmd, next);
26739297ba4SSam Leffler 	else
26839297ba4SSam Leffler 		LIST_INSERT_AFTER(last, cmd, next);
26939297ba4SSam Leffler }
27039297ba4SSam Leffler 
27139297ba4SSam Leffler /*
27239297ba4SSam Leffler  * Remove a command previously registered with db_command_register.
27339297ba4SSam Leffler  */
27439297ba4SSam Leffler void
2754ef7db5aSMitchell Horne db_command_unregister(struct db_command_table *list, struct db_command *cmd)
27639297ba4SSam Leffler {
2774ef7db5aSMitchell Horne 	struct db_command *c;
27839297ba4SSam Leffler 
27939297ba4SSam Leffler 	LIST_FOREACH(c, list, next) {
28039297ba4SSam Leffler 		if (cmd == c) {
28139297ba4SSam Leffler 			LIST_REMOVE(cmd, next);
28239297ba4SSam Leffler 			return;
28339297ba4SSam Leffler 		}
28439297ba4SSam Leffler 	}
28539297ba4SSam Leffler 	/* NB: intentionally quiet */
28639297ba4SSam Leffler }
28739297ba4SSam Leffler 
28839297ba4SSam Leffler /*
289e1e31c0eSJohn Baldwin  * Helper function to match a single command.
2905b81b6b3SRodney W. Grimes  */
291e1e31c0eSJohn Baldwin static void
2924ef7db5aSMitchell Horne db_cmd_match(char *name, struct db_command *cmd, struct db_command **cmdp,
293a41dd031SPedro F. Giffuni     int *resultp)
294e1e31c0eSJohn Baldwin {
295e1e31c0eSJohn Baldwin 	char *lp, *rp;
296e1e31c0eSJohn Baldwin 	int c;
2975b81b6b3SRodney W. Grimes 
2985b81b6b3SRodney W. Grimes 	lp = name;
2995b81b6b3SRodney W. Grimes 	rp = cmd->name;
3005b81b6b3SRodney W. Grimes 	while ((c = *lp) == *rp) {
3015b81b6b3SRodney W. Grimes 		if (c == 0) {
3025b81b6b3SRodney W. Grimes 			/* complete match */
3035b81b6b3SRodney W. Grimes 			*cmdp = cmd;
304e1e31c0eSJohn Baldwin 			*resultp = CMD_UNIQUE;
305e1e31c0eSJohn Baldwin 			return;
3065b81b6b3SRodney W. Grimes 		}
3075b81b6b3SRodney W. Grimes 		lp++;
3085b81b6b3SRodney W. Grimes 		rp++;
3095b81b6b3SRodney W. Grimes 	}
3105b81b6b3SRodney W. Grimes 	if (c == 0) {
3115b81b6b3SRodney W. Grimes 		/* end of name, not end of command -
3125b81b6b3SRodney W. Grimes 		   partial match */
313e1e31c0eSJohn Baldwin 		if (*resultp == CMD_FOUND) {
314e1e31c0eSJohn Baldwin 			*resultp = CMD_AMBIGUOUS;
3155b81b6b3SRodney W. Grimes 			/* but keep looking for a full match -
3165b81b6b3SRodney W. Grimes 			   this lets us match single letters */
317d85c9cefSRyan Libby 		} else if (*resultp == CMD_NONE) {
3185b81b6b3SRodney W. Grimes 			*cmdp = cmd;
319e1e31c0eSJohn Baldwin 			*resultp = CMD_FOUND;
3205b81b6b3SRodney W. Grimes 		}
3215b81b6b3SRodney W. Grimes 	}
3225b81b6b3SRodney W. Grimes }
3236337f4efSBruce Evans 
324e1e31c0eSJohn Baldwin /*
325e1e31c0eSJohn Baldwin  * Search for command prefix.
326e1e31c0eSJohn Baldwin  */
327e1e31c0eSJohn Baldwin static int
3284ef7db5aSMitchell Horne db_cmd_search(char *name, struct db_command_table *table,
3294ef7db5aSMitchell Horne     struct db_command **cmdp)
330e1e31c0eSJohn Baldwin {
3314ef7db5aSMitchell Horne 	struct db_command *cmd;
332e1e31c0eSJohn Baldwin 	int result = CMD_NONE;
333e1e31c0eSJohn Baldwin 
33439297ba4SSam Leffler 	LIST_FOREACH(cmd, table, next) {
335e1e31c0eSJohn Baldwin 		db_cmd_match(name,cmd,cmdp,&result);
336e1e31c0eSJohn Baldwin 		if (result == CMD_UNIQUE)
33739297ba4SSam Leffler 			break;
3386337f4efSBruce Evans 	}
33939297ba4SSam Leffler 
3405b81b6b3SRodney W. Grimes 	if (result == CMD_NONE) {
3415b81b6b3SRodney W. Grimes 		/* check for 'help' */
3425b81b6b3SRodney W. Grimes 		if (name[0] == 'h' && name[1] == 'e'
3435b81b6b3SRodney W. Grimes 		    && name[2] == 'l' && name[3] == 'p')
3445b81b6b3SRodney W. Grimes 			result = CMD_HELP;
3455b81b6b3SRodney W. Grimes 	}
3465b81b6b3SRodney W. Grimes 	return (result);
3475b81b6b3SRodney W. Grimes }
3485b81b6b3SRodney W. Grimes 
349f73a856dSPoul-Henning Kamp static void
3504ef7db5aSMitchell Horne db_cmd_list(struct db_command_table *table)
3515b81b6b3SRodney W. Grimes {
3524ef7db5aSMitchell Horne 	struct db_command *cmd;
353b5bd6c73SEdward Tomasz Napierala 	int have_subcommands;
3545b81b6b3SRodney W. Grimes 
355b5bd6c73SEdward Tomasz Napierala 	have_subcommands = 0;
35639297ba4SSam Leffler 	LIST_FOREACH(cmd, table, next) {
357b5bd6c73SEdward Tomasz Napierala 		if (cmd->more != NULL)
358b5bd6c73SEdward Tomasz Napierala 			have_subcommands++;
359527be4f2SJohn-Mark Gurney 		db_printf("%-16s", cmd->name);
360527be4f2SJohn-Mark Gurney 		db_end_line(16);
3615b81b6b3SRodney W. Grimes 	}
362b5bd6c73SEdward Tomasz Napierala 
363b5bd6c73SEdward Tomasz Napierala 	if (have_subcommands > 0) {
364b5bd6c73SEdward Tomasz Napierala 		db_printf("\nThe following have subcommands; append \"help\" "
365b5bd6c73SEdward Tomasz Napierala 		    "to list (e.g. \"show help\"):\n");
366b5bd6c73SEdward Tomasz Napierala 		LIST_FOREACH(cmd, table, next) {
367b5bd6c73SEdward Tomasz Napierala 			if (cmd->more == NULL)
368b5bd6c73SEdward Tomasz Napierala 				continue;
369b5bd6c73SEdward Tomasz Napierala 			db_printf("%-16s", cmd->name);
370b5bd6c73SEdward Tomasz Napierala 			db_end_line(16);
371b5bd6c73SEdward Tomasz Napierala 		}
372b5bd6c73SEdward Tomasz Napierala 	}
3735b81b6b3SRodney W. Grimes }
3745b81b6b3SRodney W. Grimes 
375f73a856dSPoul-Henning Kamp static void
3764ef7db5aSMitchell Horne db_command(struct db_command **last_cmdp, struct db_command_table *cmd_table,
3774f2ad624SMitchell Horne     bool dopager)
3785b81b6b3SRodney W. Grimes {
3795b81b6b3SRodney W. Grimes 	char modif[TOK_STRING_SIZE];
3804f2ad624SMitchell Horne 	struct db_command *cmd = NULL;
3815b81b6b3SRodney W. Grimes 	db_expr_t addr, count;
3824f2ad624SMitchell Horne 	int t, result;
383cd508278SPedro F. Giffuni 	bool have_addr = false;
3845b81b6b3SRodney W. Grimes 
3855b81b6b3SRodney W. Grimes 	t = db_read_token();
3865b81b6b3SRodney W. Grimes 	if (t == tEOL) {
3875b81b6b3SRodney W. Grimes 		/* empty line repeats last command, at 'next' */
3885b81b6b3SRodney W. Grimes 		cmd = *last_cmdp;
3895b81b6b3SRodney W. Grimes 		addr = (db_expr_t)db_next;
3902b490bc7SPedro F. Giffuni 		have_addr = false;
3915b81b6b3SRodney W. Grimes 		count = 1;
3925b81b6b3SRodney W. Grimes 		modif[0] = '\0';
3934f2ad624SMitchell Horne 	} else if (t == tEXCL) {
3944f2ad624SMitchell Horne 		db_fncall((db_expr_t)0, false, (db_expr_t)0, NULL);
3955b81b6b3SRodney W. Grimes 		return;
3964f2ad624SMitchell Horne 	} else if (t != tIDENT) {
3979990da25SEdward Tomasz Napierala 		db_printf("Unrecognized input; use \"help\" "
3989990da25SEdward Tomasz Napierala 	            "to list available commands\n");
3995b81b6b3SRodney W. Grimes 		db_flush_lex();
4005b81b6b3SRodney W. Grimes 		return;
4014f2ad624SMitchell Horne 	} else {
4025b81b6b3SRodney W. Grimes 		/*
4035b81b6b3SRodney W. Grimes 		 * Search for command
4045b81b6b3SRodney W. Grimes 		 */
4054f2ad624SMitchell Horne 		while (cmd_table != NULL) {
4064f2ad624SMitchell Horne 			result = db_cmd_search(db_tok_string, cmd_table, &cmd);
4075b81b6b3SRodney W. Grimes 			switch (result) {
4085b81b6b3SRodney W. Grimes 			case CMD_NONE:
409b5bd6c73SEdward Tomasz Napierala 				db_printf("No such command; use \"help\" "
410b5bd6c73SEdward Tomasz Napierala 				    "to list available commands\n");
4115b81b6b3SRodney W. Grimes 				db_flush_lex();
4125b81b6b3SRodney W. Grimes 				return;
4135b81b6b3SRodney W. Grimes 			case CMD_AMBIGUOUS:
4145b81b6b3SRodney W. Grimes 				db_printf("Ambiguous\n");
4155b81b6b3SRodney W. Grimes 				db_flush_lex();
4165b81b6b3SRodney W. Grimes 				return;
4175b81b6b3SRodney W. Grimes 			case CMD_HELP:
418b5bd6c73SEdward Tomasz Napierala 				if (cmd_table == &db_cmd_table) {
419b5bd6c73SEdward Tomasz Napierala 					db_printf("This is ddb(4), the kernel debugger; "
420c6404e66SGavin Atkinson 					    "see https://man.FreeBSD.org/ddb/4 for help.\n");
421b5bd6c73SEdward Tomasz Napierala 					db_printf("Use \"bt\" for backtrace, \"dump\" for "
422b5bd6c73SEdward Tomasz Napierala 					    "kernel core dump, \"reset\" to reboot.\n");
423b5bd6c73SEdward Tomasz Napierala 					db_printf("Available commands:\n");
424b5bd6c73SEdward Tomasz Napierala 				}
425e1e31c0eSJohn Baldwin 				db_cmd_list(cmd_table);
4265b81b6b3SRodney W. Grimes 				db_flush_lex();
4275b81b6b3SRodney W. Grimes 				return;
4284f2ad624SMitchell Horne 			case CMD_UNIQUE:
4294f2ad624SMitchell Horne 			case CMD_FOUND:
4305b81b6b3SRodney W. Grimes 				break;
4315b81b6b3SRodney W. Grimes 			}
432e1e31c0eSJohn Baldwin 			if ((cmd_table = cmd->more) != NULL) {
4335b81b6b3SRodney W. Grimes 				t = db_read_token();
4345b81b6b3SRodney W. Grimes 				if (t != tIDENT) {
435b5bd6c73SEdward Tomasz Napierala 					db_printf("Subcommand required; "
436b5bd6c73SEdward Tomasz Napierala 					    "available subcommands:\n");
437e1e31c0eSJohn Baldwin 					db_cmd_list(cmd_table);
4385b81b6b3SRodney W. Grimes 					db_flush_lex();
4395b81b6b3SRodney W. Grimes 					return;
4405b81b6b3SRodney W. Grimes 				}
4415b81b6b3SRodney W. Grimes 			}
4425b81b6b3SRodney W. Grimes 		}
4435b81b6b3SRodney W. Grimes 
4445b81b6b3SRodney W. Grimes 		if ((cmd->flag & CS_OWN) == 0) {
4455b81b6b3SRodney W. Grimes 			/*
4465b81b6b3SRodney W. Grimes 			 * Standard syntax:
4475b81b6b3SRodney W. Grimes 			 * command [/modifier] [addr] [,count]
4485b81b6b3SRodney W. Grimes 			 */
4495b81b6b3SRodney W. Grimes 			t = db_read_token();
4505b81b6b3SRodney W. Grimes 			if (t == tSLASH) {
4515b81b6b3SRodney W. Grimes 				t = db_read_token();
4525b81b6b3SRodney W. Grimes 				if (t != tIDENT) {
4535b81b6b3SRodney W. Grimes 					db_printf("Bad modifier\n");
4545b81b6b3SRodney W. Grimes 					db_flush_lex();
4555b81b6b3SRodney W. Grimes 					return;
4565b81b6b3SRodney W. Grimes 				}
4575b81b6b3SRodney W. Grimes 				db_strcpy(modif, db_tok_string);
4584f2ad624SMitchell Horne 			} else {
4595b81b6b3SRodney W. Grimes 				db_unread_token(t);
4605b81b6b3SRodney W. Grimes 				modif[0] = '\0';
4615b81b6b3SRodney W. Grimes 			}
4625b81b6b3SRodney W. Grimes 
4635b81b6b3SRodney W. Grimes 			if (db_expression(&addr)) {
4645b81b6b3SRodney W. Grimes 				db_dot = (db_addr_t) addr;
4655b81b6b3SRodney W. Grimes 				db_last_addr = db_dot;
4662b490bc7SPedro F. Giffuni 				have_addr = true;
4674f2ad624SMitchell Horne 			} else {
4685b81b6b3SRodney W. Grimes 				addr = (db_expr_t) db_dot;
4692b490bc7SPedro F. Giffuni 				have_addr = false;
4705b81b6b3SRodney W. Grimes 			}
4714f2ad624SMitchell Horne 
4725b81b6b3SRodney W. Grimes 			t = db_read_token();
4735b81b6b3SRodney W. Grimes 			if (t == tCOMMA) {
4745b81b6b3SRodney W. Grimes 				if (!db_expression(&count)) {
4755b81b6b3SRodney W. Grimes 					db_printf("Count missing\n");
4765b81b6b3SRodney W. Grimes 					db_flush_lex();
4775b81b6b3SRodney W. Grimes 					return;
4785b81b6b3SRodney W. Grimes 				}
4794f2ad624SMitchell Horne 			} else {
4805b81b6b3SRodney W. Grimes 				db_unread_token(t);
4815b81b6b3SRodney W. Grimes 				count = -1;
4825b81b6b3SRodney W. Grimes 			}
4834f2ad624SMitchell Horne 
4845b81b6b3SRodney W. Grimes 			if ((cmd->flag & CS_MORE) == 0) {
4855b81b6b3SRodney W. Grimes 				db_skip_to_eol();
4865b81b6b3SRodney W. Grimes 			}
4875b81b6b3SRodney W. Grimes 		}
4885b81b6b3SRodney W. Grimes 	}
4894f2ad624SMitchell Horne 
4905b81b6b3SRodney W. Grimes 	*last_cmdp = cmd;
4919f915a92SPedro F. Giffuni 	if (cmd != NULL) {
4922449b9e5SMitchell Horne #ifdef MAC
4932449b9e5SMitchell Horne 		if (mac_ddb_command_exec(cmd, addr, have_addr, count, modif)) {
4942449b9e5SMitchell Horne 			db_printf("MAC prevented execution of command %s\n",
4952449b9e5SMitchell Horne 			    cmd->name);
4962449b9e5SMitchell Horne 			return;
4972449b9e5SMitchell Horne 		}
4982449b9e5SMitchell Horne #endif
4995b81b6b3SRodney W. Grimes 		/*
5005b81b6b3SRodney W. Grimes 		 * Execute the command.
5015b81b6b3SRodney W. Grimes 		 */
502c9b0cc3bSRobert Watson 		if (dopager)
50319e9205aSJohn Baldwin 			db_enable_pager();
504c9b0cc3bSRobert Watson 		else
505c9b0cc3bSRobert Watson 			db_disable_pager();
5065b81b6b3SRodney W. Grimes 		(*cmd->fcn)(addr, have_addr, count, modif);
507c9b0cc3bSRobert Watson 		if (dopager)
50819e9205aSJohn Baldwin 			db_disable_pager();
5095b81b6b3SRodney W. Grimes 
5105b81b6b3SRodney W. Grimes 		if (cmd->flag & CS_SET_DOT) {
5115b81b6b3SRodney W. Grimes 			/*
5124f2ad624SMitchell Horne 			 * If command changes dot, set dot to previous address
5134f2ad624SMitchell Horne 			 * displayed (if 'ed' style).
5145b81b6b3SRodney W. Grimes 			 */
5154f2ad624SMitchell Horne 			db_dot = db_ed_style ? db_prev : db_next;
5164f2ad624SMitchell Horne 		} else {
5175b81b6b3SRodney W. Grimes 			/*
5184f2ad624SMitchell Horne 			 * If command does not change dot, set 'next' location
5194f2ad624SMitchell Horne 			 * to be the same.
5205b81b6b3SRodney W. Grimes 			 */
5215b81b6b3SRodney W. Grimes 			db_next = db_dot;
5225b81b6b3SRodney W. Grimes 		}
5235b81b6b3SRodney W. Grimes 	}
5245b81b6b3SRodney W. Grimes }
5255b81b6b3SRodney W. Grimes 
5265b81b6b3SRodney W. Grimes /*
527b7aa38c1SBruce Evans  * At least one non-optional command must be implemented using
528b7aa38c1SBruce Evans  * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
529b7aa38c1SBruce Evans  */
530c84c5e00SMitchell Horne DB_COMMAND_FLAGS(panic, db_panic, DB_CMD_MEMSAFE)
5313eac4884SPoul-Henning Kamp {
53215cc91d3SJohn Baldwin 	db_disable_pager();
533edf8a815SDavid Greenman 	panic("from debugger");
5343eac4884SPoul-Henning Kamp }
5353eac4884SPoul-Henning Kamp 
5363eac4884SPoul-Henning Kamp void
537a41dd031SPedro F. Giffuni db_command_loop(void)
5385b81b6b3SRodney W. Grimes {
5395b81b6b3SRodney W. Grimes 	/*
5405b81b6b3SRodney W. Grimes 	 * Initialize 'prev' and 'next' to dot.
5415b81b6b3SRodney W. Grimes 	 */
5425b81b6b3SRodney W. Grimes 	db_prev = db_dot;
5435b81b6b3SRodney W. Grimes 	db_next = db_dot;
5445b81b6b3SRodney W. Grimes 
5455b81b6b3SRodney W. Grimes 	db_cmd_loop_done = 0;
5465b81b6b3SRodney W. Grimes 	while (!db_cmd_loop_done) {
5475b81b6b3SRodney W. Grimes 		if (db_print_position() != 0)
5485b81b6b3SRodney W. Grimes 			db_printf("\n");
5495b81b6b3SRodney W. Grimes 
5505b81b6b3SRodney W. Grimes 		db_printf("db> ");
5515b81b6b3SRodney W. Grimes 		(void)db_read_line();
5525b81b6b3SRodney W. Grimes 
5534f2ad624SMitchell Horne 		db_command(&db_last_command, &db_cmd_table, /* dopager */ true);
5545b81b6b3SRodney W. Grimes 	}
5555b81b6b3SRodney W. Grimes }
5565b81b6b3SRodney W. Grimes 
557c9b0cc3bSRobert Watson /*
558c9b0cc3bSRobert Watson  * Execute a command on behalf of a script.  The caller is responsible for
559c9b0cc3bSRobert Watson  * making sure that the command string is < DB_MAXLINE or it will be
560c9b0cc3bSRobert Watson  * truncated.
561c9b0cc3bSRobert Watson  *
562c9b0cc3bSRobert Watson  * XXXRW: Runs by injecting faked input into DDB input stream; it would be
563c9b0cc3bSRobert Watson  * nicer to use an alternative approach that didn't mess with the previous
564c9b0cc3bSRobert Watson  * command buffer.
565c9b0cc3bSRobert Watson  */
566c9b0cc3bSRobert Watson void
567c9b0cc3bSRobert Watson db_command_script(const char *command)
568c9b0cc3bSRobert Watson {
569c9b0cc3bSRobert Watson 	db_prev = db_next = db_dot;
570c9b0cc3bSRobert Watson 	db_inject_line(command);
5714f2ad624SMitchell Horne 	db_command(&db_last_command, &db_cmd_table, /* dopager */ false);
572c9b0cc3bSRobert Watson }
573c9b0cc3bSRobert Watson 
5745b81b6b3SRodney W. Grimes void
575a41dd031SPedro F. Giffuni db_error(const char *s)
5765b81b6b3SRodney W. Grimes {
5775b81b6b3SRodney W. Grimes 	if (s)
57858d9a059SKris Kennaway 	    db_printf("%s", s);
5795b81b6b3SRodney W. Grimes 	db_flush_lex();
580212ff84fSEdward Tomasz Napierala 	kdb_reenter_silent();
5815b81b6b3SRodney W. Grimes }
5825b81b6b3SRodney W. Grimes 
583299cceefSMarcel Moolenaar static void
584cd508278SPedro F. Giffuni db_dump(db_expr_t dummy, bool dummy2, db_expr_t dummy3, char *dummy4)
585299cceefSMarcel Moolenaar {
586299cceefSMarcel Moolenaar 	int error;
587299cceefSMarcel Moolenaar 
58821d748a9SAlfred Perlstein 	if (textdump_pending) {
58921d748a9SAlfred Perlstein 		db_printf("textdump_pending set.\n"
59021d748a9SAlfred Perlstein 		    "run \"textdump unset\" first or \"textdump dump\" for a textdump.\n");
59121d748a9SAlfred Perlstein 		return;
59221d748a9SAlfred Perlstein 	}
5932b490bc7SPedro F. Giffuni 	error = doadump(false);
594299cceefSMarcel Moolenaar 	if (error) {
595299cceefSMarcel Moolenaar 		db_printf("Cannot dump: ");
596299cceefSMarcel Moolenaar 		switch (error) {
597299cceefSMarcel Moolenaar 		case EBUSY:
598299cceefSMarcel Moolenaar 			db_printf("debugger got invoked while dumping.\n");
599299cceefSMarcel Moolenaar 			break;
600299cceefSMarcel Moolenaar 		case ENXIO:
601299cceefSMarcel Moolenaar 			db_printf("no dump device specified.\n");
602299cceefSMarcel Moolenaar 			break;
603299cceefSMarcel Moolenaar 		default:
604299cceefSMarcel Moolenaar 			db_printf("unknown error (error=%d).\n", error);
605299cceefSMarcel Moolenaar 			break;
606299cceefSMarcel Moolenaar 		}
607299cceefSMarcel Moolenaar 	}
608299cceefSMarcel Moolenaar }
6095b81b6b3SRodney W. Grimes 
6105b81b6b3SRodney W. Grimes /*
6115b81b6b3SRodney W. Grimes  * Call random function:
6125b81b6b3SRodney W. Grimes  * !expr(arg,arg,arg)
6135b81b6b3SRodney W. Grimes  */
614a2aeb24eSMarcel Moolenaar 
615a2aeb24eSMarcel Moolenaar /* The generic implementation supports a maximum of 10 arguments. */
616a2aeb24eSMarcel Moolenaar typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
617a2aeb24eSMarcel Moolenaar     db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
618a2aeb24eSMarcel Moolenaar 
619a2aeb24eSMarcel Moolenaar static __inline int
620a2aeb24eSMarcel Moolenaar db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
621a2aeb24eSMarcel Moolenaar {
622a2aeb24eSMarcel Moolenaar 	__db_f *f = (__db_f *)addr;
623a2aeb24eSMarcel Moolenaar 
624a2aeb24eSMarcel Moolenaar 	if (nargs > 10) {
625a2aeb24eSMarcel Moolenaar 		db_printf("Too many arguments (max 10)\n");
626a2aeb24eSMarcel Moolenaar 		return (0);
627a2aeb24eSMarcel Moolenaar 	}
628a2aeb24eSMarcel Moolenaar 	*rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
629a2aeb24eSMarcel Moolenaar 	    args[6], args[7], args[8], args[9]);
630a2aeb24eSMarcel Moolenaar 	return (1);
631a2aeb24eSMarcel Moolenaar }
632a2aeb24eSMarcel Moolenaar 
633f73a856dSPoul-Henning Kamp static void
634cd508278SPedro F. Giffuni db_fncall(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
6355b81b6b3SRodney W. Grimes {
6365b81b6b3SRodney W. Grimes 	db_expr_t	fn_addr;
637a2aeb24eSMarcel Moolenaar 	db_expr_t	args[DB_MAXARGS];
6385b81b6b3SRodney W. Grimes 	int		nargs = 0;
6395b81b6b3SRodney W. Grimes 	db_expr_t	retval;
6405b81b6b3SRodney W. Grimes 	int		t;
6415b81b6b3SRodney W. Grimes 
6425b81b6b3SRodney W. Grimes 	if (!db_expression(&fn_addr)) {
6435b81b6b3SRodney W. Grimes 	    db_printf("Bad function\n");
6445b81b6b3SRodney W. Grimes 	    db_flush_lex();
6455b81b6b3SRodney W. Grimes 	    return;
6465b81b6b3SRodney W. Grimes 	}
6475b81b6b3SRodney W. Grimes 
6485b81b6b3SRodney W. Grimes 	t = db_read_token();
6495b81b6b3SRodney W. Grimes 	if (t == tLPAREN) {
6505b81b6b3SRodney W. Grimes 	    if (db_expression(&args[0])) {
6515b81b6b3SRodney W. Grimes 		nargs++;
6525b81b6b3SRodney W. Grimes 		while ((t = db_read_token()) == tCOMMA) {
653a2aeb24eSMarcel Moolenaar 		    if (nargs == DB_MAXARGS) {
654a2aeb24eSMarcel Moolenaar 			db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
6555b81b6b3SRodney W. Grimes 			db_flush_lex();
6565b81b6b3SRodney W. Grimes 			return;
6575b81b6b3SRodney W. Grimes 		    }
6585b81b6b3SRodney W. Grimes 		    if (!db_expression(&args[nargs])) {
6595b81b6b3SRodney W. Grimes 			db_printf("Argument missing\n");
6605b81b6b3SRodney W. Grimes 			db_flush_lex();
6615b81b6b3SRodney W. Grimes 			return;
6625b81b6b3SRodney W. Grimes 		    }
6635b81b6b3SRodney W. Grimes 		    nargs++;
6645b81b6b3SRodney W. Grimes 		}
6655b81b6b3SRodney W. Grimes 		db_unread_token(t);
6665b81b6b3SRodney W. Grimes 	    }
6675b81b6b3SRodney W. Grimes 	    if (db_read_token() != tRPAREN) {
6689990da25SEdward Tomasz Napierala 	        db_printf("Mismatched parens\n");
6695b81b6b3SRodney W. Grimes 		db_flush_lex();
6705b81b6b3SRodney W. Grimes 		return;
6715b81b6b3SRodney W. Grimes 	    }
6725b81b6b3SRodney W. Grimes 	}
6735b81b6b3SRodney W. Grimes 	db_skip_to_eol();
67415cc91d3SJohn Baldwin 	db_disable_pager();
6755b81b6b3SRodney W. Grimes 
676a2aeb24eSMarcel Moolenaar 	if (DB_CALL(fn_addr, &retval, nargs, args))
677a2aeb24eSMarcel Moolenaar 		db_printf("= %#lr\n", (long)retval);
6785b81b6b3SRodney W. Grimes }
679ad146781SPaul Traina 
6805370c3b6SPeter Wemm static void
681cd508278SPedro F. Giffuni db_halt(db_expr_t dummy, bool dummy2, db_expr_t dummy3, char *dummy4)
682e4b732cfSBruce Evans {
683e4b732cfSBruce Evans 
684e4b732cfSBruce Evans 	cpu_halt();
685e4b732cfSBruce Evans }
686e4b732cfSBruce Evans 
687e4b732cfSBruce Evans static void
688cd508278SPedro F. Giffuni db_kill(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
68919d2c78fSDima Dorfman {
69019d2c78fSDima Dorfman 	db_expr_t old_radix, pid, sig;
69119d2c78fSDima Dorfman 	struct proc *p;
69219d2c78fSDima Dorfman 
69319d2c78fSDima Dorfman #define	DB_ERROR(f)	do { db_printf f; db_flush_lex(); goto out; } while (0)
69419d2c78fSDima Dorfman 
69519d2c78fSDima Dorfman 	/*
69619d2c78fSDima Dorfman 	 * PIDs and signal numbers are typically represented in base
69719d2c78fSDima Dorfman 	 * 10, so make that the default here.  It can, of course, be
69819d2c78fSDima Dorfman 	 * overridden by specifying a prefix.
69919d2c78fSDima Dorfman 	 */
70019d2c78fSDima Dorfman 	old_radix = db_radix;
70119d2c78fSDima Dorfman 	db_radix = 10;
70219d2c78fSDima Dorfman 	/* Retrieve arguments. */
70319d2c78fSDima Dorfman 	if (!db_expression(&sig))
70419d2c78fSDima Dorfman 		DB_ERROR(("Missing signal number\n"));
70519d2c78fSDima Dorfman 	if (!db_expression(&pid))
70619d2c78fSDima Dorfman 		DB_ERROR(("Missing process ID\n"));
70719d2c78fSDima Dorfman 	db_skip_to_eol();
708a29af74bSKonstantin Belousov 	if (!_SIG_VALID(sig))
70919d2c78fSDima Dorfman 		DB_ERROR(("Signal number out of range\n"));
71019d2c78fSDima Dorfman 
71119d2c78fSDima Dorfman 	/*
71219d2c78fSDima Dorfman 	 * Find the process in question.  allproc_lock is not needed
71319d2c78fSDima Dorfman 	 * since we're in DDB.
71419d2c78fSDima Dorfman 	 */
71519d2c78fSDima Dorfman 	/* sx_slock(&allproc_lock); */
716f67af5c9SXin LI 	FOREACH_PROC_IN_SYSTEM(p)
71719d2c78fSDima Dorfman 	    if (p->p_pid == pid)
71819d2c78fSDima Dorfman 		    break;
71919d2c78fSDima Dorfman 	/* sx_sunlock(&allproc_lock); */
72019d2c78fSDima Dorfman 	if (p == NULL)
721e30e3e9eSMatt Jacob 		DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
72219d2c78fSDima Dorfman 
72319d2c78fSDima Dorfman 	/* If it's already locked, bail; otherwise, do the deed. */
72419d2c78fSDima Dorfman 	if (PROC_TRYLOCK(p) == 0)
725e30e3e9eSMatt Jacob 		DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
72619d2c78fSDima Dorfman 	else {
727a3de221dSKonstantin Belousov 		pksignal(p, sig, NULL);
72819d2c78fSDima Dorfman 		PROC_UNLOCK(p);
72919d2c78fSDima Dorfman 	}
73019d2c78fSDima Dorfman 
73119d2c78fSDima Dorfman out:
73219d2c78fSDima Dorfman 	db_radix = old_radix;
73319d2c78fSDima Dorfman #undef DB_ERROR
73419d2c78fSDima Dorfman }
73519d2c78fSDima Dorfman 
7360f59fbc3SBjoern A. Zeeb /*
7370f59fbc3SBjoern A. Zeeb  * Reboot.  In case there is an additional argument, take it as delay in
7380f59fbc3SBjoern A. Zeeb  * seconds.  Default to 15s if we cannot parse it and make sure we will
7390f59fbc3SBjoern A. Zeeb  * never wait longer than 1 week.  Some code is similar to
7400f59fbc3SBjoern A. Zeeb  * kern_shutdown.c:shutdown_panic().
7410f59fbc3SBjoern A. Zeeb  */
7420f59fbc3SBjoern A. Zeeb #ifndef	DB_RESET_MAXDELAY
7430f59fbc3SBjoern A. Zeeb #define	DB_RESET_MAXDELAY	(3600 * 24 * 7)
7440f59fbc3SBjoern A. Zeeb #endif
7450f59fbc3SBjoern A. Zeeb 
74619d2c78fSDima Dorfman static void
747cd508278SPedro F. Giffuni db_reset(db_expr_t addr, bool have_addr, db_expr_t count __unused,
7480f59fbc3SBjoern A. Zeeb     char *modif __unused)
7495370c3b6SPeter Wemm {
7500f59fbc3SBjoern A. Zeeb 	int delay, loop;
7510f59fbc3SBjoern A. Zeeb 
7520f59fbc3SBjoern A. Zeeb 	if (have_addr) {
7530f59fbc3SBjoern A. Zeeb 		delay = (int)db_hex2dec(addr);
7540f59fbc3SBjoern A. Zeeb 
7550f59fbc3SBjoern A. Zeeb 		/* If we parse to fail, use 15s. */
7560f59fbc3SBjoern A. Zeeb 		if (delay == -1)
7570f59fbc3SBjoern A. Zeeb 			delay = 15;
7580f59fbc3SBjoern A. Zeeb 
7590f59fbc3SBjoern A. Zeeb 		/* Cap at one week. */
7600f59fbc3SBjoern A. Zeeb 		if ((uintmax_t)delay > (uintmax_t)DB_RESET_MAXDELAY)
7610f59fbc3SBjoern A. Zeeb 			delay = DB_RESET_MAXDELAY;
7620f59fbc3SBjoern A. Zeeb 
7630f59fbc3SBjoern A. Zeeb 		db_printf("Automatic reboot in %d seconds - "
7640f59fbc3SBjoern A. Zeeb 		    "press a key on the console to abort\n", delay);
7650f59fbc3SBjoern A. Zeeb 		for (loop = delay * 10; loop > 0; --loop) {
7660f59fbc3SBjoern A. Zeeb 			DELAY(1000 * 100); /* 1/10th second */
7670f59fbc3SBjoern A. Zeeb 			/* Did user type a key? */
7680f59fbc3SBjoern A. Zeeb 			if (cncheckc() != -1)
7690f59fbc3SBjoern A. Zeeb 				return;
7700f59fbc3SBjoern A. Zeeb 		}
7710f59fbc3SBjoern A. Zeeb 	}
7725370c3b6SPeter Wemm 
7735370c3b6SPeter Wemm 	cpu_reset();
7745370c3b6SPeter Wemm }
77574cc032bSPoul-Henning Kamp 
77674cc032bSPoul-Henning Kamp static void
777cd508278SPedro F. Giffuni db_watchdog(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
77874cc032bSPoul-Henning Kamp {
7798b927d7bSAttilio Rao 	db_expr_t old_radix, tout;
7808b927d7bSAttilio Rao 	int err, i;
7818b927d7bSAttilio Rao 
7828b927d7bSAttilio Rao 	old_radix = db_radix;
7838b927d7bSAttilio Rao 	db_radix = 10;
7848b927d7bSAttilio Rao 	err = db_expression(&tout);
7858b927d7bSAttilio Rao 	db_skip_to_eol();
7868b927d7bSAttilio Rao 	db_radix = old_radix;
7878b927d7bSAttilio Rao 
7888b927d7bSAttilio Rao 	/* If no argument is provided the watchdog will just be disabled. */
7898b927d7bSAttilio Rao 	if (err == 0) {
7908b927d7bSAttilio Rao 		db_printf("No argument provided, disabling watchdog\n");
7918b927d7bSAttilio Rao 		tout = 0;
7928b927d7bSAttilio Rao 	} else if ((tout & WD_INTERVAL) == WD_TO_NEVER) {
7938b927d7bSAttilio Rao 		db_error("Out of range watchdog interval\n");
7948b927d7bSAttilio Rao 		return;
7958b927d7bSAttilio Rao 	}
7968b927d7bSAttilio Rao 	EVENTHANDLER_INVOKE(watchdog_list, tout, &i);
79774cc032bSPoul-Henning Kamp }
798f3be7cb3SMarcel Moolenaar 
799f3be7cb3SMarcel Moolenaar static void
800cd508278SPedro F. Giffuni db_gdb(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
801f3be7cb3SMarcel Moolenaar {
802f3be7cb3SMarcel Moolenaar 
8033a5d3671SMatthew D Fleming 	if (kdb_dbbe_select("gdb") != 0) {
804f3be7cb3SMarcel Moolenaar 		db_printf("The remote GDB backend could not be selected.\n");
8053a5d3671SMatthew D Fleming 		return;
8063a5d3671SMatthew D Fleming 	}
8073a5d3671SMatthew D Fleming 	/*
8083a5d3671SMatthew D Fleming 	 * Mark that we are done in the debugger.  kdb_trap()
8093a5d3671SMatthew D Fleming 	 * should re-enter with the new backend.
8103a5d3671SMatthew D Fleming 	 */
8113a5d3671SMatthew D Fleming 	db_cmd_loop_done = 1;
8123a5d3671SMatthew D Fleming 	db_printf("(ctrl-c will return control to ddb)\n");
813f3be7cb3SMarcel Moolenaar }
814fd32d93bSMarcel Moolenaar 
815fd32d93bSMarcel Moolenaar static void
816cd508278SPedro F. Giffuni db_stack_trace(db_expr_t tid, bool hastid, db_expr_t count, char *modif)
817fd32d93bSMarcel Moolenaar {
818fd32d93bSMarcel Moolenaar 	struct thread *td;
819fd32d93bSMarcel Moolenaar 	db_expr_t radix;
820a13aca1aSRobert Watson 	pid_t pid;
821fd32d93bSMarcel Moolenaar 	int t;
822fd32d93bSMarcel Moolenaar 
823fd32d93bSMarcel Moolenaar 	/*
824fd32d93bSMarcel Moolenaar 	 * We parse our own arguments. We don't like the default radix.
825fd32d93bSMarcel Moolenaar 	 */
826fd32d93bSMarcel Moolenaar 	radix = db_radix;
827fd32d93bSMarcel Moolenaar 	db_radix = 10;
828fd32d93bSMarcel Moolenaar 	hastid = db_expression(&tid);
829fd32d93bSMarcel Moolenaar 	t = db_read_token();
830fd32d93bSMarcel Moolenaar 	if (t == tCOMMA) {
831fd32d93bSMarcel Moolenaar 		if (!db_expression(&count)) {
832fd32d93bSMarcel Moolenaar 			db_printf("Count missing\n");
833fd32d93bSMarcel Moolenaar 			db_flush_lex();
8343531bbb5SWarner Losh 			db_radix = radix;
835fd32d93bSMarcel Moolenaar 			return;
836fd32d93bSMarcel Moolenaar 		}
837fd32d93bSMarcel Moolenaar 	} else {
838fd32d93bSMarcel Moolenaar 		db_unread_token(t);
839fd32d93bSMarcel Moolenaar 		count = -1;
840fd32d93bSMarcel Moolenaar 	}
841fd32d93bSMarcel Moolenaar 	db_skip_to_eol();
842fd32d93bSMarcel Moolenaar 	db_radix = radix;
843fd32d93bSMarcel Moolenaar 
844fd32d93bSMarcel Moolenaar 	if (hastid) {
845fd32d93bSMarcel Moolenaar 		td = kdb_thr_lookup((lwpid_t)tid);
846fd32d93bSMarcel Moolenaar 		if (td == NULL)
847fd32d93bSMarcel Moolenaar 			td = kdb_thr_from_pid((pid_t)tid);
848fd32d93bSMarcel Moolenaar 		if (td == NULL) {
849fd32d93bSMarcel Moolenaar 			db_printf("Thread %d not found\n", (int)tid);
850fd32d93bSMarcel Moolenaar 			return;
851fd32d93bSMarcel Moolenaar 		}
852fd32d93bSMarcel Moolenaar 	} else
853fd32d93bSMarcel Moolenaar 		td = kdb_thread;
854a13aca1aSRobert Watson 	if (td->td_proc != NULL)
855a13aca1aSRobert Watson 		pid = td->td_proc->p_pid;
856a13aca1aSRobert Watson 	else
857a13aca1aSRobert Watson 		pid = -1;
858a13aca1aSRobert Watson 	db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
859825d2341SJohn Baldwin 	if (td->td_proc != NULL && (td->td_proc->p_flag & P_INMEM) == 0)
860825d2341SJohn Baldwin 		db_printf("--- swapped out\n");
861825d2341SJohn Baldwin 	else
862fd32d93bSMarcel Moolenaar 		db_trace_thread(td, count);
863fd32d93bSMarcel Moolenaar }
864a7ad956bSRobert Watson 
865a7ad956bSRobert Watson static void
8667e89a322SConrad Meyer _db_stack_trace_all(bool active_only)
867a7ad956bSRobert Watson {
868a7ad956bSRobert Watson 	struct thread *td;
8699641e389SKonstantin Belousov 	jmp_buf jb;
8709641e389SKonstantin Belousov 	void *prev_jb;
871a7ad956bSRobert Watson 
8723e06c7daSJohn Baldwin 	for (td = kdb_thr_first(); td != NULL; td = kdb_thr_next(td)) {
8739641e389SKonstantin Belousov 		prev_jb = kdb_jmpbuf(jb);
8749641e389SKonstantin Belousov 		if (setjmp(jb) == 0) {
875fa2528acSAlex Richardson 			if (TD_IS_RUNNING(td))
8767e89a322SConrad Meyer 				db_printf("\nTracing command %s pid %d"
8777e89a322SConrad Meyer 				    " tid %ld td %p (CPU %d)\n",
8783e06c7daSJohn Baldwin 				    td->td_proc->p_comm, td->td_proc->p_pid,
8793e06c7daSJohn Baldwin 				    (long)td->td_tid, td, td->td_oncpu);
8807e89a322SConrad Meyer 			else if (active_only)
8817e89a322SConrad Meyer 				continue;
8827e89a322SConrad Meyer 			else
8837e89a322SConrad Meyer 				db_printf("\nTracing command %s pid %d"
8843e06c7daSJohn Baldwin 				    " tid %ld td %p\n", td->td_proc->p_comm,
8853e06c7daSJohn Baldwin 				    td->td_proc->p_pid, (long)td->td_tid, td);
8863e06c7daSJohn Baldwin 			if (td->td_proc->p_flag & P_INMEM)
887a7ad956bSRobert Watson 				db_trace_thread(td, -1);
8883e06c7daSJohn Baldwin 			else
8893e06c7daSJohn Baldwin 				db_printf("--- swapped out\n");
8909641e389SKonstantin Belousov 			if (db_pager_quit) {
8919641e389SKonstantin Belousov 				kdb_jmpbuf(prev_jb);
892da927f93SOlivier Houchard 				return;
893a7ad956bSRobert Watson 			}
894a7ad956bSRobert Watson 		}
8959641e389SKonstantin Belousov 		kdb_jmpbuf(prev_jb);
8969641e389SKonstantin Belousov 	}
8979641e389SKonstantin Belousov }
8980f59fbc3SBjoern A. Zeeb 
8997e89a322SConrad Meyer static void
9007e89a322SConrad Meyer db_stack_trace_active(db_expr_t dummy, bool dummy2, db_expr_t dummy3,
9017e89a322SConrad Meyer     char *dummy4)
9027e89a322SConrad Meyer {
9037e89a322SConrad Meyer 
9047e89a322SConrad Meyer 	_db_stack_trace_all(true);
9057e89a322SConrad Meyer }
9067e89a322SConrad Meyer 
9077e89a322SConrad Meyer static void
9087e89a322SConrad Meyer db_stack_trace_all(db_expr_t dummy, bool dummy2, db_expr_t dummy3,
9097e89a322SConrad Meyer     char *dummy4)
9107e89a322SConrad Meyer {
9117e89a322SConrad Meyer 
9127e89a322SConrad Meyer 	_db_stack_trace_all(false);
9137e89a322SConrad Meyer }
9147e89a322SConrad Meyer 
9150f59fbc3SBjoern A. Zeeb /*
9160f59fbc3SBjoern A. Zeeb  * Take the parsed expression value from the command line that was parsed
9170f59fbc3SBjoern A. Zeeb  * as a hexadecimal value and convert it as if the expression was parsed
9180f59fbc3SBjoern A. Zeeb  * as a decimal value.  Returns -1 if the expression was not a valid
9190f59fbc3SBjoern A. Zeeb  * decimal value.
9200f59fbc3SBjoern A. Zeeb  */
9210f59fbc3SBjoern A. Zeeb db_expr_t
9220f59fbc3SBjoern A. Zeeb db_hex2dec(db_expr_t expr)
9230f59fbc3SBjoern A. Zeeb {
9240f59fbc3SBjoern A. Zeeb 	uintptr_t x, y;
9250f59fbc3SBjoern A. Zeeb 	db_expr_t val;
9260f59fbc3SBjoern A. Zeeb 
9270f59fbc3SBjoern A. Zeeb 	y = 1;
9280f59fbc3SBjoern A. Zeeb 	val = 0;
9290f59fbc3SBjoern A. Zeeb 	x = expr;
9300f59fbc3SBjoern A. Zeeb 	while (x != 0) {
9310f59fbc3SBjoern A. Zeeb 		if (x % 16 > 9)
9320f59fbc3SBjoern A. Zeeb 			return (-1);
9330f59fbc3SBjoern A. Zeeb 		val += (x % 16) * (y);
9340f59fbc3SBjoern A. Zeeb 		x >>= 4;
9350f59fbc3SBjoern A. Zeeb 		y *= 10;
9360f59fbc3SBjoern A. Zeeb 	}
9370f59fbc3SBjoern A. Zeeb 	return (val);
9380f59fbc3SBjoern A. Zeeb }
939