xref: /freebsd/sys/ddb/db_command.c (revision fd32d93b97086acaf80526bbb55f735b35d074fb)
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>
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;
7119d2c78fSDima Dorfman static db_cmdfcn_t	db_kill;
725370c3b6SPeter Wemm static db_cmdfcn_t	db_reset;
73fd32d93bSMarcel Moolenaar static db_cmdfcn_t	db_stack_trace;
7474cc032bSPoul-Henning Kamp static db_cmdfcn_t	db_watchdog;
75ad146781SPaul Traina 
766337f4efSBruce Evans /* XXX this is actually forward-static. */
776337f4efSBruce Evans extern struct command	db_show_cmds[];
786337f4efSBruce Evans 
795b81b6b3SRodney W. Grimes /*
805b81b6b3SRodney W. Grimes  * if 'ed' style: 'dot' is set at start of last item printed,
815b81b6b3SRodney W. Grimes  * and '+' points to next line.
825b81b6b3SRodney W. Grimes  * Otherwise: 'dot' points to next item, '..' points to last.
835b81b6b3SRodney W. Grimes  */
84f73a856dSPoul-Henning Kamp static boolean_t	db_ed_style = TRUE;
855b81b6b3SRodney W. Grimes 
865b81b6b3SRodney W. Grimes /*
875b81b6b3SRodney W. Grimes  * Utility routine - discard tokens through end-of-line.
885b81b6b3SRodney W. Grimes  */
895b81b6b3SRodney W. Grimes void
905b81b6b3SRodney W. Grimes db_skip_to_eol()
915b81b6b3SRodney W. Grimes {
925b81b6b3SRodney W. Grimes 	int	t;
935b81b6b3SRodney W. Grimes 	do {
945b81b6b3SRodney W. Grimes 	    t = db_read_token();
955b81b6b3SRodney W. Grimes 	} while (t != tEOL);
965b81b6b3SRodney W. Grimes }
975b81b6b3SRodney W. Grimes 
985b81b6b3SRodney W. Grimes /*
995b81b6b3SRodney W. Grimes  * Results of command search.
1005b81b6b3SRodney W. Grimes  */
1015b81b6b3SRodney W. Grimes #define	CMD_UNIQUE	0
1025b81b6b3SRodney W. Grimes #define	CMD_FOUND	1
1035b81b6b3SRodney W. Grimes #define	CMD_NONE	2
1045b81b6b3SRodney W. Grimes #define	CMD_AMBIGUOUS	3
1055b81b6b3SRodney W. Grimes #define	CMD_HELP	4
1065b81b6b3SRodney W. Grimes 
107763df836SBruce Evans static void	db_cmd_list(struct command *table, struct command **aux_tablep,
10814e10f99SAlfred Perlstein 		    struct command **aux_tablep_end);
10914e10f99SAlfred Perlstein static int	db_cmd_search(char *name, struct command *table,
1106337f4efSBruce Evans 		    struct command **aux_tablep,
111763df836SBruce Evans 		    struct command **aux_tablep_end, struct command **cmdp);
11214e10f99SAlfred Perlstein static void	db_command(struct command **last_cmdp,
113763df836SBruce Evans 		    struct command *cmd_table, struct command **aux_cmd_tablep,
11414e10f99SAlfred Perlstein 		    struct command **aux_cmd_tablep_end);
115058284fcSBruce Evans 
1165b81b6b3SRodney W. Grimes /*
1175b81b6b3SRodney W. Grimes  * Search for command prefix.
1185b81b6b3SRodney W. Grimes  */
119f73a856dSPoul-Henning Kamp static int
120f41325dbSPeter Wemm db_cmd_search(name, table, aux_tablep, aux_tablep_end, cmdp)
1215b81b6b3SRodney W. Grimes 	char *		name;
1225b81b6b3SRodney W. Grimes 	struct command	*table;
1236337f4efSBruce Evans 	struct command	**aux_tablep;
124f41325dbSPeter Wemm 	struct command	**aux_tablep_end;
1255b81b6b3SRodney W. Grimes 	struct command	**cmdp;	/* out */
1265b81b6b3SRodney W. Grimes {
1275b81b6b3SRodney W. Grimes 	struct command	*cmd;
1286337f4efSBruce Evans 	struct command	**aux_cmdp;
1295b81b6b3SRodney W. Grimes 	int		result = CMD_NONE;
1305b81b6b3SRodney W. Grimes 
1315b81b6b3SRodney W. Grimes 	for (cmd = table; cmd->name != 0; cmd++) {
1325b81b6b3SRodney W. Grimes 	    register char *lp;
1335b81b6b3SRodney W. Grimes 	    register char *rp;
1345b81b6b3SRodney W. Grimes 	    register int  c;
1355b81b6b3SRodney W. Grimes 
1365b81b6b3SRodney W. Grimes 	    lp = name;
1375b81b6b3SRodney W. Grimes 	    rp = cmd->name;
1385b81b6b3SRodney W. Grimes 	    while ((c = *lp) == *rp) {
1395b81b6b3SRodney W. Grimes 		if (c == 0) {
1405b81b6b3SRodney W. Grimes 		    /* complete match */
1415b81b6b3SRodney W. Grimes 		    *cmdp = cmd;
1425b81b6b3SRodney W. Grimes 		    return (CMD_UNIQUE);
1435b81b6b3SRodney W. Grimes 		}
1445b81b6b3SRodney W. Grimes 		lp++;
1455b81b6b3SRodney W. Grimes 		rp++;
1465b81b6b3SRodney W. Grimes 	    }
1475b81b6b3SRodney W. Grimes 	    if (c == 0) {
1485b81b6b3SRodney W. Grimes 		/* end of name, not end of command -
1495b81b6b3SRodney W. Grimes 		   partial match */
1505b81b6b3SRodney W. Grimes 		if (result == CMD_FOUND) {
1515b81b6b3SRodney W. Grimes 		    result = CMD_AMBIGUOUS;
1525b81b6b3SRodney W. Grimes 		    /* but keep looking for a full match -
1535b81b6b3SRodney W. Grimes 		       this lets us match single letters */
1545b81b6b3SRodney W. Grimes 		}
1555b81b6b3SRodney W. Grimes 		else {
1565b81b6b3SRodney W. Grimes 		    *cmdp = cmd;
1575b81b6b3SRodney W. Grimes 		    result = CMD_FOUND;
1585b81b6b3SRodney W. Grimes 		}
1595b81b6b3SRodney W. Grimes 	    }
1605b81b6b3SRodney W. Grimes 	}
1616337f4efSBruce Evans 	if (result == CMD_NONE && aux_tablep != 0)
1626337f4efSBruce Evans 	    /* XXX repeat too much code. */
163f41325dbSPeter Wemm 	    for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
1646337f4efSBruce Evans 		register char *lp;
1656337f4efSBruce Evans 		register char *rp;
1666337f4efSBruce Evans 		register int  c;
1676337f4efSBruce Evans 
1686337f4efSBruce Evans 		lp = name;
1696337f4efSBruce Evans 		rp = (*aux_cmdp)->name;
1706337f4efSBruce Evans 		while ((c = *lp) == *rp) {
1716337f4efSBruce Evans 		    if (c == 0) {
1726337f4efSBruce Evans 			/* complete match */
1736337f4efSBruce Evans 			*cmdp = *aux_cmdp;
1746337f4efSBruce Evans 			return (CMD_UNIQUE);
1756337f4efSBruce Evans 		    }
1766337f4efSBruce Evans 		    lp++;
1776337f4efSBruce Evans 		    rp++;
1786337f4efSBruce Evans 		}
1796337f4efSBruce Evans 		if (c == 0) {
1806337f4efSBruce Evans 		    /* end of name, not end of command -
1816337f4efSBruce Evans 		       partial match */
1826337f4efSBruce Evans 		    if (result == CMD_FOUND) {
1836337f4efSBruce Evans 			result = CMD_AMBIGUOUS;
1846337f4efSBruce Evans 			/* but keep looking for a full match -
1856337f4efSBruce Evans 			   this lets us match single letters */
1866337f4efSBruce Evans 		    }
1876337f4efSBruce Evans 		    else {
1886337f4efSBruce Evans 			*cmdp = *aux_cmdp;
1896337f4efSBruce Evans 			result = CMD_FOUND;
1906337f4efSBruce Evans 		    }
1916337f4efSBruce Evans 		}
1926337f4efSBruce Evans 	    }
1935b81b6b3SRodney W. Grimes 	if (result == CMD_NONE) {
1945b81b6b3SRodney W. Grimes 	    /* check for 'help' */
1955b81b6b3SRodney W. Grimes 		if (name[0] == 'h' && name[1] == 'e'
1965b81b6b3SRodney W. Grimes 		    && name[2] == 'l' && name[3] == 'p')
1975b81b6b3SRodney W. Grimes 			result = CMD_HELP;
1985b81b6b3SRodney W. Grimes 	}
1995b81b6b3SRodney W. Grimes 	return (result);
2005b81b6b3SRodney W. Grimes }
2015b81b6b3SRodney W. Grimes 
202f73a856dSPoul-Henning Kamp static void
203f41325dbSPeter Wemm db_cmd_list(table, aux_tablep, aux_tablep_end)
2045b81b6b3SRodney W. Grimes 	struct command *table;
2056337f4efSBruce Evans 	struct command **aux_tablep;
206f41325dbSPeter Wemm 	struct command **aux_tablep_end;
2075b81b6b3SRodney W. Grimes {
2085b81b6b3SRodney W. Grimes 	register struct command *cmd;
2096337f4efSBruce Evans 	register struct command **aux_cmdp;
2105b81b6b3SRodney W. Grimes 
2115b81b6b3SRodney W. Grimes 	for (cmd = table; cmd->name != 0; cmd++) {
2125b81b6b3SRodney W. Grimes 	    db_printf("%-12s", cmd->name);
2135b81b6b3SRodney W. Grimes 	    db_end_line();
2145b81b6b3SRodney W. Grimes 	}
2156337f4efSBruce Evans 	if (aux_tablep == 0)
2166337f4efSBruce Evans 	    return;
217f41325dbSPeter Wemm 	for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
2186337f4efSBruce Evans 	    db_printf("%-12s", (*aux_cmdp)->name);
2196337f4efSBruce Evans 	    db_end_line();
2206337f4efSBruce Evans 	}
2215b81b6b3SRodney W. Grimes }
2225b81b6b3SRodney W. Grimes 
223f73a856dSPoul-Henning Kamp static void
224f41325dbSPeter Wemm db_command(last_cmdp, cmd_table, aux_cmd_tablep, aux_cmd_tablep_end)
2255b81b6b3SRodney W. Grimes 	struct command	**last_cmdp;	/* IN_OUT */
2265b81b6b3SRodney W. Grimes 	struct command	*cmd_table;
2276337f4efSBruce Evans 	struct command	**aux_cmd_tablep;
228f41325dbSPeter Wemm 	struct command	**aux_cmd_tablep_end;
2295b81b6b3SRodney W. Grimes {
2305b81b6b3SRodney W. Grimes 	struct command	*cmd;
2315b81b6b3SRodney W. Grimes 	int		t;
2325b81b6b3SRodney W. Grimes 	char		modif[TOK_STRING_SIZE];
2335b81b6b3SRodney W. Grimes 	db_expr_t	addr, count;
234381fe1aaSGarrett Wollman 	boolean_t	have_addr = FALSE;
2355b81b6b3SRodney W. Grimes 	int		result;
2365b81b6b3SRodney W. Grimes 
2375b81b6b3SRodney W. Grimes 	t = db_read_token();
2385b81b6b3SRodney W. Grimes 	if (t == tEOL) {
2395b81b6b3SRodney W. Grimes 	    /* empty line repeats last command, at 'next' */
2405b81b6b3SRodney W. Grimes 	    cmd = *last_cmdp;
2415b81b6b3SRodney W. Grimes 	    addr = (db_expr_t)db_next;
2425b81b6b3SRodney W. Grimes 	    have_addr = FALSE;
2435b81b6b3SRodney W. Grimes 	    count = 1;
2445b81b6b3SRodney W. Grimes 	    modif[0] = '\0';
2455b81b6b3SRodney W. Grimes 	}
2465b81b6b3SRodney W. Grimes 	else if (t == tEXCL) {
247976794d9SBruce Evans 	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
2485b81b6b3SRodney W. Grimes 	    return;
2495b81b6b3SRodney W. Grimes 	}
2505b81b6b3SRodney W. Grimes 	else if (t != tIDENT) {
2515b81b6b3SRodney W. Grimes 	    db_printf("?\n");
2525b81b6b3SRodney W. Grimes 	    db_flush_lex();
2535b81b6b3SRodney W. Grimes 	    return;
2545b81b6b3SRodney W. Grimes 	}
2555b81b6b3SRodney W. Grimes 	else {
2565b81b6b3SRodney W. Grimes 	    /*
2575b81b6b3SRodney W. Grimes 	     * Search for command
2585b81b6b3SRodney W. Grimes 	     */
2595b81b6b3SRodney W. Grimes 	    while (cmd_table) {
2605b81b6b3SRodney W. Grimes 		result = db_cmd_search(db_tok_string,
2615b81b6b3SRodney W. Grimes 				       cmd_table,
2626337f4efSBruce Evans 				       aux_cmd_tablep,
263f41325dbSPeter Wemm 				       aux_cmd_tablep_end,
2645b81b6b3SRodney W. Grimes 				       &cmd);
2655b81b6b3SRodney W. Grimes 		switch (result) {
2665b81b6b3SRodney W. Grimes 		    case CMD_NONE:
2675b81b6b3SRodney W. Grimes 			db_printf("No such command\n");
2685b81b6b3SRodney W. Grimes 			db_flush_lex();
2695b81b6b3SRodney W. Grimes 			return;
2705b81b6b3SRodney W. Grimes 		    case CMD_AMBIGUOUS:
2715b81b6b3SRodney W. Grimes 			db_printf("Ambiguous\n");
2725b81b6b3SRodney W. Grimes 			db_flush_lex();
2735b81b6b3SRodney W. Grimes 			return;
2745b81b6b3SRodney W. Grimes 		    case CMD_HELP:
275f41325dbSPeter Wemm 			db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
2765b81b6b3SRodney W. Grimes 			db_flush_lex();
2775b81b6b3SRodney W. Grimes 			return;
2785b81b6b3SRodney W. Grimes 		    default:
2795b81b6b3SRodney W. Grimes 			break;
2805b81b6b3SRodney W. Grimes 		}
2815b81b6b3SRodney W. Grimes 		if ((cmd_table = cmd->more) != 0) {
2826337f4efSBruce Evans 		    /* XXX usually no more aux's. */
2836337f4efSBruce Evans 		    aux_cmd_tablep = 0;
284c2476fafSPoul-Henning Kamp 		    if (cmd_table == db_show_cmds) {
285f41325dbSPeter Wemm 			aux_cmd_tablep = SET_BEGIN(db_show_cmd_set);
286f41325dbSPeter Wemm 			aux_cmd_tablep_end = SET_LIMIT(db_show_cmd_set);
287c2476fafSPoul-Henning Kamp 		    }
2886337f4efSBruce Evans 
2895b81b6b3SRodney W. Grimes 		    t = db_read_token();
2905b81b6b3SRodney W. Grimes 		    if (t != tIDENT) {
291f41325dbSPeter Wemm 			db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
2925b81b6b3SRodney W. Grimes 			db_flush_lex();
2935b81b6b3SRodney W. Grimes 			return;
2945b81b6b3SRodney W. Grimes 		    }
2955b81b6b3SRodney W. Grimes 		}
2965b81b6b3SRodney W. Grimes 	    }
2975b81b6b3SRodney W. Grimes 
2985b81b6b3SRodney W. Grimes 	    if ((cmd->flag & CS_OWN) == 0) {
2995b81b6b3SRodney W. Grimes 		/*
3005b81b6b3SRodney W. Grimes 		 * Standard syntax:
3015b81b6b3SRodney W. Grimes 		 * command [/modifier] [addr] [,count]
3025b81b6b3SRodney W. Grimes 		 */
3035b81b6b3SRodney W. Grimes 		t = db_read_token();
3045b81b6b3SRodney W. Grimes 		if (t == tSLASH) {
3055b81b6b3SRodney W. Grimes 		    t = db_read_token();
3065b81b6b3SRodney W. Grimes 		    if (t != tIDENT) {
3075b81b6b3SRodney W. Grimes 			db_printf("Bad modifier\n");
3085b81b6b3SRodney W. Grimes 			db_flush_lex();
3095b81b6b3SRodney W. Grimes 			return;
3105b81b6b3SRodney W. Grimes 		    }
3115b81b6b3SRodney W. Grimes 		    db_strcpy(modif, db_tok_string);
3125b81b6b3SRodney W. Grimes 		}
3135b81b6b3SRodney W. Grimes 		else {
3145b81b6b3SRodney W. Grimes 		    db_unread_token(t);
3155b81b6b3SRodney W. Grimes 		    modif[0] = '\0';
3165b81b6b3SRodney W. Grimes 		}
3175b81b6b3SRodney W. Grimes 
3185b81b6b3SRodney W. Grimes 		if (db_expression(&addr)) {
3195b81b6b3SRodney W. Grimes 		    db_dot = (db_addr_t) addr;
3205b81b6b3SRodney W. Grimes 		    db_last_addr = db_dot;
3215b81b6b3SRodney W. Grimes 		    have_addr = TRUE;
3225b81b6b3SRodney W. Grimes 		}
3235b81b6b3SRodney W. Grimes 		else {
3245b81b6b3SRodney W. Grimes 		    addr = (db_expr_t) db_dot;
3255b81b6b3SRodney W. Grimes 		    have_addr = FALSE;
3265b81b6b3SRodney W. Grimes 		}
3275b81b6b3SRodney W. Grimes 		t = db_read_token();
3285b81b6b3SRodney W. Grimes 		if (t == tCOMMA) {
3295b81b6b3SRodney W. Grimes 		    if (!db_expression(&count)) {
3305b81b6b3SRodney W. Grimes 			db_printf("Count missing\n");
3315b81b6b3SRodney W. Grimes 			db_flush_lex();
3325b81b6b3SRodney W. Grimes 			return;
3335b81b6b3SRodney W. Grimes 		    }
3345b81b6b3SRodney W. Grimes 		}
3355b81b6b3SRodney W. Grimes 		else {
3365b81b6b3SRodney W. Grimes 		    db_unread_token(t);
3375b81b6b3SRodney W. Grimes 		    count = -1;
3385b81b6b3SRodney W. Grimes 		}
3395b81b6b3SRodney W. Grimes 		if ((cmd->flag & CS_MORE) == 0) {
3405b81b6b3SRodney W. Grimes 		    db_skip_to_eol();
3415b81b6b3SRodney W. Grimes 		}
3425b81b6b3SRodney W. Grimes 	    }
3435b81b6b3SRodney W. Grimes 	}
3445b81b6b3SRodney W. Grimes 	*last_cmdp = cmd;
3455b81b6b3SRodney W. Grimes 	if (cmd != 0) {
3465b81b6b3SRodney W. Grimes 	    /*
3475b81b6b3SRodney W. Grimes 	     * Execute the command.
3485b81b6b3SRodney W. Grimes 	     */
3495b81b6b3SRodney W. Grimes 	    (*cmd->fcn)(addr, have_addr, count, modif);
3501e16f609SJohn Baldwin 	    db_setup_paging(NULL, NULL, -1);
3515b81b6b3SRodney W. Grimes 
3525b81b6b3SRodney W. Grimes 	    if (cmd->flag & CS_SET_DOT) {
3535b81b6b3SRodney W. Grimes 		/*
3545b81b6b3SRodney W. Grimes 		 * If command changes dot, set dot to
3555b81b6b3SRodney W. Grimes 		 * previous address displayed (if 'ed' style).
3565b81b6b3SRodney W. Grimes 		 */
3575b81b6b3SRodney W. Grimes 		if (db_ed_style) {
3585b81b6b3SRodney W. Grimes 		    db_dot = db_prev;
3595b81b6b3SRodney W. Grimes 		}
3605b81b6b3SRodney W. Grimes 		else {
3615b81b6b3SRodney W. Grimes 		    db_dot = db_next;
3625b81b6b3SRodney W. Grimes 		}
3635b81b6b3SRodney W. Grimes 	    }
3645b81b6b3SRodney W. Grimes 	    else {
3655b81b6b3SRodney W. Grimes 		/*
3665b81b6b3SRodney W. Grimes 		 * If command does not change dot,
3675b81b6b3SRodney W. Grimes 		 * set 'next' location to be the same.
3685b81b6b3SRodney W. Grimes 		 */
3695b81b6b3SRodney W. Grimes 		db_next = db_dot;
3705b81b6b3SRodney W. Grimes 	    }
3715b81b6b3SRodney W. Grimes 	}
3725b81b6b3SRodney W. Grimes }
3735b81b6b3SRodney W. Grimes 
3745b81b6b3SRodney W. Grimes /*
3755b81b6b3SRodney W. Grimes  * 'show' commands
3765b81b6b3SRodney W. Grimes  */
377f23b4c91SGarrett Wollman 
378f73a856dSPoul-Henning Kamp static struct command db_show_all_cmds[] = {
3798a129caeSDavid Greenman 	{ "procs",	db_ps,			0,	0 },
3805b81b6b3SRodney W. Grimes 	{ (char *)0 }
3815b81b6b3SRodney W. Grimes };
3825b81b6b3SRodney W. Grimes 
383f73a856dSPoul-Henning Kamp static struct command db_show_cmds[] = {
3845b81b6b3SRodney W. Grimes 	{ "all",	0,			0,	db_show_all_cmds },
3855b81b6b3SRodney W. Grimes 	{ "registers",	db_show_regs,		0,	0 },
3865b81b6b3SRodney W. Grimes 	{ "breaks",	db_listbreak_cmd, 	0,	0 },
38737224cd3SMarcel Moolenaar 	{ "threads",	db_show_threads,	0,	0 },
3885b81b6b3SRodney W. Grimes 	{ (char *)0, }
3895b81b6b3SRodney W. Grimes };
3905b81b6b3SRodney W. Grimes 
391f73a856dSPoul-Henning Kamp static struct command db_command_table[] = {
3925b81b6b3SRodney W. Grimes 	{ "print",	db_print_cmd,		0,	0 },
39323898040SJoerg Wunsch 	{ "p",		db_print_cmd,		0,	0 },
3945b81b6b3SRodney W. Grimes 	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
3955b81b6b3SRodney W. Grimes 	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
3965b81b6b3SRodney W. Grimes 	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
3975b81b6b3SRodney W. Grimes 	{ "set",	db_set_cmd,		CS_OWN,	0 },
3985b81b6b3SRodney W. Grimes 	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
3995b81b6b3SRodney W. Grimes 	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
4005b81b6b3SRodney W. Grimes 	{ "delete",	db_delete_cmd,		0,	0 },
4015b81b6b3SRodney W. Grimes 	{ "d",		db_delete_cmd,		0,	0 },
4025b81b6b3SRodney W. Grimes 	{ "break",	db_breakpoint_cmd,	0,	0 },
4035b81b6b3SRodney W. Grimes 	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
4045b81b6b3SRodney W. Grimes 	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
40517bbfb58SBrian S. Dean 	{ "dhwatch",	db_deletehwatch_cmd,	0,      0 },
40617bbfb58SBrian S. Dean 	{ "hwatch",	db_hwatchpoint_cmd,	0,      0 },
4075b81b6b3SRodney W. Grimes 	{ "step",	db_single_step_cmd,	0,	0 },
4085b81b6b3SRodney W. Grimes 	{ "s",		db_single_step_cmd,	0,	0 },
4095b81b6b3SRodney W. Grimes 	{ "continue",	db_continue_cmd,	0,	0 },
4105b81b6b3SRodney W. Grimes 	{ "c",		db_continue_cmd,	0,	0 },
4115b81b6b3SRodney W. Grimes 	{ "until",	db_trace_until_call_cmd,0,	0 },
4125b81b6b3SRodney W. Grimes 	{ "next",	db_trace_until_matching_cmd,0,	0 },
4135b81b6b3SRodney W. Grimes 	{ "match",	db_trace_until_matching_cmd,0,	0 },
414fd32d93bSMarcel Moolenaar 	{ "trace",	db_stack_trace,		0,	0 },
415fd32d93bSMarcel Moolenaar 	{ "where",	db_stack_trace,		0,	0 },
4165b81b6b3SRodney W. Grimes 	{ "call",	db_fncall,		CS_OWN,	0 },
4175b81b6b3SRodney W. Grimes 	{ "show",	0,			0,	db_show_cmds },
41851f4a07bSGuido van Rooij 	{ "ps",		db_ps,			0,	0 },
419f3be7cb3SMarcel Moolenaar 	{ "gdb",	db_gdb,			0,	0 },
4205370c3b6SPeter Wemm 	{ "reset",	db_reset,		0,	0 },
42119d2c78fSDima Dorfman 	{ "kill",	db_kill,		CS_OWN,	0 },
42274cc032bSPoul-Henning Kamp 	{ "watchdog",	db_watchdog,		0,	0 },
42337224cd3SMarcel Moolenaar 	{ "thread",	db_set_thread,		CS_OWN,	0 },
4245b81b6b3SRodney W. Grimes 	{ (char *)0, }
4255b81b6b3SRodney W. Grimes };
4265b81b6b3SRodney W. Grimes 
427f73a856dSPoul-Henning Kamp static struct command	*db_last_command = 0;
4285b81b6b3SRodney W. Grimes 
429b7aa38c1SBruce Evans /*
430b7aa38c1SBruce Evans  * At least one non-optional command must be implemented using
431b7aa38c1SBruce Evans  * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
432b7aa38c1SBruce Evans  */
433b7aa38c1SBruce Evans DB_COMMAND(panic, db_panic)
4343eac4884SPoul-Henning Kamp {
435edf8a815SDavid Greenman 	panic("from debugger");
4363eac4884SPoul-Henning Kamp }
4373eac4884SPoul-Henning Kamp 
4383eac4884SPoul-Henning Kamp void
4395b81b6b3SRodney W. Grimes db_command_loop()
4405b81b6b3SRodney W. Grimes {
4415b81b6b3SRodney W. Grimes 	/*
4425b81b6b3SRodney W. Grimes 	 * Initialize 'prev' and 'next' to dot.
4435b81b6b3SRodney W. Grimes 	 */
4445b81b6b3SRodney W. Grimes 	db_prev = db_dot;
4455b81b6b3SRodney W. Grimes 	db_next = db_dot;
4465b81b6b3SRodney W. Grimes 
4475b81b6b3SRodney W. Grimes 	db_cmd_loop_done = 0;
4485b81b6b3SRodney W. Grimes 	while (!db_cmd_loop_done) {
4495b81b6b3SRodney W. Grimes 	    if (db_print_position() != 0)
4505b81b6b3SRodney W. Grimes 		db_printf("\n");
4515b81b6b3SRodney W. Grimes 
4525b81b6b3SRodney W. Grimes 	    db_printf("db> ");
4535b81b6b3SRodney W. Grimes 	    (void) db_read_line();
4545b81b6b3SRodney W. Grimes 
4556337f4efSBruce Evans 	    db_command(&db_last_command, db_command_table,
456f41325dbSPeter Wemm 		       SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set));
4575b81b6b3SRodney W. Grimes 	}
4585b81b6b3SRodney W. Grimes }
4595b81b6b3SRodney W. Grimes 
4605b81b6b3SRodney W. Grimes void
4615b81b6b3SRodney W. Grimes db_error(s)
462bda9921dSMark Murray 	const char *s;
4635b81b6b3SRodney W. Grimes {
4645b81b6b3SRodney W. Grimes 	if (s)
46558d9a059SKris Kennaway 	    db_printf("%s", s);
4665b81b6b3SRodney W. Grimes 	db_flush_lex();
46737224cd3SMarcel Moolenaar 	kdb_reenter();
4685b81b6b3SRodney W. Grimes }
4695b81b6b3SRodney W. Grimes 
4705b81b6b3SRodney W. Grimes 
4715b81b6b3SRodney W. Grimes /*
4725b81b6b3SRodney W. Grimes  * Call random function:
4735b81b6b3SRodney W. Grimes  * !expr(arg,arg,arg)
4745b81b6b3SRodney W. Grimes  */
475f73a856dSPoul-Henning Kamp static void
476976794d9SBruce Evans db_fncall(dummy1, dummy2, dummy3, dummy4)
477976794d9SBruce Evans 	db_expr_t	dummy1;
478976794d9SBruce Evans 	boolean_t	dummy2;
479976794d9SBruce Evans 	db_expr_t	dummy3;
480976794d9SBruce Evans 	char *		dummy4;
4815b81b6b3SRodney W. Grimes {
4825b81b6b3SRodney W. Grimes 	db_expr_t	fn_addr;
483058284fcSBruce Evans #define	MAXARGS		11	/* XXX only 10 are passed */
4845b81b6b3SRodney W. Grimes 	db_expr_t	args[MAXARGS];
4855b81b6b3SRodney W. Grimes 	int		nargs = 0;
4865b81b6b3SRodney W. Grimes 	db_expr_t	retval;
48714e10f99SAlfred Perlstein 	typedef db_expr_t fcn_10args_t(db_expr_t, db_expr_t, db_expr_t,
488763df836SBruce Evans 			    db_expr_t, db_expr_t, db_expr_t, db_expr_t,
489763df836SBruce Evans 			    db_expr_t, db_expr_t, db_expr_t);
490058284fcSBruce Evans 	fcn_10args_t	*func;
4915b81b6b3SRodney W. Grimes 	int		t;
4925b81b6b3SRodney W. Grimes 
4935b81b6b3SRodney W. Grimes 	if (!db_expression(&fn_addr)) {
4945b81b6b3SRodney W. Grimes 	    db_printf("Bad function\n");
4955b81b6b3SRodney W. Grimes 	    db_flush_lex();
4965b81b6b3SRodney W. Grimes 	    return;
4975b81b6b3SRodney W. Grimes 	}
498058284fcSBruce Evans 	func = (fcn_10args_t *)fn_addr;	/* XXX */
4995b81b6b3SRodney W. Grimes 
5005b81b6b3SRodney W. Grimes 	t = db_read_token();
5015b81b6b3SRodney W. Grimes 	if (t == tLPAREN) {
5025b81b6b3SRodney W. Grimes 	    if (db_expression(&args[0])) {
5035b81b6b3SRodney W. Grimes 		nargs++;
5045b81b6b3SRodney W. Grimes 		while ((t = db_read_token()) == tCOMMA) {
5055b81b6b3SRodney W. Grimes 		    if (nargs == MAXARGS) {
5065b81b6b3SRodney W. Grimes 			db_printf("Too many arguments\n");
5075b81b6b3SRodney W. Grimes 			db_flush_lex();
5085b81b6b3SRodney W. Grimes 			return;
5095b81b6b3SRodney W. Grimes 		    }
5105b81b6b3SRodney W. Grimes 		    if (!db_expression(&args[nargs])) {
5115b81b6b3SRodney W. Grimes 			db_printf("Argument missing\n");
5125b81b6b3SRodney W. Grimes 			db_flush_lex();
5135b81b6b3SRodney W. Grimes 			return;
5145b81b6b3SRodney W. Grimes 		    }
5155b81b6b3SRodney W. Grimes 		    nargs++;
5165b81b6b3SRodney W. Grimes 		}
5175b81b6b3SRodney W. Grimes 		db_unread_token(t);
5185b81b6b3SRodney W. Grimes 	    }
5195b81b6b3SRodney W. Grimes 	    if (db_read_token() != tRPAREN) {
5205b81b6b3SRodney W. Grimes 		db_printf("?\n");
5215b81b6b3SRodney W. Grimes 		db_flush_lex();
5225b81b6b3SRodney W. Grimes 		return;
5235b81b6b3SRodney W. Grimes 	    }
5245b81b6b3SRodney W. Grimes 	}
5255b81b6b3SRodney W. Grimes 	db_skip_to_eol();
5265b81b6b3SRodney W. Grimes 
5275b81b6b3SRodney W. Grimes 	while (nargs < MAXARGS) {
5285b81b6b3SRodney W. Grimes 	    args[nargs++] = 0;
5295b81b6b3SRodney W. Grimes 	}
5305b81b6b3SRodney W. Grimes 
5315b81b6b3SRodney W. Grimes 	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
5325b81b6b3SRodney W. Grimes 			 args[5], args[6], args[7], args[8], args[9] );
5331c6989faSPeter Wemm 	db_printf("%#lr\n", (long)retval);
5345b81b6b3SRodney W. Grimes }
535ad146781SPaul Traina 
5365370c3b6SPeter Wemm static void
53719d2c78fSDima Dorfman db_kill(dummy1, dummy2, dummy3, dummy4)
53819d2c78fSDima Dorfman 	db_expr_t	dummy1;
53919d2c78fSDima Dorfman 	boolean_t	dummy2;
54019d2c78fSDima Dorfman 	db_expr_t	dummy3;
54119d2c78fSDima Dorfman 	char *		dummy4;
54219d2c78fSDima Dorfman {
54319d2c78fSDima Dorfman 	db_expr_t old_radix, pid, sig;
54419d2c78fSDima Dorfman 	struct proc *p;
54519d2c78fSDima Dorfman 
54619d2c78fSDima Dorfman #define DB_ERROR(f)	do { db_printf f; db_flush_lex(); goto out; } while (0)
54719d2c78fSDima Dorfman 
54819d2c78fSDima Dorfman 	/*
54919d2c78fSDima Dorfman 	 * PIDs and signal numbers are typically represented in base
55019d2c78fSDima Dorfman 	 * 10, so make that the default here.  It can, of course, be
55119d2c78fSDima Dorfman 	 * overridden by specifying a prefix.
55219d2c78fSDima Dorfman 	 */
55319d2c78fSDima Dorfman 	old_radix = db_radix;
55419d2c78fSDima Dorfman 	db_radix = 10;
55519d2c78fSDima Dorfman 	/* Retrieve arguments. */
55619d2c78fSDima Dorfman 	if (!db_expression(&sig))
55719d2c78fSDima Dorfman 		DB_ERROR(("Missing signal number\n"));
55819d2c78fSDima Dorfman 	if (!db_expression(&pid))
55919d2c78fSDima Dorfman 		DB_ERROR(("Missing process ID\n"));
56019d2c78fSDima Dorfman 	db_skip_to_eol();
56119d2c78fSDima Dorfman 	if (sig < 0 || sig > _SIG_MAXSIG)
56219d2c78fSDima Dorfman 		DB_ERROR(("Signal number out of range\n"));
56319d2c78fSDima Dorfman 
56419d2c78fSDima Dorfman 	/*
56519d2c78fSDima Dorfman 	 * Find the process in question.  allproc_lock is not needed
56619d2c78fSDima Dorfman 	 * since we're in DDB.
56719d2c78fSDima Dorfman 	 */
56819d2c78fSDima Dorfman 	/* sx_slock(&allproc_lock); */
56919d2c78fSDima Dorfman 	LIST_FOREACH(p, &allproc, p_list)
57019d2c78fSDima Dorfman 	    if (p->p_pid == pid)
57119d2c78fSDima Dorfman 		    break;
57219d2c78fSDima Dorfman 	/* sx_sunlock(&allproc_lock); */
57319d2c78fSDima Dorfman 	if (p == NULL)
574e30e3e9eSMatt Jacob 		DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
57519d2c78fSDima Dorfman 
57619d2c78fSDima Dorfman 	/* If it's already locked, bail; otherwise, do the deed. */
57719d2c78fSDima Dorfman 	if (PROC_TRYLOCK(p) == 0)
578e30e3e9eSMatt Jacob 		DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
57919d2c78fSDima Dorfman 	else {
58019d2c78fSDima Dorfman 		psignal(p, sig);
58119d2c78fSDima Dorfman 		PROC_UNLOCK(p);
58219d2c78fSDima Dorfman 	}
58319d2c78fSDima Dorfman 
58419d2c78fSDima Dorfman out:
58519d2c78fSDima Dorfman 	db_radix = old_radix;
58619d2c78fSDima Dorfman #undef DB_ERROR
58719d2c78fSDima Dorfman }
58819d2c78fSDima Dorfman 
58919d2c78fSDima Dorfman static void
5905370c3b6SPeter Wemm db_reset(dummy1, dummy2, dummy3, dummy4)
5915370c3b6SPeter Wemm 	db_expr_t	dummy1;
5925370c3b6SPeter Wemm 	boolean_t	dummy2;
5935370c3b6SPeter Wemm 	db_expr_t	dummy3;
5945370c3b6SPeter Wemm 	char *		dummy4;
5955370c3b6SPeter Wemm {
5965370c3b6SPeter Wemm 
5975370c3b6SPeter Wemm 	cpu_reset();
5985370c3b6SPeter Wemm }
59974cc032bSPoul-Henning Kamp 
60074cc032bSPoul-Henning Kamp static void
60174cc032bSPoul-Henning Kamp db_watchdog(dummy1, dummy2, dummy3, dummy4)
60274cc032bSPoul-Henning Kamp 	db_expr_t	dummy1;
60374cc032bSPoul-Henning Kamp 	boolean_t	dummy2;
60474cc032bSPoul-Henning Kamp 	db_expr_t	dummy3;
60574cc032bSPoul-Henning Kamp 	char *		dummy4;
60674cc032bSPoul-Henning Kamp {
60774cc032bSPoul-Henning Kamp 	int i;
60874cc032bSPoul-Henning Kamp 
60974cc032bSPoul-Henning Kamp 	/*
61074cc032bSPoul-Henning Kamp 	 * XXX: It might make sense to be able to set the watchdog to a
61174cc032bSPoul-Henning Kamp 	 * XXX: timeout here so that failure or hang as a result of subsequent
61274cc032bSPoul-Henning Kamp 	 * XXX: ddb commands could be recovered by a reset.
61374cc032bSPoul-Henning Kamp 	 */
61474cc032bSPoul-Henning Kamp 
61574cc032bSPoul-Henning Kamp 	EVENTHANDLER_INVOKE(watchdog_list, 0, &i);
61674cc032bSPoul-Henning Kamp }
617f3be7cb3SMarcel Moolenaar 
618f3be7cb3SMarcel Moolenaar static void
619f3be7cb3SMarcel Moolenaar db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
620f3be7cb3SMarcel Moolenaar {
621f3be7cb3SMarcel Moolenaar 
622f3be7cb3SMarcel Moolenaar 	if (kdb_dbbe_select("gdb") != 0)
623f3be7cb3SMarcel Moolenaar 		db_printf("The remote GDB backend could not be selected.\n");
624f3be7cb3SMarcel Moolenaar 	else
625f3be7cb3SMarcel Moolenaar 		db_printf("Step to enter the remote GDB backend.\n");
626f3be7cb3SMarcel Moolenaar }
627fd32d93bSMarcel Moolenaar 
628fd32d93bSMarcel Moolenaar static void
629fd32d93bSMarcel Moolenaar db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
630fd32d93bSMarcel Moolenaar {
631fd32d93bSMarcel Moolenaar 	struct thread *td;
632fd32d93bSMarcel Moolenaar 	db_expr_t radix;
633fd32d93bSMarcel Moolenaar 	int t;
634fd32d93bSMarcel Moolenaar 
635fd32d93bSMarcel Moolenaar 	/*
636fd32d93bSMarcel Moolenaar 	 * We parse our own arguments. We don't like the default radix.
637fd32d93bSMarcel Moolenaar 	 */
638fd32d93bSMarcel Moolenaar 	radix = db_radix;
639fd32d93bSMarcel Moolenaar 	db_radix = 10;
640fd32d93bSMarcel Moolenaar 	hastid = db_expression(&tid);
641fd32d93bSMarcel Moolenaar 	t = db_read_token();
642fd32d93bSMarcel Moolenaar 	if (t == tCOMMA) {
643fd32d93bSMarcel Moolenaar 		if (!db_expression(&count)) {
644fd32d93bSMarcel Moolenaar 			db_printf("Count missing\n");
645fd32d93bSMarcel Moolenaar 			db_flush_lex();
646fd32d93bSMarcel Moolenaar 			return;
647fd32d93bSMarcel Moolenaar 		}
648fd32d93bSMarcel Moolenaar 	} else {
649fd32d93bSMarcel Moolenaar 		db_unread_token(t);
650fd32d93bSMarcel Moolenaar 		count = -1;
651fd32d93bSMarcel Moolenaar 	}
652fd32d93bSMarcel Moolenaar 	db_skip_to_eol();
653fd32d93bSMarcel Moolenaar 	db_radix = radix;
654fd32d93bSMarcel Moolenaar 
655fd32d93bSMarcel Moolenaar 	if (hastid) {
656fd32d93bSMarcel Moolenaar 		td = kdb_thr_lookup((lwpid_t)tid);
657fd32d93bSMarcel Moolenaar 		if (td == NULL)
658fd32d93bSMarcel Moolenaar 			td = kdb_thr_from_pid((pid_t)tid);
659fd32d93bSMarcel Moolenaar 		if (td == NULL) {
660fd32d93bSMarcel Moolenaar 			db_printf("Thread %d not found\n", (int)tid);
661fd32d93bSMarcel Moolenaar 			return;
662fd32d93bSMarcel Moolenaar 		}
663fd32d93bSMarcel Moolenaar 	} else
664fd32d93bSMarcel Moolenaar 		td = kdb_thread;
665fd32d93bSMarcel Moolenaar 	db_trace_thread(td, count);
666fd32d93bSMarcel Moolenaar }
667