db_command.c revision 4
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/*
27 * HISTORY
28 * $Log: db_command.c,v $
29 *
30 * PATCHES MAGIC                LEVEL   PATCH THAT GOT US HERE
31 * --------------------         -----   ----------------------
32 * CURRENT PATCH LEVEL:         1       00081
33 * --------------------         -----   ----------------------
34 *
35 * 01 Feb 93	Julian Elischer		move strcmp to a more general
36 *					part of the kernel.
37 *
38 * Revision 1.1  1992/03/25  21:45:02  pace
39 * Initial revision
40 *
41 * Revision 2.6  91/02/05  17:06:10  mrt
42 * 	Changed to new Mach copyright
43 * 	[91/01/31  16:17:18  mrt]
44 *
45 * Revision 2.5  91/01/08  17:31:54  rpd
46 * 	Forward reference for db_fncall();
47 * 	[91/01/04  12:35:17  rvb]
48 *
49 * 	Add call as a synonym for ! and match for next
50 * 	[91/01/04  12:14:48  rvb]
51 *
52 * Revision 2.4  90/11/07  16:49:15  rpd
53 * 	Added search.
54 * 	[90/11/06            rpd]
55 *
56 * Revision 2.3  90/10/25  14:43:45  rwd
57 * 	Changed db_fncall to print the result unsigned.
58 * 	[90/10/19            rpd]
59 *
60 * 	Added CS_MORE to db_watchpoint_cmd.
61 * 	[90/10/17            rpd]
62 * 	Added watchpoint commands: watch, dwatch, show watches.
63 * 	[90/10/16            rpd]
64 *
65 * Revision 2.2  90/08/27  21:50:10  dbg
66 * 	Remove 'listbreaks' - use 'show breaks' instead.  Change 'show
67 * 	threads' to 'show all threads' to avoid clash with 'show thread'.
68 * 	Set 'dot' here from db_prev or db_next, depending on 'db_ed_style'
69 * 	flag and syntax table.
70 * 	[90/08/22            dbg]
71 * 	Reduce lint.
72 * 	[90/08/07            dbg]
73 * 	Created.
74 * 	[90/07/25            dbg]
75 *
76 */
77/*
78 *	Author: David B. Golub, Carnegie Mellon University
79 *	Date:	7/90
80 */
81/*
82 * Command dispatcher.
83 */
84#include "param.h"
85#include "proc.h"
86#include <machine/db_machdep.h>		/* type definitions */
87
88#include <ddb/db_lex.h>
89#include <ddb/db_output.h>
90
91#include <setjmp.h>
92
93/*
94 * Exported global variables
95 */
96boolean_t	db_cmd_loop_done;
97jmp_buf		db_jmpbuf;
98db_addr_t	db_dot;
99db_addr_t	db_last_addr;
100db_addr_t	db_prev;
101db_addr_t	db_next;
102
103/*
104 * if 'ed' style: 'dot' is set at start of last item printed,
105 * and '+' points to next line.
106 * Otherwise: 'dot' points to next item, '..' points to last.
107 */
108boolean_t	db_ed_style = TRUE;
109
110
111/*
112 * Utility routine - discard tokens through end-of-line.
113 */
114void
115db_skip_to_eol()
116{
117	int	t;
118	do {
119	    t = db_read_token();
120	} while (t != tEOL);
121}
122
123/*
124 * Command table
125 */
126struct command {
127	char *	name;		/* command name */
128	void	(*fcn)();	/* function to call */
129	int	flag;		/* extra info: */
130#define	CS_OWN		0x1	    /* non-standard syntax */
131#define	CS_MORE		0x2	    /* standard syntax, but may have other
132				       words at end */
133#define	CS_SET_DOT	0x100	    /* set dot after command */
134	struct command *more;	/* another level of command */
135};
136
137/*
138 * Results of command search.
139 */
140#define	CMD_UNIQUE	0
141#define	CMD_FOUND	1
142#define	CMD_NONE	2
143#define	CMD_AMBIGUOUS	3
144#define	CMD_HELP	4
145
146/*
147 * Search for command prefix.
148 */
149int
150db_cmd_search(name, table, cmdp)
151	char *		name;
152	struct command	*table;
153	struct command	**cmdp;	/* out */
154{
155	struct command	*cmd;
156	int		result = CMD_NONE;
157
158	for (cmd = table; cmd->name != 0; cmd++) {
159	    register char *lp;
160	    register char *rp;
161	    register int  c;
162
163	    lp = name;
164	    rp = cmd->name;
165	    while ((c = *lp) == *rp) {
166		if (c == 0) {
167		    /* complete match */
168		    *cmdp = cmd;
169		    return (CMD_UNIQUE);
170		}
171		lp++;
172		rp++;
173	    }
174	    if (c == 0) {
175		/* end of name, not end of command -
176		   partial match */
177		if (result == CMD_FOUND) {
178		    result = CMD_AMBIGUOUS;
179		    /* but keep looking for a full match -
180		       this lets us match single letters */
181		}
182		else {
183		    *cmdp = cmd;
184		    result = CMD_FOUND;
185		}
186	    }
187	}
188	if (result == CMD_NONE) {
189	    /* check for 'help' */
190		if (name[0] == 'h' && name[1] == 'e'
191		    && name[2] == 'l' && name[3] == 'p')
192			result = CMD_HELP;
193	}
194	return (result);
195}
196
197void
198db_cmd_list(table)
199	struct command *table;
200{
201	register struct command *cmd;
202
203	for (cmd = table; cmd->name != 0; cmd++) {
204	    db_printf("%-12s", cmd->name);
205	    db_end_line();
206	}
207}
208
209void
210db_command(last_cmdp, cmd_table)
211	struct command	**last_cmdp;	/* IN_OUT */
212	struct command	*cmd_table;
213{
214	struct command	*cmd;
215	int		t;
216	char		modif[TOK_STRING_SIZE];
217	db_expr_t	addr, count;
218	boolean_t	have_addr;
219	int		result;
220
221	t = db_read_token();
222	if (t == tEOL) {
223	    /* empty line repeats last command, at 'next' */
224	    cmd = *last_cmdp;
225	    addr = (db_expr_t)db_next;
226	    have_addr = FALSE;
227	    count = 1;
228	    modif[0] = '\0';
229	}
230	else if (t == tEXCL) {
231	    void db_fncall();
232	    db_fncall();
233	    return;
234	}
235	else if (t != tIDENT) {
236	    db_printf("?\n");
237	    db_flush_lex();
238	    return;
239	}
240	else {
241	    /*
242	     * Search for command
243	     */
244	    while (cmd_table) {
245		result = db_cmd_search(db_tok_string,
246				       cmd_table,
247				       &cmd);
248		switch (result) {
249		    case CMD_NONE:
250			db_printf("No such command\n");
251			db_flush_lex();
252			return;
253		    case CMD_AMBIGUOUS:
254			db_printf("Ambiguous\n");
255			db_flush_lex();
256			return;
257		    case CMD_HELP:
258			db_cmd_list(cmd_table);
259			db_flush_lex();
260			return;
261		    default:
262			break;
263		}
264		if ((cmd_table = cmd->more) != 0) {
265		    t = db_read_token();
266		    if (t != tIDENT) {
267			db_cmd_list(cmd_table);
268			db_flush_lex();
269			return;
270		    }
271		}
272	    }
273
274	    if ((cmd->flag & CS_OWN) == 0) {
275		/*
276		 * Standard syntax:
277		 * command [/modifier] [addr] [,count]
278		 */
279		t = db_read_token();
280		if (t == tSLASH) {
281		    t = db_read_token();
282		    if (t != tIDENT) {
283			db_printf("Bad modifier\n");
284			db_flush_lex();
285			return;
286		    }
287		    db_strcpy(modif, db_tok_string);
288		}
289		else {
290		    db_unread_token(t);
291		    modif[0] = '\0';
292		}
293
294		if (db_expression(&addr)) {
295		    db_dot = (db_addr_t) addr;
296		    db_last_addr = db_dot;
297		    have_addr = TRUE;
298		}
299		else {
300		    addr = (db_expr_t) db_dot;
301		    have_addr = FALSE;
302		}
303		t = db_read_token();
304		if (t == tCOMMA) {
305		    if (!db_expression(&count)) {
306			db_printf("Count missing\n");
307			db_flush_lex();
308			return;
309		    }
310		}
311		else {
312		    db_unread_token(t);
313		    count = -1;
314		}
315		if ((cmd->flag & CS_MORE) == 0) {
316		    db_skip_to_eol();
317		}
318	    }
319	}
320	*last_cmdp = cmd;
321	if (cmd != 0) {
322	    /*
323	     * Execute the command.
324	     */
325	    (*cmd->fcn)(addr, have_addr, count, modif);
326
327	    if (cmd->flag & CS_SET_DOT) {
328		/*
329		 * If command changes dot, set dot to
330		 * previous address displayed (if 'ed' style).
331		 */
332		if (db_ed_style) {
333		    db_dot = db_prev;
334		}
335		else {
336		    db_dot = db_next;
337		}
338	    }
339	    else {
340		/*
341		 * If command does not change dot,
342		 * set 'next' location to be the same.
343		 */
344		db_next = db_dot;
345	    }
346	}
347}
348
349/*
350 * 'show' commands
351 */
352extern void	db_listbreak_cmd();
353extern void	db_listwatch_cmd();
354extern void	db_show_regs(), db_show_one_thread(), db_show_all_threads();
355extern void	vm_map_print(), vm_object_print(), vm_page_print();
356extern void	ipc_port_print();
357void		db_show_help();
358
359struct command db_show_all_cmds[] = {
360#if 0
361	{ "threads",	db_show_all_threads,0,	0 },
362#endif
363	{ (char *)0 }
364};
365
366struct command db_show_cmds[] = {
367	{ "all",	0,			0,	db_show_all_cmds },
368	{ "registers",	db_show_regs,		0,	0 },
369	{ "breaks",	db_listbreak_cmd, 	0,	0 },
370	{ "watches",	db_listwatch_cmd, 	0,	0 },
371#if 0
372	{ "thread",	db_show_one_thread,	0,	0 },
373#endif
374	{ "map",	vm_map_print,		0,	0 },
375	{ "object",	vm_object_print,	0,	0 },
376#if 0
377	{ "page",	vm_page_print,		0,	0 },
378#endif
379#if 0
380	{ "port",	ipc_port_print,		0,	0 },
381#endif
382	{ (char *)0, }
383};
384
385extern void	db_print_cmd(), db_examine_cmd(), db_set_cmd();
386extern void	db_search_cmd();
387extern void	db_write_cmd();
388extern void	db_delete_cmd(), db_breakpoint_cmd();
389extern void	db_deletewatch_cmd(), db_watchpoint_cmd();
390extern void	db_single_step_cmd(), db_trace_until_call_cmd(),
391		db_trace_until_matching_cmd(), db_continue_cmd();
392extern void	db_stack_trace_cmd();
393void		db_help_cmd();
394void		db_fncall();
395
396struct command db_command_table[] = {
397	{ "print",	db_print_cmd,		0,	0 },
398	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
399	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
400	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
401	{ "set",	db_set_cmd,		CS_OWN,	0 },
402	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
403	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
404	{ "delete",	db_delete_cmd,		0,	0 },
405	{ "d",		db_delete_cmd,		0,	0 },
406	{ "break",	db_breakpoint_cmd,	0,	0 },
407	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
408	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
409	{ "step",	db_single_step_cmd,	0,	0 },
410	{ "s",		db_single_step_cmd,	0,	0 },
411	{ "continue",	db_continue_cmd,	0,	0 },
412	{ "c",		db_continue_cmd,	0,	0 },
413	{ "until",	db_trace_until_call_cmd,0,	0 },
414	{ "next",	db_trace_until_matching_cmd,0,	0 },
415	{ "match",	db_trace_until_matching_cmd,0,	0 },
416	{ "trace",	db_stack_trace_cmd,	0,	0 },
417	{ "call",	db_fncall,		CS_OWN,	0 },
418	{ "show",	0,			0,	db_show_cmds },
419	{ (char *)0, }
420};
421
422struct command	*db_last_command = 0;
423
424void
425db_help_cmd()
426{
427	struct command *cmd = db_command_table;
428
429	while (cmd->name != 0) {
430	    db_printf("%-12s", cmd->name);
431	    db_end_line();
432	    cmd++;
433	}
434}
435
436void
437db_command_loop()
438{
439	/*
440	 * Initialize 'prev' and 'next' to dot.
441	 */
442	db_prev = db_dot;
443	db_next = db_dot;
444
445	db_cmd_loop_done = 0;
446	while (!db_cmd_loop_done) {
447
448	    (void) setjmp(db_jmpbuf);
449	    if (db_print_position() != 0)
450		db_printf("\n");
451
452	    db_printf("db> ");
453	    (void) db_read_line();
454
455	    db_command(&db_last_command, db_command_table);
456	}
457}
458
459void
460db_error(s)
461	char *s;
462{
463	if (s)
464	    db_printf(s);
465	db_flush_lex();
466	longjmp(db_jmpbuf, 1);
467}
468
469
470/*
471 * Call random function:
472 * !expr(arg,arg,arg)
473 */
474void
475db_fncall()
476{
477	db_expr_t	fn_addr;
478#define	MAXARGS		11
479	db_expr_t	args[MAXARGS];
480	int		nargs = 0;
481	db_expr_t	retval;
482	db_expr_t	(*func)();
483	int		t;
484
485	if (!db_expression(&fn_addr)) {
486	    db_printf("Bad function\n");
487	    db_flush_lex();
488	    return;
489	}
490	func = (db_expr_t (*) ()) fn_addr;
491
492	t = db_read_token();
493	if (t == tLPAREN) {
494	    if (db_expression(&args[0])) {
495		nargs++;
496		while ((t = db_read_token()) == tCOMMA) {
497		    if (nargs == MAXARGS) {
498			db_printf("Too many arguments\n");
499			db_flush_lex();
500			return;
501		    }
502		    if (!db_expression(&args[nargs])) {
503			db_printf("Argument missing\n");
504			db_flush_lex();
505			return;
506		    }
507		    nargs++;
508		}
509		db_unread_token(t);
510	    }
511	    if (db_read_token() != tRPAREN) {
512		db_printf("?\n");
513		db_flush_lex();
514		return;
515	    }
516	}
517	db_skip_to_eol();
518
519	while (nargs < MAXARGS) {
520	    args[nargs++] = 0;
521	}
522
523	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
524			 args[5], args[6], args[7], args[8], args[9] );
525	db_printf("%#n\n", retval);
526}
527