1dd3cb568SWarner Losh /*- 25b81b6b3SRodney W. Grimes * Mach Operating System 35b81b6b3SRodney W. Grimes * Copyright (c) 1991,1990 Carnegie Mellon University 45b81b6b3SRodney W. Grimes * All Rights Reserved. 55b81b6b3SRodney W. Grimes * 65b81b6b3SRodney W. Grimes * Permission to use, copy, modify and distribute this software and its 75b81b6b3SRodney W. Grimes * documentation is hereby granted, provided that both the copyright 85b81b6b3SRodney W. Grimes * notice and this permission notice appear in all copies of the 95b81b6b3SRodney W. Grimes * software, derivative works or modified versions, and any portions 105b81b6b3SRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 115b81b6b3SRodney W. Grimes * 125b81b6b3SRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 135b81b6b3SRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 145b81b6b3SRodney W. Grimes * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 155b81b6b3SRodney W. Grimes * 165b81b6b3SRodney W. Grimes * Carnegie Mellon requests users of this software to return to 175b81b6b3SRodney W. Grimes * 185b81b6b3SRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 195b81b6b3SRodney W. Grimes * School of Computer Science 205b81b6b3SRodney W. Grimes * Carnegie Mellon University 215b81b6b3SRodney W. Grimes * Pittsburgh PA 15213-3890 225b81b6b3SRodney W. Grimes * 235b81b6b3SRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 245b81b6b3SRodney W. Grimes * rights to redistribute these changes. 255b81b6b3SRodney W. Grimes */ 265b81b6b3SRodney W. Grimes /* 275b81b6b3SRodney W. Grimes * Author: David B. Golub, Carnegie Mellon University 285b81b6b3SRodney W. Grimes * Date: 7/90 295b81b6b3SRodney W. Grimes */ 305b81b6b3SRodney W. Grimes /* 315b81b6b3SRodney W. Grimes * Command dispatcher. 325b81b6b3SRodney W. Grimes */ 33753960f7SDavid E. O'Brien 34753960f7SDavid E. O'Brien #include <sys/cdefs.h> 35753960f7SDavid E. O'Brien __FBSDID("$FreeBSD$"); 36753960f7SDavid E. O'Brien 37f540b106SGarrett Wollman #include <sys/param.h> 380ec81012SJohn Polstra #include <sys/linker_set.h> 3919d2c78fSDima Dorfman #include <sys/lock.h> 4037224cd3SMarcel Moolenaar #include <sys/kdb.h> 4119d2c78fSDima Dorfman #include <sys/mutex.h> 4219d2c78fSDima Dorfman #include <sys/proc.h> 43ad146781SPaul Traina #include <sys/reboot.h> 4419d2c78fSDima Dorfman #include <sys/signalvar.h> 45f540b106SGarrett Wollman #include <sys/systm.h> 46ce9edcf5SPoul-Henning Kamp #include <sys/cons.h> 47*299cceefSMarcel Moolenaar #include <sys/conf.h> 4874cc032bSPoul-Henning Kamp #include <sys/watchdog.h> 4939297ba4SSam Leffler #include <sys/kernel.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 595b81b6b3SRodney W. Grimes /* 605b81b6b3SRodney W. Grimes * Exported global variables 615b81b6b3SRodney W. Grimes */ 625b81b6b3SRodney W. Grimes boolean_t db_cmd_loop_done; 635b81b6b3SRodney W. Grimes db_addr_t db_dot; 645b81b6b3SRodney W. Grimes db_addr_t db_last_addr; 655b81b6b3SRodney W. Grimes db_addr_t db_prev; 665b81b6b3SRodney W. Grimes db_addr_t db_next; 67f41325dbSPeter Wemm 68*299cceefSMarcel Moolenaar static db_cmdfcn_t db_dump; 69f73a856dSPoul-Henning Kamp static db_cmdfcn_t db_fncall; 70f3be7cb3SMarcel Moolenaar static db_cmdfcn_t db_gdb; 71e4b732cfSBruce Evans static db_cmdfcn_t db_halt; 7219d2c78fSDima Dorfman static db_cmdfcn_t db_kill; 735370c3b6SPeter Wemm static db_cmdfcn_t db_reset; 74fd32d93bSMarcel Moolenaar static db_cmdfcn_t db_stack_trace; 75a7ad956bSRobert Watson static db_cmdfcn_t db_stack_trace_all; 7674cc032bSPoul-Henning Kamp static db_cmdfcn_t db_watchdog; 77ad146781SPaul Traina 78cec9a4bfSDavid E. O'Brien /* 79cec9a4bfSDavid E. O'Brien * 'show' commands 80cec9a4bfSDavid E. O'Brien */ 81cec9a4bfSDavid E. O'Brien 82cec9a4bfSDavid E. O'Brien static struct command db_show_all_cmds[] = { 8339297ba4SSam Leffler { "trace", db_stack_trace_all, 0, 0 }, 84cec9a4bfSDavid E. O'Brien }; 8539297ba4SSam Leffler struct command_table db_show_all_table = 8639297ba4SSam Leffler LIST_HEAD_INITIALIZER(db_show_all_table); 87e1e31c0eSJohn Baldwin 88cec9a4bfSDavid E. O'Brien static struct command db_show_cmds[] = { 89e1e31c0eSJohn Baldwin { "all", 0, 0, &db_show_all_table }, 90cec9a4bfSDavid E. O'Brien { "registers", db_show_regs, 0, 0 }, 91cec9a4bfSDavid E. O'Brien { "breaks", db_listbreak_cmd, 0, 0 }, 92cec9a4bfSDavid E. O'Brien { "threads", db_show_threads, 0, 0 }, 93cec9a4bfSDavid E. O'Brien }; 9439297ba4SSam Leffler struct command_table db_show_table = LIST_HEAD_INITIALIZER(db_show_table); 95cec9a4bfSDavid E. O'Brien 9639297ba4SSam Leffler static struct command db_cmds[] = { 97cec9a4bfSDavid E. O'Brien { "print", db_print_cmd, 0, 0 }, 98cec9a4bfSDavid E. O'Brien { "p", db_print_cmd, 0, 0 }, 99cec9a4bfSDavid E. O'Brien { "examine", db_examine_cmd, CS_SET_DOT, 0 }, 100cec9a4bfSDavid E. O'Brien { "x", db_examine_cmd, CS_SET_DOT, 0 }, 101cec9a4bfSDavid E. O'Brien { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 }, 102cec9a4bfSDavid E. O'Brien { "set", db_set_cmd, CS_OWN, 0 }, 103cec9a4bfSDavid E. O'Brien { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 }, 104cec9a4bfSDavid E. O'Brien { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 }, 105cec9a4bfSDavid E. O'Brien { "delete", db_delete_cmd, 0, 0 }, 106cec9a4bfSDavid E. O'Brien { "d", db_delete_cmd, 0, 0 }, 107*299cceefSMarcel Moolenaar { "dump", db_dump, 0, 0 }, 108cec9a4bfSDavid E. O'Brien { "break", db_breakpoint_cmd, 0, 0 }, 10975149ab8SBruce Evans { "b", db_breakpoint_cmd, 0, 0 }, 110cec9a4bfSDavid E. O'Brien { "dwatch", db_deletewatch_cmd, 0, 0 }, 111cec9a4bfSDavid E. O'Brien { "watch", db_watchpoint_cmd, CS_MORE,0 }, 112cec9a4bfSDavid E. O'Brien { "dhwatch", db_deletehwatch_cmd, 0, 0 }, 113cec9a4bfSDavid E. O'Brien { "hwatch", db_hwatchpoint_cmd, 0, 0 }, 114cec9a4bfSDavid E. O'Brien { "step", db_single_step_cmd, 0, 0 }, 115cec9a4bfSDavid E. O'Brien { "s", db_single_step_cmd, 0, 0 }, 116cec9a4bfSDavid E. O'Brien { "continue", db_continue_cmd, 0, 0 }, 117cec9a4bfSDavid E. O'Brien { "c", db_continue_cmd, 0, 0 }, 118cec9a4bfSDavid E. O'Brien { "until", db_trace_until_call_cmd,0, 0 }, 119cec9a4bfSDavid E. O'Brien { "next", db_trace_until_matching_cmd,0, 0 }, 120cec9a4bfSDavid E. O'Brien { "match", db_trace_until_matching_cmd,0, 0 }, 121cec9a4bfSDavid E. O'Brien { "trace", db_stack_trace, CS_OWN, 0 }, 12275149ab8SBruce Evans { "t", db_stack_trace, CS_OWN, 0 }, 12339297ba4SSam Leffler /* XXX alias for all trace */ 1243924da21SJohn Baldwin { "alltrace", db_stack_trace_all, 0, 0 }, 125cec9a4bfSDavid E. O'Brien { "where", db_stack_trace, CS_OWN, 0 }, 1263924da21SJohn Baldwin { "bt", db_stack_trace, CS_OWN, 0 }, 127cec9a4bfSDavid E. O'Brien { "call", db_fncall, CS_OWN, 0 }, 128e1e31c0eSJohn Baldwin { "show", 0, 0, &db_show_table }, 129cec9a4bfSDavid E. O'Brien { "ps", db_ps, 0, 0 }, 130cec9a4bfSDavid E. O'Brien { "gdb", db_gdb, 0, 0 }, 131e4b732cfSBruce Evans { "halt", db_halt, 0, 0 }, 132e4b732cfSBruce Evans { "reboot", db_reset, 0, 0 }, 133cec9a4bfSDavid E. O'Brien { "reset", db_reset, 0, 0 }, 134cec9a4bfSDavid E. O'Brien { "kill", db_kill, CS_OWN, 0 }, 1358b927d7bSAttilio Rao { "watchdog", db_watchdog, CS_OWN, 0 }, 136cec9a4bfSDavid E. O'Brien { "thread", db_set_thread, CS_OWN, 0 }, 137c9b0cc3bSRobert Watson { "run", db_run_cmd, CS_OWN, 0 }, 138c9b0cc3bSRobert Watson { "script", db_script_cmd, CS_OWN, 0 }, 139c9b0cc3bSRobert Watson { "scripts", db_scripts_cmd, 0, 0 }, 140c9b0cc3bSRobert Watson { "unscript", db_unscript_cmd, CS_OWN, 0 }, 141086fec57SRobert Watson { "capture", db_capture_cmd, CS_OWN, 0 }, 142618c7db3SRobert Watson { "textdump", db_textdump_cmd, CS_OWN, 0 }, 143cec9a4bfSDavid E. O'Brien }; 14439297ba4SSam Leffler struct command_table db_cmd_table = LIST_HEAD_INITIALIZER(db_cmd_table); 145e1e31c0eSJohn Baldwin 146cec9a4bfSDavid E. O'Brien static struct command *db_last_command = 0; 1476337f4efSBruce Evans 1485b81b6b3SRodney W. Grimes /* 1495b81b6b3SRodney W. Grimes * if 'ed' style: 'dot' is set at start of last item printed, 1505b81b6b3SRodney W. Grimes * and '+' points to next line. 1515b81b6b3SRodney W. Grimes * Otherwise: 'dot' points to next item, '..' points to last. 1525b81b6b3SRodney W. Grimes */ 153f73a856dSPoul-Henning Kamp static boolean_t db_ed_style = TRUE; 1545b81b6b3SRodney W. Grimes 1555b81b6b3SRodney W. Grimes /* 1565b81b6b3SRodney W. Grimes * Utility routine - discard tokens through end-of-line. 1575b81b6b3SRodney W. Grimes */ 1585b81b6b3SRodney W. Grimes void 1595b81b6b3SRodney W. Grimes db_skip_to_eol() 1605b81b6b3SRodney W. Grimes { 1615b81b6b3SRodney W. Grimes int t; 1625b81b6b3SRodney W. Grimes do { 1635b81b6b3SRodney W. Grimes t = db_read_token(); 1645b81b6b3SRodney W. Grimes } while (t != tEOL); 1655b81b6b3SRodney W. Grimes } 1665b81b6b3SRodney W. Grimes 1675b81b6b3SRodney W. Grimes /* 1685b81b6b3SRodney W. Grimes * Results of command search. 1695b81b6b3SRodney W. Grimes */ 1705b81b6b3SRodney W. Grimes #define CMD_UNIQUE 0 1715b81b6b3SRodney W. Grimes #define CMD_FOUND 1 1725b81b6b3SRodney W. Grimes #define CMD_NONE 2 1735b81b6b3SRodney W. Grimes #define CMD_AMBIGUOUS 3 1745b81b6b3SRodney W. Grimes #define CMD_HELP 4 1755b81b6b3SRodney W. Grimes 176e1e31c0eSJohn Baldwin static void db_cmd_match(char *name, struct command *cmd, 177e1e31c0eSJohn Baldwin struct command **cmdp, int *resultp); 178e1e31c0eSJohn Baldwin static void db_cmd_list(struct command_table *table); 179e1e31c0eSJohn Baldwin static int db_cmd_search(char *name, struct command_table *table, 180e1e31c0eSJohn Baldwin struct command **cmdp); 18114e10f99SAlfred Perlstein static void db_command(struct command **last_cmdp, 182c9b0cc3bSRobert Watson struct command_table *cmd_table, int dopager); 183058284fcSBruce Evans 1845b81b6b3SRodney W. Grimes /* 18539297ba4SSam Leffler * Initialize the command lists from the static tables. 18639297ba4SSam Leffler */ 18793d4804bSJohn Baldwin void 18893d4804bSJohn Baldwin db_command_init(void) 18939297ba4SSam Leffler { 19039297ba4SSam Leffler #define N(a) (sizeof(a) / sizeof(a[0])) 19139297ba4SSam Leffler int i; 19239297ba4SSam Leffler 19339297ba4SSam Leffler for (i = 0; i < N(db_cmds); i++) 19439297ba4SSam Leffler db_command_register(&db_cmd_table, &db_cmds[i]); 19539297ba4SSam Leffler for (i = 0; i < N(db_show_cmds); i++) 19639297ba4SSam Leffler db_command_register(&db_show_table, &db_show_cmds[i]); 19739297ba4SSam Leffler for (i = 0; i < N(db_show_all_cmds); i++) 19839297ba4SSam Leffler db_command_register(&db_show_all_table, &db_show_all_cmds[i]); 19939297ba4SSam Leffler #undef N 20039297ba4SSam Leffler } 20139297ba4SSam Leffler 20239297ba4SSam Leffler /* 20339297ba4SSam Leffler * Register a command. 20439297ba4SSam Leffler */ 20539297ba4SSam Leffler void 20639297ba4SSam Leffler db_command_register(struct command_table *list, struct command *cmd) 20739297ba4SSam Leffler { 20839297ba4SSam Leffler struct command *c, *last; 20939297ba4SSam Leffler 21039297ba4SSam Leffler last = NULL; 21139297ba4SSam Leffler LIST_FOREACH(c, list, next) { 21239297ba4SSam Leffler int n = strcmp(cmd->name, c->name); 21339297ba4SSam Leffler 21439297ba4SSam Leffler /* Check that the command is not already present. */ 21539297ba4SSam Leffler if (n == 0) { 21639297ba4SSam Leffler printf("%s: Warning, the command \"%s\" already exists;" 21739297ba4SSam Leffler " ignoring request\n", __func__, cmd->name); 21839297ba4SSam Leffler return; 21939297ba4SSam Leffler } 22039297ba4SSam Leffler if (n < 0) { 22139297ba4SSam Leffler /* NB: keep list sorted lexicographically */ 22239297ba4SSam Leffler LIST_INSERT_BEFORE(c, cmd, next); 22339297ba4SSam Leffler return; 22439297ba4SSam Leffler } 22539297ba4SSam Leffler last = c; 22639297ba4SSam Leffler } 22739297ba4SSam Leffler if (last == NULL) 22839297ba4SSam Leffler LIST_INSERT_HEAD(list, cmd, next); 22939297ba4SSam Leffler else 23039297ba4SSam Leffler LIST_INSERT_AFTER(last, cmd, next); 23139297ba4SSam Leffler } 23239297ba4SSam Leffler 23339297ba4SSam Leffler /* 23439297ba4SSam Leffler * Remove a command previously registered with db_command_register. 23539297ba4SSam Leffler */ 23639297ba4SSam Leffler void 23739297ba4SSam Leffler db_command_unregister(struct command_table *list, struct command *cmd) 23839297ba4SSam Leffler { 23939297ba4SSam Leffler struct command *c; 24039297ba4SSam Leffler 24139297ba4SSam Leffler LIST_FOREACH(c, list, next) { 24239297ba4SSam Leffler if (cmd == c) { 24339297ba4SSam Leffler LIST_REMOVE(cmd, next); 24439297ba4SSam Leffler return; 24539297ba4SSam Leffler } 24639297ba4SSam Leffler } 24739297ba4SSam Leffler /* NB: intentionally quiet */ 24839297ba4SSam Leffler } 24939297ba4SSam Leffler 25039297ba4SSam Leffler /* 251e1e31c0eSJohn Baldwin * Helper function to match a single command. 2525b81b6b3SRodney W. Grimes */ 253e1e31c0eSJohn Baldwin static void 254e1e31c0eSJohn Baldwin db_cmd_match(name, cmd, cmdp, resultp) 2555b81b6b3SRodney W. Grimes char * name; 2565b81b6b3SRodney W. Grimes struct command *cmd; 257e1e31c0eSJohn Baldwin struct command **cmdp; /* out */ 258e1e31c0eSJohn Baldwin int * resultp; 259e1e31c0eSJohn Baldwin { 260e1e31c0eSJohn Baldwin char *lp, *rp; 261e1e31c0eSJohn Baldwin int c; 2625b81b6b3SRodney W. Grimes 2635b81b6b3SRodney W. Grimes lp = name; 2645b81b6b3SRodney W. Grimes rp = cmd->name; 2655b81b6b3SRodney W. Grimes while ((c = *lp) == *rp) { 2665b81b6b3SRodney W. Grimes if (c == 0) { 2675b81b6b3SRodney W. Grimes /* complete match */ 2685b81b6b3SRodney W. Grimes *cmdp = cmd; 269e1e31c0eSJohn Baldwin *resultp = CMD_UNIQUE; 270e1e31c0eSJohn Baldwin return; 2715b81b6b3SRodney W. Grimes } 2725b81b6b3SRodney W. Grimes lp++; 2735b81b6b3SRodney W. Grimes rp++; 2745b81b6b3SRodney W. Grimes } 2755b81b6b3SRodney W. Grimes if (c == 0) { 2765b81b6b3SRodney W. Grimes /* end of name, not end of command - 2775b81b6b3SRodney W. Grimes partial match */ 278e1e31c0eSJohn Baldwin if (*resultp == CMD_FOUND) { 279e1e31c0eSJohn Baldwin *resultp = CMD_AMBIGUOUS; 2805b81b6b3SRodney W. Grimes /* but keep looking for a full match - 2815b81b6b3SRodney W. Grimes this lets us match single letters */ 282e1e31c0eSJohn Baldwin } else { 2835b81b6b3SRodney W. Grimes *cmdp = cmd; 284e1e31c0eSJohn Baldwin *resultp = CMD_FOUND; 2855b81b6b3SRodney W. Grimes } 2865b81b6b3SRodney W. Grimes } 2875b81b6b3SRodney W. Grimes } 2886337f4efSBruce Evans 289e1e31c0eSJohn Baldwin /* 290e1e31c0eSJohn Baldwin * Search for command prefix. 291e1e31c0eSJohn Baldwin */ 292e1e31c0eSJohn Baldwin static int 293e1e31c0eSJohn Baldwin db_cmd_search(name, table, cmdp) 294e1e31c0eSJohn Baldwin char * name; 295e1e31c0eSJohn Baldwin struct command_table *table; 296e1e31c0eSJohn Baldwin struct command **cmdp; /* out */ 297e1e31c0eSJohn Baldwin { 298e1e31c0eSJohn Baldwin struct command *cmd; 299e1e31c0eSJohn Baldwin int result = CMD_NONE; 300e1e31c0eSJohn Baldwin 30139297ba4SSam Leffler LIST_FOREACH(cmd, table, next) { 302e1e31c0eSJohn Baldwin db_cmd_match(name,cmd,cmdp,&result); 303e1e31c0eSJohn Baldwin if (result == CMD_UNIQUE) 30439297ba4SSam Leffler break; 3056337f4efSBruce Evans } 30639297ba4SSam Leffler 3075b81b6b3SRodney W. Grimes if (result == CMD_NONE) { 3085b81b6b3SRodney W. Grimes /* check for 'help' */ 3095b81b6b3SRodney W. Grimes if (name[0] == 'h' && name[1] == 'e' 3105b81b6b3SRodney W. Grimes && name[2] == 'l' && name[3] == 'p') 3115b81b6b3SRodney W. Grimes result = CMD_HELP; 3125b81b6b3SRodney W. Grimes } 3135b81b6b3SRodney W. Grimes return (result); 3145b81b6b3SRodney W. Grimes } 3155b81b6b3SRodney W. Grimes 316f73a856dSPoul-Henning Kamp static void 317e1e31c0eSJohn Baldwin db_cmd_list(table) 318e1e31c0eSJohn Baldwin struct command_table *table; 3195b81b6b3SRodney W. Grimes { 3205b81b6b3SRodney W. Grimes register struct command *cmd; 3215b81b6b3SRodney W. Grimes 32239297ba4SSam Leffler LIST_FOREACH(cmd, table, next) { 3235b81b6b3SRodney W. Grimes db_printf("%-12s", cmd->name); 3242481da74SBruce Evans db_end_line(12); 3255b81b6b3SRodney W. Grimes } 3265b81b6b3SRodney W. Grimes } 3275b81b6b3SRodney W. Grimes 328f73a856dSPoul-Henning Kamp static void 329c9b0cc3bSRobert Watson db_command(last_cmdp, cmd_table, dopager) 3305b81b6b3SRodney W. Grimes struct command **last_cmdp; /* IN_OUT */ 331e1e31c0eSJohn Baldwin struct command_table *cmd_table; 332c9b0cc3bSRobert Watson int dopager; 3335b81b6b3SRodney W. Grimes { 33439297ba4SSam Leffler struct command *cmd = NULL; 3355b81b6b3SRodney W. Grimes int t; 3365b81b6b3SRodney W. Grimes char modif[TOK_STRING_SIZE]; 3375b81b6b3SRodney W. Grimes db_expr_t addr, count; 338381fe1aaSGarrett Wollman boolean_t have_addr = FALSE; 3395b81b6b3SRodney W. Grimes int result; 3405b81b6b3SRodney W. Grimes 3415b81b6b3SRodney W. Grimes t = db_read_token(); 3425b81b6b3SRodney W. Grimes if (t == tEOL) { 3435b81b6b3SRodney W. Grimes /* empty line repeats last command, at 'next' */ 3445b81b6b3SRodney W. Grimes cmd = *last_cmdp; 3455b81b6b3SRodney W. Grimes addr = (db_expr_t)db_next; 3465b81b6b3SRodney W. Grimes have_addr = FALSE; 3475b81b6b3SRodney W. Grimes count = 1; 3485b81b6b3SRodney W. Grimes modif[0] = '\0'; 3495b81b6b3SRodney W. Grimes } 3505b81b6b3SRodney W. Grimes else if (t == tEXCL) { 351976794d9SBruce Evans db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0); 3525b81b6b3SRodney W. Grimes return; 3535b81b6b3SRodney W. Grimes } 3545b81b6b3SRodney W. Grimes else if (t != tIDENT) { 3555b81b6b3SRodney W. Grimes db_printf("?\n"); 3565b81b6b3SRodney W. Grimes db_flush_lex(); 3575b81b6b3SRodney W. Grimes return; 3585b81b6b3SRodney W. Grimes } 3595b81b6b3SRodney W. Grimes else { 3605b81b6b3SRodney W. Grimes /* 3615b81b6b3SRodney W. Grimes * Search for command 3625b81b6b3SRodney W. Grimes */ 3635b81b6b3SRodney W. Grimes while (cmd_table) { 3645b81b6b3SRodney W. Grimes result = db_cmd_search(db_tok_string, 3655b81b6b3SRodney W. Grimes cmd_table, 3665b81b6b3SRodney W. Grimes &cmd); 3675b81b6b3SRodney W. Grimes switch (result) { 3685b81b6b3SRodney W. Grimes case CMD_NONE: 3695b81b6b3SRodney W. Grimes db_printf("No such command\n"); 3705b81b6b3SRodney W. Grimes db_flush_lex(); 3715b81b6b3SRodney W. Grimes return; 3725b81b6b3SRodney W. Grimes case CMD_AMBIGUOUS: 3735b81b6b3SRodney W. Grimes db_printf("Ambiguous\n"); 3745b81b6b3SRodney W. Grimes db_flush_lex(); 3755b81b6b3SRodney W. Grimes return; 3765b81b6b3SRodney W. Grimes case CMD_HELP: 377e1e31c0eSJohn Baldwin db_cmd_list(cmd_table); 3785b81b6b3SRodney W. Grimes db_flush_lex(); 3795b81b6b3SRodney W. Grimes return; 3805b81b6b3SRodney W. Grimes default: 3815b81b6b3SRodney W. Grimes break; 3825b81b6b3SRodney W. Grimes } 383e1e31c0eSJohn Baldwin if ((cmd_table = cmd->more) != NULL) { 3845b81b6b3SRodney W. Grimes t = db_read_token(); 3855b81b6b3SRodney W. Grimes if (t != tIDENT) { 386e1e31c0eSJohn Baldwin db_cmd_list(cmd_table); 3875b81b6b3SRodney W. Grimes db_flush_lex(); 3885b81b6b3SRodney W. Grimes return; 3895b81b6b3SRodney W. Grimes } 3905b81b6b3SRodney W. Grimes } 3915b81b6b3SRodney W. Grimes } 3925b81b6b3SRodney W. Grimes 3935b81b6b3SRodney W. Grimes if ((cmd->flag & CS_OWN) == 0) { 3945b81b6b3SRodney W. Grimes /* 3955b81b6b3SRodney W. Grimes * Standard syntax: 3965b81b6b3SRodney W. Grimes * command [/modifier] [addr] [,count] 3975b81b6b3SRodney W. Grimes */ 3985b81b6b3SRodney W. Grimes t = db_read_token(); 3995b81b6b3SRodney W. Grimes if (t == tSLASH) { 4005b81b6b3SRodney W. Grimes t = db_read_token(); 4015b81b6b3SRodney W. Grimes if (t != tIDENT) { 4025b81b6b3SRodney W. Grimes db_printf("Bad modifier\n"); 4035b81b6b3SRodney W. Grimes db_flush_lex(); 4045b81b6b3SRodney W. Grimes return; 4055b81b6b3SRodney W. Grimes } 4065b81b6b3SRodney W. Grimes db_strcpy(modif, db_tok_string); 4075b81b6b3SRodney W. Grimes } 4085b81b6b3SRodney W. Grimes else { 4095b81b6b3SRodney W. Grimes db_unread_token(t); 4105b81b6b3SRodney W. Grimes modif[0] = '\0'; 4115b81b6b3SRodney W. Grimes } 4125b81b6b3SRodney W. Grimes 4135b81b6b3SRodney W. Grimes if (db_expression(&addr)) { 4145b81b6b3SRodney W. Grimes db_dot = (db_addr_t) addr; 4155b81b6b3SRodney W. Grimes db_last_addr = db_dot; 4165b81b6b3SRodney W. Grimes have_addr = TRUE; 4175b81b6b3SRodney W. Grimes } 4185b81b6b3SRodney W. Grimes else { 4195b81b6b3SRodney W. Grimes addr = (db_expr_t) db_dot; 4205b81b6b3SRodney W. Grimes have_addr = FALSE; 4215b81b6b3SRodney W. Grimes } 4225b81b6b3SRodney W. Grimes t = db_read_token(); 4235b81b6b3SRodney W. Grimes if (t == tCOMMA) { 4245b81b6b3SRodney W. Grimes if (!db_expression(&count)) { 4255b81b6b3SRodney W. Grimes db_printf("Count missing\n"); 4265b81b6b3SRodney W. Grimes db_flush_lex(); 4275b81b6b3SRodney W. Grimes return; 4285b81b6b3SRodney W. Grimes } 4295b81b6b3SRodney W. Grimes } 4305b81b6b3SRodney W. Grimes else { 4315b81b6b3SRodney W. Grimes db_unread_token(t); 4325b81b6b3SRodney W. Grimes count = -1; 4335b81b6b3SRodney W. Grimes } 4345b81b6b3SRodney W. Grimes if ((cmd->flag & CS_MORE) == 0) { 4355b81b6b3SRodney W. Grimes db_skip_to_eol(); 4365b81b6b3SRodney W. Grimes } 4375b81b6b3SRodney W. Grimes } 4385b81b6b3SRodney W. Grimes } 4395b81b6b3SRodney W. Grimes *last_cmdp = cmd; 4405b81b6b3SRodney W. Grimes if (cmd != 0) { 4415b81b6b3SRodney W. Grimes /* 4425b81b6b3SRodney W. Grimes * Execute the command. 4435b81b6b3SRodney W. Grimes */ 444c9b0cc3bSRobert Watson if (dopager) 44519e9205aSJohn Baldwin db_enable_pager(); 446c9b0cc3bSRobert Watson else 447c9b0cc3bSRobert Watson db_disable_pager(); 4485b81b6b3SRodney W. Grimes (*cmd->fcn)(addr, have_addr, count, modif); 449c9b0cc3bSRobert Watson if (dopager) 45019e9205aSJohn Baldwin db_disable_pager(); 4515b81b6b3SRodney W. Grimes 4525b81b6b3SRodney W. Grimes if (cmd->flag & CS_SET_DOT) { 4535b81b6b3SRodney W. Grimes /* 4545b81b6b3SRodney W. Grimes * If command changes dot, set dot to 4555b81b6b3SRodney W. Grimes * previous address displayed (if 'ed' style). 4565b81b6b3SRodney W. Grimes */ 4575b81b6b3SRodney W. Grimes if (db_ed_style) { 4585b81b6b3SRodney W. Grimes db_dot = db_prev; 4595b81b6b3SRodney W. Grimes } 4605b81b6b3SRodney W. Grimes else { 4615b81b6b3SRodney W. Grimes db_dot = db_next; 4625b81b6b3SRodney W. Grimes } 4635b81b6b3SRodney W. Grimes } 4645b81b6b3SRodney W. Grimes else { 4655b81b6b3SRodney W. Grimes /* 4665b81b6b3SRodney W. Grimes * If command does not change dot, 4675b81b6b3SRodney W. Grimes * set 'next' location to be the same. 4685b81b6b3SRodney W. Grimes */ 4695b81b6b3SRodney W. Grimes db_next = db_dot; 4705b81b6b3SRodney W. Grimes } 4715b81b6b3SRodney W. Grimes } 4725b81b6b3SRodney W. Grimes } 4735b81b6b3SRodney W. Grimes 4745b81b6b3SRodney W. Grimes /* 475b7aa38c1SBruce Evans * At least one non-optional command must be implemented using 476b7aa38c1SBruce Evans * DB_COMMAND() so that db_cmd_set gets created. Here is one. 477b7aa38c1SBruce Evans */ 478b7aa38c1SBruce Evans DB_COMMAND(panic, db_panic) 4793eac4884SPoul-Henning Kamp { 48015cc91d3SJohn Baldwin db_disable_pager(); 481edf8a815SDavid Greenman panic("from debugger"); 4823eac4884SPoul-Henning Kamp } 4833eac4884SPoul-Henning Kamp 4843eac4884SPoul-Henning Kamp void 4855b81b6b3SRodney W. Grimes db_command_loop() 4865b81b6b3SRodney W. Grimes { 4875b81b6b3SRodney W. Grimes /* 4885b81b6b3SRodney W. Grimes * Initialize 'prev' and 'next' to dot. 4895b81b6b3SRodney W. Grimes */ 4905b81b6b3SRodney W. Grimes db_prev = db_dot; 4915b81b6b3SRodney W. Grimes db_next = db_dot; 4925b81b6b3SRodney W. Grimes 4935b81b6b3SRodney W. Grimes db_cmd_loop_done = 0; 4945b81b6b3SRodney W. Grimes while (!db_cmd_loop_done) { 4955b81b6b3SRodney W. Grimes if (db_print_position() != 0) 4965b81b6b3SRodney W. Grimes db_printf("\n"); 4975b81b6b3SRodney W. Grimes 4985b81b6b3SRodney W. Grimes db_printf("db> "); 4995b81b6b3SRodney W. Grimes (void) db_read_line(); 5005b81b6b3SRodney W. Grimes 50139297ba4SSam Leffler db_command(&db_last_command, &db_cmd_table, /* dopager */ 1); 5025b81b6b3SRodney W. Grimes } 5035b81b6b3SRodney W. Grimes } 5045b81b6b3SRodney W. Grimes 505c9b0cc3bSRobert Watson /* 506c9b0cc3bSRobert Watson * Execute a command on behalf of a script. The caller is responsible for 507c9b0cc3bSRobert Watson * making sure that the command string is < DB_MAXLINE or it will be 508c9b0cc3bSRobert Watson * truncated. 509c9b0cc3bSRobert Watson * 510c9b0cc3bSRobert Watson * XXXRW: Runs by injecting faked input into DDB input stream; it would be 511c9b0cc3bSRobert Watson * nicer to use an alternative approach that didn't mess with the previous 512c9b0cc3bSRobert Watson * command buffer. 513c9b0cc3bSRobert Watson */ 514c9b0cc3bSRobert Watson void 515c9b0cc3bSRobert Watson db_command_script(const char *command) 516c9b0cc3bSRobert Watson { 517c9b0cc3bSRobert Watson db_prev = db_next = db_dot; 518c9b0cc3bSRobert Watson db_inject_line(command); 51939297ba4SSam Leffler db_command(&db_last_command, &db_cmd_table, /* dopager */ 0); 520c9b0cc3bSRobert Watson } 521c9b0cc3bSRobert Watson 5225b81b6b3SRodney W. Grimes void 5235b81b6b3SRodney W. Grimes db_error(s) 524bda9921dSMark Murray const char *s; 5255b81b6b3SRodney W. Grimes { 5265b81b6b3SRodney W. Grimes if (s) 52758d9a059SKris Kennaway db_printf("%s", s); 5285b81b6b3SRodney W. Grimes db_flush_lex(); 52937224cd3SMarcel Moolenaar kdb_reenter(); 5305b81b6b3SRodney W. Grimes } 5315b81b6b3SRodney W. Grimes 532*299cceefSMarcel Moolenaar static void 533*299cceefSMarcel Moolenaar db_dump(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4) 534*299cceefSMarcel Moolenaar { 535*299cceefSMarcel Moolenaar int error; 536*299cceefSMarcel Moolenaar 537*299cceefSMarcel Moolenaar error = doadump(FALSE); 538*299cceefSMarcel Moolenaar if (error) { 539*299cceefSMarcel Moolenaar db_printf("Cannot dump: "); 540*299cceefSMarcel Moolenaar switch (error) { 541*299cceefSMarcel Moolenaar case EBUSY: 542*299cceefSMarcel Moolenaar db_printf("debugger got invoked while dumping.\n"); 543*299cceefSMarcel Moolenaar break; 544*299cceefSMarcel Moolenaar case ENXIO: 545*299cceefSMarcel Moolenaar db_printf("no dump device specified.\n"); 546*299cceefSMarcel Moolenaar break; 547*299cceefSMarcel Moolenaar default: 548*299cceefSMarcel Moolenaar db_printf("unknown error (error=%d).\n", error); 549*299cceefSMarcel Moolenaar break; 550*299cceefSMarcel Moolenaar } 551*299cceefSMarcel Moolenaar } 552*299cceefSMarcel Moolenaar } 5535b81b6b3SRodney W. Grimes 5545b81b6b3SRodney W. Grimes /* 5555b81b6b3SRodney W. Grimes * Call random function: 5565b81b6b3SRodney W. Grimes * !expr(arg,arg,arg) 5575b81b6b3SRodney W. Grimes */ 558a2aeb24eSMarcel Moolenaar 559a2aeb24eSMarcel Moolenaar /* The generic implementation supports a maximum of 10 arguments. */ 560a2aeb24eSMarcel Moolenaar typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t, 561a2aeb24eSMarcel Moolenaar db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t); 562a2aeb24eSMarcel Moolenaar 563a2aeb24eSMarcel Moolenaar static __inline int 564a2aeb24eSMarcel Moolenaar db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[]) 565a2aeb24eSMarcel Moolenaar { 566a2aeb24eSMarcel Moolenaar __db_f *f = (__db_f *)addr; 567a2aeb24eSMarcel Moolenaar 568a2aeb24eSMarcel Moolenaar if (nargs > 10) { 569a2aeb24eSMarcel Moolenaar db_printf("Too many arguments (max 10)\n"); 570a2aeb24eSMarcel Moolenaar return (0); 571a2aeb24eSMarcel Moolenaar } 572a2aeb24eSMarcel Moolenaar *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5], 573a2aeb24eSMarcel Moolenaar args[6], args[7], args[8], args[9]); 574a2aeb24eSMarcel Moolenaar return (1); 575a2aeb24eSMarcel Moolenaar } 576a2aeb24eSMarcel Moolenaar 577f73a856dSPoul-Henning Kamp static void 578976794d9SBruce Evans db_fncall(dummy1, dummy2, dummy3, dummy4) 579976794d9SBruce Evans db_expr_t dummy1; 580976794d9SBruce Evans boolean_t dummy2; 581976794d9SBruce Evans db_expr_t dummy3; 582976794d9SBruce Evans char * dummy4; 5835b81b6b3SRodney W. Grimes { 5845b81b6b3SRodney W. Grimes db_expr_t fn_addr; 585a2aeb24eSMarcel Moolenaar db_expr_t args[DB_MAXARGS]; 5865b81b6b3SRodney W. Grimes int nargs = 0; 5875b81b6b3SRodney W. Grimes db_expr_t retval; 5885b81b6b3SRodney W. Grimes int t; 5895b81b6b3SRodney W. Grimes 5905b81b6b3SRodney W. Grimes if (!db_expression(&fn_addr)) { 5915b81b6b3SRodney W. Grimes db_printf("Bad function\n"); 5925b81b6b3SRodney W. Grimes db_flush_lex(); 5935b81b6b3SRodney W. Grimes return; 5945b81b6b3SRodney W. Grimes } 5955b81b6b3SRodney W. Grimes 5965b81b6b3SRodney W. Grimes t = db_read_token(); 5975b81b6b3SRodney W. Grimes if (t == tLPAREN) { 5985b81b6b3SRodney W. Grimes if (db_expression(&args[0])) { 5995b81b6b3SRodney W. Grimes nargs++; 6005b81b6b3SRodney W. Grimes while ((t = db_read_token()) == tCOMMA) { 601a2aeb24eSMarcel Moolenaar if (nargs == DB_MAXARGS) { 602a2aeb24eSMarcel Moolenaar db_printf("Too many arguments (max %d)\n", DB_MAXARGS); 6035b81b6b3SRodney W. Grimes db_flush_lex(); 6045b81b6b3SRodney W. Grimes return; 6055b81b6b3SRodney W. Grimes } 6065b81b6b3SRodney W. Grimes if (!db_expression(&args[nargs])) { 6075b81b6b3SRodney W. Grimes db_printf("Argument missing\n"); 6085b81b6b3SRodney W. Grimes db_flush_lex(); 6095b81b6b3SRodney W. Grimes return; 6105b81b6b3SRodney W. Grimes } 6115b81b6b3SRodney W. Grimes nargs++; 6125b81b6b3SRodney W. Grimes } 6135b81b6b3SRodney W. Grimes db_unread_token(t); 6145b81b6b3SRodney W. Grimes } 6155b81b6b3SRodney W. Grimes if (db_read_token() != tRPAREN) { 6165b81b6b3SRodney W. Grimes db_printf("?\n"); 6175b81b6b3SRodney W. Grimes db_flush_lex(); 6185b81b6b3SRodney W. Grimes return; 6195b81b6b3SRodney W. Grimes } 6205b81b6b3SRodney W. Grimes } 6215b81b6b3SRodney W. Grimes db_skip_to_eol(); 62215cc91d3SJohn Baldwin db_disable_pager(); 6235b81b6b3SRodney W. Grimes 624a2aeb24eSMarcel Moolenaar if (DB_CALL(fn_addr, &retval, nargs, args)) 625a2aeb24eSMarcel Moolenaar db_printf("= %#lr\n", (long)retval); 6265b81b6b3SRodney W. Grimes } 627ad146781SPaul Traina 6285370c3b6SPeter Wemm static void 629e4b732cfSBruce Evans db_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4) 630e4b732cfSBruce Evans { 631e4b732cfSBruce Evans 632e4b732cfSBruce Evans cpu_halt(); 633e4b732cfSBruce Evans } 634e4b732cfSBruce Evans 635e4b732cfSBruce Evans static void 63619d2c78fSDima Dorfman db_kill(dummy1, dummy2, dummy3, dummy4) 63719d2c78fSDima Dorfman db_expr_t dummy1; 63819d2c78fSDima Dorfman boolean_t dummy2; 63919d2c78fSDima Dorfman db_expr_t dummy3; 64019d2c78fSDima Dorfman char * dummy4; 64119d2c78fSDima Dorfman { 64219d2c78fSDima Dorfman db_expr_t old_radix, pid, sig; 64319d2c78fSDima Dorfman struct proc *p; 64419d2c78fSDima Dorfman 64519d2c78fSDima Dorfman #define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0) 64619d2c78fSDima Dorfman 64719d2c78fSDima Dorfman /* 64819d2c78fSDima Dorfman * PIDs and signal numbers are typically represented in base 64919d2c78fSDima Dorfman * 10, so make that the default here. It can, of course, be 65019d2c78fSDima Dorfman * overridden by specifying a prefix. 65119d2c78fSDima Dorfman */ 65219d2c78fSDima Dorfman old_radix = db_radix; 65319d2c78fSDima Dorfman db_radix = 10; 65419d2c78fSDima Dorfman /* Retrieve arguments. */ 65519d2c78fSDima Dorfman if (!db_expression(&sig)) 65619d2c78fSDima Dorfman DB_ERROR(("Missing signal number\n")); 65719d2c78fSDima Dorfman if (!db_expression(&pid)) 65819d2c78fSDima Dorfman DB_ERROR(("Missing process ID\n")); 65919d2c78fSDima Dorfman db_skip_to_eol(); 660a29af74bSKonstantin Belousov if (!_SIG_VALID(sig)) 66119d2c78fSDima Dorfman DB_ERROR(("Signal number out of range\n")); 66219d2c78fSDima Dorfman 66319d2c78fSDima Dorfman /* 66419d2c78fSDima Dorfman * Find the process in question. allproc_lock is not needed 66519d2c78fSDima Dorfman * since we're in DDB. 66619d2c78fSDima Dorfman */ 66719d2c78fSDima Dorfman /* sx_slock(&allproc_lock); */ 668f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) 66919d2c78fSDima Dorfman if (p->p_pid == pid) 67019d2c78fSDima Dorfman break; 67119d2c78fSDima Dorfman /* sx_sunlock(&allproc_lock); */ 67219d2c78fSDima Dorfman if (p == NULL) 673e30e3e9eSMatt Jacob DB_ERROR(("Can't find process with pid %ld\n", (long) pid)); 67419d2c78fSDima Dorfman 67519d2c78fSDima Dorfman /* If it's already locked, bail; otherwise, do the deed. */ 67619d2c78fSDima Dorfman if (PROC_TRYLOCK(p) == 0) 677e30e3e9eSMatt Jacob DB_ERROR(("Can't lock process with pid %ld\n", (long) pid)); 67819d2c78fSDima Dorfman else { 679a3de221dSKonstantin Belousov pksignal(p, sig, NULL); 68019d2c78fSDima Dorfman PROC_UNLOCK(p); 68119d2c78fSDima Dorfman } 68219d2c78fSDima Dorfman 68319d2c78fSDima Dorfman out: 68419d2c78fSDima Dorfman db_radix = old_radix; 68519d2c78fSDima Dorfman #undef DB_ERROR 68619d2c78fSDima Dorfman } 68719d2c78fSDima Dorfman 6880f59fbc3SBjoern A. Zeeb /* 6890f59fbc3SBjoern A. Zeeb * Reboot. In case there is an additional argument, take it as delay in 6900f59fbc3SBjoern A. Zeeb * seconds. Default to 15s if we cannot parse it and make sure we will 6910f59fbc3SBjoern A. Zeeb * never wait longer than 1 week. Some code is similar to 6920f59fbc3SBjoern A. Zeeb * kern_shutdown.c:shutdown_panic(). 6930f59fbc3SBjoern A. Zeeb */ 6940f59fbc3SBjoern A. Zeeb #ifndef DB_RESET_MAXDELAY 6950f59fbc3SBjoern A. Zeeb #define DB_RESET_MAXDELAY (3600 * 24 * 7) 6960f59fbc3SBjoern A. Zeeb #endif 6970f59fbc3SBjoern A. Zeeb 69819d2c78fSDima Dorfman static void 6990f59fbc3SBjoern A. Zeeb db_reset(db_expr_t addr, boolean_t have_addr, db_expr_t count __unused, 7000f59fbc3SBjoern A. Zeeb char *modif __unused) 7015370c3b6SPeter Wemm { 7020f59fbc3SBjoern A. Zeeb int delay, loop; 7030f59fbc3SBjoern A. Zeeb 7040f59fbc3SBjoern A. Zeeb if (have_addr) { 7050f59fbc3SBjoern A. Zeeb delay = (int)db_hex2dec(addr); 7060f59fbc3SBjoern A. Zeeb 7070f59fbc3SBjoern A. Zeeb /* If we parse to fail, use 15s. */ 7080f59fbc3SBjoern A. Zeeb if (delay == -1) 7090f59fbc3SBjoern A. Zeeb delay = 15; 7100f59fbc3SBjoern A. Zeeb 7110f59fbc3SBjoern A. Zeeb /* Cap at one week. */ 7120f59fbc3SBjoern A. Zeeb if ((uintmax_t)delay > (uintmax_t)DB_RESET_MAXDELAY) 7130f59fbc3SBjoern A. Zeeb delay = DB_RESET_MAXDELAY; 7140f59fbc3SBjoern A. Zeeb 7150f59fbc3SBjoern A. Zeeb db_printf("Automatic reboot in %d seconds - " 7160f59fbc3SBjoern A. Zeeb "press a key on the console to abort\n", delay); 7170f59fbc3SBjoern A. Zeeb for (loop = delay * 10; loop > 0; --loop) { 7180f59fbc3SBjoern A. Zeeb DELAY(1000 * 100); /* 1/10th second */ 7190f59fbc3SBjoern A. Zeeb /* Did user type a key? */ 7200f59fbc3SBjoern A. Zeeb if (cncheckc() != -1) 7210f59fbc3SBjoern A. Zeeb return; 7220f59fbc3SBjoern A. Zeeb } 7230f59fbc3SBjoern A. Zeeb } 7245370c3b6SPeter Wemm 7255370c3b6SPeter Wemm cpu_reset(); 7265370c3b6SPeter Wemm } 72774cc032bSPoul-Henning Kamp 72874cc032bSPoul-Henning Kamp static void 72974cc032bSPoul-Henning Kamp db_watchdog(dummy1, dummy2, dummy3, dummy4) 73074cc032bSPoul-Henning Kamp db_expr_t dummy1; 73174cc032bSPoul-Henning Kamp boolean_t dummy2; 73274cc032bSPoul-Henning Kamp db_expr_t dummy3; 73374cc032bSPoul-Henning Kamp char * dummy4; 73474cc032bSPoul-Henning Kamp { 7358b927d7bSAttilio Rao db_expr_t old_radix, tout; 7368b927d7bSAttilio Rao int err, i; 7378b927d7bSAttilio Rao 7388b927d7bSAttilio Rao old_radix = db_radix; 7398b927d7bSAttilio Rao db_radix = 10; 7408b927d7bSAttilio Rao err = db_expression(&tout); 7418b927d7bSAttilio Rao db_skip_to_eol(); 7428b927d7bSAttilio Rao db_radix = old_radix; 7438b927d7bSAttilio Rao 7448b927d7bSAttilio Rao /* If no argument is provided the watchdog will just be disabled. */ 7458b927d7bSAttilio Rao if (err == 0) { 7468b927d7bSAttilio Rao db_printf("No argument provided, disabling watchdog\n"); 7478b927d7bSAttilio Rao tout = 0; 7488b927d7bSAttilio Rao } else if ((tout & WD_INTERVAL) == WD_TO_NEVER) { 7498b927d7bSAttilio Rao db_error("Out of range watchdog interval\n"); 7508b927d7bSAttilio Rao return; 7518b927d7bSAttilio Rao } 7528b927d7bSAttilio Rao EVENTHANDLER_INVOKE(watchdog_list, tout, &i); 75374cc032bSPoul-Henning Kamp } 754f3be7cb3SMarcel Moolenaar 755f3be7cb3SMarcel Moolenaar static void 756f3be7cb3SMarcel Moolenaar db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4) 757f3be7cb3SMarcel Moolenaar { 758f3be7cb3SMarcel Moolenaar 7593a5d3671SMatthew D Fleming if (kdb_dbbe_select("gdb") != 0) { 760f3be7cb3SMarcel Moolenaar db_printf("The remote GDB backend could not be selected.\n"); 7613a5d3671SMatthew D Fleming return; 7623a5d3671SMatthew D Fleming } 7633a5d3671SMatthew D Fleming /* 7643a5d3671SMatthew D Fleming * Mark that we are done in the debugger. kdb_trap() 7653a5d3671SMatthew D Fleming * should re-enter with the new backend. 7663a5d3671SMatthew D Fleming */ 7673a5d3671SMatthew D Fleming db_cmd_loop_done = 1; 7683a5d3671SMatthew D Fleming db_printf("(ctrl-c will return control to ddb)\n"); 769f3be7cb3SMarcel Moolenaar } 770fd32d93bSMarcel Moolenaar 771fd32d93bSMarcel Moolenaar static void 772fd32d93bSMarcel Moolenaar db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif) 773fd32d93bSMarcel Moolenaar { 774fd32d93bSMarcel Moolenaar struct thread *td; 775fd32d93bSMarcel Moolenaar db_expr_t radix; 776a13aca1aSRobert Watson pid_t pid; 777fd32d93bSMarcel Moolenaar int t; 778fd32d93bSMarcel Moolenaar 779fd32d93bSMarcel Moolenaar /* 780fd32d93bSMarcel Moolenaar * We parse our own arguments. We don't like the default radix. 781fd32d93bSMarcel Moolenaar */ 782fd32d93bSMarcel Moolenaar radix = db_radix; 783fd32d93bSMarcel Moolenaar db_radix = 10; 784fd32d93bSMarcel Moolenaar hastid = db_expression(&tid); 785fd32d93bSMarcel Moolenaar t = db_read_token(); 786fd32d93bSMarcel Moolenaar if (t == tCOMMA) { 787fd32d93bSMarcel Moolenaar if (!db_expression(&count)) { 788fd32d93bSMarcel Moolenaar db_printf("Count missing\n"); 789fd32d93bSMarcel Moolenaar db_flush_lex(); 790fd32d93bSMarcel Moolenaar return; 791fd32d93bSMarcel Moolenaar } 792fd32d93bSMarcel Moolenaar } else { 793fd32d93bSMarcel Moolenaar db_unread_token(t); 794fd32d93bSMarcel Moolenaar count = -1; 795fd32d93bSMarcel Moolenaar } 796fd32d93bSMarcel Moolenaar db_skip_to_eol(); 797fd32d93bSMarcel Moolenaar db_radix = radix; 798fd32d93bSMarcel Moolenaar 799fd32d93bSMarcel Moolenaar if (hastid) { 800fd32d93bSMarcel Moolenaar td = kdb_thr_lookup((lwpid_t)tid); 801fd32d93bSMarcel Moolenaar if (td == NULL) 802fd32d93bSMarcel Moolenaar td = kdb_thr_from_pid((pid_t)tid); 803fd32d93bSMarcel Moolenaar if (td == NULL) { 804fd32d93bSMarcel Moolenaar db_printf("Thread %d not found\n", (int)tid); 805fd32d93bSMarcel Moolenaar return; 806fd32d93bSMarcel Moolenaar } 807fd32d93bSMarcel Moolenaar } else 808fd32d93bSMarcel Moolenaar td = kdb_thread; 809a13aca1aSRobert Watson if (td->td_proc != NULL) 810a13aca1aSRobert Watson pid = td->td_proc->p_pid; 811a13aca1aSRobert Watson else 812a13aca1aSRobert Watson pid = -1; 813a13aca1aSRobert Watson db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td); 814fd32d93bSMarcel Moolenaar db_trace_thread(td, count); 815fd32d93bSMarcel Moolenaar } 816a7ad956bSRobert Watson 817a7ad956bSRobert Watson static void 818a7ad956bSRobert Watson db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, 819a7ad956bSRobert Watson char *dummy4) 820a7ad956bSRobert Watson { 821a7ad956bSRobert Watson struct proc *p; 822a7ad956bSRobert Watson struct thread *td; 8239641e389SKonstantin Belousov jmp_buf jb; 8249641e389SKonstantin Belousov void *prev_jb; 825a7ad956bSRobert Watson 826f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 8279641e389SKonstantin Belousov prev_jb = kdb_jmpbuf(jb); 8289641e389SKonstantin Belousov if (setjmp(jb) == 0) { 829a7ad956bSRobert Watson FOREACH_THREAD_IN_PROC(p, td) { 830a7ad956bSRobert Watson db_printf("\nTracing command %s pid %d tid %ld td %p\n", 831a7ad956bSRobert Watson p->p_comm, p->p_pid, (long)td->td_tid, td); 832a7ad956bSRobert Watson db_trace_thread(td, -1); 8339641e389SKonstantin Belousov if (db_pager_quit) { 8349641e389SKonstantin Belousov kdb_jmpbuf(prev_jb); 835da927f93SOlivier Houchard return; 836a7ad956bSRobert Watson } 837a7ad956bSRobert Watson } 838a7ad956bSRobert Watson } 8399641e389SKonstantin Belousov kdb_jmpbuf(prev_jb); 8409641e389SKonstantin Belousov } 8419641e389SKonstantin Belousov } 8420f59fbc3SBjoern A. Zeeb 8430f59fbc3SBjoern A. Zeeb /* 8440f59fbc3SBjoern A. Zeeb * Take the parsed expression value from the command line that was parsed 8450f59fbc3SBjoern A. Zeeb * as a hexadecimal value and convert it as if the expression was parsed 8460f59fbc3SBjoern A. Zeeb * as a decimal value. Returns -1 if the expression was not a valid 8470f59fbc3SBjoern A. Zeeb * decimal value. 8480f59fbc3SBjoern A. Zeeb */ 8490f59fbc3SBjoern A. Zeeb db_expr_t 8500f59fbc3SBjoern A. Zeeb db_hex2dec(db_expr_t expr) 8510f59fbc3SBjoern A. Zeeb { 8520f59fbc3SBjoern A. Zeeb uintptr_t x, y; 8530f59fbc3SBjoern A. Zeeb db_expr_t val; 8540f59fbc3SBjoern A. Zeeb 8550f59fbc3SBjoern A. Zeeb y = 1; 8560f59fbc3SBjoern A. Zeeb val = 0; 8570f59fbc3SBjoern A. Zeeb x = expr; 8580f59fbc3SBjoern A. Zeeb while (x != 0) { 8590f59fbc3SBjoern A. Zeeb if (x % 16 > 9) 8600f59fbc3SBjoern A. Zeeb return (-1); 8610f59fbc3SBjoern A. Zeeb val += (x % 16) * (y); 8620f59fbc3SBjoern A. Zeeb x >>= 4; 8630f59fbc3SBjoern A. Zeeb y *= 10; 8640f59fbc3SBjoern A. Zeeb } 8650f59fbc3SBjoern A. Zeeb return (val); 8660f59fbc3SBjoern A. Zeeb } 867