db_command.c revision 1.7 1 /*
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
25 *
26 * $Id: db_command.c,v 1.7 1993/12/18 04:46:29 mycroft Exp $
27 */
28
29 /*
30 * Command dispatcher.
31 */
32 #include <sys/param.h>
33 #include <sys/proc.h>
34
35 #include <machine/db_machdep.h> /* type definitions */
36
37 #include <ddb/db_lex.h>
38 #include <ddb/db_output.h>
39
40 #include <setjmp.h>
41
42 /*
43 * Exported global variables
44 */
45 boolean_t db_cmd_loop_done;
46 jmp_buf db_jmpbuf;
47 db_addr_t db_dot;
48 db_addr_t db_last_addr;
49 db_addr_t db_prev;
50 db_addr_t db_next;
51
52 /*
53 * if 'ed' style: 'dot' is set at start of last item printed,
54 * and '+' points to next line.
55 * Otherwise: 'dot' points to next item, '..' points to last.
56 */
57 boolean_t db_ed_style = TRUE;
58
59
60 /*
61 * Utility routine - discard tokens through end-of-line.
62 */
63 void
64 db_skip_to_eol()
65 {
66 int t;
67 do {
68 t = db_read_token();
69 } while (t != tEOL);
70 }
71
72 /*
73 * Command table
74 */
75 struct command {
76 char * name; /* command name */
77 void (*fcn)(); /* function to call */
78 int flag; /* extra info: */
79 #define CS_OWN 0x1 /* non-standard syntax */
80 #define CS_MORE 0x2 /* standard syntax, but may have other
81 words at end */
82 #define CS_SET_DOT 0x100 /* set dot after command */
83 struct command *more; /* another level of command */
84 };
85
86 /*
87 * Results of command search.
88 */
89 #define CMD_UNIQUE 0
90 #define CMD_FOUND 1
91 #define CMD_NONE 2
92 #define CMD_AMBIGUOUS 3
93 #define CMD_HELP 4
94
95 /*
96 * Search for command prefix.
97 */
98 int
99 db_cmd_search(name, table, cmdp)
100 char * name;
101 struct command *table;
102 struct command **cmdp; /* out */
103 {
104 struct command *cmd;
105 int result = CMD_NONE;
106
107 for (cmd = table; cmd->name != 0; cmd++) {
108 register char *lp;
109 register char *rp;
110 register int c;
111
112 lp = name;
113 rp = cmd->name;
114 while ((c = *lp) == *rp) {
115 if (c == 0) {
116 /* complete match */
117 *cmdp = cmd;
118 return (CMD_UNIQUE);
119 }
120 lp++;
121 rp++;
122 }
123 if (c == 0) {
124 /* end of name, not end of command -
125 partial match */
126 if (result == CMD_FOUND) {
127 result = CMD_AMBIGUOUS;
128 /* but keep looking for a full match -
129 this lets us match single letters */
130 }
131 else {
132 *cmdp = cmd;
133 result = CMD_FOUND;
134 }
135 }
136 }
137 if (result == CMD_NONE) {
138 /* check for 'help' */
139 if (name[0] == 'h' && name[1] == 'e'
140 && name[2] == 'l' && name[3] == 'p')
141 result = CMD_HELP;
142 }
143 return (result);
144 }
145
146 void
147 db_cmd_list(table)
148 struct command *table;
149 {
150 register struct command *cmd;
151
152 for (cmd = table; cmd->name != 0; cmd++) {
153 db_printf("%-12s", cmd->name);
154 db_end_line();
155 }
156 }
157
158 void
159 db_command(last_cmdp, cmd_table)
160 struct command **last_cmdp; /* IN_OUT */
161 struct command *cmd_table;
162 {
163 struct command *cmd;
164 int t;
165 char modif[TOK_STRING_SIZE];
166 db_expr_t addr, count;
167 boolean_t have_addr;
168 int result;
169
170 t = db_read_token();
171 if (t == tEOL) {
172 /* empty line repeats last command, at 'next' */
173 cmd = *last_cmdp;
174 addr = (db_expr_t)db_next;
175 have_addr = FALSE;
176 count = 1;
177 modif[0] = '\0';
178 }
179 else if (t == tEXCL) {
180 void db_fncall();
181 db_fncall();
182 return;
183 }
184 else if (t != tIDENT) {
185 db_printf("?\n");
186 db_flush_lex();
187 return;
188 }
189 else {
190 /*
191 * Search for command
192 */
193 while (cmd_table) {
194 result = db_cmd_search(db_tok_string,
195 cmd_table,
196 &cmd);
197 switch (result) {
198 case CMD_NONE:
199 db_printf("No such command\n");
200 db_flush_lex();
201 return;
202 case CMD_AMBIGUOUS:
203 db_printf("Ambiguous\n");
204 db_flush_lex();
205 return;
206 case CMD_HELP:
207 db_cmd_list(cmd_table);
208 db_flush_lex();
209 return;
210 default:
211 break;
212 }
213 if ((cmd_table = cmd->more) != 0) {
214 t = db_read_token();
215 if (t != tIDENT) {
216 db_cmd_list(cmd_table);
217 db_flush_lex();
218 return;
219 }
220 }
221 }
222
223 if ((cmd->flag & CS_OWN) == 0) {
224 /*
225 * Standard syntax:
226 * command [/modifier] [addr] [,count]
227 */
228 t = db_read_token();
229 if (t == tSLASH) {
230 t = db_read_token();
231 if (t != tIDENT) {
232 db_printf("Bad modifier\n");
233 db_flush_lex();
234 return;
235 }
236 db_strcpy(modif, db_tok_string);
237 }
238 else {
239 db_unread_token(t);
240 modif[0] = '\0';
241 }
242
243 if (db_expression(&addr)) {
244 db_dot = (db_addr_t) addr;
245 db_last_addr = db_dot;
246 have_addr = TRUE;
247 }
248 else {
249 addr = (db_expr_t) db_dot;
250 have_addr = FALSE;
251 }
252 t = db_read_token();
253 if (t == tCOMMA) {
254 if (!db_expression(&count)) {
255 db_printf("Count missing\n");
256 db_flush_lex();
257 return;
258 }
259 }
260 else {
261 db_unread_token(t);
262 count = -1;
263 }
264 if ((cmd->flag & CS_MORE) == 0) {
265 db_skip_to_eol();
266 }
267 }
268 }
269 *last_cmdp = cmd;
270 if (cmd != 0) {
271 /*
272 * Execute the command.
273 */
274 (*cmd->fcn)(addr, have_addr, count, modif);
275
276 if (cmd->flag & CS_SET_DOT) {
277 /*
278 * If command changes dot, set dot to
279 * previous address displayed (if 'ed' style).
280 */
281 if (db_ed_style) {
282 db_dot = db_prev;
283 }
284 else {
285 db_dot = db_next;
286 }
287 }
288 else {
289 /*
290 * If command does not change dot,
291 * set 'next' location to be the same.
292 */
293 db_next = db_dot;
294 }
295 }
296 }
297
298 /*ARGSUSED*/
299 void
300 db_map_print_cmd(addr, have_addr, count, modif)
301 db_expr_t addr;
302 int have_addr;
303 db_expr_t count;
304 char * modif;
305 {
306 extern void _vm_map_print();
307 boolean_t full = FALSE;
308
309 if (modif[0] == 'f')
310 full = TRUE;
311
312 _vm_map_print(addr, full, db_printf);
313 }
314
315 /*ARGSUSED*/
316 void
317 db_object_print_cmd(addr, have_addr, count, modif)
318 db_expr_t addr;
319 int have_addr;
320 db_expr_t count;
321 char * modif;
322 {
323 extern void _vm_object_print();
324 boolean_t full = FALSE;
325
326 if (modif[0] == 'f')
327 full = TRUE;
328
329 _vm_object_print(addr, full, db_printf);
330 }
331
332 /*
333 * 'show' commands
334 */
335 extern void db_show_all_procs();
336 extern void db_listbreak_cmd();
337 extern void db_listwatch_cmd();
338 extern void db_show_regs();
339 void db_show_help();
340
341 struct command db_show_all_cmds[] = {
342 { "procs", db_show_all_procs,0, 0 },
343 { (char *)0 }
344 };
345
346 struct command db_show_cmds[] = {
347 { "all", 0, 0, db_show_all_cmds },
348 { "registers", db_show_regs, 0, 0 },
349 { "breaks", db_listbreak_cmd, 0, 0 },
350 { "watches", db_listwatch_cmd, 0, 0 },
351 { "map", db_map_print_cmd, 0, 0 },
352 { "object", db_object_print_cmd, 0, 0 },
353 { (char *)0, }
354 };
355
356 extern void db_print_cmd(), db_examine_cmd(), db_set_cmd();
357 extern void db_search_cmd();
358 extern void db_write_cmd();
359 extern void db_delete_cmd(), db_breakpoint_cmd();
360 extern void db_deletewatch_cmd(), db_watchpoint_cmd();
361 extern void db_single_step_cmd(), db_trace_until_call_cmd(),
362 db_trace_until_matching_cmd(), db_continue_cmd();
363 extern void db_stack_trace_cmd();
364 void db_help_cmd();
365 void db_fncall();
366
367 struct command db_command_table[] = {
368 #ifdef DB_MACHINE_COMMANDS
369 /* this must be the first entry, if it exists */
370 { "machine", 0, 0, 0},
371 #endif
372 { "print", db_print_cmd, 0, 0 },
373 { "examine", db_examine_cmd, CS_SET_DOT, 0 },
374 { "x", db_examine_cmd, CS_SET_DOT, 0 },
375 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 },
376 { "set", db_set_cmd, CS_OWN, 0 },
377 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
378 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
379 { "delete", db_delete_cmd, 0, 0 },
380 { "d", db_delete_cmd, 0, 0 },
381 { "break", db_breakpoint_cmd, 0, 0 },
382 { "dwatch", db_deletewatch_cmd, 0, 0 },
383 { "watch", db_watchpoint_cmd, CS_MORE, 0 },
384 { "step", db_single_step_cmd, 0, 0 },
385 { "s", db_single_step_cmd, 0, 0 },
386 { "continue", db_continue_cmd, 0, 0 },
387 { "c", db_continue_cmd, 0, 0 },
388 { "until", db_trace_until_call_cmd,0, 0 },
389 { "next", db_trace_until_matching_cmd,0, 0 },
390 { "match", db_trace_until_matching_cmd,0, 0 },
391 { "trace", db_stack_trace_cmd, 0, 0 },
392 { "call", db_fncall, CS_OWN, 0 },
393 { "ps", db_show_all_procs, 0, 0 },
394 { "show", 0, 0, db_show_cmds },
395 { (char *)0, }
396 };
397
398 #ifdef DB_MACHINE_COMMANDS
399
400 /* this function should be called to install the machine dependent
401 commands. It should be called before the debugger is enabled */
402 void db_machine_commands_install(ptr)
403 struct db_command *ptr;
404 {
405 db_command_table[0].more = ptr;
406 return;
407 }
408
409 #endif
410
411 struct command *db_last_command = 0;
412
413 void
414 db_help_cmd()
415 {
416 struct command *cmd = db_command_table;
417
418 while (cmd->name != 0) {
419 db_printf("%-12s", cmd->name);
420 db_end_line();
421 cmd++;
422 }
423 }
424
425 void
426 db_command_loop()
427 {
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 while (!db_cmd_loop_done) {
438
439 (void) setjmp(db_jmpbuf);
440 if (db_print_position() != 0)
441 db_printf("\n");
442 db_output_line = 0;
443
444 db_printf("db> ");
445 (void) db_read_line();
446
447 db_command(&db_last_command, db_command_table);
448 }
449 }
450
451 void
452 db_error(s)
453 char *s;
454 {
455 if (s)
456 db_printf(s);
457 db_flush_lex();
458 longjmp(db_jmpbuf, 1);
459 }
460
461
462 /*
463 * Call random function:
464 * !expr(arg,arg,arg)
465 */
466 void
467 db_fncall()
468 {
469 db_expr_t fn_addr;
470 #define MAXARGS 11
471 db_expr_t args[MAXARGS];
472 int nargs = 0;
473 db_expr_t retval;
474 db_expr_t (*func)();
475 int t;
476
477 if (!db_expression(&fn_addr)) {
478 db_printf("Bad function\n");
479 db_flush_lex();
480 return;
481 }
482 func = (db_expr_t (*) ()) fn_addr;
483
484 t = db_read_token();
485 if (t == tLPAREN) {
486 if (db_expression(&args[0])) {
487 nargs++;
488 while ((t = db_read_token()) == tCOMMA) {
489 if (nargs == MAXARGS) {
490 db_printf("Too many arguments\n");
491 db_flush_lex();
492 return;
493 }
494 if (!db_expression(&args[nargs])) {
495 db_printf("Argument missing\n");
496 db_flush_lex();
497 return;
498 }
499 nargs++;
500 }
501 db_unread_token(t);
502 }
503 if (db_read_token() != tRPAREN) {
504 db_printf("?\n");
505 db_flush_lex();
506 return;
507 }
508 }
509 db_skip_to_eol();
510
511 while (nargs < MAXARGS) {
512 args[nargs++] = 0;
513 }
514
515 retval = (*func)(args[0], args[1], args[2], args[3], args[4],
516 args[5], args[6], args[7], args[8], args[9] );
517 db_printf("%#n\n", retval);
518 }
519