xref: /freebsd/sys/ddb/db_command.c (revision 74cc032b414cfc02eae1cc5837cbc52082bd1495)
15b81b6b3SRodney W. Grimes /*
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>
4019d2c78fSDima Dorfman #include <sys/mutex.h>
4119d2c78fSDima Dorfman #include <sys/proc.h>
42ad146781SPaul Traina #include <sys/reboot.h>
4319d2c78fSDima Dorfman #include <sys/signalvar.h>
44f540b106SGarrett Wollman #include <sys/systm.h>
45ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
4674cc032bSPoul-Henning Kamp #include <sys/watchdog.h>
473000820aSPoul-Henning Kamp 
485ccbc3ccSBruce Evans #include <ddb/ddb.h>
49058284fcSBruce Evans #include <ddb/db_command.h>
505b81b6b3SRodney W. Grimes #include <ddb/db_lex.h>
515b81b6b3SRodney W. Grimes #include <ddb/db_output.h>
525b81b6b3SRodney W. Grimes 
5326502503SMarcel Moolenaar #include <machine/cpu.h>
540b1ae809SJulian Elischer #include <machine/setjmp.h>
555b81b6b3SRodney W. Grimes 
565b81b6b3SRodney W. Grimes /*
575b81b6b3SRodney W. Grimes  * Exported global variables
585b81b6b3SRodney W. Grimes  */
595b81b6b3SRodney W. Grimes boolean_t	db_cmd_loop_done;
605b81b6b3SRodney W. Grimes db_addr_t	db_dot;
616337f4efSBruce Evans jmp_buf		db_jmpbuf;
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;
70ad146781SPaul Traina static db_cmdfcn_t	db_gdb;
7119d2c78fSDima Dorfman static db_cmdfcn_t	db_kill;
725370c3b6SPeter Wemm static db_cmdfcn_t	db_reset;
7374cc032bSPoul-Henning Kamp static db_cmdfcn_t	db_watchdog;
74ad146781SPaul Traina 
756337f4efSBruce Evans /* XXX this is actually forward-static. */
766337f4efSBruce Evans extern struct command	db_show_cmds[];
776337f4efSBruce Evans 
785b81b6b3SRodney W. Grimes /*
795b81b6b3SRodney W. Grimes  * if 'ed' style: 'dot' is set at start of last item printed,
805b81b6b3SRodney W. Grimes  * and '+' points to next line.
815b81b6b3SRodney W. Grimes  * Otherwise: 'dot' points to next item, '..' points to last.
825b81b6b3SRodney W. Grimes  */
83f73a856dSPoul-Henning Kamp static boolean_t	db_ed_style = TRUE;
845b81b6b3SRodney W. Grimes 
855b81b6b3SRodney W. Grimes /*
865b81b6b3SRodney W. Grimes  * Utility routine - discard tokens through end-of-line.
875b81b6b3SRodney W. Grimes  */
885b81b6b3SRodney W. Grimes void
895b81b6b3SRodney W. Grimes db_skip_to_eol()
905b81b6b3SRodney W. Grimes {
915b81b6b3SRodney W. Grimes 	int	t;
925b81b6b3SRodney W. Grimes 	do {
935b81b6b3SRodney W. Grimes 	    t = db_read_token();
945b81b6b3SRodney W. Grimes 	} while (t != tEOL);
955b81b6b3SRodney W. Grimes }
965b81b6b3SRodney W. Grimes 
975b81b6b3SRodney W. Grimes /*
985b81b6b3SRodney W. Grimes  * Results of command search.
995b81b6b3SRodney W. Grimes  */
1005b81b6b3SRodney W. Grimes #define	CMD_UNIQUE	0
1015b81b6b3SRodney W. Grimes #define	CMD_FOUND	1
1025b81b6b3SRodney W. Grimes #define	CMD_NONE	2
1035b81b6b3SRodney W. Grimes #define	CMD_AMBIGUOUS	3
1045b81b6b3SRodney W. Grimes #define	CMD_HELP	4
1055b81b6b3SRodney W. Grimes 
106763df836SBruce Evans static void	db_cmd_list(struct command *table, struct command **aux_tablep,
10714e10f99SAlfred Perlstein 		    struct command **aux_tablep_end);
10814e10f99SAlfred Perlstein static int	db_cmd_search(char *name, struct command *table,
1096337f4efSBruce Evans 		    struct command **aux_tablep,
110763df836SBruce Evans 		    struct command **aux_tablep_end, struct command **cmdp);
11114e10f99SAlfred Perlstein static void	db_command(struct command **last_cmdp,
112763df836SBruce Evans 		    struct command *cmd_table, struct command **aux_cmd_tablep,
11314e10f99SAlfred Perlstein 		    struct command **aux_cmd_tablep_end);
114058284fcSBruce Evans 
1155b81b6b3SRodney W. Grimes /*
1165b81b6b3SRodney W. Grimes  * Search for command prefix.
1175b81b6b3SRodney W. Grimes  */
118f73a856dSPoul-Henning Kamp static int
119f41325dbSPeter Wemm db_cmd_search(name, table, aux_tablep, aux_tablep_end, cmdp)
1205b81b6b3SRodney W. Grimes 	char *		name;
1215b81b6b3SRodney W. Grimes 	struct command	*table;
1226337f4efSBruce Evans 	struct command	**aux_tablep;
123f41325dbSPeter Wemm 	struct command	**aux_tablep_end;
1245b81b6b3SRodney W. Grimes 	struct command	**cmdp;	/* out */
1255b81b6b3SRodney W. Grimes {
1265b81b6b3SRodney W. Grimes 	struct command	*cmd;
1276337f4efSBruce Evans 	struct command	**aux_cmdp;
1285b81b6b3SRodney W. Grimes 	int		result = CMD_NONE;
1295b81b6b3SRodney W. Grimes 
1305b81b6b3SRodney W. Grimes 	for (cmd = table; cmd->name != 0; cmd++) {
1315b81b6b3SRodney W. Grimes 	    register char *lp;
1325b81b6b3SRodney W. Grimes 	    register char *rp;
1335b81b6b3SRodney W. Grimes 	    register int  c;
1345b81b6b3SRodney W. Grimes 
1355b81b6b3SRodney W. Grimes 	    lp = name;
1365b81b6b3SRodney W. Grimes 	    rp = cmd->name;
1375b81b6b3SRodney W. Grimes 	    while ((c = *lp) == *rp) {
1385b81b6b3SRodney W. Grimes 		if (c == 0) {
1395b81b6b3SRodney W. Grimes 		    /* complete match */
1405b81b6b3SRodney W. Grimes 		    *cmdp = cmd;
1415b81b6b3SRodney W. Grimes 		    return (CMD_UNIQUE);
1425b81b6b3SRodney W. Grimes 		}
1435b81b6b3SRodney W. Grimes 		lp++;
1445b81b6b3SRodney W. Grimes 		rp++;
1455b81b6b3SRodney W. Grimes 	    }
1465b81b6b3SRodney W. Grimes 	    if (c == 0) {
1475b81b6b3SRodney W. Grimes 		/* end of name, not end of command -
1485b81b6b3SRodney W. Grimes 		   partial match */
1495b81b6b3SRodney W. Grimes 		if (result == CMD_FOUND) {
1505b81b6b3SRodney W. Grimes 		    result = CMD_AMBIGUOUS;
1515b81b6b3SRodney W. Grimes 		    /* but keep looking for a full match -
1525b81b6b3SRodney W. Grimes 		       this lets us match single letters */
1535b81b6b3SRodney W. Grimes 		}
1545b81b6b3SRodney W. Grimes 		else {
1555b81b6b3SRodney W. Grimes 		    *cmdp = cmd;
1565b81b6b3SRodney W. Grimes 		    result = CMD_FOUND;
1575b81b6b3SRodney W. Grimes 		}
1585b81b6b3SRodney W. Grimes 	    }
1595b81b6b3SRodney W. Grimes 	}
1606337f4efSBruce Evans 	if (result == CMD_NONE && aux_tablep != 0)
1616337f4efSBruce Evans 	    /* XXX repeat too much code. */
162f41325dbSPeter Wemm 	    for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
1636337f4efSBruce Evans 		register char *lp;
1646337f4efSBruce Evans 		register char *rp;
1656337f4efSBruce Evans 		register int  c;
1666337f4efSBruce Evans 
1676337f4efSBruce Evans 		lp = name;
1686337f4efSBruce Evans 		rp = (*aux_cmdp)->name;
1696337f4efSBruce Evans 		while ((c = *lp) == *rp) {
1706337f4efSBruce Evans 		    if (c == 0) {
1716337f4efSBruce Evans 			/* complete match */
1726337f4efSBruce Evans 			*cmdp = *aux_cmdp;
1736337f4efSBruce Evans 			return (CMD_UNIQUE);
1746337f4efSBruce Evans 		    }
1756337f4efSBruce Evans 		    lp++;
1766337f4efSBruce Evans 		    rp++;
1776337f4efSBruce Evans 		}
1786337f4efSBruce Evans 		if (c == 0) {
1796337f4efSBruce Evans 		    /* end of name, not end of command -
1806337f4efSBruce Evans 		       partial match */
1816337f4efSBruce Evans 		    if (result == CMD_FOUND) {
1826337f4efSBruce Evans 			result = CMD_AMBIGUOUS;
1836337f4efSBruce Evans 			/* but keep looking for a full match -
1846337f4efSBruce Evans 			   this lets us match single letters */
1856337f4efSBruce Evans 		    }
1866337f4efSBruce Evans 		    else {
1876337f4efSBruce Evans 			*cmdp = *aux_cmdp;
1886337f4efSBruce Evans 			result = CMD_FOUND;
1896337f4efSBruce Evans 		    }
1906337f4efSBruce Evans 		}
1916337f4efSBruce Evans 	    }
1925b81b6b3SRodney W. Grimes 	if (result == CMD_NONE) {
1935b81b6b3SRodney W. Grimes 	    /* check for 'help' */
1945b81b6b3SRodney W. Grimes 		if (name[0] == 'h' && name[1] == 'e'
1955b81b6b3SRodney W. Grimes 		    && name[2] == 'l' && name[3] == 'p')
1965b81b6b3SRodney W. Grimes 			result = CMD_HELP;
1975b81b6b3SRodney W. Grimes 	}
1985b81b6b3SRodney W. Grimes 	return (result);
1995b81b6b3SRodney W. Grimes }
2005b81b6b3SRodney W. Grimes 
201f73a856dSPoul-Henning Kamp static void
202f41325dbSPeter Wemm db_cmd_list(table, aux_tablep, aux_tablep_end)
2035b81b6b3SRodney W. Grimes 	struct command *table;
2046337f4efSBruce Evans 	struct command **aux_tablep;
205f41325dbSPeter Wemm 	struct command **aux_tablep_end;
2065b81b6b3SRodney W. Grimes {
2075b81b6b3SRodney W. Grimes 	register struct command *cmd;
2086337f4efSBruce Evans 	register struct command **aux_cmdp;
2095b81b6b3SRodney W. Grimes 
2105b81b6b3SRodney W. Grimes 	for (cmd = table; cmd->name != 0; cmd++) {
2115b81b6b3SRodney W. Grimes 	    db_printf("%-12s", cmd->name);
2125b81b6b3SRodney W. Grimes 	    db_end_line();
2135b81b6b3SRodney W. Grimes 	}
2146337f4efSBruce Evans 	if (aux_tablep == 0)
2156337f4efSBruce Evans 	    return;
216f41325dbSPeter Wemm 	for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
2176337f4efSBruce Evans 	    db_printf("%-12s", (*aux_cmdp)->name);
2186337f4efSBruce Evans 	    db_end_line();
2196337f4efSBruce Evans 	}
2205b81b6b3SRodney W. Grimes }
2215b81b6b3SRodney W. Grimes 
222f73a856dSPoul-Henning Kamp static void
223f41325dbSPeter Wemm db_command(last_cmdp, cmd_table, aux_cmd_tablep, aux_cmd_tablep_end)
2245b81b6b3SRodney W. Grimes 	struct command	**last_cmdp;	/* IN_OUT */
2255b81b6b3SRodney W. Grimes 	struct command	*cmd_table;
2266337f4efSBruce Evans 	struct command	**aux_cmd_tablep;
227f41325dbSPeter Wemm 	struct command	**aux_cmd_tablep_end;
2285b81b6b3SRodney W. Grimes {
2295b81b6b3SRodney W. Grimes 	struct command	*cmd;
2305b81b6b3SRodney W. Grimes 	int		t;
2315b81b6b3SRodney W. Grimes 	char		modif[TOK_STRING_SIZE];
2325b81b6b3SRodney W. Grimes 	db_expr_t	addr, count;
233381fe1aaSGarrett Wollman 	boolean_t	have_addr = FALSE;
2345b81b6b3SRodney W. Grimes 	int		result;
2355b81b6b3SRodney W. Grimes 
2365b81b6b3SRodney W. Grimes 	t = db_read_token();
2375b81b6b3SRodney W. Grimes 	if (t == tEOL) {
2385b81b6b3SRodney W. Grimes 	    /* empty line repeats last command, at 'next' */
2395b81b6b3SRodney W. Grimes 	    cmd = *last_cmdp;
2405b81b6b3SRodney W. Grimes 	    addr = (db_expr_t)db_next;
2415b81b6b3SRodney W. Grimes 	    have_addr = FALSE;
2425b81b6b3SRodney W. Grimes 	    count = 1;
2435b81b6b3SRodney W. Grimes 	    modif[0] = '\0';
2445b81b6b3SRodney W. Grimes 	}
2455b81b6b3SRodney W. Grimes 	else if (t == tEXCL) {
246976794d9SBruce Evans 	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
2475b81b6b3SRodney W. Grimes 	    return;
2485b81b6b3SRodney W. Grimes 	}
2495b81b6b3SRodney W. Grimes 	else if (t != tIDENT) {
2505b81b6b3SRodney W. Grimes 	    db_printf("?\n");
2515b81b6b3SRodney W. Grimes 	    db_flush_lex();
2525b81b6b3SRodney W. Grimes 	    return;
2535b81b6b3SRodney W. Grimes 	}
2545b81b6b3SRodney W. Grimes 	else {
2555b81b6b3SRodney W. Grimes 	    /*
2565b81b6b3SRodney W. Grimes 	     * Search for command
2575b81b6b3SRodney W. Grimes 	     */
2585b81b6b3SRodney W. Grimes 	    while (cmd_table) {
2595b81b6b3SRodney W. Grimes 		result = db_cmd_search(db_tok_string,
2605b81b6b3SRodney W. Grimes 				       cmd_table,
2616337f4efSBruce Evans 				       aux_cmd_tablep,
262f41325dbSPeter Wemm 				       aux_cmd_tablep_end,
2635b81b6b3SRodney W. Grimes 				       &cmd);
2645b81b6b3SRodney W. Grimes 		switch (result) {
2655b81b6b3SRodney W. Grimes 		    case CMD_NONE:
2665b81b6b3SRodney W. Grimes 			db_printf("No such command\n");
2675b81b6b3SRodney W. Grimes 			db_flush_lex();
2685b81b6b3SRodney W. Grimes 			return;
2695b81b6b3SRodney W. Grimes 		    case CMD_AMBIGUOUS:
2705b81b6b3SRodney W. Grimes 			db_printf("Ambiguous\n");
2715b81b6b3SRodney W. Grimes 			db_flush_lex();
2725b81b6b3SRodney W. Grimes 			return;
2735b81b6b3SRodney W. Grimes 		    case CMD_HELP:
274f41325dbSPeter Wemm 			db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
2755b81b6b3SRodney W. Grimes 			db_flush_lex();
2765b81b6b3SRodney W. Grimes 			return;
2775b81b6b3SRodney W. Grimes 		    default:
2785b81b6b3SRodney W. Grimes 			break;
2795b81b6b3SRodney W. Grimes 		}
2805b81b6b3SRodney W. Grimes 		if ((cmd_table = cmd->more) != 0) {
2816337f4efSBruce Evans 		    /* XXX usually no more aux's. */
2826337f4efSBruce Evans 		    aux_cmd_tablep = 0;
283c2476fafSPoul-Henning Kamp 		    if (cmd_table == db_show_cmds) {
284f41325dbSPeter Wemm 			aux_cmd_tablep = SET_BEGIN(db_show_cmd_set);
285f41325dbSPeter Wemm 			aux_cmd_tablep_end = SET_LIMIT(db_show_cmd_set);
286c2476fafSPoul-Henning Kamp 		    }
2876337f4efSBruce Evans 
2885b81b6b3SRodney W. Grimes 		    t = db_read_token();
2895b81b6b3SRodney W. Grimes 		    if (t != tIDENT) {
290f41325dbSPeter Wemm 			db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
2915b81b6b3SRodney W. Grimes 			db_flush_lex();
2925b81b6b3SRodney W. Grimes 			return;
2935b81b6b3SRodney W. Grimes 		    }
2945b81b6b3SRodney W. Grimes 		}
2955b81b6b3SRodney W. Grimes 	    }
2965b81b6b3SRodney W. Grimes 
2975b81b6b3SRodney W. Grimes 	    if ((cmd->flag & CS_OWN) == 0) {
2985b81b6b3SRodney W. Grimes 		/*
2995b81b6b3SRodney W. Grimes 		 * Standard syntax:
3005b81b6b3SRodney W. Grimes 		 * command [/modifier] [addr] [,count]
3015b81b6b3SRodney W. Grimes 		 */
3025b81b6b3SRodney W. Grimes 		t = db_read_token();
3035b81b6b3SRodney W. Grimes 		if (t == tSLASH) {
3045b81b6b3SRodney W. Grimes 		    t = db_read_token();
3055b81b6b3SRodney W. Grimes 		    if (t != tIDENT) {
3065b81b6b3SRodney W. Grimes 			db_printf("Bad modifier\n");
3075b81b6b3SRodney W. Grimes 			db_flush_lex();
3085b81b6b3SRodney W. Grimes 			return;
3095b81b6b3SRodney W. Grimes 		    }
3105b81b6b3SRodney W. Grimes 		    db_strcpy(modif, db_tok_string);
3115b81b6b3SRodney W. Grimes 		}
3125b81b6b3SRodney W. Grimes 		else {
3135b81b6b3SRodney W. Grimes 		    db_unread_token(t);
3145b81b6b3SRodney W. Grimes 		    modif[0] = '\0';
3155b81b6b3SRodney W. Grimes 		}
3165b81b6b3SRodney W. Grimes 
3175b81b6b3SRodney W. Grimes 		if (db_expression(&addr)) {
3185b81b6b3SRodney W. Grimes 		    db_dot = (db_addr_t) addr;
3195b81b6b3SRodney W. Grimes 		    db_last_addr = db_dot;
3205b81b6b3SRodney W. Grimes 		    have_addr = TRUE;
3215b81b6b3SRodney W. Grimes 		}
3225b81b6b3SRodney W. Grimes 		else {
3235b81b6b3SRodney W. Grimes 		    addr = (db_expr_t) db_dot;
3245b81b6b3SRodney W. Grimes 		    have_addr = FALSE;
3255b81b6b3SRodney W. Grimes 		}
3265b81b6b3SRodney W. Grimes 		t = db_read_token();
3275b81b6b3SRodney W. Grimes 		if (t == tCOMMA) {
3285b81b6b3SRodney W. Grimes 		    if (!db_expression(&count)) {
3295b81b6b3SRodney W. Grimes 			db_printf("Count missing\n");
3305b81b6b3SRodney W. Grimes 			db_flush_lex();
3315b81b6b3SRodney W. Grimes 			return;
3325b81b6b3SRodney W. Grimes 		    }
3335b81b6b3SRodney W. Grimes 		}
3345b81b6b3SRodney W. Grimes 		else {
3355b81b6b3SRodney W. Grimes 		    db_unread_token(t);
3365b81b6b3SRodney W. Grimes 		    count = -1;
3375b81b6b3SRodney W. Grimes 		}
3385b81b6b3SRodney W. Grimes 		if ((cmd->flag & CS_MORE) == 0) {
3395b81b6b3SRodney W. Grimes 		    db_skip_to_eol();
3405b81b6b3SRodney W. Grimes 		}
3415b81b6b3SRodney W. Grimes 	    }
3425b81b6b3SRodney W. Grimes 	}
3435b81b6b3SRodney W. Grimes 	*last_cmdp = cmd;
3445b81b6b3SRodney W. Grimes 	if (cmd != 0) {
3455b81b6b3SRodney W. Grimes 	    /*
3465b81b6b3SRodney W. Grimes 	     * Execute the command.
3475b81b6b3SRodney W. Grimes 	     */
3485b81b6b3SRodney W. Grimes 	    (*cmd->fcn)(addr, have_addr, count, modif);
3491e16f609SJohn Baldwin 	    db_setup_paging(NULL, NULL, -1);
3505b81b6b3SRodney W. Grimes 
3515b81b6b3SRodney W. Grimes 	    if (cmd->flag & CS_SET_DOT) {
3525b81b6b3SRodney W. Grimes 		/*
3535b81b6b3SRodney W. Grimes 		 * If command changes dot, set dot to
3545b81b6b3SRodney W. Grimes 		 * previous address displayed (if 'ed' style).
3555b81b6b3SRodney W. Grimes 		 */
3565b81b6b3SRodney W. Grimes 		if (db_ed_style) {
3575b81b6b3SRodney W. Grimes 		    db_dot = db_prev;
3585b81b6b3SRodney W. Grimes 		}
3595b81b6b3SRodney W. Grimes 		else {
3605b81b6b3SRodney W. Grimes 		    db_dot = db_next;
3615b81b6b3SRodney W. Grimes 		}
3625b81b6b3SRodney W. Grimes 	    }
3635b81b6b3SRodney W. Grimes 	    else {
3645b81b6b3SRodney W. Grimes 		/*
3655b81b6b3SRodney W. Grimes 		 * If command does not change dot,
3665b81b6b3SRodney W. Grimes 		 * set 'next' location to be the same.
3675b81b6b3SRodney W. Grimes 		 */
3685b81b6b3SRodney W. Grimes 		db_next = db_dot;
3695b81b6b3SRodney W. Grimes 	    }
3705b81b6b3SRodney W. Grimes 	}
3715b81b6b3SRodney W. Grimes }
3725b81b6b3SRodney W. Grimes 
3735b81b6b3SRodney W. Grimes /*
3745b81b6b3SRodney W. Grimes  * 'show' commands
3755b81b6b3SRodney W. Grimes  */
376f23b4c91SGarrett Wollman 
377f73a856dSPoul-Henning Kamp static struct command db_show_all_cmds[] = {
3785b81b6b3SRodney W. Grimes #if 0
3795b81b6b3SRodney W. Grimes 	{ "threads",	db_show_all_threads,	0,	0 },
38026f9a767SRodney W. Grimes #endif
3818a129caeSDavid Greenman 	{ "procs",	db_ps,			0,	0 },
3825b81b6b3SRodney W. Grimes 	{ (char *)0 }
3835b81b6b3SRodney W. Grimes };
3845b81b6b3SRodney W. Grimes 
385f73a856dSPoul-Henning Kamp static struct command db_show_cmds[] = {
3865b81b6b3SRodney W. Grimes 	{ "all",	0,			0,	db_show_all_cmds },
3875b81b6b3SRodney W. Grimes 	{ "registers",	db_show_regs,		0,	0 },
3885b81b6b3SRodney W. Grimes 	{ "breaks",	db_listbreak_cmd, 	0,	0 },
3895b81b6b3SRodney W. Grimes 	{ "thread",	db_show_one_thread,	0,	0 },
3905b81b6b3SRodney W. Grimes #if 0
3915b81b6b3SRodney W. Grimes 	{ "port",	ipc_port_print,		0,	0 },
3925b81b6b3SRodney W. Grimes #endif
3935b81b6b3SRodney W. Grimes 	{ (char *)0, }
3945b81b6b3SRodney W. Grimes };
3955b81b6b3SRodney W. Grimes 
396f73a856dSPoul-Henning Kamp static struct command db_command_table[] = {
3975b81b6b3SRodney W. Grimes 	{ "print",	db_print_cmd,		0,	0 },
39823898040SJoerg Wunsch 	{ "p",		db_print_cmd,		0,	0 },
3995b81b6b3SRodney W. Grimes 	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
4005b81b6b3SRodney W. Grimes 	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
4015b81b6b3SRodney W. Grimes 	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
4025b81b6b3SRodney W. Grimes 	{ "set",	db_set_cmd,		CS_OWN,	0 },
4035b81b6b3SRodney W. Grimes 	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
4045b81b6b3SRodney W. Grimes 	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
4055b81b6b3SRodney W. Grimes 	{ "delete",	db_delete_cmd,		0,	0 },
4065b81b6b3SRodney W. Grimes 	{ "d",		db_delete_cmd,		0,	0 },
4075b81b6b3SRodney W. Grimes 	{ "break",	db_breakpoint_cmd,	0,	0 },
4085b81b6b3SRodney W. Grimes 	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
4095b81b6b3SRodney W. Grimes 	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
41017bbfb58SBrian S. Dean 	{ "dhwatch",	db_deletehwatch_cmd,	0,      0 },
41117bbfb58SBrian S. Dean 	{ "hwatch",	db_hwatchpoint_cmd,	0,      0 },
4125b81b6b3SRodney W. Grimes 	{ "step",	db_single_step_cmd,	0,	0 },
4135b81b6b3SRodney W. Grimes 	{ "s",		db_single_step_cmd,	0,	0 },
4145b81b6b3SRodney W. Grimes 	{ "continue",	db_continue_cmd,	0,	0 },
4155b81b6b3SRodney W. Grimes 	{ "c",		db_continue_cmd,	0,	0 },
4165b81b6b3SRodney W. Grimes 	{ "until",	db_trace_until_call_cmd,0,	0 },
4175b81b6b3SRodney W. Grimes 	{ "next",	db_trace_until_matching_cmd,0,	0 },
4185b81b6b3SRodney W. Grimes 	{ "match",	db_trace_until_matching_cmd,0,	0 },
4195b81b6b3SRodney W. Grimes 	{ "trace",	db_stack_trace_cmd,	0,	0 },
42004361c8cSPoul-Henning Kamp 	{ "where",	db_stack_trace_cmd,	0,	0 },
4215b81b6b3SRodney W. Grimes 	{ "call",	db_fncall,		CS_OWN,	0 },
4225b81b6b3SRodney W. Grimes 	{ "show",	0,			0,	db_show_cmds },
42351f4a07bSGuido van Rooij 	{ "ps",		db_ps,			0,	0 },
424ad146781SPaul Traina 	{ "gdb",	db_gdb,			0,	0 },
4255370c3b6SPeter Wemm 	{ "reset",	db_reset,		0,	0 },
42619d2c78fSDima Dorfman 	{ "kill",	db_kill,		CS_OWN,	0 },
42774cc032bSPoul-Henning Kamp 	{ "watchdog",	db_watchdog,		0,	0 },
4285b81b6b3SRodney W. Grimes 	{ (char *)0, }
4295b81b6b3SRodney W. Grimes };
4305b81b6b3SRodney W. Grimes 
431f73a856dSPoul-Henning Kamp static struct command	*db_last_command = 0;
4325b81b6b3SRodney W. Grimes 
433b5e8ce9fSBruce Evans #if 0
4345b81b6b3SRodney W. Grimes void
4355b81b6b3SRodney W. Grimes db_help_cmd()
4365b81b6b3SRodney W. Grimes {
4375b81b6b3SRodney W. Grimes 	struct command *cmd = db_command_table;
4385b81b6b3SRodney W. Grimes 
4395b81b6b3SRodney W. Grimes 	while (cmd->name != 0) {
4405b81b6b3SRodney W. Grimes 	    db_printf("%-12s", cmd->name);
4415b81b6b3SRodney W. Grimes 	    db_end_line();
4425b81b6b3SRodney W. Grimes 	    cmd++;
4435b81b6b3SRodney W. Grimes 	}
4445b81b6b3SRodney W. Grimes }
445b5e8ce9fSBruce Evans #endif
4465b81b6b3SRodney W. Grimes 
447b7aa38c1SBruce Evans /*
448b7aa38c1SBruce Evans  * At least one non-optional command must be implemented using
449b7aa38c1SBruce Evans  * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
450b7aa38c1SBruce Evans  */
451b7aa38c1SBruce Evans DB_COMMAND(panic, db_panic)
4523eac4884SPoul-Henning Kamp {
453edf8a815SDavid Greenman 	panic("from debugger");
4543eac4884SPoul-Henning Kamp }
4553eac4884SPoul-Henning Kamp 
4563eac4884SPoul-Henning Kamp void
4575b81b6b3SRodney W. Grimes db_command_loop()
4585b81b6b3SRodney W. Grimes {
4595b81b6b3SRodney W. Grimes 	/*
4605b81b6b3SRodney W. Grimes 	 * Initialize 'prev' and 'next' to dot.
4615b81b6b3SRodney W. Grimes 	 */
4625b81b6b3SRodney W. Grimes 	db_prev = db_dot;
4635b81b6b3SRodney W. Grimes 	db_next = db_dot;
4645b81b6b3SRodney W. Grimes 
4655b81b6b3SRodney W. Grimes 	db_cmd_loop_done = 0;
4665b81b6b3SRodney W. Grimes 	while (!db_cmd_loop_done) {
4675b81b6b3SRodney W. Grimes 
4685b81b6b3SRodney W. Grimes 	    (void) setjmp(db_jmpbuf);
4695b81b6b3SRodney W. Grimes 	    if (db_print_position() != 0)
4705b81b6b3SRodney W. Grimes 		db_printf("\n");
4715b81b6b3SRodney W. Grimes 
4725b81b6b3SRodney W. Grimes 	    db_printf("db> ");
4735b81b6b3SRodney W. Grimes 	    (void) db_read_line();
4745b81b6b3SRodney W. Grimes 
4756337f4efSBruce Evans 	    db_command(&db_last_command, db_command_table,
476f41325dbSPeter Wemm 		       SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set));
4775b81b6b3SRodney W. Grimes 	}
4785b81b6b3SRodney W. Grimes }
4795b81b6b3SRodney W. Grimes 
4805b81b6b3SRodney W. Grimes void
4815b81b6b3SRodney W. Grimes db_error(s)
482bda9921dSMark Murray 	const char *s;
4835b81b6b3SRodney W. Grimes {
4845b81b6b3SRodney W. Grimes 	if (s)
48558d9a059SKris Kennaway 	    db_printf("%s", s);
4865b81b6b3SRodney W. Grimes 	db_flush_lex();
4875b81b6b3SRodney W. Grimes 	longjmp(db_jmpbuf, 1);
4885b81b6b3SRodney W. Grimes }
4895b81b6b3SRodney W. Grimes 
4905b81b6b3SRodney W. Grimes 
4915b81b6b3SRodney W. Grimes /*
4925b81b6b3SRodney W. Grimes  * Call random function:
4935b81b6b3SRodney W. Grimes  * !expr(arg,arg,arg)
4945b81b6b3SRodney W. Grimes  */
495f73a856dSPoul-Henning Kamp static void
496976794d9SBruce Evans db_fncall(dummy1, dummy2, dummy3, dummy4)
497976794d9SBruce Evans 	db_expr_t	dummy1;
498976794d9SBruce Evans 	boolean_t	dummy2;
499976794d9SBruce Evans 	db_expr_t	dummy3;
500976794d9SBruce Evans 	char *		dummy4;
5015b81b6b3SRodney W. Grimes {
5025b81b6b3SRodney W. Grimes 	db_expr_t	fn_addr;
503058284fcSBruce Evans #define	MAXARGS		11	/* XXX only 10 are passed */
5045b81b6b3SRodney W. Grimes 	db_expr_t	args[MAXARGS];
5055b81b6b3SRodney W. Grimes 	int		nargs = 0;
5065b81b6b3SRodney W. Grimes 	db_expr_t	retval;
50714e10f99SAlfred Perlstein 	typedef db_expr_t fcn_10args_t(db_expr_t, db_expr_t, db_expr_t,
508763df836SBruce Evans 			    db_expr_t, db_expr_t, db_expr_t, db_expr_t,
509763df836SBruce Evans 			    db_expr_t, db_expr_t, db_expr_t);
510058284fcSBruce Evans 	fcn_10args_t	*func;
5115b81b6b3SRodney W. Grimes 	int		t;
5125b81b6b3SRodney W. Grimes 
5135b81b6b3SRodney W. Grimes 	if (!db_expression(&fn_addr)) {
5145b81b6b3SRodney W. Grimes 	    db_printf("Bad function\n");
5155b81b6b3SRodney W. Grimes 	    db_flush_lex();
5165b81b6b3SRodney W. Grimes 	    return;
5175b81b6b3SRodney W. Grimes 	}
518058284fcSBruce Evans 	func = (fcn_10args_t *)fn_addr;	/* XXX */
5195b81b6b3SRodney W. Grimes 
5205b81b6b3SRodney W. Grimes 	t = db_read_token();
5215b81b6b3SRodney W. Grimes 	if (t == tLPAREN) {
5225b81b6b3SRodney W. Grimes 	    if (db_expression(&args[0])) {
5235b81b6b3SRodney W. Grimes 		nargs++;
5245b81b6b3SRodney W. Grimes 		while ((t = db_read_token()) == tCOMMA) {
5255b81b6b3SRodney W. Grimes 		    if (nargs == MAXARGS) {
5265b81b6b3SRodney W. Grimes 			db_printf("Too many arguments\n");
5275b81b6b3SRodney W. Grimes 			db_flush_lex();
5285b81b6b3SRodney W. Grimes 			return;
5295b81b6b3SRodney W. Grimes 		    }
5305b81b6b3SRodney W. Grimes 		    if (!db_expression(&args[nargs])) {
5315b81b6b3SRodney W. Grimes 			db_printf("Argument missing\n");
5325b81b6b3SRodney W. Grimes 			db_flush_lex();
5335b81b6b3SRodney W. Grimes 			return;
5345b81b6b3SRodney W. Grimes 		    }
5355b81b6b3SRodney W. Grimes 		    nargs++;
5365b81b6b3SRodney W. Grimes 		}
5375b81b6b3SRodney W. Grimes 		db_unread_token(t);
5385b81b6b3SRodney W. Grimes 	    }
5395b81b6b3SRodney W. Grimes 	    if (db_read_token() != tRPAREN) {
5405b81b6b3SRodney W. Grimes 		db_printf("?\n");
5415b81b6b3SRodney W. Grimes 		db_flush_lex();
5425b81b6b3SRodney W. Grimes 		return;
5435b81b6b3SRodney W. Grimes 	    }
5445b81b6b3SRodney W. Grimes 	}
5455b81b6b3SRodney W. Grimes 	db_skip_to_eol();
5465b81b6b3SRodney W. Grimes 
5475b81b6b3SRodney W. Grimes 	while (nargs < MAXARGS) {
5485b81b6b3SRodney W. Grimes 	    args[nargs++] = 0;
5495b81b6b3SRodney W. Grimes 	}
5505b81b6b3SRodney W. Grimes 
5515b81b6b3SRodney W. Grimes 	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
5525b81b6b3SRodney W. Grimes 			 args[5], args[6], args[7], args[8], args[9] );
5531c6989faSPeter Wemm 	db_printf("%#lr\n", (long)retval);
5545b81b6b3SRodney W. Grimes }
555ad146781SPaul Traina 
556ad146781SPaul Traina /* Enter GDB remote protocol debugger on the next trap. */
557ad146781SPaul Traina 
558029f0b69SPoul-Henning Kamp void	  *gdb_arg = NULL;
5593000820aSPoul-Henning Kamp cn_getc_t *gdb_getc;
5603000820aSPoul-Henning Kamp cn_putc_t *gdb_putc;
5615c92a5b3SKirk McKusick 
562ad146781SPaul Traina static void
563ad146781SPaul Traina db_gdb (dummy1, dummy2, dummy3, dummy4)
564ad146781SPaul Traina 	db_expr_t	dummy1;
565ad146781SPaul Traina 	boolean_t	dummy2;
566ad146781SPaul Traina 	db_expr_t	dummy3;
567ad146781SPaul Traina 	char *		dummy4;
568ad146781SPaul Traina {
5695c92a5b3SKirk McKusick 
570029f0b69SPoul-Henning Kamp 	if (gdb_arg == NULL) {
5715c92a5b3SKirk McKusick 		db_printf("No gdb port enabled. Set flag 0x80 on desired port\n");
5725c92a5b3SKirk McKusick 		db_printf("in your configuration file (currently sio only).\n");
5735c92a5b3SKirk McKusick 		return;
5745c92a5b3SKirk McKusick 	}
575ad146781SPaul Traina 	boothowto ^= RB_GDB;
576ad146781SPaul Traina 
577ad146781SPaul Traina 	db_printf("Next trap will enter %s\n",
578ad146781SPaul Traina 		   boothowto & RB_GDB ? "GDB remote protocol mode"
579ad146781SPaul Traina 				      : "DDB debugger");
580ad146781SPaul Traina }
5815370c3b6SPeter Wemm 
5825370c3b6SPeter Wemm static void
58319d2c78fSDima Dorfman db_kill(dummy1, dummy2, dummy3, dummy4)
58419d2c78fSDima Dorfman 	db_expr_t	dummy1;
58519d2c78fSDima Dorfman 	boolean_t	dummy2;
58619d2c78fSDima Dorfman 	db_expr_t	dummy3;
58719d2c78fSDima Dorfman 	char *		dummy4;
58819d2c78fSDima Dorfman {
58919d2c78fSDima Dorfman 	db_expr_t old_radix, pid, sig;
59019d2c78fSDima Dorfman 	struct proc *p;
59119d2c78fSDima Dorfman 
59219d2c78fSDima Dorfman #define DB_ERROR(f)	do { db_printf f; db_flush_lex(); goto out; } while (0)
59319d2c78fSDima Dorfman 
59419d2c78fSDima Dorfman 	/*
59519d2c78fSDima Dorfman 	 * PIDs and signal numbers are typically represented in base
59619d2c78fSDima Dorfman 	 * 10, so make that the default here.  It can, of course, be
59719d2c78fSDima Dorfman 	 * overridden by specifying a prefix.
59819d2c78fSDima Dorfman 	 */
59919d2c78fSDima Dorfman 	old_radix = db_radix;
60019d2c78fSDima Dorfman 	db_radix = 10;
60119d2c78fSDima Dorfman 	/* Retrieve arguments. */
60219d2c78fSDima Dorfman 	if (!db_expression(&sig))
60319d2c78fSDima Dorfman 		DB_ERROR(("Missing signal number\n"));
60419d2c78fSDima Dorfman 	if (!db_expression(&pid))
60519d2c78fSDima Dorfman 		DB_ERROR(("Missing process ID\n"));
60619d2c78fSDima Dorfman 	db_skip_to_eol();
60719d2c78fSDima Dorfman 	if (sig < 0 || sig > _SIG_MAXSIG)
60819d2c78fSDima Dorfman 		DB_ERROR(("Signal number out of range\n"));
60919d2c78fSDima Dorfman 
61019d2c78fSDima Dorfman 	/*
61119d2c78fSDima Dorfman 	 * Find the process in question.  allproc_lock is not needed
61219d2c78fSDima Dorfman 	 * since we're in DDB.
61319d2c78fSDima Dorfman 	 */
61419d2c78fSDima Dorfman 	/* sx_slock(&allproc_lock); */
61519d2c78fSDima Dorfman 	LIST_FOREACH(p, &allproc, p_list)
61619d2c78fSDima Dorfman 	    if (p->p_pid == pid)
61719d2c78fSDima Dorfman 		    break;
61819d2c78fSDima Dorfman 	/* sx_sunlock(&allproc_lock); */
61919d2c78fSDima Dorfman 	if (p == NULL)
620e30e3e9eSMatt Jacob 		DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
62119d2c78fSDima Dorfman 
62219d2c78fSDima Dorfman 	/* If it's already locked, bail; otherwise, do the deed. */
62319d2c78fSDima Dorfman 	if (PROC_TRYLOCK(p) == 0)
624e30e3e9eSMatt Jacob 		DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
62519d2c78fSDima Dorfman 	else {
62619d2c78fSDima Dorfman 		psignal(p, sig);
62719d2c78fSDima Dorfman 		PROC_UNLOCK(p);
62819d2c78fSDima Dorfman 	}
62919d2c78fSDima Dorfman 
63019d2c78fSDima Dorfman out:
63119d2c78fSDima Dorfman 	db_radix = old_radix;
63219d2c78fSDima Dorfman #undef DB_ERROR
63319d2c78fSDima Dorfman }
63419d2c78fSDima Dorfman 
63519d2c78fSDima Dorfman static void
6365370c3b6SPeter Wemm db_reset(dummy1, dummy2, dummy3, dummy4)
6375370c3b6SPeter Wemm 	db_expr_t	dummy1;
6385370c3b6SPeter Wemm 	boolean_t	dummy2;
6395370c3b6SPeter Wemm 	db_expr_t	dummy3;
6405370c3b6SPeter Wemm 	char *		dummy4;
6415370c3b6SPeter Wemm {
6425370c3b6SPeter Wemm 
6435370c3b6SPeter Wemm 	cpu_reset();
6445370c3b6SPeter Wemm }
64574cc032bSPoul-Henning Kamp 
64674cc032bSPoul-Henning Kamp static void
64774cc032bSPoul-Henning Kamp db_watchdog(dummy1, dummy2, dummy3, dummy4)
64874cc032bSPoul-Henning Kamp 	db_expr_t	dummy1;
64974cc032bSPoul-Henning Kamp 	boolean_t	dummy2;
65074cc032bSPoul-Henning Kamp 	db_expr_t	dummy3;
65174cc032bSPoul-Henning Kamp 	char *		dummy4;
65274cc032bSPoul-Henning Kamp {
65374cc032bSPoul-Henning Kamp 	int i;
65474cc032bSPoul-Henning Kamp 
65574cc032bSPoul-Henning Kamp 	/*
65674cc032bSPoul-Henning Kamp 	 * XXX: It might make sense to be able to set the watchdog to a
65774cc032bSPoul-Henning Kamp 	 * XXX: timeout here so that failure or hang as a result of subsequent
65874cc032bSPoul-Henning Kamp 	 * XXX: ddb commands could be recovered by a reset.
65974cc032bSPoul-Henning Kamp 	 */
66074cc032bSPoul-Henning Kamp 
66174cc032bSPoul-Henning Kamp 	EVENTHANDLER_INVOKE(watchdog_list, 0, &i);
66274cc032bSPoul-Henning Kamp }
663