db_command.c revision 148919
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 148919 2005-08-10 07:08:14Z obrien $");
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;
7186998Sddstatic db_cmdfcn_t	db_kill;
7285944Speterstatic db_cmdfcn_t	db_reset;
73132482Smarcelstatic db_cmdfcn_t	db_stack_trace;
74126399Sphkstatic db_cmdfcn_t	db_watchdog;
7517848Spst
76148919Sobrien/*
77148919Sobrien * 'show' commands
78148919Sobrien */
7918296Sbde
80148919Sobrienstatic struct command db_show_all_cmds[] = {
81148919Sobrien	{ "procs",	db_ps,			0,	0 },
82148919Sobrien	{ (char *)0 }
83148919Sobrien};
84148919Sobrien
85148919Sobrienstatic struct command db_show_cmds[] = {
86148919Sobrien	{ "all",	0,			0,	db_show_all_cmds },
87148919Sobrien	{ "registers",	db_show_regs,		0,	0 },
88148919Sobrien	{ "breaks",	db_listbreak_cmd, 	0,	0 },
89148919Sobrien	{ "threads",	db_show_threads,	0,	0 },
90148919Sobrien	{ (char *)0, }
91148919Sobrien};
92148919Sobrien
93148919Sobrienstatic struct command db_command_table[] = {
94148919Sobrien	{ "print",	db_print_cmd,		0,	0 },
95148919Sobrien	{ "p",		db_print_cmd,		0,	0 },
96148919Sobrien	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
97148919Sobrien	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
98148919Sobrien	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
99148919Sobrien	{ "set",	db_set_cmd,		CS_OWN,	0 },
100148919Sobrien	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
101148919Sobrien	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
102148919Sobrien	{ "delete",	db_delete_cmd,		0,	0 },
103148919Sobrien	{ "d",		db_delete_cmd,		0,	0 },
104148919Sobrien	{ "break",	db_breakpoint_cmd,	0,	0 },
105148919Sobrien	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
106148919Sobrien	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
107148919Sobrien	{ "dhwatch",	db_deletehwatch_cmd,	0,      0 },
108148919Sobrien	{ "hwatch",	db_hwatchpoint_cmd,	0,      0 },
109148919Sobrien	{ "step",	db_single_step_cmd,	0,	0 },
110148919Sobrien	{ "s",		db_single_step_cmd,	0,	0 },
111148919Sobrien	{ "continue",	db_continue_cmd,	0,	0 },
112148919Sobrien	{ "c",		db_continue_cmd,	0,	0 },
113148919Sobrien	{ "until",	db_trace_until_call_cmd,0,	0 },
114148919Sobrien	{ "next",	db_trace_until_matching_cmd,0,	0 },
115148919Sobrien	{ "match",	db_trace_until_matching_cmd,0,	0 },
116148919Sobrien	{ "trace",	db_stack_trace,		CS_OWN,	0 },
117148919Sobrien	{ "where",	db_stack_trace,		CS_OWN,	0 },
118148919Sobrien	{ "call",	db_fncall,		CS_OWN,	0 },
119148919Sobrien	{ "show",	0,			0,	db_show_cmds },
120148919Sobrien	{ "ps",		db_ps,			0,	0 },
121148919Sobrien	{ "gdb",	db_gdb,			0,	0 },
122148919Sobrien	{ "reset",	db_reset,		0,	0 },
123148919Sobrien	{ "kill",	db_kill,		CS_OWN,	0 },
124148919Sobrien	{ "watchdog",	db_watchdog,		0,	0 },
125148919Sobrien	{ "thread",	db_set_thread,		CS_OWN,	0 },
126148919Sobrien	{ (char *)0, }
127148919Sobrien};
128148919Sobrien
129148919Sobrienstatic struct command	*db_last_command = 0;
130148919Sobrien
1314Srgrimes/*
1324Srgrimes * if 'ed' style: 'dot' is set at start of last item printed,
1334Srgrimes * and '+' points to next line.
1344Srgrimes * Otherwise: 'dot' points to next item, '..' points to last.
1354Srgrimes */
13612515Sphkstatic boolean_t	db_ed_style = TRUE;
1374Srgrimes
1384Srgrimes/*
1394Srgrimes * Utility routine - discard tokens through end-of-line.
1404Srgrimes */
1414Srgrimesvoid
1424Srgrimesdb_skip_to_eol()
1434Srgrimes{
1444Srgrimes	int	t;
1454Srgrimes	do {
1464Srgrimes	    t = db_read_token();
1474Srgrimes	} while (t != tEOL);
1484Srgrimes}
1494Srgrimes
1504Srgrimes/*
1514Srgrimes * Results of command search.
1524Srgrimes */
1534Srgrimes#define	CMD_UNIQUE	0
1544Srgrimes#define	CMD_FOUND	1
1554Srgrimes#define	CMD_NONE	2
1564Srgrimes#define	CMD_AMBIGUOUS	3
1574Srgrimes#define	CMD_HELP	4
1584Srgrimes
15993009Sbdestatic void	db_cmd_list(struct command *table, struct command **aux_tablep,
16093009Sbde		    struct command **aux_tablep_end);
16192756Salfredstatic int	db_cmd_search(char *name, struct command *table,
16293009Sbde		    struct command **aux_tablep,
16393009Sbde		    struct command **aux_tablep_end, struct command **cmdp);
16492756Salfredstatic void	db_command(struct command **last_cmdp,
16593009Sbde		    struct command *cmd_table, struct command **aux_cmd_tablep,
16693009Sbde		    struct command **aux_cmd_tablep_end);
16712473Sbde
1684Srgrimes/*
1694Srgrimes * Search for command prefix.
1704Srgrimes */
17112515Sphkstatic int
17278161Speterdb_cmd_search(name, table, aux_tablep, aux_tablep_end, cmdp)
1734Srgrimes	char *		name;
1744Srgrimes	struct command	*table;
17518296Sbde	struct command	**aux_tablep;
17678161Speter	struct command	**aux_tablep_end;
1774Srgrimes	struct command	**cmdp;	/* out */
1784Srgrimes{
1794Srgrimes	struct command	*cmd;
18018296Sbde	struct command	**aux_cmdp;
1814Srgrimes	int		result = CMD_NONE;
1824Srgrimes
1834Srgrimes	for (cmd = table; cmd->name != 0; cmd++) {
1844Srgrimes	    register char *lp;
1854Srgrimes	    register char *rp;
1864Srgrimes	    register int  c;
1874Srgrimes
1884Srgrimes	    lp = name;
1894Srgrimes	    rp = cmd->name;
1904Srgrimes	    while ((c = *lp) == *rp) {
1914Srgrimes		if (c == 0) {
1924Srgrimes		    /* complete match */
1934Srgrimes		    *cmdp = cmd;
1944Srgrimes		    return (CMD_UNIQUE);
1954Srgrimes		}
1964Srgrimes		lp++;
1974Srgrimes		rp++;
1984Srgrimes	    }
1994Srgrimes	    if (c == 0) {
2004Srgrimes		/* end of name, not end of command -
2014Srgrimes		   partial match */
2024Srgrimes		if (result == CMD_FOUND) {
2034Srgrimes		    result = CMD_AMBIGUOUS;
2044Srgrimes		    /* but keep looking for a full match -
2054Srgrimes		       this lets us match single letters */
2064Srgrimes		}
2074Srgrimes		else {
2084Srgrimes		    *cmdp = cmd;
2094Srgrimes		    result = CMD_FOUND;
2104Srgrimes		}
2114Srgrimes	    }
2124Srgrimes	}
21318296Sbde	if (result == CMD_NONE && aux_tablep != 0)
21418296Sbde	    /* XXX repeat too much code. */
21578161Speter	    for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
21618296Sbde		register char *lp;
21718296Sbde		register char *rp;
21818296Sbde		register int  c;
21918296Sbde
22018296Sbde		lp = name;
22118296Sbde		rp = (*aux_cmdp)->name;
22218296Sbde		while ((c = *lp) == *rp) {
22318296Sbde		    if (c == 0) {
22418296Sbde			/* complete match */
22518296Sbde			*cmdp = *aux_cmdp;
22618296Sbde			return (CMD_UNIQUE);
22718296Sbde		    }
22818296Sbde		    lp++;
22918296Sbde		    rp++;
23018296Sbde		}
23118296Sbde		if (c == 0) {
23218296Sbde		    /* end of name, not end of command -
23318296Sbde		       partial match */
23418296Sbde		    if (result == CMD_FOUND) {
23518296Sbde			result = CMD_AMBIGUOUS;
23618296Sbde			/* but keep looking for a full match -
23718296Sbde			   this lets us match single letters */
23818296Sbde		    }
23918296Sbde		    else {
24018296Sbde			*cmdp = *aux_cmdp;
24118296Sbde			result = CMD_FOUND;
24218296Sbde		    }
24318296Sbde		}
24418296Sbde	    }
2454Srgrimes	if (result == CMD_NONE) {
2464Srgrimes	    /* check for 'help' */
2474Srgrimes		if (name[0] == 'h' && name[1] == 'e'
2484Srgrimes		    && name[2] == 'l' && name[3] == 'p')
2494Srgrimes			result = CMD_HELP;
2504Srgrimes	}
2514Srgrimes	return (result);
2524Srgrimes}
2534Srgrimes
25412515Sphkstatic void
25578161Speterdb_cmd_list(table, aux_tablep, aux_tablep_end)
2564Srgrimes	struct command *table;
25718296Sbde	struct command **aux_tablep;
25878161Speter	struct command **aux_tablep_end;
2594Srgrimes{
2604Srgrimes	register struct command *cmd;
26118296Sbde	register struct command **aux_cmdp;
2624Srgrimes
2634Srgrimes	for (cmd = table; cmd->name != 0; cmd++) {
2644Srgrimes	    db_printf("%-12s", cmd->name);
2654Srgrimes	    db_end_line();
2664Srgrimes	}
26718296Sbde	if (aux_tablep == 0)
26818296Sbde	    return;
26978161Speter	for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
27018296Sbde	    db_printf("%-12s", (*aux_cmdp)->name);
27118296Sbde	    db_end_line();
27218296Sbde	}
2734Srgrimes}
2744Srgrimes
27512515Sphkstatic void
27678161Speterdb_command(last_cmdp, cmd_table, aux_cmd_tablep, aux_cmd_tablep_end)
2774Srgrimes	struct command	**last_cmdp;	/* IN_OUT */
2784Srgrimes	struct command	*cmd_table;
27918296Sbde	struct command	**aux_cmd_tablep;
28078161Speter	struct command	**aux_cmd_tablep_end;
2814Srgrimes{
2824Srgrimes	struct command	*cmd;
2834Srgrimes	int		t;
2844Srgrimes	char		modif[TOK_STRING_SIZE];
2854Srgrimes	db_expr_t	addr, count;
286798Swollman	boolean_t	have_addr = FALSE;
2874Srgrimes	int		result;
2884Srgrimes
2894Srgrimes	t = db_read_token();
2904Srgrimes	if (t == tEOL) {
2914Srgrimes	    /* empty line repeats last command, at 'next' */
2924Srgrimes	    cmd = *last_cmdp;
2934Srgrimes	    addr = (db_expr_t)db_next;
2944Srgrimes	    have_addr = FALSE;
2954Srgrimes	    count = 1;
2964Srgrimes	    modif[0] = '\0';
2974Srgrimes	}
2984Srgrimes	else if (t == tEXCL) {
29910348Sbde	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
3004Srgrimes	    return;
3014Srgrimes	}
3024Srgrimes	else if (t != tIDENT) {
3034Srgrimes	    db_printf("?\n");
3044Srgrimes	    db_flush_lex();
3054Srgrimes	    return;
3064Srgrimes	}
3074Srgrimes	else {
3084Srgrimes	    /*
3094Srgrimes	     * Search for command
3104Srgrimes	     */
3114Srgrimes	    while (cmd_table) {
3124Srgrimes		result = db_cmd_search(db_tok_string,
3134Srgrimes				       cmd_table,
31418296Sbde				       aux_cmd_tablep,
31578161Speter				       aux_cmd_tablep_end,
3164Srgrimes				       &cmd);
3174Srgrimes		switch (result) {
3184Srgrimes		    case CMD_NONE:
3194Srgrimes			db_printf("No such command\n");
3204Srgrimes			db_flush_lex();
3214Srgrimes			return;
3224Srgrimes		    case CMD_AMBIGUOUS:
3234Srgrimes			db_printf("Ambiguous\n");
3244Srgrimes			db_flush_lex();
3254Srgrimes			return;
3264Srgrimes		    case CMD_HELP:
32778161Speter			db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
3284Srgrimes			db_flush_lex();
3294Srgrimes			return;
3304Srgrimes		    default:
3314Srgrimes			break;
3324Srgrimes		}
3334Srgrimes		if ((cmd_table = cmd->more) != 0) {
33418296Sbde		    /* XXX usually no more aux's. */
33518296Sbde		    aux_cmd_tablep = 0;
336104321Sphk		    if (cmd_table == db_show_cmds) {
33778161Speter			aux_cmd_tablep = SET_BEGIN(db_show_cmd_set);
33878161Speter			aux_cmd_tablep_end = SET_LIMIT(db_show_cmd_set);
339104321Sphk		    }
34018296Sbde
3414Srgrimes		    t = db_read_token();
3424Srgrimes		    if (t != tIDENT) {
34378161Speter			db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
3444Srgrimes			db_flush_lex();
3454Srgrimes			return;
3464Srgrimes		    }
3474Srgrimes		}
3484Srgrimes	    }
3494Srgrimes
3504Srgrimes	    if ((cmd->flag & CS_OWN) == 0) {
3514Srgrimes		/*
3524Srgrimes		 * Standard syntax:
3534Srgrimes		 * command [/modifier] [addr] [,count]
3544Srgrimes		 */
3554Srgrimes		t = db_read_token();
3564Srgrimes		if (t == tSLASH) {
3574Srgrimes		    t = db_read_token();
3584Srgrimes		    if (t != tIDENT) {
3594Srgrimes			db_printf("Bad modifier\n");
3604Srgrimes			db_flush_lex();
3614Srgrimes			return;
3624Srgrimes		    }
3634Srgrimes		    db_strcpy(modif, db_tok_string);
3644Srgrimes		}
3654Srgrimes		else {
3664Srgrimes		    db_unread_token(t);
3674Srgrimes		    modif[0] = '\0';
3684Srgrimes		}
3694Srgrimes
3704Srgrimes		if (db_expression(&addr)) {
3714Srgrimes		    db_dot = (db_addr_t) addr;
3724Srgrimes		    db_last_addr = db_dot;
3734Srgrimes		    have_addr = TRUE;
3744Srgrimes		}
3754Srgrimes		else {
3764Srgrimes		    addr = (db_expr_t) db_dot;
3774Srgrimes		    have_addr = FALSE;
3784Srgrimes		}
3794Srgrimes		t = db_read_token();
3804Srgrimes		if (t == tCOMMA) {
3814Srgrimes		    if (!db_expression(&count)) {
3824Srgrimes			db_printf("Count missing\n");
3834Srgrimes			db_flush_lex();
3844Srgrimes			return;
3854Srgrimes		    }
3864Srgrimes		}
3874Srgrimes		else {
3884Srgrimes		    db_unread_token(t);
3894Srgrimes		    count = -1;
3904Srgrimes		}
3914Srgrimes		if ((cmd->flag & CS_MORE) == 0) {
3924Srgrimes		    db_skip_to_eol();
3934Srgrimes		}
3944Srgrimes	    }
3954Srgrimes	}
3964Srgrimes	*last_cmdp = cmd;
3974Srgrimes	if (cmd != 0) {
3984Srgrimes	    /*
3994Srgrimes	     * Execute the command.
4004Srgrimes	     */
4014Srgrimes	    (*cmd->fcn)(addr, have_addr, count, modif);
402118268Sjhb	    db_setup_paging(NULL, NULL, -1);
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{
4327170Sdg	panic("from debugger");
4336204Sphk}
4346204Sphk
4356204Sphkvoid
4364Srgrimesdb_command_loop()
4374Srgrimes{
4384Srgrimes	/*
4394Srgrimes	 * Initialize 'prev' and 'next' to dot.
4404Srgrimes	 */
4414Srgrimes	db_prev = db_dot;
4424Srgrimes	db_next = db_dot;
4434Srgrimes
4444Srgrimes	db_cmd_loop_done = 0;
4454Srgrimes	while (!db_cmd_loop_done) {
4464Srgrimes	    if (db_print_position() != 0)
4474Srgrimes		db_printf("\n");
4484Srgrimes
4494Srgrimes	    db_printf("db> ");
4504Srgrimes	    (void) db_read_line();
4514Srgrimes
45218296Sbde	    db_command(&db_last_command, db_command_table,
45378161Speter		       SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set));
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();
5364Srgrimes
537147745Smarcel	if (DB_CALL(fn_addr, &retval, nargs, args))
538147745Smarcel		db_printf("= %#lr\n", (long)retval);
5394Srgrimes}
54017848Spst
54117848Spststatic void
54286998Sdddb_kill(dummy1, dummy2, dummy3, dummy4)
54386998Sdd	db_expr_t	dummy1;
54486998Sdd	boolean_t	dummy2;
54586998Sdd	db_expr_t	dummy3;
54686998Sdd	char *		dummy4;
54786998Sdd{
54886998Sdd	db_expr_t old_radix, pid, sig;
54986998Sdd	struct proc *p;
55086998Sdd
55186998Sdd#define DB_ERROR(f)	do { db_printf f; db_flush_lex(); goto out; } while (0)
55286998Sdd
55386998Sdd	/*
55486998Sdd	 * PIDs and signal numbers are typically represented in base
55586998Sdd	 * 10, so make that the default here.  It can, of course, be
55686998Sdd	 * overridden by specifying a prefix.
55786998Sdd	 */
55886998Sdd	old_radix = db_radix;
55986998Sdd	db_radix = 10;
56086998Sdd	/* Retrieve arguments. */
56186998Sdd	if (!db_expression(&sig))
56286998Sdd		DB_ERROR(("Missing signal number\n"));
56386998Sdd	if (!db_expression(&pid))
56486998Sdd		DB_ERROR(("Missing process ID\n"));
56586998Sdd	db_skip_to_eol();
56686998Sdd	if (sig < 0 || sig > _SIG_MAXSIG)
56786998Sdd		DB_ERROR(("Signal number out of range\n"));
56886998Sdd
56986998Sdd	/*
57086998Sdd	 * Find the process in question.  allproc_lock is not needed
57186998Sdd	 * since we're in DDB.
57286998Sdd	 */
57386998Sdd	/* sx_slock(&allproc_lock); */
57486998Sdd	LIST_FOREACH(p, &allproc, p_list)
57586998Sdd	    if (p->p_pid == pid)
57686998Sdd		    break;
57786998Sdd	/* sx_sunlock(&allproc_lock); */
57886998Sdd	if (p == NULL)
57989442Smjacob		DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
58086998Sdd
58186998Sdd	/* If it's already locked, bail; otherwise, do the deed. */
58286998Sdd	if (PROC_TRYLOCK(p) == 0)
58389442Smjacob		DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
58486998Sdd	else {
58586998Sdd		psignal(p, sig);
58686998Sdd		PROC_UNLOCK(p);
58786998Sdd	}
58886998Sdd
58986998Sddout:
59086998Sdd	db_radix = old_radix;
59186998Sdd#undef DB_ERROR
59286998Sdd}
59386998Sdd
59486998Sddstatic void
59585944Speterdb_reset(dummy1, dummy2, dummy3, dummy4)
59685944Speter	db_expr_t	dummy1;
59785944Speter	boolean_t	dummy2;
59885944Speter	db_expr_t	dummy3;
59985944Speter	char *		dummy4;
60085944Speter{
60185944Speter
60285944Speter	cpu_reset();
60385944Speter}
604126399Sphk
605126399Sphkstatic void
606126399Sphkdb_watchdog(dummy1, dummy2, dummy3, dummy4)
607126399Sphk	db_expr_t	dummy1;
608126399Sphk	boolean_t	dummy2;
609126399Sphk	db_expr_t	dummy3;
610126399Sphk	char *		dummy4;
611126399Sphk{
612126399Sphk	int i;
613126399Sphk
614126399Sphk	/*
615126399Sphk	 * XXX: It might make sense to be able to set the watchdog to a
616126399Sphk	 * XXX: timeout here so that failure or hang as a result of subsequent
617126399Sphk	 * XXX: ddb commands could be recovered by a reset.
618126399Sphk	 */
619126399Sphk
620126399Sphk	EVENTHANDLER_INVOKE(watchdog_list, 0, &i);
621126399Sphk}
622132002Smarcel
623132002Smarcelstatic void
624132002Smarceldb_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
625132002Smarcel{
626132002Smarcel
627132002Smarcel	if (kdb_dbbe_select("gdb") != 0)
628132002Smarcel		db_printf("The remote GDB backend could not be selected.\n");
629132002Smarcel	else
630132002Smarcel		db_printf("Step to enter the remote GDB backend.\n");
631132002Smarcel}
632132482Smarcel
633132482Smarcelstatic void
634132482Smarceldb_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
635132482Smarcel{
636132482Smarcel	struct thread *td;
637132482Smarcel	db_expr_t radix;
638138038Srwatson	pid_t pid;
639132482Smarcel	int t;
640132482Smarcel
641132482Smarcel	/*
642132482Smarcel	 * We parse our own arguments. We don't like the default radix.
643132482Smarcel	 */
644132482Smarcel	radix = db_radix;
645132482Smarcel	db_radix = 10;
646132482Smarcel	hastid = db_expression(&tid);
647132482Smarcel	t = db_read_token();
648132482Smarcel	if (t == tCOMMA) {
649132482Smarcel		if (!db_expression(&count)) {
650132482Smarcel			db_printf("Count missing\n");
651132482Smarcel			db_flush_lex();
652132482Smarcel			return;
653132482Smarcel		}
654132482Smarcel	} else {
655132482Smarcel		db_unread_token(t);
656132482Smarcel		count = -1;
657132482Smarcel	}
658132482Smarcel	db_skip_to_eol();
659132482Smarcel	db_radix = radix;
660132482Smarcel
661132482Smarcel	if (hastid) {
662132482Smarcel		td = kdb_thr_lookup((lwpid_t)tid);
663132482Smarcel		if (td == NULL)
664132482Smarcel			td = kdb_thr_from_pid((pid_t)tid);
665132482Smarcel		if (td == NULL) {
666132482Smarcel			db_printf("Thread %d not found\n", (int)tid);
667132482Smarcel			return;
668132482Smarcel		}
669132482Smarcel	} else
670132482Smarcel		td = kdb_thread;
671138038Srwatson	if (td->td_proc != NULL)
672138038Srwatson		pid = td->td_proc->p_pid;
673138038Srwatson	else
674138038Srwatson		pid = -1;
675138038Srwatson	db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
676132482Smarcel	db_trace_thread(td, count);
677132482Smarcel}
678