FreeBSD/Linux Kernel Cross Reference
sys/ddb/db_command.c
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@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 * $FreeBSD$
27 */
28
29 /*
30 * Author: David B. Golub, Carnegie Mellon University
31 * Date: 7/90
32 */
33
34 /*
35 * Command dispatcher.
36 */
37 #include <sys/param.h>
38 #include <sys/linker_set.h>
39 #include <sys/reboot.h>
40 #include <sys/systm.h>
41
42 #include <ddb/ddb.h>
43 #include <ddb/db_command.h>
44 #include <ddb/db_lex.h>
45 #include <ddb/db_output.h>
46
47 #include <setjmp.h>
48
49 /*
50 * Exported global variables
51 */
52 boolean_t db_cmd_loop_done;
53 extern struct linker_set db_cmd_set;
54 db_addr_t db_dot;
55 jmp_buf db_jmpbuf;
56 db_addr_t db_last_addr;
57 db_addr_t db_prev;
58 db_addr_t db_next;
59 extern struct linker_set db_show_cmd_set;
60
61 static db_cmdfcn_t db_fncall;
62 static db_cmdfcn_t db_gdb;
63
64 /* XXX this is actually forward-static. */
65 extern struct command db_show_cmds[];
66
67 /*
68 * if 'ed' style: 'dot' is set at start of last item printed,
69 * and '+' points to next line.
70 * Otherwise: 'dot' points to next item, '..' points to last.
71 */
72 static boolean_t db_ed_style = TRUE;
73
74 /*
75 * Utility routine - discard tokens through end-of-line.
76 */
77 void
78 db_skip_to_eol()
79 {
80 int t;
81 do {
82 t = db_read_token();
83 } while (t != tEOL);
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 static void db_cmd_list __P((struct command *table,
96 struct command **aux_tablep));
97 static int db_cmd_search __P((char *name, struct command *table,
98 struct command **aux_tablep,
99 struct command **cmdp));
100 static void db_command __P((struct command **last_cmdp,
101 struct command *cmd_table,
102 struct command **aux_cmd_tablep));
103
104 /*
105 * Search for command prefix.
106 */
107 static int
108 db_cmd_search(name, table, aux_tablep, cmdp)
109 char * name;
110 struct command *table;
111 struct command **aux_tablep;
112 struct command **cmdp; /* out */
113 {
114 struct command *cmd;
115 struct command **aux_cmdp;
116 int result = CMD_NONE;
117
118 for (cmd = table; cmd->name != 0; cmd++) {
119 register char *lp;
120 register char *rp;
121 register int c;
122
123 lp = name;
124 rp = cmd->name;
125 while ((c = *lp) == *rp) {
126 if (c == 0) {
127 /* complete match */
128 *cmdp = cmd;
129 return (CMD_UNIQUE);
130 }
131 lp++;
132 rp++;
133 }
134 if (c == 0) {
135 /* end of name, not end of command -
136 partial match */
137 if (result == CMD_FOUND) {
138 result = CMD_AMBIGUOUS;
139 /* but keep looking for a full match -
140 this lets us match single letters */
141 }
142 else {
143 *cmdp = cmd;
144 result = CMD_FOUND;
145 }
146 }
147 }
148 if (result == CMD_NONE && aux_tablep != 0)
149 /* XXX repeat too much code. */
150 for (aux_cmdp = aux_tablep; *aux_cmdp != 0; aux_cmdp++) {
151 register char *lp;
152 register char *rp;
153 register int c;
154
155 lp = name;
156 rp = (*aux_cmdp)->name;
157 while ((c = *lp) == *rp) {
158 if (c == 0) {
159 /* complete match */
160 *cmdp = *aux_cmdp;
161 return (CMD_UNIQUE);
162 }
163 lp++;
164 rp++;
165 }
166 if (c == 0) {
167 /* end of name, not end of command -
168 partial match */
169 if (result == CMD_FOUND) {
170 result = CMD_AMBIGUOUS;
171 /* but keep looking for a full match -
172 this lets us match single letters */
173 }
174 else {
175 *cmdp = *aux_cmdp;
176 result = CMD_FOUND;
177 }
178 }
179 }
180 if (result == CMD_NONE) {
181 /* check for 'help' */
182 if (name[0] == 'h' && name[1] == 'e'
183 && name[2] == 'l' && name[3] == 'p')
184 result = CMD_HELP;
185 }
186 return (result);
187 }
188
189 static void
190 db_cmd_list(table, aux_tablep)
191 struct command *table;
192 struct command **aux_tablep;
193 {
194 register struct command *cmd;
195 register struct command **aux_cmdp;
196
197 for (cmd = table; cmd->name != 0; cmd++) {
198 db_printf("%-12s", cmd->name);
199 db_end_line();
200 }
201 if (aux_tablep == 0)
202 return;
203 for (aux_cmdp = aux_tablep; *aux_cmdp != 0; aux_cmdp++) {
204 db_printf("%-12s", (*aux_cmdp)->name);
205 db_end_line();
206 }
207 }
208
209 static void
210 db_command(last_cmdp, cmd_table, aux_cmd_tablep)
211 struct command **last_cmdp; /* IN_OUT */
212 struct command *cmd_table;
213 struct command **aux_cmd_tablep;
214 {
215 struct command *cmd;
216 int t;
217 char modif[TOK_STRING_SIZE];
218 db_expr_t addr, count;
219 boolean_t have_addr = FALSE;
220 int result;
221
222 t = db_read_token();
223 if (t == tEOL) {
224 /* empty line repeats last command, at 'next' */
225 cmd = *last_cmdp;
226 addr = (db_expr_t)db_next;
227 have_addr = FALSE;
228 count = 1;
229 modif[0] = '\0';
230 }
231 else if (t == tEXCL) {
232 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
233 return;
234 }
235 else if (t != tIDENT) {
236 db_printf("?\n");
237 db_flush_lex();
238 return;
239 }
240 else {
241 /*
242 * Search for command
243 */
244 while (cmd_table) {
245 result = db_cmd_search(db_tok_string,
246 cmd_table,
247 aux_cmd_tablep,
248 &cmd);
249 switch (result) {
250 case CMD_NONE:
251 db_printf("No such command\n");
252 db_flush_lex();
253 return;
254 case CMD_AMBIGUOUS:
255 db_printf("Ambiguous\n");
256 db_flush_lex();
257 return;
258 case CMD_HELP:
259 db_cmd_list(cmd_table, aux_cmd_tablep);
260 db_flush_lex();
261 return;
262 default:
263 break;
264 }
265 if ((cmd_table = cmd->more) != 0) {
266 /* XXX usually no more aux's. */
267 aux_cmd_tablep = 0;
268 if (cmd_table == db_show_cmds)
269 aux_cmd_tablep =
270 (struct command **)&db_show_cmd_set.ls_items[0];
271
272 t = db_read_token();
273 if (t != tIDENT) {
274 db_cmd_list(cmd_table, aux_cmd_tablep);
275 db_flush_lex();
276 return;
277 }
278 }
279 }
280
281 if ((cmd->flag & CS_OWN) == 0) {
282 /*
283 * Standard syntax:
284 * command [/modifier] [addr] [,count]
285 */
286 t = db_read_token();
287 if (t == tSLASH) {
288 t = db_read_token();
289 if (t != tIDENT) {
290 db_printf("Bad modifier\n");
291 db_flush_lex();
292 return;
293 }
294 db_strcpy(modif, db_tok_string);
295 }
296 else {
297 db_unread_token(t);
298 modif[0] = '\0';
299 }
300
301 if (db_expression(&addr)) {
302 db_dot = (db_addr_t) addr;
303 db_last_addr = db_dot;
304 have_addr = TRUE;
305 }
306 else {
307 addr = (db_expr_t) db_dot;
308 have_addr = FALSE;
309 }
310 t = db_read_token();
311 if (t == tCOMMA) {
312 if (!db_expression(&count)) {
313 db_printf("Count missing\n");
314 db_flush_lex();
315 return;
316 }
317 }
318 else {
319 db_unread_token(t);
320 count = -1;
321 }
322 if ((cmd->flag & CS_MORE) == 0) {
323 db_skip_to_eol();
324 }
325 }
326 }
327 *last_cmdp = cmd;
328 if (cmd != 0) {
329 /*
330 * Execute the command.
331 */
332 (*cmd->fcn)(addr, have_addr, count, modif);
333
334 if (cmd->flag & CS_SET_DOT) {
335 /*
336 * If command changes dot, set dot to
337 * previous address displayed (if 'ed' style).
338 */
339 if (db_ed_style) {
340 db_dot = db_prev;
341 }
342 else {
343 db_dot = db_next;
344 }
345 }
346 else {
347 /*
348 * If command does not change dot,
349 * set 'next' location to be the same.
350 */
351 db_next = db_dot;
352 }
353 }
354 }
355
356 /*
357 * 'show' commands
358 */
359
360 static struct command db_show_all_cmds[] = {
361 #if 0
362 { "threads", db_show_all_threads, 0, 0 },
363 #endif
364 { "procs", db_ps, 0, 0 },
365 { (char *)0 }
366 };
367
368 static struct command db_show_cmds[] = {
369 { "all", 0, 0, db_show_all_cmds },
370 { "registers", db_show_regs, 0, 0 },
371 { "breaks", db_listbreak_cmd, 0, 0 },
372 #if 0
373 { "thread", db_show_one_thread, 0, 0 },
374 #endif
375 #if 0
376 { "port", ipc_port_print, 0, 0 },
377 #endif
378 { (char *)0, }
379 };
380
381 static struct command db_command_table[] = {
382 { "print", db_print_cmd, 0, 0 },
383 { "p", db_print_cmd, 0, 0 },
384 { "examine", db_examine_cmd, CS_SET_DOT, 0 },
385 { "x", db_examine_cmd, CS_SET_DOT, 0 },
386 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 },
387 { "set", db_set_cmd, CS_OWN, 0 },
388 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
389 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
390 { "delete", db_delete_cmd, 0, 0 },
391 { "d", db_delete_cmd, 0, 0 },
392 { "break", db_breakpoint_cmd, 0, 0 },
393 { "dwatch", db_deletewatch_cmd, 0, 0 },
394 { "watch", db_watchpoint_cmd, CS_MORE,0 },
395 { "step", db_single_step_cmd, 0, 0 },
396 { "s", db_single_step_cmd, 0, 0 },
397 { "continue", db_continue_cmd, 0, 0 },
398 { "c", db_continue_cmd, 0, 0 },
399 { "until", db_trace_until_call_cmd,0, 0 },
400 { "next", db_trace_until_matching_cmd,0, 0 },
401 { "match", db_trace_until_matching_cmd,0, 0 },
402 { "trace", db_stack_trace_cmd, 0, 0 },
403 { "call", db_fncall, CS_OWN, 0 },
404 { "show", 0, 0, db_show_cmds },
405 { "ps", db_ps, 0, 0 },
406 { "gdb", db_gdb, 0, 0 },
407 { (char *)0, }
408 };
409
410 static struct command *db_last_command = 0;
411
412 #if 0
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 #endif
425
426 /*
427 * At least one non-optional command must be implemented using
428 * DB_COMMAND() so that db_cmd_set gets created. Here is one.
429 */
430 DB_COMMAND(panic, db_panic)
431 {
432 panic("from debugger");
433 }
434
435 void
436 db_command_loop()
437 {
438 /*
439 * Initialize 'prev' and 'next' to dot.
440 */
441 db_prev = db_dot;
442 db_next = db_dot;
443
444 db_cmd_loop_done = 0;
445 while (!db_cmd_loop_done) {
446
447 (void) setjmp(db_jmpbuf);
448 if (db_print_position() != 0)
449 db_printf("\n");
450
451 db_printf("db> ");
452 (void) db_read_line();
453
454 db_command(&db_last_command, db_command_table,
455 (struct command **)&db_cmd_set.ls_items[0]);
456 }
457 }
458
459 void
460 db_error(s)
461 char *s;
462 {
463 if (s)
464 db_printf(s);
465 db_flush_lex();
466 longjmp(db_jmpbuf, 1);
467 }
468
469
470 /*
471 * Call random function:
472 * !expr(arg,arg,arg)
473 */
474 static void
475 db_fncall(dummy1, dummy2, dummy3, dummy4)
476 db_expr_t dummy1;
477 boolean_t dummy2;
478 db_expr_t dummy3;
479 char * dummy4;
480 {
481 db_expr_t fn_addr;
482 #define MAXARGS 11 /* XXX only 10 are passed */
483 db_expr_t args[MAXARGS];
484 int nargs = 0;
485 db_expr_t retval;
486 typedef db_expr_t fcn_10args_t __P((db_expr_t, db_expr_t, db_expr_t,
487 db_expr_t, db_expr_t, db_expr_t,
488 db_expr_t, db_expr_t, db_expr_t,
489 db_expr_t));
490 fcn_10args_t *func;
491 int t;
492
493 if (!db_expression(&fn_addr)) {
494 db_printf("Bad function\n");
495 db_flush_lex();
496 return;
497 }
498 func = (fcn_10args_t *)fn_addr; /* XXX */
499
500 t = db_read_token();
501 if (t == tLPAREN) {
502 if (db_expression(&args[0])) {
503 nargs++;
504 while ((t = db_read_token()) == tCOMMA) {
505 if (nargs == MAXARGS) {
506 db_printf("Too many arguments\n");
507 db_flush_lex();
508 return;
509 }
510 if (!db_expression(&args[nargs])) {
511 db_printf("Argument missing\n");
512 db_flush_lex();
513 return;
514 }
515 nargs++;
516 }
517 db_unread_token(t);
518 }
519 if (db_read_token() != tRPAREN) {
520 db_printf("?\n");
521 db_flush_lex();
522 return;
523 }
524 }
525 db_skip_to_eol();
526
527 while (nargs < MAXARGS) {
528 args[nargs++] = 0;
529 }
530
531 retval = (*func)(args[0], args[1], args[2], args[3], args[4],
532 args[5], args[6], args[7], args[8], args[9] );
533 db_printf("%#r\n", retval);
534 }
535
536 /* Enter GDB remote protocol debugger on the next trap. */
537
538 static void
539 db_gdb (dummy1, dummy2, dummy3, dummy4)
540 db_expr_t dummy1;
541 boolean_t dummy2;
542 db_expr_t dummy3;
543 char * dummy4;
544 {
545 boothowto ^= RB_GDB;
546
547 db_printf("Next trap will enter %s\n",
548 boothowto & RB_GDB ? "GDB remote protocol mode"
549 : "DDB debugger");
550 }
Cache object: 0e97a4c8d9e372bb2285d6b45d85a904
|