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