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