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
160273265Spfgdb_skip_to_eol(void)
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
255273265Spfgdb_cmd_match(char *name, struct command *cmd, struct command **cmdp,
256273265Spfg    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
291273265Spfgdb_cmd_search(char *name, struct command_table *table, struct command **cmdp)
292156412Sjhb{
293156412Sjhb	struct command	*cmd;
294156412Sjhb	int		result = CMD_NONE;
295156412Sjhb
296183054Ssam	LIST_FOREACH(cmd, table, next) {
297183054Ssam		db_cmd_match(name,cmd,cmdp,&result);
298156412Sjhb		if (result == CMD_UNIQUE)
299183054Ssam			break;
300156412Sjhb	}
301183054Ssam
3024Srgrimes	if (result == CMD_NONE) {
303156412Sjhb		/* check for 'help' */
3044Srgrimes		if (name[0] == 'h' && name[1] == 'e'
3054Srgrimes		    && name[2] == 'l' && name[3] == 'p')
3064Srgrimes			result = CMD_HELP;
3074Srgrimes	}
3084Srgrimes	return (result);
3094Srgrimes}
3104Srgrimes
31112515Sphkstatic void
312273265Spfgdb_cmd_list(struct command_table *table)
3134Srgrimes{
314183054Ssam	register struct command	*cmd;
3154Srgrimes
316183054Ssam	LIST_FOREACH(cmd, table, next) {
317183054Ssam		db_printf("%-12s", cmd->name);
318183054Ssam		db_end_line(12);
3194Srgrimes	}
3204Srgrimes}
3214Srgrimes
32212515Sphkstatic void
323273265Spfgdb_command(struct command **last_cmdp, struct command_table *cmd_table,
324273265Spfg    int dopager)
3254Srgrimes{
326183054Ssam	struct command	*cmd = NULL;
3274Srgrimes	int		t;
3284Srgrimes	char		modif[TOK_STRING_SIZE];
3294Srgrimes	db_expr_t	addr, count;
330798Swollman	boolean_t	have_addr = FALSE;
3314Srgrimes	int		result;
3324Srgrimes
3334Srgrimes	t = db_read_token();
3344Srgrimes	if (t == tEOL) {
3354Srgrimes	    /* empty line repeats last command, at 'next' */
3364Srgrimes	    cmd = *last_cmdp;
3374Srgrimes	    addr = (db_expr_t)db_next;
3384Srgrimes	    have_addr = FALSE;
3394Srgrimes	    count = 1;
3404Srgrimes	    modif[0] = '\0';
3414Srgrimes	}
3424Srgrimes	else if (t == tEXCL) {
34310348Sbde	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
3444Srgrimes	    return;
3454Srgrimes	}
3464Srgrimes	else if (t != tIDENT) {
3474Srgrimes	    db_printf("?\n");
3484Srgrimes	    db_flush_lex();
3494Srgrimes	    return;
3504Srgrimes	}
3514Srgrimes	else {
3524Srgrimes	    /*
3534Srgrimes	     * Search for command
3544Srgrimes	     */
3554Srgrimes	    while (cmd_table) {
3564Srgrimes		result = db_cmd_search(db_tok_string,
3574Srgrimes				       cmd_table,
3584Srgrimes				       &cmd);
3594Srgrimes		switch (result) {
3604Srgrimes		    case CMD_NONE:
3614Srgrimes			db_printf("No such command\n");
3624Srgrimes			db_flush_lex();
3634Srgrimes			return;
3644Srgrimes		    case CMD_AMBIGUOUS:
3654Srgrimes			db_printf("Ambiguous\n");
3664Srgrimes			db_flush_lex();
3674Srgrimes			return;
3684Srgrimes		    case CMD_HELP:
369156412Sjhb			db_cmd_list(cmd_table);
3704Srgrimes			db_flush_lex();
3714Srgrimes			return;
3724Srgrimes		    default:
3734Srgrimes			break;
3744Srgrimes		}
375156412Sjhb		if ((cmd_table = cmd->more) != NULL) {
3764Srgrimes		    t = db_read_token();
3774Srgrimes		    if (t != tIDENT) {
378156412Sjhb			db_cmd_list(cmd_table);
3794Srgrimes			db_flush_lex();
3804Srgrimes			return;
3814Srgrimes		    }
3824Srgrimes		}
3834Srgrimes	    }
3844Srgrimes
3854Srgrimes	    if ((cmd->flag & CS_OWN) == 0) {
3864Srgrimes		/*
3874Srgrimes		 * Standard syntax:
3884Srgrimes		 * command [/modifier] [addr] [,count]
3894Srgrimes		 */
3904Srgrimes		t = db_read_token();
3914Srgrimes		if (t == tSLASH) {
3924Srgrimes		    t = db_read_token();
3934Srgrimes		    if (t != tIDENT) {
3944Srgrimes			db_printf("Bad modifier\n");
3954Srgrimes			db_flush_lex();
3964Srgrimes			return;
3974Srgrimes		    }
3984Srgrimes		    db_strcpy(modif, db_tok_string);
3994Srgrimes		}
4004Srgrimes		else {
4014Srgrimes		    db_unread_token(t);
4024Srgrimes		    modif[0] = '\0';
4034Srgrimes		}
4044Srgrimes
4054Srgrimes		if (db_expression(&addr)) {
4064Srgrimes		    db_dot = (db_addr_t) addr;
4074Srgrimes		    db_last_addr = db_dot;
4084Srgrimes		    have_addr = TRUE;
4094Srgrimes		}
4104Srgrimes		else {
4114Srgrimes		    addr = (db_expr_t) db_dot;
4124Srgrimes		    have_addr = FALSE;
4134Srgrimes		}
4144Srgrimes		t = db_read_token();
4154Srgrimes		if (t == tCOMMA) {
4164Srgrimes		    if (!db_expression(&count)) {
4174Srgrimes			db_printf("Count missing\n");
4184Srgrimes			db_flush_lex();
4194Srgrimes			return;
4204Srgrimes		    }
4214Srgrimes		}
4224Srgrimes		else {
4234Srgrimes		    db_unread_token(t);
4244Srgrimes		    count = -1;
4254Srgrimes		}
4264Srgrimes		if ((cmd->flag & CS_MORE) == 0) {
4274Srgrimes		    db_skip_to_eol();
4284Srgrimes		}
4294Srgrimes	    }
4304Srgrimes	}
4314Srgrimes	*last_cmdp = cmd;
4324Srgrimes	if (cmd != 0) {
4334Srgrimes	    /*
4344Srgrimes	     * Execute the command.
4354Srgrimes	     */
436174914Srwatson	    if (dopager)
437174914Srwatson		db_enable_pager();
438174914Srwatson	    else
439174914Srwatson		db_disable_pager();
4404Srgrimes	    (*cmd->fcn)(addr, have_addr, count, modif);
441174914Srwatson	    if (dopager)
442174914Srwatson		db_disable_pager();
4434Srgrimes
4444Srgrimes	    if (cmd->flag & CS_SET_DOT) {
4454Srgrimes		/*
4464Srgrimes		 * If command changes dot, set dot to
4474Srgrimes		 * previous address displayed (if 'ed' style).
4484Srgrimes		 */
4494Srgrimes		if (db_ed_style) {
4504Srgrimes		    db_dot = db_prev;
4514Srgrimes		}
4524Srgrimes		else {
4534Srgrimes		    db_dot = db_next;
4544Srgrimes		}
4554Srgrimes	    }
4564Srgrimes	    else {
4574Srgrimes		/*
4584Srgrimes		 * If command does not change dot,
4594Srgrimes		 * set 'next' location to be the same.
4604Srgrimes		 */
4614Srgrimes		db_next = db_dot;
4624Srgrimes	    }
4634Srgrimes	}
4644Srgrimes}
4654Srgrimes
4664Srgrimes/*
46733296Sbde * At least one non-optional command must be implemented using
46833296Sbde * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
46933296Sbde */
47033296SbdeDB_COMMAND(panic, db_panic)
4716204Sphk{
472160505Sjhb	db_disable_pager();
4737170Sdg	panic("from debugger");
4746204Sphk}
4756204Sphk
4766204Sphkvoid
477273265Spfgdb_command_loop(void)
4784Srgrimes{
4794Srgrimes	/*
4804Srgrimes	 * Initialize 'prev' and 'next' to dot.
4814Srgrimes	 */
4824Srgrimes	db_prev = db_dot;
4834Srgrimes	db_next = db_dot;
4844Srgrimes
4854Srgrimes	db_cmd_loop_done = 0;
4864Srgrimes	while (!db_cmd_loop_done) {
4874Srgrimes	    if (db_print_position() != 0)
4884Srgrimes		db_printf("\n");
4894Srgrimes
4904Srgrimes	    db_printf("db> ");
4914Srgrimes	    (void) db_read_line();
4924Srgrimes
493183054Ssam	    db_command(&db_last_command, &db_cmd_table, /* dopager */ 1);
4944Srgrimes	}
4954Srgrimes}
4964Srgrimes
497174914Srwatson/*
498174914Srwatson * Execute a command on behalf of a script.  The caller is responsible for
499174914Srwatson * making sure that the command string is < DB_MAXLINE or it will be
500174914Srwatson * truncated.
501174914Srwatson *
502174914Srwatson * XXXRW: Runs by injecting faked input into DDB input stream; it would be
503174914Srwatson * nicer to use an alternative approach that didn't mess with the previous
504174914Srwatson * command buffer.
505174914Srwatson */
5064Srgrimesvoid
507174914Srwatsondb_command_script(const char *command)
508174914Srwatson{
509174914Srwatson	db_prev = db_next = db_dot;
510174914Srwatson	db_inject_line(command);
511183054Ssam	db_command(&db_last_command, &db_cmd_table, /* dopager */ 0);
512174914Srwatson}
513174914Srwatson
514174914Srwatsonvoid
515273265Spfgdb_error(const char *s)
5164Srgrimes{
5174Srgrimes	if (s)
51879884Skris	    db_printf("%s", s);
5194Srgrimes	db_flush_lex();
520131952Smarcel	kdb_reenter();
5214Srgrimes}
5224Srgrimes
523222801Smarcelstatic void
524222801Smarceldb_dump(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
525222801Smarcel{
526222801Smarcel	int error;
5274Srgrimes
528242424Salfred	if (textdump_pending) {
529242424Salfred		db_printf("textdump_pending set.\n"
530242424Salfred		    "run \"textdump unset\" first or \"textdump dump\" for a textdump.\n");
531242424Salfred		return;
532242424Salfred	}
533222801Smarcel	error = doadump(FALSE);
534222801Smarcel	if (error) {
535222801Smarcel		db_printf("Cannot dump: ");
536222801Smarcel		switch (error) {
537222801Smarcel		case EBUSY:
538222801Smarcel			db_printf("debugger got invoked while dumping.\n");
539222801Smarcel			break;
540222801Smarcel		case ENXIO:
541222801Smarcel			db_printf("no dump device specified.\n");
542222801Smarcel			break;
543222801Smarcel		default:
544222801Smarcel			db_printf("unknown error (error=%d).\n", error);
545222801Smarcel			break;
546222801Smarcel		}
547222801Smarcel	}
548222801Smarcel}
549222801Smarcel
5504Srgrimes/*
5514Srgrimes * Call random function:
5524Srgrimes * !expr(arg,arg,arg)
5534Srgrimes */
554147745Smarcel
555147745Smarcel/* The generic implementation supports a maximum of 10 arguments. */
556147745Smarceltypedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
557147745Smarcel    db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
558147745Smarcel
559147745Smarcelstatic __inline int
560147745Smarceldb_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
561147745Smarcel{
562147745Smarcel	__db_f *f = (__db_f *)addr;
563147745Smarcel
564147745Smarcel	if (nargs > 10) {
565147745Smarcel		db_printf("Too many arguments (max 10)\n");
566147745Smarcel		return (0);
567147745Smarcel	}
568147745Smarcel	*rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
569147745Smarcel	    args[6], args[7], args[8], args[9]);
570147745Smarcel	return (1);
571147745Smarcel}
572147745Smarcel
57312515Sphkstatic void
574273265Spfgdb_fncall(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
5754Srgrimes{
5764Srgrimes	db_expr_t	fn_addr;
577147745Smarcel	db_expr_t	args[DB_MAXARGS];
5784Srgrimes	int		nargs = 0;
5794Srgrimes	db_expr_t	retval;
5804Srgrimes	int		t;
5814Srgrimes
5824Srgrimes	if (!db_expression(&fn_addr)) {
5834Srgrimes	    db_printf("Bad function\n");
5844Srgrimes	    db_flush_lex();
5854Srgrimes	    return;
5864Srgrimes	}
5874Srgrimes
5884Srgrimes	t = db_read_token();
5894Srgrimes	if (t == tLPAREN) {
5904Srgrimes	    if (db_expression(&args[0])) {
5914Srgrimes		nargs++;
5924Srgrimes		while ((t = db_read_token()) == tCOMMA) {
593147745Smarcel		    if (nargs == DB_MAXARGS) {
594147745Smarcel			db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
5954Srgrimes			db_flush_lex();
5964Srgrimes			return;
5974Srgrimes		    }
5984Srgrimes		    if (!db_expression(&args[nargs])) {
5994Srgrimes			db_printf("Argument missing\n");
6004Srgrimes			db_flush_lex();
6014Srgrimes			return;
6024Srgrimes		    }
6034Srgrimes		    nargs++;
6044Srgrimes		}
6054Srgrimes		db_unread_token(t);
6064Srgrimes	    }
6074Srgrimes	    if (db_read_token() != tRPAREN) {
6084Srgrimes		db_printf("?\n");
6094Srgrimes		db_flush_lex();
6104Srgrimes		return;
6114Srgrimes	    }
6124Srgrimes	}
6134Srgrimes	db_skip_to_eol();
614160505Sjhb	db_disable_pager();
6154Srgrimes
616147745Smarcel	if (DB_CALL(fn_addr, &retval, nargs, args))
617147745Smarcel		db_printf("= %#lr\n", (long)retval);
6184Srgrimes}
61917848Spst
62017848Spststatic void
621163192Sbdedb_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
622163192Sbde{
623163192Sbde
624163192Sbde	cpu_halt();
625163192Sbde}
626163192Sbde
627163192Sbdestatic void
628273265Spfgdb_kill(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
62986998Sdd{
63086998Sdd	db_expr_t old_radix, pid, sig;
63186998Sdd	struct proc *p;
63286998Sdd
633273265Spfg#define	DB_ERROR(f)	do { db_printf f; db_flush_lex(); goto out; } while (0)
63486998Sdd
63586998Sdd	/*
63686998Sdd	 * PIDs and signal numbers are typically represented in base
63786998Sdd	 * 10, so make that the default here.  It can, of course, be
63886998Sdd	 * overridden by specifying a prefix.
63986998Sdd	 */
64086998Sdd	old_radix = db_radix;
64186998Sdd	db_radix = 10;
64286998Sdd	/* Retrieve arguments. */
64386998Sdd	if (!db_expression(&sig))
64486998Sdd		DB_ERROR(("Missing signal number\n"));
64586998Sdd	if (!db_expression(&pid))
64686998Sdd		DB_ERROR(("Missing process ID\n"));
64786998Sdd	db_skip_to_eol();
648209934Skib	if (!_SIG_VALID(sig))
64986998Sdd		DB_ERROR(("Signal number out of range\n"));
65086998Sdd
65186998Sdd	/*
65286998Sdd	 * Find the process in question.  allproc_lock is not needed
65386998Sdd	 * since we're in DDB.
65486998Sdd	 */
65586998Sdd	/* sx_slock(&allproc_lock); */
656166074Sdelphij	FOREACH_PROC_IN_SYSTEM(p)
65786998Sdd	    if (p->p_pid == pid)
65886998Sdd		    break;
65986998Sdd	/* sx_sunlock(&allproc_lock); */
66086998Sdd	if (p == NULL)
66189442Smjacob		DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
66286998Sdd
66386998Sdd	/* If it's already locked, bail; otherwise, do the deed. */
66486998Sdd	if (PROC_TRYLOCK(p) == 0)
66589442Smjacob		DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
66686998Sdd	else {
667199355Skib		pksignal(p, sig, NULL);
66886998Sdd		PROC_UNLOCK(p);
66986998Sdd	}
67086998Sdd
67186998Sddout:
67286998Sdd	db_radix = old_radix;
67386998Sdd#undef DB_ERROR
67486998Sdd}
67586998Sdd
676208509Sbz/*
677208509Sbz * Reboot.  In case there is an additional argument, take it as delay in
678208509Sbz * seconds.  Default to 15s if we cannot parse it and make sure we will
679208509Sbz * never wait longer than 1 week.  Some code is similar to
680208509Sbz * kern_shutdown.c:shutdown_panic().
681208509Sbz */
682208509Sbz#ifndef	DB_RESET_MAXDELAY
683208509Sbz#define	DB_RESET_MAXDELAY	(3600 * 24 * 7)
684208509Sbz#endif
685208509Sbz
68686998Sddstatic void
687208509Sbzdb_reset(db_expr_t addr, boolean_t have_addr, db_expr_t count __unused,
688208509Sbz    char *modif __unused)
68985944Speter{
690208509Sbz	int delay, loop;
69185944Speter
692208509Sbz	if (have_addr) {
693208509Sbz		delay = (int)db_hex2dec(addr);
694208509Sbz
695208509Sbz		/* If we parse to fail, use 15s. */
696208509Sbz		if (delay == -1)
697208509Sbz			delay = 15;
698208509Sbz
699208509Sbz		/* Cap at one week. */
700208509Sbz		if ((uintmax_t)delay > (uintmax_t)DB_RESET_MAXDELAY)
701208509Sbz			delay = DB_RESET_MAXDELAY;
702208509Sbz
703208509Sbz		db_printf("Automatic reboot in %d seconds - "
704208509Sbz		    "press a key on the console to abort\n", delay);
705208509Sbz		for (loop = delay * 10; loop > 0; --loop) {
706208509Sbz			DELAY(1000 * 100); /* 1/10th second */
707208509Sbz			/* Did user type a key? */
708208509Sbz			if (cncheckc() != -1)
709208509Sbz				return;
710208509Sbz		}
711208509Sbz	}
712208509Sbz
71385944Speter	cpu_reset();
71485944Speter}
715126399Sphk
716126399Sphkstatic void
717273265Spfgdb_watchdog(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
718126399Sphk{
719220362Sattilio	db_expr_t old_radix, tout;
720220362Sattilio	int err, i;
721126399Sphk
722220362Sattilio	old_radix = db_radix;
723220362Sattilio	db_radix = 10;
724220362Sattilio	err = db_expression(&tout);
725220362Sattilio	db_skip_to_eol();
726220362Sattilio	db_radix = old_radix;
727126399Sphk
728220362Sattilio	/* If no argument is provided the watchdog will just be disabled. */
729220362Sattilio	if (err == 0) {
730220362Sattilio		db_printf("No argument provided, disabling watchdog\n");
731220362Sattilio		tout = 0;
732220362Sattilio	} else if ((tout & WD_INTERVAL) == WD_TO_NEVER) {
733220362Sattilio		db_error("Out of range watchdog interval\n");
734220362Sattilio		return;
735220362Sattilio	}
736220362Sattilio	EVENTHANDLER_INVOKE(watchdog_list, tout, &i);
737126399Sphk}
738132002Smarcel
739132002Smarcelstatic void
740132002Smarceldb_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
741132002Smarcel{
742132002Smarcel
743218825Smdf	if (kdb_dbbe_select("gdb") != 0) {
744132002Smarcel		db_printf("The remote GDB backend could not be selected.\n");
745218825Smdf		return;
746218825Smdf	}
747218825Smdf	/*
748218825Smdf	 * Mark that we are done in the debugger.  kdb_trap()
749218825Smdf	 * should re-enter with the new backend.
750218825Smdf	 */
751218825Smdf	db_cmd_loop_done = 1;
752218825Smdf	db_printf("(ctrl-c will return control to ddb)\n");
753132002Smarcel}
754132482Smarcel
755132482Smarcelstatic void
756132482Smarceldb_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
757132482Smarcel{
758132482Smarcel	struct thread *td;
759132482Smarcel	db_expr_t radix;
760138038Srwatson	pid_t pid;
761132482Smarcel	int t;
762132482Smarcel
763132482Smarcel	/*
764132482Smarcel	 * We parse our own arguments. We don't like the default radix.
765132482Smarcel	 */
766132482Smarcel	radix = db_radix;
767132482Smarcel	db_radix = 10;
768132482Smarcel	hastid = db_expression(&tid);
769132482Smarcel	t = db_read_token();
770132482Smarcel	if (t == tCOMMA) {
771132482Smarcel		if (!db_expression(&count)) {
772132482Smarcel			db_printf("Count missing\n");
773132482Smarcel			db_flush_lex();
774132482Smarcel			return;
775132482Smarcel		}
776132482Smarcel	} else {
777132482Smarcel		db_unread_token(t);
778132482Smarcel		count = -1;
779132482Smarcel	}
780132482Smarcel	db_skip_to_eol();
781132482Smarcel	db_radix = radix;
782132482Smarcel
783132482Smarcel	if (hastid) {
784132482Smarcel		td = kdb_thr_lookup((lwpid_t)tid);
785132482Smarcel		if (td == NULL)
786132482Smarcel			td = kdb_thr_from_pid((pid_t)tid);
787132482Smarcel		if (td == NULL) {
788132482Smarcel			db_printf("Thread %d not found\n", (int)tid);
789132482Smarcel			return;
790132482Smarcel		}
791132482Smarcel	} else
792132482Smarcel		td = kdb_thread;
793138038Srwatson	if (td->td_proc != NULL)
794138038Srwatson		pid = td->td_proc->p_pid;
795138038Srwatson	else
796138038Srwatson		pid = -1;
797138038Srwatson	db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
798132482Smarcel	db_trace_thread(td, count);
799132482Smarcel}
800150819Srwatson
801150819Srwatsonstatic void
802150819Srwatsondb_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
803150819Srwatson    char *dummy4)
804150819Srwatson{
805150819Srwatson	struct proc *p;
806150819Srwatson	struct thread *td;
807163909Skib	jmp_buf jb;
808163909Skib	void *prev_jb;
809150819Srwatson
810166074Sdelphij	FOREACH_PROC_IN_SYSTEM(p) {
811163909Skib		prev_jb = kdb_jmpbuf(jb);
812163909Skib		if (setjmp(jb) == 0) {
813163909Skib			FOREACH_THREAD_IN_PROC(p, td) {
814163909Skib				db_printf("\nTracing command %s pid %d tid %ld td %p\n",
815163909Skib					  p->p_comm, p->p_pid, (long)td->td_tid, td);
816163909Skib				db_trace_thread(td, -1);
817163909Skib				if (db_pager_quit) {
818163909Skib					kdb_jmpbuf(prev_jb);
819163909Skib					return;
820163909Skib				}
821163909Skib			}
822150819Srwatson		}
823163909Skib		kdb_jmpbuf(prev_jb);
824150819Srwatson	}
825150819Srwatson}
826208509Sbz
827208509Sbz/*
828208509Sbz * Take the parsed expression value from the command line that was parsed
829208509Sbz * as a hexadecimal value and convert it as if the expression was parsed
830208509Sbz * as a decimal value.  Returns -1 if the expression was not a valid
831208509Sbz * decimal value.
832208509Sbz */
833208509Sbzdb_expr_t
834208509Sbzdb_hex2dec(db_expr_t expr)
835208509Sbz{
836208509Sbz	uintptr_t x, y;
837208509Sbz	db_expr_t val;
838208509Sbz
839208509Sbz	y = 1;
840208509Sbz	val = 0;
841208509Sbz	x = expr;
842208509Sbz	while (x != 0) {
843208509Sbz		if (x % 16 > 9)
844208509Sbz			return (-1);
845208509Sbz		val += (x % 16) * (y);
846208509Sbz		x >>= 4;
847208509Sbz		y *= 10;
848208509Sbz	}
849208509Sbz	return (val);
850208509Sbz}
851