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