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.
26 * $FreeBSD: src/sys/ddb/db_command.c,v 1.34.2.2 2001/07/29 22:48:36 kris Exp $
27 * $DragonFly: src/sys/ddb/db_command.c,v 1.4 2003/07/28 04:56:35 hmp Exp $
31 * Author: David B. Golub, Carnegie Mellon University
38 #include <sys/param.h>
39 #include <sys/linker_set.h>
40 #include <sys/reboot.h>
41 #include <sys/systm.h>
45 #include <ddb/db_command.h>
46 #include <ddb/db_lex.h>
47 #include <ddb/db_output.h>
49 #include <machine/md_var.h> /* needed for db_reset() */
54 * Exported global variables
56 boolean_t db_cmd_loop_done;
57 extern struct linker_set db_cmd_set;
60 db_addr_t db_last_addr;
63 extern struct linker_set db_show_cmd_set;
65 static db_cmdfcn_t db_fncall;
66 static db_cmdfcn_t db_gdb;
67 static db_cmdfcn_t db_reset;
69 /* XXX this is actually forward-static. */
70 extern struct command db_show_cmds[];
73 * if 'ed' style: 'dot' is set at start of last item printed,
74 * and '+' points to next line.
75 * Otherwise: 'dot' points to next item, '..' points to last.
77 static boolean_t db_ed_style = TRUE;
80 * Utility routine - discard tokens through end-of-line.
92 * Results of command search.
97 #define CMD_AMBIGUOUS 3
100 static void db_cmd_list __P((struct command *table,
101 struct command **aux_tablep));
102 static int db_cmd_search __P((char *name, struct command *table,
103 struct command **aux_tablep,
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));
110 * Search for command prefix.
113 db_cmd_search(name, table, aux_tablep, cmdp)
115 struct command *table;
116 struct command **aux_tablep;
117 struct command **cmdp; /* out */
120 struct command **aux_cmdp;
121 int result = CMD_NONE;
123 for (cmd = table; cmd->name != 0; cmd++) {
130 while ((c = *lp) == *rp) {
140 /* end of name, not end of command -
142 if (result == CMD_FOUND) {
143 result = CMD_AMBIGUOUS;
144 /* but keep looking for a full match -
145 this lets us match single letters */
153 if (result == CMD_NONE && aux_tablep != 0)
154 /* XXX repeat too much code. */
155 for (aux_cmdp = aux_tablep; *aux_cmdp != 0; aux_cmdp++) {
161 rp = (*aux_cmdp)->name;
162 while ((c = *lp) == *rp) {
172 /* end of name, not end of command -
174 if (result == CMD_FOUND) {
175 result = CMD_AMBIGUOUS;
176 /* but keep looking for a full match -
177 this lets us match single letters */
185 if (result == CMD_NONE) {
186 /* check for 'help' */
187 if (name[0] == 'h' && name[1] == 'e'
188 && name[2] == 'l' && name[3] == 'p')
195 db_cmd_list(table, aux_tablep)
196 struct command *table;
197 struct command **aux_tablep;
200 struct command **aux_cmdp;
202 for (cmd = table; cmd->name != 0; cmd++) {
203 db_printf("%-12s", cmd->name);
208 for (aux_cmdp = aux_tablep; *aux_cmdp != 0; aux_cmdp++) {
209 db_printf("%-12s", (*aux_cmdp)->name);
215 db_command(last_cmdp, cmd_table, aux_cmd_tablep)
216 struct command **last_cmdp; /* IN_OUT */
217 struct command *cmd_table;
218 struct command **aux_cmd_tablep;
222 char modif[TOK_STRING_SIZE];
223 db_expr_t addr, count;
224 boolean_t have_addr = FALSE;
229 /* empty line repeats last command, at 'next' */
231 addr = (db_expr_t)db_next;
236 else if (t == tEXCL) {
237 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
240 else if (t != tIDENT) {
250 result = db_cmd_search(db_tok_string,
256 db_printf("No such command\n");
260 db_printf("Ambiguous\n");
264 db_cmd_list(cmd_table, aux_cmd_tablep);
270 if ((cmd_table = cmd->more) != 0) {
271 /* XXX usually no more aux's. */
273 if (cmd_table == db_show_cmds)
275 (struct command **)&db_show_cmd_set.ls_items[0];
279 db_cmd_list(cmd_table, aux_cmd_tablep);
286 if ((cmd->flag & CS_OWN) == 0) {
289 * command [/modifier] [addr] [,count]
295 db_printf("Bad modifier\n");
299 db_strcpy(modif, db_tok_string);
306 if (db_expression(&addr)) {
307 db_dot = (db_addr_t) addr;
308 db_last_addr = db_dot;
312 addr = (db_expr_t) db_dot;
317 if (!db_expression(&count)) {
318 db_printf("Count missing\n");
327 if ((cmd->flag & CS_MORE) == 0) {
335 * Execute the command.
337 (*cmd->fcn)(addr, have_addr, count, modif);
339 if (cmd->flag & CS_SET_DOT) {
341 * If command changes dot, set dot to
342 * previous address displayed (if 'ed' style).
353 * If command does not change dot,
354 * set 'next' location to be the same.
365 static struct command db_show_all_cmds[] = {
367 { "threads", db_show_all_threads, 0, 0 },
369 { "procs", db_ps, 0, 0 },
373 static struct command db_show_cmds[] = {
374 { "all", 0, 0, db_show_all_cmds },
375 { "registers", db_show_regs, 0, 0 },
376 { "breaks", db_listbreak_cmd, 0, 0 },
378 { "thread", db_show_one_thread, 0, 0 },
381 { "port", ipc_port_print, 0, 0 },
386 static struct command db_command_table[] = {
387 { "print", db_print_cmd, 0, 0 },
388 { "p", db_print_cmd, 0, 0 },
389 { "examine", db_examine_cmd, CS_SET_DOT, 0 },
390 { "x", db_examine_cmd, CS_SET_DOT, 0 },
391 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 },
392 { "set", db_set_cmd, CS_OWN, 0 },
393 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
394 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
395 { "delete", db_delete_cmd, 0, 0 },
396 { "d", db_delete_cmd, 0, 0 },
397 { "break", db_breakpoint_cmd, 0, 0 },
398 { "dwatch", db_deletewatch_cmd, 0, 0 },
399 { "watch", db_watchpoint_cmd, CS_MORE,0 },
400 { "dhwatch", db_deletehwatch_cmd, 0, 0 },
401 { "hwatch", db_hwatchpoint_cmd, 0, 0 },
402 { "step", db_single_step_cmd, 0, 0 },
403 { "s", db_single_step_cmd, 0, 0 },
404 { "continue", db_continue_cmd, 0, 0 },
405 { "c", db_continue_cmd, 0, 0 },
406 { "until", db_trace_until_call_cmd,0, 0 },
407 { "next", db_trace_until_matching_cmd,0, 0 },
408 { "match", db_trace_until_matching_cmd,0, 0 },
409 { "trace", db_stack_trace_cmd, 0, 0 },
410 { "where", db_stack_trace_cmd, 0, 0 },
411 { "call", db_fncall, CS_OWN, 0 },
412 { "show", 0, 0, db_show_cmds },
413 { "ps", db_ps, 0, 0 },
414 { "gdb", db_gdb, 0, 0 },
415 { "reset", db_reset, 0, 0 },
419 static struct command *db_last_command = 0;
425 struct command *cmd = db_command_table;
427 while (cmd->name != 0) {
428 db_printf("%-12s", cmd->name);
436 * At least one non-optional command must be implemented using
437 * DB_COMMAND() so that db_cmd_set gets created. Here is one.
439 DB_COMMAND(panic, db_panic)
441 panic("from debugger");
448 * Initialize 'prev' and 'next' to dot.
453 db_cmd_loop_done = 0;
454 while (!db_cmd_loop_done) {
456 (void) setjmp(db_jmpbuf);
457 if (db_print_position() != 0)
461 (void) db_read_line();
463 db_command(&db_last_command, db_command_table,
464 (struct command **)&db_cmd_set.ls_items[0]);
475 longjmp(db_jmpbuf, 1);
480 * Call random function:
484 db_fncall(dummy1, dummy2, dummy3, dummy4)
491 #define MAXARGS 11 /* XXX only 10 are passed */
492 db_expr_t args[MAXARGS];
495 typedef db_expr_t fcn_10args_t __P((db_expr_t, db_expr_t, db_expr_t,
496 db_expr_t, db_expr_t, db_expr_t,
497 db_expr_t, db_expr_t, db_expr_t,
502 if (!db_expression(&fn_addr)) {
503 db_printf("Bad function\n");
507 func = (fcn_10args_t *)fn_addr; /* XXX */
511 if (db_expression(&args[0])) {
513 while ((t = db_read_token()) == tCOMMA) {
514 if (nargs == MAXARGS) {
515 db_printf("Too many arguments\n");
519 if (!db_expression(&args[nargs])) {
520 db_printf("Argument missing\n");
528 if (db_read_token() != tRPAREN) {
536 while (nargs < MAXARGS) {
540 retval = (*func)(args[0], args[1], args[2], args[3], args[4],
541 args[5], args[6], args[7], args[8], args[9] );
542 db_printf("%#lr\n", (long)retval);
545 /* Enter GDB remote protocol debugger on the next trap. */
547 dev_t gdbdev = NODEV;
552 db_gdb (dummy1, dummy2, dummy3, dummy4)
559 if (gdbdev == NODEV) {
560 db_printf("No gdb port enabled. Set flag 0x80 on desired port\n");
561 db_printf("in your configuration file (currently sio only).\n");
566 db_printf("Next trap will enter %s\n",
567 boothowto & RB_GDB ? "GDB remote protocol mode"