db_command.c revision 324265
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: stable/11/sys/ddb/db_command.c 324265 2017-10-04 11:53:05Z trasz $");
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 */
62283248Spfgbool		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[] = {
83283296Spfg	{ "trace",	db_stack_trace_all,	0,	NULL },
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 },
90283296Spfg	{ "registers",	db_show_regs,		0,	NULL },
91283296Spfg	{ "breaks",	db_listbreak_cmd, 	0,	NULL },
92283296Spfg	{ "threads",	db_show_threads,	0,	NULL },
93148919Sobrien};
94183054Ssamstruct command_table db_show_table = LIST_HEAD_INITIALIZER(db_show_table);
95148919Sobrien
96183054Ssamstatic struct command db_cmds[] = {
97283296Spfg	{ "print",	db_print_cmd,		0,	NULL },
98283296Spfg	{ "p",		db_print_cmd,		0,	NULL },
99283296Spfg	{ "examine",	db_examine_cmd,		CS_SET_DOT, NULL },
100283296Spfg	{ "x",		db_examine_cmd,		CS_SET_DOT, NULL },
101283296Spfg	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, NULL },
102283296Spfg	{ "set",	db_set_cmd,		CS_OWN,	NULL },
103283296Spfg	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
104283296Spfg	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
105283296Spfg	{ "delete",	db_delete_cmd,		0,	NULL },
106283296Spfg	{ "d",		db_delete_cmd,		0,	NULL },
107283296Spfg	{ "dump",	db_dump,		0,	NULL },
108283296Spfg	{ "break",	db_breakpoint_cmd,	0,	NULL },
109283296Spfg	{ "b",		db_breakpoint_cmd,	0,	NULL },
110283296Spfg	{ "dwatch",	db_deletewatch_cmd,	0,	NULL },
111283296Spfg	{ "watch",	db_watchpoint_cmd,	CS_MORE,NULL },
112283296Spfg	{ "dhwatch",	db_deletehwatch_cmd,	0,      NULL },
113283296Spfg	{ "hwatch",	db_hwatchpoint_cmd,	0,      NULL },
114283296Spfg	{ "step",	db_single_step_cmd,	0,	NULL },
115283296Spfg	{ "s",		db_single_step_cmd,	0,	NULL },
116283296Spfg	{ "continue",	db_continue_cmd,	0,	NULL },
117283296Spfg	{ "c",		db_continue_cmd,	0,	NULL },
118283296Spfg	{ "until",	db_trace_until_call_cmd,0,	NULL },
119283296Spfg	{ "next",	db_trace_until_matching_cmd,0,	NULL },
120283296Spfg	{ "match",	db_trace_until_matching_cmd,0,	NULL },
121283296Spfg	{ "trace",	db_stack_trace,		CS_OWN,	NULL },
122283296Spfg	{ "t",		db_stack_trace,		CS_OWN,	NULL },
123183054Ssam	/* XXX alias for all trace */
124283296Spfg	{ "alltrace",	db_stack_trace_all,	0,	NULL },
125283296Spfg	{ "where",	db_stack_trace,		CS_OWN,	NULL },
126283296Spfg	{ "bt",		db_stack_trace,		CS_OWN,	NULL },
127283296Spfg	{ "call",	db_fncall,		CS_OWN,	NULL },
128156412Sjhb	{ "show",	0,			0,	&db_show_table },
129283296Spfg	{ "ps",		db_ps,			0,	NULL },
130283296Spfg	{ "gdb",	db_gdb,			0,	NULL },
131283296Spfg	{ "halt",	db_halt,		0,	NULL },
132283296Spfg	{ "reboot",	db_reset,		0,	NULL },
133283296Spfg	{ "reset",	db_reset,		0,	NULL },
134283296Spfg	{ "kill",	db_kill,		CS_OWN,	NULL },
135283296Spfg	{ "watchdog",	db_watchdog,		CS_OWN,	NULL },
136283296Spfg	{ "thread",	db_set_thread,		CS_OWN,	NULL },
137283296Spfg	{ "run",	db_run_cmd,		CS_OWN,	NULL },
138283296Spfg	{ "script",	db_script_cmd,		CS_OWN,	NULL },
139283296Spfg	{ "scripts",	db_scripts_cmd,		0,	NULL },
140283296Spfg	{ "unscript",	db_unscript_cmd,	CS_OWN,	NULL },
141283296Spfg	{ "capture",	db_capture_cmd,		CS_OWN,	NULL },
142283296Spfg	{ "textdump",	db_textdump_cmd,	CS_OWN, NULL },
143283296Spfg	{ "findstack",	db_findstack_cmd,	0,	NULL },
144148919Sobrien};
145183054Ssamstruct command_table db_cmd_table = LIST_HEAD_INITIALIZER(db_cmd_table);
146148919Sobrien
147298073Spfgstatic struct command	*db_last_command = NULL;
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 */
154283248Spfgstatic bool	db_ed_style = true;
1554Srgrimes
1564Srgrimes/*
1574Srgrimes * Utility routine - discard tokens through end-of-line.
1584Srgrimes */
1594Srgrimesvoid
160273006Spfgdb_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
255273006Spfgdb_cmd_match(char *name, struct command *cmd, struct command **cmdp,
256273006Spfg    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
291273006Spfgdb_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
312273006Spfgdb_cmd_list(struct command_table *table)
3134Srgrimes{
314283315Spfg	struct command	*cmd;
315324265Strasz	int have_subcommands;
3164Srgrimes
317324265Strasz	have_subcommands = 0;
318183054Ssam	LIST_FOREACH(cmd, table, next) {
319324265Strasz		if (cmd->more != NULL)
320324265Strasz			have_subcommands++;
321266985Sjmg		db_printf("%-16s", cmd->name);
322266985Sjmg		db_end_line(16);
3234Srgrimes	}
324324265Strasz
325324265Strasz	if (have_subcommands > 0) {
326324265Strasz		db_printf("\nThe following have subcommands; append \"help\" "
327324265Strasz		    "to list (e.g. \"show help\"):\n");
328324265Strasz		LIST_FOREACH(cmd, table, next) {
329324265Strasz			if (cmd->more == NULL)
330324265Strasz				continue;
331324265Strasz			db_printf("%-16s", cmd->name);
332324265Strasz			db_end_line(16);
333324265Strasz		}
334324265Strasz	}
3354Srgrimes}
3364Srgrimes
33712515Sphkstatic void
338273006Spfgdb_command(struct command **last_cmdp, struct command_table *cmd_table,
339273006Spfg    int dopager)
3404Srgrimes{
341183054Ssam	struct command	*cmd = NULL;
3424Srgrimes	int		t;
3434Srgrimes	char		modif[TOK_STRING_SIZE];
3444Srgrimes	db_expr_t	addr, count;
345283248Spfg	bool		have_addr = false;
3464Srgrimes	int		result;
3474Srgrimes
3484Srgrimes	t = db_read_token();
3494Srgrimes	if (t == tEOL) {
3504Srgrimes	    /* empty line repeats last command, at 'next' */
3514Srgrimes	    cmd = *last_cmdp;
3524Srgrimes	    addr = (db_expr_t)db_next;
353283088Spfg	    have_addr = false;
3544Srgrimes	    count = 1;
3554Srgrimes	    modif[0] = '\0';
3564Srgrimes	}
3574Srgrimes	else if (t == tEXCL) {
358283248Spfg	    db_fncall((db_expr_t)0, (bool)false, (db_expr_t)0, (char *)0);
3594Srgrimes	    return;
3604Srgrimes	}
3614Srgrimes	else if (t != tIDENT) {
3624Srgrimes	    db_printf("?\n");
3634Srgrimes	    db_flush_lex();
3644Srgrimes	    return;
3654Srgrimes	}
3664Srgrimes	else {
3674Srgrimes	    /*
3684Srgrimes	     * Search for command
3694Srgrimes	     */
3704Srgrimes	    while (cmd_table) {
3714Srgrimes		result = db_cmd_search(db_tok_string,
3724Srgrimes				       cmd_table,
3734Srgrimes				       &cmd);
3744Srgrimes		switch (result) {
3754Srgrimes		    case CMD_NONE:
376324265Strasz			db_printf("No such command; use \"help\" "
377324265Strasz			    "to list available commands\n");
3784Srgrimes			db_flush_lex();
3794Srgrimes			return;
3804Srgrimes		    case CMD_AMBIGUOUS:
3814Srgrimes			db_printf("Ambiguous\n");
3824Srgrimes			db_flush_lex();
3834Srgrimes			return;
3844Srgrimes		    case CMD_HELP:
385324265Strasz			if (cmd_table == &db_cmd_table) {
386324265Strasz			    db_printf("This is ddb(4), the kernel debugger; "
387324265Strasz			        "see http://man.freebsd.org/ddb/4 for help.\n");
388324265Strasz			    db_printf("Use \"bt\" for backtrace, \"dump\" for "
389324265Strasz			        "kernel core dump, \"reset\" to reboot.\n");
390324265Strasz			    db_printf("Available commands:\n");
391324265Strasz			}
392156412Sjhb			db_cmd_list(cmd_table);
3934Srgrimes			db_flush_lex();
3944Srgrimes			return;
3954Srgrimes		    default:
3964Srgrimes			break;
3974Srgrimes		}
398156412Sjhb		if ((cmd_table = cmd->more) != NULL) {
3994Srgrimes		    t = db_read_token();
4004Srgrimes		    if (t != tIDENT) {
401324265Strasz			db_printf("Subcommand required; "
402324265Strasz			    "available subcommands:\n");
403156412Sjhb			db_cmd_list(cmd_table);
4044Srgrimes			db_flush_lex();
4054Srgrimes			return;
4064Srgrimes		    }
4074Srgrimes		}
4084Srgrimes	    }
4094Srgrimes
4104Srgrimes	    if ((cmd->flag & CS_OWN) == 0) {
4114Srgrimes		/*
4124Srgrimes		 * Standard syntax:
4134Srgrimes		 * command [/modifier] [addr] [,count]
4144Srgrimes		 */
4154Srgrimes		t = db_read_token();
4164Srgrimes		if (t == tSLASH) {
4174Srgrimes		    t = db_read_token();
4184Srgrimes		    if (t != tIDENT) {
4194Srgrimes			db_printf("Bad modifier\n");
4204Srgrimes			db_flush_lex();
4214Srgrimes			return;
4224Srgrimes		    }
4234Srgrimes		    db_strcpy(modif, db_tok_string);
4244Srgrimes		}
4254Srgrimes		else {
4264Srgrimes		    db_unread_token(t);
4274Srgrimes		    modif[0] = '\0';
4284Srgrimes		}
4294Srgrimes
4304Srgrimes		if (db_expression(&addr)) {
4314Srgrimes		    db_dot = (db_addr_t) addr;
4324Srgrimes		    db_last_addr = db_dot;
433283088Spfg		    have_addr = true;
4344Srgrimes		}
4354Srgrimes		else {
4364Srgrimes		    addr = (db_expr_t) db_dot;
437283088Spfg		    have_addr = false;
4384Srgrimes		}
4394Srgrimes		t = db_read_token();
4404Srgrimes		if (t == tCOMMA) {
4414Srgrimes		    if (!db_expression(&count)) {
4424Srgrimes			db_printf("Count missing\n");
4434Srgrimes			db_flush_lex();
4444Srgrimes			return;
4454Srgrimes		    }
4464Srgrimes		}
4474Srgrimes		else {
4484Srgrimes		    db_unread_token(t);
4494Srgrimes		    count = -1;
4504Srgrimes		}
4514Srgrimes		if ((cmd->flag & CS_MORE) == 0) {
4524Srgrimes		    db_skip_to_eol();
4534Srgrimes		}
4544Srgrimes	    }
4554Srgrimes	}
4564Srgrimes	*last_cmdp = cmd;
457298073Spfg	if (cmd != NULL) {
4584Srgrimes	    /*
4594Srgrimes	     * Execute the command.
4604Srgrimes	     */
461174914Srwatson	    if (dopager)
462174914Srwatson		db_enable_pager();
463174914Srwatson	    else
464174914Srwatson		db_disable_pager();
4654Srgrimes	    (*cmd->fcn)(addr, have_addr, count, modif);
466174914Srwatson	    if (dopager)
467174914Srwatson		db_disable_pager();
4684Srgrimes
4694Srgrimes	    if (cmd->flag & CS_SET_DOT) {
4704Srgrimes		/*
4714Srgrimes		 * If command changes dot, set dot to
4724Srgrimes		 * previous address displayed (if 'ed' style).
4734Srgrimes		 */
4744Srgrimes		if (db_ed_style) {
4754Srgrimes		    db_dot = db_prev;
4764Srgrimes		}
4774Srgrimes		else {
4784Srgrimes		    db_dot = db_next;
4794Srgrimes		}
4804Srgrimes	    }
4814Srgrimes	    else {
4824Srgrimes		/*
4834Srgrimes		 * If command does not change dot,
4844Srgrimes		 * set 'next' location to be the same.
4854Srgrimes		 */
4864Srgrimes		db_next = db_dot;
4874Srgrimes	    }
4884Srgrimes	}
4894Srgrimes}
4904Srgrimes
4914Srgrimes/*
49233296Sbde * At least one non-optional command must be implemented using
49333296Sbde * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
49433296Sbde */
49533296SbdeDB_COMMAND(panic, db_panic)
4966204Sphk{
497160505Sjhb	db_disable_pager();
4987170Sdg	panic("from debugger");
4996204Sphk}
5006204Sphk
5016204Sphkvoid
502273006Spfgdb_command_loop(void)
5034Srgrimes{
5044Srgrimes	/*
5054Srgrimes	 * Initialize 'prev' and 'next' to dot.
5064Srgrimes	 */
5074Srgrimes	db_prev = db_dot;
5084Srgrimes	db_next = db_dot;
5094Srgrimes
5104Srgrimes	db_cmd_loop_done = 0;
5114Srgrimes	while (!db_cmd_loop_done) {
5124Srgrimes	    if (db_print_position() != 0)
5134Srgrimes		db_printf("\n");
5144Srgrimes
5154Srgrimes	    db_printf("db> ");
5164Srgrimes	    (void) db_read_line();
5174Srgrimes
518183054Ssam	    db_command(&db_last_command, &db_cmd_table, /* dopager */ 1);
5194Srgrimes	}
5204Srgrimes}
5214Srgrimes
522174914Srwatson/*
523174914Srwatson * Execute a command on behalf of a script.  The caller is responsible for
524174914Srwatson * making sure that the command string is < DB_MAXLINE or it will be
525174914Srwatson * truncated.
526174914Srwatson *
527174914Srwatson * XXXRW: Runs by injecting faked input into DDB input stream; it would be
528174914Srwatson * nicer to use an alternative approach that didn't mess with the previous
529174914Srwatson * command buffer.
530174914Srwatson */
5314Srgrimesvoid
532174914Srwatsondb_command_script(const char *command)
533174914Srwatson{
534174914Srwatson	db_prev = db_next = db_dot;
535174914Srwatson	db_inject_line(command);
536183054Ssam	db_command(&db_last_command, &db_cmd_table, /* dopager */ 0);
537174914Srwatson}
538174914Srwatson
539174914Srwatsonvoid
540273006Spfgdb_error(const char *s)
5414Srgrimes{
5424Srgrimes	if (s)
54379884Skris	    db_printf("%s", s);
5444Srgrimes	db_flush_lex();
545131952Smarcel	kdb_reenter();
5464Srgrimes}
5474Srgrimes
548222801Smarcelstatic void
549283248Spfgdb_dump(db_expr_t dummy, bool dummy2, db_expr_t dummy3, char *dummy4)
550222801Smarcel{
551222801Smarcel	int error;
5524Srgrimes
553242424Salfred	if (textdump_pending) {
554242424Salfred		db_printf("textdump_pending set.\n"
555242424Salfred		    "run \"textdump unset\" first or \"textdump dump\" for a textdump.\n");
556242424Salfred		return;
557242424Salfred	}
558283088Spfg	error = doadump(false);
559222801Smarcel	if (error) {
560222801Smarcel		db_printf("Cannot dump: ");
561222801Smarcel		switch (error) {
562222801Smarcel		case EBUSY:
563222801Smarcel			db_printf("debugger got invoked while dumping.\n");
564222801Smarcel			break;
565222801Smarcel		case ENXIO:
566222801Smarcel			db_printf("no dump device specified.\n");
567222801Smarcel			break;
568222801Smarcel		default:
569222801Smarcel			db_printf("unknown error (error=%d).\n", error);
570222801Smarcel			break;
571222801Smarcel		}
572222801Smarcel	}
573222801Smarcel}
574222801Smarcel
5754Srgrimes/*
5764Srgrimes * Call random function:
5774Srgrimes * !expr(arg,arg,arg)
5784Srgrimes */
579147745Smarcel
580147745Smarcel/* The generic implementation supports a maximum of 10 arguments. */
581147745Smarceltypedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
582147745Smarcel    db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
583147745Smarcel
584147745Smarcelstatic __inline int
585147745Smarceldb_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
586147745Smarcel{
587147745Smarcel	__db_f *f = (__db_f *)addr;
588147745Smarcel
589147745Smarcel	if (nargs > 10) {
590147745Smarcel		db_printf("Too many arguments (max 10)\n");
591147745Smarcel		return (0);
592147745Smarcel	}
593147745Smarcel	*rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
594147745Smarcel	    args[6], args[7], args[8], args[9]);
595147745Smarcel	return (1);
596147745Smarcel}
597147745Smarcel
59812515Sphkstatic void
599283248Spfgdb_fncall(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
6004Srgrimes{
6014Srgrimes	db_expr_t	fn_addr;
602147745Smarcel	db_expr_t	args[DB_MAXARGS];
6034Srgrimes	int		nargs = 0;
6044Srgrimes	db_expr_t	retval;
6054Srgrimes	int		t;
6064Srgrimes
6074Srgrimes	if (!db_expression(&fn_addr)) {
6084Srgrimes	    db_printf("Bad function\n");
6094Srgrimes	    db_flush_lex();
6104Srgrimes	    return;
6114Srgrimes	}
6124Srgrimes
6134Srgrimes	t = db_read_token();
6144Srgrimes	if (t == tLPAREN) {
6154Srgrimes	    if (db_expression(&args[0])) {
6164Srgrimes		nargs++;
6174Srgrimes		while ((t = db_read_token()) == tCOMMA) {
618147745Smarcel		    if (nargs == DB_MAXARGS) {
619147745Smarcel			db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
6204Srgrimes			db_flush_lex();
6214Srgrimes			return;
6224Srgrimes		    }
6234Srgrimes		    if (!db_expression(&args[nargs])) {
6244Srgrimes			db_printf("Argument missing\n");
6254Srgrimes			db_flush_lex();
6264Srgrimes			return;
6274Srgrimes		    }
6284Srgrimes		    nargs++;
6294Srgrimes		}
6304Srgrimes		db_unread_token(t);
6314Srgrimes	    }
6324Srgrimes	    if (db_read_token() != tRPAREN) {
6334Srgrimes		db_printf("?\n");
6344Srgrimes		db_flush_lex();
6354Srgrimes		return;
6364Srgrimes	    }
6374Srgrimes	}
6384Srgrimes	db_skip_to_eol();
639160505Sjhb	db_disable_pager();
6404Srgrimes
641147745Smarcel	if (DB_CALL(fn_addr, &retval, nargs, args))
642147745Smarcel		db_printf("= %#lr\n", (long)retval);
6434Srgrimes}
64417848Spst
64517848Spststatic void
646283248Spfgdb_halt(db_expr_t dummy, bool dummy2, db_expr_t dummy3, char *dummy4)
647163192Sbde{
648163192Sbde
649163192Sbde	cpu_halt();
650163192Sbde}
651163192Sbde
652163192Sbdestatic void
653283248Spfgdb_kill(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
65486998Sdd{
65586998Sdd	db_expr_t old_radix, pid, sig;
65686998Sdd	struct proc *p;
65786998Sdd
658272958Spfg#define	DB_ERROR(f)	do { db_printf f; db_flush_lex(); goto out; } while (0)
65986998Sdd
66086998Sdd	/*
66186998Sdd	 * PIDs and signal numbers are typically represented in base
66286998Sdd	 * 10, so make that the default here.  It can, of course, be
66386998Sdd	 * overridden by specifying a prefix.
66486998Sdd	 */
66586998Sdd	old_radix = db_radix;
66686998Sdd	db_radix = 10;
66786998Sdd	/* Retrieve arguments. */
66886998Sdd	if (!db_expression(&sig))
66986998Sdd		DB_ERROR(("Missing signal number\n"));
67086998Sdd	if (!db_expression(&pid))
67186998Sdd		DB_ERROR(("Missing process ID\n"));
67286998Sdd	db_skip_to_eol();
673209934Skib	if (!_SIG_VALID(sig))
67486998Sdd		DB_ERROR(("Signal number out of range\n"));
67586998Sdd
67686998Sdd	/*
67786998Sdd	 * Find the process in question.  allproc_lock is not needed
67886998Sdd	 * since we're in DDB.
67986998Sdd	 */
68086998Sdd	/* sx_slock(&allproc_lock); */
681166074Sdelphij	FOREACH_PROC_IN_SYSTEM(p)
68286998Sdd	    if (p->p_pid == pid)
68386998Sdd		    break;
68486998Sdd	/* sx_sunlock(&allproc_lock); */
68586998Sdd	if (p == NULL)
68689442Smjacob		DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
68786998Sdd
68886998Sdd	/* If it's already locked, bail; otherwise, do the deed. */
68986998Sdd	if (PROC_TRYLOCK(p) == 0)
69089442Smjacob		DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
69186998Sdd	else {
692199355Skib		pksignal(p, sig, NULL);
69386998Sdd		PROC_UNLOCK(p);
69486998Sdd	}
69586998Sdd
69686998Sddout:
69786998Sdd	db_radix = old_radix;
69886998Sdd#undef DB_ERROR
69986998Sdd}
70086998Sdd
701208509Sbz/*
702208509Sbz * Reboot.  In case there is an additional argument, take it as delay in
703208509Sbz * seconds.  Default to 15s if we cannot parse it and make sure we will
704208509Sbz * never wait longer than 1 week.  Some code is similar to
705208509Sbz * kern_shutdown.c:shutdown_panic().
706208509Sbz */
707208509Sbz#ifndef	DB_RESET_MAXDELAY
708208509Sbz#define	DB_RESET_MAXDELAY	(3600 * 24 * 7)
709208509Sbz#endif
710208509Sbz
71186998Sddstatic void
712283248Spfgdb_reset(db_expr_t addr, bool have_addr, db_expr_t count __unused,
713208509Sbz    char *modif __unused)
71485944Speter{
715208509Sbz	int delay, loop;
71685944Speter
717208509Sbz	if (have_addr) {
718208509Sbz		delay = (int)db_hex2dec(addr);
719208509Sbz
720208509Sbz		/* If we parse to fail, use 15s. */
721208509Sbz		if (delay == -1)
722208509Sbz			delay = 15;
723208509Sbz
724208509Sbz		/* Cap at one week. */
725208509Sbz		if ((uintmax_t)delay > (uintmax_t)DB_RESET_MAXDELAY)
726208509Sbz			delay = DB_RESET_MAXDELAY;
727208509Sbz
728208509Sbz		db_printf("Automatic reboot in %d seconds - "
729208509Sbz		    "press a key on the console to abort\n", delay);
730208509Sbz		for (loop = delay * 10; loop > 0; --loop) {
731208509Sbz			DELAY(1000 * 100); /* 1/10th second */
732208509Sbz			/* Did user type a key? */
733208509Sbz			if (cncheckc() != -1)
734208509Sbz				return;
735208509Sbz		}
736208509Sbz	}
737208509Sbz
73885944Speter	cpu_reset();
73985944Speter}
740126399Sphk
741126399Sphkstatic void
742283248Spfgdb_watchdog(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
743126399Sphk{
744220362Sattilio	db_expr_t old_radix, tout;
745220362Sattilio	int err, i;
746126399Sphk
747220362Sattilio	old_radix = db_radix;
748220362Sattilio	db_radix = 10;
749220362Sattilio	err = db_expression(&tout);
750220362Sattilio	db_skip_to_eol();
751220362Sattilio	db_radix = old_radix;
752126399Sphk
753220362Sattilio	/* If no argument is provided the watchdog will just be disabled. */
754220362Sattilio	if (err == 0) {
755220362Sattilio		db_printf("No argument provided, disabling watchdog\n");
756220362Sattilio		tout = 0;
757220362Sattilio	} else if ((tout & WD_INTERVAL) == WD_TO_NEVER) {
758220362Sattilio		db_error("Out of range watchdog interval\n");
759220362Sattilio		return;
760220362Sattilio	}
761220362Sattilio	EVENTHANDLER_INVOKE(watchdog_list, tout, &i);
762126399Sphk}
763132002Smarcel
764132002Smarcelstatic void
765283248Spfgdb_gdb(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
766132002Smarcel{
767132002Smarcel
768218825Smdf	if (kdb_dbbe_select("gdb") != 0) {
769132002Smarcel		db_printf("The remote GDB backend could not be selected.\n");
770218825Smdf		return;
771218825Smdf	}
772218825Smdf	/*
773218825Smdf	 * Mark that we are done in the debugger.  kdb_trap()
774218825Smdf	 * should re-enter with the new backend.
775218825Smdf	 */
776218825Smdf	db_cmd_loop_done = 1;
777218825Smdf	db_printf("(ctrl-c will return control to ddb)\n");
778132002Smarcel}
779132482Smarcel
780132482Smarcelstatic void
781283248Spfgdb_stack_trace(db_expr_t tid, bool hastid, db_expr_t count, char *modif)
782132482Smarcel{
783132482Smarcel	struct thread *td;
784132482Smarcel	db_expr_t radix;
785138038Srwatson	pid_t pid;
786132482Smarcel	int t;
787132482Smarcel
788132482Smarcel	/*
789132482Smarcel	 * We parse our own arguments. We don't like the default radix.
790132482Smarcel	 */
791132482Smarcel	radix = db_radix;
792132482Smarcel	db_radix = 10;
793132482Smarcel	hastid = db_expression(&tid);
794132482Smarcel	t = db_read_token();
795132482Smarcel	if (t == tCOMMA) {
796132482Smarcel		if (!db_expression(&count)) {
797132482Smarcel			db_printf("Count missing\n");
798132482Smarcel			db_flush_lex();
799132482Smarcel			return;
800132482Smarcel		}
801132482Smarcel	} else {
802132482Smarcel		db_unread_token(t);
803132482Smarcel		count = -1;
804132482Smarcel	}
805132482Smarcel	db_skip_to_eol();
806132482Smarcel	db_radix = radix;
807132482Smarcel
808132482Smarcel	if (hastid) {
809132482Smarcel		td = kdb_thr_lookup((lwpid_t)tid);
810132482Smarcel		if (td == NULL)
811132482Smarcel			td = kdb_thr_from_pid((pid_t)tid);
812132482Smarcel		if (td == NULL) {
813132482Smarcel			db_printf("Thread %d not found\n", (int)tid);
814132482Smarcel			return;
815132482Smarcel		}
816132482Smarcel	} else
817132482Smarcel		td = kdb_thread;
818138038Srwatson	if (td->td_proc != NULL)
819138038Srwatson		pid = td->td_proc->p_pid;
820138038Srwatson	else
821138038Srwatson		pid = -1;
822138038Srwatson	db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
823132482Smarcel	db_trace_thread(td, count);
824132482Smarcel}
825150819Srwatson
826150819Srwatsonstatic void
827283248Spfgdb_stack_trace_all(db_expr_t dummy, bool dummy2, db_expr_t dummy3,
828150819Srwatson    char *dummy4)
829150819Srwatson{
830150819Srwatson	struct proc *p;
831150819Srwatson	struct thread *td;
832163909Skib	jmp_buf jb;
833163909Skib	void *prev_jb;
834150819Srwatson
835166074Sdelphij	FOREACH_PROC_IN_SYSTEM(p) {
836163909Skib		prev_jb = kdb_jmpbuf(jb);
837163909Skib		if (setjmp(jb) == 0) {
838163909Skib			FOREACH_THREAD_IN_PROC(p, td) {
839163909Skib				db_printf("\nTracing command %s pid %d tid %ld td %p\n",
840163909Skib					  p->p_comm, p->p_pid, (long)td->td_tid, td);
841163909Skib				db_trace_thread(td, -1);
842163909Skib				if (db_pager_quit) {
843163909Skib					kdb_jmpbuf(prev_jb);
844163909Skib					return;
845163909Skib				}
846163909Skib			}
847150819Srwatson		}
848163909Skib		kdb_jmpbuf(prev_jb);
849150819Srwatson	}
850150819Srwatson}
851208509Sbz
852208509Sbz/*
853208509Sbz * Take the parsed expression value from the command line that was parsed
854208509Sbz * as a hexadecimal value and convert it as if the expression was parsed
855208509Sbz * as a decimal value.  Returns -1 if the expression was not a valid
856208509Sbz * decimal value.
857208509Sbz */
858208509Sbzdb_expr_t
859208509Sbzdb_hex2dec(db_expr_t expr)
860208509Sbz{
861208509Sbz	uintptr_t x, y;
862208509Sbz	db_expr_t val;
863208509Sbz
864208509Sbz	y = 1;
865208509Sbz	val = 0;
866208509Sbz	x = expr;
867208509Sbz	while (x != 0) {
868208509Sbz		if (x % 16 > 9)
869208509Sbz			return (-1);
870208509Sbz		val += (x % 16) * (y);
871208509Sbz		x >>= 4;
872208509Sbz		y *= 10;
873208509Sbz	}
874208509Sbz	return (val);
875208509Sbz}
876