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" 22*3eb2b44dSZachary Turner #include "lldb/Host/OptionParser.h" 23e9a5627eSJohnny Chen #include "lldb/Interpreter/CommandInterpreter.h" 24e9a5627eSJohnny Chen #include "lldb/Interpreter/CommandReturnObject.h" 25e9a5627eSJohnny Chen #include "lldb/Target/Target.h" 26e9a5627eSJohnny Chen #include "lldb/Target/Thread.h" 27e9a5627eSJohnny Chen 28e9a5627eSJohnny Chen using namespace lldb; 29e9a5627eSJohnny Chen using namespace lldb_private; 30e9a5627eSJohnny Chen 31e9a5627eSJohnny Chen //------------------------------------------------------------------------- 32e9a5627eSJohnny Chen // CommandObjectWatchpointCommandAdd 33e9a5627eSJohnny Chen //------------------------------------------------------------------------- 34e9a5627eSJohnny Chen 351f0f5b5bSZachary Turner // FIXME: "script-type" needs to have its contents determined dynamically, so 361f0f5b5bSZachary Turner // somebody can add a new scripting 371f0f5b5bSZachary Turner // language to lldb and have it pickable here without having to change this 381f0f5b5bSZachary Turner // enumeration by hand and rebuild lldb proper. 391f0f5b5bSZachary Turner 401f0f5b5bSZachary Turner static OptionEnumValueElement g_script_option_enumeration[4] = { 411f0f5b5bSZachary Turner {eScriptLanguageNone, "command", 421f0f5b5bSZachary Turner "Commands are in the lldb command interpreter language"}, 431f0f5b5bSZachary Turner {eScriptLanguagePython, "python", "Commands are in the Python language."}, 441f0f5b5bSZachary Turner {eSortOrderByName, "default-script", 451f0f5b5bSZachary Turner "Commands are in the default scripting language."}, 461f0f5b5bSZachary Turner {0, nullptr, nullptr}}; 471f0f5b5bSZachary Turner 481f0f5b5bSZachary Turner static OptionDefinition g_watchpoint_command_add_options[] = { 491f0f5b5bSZachary Turner // clang-format off 501f0f5b5bSZachary 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." }, 511f0f5b5bSZachary 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." }, 521f0f5b5bSZachary 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." }, 531f0f5b5bSZachary 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." } 541f0f5b5bSZachary Turner // clang-format on 551f0f5b5bSZachary Turner }; 561f0f5b5bSZachary Turner 57b9c1b51eSKate Stone class CommandObjectWatchpointCommandAdd : public CommandObjectParsed, 58b9c1b51eSKate Stone public IOHandlerDelegateMultiline { 59e9a5627eSJohnny Chen public: 607428a18cSKate Stone CommandObjectWatchpointCommandAdd(CommandInterpreter &interpreter) 61b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "add", 62b9c1b51eSKate Stone "Add a set of LLDB commands to a watchpoint, to be " 63b9c1b51eSKate Stone "executed whenever the watchpoint is hit.", 64b9c1b51eSKate Stone nullptr), 65b9c1b51eSKate Stone IOHandlerDelegateMultiline("DONE", 66b9c1b51eSKate Stone IOHandlerDelegate::Completion::LLDBCommand), 67b9c1b51eSKate Stone m_options() { 68e9a5627eSJohnny Chen SetHelpLong( 69ea671fbdSKate Stone R"( 70ea671fbdSKate Stone General information about entering watchpoint commands 71ea671fbdSKate Stone ------------------------------------------------------ 72ea671fbdSKate Stone 73b9c1b51eSKate Stone )" 74b9c1b51eSKate Stone "This command will prompt for commands to be executed when the specified \ 75ea671fbdSKate Stone watchpoint is hit. Each command is typed on its own line following the '> ' \ 76b9c1b51eSKate Stone prompt until 'DONE' is entered." 77b9c1b51eSKate Stone R"( 78ea671fbdSKate Stone 79b9c1b51eSKate Stone )" 80b9c1b51eSKate Stone "Syntactic errors may not be detected when initially entered, and many \ 81ea671fbdSKate Stone malformed commands can silently fail when executed. If your watchpoint commands \ 82b9c1b51eSKate Stone do not appear to be executing, double-check the command syntax." 83b9c1b51eSKate Stone R"( 84ea671fbdSKate Stone 85b9c1b51eSKate Stone )" 86b9c1b51eSKate Stone "Note: You may enter any debugger command exactly as you would at the debugger \ 87ea671fbdSKate Stone prompt. There is no limit to the number of commands supplied, but do NOT enter \ 88b9c1b51eSKate Stone more than one command per line." 89b9c1b51eSKate Stone R"( 90ea671fbdSKate Stone 91ea671fbdSKate Stone Special information about PYTHON watchpoint commands 92ea671fbdSKate Stone ---------------------------------------------------- 93ea671fbdSKate Stone 94b9c1b51eSKate Stone )" 95b9c1b51eSKate Stone "You may enter either one or more lines of Python, including function \ 96ea671fbdSKate Stone definitions or calls to functions that will have been imported by the time \ 97ea671fbdSKate Stone the code executes. Single line watchpoint commands will be interpreted 'as is' \ 98ea671fbdSKate Stone when the watchpoint is hit. Multiple lines of Python will be wrapped in a \ 99b9c1b51eSKate Stone generated function, and a call to the function will be attached to the watchpoint." 100b9c1b51eSKate Stone R"( 101ea671fbdSKate Stone 102ea671fbdSKate Stone This auto-generated function is passed in three arguments: 103ea671fbdSKate Stone 104ea671fbdSKate Stone frame: an lldb.SBFrame object for the frame which hit the watchpoint. 105ea671fbdSKate Stone 106ea671fbdSKate Stone wp: the watchpoint that was hit. 107ea671fbdSKate Stone 108b9c1b51eSKate Stone )" 109b9c1b51eSKate Stone "When specifying a python function with the --python-function option, you need \ 110b9c1b51eSKate Stone to supply the function name prepended by the module name:" 111b9c1b51eSKate Stone R"( 112ea671fbdSKate Stone 113ea671fbdSKate Stone --python-function myutils.watchpoint_callback 114ea671fbdSKate Stone 115ea671fbdSKate Stone The function itself must have the following prototype: 116ea671fbdSKate Stone 117ea671fbdSKate Stone def watchpoint_callback(frame, wp): 118ea671fbdSKate Stone # Your code goes here 119ea671fbdSKate Stone 120b9c1b51eSKate Stone )" 121b9c1b51eSKate Stone "The arguments are the same as the arguments passed to generated functions as \ 122ea671fbdSKate Stone described above. Note that the global variable 'lldb.frame' will NOT be updated when \ 123ea671fbdSKate Stone this function is called, so be sure to use the 'frame' argument. The 'frame' argument \ 124ea671fbdSKate Stone can get you to the thread via frame.GetThread(), the thread can get you to the \ 125ea671fbdSKate Stone process via thread.GetProcess(), and the process can get you back to the target \ 126b9c1b51eSKate Stone via process.GetTarget()." 127b9c1b51eSKate Stone R"( 128ea671fbdSKate Stone 129b9c1b51eSKate Stone )" 130b9c1b51eSKate Stone "Important Note: As Python code gets collected into functions, access to global \ 131ea671fbdSKate Stone variables requires explicit scoping using the 'global' keyword. Be sure to use correct \ 132b9c1b51eSKate Stone Python syntax, including indentation, when entering Python watchpoint commands." 133b9c1b51eSKate Stone R"( 134ea671fbdSKate Stone 135ea671fbdSKate Stone Example Python one-line watchpoint command: 136ea671fbdSKate Stone 137ea671fbdSKate Stone (lldb) watchpoint command add -s python 1 138ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 139ea671fbdSKate Stone > print "Hit this watchpoint!" 140ea671fbdSKate Stone > DONE 141ea671fbdSKate Stone 142ea671fbdSKate Stone As a convenience, this also works for a short Python one-liner: 143ea671fbdSKate Stone 144ea671fbdSKate Stone (lldb) watchpoint command add -s python 1 -o 'import time; print time.asctime()' 145ea671fbdSKate Stone (lldb) run 146ea671fbdSKate Stone Launching '.../a.out' (x86_64) 147ea671fbdSKate Stone (lldb) Fri Sep 10 12:17:45 2010 148ea671fbdSKate Stone Process 21778 Stopped 149ea671fbdSKate Stone * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = watchpoint 1.1, queue = com.apple.main-thread 150ea671fbdSKate Stone 36 151ea671fbdSKate Stone 37 int c(int val) 152ea671fbdSKate Stone 38 { 153ea671fbdSKate Stone 39 -> return val + 3; 154ea671fbdSKate Stone 40 } 155ea671fbdSKate Stone 41 156ea671fbdSKate Stone 42 int main (int argc, char const *argv[]) 157ea671fbdSKate Stone 158ea671fbdSKate Stone Example multiple line Python watchpoint command, using function definition: 159ea671fbdSKate Stone 160ea671fbdSKate Stone (lldb) watchpoint command add -s python 1 161ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 162ea671fbdSKate Stone > def watchpoint_output (wp_no): 163ea671fbdSKate Stone > out_string = "Hit watchpoint number " + repr (wp_no) 164ea671fbdSKate Stone > print out_string 165ea671fbdSKate Stone > return True 166ea671fbdSKate Stone > watchpoint_output (1) 167ea671fbdSKate Stone > DONE 168ea671fbdSKate Stone 169ea671fbdSKate Stone Example multiple line Python watchpoint command, using 'loose' Python: 170ea671fbdSKate Stone 171ea671fbdSKate Stone (lldb) watchpoint command add -s p 1 172ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 173ea671fbdSKate Stone > global wp_count 174ea671fbdSKate Stone > wp_count = wp_count + 1 175ea671fbdSKate Stone > print "Hit this watchpoint " + repr(wp_count) + " times!" 176ea671fbdSKate Stone > DONE 177ea671fbdSKate Stone 178b9c1b51eSKate Stone )" 179b9c1b51eSKate Stone "In this case, since there is a reference to a global variable, \ 180ea671fbdSKate Stone 'wp_count', you will also need to make sure 'wp_count' exists and is \ 181b9c1b51eSKate Stone initialized:" 182b9c1b51eSKate Stone R"( 183ea671fbdSKate Stone 184ea671fbdSKate Stone (lldb) script 185ea671fbdSKate Stone >>> wp_count = 0 186ea671fbdSKate Stone >>> quit() 187ea671fbdSKate Stone 188b9c1b51eSKate Stone )" 189b9c1b51eSKate Stone "Final Note: A warning that no watchpoint command was generated when there \ 190b9c1b51eSKate Stone are no syntax errors may indicate that a function was declared but never called."); 191e9a5627eSJohnny Chen 192e9a5627eSJohnny Chen CommandArgumentEntry arg; 193e9a5627eSJohnny Chen CommandArgumentData wp_id_arg; 194e9a5627eSJohnny Chen 195e9a5627eSJohnny Chen // Define the first (and only) variant of this arg. 196e9a5627eSJohnny Chen wp_id_arg.arg_type = eArgTypeWatchpointID; 197e9a5627eSJohnny Chen wp_id_arg.arg_repetition = eArgRepeatPlain; 198e9a5627eSJohnny Chen 199b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 200b9c1b51eSKate Stone // argument entry. 201e9a5627eSJohnny Chen arg.push_back(wp_id_arg); 202e9a5627eSJohnny Chen 203e9a5627eSJohnny Chen // Push the data for the first argument into the m_arguments vector. 204e9a5627eSJohnny Chen m_arguments.push_back(arg); 205e9a5627eSJohnny Chen } 206e9a5627eSJohnny Chen 20749bcfd80SEugene Zelenko ~CommandObjectWatchpointCommandAdd() override = default; 208e9a5627eSJohnny Chen 209b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 210e9a5627eSJohnny Chen 211b9c1b51eSKate Stone void IOHandlerActivated(IOHandler &io_handler) override { 21244d93782SGreg Clayton StreamFileSP output_sp(io_handler.GetOutputStreamFile()); 213b9c1b51eSKate Stone if (output_sp) { 214b9c1b51eSKate Stone output_sp->PutCString( 215b9c1b51eSKate Stone "Enter your debugger command(s). Type 'DONE' to end.\n"); 21644d93782SGreg Clayton output_sp->Flush(); 21744d93782SGreg Clayton } 21844d93782SGreg Clayton } 21944d93782SGreg Clayton 220b9c1b51eSKate Stone void IOHandlerInputComplete(IOHandler &io_handler, 221b9c1b51eSKate Stone std::string &line) override { 22244d93782SGreg Clayton io_handler.SetIsDone(true); 22344d93782SGreg Clayton 224b9c1b51eSKate Stone // The WatchpointOptions object is owned by the watchpoint or watchpoint 225b9c1b51eSKate Stone // location 226b9c1b51eSKate Stone WatchpointOptions *wp_options = 227b9c1b51eSKate Stone (WatchpointOptions *)io_handler.GetUserData(); 228b9c1b51eSKate Stone if (wp_options) { 229b9c1b51eSKate Stone std::unique_ptr<WatchpointOptions::CommandData> data_ap( 230b9c1b51eSKate Stone new WatchpointOptions::CommandData()); 231b9c1b51eSKate Stone if (data_ap) { 23244d93782SGreg Clayton data_ap->user_source.SplitIntoLines(line); 2334e4fbe82SZachary Turner auto baton_sp = std::make_shared<WatchpointOptions::CommandBaton>( 2344e4fbe82SZachary Turner std::move(data_ap)); 23544d93782SGreg Clayton wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp); 23644d93782SGreg Clayton } 23744d93782SGreg Clayton } 23844d93782SGreg Clayton } 23944d93782SGreg Clayton 240b9c1b51eSKate Stone void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, 241b9c1b51eSKate Stone CommandReturnObject &result) { 242b9c1b51eSKate Stone m_interpreter.GetLLDBCommandsFromIOHandler( 243b9c1b51eSKate Stone "> ", // Prompt 24444d93782SGreg Clayton *this, // IOHandlerDelegate 24544d93782SGreg Clayton true, // Run IOHandler in async mode 246b9c1b51eSKate Stone wp_options); // Baton for the "io_handler" that will be passed back into 247b9c1b51eSKate Stone // our IOHandlerDelegate functions 248e9a5627eSJohnny Chen } 249e9a5627eSJohnny Chen 250e9a5627eSJohnny Chen /// Set a one-liner as the callback for the watchpoint. 251b9c1b51eSKate Stone void SetWatchpointCommandCallback(WatchpointOptions *wp_options, 252b9c1b51eSKate Stone const char *oneliner) { 253b9c1b51eSKate Stone std::unique_ptr<WatchpointOptions::CommandData> data_ap( 254b9c1b51eSKate Stone new WatchpointOptions::CommandData()); 255e9a5627eSJohnny Chen 256e9a5627eSJohnny Chen // It's necessary to set both user_source and script_source to the oneliner. 257b9c1b51eSKate Stone // The former is used to generate callback description (as in watchpoint 258b9c1b51eSKate Stone // command list) 259b9c1b51eSKate Stone // while the latter is used for Python to interpret during the actual 260b9c1b51eSKate Stone // 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 289b9c1b51eSKate Stone // output will come out synchronously 290e9a5627eSJohnny Chen // if the debugger is set up that 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 322fe11483bSZachary Turner Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 323b9c1b51eSKate Stone ExecutionContext *execution_context) override { 324e9a5627eSJohnny Chen Error 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': 334b9c1b51eSKate Stone m_script_language = (lldb::ScriptLanguage)Args::StringToOptionEnum( 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; 344fe11483bSZachary Turner m_stop_on_error = Args::StringToBoolean(option_arg, false, &success); 345e9a5627eSJohnny Chen if (!success) 346b9c1b51eSKate Stone error.SetErrorStringWithFormat( 347fe11483bSZachary Turner "invalid value for stop-on-error: \"%s\"", 348fe11483bSZachary Turner option_arg.str().c_str()); 349b9c1b51eSKate Stone } break; 350e9a5627eSJohnny Chen 351e9a5627eSJohnny Chen case 'F': 352e9a5627eSJohnny Chen m_use_one_liner = false; 353e9a5627eSJohnny Chen m_use_script_language = true; 354e9a5627eSJohnny Chen m_function_name.assign(option_arg); 355e9a5627eSJohnny Chen break; 356e9a5627eSJohnny Chen 357e9a5627eSJohnny Chen default: 358e9a5627eSJohnny Chen break; 359e9a5627eSJohnny Chen } 360e9a5627eSJohnny Chen return error; 361e9a5627eSJohnny Chen } 36249bcfd80SEugene Zelenko 363b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 364e9a5627eSJohnny Chen m_use_commands = true; 365e9a5627eSJohnny Chen m_use_script_language = false; 366e9a5627eSJohnny Chen m_script_language = eScriptLanguageNone; 367e9a5627eSJohnny Chen 368e9a5627eSJohnny Chen m_use_one_liner = false; 369e9a5627eSJohnny Chen m_stop_on_error = true; 370e9a5627eSJohnny Chen m_one_liner.clear(); 371e9a5627eSJohnny Chen m_function_name.clear(); 372e9a5627eSJohnny Chen } 373e9a5627eSJohnny Chen 3741f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 37570602439SZachary Turner return llvm::makeArrayRef(g_watchpoint_command_add_options); 3761f0f5b5bSZachary Turner } 377e9a5627eSJohnny Chen 378e9a5627eSJohnny Chen // Instance variables to hold the values for command options. 379e9a5627eSJohnny Chen 380e9a5627eSJohnny Chen bool m_use_commands; 381e9a5627eSJohnny Chen bool m_use_script_language; 382e9a5627eSJohnny Chen lldb::ScriptLanguage m_script_language; 383e9a5627eSJohnny Chen 384e9a5627eSJohnny Chen // Instance variables to hold the values for one_liner options. 385e9a5627eSJohnny Chen bool m_use_one_liner; 386e9a5627eSJohnny Chen std::string m_one_liner; 387e9a5627eSJohnny Chen bool m_stop_on_error; 388e9a5627eSJohnny Chen std::string m_function_name; 389e9a5627eSJohnny Chen }; 390e9a5627eSJohnny Chen 391e9a5627eSJohnny Chen protected: 392b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 393e9a5627eSJohnny Chen Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 394e9a5627eSJohnny Chen 395b9c1b51eSKate Stone if (target == nullptr) { 396b9c1b51eSKate Stone result.AppendError("There is not a current executable; there are no " 397b9c1b51eSKate Stone "watchpoints to which to add commands"); 398e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 399e9a5627eSJohnny Chen return false; 400e9a5627eSJohnny Chen } 401e9a5627eSJohnny Chen 402e9a5627eSJohnny Chen const WatchpointList &watchpoints = target->GetWatchpointList(); 403e9a5627eSJohnny Chen size_t num_watchpoints = watchpoints.GetSize(); 404e9a5627eSJohnny Chen 405b9c1b51eSKate Stone if (num_watchpoints == 0) { 406e9a5627eSJohnny Chen result.AppendError("No watchpoints exist to have commands added"); 407e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 408e9a5627eSJohnny Chen return false; 409e9a5627eSJohnny Chen } 410e9a5627eSJohnny Chen 411b9c1b51eSKate Stone if (!m_options.m_use_script_language && 412b9c1b51eSKate Stone !m_options.m_function_name.empty()) { 413b9c1b51eSKate Stone result.AppendError("need to enable scripting to have a function run as a " 414b9c1b51eSKate Stone "watchpoint command"); 415e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 416e9a5627eSJohnny Chen return false; 417e9a5627eSJohnny Chen } 418e9a5627eSJohnny Chen 419e9a5627eSJohnny Chen std::vector<uint32_t> valid_wp_ids; 420b9c1b51eSKate Stone if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, 421b9c1b51eSKate Stone valid_wp_ids)) { 422e9a5627eSJohnny Chen result.AppendError("Invalid watchpoints specification."); 423e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 424e9a5627eSJohnny Chen return false; 425e9a5627eSJohnny Chen } 426e9a5627eSJohnny Chen 427e9a5627eSJohnny Chen result.SetStatus(eReturnStatusSuccessFinishNoResult); 428e9a5627eSJohnny Chen const size_t count = valid_wp_ids.size(); 429b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 430e9a5627eSJohnny Chen uint32_t cur_wp_id = valid_wp_ids.at(i); 431b9c1b51eSKate Stone if (cur_wp_id != LLDB_INVALID_WATCH_ID) { 432e9a5627eSJohnny Chen Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get(); 433e9a5627eSJohnny Chen // Sanity check wp first. 434b9c1b51eSKate Stone if (wp == nullptr) 435b9c1b51eSKate Stone continue; 436e9a5627eSJohnny Chen 437e9a5627eSJohnny Chen WatchpointOptions *wp_options = wp->GetOptions(); 438e9a5627eSJohnny Chen // Skip this watchpoint if wp_options is not good. 439b9c1b51eSKate Stone if (wp_options == nullptr) 440b9c1b51eSKate Stone continue; 441e9a5627eSJohnny Chen 442e9a5627eSJohnny Chen // If we are using script language, get the script interpreter 443e9a5627eSJohnny Chen // in order to set or collect command callback. Otherwise, call 444e9a5627eSJohnny Chen // the methods associated with this object. 445b9c1b51eSKate Stone if (m_options.m_use_script_language) { 446e9a5627eSJohnny Chen // Special handling for one-liner specified inline. 447b9c1b51eSKate Stone if (m_options.m_use_one_liner) { 448b9c1b51eSKate Stone m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback( 449b9c1b51eSKate Stone wp_options, m_options.m_one_liner.c_str()); 450e9a5627eSJohnny Chen } 451e9a5627eSJohnny Chen // Special handling for using a Python function by name 452b9c1b51eSKate Stone // instead of extending the watchpoint callback data structures, we 453b9c1b51eSKate Stone // just automatize 454b9c1b51eSKate Stone // what the user would do manually: make their watchpoint command be a 455b9c1b51eSKate Stone // 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 " 6727428a18cSKate Stone "executed when the watchpoint is hit (watchpoint 'commmands').", 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