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