db_command.c revision 166074
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 166074 2007-01-17 15:05:52Z delphij $");
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>
4812734Sbde
492056Swollman#include <ddb/ddb.h>
5012473Sbde#include <ddb/db_command.h>
514Srgrimes#include <ddb/db_lex.h>
524Srgrimes#include <ddb/db_output.h>
534Srgrimes
54118990Smarcel#include <machine/cpu.h>
5579418Sjulian#include <machine/setjmp.h>
564Srgrimes
574Srgrimes/*
584Srgrimes * Exported global variables
594Srgrimes */
604Srgrimesboolean_t	db_cmd_loop_done;
6118296Sbdedb_addr_t	db_dot;
624Srgrimesdb_addr_t	db_last_addr;
634Srgrimesdb_addr_t	db_prev;
644Srgrimesdb_addr_t	db_next;
654Srgrimes
6678161SpeterSET_DECLARE(db_cmd_set, struct command);
6778161SpeterSET_DECLARE(db_show_cmd_set, struct command);
6878161Speter
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[] = {
83148919Sobrien	{ "procs",	db_ps,			0,	0 },
84148919Sobrien	{ (char *)0 }
85148919Sobrien};
86148919Sobrien
87156412Sjhbstatic struct command_table db_show_all_table = {
88156412Sjhb	db_show_all_cmds
89156412Sjhb};
90156412Sjhb
91148919Sobrienstatic struct command db_show_cmds[] = {
92156412Sjhb	{ "all",	0,			0,	&db_show_all_table },
93148919Sobrien	{ "registers",	db_show_regs,		0,	0 },
94148919Sobrien	{ "breaks",	db_listbreak_cmd, 	0,	0 },
95148919Sobrien	{ "threads",	db_show_threads,	0,	0 },
96148919Sobrien	{ (char *)0, }
97148919Sobrien};
98148919Sobrien
99156412Sjhbstatic struct command_table db_show_table = {
100156412Sjhb	db_show_cmds,
101156412Sjhb	SET_BEGIN(db_show_cmd_set),
102156412Sjhb	SET_LIMIT(db_show_cmd_set)
103156412Sjhb};
104156412Sjhb
105156412Sjhbstatic struct command db_commands[] = {
106148919Sobrien	{ "print",	db_print_cmd,		0,	0 },
107148919Sobrien	{ "p",		db_print_cmd,		0,	0 },
108148919Sobrien	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
109148919Sobrien	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
110148919Sobrien	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
111148919Sobrien	{ "set",	db_set_cmd,		CS_OWN,	0 },
112148919Sobrien	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
113148919Sobrien	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
114148919Sobrien	{ "delete",	db_delete_cmd,		0,	0 },
115148919Sobrien	{ "d",		db_delete_cmd,		0,	0 },
116148919Sobrien	{ "break",	db_breakpoint_cmd,	0,	0 },
117163135Sbde	{ "b",		db_breakpoint_cmd,	0,	0 },
118148919Sobrien	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
119148919Sobrien	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
120148919Sobrien	{ "dhwatch",	db_deletehwatch_cmd,	0,      0 },
121148919Sobrien	{ "hwatch",	db_hwatchpoint_cmd,	0,      0 },
122148919Sobrien	{ "step",	db_single_step_cmd,	0,	0 },
123148919Sobrien	{ "s",		db_single_step_cmd,	0,	0 },
124148919Sobrien	{ "continue",	db_continue_cmd,	0,	0 },
125148919Sobrien	{ "c",		db_continue_cmd,	0,	0 },
126148919Sobrien	{ "until",	db_trace_until_call_cmd,0,	0 },
127148919Sobrien	{ "next",	db_trace_until_matching_cmd,0,	0 },
128148919Sobrien	{ "match",	db_trace_until_matching_cmd,0,	0 },
129148919Sobrien	{ "trace",	db_stack_trace,		CS_OWN,	0 },
130163135Sbde	{ "t",		db_stack_trace,		CS_OWN,	0 },
131151622Sjhb	{ "alltrace",	db_stack_trace_all,	0,	0 },
132148919Sobrien	{ "where",	db_stack_trace,		CS_OWN,	0 },
133151622Sjhb	{ "bt",		db_stack_trace,		CS_OWN,	0 },
134148919Sobrien	{ "call",	db_fncall,		CS_OWN,	0 },
135156412Sjhb	{ "show",	0,			0,	&db_show_table },
136148919Sobrien	{ "ps",		db_ps,			0,	0 },
137148919Sobrien	{ "gdb",	db_gdb,			0,	0 },
138163192Sbde	{ "halt",	db_halt,		0,	0 },
139163192Sbde	{ "reboot",	db_reset,		0,	0 },
140148919Sobrien	{ "reset",	db_reset,		0,	0 },
141148919Sobrien	{ "kill",	db_kill,		CS_OWN,	0 },
142148919Sobrien	{ "watchdog",	db_watchdog,		0,	0 },
143148919Sobrien	{ "thread",	db_set_thread,		CS_OWN,	0 },
144148919Sobrien	{ (char *)0, }
145148919Sobrien};
146148919Sobrien
147156412Sjhbstatic struct command_table db_command_table = {
148156412Sjhb	db_commands,
149156412Sjhb	SET_BEGIN(db_cmd_set),
150156412Sjhb	SET_LIMIT(db_cmd_set)
151156412Sjhb};
152156412Sjhb
153148919Sobrienstatic struct command	*db_last_command = 0;
154148919Sobrien
1554Srgrimes/*
1564Srgrimes * if 'ed' style: 'dot' is set at start of last item printed,
1574Srgrimes * and '+' points to next line.
1584Srgrimes * Otherwise: 'dot' points to next item, '..' points to last.
1594Srgrimes */
16012515Sphkstatic boolean_t	db_ed_style = TRUE;
1614Srgrimes
1624Srgrimes/*
1634Srgrimes * Utility routine - discard tokens through end-of-line.
1644Srgrimes */
1654Srgrimesvoid
1664Srgrimesdb_skip_to_eol()
1674Srgrimes{
1684Srgrimes	int	t;
1694Srgrimes	do {
1704Srgrimes	    t = db_read_token();
1714Srgrimes	} while (t != tEOL);
1724Srgrimes}
1734Srgrimes
1744Srgrimes/*
1754Srgrimes * Results of command search.
1764Srgrimes */
1774Srgrimes#define	CMD_UNIQUE	0
1784Srgrimes#define	CMD_FOUND	1
1794Srgrimes#define	CMD_NONE	2
1804Srgrimes#define	CMD_AMBIGUOUS	3
1814Srgrimes#define	CMD_HELP	4
1824Srgrimes
183156412Sjhbstatic void	db_cmd_match(char *name, struct command *cmd,
184156412Sjhb		    struct command **cmdp, int *resultp);
185156412Sjhbstatic void	db_cmd_list(struct command_table *table);
186156412Sjhbstatic int	db_cmd_search(char *name, struct command_table *table,
187156412Sjhb		    struct command **cmdp);
18892756Salfredstatic void	db_command(struct command **last_cmdp,
189156412Sjhb		    struct command_table *cmd_table);
19012473Sbde
1914Srgrimes/*
192156412Sjhb * Helper function to match a single command.
1934Srgrimes */
194156412Sjhbstatic void
195156412Sjhbdb_cmd_match(name, cmd, cmdp, resultp)
1964Srgrimes	char *		name;
197156412Sjhb	struct command	*cmd;
1984Srgrimes	struct command	**cmdp;	/* out */
199156412Sjhb	int *		resultp;
2004Srgrimes{
201156412Sjhb	char *lp, *rp;
202156412Sjhb	int c;
2034Srgrimes
204156412Sjhb	lp = name;
205156412Sjhb	rp = cmd->name;
206156412Sjhb	while ((c = *lp) == *rp) {
2074Srgrimes		if (c == 0) {
208156412Sjhb			/* complete match */
209156412Sjhb			*cmdp = cmd;
210156412Sjhb			*resultp = CMD_UNIQUE;
211156412Sjhb			return;
2124Srgrimes		}
2134Srgrimes		lp++;
2144Srgrimes		rp++;
215156412Sjhb	}
216156412Sjhb	if (c == 0) {
2174Srgrimes		/* end of name, not end of command -
2184Srgrimes		   partial match */
219156412Sjhb		if (*resultp == CMD_FOUND) {
220156412Sjhb			*resultp = CMD_AMBIGUOUS;
221156412Sjhb			/* but keep looking for a full match -
222156412Sjhb			   this lets us match single letters */
223156412Sjhb		} else {
224156412Sjhb			*cmdp = cmd;
225156412Sjhb			*resultp = CMD_FOUND;
2264Srgrimes		}
2274Srgrimes	}
228156412Sjhb}
22918296Sbde
230156412Sjhb/*
231156412Sjhb * Search for command prefix.
232156412Sjhb */
233156412Sjhbstatic int
234156412Sjhbdb_cmd_search(name, table, cmdp)
235156412Sjhb	char *		name;
236156412Sjhb	struct command_table *table;
237156412Sjhb	struct command	**cmdp;	/* out */
238156412Sjhb{
239156412Sjhb	struct command	*cmd;
240156412Sjhb	struct command	**aux_cmdp;
241156412Sjhb	int		result = CMD_NONE;
242156412Sjhb
243156412Sjhb	for (cmd = table->table; cmd->name != 0; cmd++) {
244156412Sjhb		db_cmd_match(name, cmd, cmdp, &result);
245156412Sjhb		if (result == CMD_UNIQUE)
24618296Sbde			return (CMD_UNIQUE);
247156412Sjhb	}
248156412Sjhb	if (table->aux_tablep != NULL)
249156412Sjhb		for (aux_cmdp = table->aux_tablep;
250156412Sjhb		     aux_cmdp < table->aux_tablep_end;
251156412Sjhb		     aux_cmdp++) {
252156412Sjhb			db_cmd_match(name, *aux_cmdp, cmdp, &result);
253156412Sjhb			if (result == CMD_UNIQUE)
254156412Sjhb				return (CMD_UNIQUE);
25518296Sbde		}
2564Srgrimes	if (result == CMD_NONE) {
257156412Sjhb		/* check for 'help' */
2584Srgrimes		if (name[0] == 'h' && name[1] == 'e'
2594Srgrimes		    && name[2] == 'l' && name[3] == 'p')
2604Srgrimes			result = CMD_HELP;
2614Srgrimes	}
2624Srgrimes	return (result);
2634Srgrimes}
2644Srgrimes
26512515Sphkstatic void
266156412Sjhbdb_cmd_list(table)
267156412Sjhb	struct command_table *table;
2684Srgrimes{
2694Srgrimes	register struct command *cmd;
27018296Sbde	register struct command **aux_cmdp;
2714Srgrimes
272156412Sjhb	for (cmd = table->table; cmd->name != 0; cmd++) {
2734Srgrimes	    db_printf("%-12s", cmd->name);
274163134Sbde	    db_end_line(12);
2754Srgrimes	}
276156412Sjhb	if (table->aux_tablep == NULL)
27718296Sbde	    return;
278156412Sjhb	for (aux_cmdp = table->aux_tablep; aux_cmdp < table->aux_tablep_end;
279156412Sjhb	     aux_cmdp++) {
28018296Sbde	    db_printf("%-12s", (*aux_cmdp)->name);
281163134Sbde	    db_end_line(12);
28218296Sbde	}
2834Srgrimes}
2844Srgrimes
28512515Sphkstatic void
286156412Sjhbdb_command(last_cmdp, cmd_table)
2874Srgrimes	struct command	**last_cmdp;	/* IN_OUT */
288156412Sjhb	struct command_table *cmd_table;
2894Srgrimes{
2904Srgrimes	struct command	*cmd;
2914Srgrimes	int		t;
2924Srgrimes	char		modif[TOK_STRING_SIZE];
2934Srgrimes	db_expr_t	addr, count;
294798Swollman	boolean_t	have_addr = FALSE;
2954Srgrimes	int		result;
2964Srgrimes
2974Srgrimes	t = db_read_token();
2984Srgrimes	if (t == tEOL) {
2994Srgrimes	    /* empty line repeats last command, at 'next' */
3004Srgrimes	    cmd = *last_cmdp;
3014Srgrimes	    addr = (db_expr_t)db_next;
3024Srgrimes	    have_addr = FALSE;
3034Srgrimes	    count = 1;
3044Srgrimes	    modif[0] = '\0';
3054Srgrimes	}
3064Srgrimes	else if (t == tEXCL) {
30710348Sbde	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
3084Srgrimes	    return;
3094Srgrimes	}
3104Srgrimes	else if (t != tIDENT) {
3114Srgrimes	    db_printf("?\n");
3124Srgrimes	    db_flush_lex();
3134Srgrimes	    return;
3144Srgrimes	}
3154Srgrimes	else {
3164Srgrimes	    /*
3174Srgrimes	     * Search for command
3184Srgrimes	     */
3194Srgrimes	    while (cmd_table) {
3204Srgrimes		result = db_cmd_search(db_tok_string,
3214Srgrimes				       cmd_table,
3224Srgrimes				       &cmd);
3234Srgrimes		switch (result) {
3244Srgrimes		    case CMD_NONE:
3254Srgrimes			db_printf("No such command\n");
3264Srgrimes			db_flush_lex();
3274Srgrimes			return;
3284Srgrimes		    case CMD_AMBIGUOUS:
3294Srgrimes			db_printf("Ambiguous\n");
3304Srgrimes			db_flush_lex();
3314Srgrimes			return;
3324Srgrimes		    case CMD_HELP:
333156412Sjhb			db_cmd_list(cmd_table);
3344Srgrimes			db_flush_lex();
3354Srgrimes			return;
3364Srgrimes		    default:
3374Srgrimes			break;
3384Srgrimes		}
339156412Sjhb		if ((cmd_table = cmd->more) != NULL) {
3404Srgrimes		    t = db_read_token();
3414Srgrimes		    if (t != tIDENT) {
342156412Sjhb			db_cmd_list(cmd_table);
3434Srgrimes			db_flush_lex();
3444Srgrimes			return;
3454Srgrimes		    }
3464Srgrimes		}
3474Srgrimes	    }
3484Srgrimes
3494Srgrimes	    if ((cmd->flag & CS_OWN) == 0) {
3504Srgrimes		/*
3514Srgrimes		 * Standard syntax:
3524Srgrimes		 * command [/modifier] [addr] [,count]
3534Srgrimes		 */
3544Srgrimes		t = db_read_token();
3554Srgrimes		if (t == tSLASH) {
3564Srgrimes		    t = db_read_token();
3574Srgrimes		    if (t != tIDENT) {
3584Srgrimes			db_printf("Bad modifier\n");
3594Srgrimes			db_flush_lex();
3604Srgrimes			return;
3614Srgrimes		    }
3624Srgrimes		    db_strcpy(modif, db_tok_string);
3634Srgrimes		}
3644Srgrimes		else {
3654Srgrimes		    db_unread_token(t);
3664Srgrimes		    modif[0] = '\0';
3674Srgrimes		}
3684Srgrimes
3694Srgrimes		if (db_expression(&addr)) {
3704Srgrimes		    db_dot = (db_addr_t) addr;
3714Srgrimes		    db_last_addr = db_dot;
3724Srgrimes		    have_addr = TRUE;
3734Srgrimes		}
3744Srgrimes		else {
3754Srgrimes		    addr = (db_expr_t) db_dot;
3764Srgrimes		    have_addr = FALSE;
3774Srgrimes		}
3784Srgrimes		t = db_read_token();
3794Srgrimes		if (t == tCOMMA) {
3804Srgrimes		    if (!db_expression(&count)) {
3814Srgrimes			db_printf("Count missing\n");
3824Srgrimes			db_flush_lex();
3834Srgrimes			return;
3844Srgrimes		    }
3854Srgrimes		}
3864Srgrimes		else {
3874Srgrimes		    db_unread_token(t);
3884Srgrimes		    count = -1;
3894Srgrimes		}
3904Srgrimes		if ((cmd->flag & CS_MORE) == 0) {
3914Srgrimes		    db_skip_to_eol();
3924Srgrimes		}
3934Srgrimes	    }
3944Srgrimes	}
3954Srgrimes	*last_cmdp = cmd;
3964Srgrimes	if (cmd != 0) {
3974Srgrimes	    /*
3984Srgrimes	     * Execute the command.
3994Srgrimes	     */
400160312Sjhb	    db_enable_pager();
4014Srgrimes	    (*cmd->fcn)(addr, have_addr, count, modif);
402160312Sjhb	    db_disable_pager();
4034Srgrimes
4044Srgrimes	    if (cmd->flag & CS_SET_DOT) {
4054Srgrimes		/*
4064Srgrimes		 * If command changes dot, set dot to
4074Srgrimes		 * previous address displayed (if 'ed' style).
4084Srgrimes		 */
4094Srgrimes		if (db_ed_style) {
4104Srgrimes		    db_dot = db_prev;
4114Srgrimes		}
4124Srgrimes		else {
4134Srgrimes		    db_dot = db_next;
4144Srgrimes		}
4154Srgrimes	    }
4164Srgrimes	    else {
4174Srgrimes		/*
4184Srgrimes		 * If command does not change dot,
4194Srgrimes		 * set 'next' location to be the same.
4204Srgrimes		 */
4214Srgrimes		db_next = db_dot;
4224Srgrimes	    }
4234Srgrimes	}
4244Srgrimes}
4254Srgrimes
4264Srgrimes/*
42733296Sbde * At least one non-optional command must be implemented using
42833296Sbde * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
42933296Sbde */
43033296SbdeDB_COMMAND(panic, db_panic)
4316204Sphk{
432160505Sjhb	db_disable_pager();
4337170Sdg	panic("from debugger");
4346204Sphk}
4356204Sphk
4366204Sphkvoid
4374Srgrimesdb_command_loop()
4384Srgrimes{
4394Srgrimes	/*
4404Srgrimes	 * Initialize 'prev' and 'next' to dot.
4414Srgrimes	 */
4424Srgrimes	db_prev = db_dot;
4434Srgrimes	db_next = db_dot;
4444Srgrimes
4454Srgrimes	db_cmd_loop_done = 0;
4464Srgrimes	while (!db_cmd_loop_done) {
4474Srgrimes	    if (db_print_position() != 0)
4484Srgrimes		db_printf("\n");
4494Srgrimes
4504Srgrimes	    db_printf("db> ");
4514Srgrimes	    (void) db_read_line();
4524Srgrimes
453156412Sjhb	    db_command(&db_last_command, &db_command_table);
4544Srgrimes	}
4554Srgrimes}
4564Srgrimes
4574Srgrimesvoid
4584Srgrimesdb_error(s)
459103746Smarkm	const char *s;
4604Srgrimes{
4614Srgrimes	if (s)
46279884Skris	    db_printf("%s", s);
4634Srgrimes	db_flush_lex();
464131952Smarcel	kdb_reenter();
4654Srgrimes}
4664Srgrimes
4674Srgrimes
4684Srgrimes/*
4694Srgrimes * Call random function:
4704Srgrimes * !expr(arg,arg,arg)
4714Srgrimes */
472147745Smarcel
473147745Smarcel/* The generic implementation supports a maximum of 10 arguments. */
474147745Smarceltypedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
475147745Smarcel    db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
476147745Smarcel
477147745Smarcelstatic __inline int
478147745Smarceldb_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
479147745Smarcel{
480147745Smarcel	__db_f *f = (__db_f *)addr;
481147745Smarcel
482147745Smarcel	if (nargs > 10) {
483147745Smarcel		db_printf("Too many arguments (max 10)\n");
484147745Smarcel		return (0);
485147745Smarcel	}
486147745Smarcel	*rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
487147745Smarcel	    args[6], args[7], args[8], args[9]);
488147745Smarcel	return (1);
489147745Smarcel}
490147745Smarcel
49112515Sphkstatic void
49210348Sbdedb_fncall(dummy1, dummy2, dummy3, dummy4)
49310348Sbde	db_expr_t	dummy1;
49410348Sbde	boolean_t	dummy2;
49510348Sbde	db_expr_t	dummy3;
49610348Sbde	char *		dummy4;
4974Srgrimes{
4984Srgrimes	db_expr_t	fn_addr;
499147745Smarcel	db_expr_t	args[DB_MAXARGS];
5004Srgrimes	int		nargs = 0;
5014Srgrimes	db_expr_t	retval;
5024Srgrimes	int		t;
5034Srgrimes
5044Srgrimes	if (!db_expression(&fn_addr)) {
5054Srgrimes	    db_printf("Bad function\n");
5064Srgrimes	    db_flush_lex();
5074Srgrimes	    return;
5084Srgrimes	}
5094Srgrimes
5104Srgrimes	t = db_read_token();
5114Srgrimes	if (t == tLPAREN) {
5124Srgrimes	    if (db_expression(&args[0])) {
5134Srgrimes		nargs++;
5144Srgrimes		while ((t = db_read_token()) == tCOMMA) {
515147745Smarcel		    if (nargs == DB_MAXARGS) {
516147745Smarcel			db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
5174Srgrimes			db_flush_lex();
5184Srgrimes			return;
5194Srgrimes		    }
5204Srgrimes		    if (!db_expression(&args[nargs])) {
5214Srgrimes			db_printf("Argument missing\n");
5224Srgrimes			db_flush_lex();
5234Srgrimes			return;
5244Srgrimes		    }
5254Srgrimes		    nargs++;
5264Srgrimes		}
5274Srgrimes		db_unread_token(t);
5284Srgrimes	    }
5294Srgrimes	    if (db_read_token() != tRPAREN) {
5304Srgrimes		db_printf("?\n");
5314Srgrimes		db_flush_lex();
5324Srgrimes		return;
5334Srgrimes	    }
5344Srgrimes	}
5354Srgrimes	db_skip_to_eol();
536160505Sjhb	db_disable_pager();
5374Srgrimes
538147745Smarcel	if (DB_CALL(fn_addr, &retval, nargs, args))
539147745Smarcel		db_printf("= %#lr\n", (long)retval);
5404Srgrimes}
54117848Spst
54217848Spststatic void
543163192Sbdedb_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
544163192Sbde{
545163192Sbde
546163192Sbde	cpu_halt();
547163192Sbde}
548163192Sbde
549163192Sbdestatic void
55086998Sdddb_kill(dummy1, dummy2, dummy3, dummy4)
55186998Sdd	db_expr_t	dummy1;
55286998Sdd	boolean_t	dummy2;
55386998Sdd	db_expr_t	dummy3;
55486998Sdd	char *		dummy4;
55586998Sdd{
55686998Sdd	db_expr_t old_radix, pid, sig;
55786998Sdd	struct proc *p;
55886998Sdd
55986998Sdd#define DB_ERROR(f)	do { db_printf f; db_flush_lex(); goto out; } while (0)
56086998Sdd
56186998Sdd	/*
56286998Sdd	 * PIDs and signal numbers are typically represented in base
56386998Sdd	 * 10, so make that the default here.  It can, of course, be
56486998Sdd	 * overridden by specifying a prefix.
56586998Sdd	 */
56686998Sdd	old_radix = db_radix;
56786998Sdd	db_radix = 10;
56886998Sdd	/* Retrieve arguments. */
56986998Sdd	if (!db_expression(&sig))
57086998Sdd		DB_ERROR(("Missing signal number\n"));
57186998Sdd	if (!db_expression(&pid))
57286998Sdd		DB_ERROR(("Missing process ID\n"));
57386998Sdd	db_skip_to_eol();
57486998Sdd	if (sig < 0 || sig > _SIG_MAXSIG)
57586998Sdd		DB_ERROR(("Signal number out of range\n"));
57686998Sdd
57786998Sdd	/*
57886998Sdd	 * Find the process in question.  allproc_lock is not needed
57986998Sdd	 * since we're in DDB.
58086998Sdd	 */
58186998Sdd	/* sx_slock(&allproc_lock); */
582166074Sdelphij	FOREACH_PROC_IN_SYSTEM(p)
58386998Sdd	    if (p->p_pid == pid)
58486998Sdd		    break;
58586998Sdd	/* sx_sunlock(&allproc_lock); */
58686998Sdd	if (p == NULL)
58789442Smjacob		DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
58886998Sdd
58986998Sdd	/* If it's already locked, bail; otherwise, do the deed. */
59086998Sdd	if (PROC_TRYLOCK(p) == 0)
59189442Smjacob		DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
59286998Sdd	else {
59386998Sdd		psignal(p, sig);
59486998Sdd		PROC_UNLOCK(p);
59586998Sdd	}
59686998Sdd
59786998Sddout:
59886998Sdd	db_radix = old_radix;
59986998Sdd#undef DB_ERROR
60086998Sdd}
60186998Sdd
60286998Sddstatic void
60385944Speterdb_reset(dummy1, dummy2, dummy3, dummy4)
60485944Speter	db_expr_t	dummy1;
60585944Speter	boolean_t	dummy2;
60685944Speter	db_expr_t	dummy3;
60785944Speter	char *		dummy4;
60885944Speter{
60985944Speter
61085944Speter	cpu_reset();
61185944Speter}
612126399Sphk
613126399Sphkstatic void
614126399Sphkdb_watchdog(dummy1, dummy2, dummy3, dummy4)
615126399Sphk	db_expr_t	dummy1;
616126399Sphk	boolean_t	dummy2;
617126399Sphk	db_expr_t	dummy3;
618126399Sphk	char *		dummy4;
619126399Sphk{
620126399Sphk	int i;
621126399Sphk
622126399Sphk	/*
623126399Sphk	 * XXX: It might make sense to be able to set the watchdog to a
624126399Sphk	 * XXX: timeout here so that failure or hang as a result of subsequent
625126399Sphk	 * XXX: ddb commands could be recovered by a reset.
626126399Sphk	 */
627126399Sphk
628126399Sphk	EVENTHANDLER_INVOKE(watchdog_list, 0, &i);
629126399Sphk}
630132002Smarcel
631132002Smarcelstatic void
632132002Smarceldb_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
633132002Smarcel{
634132002Smarcel
635132002Smarcel	if (kdb_dbbe_select("gdb") != 0)
636132002Smarcel		db_printf("The remote GDB backend could not be selected.\n");
637132002Smarcel	else
638132002Smarcel		db_printf("Step to enter the remote GDB backend.\n");
639132002Smarcel}
640132482Smarcel
641132482Smarcelstatic void
642132482Smarceldb_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
643132482Smarcel{
644132482Smarcel	struct thread *td;
645132482Smarcel	db_expr_t radix;
646138038Srwatson	pid_t pid;
647132482Smarcel	int t;
648132482Smarcel
649132482Smarcel	/*
650132482Smarcel	 * We parse our own arguments. We don't like the default radix.
651132482Smarcel	 */
652132482Smarcel	radix = db_radix;
653132482Smarcel	db_radix = 10;
654132482Smarcel	hastid = db_expression(&tid);
655132482Smarcel	t = db_read_token();
656132482Smarcel	if (t == tCOMMA) {
657132482Smarcel		if (!db_expression(&count)) {
658132482Smarcel			db_printf("Count missing\n");
659132482Smarcel			db_flush_lex();
660132482Smarcel			return;
661132482Smarcel		}
662132482Smarcel	} else {
663132482Smarcel		db_unread_token(t);
664132482Smarcel		count = -1;
665132482Smarcel	}
666132482Smarcel	db_skip_to_eol();
667132482Smarcel	db_radix = radix;
668132482Smarcel
669132482Smarcel	if (hastid) {
670132482Smarcel		td = kdb_thr_lookup((lwpid_t)tid);
671132482Smarcel		if (td == NULL)
672132482Smarcel			td = kdb_thr_from_pid((pid_t)tid);
673132482Smarcel		if (td == NULL) {
674132482Smarcel			db_printf("Thread %d not found\n", (int)tid);
675132482Smarcel			return;
676132482Smarcel		}
677132482Smarcel	} else
678132482Smarcel		td = kdb_thread;
679138038Srwatson	if (td->td_proc != NULL)
680138038Srwatson		pid = td->td_proc->p_pid;
681138038Srwatson	else
682138038Srwatson		pid = -1;
683138038Srwatson	db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
684132482Smarcel	db_trace_thread(td, count);
685132482Smarcel}
686150819Srwatson
687150819Srwatsonstatic void
688150819Srwatsondb_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
689150819Srwatson    char *dummy4)
690150819Srwatson{
691150819Srwatson	struct proc *p;
692150819Srwatson	struct thread *td;
693163909Skib	jmp_buf jb;
694163909Skib	void *prev_jb;
695150819Srwatson
696166074Sdelphij	FOREACH_PROC_IN_SYSTEM(p) {
697163909Skib		prev_jb = kdb_jmpbuf(jb);
698163909Skib		if (setjmp(jb) == 0) {
699163909Skib			FOREACH_THREAD_IN_PROC(p, td) {
700163909Skib				db_printf("\nTracing command %s pid %d tid %ld td %p\n",
701163909Skib					  p->p_comm, p->p_pid, (long)td->td_tid, td);
702163909Skib				db_trace_thread(td, -1);
703163909Skib				if (db_pager_quit) {
704163909Skib					kdb_jmpbuf(prev_jb);
705163909Skib					return;
706163909Skib				}
707163909Skib			}
708150819Srwatson		}
709163909Skib		kdb_jmpbuf(prev_jb);
710150819Srwatson	}
711150819Srwatson}
712