1139747Simp/*- 24Srgrimes * Mach Operating System 34Srgrimes * Copyright (c) 1991,1990 Carnegie Mellon University 44Srgrimes * All Rights Reserved. 58876Srgrimes * 64Srgrimes * Permission to use, copy, modify and distribute this software and its 74Srgrimes * documentation is hereby granted, provided that both the copyright 84Srgrimes * notice and this permission notice appear in all copies of the 94Srgrimes * software, derivative works or modified versions, and any portions 104Srgrimes * thereof, and that both notices appear in supporting documentation. 118876Srgrimes * 128876Srgrimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 134Srgrimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 144Srgrimes * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 158876Srgrimes * 164Srgrimes * Carnegie Mellon requests users of this software to return to 178876Srgrimes * 184Srgrimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 194Srgrimes * School of Computer Science 204Srgrimes * Carnegie Mellon University 214Srgrimes * Pittsburgh PA 15213-3890 228876Srgrimes * 234Srgrimes * any improvements or extensions that they make and grant Carnegie the 244Srgrimes * rights to redistribute these changes. 254Srgrimes */ 264Srgrimes/* 274Srgrimes * Author: David B. Golub, Carnegie Mellon University 284Srgrimes * Date: 7/90 294Srgrimes */ 304Srgrimes/* 314Srgrimes * Command dispatcher. 324Srgrimes */ 33116176Sobrien 34116176Sobrien#include <sys/cdefs.h> 35116176Sobrien__FBSDID("$FreeBSD: stable/11/sys/ddb/db_command.c 324267 2017-10-04 11:55:34Z trasz $"); 36116176Sobrien 372056Swollman#include <sys/param.h> 3842654Sjdp#include <sys/linker_set.h> 3986998Sdd#include <sys/lock.h> 40131952Smarcel#include <sys/kdb.h> 4186998Sdd#include <sys/mutex.h> 4286998Sdd#include <sys/proc.h> 4317848Spst#include <sys/reboot.h> 4486998Sdd#include <sys/signalvar.h> 452056Swollman#include <sys/systm.h> 4649558Sphk#include <sys/cons.h> 47222801Smarcel#include <sys/conf.h> 48126399Sphk#include <sys/watchdog.h> 49183054Ssam#include <sys/kernel.h> 5012734Sbde 512056Swollman#include <ddb/ddb.h> 5212473Sbde#include <ddb/db_command.h> 534Srgrimes#include <ddb/db_lex.h> 544Srgrimes#include <ddb/db_output.h> 554Srgrimes 56118990Smarcel#include <machine/cpu.h> 5779418Sjulian#include <machine/setjmp.h> 584Srgrimes 594Srgrimes/* 604Srgrimes * Exported global variables 614Srgrimes */ 62283248Spfgbool db_cmd_loop_done; 6318296Sbdedb_addr_t db_dot; 644Srgrimesdb_addr_t db_last_addr; 654Srgrimesdb_addr_t db_prev; 664Srgrimesdb_addr_t db_next; 674Srgrimes 68222801Smarcelstatic db_cmdfcn_t db_dump; 6912515Sphkstatic db_cmdfcn_t db_fncall; 70132002Smarcelstatic db_cmdfcn_t db_gdb; 71163192Sbdestatic db_cmdfcn_t db_halt; 7286998Sddstatic db_cmdfcn_t db_kill; 7385944Speterstatic db_cmdfcn_t db_reset; 74132482Smarcelstatic db_cmdfcn_t db_stack_trace; 75150819Srwatsonstatic db_cmdfcn_t db_stack_trace_all; 76126399Sphkstatic db_cmdfcn_t db_watchdog; 7717848Spst 78148919Sobrien/* 79148919Sobrien * 'show' commands 80148919Sobrien */ 8118296Sbde 82148919Sobrienstatic struct command db_show_all_cmds[] = { 83283296Spfg { "trace", db_stack_trace_all, 0, NULL }, 84148919Sobrien}; 85183054Ssamstruct command_table db_show_all_table = 86183054Ssam LIST_HEAD_INITIALIZER(db_show_all_table); 87148919Sobrien 88148919Sobrienstatic struct command db_show_cmds[] = { 89156412Sjhb { "all", 0, 0, &db_show_all_table }, 90283296Spfg { "registers", db_show_regs, 0, NULL }, 91283296Spfg { "breaks", db_listbreak_cmd, 0, NULL }, 92283296Spfg { "threads", db_show_threads, 0, NULL }, 93148919Sobrien}; 94183054Ssamstruct command_table db_show_table = LIST_HEAD_INITIALIZER(db_show_table); 95148919Sobrien 96183054Ssamstatic struct command db_cmds[] = { 97283296Spfg { "print", db_print_cmd, 0, NULL }, 98283296Spfg { "p", db_print_cmd, 0, NULL }, 99283296Spfg { "examine", db_examine_cmd, CS_SET_DOT, NULL }, 100283296Spfg { "x", db_examine_cmd, CS_SET_DOT, NULL }, 101283296Spfg { "search", db_search_cmd, CS_OWN|CS_SET_DOT, NULL }, 102283296Spfg { "set", db_set_cmd, CS_OWN, NULL }, 103283296Spfg { "write", db_write_cmd, CS_MORE|CS_SET_DOT, NULL }, 104283296Spfg { "w", db_write_cmd, CS_MORE|CS_SET_DOT, NULL }, 105283296Spfg { "delete", db_delete_cmd, 0, NULL }, 106283296Spfg { "d", db_delete_cmd, 0, NULL }, 107283296Spfg { "dump", db_dump, 0, NULL }, 108283296Spfg { "break", db_breakpoint_cmd, 0, NULL }, 109283296Spfg { "b", db_breakpoint_cmd, 0, NULL }, 110283296Spfg { "dwatch", db_deletewatch_cmd, 0, NULL }, 111283296Spfg { "watch", db_watchpoint_cmd, CS_MORE,NULL }, 112283296Spfg { "dhwatch", db_deletehwatch_cmd, 0, NULL }, 113283296Spfg { "hwatch", db_hwatchpoint_cmd, 0, NULL }, 114283296Spfg { "step", db_single_step_cmd, 0, NULL }, 115283296Spfg { "s", db_single_step_cmd, 0, NULL }, 116283296Spfg { "continue", db_continue_cmd, 0, NULL }, 117283296Spfg { "c", db_continue_cmd, 0, NULL }, 118283296Spfg { "until", db_trace_until_call_cmd,0, NULL }, 119283296Spfg { "next", db_trace_until_matching_cmd,0, NULL }, 120283296Spfg { "match", db_trace_until_matching_cmd,0, NULL }, 121283296Spfg { "trace", db_stack_trace, CS_OWN, NULL }, 122283296Spfg { "t", db_stack_trace, CS_OWN, NULL }, 123183054Ssam /* XXX alias for all trace */ 124283296Spfg { "alltrace", db_stack_trace_all, 0, NULL }, 125283296Spfg { "where", db_stack_trace, CS_OWN, NULL }, 126283296Spfg { "bt", db_stack_trace, CS_OWN, NULL }, 127283296Spfg { "call", db_fncall, CS_OWN, NULL }, 128156412Sjhb { "show", 0, 0, &db_show_table }, 129283296Spfg { "ps", db_ps, 0, NULL }, 130283296Spfg { "gdb", db_gdb, 0, NULL }, 131283296Spfg { "halt", db_halt, 0, NULL }, 132283296Spfg { "reboot", db_reset, 0, NULL }, 133283296Spfg { "reset", db_reset, 0, NULL }, 134283296Spfg { "kill", db_kill, CS_OWN, NULL }, 135283296Spfg { "watchdog", db_watchdog, CS_OWN, NULL }, 136283296Spfg { "thread", db_set_thread, CS_OWN, NULL }, 137283296Spfg { "run", db_run_cmd, CS_OWN, NULL }, 138283296Spfg { "script", db_script_cmd, CS_OWN, NULL }, 139283296Spfg { "scripts", db_scripts_cmd, 0, NULL }, 140283296Spfg { "unscript", db_unscript_cmd, CS_OWN, NULL }, 141283296Spfg { "capture", db_capture_cmd, CS_OWN, NULL }, 142283296Spfg { "textdump", db_textdump_cmd, CS_OWN, NULL }, 143283296Spfg { "findstack", db_findstack_cmd, 0, NULL }, 144148919Sobrien}; 145183054Ssamstruct command_table db_cmd_table = LIST_HEAD_INITIALIZER(db_cmd_table); 146148919Sobrien 147298073Spfgstatic struct command *db_last_command = NULL; 148148919Sobrien 1494Srgrimes/* 1504Srgrimes * if 'ed' style: 'dot' is set at start of last item printed, 1514Srgrimes * and '+' points to next line. 1524Srgrimes * Otherwise: 'dot' points to next item, '..' points to last. 1534Srgrimes */ 154283248Spfgstatic bool db_ed_style = true; 1554Srgrimes 1564Srgrimes/* 1574Srgrimes * Utility routine - discard tokens through end-of-line. 1584Srgrimes */ 1594Srgrimesvoid 160273006Spfgdb_skip_to_eol(void) 1614Srgrimes{ 1624Srgrimes int t; 1634Srgrimes do { 1644Srgrimes t = db_read_token(); 1654Srgrimes } while (t != tEOL); 1664Srgrimes} 1674Srgrimes 1684Srgrimes/* 1694Srgrimes * Results of command search. 1704Srgrimes */ 1714Srgrimes#define CMD_UNIQUE 0 1724Srgrimes#define CMD_FOUND 1 1734Srgrimes#define CMD_NONE 2 1744Srgrimes#define CMD_AMBIGUOUS 3 1754Srgrimes#define CMD_HELP 4 1764Srgrimes 177156412Sjhbstatic void db_cmd_match(char *name, struct command *cmd, 178156412Sjhb struct command **cmdp, int *resultp); 179156412Sjhbstatic void db_cmd_list(struct command_table *table); 180156412Sjhbstatic int db_cmd_search(char *name, struct command_table *table, 181156412Sjhb struct command **cmdp); 18292756Salfredstatic void db_command(struct command **last_cmdp, 183174914Srwatson struct command_table *cmd_table, int dopager); 18412473Sbde 1854Srgrimes/* 186183054Ssam * Initialize the command lists from the static tables. 187183054Ssam */ 188183360Sjhbvoid 189183360Sjhbdb_command_init(void) 190183054Ssam{ 191183054Ssam#define N(a) (sizeof(a) / sizeof(a[0])) 192183054Ssam int i; 193183054Ssam 194183054Ssam for (i = 0; i < N(db_cmds); i++) 195183054Ssam db_command_register(&db_cmd_table, &db_cmds[i]); 196183054Ssam for (i = 0; i < N(db_show_cmds); i++) 197183054Ssam db_command_register(&db_show_table, &db_show_cmds[i]); 198183054Ssam for (i = 0; i < N(db_show_all_cmds); i++) 199183054Ssam db_command_register(&db_show_all_table, &db_show_all_cmds[i]); 200183054Ssam#undef N 201183054Ssam} 202183054Ssam 203183054Ssam/* 204183054Ssam * Register a command. 205183054Ssam */ 206183054Ssamvoid 207183054Ssamdb_command_register(struct command_table *list, struct command *cmd) 208183054Ssam{ 209183054Ssam struct command *c, *last; 210183054Ssam 211183054Ssam last = NULL; 212183054Ssam LIST_FOREACH(c, list, next) { 213183054Ssam int n = strcmp(cmd->name, c->name); 214183054Ssam 215183054Ssam /* Check that the command is not already present. */ 216183054Ssam if (n == 0) { 217183054Ssam printf("%s: Warning, the command \"%s\" already exists;" 218183054Ssam " ignoring request\n", __func__, cmd->name); 219183054Ssam return; 220183054Ssam } 221183054Ssam if (n < 0) { 222183054Ssam /* NB: keep list sorted lexicographically */ 223183054Ssam LIST_INSERT_BEFORE(c, cmd, next); 224183054Ssam return; 225183054Ssam } 226183054Ssam last = c; 227183054Ssam } 228183054Ssam if (last == NULL) 229183054Ssam LIST_INSERT_HEAD(list, cmd, next); 230183054Ssam else 231183054Ssam LIST_INSERT_AFTER(last, cmd, next); 232183054Ssam} 233183054Ssam 234183054Ssam/* 235183054Ssam * Remove a command previously registered with db_command_register. 236183054Ssam */ 237183054Ssamvoid 238183054Ssamdb_command_unregister(struct command_table *list, struct command *cmd) 239183054Ssam{ 240183054Ssam struct command *c; 241183054Ssam 242183054Ssam LIST_FOREACH(c, list, next) { 243183054Ssam if (cmd == c) { 244183054Ssam LIST_REMOVE(cmd, next); 245183054Ssam return; 246183054Ssam } 247183054Ssam } 248183054Ssam /* NB: intentionally quiet */ 249183054Ssam} 250183054Ssam 251183054Ssam/* 252156412Sjhb * Helper function to match a single command. 2534Srgrimes */ 254156412Sjhbstatic void 255273006Spfgdb_cmd_match(char *name, struct command *cmd, struct command **cmdp, 256273006Spfg int *resultp) 2574Srgrimes{ 258156412Sjhb char *lp, *rp; 259156412Sjhb int c; 2604Srgrimes 261156412Sjhb lp = name; 262156412Sjhb rp = cmd->name; 263156412Sjhb while ((c = *lp) == *rp) { 2644Srgrimes if (c == 0) { 265156412Sjhb /* complete match */ 266156412Sjhb *cmdp = cmd; 267156412Sjhb *resultp = CMD_UNIQUE; 268156412Sjhb return; 2694Srgrimes } 2704Srgrimes lp++; 2714Srgrimes rp++; 272156412Sjhb } 273156412Sjhb if (c == 0) { 2744Srgrimes /* end of name, not end of command - 2754Srgrimes partial match */ 276156412Sjhb if (*resultp == CMD_FOUND) { 277156412Sjhb *resultp = CMD_AMBIGUOUS; 278156412Sjhb /* but keep looking for a full match - 279156412Sjhb this lets us match single letters */ 280156412Sjhb } else { 281156412Sjhb *cmdp = cmd; 282156412Sjhb *resultp = CMD_FOUND; 2834Srgrimes } 2844Srgrimes } 285156412Sjhb} 28618296Sbde 287156412Sjhb/* 288156412Sjhb * Search for command prefix. 289156412Sjhb */ 290156412Sjhbstatic int 291273006Spfgdb_cmd_search(char *name, struct command_table *table, struct command **cmdp) 292156412Sjhb{ 293156412Sjhb struct command *cmd; 294156412Sjhb int result = CMD_NONE; 295156412Sjhb 296183054Ssam LIST_FOREACH(cmd, table, next) { 297183054Ssam db_cmd_match(name,cmd,cmdp,&result); 298156412Sjhb if (result == CMD_UNIQUE) 299183054Ssam break; 300156412Sjhb } 301183054Ssam 3024Srgrimes if (result == CMD_NONE) { 303156412Sjhb /* check for 'help' */ 3044Srgrimes if (name[0] == 'h' && name[1] == 'e' 3054Srgrimes && name[2] == 'l' && name[3] == 'p') 3064Srgrimes result = CMD_HELP; 3074Srgrimes } 3084Srgrimes return (result); 3094Srgrimes} 3104Srgrimes 31112515Sphkstatic void 312273006Spfgdb_cmd_list(struct command_table *table) 3134Srgrimes{ 314283315Spfg struct command *cmd; 315324265Strasz int have_subcommands; 3164Srgrimes 317324265Strasz have_subcommands = 0; 318183054Ssam LIST_FOREACH(cmd, table, next) { 319324265Strasz if (cmd->more != NULL) 320324265Strasz have_subcommands++; 321266985Sjmg db_printf("%-16s", cmd->name); 322266985Sjmg db_end_line(16); 3234Srgrimes } 324324265Strasz 325324265Strasz if (have_subcommands > 0) { 326324265Strasz db_printf("\nThe following have subcommands; append \"help\" " 327324265Strasz "to list (e.g. \"show help\"):\n"); 328324265Strasz LIST_FOREACH(cmd, table, next) { 329324265Strasz if (cmd->more == NULL) 330324265Strasz continue; 331324265Strasz db_printf("%-16s", cmd->name); 332324265Strasz db_end_line(16); 333324265Strasz } 334324265Strasz } 3354Srgrimes} 3364Srgrimes 33712515Sphkstatic void 338273006Spfgdb_command(struct command **last_cmdp, struct command_table *cmd_table, 339273006Spfg int dopager) 3404Srgrimes{ 341183054Ssam struct command *cmd = NULL; 3424Srgrimes int t; 3434Srgrimes char modif[TOK_STRING_SIZE]; 3444Srgrimes db_expr_t addr, count; 345283248Spfg bool have_addr = false; 3464Srgrimes int result; 3474Srgrimes 3484Srgrimes t = db_read_token(); 3494Srgrimes if (t == tEOL) { 3504Srgrimes /* empty line repeats last command, at 'next' */ 3514Srgrimes cmd = *last_cmdp; 3524Srgrimes addr = (db_expr_t)db_next; 353283088Spfg have_addr = false; 3544Srgrimes count = 1; 3554Srgrimes modif[0] = '\0'; 3564Srgrimes } 3574Srgrimes else if (t == tEXCL) { 358283248Spfg db_fncall((db_expr_t)0, (bool)false, (db_expr_t)0, (char *)0); 3594Srgrimes return; 3604Srgrimes } 3614Srgrimes else if (t != tIDENT) { 362324267Strasz db_printf("Unrecognized input; use \"help\" " 363324267Strasz "to list available commands\n"); 3644Srgrimes db_flush_lex(); 3654Srgrimes return; 3664Srgrimes } 3674Srgrimes else { 3684Srgrimes /* 3694Srgrimes * Search for command 3704Srgrimes */ 3714Srgrimes while (cmd_table) { 3724Srgrimes result = db_cmd_search(db_tok_string, 3734Srgrimes cmd_table, 3744Srgrimes &cmd); 3754Srgrimes switch (result) { 3764Srgrimes case CMD_NONE: 377324265Strasz db_printf("No such command; use \"help\" " 378324265Strasz "to list available commands\n"); 3794Srgrimes db_flush_lex(); 3804Srgrimes return; 3814Srgrimes case CMD_AMBIGUOUS: 3824Srgrimes db_printf("Ambiguous\n"); 3834Srgrimes db_flush_lex(); 3844Srgrimes return; 3854Srgrimes case CMD_HELP: 386324265Strasz if (cmd_table == &db_cmd_table) { 387324265Strasz db_printf("This is ddb(4), the kernel debugger; " 388324265Strasz "see http://man.freebsd.org/ddb/4 for help.\n"); 389324265Strasz db_printf("Use \"bt\" for backtrace, \"dump\" for " 390324265Strasz "kernel core dump, \"reset\" to reboot.\n"); 391324265Strasz db_printf("Available commands:\n"); 392324265Strasz } 393156412Sjhb db_cmd_list(cmd_table); 3944Srgrimes db_flush_lex(); 3954Srgrimes return; 3964Srgrimes default: 3974Srgrimes break; 3984Srgrimes } 399156412Sjhb if ((cmd_table = cmd->more) != NULL) { 4004Srgrimes t = db_read_token(); 4014Srgrimes if (t != tIDENT) { 402324265Strasz db_printf("Subcommand required; " 403324265Strasz "available subcommands:\n"); 404156412Sjhb db_cmd_list(cmd_table); 4054Srgrimes db_flush_lex(); 4064Srgrimes return; 4074Srgrimes } 4084Srgrimes } 4094Srgrimes } 4104Srgrimes 4114Srgrimes if ((cmd->flag & CS_OWN) == 0) { 4124Srgrimes /* 4134Srgrimes * Standard syntax: 4144Srgrimes * command [/modifier] [addr] [,count] 4154Srgrimes */ 4164Srgrimes t = db_read_token(); 4174Srgrimes if (t == tSLASH) { 4184Srgrimes t = db_read_token(); 4194Srgrimes if (t != tIDENT) { 4204Srgrimes db_printf("Bad modifier\n"); 4214Srgrimes db_flush_lex(); 4224Srgrimes return; 4234Srgrimes } 4244Srgrimes db_strcpy(modif, db_tok_string); 4254Srgrimes } 4264Srgrimes else { 4274Srgrimes db_unread_token(t); 4284Srgrimes modif[0] = '\0'; 4294Srgrimes } 4304Srgrimes 4314Srgrimes if (db_expression(&addr)) { 4324Srgrimes db_dot = (db_addr_t) addr; 4334Srgrimes db_last_addr = db_dot; 434283088Spfg have_addr = true; 4354Srgrimes } 4364Srgrimes else { 4374Srgrimes addr = (db_expr_t) db_dot; 438283088Spfg have_addr = false; 4394Srgrimes } 4404Srgrimes t = db_read_token(); 4414Srgrimes if (t == tCOMMA) { 4424Srgrimes if (!db_expression(&count)) { 4434Srgrimes db_printf("Count missing\n"); 4444Srgrimes db_flush_lex(); 4454Srgrimes return; 4464Srgrimes } 4474Srgrimes } 4484Srgrimes else { 4494Srgrimes db_unread_token(t); 4504Srgrimes count = -1; 4514Srgrimes } 4524Srgrimes if ((cmd->flag & CS_MORE) == 0) { 4534Srgrimes db_skip_to_eol(); 4544Srgrimes } 4554Srgrimes } 4564Srgrimes } 4574Srgrimes *last_cmdp = cmd; 458298073Spfg if (cmd != NULL) { 4594Srgrimes /* 4604Srgrimes * Execute the command. 4614Srgrimes */ 462174914Srwatson if (dopager) 463174914Srwatson db_enable_pager(); 464174914Srwatson else 465174914Srwatson db_disable_pager(); 4664Srgrimes (*cmd->fcn)(addr, have_addr, count, modif); 467174914Srwatson if (dopager) 468174914Srwatson db_disable_pager(); 4694Srgrimes 4704Srgrimes if (cmd->flag & CS_SET_DOT) { 4714Srgrimes /* 4724Srgrimes * If command changes dot, set dot to 4734Srgrimes * previous address displayed (if 'ed' style). 4744Srgrimes */ 4754Srgrimes if (db_ed_style) { 4764Srgrimes db_dot = db_prev; 4774Srgrimes } 4784Srgrimes else { 4794Srgrimes db_dot = db_next; 4804Srgrimes } 4814Srgrimes } 4824Srgrimes else { 4834Srgrimes /* 4844Srgrimes * If command does not change dot, 4854Srgrimes * set 'next' location to be the same. 4864Srgrimes */ 4874Srgrimes db_next = db_dot; 4884Srgrimes } 4894Srgrimes } 4904Srgrimes} 4914Srgrimes 4924Srgrimes/* 49333296Sbde * At least one non-optional command must be implemented using 49433296Sbde * DB_COMMAND() so that db_cmd_set gets created. Here is one. 49533296Sbde */ 49633296SbdeDB_COMMAND(panic, db_panic) 4976204Sphk{ 498160505Sjhb db_disable_pager(); 4997170Sdg panic("from debugger"); 5006204Sphk} 5016204Sphk 5026204Sphkvoid 503273006Spfgdb_command_loop(void) 5044Srgrimes{ 5054Srgrimes /* 5064Srgrimes * Initialize 'prev' and 'next' to dot. 5074Srgrimes */ 5084Srgrimes db_prev = db_dot; 5094Srgrimes db_next = db_dot; 5104Srgrimes 5114Srgrimes db_cmd_loop_done = 0; 5124Srgrimes while (!db_cmd_loop_done) { 5134Srgrimes if (db_print_position() != 0) 5144Srgrimes db_printf("\n"); 5154Srgrimes 5164Srgrimes db_printf("db> "); 5174Srgrimes (void) db_read_line(); 5184Srgrimes 519183054Ssam db_command(&db_last_command, &db_cmd_table, /* dopager */ 1); 5204Srgrimes } 5214Srgrimes} 5224Srgrimes 523174914Srwatson/* 524174914Srwatson * Execute a command on behalf of a script. The caller is responsible for 525174914Srwatson * making sure that the command string is < DB_MAXLINE or it will be 526174914Srwatson * truncated. 527174914Srwatson * 528174914Srwatson * XXXRW: Runs by injecting faked input into DDB input stream; it would be 529174914Srwatson * nicer to use an alternative approach that didn't mess with the previous 530174914Srwatson * command buffer. 531174914Srwatson */ 5324Srgrimesvoid 533174914Srwatsondb_command_script(const char *command) 534174914Srwatson{ 535174914Srwatson db_prev = db_next = db_dot; 536174914Srwatson db_inject_line(command); 537183054Ssam db_command(&db_last_command, &db_cmd_table, /* dopager */ 0); 538174914Srwatson} 539174914Srwatson 540174914Srwatsonvoid 541273006Spfgdb_error(const char *s) 5424Srgrimes{ 5434Srgrimes if (s) 54479884Skris db_printf("%s", s); 5454Srgrimes db_flush_lex(); 546131952Smarcel kdb_reenter(); 5474Srgrimes} 5484Srgrimes 549222801Smarcelstatic void 550283248Spfgdb_dump(db_expr_t dummy, bool dummy2, db_expr_t dummy3, char *dummy4) 551222801Smarcel{ 552222801Smarcel int error; 5534Srgrimes 554242424Salfred if (textdump_pending) { 555242424Salfred db_printf("textdump_pending set.\n" 556242424Salfred "run \"textdump unset\" first or \"textdump dump\" for a textdump.\n"); 557242424Salfred return; 558242424Salfred } 559283088Spfg error = doadump(false); 560222801Smarcel if (error) { 561222801Smarcel db_printf("Cannot dump: "); 562222801Smarcel switch (error) { 563222801Smarcel case EBUSY: 564222801Smarcel db_printf("debugger got invoked while dumping.\n"); 565222801Smarcel break; 566222801Smarcel case ENXIO: 567222801Smarcel db_printf("no dump device specified.\n"); 568222801Smarcel break; 569222801Smarcel default: 570222801Smarcel db_printf("unknown error (error=%d).\n", error); 571222801Smarcel break; 572222801Smarcel } 573222801Smarcel } 574222801Smarcel} 575222801Smarcel 5764Srgrimes/* 5774Srgrimes * Call random function: 5784Srgrimes * !expr(arg,arg,arg) 5794Srgrimes */ 580147745Smarcel 581147745Smarcel/* The generic implementation supports a maximum of 10 arguments. */ 582147745Smarceltypedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t, 583147745Smarcel db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t); 584147745Smarcel 585147745Smarcelstatic __inline int 586147745Smarceldb_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[]) 587147745Smarcel{ 588147745Smarcel __db_f *f = (__db_f *)addr; 589147745Smarcel 590147745Smarcel if (nargs > 10) { 591147745Smarcel db_printf("Too many arguments (max 10)\n"); 592147745Smarcel return (0); 593147745Smarcel } 594147745Smarcel *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5], 595147745Smarcel args[6], args[7], args[8], args[9]); 596147745Smarcel return (1); 597147745Smarcel} 598147745Smarcel 59912515Sphkstatic void 600283248Spfgdb_fncall(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 6014Srgrimes{ 6024Srgrimes db_expr_t fn_addr; 603147745Smarcel db_expr_t args[DB_MAXARGS]; 6044Srgrimes int nargs = 0; 6054Srgrimes db_expr_t retval; 6064Srgrimes int t; 6074Srgrimes 6084Srgrimes if (!db_expression(&fn_addr)) { 6094Srgrimes db_printf("Bad function\n"); 6104Srgrimes db_flush_lex(); 6114Srgrimes return; 6124Srgrimes } 6134Srgrimes 6144Srgrimes t = db_read_token(); 6154Srgrimes if (t == tLPAREN) { 6164Srgrimes if (db_expression(&args[0])) { 6174Srgrimes nargs++; 6184Srgrimes while ((t = db_read_token()) == tCOMMA) { 619147745Smarcel if (nargs == DB_MAXARGS) { 620147745Smarcel db_printf("Too many arguments (max %d)\n", DB_MAXARGS); 6214Srgrimes db_flush_lex(); 6224Srgrimes return; 6234Srgrimes } 6244Srgrimes if (!db_expression(&args[nargs])) { 6254Srgrimes db_printf("Argument missing\n"); 6264Srgrimes db_flush_lex(); 6274Srgrimes return; 6284Srgrimes } 6294Srgrimes nargs++; 6304Srgrimes } 6314Srgrimes db_unread_token(t); 6324Srgrimes } 6334Srgrimes if (db_read_token() != tRPAREN) { 634324267Strasz db_printf("Mismatched parens\n"); 6354Srgrimes db_flush_lex(); 6364Srgrimes return; 6374Srgrimes } 6384Srgrimes } 6394Srgrimes db_skip_to_eol(); 640160505Sjhb db_disable_pager(); 6414Srgrimes 642147745Smarcel if (DB_CALL(fn_addr, &retval, nargs, args)) 643147745Smarcel db_printf("= %#lr\n", (long)retval); 6444Srgrimes} 64517848Spst 64617848Spststatic void 647283248Spfgdb_halt(db_expr_t dummy, bool dummy2, db_expr_t dummy3, char *dummy4) 648163192Sbde{ 649163192Sbde 650163192Sbde cpu_halt(); 651163192Sbde} 652163192Sbde 653163192Sbdestatic void 654283248Spfgdb_kill(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 65586998Sdd{ 65686998Sdd db_expr_t old_radix, pid, sig; 65786998Sdd struct proc *p; 65886998Sdd 659272958Spfg#define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0) 66086998Sdd 66186998Sdd /* 66286998Sdd * PIDs and signal numbers are typically represented in base 66386998Sdd * 10, so make that the default here. It can, of course, be 66486998Sdd * overridden by specifying a prefix. 66586998Sdd */ 66686998Sdd old_radix = db_radix; 66786998Sdd db_radix = 10; 66886998Sdd /* Retrieve arguments. */ 66986998Sdd if (!db_expression(&sig)) 67086998Sdd DB_ERROR(("Missing signal number\n")); 67186998Sdd if (!db_expression(&pid)) 67286998Sdd DB_ERROR(("Missing process ID\n")); 67386998Sdd db_skip_to_eol(); 674209934Skib if (!_SIG_VALID(sig)) 67586998Sdd DB_ERROR(("Signal number out of range\n")); 67686998Sdd 67786998Sdd /* 67886998Sdd * Find the process in question. allproc_lock is not needed 67986998Sdd * since we're in DDB. 68086998Sdd */ 68186998Sdd /* sx_slock(&allproc_lock); */ 682166074Sdelphij FOREACH_PROC_IN_SYSTEM(p) 68386998Sdd if (p->p_pid == pid) 68486998Sdd break; 68586998Sdd /* sx_sunlock(&allproc_lock); */ 68686998Sdd if (p == NULL) 68789442Smjacob DB_ERROR(("Can't find process with pid %ld\n", (long) pid)); 68886998Sdd 68986998Sdd /* If it's already locked, bail; otherwise, do the deed. */ 69086998Sdd if (PROC_TRYLOCK(p) == 0) 69189442Smjacob DB_ERROR(("Can't lock process with pid %ld\n", (long) pid)); 69286998Sdd else { 693199355Skib pksignal(p, sig, NULL); 69486998Sdd PROC_UNLOCK(p); 69586998Sdd } 69686998Sdd 69786998Sddout: 69886998Sdd db_radix = old_radix; 69986998Sdd#undef DB_ERROR 70086998Sdd} 70186998Sdd 702208509Sbz/* 703208509Sbz * Reboot. In case there is an additional argument, take it as delay in 704208509Sbz * seconds. Default to 15s if we cannot parse it and make sure we will 705208509Sbz * never wait longer than 1 week. Some code is similar to 706208509Sbz * kern_shutdown.c:shutdown_panic(). 707208509Sbz */ 708208509Sbz#ifndef DB_RESET_MAXDELAY 709208509Sbz#define DB_RESET_MAXDELAY (3600 * 24 * 7) 710208509Sbz#endif 711208509Sbz 71286998Sddstatic void 713283248Spfgdb_reset(db_expr_t addr, bool have_addr, db_expr_t count __unused, 714208509Sbz char *modif __unused) 71585944Speter{ 716208509Sbz int delay, loop; 71785944Speter 718208509Sbz if (have_addr) { 719208509Sbz delay = (int)db_hex2dec(addr); 720208509Sbz 721208509Sbz /* If we parse to fail, use 15s. */ 722208509Sbz if (delay == -1) 723208509Sbz delay = 15; 724208509Sbz 725208509Sbz /* Cap at one week. */ 726208509Sbz if ((uintmax_t)delay > (uintmax_t)DB_RESET_MAXDELAY) 727208509Sbz delay = DB_RESET_MAXDELAY; 728208509Sbz 729208509Sbz db_printf("Automatic reboot in %d seconds - " 730208509Sbz "press a key on the console to abort\n", delay); 731208509Sbz for (loop = delay * 10; loop > 0; --loop) { 732208509Sbz DELAY(1000 * 100); /* 1/10th second */ 733208509Sbz /* Did user type a key? */ 734208509Sbz if (cncheckc() != -1) 735208509Sbz return; 736208509Sbz } 737208509Sbz } 738208509Sbz 73985944Speter cpu_reset(); 74085944Speter} 741126399Sphk 742126399Sphkstatic void 743283248Spfgdb_watchdog(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 744126399Sphk{ 745220362Sattilio db_expr_t old_radix, tout; 746220362Sattilio int err, i; 747126399Sphk 748220362Sattilio old_radix = db_radix; 749220362Sattilio db_radix = 10; 750220362Sattilio err = db_expression(&tout); 751220362Sattilio db_skip_to_eol(); 752220362Sattilio db_radix = old_radix; 753126399Sphk 754220362Sattilio /* If no argument is provided the watchdog will just be disabled. */ 755220362Sattilio if (err == 0) { 756220362Sattilio db_printf("No argument provided, disabling watchdog\n"); 757220362Sattilio tout = 0; 758220362Sattilio } else if ((tout & WD_INTERVAL) == WD_TO_NEVER) { 759220362Sattilio db_error("Out of range watchdog interval\n"); 760220362Sattilio return; 761220362Sattilio } 762220362Sattilio EVENTHANDLER_INVOKE(watchdog_list, tout, &i); 763126399Sphk} 764132002Smarcel 765132002Smarcelstatic void 766283248Spfgdb_gdb(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 767132002Smarcel{ 768132002Smarcel 769218825Smdf if (kdb_dbbe_select("gdb") != 0) { 770132002Smarcel db_printf("The remote GDB backend could not be selected.\n"); 771218825Smdf return; 772218825Smdf } 773218825Smdf /* 774218825Smdf * Mark that we are done in the debugger. kdb_trap() 775218825Smdf * should re-enter with the new backend. 776218825Smdf */ 777218825Smdf db_cmd_loop_done = 1; 778218825Smdf db_printf("(ctrl-c will return control to ddb)\n"); 779132002Smarcel} 780132482Smarcel 781132482Smarcelstatic void 782283248Spfgdb_stack_trace(db_expr_t tid, bool hastid, db_expr_t count, char *modif) 783132482Smarcel{ 784132482Smarcel struct thread *td; 785132482Smarcel db_expr_t radix; 786138038Srwatson pid_t pid; 787132482Smarcel int t; 788132482Smarcel 789132482Smarcel /* 790132482Smarcel * We parse our own arguments. We don't like the default radix. 791132482Smarcel */ 792132482Smarcel radix = db_radix; 793132482Smarcel db_radix = 10; 794132482Smarcel hastid = db_expression(&tid); 795132482Smarcel t = db_read_token(); 796132482Smarcel if (t == tCOMMA) { 797132482Smarcel if (!db_expression(&count)) { 798132482Smarcel db_printf("Count missing\n"); 799132482Smarcel db_flush_lex(); 800132482Smarcel return; 801132482Smarcel } 802132482Smarcel } else { 803132482Smarcel db_unread_token(t); 804132482Smarcel count = -1; 805132482Smarcel } 806132482Smarcel db_skip_to_eol(); 807132482Smarcel db_radix = radix; 808132482Smarcel 809132482Smarcel if (hastid) { 810132482Smarcel td = kdb_thr_lookup((lwpid_t)tid); 811132482Smarcel if (td == NULL) 812132482Smarcel td = kdb_thr_from_pid((pid_t)tid); 813132482Smarcel if (td == NULL) { 814132482Smarcel db_printf("Thread %d not found\n", (int)tid); 815132482Smarcel return; 816132482Smarcel } 817132482Smarcel } else 818132482Smarcel td = kdb_thread; 819138038Srwatson if (td->td_proc != NULL) 820138038Srwatson pid = td->td_proc->p_pid; 821138038Srwatson else 822138038Srwatson pid = -1; 823138038Srwatson db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td); 824132482Smarcel db_trace_thread(td, count); 825132482Smarcel} 826150819Srwatson 827150819Srwatsonstatic void 828283248Spfgdb_stack_trace_all(db_expr_t dummy, bool dummy2, db_expr_t dummy3, 829150819Srwatson char *dummy4) 830150819Srwatson{ 831150819Srwatson struct proc *p; 832150819Srwatson struct thread *td; 833163909Skib jmp_buf jb; 834163909Skib void *prev_jb; 835150819Srwatson 836166074Sdelphij FOREACH_PROC_IN_SYSTEM(p) { 837163909Skib prev_jb = kdb_jmpbuf(jb); 838163909Skib if (setjmp(jb) == 0) { 839163909Skib FOREACH_THREAD_IN_PROC(p, td) { 840163909Skib db_printf("\nTracing command %s pid %d tid %ld td %p\n", 841163909Skib p->p_comm, p->p_pid, (long)td->td_tid, td); 842163909Skib db_trace_thread(td, -1); 843163909Skib if (db_pager_quit) { 844163909Skib kdb_jmpbuf(prev_jb); 845163909Skib return; 846163909Skib } 847163909Skib } 848150819Srwatson } 849163909Skib kdb_jmpbuf(prev_jb); 850150819Srwatson } 851150819Srwatson} 852208509Sbz 853208509Sbz/* 854208509Sbz * Take the parsed expression value from the command line that was parsed 855208509Sbz * as a hexadecimal value and convert it as if the expression was parsed 856208509Sbz * as a decimal value. Returns -1 if the expression was not a valid 857208509Sbz * decimal value. 858208509Sbz */ 859208509Sbzdb_expr_t 860208509Sbzdb_hex2dec(db_expr_t expr) 861208509Sbz{ 862208509Sbz uintptr_t x, y; 863208509Sbz db_expr_t val; 864208509Sbz 865208509Sbz y = 1; 866208509Sbz val = 0; 867208509Sbz x = expr; 868208509Sbz while (x != 0) { 869208509Sbz if (x % 16 > 9) 870208509Sbz return (-1); 871208509Sbz val += (x % 16) * (y); 872208509Sbz x >>= 4; 873208509Sbz y *= 10; 874208509Sbz } 875208509Sbz return (val); 876208509Sbz} 877