db_command.c revision 183054
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 183054 2008-09-15 22:45:14Z sam $");
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 */
184183054Ssamstatic void
185183054Ssamdb_cmd_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}
198183054SsamSYSINIT(_cmd_init, SI_SUB_KLD, SI_ORDER_FIRST, db_cmd_init, NULL);
199183054Ssam
200183054Ssam/*
201183054Ssam * Register a command.
202183054Ssam */
203183054Ssamvoid
204183054Ssamdb_command_register(struct command_table *list, struct command *cmd)
205183054Ssam{
206183054Ssam	struct command *c, *last;
207183054Ssam
208183054Ssam	last = NULL;
209183054Ssam	LIST_FOREACH(c, list, next) {
210183054Ssam		int n = strcmp(cmd->name, c->name);
211183054Ssam
212183054Ssam		/* Check that the command is not already present. */
213183054Ssam		if (n == 0) {
214183054Ssam			printf("%s: Warning, the command \"%s\" already exists;"
215183054Ssam			     " ignoring request\n", __func__, cmd->name);
216183054Ssam			return;
217183054Ssam		}
218183054Ssam		if (n < 0) {
219183054Ssam			/* NB: keep list sorted lexicographically */
220183054Ssam			LIST_INSERT_BEFORE(c, cmd, next);
221183054Ssam			return;
222183054Ssam		}
223183054Ssam		last = c;
224183054Ssam	}
225183054Ssam	if (last == NULL)
226183054Ssam		LIST_INSERT_HEAD(list, cmd, next);
227183054Ssam	else
228183054Ssam		LIST_INSERT_AFTER(last, cmd, next);
229183054Ssam}
230183054Ssam
231183054Ssam/*
232183054Ssam * Remove a command previously registered with db_command_register.
233183054Ssam */
234183054Ssamvoid
235183054Ssamdb_command_unregister(struct command_table *list, struct command *cmd)
236183054Ssam{
237183054Ssam	struct command *c;
238183054Ssam
239183054Ssam	LIST_FOREACH(c, list, next) {
240183054Ssam		if (cmd == c) {
241183054Ssam			LIST_REMOVE(cmd, next);
242183054Ssam			return;
243183054Ssam		}
244183054Ssam	}
245183054Ssam	/* NB: intentionally quiet */
246183054Ssam}
247183054Ssam
248183054Ssam/*
249156412Sjhb * Helper function to match a single command.
2504Srgrimes */
251156412Sjhbstatic void
252156412Sjhbdb_cmd_match(name, cmd, cmdp, resultp)
2534Srgrimes	char *		name;
254156412Sjhb	struct command	*cmd;
2554Srgrimes	struct command	**cmdp;	/* out */
256156412Sjhb	int *		resultp;
2574Srgrimes{
258156412Sjhb	char *lp, *rp;
259156412Sjhb	int c;
2604Srgrimes
261156412Sjhb	lp = name;
262156412Sjhb	rp = cmd->name;
263156412Sjhb	while ((c = *lp) == *rp) {
2644Srgrimes		if (c == 0) {
265156412Sjhb			/* complete match */
266156412Sjhb			*cmdp = cmd;
267156412Sjhb			*resultp = CMD_UNIQUE;
268156412Sjhb			return;
2694Srgrimes		}
2704Srgrimes		lp++;
2714Srgrimes		rp++;
272156412Sjhb	}
273156412Sjhb	if (c == 0) {
2744Srgrimes		/* end of name, not end of command -
2754Srgrimes		   partial match */
276156412Sjhb		if (*resultp == CMD_FOUND) {
277156412Sjhb			*resultp = CMD_AMBIGUOUS;
278156412Sjhb			/* but keep looking for a full match -
279156412Sjhb			   this lets us match single letters */
280156412Sjhb		} else {
281156412Sjhb			*cmdp = cmd;
282156412Sjhb			*resultp = CMD_FOUND;
2834Srgrimes		}
2844Srgrimes	}
285156412Sjhb}
28618296Sbde
287156412Sjhb/*
288156412Sjhb * Search for command prefix.
289156412Sjhb */
290156412Sjhbstatic int
291156412Sjhbdb_cmd_search(name, table, cmdp)
292156412Sjhb	char *		name;
293156412Sjhb	struct command_table *table;
294156412Sjhb	struct command	**cmdp;	/* out */
295156412Sjhb{
296156412Sjhb	struct command	*cmd;
297156412Sjhb	int		result = CMD_NONE;
298156412Sjhb
299183054Ssam	LIST_FOREACH(cmd, table, next) {
300183054Ssam		db_cmd_match(name,cmd,cmdp,&result);
301156412Sjhb		if (result == CMD_UNIQUE)
302183054Ssam			break;
303156412Sjhb	}
304183054Ssam
3054Srgrimes	if (result == CMD_NONE) {
306156412Sjhb		/* check for 'help' */
3074Srgrimes		if (name[0] == 'h' && name[1] == 'e'
3084Srgrimes		    && name[2] == 'l' && name[3] == 'p')
3094Srgrimes			result = CMD_HELP;
3104Srgrimes	}
3114Srgrimes	return (result);
3124Srgrimes}
3134Srgrimes
31412515Sphkstatic void
315156412Sjhbdb_cmd_list(table)
316156412Sjhb	struct command_table *table;
3174Srgrimes{
318183054Ssam	register struct command	*cmd;
3194Srgrimes
320183054Ssam	LIST_FOREACH(cmd, table, next) {
321183054Ssam		db_printf("%-12s", cmd->name);
322183054Ssam		db_end_line(12);
3234Srgrimes	}
3244Srgrimes}
3254Srgrimes
32612515Sphkstatic void
327174914Srwatsondb_command(last_cmdp, cmd_table, dopager)
3284Srgrimes	struct command	**last_cmdp;	/* IN_OUT */
329156412Sjhb	struct command_table *cmd_table;
330174914Srwatson	int dopager;
3314Srgrimes{
332183054Ssam	struct command	*cmd = NULL;
3334Srgrimes	int		t;
3344Srgrimes	char		modif[TOK_STRING_SIZE];
3354Srgrimes	db_expr_t	addr, count;
336798Swollman	boolean_t	have_addr = FALSE;
3374Srgrimes	int		result;
3384Srgrimes
3394Srgrimes	t = db_read_token();
3404Srgrimes	if (t == tEOL) {
3414Srgrimes	    /* empty line repeats last command, at 'next' */
3424Srgrimes	    cmd = *last_cmdp;
3434Srgrimes	    addr = (db_expr_t)db_next;
3444Srgrimes	    have_addr = FALSE;
3454Srgrimes	    count = 1;
3464Srgrimes	    modif[0] = '\0';
3474Srgrimes	}
3484Srgrimes	else if (t == tEXCL) {
34910348Sbde	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
3504Srgrimes	    return;
3514Srgrimes	}
3524Srgrimes	else if (t != tIDENT) {
3534Srgrimes	    db_printf("?\n");
3544Srgrimes	    db_flush_lex();
3554Srgrimes	    return;
3564Srgrimes	}
3574Srgrimes	else {
3584Srgrimes	    /*
3594Srgrimes	     * Search for command
3604Srgrimes	     */
3614Srgrimes	    while (cmd_table) {
3624Srgrimes		result = db_cmd_search(db_tok_string,
3634Srgrimes				       cmd_table,
3644Srgrimes				       &cmd);
3654Srgrimes		switch (result) {
3664Srgrimes		    case CMD_NONE:
3674Srgrimes			db_printf("No such command\n");
3684Srgrimes			db_flush_lex();
3694Srgrimes			return;
3704Srgrimes		    case CMD_AMBIGUOUS:
3714Srgrimes			db_printf("Ambiguous\n");
3724Srgrimes			db_flush_lex();
3734Srgrimes			return;
3744Srgrimes		    case CMD_HELP:
375156412Sjhb			db_cmd_list(cmd_table);
3764Srgrimes			db_flush_lex();
3774Srgrimes			return;
3784Srgrimes		    default:
3794Srgrimes			break;
3804Srgrimes		}
381156412Sjhb		if ((cmd_table = cmd->more) != NULL) {
3824Srgrimes		    t = db_read_token();
3834Srgrimes		    if (t != tIDENT) {
384156412Sjhb			db_cmd_list(cmd_table);
3854Srgrimes			db_flush_lex();
3864Srgrimes			return;
3874Srgrimes		    }
3884Srgrimes		}
3894Srgrimes	    }
3904Srgrimes
3914Srgrimes	    if ((cmd->flag & CS_OWN) == 0) {
3924Srgrimes		/*
3934Srgrimes		 * Standard syntax:
3944Srgrimes		 * command [/modifier] [addr] [,count]
3954Srgrimes		 */
3964Srgrimes		t = db_read_token();
3974Srgrimes		if (t == tSLASH) {
3984Srgrimes		    t = db_read_token();
3994Srgrimes		    if (t != tIDENT) {
4004Srgrimes			db_printf("Bad modifier\n");
4014Srgrimes			db_flush_lex();
4024Srgrimes			return;
4034Srgrimes		    }
4044Srgrimes		    db_strcpy(modif, db_tok_string);
4054Srgrimes		}
4064Srgrimes		else {
4074Srgrimes		    db_unread_token(t);
4084Srgrimes		    modif[0] = '\0';
4094Srgrimes		}
4104Srgrimes
4114Srgrimes		if (db_expression(&addr)) {
4124Srgrimes		    db_dot = (db_addr_t) addr;
4134Srgrimes		    db_last_addr = db_dot;
4144Srgrimes		    have_addr = TRUE;
4154Srgrimes		}
4164Srgrimes		else {
4174Srgrimes		    addr = (db_expr_t) db_dot;
4184Srgrimes		    have_addr = FALSE;
4194Srgrimes		}
4204Srgrimes		t = db_read_token();
4214Srgrimes		if (t == tCOMMA) {
4224Srgrimes		    if (!db_expression(&count)) {
4234Srgrimes			db_printf("Count missing\n");
4244Srgrimes			db_flush_lex();
4254Srgrimes			return;
4264Srgrimes		    }
4274Srgrimes		}
4284Srgrimes		else {
4294Srgrimes		    db_unread_token(t);
4304Srgrimes		    count = -1;
4314Srgrimes		}
4324Srgrimes		if ((cmd->flag & CS_MORE) == 0) {
4334Srgrimes		    db_skip_to_eol();
4344Srgrimes		}
4354Srgrimes	    }
4364Srgrimes	}
4374Srgrimes	*last_cmdp = cmd;
4384Srgrimes	if (cmd != 0) {
4394Srgrimes	    /*
4404Srgrimes	     * Execute the command.
4414Srgrimes	     */
442174914Srwatson	    if (dopager)
443174914Srwatson		db_enable_pager();
444174914Srwatson	    else
445174914Srwatson		db_disable_pager();
4464Srgrimes	    (*cmd->fcn)(addr, have_addr, count, modif);
447174914Srwatson	    if (dopager)
448174914Srwatson		db_disable_pager();
4494Srgrimes
4504Srgrimes	    if (cmd->flag & CS_SET_DOT) {
4514Srgrimes		/*
4524Srgrimes		 * If command changes dot, set dot to
4534Srgrimes		 * previous address displayed (if 'ed' style).
4544Srgrimes		 */
4554Srgrimes		if (db_ed_style) {
4564Srgrimes		    db_dot = db_prev;
4574Srgrimes		}
4584Srgrimes		else {
4594Srgrimes		    db_dot = db_next;
4604Srgrimes		}
4614Srgrimes	    }
4624Srgrimes	    else {
4634Srgrimes		/*
4644Srgrimes		 * If command does not change dot,
4654Srgrimes		 * set 'next' location to be the same.
4664Srgrimes		 */
4674Srgrimes		db_next = db_dot;
4684Srgrimes	    }
4694Srgrimes	}
4704Srgrimes}
4714Srgrimes
4724Srgrimes/*
47333296Sbde * At least one non-optional command must be implemented using
47433296Sbde * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
47533296Sbde */
47633296SbdeDB_COMMAND(panic, db_panic)
4776204Sphk{
478160505Sjhb	db_disable_pager();
4797170Sdg	panic("from debugger");
4806204Sphk}
4816204Sphk
4826204Sphkvoid
4834Srgrimesdb_command_loop()
4844Srgrimes{
4854Srgrimes	/*
4864Srgrimes	 * Initialize 'prev' and 'next' to dot.
4874Srgrimes	 */
4884Srgrimes	db_prev = db_dot;
4894Srgrimes	db_next = db_dot;
4904Srgrimes
4914Srgrimes	db_cmd_loop_done = 0;
4924Srgrimes	while (!db_cmd_loop_done) {
4934Srgrimes	    if (db_print_position() != 0)
4944Srgrimes		db_printf("\n");
4954Srgrimes
4964Srgrimes	    db_printf("db> ");
4974Srgrimes	    (void) db_read_line();
4984Srgrimes
499183054Ssam	    db_command(&db_last_command, &db_cmd_table, /* dopager */ 1);
5004Srgrimes	}
5014Srgrimes}
5024Srgrimes
503174914Srwatson/*
504174914Srwatson * Execute a command on behalf of a script.  The caller is responsible for
505174914Srwatson * making sure that the command string is < DB_MAXLINE or it will be
506174914Srwatson * truncated.
507174914Srwatson *
508174914Srwatson * XXXRW: Runs by injecting faked input into DDB input stream; it would be
509174914Srwatson * nicer to use an alternative approach that didn't mess with the previous
510174914Srwatson * command buffer.
511174914Srwatson */
5124Srgrimesvoid
513174914Srwatsondb_command_script(const char *command)
514174914Srwatson{
515174914Srwatson	db_prev = db_next = db_dot;
516174914Srwatson	db_inject_line(command);
517183054Ssam	db_command(&db_last_command, &db_cmd_table, /* dopager */ 0);
518174914Srwatson}
519174914Srwatson
520174914Srwatsonvoid
5214Srgrimesdb_error(s)
522103746Smarkm	const char *s;
5234Srgrimes{
5244Srgrimes	if (s)
52579884Skris	    db_printf("%s", s);
5264Srgrimes	db_flush_lex();
527131952Smarcel	kdb_reenter();
5284Srgrimes}
5294Srgrimes
5304Srgrimes
5314Srgrimes/*
5324Srgrimes * Call random function:
5334Srgrimes * !expr(arg,arg,arg)
5344Srgrimes */
535147745Smarcel
536147745Smarcel/* The generic implementation supports a maximum of 10 arguments. */
537147745Smarceltypedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
538147745Smarcel    db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
539147745Smarcel
540147745Smarcelstatic __inline int
541147745Smarceldb_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
542147745Smarcel{
543147745Smarcel	__db_f *f = (__db_f *)addr;
544147745Smarcel
545147745Smarcel	if (nargs > 10) {
546147745Smarcel		db_printf("Too many arguments (max 10)\n");
547147745Smarcel		return (0);
548147745Smarcel	}
549147745Smarcel	*rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
550147745Smarcel	    args[6], args[7], args[8], args[9]);
551147745Smarcel	return (1);
552147745Smarcel}
553147745Smarcel
55412515Sphkstatic void
55510348Sbdedb_fncall(dummy1, dummy2, dummy3, dummy4)
55610348Sbde	db_expr_t	dummy1;
55710348Sbde	boolean_t	dummy2;
55810348Sbde	db_expr_t	dummy3;
55910348Sbde	char *		dummy4;
5604Srgrimes{
5614Srgrimes	db_expr_t	fn_addr;
562147745Smarcel	db_expr_t	args[DB_MAXARGS];
5634Srgrimes	int		nargs = 0;
5644Srgrimes	db_expr_t	retval;
5654Srgrimes	int		t;
5664Srgrimes
5674Srgrimes	if (!db_expression(&fn_addr)) {
5684Srgrimes	    db_printf("Bad function\n");
5694Srgrimes	    db_flush_lex();
5704Srgrimes	    return;
5714Srgrimes	}
5724Srgrimes
5734Srgrimes	t = db_read_token();
5744Srgrimes	if (t == tLPAREN) {
5754Srgrimes	    if (db_expression(&args[0])) {
5764Srgrimes		nargs++;
5774Srgrimes		while ((t = db_read_token()) == tCOMMA) {
578147745Smarcel		    if (nargs == DB_MAXARGS) {
579147745Smarcel			db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
5804Srgrimes			db_flush_lex();
5814Srgrimes			return;
5824Srgrimes		    }
5834Srgrimes		    if (!db_expression(&args[nargs])) {
5844Srgrimes			db_printf("Argument missing\n");
5854Srgrimes			db_flush_lex();
5864Srgrimes			return;
5874Srgrimes		    }
5884Srgrimes		    nargs++;
5894Srgrimes		}
5904Srgrimes		db_unread_token(t);
5914Srgrimes	    }
5924Srgrimes	    if (db_read_token() != tRPAREN) {
5934Srgrimes		db_printf("?\n");
5944Srgrimes		db_flush_lex();
5954Srgrimes		return;
5964Srgrimes	    }
5974Srgrimes	}
5984Srgrimes	db_skip_to_eol();
599160505Sjhb	db_disable_pager();
6004Srgrimes
601147745Smarcel	if (DB_CALL(fn_addr, &retval, nargs, args))
602147745Smarcel		db_printf("= %#lr\n", (long)retval);
6034Srgrimes}
60417848Spst
60517848Spststatic void
606163192Sbdedb_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
607163192Sbde{
608163192Sbde
609163192Sbde	cpu_halt();
610163192Sbde}
611163192Sbde
612163192Sbdestatic void
61386998Sdddb_kill(dummy1, dummy2, dummy3, dummy4)
61486998Sdd	db_expr_t	dummy1;
61586998Sdd	boolean_t	dummy2;
61686998Sdd	db_expr_t	dummy3;
61786998Sdd	char *		dummy4;
61886998Sdd{
61986998Sdd	db_expr_t old_radix, pid, sig;
62086998Sdd	struct proc *p;
62186998Sdd
62286998Sdd#define DB_ERROR(f)	do { db_printf f; db_flush_lex(); goto out; } while (0)
62386998Sdd
62486998Sdd	/*
62586998Sdd	 * PIDs and signal numbers are typically represented in base
62686998Sdd	 * 10, so make that the default here.  It can, of course, be
62786998Sdd	 * overridden by specifying a prefix.
62886998Sdd	 */
62986998Sdd	old_radix = db_radix;
63086998Sdd	db_radix = 10;
63186998Sdd	/* Retrieve arguments. */
63286998Sdd	if (!db_expression(&sig))
63386998Sdd		DB_ERROR(("Missing signal number\n"));
63486998Sdd	if (!db_expression(&pid))
63586998Sdd		DB_ERROR(("Missing process ID\n"));
63686998Sdd	db_skip_to_eol();
637176897Srwatson	if (sig < 1 || sig > _SIG_MAXSIG)
63886998Sdd		DB_ERROR(("Signal number out of range\n"));
63986998Sdd
64086998Sdd	/*
64186998Sdd	 * Find the process in question.  allproc_lock is not needed
64286998Sdd	 * since we're in DDB.
64386998Sdd	 */
64486998Sdd	/* sx_slock(&allproc_lock); */
645166074Sdelphij	FOREACH_PROC_IN_SYSTEM(p)
64686998Sdd	    if (p->p_pid == pid)
64786998Sdd		    break;
64886998Sdd	/* sx_sunlock(&allproc_lock); */
64986998Sdd	if (p == NULL)
65089442Smjacob		DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
65186998Sdd
65286998Sdd	/* If it's already locked, bail; otherwise, do the deed. */
65386998Sdd	if (PROC_TRYLOCK(p) == 0)
65489442Smjacob		DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
65586998Sdd	else {
65686998Sdd		psignal(p, sig);
65786998Sdd		PROC_UNLOCK(p);
65886998Sdd	}
65986998Sdd
66086998Sddout:
66186998Sdd	db_radix = old_radix;
66286998Sdd#undef DB_ERROR
66386998Sdd}
66486998Sdd
66586998Sddstatic void
66685944Speterdb_reset(dummy1, dummy2, dummy3, dummy4)
66785944Speter	db_expr_t	dummy1;
66885944Speter	boolean_t	dummy2;
66985944Speter	db_expr_t	dummy3;
67085944Speter	char *		dummy4;
67185944Speter{
67285944Speter
67385944Speter	cpu_reset();
67485944Speter}
675126399Sphk
676126399Sphkstatic void
677126399Sphkdb_watchdog(dummy1, dummy2, dummy3, dummy4)
678126399Sphk	db_expr_t	dummy1;
679126399Sphk	boolean_t	dummy2;
680126399Sphk	db_expr_t	dummy3;
681126399Sphk	char *		dummy4;
682126399Sphk{
683126399Sphk	int i;
684126399Sphk
685126399Sphk	/*
686126399Sphk	 * XXX: It might make sense to be able to set the watchdog to a
687126399Sphk	 * XXX: timeout here so that failure or hang as a result of subsequent
688126399Sphk	 * XXX: ddb commands could be recovered by a reset.
689126399Sphk	 */
690126399Sphk
691126399Sphk	EVENTHANDLER_INVOKE(watchdog_list, 0, &i);
692126399Sphk}
693132002Smarcel
694132002Smarcelstatic void
695132002Smarceldb_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
696132002Smarcel{
697132002Smarcel
698132002Smarcel	if (kdb_dbbe_select("gdb") != 0)
699132002Smarcel		db_printf("The remote GDB backend could not be selected.\n");
700132002Smarcel	else
701132002Smarcel		db_printf("Step to enter the remote GDB backend.\n");
702132002Smarcel}
703132482Smarcel
704132482Smarcelstatic void
705132482Smarceldb_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
706132482Smarcel{
707132482Smarcel	struct thread *td;
708132482Smarcel	db_expr_t radix;
709138038Srwatson	pid_t pid;
710132482Smarcel	int t;
711132482Smarcel
712132482Smarcel	/*
713132482Smarcel	 * We parse our own arguments. We don't like the default radix.
714132482Smarcel	 */
715132482Smarcel	radix = db_radix;
716132482Smarcel	db_radix = 10;
717132482Smarcel	hastid = db_expression(&tid);
718132482Smarcel	t = db_read_token();
719132482Smarcel	if (t == tCOMMA) {
720132482Smarcel		if (!db_expression(&count)) {
721132482Smarcel			db_printf("Count missing\n");
722132482Smarcel			db_flush_lex();
723132482Smarcel			return;
724132482Smarcel		}
725132482Smarcel	} else {
726132482Smarcel		db_unread_token(t);
727132482Smarcel		count = -1;
728132482Smarcel	}
729132482Smarcel	db_skip_to_eol();
730132482Smarcel	db_radix = radix;
731132482Smarcel
732132482Smarcel	if (hastid) {
733132482Smarcel		td = kdb_thr_lookup((lwpid_t)tid);
734132482Smarcel		if (td == NULL)
735132482Smarcel			td = kdb_thr_from_pid((pid_t)tid);
736132482Smarcel		if (td == NULL) {
737132482Smarcel			db_printf("Thread %d not found\n", (int)tid);
738132482Smarcel			return;
739132482Smarcel		}
740132482Smarcel	} else
741132482Smarcel		td = kdb_thread;
742138038Srwatson	if (td->td_proc != NULL)
743138038Srwatson		pid = td->td_proc->p_pid;
744138038Srwatson	else
745138038Srwatson		pid = -1;
746138038Srwatson	db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
747132482Smarcel	db_trace_thread(td, count);
748132482Smarcel}
749150819Srwatson
750150819Srwatsonstatic void
751150819Srwatsondb_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
752150819Srwatson    char *dummy4)
753150819Srwatson{
754150819Srwatson	struct proc *p;
755150819Srwatson	struct thread *td;
756163909Skib	jmp_buf jb;
757163909Skib	void *prev_jb;
758150819Srwatson
759166074Sdelphij	FOREACH_PROC_IN_SYSTEM(p) {
760163909Skib		prev_jb = kdb_jmpbuf(jb);
761163909Skib		if (setjmp(jb) == 0) {
762163909Skib			FOREACH_THREAD_IN_PROC(p, td) {
763163909Skib				db_printf("\nTracing command %s pid %d tid %ld td %p\n",
764163909Skib					  p->p_comm, p->p_pid, (long)td->td_tid, td);
765163909Skib				db_trace_thread(td, -1);
766163909Skib				if (db_pager_quit) {
767163909Skib					kdb_jmpbuf(prev_jb);
768163909Skib					return;
769163909Skib				}
770163909Skib			}
771150819Srwatson		}
772163909Skib		kdb_jmpbuf(prev_jb);
773150819Srwatson	}
774150819Srwatson}
775