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