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), 129076b64e8SAndrew Turner #ifdef HAS_HW_BREAKPOINT 130076b64e8SAndrew Turner DB_CMD("dhbreak", db_deletehbreak_cmd, 0), 131076b64e8SAndrew Turner DB_CMD("hbreak", db_hbreakpoint_cmd, 0), 132076b64e8SAndrew Turner #endif 133c036339dSMark Johnston DB_CMD("break", db_breakpoint_cmd, 0), 134c036339dSMark Johnston DB_CMD("b", db_breakpoint_cmd, 0), 135c036339dSMark Johnston DB_CMD("dwatch", db_deletewatch_cmd, 0), 136c036339dSMark Johnston DB_CMD("watch", db_watchpoint_cmd, CS_MORE), 137c036339dSMark Johnston DB_CMD("dhwatch", db_deletehwatch_cmd, 0), 138c036339dSMark Johnston DB_CMD("hwatch", db_hwatchpoint_cmd, 0), 139a305b20eSMitchell Horne DB_CMD("step", db_single_step_cmd, DB_CMD_MEMSAFE), 140a305b20eSMitchell Horne DB_CMD("s", db_single_step_cmd, DB_CMD_MEMSAFE), 141a305b20eSMitchell Horne DB_CMD("continue", db_continue_cmd, DB_CMD_MEMSAFE), 142a305b20eSMitchell Horne DB_CMD("c", db_continue_cmd, DB_CMD_MEMSAFE), 143a305b20eSMitchell Horne DB_CMD("until", db_trace_until_call_cmd, DB_CMD_MEMSAFE), 144a305b20eSMitchell Horne DB_CMD("next", db_trace_until_matching_cmd, DB_CMD_MEMSAFE), 1458a099482SMitchell Horne DB_CMD("match", db_trace_until_matching_cmd, 0), 146a305b20eSMitchell Horne DB_CMD("trace", db_stack_trace, CS_OWN|DB_CMD_MEMSAFE), 147a305b20eSMitchell Horne DB_CMD("t", db_stack_trace, CS_OWN|DB_CMD_MEMSAFE), 1487e89a322SConrad Meyer /* XXX alias for active trace */ 149a305b20eSMitchell Horne DB_CMD("acttrace", db_stack_trace_active, DB_CMD_MEMSAFE), 15039297ba4SSam Leffler /* XXX alias for all trace */ 151a305b20eSMitchell Horne DB_CMD("alltrace", db_stack_trace_all, DB_CMD_MEMSAFE), 152a305b20eSMitchell Horne DB_CMD("where", db_stack_trace, CS_OWN|DB_CMD_MEMSAFE), 153a305b20eSMitchell Horne DB_CMD("bt", db_stack_trace, CS_OWN|DB_CMD_MEMSAFE), 1548a099482SMitchell Horne DB_CMD("call", db_fncall, CS_OWN), 155a305b20eSMitchell Horne DB_CMD("ps", db_ps, DB_CMD_MEMSAFE), 1568a099482SMitchell Horne DB_CMD("gdb", db_gdb, 0), 157a305b20eSMitchell Horne DB_CMD("halt", db_halt, DB_CMD_MEMSAFE), 158a305b20eSMitchell Horne DB_CMD("reboot", db_reset, DB_CMD_MEMSAFE), 159a305b20eSMitchell Horne DB_CMD("reset", db_reset, DB_CMD_MEMSAFE), 160a305b20eSMitchell Horne DB_CMD("kill", db_kill, CS_OWN|DB_CMD_MEMSAFE), 161a305b20eSMitchell Horne DB_CMD("watchdog", db_watchdog, CS_OWN|DB_CMD_MEMSAFE), 1628a099482SMitchell Horne DB_CMD("thread", db_set_thread, 0), 163a305b20eSMitchell Horne DB_CMD("run", db_run_cmd, CS_OWN|DB_CMD_MEMSAFE), 164a305b20eSMitchell Horne DB_CMD("script", db_script_cmd, CS_OWN|DB_CMD_MEMSAFE), 165a305b20eSMitchell Horne DB_CMD("scripts", db_scripts_cmd, DB_CMD_MEMSAFE), 166a305b20eSMitchell Horne DB_CMD("unscript", db_unscript_cmd, CS_OWN|DB_CMD_MEMSAFE), 167a305b20eSMitchell Horne DB_CMD("capture", db_capture_cmd, CS_OWN|DB_CMD_MEMSAFE), 168a305b20eSMitchell Horne DB_CMD("textdump", db_textdump_cmd, CS_OWN|DB_CMD_MEMSAFE), 1698a099482SMitchell Horne DB_CMD("findstack", db_findstack_cmd, 0), 170c21bc6f3SBojan Novković DB_CMD("pprint", db_pprint_cmd, CS_OWN), 171cec9a4bfSDavid E. O'Brien }; 1724ef7db5aSMitchell Horne struct db_command_table db_cmd_table = LIST_HEAD_INITIALIZER(db_cmd_table); 173e1e31c0eSJohn Baldwin 1748a099482SMitchell Horne #undef DB_CMD 1758a099482SMitchell Horne #undef DB_TABLE 1768a099482SMitchell Horne 1774ef7db5aSMitchell Horne static struct db_command *db_last_command = NULL; 1786337f4efSBruce Evans 1795b81b6b3SRodney W. Grimes /* 1805b81b6b3SRodney W. Grimes * if 'ed' style: 'dot' is set at start of last item printed, 1815b81b6b3SRodney W. Grimes * and '+' points to next line. 1825b81b6b3SRodney W. Grimes * Otherwise: 'dot' points to next item, '..' points to last. 1835b81b6b3SRodney W. Grimes */ 184cd508278SPedro F. Giffuni static bool db_ed_style = true; 1855b81b6b3SRodney W. Grimes 1865b81b6b3SRodney W. Grimes /* 1875b81b6b3SRodney W. Grimes * Utility routine - discard tokens through end-of-line. 1885b81b6b3SRodney W. Grimes */ 1895b81b6b3SRodney W. Grimes void 190a41dd031SPedro F. Giffuni db_skip_to_eol(void) 1915b81b6b3SRodney W. Grimes { 1925b81b6b3SRodney W. Grimes int t; 1934f2ad624SMitchell Horne 1945b81b6b3SRodney W. Grimes do { 1955b81b6b3SRodney W. Grimes t = db_read_token(); 1965b81b6b3SRodney W. Grimes } while (t != tEOL); 1975b81b6b3SRodney W. Grimes } 1985b81b6b3SRodney W. Grimes 1995b81b6b3SRodney W. Grimes /* 2005b81b6b3SRodney W. Grimes * Results of command search. 2015b81b6b3SRodney W. Grimes */ 2025b81b6b3SRodney W. Grimes #define CMD_UNIQUE 0 2035b81b6b3SRodney W. Grimes #define CMD_FOUND 1 2045b81b6b3SRodney W. Grimes #define CMD_NONE 2 2055b81b6b3SRodney W. Grimes #define CMD_AMBIGUOUS 3 2065b81b6b3SRodney W. Grimes #define CMD_HELP 4 2075b81b6b3SRodney W. Grimes 2084ef7db5aSMitchell Horne static void db_cmd_match(char *name, struct db_command *cmd, 2094ef7db5aSMitchell Horne struct db_command **cmdp, int *resultp); 2104ef7db5aSMitchell Horne static void db_cmd_list(struct db_command_table *table); 2114ef7db5aSMitchell Horne static int db_cmd_search(char *name, struct db_command_table *table, 2124ef7db5aSMitchell Horne struct db_command **cmdp); 2134ef7db5aSMitchell Horne static void db_command(struct db_command **last_cmdp, 2144f2ad624SMitchell Horne struct db_command_table *cmd_table, bool dopager); 215058284fcSBruce Evans 2165b81b6b3SRodney W. Grimes /* 21739297ba4SSam Leffler * Initialize the command lists from the static tables. 21839297ba4SSam Leffler */ 21993d4804bSJohn Baldwin void 22093d4804bSJohn Baldwin db_command_init(void) 22139297ba4SSam Leffler { 22239297ba4SSam Leffler int i; 22339297ba4SSam Leffler 2244f2ad624SMitchell Horne for (i = 0; i < nitems(db_cmds); i++) 22539297ba4SSam Leffler db_command_register(&db_cmd_table, &db_cmds[i]); 2264f2ad624SMitchell Horne for (i = 0; i < nitems(db_show_cmds); i++) 22739297ba4SSam Leffler db_command_register(&db_show_table, &db_show_cmds[i]); 2284f2ad624SMitchell Horne for (i = 0; i < nitems(db_show_active_cmds); i++) 2297e89a322SConrad Meyer db_command_register(&db_show_active_table, 2307e89a322SConrad Meyer &db_show_active_cmds[i]); 2314f2ad624SMitchell Horne for (i = 0; i < nitems(db_show_all_cmds); i++) 23239297ba4SSam Leffler db_command_register(&db_show_all_table, &db_show_all_cmds[i]); 23339297ba4SSam Leffler } 23439297ba4SSam Leffler 23539297ba4SSam Leffler /* 23639297ba4SSam Leffler * Register a command. 23739297ba4SSam Leffler */ 23839297ba4SSam Leffler void 2394ef7db5aSMitchell Horne db_command_register(struct db_command_table *list, struct db_command *cmd) 24039297ba4SSam Leffler { 2414ef7db5aSMitchell Horne struct db_command *c, *last; 24239297ba4SSam Leffler 2432449b9e5SMitchell Horne #ifdef MAC 2442449b9e5SMitchell Horne if (mac_ddb_command_register(list, cmd)) { 2452449b9e5SMitchell Horne printf("%s: MAC policy refused registration of command %s\n", 2462449b9e5SMitchell Horne __func__, cmd->name); 2472449b9e5SMitchell Horne return; 2482449b9e5SMitchell Horne } 2492449b9e5SMitchell Horne #endif 25039297ba4SSam Leffler last = NULL; 25139297ba4SSam Leffler LIST_FOREACH(c, list, next) { 25239297ba4SSam Leffler int n = strcmp(cmd->name, c->name); 25339297ba4SSam Leffler 25439297ba4SSam Leffler /* Check that the command is not already present. */ 25539297ba4SSam Leffler if (n == 0) { 25639297ba4SSam Leffler printf("%s: Warning, the command \"%s\" already exists;" 25739297ba4SSam Leffler " ignoring request\n", __func__, cmd->name); 25839297ba4SSam Leffler return; 25939297ba4SSam Leffler } 26039297ba4SSam Leffler if (n < 0) { 26139297ba4SSam Leffler /* NB: keep list sorted lexicographically */ 26239297ba4SSam Leffler LIST_INSERT_BEFORE(c, cmd, next); 26339297ba4SSam Leffler return; 26439297ba4SSam Leffler } 26539297ba4SSam Leffler last = c; 26639297ba4SSam Leffler } 26739297ba4SSam Leffler if (last == NULL) 26839297ba4SSam Leffler LIST_INSERT_HEAD(list, cmd, next); 26939297ba4SSam Leffler else 27039297ba4SSam Leffler LIST_INSERT_AFTER(last, cmd, next); 27139297ba4SSam Leffler } 27239297ba4SSam Leffler 27339297ba4SSam Leffler /* 27439297ba4SSam Leffler * Remove a command previously registered with db_command_register. 27539297ba4SSam Leffler */ 27639297ba4SSam Leffler void 2774ef7db5aSMitchell Horne db_command_unregister(struct db_command_table *list, struct db_command *cmd) 27839297ba4SSam Leffler { 2794ef7db5aSMitchell Horne struct db_command *c; 28039297ba4SSam Leffler 28139297ba4SSam Leffler LIST_FOREACH(c, list, next) { 28239297ba4SSam Leffler if (cmd == c) { 28339297ba4SSam Leffler LIST_REMOVE(cmd, next); 28439297ba4SSam Leffler return; 28539297ba4SSam Leffler } 28639297ba4SSam Leffler } 28739297ba4SSam Leffler /* NB: intentionally quiet */ 28839297ba4SSam Leffler } 28939297ba4SSam Leffler 29039297ba4SSam Leffler /* 291e1e31c0eSJohn Baldwin * Helper function to match a single command. 2925b81b6b3SRodney W. Grimes */ 293e1e31c0eSJohn Baldwin static void 2944ef7db5aSMitchell Horne db_cmd_match(char *name, struct db_command *cmd, struct db_command **cmdp, 295a41dd031SPedro F. Giffuni int *resultp) 296e1e31c0eSJohn Baldwin { 297e1e31c0eSJohn Baldwin char *lp, *rp; 298e1e31c0eSJohn Baldwin int c; 2995b81b6b3SRodney W. Grimes 3005b81b6b3SRodney W. Grimes lp = name; 3015b81b6b3SRodney W. Grimes rp = cmd->name; 3025b81b6b3SRodney W. Grimes while ((c = *lp) == *rp) { 3035b81b6b3SRodney W. Grimes if (c == 0) { 3045b81b6b3SRodney W. Grimes /* complete match */ 3055b81b6b3SRodney W. Grimes *cmdp = cmd; 306e1e31c0eSJohn Baldwin *resultp = CMD_UNIQUE; 307e1e31c0eSJohn Baldwin return; 3085b81b6b3SRodney W. Grimes } 3095b81b6b3SRodney W. Grimes lp++; 3105b81b6b3SRodney W. Grimes rp++; 3115b81b6b3SRodney W. Grimes } 3125b81b6b3SRodney W. Grimes if (c == 0) { 3135b81b6b3SRodney W. Grimes /* end of name, not end of command - 3145b81b6b3SRodney W. Grimes partial match */ 315e1e31c0eSJohn Baldwin if (*resultp == CMD_FOUND) { 316e1e31c0eSJohn Baldwin *resultp = CMD_AMBIGUOUS; 3175b81b6b3SRodney W. Grimes /* but keep looking for a full match - 3185b81b6b3SRodney W. Grimes this lets us match single letters */ 319d85c9cefSRyan Libby } else if (*resultp == CMD_NONE) { 3205b81b6b3SRodney W. Grimes *cmdp = cmd; 321e1e31c0eSJohn Baldwin *resultp = CMD_FOUND; 3225b81b6b3SRodney W. Grimes } 3235b81b6b3SRodney W. Grimes } 3245b81b6b3SRodney W. Grimes } 3256337f4efSBruce Evans 326e1e31c0eSJohn Baldwin /* 327e1e31c0eSJohn Baldwin * Search for command prefix. 328e1e31c0eSJohn Baldwin */ 329e1e31c0eSJohn Baldwin static int 3304ef7db5aSMitchell Horne db_cmd_search(char *name, struct db_command_table *table, 3314ef7db5aSMitchell Horne struct db_command **cmdp) 332e1e31c0eSJohn Baldwin { 3334ef7db5aSMitchell Horne struct db_command *cmd; 334e1e31c0eSJohn Baldwin int result = CMD_NONE; 335e1e31c0eSJohn Baldwin 33639297ba4SSam Leffler LIST_FOREACH(cmd, table, next) { 337e1e31c0eSJohn Baldwin db_cmd_match(name,cmd,cmdp,&result); 338e1e31c0eSJohn Baldwin if (result == CMD_UNIQUE) 33939297ba4SSam Leffler break; 3406337f4efSBruce Evans } 34139297ba4SSam Leffler 3425b81b6b3SRodney W. Grimes if (result == CMD_NONE) { 3435b81b6b3SRodney W. Grimes /* check for 'help' */ 3445b81b6b3SRodney W. Grimes if (name[0] == 'h' && name[1] == 'e' 3455b81b6b3SRodney W. Grimes && name[2] == 'l' && name[3] == 'p') 3465b81b6b3SRodney W. Grimes result = CMD_HELP; 3475b81b6b3SRodney W. Grimes } 3485b81b6b3SRodney W. Grimes return (result); 3495b81b6b3SRodney W. Grimes } 3505b81b6b3SRodney W. Grimes 351f73a856dSPoul-Henning Kamp static void 3524ef7db5aSMitchell Horne db_cmd_list(struct db_command_table *table) 3535b81b6b3SRodney W. Grimes { 3544ef7db5aSMitchell Horne struct db_command *cmd; 355b5bd6c73SEdward Tomasz Napierala int have_subcommands; 3565b81b6b3SRodney W. Grimes 357b5bd6c73SEdward Tomasz Napierala have_subcommands = 0; 35839297ba4SSam Leffler LIST_FOREACH(cmd, table, next) { 359b5bd6c73SEdward Tomasz Napierala if (cmd->more != NULL) 360b5bd6c73SEdward Tomasz Napierala have_subcommands++; 361527be4f2SJohn-Mark Gurney db_printf("%-16s", cmd->name); 362527be4f2SJohn-Mark Gurney db_end_line(16); 3635b81b6b3SRodney W. Grimes } 364b5bd6c73SEdward Tomasz Napierala 365b5bd6c73SEdward Tomasz Napierala if (have_subcommands > 0) { 366b5bd6c73SEdward Tomasz Napierala db_printf("\nThe following have subcommands; append \"help\" " 367b5bd6c73SEdward Tomasz Napierala "to list (e.g. \"show help\"):\n"); 368b5bd6c73SEdward Tomasz Napierala LIST_FOREACH(cmd, table, next) { 369b5bd6c73SEdward Tomasz Napierala if (cmd->more == NULL) 370b5bd6c73SEdward Tomasz Napierala continue; 371b5bd6c73SEdward Tomasz Napierala db_printf("%-16s", cmd->name); 372b5bd6c73SEdward Tomasz Napierala db_end_line(16); 373b5bd6c73SEdward Tomasz Napierala } 374b5bd6c73SEdward Tomasz Napierala } 3755b81b6b3SRodney W. Grimes } 3765b81b6b3SRodney W. Grimes 377f73a856dSPoul-Henning Kamp static void 3784ef7db5aSMitchell Horne db_command(struct db_command **last_cmdp, struct db_command_table *cmd_table, 3794f2ad624SMitchell Horne bool dopager) 3805b81b6b3SRodney W. Grimes { 3815b81b6b3SRodney W. Grimes char modif[TOK_STRING_SIZE]; 3824f2ad624SMitchell Horne struct db_command *cmd = NULL; 3835b81b6b3SRodney W. Grimes db_expr_t addr, count; 3844f2ad624SMitchell Horne int t, result; 385cd508278SPedro F. Giffuni bool have_addr = false; 3865b81b6b3SRodney W. Grimes 3875b81b6b3SRodney W. Grimes t = db_read_token(); 3885b81b6b3SRodney W. Grimes if (t == tEOL) { 3895b81b6b3SRodney W. Grimes /* empty line repeats last command, at 'next' */ 3905b81b6b3SRodney W. Grimes cmd = *last_cmdp; 3915b81b6b3SRodney W. Grimes addr = (db_expr_t)db_next; 3922b490bc7SPedro F. Giffuni have_addr = false; 3935b81b6b3SRodney W. Grimes count = 1; 3945b81b6b3SRodney W. Grimes modif[0] = '\0'; 3954f2ad624SMitchell Horne } else if (t == tEXCL) { 3964f2ad624SMitchell Horne db_fncall((db_expr_t)0, false, (db_expr_t)0, NULL); 3975b81b6b3SRodney W. Grimes return; 3984f2ad624SMitchell Horne } else if (t != tIDENT) { 3999990da25SEdward Tomasz Napierala db_printf("Unrecognized input; use \"help\" " 4009990da25SEdward Tomasz Napierala "to list available commands\n"); 4015b81b6b3SRodney W. Grimes db_flush_lex(); 4025b81b6b3SRodney W. Grimes return; 4034f2ad624SMitchell Horne } else { 4045b81b6b3SRodney W. Grimes /* 4055b81b6b3SRodney W. Grimes * Search for command 4065b81b6b3SRodney W. Grimes */ 4074f2ad624SMitchell Horne while (cmd_table != NULL) { 4084f2ad624SMitchell Horne result = db_cmd_search(db_tok_string, cmd_table, &cmd); 4095b81b6b3SRodney W. Grimes switch (result) { 4105b81b6b3SRodney W. Grimes case CMD_NONE: 411b5bd6c73SEdward Tomasz Napierala db_printf("No such command; use \"help\" " 412b5bd6c73SEdward Tomasz Napierala "to list available commands\n"); 4135b81b6b3SRodney W. Grimes db_flush_lex(); 4145b81b6b3SRodney W. Grimes return; 4155b81b6b3SRodney W. Grimes case CMD_AMBIGUOUS: 4165b81b6b3SRodney W. Grimes db_printf("Ambiguous\n"); 4175b81b6b3SRodney W. Grimes db_flush_lex(); 4185b81b6b3SRodney W. Grimes return; 4195b81b6b3SRodney W. Grimes case CMD_HELP: 420b5bd6c73SEdward Tomasz Napierala if (cmd_table == &db_cmd_table) { 421b5bd6c73SEdward Tomasz Napierala db_printf("This is ddb(4), the kernel debugger; " 422c6404e66SGavin Atkinson "see https://man.FreeBSD.org/ddb/4 for help.\n"); 423b5bd6c73SEdward Tomasz Napierala db_printf("Use \"bt\" for backtrace, \"dump\" for " 424b5bd6c73SEdward Tomasz Napierala "kernel core dump, \"reset\" to reboot.\n"); 425b5bd6c73SEdward Tomasz Napierala db_printf("Available commands:\n"); 426b5bd6c73SEdward Tomasz Napierala } 427e1e31c0eSJohn Baldwin db_cmd_list(cmd_table); 4285b81b6b3SRodney W. Grimes db_flush_lex(); 4295b81b6b3SRodney W. Grimes return; 4304f2ad624SMitchell Horne case CMD_UNIQUE: 4314f2ad624SMitchell Horne case CMD_FOUND: 4325b81b6b3SRodney W. Grimes break; 4335b81b6b3SRodney W. Grimes } 434e1e31c0eSJohn Baldwin if ((cmd_table = cmd->more) != NULL) { 4355b81b6b3SRodney W. Grimes t = db_read_token(); 4365b81b6b3SRodney W. Grimes if (t != tIDENT) { 437b5bd6c73SEdward Tomasz Napierala db_printf("Subcommand required; " 438b5bd6c73SEdward Tomasz Napierala "available subcommands:\n"); 439e1e31c0eSJohn Baldwin db_cmd_list(cmd_table); 4405b81b6b3SRodney W. Grimes db_flush_lex(); 4415b81b6b3SRodney W. Grimes return; 4425b81b6b3SRodney W. Grimes } 4435b81b6b3SRodney W. Grimes } 4445b81b6b3SRodney W. Grimes } 4455b81b6b3SRodney W. Grimes 4465b81b6b3SRodney W. Grimes if ((cmd->flag & CS_OWN) == 0) { 4475b81b6b3SRodney W. Grimes /* 4485b81b6b3SRodney W. Grimes * Standard syntax: 4495b81b6b3SRodney W. Grimes * command [/modifier] [addr] [,count] 4505b81b6b3SRodney W. Grimes */ 4515b81b6b3SRodney W. Grimes t = db_read_token(); 4525b81b6b3SRodney W. Grimes if (t == tSLASH) { 4535b81b6b3SRodney W. Grimes t = db_read_token(); 4545b81b6b3SRodney W. Grimes if (t != tIDENT) { 4555b81b6b3SRodney W. Grimes db_printf("Bad modifier\n"); 4565b81b6b3SRodney W. Grimes db_flush_lex(); 4575b81b6b3SRodney W. Grimes return; 4585b81b6b3SRodney W. Grimes } 4595b81b6b3SRodney W. Grimes db_strcpy(modif, db_tok_string); 4604f2ad624SMitchell Horne } else { 4615b81b6b3SRodney W. Grimes db_unread_token(t); 4625b81b6b3SRodney W. Grimes modif[0] = '\0'; 4635b81b6b3SRodney W. Grimes } 4645b81b6b3SRodney W. Grimes 4655b81b6b3SRodney W. Grimes if (db_expression(&addr)) { 4665b81b6b3SRodney W. Grimes db_dot = (db_addr_t) addr; 4675b81b6b3SRodney W. Grimes db_last_addr = db_dot; 4682b490bc7SPedro F. Giffuni have_addr = true; 4694f2ad624SMitchell Horne } else { 4705b81b6b3SRodney W. Grimes addr = (db_expr_t) db_dot; 4712b490bc7SPedro F. Giffuni have_addr = false; 4725b81b6b3SRodney W. Grimes } 4734f2ad624SMitchell Horne 4745b81b6b3SRodney W. Grimes t = db_read_token(); 4755b81b6b3SRodney W. Grimes if (t == tCOMMA) { 4765b81b6b3SRodney W. Grimes if (!db_expression(&count)) { 4775b81b6b3SRodney W. Grimes db_printf("Count missing\n"); 4785b81b6b3SRodney W. Grimes db_flush_lex(); 4795b81b6b3SRodney W. Grimes return; 4805b81b6b3SRodney W. Grimes } 4814f2ad624SMitchell Horne } else { 4825b81b6b3SRodney W. Grimes db_unread_token(t); 4835b81b6b3SRodney W. Grimes count = -1; 4845b81b6b3SRodney W. Grimes } 4854f2ad624SMitchell Horne 4865b81b6b3SRodney W. Grimes if ((cmd->flag & CS_MORE) == 0) { 4875b81b6b3SRodney W. Grimes db_skip_to_eol(); 4885b81b6b3SRodney W. Grimes } 4895b81b6b3SRodney W. Grimes } 4905b81b6b3SRodney W. Grimes } 4914f2ad624SMitchell Horne 4925b81b6b3SRodney W. Grimes *last_cmdp = cmd; 4939f915a92SPedro F. Giffuni if (cmd != NULL) { 4942449b9e5SMitchell Horne #ifdef MAC 4952449b9e5SMitchell Horne if (mac_ddb_command_exec(cmd, addr, have_addr, count, modif)) { 4962449b9e5SMitchell Horne db_printf("MAC prevented execution of command %s\n", 4972449b9e5SMitchell Horne cmd->name); 4982449b9e5SMitchell Horne return; 4992449b9e5SMitchell Horne } 5002449b9e5SMitchell Horne #endif 5015b81b6b3SRodney W. Grimes /* 5025b81b6b3SRodney W. Grimes * Execute the command. 5035b81b6b3SRodney W. Grimes */ 504c9b0cc3bSRobert Watson if (dopager) 50519e9205aSJohn Baldwin db_enable_pager(); 506c9b0cc3bSRobert Watson else 507c9b0cc3bSRobert Watson db_disable_pager(); 5085b81b6b3SRodney W. Grimes (*cmd->fcn)(addr, have_addr, count, modif); 509c9b0cc3bSRobert Watson if (dopager) 51019e9205aSJohn Baldwin db_disable_pager(); 5115b81b6b3SRodney W. Grimes 5125b81b6b3SRodney W. Grimes if (cmd->flag & CS_SET_DOT) { 5135b81b6b3SRodney W. Grimes /* 5144f2ad624SMitchell Horne * If command changes dot, set dot to previous address 5154f2ad624SMitchell Horne * displayed (if 'ed' style). 5165b81b6b3SRodney W. Grimes */ 5174f2ad624SMitchell Horne db_dot = db_ed_style ? db_prev : db_next; 5184f2ad624SMitchell Horne } else { 5195b81b6b3SRodney W. Grimes /* 5204f2ad624SMitchell Horne * If command does not change dot, set 'next' location 5214f2ad624SMitchell Horne * to be the same. 5225b81b6b3SRodney W. Grimes */ 5235b81b6b3SRodney W. Grimes db_next = db_dot; 5245b81b6b3SRodney W. Grimes } 5255b81b6b3SRodney W. Grimes } 5265b81b6b3SRodney W. Grimes } 5275b81b6b3SRodney W. Grimes 5285b81b6b3SRodney W. Grimes /* 529b7aa38c1SBruce Evans * At least one non-optional command must be implemented using 530b7aa38c1SBruce Evans * DB_COMMAND() so that db_cmd_set gets created. Here is one. 531b7aa38c1SBruce Evans */ 532c84c5e00SMitchell Horne DB_COMMAND_FLAGS(panic, db_panic, DB_CMD_MEMSAFE) 5333eac4884SPoul-Henning Kamp { 53415cc91d3SJohn Baldwin db_disable_pager(); 535edf8a815SDavid Greenman panic("from debugger"); 5363eac4884SPoul-Henning Kamp } 5373eac4884SPoul-Henning Kamp 5383eac4884SPoul-Henning Kamp void 539a41dd031SPedro F. Giffuni db_command_loop(void) 5405b81b6b3SRodney W. Grimes { 5415b81b6b3SRodney W. Grimes /* 5425b81b6b3SRodney W. Grimes * Initialize 'prev' and 'next' to dot. 5435b81b6b3SRodney W. Grimes */ 5445b81b6b3SRodney W. Grimes db_prev = db_dot; 5455b81b6b3SRodney W. Grimes db_next = db_dot; 5465b81b6b3SRodney W. Grimes 5475b81b6b3SRodney W. Grimes db_cmd_loop_done = 0; 5485b81b6b3SRodney W. Grimes while (!db_cmd_loop_done) { 5495b81b6b3SRodney W. Grimes if (db_print_position() != 0) 5505b81b6b3SRodney W. Grimes db_printf("\n"); 5515b81b6b3SRodney W. Grimes 5525b81b6b3SRodney W. Grimes db_printf("db> "); 5535b81b6b3SRodney W. Grimes (void)db_read_line(); 5545b81b6b3SRodney W. Grimes 5554f2ad624SMitchell Horne db_command(&db_last_command, &db_cmd_table, /* dopager */ true); 5565b81b6b3SRodney W. Grimes } 5575b81b6b3SRodney W. Grimes } 5585b81b6b3SRodney W. Grimes 559c9b0cc3bSRobert Watson /* 560c9b0cc3bSRobert Watson * Execute a command on behalf of a script. The caller is responsible for 561c9b0cc3bSRobert Watson * making sure that the command string is < DB_MAXLINE or it will be 562c9b0cc3bSRobert Watson * truncated. 563c9b0cc3bSRobert Watson * 564c9b0cc3bSRobert Watson * XXXRW: Runs by injecting faked input into DDB input stream; it would be 565c9b0cc3bSRobert Watson * nicer to use an alternative approach that didn't mess with the previous 566c9b0cc3bSRobert Watson * command buffer. 567c9b0cc3bSRobert Watson */ 568c9b0cc3bSRobert Watson void 569c9b0cc3bSRobert Watson db_command_script(const char *command) 570c9b0cc3bSRobert Watson { 571c9b0cc3bSRobert Watson db_prev = db_next = db_dot; 572c9b0cc3bSRobert Watson db_inject_line(command); 5734f2ad624SMitchell Horne db_command(&db_last_command, &db_cmd_table, /* dopager */ false); 574c9b0cc3bSRobert Watson } 575c9b0cc3bSRobert Watson 5765b81b6b3SRodney W. Grimes void 577a41dd031SPedro F. Giffuni db_error(const char *s) 5785b81b6b3SRodney W. Grimes { 5795b81b6b3SRodney W. Grimes if (s) 58058d9a059SKris Kennaway db_printf("%s", s); 5815b81b6b3SRodney W. Grimes db_flush_lex(); 582212ff84fSEdward Tomasz Napierala kdb_reenter_silent(); 583*f0a7df4aSRyan Libby panic("%s: did not reenter debugger", __func__); 5845b81b6b3SRodney W. Grimes } 5855b81b6b3SRodney W. Grimes 586299cceefSMarcel Moolenaar static void 587cd508278SPedro F. Giffuni db_dump(db_expr_t dummy, bool dummy2, db_expr_t dummy3, char *dummy4) 588299cceefSMarcel Moolenaar { 589299cceefSMarcel Moolenaar int error; 590299cceefSMarcel Moolenaar 59121d748a9SAlfred Perlstein if (textdump_pending) { 59221d748a9SAlfred Perlstein db_printf("textdump_pending set.\n" 59321d748a9SAlfred Perlstein "run \"textdump unset\" first or \"textdump dump\" for a textdump.\n"); 59421d748a9SAlfred Perlstein return; 59521d748a9SAlfred Perlstein } 5962b490bc7SPedro F. Giffuni error = doadump(false); 597299cceefSMarcel Moolenaar if (error) { 598299cceefSMarcel Moolenaar db_printf("Cannot dump: "); 599299cceefSMarcel Moolenaar switch (error) { 600299cceefSMarcel Moolenaar case EBUSY: 601299cceefSMarcel Moolenaar db_printf("debugger got invoked while dumping.\n"); 602299cceefSMarcel Moolenaar break; 603299cceefSMarcel Moolenaar case ENXIO: 604299cceefSMarcel Moolenaar db_printf("no dump device specified.\n"); 605299cceefSMarcel Moolenaar break; 606299cceefSMarcel Moolenaar default: 607299cceefSMarcel Moolenaar db_printf("unknown error (error=%d).\n", error); 608299cceefSMarcel Moolenaar break; 609299cceefSMarcel Moolenaar } 610299cceefSMarcel Moolenaar } 611299cceefSMarcel Moolenaar } 6125b81b6b3SRodney W. Grimes 6135b81b6b3SRodney W. Grimes /* 6145b81b6b3SRodney W. Grimes * Call random function: 6155b81b6b3SRodney W. Grimes * !expr(arg,arg,arg) 6165b81b6b3SRodney W. Grimes */ 617a2aeb24eSMarcel Moolenaar 618a2aeb24eSMarcel Moolenaar /* The generic implementation supports a maximum of 10 arguments. */ 619a2aeb24eSMarcel Moolenaar typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t, 620a2aeb24eSMarcel Moolenaar db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t); 621a2aeb24eSMarcel Moolenaar 622a2aeb24eSMarcel Moolenaar static __inline int 623a2aeb24eSMarcel Moolenaar db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[]) 624a2aeb24eSMarcel Moolenaar { 625a2aeb24eSMarcel Moolenaar __db_f *f = (__db_f *)addr; 626a2aeb24eSMarcel Moolenaar 627a2aeb24eSMarcel Moolenaar if (nargs > 10) { 628a2aeb24eSMarcel Moolenaar db_printf("Too many arguments (max 10)\n"); 629a2aeb24eSMarcel Moolenaar return (0); 630a2aeb24eSMarcel Moolenaar } 631a2aeb24eSMarcel Moolenaar *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5], 632a2aeb24eSMarcel Moolenaar args[6], args[7], args[8], args[9]); 633a2aeb24eSMarcel Moolenaar return (1); 634a2aeb24eSMarcel Moolenaar } 635a2aeb24eSMarcel Moolenaar 636f73a856dSPoul-Henning Kamp static void 637cd508278SPedro F. Giffuni db_fncall(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 6385b81b6b3SRodney W. Grimes { 6395b81b6b3SRodney W. Grimes db_expr_t fn_addr; 640a2aeb24eSMarcel Moolenaar db_expr_t args[DB_MAXARGS]; 6415b81b6b3SRodney W. Grimes int nargs = 0; 6425b81b6b3SRodney W. Grimes db_expr_t retval; 6435b81b6b3SRodney W. Grimes int t; 6445b81b6b3SRodney W. Grimes 6455b81b6b3SRodney W. Grimes if (!db_expression(&fn_addr)) { 6465b81b6b3SRodney W. Grimes db_printf("Bad function\n"); 6475b81b6b3SRodney W. Grimes db_flush_lex(); 6485b81b6b3SRodney W. Grimes return; 6495b81b6b3SRodney W. Grimes } 6505b81b6b3SRodney W. Grimes 6515b81b6b3SRodney W. Grimes t = db_read_token(); 6525b81b6b3SRodney W. Grimes if (t == tLPAREN) { 6535b81b6b3SRodney W. Grimes if (db_expression(&args[0])) { 6545b81b6b3SRodney W. Grimes nargs++; 6555b81b6b3SRodney W. Grimes while ((t = db_read_token()) == tCOMMA) { 656a2aeb24eSMarcel Moolenaar if (nargs == DB_MAXARGS) { 657a2aeb24eSMarcel Moolenaar db_printf("Too many arguments (max %d)\n", DB_MAXARGS); 6585b81b6b3SRodney W. Grimes db_flush_lex(); 6595b81b6b3SRodney W. Grimes return; 6605b81b6b3SRodney W. Grimes } 6615b81b6b3SRodney W. Grimes if (!db_expression(&args[nargs])) { 6625b81b6b3SRodney W. Grimes db_printf("Argument missing\n"); 6635b81b6b3SRodney W. Grimes db_flush_lex(); 6645b81b6b3SRodney W. Grimes return; 6655b81b6b3SRodney W. Grimes } 6665b81b6b3SRodney W. Grimes nargs++; 6675b81b6b3SRodney W. Grimes } 6685b81b6b3SRodney W. Grimes db_unread_token(t); 6695b81b6b3SRodney W. Grimes } 6705b81b6b3SRodney W. Grimes if (db_read_token() != tRPAREN) { 6719990da25SEdward Tomasz Napierala db_printf("Mismatched parens\n"); 6725b81b6b3SRodney W. Grimes db_flush_lex(); 6735b81b6b3SRodney W. Grimes return; 6745b81b6b3SRodney W. Grimes } 6755b81b6b3SRodney W. Grimes } 6765b81b6b3SRodney W. Grimes db_skip_to_eol(); 67715cc91d3SJohn Baldwin db_disable_pager(); 6785b81b6b3SRodney W. Grimes 679a2aeb24eSMarcel Moolenaar if (DB_CALL(fn_addr, &retval, nargs, args)) 680a2aeb24eSMarcel Moolenaar db_printf("= %#lr\n", (long)retval); 6815b81b6b3SRodney W. Grimes } 682ad146781SPaul Traina 6835370c3b6SPeter Wemm static void 684cd508278SPedro F. Giffuni db_halt(db_expr_t dummy, bool dummy2, db_expr_t dummy3, char *dummy4) 685e4b732cfSBruce Evans { 686e4b732cfSBruce Evans 687e4b732cfSBruce Evans cpu_halt(); 688e4b732cfSBruce Evans } 689e4b732cfSBruce Evans 690e4b732cfSBruce Evans static void 691cd508278SPedro F. Giffuni db_kill(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 69219d2c78fSDima Dorfman { 69319d2c78fSDima Dorfman db_expr_t old_radix, pid, sig; 69419d2c78fSDima Dorfman struct proc *p; 69519d2c78fSDima Dorfman 69619d2c78fSDima Dorfman #define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0) 69719d2c78fSDima Dorfman 69819d2c78fSDima Dorfman /* 69919d2c78fSDima Dorfman * PIDs and signal numbers are typically represented in base 70019d2c78fSDima Dorfman * 10, so make that the default here. It can, of course, be 70119d2c78fSDima Dorfman * overridden by specifying a prefix. 70219d2c78fSDima Dorfman */ 70319d2c78fSDima Dorfman old_radix = db_radix; 70419d2c78fSDima Dorfman db_radix = 10; 70519d2c78fSDima Dorfman /* Retrieve arguments. */ 70619d2c78fSDima Dorfman if (!db_expression(&sig)) 70719d2c78fSDima Dorfman DB_ERROR(("Missing signal number\n")); 70819d2c78fSDima Dorfman if (!db_expression(&pid)) 70919d2c78fSDima Dorfman DB_ERROR(("Missing process ID\n")); 71019d2c78fSDima Dorfman db_skip_to_eol(); 711a29af74bSKonstantin Belousov if (!_SIG_VALID(sig)) 71219d2c78fSDima Dorfman DB_ERROR(("Signal number out of range\n")); 71319d2c78fSDima Dorfman 71419d2c78fSDima Dorfman /* 71519d2c78fSDima Dorfman * Find the process in question. allproc_lock is not needed 71619d2c78fSDima Dorfman * since we're in DDB. 71719d2c78fSDima Dorfman */ 71819d2c78fSDima Dorfman /* sx_slock(&allproc_lock); */ 719f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) 72019d2c78fSDima Dorfman if (p->p_pid == pid) 72119d2c78fSDima Dorfman break; 72219d2c78fSDima Dorfman /* sx_sunlock(&allproc_lock); */ 72319d2c78fSDima Dorfman if (p == NULL) 724e30e3e9eSMatt Jacob DB_ERROR(("Can't find process with pid %ld\n", (long) pid)); 72519d2c78fSDima Dorfman 72619d2c78fSDima Dorfman /* If it's already locked, bail; otherwise, do the deed. */ 72719d2c78fSDima Dorfman if (PROC_TRYLOCK(p) == 0) 728e30e3e9eSMatt Jacob DB_ERROR(("Can't lock process with pid %ld\n", (long) pid)); 72919d2c78fSDima Dorfman else { 730a3de221dSKonstantin Belousov pksignal(p, sig, NULL); 73119d2c78fSDima Dorfman PROC_UNLOCK(p); 73219d2c78fSDima Dorfman } 73319d2c78fSDima Dorfman 73419d2c78fSDima Dorfman out: 73519d2c78fSDima Dorfman db_radix = old_radix; 73619d2c78fSDima Dorfman #undef DB_ERROR 73719d2c78fSDima Dorfman } 73819d2c78fSDima Dorfman 7390f59fbc3SBjoern A. Zeeb /* 7400f59fbc3SBjoern A. Zeeb * Reboot. In case there is an additional argument, take it as delay in 7410f59fbc3SBjoern A. Zeeb * seconds. Default to 15s if we cannot parse it and make sure we will 7420f59fbc3SBjoern A. Zeeb * never wait longer than 1 week. Some code is similar to 7430f59fbc3SBjoern A. Zeeb * kern_shutdown.c:shutdown_panic(). 7440f59fbc3SBjoern A. Zeeb */ 7450f59fbc3SBjoern A. Zeeb #ifndef DB_RESET_MAXDELAY 7460f59fbc3SBjoern A. Zeeb #define DB_RESET_MAXDELAY (3600 * 24 * 7) 7470f59fbc3SBjoern A. Zeeb #endif 7480f59fbc3SBjoern A. Zeeb 74919d2c78fSDima Dorfman static void 750cd508278SPedro F. Giffuni db_reset(db_expr_t addr, bool have_addr, db_expr_t count __unused, 75156448506SMitchell Horne char *modif) 7525370c3b6SPeter Wemm { 7530f59fbc3SBjoern A. Zeeb int delay, loop; 7540f59fbc3SBjoern A. Zeeb 7550f59fbc3SBjoern A. Zeeb if (have_addr) { 7560f59fbc3SBjoern A. Zeeb delay = (int)db_hex2dec(addr); 7570f59fbc3SBjoern A. Zeeb 7580f59fbc3SBjoern A. Zeeb /* If we parse to fail, use 15s. */ 7590f59fbc3SBjoern A. Zeeb if (delay == -1) 7600f59fbc3SBjoern A. Zeeb delay = 15; 7610f59fbc3SBjoern A. Zeeb 7620f59fbc3SBjoern A. Zeeb /* Cap at one week. */ 7630f59fbc3SBjoern A. Zeeb if ((uintmax_t)delay > (uintmax_t)DB_RESET_MAXDELAY) 7640f59fbc3SBjoern A. Zeeb delay = DB_RESET_MAXDELAY; 7650f59fbc3SBjoern A. Zeeb 7660f59fbc3SBjoern A. Zeeb db_printf("Automatic reboot in %d seconds - " 7670f59fbc3SBjoern A. Zeeb "press a key on the console to abort\n", delay); 7680f59fbc3SBjoern A. Zeeb for (loop = delay * 10; loop > 0; --loop) { 7690f59fbc3SBjoern A. Zeeb DELAY(1000 * 100); /* 1/10th second */ 7700f59fbc3SBjoern A. Zeeb /* Did user type a key? */ 7710f59fbc3SBjoern A. Zeeb if (cncheckc() != -1) 7720f59fbc3SBjoern A. Zeeb return; 7730f59fbc3SBjoern A. Zeeb } 7740f59fbc3SBjoern A. Zeeb } 7755370c3b6SPeter Wemm 77656448506SMitchell Horne /* 77756448506SMitchell Horne * Conditionally try the standard reboot path, so any registered 77856448506SMitchell Horne * shutdown/reset handlers have a chance to run first. Some platforms 77956448506SMitchell Horne * may not support the machine-dependent mechanism used by cpu_reset() 78056448506SMitchell Horne * and rely on some other non-standard mechanism to perform the reset. 78156448506SMitchell Horne * For example, the BCM2835 watchdog driver or gpio-poweroff driver. 78256448506SMitchell Horne */ 78356448506SMitchell Horne if (modif[0] != 's') { 78456448506SMitchell Horne kern_reboot(RB_NOSYNC); 78556448506SMitchell Horne /* NOTREACHED */ 78656448506SMitchell Horne } 78756448506SMitchell Horne 7885370c3b6SPeter Wemm cpu_reset(); 7895370c3b6SPeter Wemm } 79074cc032bSPoul-Henning Kamp 79174cc032bSPoul-Henning Kamp static void 792cd508278SPedro F. Giffuni db_watchdog(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 79374cc032bSPoul-Henning Kamp { 7948b927d7bSAttilio Rao db_expr_t old_radix, tout; 7958b927d7bSAttilio Rao int err, i; 7968b927d7bSAttilio Rao 7978b927d7bSAttilio Rao old_radix = db_radix; 7988b927d7bSAttilio Rao db_radix = 10; 7998b927d7bSAttilio Rao err = db_expression(&tout); 8008b927d7bSAttilio Rao db_skip_to_eol(); 8018b927d7bSAttilio Rao db_radix = old_radix; 8028b927d7bSAttilio Rao 8038b927d7bSAttilio Rao /* If no argument is provided the watchdog will just be disabled. */ 8048b927d7bSAttilio Rao if (err == 0) { 8058b927d7bSAttilio Rao db_printf("No argument provided, disabling watchdog\n"); 8068b927d7bSAttilio Rao tout = 0; 8078b927d7bSAttilio Rao } else if ((tout & WD_INTERVAL) == WD_TO_NEVER) { 8088b927d7bSAttilio Rao db_error("Out of range watchdog interval\n"); 8098b927d7bSAttilio Rao return; 8108b927d7bSAttilio Rao } 8118b927d7bSAttilio Rao EVENTHANDLER_INVOKE(watchdog_list, tout, &i); 81274cc032bSPoul-Henning Kamp } 813f3be7cb3SMarcel Moolenaar 814f3be7cb3SMarcel Moolenaar static void 815cd508278SPedro F. Giffuni db_gdb(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 816f3be7cb3SMarcel Moolenaar { 817f3be7cb3SMarcel Moolenaar 8183a5d3671SMatthew D Fleming if (kdb_dbbe_select("gdb") != 0) { 819f3be7cb3SMarcel Moolenaar db_printf("The remote GDB backend could not be selected.\n"); 8203a5d3671SMatthew D Fleming return; 8213a5d3671SMatthew D Fleming } 8223a5d3671SMatthew D Fleming /* 8233a5d3671SMatthew D Fleming * Mark that we are done in the debugger. kdb_trap() 8243a5d3671SMatthew D Fleming * should re-enter with the new backend. 8253a5d3671SMatthew D Fleming */ 8263a5d3671SMatthew D Fleming db_cmd_loop_done = 1; 8273a5d3671SMatthew D Fleming db_printf("(ctrl-c will return control to ddb)\n"); 828f3be7cb3SMarcel Moolenaar } 829fd32d93bSMarcel Moolenaar 830fd32d93bSMarcel Moolenaar static void 831cd508278SPedro F. Giffuni db_stack_trace(db_expr_t tid, bool hastid, db_expr_t count, char *modif) 832fd32d93bSMarcel Moolenaar { 833fd32d93bSMarcel Moolenaar struct thread *td; 834fd32d93bSMarcel Moolenaar db_expr_t radix; 835a13aca1aSRobert Watson pid_t pid; 836fd32d93bSMarcel Moolenaar int t; 837fd32d93bSMarcel Moolenaar 838fd32d93bSMarcel Moolenaar /* 839fd32d93bSMarcel Moolenaar * We parse our own arguments. We don't like the default radix. 840fd32d93bSMarcel Moolenaar */ 841fd32d93bSMarcel Moolenaar radix = db_radix; 842fd32d93bSMarcel Moolenaar db_radix = 10; 843fd32d93bSMarcel Moolenaar hastid = db_expression(&tid); 844fd32d93bSMarcel Moolenaar t = db_read_token(); 845fd32d93bSMarcel Moolenaar if (t == tCOMMA) { 846fd32d93bSMarcel Moolenaar if (!db_expression(&count)) { 847fd32d93bSMarcel Moolenaar db_printf("Count missing\n"); 848fd32d93bSMarcel Moolenaar db_flush_lex(); 8493531bbb5SWarner Losh db_radix = radix; 850fd32d93bSMarcel Moolenaar return; 851fd32d93bSMarcel Moolenaar } 852fd32d93bSMarcel Moolenaar } else { 853fd32d93bSMarcel Moolenaar db_unread_token(t); 854fd32d93bSMarcel Moolenaar count = -1; 855fd32d93bSMarcel Moolenaar } 856fd32d93bSMarcel Moolenaar db_skip_to_eol(); 857fd32d93bSMarcel Moolenaar db_radix = radix; 858fd32d93bSMarcel Moolenaar 859fd32d93bSMarcel Moolenaar if (hastid) { 860fd32d93bSMarcel Moolenaar td = kdb_thr_lookup((lwpid_t)tid); 861fd32d93bSMarcel Moolenaar if (td == NULL) 862fd32d93bSMarcel Moolenaar td = kdb_thr_from_pid((pid_t)tid); 863fd32d93bSMarcel Moolenaar if (td == NULL) { 864fd32d93bSMarcel Moolenaar db_printf("Thread %d not found\n", (int)tid); 865fd32d93bSMarcel Moolenaar return; 866fd32d93bSMarcel Moolenaar } 867fd32d93bSMarcel Moolenaar } else 868fd32d93bSMarcel Moolenaar td = kdb_thread; 869a13aca1aSRobert Watson if (td->td_proc != NULL) 870a13aca1aSRobert Watson pid = td->td_proc->p_pid; 871a13aca1aSRobert Watson else 872a13aca1aSRobert Watson pid = -1; 873a13aca1aSRobert Watson db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td); 874fd32d93bSMarcel Moolenaar db_trace_thread(td, count); 875fd32d93bSMarcel Moolenaar } 876a7ad956bSRobert Watson 877a7ad956bSRobert Watson static void 8787e89a322SConrad Meyer _db_stack_trace_all(bool active_only) 879a7ad956bSRobert Watson { 880a7ad956bSRobert Watson struct thread *td; 8819641e389SKonstantin Belousov jmp_buf jb; 8829641e389SKonstantin Belousov void *prev_jb; 883a7ad956bSRobert Watson 8843e06c7daSJohn Baldwin for (td = kdb_thr_first(); td != NULL; td = kdb_thr_next(td)) { 8859641e389SKonstantin Belousov prev_jb = kdb_jmpbuf(jb); 8869641e389SKonstantin Belousov if (setjmp(jb) == 0) { 887fa2528acSAlex Richardson if (TD_IS_RUNNING(td)) 8887e89a322SConrad Meyer db_printf("\nTracing command %s pid %d" 8897e89a322SConrad Meyer " tid %ld td %p (CPU %d)\n", 8903e06c7daSJohn Baldwin td->td_proc->p_comm, td->td_proc->p_pid, 8913e06c7daSJohn Baldwin (long)td->td_tid, td, td->td_oncpu); 8927e89a322SConrad Meyer else if (active_only) 8937e89a322SConrad Meyer continue; 8947e89a322SConrad Meyer else 8957e89a322SConrad Meyer db_printf("\nTracing command %s pid %d" 8963e06c7daSJohn Baldwin " tid %ld td %p\n", td->td_proc->p_comm, 8973e06c7daSJohn Baldwin td->td_proc->p_pid, (long)td->td_tid, td); 898a7ad956bSRobert Watson db_trace_thread(td, -1); 8999641e389SKonstantin Belousov if (db_pager_quit) { 9009641e389SKonstantin Belousov kdb_jmpbuf(prev_jb); 901da927f93SOlivier Houchard return; 902a7ad956bSRobert Watson } 903a7ad956bSRobert Watson } 9049641e389SKonstantin Belousov kdb_jmpbuf(prev_jb); 9059641e389SKonstantin Belousov } 9069641e389SKonstantin Belousov } 9070f59fbc3SBjoern A. Zeeb 9087e89a322SConrad Meyer static void 9097e89a322SConrad Meyer db_stack_trace_active(db_expr_t dummy, bool dummy2, db_expr_t dummy3, 9107e89a322SConrad Meyer char *dummy4) 9117e89a322SConrad Meyer { 9127e89a322SConrad Meyer 9137e89a322SConrad Meyer _db_stack_trace_all(true); 9147e89a322SConrad Meyer } 9157e89a322SConrad Meyer 9167e89a322SConrad Meyer static void 9177e89a322SConrad Meyer db_stack_trace_all(db_expr_t dummy, bool dummy2, db_expr_t dummy3, 9187e89a322SConrad Meyer char *dummy4) 9197e89a322SConrad Meyer { 9207e89a322SConrad Meyer 9217e89a322SConrad Meyer _db_stack_trace_all(false); 9227e89a322SConrad Meyer } 9237e89a322SConrad Meyer 9240f59fbc3SBjoern A. Zeeb /* 9250f59fbc3SBjoern A. Zeeb * Take the parsed expression value from the command line that was parsed 9260f59fbc3SBjoern A. Zeeb * as a hexadecimal value and convert it as if the expression was parsed 9270f59fbc3SBjoern A. Zeeb * as a decimal value. Returns -1 if the expression was not a valid 9280f59fbc3SBjoern A. Zeeb * decimal value. 9290f59fbc3SBjoern A. Zeeb */ 9300f59fbc3SBjoern A. Zeeb db_expr_t 9310f59fbc3SBjoern A. Zeeb db_hex2dec(db_expr_t expr) 9320f59fbc3SBjoern A. Zeeb { 9330f59fbc3SBjoern A. Zeeb uintptr_t x, y; 9340f59fbc3SBjoern A. Zeeb db_expr_t val; 9350f59fbc3SBjoern A. Zeeb 9360f59fbc3SBjoern A. Zeeb y = 1; 9370f59fbc3SBjoern A. Zeeb val = 0; 9380f59fbc3SBjoern A. Zeeb x = expr; 9390f59fbc3SBjoern A. Zeeb while (x != 0) { 9400f59fbc3SBjoern A. Zeeb if (x % 16 > 9) 9410f59fbc3SBjoern A. Zeeb return (-1); 9420f59fbc3SBjoern A. Zeeb val += (x % 16) * (y); 9430f59fbc3SBjoern A. Zeeb x >>= 4; 9440f59fbc3SBjoern A. Zeeb y *= 10; 9450f59fbc3SBjoern A. Zeeb } 9460f59fbc3SBjoern A. Zeeb return (val); 9470f59fbc3SBjoern A. Zeeb } 948