db_command.c revision 18296
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.19 1996/08/27 19:46:28 pst 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/kernel.h>
39#include <sys/reboot.h>
40#include <sys/systm.h>
41
42#include <ddb/ddb.h>
43#include <ddb/db_command.h>
44#include <ddb/db_lex.h>
45#include <ddb/db_output.h>
46
47#include <setjmp.h>
48
49/*
50 * Exported global variables
51 */
52struct linker_set	db_cmd_set;
53boolean_t	db_cmd_loop_done;
54db_addr_t	db_dot;
55jmp_buf		db_jmpbuf;
56db_addr_t	db_last_addr;
57db_addr_t	db_prev;
58db_addr_t	db_next;
59struct linker_set	db_show_cmd_set;
60
61static db_cmdfcn_t	db_fncall;
62static db_cmdfcn_t	db_gdb;
63static db_cmdfcn_t	db_panic;
64
65/* XXX this is actually forward-static. */
66extern struct command	db_show_cmds[];
67
68/*
69 * if 'ed' style: 'dot' is set at start of last item printed,
70 * and '+' points to next line.
71 * Otherwise: 'dot' points to next item, '..' points to last.
72 */
73static boolean_t	db_ed_style = TRUE;
74
75/*
76 * Utility routine - discard tokens through end-of-line.
77 */
78void
79db_skip_to_eol()
80{
81	int	t;
82	do {
83	    t = db_read_token();
84	} while (t != tEOL);
85}
86
87/*
88 * Results of command search.
89 */
90#define	CMD_UNIQUE	0
91#define	CMD_FOUND	1
92#define	CMD_NONE	2
93#define	CMD_AMBIGUOUS	3
94#define	CMD_HELP	4
95
96static void	db_cmd_list __P((struct command *table,
97				 struct command **aux_tablep));
98static int	db_cmd_search __P((char *name, struct command *table,
99				   struct command **aux_tablep,
100				   struct command **cmdp));
101static void	db_command __P((struct command **last_cmdp,
102				struct command *cmd_table,
103				struct command **aux_cmd_tablep));
104
105/*
106 * Search for command prefix.
107 */
108static int
109db_cmd_search(name, table, aux_tablep, cmdp)
110	char *		name;
111	struct command	*table;
112	struct command	**aux_tablep;
113	struct command	**cmdp;	/* out */
114{
115	struct command	*cmd;
116	struct command	**aux_cmdp;
117	int		result = CMD_NONE;
118
119	for (cmd = table; cmd->name != 0; cmd++) {
120	    register char *lp;
121	    register char *rp;
122	    register int  c;
123
124	    lp = name;
125	    rp = cmd->name;
126	    while ((c = *lp) == *rp) {
127		if (c == 0) {
128		    /* complete match */
129		    *cmdp = cmd;
130		    return (CMD_UNIQUE);
131		}
132		lp++;
133		rp++;
134	    }
135	    if (c == 0) {
136		/* end of name, not end of command -
137		   partial match */
138		if (result == CMD_FOUND) {
139		    result = CMD_AMBIGUOUS;
140		    /* but keep looking for a full match -
141		       this lets us match single letters */
142		}
143		else {
144		    *cmdp = cmd;
145		    result = CMD_FOUND;
146		}
147	    }
148	}
149	if (result == CMD_NONE && aux_tablep != 0)
150	    /* XXX repeat too much code. */
151	    for (aux_cmdp = aux_tablep; *aux_cmdp != 0; aux_cmdp++) {
152		register char *lp;
153		register char *rp;
154		register int  c;
155
156		lp = name;
157		rp = (*aux_cmdp)->name;
158		while ((c = *lp) == *rp) {
159		    if (c == 0) {
160			/* complete match */
161			*cmdp = *aux_cmdp;
162			return (CMD_UNIQUE);
163		    }
164		    lp++;
165		    rp++;
166		}
167		if (c == 0) {
168		    /* end of name, not end of command -
169		       partial match */
170		    if (result == CMD_FOUND) {
171			result = CMD_AMBIGUOUS;
172			/* but keep looking for a full match -
173			   this lets us match single letters */
174		    }
175		    else {
176			*cmdp = *aux_cmdp;
177			result = CMD_FOUND;
178		    }
179		}
180	    }
181	if (result == CMD_NONE) {
182	    /* check for 'help' */
183		if (name[0] == 'h' && name[1] == 'e'
184		    && name[2] == 'l' && name[3] == 'p')
185			result = CMD_HELP;
186	}
187	return (result);
188}
189
190static void
191db_cmd_list(table, aux_tablep)
192	struct command *table;
193	struct command **aux_tablep;
194{
195	register struct command *cmd;
196	register struct command **aux_cmdp;
197
198	for (cmd = table; cmd->name != 0; cmd++) {
199	    db_printf("%-12s", cmd->name);
200	    db_end_line();
201	}
202	if (aux_tablep == 0)
203	    return;
204	for (aux_cmdp = aux_tablep; *aux_cmdp != 0; aux_cmdp++) {
205	    db_printf("%-12s", (*aux_cmdp)->name);
206	    db_end_line();
207	}
208}
209
210static void
211db_command(last_cmdp, cmd_table, aux_cmd_tablep)
212	struct command	**last_cmdp;	/* IN_OUT */
213	struct command	*cmd_table;
214	struct command	**aux_cmd_tablep;
215{
216	struct command	*cmd;
217	int		t;
218	char		modif[TOK_STRING_SIZE];
219	db_expr_t	addr, count;
220	boolean_t	have_addr = FALSE;
221	int		result;
222
223	t = db_read_token();
224	if (t == tEOL) {
225	    /* empty line repeats last command, at 'next' */
226	    cmd = *last_cmdp;
227	    addr = (db_expr_t)db_next;
228	    have_addr = FALSE;
229	    count = 1;
230	    modif[0] = '\0';
231	}
232	else if (t == tEXCL) {
233	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
234	    return;
235	}
236	else if (t != tIDENT) {
237	    db_printf("?\n");
238	    db_flush_lex();
239	    return;
240	}
241	else {
242	    /*
243	     * Search for command
244	     */
245	    while (cmd_table) {
246		result = db_cmd_search(db_tok_string,
247				       cmd_table,
248				       aux_cmd_tablep,
249				       &cmd);
250		switch (result) {
251		    case CMD_NONE:
252			db_printf("No such command\n");
253			db_flush_lex();
254			return;
255		    case CMD_AMBIGUOUS:
256			db_printf("Ambiguous\n");
257			db_flush_lex();
258			return;
259		    case CMD_HELP:
260			db_cmd_list(cmd_table, aux_cmd_tablep);
261			db_flush_lex();
262			return;
263		    default:
264			break;
265		}
266		if ((cmd_table = cmd->more) != 0) {
267		    /* XXX usually no more aux's. */
268		    aux_cmd_tablep = 0;
269		    if (cmd_table == db_show_cmds)
270			aux_cmd_tablep =
271			    (struct command **)&db_show_cmd_set.ls_items[0];
272
273		    t = db_read_token();
274		    if (t != tIDENT) {
275			db_cmd_list(cmd_table, aux_cmd_tablep);
276			db_flush_lex();
277			return;
278		    }
279		}
280	    }
281
282	    if ((cmd->flag & CS_OWN) == 0) {
283		/*
284		 * Standard syntax:
285		 * command [/modifier] [addr] [,count]
286		 */
287		t = db_read_token();
288		if (t == tSLASH) {
289		    t = db_read_token();
290		    if (t != tIDENT) {
291			db_printf("Bad modifier\n");
292			db_flush_lex();
293			return;
294		    }
295		    db_strcpy(modif, db_tok_string);
296		}
297		else {
298		    db_unread_token(t);
299		    modif[0] = '\0';
300		}
301
302		if (db_expression(&addr)) {
303		    db_dot = (db_addr_t) addr;
304		    db_last_addr = db_dot;
305		    have_addr = TRUE;
306		}
307		else {
308		    addr = (db_expr_t) db_dot;
309		    have_addr = FALSE;
310		}
311		t = db_read_token();
312		if (t == tCOMMA) {
313		    if (!db_expression(&count)) {
314			db_printf("Count missing\n");
315			db_flush_lex();
316			return;
317		    }
318		}
319		else {
320		    db_unread_token(t);
321		    count = -1;
322		}
323		if ((cmd->flag & CS_MORE) == 0) {
324		    db_skip_to_eol();
325		}
326	    }
327	}
328	*last_cmdp = cmd;
329	if (cmd != 0) {
330	    /*
331	     * Execute the command.
332	     */
333	    (*cmd->fcn)(addr, have_addr, count, modif);
334
335	    if (cmd->flag & CS_SET_DOT) {
336		/*
337		 * If command changes dot, set dot to
338		 * previous address displayed (if 'ed' style).
339		 */
340		if (db_ed_style) {
341		    db_dot = db_prev;
342		}
343		else {
344		    db_dot = db_next;
345		}
346	    }
347	    else {
348		/*
349		 * If command does not change dot,
350		 * set 'next' location to be the same.
351		 */
352		db_next = db_dot;
353	    }
354	}
355}
356
357/*
358 * 'show' commands
359 */
360
361static struct command db_show_all_cmds[] = {
362#if 0
363	{ "threads",	db_show_all_threads,	0,	0 },
364#endif
365	{ "procs",	db_ps,			0,	0 },
366	{ (char *)0 }
367};
368
369static struct command db_show_cmds[] = {
370	{ "all",	0,			0,	db_show_all_cmds },
371	{ "registers",	db_show_regs,		0,	0 },
372	{ "breaks",	db_listbreak_cmd, 	0,	0 },
373	{ "watches",	db_listwatch_cmd, 	0,	0 },
374#if 0
375	{ "thread",	db_show_one_thread,	0,	0 },
376#endif
377#if 0
378	{ "port",	ipc_port_print,		0,	0 },
379#endif
380	{ (char *)0, }
381};
382
383static struct command db_command_table[] = {
384	{ "print",	db_print_cmd,		0,	0 },
385	{ "p",		db_print_cmd,		0,	0 },
386	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
387	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
388	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
389	{ "set",	db_set_cmd,		CS_OWN,	0 },
390	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
391	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
392	{ "delete",	db_delete_cmd,		0,	0 },
393	{ "d",		db_delete_cmd,		0,	0 },
394	{ "break",	db_breakpoint_cmd,	0,	0 },
395	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
396	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
397	{ "step",	db_single_step_cmd,	0,	0 },
398	{ "s",		db_single_step_cmd,	0,	0 },
399	{ "continue",	db_continue_cmd,	0,	0 },
400	{ "c",		db_continue_cmd,	0,	0 },
401	{ "until",	db_trace_until_call_cmd,0,	0 },
402	{ "next",	db_trace_until_matching_cmd,0,	0 },
403	{ "match",	db_trace_until_matching_cmd,0,	0 },
404	{ "trace",	db_stack_trace_cmd,	0,	0 },
405	{ "call",	db_fncall,		CS_OWN,	0 },
406	{ "show",	0,			0,	db_show_cmds },
407	{ "ps",		db_ps,			0,	0 },
408	{ "panic",	db_panic,		0,	0 },
409	{ "gdb",	db_gdb,			0,	0 },
410	{ (char *)0, }
411};
412
413static struct command	*db_last_command = 0;
414
415#if 0
416void
417db_help_cmd()
418{
419	struct command *cmd = db_command_table;
420
421	while (cmd->name != 0) {
422	    db_printf("%-12s", cmd->name);
423	    db_end_line();
424	    cmd++;
425	}
426}
427#endif
428
429static void
430db_panic(dummy1, dummy2, dummy3, dummy4)
431	db_expr_t	dummy1;
432	boolean_t	dummy2;
433	db_expr_t	dummy3;
434	char *		dummy4;
435{
436	panic("from debugger");
437}
438
439void
440db_command_loop()
441{
442	/*
443	 * Initialize 'prev' and 'next' to dot.
444	 */
445	db_prev = db_dot;
446	db_next = db_dot;
447
448	db_cmd_loop_done = 0;
449	while (!db_cmd_loop_done) {
450
451	    (void) setjmp(db_jmpbuf);
452	    if (db_print_position() != 0)
453		db_printf("\n");
454
455	    db_printf("db> ");
456	    (void) db_read_line();
457
458	    db_command(&db_last_command, db_command_table,
459		       (struct command **)&db_cmd_set.ls_items[0]);
460	}
461}
462
463void
464db_error(s)
465	char *s;
466{
467	if (s)
468	    db_printf(s);
469	db_flush_lex();
470	longjmp(db_jmpbuf, 1);
471}
472
473
474/*
475 * Call random function:
476 * !expr(arg,arg,arg)
477 */
478static void
479db_fncall(dummy1, dummy2, dummy3, dummy4)
480	db_expr_t	dummy1;
481	boolean_t	dummy2;
482	db_expr_t	dummy3;
483	char *		dummy4;
484{
485	db_expr_t	fn_addr;
486#define	MAXARGS		11	/* XXX only 10 are passed */
487	db_expr_t	args[MAXARGS];
488	int		nargs = 0;
489	db_expr_t	retval;
490	typedef db_expr_t fcn_10args_t __P((db_expr_t, db_expr_t, db_expr_t,
491					    db_expr_t, db_expr_t, db_expr_t,
492					    db_expr_t, db_expr_t, db_expr_t,
493					    db_expr_t));
494	fcn_10args_t	*func;
495	int		t;
496
497	if (!db_expression(&fn_addr)) {
498	    db_printf("Bad function\n");
499	    db_flush_lex();
500	    return;
501	}
502	func = (fcn_10args_t *)fn_addr;	/* XXX */
503
504	t = db_read_token();
505	if (t == tLPAREN) {
506	    if (db_expression(&args[0])) {
507		nargs++;
508		while ((t = db_read_token()) == tCOMMA) {
509		    if (nargs == MAXARGS) {
510			db_printf("Too many arguments\n");
511			db_flush_lex();
512			return;
513		    }
514		    if (!db_expression(&args[nargs])) {
515			db_printf("Argument missing\n");
516			db_flush_lex();
517			return;
518		    }
519		    nargs++;
520		}
521		db_unread_token(t);
522	    }
523	    if (db_read_token() != tRPAREN) {
524		db_printf("?\n");
525		db_flush_lex();
526		return;
527	    }
528	}
529	db_skip_to_eol();
530
531	while (nargs < MAXARGS) {
532	    args[nargs++] = 0;
533	}
534
535	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
536			 args[5], args[6], args[7], args[8], args[9] );
537	db_printf("%#n\n", retval);
538}
539
540/* Enter GDB remote protocol debugger on the next trap. */
541
542static void
543db_gdb (dummy1, dummy2, dummy3, dummy4)
544	db_expr_t	dummy1;
545	boolean_t	dummy2;
546	db_expr_t	dummy3;
547	char *		dummy4;
548{
549	boothowto ^= RB_GDB;
550
551	db_printf("Next trap will enter %s\n",
552		   boothowto & RB_GDB ? "GDB remote protocol mode"
553				      : "DDB debugger");
554}
555