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