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