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