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