xref: /freebsd/sys/ddb/db_command.c (revision c9b0cc3b9623c30583400b137b82ec92483435ad)
1dd3cb568SWarner Losh /*-
25b81b6b3SRodney W. Grimes  * Mach Operating System
35b81b6b3SRodney W. Grimes  * Copyright (c) 1991,1990 Carnegie Mellon University
45b81b6b3SRodney W. Grimes  * All Rights Reserved.
55b81b6b3SRodney W. Grimes  *
65b81b6b3SRodney W. Grimes  * Permission to use, copy, modify and distribute this software and its
75b81b6b3SRodney W. Grimes  * documentation is hereby granted, provided that both the copyright
85b81b6b3SRodney W. Grimes  * notice and this permission notice appear in all copies of the
95b81b6b3SRodney W. Grimes  * software, derivative works or modified versions, and any portions
105b81b6b3SRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
115b81b6b3SRodney W. Grimes  *
125b81b6b3SRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
135b81b6b3SRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
145b81b6b3SRodney W. Grimes  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
155b81b6b3SRodney W. Grimes  *
165b81b6b3SRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
175b81b6b3SRodney W. Grimes  *
185b81b6b3SRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
195b81b6b3SRodney W. Grimes  *  School of Computer Science
205b81b6b3SRodney W. Grimes  *  Carnegie Mellon University
215b81b6b3SRodney W. Grimes  *  Pittsburgh PA 15213-3890
225b81b6b3SRodney W. Grimes  *
235b81b6b3SRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
245b81b6b3SRodney W. Grimes  * rights to redistribute these changes.
255b81b6b3SRodney W. Grimes  */
265b81b6b3SRodney W. Grimes /*
275b81b6b3SRodney W. Grimes  *	Author: David B. Golub, Carnegie Mellon University
285b81b6b3SRodney W. Grimes  *	Date:	7/90
295b81b6b3SRodney W. Grimes  */
305b81b6b3SRodney W. Grimes /*
315b81b6b3SRodney W. Grimes  * Command dispatcher.
325b81b6b3SRodney W. Grimes  */
33753960f7SDavid E. O'Brien 
34753960f7SDavid E. O'Brien #include <sys/cdefs.h>
35753960f7SDavid E. O'Brien __FBSDID("$FreeBSD$");
36753960f7SDavid E. O'Brien 
37f540b106SGarrett Wollman #include <sys/param.h>
380ec81012SJohn Polstra #include <sys/linker_set.h>
3919d2c78fSDima Dorfman #include <sys/lock.h>
4037224cd3SMarcel Moolenaar #include <sys/kdb.h>
4119d2c78fSDima Dorfman #include <sys/mutex.h>
4219d2c78fSDima Dorfman #include <sys/proc.h>
43ad146781SPaul Traina #include <sys/reboot.h>
4419d2c78fSDima Dorfman #include <sys/signalvar.h>
45f540b106SGarrett Wollman #include <sys/systm.h>
46ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
4774cc032bSPoul-Henning Kamp #include <sys/watchdog.h>
483000820aSPoul-Henning Kamp 
495ccbc3ccSBruce Evans #include <ddb/ddb.h>
50058284fcSBruce Evans #include <ddb/db_command.h>
515b81b6b3SRodney W. Grimes #include <ddb/db_lex.h>
525b81b6b3SRodney W. Grimes #include <ddb/db_output.h>
535b81b6b3SRodney W. Grimes 
5426502503SMarcel Moolenaar #include <machine/cpu.h>
550b1ae809SJulian Elischer #include <machine/setjmp.h>
565b81b6b3SRodney W. Grimes 
575b81b6b3SRodney W. Grimes /*
585b81b6b3SRodney W. Grimes  * Exported global variables
595b81b6b3SRodney W. Grimes  */
605b81b6b3SRodney W. Grimes boolean_t	db_cmd_loop_done;
615b81b6b3SRodney W. Grimes db_addr_t	db_dot;
625b81b6b3SRodney W. Grimes db_addr_t	db_last_addr;
635b81b6b3SRodney W. Grimes db_addr_t	db_prev;
645b81b6b3SRodney W. Grimes db_addr_t	db_next;
65f41325dbSPeter Wemm 
66f41325dbSPeter Wemm SET_DECLARE(db_cmd_set, struct command);
67f41325dbSPeter Wemm SET_DECLARE(db_show_cmd_set, struct command);
685b81b6b3SRodney W. Grimes 
69f73a856dSPoul-Henning Kamp static db_cmdfcn_t	db_fncall;
70f3be7cb3SMarcel Moolenaar static db_cmdfcn_t	db_gdb;
71e4b732cfSBruce Evans static db_cmdfcn_t	db_halt;
7219d2c78fSDima Dorfman static db_cmdfcn_t	db_kill;
735370c3b6SPeter Wemm static db_cmdfcn_t	db_reset;
74fd32d93bSMarcel Moolenaar static db_cmdfcn_t	db_stack_trace;
75a7ad956bSRobert Watson static db_cmdfcn_t	db_stack_trace_all;
7674cc032bSPoul-Henning Kamp static db_cmdfcn_t	db_watchdog;
77ad146781SPaul Traina 
78cec9a4bfSDavid E. O'Brien /*
79cec9a4bfSDavid E. O'Brien  * 'show' commands
80cec9a4bfSDavid E. O'Brien  */
81cec9a4bfSDavid E. O'Brien 
82cec9a4bfSDavid E. O'Brien static struct command db_show_all_cmds[] = {
83cec9a4bfSDavid E. O'Brien 	{ "procs",	db_ps,			0,	0 },
84cec9a4bfSDavid E. O'Brien 	{ (char *)0 }
85cec9a4bfSDavid E. O'Brien };
86cec9a4bfSDavid E. O'Brien 
87e1e31c0eSJohn Baldwin static struct command_table db_show_all_table = {
88e1e31c0eSJohn Baldwin 	db_show_all_cmds
89e1e31c0eSJohn Baldwin };
90e1e31c0eSJohn Baldwin 
91cec9a4bfSDavid E. O'Brien static struct command db_show_cmds[] = {
92e1e31c0eSJohn Baldwin 	{ "all",	0,			0,	&db_show_all_table },
93cec9a4bfSDavid E. O'Brien 	{ "registers",	db_show_regs,		0,	0 },
94cec9a4bfSDavid E. O'Brien 	{ "breaks",	db_listbreak_cmd, 	0,	0 },
95cec9a4bfSDavid E. O'Brien 	{ "threads",	db_show_threads,	0,	0 },
96cec9a4bfSDavid E. O'Brien 	{ (char *)0, }
97cec9a4bfSDavid E. O'Brien };
98cec9a4bfSDavid E. O'Brien 
99e1e31c0eSJohn Baldwin static struct command_table db_show_table = {
100e1e31c0eSJohn Baldwin 	db_show_cmds,
101e1e31c0eSJohn Baldwin 	SET_BEGIN(db_show_cmd_set),
102e1e31c0eSJohn Baldwin 	SET_LIMIT(db_show_cmd_set)
103e1e31c0eSJohn Baldwin };
104e1e31c0eSJohn Baldwin 
105e1e31c0eSJohn Baldwin static struct command db_commands[] = {
106cec9a4bfSDavid E. O'Brien 	{ "print",	db_print_cmd,		0,	0 },
107cec9a4bfSDavid E. O'Brien 	{ "p",		db_print_cmd,		0,	0 },
108cec9a4bfSDavid E. O'Brien 	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
109cec9a4bfSDavid E. O'Brien 	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
110cec9a4bfSDavid E. O'Brien 	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
111cec9a4bfSDavid E. O'Brien 	{ "set",	db_set_cmd,		CS_OWN,	0 },
112cec9a4bfSDavid E. O'Brien 	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
113cec9a4bfSDavid E. O'Brien 	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
114cec9a4bfSDavid E. O'Brien 	{ "delete",	db_delete_cmd,		0,	0 },
115cec9a4bfSDavid E. O'Brien 	{ "d",		db_delete_cmd,		0,	0 },
116cec9a4bfSDavid E. O'Brien 	{ "break",	db_breakpoint_cmd,	0,	0 },
11775149ab8SBruce Evans 	{ "b",		db_breakpoint_cmd,	0,	0 },
118cec9a4bfSDavid E. O'Brien 	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
119cec9a4bfSDavid E. O'Brien 	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
120cec9a4bfSDavid E. O'Brien 	{ "dhwatch",	db_deletehwatch_cmd,	0,      0 },
121cec9a4bfSDavid E. O'Brien 	{ "hwatch",	db_hwatchpoint_cmd,	0,      0 },
122cec9a4bfSDavid E. O'Brien 	{ "step",	db_single_step_cmd,	0,	0 },
123cec9a4bfSDavid E. O'Brien 	{ "s",		db_single_step_cmd,	0,	0 },
124cec9a4bfSDavid E. O'Brien 	{ "continue",	db_continue_cmd,	0,	0 },
125cec9a4bfSDavid E. O'Brien 	{ "c",		db_continue_cmd,	0,	0 },
126cec9a4bfSDavid E. O'Brien 	{ "until",	db_trace_until_call_cmd,0,	0 },
127cec9a4bfSDavid E. O'Brien 	{ "next",	db_trace_until_matching_cmd,0,	0 },
128cec9a4bfSDavid E. O'Brien 	{ "match",	db_trace_until_matching_cmd,0,	0 },
129cec9a4bfSDavid E. O'Brien 	{ "trace",	db_stack_trace,		CS_OWN,	0 },
13075149ab8SBruce Evans 	{ "t",		db_stack_trace,		CS_OWN,	0 },
1313924da21SJohn Baldwin 	{ "alltrace",	db_stack_trace_all,	0,	0 },
132cec9a4bfSDavid E. O'Brien 	{ "where",	db_stack_trace,		CS_OWN,	0 },
1333924da21SJohn Baldwin 	{ "bt",		db_stack_trace,		CS_OWN,	0 },
134cec9a4bfSDavid E. O'Brien 	{ "call",	db_fncall,		CS_OWN,	0 },
135e1e31c0eSJohn Baldwin 	{ "show",	0,			0,	&db_show_table },
136cec9a4bfSDavid E. O'Brien 	{ "ps",		db_ps,			0,	0 },
137cec9a4bfSDavid E. O'Brien 	{ "gdb",	db_gdb,			0,	0 },
138e4b732cfSBruce Evans 	{ "halt",	db_halt,		0,	0 },
139e4b732cfSBruce Evans 	{ "reboot",	db_reset,		0,	0 },
140cec9a4bfSDavid E. O'Brien 	{ "reset",	db_reset,		0,	0 },
141cec9a4bfSDavid E. O'Brien 	{ "kill",	db_kill,		CS_OWN,	0 },
142cec9a4bfSDavid E. O'Brien 	{ "watchdog",	db_watchdog,		0,	0 },
143cec9a4bfSDavid E. O'Brien 	{ "thread",	db_set_thread,		CS_OWN,	0 },
144c9b0cc3bSRobert Watson 	{ "run",	db_run_cmd,		CS_OWN,	0 },
145c9b0cc3bSRobert Watson 	{ "script",	db_script_cmd,		CS_OWN,	0 },
146c9b0cc3bSRobert Watson 	{ "scripts",	db_scripts_cmd,		0,	0 },
147c9b0cc3bSRobert Watson 	{ "unscript",	db_unscript_cmd,	CS_OWN,	0 },
148086fec57SRobert Watson 	{ "capture",	db_capture_cmd,		CS_OWN,	0 },
149cec9a4bfSDavid E. O'Brien 	{ (char *)0, }
150cec9a4bfSDavid E. O'Brien };
151cec9a4bfSDavid E. O'Brien 
152e1e31c0eSJohn Baldwin static struct command_table db_command_table = {
153e1e31c0eSJohn Baldwin 	db_commands,
154e1e31c0eSJohn Baldwin 	SET_BEGIN(db_cmd_set),
155e1e31c0eSJohn Baldwin 	SET_LIMIT(db_cmd_set)
156e1e31c0eSJohn Baldwin };
157e1e31c0eSJohn Baldwin 
158cec9a4bfSDavid E. O'Brien static struct command	*db_last_command = 0;
1596337f4efSBruce Evans 
1605b81b6b3SRodney W. Grimes /*
1615b81b6b3SRodney W. Grimes  * if 'ed' style: 'dot' is set at start of last item printed,
1625b81b6b3SRodney W. Grimes  * and '+' points to next line.
1635b81b6b3SRodney W. Grimes  * Otherwise: 'dot' points to next item, '..' points to last.
1645b81b6b3SRodney W. Grimes  */
165f73a856dSPoul-Henning Kamp static boolean_t	db_ed_style = TRUE;
1665b81b6b3SRodney W. Grimes 
1675b81b6b3SRodney W. Grimes /*
1685b81b6b3SRodney W. Grimes  * Utility routine - discard tokens through end-of-line.
1695b81b6b3SRodney W. Grimes  */
1705b81b6b3SRodney W. Grimes void
1715b81b6b3SRodney W. Grimes db_skip_to_eol()
1725b81b6b3SRodney W. Grimes {
1735b81b6b3SRodney W. Grimes 	int	t;
1745b81b6b3SRodney W. Grimes 	do {
1755b81b6b3SRodney W. Grimes 	    t = db_read_token();
1765b81b6b3SRodney W. Grimes 	} while (t != tEOL);
1775b81b6b3SRodney W. Grimes }
1785b81b6b3SRodney W. Grimes 
1795b81b6b3SRodney W. Grimes /*
1805b81b6b3SRodney W. Grimes  * Results of command search.
1815b81b6b3SRodney W. Grimes  */
1825b81b6b3SRodney W. Grimes #define	CMD_UNIQUE	0
1835b81b6b3SRodney W. Grimes #define	CMD_FOUND	1
1845b81b6b3SRodney W. Grimes #define	CMD_NONE	2
1855b81b6b3SRodney W. Grimes #define	CMD_AMBIGUOUS	3
1865b81b6b3SRodney W. Grimes #define	CMD_HELP	4
1875b81b6b3SRodney W. Grimes 
188e1e31c0eSJohn Baldwin static void	db_cmd_match(char *name, struct command *cmd,
189e1e31c0eSJohn Baldwin 		    struct command **cmdp, int *resultp);
190e1e31c0eSJohn Baldwin static void	db_cmd_list(struct command_table *table);
191e1e31c0eSJohn Baldwin static int	db_cmd_search(char *name, struct command_table *table,
192e1e31c0eSJohn Baldwin 		    struct command **cmdp);
19314e10f99SAlfred Perlstein static void	db_command(struct command **last_cmdp,
194c9b0cc3bSRobert Watson 		    struct command_table *cmd_table, int dopager);
195058284fcSBruce Evans 
1965b81b6b3SRodney W. Grimes /*
197e1e31c0eSJohn Baldwin  * Helper function to match a single command.
1985b81b6b3SRodney W. Grimes  */
199e1e31c0eSJohn Baldwin static void
200e1e31c0eSJohn Baldwin db_cmd_match(name, cmd, cmdp, resultp)
2015b81b6b3SRodney W. Grimes 	char *		name;
2025b81b6b3SRodney W. Grimes 	struct command	*cmd;
203e1e31c0eSJohn Baldwin 	struct command	**cmdp;	/* out */
204e1e31c0eSJohn Baldwin 	int *		resultp;
205e1e31c0eSJohn Baldwin {
206e1e31c0eSJohn Baldwin 	char *lp, *rp;
207e1e31c0eSJohn Baldwin 	int c;
2085b81b6b3SRodney W. Grimes 
2095b81b6b3SRodney W. Grimes 	lp = name;
2105b81b6b3SRodney W. Grimes 	rp = cmd->name;
2115b81b6b3SRodney W. Grimes 	while ((c = *lp) == *rp) {
2125b81b6b3SRodney W. Grimes 		if (c == 0) {
2135b81b6b3SRodney W. Grimes 			/* complete match */
2145b81b6b3SRodney W. Grimes 			*cmdp = cmd;
215e1e31c0eSJohn Baldwin 			*resultp = CMD_UNIQUE;
216e1e31c0eSJohn Baldwin 			return;
2175b81b6b3SRodney W. Grimes 		}
2185b81b6b3SRodney W. Grimes 		lp++;
2195b81b6b3SRodney W. Grimes 		rp++;
2205b81b6b3SRodney W. Grimes 	}
2215b81b6b3SRodney W. Grimes 	if (c == 0) {
2225b81b6b3SRodney W. Grimes 		/* end of name, not end of command -
2235b81b6b3SRodney W. Grimes 		   partial match */
224e1e31c0eSJohn Baldwin 		if (*resultp == CMD_FOUND) {
225e1e31c0eSJohn Baldwin 			*resultp = CMD_AMBIGUOUS;
2265b81b6b3SRodney W. Grimes 			/* but keep looking for a full match -
2275b81b6b3SRodney W. Grimes 			   this lets us match single letters */
228e1e31c0eSJohn Baldwin 		} else {
2295b81b6b3SRodney W. Grimes 			*cmdp = cmd;
230e1e31c0eSJohn Baldwin 			*resultp = CMD_FOUND;
2315b81b6b3SRodney W. Grimes 		}
2325b81b6b3SRodney W. Grimes 	}
2335b81b6b3SRodney W. Grimes }
2346337f4efSBruce Evans 
235e1e31c0eSJohn Baldwin /*
236e1e31c0eSJohn Baldwin  * Search for command prefix.
237e1e31c0eSJohn Baldwin  */
238e1e31c0eSJohn Baldwin static int
239e1e31c0eSJohn Baldwin db_cmd_search(name, table, cmdp)
240e1e31c0eSJohn Baldwin 	char *		name;
241e1e31c0eSJohn Baldwin 	struct command_table *table;
242e1e31c0eSJohn Baldwin 	struct command	**cmdp;	/* out */
243e1e31c0eSJohn Baldwin {
244e1e31c0eSJohn Baldwin 	struct command	*cmd;
245e1e31c0eSJohn Baldwin 	struct command	**aux_cmdp;
246e1e31c0eSJohn Baldwin 	int		result = CMD_NONE;
247e1e31c0eSJohn Baldwin 
248e1e31c0eSJohn Baldwin 	for (cmd = table->table; cmd->name != 0; cmd++) {
249e1e31c0eSJohn Baldwin 		db_cmd_match(name, cmd, cmdp, &result);
250e1e31c0eSJohn Baldwin 		if (result == CMD_UNIQUE)
2516337f4efSBruce Evans 			return (CMD_UNIQUE);
2526337f4efSBruce Evans 	}
253e1e31c0eSJohn Baldwin 	if (table->aux_tablep != NULL)
254e1e31c0eSJohn Baldwin 		for (aux_cmdp = table->aux_tablep;
255e1e31c0eSJohn Baldwin 		     aux_cmdp < table->aux_tablep_end;
256e1e31c0eSJohn Baldwin 		     aux_cmdp++) {
257e1e31c0eSJohn Baldwin 			db_cmd_match(name, *aux_cmdp, cmdp, &result);
258e1e31c0eSJohn Baldwin 			if (result == CMD_UNIQUE)
259e1e31c0eSJohn Baldwin 				return (CMD_UNIQUE);
2606337f4efSBruce Evans 		}
2615b81b6b3SRodney W. Grimes 	if (result == CMD_NONE) {
2625b81b6b3SRodney W. Grimes 		/* check for 'help' */
2635b81b6b3SRodney W. Grimes 		if (name[0] == 'h' && name[1] == 'e'
2645b81b6b3SRodney W. Grimes 		    && name[2] == 'l' && name[3] == 'p')
2655b81b6b3SRodney W. Grimes 			result = CMD_HELP;
2665b81b6b3SRodney W. Grimes 	}
2675b81b6b3SRodney W. Grimes 	return (result);
2685b81b6b3SRodney W. Grimes }
2695b81b6b3SRodney W. Grimes 
270f73a856dSPoul-Henning Kamp static void
271e1e31c0eSJohn Baldwin db_cmd_list(table)
272e1e31c0eSJohn Baldwin 	struct command_table *table;
2735b81b6b3SRodney W. Grimes {
2745b81b6b3SRodney W. Grimes 	register struct command *cmd;
2756337f4efSBruce Evans 	register struct command **aux_cmdp;
2765b81b6b3SRodney W. Grimes 
277e1e31c0eSJohn Baldwin 	for (cmd = table->table; cmd->name != 0; cmd++) {
2785b81b6b3SRodney W. Grimes 	    db_printf("%-12s", cmd->name);
2792481da74SBruce Evans 	    db_end_line(12);
2805b81b6b3SRodney W. Grimes 	}
281e1e31c0eSJohn Baldwin 	if (table->aux_tablep == NULL)
2826337f4efSBruce Evans 	    return;
283e1e31c0eSJohn Baldwin 	for (aux_cmdp = table->aux_tablep; aux_cmdp < table->aux_tablep_end;
284e1e31c0eSJohn Baldwin 	     aux_cmdp++) {
2856337f4efSBruce Evans 	    db_printf("%-12s", (*aux_cmdp)->name);
2862481da74SBruce Evans 	    db_end_line(12);
2876337f4efSBruce Evans 	}
2885b81b6b3SRodney W. Grimes }
2895b81b6b3SRodney W. Grimes 
290f73a856dSPoul-Henning Kamp static void
291c9b0cc3bSRobert Watson db_command(last_cmdp, cmd_table, dopager)
2925b81b6b3SRodney W. Grimes 	struct command	**last_cmdp;	/* IN_OUT */
293e1e31c0eSJohn Baldwin 	struct command_table *cmd_table;
294c9b0cc3bSRobert Watson 	int dopager;
2955b81b6b3SRodney W. Grimes {
2965b81b6b3SRodney W. Grimes 	struct command	*cmd;
2975b81b6b3SRodney W. Grimes 	int		t;
2985b81b6b3SRodney W. Grimes 	char		modif[TOK_STRING_SIZE];
2995b81b6b3SRodney W. Grimes 	db_expr_t	addr, count;
300381fe1aaSGarrett Wollman 	boolean_t	have_addr = FALSE;
3015b81b6b3SRodney W. Grimes 	int		result;
3025b81b6b3SRodney W. Grimes 
3035b81b6b3SRodney W. Grimes 	t = db_read_token();
3045b81b6b3SRodney W. Grimes 	if (t == tEOL) {
3055b81b6b3SRodney W. Grimes 	    /* empty line repeats last command, at 'next' */
3065b81b6b3SRodney W. Grimes 	    cmd = *last_cmdp;
3075b81b6b3SRodney W. Grimes 	    addr = (db_expr_t)db_next;
3085b81b6b3SRodney W. Grimes 	    have_addr = FALSE;
3095b81b6b3SRodney W. Grimes 	    count = 1;
3105b81b6b3SRodney W. Grimes 	    modif[0] = '\0';
3115b81b6b3SRodney W. Grimes 	}
3125b81b6b3SRodney W. Grimes 	else if (t == tEXCL) {
313976794d9SBruce Evans 	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
3145b81b6b3SRodney W. Grimes 	    return;
3155b81b6b3SRodney W. Grimes 	}
3165b81b6b3SRodney W. Grimes 	else if (t != tIDENT) {
3175b81b6b3SRodney W. Grimes 	    db_printf("?\n");
3185b81b6b3SRodney W. Grimes 	    db_flush_lex();
3195b81b6b3SRodney W. Grimes 	    return;
3205b81b6b3SRodney W. Grimes 	}
3215b81b6b3SRodney W. Grimes 	else {
3225b81b6b3SRodney W. Grimes 	    /*
3235b81b6b3SRodney W. Grimes 	     * Search for command
3245b81b6b3SRodney W. Grimes 	     */
3255b81b6b3SRodney W. Grimes 	    while (cmd_table) {
3265b81b6b3SRodney W. Grimes 		result = db_cmd_search(db_tok_string,
3275b81b6b3SRodney W. Grimes 				       cmd_table,
3285b81b6b3SRodney W. Grimes 				       &cmd);
3295b81b6b3SRodney W. Grimes 		switch (result) {
3305b81b6b3SRodney W. Grimes 		    case CMD_NONE:
3315b81b6b3SRodney W. Grimes 			db_printf("No such command\n");
3325b81b6b3SRodney W. Grimes 			db_flush_lex();
3335b81b6b3SRodney W. Grimes 			return;
3345b81b6b3SRodney W. Grimes 		    case CMD_AMBIGUOUS:
3355b81b6b3SRodney W. Grimes 			db_printf("Ambiguous\n");
3365b81b6b3SRodney W. Grimes 			db_flush_lex();
3375b81b6b3SRodney W. Grimes 			return;
3385b81b6b3SRodney W. Grimes 		    case CMD_HELP:
339e1e31c0eSJohn Baldwin 			db_cmd_list(cmd_table);
3405b81b6b3SRodney W. Grimes 			db_flush_lex();
3415b81b6b3SRodney W. Grimes 			return;
3425b81b6b3SRodney W. Grimes 		    default:
3435b81b6b3SRodney W. Grimes 			break;
3445b81b6b3SRodney W. Grimes 		}
345e1e31c0eSJohn Baldwin 		if ((cmd_table = cmd->more) != NULL) {
3465b81b6b3SRodney W. Grimes 		    t = db_read_token();
3475b81b6b3SRodney W. Grimes 		    if (t != tIDENT) {
348e1e31c0eSJohn Baldwin 			db_cmd_list(cmd_table);
3495b81b6b3SRodney W. Grimes 			db_flush_lex();
3505b81b6b3SRodney W. Grimes 			return;
3515b81b6b3SRodney W. Grimes 		    }
3525b81b6b3SRodney W. Grimes 		}
3535b81b6b3SRodney W. Grimes 	    }
3545b81b6b3SRodney W. Grimes 
3555b81b6b3SRodney W. Grimes 	    if ((cmd->flag & CS_OWN) == 0) {
3565b81b6b3SRodney W. Grimes 		/*
3575b81b6b3SRodney W. Grimes 		 * Standard syntax:
3585b81b6b3SRodney W. Grimes 		 * command [/modifier] [addr] [,count]
3595b81b6b3SRodney W. Grimes 		 */
3605b81b6b3SRodney W. Grimes 		t = db_read_token();
3615b81b6b3SRodney W. Grimes 		if (t == tSLASH) {
3625b81b6b3SRodney W. Grimes 		    t = db_read_token();
3635b81b6b3SRodney W. Grimes 		    if (t != tIDENT) {
3645b81b6b3SRodney W. Grimes 			db_printf("Bad modifier\n");
3655b81b6b3SRodney W. Grimes 			db_flush_lex();
3665b81b6b3SRodney W. Grimes 			return;
3675b81b6b3SRodney W. Grimes 		    }
3685b81b6b3SRodney W. Grimes 		    db_strcpy(modif, db_tok_string);
3695b81b6b3SRodney W. Grimes 		}
3705b81b6b3SRodney W. Grimes 		else {
3715b81b6b3SRodney W. Grimes 		    db_unread_token(t);
3725b81b6b3SRodney W. Grimes 		    modif[0] = '\0';
3735b81b6b3SRodney W. Grimes 		}
3745b81b6b3SRodney W. Grimes 
3755b81b6b3SRodney W. Grimes 		if (db_expression(&addr)) {
3765b81b6b3SRodney W. Grimes 		    db_dot = (db_addr_t) addr;
3775b81b6b3SRodney W. Grimes 		    db_last_addr = db_dot;
3785b81b6b3SRodney W. Grimes 		    have_addr = TRUE;
3795b81b6b3SRodney W. Grimes 		}
3805b81b6b3SRodney W. Grimes 		else {
3815b81b6b3SRodney W. Grimes 		    addr = (db_expr_t) db_dot;
3825b81b6b3SRodney W. Grimes 		    have_addr = FALSE;
3835b81b6b3SRodney W. Grimes 		}
3845b81b6b3SRodney W. Grimes 		t = db_read_token();
3855b81b6b3SRodney W. Grimes 		if (t == tCOMMA) {
3865b81b6b3SRodney W. Grimes 		    if (!db_expression(&count)) {
3875b81b6b3SRodney W. Grimes 			db_printf("Count missing\n");
3885b81b6b3SRodney W. Grimes 			db_flush_lex();
3895b81b6b3SRodney W. Grimes 			return;
3905b81b6b3SRodney W. Grimes 		    }
3915b81b6b3SRodney W. Grimes 		}
3925b81b6b3SRodney W. Grimes 		else {
3935b81b6b3SRodney W. Grimes 		    db_unread_token(t);
3945b81b6b3SRodney W. Grimes 		    count = -1;
3955b81b6b3SRodney W. Grimes 		}
3965b81b6b3SRodney W. Grimes 		if ((cmd->flag & CS_MORE) == 0) {
3975b81b6b3SRodney W. Grimes 		    db_skip_to_eol();
3985b81b6b3SRodney W. Grimes 		}
3995b81b6b3SRodney W. Grimes 	    }
4005b81b6b3SRodney W. Grimes 	}
4015b81b6b3SRodney W. Grimes 	*last_cmdp = cmd;
4025b81b6b3SRodney W. Grimes 	if (cmd != 0) {
4035b81b6b3SRodney W. Grimes 	    /*
4045b81b6b3SRodney W. Grimes 	     * Execute the command.
4055b81b6b3SRodney W. Grimes 	     */
406c9b0cc3bSRobert Watson 	    if (dopager)
40719e9205aSJohn Baldwin 		db_enable_pager();
408c9b0cc3bSRobert Watson 	    else
409c9b0cc3bSRobert Watson 		db_disable_pager();
4105b81b6b3SRodney W. Grimes 	    (*cmd->fcn)(addr, have_addr, count, modif);
411c9b0cc3bSRobert Watson 	    if (dopager)
41219e9205aSJohn Baldwin 		db_disable_pager();
4135b81b6b3SRodney W. Grimes 
4145b81b6b3SRodney W. Grimes 	    if (cmd->flag & CS_SET_DOT) {
4155b81b6b3SRodney W. Grimes 		/*
4165b81b6b3SRodney W. Grimes 		 * If command changes dot, set dot to
4175b81b6b3SRodney W. Grimes 		 * previous address displayed (if 'ed' style).
4185b81b6b3SRodney W. Grimes 		 */
4195b81b6b3SRodney W. Grimes 		if (db_ed_style) {
4205b81b6b3SRodney W. Grimes 		    db_dot = db_prev;
4215b81b6b3SRodney W. Grimes 		}
4225b81b6b3SRodney W. Grimes 		else {
4235b81b6b3SRodney W. Grimes 		    db_dot = db_next;
4245b81b6b3SRodney W. Grimes 		}
4255b81b6b3SRodney W. Grimes 	    }
4265b81b6b3SRodney W. Grimes 	    else {
4275b81b6b3SRodney W. Grimes 		/*
4285b81b6b3SRodney W. Grimes 		 * If command does not change dot,
4295b81b6b3SRodney W. Grimes 		 * set 'next' location to be the same.
4305b81b6b3SRodney W. Grimes 		 */
4315b81b6b3SRodney W. Grimes 		db_next = db_dot;
4325b81b6b3SRodney W. Grimes 	    }
4335b81b6b3SRodney W. Grimes 	}
4345b81b6b3SRodney W. Grimes }
4355b81b6b3SRodney W. Grimes 
4365b81b6b3SRodney W. Grimes /*
437b7aa38c1SBruce Evans  * At least one non-optional command must be implemented using
438b7aa38c1SBruce Evans  * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
439b7aa38c1SBruce Evans  */
440b7aa38c1SBruce Evans DB_COMMAND(panic, db_panic)
4413eac4884SPoul-Henning Kamp {
44215cc91d3SJohn Baldwin 	db_disable_pager();
443edf8a815SDavid Greenman 	panic("from debugger");
4443eac4884SPoul-Henning Kamp }
4453eac4884SPoul-Henning Kamp 
4463eac4884SPoul-Henning Kamp void
4475b81b6b3SRodney W. Grimes db_command_loop()
4485b81b6b3SRodney W. Grimes {
4495b81b6b3SRodney W. Grimes 	/*
4505b81b6b3SRodney W. Grimes 	 * Initialize 'prev' and 'next' to dot.
4515b81b6b3SRodney W. Grimes 	 */
4525b81b6b3SRodney W. Grimes 	db_prev = db_dot;
4535b81b6b3SRodney W. Grimes 	db_next = db_dot;
4545b81b6b3SRodney W. Grimes 
4555b81b6b3SRodney W. Grimes 	db_cmd_loop_done = 0;
4565b81b6b3SRodney W. Grimes 	while (!db_cmd_loop_done) {
4575b81b6b3SRodney W. Grimes 	    if (db_print_position() != 0)
4585b81b6b3SRodney W. Grimes 		db_printf("\n");
4595b81b6b3SRodney W. Grimes 
4605b81b6b3SRodney W. Grimes 	    db_printf("db> ");
4615b81b6b3SRodney W. Grimes 	    (void) db_read_line();
4625b81b6b3SRodney W. Grimes 
463c9b0cc3bSRobert Watson 	    db_command(&db_last_command, &db_command_table, /* dopager */ 1);
4645b81b6b3SRodney W. Grimes 	}
4655b81b6b3SRodney W. Grimes }
4665b81b6b3SRodney W. Grimes 
467c9b0cc3bSRobert Watson /*
468c9b0cc3bSRobert Watson  * Execute a command on behalf of a script.  The caller is responsible for
469c9b0cc3bSRobert Watson  * making sure that the command string is < DB_MAXLINE or it will be
470c9b0cc3bSRobert Watson  * truncated.
471c9b0cc3bSRobert Watson  *
472c9b0cc3bSRobert Watson  * XXXRW: Runs by injecting faked input into DDB input stream; it would be
473c9b0cc3bSRobert Watson  * nicer to use an alternative approach that didn't mess with the previous
474c9b0cc3bSRobert Watson  * command buffer.
475c9b0cc3bSRobert Watson  */
476c9b0cc3bSRobert Watson void
477c9b0cc3bSRobert Watson db_command_script(const char *command)
478c9b0cc3bSRobert Watson {
479c9b0cc3bSRobert Watson 	db_prev = db_next = db_dot;
480c9b0cc3bSRobert Watson 	db_inject_line(command);
481c9b0cc3bSRobert Watson 	db_command(&db_last_command, &db_command_table, /* dopager */ 0);
482c9b0cc3bSRobert Watson }
483c9b0cc3bSRobert Watson 
4845b81b6b3SRodney W. Grimes void
4855b81b6b3SRodney W. Grimes db_error(s)
486bda9921dSMark Murray 	const char *s;
4875b81b6b3SRodney W. Grimes {
4885b81b6b3SRodney W. Grimes 	if (s)
48958d9a059SKris Kennaway 	    db_printf("%s", s);
4905b81b6b3SRodney W. Grimes 	db_flush_lex();
49137224cd3SMarcel Moolenaar 	kdb_reenter();
4925b81b6b3SRodney W. Grimes }
4935b81b6b3SRodney W. Grimes 
4945b81b6b3SRodney W. Grimes 
4955b81b6b3SRodney W. Grimes /*
4965b81b6b3SRodney W. Grimes  * Call random function:
4975b81b6b3SRodney W. Grimes  * !expr(arg,arg,arg)
4985b81b6b3SRodney W. Grimes  */
499a2aeb24eSMarcel Moolenaar 
500a2aeb24eSMarcel Moolenaar /* The generic implementation supports a maximum of 10 arguments. */
501a2aeb24eSMarcel Moolenaar typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
502a2aeb24eSMarcel Moolenaar     db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
503a2aeb24eSMarcel Moolenaar 
504a2aeb24eSMarcel Moolenaar static __inline int
505a2aeb24eSMarcel Moolenaar db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
506a2aeb24eSMarcel Moolenaar {
507a2aeb24eSMarcel Moolenaar 	__db_f *f = (__db_f *)addr;
508a2aeb24eSMarcel Moolenaar 
509a2aeb24eSMarcel Moolenaar 	if (nargs > 10) {
510a2aeb24eSMarcel Moolenaar 		db_printf("Too many arguments (max 10)\n");
511a2aeb24eSMarcel Moolenaar 		return (0);
512a2aeb24eSMarcel Moolenaar 	}
513a2aeb24eSMarcel Moolenaar 	*rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
514a2aeb24eSMarcel Moolenaar 	    args[6], args[7], args[8], args[9]);
515a2aeb24eSMarcel Moolenaar 	return (1);
516a2aeb24eSMarcel Moolenaar }
517a2aeb24eSMarcel Moolenaar 
518f73a856dSPoul-Henning Kamp static void
519976794d9SBruce Evans db_fncall(dummy1, dummy2, dummy3, dummy4)
520976794d9SBruce Evans 	db_expr_t	dummy1;
521976794d9SBruce Evans 	boolean_t	dummy2;
522976794d9SBruce Evans 	db_expr_t	dummy3;
523976794d9SBruce Evans 	char *		dummy4;
5245b81b6b3SRodney W. Grimes {
5255b81b6b3SRodney W. Grimes 	db_expr_t	fn_addr;
526a2aeb24eSMarcel Moolenaar 	db_expr_t	args[DB_MAXARGS];
5275b81b6b3SRodney W. Grimes 	int		nargs = 0;
5285b81b6b3SRodney W. Grimes 	db_expr_t	retval;
5295b81b6b3SRodney W. Grimes 	int		t;
5305b81b6b3SRodney W. Grimes 
5315b81b6b3SRodney W. Grimes 	if (!db_expression(&fn_addr)) {
5325b81b6b3SRodney W. Grimes 	    db_printf("Bad function\n");
5335b81b6b3SRodney W. Grimes 	    db_flush_lex();
5345b81b6b3SRodney W. Grimes 	    return;
5355b81b6b3SRodney W. Grimes 	}
5365b81b6b3SRodney W. Grimes 
5375b81b6b3SRodney W. Grimes 	t = db_read_token();
5385b81b6b3SRodney W. Grimes 	if (t == tLPAREN) {
5395b81b6b3SRodney W. Grimes 	    if (db_expression(&args[0])) {
5405b81b6b3SRodney W. Grimes 		nargs++;
5415b81b6b3SRodney W. Grimes 		while ((t = db_read_token()) == tCOMMA) {
542a2aeb24eSMarcel Moolenaar 		    if (nargs == DB_MAXARGS) {
543a2aeb24eSMarcel Moolenaar 			db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
5445b81b6b3SRodney W. Grimes 			db_flush_lex();
5455b81b6b3SRodney W. Grimes 			return;
5465b81b6b3SRodney W. Grimes 		    }
5475b81b6b3SRodney W. Grimes 		    if (!db_expression(&args[nargs])) {
5485b81b6b3SRodney W. Grimes 			db_printf("Argument missing\n");
5495b81b6b3SRodney W. Grimes 			db_flush_lex();
5505b81b6b3SRodney W. Grimes 			return;
5515b81b6b3SRodney W. Grimes 		    }
5525b81b6b3SRodney W. Grimes 		    nargs++;
5535b81b6b3SRodney W. Grimes 		}
5545b81b6b3SRodney W. Grimes 		db_unread_token(t);
5555b81b6b3SRodney W. Grimes 	    }
5565b81b6b3SRodney W. Grimes 	    if (db_read_token() != tRPAREN) {
5575b81b6b3SRodney W. Grimes 		db_printf("?\n");
5585b81b6b3SRodney W. Grimes 		db_flush_lex();
5595b81b6b3SRodney W. Grimes 		return;
5605b81b6b3SRodney W. Grimes 	    }
5615b81b6b3SRodney W. Grimes 	}
5625b81b6b3SRodney W. Grimes 	db_skip_to_eol();
56315cc91d3SJohn Baldwin 	db_disable_pager();
5645b81b6b3SRodney W. Grimes 
565a2aeb24eSMarcel Moolenaar 	if (DB_CALL(fn_addr, &retval, nargs, args))
566a2aeb24eSMarcel Moolenaar 		db_printf("= %#lr\n", (long)retval);
5675b81b6b3SRodney W. Grimes }
568ad146781SPaul Traina 
5695370c3b6SPeter Wemm static void
570e4b732cfSBruce Evans db_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
571e4b732cfSBruce Evans {
572e4b732cfSBruce Evans 
573e4b732cfSBruce Evans 	cpu_halt();
574e4b732cfSBruce Evans }
575e4b732cfSBruce Evans 
576e4b732cfSBruce Evans static void
57719d2c78fSDima Dorfman db_kill(dummy1, dummy2, dummy3, dummy4)
57819d2c78fSDima Dorfman 	db_expr_t	dummy1;
57919d2c78fSDima Dorfman 	boolean_t	dummy2;
58019d2c78fSDima Dorfman 	db_expr_t	dummy3;
58119d2c78fSDima Dorfman 	char *		dummy4;
58219d2c78fSDima Dorfman {
58319d2c78fSDima Dorfman 	db_expr_t old_radix, pid, sig;
58419d2c78fSDima Dorfman 	struct proc *p;
58519d2c78fSDima Dorfman 
58619d2c78fSDima Dorfman #define DB_ERROR(f)	do { db_printf f; db_flush_lex(); goto out; } while (0)
58719d2c78fSDima Dorfman 
58819d2c78fSDima Dorfman 	/*
58919d2c78fSDima Dorfman 	 * PIDs and signal numbers are typically represented in base
59019d2c78fSDima Dorfman 	 * 10, so make that the default here.  It can, of course, be
59119d2c78fSDima Dorfman 	 * overridden by specifying a prefix.
59219d2c78fSDima Dorfman 	 */
59319d2c78fSDima Dorfman 	old_radix = db_radix;
59419d2c78fSDima Dorfman 	db_radix = 10;
59519d2c78fSDima Dorfman 	/* Retrieve arguments. */
59619d2c78fSDima Dorfman 	if (!db_expression(&sig))
59719d2c78fSDima Dorfman 		DB_ERROR(("Missing signal number\n"));
59819d2c78fSDima Dorfman 	if (!db_expression(&pid))
59919d2c78fSDima Dorfman 		DB_ERROR(("Missing process ID\n"));
60019d2c78fSDima Dorfman 	db_skip_to_eol();
60119d2c78fSDima Dorfman 	if (sig < 0 || sig > _SIG_MAXSIG)
60219d2c78fSDima Dorfman 		DB_ERROR(("Signal number out of range\n"));
60319d2c78fSDima Dorfman 
60419d2c78fSDima Dorfman 	/*
60519d2c78fSDima Dorfman 	 * Find the process in question.  allproc_lock is not needed
60619d2c78fSDima Dorfman 	 * since we're in DDB.
60719d2c78fSDima Dorfman 	 */
60819d2c78fSDima Dorfman 	/* sx_slock(&allproc_lock); */
609f67af5c9SXin LI 	FOREACH_PROC_IN_SYSTEM(p)
61019d2c78fSDima Dorfman 	    if (p->p_pid == pid)
61119d2c78fSDima Dorfman 		    break;
61219d2c78fSDima Dorfman 	/* sx_sunlock(&allproc_lock); */
61319d2c78fSDima Dorfman 	if (p == NULL)
614e30e3e9eSMatt Jacob 		DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
61519d2c78fSDima Dorfman 
61619d2c78fSDima Dorfman 	/* If it's already locked, bail; otherwise, do the deed. */
61719d2c78fSDima Dorfman 	if (PROC_TRYLOCK(p) == 0)
618e30e3e9eSMatt Jacob 		DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
61919d2c78fSDima Dorfman 	else {
62019d2c78fSDima Dorfman 		psignal(p, sig);
62119d2c78fSDima Dorfman 		PROC_UNLOCK(p);
62219d2c78fSDima Dorfman 	}
62319d2c78fSDima Dorfman 
62419d2c78fSDima Dorfman out:
62519d2c78fSDima Dorfman 	db_radix = old_radix;
62619d2c78fSDima Dorfman #undef DB_ERROR
62719d2c78fSDima Dorfman }
62819d2c78fSDima Dorfman 
62919d2c78fSDima Dorfman static void
6305370c3b6SPeter Wemm db_reset(dummy1, dummy2, dummy3, dummy4)
6315370c3b6SPeter Wemm 	db_expr_t	dummy1;
6325370c3b6SPeter Wemm 	boolean_t	dummy2;
6335370c3b6SPeter Wemm 	db_expr_t	dummy3;
6345370c3b6SPeter Wemm 	char *		dummy4;
6355370c3b6SPeter Wemm {
6365370c3b6SPeter Wemm 
6375370c3b6SPeter Wemm 	cpu_reset();
6385370c3b6SPeter Wemm }
63974cc032bSPoul-Henning Kamp 
64074cc032bSPoul-Henning Kamp static void
64174cc032bSPoul-Henning Kamp db_watchdog(dummy1, dummy2, dummy3, dummy4)
64274cc032bSPoul-Henning Kamp 	db_expr_t	dummy1;
64374cc032bSPoul-Henning Kamp 	boolean_t	dummy2;
64474cc032bSPoul-Henning Kamp 	db_expr_t	dummy3;
64574cc032bSPoul-Henning Kamp 	char *		dummy4;
64674cc032bSPoul-Henning Kamp {
64774cc032bSPoul-Henning Kamp 	int i;
64874cc032bSPoul-Henning Kamp 
64974cc032bSPoul-Henning Kamp 	/*
65074cc032bSPoul-Henning Kamp 	 * XXX: It might make sense to be able to set the watchdog to a
65174cc032bSPoul-Henning Kamp 	 * XXX: timeout here so that failure or hang as a result of subsequent
65274cc032bSPoul-Henning Kamp 	 * XXX: ddb commands could be recovered by a reset.
65374cc032bSPoul-Henning Kamp 	 */
65474cc032bSPoul-Henning Kamp 
65574cc032bSPoul-Henning Kamp 	EVENTHANDLER_INVOKE(watchdog_list, 0, &i);
65674cc032bSPoul-Henning Kamp }
657f3be7cb3SMarcel Moolenaar 
658f3be7cb3SMarcel Moolenaar static void
659f3be7cb3SMarcel Moolenaar db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
660f3be7cb3SMarcel Moolenaar {
661f3be7cb3SMarcel Moolenaar 
662f3be7cb3SMarcel Moolenaar 	if (kdb_dbbe_select("gdb") != 0)
663f3be7cb3SMarcel Moolenaar 		db_printf("The remote GDB backend could not be selected.\n");
664f3be7cb3SMarcel Moolenaar 	else
665f3be7cb3SMarcel Moolenaar 		db_printf("Step to enter the remote GDB backend.\n");
666f3be7cb3SMarcel Moolenaar }
667fd32d93bSMarcel Moolenaar 
668fd32d93bSMarcel Moolenaar static void
669fd32d93bSMarcel Moolenaar db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
670fd32d93bSMarcel Moolenaar {
671fd32d93bSMarcel Moolenaar 	struct thread *td;
672fd32d93bSMarcel Moolenaar 	db_expr_t radix;
673a13aca1aSRobert Watson 	pid_t pid;
674fd32d93bSMarcel Moolenaar 	int t;
675fd32d93bSMarcel Moolenaar 
676fd32d93bSMarcel Moolenaar 	/*
677fd32d93bSMarcel Moolenaar 	 * We parse our own arguments. We don't like the default radix.
678fd32d93bSMarcel Moolenaar 	 */
679fd32d93bSMarcel Moolenaar 	radix = db_radix;
680fd32d93bSMarcel Moolenaar 	db_radix = 10;
681fd32d93bSMarcel Moolenaar 	hastid = db_expression(&tid);
682fd32d93bSMarcel Moolenaar 	t = db_read_token();
683fd32d93bSMarcel Moolenaar 	if (t == tCOMMA) {
684fd32d93bSMarcel Moolenaar 		if (!db_expression(&count)) {
685fd32d93bSMarcel Moolenaar 			db_printf("Count missing\n");
686fd32d93bSMarcel Moolenaar 			db_flush_lex();
687fd32d93bSMarcel Moolenaar 			return;
688fd32d93bSMarcel Moolenaar 		}
689fd32d93bSMarcel Moolenaar 	} else {
690fd32d93bSMarcel Moolenaar 		db_unread_token(t);
691fd32d93bSMarcel Moolenaar 		count = -1;
692fd32d93bSMarcel Moolenaar 	}
693fd32d93bSMarcel Moolenaar 	db_skip_to_eol();
694fd32d93bSMarcel Moolenaar 	db_radix = radix;
695fd32d93bSMarcel Moolenaar 
696fd32d93bSMarcel Moolenaar 	if (hastid) {
697fd32d93bSMarcel Moolenaar 		td = kdb_thr_lookup((lwpid_t)tid);
698fd32d93bSMarcel Moolenaar 		if (td == NULL)
699fd32d93bSMarcel Moolenaar 			td = kdb_thr_from_pid((pid_t)tid);
700fd32d93bSMarcel Moolenaar 		if (td == NULL) {
701fd32d93bSMarcel Moolenaar 			db_printf("Thread %d not found\n", (int)tid);
702fd32d93bSMarcel Moolenaar 			return;
703fd32d93bSMarcel Moolenaar 		}
704fd32d93bSMarcel Moolenaar 	} else
705fd32d93bSMarcel Moolenaar 		td = kdb_thread;
706a13aca1aSRobert Watson 	if (td->td_proc != NULL)
707a13aca1aSRobert Watson 		pid = td->td_proc->p_pid;
708a13aca1aSRobert Watson 	else
709a13aca1aSRobert Watson 		pid = -1;
710a13aca1aSRobert Watson 	db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
711fd32d93bSMarcel Moolenaar 	db_trace_thread(td, count);
712fd32d93bSMarcel Moolenaar }
713a7ad956bSRobert Watson 
714a7ad956bSRobert Watson static void
715a7ad956bSRobert Watson db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
716a7ad956bSRobert Watson     char *dummy4)
717a7ad956bSRobert Watson {
718a7ad956bSRobert Watson 	struct proc *p;
719a7ad956bSRobert Watson 	struct thread *td;
7209641e389SKonstantin Belousov 	jmp_buf jb;
7219641e389SKonstantin Belousov 	void *prev_jb;
722a7ad956bSRobert Watson 
723f67af5c9SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
7249641e389SKonstantin Belousov 		prev_jb = kdb_jmpbuf(jb);
7259641e389SKonstantin Belousov 		if (setjmp(jb) == 0) {
726a7ad956bSRobert Watson 			FOREACH_THREAD_IN_PROC(p, td) {
727a7ad956bSRobert Watson 				db_printf("\nTracing command %s pid %d tid %ld td %p\n",
728a7ad956bSRobert Watson 					  p->p_comm, p->p_pid, (long)td->td_tid, td);
729a7ad956bSRobert Watson 				db_trace_thread(td, -1);
7309641e389SKonstantin Belousov 				if (db_pager_quit) {
7319641e389SKonstantin Belousov 					kdb_jmpbuf(prev_jb);
732da927f93SOlivier Houchard 					return;
733a7ad956bSRobert Watson 				}
734a7ad956bSRobert Watson 			}
735a7ad956bSRobert Watson 		}
7369641e389SKonstantin Belousov 		kdb_jmpbuf(prev_jb);
7379641e389SKonstantin Belousov 	}
7389641e389SKonstantin Belousov }
739