db_command.c revision 1.43.2.1 1 /* $NetBSD: db_command.c,v 1.43.2.1 2000/06/22 17:06:11 minoura 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
42 #include <machine/db_machdep.h> /* type definitions */
43
44 #if defined(_KERNEL) && !defined(_LKM)
45 #include "opt_multiprocessor.h"
46 #endif
47 #ifdef MULTIPROCESSOR
48 #include <machine/cpu.h>
49 #endif
50
51 #include <ddb/db_lex.h>
52 #include <ddb/db_output.h>
53 #include <ddb/db_command.h>
54 #include <ddb/db_break.h>
55 #include <ddb/db_watch.h>
56 #include <ddb/db_run.h>
57 #include <ddb/db_variables.h>
58 #include <ddb/db_interface.h>
59 #include <ddb/db_sym.h>
60 #include <ddb/db_extern.h>
61
62 #include <vm/vm.h>
63
64 #include <uvm/uvm_extern.h>
65 #include <uvm/uvm_ddb.h>
66
67 #include "arp.h"
68
69 /*
70 * Exported global variables
71 */
72 boolean_t db_cmd_loop_done;
73 label_t *db_recover;
74
75 /*
76 * if 'ed' style: 'dot' is set at start of last item printed,
77 * and '+' points to next line.
78 * Otherwise: 'dot' points to next item, '..' points to last.
79 */
80 boolean_t db_ed_style = TRUE;
81
82 /*
83 * Utility routine - discard tokens through end-of-line.
84 */
85 void
86 db_skip_to_eol()
87 {
88 int t;
89 do {
90 t = db_read_token();
91 } while (t != tEOL);
92 }
93
94 /*
95 * Results of command search.
96 */
97 #define CMD_UNIQUE 0
98 #define CMD_FOUND 1
99 #define CMD_NONE 2
100 #define CMD_AMBIGUOUS 3
101 #define CMD_HELP 4
102
103 /*
104 * Search for command prefix.
105 */
106 int
107 db_cmd_search(name, table, cmdp)
108 char *name;
109 struct db_command *table;
110 struct db_command **cmdp; /* out */
111 {
112 struct db_command *cmd;
113 int result = CMD_NONE;
114
115 for (cmd = table; cmd->name != 0; cmd++) {
116 char *lp;
117 char *rp;
118 int c;
119
120 lp = name;
121 rp = cmd->name;
122 while ((c = *lp) == *rp) {
123 if (c == 0) {
124 /* complete match */
125 *cmdp = cmd;
126 return (CMD_UNIQUE);
127 }
128 lp++;
129 rp++;
130 }
131 if (c == 0) {
132 /* end of name, not end of command -
133 partial match */
134 if (result == CMD_FOUND) {
135 result = CMD_AMBIGUOUS;
136 /* but keep looking for a full match -
137 this lets us match single letters */
138 }
139 else {
140 *cmdp = cmd;
141 result = CMD_FOUND;
142 }
143 }
144 }
145 if (result == CMD_NONE) {
146 /* check for 'help' */
147 if (name[0] == 'h' && name[1] == 'e'
148 && name[2] == 'l' && name[3] == 'p')
149 result = CMD_HELP;
150 }
151 return (result);
152 }
153
154 void
155 db_cmd_list(table)
156 struct db_command *table;
157 {
158 int i, j, w, columns, lines, width=0, items, numcmds;
159 char *p;
160
161 for (numcmds = 0; table[numcmds].name != NULL; numcmds++) {
162 w = strlen(table[numcmds].name);
163 if (w > width)
164 width = w;
165 }
166 width = DB_NEXT_TAB(width);
167 items = 0;
168
169 columns = db_max_width / width;
170 if (columns == 0)
171 columns = 1;
172 lines = (numcmds + columns - 1) / columns;
173 for (i = 0; i < lines; i++) {
174 for (j = 0; j < columns; j++) {
175 p = table[j * lines + i].name;
176 if (p)
177 db_printf("%s", p);
178 if (j * lines + i + lines >= numcmds) {
179 db_putchar('\n');
180 break;
181 }
182 w = strlen(p);
183 while (w < width) {
184 w = DB_NEXT_TAB(w);
185 db_putchar('\t');
186 }
187 }
188 }
189 }
190
191 void
192 db_command(last_cmdp, cmd_table)
193 struct db_command **last_cmdp; /* IN_OUT */
194 struct db_command *cmd_table;
195 {
196 struct db_command *cmd;
197 int t;
198 char modif[TOK_STRING_SIZE];
199 db_expr_t addr, count;
200 boolean_t have_addr = FALSE;
201 int result;
202
203 static db_expr_t last_count = 0;
204
205 t = db_read_token();
206 if ((t == tEOL) || (t == tCOMMA)) {
207 /*
208 * An empty line repeats last command, at 'next'.
209 * Only a count repeats the last command with the new count.
210 */
211 cmd = *last_cmdp;
212 addr = (db_expr_t)db_next;
213 if (t == tCOMMA) {
214 if (!db_expression(&count)) {
215 db_printf("Count missing\n");
216 db_flush_lex();
217 return;
218 }
219 } else
220 count = last_count;
221 have_addr = FALSE;
222 modif[0] = '\0';
223 db_skip_to_eol();
224 }
225 else if (t == tEXCL) {
226 db_fncall(0, 0, 0, NULL);
227 return;
228 }
229 else if (t != tIDENT) {
230 db_printf("?\n");
231 db_flush_lex();
232 return;
233 }
234 else {
235 /*
236 * Search for command
237 */
238 while (cmd_table) {
239 result = db_cmd_search(db_tok_string,
240 cmd_table,
241 &cmd);
242 switch (result) {
243 case CMD_NONE:
244 db_printf("No such command\n");
245 db_flush_lex();
246 return;
247 case CMD_AMBIGUOUS:
248 db_printf("Ambiguous\n");
249 db_flush_lex();
250 return;
251 case CMD_HELP:
252 db_cmd_list(cmd_table);
253 db_flush_lex();
254 return;
255 default:
256 break;
257 }
258 if ((cmd_table = cmd->more) != 0) {
259 t = db_read_token();
260 if (t != tIDENT) {
261 db_cmd_list(cmd_table);
262 db_flush_lex();
263 return;
264 }
265 }
266 }
267
268 if ((cmd->flag & CS_OWN) == 0) {
269 /*
270 * Standard syntax:
271 * command [/modifier] [addr] [,count]
272 */
273 t = db_read_token();
274 if (t == tSLASH) {
275 t = db_read_token();
276 if (t != tIDENT) {
277 db_printf("Bad modifier\n");
278 db_flush_lex();
279 return;
280 }
281 db_strcpy(modif, db_tok_string);
282 }
283 else {
284 db_unread_token(t);
285 modif[0] = '\0';
286 }
287
288 if (db_expression(&addr)) {
289 db_dot = (db_addr_t) addr;
290 db_last_addr = db_dot;
291 have_addr = TRUE;
292 }
293 else {
294 addr = (db_expr_t) db_dot;
295 have_addr = FALSE;
296 }
297 t = db_read_token();
298 if (t == tCOMMA) {
299 if (!db_expression(&count)) {
300 db_printf("Count missing\n");
301 db_flush_lex();
302 return;
303 }
304 }
305 else {
306 db_unread_token(t);
307 count = -1;
308 }
309 if ((cmd->flag & CS_MORE) == 0) {
310 db_skip_to_eol();
311 }
312 }
313 }
314 *last_cmdp = cmd;
315 last_count = count;
316 if (cmd != 0) {
317 /*
318 * Execute the command.
319 */
320 (*cmd->fcn)(addr, have_addr, count, modif);
321
322 if (cmd->flag & CS_SET_DOT) {
323 /*
324 * If command changes dot, set dot to
325 * previous address displayed (if 'ed' style).
326 */
327 if (db_ed_style) {
328 db_dot = db_prev;
329 }
330 else {
331 db_dot = db_next;
332 }
333 }
334 else {
335 /*
336 * If command does not change dot,
337 * set 'next' location to be the same.
338 */
339 db_next = db_dot;
340 }
341 }
342 }
343
344 /*ARGSUSED*/
345 void
346 db_map_print_cmd(addr, have_addr, count, modif)
347 db_expr_t addr;
348 int have_addr;
349 db_expr_t count;
350 char * modif;
351 {
352 boolean_t full = FALSE;
353
354 if (modif[0] == 'f')
355 full = TRUE;
356
357 uvm_map_printit((vm_map_t) addr, full, db_printf);
358 }
359
360 /*ARGSUSED*/
361 void
362 db_object_print_cmd(addr, have_addr, count, modif)
363 db_expr_t addr;
364 int have_addr;
365 db_expr_t count;
366 char * modif;
367 {
368 boolean_t full = FALSE;
369
370 if (modif[0] == 'f')
371 full = TRUE;
372
373 uvm_object_printit((struct uvm_object *) addr, full, db_printf);
374 }
375
376 /*ARGSUSED*/
377 void
378 db_page_print_cmd(addr, have_addr, count, modif)
379 db_expr_t addr;
380 int have_addr;
381 db_expr_t count;
382 char * modif;
383 {
384 boolean_t full = FALSE;
385
386 if (modif[0] == 'f')
387 full = TRUE;
388
389 uvm_page_printit((struct vm_page *) addr, full, db_printf);
390 }
391
392 /*ARGSUSED*/
393 void
394 db_buf_print_cmd(addr, have_addr, count, modif)
395 db_expr_t addr;
396 int have_addr;
397 db_expr_t count;
398 char * modif;
399 {
400 boolean_t full = FALSE;
401
402 if (modif[0] == 'f')
403 full = TRUE;
404
405 vfs_buf_print((struct buf *)addr, full, db_printf);
406 }
407
408 /*ARGSUSED*/
409 void
410 db_vnode_print_cmd(addr, have_addr, count, modif)
411 db_expr_t addr;
412 int have_addr;
413 db_expr_t count;
414 char * modif;
415 {
416 boolean_t full = FALSE;
417
418 if (modif[0] == 'f')
419 full = TRUE;
420
421 vfs_vnode_print((struct vnode *)addr, full, db_printf);
422 }
423
424 /*ARGSUSED*/
425 void
426 db_pool_print_cmd(addr, have_addr, count, modif)
427 db_expr_t addr;
428 int have_addr;
429 db_expr_t count;
430 char * modif;
431 {
432
433 pool_printit((struct pool *) addr, modif, db_printf);
434 }
435
436 /*
437 * 'show' commands
438 */
439
440 struct db_command db_show_all_cmds[] = {
441 { "callout", db_show_callout, 0, NULL },
442 { "procs", db_show_all_procs, 0, NULL },
443 { NULL, NULL, 0, NULL }
444 };
445
446 struct db_command db_show_cmds[] = {
447 { "all", NULL, 0, db_show_all_cmds },
448 #if defined(INET) && (NARP > 0)
449 { "arptab", db_show_arptab, 0, NULL },
450 #endif
451 { "breaks", db_listbreak_cmd, 0, NULL },
452 { "map", db_map_print_cmd, 0, NULL },
453 { "object", db_object_print_cmd, 0, NULL },
454 { "page", db_page_print_cmd, 0, NULL },
455 { "buf", db_buf_print_cmd, 0, NULL },
456 { "vnode", db_vnode_print_cmd, 0, NULL },
457 { "pool", db_pool_print_cmd, 0, NULL },
458 { "registers", db_show_regs, 0, NULL },
459 { "watches", db_listwatch_cmd, 0, NULL },
460 { NULL, NULL, 0, NULL }
461 };
462
463 struct db_command db_command_table[] = {
464 { "break", db_breakpoint_cmd, 0, NULL },
465 { "c", db_continue_cmd, 0, NULL },
466 { "call", db_fncall, CS_OWN, NULL },
467 { "callout", db_show_callout, 0, NULL },
468 { "continue", db_continue_cmd, 0, NULL },
469 { "d", db_delete_cmd, 0, NULL },
470 { "delete", db_delete_cmd, 0, NULL },
471 { "dwatch", db_deletewatch_cmd, 0, NULL },
472 { "examine", db_examine_cmd, CS_SET_DOT, NULL },
473 { "kill", db_kill_proc, CS_OWN, NULL },
474 #ifdef DB_MACHINE_COMMANDS
475 { "machine", NULL, 0, NULL },
476 #endif
477 { "match", db_trace_until_matching_cmd,0, NULL },
478 { "next", db_trace_until_matching_cmd,0, NULL },
479 { "p", db_print_cmd, 0, NULL },
480 { "print", db_print_cmd, 0, NULL },
481 { "ps", db_show_all_procs, 0, NULL },
482 { "reboot", db_reboot_cmd, CS_OWN, NULL },
483 { "s", db_single_step_cmd, 0, NULL },
484 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, NULL },
485 { "set", db_set_cmd, CS_OWN, NULL },
486 { "show", NULL, 0, db_show_cmds },
487 { "sifting", db_sifting_cmd, CS_OWN, NULL },
488 { "step", db_single_step_cmd, 0, NULL },
489 { "sync", db_sync_cmd, CS_OWN, NULL },
490 { "trace", db_stack_trace_cmd, 0, NULL },
491 { "until", db_trace_until_call_cmd,0, NULL },
492 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, NULL },
493 { "watch", db_watchpoint_cmd, CS_MORE, NULL },
494 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, NULL },
495 { "x", db_examine_cmd, CS_SET_DOT, NULL },
496 { NULL, NULL, 0, NULL }
497 };
498
499 #ifdef DB_MACHINE_COMMANDS
500
501 /*
502 * this function should be called to install the machine dependent
503 * commands. It should be called before the debugger is enabled
504 */
505 void
506 db_machine_commands_install(ptr)
507 struct db_command *ptr;
508 {
509 struct db_command *cmd;
510
511 for (cmd = db_command_table; cmd != 0; cmd++) {
512 if (strcmp(cmd->name, "machine") == 0) {
513 cmd->more = ptr;
514 break;
515 }
516 }
517 }
518
519 #endif
520
521 struct db_command *db_last_command = 0;
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);
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