2 * SPDX-License-Identifier: MIT-CMU
4 * Mach Operating System
5 * Copyright (c) 1991,1990 Carnegie Mellon University
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
29 * Author: David B. Golub, Carnegie Mellon University
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/eventhandler.h>
41 #include <sys/linker_set.h>
44 #include <sys/mutex.h>
46 #include <sys/reboot.h>
47 #include <sys/signalvar.h>
48 #include <sys/systm.h>
51 #include <sys/watchdog.h>
52 #include <sys/kernel.h>
55 #include <ddb/db_command.h>
56 #include <ddb/db_lex.h>
57 #include <ddb/db_output.h>
59 #include <machine/cpu.h>
60 #include <machine/setjmp.h>
63 * Exported global variables
67 db_addr_t db_last_addr;
71 static db_cmdfcn_t db_dump;
72 static db_cmdfcn_t db_fncall;
73 static db_cmdfcn_t db_gdb;
74 static db_cmdfcn_t db_halt;
75 static db_cmdfcn_t db_kill;
76 static db_cmdfcn_t db_reset;
77 static db_cmdfcn_t db_stack_trace;
78 static db_cmdfcn_t db_stack_trace_active;
79 static db_cmdfcn_t db_stack_trace_all;
80 static db_cmdfcn_t db_watchdog;
86 static struct command db_show_active_cmds[] = {
87 { "trace", db_stack_trace_active, 0, NULL },
89 struct command_table db_show_active_table =
90 LIST_HEAD_INITIALIZER(db_show_active_table);
92 static struct command db_show_all_cmds[] = {
93 { "trace", db_stack_trace_all, 0, NULL },
95 struct command_table db_show_all_table =
96 LIST_HEAD_INITIALIZER(db_show_all_table);
98 static struct command db_show_cmds[] = {
99 { "active", 0, 0, &db_show_active_table },
100 { "all", 0, 0, &db_show_all_table },
101 { "registers", db_show_regs, 0, NULL },
102 { "breaks", db_listbreak_cmd, 0, NULL },
103 { "threads", db_show_threads, 0, NULL },
105 struct command_table db_show_table = LIST_HEAD_INITIALIZER(db_show_table);
107 static struct command db_cmds[] = {
108 { "print", db_print_cmd, 0, NULL },
109 { "p", db_print_cmd, 0, NULL },
110 { "examine", db_examine_cmd, CS_SET_DOT, NULL },
111 { "x", db_examine_cmd, CS_SET_DOT, NULL },
112 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, NULL },
113 { "set", db_set_cmd, CS_OWN, NULL },
114 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, NULL },
115 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, NULL },
116 { "delete", db_delete_cmd, 0, NULL },
117 { "d", db_delete_cmd, 0, NULL },
118 { "dump", db_dump, 0, NULL },
119 { "break", db_breakpoint_cmd, 0, NULL },
120 { "b", db_breakpoint_cmd, 0, NULL },
121 { "dwatch", db_deletewatch_cmd, 0, NULL },
122 { "watch", db_watchpoint_cmd, CS_MORE,NULL },
123 { "dhwatch", db_deletehwatch_cmd, 0, NULL },
124 { "hwatch", db_hwatchpoint_cmd, 0, NULL },
125 { "step", db_single_step_cmd, 0, NULL },
126 { "s", db_single_step_cmd, 0, NULL },
127 { "continue", db_continue_cmd, 0, NULL },
128 { "c", db_continue_cmd, 0, NULL },
129 { "until", db_trace_until_call_cmd,0, NULL },
130 { "next", db_trace_until_matching_cmd,0, NULL },
131 { "match", db_trace_until_matching_cmd,0, NULL },
132 { "trace", db_stack_trace, CS_OWN, NULL },
133 { "t", db_stack_trace, CS_OWN, NULL },
134 /* XXX alias for active trace */
135 { "acttrace", db_stack_trace_active, 0, NULL },
136 /* XXX alias for all trace */
137 { "alltrace", db_stack_trace_all, 0, NULL },
138 { "where", db_stack_trace, CS_OWN, NULL },
139 { "bt", db_stack_trace, CS_OWN, NULL },
140 { "call", db_fncall, CS_OWN, NULL },
141 { "show", 0, 0, &db_show_table },
142 { "ps", db_ps, 0, NULL },
143 { "gdb", db_gdb, 0, NULL },
144 { "halt", db_halt, 0, NULL },
145 { "reboot", db_reset, 0, NULL },
146 { "reset", db_reset, 0, NULL },
147 { "kill", db_kill, CS_OWN, NULL },
148 { "watchdog", db_watchdog, CS_OWN, NULL },
149 { "thread", db_set_thread, 0, NULL },
150 { "run", db_run_cmd, CS_OWN, NULL },
151 { "script", db_script_cmd, CS_OWN, NULL },
152 { "scripts", db_scripts_cmd, 0, NULL },
153 { "unscript", db_unscript_cmd, CS_OWN, NULL },
154 { "capture", db_capture_cmd, CS_OWN, NULL },
155 { "textdump", db_textdump_cmd, CS_OWN, NULL },
156 { "findstack", db_findstack_cmd, 0, NULL },
158 struct command_table db_cmd_table = LIST_HEAD_INITIALIZER(db_cmd_table);
160 static struct command *db_last_command = NULL;
163 * if 'ed' style: 'dot' is set at start of last item printed,
164 * and '+' points to next line.
165 * Otherwise: 'dot' points to next item, '..' points to last.
167 static bool db_ed_style = true;
170 * Utility routine - discard tokens through end-of-line.
182 * Results of command search.
187 #define CMD_AMBIGUOUS 3
190 static void db_cmd_match(char *name, struct command *cmd,
191 struct command **cmdp, int *resultp);
192 static void db_cmd_list(struct command_table *table);
193 static int db_cmd_search(char *name, struct command_table *table,
194 struct command **cmdp);
195 static void db_command(struct command **last_cmdp,
196 struct command_table *cmd_table, int dopager);
199 * Initialize the command lists from the static tables.
202 db_command_init(void)
204 #define N(a) (sizeof(a) / sizeof(a[0]))
207 for (i = 0; i < N(db_cmds); i++)
208 db_command_register(&db_cmd_table, &db_cmds[i]);
209 for (i = 0; i < N(db_show_cmds); i++)
210 db_command_register(&db_show_table, &db_show_cmds[i]);
211 for (i = 0; i < N(db_show_active_cmds); i++)
212 db_command_register(&db_show_active_table,
213 &db_show_active_cmds[i]);
214 for (i = 0; i < N(db_show_all_cmds); i++)
215 db_command_register(&db_show_all_table, &db_show_all_cmds[i]);
220 * Register a command.
223 db_command_register(struct command_table *list, struct command *cmd)
225 struct command *c, *last;
228 LIST_FOREACH(c, list, next) {
229 int n = strcmp(cmd->name, c->name);
231 /* Check that the command is not already present. */
233 printf("%s: Warning, the command \"%s\" already exists;"
234 " ignoring request\n", __func__, cmd->name);
238 /* NB: keep list sorted lexicographically */
239 LIST_INSERT_BEFORE(c, cmd, next);
245 LIST_INSERT_HEAD(list, cmd, next);
247 LIST_INSERT_AFTER(last, cmd, next);
251 * Remove a command previously registered with db_command_register.
254 db_command_unregister(struct command_table *list, struct command *cmd)
258 LIST_FOREACH(c, list, next) {
260 LIST_REMOVE(cmd, next);
264 /* NB: intentionally quiet */
268 * Helper function to match a single command.
271 db_cmd_match(char *name, struct command *cmd, struct command **cmdp,
279 while ((c = *lp) == *rp) {
283 *resultp = CMD_UNIQUE;
290 /* end of name, not end of command -
292 if (*resultp == CMD_FOUND) {
293 *resultp = CMD_AMBIGUOUS;
294 /* but keep looking for a full match -
295 this lets us match single letters */
296 } else if (*resultp == CMD_NONE) {
298 *resultp = CMD_FOUND;
304 * Search for command prefix.
307 db_cmd_search(char *name, struct command_table *table, struct command **cmdp)
310 int result = CMD_NONE;
312 LIST_FOREACH(cmd, table, next) {
313 db_cmd_match(name,cmd,cmdp,&result);
314 if (result == CMD_UNIQUE)
318 if (result == CMD_NONE) {
319 /* check for 'help' */
320 if (name[0] == 'h' && name[1] == 'e'
321 && name[2] == 'l' && name[3] == 'p')
328 db_cmd_list(struct command_table *table)
331 int have_subcommands;
333 have_subcommands = 0;
334 LIST_FOREACH(cmd, table, next) {
335 if (cmd->more != NULL)
337 db_printf("%-16s", cmd->name);
341 if (have_subcommands > 0) {
342 db_printf("\nThe following have subcommands; append \"help\" "
343 "to list (e.g. \"show help\"):\n");
344 LIST_FOREACH(cmd, table, next) {
345 if (cmd->more == NULL)
347 db_printf("%-16s", cmd->name);
354 db_command(struct command **last_cmdp, struct command_table *cmd_table,
357 struct command *cmd = NULL;
359 char modif[TOK_STRING_SIZE];
360 db_expr_t addr, count;
361 bool have_addr = false;
366 /* empty line repeats last command, at 'next' */
368 addr = (db_expr_t)db_next;
373 else if (t == tEXCL) {
374 db_fncall((db_expr_t)0, (bool)false, (db_expr_t)0, (char *)0);
377 else if (t != tIDENT) {
378 db_printf("Unrecognized input; use \"help\" "
379 "to list available commands\n");
388 result = db_cmd_search(db_tok_string,
393 db_printf("No such command; use \"help\" "
394 "to list available commands\n");
398 db_printf("Ambiguous\n");
402 if (cmd_table == &db_cmd_table) {
403 db_printf("This is ddb(4), the kernel debugger; "
404 "see https://man.FreeBSD.org/ddb/4 for help.\n");
405 db_printf("Use \"bt\" for backtrace, \"dump\" for "
406 "kernel core dump, \"reset\" to reboot.\n");
407 db_printf("Available commands:\n");
409 db_cmd_list(cmd_table);
415 if ((cmd_table = cmd->more) != NULL) {
418 db_printf("Subcommand required; "
419 "available subcommands:\n");
420 db_cmd_list(cmd_table);
427 if ((cmd->flag & CS_OWN) == 0) {
430 * command [/modifier] [addr] [,count]
436 db_printf("Bad modifier\n");
440 db_strcpy(modif, db_tok_string);
447 if (db_expression(&addr)) {
448 db_dot = (db_addr_t) addr;
449 db_last_addr = db_dot;
453 addr = (db_expr_t) db_dot;
458 if (!db_expression(&count)) {
459 db_printf("Count missing\n");
468 if ((cmd->flag & CS_MORE) == 0) {
476 * Execute the command.
482 (*cmd->fcn)(addr, have_addr, count, modif);
486 if (cmd->flag & CS_SET_DOT) {
488 * If command changes dot, set dot to
489 * previous address displayed (if 'ed' style).
500 * If command does not change dot,
501 * set 'next' location to be the same.
509 * At least one non-optional command must be implemented using
510 * DB_COMMAND() so that db_cmd_set gets created. Here is one.
512 DB_COMMAND(panic, db_panic)
515 panic("from debugger");
519 db_command_loop(void)
522 * Initialize 'prev' and 'next' to dot.
527 db_cmd_loop_done = 0;
528 while (!db_cmd_loop_done) {
529 if (db_print_position() != 0)
533 (void) db_read_line();
535 db_command(&db_last_command, &db_cmd_table, /* dopager */ 1);
540 * Execute a command on behalf of a script. The caller is responsible for
541 * making sure that the command string is < DB_MAXLINE or it will be
544 * XXXRW: Runs by injecting faked input into DDB input stream; it would be
545 * nicer to use an alternative approach that didn't mess with the previous
549 db_command_script(const char *command)
551 db_prev = db_next = db_dot;
552 db_inject_line(command);
553 db_command(&db_last_command, &db_cmd_table, /* dopager */ 0);
557 db_error(const char *s)
562 kdb_reenter_silent();
566 db_dump(db_expr_t dummy, bool dummy2, db_expr_t dummy3, char *dummy4)
570 if (textdump_pending) {
571 db_printf("textdump_pending set.\n"
572 "run \"textdump unset\" first or \"textdump dump\" for a textdump.\n");
575 error = doadump(false);
577 db_printf("Cannot dump: ");
580 db_printf("debugger got invoked while dumping.\n");
583 db_printf("no dump device specified.\n");
586 db_printf("unknown error (error=%d).\n", error);
593 * Call random function:
597 /* The generic implementation supports a maximum of 10 arguments. */
598 typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
599 db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
602 db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
604 __db_f *f = (__db_f *)addr;
607 db_printf("Too many arguments (max 10)\n");
610 *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
611 args[6], args[7], args[8], args[9]);
616 db_fncall(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
619 db_expr_t args[DB_MAXARGS];
624 if (!db_expression(&fn_addr)) {
625 db_printf("Bad function\n");
632 if (db_expression(&args[0])) {
634 while ((t = db_read_token()) == tCOMMA) {
635 if (nargs == DB_MAXARGS) {
636 db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
640 if (!db_expression(&args[nargs])) {
641 db_printf("Argument missing\n");
649 if (db_read_token() != tRPAREN) {
650 db_printf("Mismatched parens\n");
658 if (DB_CALL(fn_addr, &retval, nargs, args))
659 db_printf("= %#lr\n", (long)retval);
663 db_halt(db_expr_t dummy, bool dummy2, db_expr_t dummy3, char *dummy4)
670 db_kill(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
672 db_expr_t old_radix, pid, sig;
675 #define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0)
678 * PIDs and signal numbers are typically represented in base
679 * 10, so make that the default here. It can, of course, be
680 * overridden by specifying a prefix.
682 old_radix = db_radix;
684 /* Retrieve arguments. */
685 if (!db_expression(&sig))
686 DB_ERROR(("Missing signal number\n"));
687 if (!db_expression(&pid))
688 DB_ERROR(("Missing process ID\n"));
690 if (!_SIG_VALID(sig))
691 DB_ERROR(("Signal number out of range\n"));
694 * Find the process in question. allproc_lock is not needed
695 * since we're in DDB.
697 /* sx_slock(&allproc_lock); */
698 FOREACH_PROC_IN_SYSTEM(p)
701 /* sx_sunlock(&allproc_lock); */
703 DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
705 /* If it's already locked, bail; otherwise, do the deed. */
706 if (PROC_TRYLOCK(p) == 0)
707 DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
709 pksignal(p, sig, NULL);
714 db_radix = old_radix;
719 * Reboot. In case there is an additional argument, take it as delay in
720 * seconds. Default to 15s if we cannot parse it and make sure we will
721 * never wait longer than 1 week. Some code is similar to
722 * kern_shutdown.c:shutdown_panic().
724 #ifndef DB_RESET_MAXDELAY
725 #define DB_RESET_MAXDELAY (3600 * 24 * 7)
729 db_reset(db_expr_t addr, bool have_addr, db_expr_t count __unused,
730 char *modif __unused)
735 delay = (int)db_hex2dec(addr);
737 /* If we parse to fail, use 15s. */
741 /* Cap at one week. */
742 if ((uintmax_t)delay > (uintmax_t)DB_RESET_MAXDELAY)
743 delay = DB_RESET_MAXDELAY;
745 db_printf("Automatic reboot in %d seconds - "
746 "press a key on the console to abort\n", delay);
747 for (loop = delay * 10; loop > 0; --loop) {
748 DELAY(1000 * 100); /* 1/10th second */
749 /* Did user type a key? */
750 if (cncheckc() != -1)
759 db_watchdog(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
761 db_expr_t old_radix, tout;
764 old_radix = db_radix;
766 err = db_expression(&tout);
768 db_radix = old_radix;
770 /* If no argument is provided the watchdog will just be disabled. */
772 db_printf("No argument provided, disabling watchdog\n");
774 } else if ((tout & WD_INTERVAL) == WD_TO_NEVER) {
775 db_error("Out of range watchdog interval\n");
778 EVENTHANDLER_INVOKE(watchdog_list, tout, &i);
782 db_gdb(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
785 if (kdb_dbbe_select("gdb") != 0) {
786 db_printf("The remote GDB backend could not be selected.\n");
790 * Mark that we are done in the debugger. kdb_trap()
791 * should re-enter with the new backend.
793 db_cmd_loop_done = 1;
794 db_printf("(ctrl-c will return control to ddb)\n");
798 db_stack_trace(db_expr_t tid, bool hastid, db_expr_t count, char *modif)
806 * We parse our own arguments. We don't like the default radix.
810 hastid = db_expression(&tid);
813 if (!db_expression(&count)) {
814 db_printf("Count missing\n");
827 td = kdb_thr_lookup((lwpid_t)tid);
829 td = kdb_thr_from_pid((pid_t)tid);
831 db_printf("Thread %d not found\n", (int)tid);
836 if (td->td_proc != NULL)
837 pid = td->td_proc->p_pid;
840 db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
841 if (td->td_proc != NULL && (td->td_proc->p_flag & P_INMEM) == 0)
842 db_printf("--- swapped out\n");
844 db_trace_thread(td, count);
848 _db_stack_trace_all(bool active_only)
854 for (td = kdb_thr_first(); td != NULL; td = kdb_thr_next(td)) {
855 prev_jb = kdb_jmpbuf(jb);
856 if (setjmp(jb) == 0) {
857 if (TD_IS_RUNNING(td))
858 db_printf("\nTracing command %s pid %d"
859 " tid %ld td %p (CPU %d)\n",
860 td->td_proc->p_comm, td->td_proc->p_pid,
861 (long)td->td_tid, td, td->td_oncpu);
862 else if (active_only)
865 db_printf("\nTracing command %s pid %d"
866 " tid %ld td %p\n", td->td_proc->p_comm,
867 td->td_proc->p_pid, (long)td->td_tid, td);
868 if (td->td_proc->p_flag & P_INMEM)
869 db_trace_thread(td, -1);
871 db_printf("--- swapped out\n");
882 db_stack_trace_active(db_expr_t dummy, bool dummy2, db_expr_t dummy3,
886 _db_stack_trace_all(true);
890 db_stack_trace_all(db_expr_t dummy, bool dummy2, db_expr_t dummy3,
894 _db_stack_trace_all(false);
898 * Take the parsed expression value from the command line that was parsed
899 * as a hexadecimal value and convert it as if the expression was parsed
900 * as a decimal value. Returns -1 if the expression was not a valid
904 db_hex2dec(db_expr_t expr)
915 val += (x % 16) * (y);