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>
48 #include <sys/watchdog.h>
49 #include <sys/kernel.h>
52 #include <ddb/db_command.h>
53 #include <ddb/db_lex.h>
54 #include <ddb/db_output.h>
56 #include <machine/cpu.h>
57 #include <machine/setjmp.h>
60 * Exported global variables
62 boolean_t db_cmd_loop_done;
64 db_addr_t db_last_addr;
68 static db_cmdfcn_t db_dump;
69 static db_cmdfcn_t db_fncall;
70 static db_cmdfcn_t db_gdb;
71 static db_cmdfcn_t db_halt;
72 static db_cmdfcn_t db_kill;
73 static db_cmdfcn_t db_reset;
74 static db_cmdfcn_t db_stack_trace;
75 static db_cmdfcn_t db_stack_trace_all;
76 static db_cmdfcn_t db_watchdog;
82 static struct command db_show_all_cmds[] = {
83 { "trace", db_stack_trace_all, 0, 0 },
85 struct command_table db_show_all_table =
86 LIST_HEAD_INITIALIZER(db_show_all_table);
88 static struct command db_show_cmds[] = {
89 { "all", 0, 0, &db_show_all_table },
90 { "registers", db_show_regs, 0, 0 },
91 { "breaks", db_listbreak_cmd, 0, 0 },
92 { "threads", db_show_threads, 0, 0 },
94 struct command_table db_show_table = LIST_HEAD_INITIALIZER(db_show_table);
96 static struct command db_cmds[] = {
97 { "print", db_print_cmd, 0, 0 },
98 { "p", db_print_cmd, 0, 0 },
99 { "examine", db_examine_cmd, CS_SET_DOT, 0 },
100 { "x", db_examine_cmd, CS_SET_DOT, 0 },
101 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 },
102 { "set", db_set_cmd, CS_OWN, 0 },
103 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
104 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
105 { "delete", db_delete_cmd, 0, 0 },
106 { "d", db_delete_cmd, 0, 0 },
107 { "dump", db_dump, 0, 0 },
108 { "break", db_breakpoint_cmd, 0, 0 },
109 { "b", db_breakpoint_cmd, 0, 0 },
110 { "dwatch", db_deletewatch_cmd, 0, 0 },
111 { "watch", db_watchpoint_cmd, CS_MORE,0 },
112 { "dhwatch", db_deletehwatch_cmd, 0, 0 },
113 { "hwatch", db_hwatchpoint_cmd, 0, 0 },
114 { "step", db_single_step_cmd, 0, 0 },
115 { "s", db_single_step_cmd, 0, 0 },
116 { "continue", db_continue_cmd, 0, 0 },
117 { "c", db_continue_cmd, 0, 0 },
118 { "until", db_trace_until_call_cmd,0, 0 },
119 { "next", db_trace_until_matching_cmd,0, 0 },
120 { "match", db_trace_until_matching_cmd,0, 0 },
121 { "trace", db_stack_trace, CS_OWN, 0 },
122 { "t", db_stack_trace, CS_OWN, 0 },
123 /* XXX alias for all trace */
124 { "alltrace", db_stack_trace_all, 0, 0 },
125 { "where", db_stack_trace, CS_OWN, 0 },
126 { "bt", db_stack_trace, CS_OWN, 0 },
127 { "call", db_fncall, CS_OWN, 0 },
128 { "show", 0, 0, &db_show_table },
129 { "ps", db_ps, 0, 0 },
130 { "gdb", db_gdb, 0, 0 },
131 { "halt", db_halt, 0, 0 },
132 { "reboot", db_reset, 0, 0 },
133 { "reset", db_reset, 0, 0 },
134 { "kill", db_kill, CS_OWN, 0 },
135 { "watchdog", db_watchdog, CS_OWN, 0 },
136 { "thread", db_set_thread, CS_OWN, 0 },
137 { "run", db_run_cmd, CS_OWN, 0 },
138 { "script", db_script_cmd, CS_OWN, 0 },
139 { "scripts", db_scripts_cmd, 0, 0 },
140 { "unscript", db_unscript_cmd, CS_OWN, 0 },
141 { "capture", db_capture_cmd, CS_OWN, 0 },
142 { "textdump", db_textdump_cmd, CS_OWN, 0 },
143 { "findstack", db_findstack_cmd, 0, 0 },
145 struct command_table db_cmd_table = LIST_HEAD_INITIALIZER(db_cmd_table);
147 static struct command *db_last_command = 0;
150 * if 'ed' style: 'dot' is set at start of last item printed,
151 * and '+' points to next line.
152 * Otherwise: 'dot' points to next item, '..' points to last.
154 static boolean_t db_ed_style = TRUE;
157 * Utility routine - discard tokens through end-of-line.
169 * Results of command search.
174 #define CMD_AMBIGUOUS 3
177 static void db_cmd_match(char *name, struct command *cmd,
178 struct command **cmdp, int *resultp);
179 static void db_cmd_list(struct command_table *table);
180 static int db_cmd_search(char *name, struct command_table *table,
181 struct command **cmdp);
182 static void db_command(struct command **last_cmdp,
183 struct command_table *cmd_table, int dopager);
186 * Initialize the command lists from the static tables.
189 db_command_init(void)
191 #define N(a) (sizeof(a) / sizeof(a[0]))
194 for (i = 0; i < N(db_cmds); i++)
195 db_command_register(&db_cmd_table, &db_cmds[i]);
196 for (i = 0; i < N(db_show_cmds); i++)
197 db_command_register(&db_show_table, &db_show_cmds[i]);
198 for (i = 0; i < N(db_show_all_cmds); i++)
199 db_command_register(&db_show_all_table, &db_show_all_cmds[i]);
204 * Register a command.
207 db_command_register(struct command_table *list, struct command *cmd)
209 struct command *c, *last;
212 LIST_FOREACH(c, list, next) {
213 int n = strcmp(cmd->name, c->name);
215 /* Check that the command is not already present. */
217 printf("%s: Warning, the command \"%s\" already exists;"
218 " ignoring request\n", __func__, cmd->name);
222 /* NB: keep list sorted lexicographically */
223 LIST_INSERT_BEFORE(c, cmd, next);
229 LIST_INSERT_HEAD(list, cmd, next);
231 LIST_INSERT_AFTER(last, cmd, next);
235 * Remove a command previously registered with db_command_register.
238 db_command_unregister(struct command_table *list, struct command *cmd)
242 LIST_FOREACH(c, list, next) {
244 LIST_REMOVE(cmd, next);
248 /* NB: intentionally quiet */
252 * Helper function to match a single command.
255 db_cmd_match(name, cmd, cmdp, resultp)
258 struct command **cmdp; /* out */
266 while ((c = *lp) == *rp) {
270 *resultp = CMD_UNIQUE;
277 /* end of name, not end of command -
279 if (*resultp == CMD_FOUND) {
280 *resultp = CMD_AMBIGUOUS;
281 /* but keep looking for a full match -
282 this lets us match single letters */
285 *resultp = CMD_FOUND;
291 * Search for command prefix.
294 db_cmd_search(name, table, cmdp)
296 struct command_table *table;
297 struct command **cmdp; /* out */
300 int result = CMD_NONE;
302 LIST_FOREACH(cmd, table, next) {
303 db_cmd_match(name,cmd,cmdp,&result);
304 if (result == CMD_UNIQUE)
308 if (result == CMD_NONE) {
309 /* check for 'help' */
310 if (name[0] == 'h' && name[1] == 'e'
311 && name[2] == 'l' && name[3] == 'p')
319 struct command_table *table;
321 register struct command *cmd;
323 LIST_FOREACH(cmd, table, next) {
324 db_printf("%-12s", cmd->name);
330 db_command(last_cmdp, cmd_table, dopager)
331 struct command **last_cmdp; /* IN_OUT */
332 struct command_table *cmd_table;
335 struct command *cmd = NULL;
337 char modif[TOK_STRING_SIZE];
338 db_expr_t addr, count;
339 boolean_t have_addr = FALSE;
344 /* empty line repeats last command, at 'next' */
346 addr = (db_expr_t)db_next;
351 else if (t == tEXCL) {
352 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
355 else if (t != tIDENT) {
365 result = db_cmd_search(db_tok_string,
370 db_printf("No such command\n");
374 db_printf("Ambiguous\n");
378 db_cmd_list(cmd_table);
384 if ((cmd_table = cmd->more) != NULL) {
387 db_cmd_list(cmd_table);
394 if ((cmd->flag & CS_OWN) == 0) {
397 * command [/modifier] [addr] [,count]
403 db_printf("Bad modifier\n");
407 db_strcpy(modif, db_tok_string);
414 if (db_expression(&addr)) {
415 db_dot = (db_addr_t) addr;
416 db_last_addr = db_dot;
420 addr = (db_expr_t) db_dot;
425 if (!db_expression(&count)) {
426 db_printf("Count missing\n");
435 if ((cmd->flag & CS_MORE) == 0) {
443 * Execute the command.
449 (*cmd->fcn)(addr, have_addr, count, modif);
453 if (cmd->flag & CS_SET_DOT) {
455 * If command changes dot, set dot to
456 * previous address displayed (if 'ed' style).
467 * If command does not change dot,
468 * set 'next' location to be the same.
476 * At least one non-optional command must be implemented using
477 * DB_COMMAND() so that db_cmd_set gets created. Here is one.
479 DB_COMMAND(panic, db_panic)
482 panic("from debugger");
489 * Initialize 'prev' and 'next' to dot.
494 db_cmd_loop_done = 0;
495 while (!db_cmd_loop_done) {
496 if (db_print_position() != 0)
500 (void) db_read_line();
502 db_command(&db_last_command, &db_cmd_table, /* dopager */ 1);
507 * Execute a command on behalf of a script. The caller is responsible for
508 * making sure that the command string is < DB_MAXLINE or it will be
511 * XXXRW: Runs by injecting faked input into DDB input stream; it would be
512 * nicer to use an alternative approach that didn't mess with the previous
516 db_command_script(const char *command)
518 db_prev = db_next = db_dot;
519 db_inject_line(command);
520 db_command(&db_last_command, &db_cmd_table, /* dopager */ 0);
534 db_dump(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
538 error = doadump(FALSE);
540 db_printf("Cannot dump: ");
543 db_printf("debugger got invoked while dumping.\n");
546 db_printf("no dump device specified.\n");
549 db_printf("unknown error (error=%d).\n", error);
556 * Call random function:
560 /* The generic implementation supports a maximum of 10 arguments. */
561 typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
562 db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
565 db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
567 __db_f *f = (__db_f *)addr;
570 db_printf("Too many arguments (max 10)\n");
573 *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
574 args[6], args[7], args[8], args[9]);
579 db_fncall(dummy1, dummy2, dummy3, dummy4)
586 db_expr_t args[DB_MAXARGS];
591 if (!db_expression(&fn_addr)) {
592 db_printf("Bad function\n");
599 if (db_expression(&args[0])) {
601 while ((t = db_read_token()) == tCOMMA) {
602 if (nargs == DB_MAXARGS) {
603 db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
607 if (!db_expression(&args[nargs])) {
608 db_printf("Argument missing\n");
616 if (db_read_token() != tRPAREN) {
625 if (DB_CALL(fn_addr, &retval, nargs, args))
626 db_printf("= %#lr\n", (long)retval);
630 db_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
637 db_kill(dummy1, dummy2, dummy3, dummy4)
643 db_expr_t old_radix, pid, sig;
646 #define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0)
649 * PIDs and signal numbers are typically represented in base
650 * 10, so make that the default here. It can, of course, be
651 * overridden by specifying a prefix.
653 old_radix = db_radix;
655 /* Retrieve arguments. */
656 if (!db_expression(&sig))
657 DB_ERROR(("Missing signal number\n"));
658 if (!db_expression(&pid))
659 DB_ERROR(("Missing process ID\n"));
661 if (!_SIG_VALID(sig))
662 DB_ERROR(("Signal number out of range\n"));
665 * Find the process in question. allproc_lock is not needed
666 * since we're in DDB.
668 /* sx_slock(&allproc_lock); */
669 FOREACH_PROC_IN_SYSTEM(p)
672 /* sx_sunlock(&allproc_lock); */
674 DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
676 /* If it's already locked, bail; otherwise, do the deed. */
677 if (PROC_TRYLOCK(p) == 0)
678 DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
680 pksignal(p, sig, NULL);
685 db_radix = old_radix;
690 * Reboot. In case there is an additional argument, take it as delay in
691 * seconds. Default to 15s if we cannot parse it and make sure we will
692 * never wait longer than 1 week. Some code is similar to
693 * kern_shutdown.c:shutdown_panic().
695 #ifndef DB_RESET_MAXDELAY
696 #define DB_RESET_MAXDELAY (3600 * 24 * 7)
700 db_reset(db_expr_t addr, boolean_t have_addr, db_expr_t count __unused,
701 char *modif __unused)
706 delay = (int)db_hex2dec(addr);
708 /* If we parse to fail, use 15s. */
712 /* Cap at one week. */
713 if ((uintmax_t)delay > (uintmax_t)DB_RESET_MAXDELAY)
714 delay = DB_RESET_MAXDELAY;
716 db_printf("Automatic reboot in %d seconds - "
717 "press a key on the console to abort\n", delay);
718 for (loop = delay * 10; loop > 0; --loop) {
719 DELAY(1000 * 100); /* 1/10th second */
720 /* Did user type a key? */
721 if (cncheckc() != -1)
730 db_watchdog(dummy1, dummy2, dummy3, dummy4)
736 db_expr_t old_radix, tout;
739 old_radix = db_radix;
741 err = db_expression(&tout);
743 db_radix = old_radix;
745 /* If no argument is provided the watchdog will just be disabled. */
747 db_printf("No argument provided, disabling watchdog\n");
749 } else if ((tout & WD_INTERVAL) == WD_TO_NEVER) {
750 db_error("Out of range watchdog interval\n");
753 EVENTHANDLER_INVOKE(watchdog_list, tout, &i);
757 db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
760 if (kdb_dbbe_select("gdb") != 0) {
761 db_printf("The remote GDB backend could not be selected.\n");
765 * Mark that we are done in the debugger. kdb_trap()
766 * should re-enter with the new backend.
768 db_cmd_loop_done = 1;
769 db_printf("(ctrl-c will return control to ddb)\n");
773 db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
781 * We parse our own arguments. We don't like the default radix.
785 hastid = db_expression(&tid);
788 if (!db_expression(&count)) {
789 db_printf("Count missing\n");
801 td = kdb_thr_lookup((lwpid_t)tid);
803 td = kdb_thr_from_pid((pid_t)tid);
805 db_printf("Thread %d not found\n", (int)tid);
810 if (td->td_proc != NULL)
811 pid = td->td_proc->p_pid;
814 db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
815 db_trace_thread(td, count);
819 db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
827 FOREACH_PROC_IN_SYSTEM(p) {
828 prev_jb = kdb_jmpbuf(jb);
829 if (setjmp(jb) == 0) {
830 FOREACH_THREAD_IN_PROC(p, td) {
831 db_printf("\nTracing command %s pid %d tid %ld td %p\n",
832 p->p_comm, p->p_pid, (long)td->td_tid, td);
833 db_trace_thread(td, -1);
845 * Take the parsed expression value from the command line that was parsed
846 * as a hexadecimal value and convert it as if the expression was parsed
847 * as a decimal value. Returns -1 if the expression was not a valid
851 db_hex2dec(db_expr_t expr)
862 val += (x % 16) * (y);