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