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