db_command.c revision 199355
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: head/sys/ddb/db_command.c 199355 2009-11-17 11:39:15Z kib $");
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>
47126399Sphk#include <sys/watchdog.h>
48183054Ssam#include <sys/kernel.h>
4912734Sbde
502056Swollman#include <ddb/ddb.h>
5112473Sbde#include <ddb/db_command.h>
524Srgrimes#include <ddb/db_lex.h>
534Srgrimes#include <ddb/db_output.h>
544Srgrimes
55118990Smarcel#include <machine/cpu.h>
5679418Sjulian#include <machine/setjmp.h>
574Srgrimes
584Srgrimes/*
594Srgrimes * Exported global variables
604Srgrimes */
614Srgrimesboolean_t	db_cmd_loop_done;
6218296Sbdedb_addr_t	db_dot;
634Srgrimesdb_addr_t	db_last_addr;
644Srgrimesdb_addr_t	db_prev;
654Srgrimesdb_addr_t	db_next;
664Srgrimes
6712515Sphkstatic db_cmdfcn_t	db_fncall;
68132002Smarcelstatic db_cmdfcn_t	db_gdb;
69163192Sbdestatic db_cmdfcn_t	db_halt;
7086998Sddstatic db_cmdfcn_t	db_kill;
7185944Speterstatic db_cmdfcn_t	db_reset;
72132482Smarcelstatic db_cmdfcn_t	db_stack_trace;
73150819Srwatsonstatic db_cmdfcn_t	db_stack_trace_all;
74126399Sphkstatic db_cmdfcn_t	db_watchdog;
7517848Spst
76148919Sobrien/*
77148919Sobrien * 'show' commands
78148919Sobrien */
7918296Sbde
80148919Sobrienstatic struct command db_show_all_cmds[] = {
81183054Ssam	{ "trace",	db_stack_trace_all,	0,	0 },
82148919Sobrien};
83183054Ssamstruct command_table db_show_all_table =
84183054Ssam    LIST_HEAD_INITIALIZER(db_show_all_table);
85148919Sobrien
86148919Sobrienstatic struct command db_show_cmds[] = {
87156412Sjhb	{ "all",	0,			0,	&db_show_all_table },
88148919Sobrien	{ "registers",	db_show_regs,		0,	0 },
89148919Sobrien	{ "breaks",	db_listbreak_cmd, 	0,	0 },
90148919Sobrien	{ "threads",	db_show_threads,	0,	0 },
91148919Sobrien};
92183054Ssamstruct command_table db_show_table = LIST_HEAD_INITIALIZER(db_show_table);
93148919Sobrien
94183054Ssamstatic struct command db_cmds[] = {
95148919Sobrien	{ "print",	db_print_cmd,		0,	0 },
96148919Sobrien	{ "p",		db_print_cmd,		0,	0 },
97148919Sobrien	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
98148919Sobrien	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
99148919Sobrien	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
100148919Sobrien	{ "set",	db_set_cmd,		CS_OWN,	0 },
101148919Sobrien	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
102148919Sobrien	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
103148919Sobrien	{ "delete",	db_delete_cmd,		0,	0 },
104148919Sobrien	{ "d",		db_delete_cmd,		0,	0 },
105148919Sobrien	{ "break",	db_breakpoint_cmd,	0,	0 },
106163135Sbde	{ "b",		db_breakpoint_cmd,	0,	0 },
107148919Sobrien	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
108148919Sobrien	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
109148919Sobrien	{ "dhwatch",	db_deletehwatch_cmd,	0,      0 },
110148919Sobrien	{ "hwatch",	db_hwatchpoint_cmd,	0,      0 },
111148919Sobrien	{ "step",	db_single_step_cmd,	0,	0 },
112148919Sobrien	{ "s",		db_single_step_cmd,	0,	0 },
113148919Sobrien	{ "continue",	db_continue_cmd,	0,	0 },
114148919Sobrien	{ "c",		db_continue_cmd,	0,	0 },
115148919Sobrien	{ "until",	db_trace_until_call_cmd,0,	0 },
116148919Sobrien	{ "next",	db_trace_until_matching_cmd,0,	0 },
117148919Sobrien	{ "match",	db_trace_until_matching_cmd,0,	0 },
118148919Sobrien	{ "trace",	db_stack_trace,		CS_OWN,	0 },
119163135Sbde	{ "t",		db_stack_trace,		CS_OWN,	0 },
120183054Ssam	/* XXX alias for all trace */
121151622Sjhb	{ "alltrace",	db_stack_trace_all,	0,	0 },
122148919Sobrien	{ "where",	db_stack_trace,		CS_OWN,	0 },
123151622Sjhb	{ "bt",		db_stack_trace,		CS_OWN,	0 },
124148919Sobrien	{ "call",	db_fncall,		CS_OWN,	0 },
125156412Sjhb	{ "show",	0,			0,	&db_show_table },
126148919Sobrien	{ "ps",		db_ps,			0,	0 },
127148919Sobrien	{ "gdb",	db_gdb,			0,	0 },
128163192Sbde	{ "halt",	db_halt,		0,	0 },
129163192Sbde	{ "reboot",	db_reset,		0,	0 },
130148919Sobrien	{ "reset",	db_reset,		0,	0 },
131148919Sobrien	{ "kill",	db_kill,		CS_OWN,	0 },
132148919Sobrien	{ "watchdog",	db_watchdog,		0,	0 },
133148919Sobrien	{ "thread",	db_set_thread,		CS_OWN,	0 },
134174914Srwatson	{ "run",	db_run_cmd,		CS_OWN,	0 },
135174914Srwatson	{ "script",	db_script_cmd,		CS_OWN,	0 },
136174914Srwatson	{ "scripts",	db_scripts_cmd,		0,	0 },
137174914Srwatson	{ "unscript",	db_unscript_cmd,	CS_OWN,	0 },
138174910Srwatson	{ "capture",	db_capture_cmd,		CS_OWN,	0 },
139174921Srwatson	{ "textdump",	db_textdump_cmd,	CS_OWN, 0 },
140148919Sobrien};
141183054Ssamstruct command_table db_cmd_table = LIST_HEAD_INITIALIZER(db_cmd_table);
142148919Sobrien
143148919Sobrienstatic struct command	*db_last_command = 0;
144148919Sobrien
1454Srgrimes/*
1464Srgrimes * if 'ed' style: 'dot' is set at start of last item printed,
1474Srgrimes * and '+' points to next line.
1484Srgrimes * Otherwise: 'dot' points to next item, '..' points to last.
1494Srgrimes */
15012515Sphkstatic boolean_t	db_ed_style = TRUE;
1514Srgrimes
1524Srgrimes/*
1534Srgrimes * Utility routine - discard tokens through end-of-line.
1544Srgrimes */
1554Srgrimesvoid
1564Srgrimesdb_skip_to_eol()
1574Srgrimes{
1584Srgrimes	int	t;
1594Srgrimes	do {
1604Srgrimes	    t = db_read_token();
1614Srgrimes	} while (t != tEOL);
1624Srgrimes}
1634Srgrimes
1644Srgrimes/*
1654Srgrimes * Results of command search.
1664Srgrimes */
1674Srgrimes#define	CMD_UNIQUE	0
1684Srgrimes#define	CMD_FOUND	1
1694Srgrimes#define	CMD_NONE	2
1704Srgrimes#define	CMD_AMBIGUOUS	3
1714Srgrimes#define	CMD_HELP	4
1724Srgrimes
173156412Sjhbstatic void	db_cmd_match(char *name, struct command *cmd,
174156412Sjhb		    struct command **cmdp, int *resultp);
175156412Sjhbstatic void	db_cmd_list(struct command_table *table);
176156412Sjhbstatic int	db_cmd_search(char *name, struct command_table *table,
177156412Sjhb		    struct command **cmdp);
17892756Salfredstatic void	db_command(struct command **last_cmdp,
179174914Srwatson		    struct command_table *cmd_table, int dopager);
18012473Sbde
1814Srgrimes/*
182183054Ssam * Initialize the command lists from the static tables.
183183054Ssam */
184183360Sjhbvoid
185183360Sjhbdb_command_init(void)
186183054Ssam{
187183054Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
188183054Ssam	int i;
189183054Ssam
190183054Ssam	for (i = 0; i < N(db_cmds); i++)
191183054Ssam		db_command_register(&db_cmd_table, &db_cmds[i]);
192183054Ssam	for (i = 0; i < N(db_show_cmds); i++)
193183054Ssam		db_command_register(&db_show_table, &db_show_cmds[i]);
194183054Ssam	for (i = 0; i < N(db_show_all_cmds); i++)
195183054Ssam		db_command_register(&db_show_all_table, &db_show_all_cmds[i]);
196183054Ssam#undef N
197183054Ssam}
198183054Ssam
199183054Ssam/*
200183054Ssam * Register a command.
201183054Ssam */
202183054Ssamvoid
203183054Ssamdb_command_register(struct command_table *list, struct command *cmd)
204183054Ssam{
205183054Ssam	struct command *c, *last;
206183054Ssam
207183054Ssam	last = NULL;
208183054Ssam	LIST_FOREACH(c, list, next) {
209183054Ssam		int n = strcmp(cmd->name, c->name);
210183054Ssam
211183054Ssam		/* Check that the command is not already present. */
212183054Ssam		if (n == 0) {
213183054Ssam			printf("%s: Warning, the command \"%s\" already exists;"
214183054Ssam			     " ignoring request\n", __func__, cmd->name);
215183054Ssam			return;
216183054Ssam		}
217183054Ssam		if (n < 0) {
218183054Ssam			/* NB: keep list sorted lexicographically */
219183054Ssam			LIST_INSERT_BEFORE(c, cmd, next);
220183054Ssam			return;
221183054Ssam		}
222183054Ssam		last = c;
223183054Ssam	}
224183054Ssam	if (last == NULL)
225183054Ssam		LIST_INSERT_HEAD(list, cmd, next);
226183054Ssam	else
227183054Ssam		LIST_INSERT_AFTER(last, cmd, next);
228183054Ssam}
229183054Ssam
230183054Ssam/*
231183054Ssam * Remove a command previously registered with db_command_register.
232183054Ssam */
233183054Ssamvoid
234183054Ssamdb_command_unregister(struct command_table *list, struct command *cmd)
235183054Ssam{
236183054Ssam	struct command *c;
237183054Ssam
238183054Ssam	LIST_FOREACH(c, list, next) {
239183054Ssam		if (cmd == c) {
240183054Ssam			LIST_REMOVE(cmd, next);
241183054Ssam			return;
242183054Ssam		}
243183054Ssam	}
244183054Ssam	/* NB: intentionally quiet */
245183054Ssam}
246183054Ssam
247183054Ssam/*
248156412Sjhb * Helper function to match a single command.
2494Srgrimes */
250156412Sjhbstatic void
251156412Sjhbdb_cmd_match(name, cmd, cmdp, resultp)
2524Srgrimes	char *		name;
253156412Sjhb	struct command	*cmd;
2544Srgrimes	struct command	**cmdp;	/* out */
255156412Sjhb	int *		resultp;
2564Srgrimes{
257156412Sjhb	char *lp, *rp;
258156412Sjhb	int c;
2594Srgrimes
260156412Sjhb	lp = name;
261156412Sjhb	rp = cmd->name;
262156412Sjhb	while ((c = *lp) == *rp) {
2634Srgrimes		if (c == 0) {
264156412Sjhb			/* complete match */
265156412Sjhb			*cmdp = cmd;
266156412Sjhb			*resultp = CMD_UNIQUE;
267156412Sjhb			return;
2684Srgrimes		}
2694Srgrimes		lp++;
2704Srgrimes		rp++;
271156412Sjhb	}
272156412Sjhb	if (c == 0) {
2734Srgrimes		/* end of name, not end of command -
2744Srgrimes		   partial match */
275156412Sjhb		if (*resultp == CMD_FOUND) {
276156412Sjhb			*resultp = CMD_AMBIGUOUS;
277156412Sjhb			/* but keep looking for a full match -
278156412Sjhb			   this lets us match single letters */
279156412Sjhb		} else {
280156412Sjhb			*cmdp = cmd;
281156412Sjhb			*resultp = CMD_FOUND;
2824Srgrimes		}
2834Srgrimes	}
284156412Sjhb}
28518296Sbde
286156412Sjhb/*
287156412Sjhb * Search for command prefix.
288156412Sjhb */
289156412Sjhbstatic int
290156412Sjhbdb_cmd_search(name, table, cmdp)
291156412Sjhb	char *		name;
292156412Sjhb	struct command_table *table;
293156412Sjhb	struct command	**cmdp;	/* out */
294156412Sjhb{
295156412Sjhb	struct command	*cmd;
296156412Sjhb	int		result = CMD_NONE;
297156412Sjhb
298183054Ssam	LIST_FOREACH(cmd, table, next) {
299183054Ssam		db_cmd_match(name,cmd,cmdp,&result);
300156412Sjhb		if (result == CMD_UNIQUE)
301183054Ssam			break;
302156412Sjhb	}
303183054Ssam
3044Srgrimes	if (result == CMD_NONE) {
305156412Sjhb		/* check for 'help' */
3064Srgrimes		if (name[0] == 'h' && name[1] == 'e'
3074Srgrimes		    && name[2] == 'l' && name[3] == 'p')
3084Srgrimes			result = CMD_HELP;
3094Srgrimes	}
3104Srgrimes	return (result);
3114Srgrimes}
3124Srgrimes
31312515Sphkstatic void
314156412Sjhbdb_cmd_list(table)
315156412Sjhb	struct command_table *table;
3164Srgrimes{
317183054Ssam	register struct command	*cmd;
3184Srgrimes
319183054Ssam	LIST_FOREACH(cmd, table, next) {
320183054Ssam		db_printf("%-12s", cmd->name);
321183054Ssam		db_end_line(12);
3224Srgrimes	}
3234Srgrimes}
3244Srgrimes
32512515Sphkstatic void
326174914Srwatsondb_command(last_cmdp, cmd_table, dopager)
3274Srgrimes	struct command	**last_cmdp;	/* IN_OUT */
328156412Sjhb	struct command_table *cmd_table;
329174914Srwatson	int dopager;
3304Srgrimes{
331183054Ssam	struct command	*cmd = NULL;
3324Srgrimes	int		t;
3334Srgrimes	char		modif[TOK_STRING_SIZE];
3344Srgrimes	db_expr_t	addr, count;
335798Swollman	boolean_t	have_addr = FALSE;
3364Srgrimes	int		result;
3374Srgrimes
3384Srgrimes	t = db_read_token();
3394Srgrimes	if (t == tEOL) {
3404Srgrimes	    /* empty line repeats last command, at 'next' */
3414Srgrimes	    cmd = *last_cmdp;
3424Srgrimes	    addr = (db_expr_t)db_next;
3434Srgrimes	    have_addr = FALSE;
3444Srgrimes	    count = 1;
3454Srgrimes	    modif[0] = '\0';
3464Srgrimes	}
3474Srgrimes	else if (t == tEXCL) {
34810348Sbde	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
3494Srgrimes	    return;
3504Srgrimes	}
3514Srgrimes	else if (t != tIDENT) {
3524Srgrimes	    db_printf("?\n");
3534Srgrimes	    db_flush_lex();
3544Srgrimes	    return;
3554Srgrimes	}
3564Srgrimes	else {
3574Srgrimes	    /*
3584Srgrimes	     * Search for command
3594Srgrimes	     */
3604Srgrimes	    while (cmd_table) {
3614Srgrimes		result = db_cmd_search(db_tok_string,
3624Srgrimes				       cmd_table,
3634Srgrimes				       &cmd);
3644Srgrimes		switch (result) {
3654Srgrimes		    case CMD_NONE:
3664Srgrimes			db_printf("No such command\n");
3674Srgrimes			db_flush_lex();
3684Srgrimes			return;
3694Srgrimes		    case CMD_AMBIGUOUS:
3704Srgrimes			db_printf("Ambiguous\n");
3714Srgrimes			db_flush_lex();
3724Srgrimes			return;
3734Srgrimes		    case CMD_HELP:
374156412Sjhb			db_cmd_list(cmd_table);
3754Srgrimes			db_flush_lex();
3764Srgrimes			return;
3774Srgrimes		    default:
3784Srgrimes			break;
3794Srgrimes		}
380156412Sjhb		if ((cmd_table = cmd->more) != NULL) {
3814Srgrimes		    t = db_read_token();
3824Srgrimes		    if (t != tIDENT) {
383156412Sjhb			db_cmd_list(cmd_table);
3844Srgrimes			db_flush_lex();
3854Srgrimes			return;
3864Srgrimes		    }
3874Srgrimes		}
3884Srgrimes	    }
3894Srgrimes
3904Srgrimes	    if ((cmd->flag & CS_OWN) == 0) {
3914Srgrimes		/*
3924Srgrimes		 * Standard syntax:
3934Srgrimes		 * command [/modifier] [addr] [,count]
3944Srgrimes		 */
3954Srgrimes		t = db_read_token();
3964Srgrimes		if (t == tSLASH) {
3974Srgrimes		    t = db_read_token();
3984Srgrimes		    if (t != tIDENT) {
3994Srgrimes			db_printf("Bad modifier\n");
4004Srgrimes			db_flush_lex();
4014Srgrimes			return;
4024Srgrimes		    }
4034Srgrimes		    db_strcpy(modif, db_tok_string);
4044Srgrimes		}
4054Srgrimes		else {
4064Srgrimes		    db_unread_token(t);
4074Srgrimes		    modif[0] = '\0';
4084Srgrimes		}
4094Srgrimes
4104Srgrimes		if (db_expression(&addr)) {
4114Srgrimes		    db_dot = (db_addr_t) addr;
4124Srgrimes		    db_last_addr = db_dot;
4134Srgrimes		    have_addr = TRUE;
4144Srgrimes		}
4154Srgrimes		else {
4164Srgrimes		    addr = (db_expr_t) db_dot;
4174Srgrimes		    have_addr = FALSE;
4184Srgrimes		}
4194Srgrimes		t = db_read_token();
4204Srgrimes		if (t == tCOMMA) {
4214Srgrimes		    if (!db_expression(&count)) {
4224Srgrimes			db_printf("Count missing\n");
4234Srgrimes			db_flush_lex();
4244Srgrimes			return;
4254Srgrimes		    }
4264Srgrimes		}
4274Srgrimes		else {
4284Srgrimes		    db_unread_token(t);
4294Srgrimes		    count = -1;
4304Srgrimes		}
4314Srgrimes		if ((cmd->flag & CS_MORE) == 0) {
4324Srgrimes		    db_skip_to_eol();
4334Srgrimes		}
4344Srgrimes	    }
4354Srgrimes	}
4364Srgrimes	*last_cmdp = cmd;
4374Srgrimes	if (cmd != 0) {
4384Srgrimes	    /*
4394Srgrimes	     * Execute the command.
4404Srgrimes	     */
441174914Srwatson	    if (dopager)
442174914Srwatson		db_enable_pager();
443174914Srwatson	    else
444174914Srwatson		db_disable_pager();
4454Srgrimes	    (*cmd->fcn)(addr, have_addr, count, modif);
446174914Srwatson	    if (dopager)
447174914Srwatson		db_disable_pager();
4484Srgrimes
4494Srgrimes	    if (cmd->flag & CS_SET_DOT) {
4504Srgrimes		/*
4514Srgrimes		 * If command changes dot, set dot to
4524Srgrimes		 * previous address displayed (if 'ed' style).
4534Srgrimes		 */
4544Srgrimes		if (db_ed_style) {
4554Srgrimes		    db_dot = db_prev;
4564Srgrimes		}
4574Srgrimes		else {
4584Srgrimes		    db_dot = db_next;
4594Srgrimes		}
4604Srgrimes	    }
4614Srgrimes	    else {
4624Srgrimes		/*
4634Srgrimes		 * If command does not change dot,
4644Srgrimes		 * set 'next' location to be the same.
4654Srgrimes		 */
4664Srgrimes		db_next = db_dot;
4674Srgrimes	    }
4684Srgrimes	}
4694Srgrimes}
4704Srgrimes
4714Srgrimes/*
47233296Sbde * At least one non-optional command must be implemented using
47333296Sbde * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
47433296Sbde */
47533296SbdeDB_COMMAND(panic, db_panic)
4766204Sphk{
477160505Sjhb	db_disable_pager();
4787170Sdg	panic("from debugger");
4796204Sphk}
4806204Sphk
4816204Sphkvoid
4824Srgrimesdb_command_loop()
4834Srgrimes{
4844Srgrimes	/*
4854Srgrimes	 * Initialize 'prev' and 'next' to dot.
4864Srgrimes	 */
4874Srgrimes	db_prev = db_dot;
4884Srgrimes	db_next = db_dot;
4894Srgrimes
4904Srgrimes	db_cmd_loop_done = 0;
4914Srgrimes	while (!db_cmd_loop_done) {
4924Srgrimes	    if (db_print_position() != 0)
4934Srgrimes		db_printf("\n");
4944Srgrimes
4954Srgrimes	    db_printf("db> ");
4964Srgrimes	    (void) db_read_line();
4974Srgrimes
498183054Ssam	    db_command(&db_last_command, &db_cmd_table, /* dopager */ 1);
4994Srgrimes	}
5004Srgrimes}
5014Srgrimes
502174914Srwatson/*
503174914Srwatson * Execute a command on behalf of a script.  The caller is responsible for
504174914Srwatson * making sure that the command string is < DB_MAXLINE or it will be
505174914Srwatson * truncated.
506174914Srwatson *
507174914Srwatson * XXXRW: Runs by injecting faked input into DDB input stream; it would be
508174914Srwatson * nicer to use an alternative approach that didn't mess with the previous
509174914Srwatson * command buffer.
510174914Srwatson */
5114Srgrimesvoid
512174914Srwatsondb_command_script(const char *command)
513174914Srwatson{
514174914Srwatson	db_prev = db_next = db_dot;
515174914Srwatson	db_inject_line(command);
516183054Ssam	db_command(&db_last_command, &db_cmd_table, /* dopager */ 0);
517174914Srwatson}
518174914Srwatson
519174914Srwatsonvoid
5204Srgrimesdb_error(s)
521103746Smarkm	const char *s;
5224Srgrimes{
5234Srgrimes	if (s)
52479884Skris	    db_printf("%s", s);
5254Srgrimes	db_flush_lex();
526131952Smarcel	kdb_reenter();
5274Srgrimes}
5284Srgrimes
5294Srgrimes
5304Srgrimes/*
5314Srgrimes * Call random function:
5324Srgrimes * !expr(arg,arg,arg)
5334Srgrimes */
534147745Smarcel
535147745Smarcel/* The generic implementation supports a maximum of 10 arguments. */
536147745Smarceltypedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
537147745Smarcel    db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
538147745Smarcel
539147745Smarcelstatic __inline int
540147745Smarceldb_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
541147745Smarcel{
542147745Smarcel	__db_f *f = (__db_f *)addr;
543147745Smarcel
544147745Smarcel	if (nargs > 10) {
545147745Smarcel		db_printf("Too many arguments (max 10)\n");
546147745Smarcel		return (0);
547147745Smarcel	}
548147745Smarcel	*rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
549147745Smarcel	    args[6], args[7], args[8], args[9]);
550147745Smarcel	return (1);
551147745Smarcel}
552147745Smarcel
55312515Sphkstatic void
55410348Sbdedb_fncall(dummy1, dummy2, dummy3, dummy4)
55510348Sbde	db_expr_t	dummy1;
55610348Sbde	boolean_t	dummy2;
55710348Sbde	db_expr_t	dummy3;
55810348Sbde	char *		dummy4;
5594Srgrimes{
5604Srgrimes	db_expr_t	fn_addr;
561147745Smarcel	db_expr_t	args[DB_MAXARGS];
5624Srgrimes	int		nargs = 0;
5634Srgrimes	db_expr_t	retval;
5644Srgrimes	int		t;
5654Srgrimes
5664Srgrimes	if (!db_expression(&fn_addr)) {
5674Srgrimes	    db_printf("Bad function\n");
5684Srgrimes	    db_flush_lex();
5694Srgrimes	    return;
5704Srgrimes	}
5714Srgrimes
5724Srgrimes	t = db_read_token();
5734Srgrimes	if (t == tLPAREN) {
5744Srgrimes	    if (db_expression(&args[0])) {
5754Srgrimes		nargs++;
5764Srgrimes		while ((t = db_read_token()) == tCOMMA) {
577147745Smarcel		    if (nargs == DB_MAXARGS) {
578147745Smarcel			db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
5794Srgrimes			db_flush_lex();
5804Srgrimes			return;
5814Srgrimes		    }
5824Srgrimes		    if (!db_expression(&args[nargs])) {
5834Srgrimes			db_printf("Argument missing\n");
5844Srgrimes			db_flush_lex();
5854Srgrimes			return;
5864Srgrimes		    }
5874Srgrimes		    nargs++;
5884Srgrimes		}
5894Srgrimes		db_unread_token(t);
5904Srgrimes	    }
5914Srgrimes	    if (db_read_token() != tRPAREN) {
5924Srgrimes		db_printf("?\n");
5934Srgrimes		db_flush_lex();
5944Srgrimes		return;
5954Srgrimes	    }
5964Srgrimes	}
5974Srgrimes	db_skip_to_eol();
598160505Sjhb	db_disable_pager();
5994Srgrimes
600147745Smarcel	if (DB_CALL(fn_addr, &retval, nargs, args))
601147745Smarcel		db_printf("= %#lr\n", (long)retval);
6024Srgrimes}
60317848Spst
60417848Spststatic void
605163192Sbdedb_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
606163192Sbde{
607163192Sbde
608163192Sbde	cpu_halt();
609163192Sbde}
610163192Sbde
611163192Sbdestatic void
61286998Sdddb_kill(dummy1, dummy2, dummy3, dummy4)
61386998Sdd	db_expr_t	dummy1;
61486998Sdd	boolean_t	dummy2;
61586998Sdd	db_expr_t	dummy3;
61686998Sdd	char *		dummy4;
61786998Sdd{
61886998Sdd	db_expr_t old_radix, pid, sig;
61986998Sdd	struct proc *p;
62086998Sdd
62186998Sdd#define DB_ERROR(f)	do { db_printf f; db_flush_lex(); goto out; } while (0)
62286998Sdd
62386998Sdd	/*
62486998Sdd	 * PIDs and signal numbers are typically represented in base
62586998Sdd	 * 10, so make that the default here.  It can, of course, be
62686998Sdd	 * overridden by specifying a prefix.
62786998Sdd	 */
62886998Sdd	old_radix = db_radix;
62986998Sdd	db_radix = 10;
63086998Sdd	/* Retrieve arguments. */
63186998Sdd	if (!db_expression(&sig))
63286998Sdd		DB_ERROR(("Missing signal number\n"));
63386998Sdd	if (!db_expression(&pid))
63486998Sdd		DB_ERROR(("Missing process ID\n"));
63586998Sdd	db_skip_to_eol();
636176897Srwatson	if (sig < 1 || sig > _SIG_MAXSIG)
63786998Sdd		DB_ERROR(("Signal number out of range\n"));
63886998Sdd
63986998Sdd	/*
64086998Sdd	 * Find the process in question.  allproc_lock is not needed
64186998Sdd	 * since we're in DDB.
64286998Sdd	 */
64386998Sdd	/* sx_slock(&allproc_lock); */
644166074Sdelphij	FOREACH_PROC_IN_SYSTEM(p)
64586998Sdd	    if (p->p_pid == pid)
64686998Sdd		    break;
64786998Sdd	/* sx_sunlock(&allproc_lock); */
64886998Sdd	if (p == NULL)
64989442Smjacob		DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
65086998Sdd
65186998Sdd	/* If it's already locked, bail; otherwise, do the deed. */
65286998Sdd	if (PROC_TRYLOCK(p) == 0)
65389442Smjacob		DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
65486998Sdd	else {
655199355Skib		pksignal(p, sig, NULL);
65686998Sdd		PROC_UNLOCK(p);
65786998Sdd	}
65886998Sdd
65986998Sddout:
66086998Sdd	db_radix = old_radix;
66186998Sdd#undef DB_ERROR
66286998Sdd}
66386998Sdd
66486998Sddstatic void
66585944Speterdb_reset(dummy1, dummy2, dummy3, dummy4)
66685944Speter	db_expr_t	dummy1;
66785944Speter	boolean_t	dummy2;
66885944Speter	db_expr_t	dummy3;
66985944Speter	char *		dummy4;
67085944Speter{
67185944Speter
67285944Speter	cpu_reset();
67385944Speter}
674126399Sphk
675126399Sphkstatic void
676126399Sphkdb_watchdog(dummy1, dummy2, dummy3, dummy4)
677126399Sphk	db_expr_t	dummy1;
678126399Sphk	boolean_t	dummy2;
679126399Sphk	db_expr_t	dummy3;
680126399Sphk	char *		dummy4;
681126399Sphk{
682126399Sphk	int i;
683126399Sphk
684126399Sphk	/*
685126399Sphk	 * XXX: It might make sense to be able to set the watchdog to a
686126399Sphk	 * XXX: timeout here so that failure or hang as a result of subsequent
687126399Sphk	 * XXX: ddb commands could be recovered by a reset.
688126399Sphk	 */
689126399Sphk
690126399Sphk	EVENTHANDLER_INVOKE(watchdog_list, 0, &i);
691126399Sphk}
692132002Smarcel
693132002Smarcelstatic void
694132002Smarceldb_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
695132002Smarcel{
696132002Smarcel
697132002Smarcel	if (kdb_dbbe_select("gdb") != 0)
698132002Smarcel		db_printf("The remote GDB backend could not be selected.\n");
699132002Smarcel	else
700132002Smarcel		db_printf("Step to enter the remote GDB backend.\n");
701132002Smarcel}
702132482Smarcel
703132482Smarcelstatic void
704132482Smarceldb_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
705132482Smarcel{
706132482Smarcel	struct thread *td;
707132482Smarcel	db_expr_t radix;
708138038Srwatson	pid_t pid;
709132482Smarcel	int t;
710132482Smarcel
711132482Smarcel	/*
712132482Smarcel	 * We parse our own arguments. We don't like the default radix.
713132482Smarcel	 */
714132482Smarcel	radix = db_radix;
715132482Smarcel	db_radix = 10;
716132482Smarcel	hastid = db_expression(&tid);
717132482Smarcel	t = db_read_token();
718132482Smarcel	if (t == tCOMMA) {
719132482Smarcel		if (!db_expression(&count)) {
720132482Smarcel			db_printf("Count missing\n");
721132482Smarcel			db_flush_lex();
722132482Smarcel			return;
723132482Smarcel		}
724132482Smarcel	} else {
725132482Smarcel		db_unread_token(t);
726132482Smarcel		count = -1;
727132482Smarcel	}
728132482Smarcel	db_skip_to_eol();
729132482Smarcel	db_radix = radix;
730132482Smarcel
731132482Smarcel	if (hastid) {
732132482Smarcel		td = kdb_thr_lookup((lwpid_t)tid);
733132482Smarcel		if (td == NULL)
734132482Smarcel			td = kdb_thr_from_pid((pid_t)tid);
735132482Smarcel		if (td == NULL) {
736132482Smarcel			db_printf("Thread %d not found\n", (int)tid);
737132482Smarcel			return;
738132482Smarcel		}
739132482Smarcel	} else
740132482Smarcel		td = kdb_thread;
741138038Srwatson	if (td->td_proc != NULL)
742138038Srwatson		pid = td->td_proc->p_pid;
743138038Srwatson	else
744138038Srwatson		pid = -1;
745138038Srwatson	db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
746132482Smarcel	db_trace_thread(td, count);
747132482Smarcel}
748150819Srwatson
749150819Srwatsonstatic void
750150819Srwatsondb_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
751150819Srwatson    char *dummy4)
752150819Srwatson{
753150819Srwatson	struct proc *p;
754150819Srwatson	struct thread *td;
755163909Skib	jmp_buf jb;
756163909Skib	void *prev_jb;
757150819Srwatson
758166074Sdelphij	FOREACH_PROC_IN_SYSTEM(p) {
759163909Skib		prev_jb = kdb_jmpbuf(jb);
760163909Skib		if (setjmp(jb) == 0) {
761163909Skib			FOREACH_THREAD_IN_PROC(p, td) {
762163909Skib				db_printf("\nTracing command %s pid %d tid %ld td %p\n",
763163909Skib					  p->p_comm, p->p_pid, (long)td->td_tid, td);
764163909Skib				db_trace_thread(td, -1);
765163909Skib				if (db_pager_quit) {
766163909Skib					kdb_jmpbuf(prev_jb);
767163909Skib					return;
768163909Skib				}
769163909Skib			}
770150819Srwatson		}
771163909Skib		kdb_jmpbuf(prev_jb);
772150819Srwatson	}
773150819Srwatson}
774