1e9a5627eSJohnny Chen //===-- CommandObjectWatchpointCommand.cpp ----------------------*- C++ -*-===// 2e9a5627eSJohnny Chen // 3e9a5627eSJohnny Chen // The LLVM Compiler Infrastructure 4e9a5627eSJohnny Chen // 5e9a5627eSJohnny Chen // This file is distributed under the University of Illinois Open Source 6e9a5627eSJohnny Chen // License. See LICENSE.TXT for details. 7e9a5627eSJohnny Chen // 8e9a5627eSJohnny Chen //===----------------------------------------------------------------------===// 9e9a5627eSJohnny Chen 10e9a5627eSJohnny Chen // C Includes 11e9a5627eSJohnny Chen // C++ Includes 1249bcfd80SEugene Zelenko #include <vector> 13e9a5627eSJohnny Chen 1449bcfd80SEugene Zelenko // Other libraries and framework includes 1549bcfd80SEugene Zelenko // Project includes 16e9a5627eSJohnny Chen #include "CommandObjectWatchpoint.h" 17b9c1b51eSKate Stone #include "CommandObjectWatchpointCommand.h" 18b9c1b51eSKate Stone #include "lldb/Breakpoint/StoppointCallbackContext.h" 19b9c1b51eSKate Stone #include "lldb/Breakpoint/Watchpoint.h" 2044d93782SGreg Clayton #include "lldb/Core/IOHandler.h" 21b9c1b51eSKate Stone #include "lldb/Core/State.h" 223eb2b44dSZachary Turner #include "lldb/Host/OptionParser.h" 23e9a5627eSJohnny Chen #include "lldb/Interpreter/CommandInterpreter.h" 24e9a5627eSJohnny Chen #include "lldb/Interpreter/CommandReturnObject.h" 2547cbf4a0SPavel Labath #include "lldb/Interpreter/OptionArgParser.h" 26e9a5627eSJohnny Chen #include "lldb/Target/Target.h" 27e9a5627eSJohnny Chen #include "lldb/Target/Thread.h" 28e9a5627eSJohnny Chen 29e9a5627eSJohnny Chen using namespace lldb; 30e9a5627eSJohnny Chen using namespace lldb_private; 31e9a5627eSJohnny Chen 32e9a5627eSJohnny Chen //------------------------------------------------------------------------- 33e9a5627eSJohnny Chen // CommandObjectWatchpointCommandAdd 34e9a5627eSJohnny Chen //------------------------------------------------------------------------- 35e9a5627eSJohnny Chen 361f0f5b5bSZachary Turner // FIXME: "script-type" needs to have its contents determined dynamically, so 371f0f5b5bSZachary Turner // somebody can add a new scripting 381f0f5b5bSZachary Turner // language to lldb and have it pickable here without having to change this 391f0f5b5bSZachary Turner // enumeration by hand and rebuild lldb proper. 401f0f5b5bSZachary Turner 411f0f5b5bSZachary Turner static OptionEnumValueElement g_script_option_enumeration[4] = { 421f0f5b5bSZachary Turner {eScriptLanguageNone, "command", 431f0f5b5bSZachary Turner "Commands are in the lldb command interpreter language"}, 441f0f5b5bSZachary Turner {eScriptLanguagePython, "python", "Commands are in the Python language."}, 451f0f5b5bSZachary Turner {eSortOrderByName, "default-script", 461f0f5b5bSZachary Turner "Commands are in the default scripting language."}, 471f0f5b5bSZachary Turner {0, nullptr, nullptr}}; 481f0f5b5bSZachary Turner 491f0f5b5bSZachary Turner static OptionDefinition g_watchpoint_command_add_options[] = { 501f0f5b5bSZachary Turner // clang-format off 511f0f5b5bSZachary Turner { LLDB_OPT_SET_1, false, "one-liner", 'o', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeOneLiner, "Specify a one-line watchpoint command inline. Be sure to surround it with quotes." }, 521f0f5b5bSZachary Turner { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Specify whether watchpoint command execution should terminate on error." }, 531f0f5b5bSZachary Turner { LLDB_OPT_SET_ALL, false, "script-type", 's', OptionParser::eRequiredArgument, nullptr, g_script_option_enumeration, 0, eArgTypeNone, "Specify the language for the commands - if none is specified, the lldb command interpreter will be used." }, 541f0f5b5bSZachary Turner { LLDB_OPT_SET_2, false, "python-function", 'F', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePythonFunction, "Give the name of a Python function to run as command for this watchpoint. Be sure to give a module name if appropriate." } 551f0f5b5bSZachary Turner // clang-format on 561f0f5b5bSZachary Turner }; 571f0f5b5bSZachary Turner 58b9c1b51eSKate Stone class CommandObjectWatchpointCommandAdd : public CommandObjectParsed, 59b9c1b51eSKate Stone public IOHandlerDelegateMultiline { 60e9a5627eSJohnny Chen public: 617428a18cSKate Stone CommandObjectWatchpointCommandAdd(CommandInterpreter &interpreter) 62b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "add", 63b9c1b51eSKate Stone "Add a set of LLDB commands to a watchpoint, to be " 64b9c1b51eSKate Stone "executed whenever the watchpoint is hit.", 65b9c1b51eSKate Stone nullptr), 66b9c1b51eSKate Stone IOHandlerDelegateMultiline("DONE", 67b9c1b51eSKate Stone IOHandlerDelegate::Completion::LLDBCommand), 68b9c1b51eSKate Stone m_options() { 69e9a5627eSJohnny Chen SetHelpLong( 70ea671fbdSKate Stone R"( 71ea671fbdSKate Stone General information about entering watchpoint commands 72ea671fbdSKate Stone ------------------------------------------------------ 73ea671fbdSKate Stone 74b9c1b51eSKate Stone )" 75b9c1b51eSKate Stone "This command will prompt for commands to be executed when the specified \ 76ea671fbdSKate Stone watchpoint is hit. Each command is typed on its own line following the '> ' \ 77b9c1b51eSKate Stone prompt until 'DONE' is entered." 78b9c1b51eSKate Stone R"( 79ea671fbdSKate Stone 80b9c1b51eSKate Stone )" 81b9c1b51eSKate Stone "Syntactic errors may not be detected when initially entered, and many \ 82ea671fbdSKate Stone malformed commands can silently fail when executed. If your watchpoint commands \ 83b9c1b51eSKate Stone do not appear to be executing, double-check the command syntax." 84b9c1b51eSKate Stone R"( 85ea671fbdSKate Stone 86b9c1b51eSKate Stone )" 87b9c1b51eSKate Stone "Note: You may enter any debugger command exactly as you would at the debugger \ 88ea671fbdSKate Stone prompt. There is no limit to the number of commands supplied, but do NOT enter \ 89b9c1b51eSKate Stone more than one command per line." 90b9c1b51eSKate Stone R"( 91ea671fbdSKate Stone 92ea671fbdSKate Stone Special information about PYTHON watchpoint commands 93ea671fbdSKate Stone ---------------------------------------------------- 94ea671fbdSKate Stone 95b9c1b51eSKate Stone )" 96b9c1b51eSKate Stone "You may enter either one or more lines of Python, including function \ 97ea671fbdSKate Stone definitions or calls to functions that will have been imported by the time \ 98ea671fbdSKate Stone the code executes. Single line watchpoint commands will be interpreted 'as is' \ 99ea671fbdSKate Stone when the watchpoint is hit. Multiple lines of Python will be wrapped in a \ 100b9c1b51eSKate Stone generated function, and a call to the function will be attached to the watchpoint." 101b9c1b51eSKate Stone R"( 102ea671fbdSKate Stone 103ea671fbdSKate Stone This auto-generated function is passed in three arguments: 104ea671fbdSKate Stone 105ea671fbdSKate Stone frame: an lldb.SBFrame object for the frame which hit the watchpoint. 106ea671fbdSKate Stone 107ea671fbdSKate Stone wp: the watchpoint that was hit. 108ea671fbdSKate Stone 109b9c1b51eSKate Stone )" 110b9c1b51eSKate Stone "When specifying a python function with the --python-function option, you need \ 111b9c1b51eSKate Stone to supply the function name prepended by the module name:" 112b9c1b51eSKate Stone R"( 113ea671fbdSKate Stone 114ea671fbdSKate Stone --python-function myutils.watchpoint_callback 115ea671fbdSKate Stone 116ea671fbdSKate Stone The function itself must have the following prototype: 117ea671fbdSKate Stone 118ea671fbdSKate Stone def watchpoint_callback(frame, wp): 119ea671fbdSKate Stone # Your code goes here 120ea671fbdSKate Stone 121b9c1b51eSKate Stone )" 122b9c1b51eSKate Stone "The arguments are the same as the arguments passed to generated functions as \ 123ea671fbdSKate Stone described above. Note that the global variable 'lldb.frame' will NOT be updated when \ 124ea671fbdSKate Stone this function is called, so be sure to use the 'frame' argument. The 'frame' argument \ 125ea671fbdSKate Stone can get you to the thread via frame.GetThread(), the thread can get you to the \ 126ea671fbdSKate Stone process via thread.GetProcess(), and the process can get you back to the target \ 127b9c1b51eSKate Stone via process.GetTarget()." 128b9c1b51eSKate Stone R"( 129ea671fbdSKate Stone 130b9c1b51eSKate Stone )" 131b9c1b51eSKate Stone "Important Note: As Python code gets collected into functions, access to global \ 132ea671fbdSKate Stone variables requires explicit scoping using the 'global' keyword. Be sure to use correct \ 133b9c1b51eSKate Stone Python syntax, including indentation, when entering Python watchpoint commands." 134b9c1b51eSKate Stone R"( 135ea671fbdSKate Stone 136ea671fbdSKate Stone Example Python one-line watchpoint command: 137ea671fbdSKate Stone 138ea671fbdSKate Stone (lldb) watchpoint command add -s python 1 139ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 140ea671fbdSKate Stone > print "Hit this watchpoint!" 141ea671fbdSKate Stone > DONE 142ea671fbdSKate Stone 143ea671fbdSKate Stone As a convenience, this also works for a short Python one-liner: 144ea671fbdSKate Stone 145ea671fbdSKate Stone (lldb) watchpoint command add -s python 1 -o 'import time; print time.asctime()' 146ea671fbdSKate Stone (lldb) run 147ea671fbdSKate Stone Launching '.../a.out' (x86_64) 148ea671fbdSKate Stone (lldb) Fri Sep 10 12:17:45 2010 149ea671fbdSKate Stone Process 21778 Stopped 150ea671fbdSKate Stone * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = watchpoint 1.1, queue = com.apple.main-thread 151ea671fbdSKate Stone 36 152ea671fbdSKate Stone 37 int c(int val) 153ea671fbdSKate Stone 38 { 154ea671fbdSKate Stone 39 -> return val + 3; 155ea671fbdSKate Stone 40 } 156ea671fbdSKate Stone 41 157ea671fbdSKate Stone 42 int main (int argc, char const *argv[]) 158ea671fbdSKate Stone 159ea671fbdSKate Stone Example multiple line Python watchpoint command, using function definition: 160ea671fbdSKate Stone 161ea671fbdSKate Stone (lldb) watchpoint command add -s python 1 162ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 163ea671fbdSKate Stone > def watchpoint_output (wp_no): 164ea671fbdSKate Stone > out_string = "Hit watchpoint number " + repr (wp_no) 165ea671fbdSKate Stone > print out_string 166ea671fbdSKate Stone > return True 167ea671fbdSKate Stone > watchpoint_output (1) 168ea671fbdSKate Stone > DONE 169ea671fbdSKate Stone 170ea671fbdSKate Stone Example multiple line Python watchpoint command, using 'loose' Python: 171ea671fbdSKate Stone 172ea671fbdSKate Stone (lldb) watchpoint command add -s p 1 173ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 174ea671fbdSKate Stone > global wp_count 175ea671fbdSKate Stone > wp_count = wp_count + 1 176ea671fbdSKate Stone > print "Hit this watchpoint " + repr(wp_count) + " times!" 177ea671fbdSKate Stone > DONE 178ea671fbdSKate Stone 179b9c1b51eSKate Stone )" 180b9c1b51eSKate Stone "In this case, since there is a reference to a global variable, \ 181ea671fbdSKate Stone 'wp_count', you will also need to make sure 'wp_count' exists and is \ 182b9c1b51eSKate Stone initialized:" 183b9c1b51eSKate Stone R"( 184ea671fbdSKate Stone 185ea671fbdSKate Stone (lldb) script 186ea671fbdSKate Stone >>> wp_count = 0 187ea671fbdSKate Stone >>> quit() 188ea671fbdSKate Stone 189b9c1b51eSKate Stone )" 190b9c1b51eSKate Stone "Final Note: A warning that no watchpoint command was generated when there \ 191b9c1b51eSKate Stone are no syntax errors may indicate that a function was declared but never called."); 192e9a5627eSJohnny Chen 193e9a5627eSJohnny Chen CommandArgumentEntry arg; 194e9a5627eSJohnny Chen CommandArgumentData wp_id_arg; 195e9a5627eSJohnny Chen 196e9a5627eSJohnny Chen // Define the first (and only) variant of this arg. 197e9a5627eSJohnny Chen wp_id_arg.arg_type = eArgTypeWatchpointID; 198e9a5627eSJohnny Chen wp_id_arg.arg_repetition = eArgRepeatPlain; 199e9a5627eSJohnny Chen 200b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 201b9c1b51eSKate Stone // argument entry. 202e9a5627eSJohnny Chen arg.push_back(wp_id_arg); 203e9a5627eSJohnny Chen 204e9a5627eSJohnny Chen // Push the data for the first argument into the m_arguments vector. 205e9a5627eSJohnny Chen m_arguments.push_back(arg); 206e9a5627eSJohnny Chen } 207e9a5627eSJohnny Chen 20849bcfd80SEugene Zelenko ~CommandObjectWatchpointCommandAdd() override = default; 209e9a5627eSJohnny Chen 210b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 211e9a5627eSJohnny Chen 212b9c1b51eSKate Stone void IOHandlerActivated(IOHandler &io_handler) override { 21344d93782SGreg Clayton StreamFileSP output_sp(io_handler.GetOutputStreamFile()); 214b9c1b51eSKate Stone if (output_sp) { 215b9c1b51eSKate Stone output_sp->PutCString( 216b9c1b51eSKate Stone "Enter your debugger command(s). Type 'DONE' to end.\n"); 21744d93782SGreg Clayton output_sp->Flush(); 21844d93782SGreg Clayton } 21944d93782SGreg Clayton } 22044d93782SGreg Clayton 221b9c1b51eSKate Stone void IOHandlerInputComplete(IOHandler &io_handler, 222b9c1b51eSKate Stone std::string &line) override { 22344d93782SGreg Clayton io_handler.SetIsDone(true); 22444d93782SGreg Clayton 225b9c1b51eSKate Stone // The WatchpointOptions object is owned by the watchpoint or watchpoint 226b9c1b51eSKate Stone // location 227b9c1b51eSKate Stone WatchpointOptions *wp_options = 228b9c1b51eSKate Stone (WatchpointOptions *)io_handler.GetUserData(); 229b9c1b51eSKate Stone if (wp_options) { 230b9c1b51eSKate Stone std::unique_ptr<WatchpointOptions::CommandData> data_ap( 231b9c1b51eSKate Stone new WatchpointOptions::CommandData()); 232b9c1b51eSKate Stone if (data_ap) { 23344d93782SGreg Clayton data_ap->user_source.SplitIntoLines(line); 2344e4fbe82SZachary Turner auto baton_sp = std::make_shared<WatchpointOptions::CommandBaton>( 2354e4fbe82SZachary Turner std::move(data_ap)); 23644d93782SGreg Clayton wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp); 23744d93782SGreg Clayton } 23844d93782SGreg Clayton } 23944d93782SGreg Clayton } 24044d93782SGreg Clayton 241b9c1b51eSKate Stone void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, 242b9c1b51eSKate Stone CommandReturnObject &result) { 243b9c1b51eSKate Stone m_interpreter.GetLLDBCommandsFromIOHandler( 244b9c1b51eSKate Stone "> ", // Prompt 24544d93782SGreg Clayton *this, // IOHandlerDelegate 24644d93782SGreg Clayton true, // Run IOHandler in async mode 247b9c1b51eSKate Stone wp_options); // Baton for the "io_handler" that will be passed back into 248b9c1b51eSKate Stone // our IOHandlerDelegate functions 249e9a5627eSJohnny Chen } 250e9a5627eSJohnny Chen 251e9a5627eSJohnny Chen /// Set a one-liner as the callback for the watchpoint. 252b9c1b51eSKate Stone void SetWatchpointCommandCallback(WatchpointOptions *wp_options, 253b9c1b51eSKate Stone const char *oneliner) { 254b9c1b51eSKate Stone std::unique_ptr<WatchpointOptions::CommandData> data_ap( 255b9c1b51eSKate Stone new WatchpointOptions::CommandData()); 256e9a5627eSJohnny Chen 25705097246SAdrian Prantl // It's necessary to set both user_source and script_source to the 25805097246SAdrian Prantl // oneliner. The former is used to generate callback description (as in 25905097246SAdrian Prantl // watchpoint command list) while the latter is used for Python to 26005097246SAdrian Prantl // interpret during the actual callback. 261e9a5627eSJohnny Chen data_ap->user_source.AppendString(oneliner); 262e9a5627eSJohnny Chen data_ap->script_source.assign(oneliner); 263e9a5627eSJohnny Chen data_ap->stop_on_error = m_options.m_stop_on_error; 264e9a5627eSJohnny Chen 2654e4fbe82SZachary Turner auto baton_sp = 2664e4fbe82SZachary Turner std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_ap)); 267e9a5627eSJohnny Chen wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp); 268e9a5627eSJohnny Chen } 269e9a5627eSJohnny Chen 270e9a5627eSJohnny Chen static bool 271e9a5627eSJohnny Chen WatchpointOptionsCallbackFunction(void *baton, 272e9a5627eSJohnny Chen StoppointCallbackContext *context, 273b9c1b51eSKate Stone lldb::user_id_t watch_id) { 274e9a5627eSJohnny Chen bool ret_value = true; 27549bcfd80SEugene Zelenko if (baton == nullptr) 276e9a5627eSJohnny Chen return true; 277e9a5627eSJohnny Chen 278b9c1b51eSKate Stone WatchpointOptions::CommandData *data = 279b9c1b51eSKate Stone (WatchpointOptions::CommandData *)baton; 280e9a5627eSJohnny Chen StringList &commands = data->user_source; 281e9a5627eSJohnny Chen 282b9c1b51eSKate Stone if (commands.GetSize() > 0) { 283e9a5627eSJohnny Chen ExecutionContext exe_ctx(context->exe_ctx_ref); 284e9a5627eSJohnny Chen Target *target = exe_ctx.GetTargetPtr(); 285b9c1b51eSKate Stone if (target) { 286e9a5627eSJohnny Chen CommandReturnObject result; 287e9a5627eSJohnny Chen Debugger &debugger = target->GetDebugger(); 288b9c1b51eSKate Stone // Rig up the results secondary output stream to the debugger's, so the 28905097246SAdrian Prantl // output will come out synchronously if the debugger is set up that 29005097246SAdrian Prantl // way. 291e9a5627eSJohnny Chen 292e9a5627eSJohnny Chen StreamSP output_stream(debugger.GetAsyncOutputStream()); 293e9a5627eSJohnny Chen StreamSP error_stream(debugger.GetAsyncErrorStream()); 294e9a5627eSJohnny Chen result.SetImmediateOutputStream(output_stream); 295e9a5627eSJohnny Chen result.SetImmediateErrorStream(error_stream); 296e9a5627eSJohnny Chen 29726c7bf93SJim Ingham CommandInterpreterRunOptions options; 29826c7bf93SJim Ingham options.SetStopOnContinue(true); 29926c7bf93SJim Ingham options.SetStopOnError(data->stop_on_error); 30026c7bf93SJim Ingham options.SetEchoCommands(false); 30126c7bf93SJim Ingham options.SetPrintResults(true); 30226c7bf93SJim Ingham options.SetAddToHistory(false); 303e9a5627eSJohnny Chen 304b9c1b51eSKate Stone debugger.GetCommandInterpreter().HandleCommands(commands, &exe_ctx, 305b9c1b51eSKate Stone options, result); 306e9a5627eSJohnny Chen result.GetImmediateOutputStream()->Flush(); 307e9a5627eSJohnny Chen result.GetImmediateErrorStream()->Flush(); 308e9a5627eSJohnny Chen } 309e9a5627eSJohnny Chen } 310e9a5627eSJohnny Chen return ret_value; 311e9a5627eSJohnny Chen } 312e9a5627eSJohnny Chen 313b9c1b51eSKate Stone class CommandOptions : public Options { 314e9a5627eSJohnny Chen public: 315b9c1b51eSKate Stone CommandOptions() 316b9c1b51eSKate Stone : Options(), m_use_commands(false), m_use_script_language(false), 317b9c1b51eSKate Stone m_script_language(eScriptLanguageNone), m_use_one_liner(false), 318b9c1b51eSKate Stone m_one_liner(), m_function_name() {} 319e9a5627eSJohnny Chen 32049bcfd80SEugene Zelenko ~CommandOptions() override = default; 321e9a5627eSJohnny Chen 32297206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 323b9c1b51eSKate Stone ExecutionContext *execution_context) override { 32497206d57SZachary Turner Status error; 3253bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val; 326e9a5627eSJohnny Chen 327b9c1b51eSKate Stone switch (short_option) { 328e9a5627eSJohnny Chen case 'o': 329e9a5627eSJohnny Chen m_use_one_liner = true; 330e9a5627eSJohnny Chen m_one_liner = option_arg; 331e9a5627eSJohnny Chen break; 332e9a5627eSJohnny Chen 333e9a5627eSJohnny Chen case 's': 33447cbf4a0SPavel Labath m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum( 335fe11483bSZachary Turner option_arg, GetDefinitions()[option_idx].enum_values, 336fe11483bSZachary Turner eScriptLanguageNone, error); 337e9a5627eSJohnny Chen 338b9c1b51eSKate Stone m_use_script_language = (m_script_language == eScriptLanguagePython || 339b9c1b51eSKate Stone m_script_language == eScriptLanguageDefault); 340e9a5627eSJohnny Chen break; 341e9a5627eSJohnny Chen 342b9c1b51eSKate Stone case 'e': { 343e9a5627eSJohnny Chen bool success = false; 34447cbf4a0SPavel Labath m_stop_on_error = 34547cbf4a0SPavel Labath OptionArgParser::ToBoolean(option_arg, false, &success); 346e9a5627eSJohnny Chen if (!success) 347b9c1b51eSKate Stone error.SetErrorStringWithFormat( 348fe11483bSZachary Turner "invalid value for stop-on-error: \"%s\"", 349fe11483bSZachary Turner option_arg.str().c_str()); 350b9c1b51eSKate Stone } break; 351e9a5627eSJohnny Chen 352e9a5627eSJohnny Chen case 'F': 353e9a5627eSJohnny Chen m_use_one_liner = false; 354e9a5627eSJohnny Chen m_use_script_language = true; 355e9a5627eSJohnny Chen m_function_name.assign(option_arg); 356e9a5627eSJohnny Chen break; 357e9a5627eSJohnny Chen 358e9a5627eSJohnny Chen default: 359e9a5627eSJohnny Chen break; 360e9a5627eSJohnny Chen } 361e9a5627eSJohnny Chen return error; 362e9a5627eSJohnny Chen } 36349bcfd80SEugene Zelenko 364b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 365e9a5627eSJohnny Chen m_use_commands = true; 366e9a5627eSJohnny Chen m_use_script_language = false; 367e9a5627eSJohnny Chen m_script_language = eScriptLanguageNone; 368e9a5627eSJohnny Chen 369e9a5627eSJohnny Chen m_use_one_liner = false; 370e9a5627eSJohnny Chen m_stop_on_error = true; 371e9a5627eSJohnny Chen m_one_liner.clear(); 372e9a5627eSJohnny Chen m_function_name.clear(); 373e9a5627eSJohnny Chen } 374e9a5627eSJohnny Chen 3751f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 37670602439SZachary Turner return llvm::makeArrayRef(g_watchpoint_command_add_options); 3771f0f5b5bSZachary Turner } 378e9a5627eSJohnny Chen 379e9a5627eSJohnny Chen // Instance variables to hold the values for command options. 380e9a5627eSJohnny Chen 381e9a5627eSJohnny Chen bool m_use_commands; 382e9a5627eSJohnny Chen bool m_use_script_language; 383e9a5627eSJohnny Chen lldb::ScriptLanguage m_script_language; 384e9a5627eSJohnny Chen 385e9a5627eSJohnny Chen // Instance variables to hold the values for one_liner options. 386e9a5627eSJohnny Chen bool m_use_one_liner; 387e9a5627eSJohnny Chen std::string m_one_liner; 388e9a5627eSJohnny Chen bool m_stop_on_error; 389e9a5627eSJohnny Chen std::string m_function_name; 390e9a5627eSJohnny Chen }; 391e9a5627eSJohnny Chen 392e9a5627eSJohnny Chen protected: 393b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 394e9a5627eSJohnny Chen Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 395e9a5627eSJohnny Chen 396b9c1b51eSKate Stone if (target == nullptr) { 397b9c1b51eSKate Stone result.AppendError("There is not a current executable; there are no " 398b9c1b51eSKate Stone "watchpoints to which to add commands"); 399e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 400e9a5627eSJohnny Chen return false; 401e9a5627eSJohnny Chen } 402e9a5627eSJohnny Chen 403e9a5627eSJohnny Chen const WatchpointList &watchpoints = target->GetWatchpointList(); 404e9a5627eSJohnny Chen size_t num_watchpoints = watchpoints.GetSize(); 405e9a5627eSJohnny Chen 406b9c1b51eSKate Stone if (num_watchpoints == 0) { 407e9a5627eSJohnny Chen result.AppendError("No watchpoints exist to have commands added"); 408e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 409e9a5627eSJohnny Chen return false; 410e9a5627eSJohnny Chen } 411e9a5627eSJohnny Chen 412b9c1b51eSKate Stone if (!m_options.m_use_script_language && 413b9c1b51eSKate Stone !m_options.m_function_name.empty()) { 414b9c1b51eSKate Stone result.AppendError("need to enable scripting to have a function run as a " 415b9c1b51eSKate Stone "watchpoint command"); 416e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 417e9a5627eSJohnny Chen return false; 418e9a5627eSJohnny Chen } 419e9a5627eSJohnny Chen 420e9a5627eSJohnny Chen std::vector<uint32_t> valid_wp_ids; 421b9c1b51eSKate Stone if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, 422b9c1b51eSKate Stone valid_wp_ids)) { 423e9a5627eSJohnny Chen result.AppendError("Invalid watchpoints specification."); 424e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 425e9a5627eSJohnny Chen return false; 426e9a5627eSJohnny Chen } 427e9a5627eSJohnny Chen 428e9a5627eSJohnny Chen result.SetStatus(eReturnStatusSuccessFinishNoResult); 429e9a5627eSJohnny Chen const size_t count = valid_wp_ids.size(); 430b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 431e9a5627eSJohnny Chen uint32_t cur_wp_id = valid_wp_ids.at(i); 432b9c1b51eSKate Stone if (cur_wp_id != LLDB_INVALID_WATCH_ID) { 433e9a5627eSJohnny Chen Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get(); 434e9a5627eSJohnny Chen // Sanity check wp first. 435b9c1b51eSKate Stone if (wp == nullptr) 436b9c1b51eSKate Stone continue; 437e9a5627eSJohnny Chen 438e9a5627eSJohnny Chen WatchpointOptions *wp_options = wp->GetOptions(); 439e9a5627eSJohnny Chen // Skip this watchpoint if wp_options is not good. 440b9c1b51eSKate Stone if (wp_options == nullptr) 441b9c1b51eSKate Stone continue; 442e9a5627eSJohnny Chen 44305097246SAdrian Prantl // If we are using script language, get the script interpreter in order 44405097246SAdrian Prantl // to set or collect command callback. Otherwise, call the methods 44505097246SAdrian Prantl // associated with this object. 446b9c1b51eSKate Stone if (m_options.m_use_script_language) { 447e9a5627eSJohnny Chen // Special handling for one-liner specified inline. 448b9c1b51eSKate Stone if (m_options.m_use_one_liner) { 449b9c1b51eSKate Stone m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback( 450b9c1b51eSKate Stone wp_options, m_options.m_one_liner.c_str()); 451e9a5627eSJohnny Chen } 45205097246SAdrian Prantl // Special handling for using a Python function by name instead of 45305097246SAdrian Prantl // extending the watchpoint callback data structures, we just 45405097246SAdrian Prantl // automatize what the user would do manually: make their watchpoint 45505097246SAdrian Prantl // command be a function call 456b9c1b51eSKate Stone else if (!m_options.m_function_name.empty()) { 457e9a5627eSJohnny Chen std::string oneliner(m_options.m_function_name); 458e9a5627eSJohnny Chen oneliner += "(frame, wp, internal_dict)"; 459b9c1b51eSKate Stone m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback( 460b9c1b51eSKate Stone wp_options, oneliner.c_str()); 461b9c1b51eSKate Stone } else { 462b9c1b51eSKate Stone m_interpreter.GetScriptInterpreter() 463b9c1b51eSKate Stone ->CollectDataForWatchpointCommandCallback(wp_options, result); 464e9a5627eSJohnny Chen } 465b9c1b51eSKate Stone } else { 466e9a5627eSJohnny Chen // Special handling for one-liner specified inline. 467e9a5627eSJohnny Chen if (m_options.m_use_one_liner) 468e9a5627eSJohnny Chen SetWatchpointCommandCallback(wp_options, 469e9a5627eSJohnny Chen m_options.m_one_liner.c_str()); 470e9a5627eSJohnny Chen else 471b9c1b51eSKate Stone CollectDataForWatchpointCommandCallback(wp_options, result); 472e9a5627eSJohnny Chen } 473e9a5627eSJohnny Chen } 474e9a5627eSJohnny Chen } 475e9a5627eSJohnny Chen 476e9a5627eSJohnny Chen return result.Succeeded(); 477e9a5627eSJohnny Chen } 478e9a5627eSJohnny Chen 479e9a5627eSJohnny Chen private: 480e9a5627eSJohnny Chen CommandOptions m_options; 481e9a5627eSJohnny Chen }; 482e9a5627eSJohnny Chen 483e9a5627eSJohnny Chen //------------------------------------------------------------------------- 484e9a5627eSJohnny Chen // CommandObjectWatchpointCommandDelete 485e9a5627eSJohnny Chen //------------------------------------------------------------------------- 486e9a5627eSJohnny Chen 487b9c1b51eSKate Stone class CommandObjectWatchpointCommandDelete : public CommandObjectParsed { 488e9a5627eSJohnny Chen public: 489b9c1b51eSKate Stone CommandObjectWatchpointCommandDelete(CommandInterpreter &interpreter) 490b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "delete", 491e9a5627eSJohnny Chen "Delete the set of commands from a watchpoint.", 492b9c1b51eSKate Stone nullptr) { 493e9a5627eSJohnny Chen CommandArgumentEntry arg; 494e9a5627eSJohnny Chen CommandArgumentData wp_id_arg; 495e9a5627eSJohnny Chen 496e9a5627eSJohnny Chen // Define the first (and only) variant of this arg. 497e9a5627eSJohnny Chen wp_id_arg.arg_type = eArgTypeWatchpointID; 498e9a5627eSJohnny Chen wp_id_arg.arg_repetition = eArgRepeatPlain; 499e9a5627eSJohnny Chen 500b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 501b9c1b51eSKate Stone // argument entry. 502e9a5627eSJohnny Chen arg.push_back(wp_id_arg); 503e9a5627eSJohnny Chen 504e9a5627eSJohnny Chen // Push the data for the first argument into the m_arguments vector. 505e9a5627eSJohnny Chen m_arguments.push_back(arg); 506e9a5627eSJohnny Chen } 507e9a5627eSJohnny Chen 50849bcfd80SEugene Zelenko ~CommandObjectWatchpointCommandDelete() override = default; 509e9a5627eSJohnny Chen 510e9a5627eSJohnny Chen protected: 511b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 512e9a5627eSJohnny Chen Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 513e9a5627eSJohnny Chen 514b9c1b51eSKate Stone if (target == nullptr) { 515b9c1b51eSKate Stone result.AppendError("There is not a current executable; there are no " 516b9c1b51eSKate Stone "watchpoints from which to delete commands"); 517e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 518e9a5627eSJohnny Chen return false; 519e9a5627eSJohnny Chen } 520e9a5627eSJohnny Chen 521e9a5627eSJohnny Chen const WatchpointList &watchpoints = target->GetWatchpointList(); 522e9a5627eSJohnny Chen size_t num_watchpoints = watchpoints.GetSize(); 523e9a5627eSJohnny Chen 524b9c1b51eSKate Stone if (num_watchpoints == 0) { 525e9a5627eSJohnny Chen result.AppendError("No watchpoints exist to have commands deleted"); 526e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 527e9a5627eSJohnny Chen return false; 528e9a5627eSJohnny Chen } 529e9a5627eSJohnny Chen 530b9c1b51eSKate Stone if (command.GetArgumentCount() == 0) { 531b9c1b51eSKate Stone result.AppendError( 532b9c1b51eSKate Stone "No watchpoint specified from which to delete the commands"); 533e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 534e9a5627eSJohnny Chen return false; 535e9a5627eSJohnny Chen } 536e9a5627eSJohnny Chen 537e9a5627eSJohnny Chen std::vector<uint32_t> valid_wp_ids; 538b9c1b51eSKate Stone if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, 539b9c1b51eSKate Stone valid_wp_ids)) { 540e9a5627eSJohnny Chen result.AppendError("Invalid watchpoints specification."); 541e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 542e9a5627eSJohnny Chen return false; 543e9a5627eSJohnny Chen } 544e9a5627eSJohnny Chen 545e9a5627eSJohnny Chen result.SetStatus(eReturnStatusSuccessFinishNoResult); 546e9a5627eSJohnny Chen const size_t count = valid_wp_ids.size(); 547b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 548e9a5627eSJohnny Chen uint32_t cur_wp_id = valid_wp_ids.at(i); 549b9c1b51eSKate Stone if (cur_wp_id != LLDB_INVALID_WATCH_ID) { 550e9a5627eSJohnny Chen Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get(); 551e9a5627eSJohnny Chen if (wp) 552e9a5627eSJohnny Chen wp->ClearCallback(); 553b9c1b51eSKate Stone } else { 554b9c1b51eSKate Stone result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", cur_wp_id); 555e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 556e9a5627eSJohnny Chen return false; 557e9a5627eSJohnny Chen } 558e9a5627eSJohnny Chen } 559e9a5627eSJohnny Chen return result.Succeeded(); 560e9a5627eSJohnny Chen } 561e9a5627eSJohnny Chen }; 562e9a5627eSJohnny Chen 563e9a5627eSJohnny Chen //------------------------------------------------------------------------- 564e9a5627eSJohnny Chen // CommandObjectWatchpointCommandList 565e9a5627eSJohnny Chen //------------------------------------------------------------------------- 566e9a5627eSJohnny Chen 567b9c1b51eSKate Stone class CommandObjectWatchpointCommandList : public CommandObjectParsed { 568e9a5627eSJohnny Chen public: 569b9c1b51eSKate Stone CommandObjectWatchpointCommandList(CommandInterpreter &interpreter) 570b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "list", "List the script or set of " 571b9c1b51eSKate Stone "commands to be executed when " 572b9c1b51eSKate Stone "the watchpoint is hit.", 573b9c1b51eSKate Stone nullptr) { 574e9a5627eSJohnny Chen CommandArgumentEntry arg; 575e9a5627eSJohnny Chen CommandArgumentData wp_id_arg; 576e9a5627eSJohnny Chen 577e9a5627eSJohnny Chen // Define the first (and only) variant of this arg. 578e9a5627eSJohnny Chen wp_id_arg.arg_type = eArgTypeWatchpointID; 579e9a5627eSJohnny Chen wp_id_arg.arg_repetition = eArgRepeatPlain; 580e9a5627eSJohnny Chen 581b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 582b9c1b51eSKate Stone // argument entry. 583e9a5627eSJohnny Chen arg.push_back(wp_id_arg); 584e9a5627eSJohnny Chen 585e9a5627eSJohnny Chen // Push the data for the first argument into the m_arguments vector. 586e9a5627eSJohnny Chen m_arguments.push_back(arg); 587e9a5627eSJohnny Chen } 588e9a5627eSJohnny Chen 58949bcfd80SEugene Zelenko ~CommandObjectWatchpointCommandList() override = default; 590e9a5627eSJohnny Chen 591e9a5627eSJohnny Chen protected: 592b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 593e9a5627eSJohnny Chen Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 594e9a5627eSJohnny Chen 595b9c1b51eSKate Stone if (target == nullptr) { 596b9c1b51eSKate Stone result.AppendError("There is not a current executable; there are no " 597b9c1b51eSKate Stone "watchpoints for which to list commands"); 598e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 599e9a5627eSJohnny Chen return false; 600e9a5627eSJohnny Chen } 601e9a5627eSJohnny Chen 602e9a5627eSJohnny Chen const WatchpointList &watchpoints = target->GetWatchpointList(); 603e9a5627eSJohnny Chen size_t num_watchpoints = watchpoints.GetSize(); 604e9a5627eSJohnny Chen 605b9c1b51eSKate Stone if (num_watchpoints == 0) { 606e9a5627eSJohnny Chen result.AppendError("No watchpoints exist for which to list commands"); 607e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 608e9a5627eSJohnny Chen return false; 609e9a5627eSJohnny Chen } 610e9a5627eSJohnny Chen 611b9c1b51eSKate Stone if (command.GetArgumentCount() == 0) { 612b9c1b51eSKate Stone result.AppendError( 613b9c1b51eSKate Stone "No watchpoint specified for which to list the commands"); 614e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 615e9a5627eSJohnny Chen return false; 616e9a5627eSJohnny Chen } 617e9a5627eSJohnny Chen 618e9a5627eSJohnny Chen std::vector<uint32_t> valid_wp_ids; 619b9c1b51eSKate Stone if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, 620b9c1b51eSKate Stone valid_wp_ids)) { 621e9a5627eSJohnny Chen result.AppendError("Invalid watchpoints specification."); 622e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 623e9a5627eSJohnny Chen return false; 624e9a5627eSJohnny Chen } 625e9a5627eSJohnny Chen 626e9a5627eSJohnny Chen result.SetStatus(eReturnStatusSuccessFinishNoResult); 627e9a5627eSJohnny Chen const size_t count = valid_wp_ids.size(); 628b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 629e9a5627eSJohnny Chen uint32_t cur_wp_id = valid_wp_ids.at(i); 630b9c1b51eSKate Stone if (cur_wp_id != LLDB_INVALID_WATCH_ID) { 631e9a5627eSJohnny Chen Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get(); 632e9a5627eSJohnny Chen 633b9c1b51eSKate Stone if (wp) { 634e9a5627eSJohnny Chen const WatchpointOptions *wp_options = wp->GetOptions(); 635b9c1b51eSKate Stone if (wp_options) { 636e9a5627eSJohnny Chen // Get the callback baton associated with the current watchpoint. 637e9a5627eSJohnny Chen const Baton *baton = wp_options->GetBaton(); 638b9c1b51eSKate Stone if (baton) { 639e9a5627eSJohnny Chen result.GetOutputStream().Printf("Watchpoint %u:\n", cur_wp_id); 640e9a5627eSJohnny Chen result.GetOutputStream().IndentMore(); 641b9c1b51eSKate Stone baton->GetDescription(&result.GetOutputStream(), 642b9c1b51eSKate Stone eDescriptionLevelFull); 643e9a5627eSJohnny Chen result.GetOutputStream().IndentLess(); 644b9c1b51eSKate Stone } else { 645b9c1b51eSKate Stone result.AppendMessageWithFormat( 646b9c1b51eSKate Stone "Watchpoint %u does not have an associated command.\n", 647e9a5627eSJohnny Chen cur_wp_id); 648e9a5627eSJohnny Chen } 649e9a5627eSJohnny Chen } 650e9a5627eSJohnny Chen result.SetStatus(eReturnStatusSuccessFinishResult); 651b9c1b51eSKate Stone } else { 652b9c1b51eSKate Stone result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", 653b9c1b51eSKate Stone cur_wp_id); 654e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 655e9a5627eSJohnny Chen } 656e9a5627eSJohnny Chen } 657e9a5627eSJohnny Chen } 658e9a5627eSJohnny Chen 659e9a5627eSJohnny Chen return result.Succeeded(); 660e9a5627eSJohnny Chen } 661e9a5627eSJohnny Chen }; 662e9a5627eSJohnny Chen 663e9a5627eSJohnny Chen //------------------------------------------------------------------------- 664e9a5627eSJohnny Chen // CommandObjectWatchpointCommand 665e9a5627eSJohnny Chen //------------------------------------------------------------------------- 666e9a5627eSJohnny Chen 667b9c1b51eSKate Stone CommandObjectWatchpointCommand::CommandObjectWatchpointCommand( 668b9c1b51eSKate Stone CommandInterpreter &interpreter) 669b9c1b51eSKate Stone : CommandObjectMultiword( 670b9c1b51eSKate Stone interpreter, "command", 671b9c1b51eSKate Stone "Commands for adding, removing and examining LLDB commands " 672*4ebdee0aSBruce Mitchener "executed when the watchpoint is hit (watchpoint 'commands').", 673b9c1b51eSKate Stone "command <sub-command> [<sub-command-options>] <watchpoint-id>") { 674b9c1b51eSKate Stone CommandObjectSP add_command_object( 675b9c1b51eSKate Stone new CommandObjectWatchpointCommandAdd(interpreter)); 676b9c1b51eSKate Stone CommandObjectSP delete_command_object( 677b9c1b51eSKate Stone new CommandObjectWatchpointCommandDelete(interpreter)); 678b9c1b51eSKate Stone CommandObjectSP list_command_object( 679b9c1b51eSKate Stone new CommandObjectWatchpointCommandList(interpreter)); 680e9a5627eSJohnny Chen 681e9a5627eSJohnny Chen add_command_object->SetCommandName("watchpoint command add"); 682e9a5627eSJohnny Chen delete_command_object->SetCommandName("watchpoint command delete"); 683e9a5627eSJohnny Chen list_command_object->SetCommandName("watchpoint command list"); 684e9a5627eSJohnny Chen 68503da4cc2SGreg Clayton LoadSubCommand("add", add_command_object); 68603da4cc2SGreg Clayton LoadSubCommand("delete", delete_command_object); 68703da4cc2SGreg Clayton LoadSubCommand("list", list_command_object); 688e9a5627eSJohnny Chen } 689e9a5627eSJohnny Chen 69049bcfd80SEugene Zelenko CommandObjectWatchpointCommand::~CommandObjectWatchpointCommand() = default; 691