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