xref: /netbsd-src/sys/ddb/db_command.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: db_command.c,v 1.63 2001/11/12 22:54:04 lukem 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 "AS IS"
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 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.63 2001/11/12 22:54:04 lukem Exp $");
35 
36 #include "opt_ddb.h"
37 #include "opt_inet.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/reboot.h>
42 #include <sys/proc.h>
43 #include <sys/vnode.h>
44 #include <sys/pool.h>
45 #include <sys/namei.h>
46 #include <sys/malloc.h>
47 
48 #include <machine/db_machdep.h>		/* type definitions */
49 
50 #if defined(_KERNEL_OPT)
51 #include "opt_multiprocessor.h"
52 #endif
53 #ifdef MULTIPROCESSOR
54 #include <machine/cpu.h>
55 #endif
56 
57 #include <ddb/db_lex.h>
58 #include <ddb/db_output.h>
59 #include <ddb/db_command.h>
60 #include <ddb/db_break.h>
61 #include <ddb/db_watch.h>
62 #include <ddb/db_run.h>
63 #include <ddb/db_variables.h>
64 #include <ddb/db_interface.h>
65 #include <ddb/db_sym.h>
66 #include <ddb/db_extern.h>
67 
68 #include <uvm/uvm_extern.h>
69 #include <uvm/uvm_ddb.h>
70 
71 #include "arp.h"
72 
73 /*
74  * Exported global variables
75  */
76 boolean_t	db_cmd_loop_done;
77 label_t		*db_recover;
78 
79 /*
80  * if 'ed' style: 'dot' is set at start of last item printed,
81  * and '+' points to next line.
82  * Otherwise: 'dot' points to next item, '..' points to last.
83  */
84 boolean_t	db_ed_style = TRUE;
85 
86 /*
87  * Utility routine - discard tokens through end-of-line.
88  */
89 void
90 db_skip_to_eol()
91 {
92 	int	t;
93 	do {
94 	    t = db_read_token();
95 	} while (t != tEOL);
96 }
97 
98 /*
99  * Results of command search.
100  */
101 #define	CMD_UNIQUE	0
102 #define	CMD_FOUND	1
103 #define	CMD_NONE	2
104 #define	CMD_AMBIGUOUS	3
105 #define	CMD_HELP	4
106 
107 /*
108  * Search for command prefix.
109  */
110 int
111 db_cmd_search(name, table, cmdp)
112 	const char		*name;
113 	const struct db_command	*table;
114 	const struct db_command	**cmdp;	/* out */
115 {
116 	const struct db_command	*cmd;
117 	int			result = CMD_NONE;
118 
119 	for (cmd = table; cmd->name != 0; cmd++) {
120 	    const char *lp;
121 	    const char *rp;
122 	    int  c;
123 
124 	    lp = name;
125 	    rp = cmd->name;
126 	    while ((c = *lp) == *rp) {
127 		if (c == 0) {
128 		    /* complete match */
129 		    *cmdp = cmd;
130 		    return (CMD_UNIQUE);
131 		}
132 		lp++;
133 		rp++;
134 	    }
135 	    if (c == 0) {
136 		/* end of name, not end of command -
137 		   partial match */
138 		if (result == CMD_FOUND) {
139 		    result = CMD_AMBIGUOUS;
140 		    /* but keep looking for a full match -
141 		       this lets us match single letters */
142 		}
143 		else {
144 		    *cmdp = cmd;
145 		    result = CMD_FOUND;
146 		}
147 	    }
148 	}
149 	if (result == CMD_NONE) {
150 	    /* check for 'help' */
151 		if (name[0] == 'h' && name[1] == 'e'
152 		    && name[2] == 'l' && name[3] == 'p')
153 			result = CMD_HELP;
154 	}
155 	return (result);
156 }
157 
158 void
159 db_cmd_list(table)
160 	const struct db_command *table;
161 {
162 	int	 i, j, w, columns, lines, width=0, items, numcmds;
163 	const char	*p;
164 
165 	for (numcmds = 0; table[numcmds].name != NULL; numcmds++) {
166 		w = strlen(table[numcmds].name);
167 		if (w > width)
168 			width = w;
169 	}
170 	width = DB_NEXT_TAB(width);
171 	items = 0;
172 
173 	columns = db_max_width / width;
174 	if (columns == 0)
175 		columns = 1;
176 	lines = (numcmds + columns - 1) / columns;
177 	for (i = 0; i < lines; i++) {
178 		for (j = 0; j < columns; j++) {
179 			p = table[j * lines + i].name;
180 			if (p)
181 				db_printf("%s", p);
182 			if (j * lines + i + lines >= numcmds) {
183 				db_putchar('\n');
184 				break;
185 			}
186 			w = strlen(p);
187 			while (w < width) {
188 				w = DB_NEXT_TAB(w);
189 				db_putchar('\t');
190 			}
191 		}
192 	}
193 }
194 
195 void
196 db_command(last_cmdp, cmd_table)
197 	const struct db_command	**last_cmdp;	/* IN_OUT */
198 	const struct db_command	*cmd_table;
199 {
200 	const struct db_command	*cmd;
201 	int		t;
202 	char		modif[TOK_STRING_SIZE];
203 	db_expr_t	addr, count;
204 	boolean_t	have_addr = FALSE;
205 	int		result;
206 
207 	static db_expr_t               last_count = 0;
208 
209 	t = db_read_token();
210 	if ((t == tEOL) || (t == tCOMMA)) {
211 	    /*
212 	     * An empty line repeats last command, at 'next'.
213 	     * Only a count repeats the last command with the new count.
214 	     */
215 	    cmd = *last_cmdp;
216 	    addr = (db_expr_t)db_next;
217 	    if (t == tCOMMA) {
218 	            if (!db_expression(&count)) {
219 			    db_printf("Count missing\n");
220 			    db_flush_lex();
221 			    return;
222 		    }
223 	    } else
224 		    count = last_count;
225 	    have_addr = FALSE;
226 	    modif[0] = '\0';
227 	    db_skip_to_eol();
228 	}
229 	else if (t == tEXCL) {
230 	    db_fncall(0, 0, 0, NULL);
231 	    return;
232 	}
233 	else if (t != tIDENT) {
234 	    db_printf("?\n");
235 	    db_flush_lex();
236 	    return;
237 	}
238 	else {
239 	    /*
240 	     * Search for command
241 	     */
242 	    while (cmd_table) {
243 		result = db_cmd_search(db_tok_string,
244 				       cmd_table,
245 				       &cmd);
246 		switch (result) {
247 		    case CMD_NONE:
248 			db_printf("No such command\n");
249 			db_flush_lex();
250 			return;
251 		    case CMD_AMBIGUOUS:
252 			db_printf("Ambiguous\n");
253 			db_flush_lex();
254 			return;
255 		    case CMD_HELP:
256 			db_cmd_list(cmd_table);
257 			db_flush_lex();
258 			return;
259 		    default:
260 			break;
261 		}
262 		if ((cmd_table = cmd->more) != 0) {
263 		    t = db_read_token();
264 		    if (t != tIDENT) {
265 			db_cmd_list(cmd_table);
266 			db_flush_lex();
267 			return;
268 		    }
269 		}
270 	    }
271 
272 	    if ((cmd->flag & CS_OWN) == 0) {
273 		/*
274 		 * Standard syntax:
275 		 * command [/modifier] [addr] [,count]
276 		 */
277 		t = db_read_token();
278 		if (t == tSLASH) {
279 		    t = db_read_token();
280 		    if (t != tIDENT) {
281 			db_printf("Bad modifier\n");
282 			db_flush_lex();
283 			return;
284 		    }
285 		    db_strcpy(modif, db_tok_string);
286 		}
287 		else {
288 		    db_unread_token(t);
289 		    modif[0] = '\0';
290 		}
291 
292 		if (db_expression(&addr)) {
293 		    db_dot = (db_addr_t) addr;
294 		    db_last_addr = db_dot;
295 		    have_addr = TRUE;
296 		}
297 		else {
298 		    addr = (db_expr_t) db_dot;
299 		    have_addr = FALSE;
300 		}
301 		t = db_read_token();
302 		if (t == tCOMMA) {
303 		    if (!db_expression(&count)) {
304 			db_printf("Count missing\n");
305 			db_flush_lex();
306 			return;
307 		    }
308 		}
309 		else {
310 		    db_unread_token(t);
311 		    count = -1;
312 		}
313 		if ((cmd->flag & CS_MORE) == 0) {
314 		    db_skip_to_eol();
315 		}
316 	    }
317 	}
318 	*last_cmdp = cmd;
319 	last_count = count;
320 	if (cmd != 0) {
321 	    /*
322 	     * Execute the command.
323 	     */
324 	    (*cmd->fcn)(addr, have_addr, count, modif);
325 
326 	    if (cmd->flag & CS_SET_DOT) {
327 		/*
328 		 * If command changes dot, set dot to
329 		 * previous address displayed (if 'ed' style).
330 		 */
331 		if (db_ed_style) {
332 		    db_dot = db_prev;
333 		}
334 		else {
335 		    db_dot = db_next;
336 		}
337 	    }
338 	    else {
339 		/*
340 		 * If command does not change dot,
341 		 * set 'next' location to be the same.
342 		 */
343 		db_next = db_dot;
344 	    }
345 	}
346 }
347 
348 /*ARGSUSED*/
349 void
350 db_map_print_cmd(addr, have_addr, count, modif)
351 	db_expr_t	addr;
352 	int		have_addr;
353 	db_expr_t	count;
354 	char *		modif;
355 {
356         boolean_t full = FALSE;
357 
358         if (modif[0] == 'f')
359                 full = TRUE;
360 
361 	if (have_addr == FALSE)
362 		addr = (db_expr_t) kernel_map;
363 
364         uvm_map_printit((struct vm_map *) addr, full, db_printf);
365 }
366 
367 /*ARGSUSED*/
368 void
369 db_malloc_print_cmd(addr, have_addr, count, modif)
370 	db_expr_t	addr;
371 	int		have_addr;
372 	db_expr_t	count;
373 	char *		modif;
374 {
375 #ifdef MALLOC_DEBUG
376 	if (!have_addr)
377 		addr = 0;
378 
379 	debug_malloc_printit(db_printf, (vaddr_t) addr);
380 #else
381 	db_printf("The kernel is not built with the MALLOC_DEBUG option.\n");
382 #endif /* MALLOC_DEBUG */
383 }
384 
385 /*ARGSUSED*/
386 void
387 db_object_print_cmd(addr, have_addr, count, modif)
388 	db_expr_t	addr;
389 	int		have_addr;
390 	db_expr_t	count;
391 	char *		modif;
392 {
393         boolean_t full = FALSE;
394 
395         if (modif[0] == 'f')
396                 full = TRUE;
397 
398 	uvm_object_printit((struct uvm_object *) addr, full, db_printf);
399 }
400 
401 /*ARGSUSED*/
402 void
403 db_page_print_cmd(addr, have_addr, count, modif)
404 	db_expr_t	addr;
405 	int		have_addr;
406 	db_expr_t	count;
407 	char *		modif;
408 {
409         boolean_t full = FALSE;
410 
411         if (modif[0] == 'f')
412                 full = TRUE;
413 
414 	uvm_page_printit((struct vm_page *) addr, full, db_printf);
415 }
416 
417 /*ARGSUSED*/
418 void
419 db_buf_print_cmd(addr, have_addr, count, modif)
420 	db_expr_t	addr;
421 	int		have_addr;
422 	db_expr_t	count;
423 	char *		modif;
424 {
425 	boolean_t full = FALSE;
426 
427 	if (modif[0] == 'f')
428 		full = TRUE;
429 
430 	vfs_buf_print((struct buf *)addr, full, db_printf);
431 }
432 
433 /*ARGSUSED*/
434 void
435 db_vnode_print_cmd(addr, have_addr, count, modif)
436 	db_expr_t	addr;
437 	int		have_addr;
438 	db_expr_t	count;
439 	char *		modif;
440 {
441 	boolean_t full = FALSE;
442 
443 	if (modif[0] == 'f')
444 		full = TRUE;
445 
446 	vfs_vnode_print((struct vnode *)addr, full, db_printf);
447 }
448 
449 /*ARGSUSED*/
450 void
451 db_pool_print_cmd(addr, have_addr, count, modif)
452 	db_expr_t	addr;
453 	int		have_addr;
454 	db_expr_t	count;
455 	char *		modif;
456 {
457 	pool_printit((struct pool *)addr, modif, db_printf);
458 }
459 
460 /*ARGSUSED*/
461 void
462 db_namecache_print_cmd(addr, have_addr, count, modif)
463 	db_expr_t	addr;
464 	int		have_addr;
465 	db_expr_t	count;
466 	char *		modif;
467 {
468 	namecache_print((struct vnode *)addr, db_printf);
469 }
470 
471 /*ARGSUSED*/
472 void
473 db_uvmexp_print_cmd(addr, have_addr, count, modif)
474 	db_expr_t	addr;
475 	int		have_addr;
476 	db_expr_t	count;
477 	char *		modif;
478 {
479 	uvmexp_print(db_printf);
480 }
481 
482 /*
483  * 'show' commands
484  */
485 
486 static const struct db_command db_show_all_cmds[] = {
487 	{ "callout",	db_show_callout,	0, NULL },
488 	{ "procs",	db_show_all_procs,	0, NULL },
489 	{ NULL, 	NULL, 			0, NULL }
490 };
491 
492 static const struct db_command db_show_cmds[] = {
493 	{ "all",	NULL,			0,	db_show_all_cmds },
494 #if defined(INET) && (NARP > 0)
495 	{ "arptab",	db_show_arptab,		0,	NULL },
496 #endif
497 	{ "breaks",	db_listbreak_cmd, 	0,	NULL },
498 	{ "buf",	db_buf_print_cmd,	0,	NULL },
499 	{ "map",	db_map_print_cmd,	0,	NULL },
500 	{ "ncache",	db_namecache_print_cmd,	0,	NULL },
501 	{ "object",	db_object_print_cmd,	0,	NULL },
502 	{ "page",	db_page_print_cmd,	0,	NULL },
503 	{ "pool",	db_pool_print_cmd,	0,	NULL },
504 	{ "registers",	db_show_regs,		0,	NULL },
505 	{ "uvmexp",	db_uvmexp_print_cmd,	0,	NULL },
506 	{ "vnode",	db_vnode_print_cmd,	0,	NULL },
507 	{ "watches",	db_listwatch_cmd, 	0,	NULL },
508 	{ "malloc",	db_malloc_print_cmd,	0,	NULL },
509 	{ NULL,		NULL,			0,	NULL }
510 };
511 
512 static const struct db_command db_command_table[] = {
513 	{ "break",	db_breakpoint_cmd,	0,		NULL },
514 	{ "c",		db_continue_cmd,	0,		NULL },
515 	{ "call",	db_fncall,		CS_OWN,		NULL },
516 	{ "callout",	db_show_callout,	0,		NULL },
517 	{ "continue",	db_continue_cmd,	0,		NULL },
518 	{ "d",		db_delete_cmd,		0,		NULL },
519 	{ "delete",	db_delete_cmd,		0,		NULL },
520 	{ "dmesg",	db_dmesg,		0,		NULL },
521 	{ "dwatch",	db_deletewatch_cmd,	0,		NULL },
522 	{ "examine",	db_examine_cmd,		CS_SET_DOT, 	NULL },
523 	{ "kill",	db_kill_proc,		CS_OWN,		NULL },
524 #ifdef DB_MACHINE_COMMANDS
525 	{ "machine",    NULL,                   0, db_machine_command_table },
526 #endif
527 	{ "match",	db_trace_until_matching_cmd,0,		NULL },
528 	{ "next",	db_trace_until_matching_cmd,0,		NULL },
529 	{ "p",		db_print_cmd,		0,		NULL },
530 	{ "print",	db_print_cmd,		0,		NULL },
531 	{ "ps",		db_show_all_procs,	0,		NULL },
532 	{ "reboot",	db_reboot_cmd,		CS_OWN,		NULL },
533 	{ "s",		db_single_step_cmd,	0,		NULL },
534 	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, NULL },
535 	{ "set",	db_set_cmd,		CS_OWN,		NULL },
536 	{ "show",	NULL,			0,		db_show_cmds },
537 	{ "sifting",    db_sifting_cmd,		CS_OWN,		NULL },
538 	{ "step",	db_single_step_cmd,	0,		NULL },
539 	{ "sync",	db_sync_cmd,		CS_OWN,		NULL },
540 	{ "trace",	db_stack_trace_cmd,	0,		NULL },
541 	{ "until",	db_trace_until_call_cmd,0,		NULL },
542 	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
543 	{ "watch",	db_watchpoint_cmd,	CS_MORE,	NULL },
544 	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
545 	{ "x",		db_examine_cmd,		CS_SET_DOT, 	NULL },
546 	{ NULL, 	NULL,			0,		NULL }
547 };
548 
549 const struct db_command	*db_last_command = NULL;
550 
551 void
552 db_command_loop()
553 {
554 	label_t		db_jmpbuf;
555 	label_t		*savejmp;
556 	extern int	db_output_line;
557 
558 	/*
559 	 * Initialize 'prev' and 'next' to dot.
560 	 */
561 	db_prev = db_dot;
562 	db_next = db_dot;
563 
564 	db_cmd_loop_done = 0;
565 
566 	savejmp = db_recover;
567 	db_recover = &db_jmpbuf;
568 	(void) setjmp(&db_jmpbuf);
569 
570 	while (!db_cmd_loop_done) {
571 		if (db_print_position() != 0)
572 			db_printf("\n");
573 		db_output_line = 0;
574 
575 
576 #ifdef MULTIPROCESSOR
577 		db_printf("db{%ld}> ", (long)cpu_number());
578 #else
579 		db_printf("db> ");
580 #endif
581 		(void) db_read_line();
582 
583 		db_command(&db_last_command, db_command_table);
584 	}
585 
586 	db_recover = savejmp;
587 }
588 
589 void
590 db_error(s)
591 	char *s;
592 {
593 	if (s)
594 	    db_printf("%s", s);
595 	db_flush_lex();
596 	longjmp(db_recover);
597 }
598 
599 
600 /*
601  * Call random function:
602  * !expr(arg,arg,arg)
603  */
604 /*ARGSUSED*/
605 void
606 db_fncall(addr, have_addr, count, modif)
607 	db_expr_t	addr;
608 	int		have_addr;
609 	db_expr_t	count;
610 	char *		modif;
611 {
612 	db_expr_t	fn_addr;
613 #define	MAXARGS		11
614 	db_expr_t	args[MAXARGS];
615 	int		nargs = 0;
616 	db_expr_t	retval;
617 	db_expr_t	(*func) __P((db_expr_t, ...));
618 	int		t;
619 
620 	if (!db_expression(&fn_addr)) {
621 	    db_printf("Bad function\n");
622 	    db_flush_lex();
623 	    return;
624 	}
625 	func = (db_expr_t (*) __P((db_expr_t, ...))) fn_addr;
626 
627 	t = db_read_token();
628 	if (t == tLPAREN) {
629 	    if (db_expression(&args[0])) {
630 		nargs++;
631 		while ((t = db_read_token()) == tCOMMA) {
632 		    if (nargs == MAXARGS) {
633 			db_printf("Too many arguments\n");
634 			db_flush_lex();
635 			return;
636 		    }
637 		    if (!db_expression(&args[nargs])) {
638 			db_printf("Argument missing\n");
639 			db_flush_lex();
640 			return;
641 		    }
642 		    nargs++;
643 		}
644 		db_unread_token(t);
645 	    }
646 	    if (db_read_token() != tRPAREN) {
647 		db_printf("?\n");
648 		db_flush_lex();
649 		return;
650 	    }
651 	}
652 	db_skip_to_eol();
653 
654 	while (nargs < MAXARGS) {
655 	    args[nargs++] = 0;
656 	}
657 
658 	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
659 			 args[5], args[6], args[7], args[8], args[9]);
660 	db_printf("%s\n", db_num_to_str(retval));
661 }
662 
663 void
664 db_reboot_cmd(addr, have_addr, count, modif)
665 	db_expr_t	addr;
666 	int		have_addr;
667 	db_expr_t	count;
668 	char *		modif;
669 {
670 	db_expr_t bootflags;
671 
672 	/* Flags, default to RB_AUTOBOOT */
673 	if (!db_expression(&bootflags))
674 		bootflags = (db_expr_t)RB_AUTOBOOT;
675 	if (db_read_token() != tEOL) {
676 	    db_error("?\n");
677 	    /*NOTREACHED*/
678 	}
679 	/*
680 	 * We are leaving DDB, never to return upward.
681 	 * Clear db_recover so that we can debug faults in functions
682 	 * called from cpu_reboot.
683 	 */
684 	db_recover = 0;
685 	cpu_reboot((int)bootflags, NULL);
686 }
687 
688 void
689 db_sifting_cmd(addr, have_addr, count, omodif)
690 	db_expr_t	addr;
691 	int		have_addr;
692 	db_expr_t	count;
693 	char *		omodif;
694 {
695 	int	mode, t;
696 
697 	t = db_read_token();
698 	if (t == tSLASH) {
699 		t = db_read_token();
700 		if (t != tIDENT) {
701 			bad_modifier:
702 			db_printf("Bad modifier\n");
703 			db_flush_lex();
704 			return;
705 		}
706 		if (!strcmp(db_tok_string, "F"))
707 			mode = 'F';
708 		else
709 			goto bad_modifier;
710 		t = db_read_token();
711 	} else
712 		mode = 0;
713 
714 	if (t == tIDENT)
715 		db_sifting(db_tok_string, mode);
716 	else {
717 		db_printf("Bad argument (non-string)\n");
718 		db_flush_lex();
719 	}
720 }
721 
722 void
723 db_stack_trace_cmd(addr, have_addr, count, modif)
724 	db_expr_t	addr;
725 	boolean_t	have_addr;
726 	db_expr_t	count;
727 	char		*modif;
728 {
729 	if (count == -1)
730 		count = 65535;
731 
732 	db_stack_trace_print(addr, have_addr, count, modif, db_printf);
733 }
734 
735 void
736 db_sync_cmd(addr, have_addr, count, modif)
737 	db_expr_t	addr;
738 	int		have_addr;
739 	db_expr_t	count;
740 	char *		modif;
741 {
742 	/*
743 	 * We are leaving DDB, never to return upward.
744 	 * Clear db_recover so that we can debug faults in functions
745 	 * called from cpu_reboot.
746 	 */
747 	db_recover = 0;
748 	cpu_reboot(RB_DUMP, NULL);
749 }
750