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