db_command.c revision 17848
1/*
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19 *  School of Computer Science
20 *  Carnegie Mellon University
21 *  Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
25 *
26 *	$Id: db_command.c,v 1.18 1995/12/10 19:07:49 bde Exp $
27 */
28
29/*
30 *	Author: David B. Golub, Carnegie Mellon University
31 *	Date:	7/90
32 */
33
34/*
35 * Command dispatcher.
36 */
37#include <sys/param.h>
38#include <sys/reboot.h>
39#include <sys/systm.h>
40
41#include <vm/vm.h>
42#include <vm/vm_extern.h>
43
44#include <ddb/ddb.h>
45#include <ddb/db_command.h>
46#include <ddb/db_lex.h>
47#include <ddb/db_output.h>
48
49#include <setjmp.h>
50
51/*
52 * Exported global variables
53 */
54boolean_t	db_cmd_loop_done;
55jmp_buf		db_jmpbuf;
56db_addr_t	db_dot;
57db_addr_t	db_last_addr;
58db_addr_t	db_prev;
59db_addr_t	db_next;
60
61static db_cmdfcn_t	db_fncall;
62static db_cmdfcn_t	db_gdb;
63static db_cmdfcn_t	db_panic;
64
65/*
66 * if 'ed' style: 'dot' is set at start of last item printed,
67 * and '+' points to next line.
68 * Otherwise: 'dot' points to next item, '..' points to last.
69 */
70static boolean_t	db_ed_style = TRUE;
71
72/*
73 * Utility routine - discard tokens through end-of-line.
74 */
75void
76db_skip_to_eol()
77{
78	int	t;
79	do {
80	    t = db_read_token();
81	} while (t != tEOL);
82}
83
84/*
85 * Command table
86 */
87struct command {
88	char *	name;		/* command name */
89	db_cmdfcn_t *fcn;	/* function to call */
90	int	flag;		/* extra info: */
91#define	CS_OWN		0x1	    /* non-standard syntax */
92#define	CS_MORE		0x2	    /* standard syntax, but may have other
93				       words at end */
94#define	CS_SET_DOT	0x100	    /* set dot after command */
95	struct command *more;	/* another level of command */
96};
97
98/*
99 * Results of command search.
100 */
101#define	CMD_UNIQUE	0
102#define	CMD_FOUND	1
103#define	CMD_NONE	2
104#define	CMD_AMBIGUOUS	3
105#define	CMD_HELP	4
106
107static void	db_cmd_list __P((struct command *table));
108static int	db_cmd_search __P((char *name, struct command *table,
109				   struct command **cmdp));
110static void	db_command __P((struct command **last_cmdp,
111				struct command *cmd_table));
112
113/*
114 * Search for command prefix.
115 */
116static int
117db_cmd_search(name, table, cmdp)
118	char *		name;
119	struct command	*table;
120	struct command	**cmdp;	/* out */
121{
122	struct command	*cmd;
123	int		result = CMD_NONE;
124
125	for (cmd = table; cmd->name != 0; cmd++) {
126	    register char *lp;
127	    register char *rp;
128	    register int  c;
129
130	    lp = name;
131	    rp = cmd->name;
132	    while ((c = *lp) == *rp) {
133		if (c == 0) {
134		    /* complete match */
135		    *cmdp = cmd;
136		    return (CMD_UNIQUE);
137		}
138		lp++;
139		rp++;
140	    }
141	    if (c == 0) {
142		/* end of name, not end of command -
143		   partial match */
144		if (result == CMD_FOUND) {
145		    result = CMD_AMBIGUOUS;
146		    /* but keep looking for a full match -
147		       this lets us match single letters */
148		}
149		else {
150		    *cmdp = cmd;
151		    result = CMD_FOUND;
152		}
153	    }
154	}
155	if (result == CMD_NONE) {
156	    /* check for 'help' */
157		if (name[0] == 'h' && name[1] == 'e'
158		    && name[2] == 'l' && name[3] == 'p')
159			result = CMD_HELP;
160	}
161	return (result);
162}
163
164static void
165db_cmd_list(table)
166	struct command *table;
167{
168	register struct command *cmd;
169
170	for (cmd = table; cmd->name != 0; cmd++) {
171	    db_printf("%-12s", cmd->name);
172	    db_end_line();
173	}
174}
175
176static void
177db_command(last_cmdp, cmd_table)
178	struct command	**last_cmdp;	/* IN_OUT */
179	struct command	*cmd_table;
180{
181	struct command	*cmd;
182	int		t;
183	char		modif[TOK_STRING_SIZE];
184	db_expr_t	addr, count;
185	boolean_t	have_addr = FALSE;
186	int		result;
187
188	t = db_read_token();
189	if (t == tEOL) {
190	    /* empty line repeats last command, at 'next' */
191	    cmd = *last_cmdp;
192	    addr = (db_expr_t)db_next;
193	    have_addr = FALSE;
194	    count = 1;
195	    modif[0] = '\0';
196	}
197	else if (t == tEXCL) {
198	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
199	    return;
200	}
201	else if (t != tIDENT) {
202	    db_printf("?\n");
203	    db_flush_lex();
204	    return;
205	}
206	else {
207	    /*
208	     * Search for command
209	     */
210	    while (cmd_table) {
211		result = db_cmd_search(db_tok_string,
212				       cmd_table,
213				       &cmd);
214		switch (result) {
215		    case CMD_NONE:
216			db_printf("No such command\n");
217			db_flush_lex();
218			return;
219		    case CMD_AMBIGUOUS:
220			db_printf("Ambiguous\n");
221			db_flush_lex();
222			return;
223		    case CMD_HELP:
224			db_cmd_list(cmd_table);
225			db_flush_lex();
226			return;
227		    default:
228			break;
229		}
230		if ((cmd_table = cmd->more) != 0) {
231		    t = db_read_token();
232		    if (t != tIDENT) {
233			db_cmd_list(cmd_table);
234			db_flush_lex();
235			return;
236		    }
237		}
238	    }
239
240	    if ((cmd->flag & CS_OWN) == 0) {
241		/*
242		 * Standard syntax:
243		 * command [/modifier] [addr] [,count]
244		 */
245		t = db_read_token();
246		if (t == tSLASH) {
247		    t = db_read_token();
248		    if (t != tIDENT) {
249			db_printf("Bad modifier\n");
250			db_flush_lex();
251			return;
252		    }
253		    db_strcpy(modif, db_tok_string);
254		}
255		else {
256		    db_unread_token(t);
257		    modif[0] = '\0';
258		}
259
260		if (db_expression(&addr)) {
261		    db_dot = (db_addr_t) addr;
262		    db_last_addr = db_dot;
263		    have_addr = TRUE;
264		}
265		else {
266		    addr = (db_expr_t) db_dot;
267		    have_addr = FALSE;
268		}
269		t = db_read_token();
270		if (t == tCOMMA) {
271		    if (!db_expression(&count)) {
272			db_printf("Count missing\n");
273			db_flush_lex();
274			return;
275		    }
276		}
277		else {
278		    db_unread_token(t);
279		    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