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>
48 #include <machine/setjmp.h>
51 * Exported global variables
53 boolean_t db_cmd_loop_done;
56 db_addr_t db_last_addr;
60 SET_DECLARE(db_cmd_set, struct command);
61 SET_DECLARE(db_show_cmd_set, struct command);
63 static db_cmdfcn_t db_fncall;
64 static db_cmdfcn_t db_gdb;
65 static db_cmdfcn_t db_reset;
67 /* XXX this is actually forward-static. */
68 extern struct command db_show_cmds[];
71 * if 'ed' style: 'dot' is set at start of last item printed,
72 * and '+' points to next line.
73 * Otherwise: 'dot' points to next item, '..' points to last.
75 static boolean_t db_ed_style = TRUE;
78 * Utility routine - discard tokens through end-of-line.
90 * Results of command search.
95 #define CMD_AMBIGUOUS 3
98 static void db_cmd_list __P((struct command *table,
99 struct command **aux_tablep,
100 struct command **aux_tablep_end));
101 static int db_cmd_search __P((char *name, struct command *table,
102 struct command **aux_tablep,
103 struct command **aux_tablep_end,
104 struct command **cmdp));
105 static void db_command __P((struct command **last_cmdp,
106 struct command *cmd_table,
107 struct command **aux_cmd_tablep,
108 struct command **aux_cmd_tablep_end));
111 * Search for command prefix.
114 db_cmd_search(name, table, aux_tablep, aux_tablep_end, cmdp)
116 struct command *table;
117 struct command **aux_tablep;
118 struct command **aux_tablep_end;
119 struct command **cmdp; /* out */
122 struct command **aux_cmdp;
123 int result = CMD_NONE;
125 for (cmd = table; cmd->name != 0; cmd++) {
132 while ((c = *lp) == *rp) {
142 /* end of name, not end of command -
144 if (result == CMD_FOUND) {
145 result = CMD_AMBIGUOUS;
146 /* but keep looking for a full match -
147 this lets us match single letters */
155 if (result == CMD_NONE && aux_tablep != 0)
156 /* XXX repeat too much code. */
157 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
163 rp = (*aux_cmdp)->name;
164 while ((c = *lp) == *rp) {
174 /* end of name, not end of command -
176 if (result == CMD_FOUND) {
177 result = CMD_AMBIGUOUS;
178 /* but keep looking for a full match -
179 this lets us match single letters */
187 if (result == CMD_NONE) {
188 /* check for 'help' */
189 if (name[0] == 'h' && name[1] == 'e'
190 && name[2] == 'l' && name[3] == 'p')
197 db_cmd_list(table, aux_tablep, aux_tablep_end)
198 struct command *table;
199 struct command **aux_tablep;
200 struct command **aux_tablep_end;
202 register struct command *cmd;
203 register struct command **aux_cmdp;
205 for (cmd = table; cmd->name != 0; cmd++) {
206 db_printf("%-12s", cmd->name);
211 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
212 db_printf("%-12s", (*aux_cmdp)->name);
218 db_command(last_cmdp, cmd_table, aux_cmd_tablep, aux_cmd_tablep_end)
219 struct command **last_cmdp; /* IN_OUT */
220 struct command *cmd_table;
221 struct command **aux_cmd_tablep;
222 struct command **aux_cmd_tablep_end;
226 char modif[TOK_STRING_SIZE];
227 db_expr_t addr, count;
228 boolean_t have_addr = FALSE;
233 /* empty line repeats last command, at 'next' */
235 addr = (db_expr_t)db_next;
240 else if (t == tEXCL) {
241 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
244 else if (t != tIDENT) {
254 result = db_cmd_search(db_tok_string,
261 db_printf("No such command\n");
265 db_printf("Ambiguous\n");
269 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
275 if ((cmd_table = cmd->more) != 0) {
276 /* XXX usually no more aux's. */
278 if (cmd_table == db_show_cmds)
279 aux_cmd_tablep = SET_BEGIN(db_show_cmd_set);
280 aux_cmd_tablep_end = SET_LIMIT(db_show_cmd_set);
284 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
291 if ((cmd->flag & CS_OWN) == 0) {
294 * command [/modifier] [addr] [,count]
300 db_printf("Bad modifier\n");
304 db_strcpy(modif, db_tok_string);
311 if (db_expression(&addr)) {
312 db_dot = (db_addr_t) addr;
313 db_last_addr = db_dot;
317 addr = (db_expr_t) db_dot;
322 if (!db_expression(&count)) {
323 db_printf("Count missing\n");
332 if ((cmd->flag & CS_MORE) == 0) {
340 * Execute the command.
342 (*cmd->fcn)(addr, have_addr, count, modif);
344 if (cmd->flag & CS_SET_DOT) {
346 * If command changes dot, set dot to
347 * previous address displayed (if 'ed' style).
358 * If command does not change dot,
359 * set 'next' location to be the same.
370 static struct command db_show_all_cmds[] = {
372 { "threads", db_show_all_threads, 0, 0 },
374 { "procs", db_ps, 0, 0 },
378 static struct command db_show_cmds[] = {
379 { "all", 0, 0, db_show_all_cmds },
380 { "registers", db_show_regs, 0, 0 },
381 { "breaks", db_listbreak_cmd, 0, 0 },
383 { "thread", db_show_one_thread, 0, 0 },
386 { "port", ipc_port_print, 0, 0 },
391 static struct command db_command_table[] = {
392 { "print", db_print_cmd, 0, 0 },
393 { "p", db_print_cmd, 0, 0 },
394 { "examine", db_examine_cmd, CS_SET_DOT, 0 },
395 { "x", db_examine_cmd, CS_SET_DOT, 0 },
396 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 },
397 { "set", db_set_cmd, CS_OWN, 0 },
398 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
399 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
400 { "delete", db_delete_cmd, 0, 0 },
401 { "d", db_delete_cmd, 0, 0 },
402 { "break", db_breakpoint_cmd, 0, 0 },
403 { "dwatch", db_deletewatch_cmd, 0, 0 },
404 { "watch", db_watchpoint_cmd, CS_MORE,0 },
405 { "dhwatch", db_deletehwatch_cmd, 0, 0 },
406 { "hwatch", db_hwatchpoint_cmd, 0, 0 },
407 { "step", db_single_step_cmd, 0, 0 },
408 { "s", db_single_step_cmd, 0, 0 },
409 { "continue", db_continue_cmd, 0, 0 },
410 { "c", db_continue_cmd, 0, 0 },
411 { "until", db_trace_until_call_cmd,0, 0 },
412 { "next", db_trace_until_matching_cmd,0, 0 },
413 { "match", db_trace_until_matching_cmd,0, 0 },
414 { "trace", db_stack_trace_cmd, 0, 0 },
415 { "call", db_fncall, CS_OWN, 0 },
416 { "show", 0, 0, db_show_cmds },
417 { "ps", db_ps, 0, 0 },
418 { "gdb", db_gdb, 0, 0 },
419 { "reset", db_reset, 0, 0 },
423 static struct command *db_last_command = 0;
429 struct command *cmd = db_command_table;
431 while (cmd->name != 0) {
432 db_printf("%-12s", cmd->name);
440 * At least one non-optional command must be implemented using
441 * DB_COMMAND() so that db_cmd_set gets created. Here is one.
443 DB_COMMAND(panic, db_panic)
445 panic("from debugger");
452 * Initialize 'prev' and 'next' to dot.
457 db_cmd_loop_done = 0;
458 while (!db_cmd_loop_done) {
460 (void) setjmp(db_jmpbuf);
461 if (db_print_position() != 0)
465 (void) db_read_line();
467 db_command(&db_last_command, db_command_table,
468 SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set));
479 longjmp(db_jmpbuf, 1);
484 * Call random function:
488 db_fncall(dummy1, dummy2, dummy3, dummy4)
495 #define MAXARGS 11 /* XXX only 10 are passed */
496 db_expr_t args[MAXARGS];
499 typedef db_expr_t fcn_10args_t __P((db_expr_t, db_expr_t, db_expr_t,
500 db_expr_t, db_expr_t, db_expr_t,
501 db_expr_t, db_expr_t, db_expr_t,
506 if (!db_expression(&fn_addr)) {
507 db_printf("Bad function\n");
511 func = (fcn_10args_t *)fn_addr; /* XXX */
515 if (db_expression(&args[0])) {
517 while ((t = db_read_token()) == tCOMMA) {
518 if (nargs == MAXARGS) {
519 db_printf("Too many arguments\n");
523 if (!db_expression(&args[nargs])) {
524 db_printf("Argument missing\n");
532 if (db_read_token() != tRPAREN) {
540 while (nargs < MAXARGS) {
544 retval = (*func)(args[0], args[1], args[2], args[3], args[4],
545 args[5], args[6], args[7], args[8], args[9] );
546 db_printf("%#lr\n", (long)retval);
549 /* Enter GDB remote protocol debugger on the next trap. */
551 dev_t gdbdev = NODEV;
556 db_gdb (dummy1, dummy2, dummy3, dummy4)
563 if (gdbdev == NODEV) {
564 db_printf("No gdb port enabled. Set flag 0x80 on desired port\n");
565 db_printf("in your configuration file (currently sio only).\n");
570 db_printf("Next trap will enter %s\n",
571 boothowto & RB_GDB ? "GDB remote protocol mode"
576 db_reset(dummy1, dummy2, dummy3, dummy4)