xref: /netbsd-src/sys/ddb/db_command.c (revision 5aefcfdc06931dd97e76246d2fe0302f7b3fe094)
1 /*	$NetBSD: db_command.c,v 1.52 2000/11/28 19:27:46 eeh 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) && !defined(_LKM)
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 	char			*name;
108 	struct db_command	*table;
109 	struct db_command	**cmdp;	/* out */
110 {
111 	struct db_command	*cmd;
112 	int			result = CMD_NONE;
113 
114 	for (cmd = table; cmd->name != 0; cmd++) {
115 	    char *lp;
116 	    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 	struct db_command *table;
156 {
157 	int	 i, j, w, columns, lines, width=0, items, numcmds;
158 	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 	struct db_command	**last_cmdp;	/* IN_OUT */
193 	struct db_command	*cmd_table;
194 {
195 	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         uvm_map_printit((vm_map_t) addr, full, db_printf);
357 }
358 
359 /*ARGSUSED*/
360 void
361 db_object_print_cmd(addr, have_addr, count, modif)
362 	db_expr_t	addr;
363 	int		have_addr;
364 	db_expr_t	count;
365 	char *		modif;
366 {
367         boolean_t full = FALSE;
368 
369         if (modif[0] == 'f')
370                 full = TRUE;
371 
372 	uvm_object_printit((struct uvm_object *) addr, full, db_printf);
373 }
374 
375 /*ARGSUSED*/
376 void
377 db_page_print_cmd(addr, have_addr, count, modif)
378 	db_expr_t	addr;
379 	int		have_addr;
380 	db_expr_t	count;
381 	char *		modif;
382 {
383         boolean_t full = FALSE;
384 
385         if (modif[0] == 'f')
386                 full = TRUE;
387 
388 	uvm_page_printit((struct vm_page *) addr, full, db_printf);
389 }
390 
391 /*ARGSUSED*/
392 void
393 db_buf_print_cmd(addr, have_addr, count, modif)
394 	db_expr_t	addr;
395 	int		have_addr;
396 	db_expr_t	count;
397 	char *		modif;
398 {
399 	boolean_t full = FALSE;
400 
401 	if (modif[0] == 'f')
402 		full = TRUE;
403 
404 	vfs_buf_print((struct buf *)addr, full, db_printf);
405 }
406 
407 /*ARGSUSED*/
408 void
409 db_vnode_print_cmd(addr, have_addr, count, modif)
410 	db_expr_t	addr;
411 	int		have_addr;
412 	db_expr_t	count;
413 	char *		modif;
414 {
415 	boolean_t full = FALSE;
416 
417 	if (modif[0] == 'f')
418 		full = TRUE;
419 
420 	vfs_vnode_print((struct vnode *)addr, full, db_printf);
421 }
422 
423 /*ARGSUSED*/
424 void
425 db_pool_print_cmd(addr, have_addr, count, modif)
426 	db_expr_t	addr;
427 	int		have_addr;
428 	db_expr_t	count;
429 	char *		modif;
430 {
431 	pool_printit((struct pool *)addr, modif, db_printf);
432 }
433 
434 /*ARGSUSED*/
435 void
436 db_namecache_print_cmd(addr, have_addr, count, modif)
437 	db_expr_t	addr;
438 	int		have_addr;
439 	db_expr_t	count;
440 	char *		modif;
441 {
442 	namecache_print((struct vnode *)addr, db_printf);
443 }
444 
445 /*ARGSUSED*/
446 void
447 db_uvmexp_print_cmd(addr, have_addr, count, modif)
448 	db_expr_t	addr;
449 	int		have_addr;
450 	db_expr_t	count;
451 	char *		modif;
452 {
453 	uvmexp_print(db_printf);
454 }
455 
456 /*
457  * 'show' commands
458  */
459 
460 struct db_command db_show_all_cmds[] = {
461 	{ "callout",	db_show_callout,	0, NULL },
462 	{ "procs",	db_show_all_procs,	0, NULL },
463 	{ NULL, 	NULL, 			0, NULL }
464 };
465 
466 struct db_command db_show_cmds[] = {
467 	{ "all",	NULL,			0,	db_show_all_cmds },
468 #if defined(INET) && (NARP > 0)
469 	{ "arptab",	db_show_arptab,		0,	NULL },
470 #endif
471 	{ "breaks",	db_listbreak_cmd, 	0,	NULL },
472 	{ "map",	db_map_print_cmd,	0,	NULL },
473 	{ "object",	db_object_print_cmd,	0,	NULL },
474 	{ "page",	db_page_print_cmd,	0,	NULL },
475 	{ "buf",	db_buf_print_cmd,	0,	NULL },
476 	{ "vnode",	db_vnode_print_cmd,	0,	NULL },
477 	{ "pool",	db_pool_print_cmd,	0,	NULL },
478 	{ "ncache",	db_namecache_print_cmd,	0,	NULL },
479 	{ "uvmexp",	db_uvmexp_print_cmd,	0,	NULL },
480 	{ "registers",	db_show_regs,		0,	NULL },
481 	{ "watches",	db_listwatch_cmd, 	0,	NULL },
482 	{ NULL,		NULL,			0,	NULL }
483 };
484 
485 struct db_command db_command_table[] = {
486 	{ "break",	db_breakpoint_cmd,	0,		NULL },
487 	{ "c",		db_continue_cmd,	0,		NULL },
488 	{ "call",	db_fncall,		CS_OWN,		NULL },
489 	{ "callout",	db_show_callout,	0,		NULL },
490 	{ "continue",	db_continue_cmd,	0,		NULL },
491 	{ "d",		db_delete_cmd,		0,		NULL },
492 	{ "delete",	db_delete_cmd,		0,		NULL },
493 	{ "dwatch",	db_deletewatch_cmd,	0,		NULL },
494 	{ "examine",	db_examine_cmd,		CS_SET_DOT, 	NULL },
495 	{ "kill",	db_kill_proc,		CS_OWN,		NULL },
496 #ifdef DB_MACHINE_COMMANDS
497 	{ "machine",    NULL,                   0,     		NULL },
498 #endif
499 	{ "match",	db_trace_until_matching_cmd,0,		NULL },
500 	{ "next",	db_trace_until_matching_cmd,0,		NULL },
501 	{ "p",		db_print_cmd,		0,		NULL },
502 	{ "print",	db_print_cmd,		0,		NULL },
503 	{ "ps",		db_show_all_procs,	0,		NULL },
504 	{ "reboot",	db_reboot_cmd,		CS_OWN,		NULL },
505 	{ "s",		db_single_step_cmd,	0,		NULL },
506 	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, NULL },
507 	{ "set",	db_set_cmd,		CS_OWN,		NULL },
508 	{ "show",	NULL,			0,		db_show_cmds },
509 	{ "sifting",    db_sifting_cmd,		CS_OWN,		NULL },
510 	{ "step",	db_single_step_cmd,	0,		NULL },
511 	{ "sync",	db_sync_cmd,		CS_OWN,		NULL },
512 	{ "trace",	db_stack_trace_cmd,	0,		NULL },
513 	{ "until",	db_trace_until_call_cmd,0,		NULL },
514 	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
515 	{ "watch",	db_watchpoint_cmd,	CS_MORE,	NULL },
516 	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
517 	{ "x",		db_examine_cmd,		CS_SET_DOT, 	NULL },
518 	{ NULL, 	NULL,			0,		NULL }
519 };
520 
521 #ifdef DB_MACHINE_COMMANDS
522 
523 /*
524  * this function should be called to install the machine dependent
525  * commands. It should be called before the debugger is enabled
526  */
527 void
528 db_machine_commands_install(ptr)
529 	struct db_command *ptr;
530 {
531 	struct db_command *cmd;
532 
533 	for (cmd = db_command_table; cmd != 0; cmd++) {
534 		if (strcmp(cmd->name, "machine") == 0) {
535 			cmd->more = ptr;
536 			break;
537 		}
538 	}
539 }
540 
541 #endif
542 
543 struct db_command	*db_last_command = 0;
544 
545 void
546 db_command_loop()
547 {
548 	label_t		db_jmpbuf;
549 	label_t		*savejmp;
550 	extern int	db_output_line;
551 
552 	/*
553 	 * Initialize 'prev' and 'next' to dot.
554 	 */
555 	db_prev = db_dot;
556 	db_next = db_dot;
557 
558 	db_cmd_loop_done = 0;
559 
560 	savejmp = db_recover;
561 	db_recover = &db_jmpbuf;
562 	(void) setjmp(&db_jmpbuf);
563 
564 	while (!db_cmd_loop_done) {
565 		if (db_print_position() != 0)
566 			db_printf("\n");
567 		db_output_line = 0;
568 
569 
570 #ifdef MULTIPROCESSOR
571 		db_printf("db{%ld}> ", (long)cpu_number());
572 #else
573 		db_printf("db> ");
574 #endif
575 		(void) db_read_line();
576 
577 		db_command(&db_last_command, db_command_table);
578 	}
579 
580 	db_recover = savejmp;
581 }
582 
583 void
584 db_error(s)
585 	char *s;
586 {
587 	if (s)
588 	    db_printf("%s", s);
589 	db_flush_lex();
590 	longjmp(db_recover);
591 }
592 
593 
594 /*
595  * Call random function:
596  * !expr(arg,arg,arg)
597  */
598 /*ARGSUSED*/
599 void
600 db_fncall(addr, have_addr, count, modif)
601 	db_expr_t	addr;
602 	int		have_addr;
603 	db_expr_t	count;
604 	char *		modif;
605 {
606 	db_expr_t	fn_addr;
607 #define	MAXARGS		11
608 	db_expr_t	args[MAXARGS];
609 	int		nargs = 0;
610 	db_expr_t	retval;
611 	db_expr_t	(*func) __P((db_expr_t, ...));
612 	int		t;
613 
614 	if (!db_expression(&fn_addr)) {
615 	    db_printf("Bad function\n");
616 	    db_flush_lex();
617 	    return;
618 	}
619 	func = (db_expr_t (*) __P((db_expr_t, ...))) fn_addr;
620 
621 	t = db_read_token();
622 	if (t == tLPAREN) {
623 	    if (db_expression(&args[0])) {
624 		nargs++;
625 		while ((t = db_read_token()) == tCOMMA) {
626 		    if (nargs == MAXARGS) {
627 			db_printf("Too many arguments\n");
628 			db_flush_lex();
629 			return;
630 		    }
631 		    if (!db_expression(&args[nargs])) {
632 			db_printf("Argument missing\n");
633 			db_flush_lex();
634 			return;
635 		    }
636 		    nargs++;
637 		}
638 		db_unread_token(t);
639 	    }
640 	    if (db_read_token() != tRPAREN) {
641 		db_printf("?\n");
642 		db_flush_lex();
643 		return;
644 	    }
645 	}
646 	db_skip_to_eol();
647 
648 	while (nargs < MAXARGS) {
649 	    args[nargs++] = 0;
650 	}
651 
652 	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
653 			 args[5], args[6], args[7], args[8], args[9]);
654 	db_printf("%s\n", db_num_to_str(retval));
655 }
656 
657 void
658 db_reboot_cmd(addr, have_addr, count, modif)
659 	db_expr_t	addr;
660 	int		have_addr;
661 	db_expr_t	count;
662 	char *		modif;
663 {
664 	db_expr_t bootflags;
665 
666 	/* Flags, default to RB_AUTOBOOT */
667 	if (!db_expression(&bootflags))
668 		bootflags = (db_expr_t)RB_AUTOBOOT;
669 	if (db_read_token() != tEOL) {
670 	    db_error("?\n");
671 	    /*NOTREACHED*/
672 	}
673 	/*
674 	 * We are leaving DDB, never to return upward.
675 	 * Clear db_recover so that we can debug faults in functions
676 	 * called from cpu_reboot.
677 	 */
678 	db_recover = 0;
679 	cpu_reboot((int)bootflags, NULL);
680 }
681 
682 void
683 db_sifting_cmd(addr, have_addr, count, omodif)
684 	db_expr_t	addr;
685 	int		have_addr;
686 	db_expr_t	count;
687 	char *		omodif;
688 {
689 	int	mode, t;
690 
691 	t = db_read_token();
692 	if (t == tSLASH) {
693 		t = db_read_token();
694 		if (t != tIDENT) {
695 			bad_modifier:
696 			db_printf("Bad modifier\n");
697 			db_flush_lex();
698 			return;
699 		}
700 		if (!strcmp(db_tok_string, "F"))
701 			mode = 'F';
702 		else
703 			goto bad_modifier;
704 		t = db_read_token();
705 	} else
706 		mode = 0;
707 
708 	if (t == tIDENT)
709 		db_sifting(db_tok_string, mode);
710 	else {
711 		db_printf("Bad argument (non-string)\n");
712 		db_flush_lex();
713 	}
714 }
715 
716 void
717 db_stack_trace_cmd(addr, have_addr, count, modif)
718 	db_expr_t	addr;
719 	boolean_t	have_addr;
720 	db_expr_t	count;
721 	char		*modif;
722 {
723 	if (count == -1)
724 		count = 65535;
725 
726 	db_stack_trace_print(addr, have_addr, count, modif, db_printf);
727 }
728 
729 void
730 db_sync_cmd(addr, have_addr, count, modif)
731 	db_expr_t	addr;
732 	int		have_addr;
733 	db_expr_t	count;
734 	char *		modif;
735 {
736 	/*
737 	 * We are leaving DDB, never to return upward.
738 	 * Clear db_recover so that we can debug faults in functions
739 	 * called from cpu_reboot.
740 	 */
741 	db_recover = 0;
742 	cpu_reboot(RB_DUMP, NULL);
743 }
744