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
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_active;
76 static db_cmdfcn_t db_stack_trace_all;
77 static db_cmdfcn_t db_watchdog;
83 static struct command db_show_active_cmds[] = {
84 { "trace", db_stack_trace_active, 0, NULL },
86 struct command_table db_show_active_table =
87 LIST_HEAD_INITIALIZER(db_show_active_table);
89 static struct command db_show_all_cmds[] = {
90 { "trace", db_stack_trace_all, 0, NULL },
92 struct command_table db_show_all_table =
93 LIST_HEAD_INITIALIZER(db_show_all_table);
95 static struct command db_show_cmds[] = {
96 { "active", 0, 0, &db_show_active_table },
97 { "all", 0, 0, &db_show_all_table },
98 { "registers", db_show_regs, 0, NULL },
99 { "breaks", db_listbreak_cmd, 0, NULL },
100 { "threads", db_show_threads, 0, NULL },
102 struct command_table db_show_table = LIST_HEAD_INITIALIZER(db_show_table);
104 static struct command db_cmds[] = {
105 { "print", db_print_cmd, 0, NULL },
106 { "p", db_print_cmd, 0, NULL },
107 { "examine", db_examine_cmd, CS_SET_DOT, NULL },
108 { "x", db_examine_cmd, CS_SET_DOT, NULL },
109 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, NULL },
110 { "set", db_set_cmd, CS_OWN, NULL },
111 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, NULL },
112 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, NULL },
113 { "delete", db_delete_cmd, 0, NULL },
114 { "d", db_delete_cmd, 0, NULL },
115 { "dump", db_dump, 0, NULL },
116 { "break", db_breakpoint_cmd, 0, NULL },
117 { "b", db_breakpoint_cmd, 0, NULL },
118 { "dwatch", db_deletewatch_cmd, 0, NULL },
119 { "watch", db_watchpoint_cmd, CS_MORE,NULL },
120 { "dhwatch", db_deletehwatch_cmd, 0, NULL },
121 { "hwatch", db_hwatchpoint_cmd, 0, NULL },
122 { "step", db_single_step_cmd, 0, NULL },
123 { "s", db_single_step_cmd, 0, NULL },
124 { "continue", db_continue_cmd, 0, NULL },
125 { "c", db_continue_cmd, 0, NULL },
126 { "until", db_trace_until_call_cmd,0, NULL },
127 { "next", db_trace_until_matching_cmd,0, NULL },
128 { "match", db_trace_until_matching_cmd,0, NULL },
129 { "trace", db_stack_trace, CS_OWN, NULL },
130 { "t", db_stack_trace, CS_OWN, NULL },
131 /* XXX alias for active trace */
132 { "acttrace", db_stack_trace_active, 0, NULL },
133 /* XXX alias for all trace */
134 { "alltrace", db_stack_trace_all, 0, NULL },
135 { "where", db_stack_trace, CS_OWN, NULL },
136 { "bt", db_stack_trace, CS_OWN, NULL },
137 { "call", db_fncall, CS_OWN, NULL },
138 { "show", 0, 0, &db_show_table },
139 { "ps", db_ps, 0, NULL },
140 { "gdb", db_gdb, 0, NULL },
141 { "halt", db_halt, 0, NULL },
142 { "reboot", db_reset, 0, NULL },
143 { "reset", db_reset, 0, NULL },
144 { "kill", db_kill, CS_OWN, NULL },
145 { "watchdog", db_watchdog, CS_OWN, NULL },
146 { "thread", db_set_thread, CS_OWN, NULL },
147 { "run", db_run_cmd, CS_OWN, NULL },
148 { "script", db_script_cmd, CS_OWN, NULL },
149 { "scripts", db_scripts_cmd, 0, NULL },
150 { "unscript", db_unscript_cmd, CS_OWN, NULL },
151 { "capture", db_capture_cmd, CS_OWN, NULL },
152 { "textdump", db_textdump_cmd, CS_OWN, NULL },
153 { "findstack", db_findstack_cmd, 0, NULL },
155 struct command_table db_cmd_table = LIST_HEAD_INITIALIZER(db_cmd_table);
157 static struct command *db_last_command = NULL;
160 * if 'ed' style: 'dot' is set at start of last item printed,
161 * and '+' points to next line.
162 * Otherwise: 'dot' points to next item, '..' points to last.
164 static bool db_ed_style = true;
167 * Utility routine - discard tokens through end-of-line.
179 * Results of command search.
184 #define CMD_AMBIGUOUS 3
187 static void db_cmd_match(char *name, struct command *cmd,
188 struct command **cmdp, int *resultp);
189 static void db_cmd_list(struct command_table *table);
190 static int db_cmd_search(char *name, struct command_table *table,
191 struct command **cmdp);
192 static void db_command(struct command **last_cmdp,
193 struct command_table *cmd_table, int dopager);
196 * Initialize the command lists from the static tables.
199 db_command_init(void)
201 #define N(a) (sizeof(a) / sizeof(a[0]))
204 for (i = 0; i < N(db_cmds); i++)
205 db_command_register(&db_cmd_table, &db_cmds[i]);
206 for (i = 0; i < N(db_show_cmds); i++)
207 db_command_register(&db_show_table, &db_show_cmds[i]);
208 for (i = 0; i < N(db_show_active_cmds); i++)
209 db_command_register(&db_show_active_table,
210 &db_show_active_cmds[i]);
211 for (i = 0; i < N(db_show_all_cmds); i++)
212 db_command_register(&db_show_all_table, &db_show_all_cmds[i]);
217 * Register a command.
220 db_command_register(struct command_table *list, struct command *cmd)
222 struct command *c, *last;
225 LIST_FOREACH(c, list, next) {
226 int n = strcmp(cmd->name, c->name);
228 /* Check that the command is not already present. */
230 printf("%s: Warning, the command \"%s\" already exists;"
231 " ignoring request\n", __func__, cmd->name);
235 /* NB: keep list sorted lexicographically */
236 LIST_INSERT_BEFORE(c, cmd, next);
242 LIST_INSERT_HEAD(list, cmd, next);
244 LIST_INSERT_AFTER(last, cmd, next);
248 * Remove a command previously registered with db_command_register.
251 db_command_unregister(struct command_table *list, struct command *cmd)
255 LIST_FOREACH(c, list, next) {
257 LIST_REMOVE(cmd, next);
261 /* NB: intentionally quiet */
265 * Helper function to match a single command.
268 db_cmd_match(char *name, struct command *cmd, struct command **cmdp,
276 while ((c = *lp) == *rp) {
280 *resultp = CMD_UNIQUE;
287 /* end of name, not end of command -
289 if (*resultp == CMD_FOUND) {
290 *resultp = CMD_AMBIGUOUS;
291 /* but keep looking for a full match -
292 this lets us match single letters */
295 *resultp = CMD_FOUND;
301 * Search for command prefix.
304 db_cmd_search(char *name, struct command_table *table, struct command **cmdp)
307 int result = CMD_NONE;
309 LIST_FOREACH(cmd, table, next) {
310 db_cmd_match(name,cmd,cmdp,&result);
311 if (result == CMD_UNIQUE)
315 if (result == CMD_NONE) {
316 /* check for 'help' */
317 if (name[0] == 'h' && name[1] == 'e'
318 && name[2] == 'l' && name[3] == 'p')
325 db_cmd_list(struct command_table *table)
329 LIST_FOREACH(cmd, table, next) {
330 db_printf("%-16s", cmd->name);
336 db_command(struct command **last_cmdp, struct command_table *cmd_table,
339 struct command *cmd = NULL;
341 char modif[TOK_STRING_SIZE];
342 db_expr_t addr, count;
343 bool have_addr = false;
348 /* empty line repeats last command, at 'next' */
350 addr = (db_expr_t)db_next;
355 else if (t == tEXCL) {
356 db_fncall((db_expr_t)0, (bool)false, (db_expr_t)0, (char *)0);
359 else if (t != tIDENT) {
369 result = db_cmd_search(db_tok_string,
374 db_printf("No such command\n");
378 db_printf("Ambiguous\n");
382 db_cmd_list(cmd_table);
388 if ((cmd_table = cmd->more) != NULL) {
391 db_cmd_list(cmd_table);
398 if ((cmd->flag & CS_OWN) == 0) {
401 * command [/modifier] [addr] [,count]
407 db_printf("Bad modifier\n");
411 db_strcpy(modif, db_tok_string);
418 if (db_expression(&addr)) {
419 db_dot = (db_addr_t) addr;
420 db_last_addr = db_dot;
424 addr = (db_expr_t) db_dot;
429 if (!db_expression(&count)) {
430 db_printf("Count missing\n");
439 if ((cmd->flag & CS_MORE) == 0) {
447 * Execute the command.
453 (*cmd->fcn)(addr, have_addr, count, modif);
457 if (cmd->flag & CS_SET_DOT) {
459 * If command changes dot, set dot to
460 * previous address displayed (if 'ed' style).
471 * If command does not change dot,
472 * set 'next' location to be the same.
480 * At least one non-optional command must be implemented using
481 * DB_COMMAND() so that db_cmd_set gets created. Here is one.
483 DB_COMMAND(panic, db_panic)
486 panic("from debugger");
490 db_command_loop(void)
493 * Initialize 'prev' and 'next' to dot.
498 db_cmd_loop_done = 0;
499 while (!db_cmd_loop_done) {
500 if (db_print_position() != 0)
504 (void) db_read_line();
506 db_command(&db_last_command, &db_cmd_table, /* dopager */ 1);
511 * Execute a command on behalf of a script. The caller is responsible for
512 * making sure that the command string is < DB_MAXLINE or it will be
515 * XXXRW: Runs by injecting faked input into DDB input stream; it would be
516 * nicer to use an alternative approach that didn't mess with the previous
520 db_command_script(const char *command)
522 db_prev = db_next = db_dot;
523 db_inject_line(command);
524 db_command(&db_last_command, &db_cmd_table, /* dopager */ 0);
528 db_error(const char *s)
537 db_dump(db_expr_t dummy, bool dummy2, db_expr_t dummy3, char *dummy4)
541 if (textdump_pending) {
542 db_printf("textdump_pending set.\n"
543 "run \"textdump unset\" first or \"textdump dump\" for a textdump.\n");
546 error = doadump(false);
548 db_printf("Cannot dump: ");
551 db_printf("debugger got invoked while dumping.\n");
554 db_printf("no dump device specified.\n");
557 db_printf("unknown error (error=%d).\n", error);
564 * Call random function:
568 /* The generic implementation supports a maximum of 10 arguments. */
569 typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
570 db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
573 db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
575 __db_f *f = (__db_f *)addr;
578 db_printf("Too many arguments (max 10)\n");
581 *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
582 args[6], args[7], args[8], args[9]);
587 db_fncall(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
590 db_expr_t args[DB_MAXARGS];
595 if (!db_expression(&fn_addr)) {
596 db_printf("Bad function\n");
603 if (db_expression(&args[0])) {
605 while ((t = db_read_token()) == tCOMMA) {
606 if (nargs == DB_MAXARGS) {
607 db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
611 if (!db_expression(&args[nargs])) {
612 db_printf("Argument missing\n");
620 if (db_read_token() != tRPAREN) {
629 if (DB_CALL(fn_addr, &retval, nargs, args))
630 db_printf("= %#lr\n", (long)retval);
634 db_halt(db_expr_t dummy, bool dummy2, db_expr_t dummy3, char *dummy4)
641 db_kill(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *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, bool 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(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
732 db_expr_t old_radix, tout;
735 old_radix = db_radix;
737 err = db_expression(&tout);
739 db_radix = old_radix;
741 /* If no argument is provided the watchdog will just be disabled. */
743 db_printf("No argument provided, disabling watchdog\n");
745 } else if ((tout & WD_INTERVAL) == WD_TO_NEVER) {
746 db_error("Out of range watchdog interval\n");
749 EVENTHANDLER_INVOKE(watchdog_list, tout, &i);
753 db_gdb(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
756 if (kdb_dbbe_select("gdb") != 0) {
757 db_printf("The remote GDB backend could not be selected.\n");
761 * Mark that we are done in the debugger. kdb_trap()
762 * should re-enter with the new backend.
764 db_cmd_loop_done = 1;
765 db_printf("(ctrl-c will return control to ddb)\n");
769 db_stack_trace(db_expr_t tid, bool hastid, db_expr_t count, char *modif)
777 * We parse our own arguments. We don't like the default radix.
781 hastid = db_expression(&tid);
784 if (!db_expression(&count)) {
785 db_printf("Count missing\n");
797 td = kdb_thr_lookup((lwpid_t)tid);
799 td = kdb_thr_from_pid((pid_t)tid);
801 db_printf("Thread %d not found\n", (int)tid);
806 if (td->td_proc != NULL)
807 pid = td->td_proc->p_pid;
810 db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
811 db_trace_thread(td, count);
815 _db_stack_trace_all(bool active_only)
822 FOREACH_PROC_IN_SYSTEM(p) {
823 prev_jb = kdb_jmpbuf(jb);
824 if (setjmp(jb) == 0) {
825 FOREACH_THREAD_IN_PROC(p, td) {
826 if (td->td_state == TDS_RUNNING)
827 db_printf("\nTracing command %s pid %d"
828 " tid %ld td %p (CPU %d)\n",
830 (long)td->td_tid, td,
832 else if (active_only)
835 db_printf("\nTracing command %s pid %d"
836 " tid %ld td %p\n", p->p_comm,
837 p->p_pid, (long)td->td_tid, td);
838 db_trace_thread(td, -1);
850 db_stack_trace_active(db_expr_t dummy, bool dummy2, db_expr_t dummy3,
854 _db_stack_trace_all(true);
858 db_stack_trace_all(db_expr_t dummy, bool dummy2, db_expr_t dummy3,
862 _db_stack_trace_all(false);
866 * Take the parsed expression value from the command line that was parsed
867 * as a hexadecimal value and convert it as if the expression was parsed
868 * as a decimal value. Returns -1 if the expression was not a valid
872 db_hex2dec(db_expr_t expr)
883 val += (x % 16) * (y);