2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
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.
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.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
30 * Author: David B. Golub, Carnegie Mellon University
37 #include <sys/param.h>
38 #include <sys/linker_set.h>
39 #include <sys/reboot.h>
40 #include <sys/systm.h>
44 #include <ddb/db_command.h>
45 #include <ddb/db_lex.h>
46 #include <ddb/db_output.h>
51 * Exported global variables
53 boolean_t db_cmd_loop_done;
54 extern struct linker_set db_cmd_set;
57 db_addr_t db_last_addr;
60 extern struct linker_set db_show_cmd_set;
62 static db_cmdfcn_t db_fncall;
63 static db_cmdfcn_t db_gdb;
65 /* XXX this is actually forward-static. */
66 extern struct command db_show_cmds[];
69 * if 'ed' style: 'dot' is set at start of last item printed,
70 * and '+' points to next line.
71 * Otherwise: 'dot' points to next item, '..' points to last.
73 static boolean_t db_ed_style = TRUE;
76 * Utility routine - discard tokens through end-of-line.
88 * Results of command search.
93 #define CMD_AMBIGUOUS 3
96 static void db_cmd_list __P((struct command *table,
97 struct command **aux_tablep));
98 static int db_cmd_search __P((char *name, struct command *table,
99 struct command **aux_tablep,
100 struct command **cmdp));
101 static void db_command __P((struct command **last_cmdp,
102 struct command *cmd_table,
103 struct command **aux_cmd_tablep));
106 * Search for command prefix.
109 db_cmd_search(name, table, aux_tablep, cmdp)
111 struct command *table;
112 struct command **aux_tablep;
113 struct command **cmdp; /* out */
116 struct command **aux_cmdp;
117 int result = CMD_NONE;
119 for (cmd = table; cmd->name != 0; cmd++) {
126 while ((c = *lp) == *rp) {
136 /* end of name, not end of command -
138 if (result == CMD_FOUND) {
139 result = CMD_AMBIGUOUS;
140 /* but keep looking for a full match -
141 this lets us match single letters */
149 if (result == CMD_NONE && aux_tablep != 0)
150 /* XXX repeat too much code. */
151 for (aux_cmdp = aux_tablep; *aux_cmdp != 0; aux_cmdp++) {
157 rp = (*aux_cmdp)->name;
158 while ((c = *lp) == *rp) {
168 /* end of name, not end of command -
170 if (result == CMD_FOUND) {
171 result = CMD_AMBIGUOUS;
172 /* but keep looking for a full match -
173 this lets us match single letters */
181 if (result == CMD_NONE) {
182 /* check for 'help' */
183 if (name[0] == 'h' && name[1] == 'e'
184 && name[2] == 'l' && name[3] == 'p')
191 db_cmd_list(table, aux_tablep)
192 struct command *table;
193 struct command **aux_tablep;
195 register struct command *cmd;
196 register struct command **aux_cmdp;
198 for (cmd = table; cmd->name != 0; cmd++) {
199 db_printf("%-12s", cmd->name);
204 for (aux_cmdp = aux_tablep; *aux_cmdp != 0; aux_cmdp++) {
205 db_printf("%-12s", (*aux_cmdp)->name);
211 db_command(last_cmdp, cmd_table, aux_cmd_tablep)
212 struct command **last_cmdp; /* IN_OUT */
213 struct command *cmd_table;
214 struct command **aux_cmd_tablep;
218 char modif[TOK_STRING_SIZE];
219 db_expr_t addr, count;
220 boolean_t have_addr = FALSE;
225 /* empty line repeats last command, at 'next' */
227 addr = (db_expr_t)db_next;
232 else if (t == tEXCL) {
233 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
236 else if (t != tIDENT) {
246 result = db_cmd_search(db_tok_string,
252 db_printf("No such command\n");
256 db_printf("Ambiguous\n");
260 db_cmd_list(cmd_table, aux_cmd_tablep);
266 if ((cmd_table = cmd->more) != 0) {
267 /* XXX usually no more aux's. */
269 if (cmd_table == db_show_cmds)
271 (struct command **)&db_show_cmd_set.ls_items[0];
275 db_cmd_list(cmd_table, aux_cmd_tablep);
282 if ((cmd->flag & CS_OWN) == 0) {
285 * command [/modifier] [addr] [,count]
291 db_printf("Bad modifier\n");
295 db_strcpy(modif, db_tok_string);
302 if (db_expression(&addr)) {
303 db_dot = (db_addr_t) addr;
304 db_last_addr = db_dot;
308 addr = (db_expr_t) db_dot;
313 if (!db_expression(&count)) {
314 db_printf("Count missing\n");
323 if ((cmd->flag & CS_MORE) == 0) {
331 * Execute the command.
333 (*cmd->fcn)(addr, have_addr, count, modif);
335 if (cmd->flag & CS_SET_DOT) {
337 * If command changes dot, set dot to
338 * previous address displayed (if 'ed' style).
349 * If command does not change dot,
350 * set 'next' location to be the same.
361 static struct command db_show_all_cmds[] = {
363 { "threads", db_show_all_threads, 0, 0 },
365 { "procs", db_ps, 0, 0 },
369 static struct command db_show_cmds[] = {
370 { "all", 0, 0, db_show_all_cmds },
371 { "registers", db_show_regs, 0, 0 },
372 { "breaks", db_listbreak_cmd, 0, 0 },
374 { "thread", db_show_one_thread, 0, 0 },
377 { "port", ipc_port_print, 0, 0 },
382 static struct command db_command_table[] = {
383 { "print", db_print_cmd, 0, 0 },
384 { "p", db_print_cmd, 0, 0 },
385 { "examine", db_examine_cmd, CS_SET_DOT, 0 },
386 { "x", db_examine_cmd, CS_SET_DOT, 0 },
387 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 },
388 { "set", db_set_cmd, CS_OWN, 0 },
389 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
390 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
391 { "delete", db_delete_cmd, 0, 0 },
392 { "d", db_delete_cmd, 0, 0 },
393 { "break", db_breakpoint_cmd, 0, 0 },
394 { "dwatch", db_deletewatch_cmd, 0, 0 },
395 { "watch", db_watchpoint_cmd, CS_MORE,0 },
396 { "step", db_single_step_cmd, 0, 0 },
397 { "s", db_single_step_cmd, 0, 0 },
398 { "continue", db_continue_cmd, 0, 0 },
399 { "c", db_continue_cmd, 0, 0 },
400 { "until", db_trace_until_call_cmd,0, 0 },
401 { "next", db_trace_until_matching_cmd,0, 0 },
402 { "match", db_trace_until_matching_cmd,0, 0 },
403 { "trace", db_stack_trace_cmd, 0, 0 },
404 { "call", db_fncall, CS_OWN, 0 },
405 { "show", 0, 0, db_show_cmds },
406 { "ps", db_ps, 0, 0 },
407 { "gdb", db_gdb, 0, 0 },
411 static struct command *db_last_command = 0;
417 struct command *cmd = db_command_table;
419 while (cmd->name != 0) {
420 db_printf("%-12s", cmd->name);
428 * At least one non-optional command must be implemented using
429 * DB_COMMAND() so that db_cmd_set gets created. Here is one.
431 DB_COMMAND(panic, db_panic)
433 panic("from debugger");
440 * Initialize 'prev' and 'next' to dot.
445 db_cmd_loop_done = 0;
446 while (!db_cmd_loop_done) {
448 (void) setjmp(db_jmpbuf);
449 if (db_print_position() != 0)
453 (void) db_read_line();
455 db_command(&db_last_command, db_command_table,
456 (struct command **)&db_cmd_set.ls_items[0]);
467 longjmp(db_jmpbuf, 1);
472 * Call random function:
476 db_fncall(dummy1, dummy2, dummy3, dummy4)
483 #define MAXARGS 11 /* XXX only 10 are passed */
484 db_expr_t args[MAXARGS];
487 typedef db_expr_t fcn_10args_t __P((db_expr_t, db_expr_t, db_expr_t,
488 db_expr_t, db_expr_t, db_expr_t,
489 db_expr_t, db_expr_t, db_expr_t,
494 if (!db_expression(&fn_addr)) {
495 db_printf("Bad function\n");
499 func = (fcn_10args_t *)fn_addr; /* XXX */
503 if (db_expression(&args[0])) {
505 while ((t = db_read_token()) == tCOMMA) {
506 if (nargs == MAXARGS) {
507 db_printf("Too many arguments\n");
511 if (!db_expression(&args[nargs])) {
512 db_printf("Argument missing\n");
520 if (db_read_token() != tRPAREN) {
528 while (nargs < MAXARGS) {
532 retval = (*func)(args[0], args[1], args[2], args[3], args[4],
533 args[5], args[6], args[7], args[8], args[9] );
534 db_printf("%#lr\n", (long)retval);
537 /* Enter GDB remote protocol debugger on the next trap. */
539 dev_t gdbdev = NODEV;
544 db_gdb (dummy1, dummy2, dummy3, dummy4)
551 if (gdbdev == NODEV) {
552 db_printf("No gdb port enabled. Set flag 0x80 on desired port\n");
553 db_printf("in your configuration file (currently sio only).\n");
558 db_printf("Next trap will enter %s\n",
559 boothowto & RB_GDB ? "GDB remote protocol mode"