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>
48 #include <sys/kernel.h>
51 #include <ddb/db_command.h>
52 #include <ddb/db_lex.h>
53 #include <ddb/db_output.h>
55 #include <machine/cpu.h>
56 #include <machine/setjmp.h>
59 * Exported global variables
61 boolean_t db_cmd_loop_done;
63 db_addr_t db_last_addr;
67 static db_cmdfcn_t db_fncall;
68 static db_cmdfcn_t db_gdb;
69 static db_cmdfcn_t db_halt;
70 static db_cmdfcn_t db_kill;
71 static db_cmdfcn_t db_reset;
72 static db_cmdfcn_t db_stack_trace;
73 static db_cmdfcn_t db_stack_trace_all;
74 static db_cmdfcn_t db_watchdog;
80 static struct command db_show_all_cmds[] = {
81 { "trace", db_stack_trace_all, 0, 0 },
83 struct command_table db_show_all_table =
84 LIST_HEAD_INITIALIZER(db_show_all_table);
86 static struct command db_show_cmds[] = {
87 { "all", 0, 0, &db_show_all_table },
88 { "registers", db_show_regs, 0, 0 },
89 { "breaks", db_listbreak_cmd, 0, 0 },
90 { "threads", db_show_threads, 0, 0 },
92 struct command_table db_show_table = LIST_HEAD_INITIALIZER(db_show_table);
94 static struct command db_cmds[] = {
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 { "b", db_breakpoint_cmd, 0, 0 },
107 { "dwatch", db_deletewatch_cmd, 0, 0 },
108 { "watch", db_watchpoint_cmd, CS_MORE,0 },
109 { "dhwatch", db_deletehwatch_cmd, 0, 0 },
110 { "hwatch", db_hwatchpoint_cmd, 0, 0 },
111 { "step", db_single_step_cmd, 0, 0 },
112 { "s", db_single_step_cmd, 0, 0 },
113 { "continue", db_continue_cmd, 0, 0 },
114 { "c", db_continue_cmd, 0, 0 },
115 { "until", db_trace_until_call_cmd,0, 0 },
116 { "next", db_trace_until_matching_cmd,0, 0 },
117 { "match", db_trace_until_matching_cmd,0, 0 },
118 { "trace", db_stack_trace, CS_OWN, 0 },
119 { "t", db_stack_trace, CS_OWN, 0 },
120 /* XXX alias for all trace */
121 { "alltrace", db_stack_trace_all, 0, 0 },
122 { "where", db_stack_trace, CS_OWN, 0 },
123 { "bt", db_stack_trace, CS_OWN, 0 },
124 { "call", db_fncall, CS_OWN, 0 },
125 { "show", 0, 0, &db_show_table },
126 { "ps", db_ps, 0, 0 },
127 { "gdb", db_gdb, 0, 0 },
128 { "halt", db_halt, 0, 0 },
129 { "reboot", db_reset, 0, 0 },
130 { "reset", db_reset, 0, 0 },
131 { "kill", db_kill, CS_OWN, 0 },
132 { "watchdog", db_watchdog, 0, 0 },
133 { "thread", db_set_thread, CS_OWN, 0 },
134 { "run", db_run_cmd, CS_OWN, 0 },
135 { "script", db_script_cmd, CS_OWN, 0 },
136 { "scripts", db_scripts_cmd, 0, 0 },
137 { "unscript", db_unscript_cmd, CS_OWN, 0 },
138 { "capture", db_capture_cmd, CS_OWN, 0 },
139 { "textdump", db_textdump_cmd, CS_OWN, 0 },
141 struct command_table db_cmd_table = LIST_HEAD_INITIALIZER(db_cmd_table);
143 static struct command *db_last_command = 0;
146 * if 'ed' style: 'dot' is set at start of last item printed,
147 * and '+' points to next line.
148 * Otherwise: 'dot' points to next item, '..' points to last.
150 static boolean_t db_ed_style = TRUE;
153 * Utility routine - discard tokens through end-of-line.
165 * Results of command search.
170 #define CMD_AMBIGUOUS 3
173 static void db_cmd_match(char *name, struct command *cmd,
174 struct command **cmdp, int *resultp);
175 static void db_cmd_list(struct command_table *table);
176 static int db_cmd_search(char *name, struct command_table *table,
177 struct command **cmdp);
178 static void db_command(struct command **last_cmdp,
179 struct command_table *cmd_table, int dopager);
182 * Initialize the command lists from the static tables.
185 db_command_init(void)
187 #define N(a) (sizeof(a) / sizeof(a[0]))
190 for (i = 0; i < N(db_cmds); i++)
191 db_command_register(&db_cmd_table, &db_cmds[i]);
192 for (i = 0; i < N(db_show_cmds); i++)
193 db_command_register(&db_show_table, &db_show_cmds[i]);
194 for (i = 0; i < N(db_show_all_cmds); i++)
195 db_command_register(&db_show_all_table, &db_show_all_cmds[i]);
200 * Register a command.
203 db_command_register(struct command_table *list, struct command *cmd)
205 struct command *c, *last;
208 LIST_FOREACH(c, list, next) {
209 int n = strcmp(cmd->name, c->name);
211 /* Check that the command is not already present. */
213 printf("%s: Warning, the command \"%s\" already exists;"
214 " ignoring request\n", __func__, cmd->name);
218 /* NB: keep list sorted lexicographically */
219 LIST_INSERT_BEFORE(c, cmd, next);
225 LIST_INSERT_HEAD(list, cmd, next);
227 LIST_INSERT_AFTER(last, cmd, next);
231 * Remove a command previously registered with db_command_register.
234 db_command_unregister(struct command_table *list, struct command *cmd)
238 LIST_FOREACH(c, list, next) {
240 LIST_REMOVE(cmd, next);
244 /* NB: intentionally quiet */
248 * Helper function to match a single command.
251 db_cmd_match(name, cmd, cmdp, resultp)
254 struct command **cmdp; /* out */
262 while ((c = *lp) == *rp) {
266 *resultp = CMD_UNIQUE;
273 /* end of name, not end of command -
275 if (*resultp == CMD_FOUND) {
276 *resultp = CMD_AMBIGUOUS;
277 /* but keep looking for a full match -
278 this lets us match single letters */
281 *resultp = CMD_FOUND;
287 * Search for command prefix.
290 db_cmd_search(name, table, cmdp)
292 struct command_table *table;
293 struct command **cmdp; /* out */
296 int result = CMD_NONE;
298 LIST_FOREACH(cmd, table, next) {
299 db_cmd_match(name,cmd,cmdp,&result);
300 if (result == CMD_UNIQUE)
304 if (result == CMD_NONE) {
305 /* check for 'help' */
306 if (name[0] == 'h' && name[1] == 'e'
307 && name[2] == 'l' && name[3] == 'p')
315 struct command_table *table;
317 register struct command *cmd;
319 LIST_FOREACH(cmd, table, next) {
320 db_printf("%-12s", cmd->name);
326 db_command(last_cmdp, cmd_table, dopager)
327 struct command **last_cmdp; /* IN_OUT */
328 struct command_table *cmd_table;
331 struct command *cmd = NULL;
333 char modif[TOK_STRING_SIZE];
334 db_expr_t addr, count;
335 boolean_t have_addr = FALSE;
340 /* empty line repeats last command, at 'next' */
342 addr = (db_expr_t)db_next;
347 else if (t == tEXCL) {
348 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
351 else if (t != tIDENT) {
361 result = db_cmd_search(db_tok_string,
366 db_printf("No such command\n");
370 db_printf("Ambiguous\n");
374 db_cmd_list(cmd_table);
380 if ((cmd_table = cmd->more) != NULL) {
383 db_cmd_list(cmd_table);
390 if ((cmd->flag & CS_OWN) == 0) {
393 * command [/modifier] [addr] [,count]
399 db_printf("Bad modifier\n");
403 db_strcpy(modif, db_tok_string);
410 if (db_expression(&addr)) {
411 db_dot = (db_addr_t) addr;
412 db_last_addr = db_dot;
416 addr = (db_expr_t) db_dot;
421 if (!db_expression(&count)) {
422 db_printf("Count missing\n");
431 if ((cmd->flag & CS_MORE) == 0) {
439 * Execute the command.
445 (*cmd->fcn)(addr, have_addr, count, modif);
449 if (cmd->flag & CS_SET_DOT) {
451 * If command changes dot, set dot to
452 * previous address displayed (if 'ed' style).
463 * If command does not change dot,
464 * set 'next' location to be the same.
472 * At least one non-optional command must be implemented using
473 * DB_COMMAND() so that db_cmd_set gets created. Here is one.
475 DB_COMMAND(panic, db_panic)
478 panic("from debugger");
485 * Initialize 'prev' and 'next' to dot.
490 db_cmd_loop_done = 0;
491 while (!db_cmd_loop_done) {
492 if (db_print_position() != 0)
496 (void) db_read_line();
498 db_command(&db_last_command, &db_cmd_table, /* dopager */ 1);
503 * Execute a command on behalf of a script. The caller is responsible for
504 * making sure that the command string is < DB_MAXLINE or it will be
507 * XXXRW: Runs by injecting faked input into DDB input stream; it would be
508 * nicer to use an alternative approach that didn't mess with the previous
512 db_command_script(const char *command)
514 db_prev = db_next = db_dot;
515 db_inject_line(command);
516 db_command(&db_last_command, &db_cmd_table, /* dopager */ 0);
531 * Call random function:
535 /* The generic implementation supports a maximum of 10 arguments. */
536 typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
537 db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
540 db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
542 __db_f *f = (__db_f *)addr;
545 db_printf("Too many arguments (max 10)\n");
548 *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
549 args[6], args[7], args[8], args[9]);
554 db_fncall(dummy1, dummy2, dummy3, dummy4)
561 db_expr_t args[DB_MAXARGS];
566 if (!db_expression(&fn_addr)) {
567 db_printf("Bad function\n");
574 if (db_expression(&args[0])) {
576 while ((t = db_read_token()) == tCOMMA) {
577 if (nargs == DB_MAXARGS) {
578 db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
582 if (!db_expression(&args[nargs])) {
583 db_printf("Argument missing\n");
591 if (db_read_token() != tRPAREN) {
600 if (DB_CALL(fn_addr, &retval, nargs, args))
601 db_printf("= %#lr\n", (long)retval);
605 db_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
612 db_kill(dummy1, dummy2, dummy3, dummy4)
618 db_expr_t old_radix, pid, sig;
621 #define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0)
624 * PIDs and signal numbers are typically represented in base
625 * 10, so make that the default here. It can, of course, be
626 * overridden by specifying a prefix.
628 old_radix = db_radix;
630 /* Retrieve arguments. */
631 if (!db_expression(&sig))
632 DB_ERROR(("Missing signal number\n"));
633 if (!db_expression(&pid))
634 DB_ERROR(("Missing process ID\n"));
636 if (!_SIG_VALID(sig))
637 DB_ERROR(("Signal number out of range\n"));
640 * Find the process in question. allproc_lock is not needed
641 * since we're in DDB.
643 /* sx_slock(&allproc_lock); */
644 FOREACH_PROC_IN_SYSTEM(p)
647 /* sx_sunlock(&allproc_lock); */
649 DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
651 /* If it's already locked, bail; otherwise, do the deed. */
652 if (PROC_TRYLOCK(p) == 0)
653 DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
655 pksignal(p, sig, NULL);
660 db_radix = old_radix;
665 * Reboot. In case there is an additional argument, take it as delay in
666 * seconds. Default to 15s if we cannot parse it and make sure we will
667 * never wait longer than 1 week. Some code is similar to
668 * kern_shutdown.c:shutdown_panic().
670 #ifndef DB_RESET_MAXDELAY
671 #define DB_RESET_MAXDELAY (3600 * 24 * 7)
675 db_reset(db_expr_t addr, boolean_t have_addr, db_expr_t count __unused,
676 char *modif __unused)
681 delay = (int)db_hex2dec(addr);
683 /* If we parse to fail, use 15s. */
687 /* Cap at one week. */
688 if ((uintmax_t)delay > (uintmax_t)DB_RESET_MAXDELAY)
689 delay = DB_RESET_MAXDELAY;
691 db_printf("Automatic reboot in %d seconds - "
692 "press a key on the console to abort\n", delay);
693 for (loop = delay * 10; loop > 0; --loop) {
694 DELAY(1000 * 100); /* 1/10th second */
695 /* Did user type a key? */
696 if (cncheckc() != -1)
705 db_watchdog(dummy1, dummy2, dummy3, dummy4)
714 * XXX: It might make sense to be able to set the watchdog to a
715 * XXX: timeout here so that failure or hang as a result of subsequent
716 * XXX: ddb commands could be recovered by a reset.
719 EVENTHANDLER_INVOKE(watchdog_list, 0, &i);
723 db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
726 if (kdb_dbbe_select("gdb") != 0)
727 db_printf("The remote GDB backend could not be selected.\n");
729 db_printf("Step to enter the remote GDB backend.\n");
733 db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
741 * We parse our own arguments. We don't like the default radix.
745 hastid = db_expression(&tid);
748 if (!db_expression(&count)) {
749 db_printf("Count missing\n");
761 td = kdb_thr_lookup((lwpid_t)tid);
763 td = kdb_thr_from_pid((pid_t)tid);
765 db_printf("Thread %d not found\n", (int)tid);
770 if (td->td_proc != NULL)
771 pid = td->td_proc->p_pid;
774 db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
775 db_trace_thread(td, count);
779 db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
787 FOREACH_PROC_IN_SYSTEM(p) {
788 prev_jb = kdb_jmpbuf(jb);
789 if (setjmp(jb) == 0) {
790 FOREACH_THREAD_IN_PROC(p, td) {
791 db_printf("\nTracing command %s pid %d tid %ld td %p\n",
792 p->p_comm, p->p_pid, (long)td->td_tid, td);
793 db_trace_thread(td, -1);
805 * Take the parsed expression value from the command line that was parsed
806 * as a hexadecimal value and convert it as if the expression was parsed
807 * as a decimal value. Returns -1 if the expression was not a valid
811 db_hex2dec(db_expr_t expr)
822 val += (x % 16) * (y);