xref: /netbsd-src/sys/ddb/db_command.c (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1 /*	$NetBSD: db_command.c,v 1.13 1994/10/09 08:29:59 mycroft Exp $	*/
2 
3 /*
4  * Mach Operating System
5  * Copyright (c) 1991,1990 Carnegie Mellon University
6  * All Rights Reserved.
7  *
8  * Permission to use, copy, modify and distribute this software and its
9  * documentation is hereby granted, provided that both the copyright
10  * notice and this permission notice appear in all copies of the
11  * software, derivative works or modified versions, and any portions
12  * thereof, and that both notices appear in supporting documentation.
13  *
14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17  *
18  * Carnegie Mellon requests users of this software to return to
19  *
20  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21  *  School of Computer Science
22  *  Carnegie Mellon University
23  *  Pittsburgh PA 15213-3890
24  *
25  * any improvements or extensions that they make and grant Carnegie the
26  * rights to redistribute these changes.
27  */
28 
29 /*
30  * Command dispatcher.
31  */
32 #include <sys/param.h>
33 #include <sys/proc.h>
34 
35 #include <machine/db_machdep.h>		/* type definitions */
36 
37 #include <ddb/db_lex.h>
38 #include <ddb/db_output.h>
39 #include <ddb/db_command.h>
40 
41 #include <setjmp.h>
42 
43 /*
44  * Exported global variables
45  */
46 boolean_t	db_cmd_loop_done;
47 jmp_buf		*db_recover;
48 
49 /*
50  * if 'ed' style: 'dot' is set at start of last item printed,
51  * and '+' points to next line.
52  * Otherwise: 'dot' points to next item, '..' points to last.
53  */
54 boolean_t	db_ed_style = TRUE;
55 
56 /*
57  * Utility routine - discard tokens through end-of-line.
58  */
59 void
60 db_skip_to_eol()
61 {
62 	int	t;
63 	do {
64 	    t = db_read_token();
65 	} while (t != tEOL);
66 }
67 
68 /*
69  * Results of command search.
70  */
71 #define	CMD_UNIQUE	0
72 #define	CMD_FOUND	1
73 #define	CMD_NONE	2
74 #define	CMD_AMBIGUOUS	3
75 #define	CMD_HELP	4
76 
77 /*
78  * Search for command prefix.
79  */
80 int
81 db_cmd_search(name, table, cmdp)
82 	char			*name;
83 	struct db_command	*table;
84 	struct db_command	**cmdp;	/* out */
85 {
86 	struct db_command	*cmd;
87 	int			result = CMD_NONE;
88 
89 	for (cmd = table; cmd->name != 0; cmd++) {
90 	    register char *lp;
91 	    register char *rp;
92 	    register int  c;
93 
94 	    lp = name;
95 	    rp = cmd->name;
96 	    while ((c = *lp) == *rp) {
97 		if (c == 0) {
98 		    /* complete match */
99 		    *cmdp = cmd;
100 		    return (CMD_UNIQUE);
101 		}
102 		lp++;
103 		rp++;
104 	    }
105 	    if (c == 0) {
106 		/* end of name, not end of command -
107 		   partial match */
108 		if (result == CMD_FOUND) {
109 		    result = CMD_AMBIGUOUS;
110 		    /* but keep looking for a full match -
111 		       this lets us match single letters */
112 		}
113 		else {
114 		    *cmdp = cmd;
115 		    result = CMD_FOUND;
116 		}
117 	    }
118 	}
119 	if (result == CMD_NONE) {
120 	    /* check for 'help' */
121 		if (name[0] == 'h' && name[1] == 'e'
122 		    && name[2] == 'l' && name[3] == 'p')
123 			result = CMD_HELP;
124 	}
125 	return (result);
126 }
127 
128 void
129 db_cmd_list(table)
130 	struct db_command *table;
131 {
132 	register struct db_command *cmd;
133 
134 	for (cmd = table; cmd->name != 0; cmd++) {
135 	    db_printf("%-12s", cmd->name);
136 	    db_end_line();
137 	}
138 }
139 
140 void
141 db_command(last_cmdp, cmd_table)
142 	struct db_command	**last_cmdp;	/* IN_OUT */
143 	struct db_command	*cmd_table;
144 {
145 	struct db_command	*cmd;
146 	int		t;
147 	char		modif[TOK_STRING_SIZE];
148 	db_expr_t	addr, count;
149 	boolean_t	have_addr;
150 	int		result;
151 
152 	t = db_read_token();
153 	if (t == tEOL) {
154 	    /* empty line repeats last command, at 'next' */
155 	    cmd = *last_cmdp;
156 	    addr = (db_expr_t)db_next;
157 	    have_addr = FALSE;
158 	    count = 1;
159 	    modif[0] = '\0';
160 	}
161 	else if (t == tEXCL) {
162 	    void db_fncall();
163 	    db_fncall();
164 	    return;
165 	}
166 	else if (t != tIDENT) {
167 	    db_printf("?\n");
168 	    db_flush_lex();
169 	    return;
170 	}
171 	else {
172 	    /*
173 	     * Search for command
174 	     */
175 	    while (cmd_table) {
176 		result = db_cmd_search(db_tok_string,
177 				       cmd_table,
178 				       &cmd);
179 		switch (result) {
180 		    case CMD_NONE:
181 			db_printf("No such command\n");
182 			db_flush_lex();
183 			return;
184 		    case CMD_AMBIGUOUS:
185 			db_printf("Ambiguous\n");
186 			db_flush_lex();
187 			return;
188 		    case CMD_HELP:
189 			db_cmd_list(cmd_table);
190 			db_flush_lex();
191 			return;
192 		    default:
193 			break;
194 		}
195 		if ((cmd_table = cmd->more) != 0) {
196 		    t = db_read_token();
197 		    if (t != tIDENT) {
198 			db_cmd_list(cmd_table);
199 			db_flush_lex();
200 			return;
201 		    }
202 		}
203 	    }
204 
205 	    if ((cmd->flag & CS_OWN) == 0) {
206 		/*
207 		 * Standard syntax:
208 		 * command [/modifier] [addr] [,count]
209 		 */
210 		t = db_read_token();
211 		if (t == tSLASH) {
212 		    t = db_read_token();
213 		    if (t != tIDENT) {
214 			db_printf("Bad modifier\n");
215 			db_flush_lex();
216 			return;
217 		    }
218 		    db_strcpy(modif, db_tok_string);
219 		}
220 		else {
221 		    db_unread_token(t);
222 		    modif[0] = '\0';
223 		}
224 
225 		if (db_expression(&addr)) {
226 		    db_dot = (db_addr_t) addr;
227 		    db_last_addr = db_dot;
228 		    have_addr = TRUE;
229 		}
230 		else {
231 		    addr = (db_expr_t) db_dot;
232 		    have_addr = FALSE;
233 		}
234 		t = db_read_token();
235 		if (t == tCOMMA) {
236 		    if (!db_expression(&count)) {
237 			db_printf("Count missing\n");
238 			db_flush_lex();
239 			return;
240 		    }
241 		}
242 		else {
243 		    db_unread_token(t);
244 		    count = -1;
245 		}
246 		if ((cmd->flag & CS_MORE) == 0) {
247 		    db_skip_to_eol();
248 		}
249 	    }
250 	}
251 	*last_cmdp = cmd;
252 	if (cmd != 0) {
253 	    /*
254 	     * Execute the command.
255 	     */
256 	    (*cmd->fcn)(addr, have_addr, count, modif);
257 
258 	    if (cmd->flag & CS_SET_DOT) {
259 		/*
260 		 * If command changes dot, set dot to
261 		 * previous address displayed (if 'ed' style).
262 		 */
263 		if (db_ed_style) {
264 		    db_dot = db_prev;
265 		}
266 		else {
267 		    db_dot = db_next;
268 		}
269 	    }
270 	    else {
271 		/*
272 		 * If command does not change dot,
273 		 * set 'next' location to be the same.
274 		 */
275 		db_next = db_dot;
276 	    }
277 	}
278 }
279 
280 /*ARGSUSED*/
281 void
282 db_map_print_cmd(addr, have_addr, count, modif)
283 	db_expr_t	addr;
284 	int		have_addr;
285 	db_expr_t	count;
286 	char *		modif;
287 {
288         extern void	_vm_map_print();
289         boolean_t full = FALSE;
290 
291         if (modif[0] == 'f')
292                 full = TRUE;
293 
294         _vm_map_print(addr, full, db_printf);
295 }
296 
297 /*ARGSUSED*/
298 void
299 db_object_print_cmd(addr, have_addr, count, modif)
300 	db_expr_t	addr;
301 	int		have_addr;
302 	db_expr_t	count;
303 	char *		modif;
304 {
305         extern void	_vm_object_print();
306         boolean_t full = FALSE;
307 
308         if (modif[0] == 'f')
309                 full = TRUE;
310 
311         _vm_object_print(addr, full, db_printf);
312 }
313 
314 /*
315  * 'show' commands
316  */
317 extern void	db_show_all_procs();
318 extern void	db_show_callout();
319 extern void	db_listbreak_cmd();
320 extern void	db_listwatch_cmd();
321 extern void	db_show_regs();
322 void		db_show_help();
323 
324 struct db_command db_show_all_cmds[] = {
325 	{ "procs",	db_show_all_procs,0,	0 },
326 	{ "callout",	db_show_callout,0,	0 },
327 	{ (char *)0 }
328 };
329 
330 struct db_command db_show_cmds[] = {
331 	{ "all",	0,			0,	db_show_all_cmds },
332 	{ "registers",	db_show_regs,		0,	0 },
333 	{ "breaks",	db_listbreak_cmd, 	0,	0 },
334 	{ "watches",	db_listwatch_cmd, 	0,	0 },
335 	{ "map",	db_map_print_cmd,	0,	0 },
336 	{ "object",	db_object_print_cmd,	0,	0 },
337 	{ (char *)0, }
338 };
339 
340 extern void	db_print_cmd(), db_examine_cmd(), db_set_cmd();
341 extern void	db_search_cmd();
342 extern void	db_write_cmd();
343 extern void	db_delete_cmd(), db_breakpoint_cmd();
344 extern void	db_deletewatch_cmd(), db_watchpoint_cmd();
345 extern void	db_single_step_cmd(), db_trace_until_call_cmd(),
346 		db_trace_until_matching_cmd(), db_continue_cmd();
347 extern void	db_stack_trace_cmd();
348 void		db_help_cmd();
349 void		db_fncall();
350 
351 struct db_command db_command_table[] = {
352 #ifdef DB_MACHINE_COMMANDS
353   /* this must be the first entry, if it exists */
354 	{ "machine",    0,                      0,     		0},
355 #endif
356 	{ "print",	db_print_cmd,		0,		0 },
357 	{ "examine",	db_examine_cmd,		CS_SET_DOT, 	0 },
358 	{ "x",		db_examine_cmd,		CS_SET_DOT, 	0 },
359 	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
360 	{ "set",	db_set_cmd,		CS_OWN,		0 },
361 	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
362 	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
363 	{ "delete",	db_delete_cmd,		0,		0 },
364 	{ "d",		db_delete_cmd,		0,		0 },
365 	{ "break",	db_breakpoint_cmd,	0,		0 },
366 	{ "dwatch",	db_deletewatch_cmd,	0,		0 },
367 	{ "watch",	db_watchpoint_cmd,	CS_MORE,	0 },
368 	{ "step",	db_single_step_cmd,	0,		0 },
369 	{ "s",		db_single_step_cmd,	0,		0 },
370 	{ "continue",	db_continue_cmd,	0,		0 },
371 	{ "c",		db_continue_cmd,	0,		0 },
372 	{ "until",	db_trace_until_call_cmd,0,		0 },
373 	{ "next",	db_trace_until_matching_cmd,0,		0 },
374 	{ "match",	db_trace_until_matching_cmd,0,		0 },
375 	{ "trace",	db_stack_trace_cmd,	0,		0 },
376 	{ "call",	db_fncall,		CS_OWN,		0 },
377 	{ "ps",		db_show_all_procs,	0,		0 },
378 	{ "callout",	db_show_callout,	0,		0 },
379 	{ "show",	0,			0,		db_show_cmds },
380 	{ (char *)0, }
381 };
382 
383 #ifdef DB_MACHINE_COMMANDS
384 
385 /* this function should be called to install the machine dependent
386    commands. It should be called before the debugger is enabled  */
387 void db_machine_commands_install(ptr)
388 struct db_command *ptr;
389 {
390   db_command_table[0].more = ptr;
391   return;
392 }
393 
394 #endif
395 
396 struct db_command	*db_last_command = 0;
397 
398 void
399 db_help_cmd()
400 {
401 	struct db_command *cmd = db_command_table;
402 
403 	while (cmd->name != 0) {
404 	    db_printf("%-12s", cmd->name);
405 	    db_end_line();
406 	    cmd++;
407 	}
408 }
409 
410 void
411 db_command_loop()
412 {
413 	jmp_buf		db_jmpbuf;
414 	jmp_buf		*savejmp = db_recover;
415 	extern int	db_output_line;
416 
417 	/*
418 	 * Initialize 'prev' and 'next' to dot.
419 	 */
420 	db_prev = db_dot;
421 	db_next = db_dot;
422 
423 	db_cmd_loop_done = 0;
424 	(void) setjmp(*(db_recover = &db_jmpbuf));
425 
426 	while (!db_cmd_loop_done) {
427 		if (db_print_position() != 0)
428 			db_printf("\n");
429 		db_output_line = 0;
430 
431 		db_printf("db> ");
432 		(void) db_read_line();
433 
434 		db_command(&db_last_command, db_command_table);
435 	}
436 
437 	db_recover = savejmp;
438 }
439 
440 void
441 db_error(s)
442 	char *s;
443 {
444 	if (s)
445 	    db_printf(s);
446 	db_flush_lex();
447 	longjmp(*db_recover, 1);
448 }
449 
450 
451 /*
452  * Call random function:
453  * !expr(arg,arg,arg)
454  */
455 void
456 db_fncall()
457 {
458 	db_expr_t	fn_addr;
459 #define	MAXARGS		11
460 	db_expr_t	args[MAXARGS];
461 	int		nargs = 0;
462 	db_expr_t	retval;
463 	db_expr_t	(*func)();
464 	int		t;
465 
466 	if (!db_expression(&fn_addr)) {
467 	    db_printf("Bad function\n");
468 	    db_flush_lex();
469 	    return;
470 	}
471 	func = (db_expr_t (*) ()) fn_addr;
472 
473 	t = db_read_token();
474 	if (t == tLPAREN) {
475 	    if (db_expression(&args[0])) {
476 		nargs++;
477 		while ((t = db_read_token()) == tCOMMA) {
478 		    if (nargs == MAXARGS) {
479 			db_printf("Too many arguments\n");
480 			db_flush_lex();
481 			return;
482 		    }
483 		    if (!db_expression(&args[nargs])) {
484 			db_printf("Argument missing\n");
485 			db_flush_lex();
486 			return;
487 		    }
488 		    nargs++;
489 		}
490 		db_unread_token(t);
491 	    }
492 	    if (db_read_token() != tRPAREN) {
493 		db_printf("?\n");
494 		db_flush_lex();
495 		return;
496 	    }
497 	}
498 	db_skip_to_eol();
499 
500 	while (nargs < MAXARGS) {
501 	    args[nargs++] = 0;
502 	}
503 
504 	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
505 			 args[5], args[6], args[7], args[8], args[9] );
506 	db_printf("%#n\n", retval);
507 }
508