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