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