db_command.c revision 6204
1277218Sjfv/*
2277218Sjfv * Mach Operating System
3277218Sjfv * Copyright (c) 1991,1990 Carnegie Mellon University
4277218Sjfv * All Rights Reserved.
5277218Sjfv *
6277218Sjfv * Permission to use, copy, modify and distribute this software and its
7277218Sjfv * documentation is hereby granted, provided that both the copyright
8277218Sjfv * notice and this permission notice appear in all copies of the
9277218Sjfv * software, derivative works or modified versions, and any portions
10277218Sjfv * thereof, and that both notices appear in supporting documentation.
11277218Sjfv *
12277218Sjfv * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13277218Sjfv * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14277218Sjfv * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15277218Sjfv *
16277218Sjfv * Carnegie Mellon requests users of this software to return to
17277218Sjfv *
18277218Sjfv *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19277218Sjfv *  School of Computer Science
20277218Sjfv *  Carnegie Mellon University
21277218Sjfv *  Pittsburgh PA 15213-3890
22277218Sjfv *
23277218Sjfv * any improvements or extensions that they make and grant Carnegie the
24277218Sjfv * rights to redistribute these changes.
25277218Sjfv *
26277218Sjfv *	$Id: db_command.c,v 1.8 1994/08/27 16:14:08 davidg Exp $
27277218Sjfv */
28277218Sjfv
29277218Sjfv/*
30277218Sjfv *	Author: David B. Golub, Carnegie Mellon University
31277218Sjfv *	Date:	7/90
32277218Sjfv */
33277218Sjfv
34285603Sbrueffer/*
35277218Sjfv * Command dispatcher.
36277218Sjfv */
37277218Sjfv#include <sys/param.h>
38277218Sjfv#include <sys/systm.h>
39277218Sjfv#include <sys/proc.h>
40277218Sjfv#include <ddb/ddb.h>
41277218Sjfv
42277218Sjfv#include <ddb/db_lex.h>
43277218Sjfv#include <ddb/db_output.h>
44277218Sjfv
45277218Sjfv#include <setjmp.h>
46277218Sjfv
47277218Sjfv/*
48277218Sjfv * Exported global variables
49277218Sjfv */
50277218Sjfvboolean_t	db_cmd_loop_done;
51277218Sjfvjmp_buf		db_jmpbuf;
52277218Sjfvdb_addr_t	db_dot;
53277218Sjfvdb_addr_t	db_last_addr;
54277218Sjfvdb_addr_t	db_prev;
55277218Sjfvdb_addr_t	db_next;
56277218Sjfv
57277218Sjfv/*
58277218Sjfv * if 'ed' style: 'dot' is set at start of last item printed,
59277218Sjfv * and '+' points to next line.
60277218Sjfv * Otherwise: 'dot' points to next item, '..' points to last.
61277218Sjfv */
62277218Sjfvboolean_t	db_ed_style = TRUE;
63277218Sjfv
64277218Sjfv
65277218Sjfv/*
66277218Sjfv * Utility routine - discard tokens through end-of-line.
67277218Sjfv */
68277218Sjfvvoid
69277218Sjfvdb_skip_to_eol()
70277218Sjfv{
71277218Sjfv	int	t;
72277218Sjfv	do {
73277218Sjfv	    t = db_read_token();
74277218Sjfv	} while (t != tEOL);
75277218Sjfv}
76277218Sjfv
77277218Sjfv/*
78277218Sjfv * Command table
79277218Sjfv */
80285603Sbruefferstruct command {
81285603Sbrueffer	char *	name;		/* command name */
82277218Sjfv	void	(*fcn)();	/* function to call */
83285603Sbrueffer	int	flag;		/* extra info: */
84277218Sjfv#define	CS_OWN		0x1	    /* non-standard syntax */
85277218Sjfv#define	CS_MORE		0x2	    /* standard syntax, but may have other
86285603Sbrueffer				       words at end */
87277218Sjfv#define	CS_SET_DOT	0x100	    /* set dot after command */
88277218Sjfv	struct command *more;	/* another level of command */
89277218Sjfv};
90277218Sjfv
91277218Sjfv/*
92277218Sjfv * Results of command search.
93277218Sjfv */
94277218Sjfv#define	CMD_UNIQUE	0
95285603Sbrueffer#define	CMD_FOUND	1
96277218Sjfv#define	CMD_NONE	2
97277218Sjfv#define	CMD_AMBIGUOUS	3
98285603Sbrueffer#define	CMD_HELP	4
99277218Sjfv
100277218Sjfv/*
101277218Sjfv * Search for command prefix.
102277218Sjfv */
103277218Sjfvint
104277218Sjfvdb_cmd_search(name, table, cmdp)
105277218Sjfv	char *		name;
106277218Sjfv	struct command	*table;
107277218Sjfv	struct command	**cmdp;	/* out */
108277218Sjfv{
109277218Sjfv	struct command	*cmd;
110277218Sjfv	int		result = CMD_NONE;
111277218Sjfv
112277218Sjfv	for (cmd = table; cmd->name != 0; cmd++) {
113277218Sjfv	    register char *lp;
114277218Sjfv	    register char *rp;
115277218Sjfv	    register int  c;
116277218Sjfv
117277218Sjfv	    lp = name;
118277218Sjfv	    rp = cmd->name;
119277218Sjfv	    while ((c = *lp) == *rp) {
120277218Sjfv		if (c == 0) {
121285603Sbrueffer		    /* complete match */
122277218Sjfv		    *cmdp = cmd;
123277218Sjfv		    return (CMD_UNIQUE);
124277218Sjfv		}
125277218Sjfv		lp++;
126277218Sjfv		rp++;
127277218Sjfv	    }
128277218Sjfv	    if (c == 0) {
129277218Sjfv		/* end of name, not end of command -
130277218Sjfv		   partial match */
131277218Sjfv		if (result == CMD_FOUND) {
132277218Sjfv		    result = CMD_AMBIGUOUS;
133277218Sjfv		    /* but keep looking for a full match -
134277218Sjfv		       this lets us match single letters */
135277218Sjfv		}
136277218Sjfv		else {
137285603Sbrueffer		    *cmdp = cmd;
138277218Sjfv		    result = CMD_FOUND;
139285603Sbrueffer		}
140	    }
141	}
142	if (result == CMD_NONE) {
143	    /* check for 'help' */
144		if (name[0] == 'h' && name[1] == 'e'
145		    && name[2] == 'l' && name[3] == 'p')
146			result = CMD_HELP;
147	}
148	return (result);
149}
150
151void
152db_cmd_list(table)
153	struct command *table;
154{
155	register struct command *cmd;
156
157	for (cmd = table; cmd->name != 0; cmd++) {
158	    db_printf("%-12s", cmd->name);
159	    db_end_line();
160	}
161}
162
163void
164db_command(last_cmdp, cmd_table)
165	struct command	**last_cmdp;	/* IN_OUT */
166	struct command	*cmd_table;
167{
168	struct command	*cmd;
169	int		t;
170	char		modif[TOK_STRING_SIZE];
171	db_expr_t	addr, count;
172	boolean_t	have_addr = FALSE;
173	int		result;
174
175	t = db_read_token();
176	if (t == tEOL) {
177	    /* empty line repeats last command, at 'next' */
178	    cmd = *last_cmdp;
179	    addr = (db_expr_t)db_next;
180	    have_addr = FALSE;
181	    count = 1;
182	    modif[0] = '\0';
183	}
184	else if (t == tEXCL) {
185	    void db_fncall();
186	    db_fncall();
187	    return;
188	}
189	else if (t != tIDENT) {
190	    db_printf("?\n");
191	    db_flush_lex();
192	    return;
193	}
194	else {
195	    /*
196	     * Search for command
197	     */
198	    while (cmd_table) {
199		result = db_cmd_search(db_tok_string,
200				       cmd_table,
201				       &cmd);
202		switch (result) {
203		    case CMD_NONE:
204			db_printf("No such command\n");
205			db_flush_lex();
206			return;
207		    case CMD_AMBIGUOUS:
208			db_printf("Ambiguous\n");
209			db_flush_lex();
210			return;
211		    case CMD_HELP:
212			db_cmd_list(cmd_table);
213			db_flush_lex();
214			return;
215		    default:
216			break;
217		}
218		if ((cmd_table = cmd->more) != 0) {
219		    t = db_read_token();
220		    if (t != tIDENT) {
221			db_cmd_list(cmd_table);
222			db_flush_lex();
223			return;
224		    }
225		}
226	    }
227
228	    if ((cmd->flag & CS_OWN) == 0) {
229		/*
230		 * Standard syntax:
231		 * command [/modifier] [addr] [,count]
232		 */
233		t = db_read_token();
234		if (t == tSLASH) {
235		    t = db_read_token();
236		    if (t != tIDENT) {
237			db_printf("Bad modifier\n");
238			db_flush_lex();
239			return;
240		    }
241		    db_strcpy(modif, db_tok_string);
242		}
243		else {
244		    db_unread_token(t);
245		    modif[0] = '\0';
246		}
247
248		if (db_expression(&addr)) {
249		    db_dot = (db_addr_t) addr;
250		    db_last_addr = db_dot;
251		    have_addr = TRUE;
252		}
253		else {
254		    addr = (db_expr_t) db_dot;
255		    have_addr = FALSE;
256		}
257		t = db_read_token();
258		if (t == tCOMMA) {
259		    if (!db_expression(&count)) {
260			db_printf("Count missing\n");
261			db_flush_lex();
262			return;
263		    }
264		}
265		else {
266		    db_unread_token(t);
267		    count = -1;
268		}
269		if ((cmd->flag & CS_MORE) == 0) {
270		    db_skip_to_eol();
271		}
272	    }
273	}
274	*last_cmdp = cmd;
275	if (cmd != 0) {
276	    /*
277	     * Execute the command.
278	     */
279	    (*cmd->fcn)(addr, have_addr, count, modif);
280
281	    if (cmd->flag & CS_SET_DOT) {
282		/*
283		 * If command changes dot, set dot to
284		 * previous address displayed (if 'ed' style).
285		 */
286		if (db_ed_style) {
287		    db_dot = db_prev;
288		}
289		else {
290		    db_dot = db_next;
291		}
292	    }
293	    else {
294		/*
295		 * If command does not change dot,
296		 * set 'next' location to be the same.
297		 */
298		db_next = db_dot;
299	    }
300	}
301}
302
303/*
304 * 'show' commands
305 */
306
307extern void	db_show_one_thread(), db_show_all_threads();
308extern void	vm_page_print();
309extern void	db_ps();
310extern void	ipc_port_print();
311void		db_show_help();
312void		db_panic();
313
314struct command db_show_all_cmds[] = {
315#if 0
316	{ "threads",	db_show_all_threads,	0,	0 },
317#endif
318	{ "procs",	db_ps,			0,	0 },
319	{ (char *)0 }
320};
321
322struct command db_show_cmds[] = {
323	{ "all",	0,			0,	db_show_all_cmds },
324	{ "registers",	db_show_regs,		0,	0 },
325	{ "breaks",	db_listbreak_cmd, 	0,	0 },
326	{ "watches",	db_listwatch_cmd, 	0,	0 },
327#if 0
328	{ "thread",	db_show_one_thread,	0,	0 },
329#endif
330	{ "map",	vm_map_print,		0,	0 },
331	{ "object",	vm_object_print,	0,	0 },
332#if 0
333	{ "page",	vm_page_print,		0,	0 },
334#endif
335#if 0
336	{ "port",	ipc_port_print,		0,	0 },
337#endif
338	{ (char *)0, }
339};
340
341void		db_help_cmd();
342void		db_fncall();
343
344struct command db_command_table[] = {
345	{ "print",	db_print_cmd,		0,	0 },
346	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
347	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
348	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
349	{ "set",	db_set_cmd,		CS_OWN,	0 },
350	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
351	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
352	{ "delete",	db_delete_cmd,		0,	0 },
353	{ "d",		db_delete_cmd,		0,	0 },
354	{ "break",	db_breakpoint_cmd,	0,	0 },
355	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
356	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
357	{ "step",	db_single_step_cmd,	0,	0 },
358	{ "s",		db_single_step_cmd,	0,	0 },
359	{ "continue",	db_continue_cmd,	0,	0 },
360	{ "c",		db_continue_cmd,	0,	0 },
361	{ "until",	db_trace_until_call_cmd,0,	0 },
362	{ "next",	db_trace_until_matching_cmd,0,	0 },
363	{ "match",	db_trace_until_matching_cmd,0,	0 },
364	{ "trace",	db_stack_trace_cmd,	0,	0 },
365	{ "call",	db_fncall,		CS_OWN,	0 },
366	{ "show",	0,			0,	db_show_cmds },
367	{ "ps",		db_ps,			0,	0 },
368	{ "panic",	db_panic,		0,	0 },
369	{ (char *)0, }
370};
371
372struct command	*db_last_command = 0;
373
374void
375db_help_cmd()
376{
377	struct command *cmd = db_command_table;
378
379	while (cmd->name != 0) {
380	    db_printf("%-12s", cmd->name);
381	    db_end_line();
382	    cmd++;
383	}
384}
385
386void
387db_panic()
388{
389	panic("from debugger\n");
390}
391
392void
393db_command_loop()
394{
395	/*
396	 * Initialize 'prev' and 'next' to dot.
397	 */
398	db_prev = db_dot;
399	db_next = db_dot;
400
401	db_cmd_loop_done = 0;
402	while (!db_cmd_loop_done) {
403
404	    (void) setjmp(db_jmpbuf);
405	    if (db_print_position() != 0)
406		db_printf("\n");
407
408	    db_printf("db> ");
409	    (void) db_read_line();
410
411	    db_command(&db_last_command, db_command_table);
412	}
413}
414
415void
416db_error(s)
417	char *s;
418{
419	if (s)
420	    db_printf(s);
421	db_flush_lex();
422	longjmp(db_jmpbuf, 1);
423}
424
425
426/*
427 * Call random function:
428 * !expr(arg,arg,arg)
429 */
430void
431db_fncall()
432{
433	db_expr_t	fn_addr;
434#define	MAXARGS		11
435	db_expr_t	args[MAXARGS];
436	int		nargs = 0;
437	db_expr_t	retval;
438	db_expr_t	(*func)();
439	int		t;
440
441	if (!db_expression(&fn_addr)) {
442	    db_printf("Bad function\n");
443	    db_flush_lex();
444	    return;
445	}
446	func = (db_expr_t (*) ()) fn_addr;
447
448	t = db_read_token();
449	if (t == tLPAREN) {
450	    if (db_expression(&args[0])) {
451		nargs++;
452		while ((t = db_read_token()) == tCOMMA) {
453		    if (nargs == MAXARGS) {
454			db_printf("Too many arguments\n");
455			db_flush_lex();
456			return;
457		    }
458		    if (!db_expression(&args[nargs])) {
459			db_printf("Argument missing\n");
460			db_flush_lex();
461			return;
462		    }
463		    nargs++;
464		}
465		db_unread_token(t);
466	    }
467	    if (db_read_token() != tRPAREN) {
468		db_printf("?\n");
469		db_flush_lex();
470		return;
471	    }
472	}
473	db_skip_to_eol();
474
475	while (nargs < MAXARGS) {
476	    args[nargs++] = 0;
477	}
478
479	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
480			 args[5], args[6], args[7], args[8], args[9] );
481	db_printf("%#n\n", retval);
482}
483