1dd3cb568SWarner Losh /*- 2796df753SPedro F. Giffuni * SPDX-License-Identifier: MIT-CMU 3796df753SPedro F. Giffuni * 45b81b6b3SRodney W. Grimes * Mach Operating System 55b81b6b3SRodney W. Grimes * Copyright (c) 1991,1990 Carnegie Mellon University 65b81b6b3SRodney W. Grimes * All Rights Reserved. 75b81b6b3SRodney W. Grimes * 85b81b6b3SRodney W. Grimes * Permission to use, copy, modify and distribute this software and its 95b81b6b3SRodney W. Grimes * documentation is hereby granted, provided that both the copyright 105b81b6b3SRodney W. Grimes * notice and this permission notice appear in all copies of the 115b81b6b3SRodney W. Grimes * software, derivative works or modified versions, and any portions 125b81b6b3SRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 135b81b6b3SRodney W. Grimes * 145b81b6b3SRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 155b81b6b3SRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 165b81b6b3SRodney W. Grimes * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 175b81b6b3SRodney W. Grimes * 185b81b6b3SRodney W. Grimes * Carnegie Mellon requests users of this software to return to 195b81b6b3SRodney W. Grimes * 205b81b6b3SRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 215b81b6b3SRodney W. Grimes * School of Computer Science 225b81b6b3SRodney W. Grimes * Carnegie Mellon University 235b81b6b3SRodney W. Grimes * Pittsburgh PA 15213-3890 245b81b6b3SRodney W. Grimes * 255b81b6b3SRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 265b81b6b3SRodney W. Grimes * rights to redistribute these changes. 275b81b6b3SRodney W. Grimes */ 285b81b6b3SRodney W. Grimes /* 295b81b6b3SRodney W. Grimes * Author: David B. Golub, Carnegie Mellon University 305b81b6b3SRodney W. Grimes * Date: 7/90 315b81b6b3SRodney W. Grimes */ 325b81b6b3SRodney W. Grimes /* 335b81b6b3SRodney W. Grimes * Command dispatcher. 345b81b6b3SRodney W. Grimes */ 35753960f7SDavid E. O'Brien 36f540b106SGarrett Wollman #include <sys/param.h> 374f2ad624SMitchell Horne #include <sys/conf.h> 384f2ad624SMitchell Horne #include <sys/cons.h> 39e2e050c8SConrad Meyer #include <sys/eventhandler.h> 404f2ad624SMitchell Horne #include <sys/kdb.h> 414f2ad624SMitchell Horne #include <sys/kernel.h> 420ec81012SJohn Polstra #include <sys/linker_set.h> 4319d2c78fSDima Dorfman #include <sys/lock.h> 4419d2c78fSDima Dorfman #include <sys/mutex.h> 4519d2c78fSDima Dorfman #include <sys/proc.h> 46ad146781SPaul Traina #include <sys/reboot.h> 4719d2c78fSDima Dorfman #include <sys/signalvar.h> 48f540b106SGarrett Wollman #include <sys/systm.h> 4974cc032bSPoul-Henning Kamp #include <sys/watchdog.h> 503000820aSPoul-Henning Kamp 515ccbc3ccSBruce Evans #include <ddb/ddb.h> 52058284fcSBruce Evans #include <ddb/db_command.h> 535b81b6b3SRodney W. Grimes #include <ddb/db_lex.h> 545b81b6b3SRodney W. Grimes #include <ddb/db_output.h> 555b81b6b3SRodney W. Grimes 5626502503SMarcel Moolenaar #include <machine/cpu.h> 570b1ae809SJulian Elischer #include <machine/setjmp.h> 585b81b6b3SRodney W. Grimes 592449b9e5SMitchell Horne #include <security/mac/mac_framework.h> 602449b9e5SMitchell Horne 615b81b6b3SRodney W. Grimes /* 625b81b6b3SRodney W. Grimes * Exported global variables 635b81b6b3SRodney W. Grimes */ 6451d025a5SJustin Hibbits int db_cmd_loop_done; 655b81b6b3SRodney W. Grimes db_addr_t db_dot; 665b81b6b3SRodney W. Grimes db_addr_t db_last_addr; 675b81b6b3SRodney W. Grimes db_addr_t db_prev; 685b81b6b3SRodney W. Grimes db_addr_t db_next; 69f41325dbSPeter Wemm 70299cceefSMarcel Moolenaar static db_cmdfcn_t db_dump; 71f73a856dSPoul-Henning Kamp static db_cmdfcn_t db_fncall; 72f3be7cb3SMarcel Moolenaar static db_cmdfcn_t db_gdb; 73e4b732cfSBruce Evans static db_cmdfcn_t db_halt; 7419d2c78fSDima Dorfman static db_cmdfcn_t db_kill; 755370c3b6SPeter Wemm static db_cmdfcn_t db_reset; 76fd32d93bSMarcel Moolenaar static db_cmdfcn_t db_stack_trace; 777e89a322SConrad Meyer static db_cmdfcn_t db_stack_trace_active; 78a7ad956bSRobert Watson static db_cmdfcn_t db_stack_trace_all; 7974cc032bSPoul-Henning Kamp static db_cmdfcn_t db_watchdog; 80ad146781SPaul Traina 818a099482SMitchell Horne #define DB_CMD(_name, _func, _flags) \ 828a099482SMitchell Horne { \ 838a099482SMitchell Horne .name = (_name), \ 848a099482SMitchell Horne .fcn = (_func), \ 858a099482SMitchell Horne .flag = (_flags), \ 868a099482SMitchell Horne .more = NULL, \ 878a099482SMitchell Horne } 888a099482SMitchell Horne #define DB_TABLE(_name, _more) \ 898a099482SMitchell Horne { \ 908a099482SMitchell Horne .name = (_name), \ 918a099482SMitchell Horne .fcn = NULL, \ 928a099482SMitchell Horne .more = (_more), \ 938a099482SMitchell Horne } 94cec9a4bfSDavid E. O'Brien 954ef7db5aSMitchell Horne static struct db_command db_show_active_cmds[] = { 96a305b20eSMitchell Horne DB_CMD("trace", db_stack_trace_active, DB_CMD_MEMSAFE), 977e89a322SConrad Meyer }; 984ef7db5aSMitchell Horne struct db_command_table db_show_active_table = 997e89a322SConrad Meyer LIST_HEAD_INITIALIZER(db_show_active_table); 1007e89a322SConrad Meyer 1014ef7db5aSMitchell Horne static struct db_command db_show_all_cmds[] = { 102a305b20eSMitchell Horne DB_CMD("trace", db_stack_trace_all, DB_CMD_MEMSAFE), 103cec9a4bfSDavid E. O'Brien }; 1044ef7db5aSMitchell Horne struct db_command_table db_show_all_table = 10539297ba4SSam Leffler LIST_HEAD_INITIALIZER(db_show_all_table); 106e1e31c0eSJohn Baldwin 1074ef7db5aSMitchell Horne static struct db_command db_show_cmds[] = { 1088a099482SMitchell Horne DB_TABLE("active", &db_show_active_table), 1098a099482SMitchell Horne DB_TABLE("all", &db_show_all_table), 110a305b20eSMitchell Horne DB_CMD("registers", db_show_regs, DB_CMD_MEMSAFE), 111a305b20eSMitchell Horne DB_CMD("breaks", db_listbreak_cmd, DB_CMD_MEMSAFE), 112a305b20eSMitchell Horne DB_CMD("threads", db_show_threads, DB_CMD_MEMSAFE), 113cec9a4bfSDavid E. O'Brien }; 1144ef7db5aSMitchell Horne struct db_command_table db_show_table = LIST_HEAD_INITIALIZER(db_show_table); 115cec9a4bfSDavid E. O'Brien 1164ef7db5aSMitchell Horne static struct db_command db_cmds[] = { 1178a099482SMitchell Horne DB_TABLE("show", &db_show_table), 1188a099482SMitchell Horne DB_CMD("print", db_print_cmd, 0), 1198a099482SMitchell Horne DB_CMD("p", db_print_cmd, 0), 1208a099482SMitchell Horne DB_CMD("examine", db_examine_cmd, CS_SET_DOT), 1218a099482SMitchell Horne DB_CMD("x", db_examine_cmd, CS_SET_DOT), 1228a099482SMitchell Horne DB_CMD("search", db_search_cmd, CS_OWN|CS_SET_DOT), 123a305b20eSMitchell Horne DB_CMD("set", db_set_cmd, CS_OWN|DB_CMD_MEMSAFE), 1248a099482SMitchell Horne DB_CMD("write", db_write_cmd, CS_MORE|CS_SET_DOT), 1258a099482SMitchell Horne DB_CMD("w", db_write_cmd, CS_MORE|CS_SET_DOT), 126c036339dSMark Johnston DB_CMD("delete", db_delete_cmd, 0), 127c036339dSMark Johnston DB_CMD("d", db_delete_cmd, 0), 128a305b20eSMitchell Horne DB_CMD("dump", db_dump, DB_CMD_MEMSAFE), 129c036339dSMark Johnston DB_CMD("break", db_breakpoint_cmd, 0), 130c036339dSMark Johnston DB_CMD("b", db_breakpoint_cmd, 0), 131c036339dSMark Johnston DB_CMD("dwatch", db_deletewatch_cmd, 0), 132c036339dSMark Johnston DB_CMD("watch", db_watchpoint_cmd, CS_MORE), 133c036339dSMark Johnston DB_CMD("dhwatch", db_deletehwatch_cmd, 0), 134c036339dSMark Johnston DB_CMD("hwatch", db_hwatchpoint_cmd, 0), 135a305b20eSMitchell Horne DB_CMD("step", db_single_step_cmd, DB_CMD_MEMSAFE), 136a305b20eSMitchell Horne DB_CMD("s", db_single_step_cmd, DB_CMD_MEMSAFE), 137a305b20eSMitchell Horne DB_CMD("continue", db_continue_cmd, DB_CMD_MEMSAFE), 138a305b20eSMitchell Horne DB_CMD("c", db_continue_cmd, DB_CMD_MEMSAFE), 139a305b20eSMitchell Horne DB_CMD("until", db_trace_until_call_cmd, DB_CMD_MEMSAFE), 140a305b20eSMitchell Horne DB_CMD("next", db_trace_until_matching_cmd, DB_CMD_MEMSAFE), 1418a099482SMitchell Horne DB_CMD("match", db_trace_until_matching_cmd, 0), 142a305b20eSMitchell Horne DB_CMD("trace", db_stack_trace, CS_OWN|DB_CMD_MEMSAFE), 143a305b20eSMitchell Horne DB_CMD("t", db_stack_trace, CS_OWN|DB_CMD_MEMSAFE), 1447e89a322SConrad Meyer /* XXX alias for active trace */ 145a305b20eSMitchell Horne DB_CMD("acttrace", db_stack_trace_active, DB_CMD_MEMSAFE), 14639297ba4SSam Leffler /* XXX alias for all trace */ 147a305b20eSMitchell Horne DB_CMD("alltrace", db_stack_trace_all, DB_CMD_MEMSAFE), 148a305b20eSMitchell Horne DB_CMD("where", db_stack_trace, CS_OWN|DB_CMD_MEMSAFE), 149a305b20eSMitchell Horne DB_CMD("bt", db_stack_trace, CS_OWN|DB_CMD_MEMSAFE), 1508a099482SMitchell Horne DB_CMD("call", db_fncall, CS_OWN), 151a305b20eSMitchell Horne DB_CMD("ps", db_ps, DB_CMD_MEMSAFE), 1528a099482SMitchell Horne DB_CMD("gdb", db_gdb, 0), 153a305b20eSMitchell Horne DB_CMD("halt", db_halt, DB_CMD_MEMSAFE), 154a305b20eSMitchell Horne DB_CMD("reboot", db_reset, DB_CMD_MEMSAFE), 155a305b20eSMitchell Horne DB_CMD("reset", db_reset, DB_CMD_MEMSAFE), 156a305b20eSMitchell Horne DB_CMD("kill", db_kill, CS_OWN|DB_CMD_MEMSAFE), 157a305b20eSMitchell Horne DB_CMD("watchdog", db_watchdog, CS_OWN|DB_CMD_MEMSAFE), 1588a099482SMitchell Horne DB_CMD("thread", db_set_thread, 0), 159a305b20eSMitchell Horne DB_CMD("run", db_run_cmd, CS_OWN|DB_CMD_MEMSAFE), 160a305b20eSMitchell Horne DB_CMD("script", db_script_cmd, CS_OWN|DB_CMD_MEMSAFE), 161a305b20eSMitchell Horne DB_CMD("scripts", db_scripts_cmd, DB_CMD_MEMSAFE), 162a305b20eSMitchell Horne DB_CMD("unscript", db_unscript_cmd, CS_OWN|DB_CMD_MEMSAFE), 163a305b20eSMitchell Horne DB_CMD("capture", db_capture_cmd, CS_OWN|DB_CMD_MEMSAFE), 164a305b20eSMitchell Horne DB_CMD("textdump", db_textdump_cmd, CS_OWN|DB_CMD_MEMSAFE), 1658a099482SMitchell Horne DB_CMD("findstack", db_findstack_cmd, 0), 166*c21bc6f3SBojan Novković DB_CMD("pprint", db_pprint_cmd, CS_OWN), 167cec9a4bfSDavid E. O'Brien }; 1684ef7db5aSMitchell Horne struct db_command_table db_cmd_table = LIST_HEAD_INITIALIZER(db_cmd_table); 169e1e31c0eSJohn Baldwin 1708a099482SMitchell Horne #undef DB_CMD 1718a099482SMitchell Horne #undef DB_TABLE 1728a099482SMitchell Horne 1734ef7db5aSMitchell Horne static struct db_command *db_last_command = NULL; 1746337f4efSBruce Evans 1755b81b6b3SRodney W. Grimes /* 1765b81b6b3SRodney W. Grimes * if 'ed' style: 'dot' is set at start of last item printed, 1775b81b6b3SRodney W. Grimes * and '+' points to next line. 1785b81b6b3SRodney W. Grimes * Otherwise: 'dot' points to next item, '..' points to last. 1795b81b6b3SRodney W. Grimes */ 180cd508278SPedro F. Giffuni static bool db_ed_style = true; 1815b81b6b3SRodney W. Grimes 1825b81b6b3SRodney W. Grimes /* 1835b81b6b3SRodney W. Grimes * Utility routine - discard tokens through end-of-line. 1845b81b6b3SRodney W. Grimes */ 1855b81b6b3SRodney W. Grimes void 186a41dd031SPedro F. Giffuni db_skip_to_eol(void) 1875b81b6b3SRodney W. Grimes { 1885b81b6b3SRodney W. Grimes int t; 1894f2ad624SMitchell Horne 1905b81b6b3SRodney W. Grimes do { 1915b81b6b3SRodney W. Grimes t = db_read_token(); 1925b81b6b3SRodney W. Grimes } while (t != tEOL); 1935b81b6b3SRodney W. Grimes } 1945b81b6b3SRodney W. Grimes 1955b81b6b3SRodney W. Grimes /* 1965b81b6b3SRodney W. Grimes * Results of command search. 1975b81b6b3SRodney W. Grimes */ 1985b81b6b3SRodney W. Grimes #define CMD_UNIQUE 0 1995b81b6b3SRodney W. Grimes #define CMD_FOUND 1 2005b81b6b3SRodney W. Grimes #define CMD_NONE 2 2015b81b6b3SRodney W. Grimes #define CMD_AMBIGUOUS 3 2025b81b6b3SRodney W. Grimes #define CMD_HELP 4 2035b81b6b3SRodney W. Grimes 2044ef7db5aSMitchell Horne static void db_cmd_match(char *name, struct db_command *cmd, 2054ef7db5aSMitchell Horne struct db_command **cmdp, int *resultp); 2064ef7db5aSMitchell Horne static void db_cmd_list(struct db_command_table *table); 2074ef7db5aSMitchell Horne static int db_cmd_search(char *name, struct db_command_table *table, 2084ef7db5aSMitchell Horne struct db_command **cmdp); 2094ef7db5aSMitchell Horne static void db_command(struct db_command **last_cmdp, 2104f2ad624SMitchell Horne struct db_command_table *cmd_table, bool dopager); 211058284fcSBruce Evans 2125b81b6b3SRodney W. Grimes /* 21339297ba4SSam Leffler * Initialize the command lists from the static tables. 21439297ba4SSam Leffler */ 21593d4804bSJohn Baldwin void 21693d4804bSJohn Baldwin db_command_init(void) 21739297ba4SSam Leffler { 21839297ba4SSam Leffler int i; 21939297ba4SSam Leffler 2204f2ad624SMitchell Horne for (i = 0; i < nitems(db_cmds); i++) 22139297ba4SSam Leffler db_command_register(&db_cmd_table, &db_cmds[i]); 2224f2ad624SMitchell Horne for (i = 0; i < nitems(db_show_cmds); i++) 22339297ba4SSam Leffler db_command_register(&db_show_table, &db_show_cmds[i]); 2244f2ad624SMitchell Horne for (i = 0; i < nitems(db_show_active_cmds); i++) 2257e89a322SConrad Meyer db_command_register(&db_show_active_table, 2267e89a322SConrad Meyer &db_show_active_cmds[i]); 2274f2ad624SMitchell Horne for (i = 0; i < nitems(db_show_all_cmds); i++) 22839297ba4SSam Leffler db_command_register(&db_show_all_table, &db_show_all_cmds[i]); 22939297ba4SSam Leffler } 23039297ba4SSam Leffler 23139297ba4SSam Leffler /* 23239297ba4SSam Leffler * Register a command. 23339297ba4SSam Leffler */ 23439297ba4SSam Leffler void 2354ef7db5aSMitchell Horne db_command_register(struct db_command_table *list, struct db_command *cmd) 23639297ba4SSam Leffler { 2374ef7db5aSMitchell Horne struct db_command *c, *last; 23839297ba4SSam Leffler 2392449b9e5SMitchell Horne #ifdef MAC 2402449b9e5SMitchell Horne if (mac_ddb_command_register(list, cmd)) { 2412449b9e5SMitchell Horne printf("%s: MAC policy refused registration of command %s\n", 2422449b9e5SMitchell Horne __func__, cmd->name); 2432449b9e5SMitchell Horne return; 2442449b9e5SMitchell Horne } 2452449b9e5SMitchell Horne #endif 24639297ba4SSam Leffler last = NULL; 24739297ba4SSam Leffler LIST_FOREACH(c, list, next) { 24839297ba4SSam Leffler int n = strcmp(cmd->name, c->name); 24939297ba4SSam Leffler 25039297ba4SSam Leffler /* Check that the command is not already present. */ 25139297ba4SSam Leffler if (n == 0) { 25239297ba4SSam Leffler printf("%s: Warning, the command \"%s\" already exists;" 25339297ba4SSam Leffler " ignoring request\n", __func__, cmd->name); 25439297ba4SSam Leffler return; 25539297ba4SSam Leffler } 25639297ba4SSam Leffler if (n < 0) { 25739297ba4SSam Leffler /* NB: keep list sorted lexicographically */ 25839297ba4SSam Leffler LIST_INSERT_BEFORE(c, cmd, next); 25939297ba4SSam Leffler return; 26039297ba4SSam Leffler } 26139297ba4SSam Leffler last = c; 26239297ba4SSam Leffler } 26339297ba4SSam Leffler if (last == NULL) 26439297ba4SSam Leffler LIST_INSERT_HEAD(list, cmd, next); 26539297ba4SSam Leffler else 26639297ba4SSam Leffler LIST_INSERT_AFTER(last, cmd, next); 26739297ba4SSam Leffler } 26839297ba4SSam Leffler 26939297ba4SSam Leffler /* 27039297ba4SSam Leffler * Remove a command previously registered with db_command_register. 27139297ba4SSam Leffler */ 27239297ba4SSam Leffler void 2734ef7db5aSMitchell Horne db_command_unregister(struct db_command_table *list, struct db_command *cmd) 27439297ba4SSam Leffler { 2754ef7db5aSMitchell Horne struct db_command *c; 27639297ba4SSam Leffler 27739297ba4SSam Leffler LIST_FOREACH(c, list, next) { 27839297ba4SSam Leffler if (cmd == c) { 27939297ba4SSam Leffler LIST_REMOVE(cmd, next); 28039297ba4SSam Leffler return; 28139297ba4SSam Leffler } 28239297ba4SSam Leffler } 28339297ba4SSam Leffler /* NB: intentionally quiet */ 28439297ba4SSam Leffler } 28539297ba4SSam Leffler 28639297ba4SSam Leffler /* 287e1e31c0eSJohn Baldwin * Helper function to match a single command. 2885b81b6b3SRodney W. Grimes */ 289e1e31c0eSJohn Baldwin static void 2904ef7db5aSMitchell Horne db_cmd_match(char *name, struct db_command *cmd, struct db_command **cmdp, 291a41dd031SPedro F. Giffuni int *resultp) 292e1e31c0eSJohn Baldwin { 293e1e31c0eSJohn Baldwin char *lp, *rp; 294e1e31c0eSJohn Baldwin int c; 2955b81b6b3SRodney W. Grimes 2965b81b6b3SRodney W. Grimes lp = name; 2975b81b6b3SRodney W. Grimes rp = cmd->name; 2985b81b6b3SRodney W. Grimes while ((c = *lp) == *rp) { 2995b81b6b3SRodney W. Grimes if (c == 0) { 3005b81b6b3SRodney W. Grimes /* complete match */ 3015b81b6b3SRodney W. Grimes *cmdp = cmd; 302e1e31c0eSJohn Baldwin *resultp = CMD_UNIQUE; 303e1e31c0eSJohn Baldwin return; 3045b81b6b3SRodney W. Grimes } 3055b81b6b3SRodney W. Grimes lp++; 3065b81b6b3SRodney W. Grimes rp++; 3075b81b6b3SRodney W. Grimes } 3085b81b6b3SRodney W. Grimes if (c == 0) { 3095b81b6b3SRodney W. Grimes /* end of name, not end of command - 3105b81b6b3SRodney W. Grimes partial match */ 311e1e31c0eSJohn Baldwin if (*resultp == CMD_FOUND) { 312e1e31c0eSJohn Baldwin *resultp = CMD_AMBIGUOUS; 3135b81b6b3SRodney W. Grimes /* but keep looking for a full match - 3145b81b6b3SRodney W. Grimes this lets us match single letters */ 315d85c9cefSRyan Libby } else if (*resultp == CMD_NONE) { 3165b81b6b3SRodney W. Grimes *cmdp = cmd; 317e1e31c0eSJohn Baldwin *resultp = CMD_FOUND; 3185b81b6b3SRodney W. Grimes } 3195b81b6b3SRodney W. Grimes } 3205b81b6b3SRodney W. Grimes } 3216337f4efSBruce Evans 322e1e31c0eSJohn Baldwin /* 323e1e31c0eSJohn Baldwin * Search for command prefix. 324e1e31c0eSJohn Baldwin */ 325e1e31c0eSJohn Baldwin static int 3264ef7db5aSMitchell Horne db_cmd_search(char *name, struct db_command_table *table, 3274ef7db5aSMitchell Horne struct db_command **cmdp) 328e1e31c0eSJohn Baldwin { 3294ef7db5aSMitchell Horne struct db_command *cmd; 330e1e31c0eSJohn Baldwin int result = CMD_NONE; 331e1e31c0eSJohn Baldwin 33239297ba4SSam Leffler LIST_FOREACH(cmd, table, next) { 333e1e31c0eSJohn Baldwin db_cmd_match(name,cmd,cmdp,&result); 334e1e31c0eSJohn Baldwin if (result == CMD_UNIQUE) 33539297ba4SSam Leffler break; 3366337f4efSBruce Evans } 33739297ba4SSam Leffler 3385b81b6b3SRodney W. Grimes if (result == CMD_NONE) { 3395b81b6b3SRodney W. Grimes /* check for 'help' */ 3405b81b6b3SRodney W. Grimes if (name[0] == 'h' && name[1] == 'e' 3415b81b6b3SRodney W. Grimes && name[2] == 'l' && name[3] == 'p') 3425b81b6b3SRodney W. Grimes result = CMD_HELP; 3435b81b6b3SRodney W. Grimes } 3445b81b6b3SRodney W. Grimes return (result); 3455b81b6b3SRodney W. Grimes } 3465b81b6b3SRodney W. Grimes 347f73a856dSPoul-Henning Kamp static void 3484ef7db5aSMitchell Horne db_cmd_list(struct db_command_table *table) 3495b81b6b3SRodney W. Grimes { 3504ef7db5aSMitchell Horne struct db_command *cmd; 351b5bd6c73SEdward Tomasz Napierala int have_subcommands; 3525b81b6b3SRodney W. Grimes 353b5bd6c73SEdward Tomasz Napierala have_subcommands = 0; 35439297ba4SSam Leffler LIST_FOREACH(cmd, table, next) { 355b5bd6c73SEdward Tomasz Napierala if (cmd->more != NULL) 356b5bd6c73SEdward Tomasz Napierala have_subcommands++; 357527be4f2SJohn-Mark Gurney db_printf("%-16s", cmd->name); 358527be4f2SJohn-Mark Gurney db_end_line(16); 3595b81b6b3SRodney W. Grimes } 360b5bd6c73SEdward Tomasz Napierala 361b5bd6c73SEdward Tomasz Napierala if (have_subcommands > 0) { 362b5bd6c73SEdward Tomasz Napierala db_printf("\nThe following have subcommands; append \"help\" " 363b5bd6c73SEdward Tomasz Napierala "to list (e.g. \"show help\"):\n"); 364b5bd6c73SEdward Tomasz Napierala LIST_FOREACH(cmd, table, next) { 365b5bd6c73SEdward Tomasz Napierala if (cmd->more == NULL) 366b5bd6c73SEdward Tomasz Napierala continue; 367b5bd6c73SEdward Tomasz Napierala db_printf("%-16s", cmd->name); 368b5bd6c73SEdward Tomasz Napierala db_end_line(16); 369b5bd6c73SEdward Tomasz Napierala } 370b5bd6c73SEdward Tomasz Napierala } 3715b81b6b3SRodney W. Grimes } 3725b81b6b3SRodney W. Grimes 373f73a856dSPoul-Henning Kamp static void 3744ef7db5aSMitchell Horne db_command(struct db_command **last_cmdp, struct db_command_table *cmd_table, 3754f2ad624SMitchell Horne bool dopager) 3765b81b6b3SRodney W. Grimes { 3775b81b6b3SRodney W. Grimes char modif[TOK_STRING_SIZE]; 3784f2ad624SMitchell Horne struct db_command *cmd = NULL; 3795b81b6b3SRodney W. Grimes db_expr_t addr, count; 3804f2ad624SMitchell Horne int t, result; 381cd508278SPedro F. Giffuni bool have_addr = false; 3825b81b6b3SRodney W. Grimes 3835b81b6b3SRodney W. Grimes t = db_read_token(); 3845b81b6b3SRodney W. Grimes if (t == tEOL) { 3855b81b6b3SRodney W. Grimes /* empty line repeats last command, at 'next' */ 3865b81b6b3SRodney W. Grimes cmd = *last_cmdp; 3875b81b6b3SRodney W. Grimes addr = (db_expr_t)db_next; 3882b490bc7SPedro F. Giffuni have_addr = false; 3895b81b6b3SRodney W. Grimes count = 1; 3905b81b6b3SRodney W. Grimes modif[0] = '\0'; 3914f2ad624SMitchell Horne } else if (t == tEXCL) { 3924f2ad624SMitchell Horne db_fncall((db_expr_t)0, false, (db_expr_t)0, NULL); 3935b81b6b3SRodney W. Grimes return; 3944f2ad624SMitchell Horne } else if (t != tIDENT) { 3959990da25SEdward Tomasz Napierala db_printf("Unrecognized input; use \"help\" " 3969990da25SEdward Tomasz Napierala "to list available commands\n"); 3975b81b6b3SRodney W. Grimes db_flush_lex(); 3985b81b6b3SRodney W. Grimes return; 3994f2ad624SMitchell Horne } else { 4005b81b6b3SRodney W. Grimes /* 4015b81b6b3SRodney W. Grimes * Search for command 4025b81b6b3SRodney W. Grimes */ 4034f2ad624SMitchell Horne while (cmd_table != NULL) { 4044f2ad624SMitchell Horne result = db_cmd_search(db_tok_string, cmd_table, &cmd); 4055b81b6b3SRodney W. Grimes switch (result) { 4065b81b6b3SRodney W. Grimes case CMD_NONE: 407b5bd6c73SEdward Tomasz Napierala db_printf("No such command; use \"help\" " 408b5bd6c73SEdward Tomasz Napierala "to list available commands\n"); 4095b81b6b3SRodney W. Grimes db_flush_lex(); 4105b81b6b3SRodney W. Grimes return; 4115b81b6b3SRodney W. Grimes case CMD_AMBIGUOUS: 4125b81b6b3SRodney W. Grimes db_printf("Ambiguous\n"); 4135b81b6b3SRodney W. Grimes db_flush_lex(); 4145b81b6b3SRodney W. Grimes return; 4155b81b6b3SRodney W. Grimes case CMD_HELP: 416b5bd6c73SEdward Tomasz Napierala if (cmd_table == &db_cmd_table) { 417b5bd6c73SEdward Tomasz Napierala db_printf("This is ddb(4), the kernel debugger; " 418c6404e66SGavin Atkinson "see https://man.FreeBSD.org/ddb/4 for help.\n"); 419b5bd6c73SEdward Tomasz Napierala db_printf("Use \"bt\" for backtrace, \"dump\" for " 420b5bd6c73SEdward Tomasz Napierala "kernel core dump, \"reset\" to reboot.\n"); 421b5bd6c73SEdward Tomasz Napierala db_printf("Available commands:\n"); 422b5bd6c73SEdward Tomasz Napierala } 423e1e31c0eSJohn Baldwin db_cmd_list(cmd_table); 4245b81b6b3SRodney W. Grimes db_flush_lex(); 4255b81b6b3SRodney W. Grimes return; 4264f2ad624SMitchell Horne case CMD_UNIQUE: 4274f2ad624SMitchell Horne case CMD_FOUND: 4285b81b6b3SRodney W. Grimes break; 4295b81b6b3SRodney W. Grimes } 430e1e31c0eSJohn Baldwin if ((cmd_table = cmd->more) != NULL) { 4315b81b6b3SRodney W. Grimes t = db_read_token(); 4325b81b6b3SRodney W. Grimes if (t != tIDENT) { 433b5bd6c73SEdward Tomasz Napierala db_printf("Subcommand required; " 434b5bd6c73SEdward Tomasz Napierala "available subcommands:\n"); 435e1e31c0eSJohn Baldwin db_cmd_list(cmd_table); 4365b81b6b3SRodney W. Grimes db_flush_lex(); 4375b81b6b3SRodney W. Grimes return; 4385b81b6b3SRodney W. Grimes } 4395b81b6b3SRodney W. Grimes } 4405b81b6b3SRodney W. Grimes } 4415b81b6b3SRodney W. Grimes 4425b81b6b3SRodney W. Grimes if ((cmd->flag & CS_OWN) == 0) { 4435b81b6b3SRodney W. Grimes /* 4445b81b6b3SRodney W. Grimes * Standard syntax: 4455b81b6b3SRodney W. Grimes * command [/modifier] [addr] [,count] 4465b81b6b3SRodney W. Grimes */ 4475b81b6b3SRodney W. Grimes t = db_read_token(); 4485b81b6b3SRodney W. Grimes if (t == tSLASH) { 4495b81b6b3SRodney W. Grimes t = db_read_token(); 4505b81b6b3SRodney W. Grimes if (t != tIDENT) { 4515b81b6b3SRodney W. Grimes db_printf("Bad modifier\n"); 4525b81b6b3SRodney W. Grimes db_flush_lex(); 4535b81b6b3SRodney W. Grimes return; 4545b81b6b3SRodney W. Grimes } 4555b81b6b3SRodney W. Grimes db_strcpy(modif, db_tok_string); 4564f2ad624SMitchell Horne } else { 4575b81b6b3SRodney W. Grimes db_unread_token(t); 4585b81b6b3SRodney W. Grimes modif[0] = '\0'; 4595b81b6b3SRodney W. Grimes } 4605b81b6b3SRodney W. Grimes 4615b81b6b3SRodney W. Grimes if (db_expression(&addr)) { 4625b81b6b3SRodney W. Grimes db_dot = (db_addr_t) addr; 4635b81b6b3SRodney W. Grimes db_last_addr = db_dot; 4642b490bc7SPedro F. Giffuni have_addr = true; 4654f2ad624SMitchell Horne } else { 4665b81b6b3SRodney W. Grimes addr = (db_expr_t) db_dot; 4672b490bc7SPedro F. Giffuni have_addr = false; 4685b81b6b3SRodney W. Grimes } 4694f2ad624SMitchell Horne 4705b81b6b3SRodney W. Grimes t = db_read_token(); 4715b81b6b3SRodney W. Grimes if (t == tCOMMA) { 4725b81b6b3SRodney W. Grimes if (!db_expression(&count)) { 4735b81b6b3SRodney W. Grimes db_printf("Count missing\n"); 4745b81b6b3SRodney W. Grimes db_flush_lex(); 4755b81b6b3SRodney W. Grimes return; 4765b81b6b3SRodney W. Grimes } 4774f2ad624SMitchell Horne } else { 4785b81b6b3SRodney W. Grimes db_unread_token(t); 4795b81b6b3SRodney W. Grimes count = -1; 4805b81b6b3SRodney W. Grimes } 4814f2ad624SMitchell Horne 4825b81b6b3SRodney W. Grimes if ((cmd->flag & CS_MORE) == 0) { 4835b81b6b3SRodney W. Grimes db_skip_to_eol(); 4845b81b6b3SRodney W. Grimes } 4855b81b6b3SRodney W. Grimes } 4865b81b6b3SRodney W. Grimes } 4874f2ad624SMitchell Horne 4885b81b6b3SRodney W. Grimes *last_cmdp = cmd; 4899f915a92SPedro F. Giffuni if (cmd != NULL) { 4902449b9e5SMitchell Horne #ifdef MAC 4912449b9e5SMitchell Horne if (mac_ddb_command_exec(cmd, addr, have_addr, count, modif)) { 4922449b9e5SMitchell Horne db_printf("MAC prevented execution of command %s\n", 4932449b9e5SMitchell Horne cmd->name); 4942449b9e5SMitchell Horne return; 4952449b9e5SMitchell Horne } 4962449b9e5SMitchell Horne #endif 4975b81b6b3SRodney W. Grimes /* 4985b81b6b3SRodney W. Grimes * Execute the command. 4995b81b6b3SRodney W. Grimes */ 500c9b0cc3bSRobert Watson if (dopager) 50119e9205aSJohn Baldwin db_enable_pager(); 502c9b0cc3bSRobert Watson else 503c9b0cc3bSRobert Watson db_disable_pager(); 5045b81b6b3SRodney W. Grimes (*cmd->fcn)(addr, have_addr, count, modif); 505c9b0cc3bSRobert Watson if (dopager) 50619e9205aSJohn Baldwin db_disable_pager(); 5075b81b6b3SRodney W. Grimes 5085b81b6b3SRodney W. Grimes if (cmd->flag & CS_SET_DOT) { 5095b81b6b3SRodney W. Grimes /* 5104f2ad624SMitchell Horne * If command changes dot, set dot to previous address 5114f2ad624SMitchell Horne * displayed (if 'ed' style). 5125b81b6b3SRodney W. Grimes */ 5134f2ad624SMitchell Horne db_dot = db_ed_style ? db_prev : db_next; 5144f2ad624SMitchell Horne } else { 5155b81b6b3SRodney W. Grimes /* 5164f2ad624SMitchell Horne * If command does not change dot, set 'next' location 5174f2ad624SMitchell Horne * to be the same. 5185b81b6b3SRodney W. Grimes */ 5195b81b6b3SRodney W. Grimes db_next = db_dot; 5205b81b6b3SRodney W. Grimes } 5215b81b6b3SRodney W. Grimes } 5225b81b6b3SRodney W. Grimes } 5235b81b6b3SRodney W. Grimes 5245b81b6b3SRodney W. Grimes /* 525b7aa38c1SBruce Evans * At least one non-optional command must be implemented using 526b7aa38c1SBruce Evans * DB_COMMAND() so that db_cmd_set gets created. Here is one. 527b7aa38c1SBruce Evans */ 528c84c5e00SMitchell Horne DB_COMMAND_FLAGS(panic, db_panic, DB_CMD_MEMSAFE) 5293eac4884SPoul-Henning Kamp { 53015cc91d3SJohn Baldwin db_disable_pager(); 531edf8a815SDavid Greenman panic("from debugger"); 5323eac4884SPoul-Henning Kamp } 5333eac4884SPoul-Henning Kamp 5343eac4884SPoul-Henning Kamp void 535a41dd031SPedro F. Giffuni db_command_loop(void) 5365b81b6b3SRodney W. Grimes { 5375b81b6b3SRodney W. Grimes /* 5385b81b6b3SRodney W. Grimes * Initialize 'prev' and 'next' to dot. 5395b81b6b3SRodney W. Grimes */ 5405b81b6b3SRodney W. Grimes db_prev = db_dot; 5415b81b6b3SRodney W. Grimes db_next = db_dot; 5425b81b6b3SRodney W. Grimes 5435b81b6b3SRodney W. Grimes db_cmd_loop_done = 0; 5445b81b6b3SRodney W. Grimes while (!db_cmd_loop_done) { 5455b81b6b3SRodney W. Grimes if (db_print_position() != 0) 5465b81b6b3SRodney W. Grimes db_printf("\n"); 5475b81b6b3SRodney W. Grimes 5485b81b6b3SRodney W. Grimes db_printf("db> "); 5495b81b6b3SRodney W. Grimes (void)db_read_line(); 5505b81b6b3SRodney W. Grimes 5514f2ad624SMitchell Horne db_command(&db_last_command, &db_cmd_table, /* dopager */ true); 5525b81b6b3SRodney W. Grimes } 5535b81b6b3SRodney W. Grimes } 5545b81b6b3SRodney W. Grimes 555c9b0cc3bSRobert Watson /* 556c9b0cc3bSRobert Watson * Execute a command on behalf of a script. The caller is responsible for 557c9b0cc3bSRobert Watson * making sure that the command string is < DB_MAXLINE or it will be 558c9b0cc3bSRobert Watson * truncated. 559c9b0cc3bSRobert Watson * 560c9b0cc3bSRobert Watson * XXXRW: Runs by injecting faked input into DDB input stream; it would be 561c9b0cc3bSRobert Watson * nicer to use an alternative approach that didn't mess with the previous 562c9b0cc3bSRobert Watson * command buffer. 563c9b0cc3bSRobert Watson */ 564c9b0cc3bSRobert Watson void 565c9b0cc3bSRobert Watson db_command_script(const char *command) 566c9b0cc3bSRobert Watson { 567c9b0cc3bSRobert Watson db_prev = db_next = db_dot; 568c9b0cc3bSRobert Watson db_inject_line(command); 5694f2ad624SMitchell Horne db_command(&db_last_command, &db_cmd_table, /* dopager */ false); 570c9b0cc3bSRobert Watson } 571c9b0cc3bSRobert Watson 5725b81b6b3SRodney W. Grimes void 573a41dd031SPedro F. Giffuni db_error(const char *s) 5745b81b6b3SRodney W. Grimes { 5755b81b6b3SRodney W. Grimes if (s) 57658d9a059SKris Kennaway db_printf("%s", s); 5775b81b6b3SRodney W. Grimes db_flush_lex(); 578212ff84fSEdward Tomasz Napierala kdb_reenter_silent(); 5795b81b6b3SRodney W. Grimes } 5805b81b6b3SRodney W. Grimes 581299cceefSMarcel Moolenaar static void 582cd508278SPedro F. Giffuni db_dump(db_expr_t dummy, bool dummy2, db_expr_t dummy3, char *dummy4) 583299cceefSMarcel Moolenaar { 584299cceefSMarcel Moolenaar int error; 585299cceefSMarcel Moolenaar 58621d748a9SAlfred Perlstein if (textdump_pending) { 58721d748a9SAlfred Perlstein db_printf("textdump_pending set.\n" 58821d748a9SAlfred Perlstein "run \"textdump unset\" first or \"textdump dump\" for a textdump.\n"); 58921d748a9SAlfred Perlstein return; 59021d748a9SAlfred Perlstein } 5912b490bc7SPedro F. Giffuni error = doadump(false); 592299cceefSMarcel Moolenaar if (error) { 593299cceefSMarcel Moolenaar db_printf("Cannot dump: "); 594299cceefSMarcel Moolenaar switch (error) { 595299cceefSMarcel Moolenaar case EBUSY: 596299cceefSMarcel Moolenaar db_printf("debugger got invoked while dumping.\n"); 597299cceefSMarcel Moolenaar break; 598299cceefSMarcel Moolenaar case ENXIO: 599299cceefSMarcel Moolenaar db_printf("no dump device specified.\n"); 600299cceefSMarcel Moolenaar break; 601299cceefSMarcel Moolenaar default: 602299cceefSMarcel Moolenaar db_printf("unknown error (error=%d).\n", error); 603299cceefSMarcel Moolenaar break; 604299cceefSMarcel Moolenaar } 605299cceefSMarcel Moolenaar } 606299cceefSMarcel Moolenaar } 6075b81b6b3SRodney W. Grimes 6085b81b6b3SRodney W. Grimes /* 6095b81b6b3SRodney W. Grimes * Call random function: 6105b81b6b3SRodney W. Grimes * !expr(arg,arg,arg) 6115b81b6b3SRodney W. Grimes */ 612a2aeb24eSMarcel Moolenaar 613a2aeb24eSMarcel Moolenaar /* The generic implementation supports a maximum of 10 arguments. */ 614a2aeb24eSMarcel Moolenaar typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t, 615a2aeb24eSMarcel Moolenaar db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t); 616a2aeb24eSMarcel Moolenaar 617a2aeb24eSMarcel Moolenaar static __inline int 618a2aeb24eSMarcel Moolenaar db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[]) 619a2aeb24eSMarcel Moolenaar { 620a2aeb24eSMarcel Moolenaar __db_f *f = (__db_f *)addr; 621a2aeb24eSMarcel Moolenaar 622a2aeb24eSMarcel Moolenaar if (nargs > 10) { 623a2aeb24eSMarcel Moolenaar db_printf("Too many arguments (max 10)\n"); 624a2aeb24eSMarcel Moolenaar return (0); 625a2aeb24eSMarcel Moolenaar } 626a2aeb24eSMarcel Moolenaar *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5], 627a2aeb24eSMarcel Moolenaar args[6], args[7], args[8], args[9]); 628a2aeb24eSMarcel Moolenaar return (1); 629a2aeb24eSMarcel Moolenaar } 630a2aeb24eSMarcel Moolenaar 631f73a856dSPoul-Henning Kamp static void 632cd508278SPedro F. Giffuni db_fncall(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 6335b81b6b3SRodney W. Grimes { 6345b81b6b3SRodney W. Grimes db_expr_t fn_addr; 635a2aeb24eSMarcel Moolenaar db_expr_t args[DB_MAXARGS]; 6365b81b6b3SRodney W. Grimes int nargs = 0; 6375b81b6b3SRodney W. Grimes db_expr_t retval; 6385b81b6b3SRodney W. Grimes int t; 6395b81b6b3SRodney W. Grimes 6405b81b6b3SRodney W. Grimes if (!db_expression(&fn_addr)) { 6415b81b6b3SRodney W. Grimes db_printf("Bad function\n"); 6425b81b6b3SRodney W. Grimes db_flush_lex(); 6435b81b6b3SRodney W. Grimes return; 6445b81b6b3SRodney W. Grimes } 6455b81b6b3SRodney W. Grimes 6465b81b6b3SRodney W. Grimes t = db_read_token(); 6475b81b6b3SRodney W. Grimes if (t == tLPAREN) { 6485b81b6b3SRodney W. Grimes if (db_expression(&args[0])) { 6495b81b6b3SRodney W. Grimes nargs++; 6505b81b6b3SRodney W. Grimes while ((t = db_read_token()) == tCOMMA) { 651a2aeb24eSMarcel Moolenaar if (nargs == DB_MAXARGS) { 652a2aeb24eSMarcel Moolenaar db_printf("Too many arguments (max %d)\n", DB_MAXARGS); 6535b81b6b3SRodney W. Grimes db_flush_lex(); 6545b81b6b3SRodney W. Grimes return; 6555b81b6b3SRodney W. Grimes } 6565b81b6b3SRodney W. Grimes if (!db_expression(&args[nargs])) { 6575b81b6b3SRodney W. Grimes db_printf("Argument missing\n"); 6585b81b6b3SRodney W. Grimes db_flush_lex(); 6595b81b6b3SRodney W. Grimes return; 6605b81b6b3SRodney W. Grimes } 6615b81b6b3SRodney W. Grimes nargs++; 6625b81b6b3SRodney W. Grimes } 6635b81b6b3SRodney W. Grimes db_unread_token(t); 6645b81b6b3SRodney W. Grimes } 6655b81b6b3SRodney W. Grimes if (db_read_token() != tRPAREN) { 6669990da25SEdward Tomasz Napierala db_printf("Mismatched parens\n"); 6675b81b6b3SRodney W. Grimes db_flush_lex(); 6685b81b6b3SRodney W. Grimes return; 6695b81b6b3SRodney W. Grimes } 6705b81b6b3SRodney W. Grimes } 6715b81b6b3SRodney W. Grimes db_skip_to_eol(); 67215cc91d3SJohn Baldwin db_disable_pager(); 6735b81b6b3SRodney W. Grimes 674a2aeb24eSMarcel Moolenaar if (DB_CALL(fn_addr, &retval, nargs, args)) 675a2aeb24eSMarcel Moolenaar db_printf("= %#lr\n", (long)retval); 6765b81b6b3SRodney W. Grimes } 677ad146781SPaul Traina 6785370c3b6SPeter Wemm static void 679cd508278SPedro F. Giffuni db_halt(db_expr_t dummy, bool dummy2, db_expr_t dummy3, char *dummy4) 680e4b732cfSBruce Evans { 681e4b732cfSBruce Evans 682e4b732cfSBruce Evans cpu_halt(); 683e4b732cfSBruce Evans } 684e4b732cfSBruce Evans 685e4b732cfSBruce Evans static void 686cd508278SPedro F. Giffuni db_kill(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 68719d2c78fSDima Dorfman { 68819d2c78fSDima Dorfman db_expr_t old_radix, pid, sig; 68919d2c78fSDima Dorfman struct proc *p; 69019d2c78fSDima Dorfman 69119d2c78fSDima Dorfman #define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0) 69219d2c78fSDima Dorfman 69319d2c78fSDima Dorfman /* 69419d2c78fSDima Dorfman * PIDs and signal numbers are typically represented in base 69519d2c78fSDima Dorfman * 10, so make that the default here. It can, of course, be 69619d2c78fSDima Dorfman * overridden by specifying a prefix. 69719d2c78fSDima Dorfman */ 69819d2c78fSDima Dorfman old_radix = db_radix; 69919d2c78fSDima Dorfman db_radix = 10; 70019d2c78fSDima Dorfman /* Retrieve arguments. */ 70119d2c78fSDima Dorfman if (!db_expression(&sig)) 70219d2c78fSDima Dorfman DB_ERROR(("Missing signal number\n")); 70319d2c78fSDima Dorfman if (!db_expression(&pid)) 70419d2c78fSDima Dorfman DB_ERROR(("Missing process ID\n")); 70519d2c78fSDima Dorfman db_skip_to_eol(); 706a29af74bSKonstantin Belousov if (!_SIG_VALID(sig)) 70719d2c78fSDima Dorfman DB_ERROR(("Signal number out of range\n")); 70819d2c78fSDima Dorfman 70919d2c78fSDima Dorfman /* 71019d2c78fSDima Dorfman * Find the process in question. allproc_lock is not needed 71119d2c78fSDima Dorfman * since we're in DDB. 71219d2c78fSDima Dorfman */ 71319d2c78fSDima Dorfman /* sx_slock(&allproc_lock); */ 714f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) 71519d2c78fSDima Dorfman if (p->p_pid == pid) 71619d2c78fSDima Dorfman break; 71719d2c78fSDima Dorfman /* sx_sunlock(&allproc_lock); */ 71819d2c78fSDima Dorfman if (p == NULL) 719e30e3e9eSMatt Jacob DB_ERROR(("Can't find process with pid %ld\n", (long) pid)); 72019d2c78fSDima Dorfman 72119d2c78fSDima Dorfman /* If it's already locked, bail; otherwise, do the deed. */ 72219d2c78fSDima Dorfman if (PROC_TRYLOCK(p) == 0) 723e30e3e9eSMatt Jacob DB_ERROR(("Can't lock process with pid %ld\n", (long) pid)); 72419d2c78fSDima Dorfman else { 725a3de221dSKonstantin Belousov pksignal(p, sig, NULL); 72619d2c78fSDima Dorfman PROC_UNLOCK(p); 72719d2c78fSDima Dorfman } 72819d2c78fSDima Dorfman 72919d2c78fSDima Dorfman out: 73019d2c78fSDima Dorfman db_radix = old_radix; 73119d2c78fSDima Dorfman #undef DB_ERROR 73219d2c78fSDima Dorfman } 73319d2c78fSDima Dorfman 7340f59fbc3SBjoern A. Zeeb /* 7350f59fbc3SBjoern A. Zeeb * Reboot. In case there is an additional argument, take it as delay in 7360f59fbc3SBjoern A. Zeeb * seconds. Default to 15s if we cannot parse it and make sure we will 7370f59fbc3SBjoern A. Zeeb * never wait longer than 1 week. Some code is similar to 7380f59fbc3SBjoern A. Zeeb * kern_shutdown.c:shutdown_panic(). 7390f59fbc3SBjoern A. Zeeb */ 7400f59fbc3SBjoern A. Zeeb #ifndef DB_RESET_MAXDELAY 7410f59fbc3SBjoern A. Zeeb #define DB_RESET_MAXDELAY (3600 * 24 * 7) 7420f59fbc3SBjoern A. Zeeb #endif 7430f59fbc3SBjoern A. Zeeb 74419d2c78fSDima Dorfman static void 745cd508278SPedro F. Giffuni db_reset(db_expr_t addr, bool have_addr, db_expr_t count __unused, 74656448506SMitchell Horne char *modif) 7475370c3b6SPeter Wemm { 7480f59fbc3SBjoern A. Zeeb int delay, loop; 7490f59fbc3SBjoern A. Zeeb 7500f59fbc3SBjoern A. Zeeb if (have_addr) { 7510f59fbc3SBjoern A. Zeeb delay = (int)db_hex2dec(addr); 7520f59fbc3SBjoern A. Zeeb 7530f59fbc3SBjoern A. Zeeb /* If we parse to fail, use 15s. */ 7540f59fbc3SBjoern A. Zeeb if (delay == -1) 7550f59fbc3SBjoern A. Zeeb delay = 15; 7560f59fbc3SBjoern A. Zeeb 7570f59fbc3SBjoern A. Zeeb /* Cap at one week. */ 7580f59fbc3SBjoern A. Zeeb if ((uintmax_t)delay > (uintmax_t)DB_RESET_MAXDELAY) 7590f59fbc3SBjoern A. Zeeb delay = DB_RESET_MAXDELAY; 7600f59fbc3SBjoern A. Zeeb 7610f59fbc3SBjoern A. Zeeb db_printf("Automatic reboot in %d seconds - " 7620f59fbc3SBjoern A. Zeeb "press a key on the console to abort\n", delay); 7630f59fbc3SBjoern A. Zeeb for (loop = delay * 10; loop > 0; --loop) { 7640f59fbc3SBjoern A. Zeeb DELAY(1000 * 100); /* 1/10th second */ 7650f59fbc3SBjoern A. Zeeb /* Did user type a key? */ 7660f59fbc3SBjoern A. Zeeb if (cncheckc() != -1) 7670f59fbc3SBjoern A. Zeeb return; 7680f59fbc3SBjoern A. Zeeb } 7690f59fbc3SBjoern A. Zeeb } 7705370c3b6SPeter Wemm 77156448506SMitchell Horne /* 77256448506SMitchell Horne * Conditionally try the standard reboot path, so any registered 77356448506SMitchell Horne * shutdown/reset handlers have a chance to run first. Some platforms 77456448506SMitchell Horne * may not support the machine-dependent mechanism used by cpu_reset() 77556448506SMitchell Horne * and rely on some other non-standard mechanism to perform the reset. 77656448506SMitchell Horne * For example, the BCM2835 watchdog driver or gpio-poweroff driver. 77756448506SMitchell Horne */ 77856448506SMitchell Horne if (modif[0] != 's') { 77956448506SMitchell Horne kern_reboot(RB_NOSYNC); 78056448506SMitchell Horne /* NOTREACHED */ 78156448506SMitchell Horne } 78256448506SMitchell Horne 7835370c3b6SPeter Wemm cpu_reset(); 7845370c3b6SPeter Wemm } 78574cc032bSPoul-Henning Kamp 78674cc032bSPoul-Henning Kamp static void 787cd508278SPedro F. Giffuni db_watchdog(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 78874cc032bSPoul-Henning Kamp { 7898b927d7bSAttilio Rao db_expr_t old_radix, tout; 7908b927d7bSAttilio Rao int err, i; 7918b927d7bSAttilio Rao 7928b927d7bSAttilio Rao old_radix = db_radix; 7938b927d7bSAttilio Rao db_radix = 10; 7948b927d7bSAttilio Rao err = db_expression(&tout); 7958b927d7bSAttilio Rao db_skip_to_eol(); 7968b927d7bSAttilio Rao db_radix = old_radix; 7978b927d7bSAttilio Rao 7988b927d7bSAttilio Rao /* If no argument is provided the watchdog will just be disabled. */ 7998b927d7bSAttilio Rao if (err == 0) { 8008b927d7bSAttilio Rao db_printf("No argument provided, disabling watchdog\n"); 8018b927d7bSAttilio Rao tout = 0; 8028b927d7bSAttilio Rao } else if ((tout & WD_INTERVAL) == WD_TO_NEVER) { 8038b927d7bSAttilio Rao db_error("Out of range watchdog interval\n"); 8048b927d7bSAttilio Rao return; 8058b927d7bSAttilio Rao } 8068b927d7bSAttilio Rao EVENTHANDLER_INVOKE(watchdog_list, tout, &i); 80774cc032bSPoul-Henning Kamp } 808f3be7cb3SMarcel Moolenaar 809f3be7cb3SMarcel Moolenaar static void 810cd508278SPedro F. Giffuni db_gdb(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 811f3be7cb3SMarcel Moolenaar { 812f3be7cb3SMarcel Moolenaar 8133a5d3671SMatthew D Fleming if (kdb_dbbe_select("gdb") != 0) { 814f3be7cb3SMarcel Moolenaar db_printf("The remote GDB backend could not be selected.\n"); 8153a5d3671SMatthew D Fleming return; 8163a5d3671SMatthew D Fleming } 8173a5d3671SMatthew D Fleming /* 8183a5d3671SMatthew D Fleming * Mark that we are done in the debugger. kdb_trap() 8193a5d3671SMatthew D Fleming * should re-enter with the new backend. 8203a5d3671SMatthew D Fleming */ 8213a5d3671SMatthew D Fleming db_cmd_loop_done = 1; 8223a5d3671SMatthew D Fleming db_printf("(ctrl-c will return control to ddb)\n"); 823f3be7cb3SMarcel Moolenaar } 824fd32d93bSMarcel Moolenaar 825fd32d93bSMarcel Moolenaar static void 826cd508278SPedro F. Giffuni db_stack_trace(db_expr_t tid, bool hastid, db_expr_t count, char *modif) 827fd32d93bSMarcel Moolenaar { 828fd32d93bSMarcel Moolenaar struct thread *td; 829fd32d93bSMarcel Moolenaar db_expr_t radix; 830a13aca1aSRobert Watson pid_t pid; 831fd32d93bSMarcel Moolenaar int t; 832fd32d93bSMarcel Moolenaar 833fd32d93bSMarcel Moolenaar /* 834fd32d93bSMarcel Moolenaar * We parse our own arguments. We don't like the default radix. 835fd32d93bSMarcel Moolenaar */ 836fd32d93bSMarcel Moolenaar radix = db_radix; 837fd32d93bSMarcel Moolenaar db_radix = 10; 838fd32d93bSMarcel Moolenaar hastid = db_expression(&tid); 839fd32d93bSMarcel Moolenaar t = db_read_token(); 840fd32d93bSMarcel Moolenaar if (t == tCOMMA) { 841fd32d93bSMarcel Moolenaar if (!db_expression(&count)) { 842fd32d93bSMarcel Moolenaar db_printf("Count missing\n"); 843fd32d93bSMarcel Moolenaar db_flush_lex(); 8443531bbb5SWarner Losh db_radix = radix; 845fd32d93bSMarcel Moolenaar return; 846fd32d93bSMarcel Moolenaar } 847fd32d93bSMarcel Moolenaar } else { 848fd32d93bSMarcel Moolenaar db_unread_token(t); 849fd32d93bSMarcel Moolenaar count = -1; 850fd32d93bSMarcel Moolenaar } 851fd32d93bSMarcel Moolenaar db_skip_to_eol(); 852fd32d93bSMarcel Moolenaar db_radix = radix; 853fd32d93bSMarcel Moolenaar 854fd32d93bSMarcel Moolenaar if (hastid) { 855fd32d93bSMarcel Moolenaar td = kdb_thr_lookup((lwpid_t)tid); 856fd32d93bSMarcel Moolenaar if (td == NULL) 857fd32d93bSMarcel Moolenaar td = kdb_thr_from_pid((pid_t)tid); 858fd32d93bSMarcel Moolenaar if (td == NULL) { 859fd32d93bSMarcel Moolenaar db_printf("Thread %d not found\n", (int)tid); 860fd32d93bSMarcel Moolenaar return; 861fd32d93bSMarcel Moolenaar } 862fd32d93bSMarcel Moolenaar } else 863fd32d93bSMarcel Moolenaar td = kdb_thread; 864a13aca1aSRobert Watson if (td->td_proc != NULL) 865a13aca1aSRobert Watson pid = td->td_proc->p_pid; 866a13aca1aSRobert Watson else 867a13aca1aSRobert Watson pid = -1; 868a13aca1aSRobert Watson db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td); 869825d2341SJohn Baldwin if (td->td_proc != NULL && (td->td_proc->p_flag & P_INMEM) == 0) 870825d2341SJohn Baldwin db_printf("--- swapped out\n"); 871825d2341SJohn Baldwin else 872fd32d93bSMarcel Moolenaar db_trace_thread(td, count); 873fd32d93bSMarcel Moolenaar } 874a7ad956bSRobert Watson 875a7ad956bSRobert Watson static void 8767e89a322SConrad Meyer _db_stack_trace_all(bool active_only) 877a7ad956bSRobert Watson { 878a7ad956bSRobert Watson struct thread *td; 8799641e389SKonstantin Belousov jmp_buf jb; 8809641e389SKonstantin Belousov void *prev_jb; 881a7ad956bSRobert Watson 8823e06c7daSJohn Baldwin for (td = kdb_thr_first(); td != NULL; td = kdb_thr_next(td)) { 8839641e389SKonstantin Belousov prev_jb = kdb_jmpbuf(jb); 8849641e389SKonstantin Belousov if (setjmp(jb) == 0) { 885fa2528acSAlex Richardson if (TD_IS_RUNNING(td)) 8867e89a322SConrad Meyer db_printf("\nTracing command %s pid %d" 8877e89a322SConrad Meyer " tid %ld td %p (CPU %d)\n", 8883e06c7daSJohn Baldwin td->td_proc->p_comm, td->td_proc->p_pid, 8893e06c7daSJohn Baldwin (long)td->td_tid, td, td->td_oncpu); 8907e89a322SConrad Meyer else if (active_only) 8917e89a322SConrad Meyer continue; 8927e89a322SConrad Meyer else 8937e89a322SConrad Meyer db_printf("\nTracing command %s pid %d" 8943e06c7daSJohn Baldwin " tid %ld td %p\n", td->td_proc->p_comm, 8953e06c7daSJohn Baldwin td->td_proc->p_pid, (long)td->td_tid, td); 8963e06c7daSJohn Baldwin if (td->td_proc->p_flag & P_INMEM) 897a7ad956bSRobert Watson db_trace_thread(td, -1); 8983e06c7daSJohn Baldwin else 8993e06c7daSJohn Baldwin db_printf("--- swapped out\n"); 9009641e389SKonstantin Belousov if (db_pager_quit) { 9019641e389SKonstantin Belousov kdb_jmpbuf(prev_jb); 902da927f93SOlivier Houchard return; 903a7ad956bSRobert Watson } 904a7ad956bSRobert Watson } 9059641e389SKonstantin Belousov kdb_jmpbuf(prev_jb); 9069641e389SKonstantin Belousov } 9079641e389SKonstantin Belousov } 9080f59fbc3SBjoern A. Zeeb 9097e89a322SConrad Meyer static void 9107e89a322SConrad Meyer db_stack_trace_active(db_expr_t dummy, bool dummy2, db_expr_t dummy3, 9117e89a322SConrad Meyer char *dummy4) 9127e89a322SConrad Meyer { 9137e89a322SConrad Meyer 9147e89a322SConrad Meyer _db_stack_trace_all(true); 9157e89a322SConrad Meyer } 9167e89a322SConrad Meyer 9177e89a322SConrad Meyer static void 9187e89a322SConrad Meyer db_stack_trace_all(db_expr_t dummy, bool dummy2, db_expr_t dummy3, 9197e89a322SConrad Meyer char *dummy4) 9207e89a322SConrad Meyer { 9217e89a322SConrad Meyer 9227e89a322SConrad Meyer _db_stack_trace_all(false); 9237e89a322SConrad Meyer } 9247e89a322SConrad Meyer 9250f59fbc3SBjoern A. Zeeb /* 9260f59fbc3SBjoern A. Zeeb * Take the parsed expression value from the command line that was parsed 9270f59fbc3SBjoern A. Zeeb * as a hexadecimal value and convert it as if the expression was parsed 9280f59fbc3SBjoern A. Zeeb * as a decimal value. Returns -1 if the expression was not a valid 9290f59fbc3SBjoern A. Zeeb * decimal value. 9300f59fbc3SBjoern A. Zeeb */ 9310f59fbc3SBjoern A. Zeeb db_expr_t 9320f59fbc3SBjoern A. Zeeb db_hex2dec(db_expr_t expr) 9330f59fbc3SBjoern A. Zeeb { 9340f59fbc3SBjoern A. Zeeb uintptr_t x, y; 9350f59fbc3SBjoern A. Zeeb db_expr_t val; 9360f59fbc3SBjoern A. Zeeb 9370f59fbc3SBjoern A. Zeeb y = 1; 9380f59fbc3SBjoern A. Zeeb val = 0; 9390f59fbc3SBjoern A. Zeeb x = expr; 9400f59fbc3SBjoern A. Zeeb while (x != 0) { 9410f59fbc3SBjoern A. Zeeb if (x % 16 > 9) 9420f59fbc3SBjoern A. Zeeb return (-1); 9430f59fbc3SBjoern A. Zeeb val += (x % 16) * (y); 9440f59fbc3SBjoern A. Zeeb x >>= 4; 9450f59fbc3SBjoern A. Zeeb y *= 10; 9460f59fbc3SBjoern A. Zeeb } 9470f59fbc3SBjoern A. Zeeb return (val); 9480f59fbc3SBjoern A. Zeeb } 949