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