db_command.c revision 1.57 1 /* $NetBSD: db_command.c,v 1.57 2001/02/11 21:12:24 jhawk 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) && !defined(_LKM)
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 uvm_map_printit((vm_map_t) addr, full, db_printf);
357 }
358
359 /*ARGSUSED*/
360 void
361 db_object_print_cmd(addr, have_addr, count, modif)
362 db_expr_t addr;
363 int have_addr;
364 db_expr_t count;
365 char * modif;
366 {
367 boolean_t full = FALSE;
368
369 if (modif[0] == 'f')
370 full = TRUE;
371
372 uvm_object_printit((struct uvm_object *) addr, full, db_printf);
373 }
374
375 /*ARGSUSED*/
376 void
377 db_page_print_cmd(addr, have_addr, count, modif)
378 db_expr_t addr;
379 int have_addr;
380 db_expr_t count;
381 char * modif;
382 {
383 boolean_t full = FALSE;
384
385 if (modif[0] == 'f')
386 full = TRUE;
387
388 uvm_page_printit((struct vm_page *) addr, full, db_printf);
389 }
390
391 /*ARGSUSED*/
392 void
393 db_buf_print_cmd(addr, have_addr, count, modif)
394 db_expr_t addr;
395 int have_addr;
396 db_expr_t count;
397 char * modif;
398 {
399 boolean_t full = FALSE;
400
401 if (modif[0] == 'f')
402 full = TRUE;
403
404 vfs_buf_print((struct buf *)addr, full, db_printf);
405 }
406
407 /*ARGSUSED*/
408 void
409 db_vnode_print_cmd(addr, have_addr, count, modif)
410 db_expr_t addr;
411 int have_addr;
412 db_expr_t count;
413 char * modif;
414 {
415 boolean_t full = FALSE;
416
417 if (modif[0] == 'f')
418 full = TRUE;
419
420 vfs_vnode_print((struct vnode *)addr, full, db_printf);
421 }
422
423 /*ARGSUSED*/
424 void
425 db_pool_print_cmd(addr, have_addr, count, modif)
426 db_expr_t addr;
427 int have_addr;
428 db_expr_t count;
429 char * modif;
430 {
431 pool_printit((struct pool *)addr, modif, db_printf);
432 }
433
434 /*ARGSUSED*/
435 void
436 db_namecache_print_cmd(addr, have_addr, count, modif)
437 db_expr_t addr;
438 int have_addr;
439 db_expr_t count;
440 char * modif;
441 {
442 namecache_print((struct vnode *)addr, db_printf);
443 }
444
445 /*ARGSUSED*/
446 void
447 db_uvmexp_print_cmd(addr, have_addr, count, modif)
448 db_expr_t addr;
449 int have_addr;
450 db_expr_t count;
451 char * modif;
452 {
453 uvmexp_print(db_printf);
454 }
455
456 /*
457 * 'show' commands
458 */
459
460 static const struct db_command db_show_all_cmds[] = {
461 { "callout", db_show_callout, 0, NULL },
462 { "procs", db_show_all_procs, 0, NULL },
463 { NULL, NULL, 0, NULL }
464 };
465
466 static const struct db_command db_show_cmds[] = {
467 { "all", NULL, 0, db_show_all_cmds },
468 #if defined(INET) && (NARP > 0)
469 { "arptab", db_show_arptab, 0, NULL },
470 #endif
471 { "breaks", db_listbreak_cmd, 0, NULL },
472 { "buf", db_buf_print_cmd, 0, NULL },
473 { "map", db_map_print_cmd, 0, NULL },
474 { "ncache", db_namecache_print_cmd, 0, NULL },
475 { "object", db_object_print_cmd, 0, NULL },
476 { "page", db_page_print_cmd, 0, NULL },
477 { "pool", db_pool_print_cmd, 0, NULL },
478 { "registers", db_show_regs, 0, NULL },
479 { "uvmexp", db_uvmexp_print_cmd, 0, NULL },
480 { "vnode", db_vnode_print_cmd, 0, NULL },
481 { "watches", db_listwatch_cmd, 0, NULL },
482 { NULL, NULL, 0, NULL }
483 };
484
485 static const struct db_command db_command_table[] = {
486 { "break", db_breakpoint_cmd, 0, NULL },
487 { "c", db_continue_cmd, 0, NULL },
488 { "call", db_fncall, CS_OWN, NULL },
489 { "callout", db_show_callout, 0, NULL },
490 { "continue", db_continue_cmd, 0, NULL },
491 { "d", db_delete_cmd, 0, NULL },
492 { "delete", db_delete_cmd, 0, NULL },
493 { "dwatch", db_deletewatch_cmd, 0, NULL },
494 { "examine", db_examine_cmd, CS_SET_DOT, NULL },
495 { "kill", db_kill_proc, CS_OWN, NULL },
496 #ifdef DB_MACHINE_COMMANDS
497 { "machine", NULL, 0, db_machine_command_table },
498 #endif
499 { "match", db_trace_until_matching_cmd,0, NULL },
500 { "next", db_trace_until_matching_cmd,0, NULL },
501 { "p", db_print_cmd, 0, NULL },
502 { "print", db_print_cmd, 0, NULL },
503 { "ps", db_show_all_procs, 0, NULL },
504 { "reboot", db_reboot_cmd, CS_OWN, NULL },
505 { "s", db_single_step_cmd, 0, NULL },
506 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, NULL },
507 { "set", db_set_cmd, CS_OWN, NULL },
508 { "show", NULL, 0, db_show_cmds },
509 { "sifting", db_sifting_cmd, CS_OWN, NULL },
510 { "step", db_single_step_cmd, 0, NULL },
511 { "sync", db_sync_cmd, CS_OWN, NULL },
512 { "trace", db_stack_trace_cmd, 0, NULL },
513 { "until", db_trace_until_call_cmd,0, NULL },
514 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, NULL },
515 { "watch", db_watchpoint_cmd, CS_MORE, NULL },
516 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, NULL },
517 { "x", db_examine_cmd, CS_SET_DOT, NULL },
518 { NULL, NULL, 0, NULL }
519 };
520
521 const struct db_command *db_last_command = NULL;
522
523 void
524 db_command_loop()
525 {
526 label_t db_jmpbuf;
527 label_t *savejmp;
528 extern int db_output_line;
529
530 /*
531 * Initialize 'prev' and 'next' to dot.
532 */
533 db_prev = db_dot;
534 db_next = db_dot;
535
536 db_cmd_loop_done = 0;
537
538 savejmp = db_recover;
539 db_recover = &db_jmpbuf;
540 (void) setjmp(&db_jmpbuf);
541
542 while (!db_cmd_loop_done) {
543 if (db_print_position() != 0)
544 db_printf("\n");
545 db_output_line = 0;
546
547
548 #ifdef MULTIPROCESSOR
549 db_printf("db{%ld}> ", (long)cpu_number());
550 #else
551 db_printf("db> ");
552 #endif
553 (void) db_read_line();
554
555 db_command(&db_last_command, db_command_table);
556 }
557
558 db_recover = savejmp;
559 }
560
561 void
562 db_error(s)
563 char *s;
564 {
565 if (s)
566 db_printf("%s", s);
567 db_flush_lex();
568 longjmp(db_recover);
569 }
570
571
572 /*
573 * Call random function:
574 * !expr(arg,arg,arg)
575 */
576 /*ARGSUSED*/
577 void
578 db_fncall(addr, have_addr, count, modif)
579 db_expr_t addr;
580 int have_addr;
581 db_expr_t count;
582 char * modif;
583 {
584 db_expr_t fn_addr;
585 #define MAXARGS 11
586 db_expr_t args[MAXARGS];
587 int nargs = 0;
588 db_expr_t retval;
589 db_expr_t (*func) __P((db_expr_t, ...));
590 int t;
591
592 if (!db_expression(&fn_addr)) {
593 db_printf("Bad function\n");
594 db_flush_lex();
595 return;
596 }
597 func = (db_expr_t (*) __P((db_expr_t, ...))) fn_addr;
598
599 t = db_read_token();
600 if (t == tLPAREN) {
601 if (db_expression(&args[0])) {
602 nargs++;
603 while ((t = db_read_token()) == tCOMMA) {
604 if (nargs == MAXARGS) {
605 db_printf("Too many arguments\n");
606 db_flush_lex();
607 return;
608 }
609 if (!db_expression(&args[nargs])) {
610 db_printf("Argument missing\n");
611 db_flush_lex();
612 return;
613 }
614 nargs++;
615 }
616 db_unread_token(t);
617 }
618 if (db_read_token() != tRPAREN) {
619 db_printf("?\n");
620 db_flush_lex();
621 return;
622 }
623 }
624 db_skip_to_eol();
625
626 while (nargs < MAXARGS) {
627 args[nargs++] = 0;
628 }
629
630 retval = (*func)(args[0], args[1], args[2], args[3], args[4],
631 args[5], args[6], args[7], args[8], args[9]);
632 db_printf("%s\n", db_num_to_str(retval));
633 }
634
635 void
636 db_reboot_cmd(addr, have_addr, count, modif)
637 db_expr_t addr;
638 int have_addr;
639 db_expr_t count;
640 char * modif;
641 {
642 db_expr_t bootflags;
643
644 /* Flags, default to RB_AUTOBOOT */
645 if (!db_expression(&bootflags))
646 bootflags = (db_expr_t)RB_AUTOBOOT;
647 if (db_read_token() != tEOL) {
648 db_error("?\n");
649 /*NOTREACHED*/
650 }
651 /*
652 * We are leaving DDB, never to return upward.
653 * Clear db_recover so that we can debug faults in functions
654 * called from cpu_reboot.
655 */
656 db_recover = 0;
657 cpu_reboot((int)bootflags, NULL);
658 }
659
660 void
661 db_sifting_cmd(addr, have_addr, count, omodif)
662 db_expr_t addr;
663 int have_addr;
664 db_expr_t count;
665 char * omodif;
666 {
667 int mode, t;
668
669 t = db_read_token();
670 if (t == tSLASH) {
671 t = db_read_token();
672 if (t != tIDENT) {
673 bad_modifier:
674 db_printf("Bad modifier\n");
675 db_flush_lex();
676 return;
677 }
678 if (!strcmp(db_tok_string, "F"))
679 mode = 'F';
680 else
681 goto bad_modifier;
682 t = db_read_token();
683 } else
684 mode = 0;
685
686 if (t == tIDENT)
687 db_sifting(db_tok_string, mode);
688 else {
689 db_printf("Bad argument (non-string)\n");
690 db_flush_lex();
691 }
692 }
693
694 void
695 db_stack_trace_cmd(addr, have_addr, count, modif)
696 db_expr_t addr;
697 boolean_t have_addr;
698 db_expr_t count;
699 char *modif;
700 {
701 if (count == -1)
702 count = 65535;
703
704 db_stack_trace_print(addr, have_addr, count, modif, db_printf);
705 }
706
707 void
708 db_sync_cmd(addr, have_addr, count, modif)
709 db_expr_t addr;
710 int have_addr;
711 db_expr_t count;
712 char * modif;
713 {
714 /*
715 * We are leaving DDB, never to return upward.
716 * Clear db_recover so that we can debug faults in functions
717 * called from cpu_reboot.
718 */
719 db_recover = 0;
720 cpu_reboot(RB_DUMP, NULL);
721 }
722