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 $
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> /* needed for db_reset() */
53 * Exported global variables
55 boolean_t db_cmd_loop_done
;
58 db_addr_t db_last_addr
;
62 SET_DECLARE(db_cmd_set
, struct command
);
63 SET_DECLARE(db_show_cmd_set
, struct command
);
65 static db_cmdfcn_t db_fncall
;
66 static db_cmdfcn_t db_gdb
;
67 static db_cmdfcn_t db_reset
;
73 static struct command db_show_all_cmds
[] = {
75 { "threads", db_show_all_threads
, 0, 0 },
77 { "procs", db_ps
, 0, 0 },
81 static struct command db_show_cmds
[] = {
82 { "all", 0, 0, db_show_all_cmds
},
83 { "registers", db_show_regs
, 0, 0 },
84 { "breaks", db_listbreak_cmd
, 0, 0 },
86 { "thread", db_show_one_thread
, 0, 0 },
89 { "port", ipc_port_print
, 0, 0 },
94 static struct command db_command_table
[] = {
95 { "print", db_print_cmd
, 0, 0 },
96 { "p", db_print_cmd
, 0, 0 },
97 { "examine", db_examine_cmd
, CS_SET_DOT
, 0 },
98 { "x", db_examine_cmd
, CS_SET_DOT
, 0 },
99 { "search", db_search_cmd
, CS_OWN
|CS_SET_DOT
, 0 },
100 { "set", db_set_cmd
, CS_OWN
, 0 },
101 { "write", db_write_cmd
, CS_MORE
|CS_SET_DOT
, 0 },
102 { "w", db_write_cmd
, CS_MORE
|CS_SET_DOT
, 0 },
103 { "delete", db_delete_cmd
, 0, 0 },
104 { "d", db_delete_cmd
, 0, 0 },
105 { "break", db_breakpoint_cmd
, 0, 0 },
106 { "dwatch", db_deletewatch_cmd
, 0, 0 },
107 { "watch", db_watchpoint_cmd
, CS_MORE
,0 },
108 { "dhwatch", db_deletehwatch_cmd
, 0, 0 },
109 { "hwatch", db_hwatchpoint_cmd
, 0, 0 },
110 { "step", db_single_step_cmd
, 0, 0 },
111 { "s", db_single_step_cmd
, 0, 0 },
112 { "continue", db_continue_cmd
, 0, 0 },
113 { "c", db_continue_cmd
, 0, 0 },
114 { "until", db_trace_until_call_cmd
,0, 0 },
115 { "next", db_trace_until_matching_cmd
,0, 0 },
116 { "match", db_trace_until_matching_cmd
,0, 0 },
117 { "trace", db_stack_trace_cmd
, 0, 0 },
118 { "where", db_stack_trace_cmd
, 0, 0 },
119 { "call", db_fncall
, CS_OWN
, 0 },
120 { "show", 0, 0, db_show_cmds
},
121 { "ps", db_ps
, 0, 0 },
122 { "gdb", db_gdb
, 0, 0 },
123 { "reset", db_reset
, 0, 0 },
127 static struct command
*db_last_command
= NULL
;
130 * if 'ed' style: 'dot' is set at start of last item printed,
131 * and '+' points to next line.
132 * Otherwise: 'dot' points to next item, '..' points to last.
134 static boolean_t db_ed_style
= TRUE
;
137 * Utility routine - discard tokens through end-of-line.
149 * Results of command search.
154 #define CMD_AMBIGUOUS 3
157 static void db_cmd_list (struct command
*table
,
158 struct command
**aux_tablep
,
159 struct command
**aux_tablep_end
);
160 static int db_cmd_search (char *name
, struct command
*table
,
161 struct command
**aux_tablep
,
162 struct command
**aux_tablep_end
,
163 struct command
**cmdp
);
164 static void db_command (struct command
**last_cmdp
,
165 struct command
*cmd_table
,
166 struct command
**aux_cmd_tablep
,
167 struct command
**aux_cmd_tablep_end
);
170 * Search for command prefix.
176 db_cmd_search(char *name
, struct command
*table
, struct command
**aux_tablep
,
177 struct command
**aux_tablep_end
, struct command
**cmdp
)
180 struct command
**aux_cmdp
;
181 int result
= CMD_NONE
;
183 for (cmd
= table
; cmd
->name
!= 0; cmd
++) {
190 while ((c
= *lp
) == *rp
) {
200 /* end of name, not end of command -
202 if (result
== CMD_FOUND
) {
203 result
= CMD_AMBIGUOUS
;
204 /* but keep looking for a full match -
205 this lets us match single letters */
213 if (result
== CMD_NONE
&& aux_tablep
!= NULL
)
214 /* XXX repeat too much code. */
215 for (aux_cmdp
= aux_tablep
; aux_cmdp
< aux_tablep_end
; aux_cmdp
++) {
221 rp
= (*aux_cmdp
)->name
;
222 while ((c
= *lp
) == *rp
) {
232 /* end of name, not end of command -
234 if (result
== CMD_FOUND
) {
235 result
= CMD_AMBIGUOUS
;
236 /* but keep looking for a full match -
237 this lets us match single letters */
245 if (result
== CMD_NONE
) {
246 /* check for 'help' */
247 if (name
[0] == 'h' && name
[1] == 'e'
248 && name
[2] == 'l' && name
[3] == 'p')
255 db_cmd_list(struct command
*table
, struct command
**aux_tablep
,
256 struct command
**aux_tablep_end
)
259 struct command
**aux_cmdp
;
261 for (cmd
= table
; cmd
->name
!= 0; cmd
++) {
262 db_printf("%-12s", cmd
->name
);
265 if (aux_tablep
== NULL
)
267 for (aux_cmdp
= aux_tablep
; aux_cmdp
< aux_tablep_end
; aux_cmdp
++) {
268 db_printf("%-12s", (*aux_cmdp
)->name
);
278 db_command(struct command
**last_cmdp
, struct command
*cmd_table
,
279 struct command
**aux_cmd_tablep
, struct command
**aux_cmd_tablep_end
)
283 char modif
[TOK_STRING_SIZE
];
284 db_expr_t addr
, count
;
285 boolean_t have_addr
= FALSE
;
291 /* empty line repeats last command, at 'next' */
293 addr
= (db_expr_t
)db_next
;
298 else if (t
== tEXCL
) {
299 db_fncall((db_expr_t
)0, (boolean_t
)0, (db_expr_t
)0, NULL
);
302 else if (t
!= tIDENT
) {
312 result
= db_cmd_search(db_tok_string
,
319 db_printf("No such command\n");
323 db_printf("Ambiguous\n");
327 db_cmd_list(cmd_table
, aux_cmd_tablep
, aux_cmd_tablep_end
);
333 if ((cmd_table
= cmd
->more
) != NULL
) {
334 /* XXX usually no more aux's. */
335 aux_cmd_tablep
= NULL
;
336 if (cmd_table
== db_show_cmds
) {
337 aux_cmd_tablep
= SET_BEGIN(db_show_cmd_set
);
338 aux_cmd_tablep_end
= SET_LIMIT(db_show_cmd_set
);
343 db_cmd_list(cmd_table
, aux_cmd_tablep
, aux_cmd_tablep_end
);
350 if ((cmd
->flag
& CS_OWN
) == 0) {
353 * command [/modifier] [addr] [,count]
358 if (t
!= tIDENT
&& t
!= tNUMBER
) {
359 db_printf("Bad modifier\n");
363 db_strcpy(modif
, db_tok_string
);
370 if (db_expression(&addr
)) {
371 db_dot
= (db_addr_t
) addr
;
372 db_last_addr
= db_dot
;
376 addr
= (db_expr_t
) db_dot
;
381 if (!db_expression(&count
)) {
382 db_printf("Count missing\n");
391 if ((cmd
->flag
& CS_MORE
) == 0) {
399 * Execute the command.
401 (*cmd
->fcn
)(addr
, have_addr
, count
, modif
);
403 if (cmd
->flag
& CS_SET_DOT
) {
405 * If command changes dot, set dot to
406 * previous address displayed (if 'ed' style).
417 * If command does not change dot,
418 * set 'next' location to be the same.
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");
449 db_command_loop(void)
452 * Initialize 'prev' and 'next' to dot.
457 db_cmd_loop_done
= 0;
458 while (!db_cmd_loop_done
) {
461 if (db_print_position() != 0)
467 db_command(&db_last_command
, db_command_table
,
468 SET_BEGIN(db_cmd_set
), SET_LIMIT(db_cmd_set
));
478 longjmp(db_jmpbuf
, 1);
483 * Call random function:
487 db_fncall(db_expr_t dummy1
, boolean_t dummy2
, db_expr_t dummy3
, char *dummy4
)
490 #define MAXARGS 11 /* XXX only 10 are passed */
491 db_expr_t args
[MAXARGS
];
494 typedef db_expr_t
fcn_10args_t (db_expr_t
, db_expr_t
, db_expr_t
,
495 db_expr_t
, db_expr_t
, db_expr_t
,
496 db_expr_t
, db_expr_t
, db_expr_t
,
501 if (!db_expression(&fn_addr
)) {
502 db_printf("Bad function\n");
506 func
= (fcn_10args_t
*)fn_addr
; /* XXX */
510 if (db_expression(&args
[0])) {
512 while ((t
= db_read_token()) == tCOMMA
) {
513 if (nargs
== MAXARGS
) {
514 db_printf("Too many arguments\n");
518 if (!db_expression(&args
[nargs
])) {
519 db_printf("Argument missing\n");
527 if (db_read_token() != tRPAREN
) {
535 while (nargs
< MAXARGS
) {
539 retval
= (*func
)(args
[0], args
[1], args
[2], args
[3], args
[4],
540 args
[5], args
[6], args
[7], args
[8], args
[9] );
541 db_printf("%#lr\n", (long)retval
);
544 /* Enter GDB remote protocol debugger on the next trap. */
547 db_gdb(db_expr_t dummy1
, boolean_t dummy2
, db_expr_t dummy3
, char *dummy4
)
549 if (gdb_tab
== NULL
) {
550 db_printf("No gdb port enabled. Set flag 0x80 on desired port\n");
551 db_printf("in your configuration file (currently sio only).\n");
556 db_printf("Next trap will enter %s\n",
557 boothowto
& RB_GDB
? "GDB remote protocol mode"
562 db_reset(db_expr_t dummy1
, boolean_t dummy2
, db_expr_t dummy3
, char * dummy4
)