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