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