db_command.c revision 17848
110216Sphk/*
210216Sphk * Mach Operating System
310216Sphk * Copyright (c) 1991,1990 Carnegie Mellon University
410216Sphk * All Rights Reserved.
510216Sphk *
610216Sphk * Permission to use, copy, modify and distribute this software and its
7139825Simp * documentation is hereby granted, provided that both the copyright
8139825Simp * notice and this permission notice appear in all copies of the
910216Sphk * software, derivative works or modified versions, and any portions
1010216Sphk * thereof, and that both notices appear in supporting documentation.
1110216Sphk *
1210216Sphk * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
1310216Sphk * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
1410216Sphk * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
1510216Sphk *
1610216Sphk * Carnegie Mellon requests users of this software to return to
1710216Sphk *
1810216Sphk *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
1910216Sphk *  School of Computer Science
2010216Sphk *  Carnegie Mellon University
2110216Sphk *  Pittsburgh PA 15213-3890
2210216Sphk *
2310216Sphk * any improvements or extensions that they make and grant Carnegie the
2410216Sphk * rights to redistribute these changes.
2510216Sphk *
2610216Sphk *	$Id: db_command.c,v 1.18 1995/12/10 19:07:49 bde Exp $
2710216Sphk */
2810216Sphk
2910216Sphk/*
3010216Sphk *	Author: David B. Golub, Carnegie Mellon University
3110216Sphk *	Date:	7/90
3259656Siwasaki */
3359656Siwasaki
3459656Siwasaki/*
3510216Sphk * Command dispatcher.
3610216Sphk */
3710216Sphk#include <sys/param.h>
3810216Sphk#include <sys/reboot.h>
3910216Sphk#include <sys/systm.h>
4010216Sphk
4110216Sphk#include <vm/vm.h>
4210216Sphk#include <vm/vm_extern.h>
4310216Sphk
4410216Sphk#include <ddb/ddb.h>
4510216Sphk#include <ddb/db_command.h>
4610216Sphk#include <ddb/db_lex.h>
4710216Sphk#include <ddb/db_output.h>
4810216Sphk
4959656Siwasaki#include <setjmp.h>
5059656Siwasaki
5159656Siwasaki/*
5210216Sphk * Exported global variables
5310216Sphk */
5459656Siwasakiboolean_t	db_cmd_loop_done;
5559656Siwasakijmp_buf		db_jmpbuf;
5659656Siwasakidb_addr_t	db_dot;
5759656Siwasakidb_addr_t	db_last_addr;
5859656Siwasakidb_addr_t	db_prev;
5959656Siwasakidb_addr_t	db_next;
6010216Sphk
6110216Sphkstatic db_cmdfcn_t	db_fncall;
6210216Sphkstatic db_cmdfcn_t	db_gdb;
6310216Sphkstatic db_cmdfcn_t	db_panic;
6410216Sphk
6510216Sphk/*
6610216Sphk * if 'ed' style: 'dot' is set at start of last item printed,
6710216Sphk * and '+' points to next line.
6810216Sphk * Otherwise: 'dot' points to next item, '..' points to last.
6910216Sphk */
7010216Sphkstatic boolean_t	db_ed_style = TRUE;
7110216Sphk
7210216Sphk/*
7310216Sphk * Utility routine - discard tokens through end-of-line.
7410216Sphk */
7510216Sphkvoid
7610216Sphkdb_skip_to_eol()
7710216Sphk{
7810216Sphk	int	t;
7910216Sphk	do {
8010216Sphk	    t = db_read_token();
8110216Sphk	} while (t != tEOL);
8210216Sphk}
8310216Sphk
8410216Sphk/*
8510216Sphk * Command table
8610216Sphk */
8710216Sphkstruct command {
8810216Sphk	char *	name;		/* command name */
8913765Smpp	db_cmdfcn_t *fcn;	/* function to call */
9010216Sphk	int	flag;		/* extra info: */
9110216Sphk#define	CS_OWN		0x1	    /* non-standard syntax */
9210216Sphk#define	CS_MORE		0x2	    /* standard syntax, but may have other
9310216Sphk				       words at end */
9410216Sphk#define	CS_SET_DOT	0x100	    /* set dot after command */
9510216Sphk	struct command *more;	/* another level of command */
9610216Sphk};
9710216Sphk
9810216Sphk/*
9910216Sphk * Results of command search.
10010216Sphk */
10110216Sphk#define	CMD_UNIQUE	0
10210216Sphk#define	CMD_FOUND	1
10310216Sphk#define	CMD_NONE	2
10410216Sphk#define	CMD_AMBIGUOUS	3
10510216Sphk#define	CMD_HELP	4
10610216Sphk
10710216Sphkstatic void	db_cmd_list __P((struct command *table));
10810216Sphkstatic int	db_cmd_search __P((char *name, struct command *table,
10910216Sphk				   struct command **cmdp));
11010216Sphkstatic void	db_command __P((struct command **last_cmdp,
11110216Sphk				struct command *cmd_table));
11210216Sphk
11310216Sphk/*
11410216Sphk * Search for command prefix.
11510216Sphk */
11610216Sphkstatic int
11710216Sphkdb_cmd_search(name, table, cmdp)
11810216Sphk	char *		name;
11910216Sphk	struct command	*table;
12010216Sphk	struct command	**cmdp;	/* out */
12110216Sphk{
12210216Sphk	struct command	*cmd;
12310216Sphk	int		result = CMD_NONE;
12410216Sphk
12510216Sphk	for (cmd = table; cmd->name != 0; cmd++) {
12610216Sphk	    register char *lp;
12710216Sphk	    register char *rp;
12810216Sphk	    register int  c;
12910216Sphk
13010216Sphk	    lp = name;
13110216Sphk	    rp = cmd->name;
13210216Sphk	    while ((c = *lp) == *rp) {
13310216Sphk		if (c == 0) {
13410216Sphk		    /* complete match */
13510216Sphk		    *cmdp = cmd;
13610216Sphk		    return (CMD_UNIQUE);
13710216Sphk		}
13810216Sphk		lp++;
13910216Sphk		rp++;
14010216Sphk	    }
14110216Sphk	    if (c == 0) {
14210216Sphk		/* end of name, not end of command -
14310216Sphk		   partial match */
14410216Sphk		if (result == CMD_FOUND) {
14510216Sphk		    result = CMD_AMBIGUOUS;
14610216Sphk		    /* but keep looking for a full match -
14710216Sphk		       this lets us match single letters */
14810216Sphk		}
14910216Sphk		else {
15010216Sphk		    *cmdp = cmd;
15110216Sphk		    result = CMD_FOUND;
15210216Sphk		}
15310216Sphk	    }
15410216Sphk	}
15510216Sphk	if (result == CMD_NONE) {
15610216Sphk	    /* check for 'help' */
15710216Sphk		if (name[0] == 'h' && name[1] == 'e'
15810216Sphk		    && name[2] == 'l' && name[3] == 'p')
15910216Sphk			result = CMD_HELP;
16010216Sphk	}
16110216Sphk	return (result);
16210216Sphk}
16310216Sphk
16410216Sphkstatic void
16510216Sphkdb_cmd_list(table)
16610216Sphk	struct command *table;
16710216Sphk{
16810216Sphk	register struct command *cmd;
16910216Sphk
17010216Sphk	for (cmd = table; cmd->name != 0; cmd++) {
17110216Sphk	    db_printf("%-12s", cmd->name);
17210216Sphk	    db_end_line();
17310216Sphk	}
17410216Sphk}
17510216Sphk
17610216Sphkstatic void
17710216Sphkdb_command(last_cmdp, cmd_table)
17810216Sphk	struct command	**last_cmdp;	/* IN_OUT */
17910216Sphk	struct command	*cmd_table;
18010216Sphk{
18110216Sphk	struct command	*cmd;
18210216Sphk	int		t;
18310216Sphk	char		modif[TOK_STRING_SIZE];
18410216Sphk	db_expr_t	addr, count;
18510216Sphk	boolean_t	have_addr = FALSE;
18610216Sphk	int		result;
18710216Sphk
18810216Sphk	t = db_read_token();
18910216Sphk	if (t == tEOL) {
19010216Sphk	    /* empty line repeats last command, at 'next' */
19110216Sphk	    cmd = *last_cmdp;
19210216Sphk	    addr = (db_expr_t)db_next;
19310216Sphk	    have_addr = FALSE;
19410216Sphk	    count = 1;
19510216Sphk	    modif[0] = '\0';
19610216Sphk	}
19710216Sphk	else if (t == tEXCL) {
19810216Sphk	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
19910216Sphk	    return;
20010216Sphk	}
20110216Sphk	else if (t != tIDENT) {
20210216Sphk	    db_printf("?\n");
20310216Sphk	    db_flush_lex();
20410216Sphk	    return;
20510216Sphk	}
20610216Sphk	else {
20710216Sphk	    /*
20810216Sphk	     * Search for command
20910216Sphk	     */
21010216Sphk	    while (cmd_table) {
21110216Sphk		result = db_cmd_search(db_tok_string,
21210216Sphk				       cmd_table,
21310216Sphk				       &cmd);
21410216Sphk		switch (result) {
21510216Sphk		    case CMD_NONE:
21610216Sphk			db_printf("No such command\n");
21710216Sphk			db_flush_lex();
21810216Sphk			return;
21910216Sphk		    case CMD_AMBIGUOUS:
22010216Sphk			db_printf("Ambiguous\n");
22110216Sphk			db_flush_lex();
22210216Sphk			return;
22310216Sphk		    case CMD_HELP:
22410216Sphk			db_cmd_list(cmd_table);
22510216Sphk			db_flush_lex();
22610216Sphk			return;
22710216Sphk		    default:
22810216Sphk			break;
22910216Sphk		}
23010216Sphk		if ((cmd_table = cmd->more) != 0) {
23110216Sphk		    t = db_read_token();
23210216Sphk		    if (t != tIDENT) {
23310216Sphk			db_cmd_list(cmd_table);
23410216Sphk			db_flush_lex();
23510216Sphk			return;
23610216Sphk		    }
23710216Sphk		}
23810216Sphk	    }
23910216Sphk
24010216Sphk	    if ((cmd->flag & CS_OWN) == 0) {
24110216Sphk		/*
24210216Sphk		 * Standard syntax:
24310216Sphk		 * command [/modifier] [addr] [,count]
24410216Sphk		 */
24510216Sphk		t = db_read_token();
24610216Sphk		if (t == tSLASH) {
24710216Sphk		    t = db_read_token();
24810216Sphk		    if (t != tIDENT) {
24910216Sphk			db_printf("Bad modifier\n");
25010216Sphk			db_flush_lex();
25110216Sphk			return;
25210216Sphk		    }
25310216Sphk		    db_strcpy(modif, db_tok_string);
25410216Sphk		}
25510216Sphk		else {
25610216Sphk		    db_unread_token(t);
25710216Sphk		    modif[0] = '\0';
25810216Sphk		}
25910216Sphk
26010216Sphk		if (db_expression(&addr)) {
26110216Sphk		    db_dot = (db_addr_t) addr;
26210216Sphk		    db_last_addr = db_dot;
26359656Siwasaki		    have_addr = TRUE;
26459656Siwasaki		}
26559656Siwasaki		else {
26659656Siwasaki		    addr = (db_expr_t) db_dot;
26759656Siwasaki		    have_addr = FALSE;
26859656Siwasaki		}
26959656Siwasaki		t = db_read_token();
27059656Siwasaki		if (t == tCOMMA) {
27159656Siwasaki		    if (!db_expression(&count)) {
27259656Siwasaki			db_printf("Count missing\n");
27359656Siwasaki			db_flush_lex();
27459656Siwasaki			return;
27559656Siwasaki		    }
27659656Siwasaki		}
27759656Siwasaki		else {
27859656Siwasaki		    db_unread_token(t);
27959656Siwasaki		    count = -1;
280		}
281		if ((cmd->flag & CS_MORE) == 0) {
282		    db_skip_to_eol();
283		}
284	    }
285	}
286	*last_cmdp = cmd;
287	if (cmd != 0) {
288	    /*
289	     * Execute the command.
290	     */
291	    (*cmd->fcn)(addr, have_addr, count, modif);
292
293	    if (cmd->flag & CS_SET_DOT) {
294		/*
295		 * If command changes dot, set dot to
296		 * previous address displayed (if 'ed' style).
297		 */
298		if (db_ed_style) {
299		    db_dot = db_prev;
300		}
301		else {
302		    db_dot = db_next;
303		}
304	    }
305	    else {
306		/*
307		 * If command does not change dot,
308		 * set 'next' location to be the same.
309		 */
310		db_next = db_dot;
311	    }
312	}
313}
314
315/*
316 * 'show' commands
317 */
318
319static struct command db_show_all_cmds[] = {
320#if 0
321	{ "threads",	db_show_all_threads,	0,	0 },
322#endif
323	{ "procs",	db_ps,			0,	0 },
324	{ (char *)0 }
325};
326
327static struct command db_show_cmds[] = {
328	{ "all",	0,			0,	db_show_all_cmds },
329	{ "registers",	db_show_regs,		0,	0 },
330	{ "breaks",	db_listbreak_cmd, 	0,	0 },
331	{ "watches",	db_listwatch_cmd, 	0,	0 },
332#if 0
333	{ "thread",	db_show_one_thread,	0,	0 },
334#endif
335	{ "map",	vm_map_print,		0,	0 },
336	{ "object",	vm_object_print,	0,	0 },
337#if 0
338	{ "page",	vm_page_print,		0,	0 },
339#endif
340#if 0
341	{ "port",	ipc_port_print,		0,	0 },
342#endif
343	{ (char *)0, }
344};
345
346static struct command db_command_table[] = {
347	{ "print",	db_print_cmd,		0,	0 },
348	{ "p",		db_print_cmd,		0,	0 },
349	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
350	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
351	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
352	{ "set",	db_set_cmd,		CS_OWN,	0 },
353	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
354	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
355	{ "delete",	db_delete_cmd,		0,	0 },
356	{ "d",		db_delete_cmd,		0,	0 },
357	{ "break",	db_breakpoint_cmd,	0,	0 },
358	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
359	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
360	{ "step",	db_single_step_cmd,	0,	0 },
361	{ "s",		db_single_step_cmd,	0,	0 },
362	{ "continue",	db_continue_cmd,	0,	0 },
363	{ "c",		db_continue_cmd,	0,	0 },
364	{ "until",	db_trace_until_call_cmd,0,	0 },
365	{ "next",	db_trace_until_matching_cmd,0,	0 },
366	{ "match",	db_trace_until_matching_cmd,0,	0 },
367	{ "trace",	db_stack_trace_cmd,	0,	0 },
368	{ "call",	db_fncall,		CS_OWN,	0 },
369	{ "show",	0,			0,	db_show_cmds },
370	{ "ps",		db_ps,			0,	0 },
371	{ "panic",	db_panic,		0,	0 },
372	{ "gdb",	db_gdb,			0,	0 },
373	{ (char *)0, }
374};
375
376static struct command	*db_last_command = 0;
377
378#if 0
379void
380db_help_cmd()
381{
382	struct command *cmd = db_command_table;
383
384	while (cmd->name != 0) {
385	    db_printf("%-12s", cmd->name);
386	    db_end_line();
387	    cmd++;
388	}
389}
390#endif
391
392static void
393db_panic(dummy1, dummy2, dummy3, dummy4)
394	db_expr_t	dummy1;
395	boolean_t	dummy2;
396	db_expr_t	dummy3;
397	char *		dummy4;
398{
399	panic("from debugger");
400}
401
402void
403db_command_loop()
404{
405	/*
406	 * Initialize 'prev' and 'next' to dot.
407	 */
408	db_prev = db_dot;
409	db_next = db_dot;
410
411	db_cmd_loop_done = 0;
412	while (!db_cmd_loop_done) {
413
414	    (void) setjmp(db_jmpbuf);
415	    if (db_print_position() != 0)
416		db_printf("\n");
417
418	    db_printf("db> ");
419	    (void) db_read_line();
420
421	    db_command(&db_last_command, db_command_table);
422	}
423}
424
425void
426db_error(s)
427	char *s;
428{
429	if (s)
430	    db_printf(s);
431	db_flush_lex();
432	longjmp(db_jmpbuf, 1);
433}
434
435
436/*
437 * Call random function:
438 * !expr(arg,arg,arg)
439 */
440static void
441db_fncall(dummy1, dummy2, dummy3, dummy4)
442	db_expr_t	dummy1;
443	boolean_t	dummy2;
444	db_expr_t	dummy3;
445	char *		dummy4;
446{
447	db_expr_t	fn_addr;
448#define	MAXARGS		11	/* XXX only 10 are passed */
449	db_expr_t	args[MAXARGS];
450	int		nargs = 0;
451	db_expr_t	retval;
452	typedef db_expr_t fcn_10args_t __P((db_expr_t, db_expr_t, db_expr_t,
453					    db_expr_t, db_expr_t, db_expr_t,
454					    db_expr_t, db_expr_t, db_expr_t,
455					    db_expr_t));
456	fcn_10args_t	*func;
457	int		t;
458
459	if (!db_expression(&fn_addr)) {
460	    db_printf("Bad function\n");
461	    db_flush_lex();
462	    return;
463	}
464	func = (fcn_10args_t *)fn_addr;	/* XXX */
465
466	t = db_read_token();
467	if (t == tLPAREN) {
468	    if (db_expression(&args[0])) {
469		nargs++;
470		while ((t = db_read_token()) == tCOMMA) {
471		    if (nargs == MAXARGS) {
472			db_printf("Too many arguments\n");
473			db_flush_lex();
474			return;
475		    }
476		    if (!db_expression(&args[nargs])) {
477			db_printf("Argument missing\n");
478			db_flush_lex();
479			return;
480		    }
481		    nargs++;
482		}
483		db_unread_token(t);
484	    }
485	    if (db_read_token() != tRPAREN) {
486		db_printf("?\n");
487		db_flush_lex();
488		return;
489	    }
490	}
491	db_skip_to_eol();
492
493	while (nargs < MAXARGS) {
494	    args[nargs++] = 0;
495	}
496
497	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
498			 args[5], args[6], args[7], args[8], args[9] );
499	db_printf("%#n\n", retval);
500}
501
502/* Enter GDB remote protocol debugger on the next trap. */
503
504static void
505db_gdb (dummy1, dummy2, dummy3, dummy4)
506	db_expr_t	dummy1;
507	boolean_t	dummy2;
508	db_expr_t	dummy3;
509	char *		dummy4;
510{
511	boothowto ^= RB_GDB;
512
513	db_printf("Next trap will enter %s\n",
514		   boothowto & RB_GDB ? "GDB remote protocol mode"
515				      : "DDB debugger");
516}
517