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> 4774cc032bSPoul-Henning Kamp #include <sys/watchdog.h> 483000820aSPoul-Henning Kamp 495ccbc3ccSBruce Evans #include <ddb/ddb.h> 50058284fcSBruce Evans #include <ddb/db_command.h> 515b81b6b3SRodney W. Grimes #include <ddb/db_lex.h> 525b81b6b3SRodney W. Grimes #include <ddb/db_output.h> 535b81b6b3SRodney W. Grimes 5426502503SMarcel Moolenaar #include <machine/cpu.h> 550b1ae809SJulian Elischer #include <machine/setjmp.h> 565b81b6b3SRodney W. Grimes 575b81b6b3SRodney W. Grimes /* 585b81b6b3SRodney W. Grimes * Exported global variables 595b81b6b3SRodney W. Grimes */ 605b81b6b3SRodney W. Grimes boolean_t db_cmd_loop_done; 615b81b6b3SRodney W. Grimes db_addr_t db_dot; 625b81b6b3SRodney W. Grimes db_addr_t db_last_addr; 635b81b6b3SRodney W. Grimes db_addr_t db_prev; 645b81b6b3SRodney W. Grimes db_addr_t db_next; 65f41325dbSPeter Wemm 66f41325dbSPeter Wemm SET_DECLARE(db_cmd_set, struct command); 67f41325dbSPeter Wemm SET_DECLARE(db_show_cmd_set, struct command); 685b81b6b3SRodney W. Grimes 69f73a856dSPoul-Henning Kamp static db_cmdfcn_t db_fncall; 70f3be7cb3SMarcel Moolenaar static db_cmdfcn_t db_gdb; 7119d2c78fSDima Dorfman static db_cmdfcn_t db_kill; 725370c3b6SPeter Wemm static db_cmdfcn_t db_reset; 73fd32d93bSMarcel Moolenaar static db_cmdfcn_t db_stack_trace; 74a7ad956bSRobert Watson static db_cmdfcn_t db_stack_trace_all; 7574cc032bSPoul-Henning Kamp static db_cmdfcn_t db_watchdog; 76ad146781SPaul Traina 77cec9a4bfSDavid E. O'Brien /* 78cec9a4bfSDavid E. O'Brien * 'show' commands 79cec9a4bfSDavid E. O'Brien */ 80cec9a4bfSDavid E. O'Brien 81cec9a4bfSDavid E. O'Brien static struct command db_show_all_cmds[] = { 82cec9a4bfSDavid E. O'Brien { "procs", db_ps, 0, 0 }, 83cec9a4bfSDavid E. O'Brien { (char *)0 } 84cec9a4bfSDavid E. O'Brien }; 85cec9a4bfSDavid E. O'Brien 86e1e31c0eSJohn Baldwin static struct command_table db_show_all_table = { 87e1e31c0eSJohn Baldwin db_show_all_cmds 88e1e31c0eSJohn Baldwin }; 89e1e31c0eSJohn Baldwin 90cec9a4bfSDavid E. O'Brien static struct command db_show_cmds[] = { 91e1e31c0eSJohn Baldwin { "all", 0, 0, &db_show_all_table }, 92cec9a4bfSDavid E. O'Brien { "registers", db_show_regs, 0, 0 }, 93cec9a4bfSDavid E. O'Brien { "breaks", db_listbreak_cmd, 0, 0 }, 94cec9a4bfSDavid E. O'Brien { "threads", db_show_threads, 0, 0 }, 95cec9a4bfSDavid E. O'Brien { (char *)0, } 96cec9a4bfSDavid E. O'Brien }; 97cec9a4bfSDavid E. O'Brien 98e1e31c0eSJohn Baldwin static struct command_table db_show_table = { 99e1e31c0eSJohn Baldwin db_show_cmds, 100e1e31c0eSJohn Baldwin SET_BEGIN(db_show_cmd_set), 101e1e31c0eSJohn Baldwin SET_LIMIT(db_show_cmd_set) 102e1e31c0eSJohn Baldwin }; 103e1e31c0eSJohn Baldwin 104e1e31c0eSJohn Baldwin static struct command db_commands[] = { 105cec9a4bfSDavid E. O'Brien { "print", db_print_cmd, 0, 0 }, 106cec9a4bfSDavid E. O'Brien { "p", db_print_cmd, 0, 0 }, 107cec9a4bfSDavid E. O'Brien { "examine", db_examine_cmd, CS_SET_DOT, 0 }, 108cec9a4bfSDavid E. O'Brien { "x", db_examine_cmd, CS_SET_DOT, 0 }, 109cec9a4bfSDavid E. O'Brien { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 }, 110cec9a4bfSDavid E. O'Brien { "set", db_set_cmd, CS_OWN, 0 }, 111cec9a4bfSDavid E. O'Brien { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 }, 112cec9a4bfSDavid E. O'Brien { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 }, 113cec9a4bfSDavid E. O'Brien { "delete", db_delete_cmd, 0, 0 }, 114cec9a4bfSDavid E. O'Brien { "d", db_delete_cmd, 0, 0 }, 115cec9a4bfSDavid E. O'Brien { "break", db_breakpoint_cmd, 0, 0 }, 116cec9a4bfSDavid E. O'Brien { "dwatch", db_deletewatch_cmd, 0, 0 }, 117cec9a4bfSDavid E. O'Brien { "watch", db_watchpoint_cmd, CS_MORE,0 }, 118cec9a4bfSDavid E. O'Brien { "dhwatch", db_deletehwatch_cmd, 0, 0 }, 119cec9a4bfSDavid E. O'Brien { "hwatch", db_hwatchpoint_cmd, 0, 0 }, 120cec9a4bfSDavid E. O'Brien { "step", db_single_step_cmd, 0, 0 }, 121cec9a4bfSDavid E. O'Brien { "s", db_single_step_cmd, 0, 0 }, 122cec9a4bfSDavid E. O'Brien { "continue", db_continue_cmd, 0, 0 }, 123cec9a4bfSDavid E. O'Brien { "c", db_continue_cmd, 0, 0 }, 124cec9a4bfSDavid E. O'Brien { "until", db_trace_until_call_cmd,0, 0 }, 125cec9a4bfSDavid E. O'Brien { "next", db_trace_until_matching_cmd,0, 0 }, 126cec9a4bfSDavid E. O'Brien { "match", db_trace_until_matching_cmd,0, 0 }, 127cec9a4bfSDavid E. O'Brien { "trace", db_stack_trace, CS_OWN, 0 }, 1283924da21SJohn Baldwin { "alltrace", db_stack_trace_all, 0, 0 }, 129cec9a4bfSDavid E. O'Brien { "where", db_stack_trace, CS_OWN, 0 }, 1303924da21SJohn Baldwin { "bt", db_stack_trace, CS_OWN, 0 }, 131cec9a4bfSDavid E. O'Brien { "call", db_fncall, CS_OWN, 0 }, 132e1e31c0eSJohn Baldwin { "show", 0, 0, &db_show_table }, 133cec9a4bfSDavid E. O'Brien { "ps", db_ps, 0, 0 }, 134cec9a4bfSDavid E. O'Brien { "gdb", db_gdb, 0, 0 }, 135cec9a4bfSDavid E. O'Brien { "reset", db_reset, 0, 0 }, 136cec9a4bfSDavid E. O'Brien { "kill", db_kill, CS_OWN, 0 }, 137cec9a4bfSDavid E. O'Brien { "watchdog", db_watchdog, 0, 0 }, 138cec9a4bfSDavid E. O'Brien { "thread", db_set_thread, CS_OWN, 0 }, 139cec9a4bfSDavid E. O'Brien { (char *)0, } 140cec9a4bfSDavid E. O'Brien }; 141cec9a4bfSDavid E. O'Brien 142e1e31c0eSJohn Baldwin static struct command_table db_command_table = { 143e1e31c0eSJohn Baldwin db_commands, 144e1e31c0eSJohn Baldwin SET_BEGIN(db_cmd_set), 145e1e31c0eSJohn Baldwin SET_LIMIT(db_cmd_set) 146e1e31c0eSJohn Baldwin }; 147e1e31c0eSJohn Baldwin 148cec9a4bfSDavid E. O'Brien static struct command *db_last_command = 0; 1496337f4efSBruce Evans 1505b81b6b3SRodney W. Grimes /* 1515b81b6b3SRodney W. Grimes * if 'ed' style: 'dot' is set at start of last item printed, 1525b81b6b3SRodney W. Grimes * and '+' points to next line. 1535b81b6b3SRodney W. Grimes * Otherwise: 'dot' points to next item, '..' points to last. 1545b81b6b3SRodney W. Grimes */ 155f73a856dSPoul-Henning Kamp static boolean_t db_ed_style = TRUE; 1565b81b6b3SRodney W. Grimes 1575b81b6b3SRodney W. Grimes /* 1585b81b6b3SRodney W. Grimes * Utility routine - discard tokens through end-of-line. 1595b81b6b3SRodney W. Grimes */ 1605b81b6b3SRodney W. Grimes void 1615b81b6b3SRodney W. Grimes db_skip_to_eol() 1625b81b6b3SRodney W. Grimes { 1635b81b6b3SRodney W. Grimes int t; 1645b81b6b3SRodney W. Grimes do { 1655b81b6b3SRodney W. Grimes t = db_read_token(); 1665b81b6b3SRodney W. Grimes } while (t != tEOL); 1675b81b6b3SRodney W. Grimes } 1685b81b6b3SRodney W. Grimes 1695b81b6b3SRodney W. Grimes /* 1705b81b6b3SRodney W. Grimes * Results of command search. 1715b81b6b3SRodney W. Grimes */ 1725b81b6b3SRodney W. Grimes #define CMD_UNIQUE 0 1735b81b6b3SRodney W. Grimes #define CMD_FOUND 1 1745b81b6b3SRodney W. Grimes #define CMD_NONE 2 1755b81b6b3SRodney W. Grimes #define CMD_AMBIGUOUS 3 1765b81b6b3SRodney W. Grimes #define CMD_HELP 4 1775b81b6b3SRodney W. Grimes 178e1e31c0eSJohn Baldwin static void db_cmd_match(char *name, struct command *cmd, 179e1e31c0eSJohn Baldwin struct command **cmdp, int *resultp); 180e1e31c0eSJohn Baldwin static void db_cmd_list(struct command_table *table); 181e1e31c0eSJohn Baldwin static int db_cmd_search(char *name, struct command_table *table, 182e1e31c0eSJohn Baldwin struct command **cmdp); 18314e10f99SAlfred Perlstein static void db_command(struct command **last_cmdp, 184e1e31c0eSJohn Baldwin struct command_table *cmd_table); 185058284fcSBruce Evans 1865b81b6b3SRodney W. Grimes /* 187e1e31c0eSJohn Baldwin * Helper function to match a single command. 1885b81b6b3SRodney W. Grimes */ 189e1e31c0eSJohn Baldwin static void 190e1e31c0eSJohn Baldwin db_cmd_match(name, cmd, cmdp, resultp) 1915b81b6b3SRodney W. Grimes char * name; 1925b81b6b3SRodney W. Grimes struct command *cmd; 193e1e31c0eSJohn Baldwin struct command **cmdp; /* out */ 194e1e31c0eSJohn Baldwin int * resultp; 195e1e31c0eSJohn Baldwin { 196e1e31c0eSJohn Baldwin char *lp, *rp; 197e1e31c0eSJohn Baldwin int c; 1985b81b6b3SRodney W. Grimes 1995b81b6b3SRodney W. Grimes lp = name; 2005b81b6b3SRodney W. Grimes rp = cmd->name; 2015b81b6b3SRodney W. Grimes while ((c = *lp) == *rp) { 2025b81b6b3SRodney W. Grimes if (c == 0) { 2035b81b6b3SRodney W. Grimes /* complete match */ 2045b81b6b3SRodney W. Grimes *cmdp = cmd; 205e1e31c0eSJohn Baldwin *resultp = CMD_UNIQUE; 206e1e31c0eSJohn Baldwin return; 2075b81b6b3SRodney W. Grimes } 2085b81b6b3SRodney W. Grimes lp++; 2095b81b6b3SRodney W. Grimes rp++; 2105b81b6b3SRodney W. Grimes } 2115b81b6b3SRodney W. Grimes if (c == 0) { 2125b81b6b3SRodney W. Grimes /* end of name, not end of command - 2135b81b6b3SRodney W. Grimes partial match */ 214e1e31c0eSJohn Baldwin if (*resultp == CMD_FOUND) { 215e1e31c0eSJohn Baldwin *resultp = CMD_AMBIGUOUS; 2165b81b6b3SRodney W. Grimes /* but keep looking for a full match - 2175b81b6b3SRodney W. Grimes this lets us match single letters */ 218e1e31c0eSJohn Baldwin } else { 2195b81b6b3SRodney W. Grimes *cmdp = cmd; 220e1e31c0eSJohn Baldwin *resultp = CMD_FOUND; 2215b81b6b3SRodney W. Grimes } 2225b81b6b3SRodney W. Grimes } 2235b81b6b3SRodney W. Grimes } 2246337f4efSBruce Evans 225e1e31c0eSJohn Baldwin /* 226e1e31c0eSJohn Baldwin * Search for command prefix. 227e1e31c0eSJohn Baldwin */ 228e1e31c0eSJohn Baldwin static int 229e1e31c0eSJohn Baldwin db_cmd_search(name, table, cmdp) 230e1e31c0eSJohn Baldwin char * name; 231e1e31c0eSJohn Baldwin struct command_table *table; 232e1e31c0eSJohn Baldwin struct command **cmdp; /* out */ 233e1e31c0eSJohn Baldwin { 234e1e31c0eSJohn Baldwin struct command *cmd; 235e1e31c0eSJohn Baldwin struct command **aux_cmdp; 236e1e31c0eSJohn Baldwin int result = CMD_NONE; 237e1e31c0eSJohn Baldwin 238e1e31c0eSJohn Baldwin for (cmd = table->table; cmd->name != 0; cmd++) { 239e1e31c0eSJohn Baldwin db_cmd_match(name, cmd, cmdp, &result); 240e1e31c0eSJohn Baldwin if (result == CMD_UNIQUE) 2416337f4efSBruce Evans return (CMD_UNIQUE); 2426337f4efSBruce Evans } 243e1e31c0eSJohn Baldwin if (table->aux_tablep != NULL) 244e1e31c0eSJohn Baldwin for (aux_cmdp = table->aux_tablep; 245e1e31c0eSJohn Baldwin aux_cmdp < table->aux_tablep_end; 246e1e31c0eSJohn Baldwin aux_cmdp++) { 247e1e31c0eSJohn Baldwin db_cmd_match(name, *aux_cmdp, cmdp, &result); 248e1e31c0eSJohn Baldwin if (result == CMD_UNIQUE) 249e1e31c0eSJohn Baldwin return (CMD_UNIQUE); 2506337f4efSBruce Evans } 2515b81b6b3SRodney W. Grimes if (result == CMD_NONE) { 2525b81b6b3SRodney W. Grimes /* check for 'help' */ 2535b81b6b3SRodney W. Grimes if (name[0] == 'h' && name[1] == 'e' 2545b81b6b3SRodney W. Grimes && name[2] == 'l' && name[3] == 'p') 2555b81b6b3SRodney W. Grimes result = CMD_HELP; 2565b81b6b3SRodney W. Grimes } 2575b81b6b3SRodney W. Grimes return (result); 2585b81b6b3SRodney W. Grimes } 2595b81b6b3SRodney W. Grimes 260f73a856dSPoul-Henning Kamp static void 261e1e31c0eSJohn Baldwin db_cmd_list(table) 262e1e31c0eSJohn Baldwin struct command_table *table; 2635b81b6b3SRodney W. Grimes { 2645b81b6b3SRodney W. Grimes register struct command *cmd; 2656337f4efSBruce Evans register struct command **aux_cmdp; 2665b81b6b3SRodney W. Grimes 267e1e31c0eSJohn Baldwin for (cmd = table->table; cmd->name != 0; cmd++) { 2685b81b6b3SRodney W. Grimes db_printf("%-12s", cmd->name); 2695b81b6b3SRodney W. Grimes db_end_line(); 2705b81b6b3SRodney W. Grimes } 271e1e31c0eSJohn Baldwin if (table->aux_tablep == NULL) 2726337f4efSBruce Evans return; 273e1e31c0eSJohn Baldwin for (aux_cmdp = table->aux_tablep; aux_cmdp < table->aux_tablep_end; 274e1e31c0eSJohn Baldwin aux_cmdp++) { 2756337f4efSBruce Evans db_printf("%-12s", (*aux_cmdp)->name); 2766337f4efSBruce Evans db_end_line(); 2776337f4efSBruce Evans } 2785b81b6b3SRodney W. Grimes } 2795b81b6b3SRodney W. Grimes 280f73a856dSPoul-Henning Kamp static void 281e1e31c0eSJohn Baldwin db_command(last_cmdp, cmd_table) 2825b81b6b3SRodney W. Grimes struct command **last_cmdp; /* IN_OUT */ 283e1e31c0eSJohn Baldwin struct command_table *cmd_table; 2845b81b6b3SRodney W. Grimes { 2855b81b6b3SRodney W. Grimes struct command *cmd; 2865b81b6b3SRodney W. Grimes int t; 2875b81b6b3SRodney W. Grimes char modif[TOK_STRING_SIZE]; 2885b81b6b3SRodney W. Grimes db_expr_t addr, count; 289381fe1aaSGarrett Wollman boolean_t have_addr = FALSE; 2905b81b6b3SRodney W. Grimes int result; 2915b81b6b3SRodney W. Grimes 2925b81b6b3SRodney W. Grimes t = db_read_token(); 2935b81b6b3SRodney W. Grimes if (t == tEOL) { 2945b81b6b3SRodney W. Grimes /* empty line repeats last command, at 'next' */ 2955b81b6b3SRodney W. Grimes cmd = *last_cmdp; 2965b81b6b3SRodney W. Grimes addr = (db_expr_t)db_next; 2975b81b6b3SRodney W. Grimes have_addr = FALSE; 2985b81b6b3SRodney W. Grimes count = 1; 2995b81b6b3SRodney W. Grimes modif[0] = '\0'; 3005b81b6b3SRodney W. Grimes } 3015b81b6b3SRodney W. Grimes else if (t == tEXCL) { 302976794d9SBruce Evans db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0); 3035b81b6b3SRodney W. Grimes return; 3045b81b6b3SRodney W. Grimes } 3055b81b6b3SRodney W. Grimes else if (t != tIDENT) { 3065b81b6b3SRodney W. Grimes db_printf("?\n"); 3075b81b6b3SRodney W. Grimes db_flush_lex(); 3085b81b6b3SRodney W. Grimes return; 3095b81b6b3SRodney W. Grimes } 3105b81b6b3SRodney W. Grimes else { 3115b81b6b3SRodney W. Grimes /* 3125b81b6b3SRodney W. Grimes * Search for command 3135b81b6b3SRodney W. Grimes */ 3145b81b6b3SRodney W. Grimes while (cmd_table) { 3155b81b6b3SRodney W. Grimes result = db_cmd_search(db_tok_string, 3165b81b6b3SRodney W. Grimes cmd_table, 3175b81b6b3SRodney W. Grimes &cmd); 3185b81b6b3SRodney W. Grimes switch (result) { 3195b81b6b3SRodney W. Grimes case CMD_NONE: 3205b81b6b3SRodney W. Grimes db_printf("No such command\n"); 3215b81b6b3SRodney W. Grimes db_flush_lex(); 3225b81b6b3SRodney W. Grimes return; 3235b81b6b3SRodney W. Grimes case CMD_AMBIGUOUS: 3245b81b6b3SRodney W. Grimes db_printf("Ambiguous\n"); 3255b81b6b3SRodney W. Grimes db_flush_lex(); 3265b81b6b3SRodney W. Grimes return; 3275b81b6b3SRodney W. Grimes case CMD_HELP: 328e1e31c0eSJohn Baldwin db_cmd_list(cmd_table); 3295b81b6b3SRodney W. Grimes db_flush_lex(); 3305b81b6b3SRodney W. Grimes return; 3315b81b6b3SRodney W. Grimes default: 3325b81b6b3SRodney W. Grimes break; 3335b81b6b3SRodney W. Grimes } 334e1e31c0eSJohn Baldwin if ((cmd_table = cmd->more) != NULL) { 3355b81b6b3SRodney W. Grimes t = db_read_token(); 3365b81b6b3SRodney W. Grimes if (t != tIDENT) { 337e1e31c0eSJohn Baldwin db_cmd_list(cmd_table); 3385b81b6b3SRodney W. Grimes db_flush_lex(); 3395b81b6b3SRodney W. Grimes return; 3405b81b6b3SRodney W. Grimes } 3415b81b6b3SRodney W. Grimes } 3425b81b6b3SRodney W. Grimes } 3435b81b6b3SRodney W. Grimes 3445b81b6b3SRodney W. Grimes if ((cmd->flag & CS_OWN) == 0) { 3455b81b6b3SRodney W. Grimes /* 3465b81b6b3SRodney W. Grimes * Standard syntax: 3475b81b6b3SRodney W. Grimes * command [/modifier] [addr] [,count] 3485b81b6b3SRodney W. Grimes */ 3495b81b6b3SRodney W. Grimes t = db_read_token(); 3505b81b6b3SRodney W. Grimes if (t == tSLASH) { 3515b81b6b3SRodney W. Grimes t = db_read_token(); 3525b81b6b3SRodney W. Grimes if (t != tIDENT) { 3535b81b6b3SRodney W. Grimes db_printf("Bad modifier\n"); 3545b81b6b3SRodney W. Grimes db_flush_lex(); 3555b81b6b3SRodney W. Grimes return; 3565b81b6b3SRodney W. Grimes } 3575b81b6b3SRodney W. Grimes db_strcpy(modif, db_tok_string); 3585b81b6b3SRodney W. Grimes } 3595b81b6b3SRodney W. Grimes else { 3605b81b6b3SRodney W. Grimes db_unread_token(t); 3615b81b6b3SRodney W. Grimes modif[0] = '\0'; 3625b81b6b3SRodney W. Grimes } 3635b81b6b3SRodney W. Grimes 3645b81b6b3SRodney W. Grimes if (db_expression(&addr)) { 3655b81b6b3SRodney W. Grimes db_dot = (db_addr_t) addr; 3665b81b6b3SRodney W. Grimes db_last_addr = db_dot; 3675b81b6b3SRodney W. Grimes have_addr = TRUE; 3685b81b6b3SRodney W. Grimes } 3695b81b6b3SRodney W. Grimes else { 3705b81b6b3SRodney W. Grimes addr = (db_expr_t) db_dot; 3715b81b6b3SRodney W. Grimes have_addr = FALSE; 3725b81b6b3SRodney W. Grimes } 3735b81b6b3SRodney W. Grimes t = db_read_token(); 3745b81b6b3SRodney W. Grimes if (t == tCOMMA) { 3755b81b6b3SRodney W. Grimes if (!db_expression(&count)) { 3765b81b6b3SRodney W. Grimes db_printf("Count missing\n"); 3775b81b6b3SRodney W. Grimes db_flush_lex(); 3785b81b6b3SRodney W. Grimes return; 3795b81b6b3SRodney W. Grimes } 3805b81b6b3SRodney W. Grimes } 3815b81b6b3SRodney W. Grimes else { 3825b81b6b3SRodney W. Grimes db_unread_token(t); 3835b81b6b3SRodney W. Grimes count = -1; 3845b81b6b3SRodney W. Grimes } 3855b81b6b3SRodney W. Grimes if ((cmd->flag & CS_MORE) == 0) { 3865b81b6b3SRodney W. Grimes db_skip_to_eol(); 3875b81b6b3SRodney W. Grimes } 3885b81b6b3SRodney W. Grimes } 3895b81b6b3SRodney W. Grimes } 3905b81b6b3SRodney W. Grimes *last_cmdp = cmd; 3915b81b6b3SRodney W. Grimes if (cmd != 0) { 3925b81b6b3SRodney W. Grimes /* 3935b81b6b3SRodney W. Grimes * Execute the command. 3945b81b6b3SRodney W. Grimes */ 39519e9205aSJohn Baldwin db_enable_pager(); 3965b81b6b3SRodney W. Grimes (*cmd->fcn)(addr, have_addr, count, modif); 39719e9205aSJohn Baldwin db_disable_pager(); 3985b81b6b3SRodney W. Grimes 3995b81b6b3SRodney W. Grimes if (cmd->flag & CS_SET_DOT) { 4005b81b6b3SRodney W. Grimes /* 4015b81b6b3SRodney W. Grimes * If command changes dot, set dot to 4025b81b6b3SRodney W. Grimes * previous address displayed (if 'ed' style). 4035b81b6b3SRodney W. Grimes */ 4045b81b6b3SRodney W. Grimes if (db_ed_style) { 4055b81b6b3SRodney W. Grimes db_dot = db_prev; 4065b81b6b3SRodney W. Grimes } 4075b81b6b3SRodney W. Grimes else { 4085b81b6b3SRodney W. Grimes db_dot = db_next; 4095b81b6b3SRodney W. Grimes } 4105b81b6b3SRodney W. Grimes } 4115b81b6b3SRodney W. Grimes else { 4125b81b6b3SRodney W. Grimes /* 4135b81b6b3SRodney W. Grimes * If command does not change dot, 4145b81b6b3SRodney W. Grimes * set 'next' location to be the same. 4155b81b6b3SRodney W. Grimes */ 4165b81b6b3SRodney W. Grimes db_next = db_dot; 4175b81b6b3SRodney W. Grimes } 4185b81b6b3SRodney W. Grimes } 4195b81b6b3SRodney W. Grimes } 4205b81b6b3SRodney W. Grimes 4215b81b6b3SRodney W. Grimes /* 422b7aa38c1SBruce Evans * At least one non-optional command must be implemented using 423b7aa38c1SBruce Evans * DB_COMMAND() so that db_cmd_set gets created. Here is one. 424b7aa38c1SBruce Evans */ 425b7aa38c1SBruce Evans DB_COMMAND(panic, db_panic) 4263eac4884SPoul-Henning Kamp { 427edf8a815SDavid Greenman panic("from debugger"); 4283eac4884SPoul-Henning Kamp } 4293eac4884SPoul-Henning Kamp 4303eac4884SPoul-Henning Kamp void 4315b81b6b3SRodney W. Grimes db_command_loop() 4325b81b6b3SRodney W. Grimes { 4335b81b6b3SRodney W. Grimes /* 4345b81b6b3SRodney W. Grimes * Initialize 'prev' and 'next' to dot. 4355b81b6b3SRodney W. Grimes */ 4365b81b6b3SRodney W. Grimes db_prev = db_dot; 4375b81b6b3SRodney W. Grimes db_next = db_dot; 4385b81b6b3SRodney W. Grimes 4395b81b6b3SRodney W. Grimes db_cmd_loop_done = 0; 4405b81b6b3SRodney W. Grimes while (!db_cmd_loop_done) { 4415b81b6b3SRodney W. Grimes if (db_print_position() != 0) 4425b81b6b3SRodney W. Grimes db_printf("\n"); 4435b81b6b3SRodney W. Grimes 4445b81b6b3SRodney W. Grimes db_printf("db> "); 4455b81b6b3SRodney W. Grimes (void) db_read_line(); 4465b81b6b3SRodney W. Grimes 447e1e31c0eSJohn Baldwin db_command(&db_last_command, &db_command_table); 4485b81b6b3SRodney W. Grimes } 4495b81b6b3SRodney W. Grimes } 4505b81b6b3SRodney W. Grimes 4515b81b6b3SRodney W. Grimes void 4525b81b6b3SRodney W. Grimes db_error(s) 453bda9921dSMark Murray const char *s; 4545b81b6b3SRodney W. Grimes { 4555b81b6b3SRodney W. Grimes if (s) 45658d9a059SKris Kennaway db_printf("%s", s); 4575b81b6b3SRodney W. Grimes db_flush_lex(); 45837224cd3SMarcel Moolenaar kdb_reenter(); 4595b81b6b3SRodney W. Grimes } 4605b81b6b3SRodney W. Grimes 4615b81b6b3SRodney W. Grimes 4625b81b6b3SRodney W. Grimes /* 4635b81b6b3SRodney W. Grimes * Call random function: 4645b81b6b3SRodney W. Grimes * !expr(arg,arg,arg) 4655b81b6b3SRodney W. Grimes */ 466a2aeb24eSMarcel Moolenaar 467a2aeb24eSMarcel Moolenaar /* The generic implementation supports a maximum of 10 arguments. */ 468a2aeb24eSMarcel Moolenaar typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t, 469a2aeb24eSMarcel Moolenaar db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t); 470a2aeb24eSMarcel Moolenaar 471a2aeb24eSMarcel Moolenaar static __inline int 472a2aeb24eSMarcel Moolenaar db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[]) 473a2aeb24eSMarcel Moolenaar { 474a2aeb24eSMarcel Moolenaar __db_f *f = (__db_f *)addr; 475a2aeb24eSMarcel Moolenaar 476a2aeb24eSMarcel Moolenaar if (nargs > 10) { 477a2aeb24eSMarcel Moolenaar db_printf("Too many arguments (max 10)\n"); 478a2aeb24eSMarcel Moolenaar return (0); 479a2aeb24eSMarcel Moolenaar } 480a2aeb24eSMarcel Moolenaar *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5], 481a2aeb24eSMarcel Moolenaar args[6], args[7], args[8], args[9]); 482a2aeb24eSMarcel Moolenaar return (1); 483a2aeb24eSMarcel Moolenaar } 484a2aeb24eSMarcel Moolenaar 485f73a856dSPoul-Henning Kamp static void 486976794d9SBruce Evans db_fncall(dummy1, dummy2, dummy3, dummy4) 487976794d9SBruce Evans db_expr_t dummy1; 488976794d9SBruce Evans boolean_t dummy2; 489976794d9SBruce Evans db_expr_t dummy3; 490976794d9SBruce Evans char * dummy4; 4915b81b6b3SRodney W. Grimes { 4925b81b6b3SRodney W. Grimes db_expr_t fn_addr; 493a2aeb24eSMarcel Moolenaar db_expr_t args[DB_MAXARGS]; 4945b81b6b3SRodney W. Grimes int nargs = 0; 4955b81b6b3SRodney W. Grimes db_expr_t retval; 4965b81b6b3SRodney W. Grimes int t; 4975b81b6b3SRodney W. Grimes 4985b81b6b3SRodney W. Grimes if (!db_expression(&fn_addr)) { 4995b81b6b3SRodney W. Grimes db_printf("Bad function\n"); 5005b81b6b3SRodney W. Grimes db_flush_lex(); 5015b81b6b3SRodney W. Grimes return; 5025b81b6b3SRodney W. Grimes } 5035b81b6b3SRodney W. Grimes 5045b81b6b3SRodney W. Grimes t = db_read_token(); 5055b81b6b3SRodney W. Grimes if (t == tLPAREN) { 5065b81b6b3SRodney W. Grimes if (db_expression(&args[0])) { 5075b81b6b3SRodney W. Grimes nargs++; 5085b81b6b3SRodney W. Grimes while ((t = db_read_token()) == tCOMMA) { 509a2aeb24eSMarcel Moolenaar if (nargs == DB_MAXARGS) { 510a2aeb24eSMarcel Moolenaar db_printf("Too many arguments (max %d)\n", DB_MAXARGS); 5115b81b6b3SRodney W. Grimes db_flush_lex(); 5125b81b6b3SRodney W. Grimes return; 5135b81b6b3SRodney W. Grimes } 5145b81b6b3SRodney W. Grimes if (!db_expression(&args[nargs])) { 5155b81b6b3SRodney W. Grimes db_printf("Argument missing\n"); 5165b81b6b3SRodney W. Grimes db_flush_lex(); 5175b81b6b3SRodney W. Grimes return; 5185b81b6b3SRodney W. Grimes } 5195b81b6b3SRodney W. Grimes nargs++; 5205b81b6b3SRodney W. Grimes } 5215b81b6b3SRodney W. Grimes db_unread_token(t); 5225b81b6b3SRodney W. Grimes } 5235b81b6b3SRodney W. Grimes if (db_read_token() != tRPAREN) { 5245b81b6b3SRodney W. Grimes db_printf("?\n"); 5255b81b6b3SRodney W. Grimes db_flush_lex(); 5265b81b6b3SRodney W. Grimes return; 5275b81b6b3SRodney W. Grimes } 5285b81b6b3SRodney W. Grimes } 5295b81b6b3SRodney W. Grimes db_skip_to_eol(); 5305b81b6b3SRodney W. Grimes 531a2aeb24eSMarcel Moolenaar if (DB_CALL(fn_addr, &retval, nargs, args)) 532a2aeb24eSMarcel Moolenaar db_printf("= %#lr\n", (long)retval); 5335b81b6b3SRodney W. Grimes } 534ad146781SPaul Traina 5355370c3b6SPeter Wemm static void 53619d2c78fSDima Dorfman db_kill(dummy1, dummy2, dummy3, dummy4) 53719d2c78fSDima Dorfman db_expr_t dummy1; 53819d2c78fSDima Dorfman boolean_t dummy2; 53919d2c78fSDima Dorfman db_expr_t dummy3; 54019d2c78fSDima Dorfman char * dummy4; 54119d2c78fSDima Dorfman { 54219d2c78fSDima Dorfman db_expr_t old_radix, pid, sig; 54319d2c78fSDima Dorfman struct proc *p; 54419d2c78fSDima Dorfman 54519d2c78fSDima Dorfman #define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0) 54619d2c78fSDima Dorfman 54719d2c78fSDima Dorfman /* 54819d2c78fSDima Dorfman * PIDs and signal numbers are typically represented in base 54919d2c78fSDima Dorfman * 10, so make that the default here. It can, of course, be 55019d2c78fSDima Dorfman * overridden by specifying a prefix. 55119d2c78fSDima Dorfman */ 55219d2c78fSDima Dorfman old_radix = db_radix; 55319d2c78fSDima Dorfman db_radix = 10; 55419d2c78fSDima Dorfman /* Retrieve arguments. */ 55519d2c78fSDima Dorfman if (!db_expression(&sig)) 55619d2c78fSDima Dorfman DB_ERROR(("Missing signal number\n")); 55719d2c78fSDima Dorfman if (!db_expression(&pid)) 55819d2c78fSDima Dorfman DB_ERROR(("Missing process ID\n")); 55919d2c78fSDima Dorfman db_skip_to_eol(); 56019d2c78fSDima Dorfman if (sig < 0 || sig > _SIG_MAXSIG) 56119d2c78fSDima Dorfman DB_ERROR(("Signal number out of range\n")); 56219d2c78fSDima Dorfman 56319d2c78fSDima Dorfman /* 56419d2c78fSDima Dorfman * Find the process in question. allproc_lock is not needed 56519d2c78fSDima Dorfman * since we're in DDB. 56619d2c78fSDima Dorfman */ 56719d2c78fSDima Dorfman /* sx_slock(&allproc_lock); */ 56819d2c78fSDima Dorfman LIST_FOREACH(p, &allproc, p_list) 56919d2c78fSDima Dorfman if (p->p_pid == pid) 57019d2c78fSDima Dorfman break; 57119d2c78fSDima Dorfman /* sx_sunlock(&allproc_lock); */ 57219d2c78fSDima Dorfman if (p == NULL) 573e30e3e9eSMatt Jacob DB_ERROR(("Can't find process with pid %ld\n", (long) pid)); 57419d2c78fSDima Dorfman 57519d2c78fSDima Dorfman /* If it's already locked, bail; otherwise, do the deed. */ 57619d2c78fSDima Dorfman if (PROC_TRYLOCK(p) == 0) 577e30e3e9eSMatt Jacob DB_ERROR(("Can't lock process with pid %ld\n", (long) pid)); 57819d2c78fSDima Dorfman else { 57919d2c78fSDima Dorfman psignal(p, sig); 58019d2c78fSDima Dorfman PROC_UNLOCK(p); 58119d2c78fSDima Dorfman } 58219d2c78fSDima Dorfman 58319d2c78fSDima Dorfman out: 58419d2c78fSDima Dorfman db_radix = old_radix; 58519d2c78fSDima Dorfman #undef DB_ERROR 58619d2c78fSDima Dorfman } 58719d2c78fSDima Dorfman 58819d2c78fSDima Dorfman static void 5895370c3b6SPeter Wemm db_reset(dummy1, dummy2, dummy3, dummy4) 5905370c3b6SPeter Wemm db_expr_t dummy1; 5915370c3b6SPeter Wemm boolean_t dummy2; 5925370c3b6SPeter Wemm db_expr_t dummy3; 5935370c3b6SPeter Wemm char * dummy4; 5945370c3b6SPeter Wemm { 5955370c3b6SPeter Wemm 5965370c3b6SPeter Wemm cpu_reset(); 5975370c3b6SPeter Wemm } 59874cc032bSPoul-Henning Kamp 59974cc032bSPoul-Henning Kamp static void 60074cc032bSPoul-Henning Kamp db_watchdog(dummy1, dummy2, dummy3, dummy4) 60174cc032bSPoul-Henning Kamp db_expr_t dummy1; 60274cc032bSPoul-Henning Kamp boolean_t dummy2; 60374cc032bSPoul-Henning Kamp db_expr_t dummy3; 60474cc032bSPoul-Henning Kamp char * dummy4; 60574cc032bSPoul-Henning Kamp { 60674cc032bSPoul-Henning Kamp int i; 60774cc032bSPoul-Henning Kamp 60874cc032bSPoul-Henning Kamp /* 60974cc032bSPoul-Henning Kamp * XXX: It might make sense to be able to set the watchdog to a 61074cc032bSPoul-Henning Kamp * XXX: timeout here so that failure or hang as a result of subsequent 61174cc032bSPoul-Henning Kamp * XXX: ddb commands could be recovered by a reset. 61274cc032bSPoul-Henning Kamp */ 61374cc032bSPoul-Henning Kamp 61474cc032bSPoul-Henning Kamp EVENTHANDLER_INVOKE(watchdog_list, 0, &i); 61574cc032bSPoul-Henning Kamp } 616f3be7cb3SMarcel Moolenaar 617f3be7cb3SMarcel Moolenaar static void 618f3be7cb3SMarcel Moolenaar db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4) 619f3be7cb3SMarcel Moolenaar { 620f3be7cb3SMarcel Moolenaar 621f3be7cb3SMarcel Moolenaar if (kdb_dbbe_select("gdb") != 0) 622f3be7cb3SMarcel Moolenaar db_printf("The remote GDB backend could not be selected.\n"); 623f3be7cb3SMarcel Moolenaar else 624f3be7cb3SMarcel Moolenaar db_printf("Step to enter the remote GDB backend.\n"); 625f3be7cb3SMarcel Moolenaar } 626fd32d93bSMarcel Moolenaar 627fd32d93bSMarcel Moolenaar static void 628fd32d93bSMarcel Moolenaar db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif) 629fd32d93bSMarcel Moolenaar { 630fd32d93bSMarcel Moolenaar struct thread *td; 631fd32d93bSMarcel Moolenaar db_expr_t radix; 632a13aca1aSRobert Watson pid_t pid; 633fd32d93bSMarcel Moolenaar int t; 634fd32d93bSMarcel Moolenaar 635fd32d93bSMarcel Moolenaar /* 636fd32d93bSMarcel Moolenaar * We parse our own arguments. We don't like the default radix. 637fd32d93bSMarcel Moolenaar */ 638fd32d93bSMarcel Moolenaar radix = db_radix; 639fd32d93bSMarcel Moolenaar db_radix = 10; 640fd32d93bSMarcel Moolenaar hastid = db_expression(&tid); 641fd32d93bSMarcel Moolenaar t = db_read_token(); 642fd32d93bSMarcel Moolenaar if (t == tCOMMA) { 643fd32d93bSMarcel Moolenaar if (!db_expression(&count)) { 644fd32d93bSMarcel Moolenaar db_printf("Count missing\n"); 645fd32d93bSMarcel Moolenaar db_flush_lex(); 646fd32d93bSMarcel Moolenaar return; 647fd32d93bSMarcel Moolenaar } 648fd32d93bSMarcel Moolenaar } else { 649fd32d93bSMarcel Moolenaar db_unread_token(t); 650fd32d93bSMarcel Moolenaar count = -1; 651fd32d93bSMarcel Moolenaar } 652fd32d93bSMarcel Moolenaar db_skip_to_eol(); 653fd32d93bSMarcel Moolenaar db_radix = radix; 654fd32d93bSMarcel Moolenaar 655fd32d93bSMarcel Moolenaar if (hastid) { 656fd32d93bSMarcel Moolenaar td = kdb_thr_lookup((lwpid_t)tid); 657fd32d93bSMarcel Moolenaar if (td == NULL) 658fd32d93bSMarcel Moolenaar td = kdb_thr_from_pid((pid_t)tid); 659fd32d93bSMarcel Moolenaar if (td == NULL) { 660fd32d93bSMarcel Moolenaar db_printf("Thread %d not found\n", (int)tid); 661fd32d93bSMarcel Moolenaar return; 662fd32d93bSMarcel Moolenaar } 663fd32d93bSMarcel Moolenaar } else 664fd32d93bSMarcel Moolenaar td = kdb_thread; 665a13aca1aSRobert Watson if (td->td_proc != NULL) 666a13aca1aSRobert Watson pid = td->td_proc->p_pid; 667a13aca1aSRobert Watson else 668a13aca1aSRobert Watson pid = -1; 669a13aca1aSRobert Watson db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td); 670fd32d93bSMarcel Moolenaar db_trace_thread(td, count); 671fd32d93bSMarcel Moolenaar } 672a7ad956bSRobert Watson 673a7ad956bSRobert Watson static void 674a7ad956bSRobert Watson db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, 675a7ad956bSRobert Watson char *dummy4) 676a7ad956bSRobert Watson { 677a7ad956bSRobert Watson struct proc *p; 678a7ad956bSRobert Watson struct thread *td; 679a7ad956bSRobert Watson 680cf93bc66SJohn Baldwin LIST_FOREACH(p, &allproc, p_list) { 681a7ad956bSRobert Watson FOREACH_THREAD_IN_PROC(p, td) { 682a7ad956bSRobert Watson db_printf("\nTracing command %s pid %d tid %ld td %p\n", 683a7ad956bSRobert Watson p->p_comm, p->p_pid, (long)td->td_tid, td); 684a7ad956bSRobert Watson db_trace_thread(td, -1); 68519e9205aSJohn Baldwin if (db_pager_quit) 686da927f93SOlivier Houchard return; 687a7ad956bSRobert Watson } 688a7ad956bSRobert Watson } 689a7ad956bSRobert Watson } 690