180814287SRaphael Isemann //===-- CommandObjectWatchpointCommand.cpp --------------------------------===// 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 22e9a5627eSJohnny Chen using namespace lldb; 23e9a5627eSJohnny Chen using namespace lldb_private; 24e9a5627eSJohnny Chen 251f0f5b5bSZachary Turner // FIXME: "script-type" needs to have its contents determined dynamically, so 26e063ecccSJonas Devlieghere // somebody can add a new scripting language to lldb and have it pickable here 27e063ecccSJonas Devlieghere // without having to change this enumeration by hand and rebuild lldb proper. 288fe53c49STatyana Krasnukha static constexpr OptionEnumValueElement g_script_option_enumeration[] = { 29e063ecccSJonas Devlieghere { 30e063ecccSJonas Devlieghere eScriptLanguageNone, 31e063ecccSJonas Devlieghere "command", 32e063ecccSJonas Devlieghere "Commands are in the lldb command interpreter language", 33e063ecccSJonas Devlieghere }, 34e063ecccSJonas Devlieghere { 35e063ecccSJonas Devlieghere eScriptLanguagePython, 36e063ecccSJonas Devlieghere "python", 37e063ecccSJonas Devlieghere "Commands are in the Python language.", 38e063ecccSJonas Devlieghere }, 39e063ecccSJonas Devlieghere { 4068cb7d85SJonas Devlieghere eScriptLanguageLua, 4168cb7d85SJonas Devlieghere "lua", 42af1b7d06SEd Maste "Commands are in the Lua language.", 4368cb7d85SJonas Devlieghere }, 4468cb7d85SJonas Devlieghere { 45e063ecccSJonas Devlieghere eSortOrderByName, 46e063ecccSJonas Devlieghere "default-script", 47e063ecccSJonas Devlieghere "Commands are in the default scripting language.", 48e063ecccSJonas Devlieghere }, 49e063ecccSJonas Devlieghere }; 501f0f5b5bSZachary Turner 518fe53c49STatyana Krasnukha static constexpr OptionEnumValues ScriptOptionEnum() { 528fe53c49STatyana Krasnukha return OptionEnumValues(g_script_option_enumeration); 538fe53c49STatyana Krasnukha } 548fe53c49STatyana Krasnukha 5560bd7a9cSRaphael Isemann #define LLDB_OPTIONS_watchpoint_command_add 5660bd7a9cSRaphael Isemann #include "CommandOptions.inc" 571f0f5b5bSZachary Turner 58b9c1b51eSKate Stone class CommandObjectWatchpointCommandAdd : public CommandObjectParsed, 59b9c1b51eSKate Stone public IOHandlerDelegateMultiline { 60e9a5627eSJohnny Chen public: 617428a18cSKate Stone CommandObjectWatchpointCommandAdd(CommandInterpreter &interpreter) 62b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "add", 63b9c1b51eSKate Stone "Add a set of LLDB commands to a watchpoint, to be " 64*60ad0fd3SJim Ingham "executed whenever the watchpoint is hit. " 65*60ad0fd3SJim Ingham "The commands added to the watchpoint replace any " 66*60ad0fd3SJim Ingham "commands previously added to it.", 6704a4c091SRaphael Isemann nullptr, eCommandRequiresTarget), 68b9c1b51eSKate Stone IOHandlerDelegateMultiline("DONE", 69b9c1b51eSKate Stone IOHandlerDelegate::Completion::LLDBCommand), 70b9c1b51eSKate Stone m_options() { 71e9a5627eSJohnny Chen SetHelpLong( 72ea671fbdSKate Stone R"( 73ea671fbdSKate Stone General information about entering watchpoint commands 74ea671fbdSKate Stone ------------------------------------------------------ 75ea671fbdSKate Stone 76b9c1b51eSKate Stone )" 77b9c1b51eSKate Stone "This command will prompt for commands to be executed when the specified \ 78ea671fbdSKate Stone watchpoint is hit. Each command is typed on its own line following the '> ' \ 79b9c1b51eSKate Stone prompt until 'DONE' is entered." 80b9c1b51eSKate Stone R"( 81ea671fbdSKate Stone 82b9c1b51eSKate Stone )" 83b9c1b51eSKate Stone "Syntactic errors may not be detected when initially entered, and many \ 84ea671fbdSKate Stone malformed commands can silently fail when executed. If your watchpoint commands \ 85b9c1b51eSKate Stone do not appear to be executing, double-check the command syntax." 86b9c1b51eSKate Stone R"( 87ea671fbdSKate Stone 88b9c1b51eSKate Stone )" 89b9c1b51eSKate Stone "Note: You may enter any debugger command exactly as you would at the debugger \ 90ea671fbdSKate Stone prompt. There is no limit to the number of commands supplied, but do NOT enter \ 91b9c1b51eSKate Stone more than one command per line." 92b9c1b51eSKate Stone R"( 93ea671fbdSKate Stone 94ea671fbdSKate Stone Special information about PYTHON watchpoint commands 95ea671fbdSKate Stone ---------------------------------------------------- 96ea671fbdSKate Stone 97b9c1b51eSKate Stone )" 98b9c1b51eSKate Stone "You may enter either one or more lines of Python, including function \ 99ea671fbdSKate Stone definitions or calls to functions that will have been imported by the time \ 100ea671fbdSKate Stone the code executes. Single line watchpoint commands will be interpreted 'as is' \ 101ea671fbdSKate Stone when the watchpoint is hit. Multiple lines of Python will be wrapped in a \ 102b9c1b51eSKate Stone generated function, and a call to the function will be attached to the watchpoint." 103b9c1b51eSKate Stone R"( 104ea671fbdSKate Stone 105ea671fbdSKate Stone This auto-generated function is passed in three arguments: 106ea671fbdSKate Stone 107ea671fbdSKate Stone frame: an lldb.SBFrame object for the frame which hit the watchpoint. 108ea671fbdSKate Stone 109ea671fbdSKate Stone wp: the watchpoint that was hit. 110ea671fbdSKate Stone 111b9c1b51eSKate Stone )" 112b9c1b51eSKate Stone "When specifying a python function with the --python-function option, you need \ 113b9c1b51eSKate Stone to supply the function name prepended by the module name:" 114b9c1b51eSKate Stone R"( 115ea671fbdSKate Stone 116ea671fbdSKate Stone --python-function myutils.watchpoint_callback 117ea671fbdSKate Stone 118ea671fbdSKate Stone The function itself must have the following prototype: 119ea671fbdSKate Stone 120ea671fbdSKate Stone def watchpoint_callback(frame, wp): 121ea671fbdSKate Stone # Your code goes here 122ea671fbdSKate Stone 123b9c1b51eSKate Stone )" 124b9c1b51eSKate Stone "The arguments are the same as the arguments passed to generated functions as \ 125ea671fbdSKate Stone described above. Note that the global variable 'lldb.frame' will NOT be updated when \ 126ea671fbdSKate Stone this function is called, so be sure to use the 'frame' argument. The 'frame' argument \ 127ea671fbdSKate Stone can get you to the thread via frame.GetThread(), the thread can get you to the \ 128ea671fbdSKate Stone process via thread.GetProcess(), and the process can get you back to the target \ 129b9c1b51eSKate Stone via process.GetTarget()." 130b9c1b51eSKate Stone R"( 131ea671fbdSKate Stone 132b9c1b51eSKate Stone )" 133b9c1b51eSKate Stone "Important Note: As Python code gets collected into functions, access to global \ 134ea671fbdSKate Stone variables requires explicit scoping using the 'global' keyword. Be sure to use correct \ 135b9c1b51eSKate Stone Python syntax, including indentation, when entering Python watchpoint commands." 136b9c1b51eSKate Stone R"( 137ea671fbdSKate Stone 138ea671fbdSKate Stone Example Python one-line watchpoint command: 139ea671fbdSKate Stone 140ea671fbdSKate Stone (lldb) watchpoint command add -s python 1 141ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 142ea671fbdSKate Stone > print "Hit this watchpoint!" 143ea671fbdSKate Stone > DONE 144ea671fbdSKate Stone 145ea671fbdSKate Stone As a convenience, this also works for a short Python one-liner: 146ea671fbdSKate Stone 147ea671fbdSKate Stone (lldb) watchpoint command add -s python 1 -o 'import time; print time.asctime()' 148ea671fbdSKate Stone (lldb) run 149ea671fbdSKate Stone Launching '.../a.out' (x86_64) 150ea671fbdSKate Stone (lldb) Fri Sep 10 12:17:45 2010 151ea671fbdSKate Stone Process 21778 Stopped 152ea671fbdSKate Stone * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = watchpoint 1.1, queue = com.apple.main-thread 153ea671fbdSKate Stone 36 154ea671fbdSKate Stone 37 int c(int val) 155ea671fbdSKate Stone 38 { 156ea671fbdSKate Stone 39 -> return val + 3; 157ea671fbdSKate Stone 40 } 158ea671fbdSKate Stone 41 159ea671fbdSKate Stone 42 int main (int argc, char const *argv[]) 160ea671fbdSKate Stone 161ea671fbdSKate Stone Example multiple line Python watchpoint command, using function definition: 162ea671fbdSKate Stone 163ea671fbdSKate Stone (lldb) watchpoint command add -s python 1 164ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 165ea671fbdSKate Stone > def watchpoint_output (wp_no): 166ea671fbdSKate Stone > out_string = "Hit watchpoint number " + repr (wp_no) 167ea671fbdSKate Stone > print out_string 168ea671fbdSKate Stone > return True 169ea671fbdSKate Stone > watchpoint_output (1) 170ea671fbdSKate Stone > DONE 171ea671fbdSKate Stone 172ea671fbdSKate Stone Example multiple line Python watchpoint command, using 'loose' Python: 173ea671fbdSKate Stone 174ea671fbdSKate Stone (lldb) watchpoint command add -s p 1 175ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 176ea671fbdSKate Stone > global wp_count 177ea671fbdSKate Stone > wp_count = wp_count + 1 178ea671fbdSKate Stone > print "Hit this watchpoint " + repr(wp_count) + " times!" 179ea671fbdSKate Stone > DONE 180ea671fbdSKate Stone 181b9c1b51eSKate Stone )" 182b9c1b51eSKate Stone "In this case, since there is a reference to a global variable, \ 183ea671fbdSKate Stone 'wp_count', you will also need to make sure 'wp_count' exists and is \ 184b9c1b51eSKate Stone initialized:" 185b9c1b51eSKate Stone R"( 186ea671fbdSKate Stone 187ea671fbdSKate Stone (lldb) script 188ea671fbdSKate Stone >>> wp_count = 0 189ea671fbdSKate Stone >>> quit() 190ea671fbdSKate Stone 191b9c1b51eSKate Stone )" 192b9c1b51eSKate Stone "Final Note: A warning that no watchpoint command was generated when there \ 193b9c1b51eSKate Stone are no syntax errors may indicate that a function was declared but never called."); 194e9a5627eSJohnny Chen 195e9a5627eSJohnny Chen CommandArgumentEntry arg; 196e9a5627eSJohnny Chen CommandArgumentData wp_id_arg; 197e9a5627eSJohnny Chen 198e9a5627eSJohnny Chen // Define the first (and only) variant of this arg. 199e9a5627eSJohnny Chen wp_id_arg.arg_type = eArgTypeWatchpointID; 200e9a5627eSJohnny Chen wp_id_arg.arg_repetition = eArgRepeatPlain; 201e9a5627eSJohnny Chen 202b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 203b9c1b51eSKate Stone // argument entry. 204e9a5627eSJohnny Chen arg.push_back(wp_id_arg); 205e9a5627eSJohnny Chen 206e9a5627eSJohnny Chen // Push the data for the first argument into the m_arguments vector. 207e9a5627eSJohnny Chen m_arguments.push_back(arg); 208e9a5627eSJohnny Chen } 209e9a5627eSJohnny Chen 21049bcfd80SEugene Zelenko ~CommandObjectWatchpointCommandAdd() override = default; 211e9a5627eSJohnny Chen 212b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 213e9a5627eSJohnny Chen 2140affb582SDave Lee void IOHandlerActivated(IOHandler &io_handler, bool interactive) override { 2157ca15ba7SLawrence D'Anna StreamFileSP output_sp(io_handler.GetOutputStreamFileSP()); 2160affb582SDave Lee if (output_sp && interactive) { 217b9c1b51eSKate Stone output_sp->PutCString( 218b9c1b51eSKate Stone "Enter your debugger command(s). Type 'DONE' to end.\n"); 21944d93782SGreg Clayton output_sp->Flush(); 22044d93782SGreg Clayton } 22144d93782SGreg Clayton } 22244d93782SGreg Clayton 223b9c1b51eSKate Stone void IOHandlerInputComplete(IOHandler &io_handler, 224b9c1b51eSKate Stone std::string &line) override { 22544d93782SGreg Clayton io_handler.SetIsDone(true); 22644d93782SGreg Clayton 227b9c1b51eSKate Stone // The WatchpointOptions object is owned by the watchpoint or watchpoint 228b9c1b51eSKate Stone // location 229b9c1b51eSKate Stone WatchpointOptions *wp_options = 230b9c1b51eSKate Stone (WatchpointOptions *)io_handler.GetUserData(); 231b9c1b51eSKate Stone if (wp_options) { 232d5b44036SJonas Devlieghere std::unique_ptr<WatchpointOptions::CommandData> data_up( 233b9c1b51eSKate Stone new WatchpointOptions::CommandData()); 234d5b44036SJonas Devlieghere if (data_up) { 235d5b44036SJonas Devlieghere data_up->user_source.SplitIntoLines(line); 2364e4fbe82SZachary Turner auto baton_sp = std::make_shared<WatchpointOptions::CommandBaton>( 237d5b44036SJonas Devlieghere std::move(data_up)); 23844d93782SGreg Clayton wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp); 23944d93782SGreg Clayton } 24044d93782SGreg Clayton } 24144d93782SGreg Clayton } 24244d93782SGreg Clayton 243b9c1b51eSKate Stone void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, 244b9c1b51eSKate Stone CommandReturnObject &result) { 245b9c1b51eSKate Stone m_interpreter.GetLLDBCommandsFromIOHandler( 246b9c1b51eSKate Stone "> ", // Prompt 24744d93782SGreg Clayton *this, // IOHandlerDelegate 248b9c1b51eSKate Stone wp_options); // Baton for the "io_handler" that will be passed back into 249b9c1b51eSKate Stone // our IOHandlerDelegate functions 250e9a5627eSJohnny Chen } 251e9a5627eSJohnny Chen 252e9a5627eSJohnny Chen /// Set a one-liner as the callback for the watchpoint. 253b9c1b51eSKate Stone void SetWatchpointCommandCallback(WatchpointOptions *wp_options, 254b9c1b51eSKate Stone const char *oneliner) { 255d5b44036SJonas Devlieghere std::unique_ptr<WatchpointOptions::CommandData> data_up( 256b9c1b51eSKate Stone new WatchpointOptions::CommandData()); 257e9a5627eSJohnny Chen 25805097246SAdrian Prantl // It's necessary to set both user_source and script_source to the 25905097246SAdrian Prantl // oneliner. The former is used to generate callback description (as in 26005097246SAdrian Prantl // watchpoint command list) while the latter is used for Python to 26105097246SAdrian Prantl // interpret during the actual callback. 262d5b44036SJonas Devlieghere data_up->user_source.AppendString(oneliner); 263d5b44036SJonas Devlieghere data_up->script_source.assign(oneliner); 264d5b44036SJonas Devlieghere data_up->stop_on_error = m_options.m_stop_on_error; 265e9a5627eSJohnny Chen 2664e4fbe82SZachary Turner auto baton_sp = 267d5b44036SJonas Devlieghere std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up)); 268e9a5627eSJohnny Chen wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp); 269e9a5627eSJohnny Chen } 270e9a5627eSJohnny Chen 271e9a5627eSJohnny Chen static bool 272e9a5627eSJohnny Chen WatchpointOptionsCallbackFunction(void *baton, 273e9a5627eSJohnny Chen StoppointCallbackContext *context, 274b9c1b51eSKate Stone lldb::user_id_t watch_id) { 275e9a5627eSJohnny Chen bool ret_value = true; 27649bcfd80SEugene Zelenko if (baton == nullptr) 277e9a5627eSJohnny Chen return true; 278e9a5627eSJohnny Chen 279b9c1b51eSKate Stone WatchpointOptions::CommandData *data = 280b9c1b51eSKate Stone (WatchpointOptions::CommandData *)baton; 281e9a5627eSJohnny Chen StringList &commands = data->user_source; 282e9a5627eSJohnny Chen 283b9c1b51eSKate Stone if (commands.GetSize() > 0) { 284e9a5627eSJohnny Chen ExecutionContext exe_ctx(context->exe_ctx_ref); 285e9a5627eSJohnny Chen Target *target = exe_ctx.GetTargetPtr(); 286b9c1b51eSKate Stone if (target) { 287e9a5627eSJohnny Chen Debugger &debugger = target->GetDebugger(); 288de019b88SJonas Devlieghere CommandReturnObject result(debugger.GetUseColor()); 289de019b88SJonas Devlieghere 290b9c1b51eSKate Stone // Rig up the results secondary output stream to the debugger's, so the 29105097246SAdrian Prantl // output will come out synchronously if the debugger is set up that 29205097246SAdrian Prantl // way. 293e9a5627eSJohnny Chen StreamSP output_stream(debugger.GetAsyncOutputStream()); 294e9a5627eSJohnny Chen StreamSP error_stream(debugger.GetAsyncErrorStream()); 295e9a5627eSJohnny Chen result.SetImmediateOutputStream(output_stream); 296e9a5627eSJohnny Chen result.SetImmediateErrorStream(error_stream); 297e9a5627eSJohnny Chen 29826c7bf93SJim Ingham CommandInterpreterRunOptions options; 29926c7bf93SJim Ingham options.SetStopOnContinue(true); 30026c7bf93SJim Ingham options.SetStopOnError(data->stop_on_error); 30126c7bf93SJim Ingham options.SetEchoCommands(false); 30226c7bf93SJim Ingham options.SetPrintResults(true); 303c0b48ab6SJonas Devlieghere options.SetPrintErrors(true); 30426c7bf93SJim Ingham options.SetAddToHistory(false); 305e9a5627eSJohnny Chen 30636de94cfSTatyana Krasnukha debugger.GetCommandInterpreter().HandleCommands(commands, exe_ctx, 307b9c1b51eSKate Stone options, result); 308e9a5627eSJohnny Chen result.GetImmediateOutputStream()->Flush(); 309e9a5627eSJohnny Chen result.GetImmediateErrorStream()->Flush(); 310e9a5627eSJohnny Chen } 311e9a5627eSJohnny Chen } 312e9a5627eSJohnny Chen return ret_value; 313e9a5627eSJohnny Chen } 314e9a5627eSJohnny Chen 315b9c1b51eSKate Stone class CommandOptions : public Options { 316e9a5627eSJohnny Chen public: 317b9c1b51eSKate Stone CommandOptions() 318b9c1b51eSKate Stone : Options(), m_use_commands(false), m_use_script_language(false), 319b9c1b51eSKate Stone m_script_language(eScriptLanguageNone), m_use_one_liner(false), 320b9c1b51eSKate Stone m_one_liner(), m_function_name() {} 321e9a5627eSJohnny Chen 32249bcfd80SEugene Zelenko ~CommandOptions() override = default; 323e9a5627eSJohnny Chen 32497206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 325b9c1b51eSKate Stone ExecutionContext *execution_context) override { 32697206d57SZachary Turner Status error; 3273bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val; 328e9a5627eSJohnny Chen 329b9c1b51eSKate Stone switch (short_option) { 330e9a5627eSJohnny Chen case 'o': 331e9a5627eSJohnny Chen m_use_one_liner = true; 332adcd0268SBenjamin Kramer m_one_liner = std::string(option_arg); 333e9a5627eSJohnny Chen break; 334e9a5627eSJohnny Chen 335e9a5627eSJohnny Chen case 's': 33647cbf4a0SPavel Labath m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum( 337fe11483bSZachary Turner option_arg, GetDefinitions()[option_idx].enum_values, 338fe11483bSZachary Turner eScriptLanguageNone, error); 339e9a5627eSJohnny Chen 34068cb7d85SJonas Devlieghere switch (m_script_language) { 34168cb7d85SJonas Devlieghere case eScriptLanguagePython: 34268cb7d85SJonas Devlieghere case eScriptLanguageLua: 34368cb7d85SJonas Devlieghere m_use_script_language = true; 34468cb7d85SJonas Devlieghere break; 34568cb7d85SJonas Devlieghere case eScriptLanguageNone: 34668cb7d85SJonas Devlieghere case eScriptLanguageUnknown: 34768cb7d85SJonas Devlieghere m_use_script_language = false; 34868cb7d85SJonas Devlieghere break; 34968cb7d85SJonas Devlieghere } 350e9a5627eSJohnny Chen break; 351e9a5627eSJohnny Chen 352b9c1b51eSKate Stone case 'e': { 353e9a5627eSJohnny Chen bool success = false; 35447cbf4a0SPavel Labath m_stop_on_error = 35547cbf4a0SPavel Labath OptionArgParser::ToBoolean(option_arg, false, &success); 356e9a5627eSJohnny Chen if (!success) 357b9c1b51eSKate Stone error.SetErrorStringWithFormat( 358fe11483bSZachary Turner "invalid value for stop-on-error: \"%s\"", 359fe11483bSZachary Turner option_arg.str().c_str()); 360b9c1b51eSKate Stone } break; 361e9a5627eSJohnny Chen 362e9a5627eSJohnny Chen case 'F': 363e9a5627eSJohnny Chen m_use_one_liner = false; 364adcd0268SBenjamin Kramer m_function_name.assign(std::string(option_arg)); 365e9a5627eSJohnny Chen break; 366e9a5627eSJohnny Chen 367e9a5627eSJohnny Chen default: 36836162014SRaphael Isemann llvm_unreachable("Unimplemented option"); 369e9a5627eSJohnny Chen } 370e9a5627eSJohnny Chen return error; 371e9a5627eSJohnny Chen } 37249bcfd80SEugene Zelenko 373b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 374e9a5627eSJohnny Chen m_use_commands = true; 375e9a5627eSJohnny Chen m_use_script_language = false; 376e9a5627eSJohnny Chen m_script_language = eScriptLanguageNone; 377e9a5627eSJohnny Chen 378e9a5627eSJohnny Chen m_use_one_liner = false; 379e9a5627eSJohnny Chen m_stop_on_error = true; 380e9a5627eSJohnny Chen m_one_liner.clear(); 381e9a5627eSJohnny Chen m_function_name.clear(); 382e9a5627eSJohnny Chen } 383e9a5627eSJohnny Chen 3841f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 38570602439SZachary Turner return llvm::makeArrayRef(g_watchpoint_command_add_options); 3861f0f5b5bSZachary Turner } 387e9a5627eSJohnny Chen 388e9a5627eSJohnny Chen // Instance variables to hold the values for command options. 389e9a5627eSJohnny Chen 390e9a5627eSJohnny Chen bool m_use_commands; 391e9a5627eSJohnny Chen bool m_use_script_language; 392e9a5627eSJohnny Chen lldb::ScriptLanguage m_script_language; 393e9a5627eSJohnny Chen 394e9a5627eSJohnny Chen // Instance variables to hold the values for one_liner options. 395e9a5627eSJohnny Chen bool m_use_one_liner; 396e9a5627eSJohnny Chen std::string m_one_liner; 397e9a5627eSJohnny Chen bool m_stop_on_error; 398e9a5627eSJohnny Chen std::string m_function_name; 399e9a5627eSJohnny Chen }; 400e9a5627eSJohnny Chen 401e9a5627eSJohnny Chen protected: 402b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 40304a4c091SRaphael Isemann Target *target = &GetSelectedTarget(); 404e9a5627eSJohnny Chen 405e9a5627eSJohnny Chen const WatchpointList &watchpoints = target->GetWatchpointList(); 406e9a5627eSJohnny Chen size_t num_watchpoints = watchpoints.GetSize(); 407e9a5627eSJohnny Chen 408b9c1b51eSKate Stone if (num_watchpoints == 0) { 409e9a5627eSJohnny Chen result.AppendError("No watchpoints exist to have commands added"); 410e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 411e9a5627eSJohnny Chen return false; 412e9a5627eSJohnny Chen } 413e9a5627eSJohnny Chen 41468cb7d85SJonas Devlieghere if (!m_options.m_function_name.empty()) { 41568cb7d85SJonas Devlieghere if (!m_options.m_use_script_language) { 41668cb7d85SJonas Devlieghere m_options.m_script_language = GetDebugger().GetScriptLanguage(); 41768cb7d85SJonas Devlieghere m_options.m_use_script_language = true; 41868cb7d85SJonas Devlieghere } 419e9a5627eSJohnny Chen } 420e9a5627eSJohnny Chen 421e9a5627eSJohnny Chen std::vector<uint32_t> valid_wp_ids; 422b9c1b51eSKate Stone if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, 423b9c1b51eSKate Stone valid_wp_ids)) { 424e9a5627eSJohnny Chen result.AppendError("Invalid watchpoints specification."); 425e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 426e9a5627eSJohnny Chen return false; 427e9a5627eSJohnny Chen } 428e9a5627eSJohnny Chen 429e9a5627eSJohnny Chen result.SetStatus(eReturnStatusSuccessFinishNoResult); 430e9a5627eSJohnny Chen const size_t count = valid_wp_ids.size(); 431b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 432e9a5627eSJohnny Chen uint32_t cur_wp_id = valid_wp_ids.at(i); 433b9c1b51eSKate Stone if (cur_wp_id != LLDB_INVALID_WATCH_ID) { 434e9a5627eSJohnny Chen Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get(); 435e9a5627eSJohnny Chen // Sanity check wp first. 436b9c1b51eSKate Stone if (wp == nullptr) 437b9c1b51eSKate Stone continue; 438e9a5627eSJohnny Chen 439e9a5627eSJohnny Chen WatchpointOptions *wp_options = wp->GetOptions(); 440e9a5627eSJohnny Chen // Skip this watchpoint if wp_options is not good. 441b9c1b51eSKate Stone if (wp_options == nullptr) 442b9c1b51eSKate Stone continue; 443e9a5627eSJohnny Chen 44405097246SAdrian Prantl // If we are using script language, get the script interpreter in order 44505097246SAdrian Prantl // to set or collect command callback. Otherwise, call the methods 44605097246SAdrian Prantl // associated with this object. 447b9c1b51eSKate Stone if (m_options.m_use_script_language) { 44868cb7d85SJonas Devlieghere ScriptInterpreter *script_interp = GetDebugger().GetScriptInterpreter( 44968cb7d85SJonas Devlieghere /*can_create=*/true, m_options.m_script_language); 450e9a5627eSJohnny Chen // Special handling for one-liner specified inline. 451b9c1b51eSKate Stone if (m_options.m_use_one_liner) { 45268cb7d85SJonas Devlieghere script_interp->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)"; 46268cb7d85SJonas Devlieghere script_interp->SetWatchpointCommandCallback( 463b9c1b51eSKate Stone wp_options, oneliner.c_str()); 464b9c1b51eSKate Stone } else { 46568cb7d85SJonas Devlieghere script_interp->CollectDataForWatchpointCommandCallback(wp_options, 46668cb7d85SJonas Devlieghere 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 // CommandObjectWatchpointCommandDelete 487e9a5627eSJohnny Chen 488b9c1b51eSKate Stone class CommandObjectWatchpointCommandDelete : public CommandObjectParsed { 489e9a5627eSJohnny Chen public: 490b9c1b51eSKate Stone CommandObjectWatchpointCommandDelete(CommandInterpreter &interpreter) 491b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "delete", 492e9a5627eSJohnny Chen "Delete the set of commands from a watchpoint.", 49304a4c091SRaphael Isemann nullptr, eCommandRequiresTarget) { 494e9a5627eSJohnny Chen CommandArgumentEntry arg; 495e9a5627eSJohnny Chen CommandArgumentData wp_id_arg; 496e9a5627eSJohnny Chen 497e9a5627eSJohnny Chen // Define the first (and only) variant of this arg. 498e9a5627eSJohnny Chen wp_id_arg.arg_type = eArgTypeWatchpointID; 499e9a5627eSJohnny Chen wp_id_arg.arg_repetition = eArgRepeatPlain; 500e9a5627eSJohnny Chen 501b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 502b9c1b51eSKate Stone // argument entry. 503e9a5627eSJohnny Chen arg.push_back(wp_id_arg); 504e9a5627eSJohnny Chen 505e9a5627eSJohnny Chen // Push the data for the first argument into the m_arguments vector. 506e9a5627eSJohnny Chen m_arguments.push_back(arg); 507e9a5627eSJohnny Chen } 508e9a5627eSJohnny Chen 50949bcfd80SEugene Zelenko ~CommandObjectWatchpointCommandDelete() override = default; 510e9a5627eSJohnny Chen 511e9a5627eSJohnny Chen protected: 512b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 51304a4c091SRaphael Isemann Target *target = &GetSelectedTarget(); 514e9a5627eSJohnny Chen 515e9a5627eSJohnny Chen const WatchpointList &watchpoints = target->GetWatchpointList(); 516e9a5627eSJohnny Chen size_t num_watchpoints = watchpoints.GetSize(); 517e9a5627eSJohnny Chen 518b9c1b51eSKate Stone if (num_watchpoints == 0) { 519e9a5627eSJohnny Chen result.AppendError("No watchpoints exist to have commands deleted"); 520e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 521e9a5627eSJohnny Chen return false; 522e9a5627eSJohnny Chen } 523e9a5627eSJohnny Chen 524b9c1b51eSKate Stone if (command.GetArgumentCount() == 0) { 525b9c1b51eSKate Stone result.AppendError( 526b9c1b51eSKate Stone "No watchpoint specified from which to delete the commands"); 527e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 528e9a5627eSJohnny Chen return false; 529e9a5627eSJohnny Chen } 530e9a5627eSJohnny Chen 531e9a5627eSJohnny Chen std::vector<uint32_t> valid_wp_ids; 532b9c1b51eSKate Stone if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, 533b9c1b51eSKate Stone valid_wp_ids)) { 534e9a5627eSJohnny Chen result.AppendError("Invalid watchpoints specification."); 535e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 536e9a5627eSJohnny Chen return false; 537e9a5627eSJohnny Chen } 538e9a5627eSJohnny Chen 539e9a5627eSJohnny Chen result.SetStatus(eReturnStatusSuccessFinishNoResult); 540e9a5627eSJohnny Chen const size_t count = valid_wp_ids.size(); 541b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 542e9a5627eSJohnny Chen uint32_t cur_wp_id = valid_wp_ids.at(i); 543b9c1b51eSKate Stone if (cur_wp_id != LLDB_INVALID_WATCH_ID) { 544e9a5627eSJohnny Chen Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get(); 545e9a5627eSJohnny Chen if (wp) 546e9a5627eSJohnny Chen wp->ClearCallback(); 547b9c1b51eSKate Stone } else { 548b9c1b51eSKate Stone result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", cur_wp_id); 549e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 550e9a5627eSJohnny Chen return false; 551e9a5627eSJohnny Chen } 552e9a5627eSJohnny Chen } 553e9a5627eSJohnny Chen return result.Succeeded(); 554e9a5627eSJohnny Chen } 555e9a5627eSJohnny Chen }; 556e9a5627eSJohnny Chen 557e9a5627eSJohnny Chen // CommandObjectWatchpointCommandList 558e9a5627eSJohnny Chen 559b9c1b51eSKate Stone class CommandObjectWatchpointCommandList : public CommandObjectParsed { 560e9a5627eSJohnny Chen public: 561b9c1b51eSKate Stone CommandObjectWatchpointCommandList(CommandInterpreter &interpreter) 56204a4c091SRaphael Isemann : CommandObjectParsed(interpreter, "list", 56304a4c091SRaphael Isemann "List the script or set of commands to be executed " 56404a4c091SRaphael Isemann "when the watchpoint is hit.", 56504a4c091SRaphael Isemann nullptr, eCommandRequiresTarget) { 566e9a5627eSJohnny Chen CommandArgumentEntry arg; 567e9a5627eSJohnny Chen CommandArgumentData wp_id_arg; 568e9a5627eSJohnny Chen 569e9a5627eSJohnny Chen // Define the first (and only) variant of this arg. 570e9a5627eSJohnny Chen wp_id_arg.arg_type = eArgTypeWatchpointID; 571e9a5627eSJohnny Chen wp_id_arg.arg_repetition = eArgRepeatPlain; 572e9a5627eSJohnny Chen 573b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 574b9c1b51eSKate Stone // argument entry. 575e9a5627eSJohnny Chen arg.push_back(wp_id_arg); 576e9a5627eSJohnny Chen 577e9a5627eSJohnny Chen // Push the data for the first argument into the m_arguments vector. 578e9a5627eSJohnny Chen m_arguments.push_back(arg); 579e9a5627eSJohnny Chen } 580e9a5627eSJohnny Chen 58149bcfd80SEugene Zelenko ~CommandObjectWatchpointCommandList() override = default; 582e9a5627eSJohnny Chen 583e9a5627eSJohnny Chen protected: 584b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 58504a4c091SRaphael Isemann Target *target = &GetSelectedTarget(); 586e9a5627eSJohnny Chen 587e9a5627eSJohnny Chen const WatchpointList &watchpoints = target->GetWatchpointList(); 588e9a5627eSJohnny Chen size_t num_watchpoints = watchpoints.GetSize(); 589e9a5627eSJohnny Chen 590b9c1b51eSKate Stone if (num_watchpoints == 0) { 591e9a5627eSJohnny Chen result.AppendError("No watchpoints exist for which to list commands"); 592e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 593e9a5627eSJohnny Chen return false; 594e9a5627eSJohnny Chen } 595e9a5627eSJohnny Chen 596b9c1b51eSKate Stone if (command.GetArgumentCount() == 0) { 597b9c1b51eSKate Stone result.AppendError( 598b9c1b51eSKate Stone "No watchpoint specified for which to list the commands"); 599e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 600e9a5627eSJohnny Chen return false; 601e9a5627eSJohnny Chen } 602e9a5627eSJohnny Chen 603e9a5627eSJohnny Chen std::vector<uint32_t> valid_wp_ids; 604b9c1b51eSKate Stone if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, 605b9c1b51eSKate Stone valid_wp_ids)) { 606e9a5627eSJohnny Chen result.AppendError("Invalid watchpoints specification."); 607e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 608e9a5627eSJohnny Chen return false; 609e9a5627eSJohnny Chen } 610e9a5627eSJohnny Chen 611e9a5627eSJohnny Chen result.SetStatus(eReturnStatusSuccessFinishNoResult); 612e9a5627eSJohnny Chen const size_t count = valid_wp_ids.size(); 613b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 614e9a5627eSJohnny Chen uint32_t cur_wp_id = valid_wp_ids.at(i); 615b9c1b51eSKate Stone if (cur_wp_id != LLDB_INVALID_WATCH_ID) { 616e9a5627eSJohnny Chen Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get(); 617e9a5627eSJohnny Chen 618b9c1b51eSKate Stone if (wp) { 619e9a5627eSJohnny Chen const WatchpointOptions *wp_options = wp->GetOptions(); 620b9c1b51eSKate Stone if (wp_options) { 621e9a5627eSJohnny Chen // Get the callback baton associated with the current watchpoint. 622e9a5627eSJohnny Chen const Baton *baton = wp_options->GetBaton(); 623b9c1b51eSKate Stone if (baton) { 624e9a5627eSJohnny Chen result.GetOutputStream().Printf("Watchpoint %u:\n", cur_wp_id); 6254f728bfcSRaphael Isemann baton->GetDescription(result.GetOutputStream().AsRawOstream(), 6264f728bfcSRaphael Isemann eDescriptionLevelFull, 6274f728bfcSRaphael Isemann result.GetOutputStream().GetIndentLevel() + 6284f728bfcSRaphael Isemann 2); 629b9c1b51eSKate Stone } else { 630b9c1b51eSKate Stone result.AppendMessageWithFormat( 631b9c1b51eSKate Stone "Watchpoint %u does not have an associated command.\n", 632e9a5627eSJohnny Chen cur_wp_id); 633e9a5627eSJohnny Chen } 634e9a5627eSJohnny Chen } 635e9a5627eSJohnny Chen result.SetStatus(eReturnStatusSuccessFinishResult); 636b9c1b51eSKate Stone } else { 637b9c1b51eSKate Stone result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", 638b9c1b51eSKate Stone cur_wp_id); 639e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 640e9a5627eSJohnny Chen } 641e9a5627eSJohnny Chen } 642e9a5627eSJohnny Chen } 643e9a5627eSJohnny Chen 644e9a5627eSJohnny Chen return result.Succeeded(); 645e9a5627eSJohnny Chen } 646e9a5627eSJohnny Chen }; 647e9a5627eSJohnny Chen 648e9a5627eSJohnny Chen // CommandObjectWatchpointCommand 649e9a5627eSJohnny Chen 650b9c1b51eSKate Stone CommandObjectWatchpointCommand::CommandObjectWatchpointCommand( 651b9c1b51eSKate Stone CommandInterpreter &interpreter) 652b9c1b51eSKate Stone : CommandObjectMultiword( 653b9c1b51eSKate Stone interpreter, "command", 654b9c1b51eSKate Stone "Commands for adding, removing and examining LLDB commands " 6554ebdee0aSBruce Mitchener "executed when the watchpoint is hit (watchpoint 'commands').", 656b9c1b51eSKate Stone "command <sub-command> [<sub-command-options>] <watchpoint-id>") { 657b9c1b51eSKate Stone CommandObjectSP add_command_object( 658b9c1b51eSKate Stone new CommandObjectWatchpointCommandAdd(interpreter)); 659b9c1b51eSKate Stone CommandObjectSP delete_command_object( 660b9c1b51eSKate Stone new CommandObjectWatchpointCommandDelete(interpreter)); 661b9c1b51eSKate Stone CommandObjectSP list_command_object( 662b9c1b51eSKate Stone new CommandObjectWatchpointCommandList(interpreter)); 663e9a5627eSJohnny Chen 664e9a5627eSJohnny Chen add_command_object->SetCommandName("watchpoint command add"); 665e9a5627eSJohnny Chen delete_command_object->SetCommandName("watchpoint command delete"); 666e9a5627eSJohnny Chen list_command_object->SetCommandName("watchpoint command list"); 667e9a5627eSJohnny Chen 66803da4cc2SGreg Clayton LoadSubCommand("add", add_command_object); 66903da4cc2SGreg Clayton LoadSubCommand("delete", delete_command_object); 67003da4cc2SGreg Clayton LoadSubCommand("list", list_command_object); 671e9a5627eSJohnny Chen } 672e9a5627eSJohnny Chen 67349bcfd80SEugene Zelenko CommandObjectWatchpointCommand::~CommandObjectWatchpointCommand() = default; 674