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/md_var.h>
49 #include <machine/setjmp.h>
52 * Exported global variables
54 boolean_t db_cmd_loop_done;
57 db_addr_t db_last_addr;
61 SET_DECLARE(db_cmd_set, struct command);
62 SET_DECLARE(db_show_cmd_set, struct command);
64 static db_cmdfcn_t db_fncall;
65 static db_cmdfcn_t db_gdb;
66 static db_cmdfcn_t db_reset;
68 /* XXX this is actually forward-static. */
69 extern struct command db_show_cmds[];
72 * if 'ed' style: 'dot' is set at start of last item printed,
73 * and '+' points to next line.
74 * Otherwise: 'dot' points to next item, '..' points to last.
76 static boolean_t db_ed_style = TRUE;
79 * Utility routine - discard tokens through end-of-line.
91 * Results of command search.
96 #define CMD_AMBIGUOUS 3
99 static void db_cmd_list __P((struct command *table,
100 struct command **aux_tablep,
101 struct command **aux_tablep_end));
102 static int db_cmd_search __P((char *name, struct command *table,
103 struct command **aux_tablep,
104 struct command **aux_tablep_end,
105 struct command **cmdp));
106 static void db_command __P((struct command **last_cmdp,
107 struct command *cmd_table,
108 struct command **aux_cmd_tablep,
109 struct command **aux_cmd_tablep_end));
112 * Search for command prefix.
115 db_cmd_search(name, table, aux_tablep, aux_tablep_end, cmdp)
117 struct command *table;
118 struct command **aux_tablep;
119 struct command **aux_tablep_end;
120 struct command **cmdp; /* out */
123 struct command **aux_cmdp;
124 int result = CMD_NONE;
126 for (cmd = table; cmd->name != 0; cmd++) {
133 while ((c = *lp) == *rp) {
143 /* end of name, not end of command -
145 if (result == CMD_FOUND) {
146 result = CMD_AMBIGUOUS;
147 /* but keep looking for a full match -
148 this lets us match single letters */
156 if (result == CMD_NONE && aux_tablep != 0)
157 /* XXX repeat too much code. */
158 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
164 rp = (*aux_cmdp)->name;
165 while ((c = *lp) == *rp) {
175 /* end of name, not end of command -
177 if (result == CMD_FOUND) {
178 result = CMD_AMBIGUOUS;
179 /* but keep looking for a full match -
180 this lets us match single letters */
188 if (result == CMD_NONE) {
189 /* check for 'help' */
190 if (name[0] == 'h' && name[1] == 'e'
191 && name[2] == 'l' && name[3] == 'p')
198 db_cmd_list(table, aux_tablep, aux_tablep_end)
199 struct command *table;
200 struct command **aux_tablep;
201 struct command **aux_tablep_end;
203 register struct command *cmd;
204 register struct command **aux_cmdp;
206 for (cmd = table; cmd->name != 0; cmd++) {
207 db_printf("%-12s", cmd->name);
212 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
213 db_printf("%-12s", (*aux_cmdp)->name);
219 db_command(last_cmdp, cmd_table, aux_cmd_tablep, aux_cmd_tablep_end)
220 struct command **last_cmdp; /* IN_OUT */
221 struct command *cmd_table;
222 struct command **aux_cmd_tablep;
223 struct command **aux_cmd_tablep_end;
227 char modif[TOK_STRING_SIZE];
228 db_expr_t addr, count;
229 boolean_t have_addr = FALSE;
234 /* empty line repeats last command, at 'next' */
236 addr = (db_expr_t)db_next;
241 else if (t == tEXCL) {
242 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
245 else if (t != tIDENT) {
255 result = db_cmd_search(db_tok_string,
262 db_printf("No such command\n");
266 db_printf("Ambiguous\n");
270 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
276 if ((cmd_table = cmd->more) != 0) {
277 /* XXX usually no more aux's. */
279 if (cmd_table == db_show_cmds)
280 aux_cmd_tablep = SET_BEGIN(db_show_cmd_set);
281 aux_cmd_tablep_end = SET_LIMIT(db_show_cmd_set);
285 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
292 if ((cmd->flag & CS_OWN) == 0) {
295 * command [/modifier] [addr] [,count]
301 db_printf("Bad modifier\n");
305 db_strcpy(modif, db_tok_string);
312 if (db_expression(&addr)) {
313 db_dot = (db_addr_t) addr;
314 db_last_addr = db_dot;
318 addr = (db_expr_t) db_dot;
323 if (!db_expression(&count)) {
324 db_printf("Count missing\n");
333 if ((cmd->flag & CS_MORE) == 0) {
341 * Execute the command.
343 (*cmd->fcn)(addr, have_addr, count, modif);
345 if (cmd->flag & CS_SET_DOT) {
347 * If command changes dot, set dot to
348 * previous address displayed (if 'ed' style).
359 * If command does not change dot,
360 * set 'next' location to be the same.
371 static struct command db_show_all_cmds[] = {
373 { "threads", db_show_all_threads, 0, 0 },
375 { "procs", db_ps, 0, 0 },
379 static struct command db_show_cmds[] = {
380 { "all", 0, 0, db_show_all_cmds },
381 { "registers", db_show_regs, 0, 0 },
382 { "breaks", db_listbreak_cmd, 0, 0 },
384 { "thread", db_show_one_thread, 0, 0 },
387 { "port", ipc_port_print, 0, 0 },
392 static struct command db_command_table[] = {
393 { "print", db_print_cmd, 0, 0 },
394 { "p", db_print_cmd, 0, 0 },
395 { "examine", db_examine_cmd, CS_SET_DOT, 0 },
396 { "x", db_examine_cmd, CS_SET_DOT, 0 },
397 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 },
398 { "set", db_set_cmd, CS_OWN, 0 },
399 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
400 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
401 { "delete", db_delete_cmd, 0, 0 },
402 { "d", db_delete_cmd, 0, 0 },
403 { "break", db_breakpoint_cmd, 0, 0 },
404 { "dwatch", db_deletewatch_cmd, 0, 0 },
405 { "watch", db_watchpoint_cmd, CS_MORE,0 },
406 { "dhwatch", db_deletehwatch_cmd, 0, 0 },
407 { "hwatch", db_hwatchpoint_cmd, 0, 0 },
408 { "step", db_single_step_cmd, 0, 0 },
409 { "s", db_single_step_cmd, 0, 0 },
410 { "continue", db_continue_cmd, 0, 0 },
411 { "c", db_continue_cmd, 0, 0 },
412 { "until", db_trace_until_call_cmd,0, 0 },
413 { "next", db_trace_until_matching_cmd,0, 0 },
414 { "match", db_trace_until_matching_cmd,0, 0 },
415 { "trace", db_stack_trace_cmd, 0, 0 },
416 { "call", db_fncall, CS_OWN, 0 },
417 { "show", 0, 0, db_show_cmds },
418 { "ps", db_ps, 0, 0 },
419 { "gdb", db_gdb, 0, 0 },
420 { "reset", db_reset, 0, 0 },
424 static struct command *db_last_command = 0;
430 struct command *cmd = db_command_table;
432 while (cmd->name != 0) {
433 db_printf("%-12s", cmd->name);
441 * At least one non-optional command must be implemented using
442 * DB_COMMAND() so that db_cmd_set gets created. Here is one.
444 DB_COMMAND(panic, db_panic)
446 panic("from debugger");
453 * Initialize 'prev' and 'next' to dot.
458 db_cmd_loop_done = 0;
459 while (!db_cmd_loop_done) {
461 (void) setjmp(db_jmpbuf);
462 if (db_print_position() != 0)
466 (void) db_read_line();
468 db_command(&db_last_command, db_command_table,
469 SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set));
480 longjmp(db_jmpbuf, 1);
485 * Call random function:
489 db_fncall(dummy1, dummy2, dummy3, dummy4)
496 #define MAXARGS 11 /* XXX only 10 are passed */
497 db_expr_t args[MAXARGS];
500 typedef db_expr_t fcn_10args_t __P((db_expr_t, db_expr_t, db_expr_t,
501 db_expr_t, db_expr_t, db_expr_t,
502 db_expr_t, db_expr_t, db_expr_t,
507 if (!db_expression(&fn_addr)) {
508 db_printf("Bad function\n");
512 func = (fcn_10args_t *)fn_addr; /* XXX */
516 if (db_expression(&args[0])) {
518 while ((t = db_read_token()) == tCOMMA) {
519 if (nargs == MAXARGS) {
520 db_printf("Too many arguments\n");
524 if (!db_expression(&args[nargs])) {
525 db_printf("Argument missing\n");
533 if (db_read_token() != tRPAREN) {
541 while (nargs < MAXARGS) {
545 retval = (*func)(args[0], args[1], args[2], args[3], args[4],
546 args[5], args[6], args[7], args[8], args[9] );
547 db_printf("%#lr\n", (long)retval);
550 /* Enter GDB remote protocol debugger on the next trap. */
552 dev_t gdbdev = NODEV;
557 db_gdb (dummy1, dummy2, dummy3, dummy4)
564 if (gdbdev == NODEV) {
565 db_printf("No gdb port enabled. Set flag 0x80 on desired port\n");
566 db_printf("in your configuration file (currently sio only).\n");
571 db_printf("Next trap will enter %s\n",
572 boothowto & RB_GDB ? "GDB remote protocol mode"
577 db_reset(dummy1, dummy2, dummy3, dummy4)