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_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 { "procs", db_ps, 0, 0 },
87 static struct command db_show_cmds[] = {
88 { "all", 0, 0, db_show_all_cmds },
89 { "registers", db_show_regs, 0, 0 },
90 { "breaks", db_listbreak_cmd, 0, 0 },
91 { "threads", db_show_threads, 0, 0 },
95 static struct command db_command_table[] = {
96 { "print", db_print_cmd, 0, 0 },
97 { "p", db_print_cmd, 0, 0 },
98 { "examine", db_examine_cmd, CS_SET_DOT, 0 },
99 { "x", db_examine_cmd, CS_SET_DOT, 0 },
100 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 },
101 { "set", db_set_cmd, CS_OWN, 0 },
102 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
103 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
104 { "delete", db_delete_cmd, 0, 0 },
105 { "d", db_delete_cmd, 0, 0 },
106 { "break", 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 { "alltrace", db_stack_trace_all, 0, 0 },
120 { "where", db_stack_trace, CS_OWN, 0 },
121 { "bt", db_stack_trace, CS_OWN, 0 },
122 { "call", db_fncall, CS_OWN, 0 },
123 { "show", 0, 0, db_show_cmds },
124 { "ps", db_ps, 0, 0 },
125 { "gdb", db_gdb, 0, 0 },
126 { "halt", db_halt, 0, 0 },
127 { "reboot", db_reset, 0, 0 },
128 { "reset", db_reset, 0, 0 },
129 { "kill", db_kill, CS_OWN, 0 },
130 { "watchdog", db_watchdog, 0, 0 },
131 { "thread", db_set_thread, CS_OWN, 0 },
135 static struct command *db_last_command = 0;
139 * if 'ed' style: 'dot' is set at start of last item printed,
140 * and '+' points to next line.
141 * Otherwise: 'dot' points to next item, '..' points to last.
143 static boolean_t db_ed_style = TRUE;
146 * Utility routine - discard tokens through end-of-line.
158 * Results of command search.
163 #define CMD_AMBIGUOUS 3
166 static void db_cmd_list(struct command *table, struct command **aux_tablep,
167 struct command **aux_tablep_end);
168 static int db_cmd_search(char *name, struct command *table,
169 struct command **aux_tablep,
170 struct command **aux_tablep_end, struct command **cmdp);
171 static void db_command(struct command **last_cmdp,
172 struct command *cmd_table, struct command **aux_cmd_tablep,
173 struct command **aux_cmd_tablep_end);
176 * Search for command prefix.
179 db_cmd_search(name, table, aux_tablep, aux_tablep_end, cmdp)
181 struct command *table;
182 struct command **aux_tablep;
183 struct command **aux_tablep_end;
184 struct command **cmdp; /* out */
187 struct command **aux_cmdp;
188 int result = CMD_NONE;
190 for (cmd = table; cmd->name != 0; cmd++) {
197 while ((c = *lp) == *rp) {
207 /* end of name, not end of command -
209 if (result == CMD_FOUND) {
210 result = CMD_AMBIGUOUS;
211 /* but keep looking for a full match -
212 this lets us match single letters */
220 if (result == CMD_NONE && aux_tablep != 0)
221 /* XXX repeat too much code. */
222 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
228 rp = (*aux_cmdp)->name;
229 while ((c = *lp) == *rp) {
239 /* end of name, not end of command -
241 if (result == CMD_FOUND) {
242 result = CMD_AMBIGUOUS;
243 /* but keep looking for a full match -
244 this lets us match single letters */
252 if (result == CMD_NONE) {
253 /* check for 'help' */
254 if (name[0] == 'h' && name[1] == 'e'
255 && name[2] == 'l' && name[3] == 'p')
262 db_cmd_list(table, aux_tablep, aux_tablep_end)
263 struct command *table;
264 struct command **aux_tablep;
265 struct command **aux_tablep_end;
267 register struct command *cmd;
268 register struct command **aux_cmdp;
270 for (cmd = table; cmd->name != 0; cmd++) {
271 db_printf("%-12s", cmd->name);
276 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
277 db_printf("%-12s", (*aux_cmdp)->name);
283 db_command(last_cmdp, cmd_table, aux_cmd_tablep, aux_cmd_tablep_end)
284 struct command **last_cmdp; /* IN_OUT */
285 struct command *cmd_table;
286 struct command **aux_cmd_tablep;
287 struct command **aux_cmd_tablep_end;
291 char modif[TOK_STRING_SIZE];
292 db_expr_t addr, count;
293 boolean_t have_addr = FALSE;
298 /* empty line repeats last command, at 'next' */
300 addr = (db_expr_t)db_next;
305 else if (t == tEXCL) {
306 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
309 else if (t != tIDENT) {
319 result = db_cmd_search(db_tok_string,
326 db_printf("No such command\n");
330 db_printf("Ambiguous\n");
334 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
340 if ((cmd_table = cmd->more) != 0) {
341 /* XXX usually no more aux's. */
343 if (cmd_table == db_show_cmds) {
344 aux_cmd_tablep = SET_BEGIN(db_show_cmd_set);
345 aux_cmd_tablep_end = SET_LIMIT(db_show_cmd_set);
350 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
357 if ((cmd->flag & CS_OWN) == 0) {
360 * command [/modifier] [addr] [,count]
366 db_printf("Bad modifier\n");
370 db_strcpy(modif, db_tok_string);
377 if (db_expression(&addr)) {
378 db_dot = (db_addr_t) addr;
379 db_last_addr = db_dot;
383 addr = (db_expr_t) db_dot;
388 if (!db_expression(&count)) {
389 db_printf("Count missing\n");
398 if ((cmd->flag & CS_MORE) == 0) {
406 * Execute the command.
408 (*cmd->fcn)(addr, have_addr, count, modif);
409 db_setup_paging(NULL, NULL, -1);
411 if (cmd->flag & CS_SET_DOT) {
413 * If command changes dot, set dot to
414 * previous address displayed (if 'ed' style).
425 * If command does not change dot,
426 * set 'next' location to be the same.
434 * At least one non-optional command must be implemented using
435 * DB_COMMAND() so that db_cmd_set gets created. Here is one.
437 DB_COMMAND(panic, db_panic)
439 panic("from debugger");
446 * Initialize 'prev' and 'next' to dot.
451 db_cmd_loop_done = 0;
452 while (!db_cmd_loop_done) {
453 if (db_print_position() != 0)
457 (void) db_read_line();
459 db_command(&db_last_command, db_command_table,
460 SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set));
476 * Call random function:
480 /* The generic implementation supports a maximum of 10 arguments. */
481 typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
482 db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
485 db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
487 __db_f *f = (__db_f *)addr;
490 db_printf("Too many arguments (max 10)\n");
493 *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
494 args[6], args[7], args[8], args[9]);
499 db_fncall(dummy1, dummy2, dummy3, dummy4)
506 db_expr_t args[DB_MAXARGS];
511 if (!db_expression(&fn_addr)) {
512 db_printf("Bad function\n");
519 if (db_expression(&args[0])) {
521 while ((t = db_read_token()) == tCOMMA) {
522 if (nargs == DB_MAXARGS) {
523 db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
527 if (!db_expression(&args[nargs])) {
528 db_printf("Argument missing\n");
536 if (db_read_token() != tRPAREN) {
544 if (DB_CALL(fn_addr, &retval, nargs, args))
545 db_printf("= %#lr\n", (long)retval);
549 db_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
556 db_kill(dummy1, dummy2, dummy3, dummy4)
562 db_expr_t old_radix, pid, sig;
565 #define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0)
568 * PIDs and signal numbers are typically represented in base
569 * 10, so make that the default here. It can, of course, be
570 * overridden by specifying a prefix.
572 old_radix = db_radix;
574 /* Retrieve arguments. */
575 if (!db_expression(&sig))
576 DB_ERROR(("Missing signal number\n"));
577 if (!db_expression(&pid))
578 DB_ERROR(("Missing process ID\n"));
580 if (sig < 1 || sig > _SIG_MAXSIG)
581 DB_ERROR(("Signal number out of range\n"));
584 * Find the process in question. allproc_lock is not needed
585 * since we're in DDB.
587 /* sx_slock(&allproc_lock); */
588 LIST_FOREACH(p, &allproc, p_list)
591 /* sx_sunlock(&allproc_lock); */
593 DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
595 /* If it's already locked, bail; otherwise, do the deed. */
596 if (PROC_TRYLOCK(p) == 0)
597 DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
604 db_radix = old_radix;
609 db_reset(dummy1, dummy2, dummy3, dummy4)
620 db_watchdog(dummy1, dummy2, dummy3, dummy4)
629 * XXX: It might make sense to be able to set the watchdog to a
630 * XXX: timeout here so that failure or hang as a result of subsequent
631 * XXX: ddb commands could be recovered by a reset.
634 EVENTHANDLER_INVOKE(watchdog_list, 0, &i);
638 db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
641 if (kdb_dbbe_select("gdb") != 0)
642 db_printf("The remote GDB backend could not be selected.\n");
644 db_printf("Step to enter the remote GDB backend.\n");
648 db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
656 * We parse our own arguments. We don't like the default radix.
660 hastid = db_expression(&tid);
663 if (!db_expression(&count)) {
664 db_printf("Count missing\n");
676 td = kdb_thr_lookup((lwpid_t)tid);
678 td = kdb_thr_from_pid((pid_t)tid);
680 db_printf("Thread %d not found\n", (int)tid);
685 if (td->td_proc != NULL)
686 pid = td->td_proc->p_pid;
689 db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
690 db_trace_thread(td, count);
694 db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
700 for (p = LIST_FIRST(&allproc); p != NULL; p = LIST_NEXT(p, p_list)) {
701 FOREACH_THREAD_IN_PROC(p, td) {
702 db_printf("\nTracing command %s pid %d tid %ld td %p\n",
703 p->p_comm, p->p_pid, (long)td->td_tid, td);
704 db_trace_thread(td, -1);