db_command.c revision 156412
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: head/sys/ddb/db_command.c 156412 2006-03-07 22:17:06Z jhb $"); 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> 47126399Sphk#include <sys/watchdog.h> 4812734Sbde 492056Swollman#include <ddb/ddb.h> 5012473Sbde#include <ddb/db_command.h> 514Srgrimes#include <ddb/db_lex.h> 524Srgrimes#include <ddb/db_output.h> 534Srgrimes 54118990Smarcel#include <machine/cpu.h> 5579418Sjulian#include <machine/setjmp.h> 564Srgrimes 574Srgrimes/* 584Srgrimes * Exported global variables 594Srgrimes */ 604Srgrimesboolean_t db_cmd_loop_done; 6118296Sbdedb_addr_t db_dot; 624Srgrimesdb_addr_t db_last_addr; 634Srgrimesdb_addr_t db_prev; 644Srgrimesdb_addr_t db_next; 654Srgrimes 6678161SpeterSET_DECLARE(db_cmd_set, struct command); 6778161SpeterSET_DECLARE(db_show_cmd_set, struct command); 6878161Speter 6912515Sphkstatic db_cmdfcn_t db_fncall; 70132002Smarcelstatic db_cmdfcn_t db_gdb; 7186998Sddstatic db_cmdfcn_t db_kill; 7285944Speterstatic db_cmdfcn_t db_reset; 73132482Smarcelstatic db_cmdfcn_t db_stack_trace; 74150819Srwatsonstatic db_cmdfcn_t db_stack_trace_all; 75126399Sphkstatic db_cmdfcn_t db_watchdog; 7617848Spst 77148919Sobrien/* 78148919Sobrien * 'show' commands 79148919Sobrien */ 8018296Sbde 81148919Sobrienstatic struct command db_show_all_cmds[] = { 82148919Sobrien { "procs", db_ps, 0, 0 }, 83148919Sobrien { (char *)0 } 84148919Sobrien}; 85148919Sobrien 86156412Sjhbstatic struct command_table db_show_all_table = { 87156412Sjhb db_show_all_cmds 88156412Sjhb}; 89156412Sjhb 90148919Sobrienstatic struct command db_show_cmds[] = { 91156412Sjhb { "all", 0, 0, &db_show_all_table }, 92148919Sobrien { "registers", db_show_regs, 0, 0 }, 93148919Sobrien { "breaks", db_listbreak_cmd, 0, 0 }, 94148919Sobrien { "threads", db_show_threads, 0, 0 }, 95148919Sobrien { (char *)0, } 96148919Sobrien}; 97148919Sobrien 98156412Sjhbstatic struct command_table db_show_table = { 99156412Sjhb db_show_cmds, 100156412Sjhb SET_BEGIN(db_show_cmd_set), 101156412Sjhb SET_LIMIT(db_show_cmd_set) 102156412Sjhb}; 103156412Sjhb 104156412Sjhbstatic struct command db_commands[] = { 105148919Sobrien { "print", db_print_cmd, 0, 0 }, 106148919Sobrien { "p", db_print_cmd, 0, 0 }, 107148919Sobrien { "examine", db_examine_cmd, CS_SET_DOT, 0 }, 108148919Sobrien { "x", db_examine_cmd, CS_SET_DOT, 0 }, 109148919Sobrien { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 }, 110148919Sobrien { "set", db_set_cmd, CS_OWN, 0 }, 111148919Sobrien { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 }, 112148919Sobrien { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 }, 113148919Sobrien { "delete", db_delete_cmd, 0, 0 }, 114148919Sobrien { "d", db_delete_cmd, 0, 0 }, 115148919Sobrien { "break", db_breakpoint_cmd, 0, 0 }, 116148919Sobrien { "dwatch", db_deletewatch_cmd, 0, 0 }, 117148919Sobrien { "watch", db_watchpoint_cmd, CS_MORE,0 }, 118148919Sobrien { "dhwatch", db_deletehwatch_cmd, 0, 0 }, 119148919Sobrien { "hwatch", db_hwatchpoint_cmd, 0, 0 }, 120148919Sobrien { "step", db_single_step_cmd, 0, 0 }, 121148919Sobrien { "s", db_single_step_cmd, 0, 0 }, 122148919Sobrien { "continue", db_continue_cmd, 0, 0 }, 123148919Sobrien { "c", db_continue_cmd, 0, 0 }, 124148919Sobrien { "until", db_trace_until_call_cmd,0, 0 }, 125148919Sobrien { "next", db_trace_until_matching_cmd,0, 0 }, 126148919Sobrien { "match", db_trace_until_matching_cmd,0, 0 }, 127148919Sobrien { "trace", db_stack_trace, CS_OWN, 0 }, 128151622Sjhb { "alltrace", db_stack_trace_all, 0, 0 }, 129148919Sobrien { "where", db_stack_trace, CS_OWN, 0 }, 130151622Sjhb { "bt", db_stack_trace, CS_OWN, 0 }, 131148919Sobrien { "call", db_fncall, CS_OWN, 0 }, 132156412Sjhb { "show", 0, 0, &db_show_table }, 133148919Sobrien { "ps", db_ps, 0, 0 }, 134148919Sobrien { "gdb", db_gdb, 0, 0 }, 135148919Sobrien { "reset", db_reset, 0, 0 }, 136148919Sobrien { "kill", db_kill, CS_OWN, 0 }, 137148919Sobrien { "watchdog", db_watchdog, 0, 0 }, 138148919Sobrien { "thread", db_set_thread, CS_OWN, 0 }, 139148919Sobrien { (char *)0, } 140148919Sobrien}; 141148919Sobrien 142156412Sjhbstatic struct command_table db_command_table = { 143156412Sjhb db_commands, 144156412Sjhb SET_BEGIN(db_cmd_set), 145156412Sjhb SET_LIMIT(db_cmd_set) 146156412Sjhb}; 147156412Sjhb 148148919Sobrienstatic struct command *db_last_command = 0; 149148919Sobrien 1504Srgrimes/* 1514Srgrimes * if 'ed' style: 'dot' is set at start of last item printed, 1524Srgrimes * and '+' points to next line. 1534Srgrimes * Otherwise: 'dot' points to next item, '..' points to last. 1544Srgrimes */ 15512515Sphkstatic boolean_t db_ed_style = TRUE; 1564Srgrimes 1574Srgrimes/* 1584Srgrimes * Utility routine - discard tokens through end-of-line. 1594Srgrimes */ 1604Srgrimesvoid 1614Srgrimesdb_skip_to_eol() 1624Srgrimes{ 1634Srgrimes int t; 1644Srgrimes do { 1654Srgrimes t = db_read_token(); 1664Srgrimes } while (t != tEOL); 1674Srgrimes} 1684Srgrimes 1694Srgrimes/* 1704Srgrimes * Results of command search. 1714Srgrimes */ 1724Srgrimes#define CMD_UNIQUE 0 1734Srgrimes#define CMD_FOUND 1 1744Srgrimes#define CMD_NONE 2 1754Srgrimes#define CMD_AMBIGUOUS 3 1764Srgrimes#define CMD_HELP 4 1774Srgrimes 178156412Sjhbstatic void db_cmd_match(char *name, struct command *cmd, 179156412Sjhb struct command **cmdp, int *resultp); 180156412Sjhbstatic void db_cmd_list(struct command_table *table); 181156412Sjhbstatic int db_cmd_search(char *name, struct command_table *table, 182156412Sjhb struct command **cmdp); 18392756Salfredstatic void db_command(struct command **last_cmdp, 184156412Sjhb struct command_table *cmd_table); 18512473Sbde 1864Srgrimes/* 187156412Sjhb * Helper function to match a single command. 1884Srgrimes */ 189156412Sjhbstatic void 190156412Sjhbdb_cmd_match(name, cmd, cmdp, resultp) 1914Srgrimes char * name; 192156412Sjhb struct command *cmd; 1934Srgrimes struct command **cmdp; /* out */ 194156412Sjhb int * resultp; 1954Srgrimes{ 196156412Sjhb char *lp, *rp; 197156412Sjhb int c; 1984Srgrimes 199156412Sjhb lp = name; 200156412Sjhb rp = cmd->name; 201156412Sjhb while ((c = *lp) == *rp) { 2024Srgrimes if (c == 0) { 203156412Sjhb /* complete match */ 204156412Sjhb *cmdp = cmd; 205156412Sjhb *resultp = CMD_UNIQUE; 206156412Sjhb return; 2074Srgrimes } 2084Srgrimes lp++; 2094Srgrimes rp++; 210156412Sjhb } 211156412Sjhb if (c == 0) { 2124Srgrimes /* end of name, not end of command - 2134Srgrimes partial match */ 214156412Sjhb if (*resultp == CMD_FOUND) { 215156412Sjhb *resultp = CMD_AMBIGUOUS; 216156412Sjhb /* but keep looking for a full match - 217156412Sjhb this lets us match single letters */ 218156412Sjhb } else { 219156412Sjhb *cmdp = cmd; 220156412Sjhb *resultp = CMD_FOUND; 2214Srgrimes } 2224Srgrimes } 223156412Sjhb} 22418296Sbde 225156412Sjhb/* 226156412Sjhb * Search for command prefix. 227156412Sjhb */ 228156412Sjhbstatic int 229156412Sjhbdb_cmd_search(name, table, cmdp) 230156412Sjhb char * name; 231156412Sjhb struct command_table *table; 232156412Sjhb struct command **cmdp; /* out */ 233156412Sjhb{ 234156412Sjhb struct command *cmd; 235156412Sjhb struct command **aux_cmdp; 236156412Sjhb int result = CMD_NONE; 237156412Sjhb 238156412Sjhb for (cmd = table->table; cmd->name != 0; cmd++) { 239156412Sjhb db_cmd_match(name, cmd, cmdp, &result); 240156412Sjhb if (result == CMD_UNIQUE) 24118296Sbde return (CMD_UNIQUE); 242156412Sjhb } 243156412Sjhb if (table->aux_tablep != NULL) 244156412Sjhb for (aux_cmdp = table->aux_tablep; 245156412Sjhb aux_cmdp < table->aux_tablep_end; 246156412Sjhb aux_cmdp++) { 247156412Sjhb db_cmd_match(name, *aux_cmdp, cmdp, &result); 248156412Sjhb if (result == CMD_UNIQUE) 249156412Sjhb return (CMD_UNIQUE); 25018296Sbde } 2514Srgrimes if (result == CMD_NONE) { 252156412Sjhb /* check for 'help' */ 2534Srgrimes if (name[0] == 'h' && name[1] == 'e' 2544Srgrimes && name[2] == 'l' && name[3] == 'p') 2554Srgrimes result = CMD_HELP; 2564Srgrimes } 2574Srgrimes return (result); 2584Srgrimes} 2594Srgrimes 26012515Sphkstatic void 261156412Sjhbdb_cmd_list(table) 262156412Sjhb struct command_table *table; 2634Srgrimes{ 2644Srgrimes register struct command *cmd; 26518296Sbde register struct command **aux_cmdp; 2664Srgrimes 267156412Sjhb for (cmd = table->table; cmd->name != 0; cmd++) { 2684Srgrimes db_printf("%-12s", cmd->name); 2694Srgrimes db_end_line(); 2704Srgrimes } 271156412Sjhb if (table->aux_tablep == NULL) 27218296Sbde return; 273156412Sjhb for (aux_cmdp = table->aux_tablep; aux_cmdp < table->aux_tablep_end; 274156412Sjhb aux_cmdp++) { 27518296Sbde db_printf("%-12s", (*aux_cmdp)->name); 27618296Sbde db_end_line(); 27718296Sbde } 2784Srgrimes} 2794Srgrimes 28012515Sphkstatic void 281156412Sjhbdb_command(last_cmdp, cmd_table) 2824Srgrimes struct command **last_cmdp; /* IN_OUT */ 283156412Sjhb struct command_table *cmd_table; 2844Srgrimes{ 2854Srgrimes struct command *cmd; 2864Srgrimes int t; 2874Srgrimes char modif[TOK_STRING_SIZE]; 2884Srgrimes db_expr_t addr, count; 289798Swollman boolean_t have_addr = FALSE; 2904Srgrimes int result; 2914Srgrimes 2924Srgrimes t = db_read_token(); 2934Srgrimes if (t == tEOL) { 2944Srgrimes /* empty line repeats last command, at 'next' */ 2954Srgrimes cmd = *last_cmdp; 2964Srgrimes addr = (db_expr_t)db_next; 2974Srgrimes have_addr = FALSE; 2984Srgrimes count = 1; 2994Srgrimes modif[0] = '\0'; 3004Srgrimes } 3014Srgrimes else if (t == tEXCL) { 30210348Sbde db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0); 3034Srgrimes return; 3044Srgrimes } 3054Srgrimes else if (t != tIDENT) { 3064Srgrimes db_printf("?\n"); 3074Srgrimes db_flush_lex(); 3084Srgrimes return; 3094Srgrimes } 3104Srgrimes else { 3114Srgrimes /* 3124Srgrimes * Search for command 3134Srgrimes */ 3144Srgrimes while (cmd_table) { 3154Srgrimes result = db_cmd_search(db_tok_string, 3164Srgrimes cmd_table, 3174Srgrimes &cmd); 3184Srgrimes switch (result) { 3194Srgrimes case CMD_NONE: 3204Srgrimes db_printf("No such command\n"); 3214Srgrimes db_flush_lex(); 3224Srgrimes return; 3234Srgrimes case CMD_AMBIGUOUS: 3244Srgrimes db_printf("Ambiguous\n"); 3254Srgrimes db_flush_lex(); 3264Srgrimes return; 3274Srgrimes case CMD_HELP: 328156412Sjhb db_cmd_list(cmd_table); 3294Srgrimes db_flush_lex(); 3304Srgrimes return; 3314Srgrimes default: 3324Srgrimes break; 3334Srgrimes } 334156412Sjhb if ((cmd_table = cmd->more) != NULL) { 3354Srgrimes t = db_read_token(); 3364Srgrimes if (t != tIDENT) { 337156412Sjhb db_cmd_list(cmd_table); 3384Srgrimes db_flush_lex(); 3394Srgrimes return; 3404Srgrimes } 3414Srgrimes } 3424Srgrimes } 3434Srgrimes 3444Srgrimes if ((cmd->flag & CS_OWN) == 0) { 3454Srgrimes /* 3464Srgrimes * Standard syntax: 3474Srgrimes * command [/modifier] [addr] [,count] 3484Srgrimes */ 3494Srgrimes t = db_read_token(); 3504Srgrimes if (t == tSLASH) { 3514Srgrimes t = db_read_token(); 3524Srgrimes if (t != tIDENT) { 3534Srgrimes db_printf("Bad modifier\n"); 3544Srgrimes db_flush_lex(); 3554Srgrimes return; 3564Srgrimes } 3574Srgrimes db_strcpy(modif, db_tok_string); 3584Srgrimes } 3594Srgrimes else { 3604Srgrimes db_unread_token(t); 3614Srgrimes modif[0] = '\0'; 3624Srgrimes } 3634Srgrimes 3644Srgrimes if (db_expression(&addr)) { 3654Srgrimes db_dot = (db_addr_t) addr; 3664Srgrimes db_last_addr = db_dot; 3674Srgrimes have_addr = TRUE; 3684Srgrimes } 3694Srgrimes else { 3704Srgrimes addr = (db_expr_t) db_dot; 3714Srgrimes have_addr = FALSE; 3724Srgrimes } 3734Srgrimes t = db_read_token(); 3744Srgrimes if (t == tCOMMA) { 3754Srgrimes if (!db_expression(&count)) { 3764Srgrimes db_printf("Count missing\n"); 3774Srgrimes db_flush_lex(); 3784Srgrimes return; 3794Srgrimes } 3804Srgrimes } 3814Srgrimes else { 3824Srgrimes db_unread_token(t); 3834Srgrimes count = -1; 3844Srgrimes } 3854Srgrimes if ((cmd->flag & CS_MORE) == 0) { 3864Srgrimes db_skip_to_eol(); 3874Srgrimes } 3884Srgrimes } 3894Srgrimes } 3904Srgrimes *last_cmdp = cmd; 3914Srgrimes if (cmd != 0) { 3924Srgrimes /* 3934Srgrimes * Execute the command. 3944Srgrimes */ 3954Srgrimes (*cmd->fcn)(addr, have_addr, count, modif); 396118268Sjhb db_setup_paging(NULL, NULL, -1); 3974Srgrimes 3984Srgrimes if (cmd->flag & CS_SET_DOT) { 3994Srgrimes /* 4004Srgrimes * If command changes dot, set dot to 4014Srgrimes * previous address displayed (if 'ed' style). 4024Srgrimes */ 4034Srgrimes if (db_ed_style) { 4044Srgrimes db_dot = db_prev; 4054Srgrimes } 4064Srgrimes else { 4074Srgrimes db_dot = db_next; 4084Srgrimes } 4094Srgrimes } 4104Srgrimes else { 4114Srgrimes /* 4124Srgrimes * If command does not change dot, 4134Srgrimes * set 'next' location to be the same. 4144Srgrimes */ 4154Srgrimes db_next = db_dot; 4164Srgrimes } 4174Srgrimes } 4184Srgrimes} 4194Srgrimes 4204Srgrimes/* 42133296Sbde * At least one non-optional command must be implemented using 42233296Sbde * DB_COMMAND() so that db_cmd_set gets created. Here is one. 42333296Sbde */ 42433296SbdeDB_COMMAND(panic, db_panic) 4256204Sphk{ 4267170Sdg panic("from debugger"); 4276204Sphk} 4286204Sphk 4296204Sphkvoid 4304Srgrimesdb_command_loop() 4314Srgrimes{ 4324Srgrimes /* 4334Srgrimes * Initialize 'prev' and 'next' to dot. 4344Srgrimes */ 4354Srgrimes db_prev = db_dot; 4364Srgrimes db_next = db_dot; 4374Srgrimes 4384Srgrimes db_cmd_loop_done = 0; 4394Srgrimes while (!db_cmd_loop_done) { 4404Srgrimes if (db_print_position() != 0) 4414Srgrimes db_printf("\n"); 4424Srgrimes 4434Srgrimes db_printf("db> "); 4444Srgrimes (void) db_read_line(); 4454Srgrimes 446156412Sjhb db_command(&db_last_command, &db_command_table); 4474Srgrimes } 4484Srgrimes} 4494Srgrimes 4504Srgrimesvoid 4514Srgrimesdb_error(s) 452103746Smarkm const char *s; 4534Srgrimes{ 4544Srgrimes if (s) 45579884Skris db_printf("%s", s); 4564Srgrimes db_flush_lex(); 457131952Smarcel kdb_reenter(); 4584Srgrimes} 4594Srgrimes 4604Srgrimes 4614Srgrimes/* 4624Srgrimes * Call random function: 4634Srgrimes * !expr(arg,arg,arg) 4644Srgrimes */ 465147745Smarcel 466147745Smarcel/* The generic implementation supports a maximum of 10 arguments. */ 467147745Smarceltypedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t, 468147745Smarcel db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t); 469147745Smarcel 470147745Smarcelstatic __inline int 471147745Smarceldb_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[]) 472147745Smarcel{ 473147745Smarcel __db_f *f = (__db_f *)addr; 474147745Smarcel 475147745Smarcel if (nargs > 10) { 476147745Smarcel db_printf("Too many arguments (max 10)\n"); 477147745Smarcel return (0); 478147745Smarcel } 479147745Smarcel *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5], 480147745Smarcel args[6], args[7], args[8], args[9]); 481147745Smarcel return (1); 482147745Smarcel} 483147745Smarcel 48412515Sphkstatic void 48510348Sbdedb_fncall(dummy1, dummy2, dummy3, dummy4) 48610348Sbde db_expr_t dummy1; 48710348Sbde boolean_t dummy2; 48810348Sbde db_expr_t dummy3; 48910348Sbde char * dummy4; 4904Srgrimes{ 4914Srgrimes db_expr_t fn_addr; 492147745Smarcel db_expr_t args[DB_MAXARGS]; 4934Srgrimes int nargs = 0; 4944Srgrimes db_expr_t retval; 4954Srgrimes int t; 4964Srgrimes 4974Srgrimes if (!db_expression(&fn_addr)) { 4984Srgrimes db_printf("Bad function\n"); 4994Srgrimes db_flush_lex(); 5004Srgrimes return; 5014Srgrimes } 5024Srgrimes 5034Srgrimes t = db_read_token(); 5044Srgrimes if (t == tLPAREN) { 5054Srgrimes if (db_expression(&args[0])) { 5064Srgrimes nargs++; 5074Srgrimes while ((t = db_read_token()) == tCOMMA) { 508147745Smarcel if (nargs == DB_MAXARGS) { 509147745Smarcel db_printf("Too many arguments (max %d)\n", DB_MAXARGS); 5104Srgrimes db_flush_lex(); 5114Srgrimes return; 5124Srgrimes } 5134Srgrimes if (!db_expression(&args[nargs])) { 5144Srgrimes db_printf("Argument missing\n"); 5154Srgrimes db_flush_lex(); 5164Srgrimes return; 5174Srgrimes } 5184Srgrimes nargs++; 5194Srgrimes } 5204Srgrimes db_unread_token(t); 5214Srgrimes } 5224Srgrimes if (db_read_token() != tRPAREN) { 5234Srgrimes db_printf("?\n"); 5244Srgrimes db_flush_lex(); 5254Srgrimes return; 5264Srgrimes } 5274Srgrimes } 5284Srgrimes db_skip_to_eol(); 5294Srgrimes 530147745Smarcel if (DB_CALL(fn_addr, &retval, nargs, args)) 531147745Smarcel db_printf("= %#lr\n", (long)retval); 5324Srgrimes} 53317848Spst 53417848Spststatic void 53586998Sdddb_kill(dummy1, dummy2, dummy3, dummy4) 53686998Sdd db_expr_t dummy1; 53786998Sdd boolean_t dummy2; 53886998Sdd db_expr_t dummy3; 53986998Sdd char * dummy4; 54086998Sdd{ 54186998Sdd db_expr_t old_radix, pid, sig; 54286998Sdd struct proc *p; 54386998Sdd 54486998Sdd#define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0) 54586998Sdd 54686998Sdd /* 54786998Sdd * PIDs and signal numbers are typically represented in base 54886998Sdd * 10, so make that the default here. It can, of course, be 54986998Sdd * overridden by specifying a prefix. 55086998Sdd */ 55186998Sdd old_radix = db_radix; 55286998Sdd db_radix = 10; 55386998Sdd /* Retrieve arguments. */ 55486998Sdd if (!db_expression(&sig)) 55586998Sdd DB_ERROR(("Missing signal number\n")); 55686998Sdd if (!db_expression(&pid)) 55786998Sdd DB_ERROR(("Missing process ID\n")); 55886998Sdd db_skip_to_eol(); 55986998Sdd if (sig < 0 || sig > _SIG_MAXSIG) 56086998Sdd DB_ERROR(("Signal number out of range\n")); 56186998Sdd 56286998Sdd /* 56386998Sdd * Find the process in question. allproc_lock is not needed 56486998Sdd * since we're in DDB. 56586998Sdd */ 56686998Sdd /* sx_slock(&allproc_lock); */ 56786998Sdd LIST_FOREACH(p, &allproc, p_list) 56886998Sdd if (p->p_pid == pid) 56986998Sdd break; 57086998Sdd /* sx_sunlock(&allproc_lock); */ 57186998Sdd if (p == NULL) 57289442Smjacob DB_ERROR(("Can't find process with pid %ld\n", (long) pid)); 57386998Sdd 57486998Sdd /* If it's already locked, bail; otherwise, do the deed. */ 57586998Sdd if (PROC_TRYLOCK(p) == 0) 57689442Smjacob DB_ERROR(("Can't lock process with pid %ld\n", (long) pid)); 57786998Sdd else { 57886998Sdd psignal(p, sig); 57986998Sdd PROC_UNLOCK(p); 58086998Sdd } 58186998Sdd 58286998Sddout: 58386998Sdd db_radix = old_radix; 58486998Sdd#undef DB_ERROR 58586998Sdd} 58686998Sdd 58786998Sddstatic void 58885944Speterdb_reset(dummy1, dummy2, dummy3, dummy4) 58985944Speter db_expr_t dummy1; 59085944Speter boolean_t dummy2; 59185944Speter db_expr_t dummy3; 59285944Speter char * dummy4; 59385944Speter{ 59485944Speter 59585944Speter cpu_reset(); 59685944Speter} 597126399Sphk 598126399Sphkstatic void 599126399Sphkdb_watchdog(dummy1, dummy2, dummy3, dummy4) 600126399Sphk db_expr_t dummy1; 601126399Sphk boolean_t dummy2; 602126399Sphk db_expr_t dummy3; 603126399Sphk char * dummy4; 604126399Sphk{ 605126399Sphk int i; 606126399Sphk 607126399Sphk /* 608126399Sphk * XXX: It might make sense to be able to set the watchdog to a 609126399Sphk * XXX: timeout here so that failure or hang as a result of subsequent 610126399Sphk * XXX: ddb commands could be recovered by a reset. 611126399Sphk */ 612126399Sphk 613126399Sphk EVENTHANDLER_INVOKE(watchdog_list, 0, &i); 614126399Sphk} 615132002Smarcel 616132002Smarcelstatic void 617132002Smarceldb_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4) 618132002Smarcel{ 619132002Smarcel 620132002Smarcel if (kdb_dbbe_select("gdb") != 0) 621132002Smarcel db_printf("The remote GDB backend could not be selected.\n"); 622132002Smarcel else 623132002Smarcel db_printf("Step to enter the remote GDB backend.\n"); 624132002Smarcel} 625132482Smarcel 626132482Smarcelstatic void 627132482Smarceldb_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif) 628132482Smarcel{ 629132482Smarcel struct thread *td; 630132482Smarcel db_expr_t radix; 631138038Srwatson pid_t pid; 632132482Smarcel int t; 633132482Smarcel 634132482Smarcel /* 635132482Smarcel * We parse our own arguments. We don't like the default radix. 636132482Smarcel */ 637132482Smarcel radix = db_radix; 638132482Smarcel db_radix = 10; 639132482Smarcel hastid = db_expression(&tid); 640132482Smarcel t = db_read_token(); 641132482Smarcel if (t == tCOMMA) { 642132482Smarcel if (!db_expression(&count)) { 643132482Smarcel db_printf("Count missing\n"); 644132482Smarcel db_flush_lex(); 645132482Smarcel return; 646132482Smarcel } 647132482Smarcel } else { 648132482Smarcel db_unread_token(t); 649132482Smarcel count = -1; 650132482Smarcel } 651132482Smarcel db_skip_to_eol(); 652132482Smarcel db_radix = radix; 653132482Smarcel 654132482Smarcel if (hastid) { 655132482Smarcel td = kdb_thr_lookup((lwpid_t)tid); 656132482Smarcel if (td == NULL) 657132482Smarcel td = kdb_thr_from_pid((pid_t)tid); 658132482Smarcel if (td == NULL) { 659132482Smarcel db_printf("Thread %d not found\n", (int)tid); 660132482Smarcel return; 661132482Smarcel } 662132482Smarcel } else 663132482Smarcel td = kdb_thread; 664138038Srwatson if (td->td_proc != NULL) 665138038Srwatson pid = td->td_proc->p_pid; 666138038Srwatson else 667138038Srwatson pid = -1; 668138038Srwatson db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td); 669132482Smarcel db_trace_thread(td, count); 670132482Smarcel} 671150819Srwatson 672150819Srwatsonstatic void 673150819Srwatsondb_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, 674150819Srwatson char *dummy4) 675150819Srwatson{ 676150819Srwatson struct proc *p; 677150819Srwatson struct thread *td; 678150842Scognet int quit; 679150819Srwatson 680150842Scognet quit = 0; 681150842Scognet db_setup_paging(db_simple_pager, &quit, db_lines_per_page); 682150819Srwatson for (p = LIST_FIRST(&allproc); p != NULL; p = LIST_NEXT(p, p_list)) { 683150819Srwatson FOREACH_THREAD_IN_PROC(p, td) { 684150819Srwatson db_printf("\nTracing command %s pid %d tid %ld td %p\n", 685150819Srwatson p->p_comm, p->p_pid, (long)td->td_tid, td); 686150819Srwatson db_trace_thread(td, -1); 687150842Scognet if (quit) 688150842Scognet return; 689150819Srwatson } 690150819Srwatson } 691150819Srwatson} 692