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);
69 static db_cmdfcn_t db_fncall;
70 static db_cmdfcn_t db_gdb;
71 static db_cmdfcn_t db_kill;
72 static db_cmdfcn_t db_reset;
73 static db_cmdfcn_t db_stack_trace;
74 static db_cmdfcn_t db_stack_trace_all;
75 static db_cmdfcn_t db_watchdog;
81 static struct command db_show_all_cmds[] = {
82 { "procs", db_ps, 0, 0 },
86 static struct command_table db_show_all_table = {
90 static struct command db_show_cmds[] = {
91 { "all", 0, 0, &db_show_all_table },
92 { "registers", db_show_regs, 0, 0 },
93 { "breaks", db_listbreak_cmd, 0, 0 },
94 { "threads", db_show_threads, 0, 0 },
98 static struct command_table db_show_table = {
100 SET_BEGIN(db_show_cmd_set),
101 SET_LIMIT(db_show_cmd_set)
104 static struct command db_commands[] = {
105 { "print", db_print_cmd, 0, 0 },
106 { "p", db_print_cmd, 0, 0 },
107 { "examine", db_examine_cmd, CS_SET_DOT, 0 },
108 { "x", db_examine_cmd, CS_SET_DOT, 0 },
109 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 },
110 { "set", db_set_cmd, CS_OWN, 0 },
111 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
112 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
113 { "delete", db_delete_cmd, 0, 0 },
114 { "d", db_delete_cmd, 0, 0 },
115 { "break", db_breakpoint_cmd, 0, 0 },
116 { "dwatch", db_deletewatch_cmd, 0, 0 },
117 { "watch", db_watchpoint_cmd, CS_MORE,0 },
118 { "dhwatch", db_deletehwatch_cmd, 0, 0 },
119 { "hwatch", db_hwatchpoint_cmd, 0, 0 },
120 { "step", db_single_step_cmd, 0, 0 },
121 { "s", db_single_step_cmd, 0, 0 },
122 { "continue", db_continue_cmd, 0, 0 },
123 { "c", db_continue_cmd, 0, 0 },
124 { "until", db_trace_until_call_cmd,0, 0 },
125 { "next", db_trace_until_matching_cmd,0, 0 },
126 { "match", db_trace_until_matching_cmd,0, 0 },
127 { "trace", db_stack_trace, CS_OWN, 0 },
128 { "alltrace", db_stack_trace_all, 0, 0 },
129 { "where", db_stack_trace, CS_OWN, 0 },
130 { "bt", db_stack_trace, CS_OWN, 0 },
131 { "call", db_fncall, CS_OWN, 0 },
132 { "show", 0, 0, &db_show_table },
133 { "ps", db_ps, 0, 0 },
134 { "gdb", db_gdb, 0, 0 },
135 { "reset", db_reset, 0, 0 },
136 { "kill", db_kill, CS_OWN, 0 },
137 { "watchdog", db_watchdog, 0, 0 },
138 { "thread", db_set_thread, CS_OWN, 0 },
142 static struct command_table db_command_table = {
144 SET_BEGIN(db_cmd_set),
145 SET_LIMIT(db_cmd_set)
148 static struct command *db_last_command = 0;
151 * if 'ed' style: 'dot' is set at start of last item printed,
152 * and '+' points to next line.
153 * Otherwise: 'dot' points to next item, '..' points to last.
155 static boolean_t db_ed_style = TRUE;
158 * Utility routine - discard tokens through end-of-line.
170 * Results of command search.
175 #define CMD_AMBIGUOUS 3
178 static void db_cmd_match(char *name, struct command *cmd,
179 struct command **cmdp, int *resultp);
180 static void db_cmd_list(struct command_table *table);
181 static int db_cmd_search(char *name, struct command_table *table,
182 struct command **cmdp);
183 static void db_command(struct command **last_cmdp,
184 struct command_table *cmd_table);
187 * Helper function to match a single command.
190 db_cmd_match(name, cmd, cmdp, resultp)
193 struct command **cmdp; /* out */
201 while ((c = *lp) == *rp) {
205 *resultp = CMD_UNIQUE;
212 /* end of name, not end of command -
214 if (*resultp == CMD_FOUND) {
215 *resultp = CMD_AMBIGUOUS;
216 /* but keep looking for a full match -
217 this lets us match single letters */
220 *resultp = CMD_FOUND;
226 * Search for command prefix.
229 db_cmd_search(name, table, cmdp)
231 struct command_table *table;
232 struct command **cmdp; /* out */
235 struct command **aux_cmdp;
236 int result = CMD_NONE;
238 for (cmd = table->table; cmd->name != 0; cmd++) {
239 db_cmd_match(name, cmd, cmdp, &result);
240 if (result == CMD_UNIQUE)
243 if (table->aux_tablep != NULL)
244 for (aux_cmdp = table->aux_tablep;
245 aux_cmdp < table->aux_tablep_end;
247 db_cmd_match(name, *aux_cmdp, cmdp, &result);
248 if (result == CMD_UNIQUE)
251 if (result == CMD_NONE) {
252 /* check for 'help' */
253 if (name[0] == 'h' && name[1] == 'e'
254 && name[2] == 'l' && name[3] == 'p')
262 struct command_table *table;
264 register struct command *cmd;
265 register struct command **aux_cmdp;
267 for (cmd = table->table; cmd->name != 0; cmd++) {
268 db_printf("%-12s", cmd->name);
271 if (table->aux_tablep == NULL)
273 for (aux_cmdp = table->aux_tablep; aux_cmdp < table->aux_tablep_end;
275 db_printf("%-12s", (*aux_cmdp)->name);
281 db_command(last_cmdp, cmd_table)
282 struct command **last_cmdp; /* IN_OUT */
283 struct command_table *cmd_table;
287 char modif[TOK_STRING_SIZE];
288 db_expr_t addr, count;
289 boolean_t have_addr = FALSE;
294 /* empty line repeats last command, at 'next' */
296 addr = (db_expr_t)db_next;
301 else if (t == tEXCL) {
302 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
305 else if (t != tIDENT) {
315 result = db_cmd_search(db_tok_string,
320 db_printf("No such command\n");
324 db_printf("Ambiguous\n");
328 db_cmd_list(cmd_table);
334 if ((cmd_table = cmd->more) != NULL) {
337 db_cmd_list(cmd_table);
344 if ((cmd->flag & CS_OWN) == 0) {
347 * command [/modifier] [addr] [,count]
353 db_printf("Bad modifier\n");
357 db_strcpy(modif, db_tok_string);
364 if (db_expression(&addr)) {
365 db_dot = (db_addr_t) addr;
366 db_last_addr = db_dot;
370 addr = (db_expr_t) db_dot;
375 if (!db_expression(&count)) {
376 db_printf("Count missing\n");
385 if ((cmd->flag & CS_MORE) == 0) {
393 * Execute the command.
396 (*cmd->fcn)(addr, have_addr, count, modif);
399 if (cmd->flag & CS_SET_DOT) {
401 * If command changes dot, set dot to
402 * previous address displayed (if 'ed' style).
413 * If command does not change dot,
414 * set 'next' location to be the same.
422 * At least one non-optional command must be implemented using
423 * DB_COMMAND() so that db_cmd_set gets created. Here is one.
425 DB_COMMAND(panic, db_panic)
428 panic("from debugger");
435 * Initialize 'prev' and 'next' to dot.
440 db_cmd_loop_done = 0;
441 while (!db_cmd_loop_done) {
442 if (db_print_position() != 0)
446 (void) db_read_line();
448 db_command(&db_last_command, &db_command_table);
464 * Call random function:
468 /* The generic implementation supports a maximum of 10 arguments. */
469 typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
470 db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
473 db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
475 __db_f *f = (__db_f *)addr;
478 db_printf("Too many arguments (max 10)\n");
481 *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
482 args[6], args[7], args[8], args[9]);
487 db_fncall(dummy1, dummy2, dummy3, dummy4)
494 db_expr_t args[DB_MAXARGS];
499 if (!db_expression(&fn_addr)) {
500 db_printf("Bad function\n");
507 if (db_expression(&args[0])) {
509 while ((t = db_read_token()) == tCOMMA) {
510 if (nargs == DB_MAXARGS) {
511 db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
515 if (!db_expression(&args[nargs])) {
516 db_printf("Argument missing\n");
524 if (db_read_token() != tRPAREN) {
533 if (DB_CALL(fn_addr, &retval, nargs, args))
534 db_printf("= %#lr\n", (long)retval);
538 db_kill(dummy1, dummy2, dummy3, dummy4)
544 db_expr_t old_radix, pid, sig;
547 #define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0)
550 * PIDs and signal numbers are typically represented in base
551 * 10, so make that the default here. It can, of course, be
552 * overridden by specifying a prefix.
554 old_radix = db_radix;
556 /* Retrieve arguments. */
557 if (!db_expression(&sig))
558 DB_ERROR(("Missing signal number\n"));
559 if (!db_expression(&pid))
560 DB_ERROR(("Missing process ID\n"));
562 if (sig < 0 || sig > _SIG_MAXSIG)
563 DB_ERROR(("Signal number out of range\n"));
566 * Find the process in question. allproc_lock is not needed
567 * since we're in DDB.
569 /* sx_slock(&allproc_lock); */
570 LIST_FOREACH(p, &allproc, p_list)
573 /* sx_sunlock(&allproc_lock); */
575 DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
577 /* If it's already locked, bail; otherwise, do the deed. */
578 if (PROC_TRYLOCK(p) == 0)
579 DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
586 db_radix = old_radix;
591 db_reset(dummy1, dummy2, dummy3, dummy4)
602 db_watchdog(dummy1, dummy2, dummy3, dummy4)
611 * XXX: It might make sense to be able to set the watchdog to a
612 * XXX: timeout here so that failure or hang as a result of subsequent
613 * XXX: ddb commands could be recovered by a reset.
616 EVENTHANDLER_INVOKE(watchdog_list, 0, &i);
620 db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
623 if (kdb_dbbe_select("gdb") != 0)
624 db_printf("The remote GDB backend could not be selected.\n");
626 db_printf("Step to enter the remote GDB backend.\n");
630 db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
638 * We parse our own arguments. We don't like the default radix.
642 hastid = db_expression(&tid);
645 if (!db_expression(&count)) {
646 db_printf("Count missing\n");
658 td = kdb_thr_lookup((lwpid_t)tid);
660 td = kdb_thr_from_pid((pid_t)tid);
662 db_printf("Thread %d not found\n", (int)tid);
667 if (td->td_proc != NULL)
668 pid = td->td_proc->p_pid;
671 db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
672 db_trace_thread(td, count);
676 db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
682 LIST_FOREACH(p, &allproc, p_list) {
683 FOREACH_THREAD_IN_PROC(p, td) {
684 db_printf("\nTracing command %s pid %d tid %ld td %p\n",
685 p->p_comm, p->p_pid, (long)td->td_tid, td);
686 db_trace_thread(td, -1);