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 271f0f5b5bSZachary Turner // FIXME: "script-type" needs to have its contents determined dynamically, so 28e063ecccSJonas Devlieghere // somebody can add a new scripting language to lldb and have it pickable here 29e063ecccSJonas Devlieghere // without having to change this enumeration by hand and rebuild lldb proper. 308fe53c49STatyana Krasnukha static constexpr OptionEnumValueElement g_script_option_enumeration[] = { 31e063ecccSJonas Devlieghere { 32e063ecccSJonas Devlieghere eScriptLanguageNone, 33e063ecccSJonas Devlieghere "command", 34e063ecccSJonas Devlieghere "Commands are in the lldb command interpreter language", 35e063ecccSJonas Devlieghere }, 36e063ecccSJonas Devlieghere { 37e063ecccSJonas Devlieghere eScriptLanguagePython, 38e063ecccSJonas Devlieghere "python", 39e063ecccSJonas Devlieghere "Commands are in the Python language.", 40e063ecccSJonas Devlieghere }, 41e063ecccSJonas Devlieghere { 42e063ecccSJonas Devlieghere eSortOrderByName, 43e063ecccSJonas Devlieghere "default-script", 44e063ecccSJonas Devlieghere "Commands are in the default scripting language.", 45e063ecccSJonas Devlieghere }, 46e063ecccSJonas Devlieghere }; 471f0f5b5bSZachary Turner 488fe53c49STatyana Krasnukha static constexpr OptionEnumValues ScriptOptionEnum() { 498fe53c49STatyana Krasnukha return OptionEnumValues(g_script_option_enumeration); 508fe53c49STatyana Krasnukha } 518fe53c49STatyana Krasnukha 5260bd7a9cSRaphael Isemann #define LLDB_OPTIONS_watchpoint_command_add 5360bd7a9cSRaphael Isemann #include "CommandOptions.inc" 541f0f5b5bSZachary Turner 55b9c1b51eSKate Stone class CommandObjectWatchpointCommandAdd : public CommandObjectParsed, 56b9c1b51eSKate Stone public IOHandlerDelegateMultiline { 57e9a5627eSJohnny Chen public: 587428a18cSKate Stone CommandObjectWatchpointCommandAdd(CommandInterpreter &interpreter) 59b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "add", 60b9c1b51eSKate Stone "Add a set of LLDB commands to a watchpoint, to be " 61b9c1b51eSKate Stone "executed whenever the watchpoint is hit.", 6204a4c091SRaphael Isemann nullptr, eCommandRequiresTarget), 63b9c1b51eSKate Stone IOHandlerDelegateMultiline("DONE", 64b9c1b51eSKate Stone IOHandlerDelegate::Completion::LLDBCommand), 65b9c1b51eSKate Stone m_options() { 66e9a5627eSJohnny Chen SetHelpLong( 67ea671fbdSKate Stone R"( 68ea671fbdSKate Stone General information about entering watchpoint commands 69ea671fbdSKate Stone ------------------------------------------------------ 70ea671fbdSKate Stone 71b9c1b51eSKate Stone )" 72b9c1b51eSKate Stone "This command will prompt for commands to be executed when the specified \ 73ea671fbdSKate Stone watchpoint is hit. Each command is typed on its own line following the '> ' \ 74b9c1b51eSKate Stone prompt until 'DONE' is entered." 75b9c1b51eSKate Stone R"( 76ea671fbdSKate Stone 77b9c1b51eSKate Stone )" 78b9c1b51eSKate Stone "Syntactic errors may not be detected when initially entered, and many \ 79ea671fbdSKate Stone malformed commands can silently fail when executed. If your watchpoint commands \ 80b9c1b51eSKate Stone do not appear to be executing, double-check the command syntax." 81b9c1b51eSKate Stone R"( 82ea671fbdSKate Stone 83b9c1b51eSKate Stone )" 84b9c1b51eSKate Stone "Note: You may enter any debugger command exactly as you would at the debugger \ 85ea671fbdSKate Stone prompt. There is no limit to the number of commands supplied, but do NOT enter \ 86b9c1b51eSKate Stone more than one command per line." 87b9c1b51eSKate Stone R"( 88ea671fbdSKate Stone 89ea671fbdSKate Stone Special information about PYTHON watchpoint commands 90ea671fbdSKate Stone ---------------------------------------------------- 91ea671fbdSKate Stone 92b9c1b51eSKate Stone )" 93b9c1b51eSKate Stone "You may enter either one or more lines of Python, including function \ 94ea671fbdSKate Stone definitions or calls to functions that will have been imported by the time \ 95ea671fbdSKate Stone the code executes. Single line watchpoint commands will be interpreted 'as is' \ 96ea671fbdSKate Stone when the watchpoint is hit. Multiple lines of Python will be wrapped in a \ 97b9c1b51eSKate Stone generated function, and a call to the function will be attached to the watchpoint." 98b9c1b51eSKate Stone R"( 99ea671fbdSKate Stone 100ea671fbdSKate Stone This auto-generated function is passed in three arguments: 101ea671fbdSKate Stone 102ea671fbdSKate Stone frame: an lldb.SBFrame object for the frame which hit the watchpoint. 103ea671fbdSKate Stone 104ea671fbdSKate Stone wp: the watchpoint that was hit. 105ea671fbdSKate Stone 106b9c1b51eSKate Stone )" 107b9c1b51eSKate Stone "When specifying a python function with the --python-function option, you need \ 108b9c1b51eSKate Stone to supply the function name prepended by the module name:" 109b9c1b51eSKate Stone R"( 110ea671fbdSKate Stone 111ea671fbdSKate Stone --python-function myutils.watchpoint_callback 112ea671fbdSKate Stone 113ea671fbdSKate Stone The function itself must have the following prototype: 114ea671fbdSKate Stone 115ea671fbdSKate Stone def watchpoint_callback(frame, wp): 116ea671fbdSKate Stone # Your code goes here 117ea671fbdSKate Stone 118b9c1b51eSKate Stone )" 119b9c1b51eSKate Stone "The arguments are the same as the arguments passed to generated functions as \ 120ea671fbdSKate Stone described above. Note that the global variable 'lldb.frame' will NOT be updated when \ 121ea671fbdSKate Stone this function is called, so be sure to use the 'frame' argument. The 'frame' argument \ 122ea671fbdSKate Stone can get you to the thread via frame.GetThread(), the thread can get you to the \ 123ea671fbdSKate Stone process via thread.GetProcess(), and the process can get you back to the target \ 124b9c1b51eSKate Stone via process.GetTarget()." 125b9c1b51eSKate Stone R"( 126ea671fbdSKate Stone 127b9c1b51eSKate Stone )" 128b9c1b51eSKate Stone "Important Note: As Python code gets collected into functions, access to global \ 129ea671fbdSKate Stone variables requires explicit scoping using the 'global' keyword. Be sure to use correct \ 130b9c1b51eSKate Stone Python syntax, including indentation, when entering Python watchpoint commands." 131b9c1b51eSKate Stone R"( 132ea671fbdSKate Stone 133ea671fbdSKate Stone Example Python one-line watchpoint command: 134ea671fbdSKate Stone 135ea671fbdSKate Stone (lldb) watchpoint command add -s python 1 136ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 137ea671fbdSKate Stone > print "Hit this watchpoint!" 138ea671fbdSKate Stone > DONE 139ea671fbdSKate Stone 140ea671fbdSKate Stone As a convenience, this also works for a short Python one-liner: 141ea671fbdSKate Stone 142ea671fbdSKate Stone (lldb) watchpoint command add -s python 1 -o 'import time; print time.asctime()' 143ea671fbdSKate Stone (lldb) run 144ea671fbdSKate Stone Launching '.../a.out' (x86_64) 145ea671fbdSKate Stone (lldb) Fri Sep 10 12:17:45 2010 146ea671fbdSKate Stone Process 21778 Stopped 147ea671fbdSKate Stone * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = watchpoint 1.1, queue = com.apple.main-thread 148ea671fbdSKate Stone 36 149ea671fbdSKate Stone 37 int c(int val) 150ea671fbdSKate Stone 38 { 151ea671fbdSKate Stone 39 -> return val + 3; 152ea671fbdSKate Stone 40 } 153ea671fbdSKate Stone 41 154ea671fbdSKate Stone 42 int main (int argc, char const *argv[]) 155ea671fbdSKate Stone 156ea671fbdSKate Stone Example multiple line Python watchpoint command, using function definition: 157ea671fbdSKate Stone 158ea671fbdSKate Stone (lldb) watchpoint command add -s python 1 159ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 160ea671fbdSKate Stone > def watchpoint_output (wp_no): 161ea671fbdSKate Stone > out_string = "Hit watchpoint number " + repr (wp_no) 162ea671fbdSKate Stone > print out_string 163ea671fbdSKate Stone > return True 164ea671fbdSKate Stone > watchpoint_output (1) 165ea671fbdSKate Stone > DONE 166ea671fbdSKate Stone 167ea671fbdSKate Stone Example multiple line Python watchpoint command, using 'loose' Python: 168ea671fbdSKate Stone 169ea671fbdSKate Stone (lldb) watchpoint command add -s p 1 170ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 171ea671fbdSKate Stone > global wp_count 172ea671fbdSKate Stone > wp_count = wp_count + 1 173ea671fbdSKate Stone > print "Hit this watchpoint " + repr(wp_count) + " times!" 174ea671fbdSKate Stone > DONE 175ea671fbdSKate Stone 176b9c1b51eSKate Stone )" 177b9c1b51eSKate Stone "In this case, since there is a reference to a global variable, \ 178ea671fbdSKate Stone 'wp_count', you will also need to make sure 'wp_count' exists and is \ 179b9c1b51eSKate Stone initialized:" 180b9c1b51eSKate Stone R"( 181ea671fbdSKate Stone 182ea671fbdSKate Stone (lldb) script 183ea671fbdSKate Stone >>> wp_count = 0 184ea671fbdSKate Stone >>> quit() 185ea671fbdSKate Stone 186b9c1b51eSKate Stone )" 187b9c1b51eSKate Stone "Final Note: A warning that no watchpoint command was generated when there \ 188b9c1b51eSKate Stone are no syntax errors may indicate that a function was declared but never called."); 189e9a5627eSJohnny Chen 190e9a5627eSJohnny Chen CommandArgumentEntry arg; 191e9a5627eSJohnny Chen CommandArgumentData wp_id_arg; 192e9a5627eSJohnny Chen 193e9a5627eSJohnny Chen // Define the first (and only) variant of this arg. 194e9a5627eSJohnny Chen wp_id_arg.arg_type = eArgTypeWatchpointID; 195e9a5627eSJohnny Chen wp_id_arg.arg_repetition = eArgRepeatPlain; 196e9a5627eSJohnny Chen 197b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 198b9c1b51eSKate Stone // argument entry. 199e9a5627eSJohnny Chen arg.push_back(wp_id_arg); 200e9a5627eSJohnny Chen 201e9a5627eSJohnny Chen // Push the data for the first argument into the m_arguments vector. 202e9a5627eSJohnny Chen m_arguments.push_back(arg); 203e9a5627eSJohnny Chen } 204e9a5627eSJohnny Chen 20549bcfd80SEugene Zelenko ~CommandObjectWatchpointCommandAdd() override = default; 206e9a5627eSJohnny Chen 207b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 208e9a5627eSJohnny Chen 2090affb582SDave Lee void IOHandlerActivated(IOHandler &io_handler, bool interactive) override { 2107ca15ba7SLawrence D'Anna StreamFileSP output_sp(io_handler.GetOutputStreamFileSP()); 2110affb582SDave Lee if (output_sp && interactive) { 212b9c1b51eSKate Stone output_sp->PutCString( 213b9c1b51eSKate Stone "Enter your debugger command(s). Type 'DONE' to end.\n"); 21444d93782SGreg Clayton output_sp->Flush(); 21544d93782SGreg Clayton } 21644d93782SGreg Clayton } 21744d93782SGreg Clayton 218b9c1b51eSKate Stone void IOHandlerInputComplete(IOHandler &io_handler, 219b9c1b51eSKate Stone std::string &line) override { 22044d93782SGreg Clayton io_handler.SetIsDone(true); 22144d93782SGreg Clayton 222b9c1b51eSKate Stone // The WatchpointOptions object is owned by the watchpoint or watchpoint 223b9c1b51eSKate Stone // location 224b9c1b51eSKate Stone WatchpointOptions *wp_options = 225b9c1b51eSKate Stone (WatchpointOptions *)io_handler.GetUserData(); 226b9c1b51eSKate Stone if (wp_options) { 227d5b44036SJonas Devlieghere std::unique_ptr<WatchpointOptions::CommandData> data_up( 228b9c1b51eSKate Stone new WatchpointOptions::CommandData()); 229d5b44036SJonas Devlieghere if (data_up) { 230d5b44036SJonas Devlieghere data_up->user_source.SplitIntoLines(line); 2314e4fbe82SZachary Turner auto baton_sp = std::make_shared<WatchpointOptions::CommandBaton>( 232d5b44036SJonas Devlieghere std::move(data_up)); 23344d93782SGreg Clayton wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp); 23444d93782SGreg Clayton } 23544d93782SGreg Clayton } 23644d93782SGreg Clayton } 23744d93782SGreg Clayton 238b9c1b51eSKate Stone void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, 239b9c1b51eSKate Stone CommandReturnObject &result) { 240b9c1b51eSKate Stone m_interpreter.GetLLDBCommandsFromIOHandler( 241b9c1b51eSKate Stone "> ", // Prompt 24244d93782SGreg Clayton *this, // IOHandlerDelegate 24344d93782SGreg Clayton true, // Run IOHandler in async mode 244b9c1b51eSKate Stone wp_options); // Baton for the "io_handler" that will be passed back into 245b9c1b51eSKate Stone // our IOHandlerDelegate functions 246e9a5627eSJohnny Chen } 247e9a5627eSJohnny Chen 248e9a5627eSJohnny Chen /// Set a one-liner as the callback for the watchpoint. 249b9c1b51eSKate Stone void SetWatchpointCommandCallback(WatchpointOptions *wp_options, 250b9c1b51eSKate Stone const char *oneliner) { 251d5b44036SJonas Devlieghere std::unique_ptr<WatchpointOptions::CommandData> data_up( 252b9c1b51eSKate Stone new WatchpointOptions::CommandData()); 253e9a5627eSJohnny Chen 25405097246SAdrian Prantl // It's necessary to set both user_source and script_source to the 25505097246SAdrian Prantl // oneliner. The former is used to generate callback description (as in 25605097246SAdrian Prantl // watchpoint command list) while the latter is used for Python to 25705097246SAdrian Prantl // interpret during the actual callback. 258d5b44036SJonas Devlieghere data_up->user_source.AppendString(oneliner); 259d5b44036SJonas Devlieghere data_up->script_source.assign(oneliner); 260d5b44036SJonas Devlieghere data_up->stop_on_error = m_options.m_stop_on_error; 261e9a5627eSJohnny Chen 2624e4fbe82SZachary Turner auto baton_sp = 263d5b44036SJonas Devlieghere std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up)); 264e9a5627eSJohnny Chen wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp); 265e9a5627eSJohnny Chen } 266e9a5627eSJohnny Chen 267e9a5627eSJohnny Chen static bool 268e9a5627eSJohnny Chen WatchpointOptionsCallbackFunction(void *baton, 269e9a5627eSJohnny Chen StoppointCallbackContext *context, 270b9c1b51eSKate Stone lldb::user_id_t watch_id) { 271e9a5627eSJohnny Chen bool ret_value = true; 27249bcfd80SEugene Zelenko if (baton == nullptr) 273e9a5627eSJohnny Chen return true; 274e9a5627eSJohnny Chen 275b9c1b51eSKate Stone WatchpointOptions::CommandData *data = 276b9c1b51eSKate Stone (WatchpointOptions::CommandData *)baton; 277e9a5627eSJohnny Chen StringList &commands = data->user_source; 278e9a5627eSJohnny Chen 279b9c1b51eSKate Stone if (commands.GetSize() > 0) { 280e9a5627eSJohnny Chen ExecutionContext exe_ctx(context->exe_ctx_ref); 281e9a5627eSJohnny Chen Target *target = exe_ctx.GetTargetPtr(); 282b9c1b51eSKate Stone if (target) { 283e9a5627eSJohnny Chen CommandReturnObject result; 284e9a5627eSJohnny Chen Debugger &debugger = target->GetDebugger(); 285b9c1b51eSKate Stone // Rig up the results secondary output stream to the debugger's, so the 28605097246SAdrian Prantl // output will come out synchronously if the debugger is set up that 28705097246SAdrian Prantl // way. 288e9a5627eSJohnny Chen 289e9a5627eSJohnny Chen StreamSP output_stream(debugger.GetAsyncOutputStream()); 290e9a5627eSJohnny Chen StreamSP error_stream(debugger.GetAsyncErrorStream()); 291e9a5627eSJohnny Chen result.SetImmediateOutputStream(output_stream); 292e9a5627eSJohnny Chen result.SetImmediateErrorStream(error_stream); 293e9a5627eSJohnny Chen 29426c7bf93SJim Ingham CommandInterpreterRunOptions options; 29526c7bf93SJim Ingham options.SetStopOnContinue(true); 29626c7bf93SJim Ingham options.SetStopOnError(data->stop_on_error); 29726c7bf93SJim Ingham options.SetEchoCommands(false); 29826c7bf93SJim Ingham options.SetPrintResults(true); 299c0b48ab6SJonas Devlieghere options.SetPrintErrors(true); 30026c7bf93SJim Ingham options.SetAddToHistory(false); 301e9a5627eSJohnny Chen 302b9c1b51eSKate Stone debugger.GetCommandInterpreter().HandleCommands(commands, &exe_ctx, 303b9c1b51eSKate Stone options, result); 304e9a5627eSJohnny Chen result.GetImmediateOutputStream()->Flush(); 305e9a5627eSJohnny Chen result.GetImmediateErrorStream()->Flush(); 306e9a5627eSJohnny Chen } 307e9a5627eSJohnny Chen } 308e9a5627eSJohnny Chen return ret_value; 309e9a5627eSJohnny Chen } 310e9a5627eSJohnny Chen 311b9c1b51eSKate Stone class CommandOptions : public Options { 312e9a5627eSJohnny Chen public: 313b9c1b51eSKate Stone CommandOptions() 314b9c1b51eSKate Stone : Options(), m_use_commands(false), m_use_script_language(false), 315b9c1b51eSKate Stone m_script_language(eScriptLanguageNone), m_use_one_liner(false), 316b9c1b51eSKate Stone m_one_liner(), m_function_name() {} 317e9a5627eSJohnny Chen 31849bcfd80SEugene Zelenko ~CommandOptions() override = default; 319e9a5627eSJohnny Chen 32097206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 321b9c1b51eSKate Stone ExecutionContext *execution_context) override { 32297206d57SZachary Turner Status error; 3233bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val; 324e9a5627eSJohnny Chen 325b9c1b51eSKate Stone switch (short_option) { 326e9a5627eSJohnny Chen case 'o': 327e9a5627eSJohnny Chen m_use_one_liner = true; 328e9a5627eSJohnny Chen m_one_liner = option_arg; 329e9a5627eSJohnny Chen break; 330e9a5627eSJohnny Chen 331e9a5627eSJohnny Chen case 's': 33247cbf4a0SPavel Labath m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum( 333fe11483bSZachary Turner option_arg, GetDefinitions()[option_idx].enum_values, 334fe11483bSZachary Turner eScriptLanguageNone, error); 335e9a5627eSJohnny Chen 336b9c1b51eSKate Stone m_use_script_language = (m_script_language == eScriptLanguagePython || 337b9c1b51eSKate Stone m_script_language == eScriptLanguageDefault); 338e9a5627eSJohnny Chen break; 339e9a5627eSJohnny Chen 340b9c1b51eSKate Stone case 'e': { 341e9a5627eSJohnny Chen bool success = false; 34247cbf4a0SPavel Labath m_stop_on_error = 34347cbf4a0SPavel Labath OptionArgParser::ToBoolean(option_arg, false, &success); 344e9a5627eSJohnny Chen if (!success) 345b9c1b51eSKate Stone error.SetErrorStringWithFormat( 346fe11483bSZachary Turner "invalid value for stop-on-error: \"%s\"", 347fe11483bSZachary Turner option_arg.str().c_str()); 348b9c1b51eSKate Stone } break; 349e9a5627eSJohnny Chen 350e9a5627eSJohnny Chen case 'F': 351e9a5627eSJohnny Chen m_use_one_liner = false; 352e9a5627eSJohnny Chen m_use_script_language = true; 353e9a5627eSJohnny Chen m_function_name.assign(option_arg); 354e9a5627eSJohnny Chen break; 355e9a5627eSJohnny Chen 356e9a5627eSJohnny Chen default: 35736162014SRaphael Isemann llvm_unreachable("Unimplemented option"); 358e9a5627eSJohnny Chen } 359e9a5627eSJohnny Chen return error; 360e9a5627eSJohnny Chen } 36149bcfd80SEugene Zelenko 362b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 363e9a5627eSJohnny Chen m_use_commands = true; 364e9a5627eSJohnny Chen m_use_script_language = false; 365e9a5627eSJohnny Chen m_script_language = eScriptLanguageNone; 366e9a5627eSJohnny Chen 367e9a5627eSJohnny Chen m_use_one_liner = false; 368e9a5627eSJohnny Chen m_stop_on_error = true; 369e9a5627eSJohnny Chen m_one_liner.clear(); 370e9a5627eSJohnny Chen m_function_name.clear(); 371e9a5627eSJohnny Chen } 372e9a5627eSJohnny Chen 3731f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 37470602439SZachary Turner return llvm::makeArrayRef(g_watchpoint_command_add_options); 3751f0f5b5bSZachary Turner } 376e9a5627eSJohnny Chen 377e9a5627eSJohnny Chen // Instance variables to hold the values for command options. 378e9a5627eSJohnny Chen 379e9a5627eSJohnny Chen bool m_use_commands; 380e9a5627eSJohnny Chen bool m_use_script_language; 381e9a5627eSJohnny Chen lldb::ScriptLanguage m_script_language; 382e9a5627eSJohnny Chen 383e9a5627eSJohnny Chen // Instance variables to hold the values for one_liner options. 384e9a5627eSJohnny Chen bool m_use_one_liner; 385e9a5627eSJohnny Chen std::string m_one_liner; 386e9a5627eSJohnny Chen bool m_stop_on_error; 387e9a5627eSJohnny Chen std::string m_function_name; 388e9a5627eSJohnny Chen }; 389e9a5627eSJohnny Chen 390e9a5627eSJohnny Chen protected: 391b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 39204a4c091SRaphael Isemann Target *target = &GetSelectedTarget(); 393e9a5627eSJohnny Chen 394e9a5627eSJohnny Chen const WatchpointList &watchpoints = target->GetWatchpointList(); 395e9a5627eSJohnny Chen size_t num_watchpoints = watchpoints.GetSize(); 396e9a5627eSJohnny Chen 397b9c1b51eSKate Stone if (num_watchpoints == 0) { 398e9a5627eSJohnny Chen result.AppendError("No watchpoints exist to have commands added"); 399e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 400e9a5627eSJohnny Chen return false; 401e9a5627eSJohnny Chen } 402e9a5627eSJohnny Chen 403b9c1b51eSKate Stone if (!m_options.m_use_script_language && 404b9c1b51eSKate Stone !m_options.m_function_name.empty()) { 405b9c1b51eSKate Stone result.AppendError("need to enable scripting to have a function run as a " 406b9c1b51eSKate Stone "watchpoint command"); 407e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 408e9a5627eSJohnny Chen return false; 409e9a5627eSJohnny Chen } 410e9a5627eSJohnny Chen 411e9a5627eSJohnny Chen std::vector<uint32_t> valid_wp_ids; 412b9c1b51eSKate Stone if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, 413b9c1b51eSKate Stone valid_wp_ids)) { 414e9a5627eSJohnny Chen result.AppendError("Invalid watchpoints specification."); 415e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 416e9a5627eSJohnny Chen return false; 417e9a5627eSJohnny Chen } 418e9a5627eSJohnny Chen 419e9a5627eSJohnny Chen result.SetStatus(eReturnStatusSuccessFinishNoResult); 420e9a5627eSJohnny Chen const size_t count = valid_wp_ids.size(); 421b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 422e9a5627eSJohnny Chen uint32_t cur_wp_id = valid_wp_ids.at(i); 423b9c1b51eSKate Stone if (cur_wp_id != LLDB_INVALID_WATCH_ID) { 424e9a5627eSJohnny Chen Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get(); 425e9a5627eSJohnny Chen // Sanity check wp first. 426b9c1b51eSKate Stone if (wp == nullptr) 427b9c1b51eSKate Stone continue; 428e9a5627eSJohnny Chen 429e9a5627eSJohnny Chen WatchpointOptions *wp_options = wp->GetOptions(); 430e9a5627eSJohnny Chen // Skip this watchpoint if wp_options is not good. 431b9c1b51eSKate Stone if (wp_options == nullptr) 432b9c1b51eSKate Stone continue; 433e9a5627eSJohnny Chen 43405097246SAdrian Prantl // If we are using script language, get the script interpreter in order 43505097246SAdrian Prantl // to set or collect command callback. Otherwise, call the methods 43605097246SAdrian Prantl // associated with this object. 437b9c1b51eSKate Stone if (m_options.m_use_script_language) { 438e9a5627eSJohnny Chen // Special handling for one-liner specified inline. 439b9c1b51eSKate Stone if (m_options.m_use_one_liner) { 4402b29b432SJonas Devlieghere GetDebugger().GetScriptInterpreter()->SetWatchpointCommandCallback( 441b9c1b51eSKate Stone wp_options, m_options.m_one_liner.c_str()); 442e9a5627eSJohnny Chen } 44305097246SAdrian Prantl // Special handling for using a Python function by name instead of 44405097246SAdrian Prantl // extending the watchpoint callback data structures, we just 44505097246SAdrian Prantl // automatize what the user would do manually: make their watchpoint 44605097246SAdrian Prantl // command be a function call 447b9c1b51eSKate Stone else if (!m_options.m_function_name.empty()) { 448e9a5627eSJohnny Chen std::string oneliner(m_options.m_function_name); 449e9a5627eSJohnny Chen oneliner += "(frame, wp, internal_dict)"; 4502b29b432SJonas Devlieghere GetDebugger().GetScriptInterpreter()->SetWatchpointCommandCallback( 451b9c1b51eSKate Stone wp_options, oneliner.c_str()); 452b9c1b51eSKate Stone } else { 4532b29b432SJonas Devlieghere GetDebugger() 4542b29b432SJonas Devlieghere .GetScriptInterpreter() 455b9c1b51eSKate Stone ->CollectDataForWatchpointCommandCallback(wp_options, result); 456e9a5627eSJohnny Chen } 457b9c1b51eSKate Stone } else { 458e9a5627eSJohnny Chen // Special handling for one-liner specified inline. 459e9a5627eSJohnny Chen if (m_options.m_use_one_liner) 460e9a5627eSJohnny Chen SetWatchpointCommandCallback(wp_options, 461e9a5627eSJohnny Chen m_options.m_one_liner.c_str()); 462e9a5627eSJohnny Chen else 463b9c1b51eSKate Stone CollectDataForWatchpointCommandCallback(wp_options, result); 464e9a5627eSJohnny Chen } 465e9a5627eSJohnny Chen } 466e9a5627eSJohnny Chen } 467e9a5627eSJohnny Chen 468e9a5627eSJohnny Chen return result.Succeeded(); 469e9a5627eSJohnny Chen } 470e9a5627eSJohnny Chen 471e9a5627eSJohnny Chen private: 472e9a5627eSJohnny Chen CommandOptions m_options; 473e9a5627eSJohnny Chen }; 474e9a5627eSJohnny Chen 475e9a5627eSJohnny Chen // CommandObjectWatchpointCommandDelete 476e9a5627eSJohnny Chen 477b9c1b51eSKate Stone class CommandObjectWatchpointCommandDelete : public CommandObjectParsed { 478e9a5627eSJohnny Chen public: 479b9c1b51eSKate Stone CommandObjectWatchpointCommandDelete(CommandInterpreter &interpreter) 480b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "delete", 481e9a5627eSJohnny Chen "Delete the set of commands from a watchpoint.", 48204a4c091SRaphael Isemann nullptr, eCommandRequiresTarget) { 483e9a5627eSJohnny Chen CommandArgumentEntry arg; 484e9a5627eSJohnny Chen CommandArgumentData wp_id_arg; 485e9a5627eSJohnny Chen 486e9a5627eSJohnny Chen // Define the first (and only) variant of this arg. 487e9a5627eSJohnny Chen wp_id_arg.arg_type = eArgTypeWatchpointID; 488e9a5627eSJohnny Chen wp_id_arg.arg_repetition = eArgRepeatPlain; 489e9a5627eSJohnny Chen 490b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 491b9c1b51eSKate Stone // argument entry. 492e9a5627eSJohnny Chen arg.push_back(wp_id_arg); 493e9a5627eSJohnny Chen 494e9a5627eSJohnny Chen // Push the data for the first argument into the m_arguments vector. 495e9a5627eSJohnny Chen m_arguments.push_back(arg); 496e9a5627eSJohnny Chen } 497e9a5627eSJohnny Chen 49849bcfd80SEugene Zelenko ~CommandObjectWatchpointCommandDelete() override = default; 499e9a5627eSJohnny Chen 500e9a5627eSJohnny Chen protected: 501b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 50204a4c091SRaphael Isemann Target *target = &GetSelectedTarget(); 503e9a5627eSJohnny Chen 504e9a5627eSJohnny Chen const WatchpointList &watchpoints = target->GetWatchpointList(); 505e9a5627eSJohnny Chen size_t num_watchpoints = watchpoints.GetSize(); 506e9a5627eSJohnny Chen 507b9c1b51eSKate Stone if (num_watchpoints == 0) { 508e9a5627eSJohnny Chen result.AppendError("No watchpoints exist to have commands deleted"); 509e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 510e9a5627eSJohnny Chen return false; 511e9a5627eSJohnny Chen } 512e9a5627eSJohnny Chen 513b9c1b51eSKate Stone if (command.GetArgumentCount() == 0) { 514b9c1b51eSKate Stone result.AppendError( 515b9c1b51eSKate Stone "No watchpoint specified from which to delete the commands"); 516e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 517e9a5627eSJohnny Chen return false; 518e9a5627eSJohnny Chen } 519e9a5627eSJohnny Chen 520e9a5627eSJohnny Chen std::vector<uint32_t> valid_wp_ids; 521b9c1b51eSKate Stone if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, 522b9c1b51eSKate Stone valid_wp_ids)) { 523e9a5627eSJohnny Chen result.AppendError("Invalid watchpoints specification."); 524e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 525e9a5627eSJohnny Chen return false; 526e9a5627eSJohnny Chen } 527e9a5627eSJohnny Chen 528e9a5627eSJohnny Chen result.SetStatus(eReturnStatusSuccessFinishNoResult); 529e9a5627eSJohnny Chen const size_t count = valid_wp_ids.size(); 530b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 531e9a5627eSJohnny Chen uint32_t cur_wp_id = valid_wp_ids.at(i); 532b9c1b51eSKate Stone if (cur_wp_id != LLDB_INVALID_WATCH_ID) { 533e9a5627eSJohnny Chen Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get(); 534e9a5627eSJohnny Chen if (wp) 535e9a5627eSJohnny Chen wp->ClearCallback(); 536b9c1b51eSKate Stone } else { 537b9c1b51eSKate Stone result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", cur_wp_id); 538e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 539e9a5627eSJohnny Chen return false; 540e9a5627eSJohnny Chen } 541e9a5627eSJohnny Chen } 542e9a5627eSJohnny Chen return result.Succeeded(); 543e9a5627eSJohnny Chen } 544e9a5627eSJohnny Chen }; 545e9a5627eSJohnny Chen 546e9a5627eSJohnny Chen // CommandObjectWatchpointCommandList 547e9a5627eSJohnny Chen 548b9c1b51eSKate Stone class CommandObjectWatchpointCommandList : public CommandObjectParsed { 549e9a5627eSJohnny Chen public: 550b9c1b51eSKate Stone CommandObjectWatchpointCommandList(CommandInterpreter &interpreter) 55104a4c091SRaphael Isemann : CommandObjectParsed(interpreter, "list", 55204a4c091SRaphael Isemann "List the script or set of commands to be executed " 55304a4c091SRaphael Isemann "when the watchpoint is hit.", 55404a4c091SRaphael Isemann nullptr, eCommandRequiresTarget) { 555e9a5627eSJohnny Chen CommandArgumentEntry arg; 556e9a5627eSJohnny Chen CommandArgumentData wp_id_arg; 557e9a5627eSJohnny Chen 558e9a5627eSJohnny Chen // Define the first (and only) variant of this arg. 559e9a5627eSJohnny Chen wp_id_arg.arg_type = eArgTypeWatchpointID; 560e9a5627eSJohnny Chen wp_id_arg.arg_repetition = eArgRepeatPlain; 561e9a5627eSJohnny Chen 562b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 563b9c1b51eSKate Stone // argument entry. 564e9a5627eSJohnny Chen arg.push_back(wp_id_arg); 565e9a5627eSJohnny Chen 566e9a5627eSJohnny Chen // Push the data for the first argument into the m_arguments vector. 567e9a5627eSJohnny Chen m_arguments.push_back(arg); 568e9a5627eSJohnny Chen } 569e9a5627eSJohnny Chen 57049bcfd80SEugene Zelenko ~CommandObjectWatchpointCommandList() override = default; 571e9a5627eSJohnny Chen 572e9a5627eSJohnny Chen protected: 573b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 57404a4c091SRaphael Isemann Target *target = &GetSelectedTarget(); 575e9a5627eSJohnny Chen 576e9a5627eSJohnny Chen const WatchpointList &watchpoints = target->GetWatchpointList(); 577e9a5627eSJohnny Chen size_t num_watchpoints = watchpoints.GetSize(); 578e9a5627eSJohnny Chen 579b9c1b51eSKate Stone if (num_watchpoints == 0) { 580e9a5627eSJohnny Chen result.AppendError("No watchpoints exist for which to list commands"); 581e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 582e9a5627eSJohnny Chen return false; 583e9a5627eSJohnny Chen } 584e9a5627eSJohnny Chen 585b9c1b51eSKate Stone if (command.GetArgumentCount() == 0) { 586b9c1b51eSKate Stone result.AppendError( 587b9c1b51eSKate Stone "No watchpoint specified for which to list the commands"); 588e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 589e9a5627eSJohnny Chen return false; 590e9a5627eSJohnny Chen } 591e9a5627eSJohnny Chen 592e9a5627eSJohnny Chen std::vector<uint32_t> valid_wp_ids; 593b9c1b51eSKate Stone if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, 594b9c1b51eSKate Stone valid_wp_ids)) { 595e9a5627eSJohnny Chen result.AppendError("Invalid watchpoints specification."); 596e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 597e9a5627eSJohnny Chen return false; 598e9a5627eSJohnny Chen } 599e9a5627eSJohnny Chen 600e9a5627eSJohnny Chen result.SetStatus(eReturnStatusSuccessFinishNoResult); 601e9a5627eSJohnny Chen const size_t count = valid_wp_ids.size(); 602b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 603e9a5627eSJohnny Chen uint32_t cur_wp_id = valid_wp_ids.at(i); 604b9c1b51eSKate Stone if (cur_wp_id != LLDB_INVALID_WATCH_ID) { 605e9a5627eSJohnny Chen Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get(); 606e9a5627eSJohnny Chen 607b9c1b51eSKate Stone if (wp) { 608e9a5627eSJohnny Chen const WatchpointOptions *wp_options = wp->GetOptions(); 609b9c1b51eSKate Stone if (wp_options) { 610e9a5627eSJohnny Chen // Get the callback baton associated with the current watchpoint. 611e9a5627eSJohnny Chen const Baton *baton = wp_options->GetBaton(); 612b9c1b51eSKate Stone if (baton) { 613e9a5627eSJohnny Chen result.GetOutputStream().Printf("Watchpoint %u:\n", cur_wp_id); 614*4f728bfcSRaphael Isemann baton->GetDescription(result.GetOutputStream().AsRawOstream(), 615*4f728bfcSRaphael Isemann eDescriptionLevelFull, 616*4f728bfcSRaphael Isemann result.GetOutputStream().GetIndentLevel() + 617*4f728bfcSRaphael Isemann 2); 618b9c1b51eSKate Stone } else { 619b9c1b51eSKate Stone result.AppendMessageWithFormat( 620b9c1b51eSKate Stone "Watchpoint %u does not have an associated command.\n", 621e9a5627eSJohnny Chen cur_wp_id); 622e9a5627eSJohnny Chen } 623e9a5627eSJohnny Chen } 624e9a5627eSJohnny Chen result.SetStatus(eReturnStatusSuccessFinishResult); 625b9c1b51eSKate Stone } else { 626b9c1b51eSKate Stone result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", 627b9c1b51eSKate Stone cur_wp_id); 628e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 629e9a5627eSJohnny Chen } 630e9a5627eSJohnny Chen } 631e9a5627eSJohnny Chen } 632e9a5627eSJohnny Chen 633e9a5627eSJohnny Chen return result.Succeeded(); 634e9a5627eSJohnny Chen } 635e9a5627eSJohnny Chen }; 636e9a5627eSJohnny Chen 637e9a5627eSJohnny Chen // CommandObjectWatchpointCommand 638e9a5627eSJohnny Chen 639b9c1b51eSKate Stone CommandObjectWatchpointCommand::CommandObjectWatchpointCommand( 640b9c1b51eSKate Stone CommandInterpreter &interpreter) 641b9c1b51eSKate Stone : CommandObjectMultiword( 642b9c1b51eSKate Stone interpreter, "command", 643b9c1b51eSKate Stone "Commands for adding, removing and examining LLDB commands " 6444ebdee0aSBruce Mitchener "executed when the watchpoint is hit (watchpoint 'commands').", 645b9c1b51eSKate Stone "command <sub-command> [<sub-command-options>] <watchpoint-id>") { 646b9c1b51eSKate Stone CommandObjectSP add_command_object( 647b9c1b51eSKate Stone new CommandObjectWatchpointCommandAdd(interpreter)); 648b9c1b51eSKate Stone CommandObjectSP delete_command_object( 649b9c1b51eSKate Stone new CommandObjectWatchpointCommandDelete(interpreter)); 650b9c1b51eSKate Stone CommandObjectSP list_command_object( 651b9c1b51eSKate Stone new CommandObjectWatchpointCommandList(interpreter)); 652e9a5627eSJohnny Chen 653e9a5627eSJohnny Chen add_command_object->SetCommandName("watchpoint command add"); 654e9a5627eSJohnny Chen delete_command_object->SetCommandName("watchpoint command delete"); 655e9a5627eSJohnny Chen list_command_object->SetCommandName("watchpoint command list"); 656e9a5627eSJohnny Chen 65703da4cc2SGreg Clayton LoadSubCommand("add", add_command_object); 65803da4cc2SGreg Clayton LoadSubCommand("delete", delete_command_object); 65903da4cc2SGreg Clayton LoadSubCommand("list", list_command_object); 660e9a5627eSJohnny Chen } 661e9a5627eSJohnny Chen 66249bcfd80SEugene Zelenko CommandObjectWatchpointCommand::~CommandObjectWatchpointCommand() = default; 663