db_command.c revision 1.36 1 /* $NetBSD: db_command.c,v 1.36 2000/04/10 02:22:13 chs 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 t = db_read_token();
201 if (t == tEOL) {
202 /* empty line repeats last command, at 'next' */
203 cmd = *last_cmdp;
204 addr = (db_expr_t)db_next;
205 have_addr = FALSE;
206 count = 1;
207 modif[0] = '\0';
208 }
209 else if (t == tEXCL) {
210 db_fncall(0, 0, 0, NULL);
211 return;
212 }
213 else if (t != tIDENT) {
214 db_printf("?\n");
215 db_flush_lex();
216 return;
217 }
218 else {
219 /*
220 * Search for command
221 */
222 while (cmd_table) {
223 result = db_cmd_search(db_tok_string,
224 cmd_table,
225 &cmd);
226 switch (result) {
227 case CMD_NONE:
228 db_printf("No such command\n");
229 db_flush_lex();
230 return;
231 case CMD_AMBIGUOUS:
232 db_printf("Ambiguous\n");
233 db_flush_lex();
234 return;
235 case CMD_HELP:
236 db_cmd_list(cmd_table);
237 db_flush_lex();
238 return;
239 default:
240 break;
241 }
242 if ((cmd_table = cmd->more) != 0) {
243 t = db_read_token();
244 if (t != tIDENT) {
245 db_cmd_list(cmd_table);
246 db_flush_lex();
247 return;
248 }
249 }
250 }
251
252 if ((cmd->flag & CS_OWN) == 0) {
253 /*
254 * Standard syntax:
255 * command [/modifier] [addr] [,count]
256 */
257 t = db_read_token();
258 if (t == tSLASH) {
259 t = db_read_token();
260 if (t != tIDENT) {
261 db_printf("Bad modifier\n");
262 db_flush_lex();
263 return;
264 }
265 db_strcpy(modif, db_tok_string);
266 }
267 else {
268 db_unread_token(t);
269 modif[0] = '\0';
270 }
271
272 if (db_expression(&addr)) {
273 db_dot = (db_addr_t) addr;
274 db_last_addr = db_dot;
275 have_addr = TRUE;
276 }
277 else {
278 addr = (db_expr_t) db_dot;
279 have_addr = FALSE;
280 }
281 t = db_read_token();
282 if (t == tCOMMA) {
283 if (!db_expression(&count)) {
284 db_printf("Count missing\n");
285 db_flush_lex();
286 return;
287 }
288 }
289 else {
290 db_unread_token(t);
291 count = -1;
292 }
293 if ((cmd->flag & CS_MORE) == 0) {
294 db_skip_to_eol();
295 }
296 }
297 }
298 *last_cmdp = cmd;
299 if (cmd != 0) {
300 /*
301 * Execute the command.
302 */
303 (*cmd->fcn)(addr, have_addr, count, modif);
304
305 if (cmd->flag & CS_SET_DOT) {
306 /*
307 * If command changes dot, set dot to
308 * previous address displayed (if 'ed' style).
309 */
310 if (db_ed_style) {
311 db_dot = db_prev;
312 }
313 else {
314 db_dot = db_next;
315 }
316 }
317 else {
318 /*
319 * If command does not change dot,
320 * set 'next' location to be the same.
321 */
322 db_next = db_dot;
323 }
324 }
325 }
326
327 /*ARGSUSED*/
328 void
329 db_map_print_cmd(addr, have_addr, count, modif)
330 db_expr_t addr;
331 int have_addr;
332 db_expr_t count;
333 char * modif;
334 {
335 boolean_t full = FALSE;
336
337 if (modif[0] == 'f')
338 full = TRUE;
339
340 uvm_map_printit((vm_map_t) addr, full, db_printf);
341 }
342
343 /*ARGSUSED*/
344 void
345 db_object_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_object_printit((struct uvm_object *) addr, full, db_printf);
357 }
358
359 /*ARGSUSED*/
360 void
361 db_page_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_page_printit((struct vm_page *) addr, full, db_printf);
373 }
374
375 /*ARGSUSED*/
376 void
377 db_buf_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 vfs_buf_print((struct buf *)addr, full, db_printf);
389 }
390
391 /*ARGSUSED*/
392 void
393 db_vnode_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_vnode_print((struct vnode *)addr, full, db_printf);
405 }
406
407 /*ARGSUSED*/
408 void
409 db_pool_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
416 pool_printit((struct pool *) addr, modif, db_printf);
417 }
418
419 /*
420 * 'show' commands
421 */
422
423 struct db_command db_show_all_cmds[] = {
424 { "callout", db_show_callout, 0, NULL },
425 { "procs", db_show_all_procs, 0, NULL },
426 { NULL, NULL, 0, NULL }
427 };
428
429 struct db_command db_show_cmds[] = {
430 { "all", NULL, 0, db_show_all_cmds },
431 { "breaks", db_listbreak_cmd, 0, NULL },
432 { "map", db_map_print_cmd, 0, NULL },
433 { "object", db_object_print_cmd, 0, NULL },
434 { "page", db_page_print_cmd, 0, NULL },
435 { "pool", db_pool_print_cmd, 0, NULL },
436 { "registers", db_show_regs, 0, NULL },
437 { "watches", db_listwatch_cmd, 0, NULL },
438 { NULL, NULL, 0, NULL }
439 };
440
441 struct db_command db_command_table[] = {
442 { "break", db_breakpoint_cmd, 0, NULL },
443 { "c", db_continue_cmd, 0, NULL },
444 { "call", db_fncall, CS_OWN, NULL },
445 { "callout", db_show_callout, 0, NULL },
446 { "continue", db_continue_cmd, 0, NULL },
447 { "d", db_delete_cmd, 0, NULL },
448 { "delete", db_delete_cmd, 0, NULL },
449 { "dwatch", db_deletewatch_cmd, 0, NULL },
450 { "examine", db_examine_cmd, CS_SET_DOT, NULL },
451 { "kill", db_kill_proc, CS_OWN, NULL },
452 #ifdef DB_MACHINE_COMMANDS
453 { "machine", NULL, 0, NULL },
454 #endif
455 { "match", db_trace_until_matching_cmd,0, NULL },
456 { "next", db_trace_until_matching_cmd,0, NULL },
457 { "print", db_print_cmd, 0, NULL },
458 { "ps", db_show_all_procs, 0, NULL },
459 { "reboot", db_reboot_cmd, CS_OWN, NULL },
460 { "s", db_single_step_cmd, 0, NULL },
461 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, NULL },
462 { "set", db_set_cmd, CS_OWN, NULL },
463 { "show", NULL, 0, db_show_cmds },
464 { "step", db_single_step_cmd, 0, NULL },
465 { "sync", db_sync_cmd, CS_OWN, NULL },
466 { "trace", db_stack_trace_cmd, 0, NULL },
467 { "until", db_trace_until_call_cmd,0, NULL },
468 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, NULL },
469 { "watch", db_watchpoint_cmd, CS_MORE, NULL },
470 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, NULL },
471 { "x", db_examine_cmd, CS_SET_DOT, NULL },
472 { NULL, NULL, 0, NULL }
473 };
474
475 #ifdef DB_MACHINE_COMMANDS
476
477 /*
478 * this function should be called to install the machine dependent
479 * commands. It should be called before the debugger is enabled
480 */
481 void
482 db_machine_commands_install(ptr)
483 struct db_command *ptr;
484 {
485 struct db_command *cmd;
486
487 for (cmd = db_command_table; cmd != 0; cmd++) {
488 if (strcmp(cmd->name, "machine") == 0) {
489 cmd->more = ptr;
490 break;
491 }
492 }
493 }
494
495 #endif
496
497 struct db_command *db_last_command = 0;
498
499 void
500 db_command_loop()
501 {
502 label_t db_jmpbuf;
503 label_t *savejmp;
504 extern int db_output_line;
505
506 /*
507 * Initialize 'prev' and 'next' to dot.
508 */
509 db_prev = db_dot;
510 db_next = db_dot;
511
512 db_cmd_loop_done = 0;
513
514 savejmp = db_recover;
515 db_recover = &db_jmpbuf;
516 (void) setjmp(&db_jmpbuf);
517
518 while (!db_cmd_loop_done) {
519 if (db_print_position() != 0)
520 db_printf("\n");
521 db_output_line = 0;
522
523 #ifdef MULTIPROCESSOR
524 db_printf("(cpu %d)", cpu_number());
525 #endif
526 db_printf("db> ");
527 (void) db_read_line();
528
529 db_command(&db_last_command, db_command_table);
530 }
531
532 db_recover = savejmp;
533 }
534
535 void
536 db_error(s)
537 char *s;
538 {
539 if (s)
540 db_printf(s);
541 db_flush_lex();
542 longjmp(db_recover);
543 }
544
545
546 /*
547 * Call random function:
548 * !expr(arg,arg,arg)
549 */
550 /*ARGSUSED*/
551 void
552 db_fncall(addr, have_addr, count, modif)
553 db_expr_t addr;
554 int have_addr;
555 db_expr_t count;
556 char * modif;
557 {
558 db_expr_t fn_addr;
559 #define MAXARGS 11
560 db_expr_t args[MAXARGS];
561 int nargs = 0;
562 db_expr_t retval;
563 db_expr_t (*func) __P((db_expr_t, ...));
564 int t;
565
566 if (!db_expression(&fn_addr)) {
567 db_printf("Bad function\n");
568 db_flush_lex();
569 return;
570 }
571 func = (db_expr_t (*) __P((db_expr_t, ...))) fn_addr;
572
573 t = db_read_token();
574 if (t == tLPAREN) {
575 if (db_expression(&args[0])) {
576 nargs++;
577 while ((t = db_read_token()) == tCOMMA) {
578 if (nargs == MAXARGS) {
579 db_printf("Too many arguments\n");
580 db_flush_lex();
581 return;
582 }
583 if (!db_expression(&args[nargs])) {
584 db_printf("Argument missing\n");
585 db_flush_lex();
586 return;
587 }
588 nargs++;
589 }
590 db_unread_token(t);
591 }
592 if (db_read_token() != tRPAREN) {
593 db_printf("?\n");
594 db_flush_lex();
595 return;
596 }
597 }
598 db_skip_to_eol();
599
600 while (nargs < MAXARGS) {
601 args[nargs++] = 0;
602 }
603
604 retval = (*func)(args[0], args[1], args[2], args[3], args[4],
605 args[5], args[6], args[7], args[8], args[9]);
606 db_printf("%#ln\n", retval);
607 }
608
609 void
610 db_reboot_cmd(addr, have_addr, count, modif)
611 db_expr_t addr;
612 int have_addr;
613 db_expr_t count;
614 char * modif;
615 {
616 db_expr_t bootflags;
617
618 /* Flags, default to RB_AUTOBOOT */
619 if (!db_expression(&bootflags))
620 bootflags = (db_expr_t)RB_AUTOBOOT;
621 if (db_read_token() != tEOL) {
622 db_error("?\n");
623 /*NOTREACHED*/
624 }
625 cpu_reboot((int)bootflags, NULL);
626 }
627
628 void
629 db_sync_cmd(addr, have_addr, count, modif)
630 db_expr_t addr;
631 int have_addr;
632 db_expr_t count;
633 char * modif;
634 {
635
636 cpu_reboot(RB_DUMP, NULL);
637 }
638