db_command.c revision 1.37 1 /* $NetBSD: db_command.c,v 1.37 2000/04/11 02:21:16 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 { "buf", db_buf_print_cmd, 0, NULL },
436 { "vnode", db_vnode_print_cmd, 0, NULL },
437 { "pool", db_pool_print_cmd, 0, NULL },
438 { "registers", db_show_regs, 0, NULL },
439 { "watches", db_listwatch_cmd, 0, NULL },
440 { NULL, NULL, 0, NULL }
441 };
442
443 struct db_command db_command_table[] = {
444 { "break", db_breakpoint_cmd, 0, NULL },
445 { "c", db_continue_cmd, 0, NULL },
446 { "call", db_fncall, CS_OWN, NULL },
447 { "callout", db_show_callout, 0, NULL },
448 { "continue", db_continue_cmd, 0, NULL },
449 { "d", db_delete_cmd, 0, NULL },
450 { "delete", db_delete_cmd, 0, NULL },
451 { "dwatch", db_deletewatch_cmd, 0, NULL },
452 { "examine", db_examine_cmd, CS_SET_DOT, NULL },
453 { "kill", db_kill_proc, CS_OWN, NULL },
454 #ifdef DB_MACHINE_COMMANDS
455 { "machine", NULL, 0, NULL },
456 #endif
457 { "match", db_trace_until_matching_cmd,0, NULL },
458 { "next", db_trace_until_matching_cmd,0, NULL },
459 { "print", db_print_cmd, 0, NULL },
460 { "ps", db_show_all_procs, 0, NULL },
461 { "reboot", db_reboot_cmd, CS_OWN, NULL },
462 { "s", db_single_step_cmd, 0, NULL },
463 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, NULL },
464 { "set", db_set_cmd, CS_OWN, NULL },
465 { "show", NULL, 0, db_show_cmds },
466 { "step", db_single_step_cmd, 0, NULL },
467 { "sync", db_sync_cmd, CS_OWN, NULL },
468 { "trace", db_stack_trace_cmd, 0, NULL },
469 { "until", db_trace_until_call_cmd,0, NULL },
470 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, NULL },
471 { "watch", db_watchpoint_cmd, CS_MORE, NULL },
472 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, NULL },
473 { "x", db_examine_cmd, CS_SET_DOT, NULL },
474 { NULL, NULL, 0, NULL }
475 };
476
477 #ifdef DB_MACHINE_COMMANDS
478
479 /*
480 * this function should be called to install the machine dependent
481 * commands. It should be called before the debugger is enabled
482 */
483 void
484 db_machine_commands_install(ptr)
485 struct db_command *ptr;
486 {
487 struct db_command *cmd;
488
489 for (cmd = db_command_table; cmd != 0; cmd++) {
490 if (strcmp(cmd->name, "machine") == 0) {
491 cmd->more = ptr;
492 break;
493 }
494 }
495 }
496
497 #endif
498
499 struct db_command *db_last_command = 0;
500
501 void
502 db_command_loop()
503 {
504 label_t db_jmpbuf;
505 label_t *savejmp;
506 extern int db_output_line;
507
508 /*
509 * Initialize 'prev' and 'next' to dot.
510 */
511 db_prev = db_dot;
512 db_next = db_dot;
513
514 db_cmd_loop_done = 0;
515
516 savejmp = db_recover;
517 db_recover = &db_jmpbuf;
518 (void) setjmp(&db_jmpbuf);
519
520 while (!db_cmd_loop_done) {
521 if (db_print_position() != 0)
522 db_printf("\n");
523 db_output_line = 0;
524
525 #ifdef MULTIPROCESSOR
526 db_printf("(cpu %d)", cpu_number());
527 #endif
528 db_printf("db> ");
529 (void) db_read_line();
530
531 db_command(&db_last_command, db_command_table);
532 }
533
534 db_recover = savejmp;
535 }
536
537 void
538 db_error(s)
539 char *s;
540 {
541 if (s)
542 db_printf(s);
543 db_flush_lex();
544 longjmp(db_recover);
545 }
546
547
548 /*
549 * Call random function:
550 * !expr(arg,arg,arg)
551 */
552 /*ARGSUSED*/
553 void
554 db_fncall(addr, have_addr, count, modif)
555 db_expr_t addr;
556 int have_addr;
557 db_expr_t count;
558 char * modif;
559 {
560 db_expr_t fn_addr;
561 #define MAXARGS 11
562 db_expr_t args[MAXARGS];
563 int nargs = 0;
564 db_expr_t retval;
565 db_expr_t (*func) __P((db_expr_t, ...));
566 int t;
567
568 if (!db_expression(&fn_addr)) {
569 db_printf("Bad function\n");
570 db_flush_lex();
571 return;
572 }
573 func = (db_expr_t (*) __P((db_expr_t, ...))) fn_addr;
574
575 t = db_read_token();
576 if (t == tLPAREN) {
577 if (db_expression(&args[0])) {
578 nargs++;
579 while ((t = db_read_token()) == tCOMMA) {
580 if (nargs == MAXARGS) {
581 db_printf("Too many arguments\n");
582 db_flush_lex();
583 return;
584 }
585 if (!db_expression(&args[nargs])) {
586 db_printf("Argument missing\n");
587 db_flush_lex();
588 return;
589 }
590 nargs++;
591 }
592 db_unread_token(t);
593 }
594 if (db_read_token() != tRPAREN) {
595 db_printf("?\n");
596 db_flush_lex();
597 return;
598 }
599 }
600 db_skip_to_eol();
601
602 while (nargs < MAXARGS) {
603 args[nargs++] = 0;
604 }
605
606 retval = (*func)(args[0], args[1], args[2], args[3], args[4],
607 args[5], args[6], args[7], args[8], args[9]);
608 db_printf("%#ln\n", retval);
609 }
610
611 void
612 db_reboot_cmd(addr, have_addr, count, modif)
613 db_expr_t addr;
614 int have_addr;
615 db_expr_t count;
616 char * modif;
617 {
618 db_expr_t bootflags;
619
620 /* Flags, default to RB_AUTOBOOT */
621 if (!db_expression(&bootflags))
622 bootflags = (db_expr_t)RB_AUTOBOOT;
623 if (db_read_token() != tEOL) {
624 db_error("?\n");
625 /*NOTREACHED*/
626 }
627 cpu_reboot((int)bootflags, NULL);
628 }
629
630 void
631 db_sync_cmd(addr, have_addr, count, modif)
632 db_expr_t addr;
633 int have_addr;
634 db_expr_t count;
635 char * modif;
636 {
637
638 cpu_reboot(RB_DUMP, NULL);
639 }
640