1139747Simp/*-
24Srgrimes * Mach Operating System
34Srgrimes * Copyright (c) 1991,1990 Carnegie Mellon University
44Srgrimes * All Rights Reserved.
58876Srgrimes *
64Srgrimes * Permission to use, copy, modify and distribute this software and its
74Srgrimes * documentation is hereby granted, provided that both the copyright
84Srgrimes * notice and this permission notice appear in all copies of the
94Srgrimes * software, derivative works or modified versions, and any portions
104Srgrimes * thereof, and that both notices appear in supporting documentation.
118876Srgrimes *
128876Srgrimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
134Srgrimes * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
144Srgrimes * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
158876Srgrimes *
164Srgrimes * Carnegie Mellon requests users of this software to return to
178876Srgrimes *
184Srgrimes *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
194Srgrimes *  School of Computer Science
204Srgrimes *  Carnegie Mellon University
214Srgrimes *  Pittsburgh PA 15213-3890
228876Srgrimes *
234Srgrimes * any improvements or extensions that they make and grant Carnegie the
244Srgrimes * rights to redistribute these changes.
254Srgrimes */
264Srgrimes/*
274Srgrimes *	Author: David B. Golub, Carnegie Mellon University
284Srgrimes *	Date:	7/90
294Srgrimes */
304Srgrimes/*
314Srgrimes * Command dispatcher.
324Srgrimes */
33116176Sobrien
34116176Sobrien#include <sys/cdefs.h>
35116176Sobrien__FBSDID("$FreeBSD$");
36116176Sobrien
372056Swollman#include <sys/param.h>
3842654Sjdp#include <sys/linker_set.h>
3986998Sdd#include <sys/lock.h>
40131952Smarcel#include <sys/kdb.h>
4186998Sdd#include <sys/mutex.h>
4286998Sdd#include <sys/proc.h>
4317848Spst#include <sys/reboot.h>
4486998Sdd#include <sys/signalvar.h>
452056Swollman#include <sys/systm.h>
4649558Sphk#include <sys/cons.h>
47222801Smarcel#include <sys/conf.h>
48126399Sphk#include <sys/watchdog.h>
49183054Ssam#include <sys/kernel.h>
5012734Sbde
512056Swollman#include <ddb/ddb.h>
5212473Sbde#include <ddb/db_command.h>
534Srgrimes#include <ddb/db_lex.h>
544Srgrimes#include <ddb/db_output.h>
554Srgrimes
56118990Smarcel#include <machine/cpu.h>
5779418Sjulian#include <machine/setjmp.h>
584Srgrimes
594Srgrimes/*
604Srgrimes * Exported global variables
614Srgrimes */
624Srgrimesboolean_t	db_cmd_loop_done;
6318296Sbdedb_addr_t	db_dot;
644Srgrimesdb_addr_t	db_last_addr;
654Srgrimesdb_addr_t	db_prev;
664Srgrimesdb_addr_t	db_next;
674Srgrimes
68222801Smarcelstatic db_cmdfcn_t	db_dump;
6912515Sphkstatic db_cmdfcn_t	db_fncall;
70132002Smarcelstatic db_cmdfcn_t	db_gdb;
71163192Sbdestatic db_cmdfcn_t	db_halt;
7286998Sddstatic db_cmdfcn_t	db_kill;
7385944Speterstatic db_cmdfcn_t	db_reset;
74132482Smarcelstatic db_cmdfcn_t	db_stack_trace;
75150819Srwatsonstatic db_cmdfcn_t	db_stack_trace_all;
76126399Sphkstatic db_cmdfcn_t	db_watchdog;
7717848Spst
78148919Sobrien/*
79148919Sobrien * 'show' commands
80148919Sobrien */
8118296Sbde
82148919Sobrienstatic struct command db_show_all_cmds[] = {
83183054Ssam	{ "trace",	db_stack_trace_all,	0,	0 },
84148919Sobrien};
85183054Ssamstruct command_table db_show_all_table =
86183054Ssam    LIST_HEAD_INITIALIZER(db_show_all_table);
87148919Sobrien
88148919Sobrienstatic struct command db_show_cmds[] = {
89156412Sjhb	{ "all",	0,			0,	&db_show_all_table },
90148919Sobrien	{ "registers",	db_show_regs,		0,	0 },
91148919Sobrien	{ "breaks",	db_listbreak_cmd, 	0,	0 },
92148919Sobrien	{ "threads",	db_show_threads,	0,	0 },
93148919Sobrien};
94183054Ssamstruct command_table db_show_table = LIST_HEAD_INITIALIZER(db_show_table);
95148919Sobrien
96183054Ssamstatic struct command db_cmds[] = {
97148919Sobrien	{ "print",	db_print_cmd,		0,	0 },
98148919Sobrien	{ "p",		db_print_cmd,		0,	0 },
99148919Sobrien	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
100148919Sobrien	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
101148919Sobrien	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
102148919Sobrien	{ "set",	db_set_cmd,		CS_OWN,	0 },
103148919Sobrien	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
104148919Sobrien	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
105148919Sobrien	{ "delete",	db_delete_cmd,		0,	0 },
106148919Sobrien	{ "d",		db_delete_cmd,		0,	0 },
107222801Smarcel	{ "dump",	db_dump,		0,	0 },
108148919Sobrien	{ "break",	db_breakpoint_cmd,	0,	0 },
109163135Sbde	{ "b",		db_breakpoint_cmd,	0,	0 },
110148919Sobrien	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
111148919Sobrien	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
112148919Sobrien	{ "dhwatch",	db_deletehwatch_cmd,	0,      0 },
113148919Sobrien	{ "hwatch",	db_hwatchpoint_cmd,	0,      0 },
114148919Sobrien	{ "step",	db_single_step_cmd,	0,	0 },
115148919Sobrien	{ "s",		db_single_step_cmd,	0,	0 },
116148919Sobrien	{ "continue",	db_continue_cmd,	0,	0 },
117148919Sobrien	{ "c",		db_continue_cmd,	0,	0 },
118148919Sobrien	{ "until",	db_trace_until_call_cmd,0,	0 },
119148919Sobrien	{ "next",	db_trace_until_matching_cmd,0,	0 },
120148919Sobrien	{ "match",	db_trace_until_matching_cmd,0,	0 },
121148919Sobrien	{ "trace",	db_stack_trace,		CS_OWN,	0 },
122163135Sbde	{ "t",		db_stack_trace,		CS_OWN,	0 },
123183054Ssam	/* XXX alias for all trace */
124151622Sjhb	{ "alltrace",	db_stack_trace_all,	0,	0 },
125148919Sobrien	{ "where",	db_stack_trace,		CS_OWN,	0 },
126151622Sjhb	{ "bt",		db_stack_trace,		CS_OWN,	0 },
127148919Sobrien	{ "call",	db_fncall,		CS_OWN,	0 },
128156412Sjhb	{ "show",	0,			0,	&db_show_table },
129148919Sobrien	{ "ps",		db_ps,			0,	0 },
130148919Sobrien	{ "gdb",	db_gdb,			0,	0 },
131163192Sbde	{ "halt",	db_halt,		0,	0 },
132163192Sbde	{ "reboot",	db_reset,		0,	0 },
133148919Sobrien	{ "reset",	db_reset,		0,	0 },
134148919Sobrien	{ "kill",	db_kill,		CS_OWN,	0 },
135220362Sattilio	{ "watchdog",	db_watchdog,		CS_OWN,	0 },
136148919Sobrien	{ "thread",	db_set_thread,		CS_OWN,	0 },
137174914Srwatson	{ "run",	db_run_cmd,		CS_OWN,	0 },
138174914Srwatson	{ "script",	db_script_cmd,		CS_OWN,	0 },
139174914Srwatson	{ "scripts",	db_scripts_cmd,		0,	0 },
140174914Srwatson	{ "unscript",	db_unscript_cmd,	CS_OWN,	0 },
141174910Srwatson	{ "capture",	db_capture_cmd,		CS_OWN,	0 },
142174921Srwatson	{ "textdump",	db_textdump_cmd,	CS_OWN, 0 },
143228569Skib	{ "findstack",	db_findstack_cmd,	0,	0 },
144148919Sobrien};
145183054Ssamstruct command_table db_cmd_table = LIST_HEAD_INITIALIZER(db_cmd_table);
146148919Sobrien
147148919Sobrienstatic struct command	*db_last_command = 0;
148148919Sobrien
1494Srgrimes/*
1504Srgrimes * if 'ed' style: 'dot' is set at start of last item printed,
1514Srgrimes * and '+' points to next line.
1524Srgrimes * Otherwise: 'dot' points to next item, '..' points to last.
1534Srgrimes */
15412515Sphkstatic boolean_t	db_ed_style = TRUE;
1554Srgrimes
1564Srgrimes/*
1574Srgrimes * Utility routine - discard tokens through end-of-line.
1584Srgrimes */
1594Srgrimesvoid
1604Srgrimesdb_skip_to_eol()
1614Srgrimes{
1624Srgrimes	int	t;
1634Srgrimes	do {
1644Srgrimes	    t = db_read_token();
1654Srgrimes	} while (t != tEOL);
1664Srgrimes}
1674Srgrimes
1684Srgrimes/*
1694Srgrimes * Results of command search.
1704Srgrimes */
1714Srgrimes#define	CMD_UNIQUE	0
1724Srgrimes#define	CMD_FOUND	1
1734Srgrimes#define	CMD_NONE	2
1744Srgrimes#define	CMD_AMBIGUOUS	3
1754Srgrimes#define	CMD_HELP	4
1764Srgrimes
177156412Sjhbstatic void	db_cmd_match(char *name, struct command *cmd,
178156412Sjhb		    struct command **cmdp, int *resultp);
179156412Sjhbstatic void	db_cmd_list(struct command_table *table);
180156412Sjhbstatic int	db_cmd_search(char *name, struct command_table *table,
181156412Sjhb		    struct command **cmdp);
18292756Salfredstatic void	db_command(struct command **last_cmdp,
183174914Srwatson		    struct command_table *cmd_table, int dopager);
18412473Sbde
1854Srgrimes/*
186183054Ssam * Initialize the command lists from the static tables.
187183054Ssam */
188183360Sjhbvoid
189183360Sjhbdb_command_init(void)
190183054Ssam{
191183054Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
192183054Ssam	int i;
193183054Ssam
194183054Ssam	for (i = 0; i < N(db_cmds); i++)
195183054Ssam		db_command_register(&db_cmd_table, &db_cmds[i]);
196183054Ssam	for (i = 0; i < N(db_show_cmds); i++)
197183054Ssam		db_command_register(&db_show_table, &db_show_cmds[i]);
198183054Ssam	for (i = 0; i < N(db_show_all_cmds); i++)
199183054Ssam		db_command_register(&db_show_all_table, &db_show_all_cmds[i]);
200183054Ssam#undef N
201183054Ssam}
202183054Ssam
203183054Ssam/*
204183054Ssam * Register a command.
205183054Ssam */
206183054Ssamvoid
207183054Ssamdb_command_register(struct command_table *list, struct command *cmd)
208183054Ssam{
209183054Ssam	struct command *c, *last;
210183054Ssam
211183054Ssam	last = NULL;
212183054Ssam	LIST_FOREACH(c, list, next) {
213183054Ssam		int n = strcmp(cmd->name, c->name);
214183054Ssam
215183054Ssam		/* Check that the command is not already present. */
216183054Ssam		if (n == 0) {
217183054Ssam			printf("%s: Warning, the command \"%s\" already exists;"
218183054Ssam			     " ignoring request\n", __func__, cmd->name);
219183054Ssam			return;
220183054Ssam		}
221183054Ssam		if (n < 0) {
222183054Ssam			/* NB: keep list sorted lexicographically */
223183054Ssam			LIST_INSERT_BEFORE(c, cmd, next);
224183054Ssam			return;
225183054Ssam		}
226183054Ssam		last = c;
227183054Ssam	}
228183054Ssam	if (last == NULL)
229183054Ssam		LIST_INSERT_HEAD(list, cmd, next);
230183054Ssam	else
231183054Ssam		LIST_INSERT_AFTER(last, cmd, next);
232183054Ssam}
233183054Ssam
234183054Ssam/*
235183054Ssam * Remove a command previously registered with db_command_register.
236183054Ssam */
237183054Ssamvoid
238183054Ssamdb_command_unregister(struct command_table *list, struct command *cmd)
239183054Ssam{
240183054Ssam	struct command *c;
241183054Ssam
242183054Ssam	LIST_FOREACH(c, list, next) {
243183054Ssam		if (cmd == c) {
244183054Ssam			LIST_REMOVE(cmd, next);
245183054Ssam			return;
246183054Ssam		}
247183054Ssam	}
248183054Ssam	/* NB: intentionally quiet */
249183054Ssam}
250183054Ssam
251183054Ssam/*
252156412Sjhb * Helper function to match a single command.
2534Srgrimes */
254156412Sjhbstatic void
255156412Sjhbdb_cmd_match(name, cmd, cmdp, resultp)
2564Srgrimes	char *		name;
257156412Sjhb	struct command	*cmd;
2584Srgrimes	struct command	**cmdp;	/* out */
259156412Sjhb	int *		resultp;
2604Srgrimes{
261156412Sjhb	char *lp, *rp;
262156412Sjhb	int c;
2634Srgrimes
264156412Sjhb	lp = name;
265156412Sjhb	rp = cmd->name;
266156412Sjhb	while ((c = *lp) == *rp) {
2674Srgrimes		if (c == 0) {
268156412Sjhb			/* complete match */
269156412Sjhb			*cmdp = cmd;
270156412Sjhb			*resultp = CMD_UNIQUE;
271156412Sjhb			return;
2724Srgrimes		}
2734Srgrimes		lp++;
2744Srgrimes		rp++;
275156412Sjhb	}
276156412Sjhb	if (c == 0) {
2774Srgrimes		/* end of name, not end of command -
2784Srgrimes		   partial match */
279156412Sjhb		if (*resultp == CMD_FOUND) {
280156412Sjhb			*resultp = CMD_AMBIGUOUS;
281156412Sjhb			/* but keep looking for a full match -
282156412Sjhb			   this lets us match single letters */
283156412Sjhb		} else {
284156412Sjhb			*cmdp = cmd;
285156412Sjhb			*resultp = CMD_FOUND;
2864Srgrimes		}
2874Srgrimes	}
288156412Sjhb}
28918296Sbde
290156412Sjhb/*
291156412Sjhb * Search for command prefix.
292156412Sjhb */
293156412Sjhbstatic int
294156412Sjhbdb_cmd_search(name, table, cmdp)
295156412Sjhb	char *		name;
296156412Sjhb	struct command_table *table;
297156412Sjhb	struct command	**cmdp;	/* out */
298156412Sjhb{
299156412Sjhb	struct command	*cmd;
300156412Sjhb	int		result = CMD_NONE;
301156412Sjhb
302183054Ssam	LIST_FOREACH(cmd, table, next) {
303183054Ssam		db_cmd_match(name,cmd,cmdp,&result);
304156412Sjhb		if (result == CMD_UNIQUE)
305183054Ssam			break;
306156412Sjhb	}
307183054Ssam
3084Srgrimes	if (result == CMD_NONE) {
309156412Sjhb		/* check for 'help' */
3104Srgrimes		if (name[0] == 'h' && name[1] == 'e'
3114Srgrimes		    && name[2] == 'l' && name[3] == 'p')
3124Srgrimes			result = CMD_HELP;
3134Srgrimes	}
3144Srgrimes	return (result);
3154Srgrimes}
3164Srgrimes
31712515Sphkstatic void
318156412Sjhbdb_cmd_list(table)
319156412Sjhb	struct command_table *table;
3204Srgrimes{
321183054Ssam	register struct command	*cmd;
3224Srgrimes
323183054Ssam	LIST_FOREACH(cmd, table, next) {
324183054Ssam		db_printf("%-12s", cmd->name);
325183054Ssam		db_end_line(12);
3264Srgrimes	}
3274Srgrimes}
3284Srgrimes
32912515Sphkstatic void
330174914Srwatsondb_command(last_cmdp, cmd_table, dopager)
3314Srgrimes	struct command	**last_cmdp;	/* IN_OUT */
332156412Sjhb	struct command_table *cmd_table;
333174914Srwatson	int dopager;
3344Srgrimes{
335183054Ssam	struct command	*cmd = NULL;
3364Srgrimes	int		t;
3374Srgrimes	char		modif[TOK_STRING_SIZE];
3384Srgrimes	db_expr_t	addr, count;
339798Swollman	boolean_t	have_addr = FALSE;
3404Srgrimes	int		result;
3414Srgrimes
3424Srgrimes	t = db_read_token();
3434Srgrimes	if (t == tEOL) {
3444Srgrimes	    /* empty line repeats last command, at 'next' */
3454Srgrimes	    cmd = *last_cmdp;
3464Srgrimes	    addr = (db_expr_t)db_next;
3474Srgrimes	    have_addr = FALSE;
3484Srgrimes	    count = 1;
3494Srgrimes	    modif[0] = '\0';
3504Srgrimes	}
3514Srgrimes	else if (t == tEXCL) {
35210348Sbde	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
3534Srgrimes	    return;
3544Srgrimes	}
3554Srgrimes	else if (t != tIDENT) {
3564Srgrimes	    db_printf("?\n");
3574Srgrimes	    db_flush_lex();
3584Srgrimes	    return;
3594Srgrimes	}
3604Srgrimes	else {
3614Srgrimes	    /*
3624Srgrimes	     * Search for command
3634Srgrimes	     */
3644Srgrimes	    while (cmd_table) {
3654Srgrimes		result = db_cmd_search(db_tok_string,
3664Srgrimes				       cmd_table,
3674Srgrimes				       &cmd);
3684Srgrimes		switch (result) {
3694Srgrimes		    case CMD_NONE:
3704Srgrimes			db_printf("No such command\n");
3714Srgrimes			db_flush_lex();
3724Srgrimes			return;
3734Srgrimes		    case CMD_AMBIGUOUS:
3744Srgrimes			db_printf("Ambiguous\n");
3754Srgrimes			db_flush_lex();
3764Srgrimes			return;
3774Srgrimes		    case CMD_HELP:
378156412Sjhb			db_cmd_list(cmd_table);
3794Srgrimes			db_flush_lex();
3804Srgrimes			return;
3814Srgrimes		    default:
3824Srgrimes			break;
3834Srgrimes		}
384156412Sjhb		if ((cmd_table = cmd->more) != NULL) {
3854Srgrimes		    t = db_read_token();
3864Srgrimes		    if (t != tIDENT) {
387156412Sjhb			db_cmd_list(cmd_table);
3884Srgrimes			db_flush_lex();
3894Srgrimes			return;
3904Srgrimes		    }
3914Srgrimes		}
3924Srgrimes	    }
3934Srgrimes
3944Srgrimes	    if ((cmd->flag & CS_OWN) == 0) {
3954Srgrimes		/*
3964Srgrimes		 * Standard syntax:
3974Srgrimes		 * command [/modifier] [addr] [,count]
3984Srgrimes		 */
3994Srgrimes		t = db_read_token();
4004Srgrimes		if (t == tSLASH) {
4014Srgrimes		    t = db_read_token();
4024Srgrimes		    if (t != tIDENT) {
4034Srgrimes			db_printf("Bad modifier\n");
4044Srgrimes			db_flush_lex();
4054Srgrimes			return;
4064Srgrimes		    }
4074Srgrimes		    db_strcpy(modif, db_tok_string);
4084Srgrimes		}
4094Srgrimes		else {
4104Srgrimes		    db_unread_token(t);
4114Srgrimes		    modif[0] = '\0';
4124Srgrimes		}
4134Srgrimes
4144Srgrimes		if (db_expression(&addr)) {
4154Srgrimes		    db_dot = (db_addr_t) addr;
4164Srgrimes		    db_last_addr = db_dot;
4174Srgrimes		    have_addr = TRUE;
4184Srgrimes		}
4194Srgrimes		else {
4204Srgrimes		    addr = (db_expr_t) db_dot;
4214Srgrimes		    have_addr = FALSE;
4224Srgrimes		}
4234Srgrimes		t = db_read_token();
4244Srgrimes		if (t == tCOMMA) {
4254Srgrimes		    if (!db_expression(&count)) {
4264Srgrimes			db_printf("Count missing\n");
4274Srgrimes			db_flush_lex();
4284Srgrimes			return;
4294Srgrimes		    }
4304Srgrimes		}
4314Srgrimes		else {
4324Srgrimes		    db_unread_token(t);
4334Srgrimes		    count = -1;
4344Srgrimes		}
4354Srgrimes		if ((cmd->flag & CS_MORE) == 0) {
4364Srgrimes		    db_skip_to_eol();
4374Srgrimes		}
4384Srgrimes	    }
4394Srgrimes	}
4404Srgrimes	*last_cmdp = cmd;
4414Srgrimes	if (cmd != 0) {
4424Srgrimes	    /*
4434Srgrimes	     * Execute the command.
4444Srgrimes	     */
445174914Srwatson	    if (dopager)
446174914Srwatson		db_enable_pager();
447174914Srwatson	    else
448174914Srwatson		db_disable_pager();
4494Srgrimes	    (*cmd->fcn)(addr, have_addr, count, modif);
450174914Srwatson	    if (dopager)
451174914Srwatson		db_disable_pager();
4524Srgrimes
4534Srgrimes	    if (cmd->flag & CS_SET_DOT) {
4544Srgrimes		/*
4554Srgrimes		 * If command changes dot, set dot to
4564Srgrimes		 * previous address displayed (if 'ed' style).
4574Srgrimes		 */
4584Srgrimes		if (db_ed_style) {
4594Srgrimes		    db_dot = db_prev;
4604Srgrimes		}
4614Srgrimes		else {
4624Srgrimes		    db_dot = db_next;
4634Srgrimes		}
4644Srgrimes	    }
4654Srgrimes	    else {
4664Srgrimes		/*
4674Srgrimes		 * If command does not change dot,
4684Srgrimes		 * set 'next' location to be the same.
4694Srgrimes		 */
4704Srgrimes		db_next = db_dot;
4714Srgrimes	    }
4724Srgrimes	}
4734Srgrimes}
4744Srgrimes
4754Srgrimes/*
47633296Sbde * At least one non-optional command must be implemented using
47733296Sbde * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
47833296Sbde */
47933296SbdeDB_COMMAND(panic, db_panic)
4806204Sphk{
481160505Sjhb	db_disable_pager();
4827170Sdg	panic("from debugger");
4836204Sphk}
4846204Sphk
4856204Sphkvoid
4864Srgrimesdb_command_loop()
4874Srgrimes{
4884Srgrimes	/*
4894Srgrimes	 * Initialize 'prev' and 'next' to dot.
4904Srgrimes	 */
4914Srgrimes	db_prev = db_dot;
4924Srgrimes	db_next = db_dot;
4934Srgrimes
4944Srgrimes	db_cmd_loop_done = 0;
4954Srgrimes	while (!db_cmd_loop_done) {
4964Srgrimes	    if (db_print_position() != 0)
4974Srgrimes		db_printf("\n");
4984Srgrimes
4994Srgrimes	    db_printf("db> ");
5004Srgrimes	    (void) db_read_line();
5014Srgrimes
502183054Ssam	    db_command(&db_last_command, &db_cmd_table, /* dopager */ 1);
5034Srgrimes	}
5044Srgrimes}
5054Srgrimes
506174914Srwatson/*
507174914Srwatson * Execute a command on behalf of a script.  The caller is responsible for
508174914Srwatson * making sure that the command string is < DB_MAXLINE or it will be
509174914Srwatson * truncated.
510174914Srwatson *
511174914Srwatson * XXXRW: Runs by injecting faked input into DDB input stream; it would be
512174914Srwatson * nicer to use an alternative approach that didn't mess with the previous
513174914Srwatson * command buffer.
514174914Srwatson */
5154Srgrimesvoid
516174914Srwatsondb_command_script(const char *command)
517174914Srwatson{
518174914Srwatson	db_prev = db_next = db_dot;
519174914Srwatson	db_inject_line(command);
520183054Ssam	db_command(&db_last_command, &db_cmd_table, /* dopager */ 0);
521174914Srwatson}
522174914Srwatson
523174914Srwatsonvoid
5244Srgrimesdb_error(s)
525103746Smarkm	const char *s;
5264Srgrimes{
5274Srgrimes	if (s)
52879884Skris	    db_printf("%s", s);
5294Srgrimes	db_flush_lex();
530131952Smarcel	kdb_reenter();
5314Srgrimes}
5324Srgrimes
533222801Smarcelstatic void
534222801Smarceldb_dump(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
535222801Smarcel{
536222801Smarcel	int error;
5374Srgrimes
538242424Salfred	if (textdump_pending) {
539242424Salfred		db_printf("textdump_pending set.\n"
540242424Salfred		    "run \"textdump unset\" first or \"textdump dump\" for a textdump.\n");
541242424Salfred		return;
542242424Salfred	}
543222801Smarcel	error = doadump(FALSE);
544222801Smarcel	if (error) {
545222801Smarcel		db_printf("Cannot dump: ");
546222801Smarcel		switch (error) {
547222801Smarcel		case EBUSY:
548222801Smarcel			db_printf("debugger got invoked while dumping.\n");
549222801Smarcel			break;
550222801Smarcel		case ENXIO:
551222801Smarcel			db_printf("no dump device specified.\n");
552222801Smarcel			break;
553222801Smarcel		default:
554222801Smarcel			db_printf("unknown error (error=%d).\n", error);
555222801Smarcel			break;
556222801Smarcel		}
557222801Smarcel	}
558222801Smarcel}
559222801Smarcel
5604Srgrimes/*
5614Srgrimes * Call random function:
5624Srgrimes * !expr(arg,arg,arg)
5634Srgrimes */
564147745Smarcel
565147745Smarcel/* The generic implementation supports a maximum of 10 arguments. */
566147745Smarceltypedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
567147745Smarcel    db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
568147745Smarcel
569147745Smarcelstatic __inline int
570147745Smarceldb_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
571147745Smarcel{
572147745Smarcel	__db_f *f = (__db_f *)addr;
573147745Smarcel
574147745Smarcel	if (nargs > 10) {
575147745Smarcel		db_printf("Too many arguments (max 10)\n");
576147745Smarcel		return (0);
577147745Smarcel	}
578147745Smarcel	*rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
579147745Smarcel	    args[6], args[7], args[8], args[9]);
580147745Smarcel	return (1);
581147745Smarcel}
582147745Smarcel
58312515Sphkstatic void
58410348Sbdedb_fncall(dummy1, dummy2, dummy3, dummy4)
58510348Sbde	db_expr_t	dummy1;
58610348Sbde	boolean_t	dummy2;
58710348Sbde	db_expr_t	dummy3;
58810348Sbde	char *		dummy4;
5894Srgrimes{
5904Srgrimes	db_expr_t	fn_addr;
591147745Smarcel	db_expr_t	args[DB_MAXARGS];
5924Srgrimes	int		nargs = 0;
5934Srgrimes	db_expr_t	retval;
5944Srgrimes	int		t;
5954Srgrimes
5964Srgrimes	if (!db_expression(&fn_addr)) {
5974Srgrimes	    db_printf("Bad function\n");
5984Srgrimes	    db_flush_lex();
5994Srgrimes	    return;
6004Srgrimes	}
6014Srgrimes
6024Srgrimes	t = db_read_token();
6034Srgrimes	if (t == tLPAREN) {
6044Srgrimes	    if (db_expression(&args[0])) {
6054Srgrimes		nargs++;
6064Srgrimes		while ((t = db_read_token()) == tCOMMA) {
607147745Smarcel		    if (nargs == DB_MAXARGS) {
608147745Smarcel			db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
6094Srgrimes			db_flush_lex();
6104Srgrimes			return;
6114Srgrimes		    }
6124Srgrimes		    if (!db_expression(&args[nargs])) {
6134Srgrimes			db_printf("Argument missing\n");
6144Srgrimes			db_flush_lex();
6154Srgrimes			return;
6164Srgrimes		    }
6174Srgrimes		    nargs++;
6184Srgrimes		}
6194Srgrimes		db_unread_token(t);
6204Srgrimes	    }
6214Srgrimes	    if (db_read_token() != tRPAREN) {
6224Srgrimes		db_printf("?\n");
6234Srgrimes		db_flush_lex();
6244Srgrimes		return;
6254Srgrimes	    }
6264Srgrimes	}
6274Srgrimes	db_skip_to_eol();
628160505Sjhb	db_disable_pager();
6294Srgrimes
630147745Smarcel	if (DB_CALL(fn_addr, &retval, nargs, args))
631147745Smarcel		db_printf("= %#lr\n", (long)retval);
6324Srgrimes}
63317848Spst
63417848Spststatic void
635163192Sbdedb_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
636163192Sbde{
637163192Sbde
638163192Sbde	cpu_halt();
639163192Sbde}
640163192Sbde
641163192Sbdestatic void
64286998Sdddb_kill(dummy1, dummy2, dummy3, dummy4)
64386998Sdd	db_expr_t	dummy1;
64486998Sdd	boolean_t	dummy2;
64586998Sdd	db_expr_t	dummy3;
64686998Sdd	char *		dummy4;
64786998Sdd{
64886998Sdd	db_expr_t old_radix, pid, sig;
64986998Sdd	struct proc *p;
65086998Sdd
65186998Sdd#define DB_ERROR(f)	do { db_printf f; db_flush_lex(); goto out; } while (0)
65286998Sdd
65386998Sdd	/*
65486998Sdd	 * PIDs and signal numbers are typically represented in base
65586998Sdd	 * 10, so make that the default here.  It can, of course, be
65686998Sdd	 * overridden by specifying a prefix.
65786998Sdd	 */
65886998Sdd	old_radix = db_radix;
65986998Sdd	db_radix = 10;
66086998Sdd	/* Retrieve arguments. */
66186998Sdd	if (!db_expression(&sig))
66286998Sdd		DB_ERROR(("Missing signal number\n"));
66386998Sdd	if (!db_expression(&pid))
66486998Sdd		DB_ERROR(("Missing process ID\n"));
66586998Sdd	db_skip_to_eol();
666209934Skib	if (!_SIG_VALID(sig))
66786998Sdd		DB_ERROR(("Signal number out of range\n"));
66886998Sdd
66986998Sdd	/*
67086998Sdd	 * Find the process in question.  allproc_lock is not needed
67186998Sdd	 * since we're in DDB.
67286998Sdd	 */
67386998Sdd	/* sx_slock(&allproc_lock); */
674166074Sdelphij	FOREACH_PROC_IN_SYSTEM(p)
67586998Sdd	    if (p->p_pid == pid)
67686998Sdd		    break;
67786998Sdd	/* sx_sunlock(&allproc_lock); */
67886998Sdd	if (p == NULL)
67989442Smjacob		DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
68086998Sdd
68186998Sdd	/* If it's already locked, bail; otherwise, do the deed. */
68286998Sdd	if (PROC_TRYLOCK(p) == 0)
68389442Smjacob		DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
68486998Sdd	else {
685199355Skib		pksignal(p, sig, NULL);
68686998Sdd		PROC_UNLOCK(p);
68786998Sdd	}
68886998Sdd
68986998Sddout:
69086998Sdd	db_radix = old_radix;
69186998Sdd#undef DB_ERROR
69286998Sdd}
69386998Sdd
694208509Sbz/*
695208509Sbz * Reboot.  In case there is an additional argument, take it as delay in
696208509Sbz * seconds.  Default to 15s if we cannot parse it and make sure we will
697208509Sbz * never wait longer than 1 week.  Some code is similar to
698208509Sbz * kern_shutdown.c:shutdown_panic().
699208509Sbz */
700208509Sbz#ifndef	DB_RESET_MAXDELAY
701208509Sbz#define	DB_RESET_MAXDELAY	(3600 * 24 * 7)
702208509Sbz#endif
703208509Sbz
70486998Sddstatic void
705208509Sbzdb_reset(db_expr_t addr, boolean_t have_addr, db_expr_t count __unused,
706208509Sbz    char *modif __unused)
70785944Speter{
708208509Sbz	int delay, loop;
70985944Speter
710208509Sbz	if (have_addr) {
711208509Sbz		delay = (int)db_hex2dec(addr);
712208509Sbz
713208509Sbz		/* If we parse to fail, use 15s. */
714208509Sbz		if (delay == -1)
715208509Sbz			delay = 15;
716208509Sbz
717208509Sbz		/* Cap at one week. */
718208509Sbz		if ((uintmax_t)delay > (uintmax_t)DB_RESET_MAXDELAY)
719208509Sbz			delay = DB_RESET_MAXDELAY;
720208509Sbz
721208509Sbz		db_printf("Automatic reboot in %d seconds - "
722208509Sbz		    "press a key on the console to abort\n", delay);
723208509Sbz		for (loop = delay * 10; loop > 0; --loop) {
724208509Sbz			DELAY(1000 * 100); /* 1/10th second */
725208509Sbz			/* Did user type a key? */
726208509Sbz			if (cncheckc() != -1)
727208509Sbz				return;
728208509Sbz		}
729208509Sbz	}
730208509Sbz
73185944Speter	cpu_reset();
73285944Speter}
733126399Sphk
734126399Sphkstatic void
735126399Sphkdb_watchdog(dummy1, dummy2, dummy3, dummy4)
736126399Sphk	db_expr_t	dummy1;
737126399Sphk	boolean_t	dummy2;
738126399Sphk	db_expr_t	dummy3;
739126399Sphk	char *		dummy4;
740126399Sphk{
741220362Sattilio	db_expr_t old_radix, tout;
742220362Sattilio	int err, i;
743126399Sphk
744220362Sattilio	old_radix = db_radix;
745220362Sattilio	db_radix = 10;
746220362Sattilio	err = db_expression(&tout);
747220362Sattilio	db_skip_to_eol();
748220362Sattilio	db_radix = old_radix;
749126399Sphk
750220362Sattilio	/* If no argument is provided the watchdog will just be disabled. */
751220362Sattilio	if (err == 0) {
752220362Sattilio		db_printf("No argument provided, disabling watchdog\n");
753220362Sattilio		tout = 0;
754220362Sattilio	} else if ((tout & WD_INTERVAL) == WD_TO_NEVER) {
755220362Sattilio		db_error("Out of range watchdog interval\n");
756220362Sattilio		return;
757220362Sattilio	}
758220362Sattilio	EVENTHANDLER_INVOKE(watchdog_list, tout, &i);
759126399Sphk}
760132002Smarcel
761132002Smarcelstatic void
762132002Smarceldb_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
763132002Smarcel{
764132002Smarcel
765218825Smdf	if (kdb_dbbe_select("gdb") != 0) {
766132002Smarcel		db_printf("The remote GDB backend could not be selected.\n");
767218825Smdf		return;
768218825Smdf	}
769218825Smdf	/*
770218825Smdf	 * Mark that we are done in the debugger.  kdb_trap()
771218825Smdf	 * should re-enter with the new backend.
772218825Smdf	 */
773218825Smdf	db_cmd_loop_done = 1;
774218825Smdf	db_printf("(ctrl-c will return control to ddb)\n");
775132002Smarcel}
776132482Smarcel
777132482Smarcelstatic void
778132482Smarceldb_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
779132482Smarcel{
780132482Smarcel	struct thread *td;
781132482Smarcel	db_expr_t radix;
782138038Srwatson	pid_t pid;
783132482Smarcel	int t;
784132482Smarcel
785132482Smarcel	/*
786132482Smarcel	 * We parse our own arguments. We don't like the default radix.
787132482Smarcel	 */
788132482Smarcel	radix = db_radix;
789132482Smarcel	db_radix = 10;
790132482Smarcel	hastid = db_expression(&tid);
791132482Smarcel	t = db_read_token();
792132482Smarcel	if (t == tCOMMA) {
793132482Smarcel		if (!db_expression(&count)) {
794132482Smarcel			db_printf("Count missing\n");
795132482Smarcel			db_flush_lex();
796132482Smarcel			return;
797132482Smarcel		}
798132482Smarcel	} else {
799132482Smarcel		db_unread_token(t);
800132482Smarcel		count = -1;
801132482Smarcel	}
802132482Smarcel	db_skip_to_eol();
803132482Smarcel	db_radix = radix;
804132482Smarcel
805132482Smarcel	if (hastid) {
806132482Smarcel		td = kdb_thr_lookup((lwpid_t)tid);
807132482Smarcel		if (td == NULL)
808132482Smarcel			td = kdb_thr_from_pid((pid_t)tid);
809132482Smarcel		if (td == NULL) {
810132482Smarcel			db_printf("Thread %d not found\n", (int)tid);
811132482Smarcel			return;
812132482Smarcel		}
813132482Smarcel	} else
814132482Smarcel		td = kdb_thread;
815138038Srwatson	if (td->td_proc != NULL)
816138038Srwatson		pid = td->td_proc->p_pid;
817138038Srwatson	else
818138038Srwatson		pid = -1;
819138038Srwatson	db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
820132482Smarcel	db_trace_thread(td, count);
821132482Smarcel}
822150819Srwatson
823150819Srwatsonstatic void
824150819Srwatsondb_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
825150819Srwatson    char *dummy4)
826150819Srwatson{
827150819Srwatson	struct proc *p;
828150819Srwatson	struct thread *td;
829163909Skib	jmp_buf jb;
830163909Skib	void *prev_jb;
831150819Srwatson
832166074Sdelphij	FOREACH_PROC_IN_SYSTEM(p) {
833163909Skib		prev_jb = kdb_jmpbuf(jb);
834163909Skib		if (setjmp(jb) == 0) {
835163909Skib			FOREACH_THREAD_IN_PROC(p, td) {
836163909Skib				db_printf("\nTracing command %s pid %d tid %ld td %p\n",
837163909Skib					  p->p_comm, p->p_pid, (long)td->td_tid, td);
838163909Skib				db_trace_thread(td, -1);
839163909Skib				if (db_pager_quit) {
840163909Skib					kdb_jmpbuf(prev_jb);
841163909Skib					return;
842163909Skib				}
843163909Skib			}
844150819Srwatson		}
845163909Skib		kdb_jmpbuf(prev_jb);
846150819Srwatson	}
847150819Srwatson}
848208509Sbz
849208509Sbz/*
850208509Sbz * Take the parsed expression value from the command line that was parsed
851208509Sbz * as a hexadecimal value and convert it as if the expression was parsed
852208509Sbz * as a decimal value.  Returns -1 if the expression was not a valid
853208509Sbz * decimal value.
854208509Sbz */
855208509Sbzdb_expr_t
856208509Sbzdb_hex2dec(db_expr_t expr)
857208509Sbz{
858208509Sbz	uintptr_t x, y;
859208509Sbz	db_expr_t val;
860208509Sbz
861208509Sbz	y = 1;
862208509Sbz	val = 0;
863208509Sbz	x = expr;
864208509Sbz	while (x != 0) {
865208509Sbz		if (x % 16 > 9)
866208509Sbz			return (-1);
867208509Sbz		val += (x % 16) * (y);
868208509Sbz		x >>= 4;
869208509Sbz		y *= 10;
870208509Sbz	}
871208509Sbz	return (val);
872208509Sbz}
873