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.
27 * Author: David B. Golub, Carnegie Mellon University
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/linker_set.h>
41 #include <sys/mutex.h>
43 #include <sys/reboot.h>
44 #include <sys/signalvar.h>
45 #include <sys/systm.h>
47 #include <sys/watchdog.h>
50 #include <ddb/db_command.h>
51 #include <ddb/db_lex.h>
52 #include <ddb/db_output.h>
54 #include <machine/cpu.h>
55 #include <machine/setjmp.h>
58 * Exported global variables
60 boolean_t db_cmd_loop_done;
62 db_addr_t db_last_addr;
66 SET_DECLARE(db_cmd_set, struct command);
67 SET_DECLARE(db_show_cmd_set, struct command);
68 SET_DECLARE(db_show_all_cmd_set, struct command);
70 static db_cmdfcn_t db_fncall;
71 static db_cmdfcn_t db_gdb;
72 static db_cmdfcn_t db_halt;
73 static db_cmdfcn_t db_kill;
74 static db_cmdfcn_t db_reset;
75 static db_cmdfcn_t db_stack_trace;
76 static db_cmdfcn_t db_stack_trace_all;
77 static db_cmdfcn_t db_watchdog;
83 static struct command db_show_all_cmds[] = {
87 static struct command_table db_show_all_table = {
89 SET_BEGIN(db_show_all_cmd_set),
90 SET_LIMIT(db_show_all_cmd_set)
93 static struct command db_show_cmds[] = {
94 { "all", 0, 0, &db_show_all_table },
95 { "registers", db_show_regs, 0, 0 },
96 { "breaks", db_listbreak_cmd, 0, 0 },
97 { "threads", db_show_threads, 0, 0 },
101 static struct command_table db_show_table = {
103 SET_BEGIN(db_show_cmd_set),
104 SET_LIMIT(db_show_cmd_set)
107 static struct command db_commands[] = {
108 { "print", db_print_cmd, 0, 0 },
109 { "p", db_print_cmd, 0, 0 },
110 { "examine", db_examine_cmd, CS_SET_DOT, 0 },
111 { "x", db_examine_cmd, CS_SET_DOT, 0 },
112 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 },
113 { "set", db_set_cmd, CS_OWN, 0 },
114 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
115 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
116 { "delete", db_delete_cmd, 0, 0 },
117 { "d", db_delete_cmd, 0, 0 },
118 { "break", db_breakpoint_cmd, 0, 0 },
119 { "b", db_breakpoint_cmd, 0, 0 },
120 { "dwatch", db_deletewatch_cmd, 0, 0 },
121 { "watch", db_watchpoint_cmd, CS_MORE,0 },
122 { "dhwatch", db_deletehwatch_cmd, 0, 0 },
123 { "hwatch", db_hwatchpoint_cmd, 0, 0 },
124 { "step", db_single_step_cmd, 0, 0 },
125 { "s", db_single_step_cmd, 0, 0 },
126 { "continue", db_continue_cmd, 0, 0 },
127 { "c", db_continue_cmd, 0, 0 },
128 { "until", db_trace_until_call_cmd,0, 0 },
129 { "next", db_trace_until_matching_cmd,0, 0 },
130 { "match", db_trace_until_matching_cmd,0, 0 },
131 { "trace", db_stack_trace, CS_OWN, 0 },
132 { "t", db_stack_trace, CS_OWN, 0 },
133 { "alltrace", db_stack_trace_all, 0, 0 },
134 { "where", db_stack_trace, CS_OWN, 0 },
135 { "bt", db_stack_trace, CS_OWN, 0 },
136 { "call", db_fncall, CS_OWN, 0 },
137 { "show", 0, 0, &db_show_table },
138 { "ps", db_ps, 0, 0 },
139 { "gdb", db_gdb, 0, 0 },
140 { "halt", db_halt, 0, 0 },
141 { "reboot", db_reset, 0, 0 },
142 { "reset", db_reset, 0, 0 },
143 { "kill", db_kill, CS_OWN, 0 },
144 { "watchdog", db_watchdog, 0, 0 },
145 { "thread", db_set_thread, CS_OWN, 0 },
146 { "run", db_run_cmd, CS_OWN, 0 },
147 { "script", db_script_cmd, CS_OWN, 0 },
148 { "scripts", db_scripts_cmd, 0, 0 },
149 { "unscript", db_unscript_cmd, CS_OWN, 0 },
150 { "capture", db_capture_cmd, CS_OWN, 0 },
151 { "textdump", db_textdump_cmd, CS_OWN, 0 },
155 static struct command_table db_command_table = {
157 SET_BEGIN(db_cmd_set),
158 SET_LIMIT(db_cmd_set)
161 static struct command *db_last_command = 0;
164 * if 'ed' style: 'dot' is set at start of last item printed,
165 * and '+' points to next line.
166 * Otherwise: 'dot' points to next item, '..' points to last.
168 static boolean_t db_ed_style = TRUE;
171 * Utility routine - discard tokens through end-of-line.
183 * Results of command search.
188 #define CMD_AMBIGUOUS 3
191 static void db_cmd_match(char *name, struct command *cmd,
192 struct command **cmdp, int *resultp);
193 static void db_cmd_list(struct command_table *table);
194 static int db_cmd_search(char *name, struct command_table *table,
195 struct command **cmdp);
196 static void db_command(struct command **last_cmdp,
197 struct command_table *cmd_table, int dopager);
200 * Helper function to match a single command.
203 db_cmd_match(name, cmd, cmdp, resultp)
206 struct command **cmdp; /* out */
214 while ((c = *lp) == *rp) {
218 *resultp = CMD_UNIQUE;
225 /* end of name, not end of command -
227 if (*resultp == CMD_FOUND) {
228 *resultp = CMD_AMBIGUOUS;
229 /* but keep looking for a full match -
230 this lets us match single letters */
233 *resultp = CMD_FOUND;
239 * Search for command prefix.
242 db_cmd_search(name, table, cmdp)
244 struct command_table *table;
245 struct command **cmdp; /* out */
248 struct command **aux_cmdp;
249 int result = CMD_NONE;
251 for (cmd = table->table; cmd->name != 0; cmd++) {
252 db_cmd_match(name, cmd, cmdp, &result);
253 if (result == CMD_UNIQUE)
256 if (table->aux_tablep != NULL)
257 for (aux_cmdp = table->aux_tablep;
258 aux_cmdp < table->aux_tablep_end;
260 db_cmd_match(name, *aux_cmdp, cmdp, &result);
261 if (result == CMD_UNIQUE)
264 if (result == CMD_NONE) {
265 /* check for 'help' */
266 if (name[0] == 'h' && name[1] == 'e'
267 && name[2] == 'l' && name[3] == 'p')
275 struct command_table *table;
277 register struct command *cmd;
278 register struct command **aux_cmdp;
280 for (cmd = table->table; cmd->name != 0; cmd++) {
281 db_printf("%-12s", cmd->name);
284 if (table->aux_tablep == NULL)
286 for (aux_cmdp = table->aux_tablep; aux_cmdp < table->aux_tablep_end;
288 db_printf("%-12s", (*aux_cmdp)->name);
294 db_command(last_cmdp, cmd_table, dopager)
295 struct command **last_cmdp; /* IN_OUT */
296 struct command_table *cmd_table;
301 char modif[TOK_STRING_SIZE];
302 db_expr_t addr, count;
303 boolean_t have_addr = FALSE;
308 /* empty line repeats last command, at 'next' */
310 addr = (db_expr_t)db_next;
315 else if (t == tEXCL) {
316 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
319 else if (t != tIDENT) {
329 result = db_cmd_search(db_tok_string,
334 db_printf("No such command\n");
338 db_printf("Ambiguous\n");
342 db_cmd_list(cmd_table);
348 if ((cmd_table = cmd->more) != NULL) {
351 db_cmd_list(cmd_table);
358 if ((cmd->flag & CS_OWN) == 0) {
361 * command [/modifier] [addr] [,count]
367 db_printf("Bad modifier\n");
371 db_strcpy(modif, db_tok_string);
378 if (db_expression(&addr)) {
379 db_dot = (db_addr_t) addr;
380 db_last_addr = db_dot;
384 addr = (db_expr_t) db_dot;
389 if (!db_expression(&count)) {
390 db_printf("Count missing\n");
399 if ((cmd->flag & CS_MORE) == 0) {
407 * Execute the command.
413 (*cmd->fcn)(addr, have_addr, count, modif);
417 if (cmd->flag & CS_SET_DOT) {
419 * If command changes dot, set dot to
420 * previous address displayed (if 'ed' style).
431 * If command does not change dot,
432 * set 'next' location to be the same.
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)
446 panic("from debugger");
453 * Initialize 'prev' and 'next' to dot.
458 db_cmd_loop_done = 0;
459 while (!db_cmd_loop_done) {
460 if (db_print_position() != 0)
464 (void) db_read_line();
466 db_command(&db_last_command, &db_command_table, /* dopager */ 1);
471 * Execute a command on behalf of a script. The caller is responsible for
472 * making sure that the command string is < DB_MAXLINE or it will be
475 * XXXRW: Runs by injecting faked input into DDB input stream; it would be
476 * nicer to use an alternative approach that didn't mess with the previous
480 db_command_script(const char *command)
482 db_prev = db_next = db_dot;
483 db_inject_line(command);
484 db_command(&db_last_command, &db_command_table, /* dopager */ 0);
499 * Call random function:
503 /* The generic implementation supports a maximum of 10 arguments. */
504 typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
505 db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
508 db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
510 __db_f *f = (__db_f *)addr;
513 db_printf("Too many arguments (max 10)\n");
516 *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
517 args[6], args[7], args[8], args[9]);
522 db_fncall(dummy1, dummy2, dummy3, dummy4)
529 db_expr_t args[DB_MAXARGS];
534 if (!db_expression(&fn_addr)) {
535 db_printf("Bad function\n");
542 if (db_expression(&args[0])) {
544 while ((t = db_read_token()) == tCOMMA) {
545 if (nargs == DB_MAXARGS) {
546 db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
550 if (!db_expression(&args[nargs])) {
551 db_printf("Argument missing\n");
559 if (db_read_token() != tRPAREN) {
568 if (DB_CALL(fn_addr, &retval, nargs, args))
569 db_printf("= %#lr\n", (long)retval);
573 db_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
580 db_kill(dummy1, dummy2, dummy3, dummy4)
586 db_expr_t old_radix, pid, sig;
589 #define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0)
592 * PIDs and signal numbers are typically represented in base
593 * 10, so make that the default here. It can, of course, be
594 * overridden by specifying a prefix.
596 old_radix = db_radix;
598 /* Retrieve arguments. */
599 if (!db_expression(&sig))
600 DB_ERROR(("Missing signal number\n"));
601 if (!db_expression(&pid))
602 DB_ERROR(("Missing process ID\n"));
604 if (sig < 1 || sig > _SIG_MAXSIG)
605 DB_ERROR(("Signal number out of range\n"));
608 * Find the process in question. allproc_lock is not needed
609 * since we're in DDB.
611 /* sx_slock(&allproc_lock); */
612 FOREACH_PROC_IN_SYSTEM(p)
615 /* sx_sunlock(&allproc_lock); */
617 DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
619 /* If it's already locked, bail; otherwise, do the deed. */
620 if (PROC_TRYLOCK(p) == 0)
621 DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
628 db_radix = old_radix;
633 db_reset(dummy1, dummy2, dummy3, dummy4)
644 db_watchdog(dummy1, dummy2, dummy3, dummy4)
653 * XXX: It might make sense to be able to set the watchdog to a
654 * XXX: timeout here so that failure or hang as a result of subsequent
655 * XXX: ddb commands could be recovered by a reset.
658 EVENTHANDLER_INVOKE(watchdog_list, 0, &i);
662 db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
665 if (kdb_dbbe_select("gdb") != 0)
666 db_printf("The remote GDB backend could not be selected.\n");
668 db_printf("Step to enter the remote GDB backend.\n");
672 db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
680 * We parse our own arguments. We don't like the default radix.
684 hastid = db_expression(&tid);
687 if (!db_expression(&count)) {
688 db_printf("Count missing\n");
700 td = kdb_thr_lookup((lwpid_t)tid);
702 td = kdb_thr_from_pid((pid_t)tid);
704 db_printf("Thread %d not found\n", (int)tid);
709 if (td->td_proc != NULL)
710 pid = td->td_proc->p_pid;
713 db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
714 db_trace_thread(td, count);
718 db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
726 FOREACH_PROC_IN_SYSTEM(p) {
727 prev_jb = kdb_jmpbuf(jb);
728 if (setjmp(jb) == 0) {
729 FOREACH_THREAD_IN_PROC(p, td) {
730 db_printf("\nTracing command %s pid %d tid %ld td %p\n",
731 p->p_comm, p->p_pid, (long)td->td_tid, td);
732 db_trace_thread(td, -1);