15ffd83dbSDimitry Andric //===-- CommandObjectWatchpointCommand.cpp --------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include <vector> 100b57cec5SDimitry Andric 110b57cec5SDimitry Andric #include "CommandObjectWatchpoint.h" 120b57cec5SDimitry Andric #include "CommandObjectWatchpointCommand.h" 130b57cec5SDimitry Andric #include "lldb/Breakpoint/StoppointCallbackContext.h" 140b57cec5SDimitry Andric #include "lldb/Breakpoint/Watchpoint.h" 150b57cec5SDimitry Andric #include "lldb/Core/IOHandler.h" 160b57cec5SDimitry Andric #include "lldb/Host/OptionParser.h" 170b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h" 18fcaf7f86SDimitry Andric #include "lldb/Interpreter/CommandOptionArgumentTable.h" 190b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h" 200b57cec5SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h" 210b57cec5SDimitry Andric #include "lldb/Target/Target.h" 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric using namespace lldb; 240b57cec5SDimitry Andric using namespace lldb_private; 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric #define LLDB_OPTIONS_watchpoint_command_add 270b57cec5SDimitry Andric #include "CommandOptions.inc" 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric class CommandObjectWatchpointCommandAdd : public CommandObjectParsed, 300b57cec5SDimitry Andric public IOHandlerDelegateMultiline { 310b57cec5SDimitry Andric public: 320b57cec5SDimitry Andric CommandObjectWatchpointCommandAdd(CommandInterpreter &interpreter) 330b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "add", 340b57cec5SDimitry Andric "Add a set of LLDB commands to a watchpoint, to be " 35fe6060f1SDimitry Andric "executed whenever the watchpoint is hit. " 36fe6060f1SDimitry Andric "The commands added to the watchpoint replace any " 37fe6060f1SDimitry Andric "commands previously added to it.", 389dba64beSDimitry Andric nullptr, eCommandRequiresTarget), 390b57cec5SDimitry Andric IOHandlerDelegateMultiline("DONE", 4004eeddc0SDimitry Andric IOHandlerDelegate::Completion::LLDBCommand) { 410b57cec5SDimitry Andric SetHelpLong( 420b57cec5SDimitry Andric R"( 430b57cec5SDimitry Andric General information about entering watchpoint commands 440b57cec5SDimitry Andric ------------------------------------------------------ 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric )" 470b57cec5SDimitry Andric "This command will prompt for commands to be executed when the specified \ 480b57cec5SDimitry Andric watchpoint is hit. Each command is typed on its own line following the '> ' \ 490b57cec5SDimitry Andric prompt until 'DONE' is entered." 500b57cec5SDimitry Andric R"( 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric )" 530b57cec5SDimitry Andric "Syntactic errors may not be detected when initially entered, and many \ 540b57cec5SDimitry Andric malformed commands can silently fail when executed. If your watchpoint commands \ 550b57cec5SDimitry Andric do not appear to be executing, double-check the command syntax." 560b57cec5SDimitry Andric R"( 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric )" 590b57cec5SDimitry Andric "Note: You may enter any debugger command exactly as you would at the debugger \ 600b57cec5SDimitry Andric prompt. There is no limit to the number of commands supplied, but do NOT enter \ 610b57cec5SDimitry Andric more than one command per line." 620b57cec5SDimitry Andric R"( 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric Special information about PYTHON watchpoint commands 650b57cec5SDimitry Andric ---------------------------------------------------- 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric )" 680b57cec5SDimitry Andric "You may enter either one or more lines of Python, including function \ 690b57cec5SDimitry Andric definitions or calls to functions that will have been imported by the time \ 700b57cec5SDimitry Andric the code executes. Single line watchpoint commands will be interpreted 'as is' \ 710b57cec5SDimitry Andric when the watchpoint is hit. Multiple lines of Python will be wrapped in a \ 720b57cec5SDimitry Andric generated function, and a call to the function will be attached to the watchpoint." 730b57cec5SDimitry Andric R"( 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric This auto-generated function is passed in three arguments: 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric frame: an lldb.SBFrame object for the frame which hit the watchpoint. 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric wp: the watchpoint that was hit. 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric )" 820b57cec5SDimitry Andric "When specifying a python function with the --python-function option, you need \ 830b57cec5SDimitry Andric to supply the function name prepended by the module name:" 840b57cec5SDimitry Andric R"( 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric --python-function myutils.watchpoint_callback 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric The function itself must have the following prototype: 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric def watchpoint_callback(frame, wp): 910b57cec5SDimitry Andric # Your code goes here 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric )" 940b57cec5SDimitry Andric "The arguments are the same as the arguments passed to generated functions as \ 950b57cec5SDimitry Andric described above. Note that the global variable 'lldb.frame' will NOT be updated when \ 960b57cec5SDimitry Andric this function is called, so be sure to use the 'frame' argument. The 'frame' argument \ 970b57cec5SDimitry Andric can get you to the thread via frame.GetThread(), the thread can get you to the \ 980b57cec5SDimitry Andric process via thread.GetProcess(), and the process can get you back to the target \ 990b57cec5SDimitry Andric via process.GetTarget()." 1000b57cec5SDimitry Andric R"( 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric )" 1030b57cec5SDimitry Andric "Important Note: As Python code gets collected into functions, access to global \ 1040b57cec5SDimitry Andric variables requires explicit scoping using the 'global' keyword. Be sure to use correct \ 1050b57cec5SDimitry Andric Python syntax, including indentation, when entering Python watchpoint commands." 1060b57cec5SDimitry Andric R"( 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric Example Python one-line watchpoint command: 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric (lldb) watchpoint command add -s python 1 1110b57cec5SDimitry Andric Enter your Python command(s). Type 'DONE' to end. 1120b57cec5SDimitry Andric > print "Hit this watchpoint!" 1130b57cec5SDimitry Andric > DONE 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric As a convenience, this also works for a short Python one-liner: 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric (lldb) watchpoint command add -s python 1 -o 'import time; print time.asctime()' 1180b57cec5SDimitry Andric (lldb) run 1190b57cec5SDimitry Andric Launching '.../a.out' (x86_64) 1200b57cec5SDimitry Andric (lldb) Fri Sep 10 12:17:45 2010 1210b57cec5SDimitry Andric Process 21778 Stopped 1220b57cec5SDimitry Andric * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = watchpoint 1.1, queue = com.apple.main-thread 1230b57cec5SDimitry Andric 36 1240b57cec5SDimitry Andric 37 int c(int val) 1250b57cec5SDimitry Andric 38 { 1260b57cec5SDimitry Andric 39 -> return val + 3; 1270b57cec5SDimitry Andric 40 } 1280b57cec5SDimitry Andric 41 1290b57cec5SDimitry Andric 42 int main (int argc, char const *argv[]) 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric Example multiple line Python watchpoint command, using function definition: 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric (lldb) watchpoint command add -s python 1 1340b57cec5SDimitry Andric Enter your Python command(s). Type 'DONE' to end. 1350b57cec5SDimitry Andric > def watchpoint_output (wp_no): 1360b57cec5SDimitry Andric > out_string = "Hit watchpoint number " + repr (wp_no) 1370b57cec5SDimitry Andric > print out_string 1380b57cec5SDimitry Andric > return True 1390b57cec5SDimitry Andric > watchpoint_output (1) 1400b57cec5SDimitry Andric > DONE 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric Example multiple line Python watchpoint command, using 'loose' Python: 1430b57cec5SDimitry Andric 1440b57cec5SDimitry Andric (lldb) watchpoint command add -s p 1 1450b57cec5SDimitry Andric Enter your Python command(s). Type 'DONE' to end. 1460b57cec5SDimitry Andric > global wp_count 1470b57cec5SDimitry Andric > wp_count = wp_count + 1 1480b57cec5SDimitry Andric > print "Hit this watchpoint " + repr(wp_count) + " times!" 1490b57cec5SDimitry Andric > DONE 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric )" 1520b57cec5SDimitry Andric "In this case, since there is a reference to a global variable, \ 1530b57cec5SDimitry Andric 'wp_count', you will also need to make sure 'wp_count' exists and is \ 1540b57cec5SDimitry Andric initialized:" 1550b57cec5SDimitry Andric R"( 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric (lldb) script 1580b57cec5SDimitry Andric >>> wp_count = 0 1590b57cec5SDimitry Andric >>> quit() 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric )" 1620b57cec5SDimitry Andric "Final Note: A warning that no watchpoint command was generated when there \ 1630b57cec5SDimitry Andric are no syntax errors may indicate that a function was declared but never called."); 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric CommandArgumentEntry arg; 1660b57cec5SDimitry Andric CommandArgumentData wp_id_arg; 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 1690b57cec5SDimitry Andric wp_id_arg.arg_type = eArgTypeWatchpointID; 1700b57cec5SDimitry Andric wp_id_arg.arg_repetition = eArgRepeatPlain; 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 1730b57cec5SDimitry Andric // argument entry. 1740b57cec5SDimitry Andric arg.push_back(wp_id_arg); 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 1770b57cec5SDimitry Andric m_arguments.push_back(arg); 1780b57cec5SDimitry Andric } 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric ~CommandObjectWatchpointCommandAdd() override = default; 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric void IOHandlerActivated(IOHandler &io_handler, bool interactive) override { 1859dba64beSDimitry Andric StreamFileSP output_sp(io_handler.GetOutputStreamFileSP()); 1860b57cec5SDimitry Andric if (output_sp && interactive) { 1870b57cec5SDimitry Andric output_sp->PutCString( 1880b57cec5SDimitry Andric "Enter your debugger command(s). Type 'DONE' to end.\n"); 1890b57cec5SDimitry Andric output_sp->Flush(); 1900b57cec5SDimitry Andric } 1910b57cec5SDimitry Andric } 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric void IOHandlerInputComplete(IOHandler &io_handler, 1940b57cec5SDimitry Andric std::string &line) override { 1950b57cec5SDimitry Andric io_handler.SetIsDone(true); 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric // The WatchpointOptions object is owned by the watchpoint or watchpoint 1980b57cec5SDimitry Andric // location 1990b57cec5SDimitry Andric WatchpointOptions *wp_options = 2000b57cec5SDimitry Andric (WatchpointOptions *)io_handler.GetUserData(); 2010b57cec5SDimitry Andric if (wp_options) { 2020b57cec5SDimitry Andric std::unique_ptr<WatchpointOptions::CommandData> data_up( 2030b57cec5SDimitry Andric new WatchpointOptions::CommandData()); 2040b57cec5SDimitry Andric if (data_up) { 2050b57cec5SDimitry Andric data_up->user_source.SplitIntoLines(line); 2060b57cec5SDimitry Andric auto baton_sp = std::make_shared<WatchpointOptions::CommandBaton>( 2070b57cec5SDimitry Andric std::move(data_up)); 2080b57cec5SDimitry Andric wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp); 2090b57cec5SDimitry Andric } 2100b57cec5SDimitry Andric } 2110b57cec5SDimitry Andric } 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, 2140b57cec5SDimitry Andric CommandReturnObject &result) { 2150b57cec5SDimitry Andric m_interpreter.GetLLDBCommandsFromIOHandler( 2160b57cec5SDimitry Andric "> ", // Prompt 2170b57cec5SDimitry Andric *this, // IOHandlerDelegate 2180b57cec5SDimitry Andric wp_options); // Baton for the "io_handler" that will be passed back into 2190b57cec5SDimitry Andric // our IOHandlerDelegate functions 2200b57cec5SDimitry Andric } 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric /// Set a one-liner as the callback for the watchpoint. 2230b57cec5SDimitry Andric void SetWatchpointCommandCallback(WatchpointOptions *wp_options, 2240b57cec5SDimitry Andric const char *oneliner) { 2250b57cec5SDimitry Andric std::unique_ptr<WatchpointOptions::CommandData> data_up( 2260b57cec5SDimitry Andric new WatchpointOptions::CommandData()); 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric // It's necessary to set both user_source and script_source to the 2290b57cec5SDimitry Andric // oneliner. The former is used to generate callback description (as in 2300b57cec5SDimitry Andric // watchpoint command list) while the latter is used for Python to 2310b57cec5SDimitry Andric // interpret during the actual callback. 2320b57cec5SDimitry Andric data_up->user_source.AppendString(oneliner); 2330b57cec5SDimitry Andric data_up->script_source.assign(oneliner); 2340b57cec5SDimitry Andric data_up->stop_on_error = m_options.m_stop_on_error; 2350b57cec5SDimitry Andric 2360b57cec5SDimitry Andric auto baton_sp = 2370b57cec5SDimitry Andric std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up)); 2380b57cec5SDimitry Andric wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp); 2390b57cec5SDimitry Andric } 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric static bool 2420b57cec5SDimitry Andric WatchpointOptionsCallbackFunction(void *baton, 2430b57cec5SDimitry Andric StoppointCallbackContext *context, 2440b57cec5SDimitry Andric lldb::user_id_t watch_id) { 2450b57cec5SDimitry Andric bool ret_value = true; 2460b57cec5SDimitry Andric if (baton == nullptr) 2470b57cec5SDimitry Andric return true; 2480b57cec5SDimitry Andric 2490b57cec5SDimitry Andric WatchpointOptions::CommandData *data = 2500b57cec5SDimitry Andric (WatchpointOptions::CommandData *)baton; 2510b57cec5SDimitry Andric StringList &commands = data->user_source; 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric if (commands.GetSize() > 0) { 2540b57cec5SDimitry Andric ExecutionContext exe_ctx(context->exe_ctx_ref); 2550b57cec5SDimitry Andric Target *target = exe_ctx.GetTargetPtr(); 2560b57cec5SDimitry Andric if (target) { 2570b57cec5SDimitry Andric Debugger &debugger = target->GetDebugger(); 2585ffd83dbSDimitry Andric CommandReturnObject result(debugger.GetUseColor()); 2595ffd83dbSDimitry Andric 2600b57cec5SDimitry Andric // Rig up the results secondary output stream to the debugger's, so the 2610b57cec5SDimitry Andric // output will come out synchronously if the debugger is set up that 2620b57cec5SDimitry Andric // way. 2630b57cec5SDimitry Andric StreamSP output_stream(debugger.GetAsyncOutputStream()); 2640b57cec5SDimitry Andric StreamSP error_stream(debugger.GetAsyncErrorStream()); 2650b57cec5SDimitry Andric result.SetImmediateOutputStream(output_stream); 2660b57cec5SDimitry Andric result.SetImmediateErrorStream(error_stream); 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric CommandInterpreterRunOptions options; 2690b57cec5SDimitry Andric options.SetStopOnContinue(true); 2700b57cec5SDimitry Andric options.SetStopOnError(data->stop_on_error); 2710b57cec5SDimitry Andric options.SetEchoCommands(false); 2720b57cec5SDimitry Andric options.SetPrintResults(true); 2730b57cec5SDimitry Andric options.SetPrintErrors(true); 2740b57cec5SDimitry Andric options.SetAddToHistory(false); 2750b57cec5SDimitry Andric 276fe6060f1SDimitry Andric debugger.GetCommandInterpreter().HandleCommands(commands, exe_ctx, 2770b57cec5SDimitry Andric options, result); 2780b57cec5SDimitry Andric result.GetImmediateOutputStream()->Flush(); 2790b57cec5SDimitry Andric result.GetImmediateErrorStream()->Flush(); 2800b57cec5SDimitry Andric } 2810b57cec5SDimitry Andric } 2820b57cec5SDimitry Andric return ret_value; 2830b57cec5SDimitry Andric } 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric class CommandOptions : public Options { 2860b57cec5SDimitry Andric public: 28781ad6265SDimitry Andric CommandOptions() = default; 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric ~CommandOptions() override = default; 2900b57cec5SDimitry Andric 2910b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 2920b57cec5SDimitry Andric ExecutionContext *execution_context) override { 2930b57cec5SDimitry Andric Status error; 2940b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric switch (short_option) { 2970b57cec5SDimitry Andric case 'o': 2980b57cec5SDimitry Andric m_use_one_liner = true; 2995ffd83dbSDimitry Andric m_one_liner = std::string(option_arg); 3000b57cec5SDimitry Andric break; 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric case 's': 3030b57cec5SDimitry Andric m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum( 3040b57cec5SDimitry Andric option_arg, GetDefinitions()[option_idx].enum_values, 3050b57cec5SDimitry Andric eScriptLanguageNone, error); 3060b57cec5SDimitry Andric 307480093f4SDimitry Andric switch (m_script_language) { 308480093f4SDimitry Andric case eScriptLanguagePython: 309480093f4SDimitry Andric case eScriptLanguageLua: 310480093f4SDimitry Andric m_use_script_language = true; 311480093f4SDimitry Andric break; 312480093f4SDimitry Andric case eScriptLanguageNone: 313480093f4SDimitry Andric case eScriptLanguageUnknown: 314480093f4SDimitry Andric m_use_script_language = false; 315480093f4SDimitry Andric break; 316480093f4SDimitry Andric } 3170b57cec5SDimitry Andric break; 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric case 'e': { 3200b57cec5SDimitry Andric bool success = false; 3210b57cec5SDimitry Andric m_stop_on_error = 3220b57cec5SDimitry Andric OptionArgParser::ToBoolean(option_arg, false, &success); 3230b57cec5SDimitry Andric if (!success) 3240b57cec5SDimitry Andric error.SetErrorStringWithFormat( 3250b57cec5SDimitry Andric "invalid value for stop-on-error: \"%s\"", 3260b57cec5SDimitry Andric option_arg.str().c_str()); 3270b57cec5SDimitry Andric } break; 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric case 'F': 3300b57cec5SDimitry Andric m_use_one_liner = false; 3315ffd83dbSDimitry Andric m_function_name.assign(std::string(option_arg)); 3320b57cec5SDimitry Andric break; 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric default: 3359dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 3360b57cec5SDimitry Andric } 3370b57cec5SDimitry Andric return error; 3380b57cec5SDimitry Andric } 3390b57cec5SDimitry Andric 3400b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 3410b57cec5SDimitry Andric m_use_commands = true; 3420b57cec5SDimitry Andric m_use_script_language = false; 3430b57cec5SDimitry Andric m_script_language = eScriptLanguageNone; 3440b57cec5SDimitry Andric 3450b57cec5SDimitry Andric m_use_one_liner = false; 3460b57cec5SDimitry Andric m_stop_on_error = true; 3470b57cec5SDimitry Andric m_one_liner.clear(); 3480b57cec5SDimitry Andric m_function_name.clear(); 3490b57cec5SDimitry Andric } 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 352bdd1243dSDimitry Andric return llvm::ArrayRef(g_watchpoint_command_add_options); 3530b57cec5SDimitry Andric } 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric // Instance variables to hold the values for command options. 3560b57cec5SDimitry Andric 357fe6060f1SDimitry Andric bool m_use_commands = false; 358fe6060f1SDimitry Andric bool m_use_script_language = false; 359fe6060f1SDimitry Andric lldb::ScriptLanguage m_script_language = eScriptLanguageNone; 3600b57cec5SDimitry Andric 3610b57cec5SDimitry Andric // Instance variables to hold the values for one_liner options. 362fe6060f1SDimitry Andric bool m_use_one_liner = false; 3630b57cec5SDimitry Andric std::string m_one_liner; 3640b57cec5SDimitry Andric bool m_stop_on_error; 3650b57cec5SDimitry Andric std::string m_function_name; 3660b57cec5SDimitry Andric }; 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric protected: 3690b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 3709dba64beSDimitry Andric Target *target = &GetSelectedTarget(); 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric const WatchpointList &watchpoints = target->GetWatchpointList(); 3730b57cec5SDimitry Andric size_t num_watchpoints = watchpoints.GetSize(); 3740b57cec5SDimitry Andric 3750b57cec5SDimitry Andric if (num_watchpoints == 0) { 3760b57cec5SDimitry Andric result.AppendError("No watchpoints exist to have commands added"); 3770b57cec5SDimitry Andric return false; 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric 380480093f4SDimitry Andric if (!m_options.m_function_name.empty()) { 381480093f4SDimitry Andric if (!m_options.m_use_script_language) { 382480093f4SDimitry Andric m_options.m_script_language = GetDebugger().GetScriptLanguage(); 383480093f4SDimitry Andric m_options.m_use_script_language = true; 384480093f4SDimitry Andric } 3850b57cec5SDimitry Andric } 3860b57cec5SDimitry Andric 3870b57cec5SDimitry Andric std::vector<uint32_t> valid_wp_ids; 3880b57cec5SDimitry Andric if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, 3890b57cec5SDimitry Andric valid_wp_ids)) { 3900b57cec5SDimitry Andric result.AppendError("Invalid watchpoints specification."); 3910b57cec5SDimitry Andric return false; 3920b57cec5SDimitry Andric } 3930b57cec5SDimitry Andric 3940b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 3950b57cec5SDimitry Andric const size_t count = valid_wp_ids.size(); 3960b57cec5SDimitry Andric for (size_t i = 0; i < count; ++i) { 3970b57cec5SDimitry Andric uint32_t cur_wp_id = valid_wp_ids.at(i); 3980b57cec5SDimitry Andric if (cur_wp_id != LLDB_INVALID_WATCH_ID) { 3990b57cec5SDimitry Andric Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get(); 4000b57cec5SDimitry Andric // Sanity check wp first. 4010b57cec5SDimitry Andric if (wp == nullptr) 4020b57cec5SDimitry Andric continue; 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric WatchpointOptions *wp_options = wp->GetOptions(); 4050b57cec5SDimitry Andric // Skip this watchpoint if wp_options is not good. 4060b57cec5SDimitry Andric if (wp_options == nullptr) 4070b57cec5SDimitry Andric continue; 4080b57cec5SDimitry Andric 4090b57cec5SDimitry Andric // If we are using script language, get the script interpreter in order 4100b57cec5SDimitry Andric // to set or collect command callback. Otherwise, call the methods 4110b57cec5SDimitry Andric // associated with this object. 4120b57cec5SDimitry Andric if (m_options.m_use_script_language) { 413480093f4SDimitry Andric ScriptInterpreter *script_interp = GetDebugger().GetScriptInterpreter( 414480093f4SDimitry Andric /*can_create=*/true, m_options.m_script_language); 4150b57cec5SDimitry Andric // Special handling for one-liner specified inline. 4160b57cec5SDimitry Andric if (m_options.m_use_one_liner) { 417480093f4SDimitry Andric script_interp->SetWatchpointCommandCallback( 418*fe013be4SDimitry Andric wp_options, m_options.m_one_liner.c_str(), 419*fe013be4SDimitry Andric /*is_callback=*/false); 4200b57cec5SDimitry Andric } 4210b57cec5SDimitry Andric // Special handling for using a Python function by name instead of 4220b57cec5SDimitry Andric // extending the watchpoint callback data structures, we just 4230b57cec5SDimitry Andric // automatize what the user would do manually: make their watchpoint 4240b57cec5SDimitry Andric // command be a function call 4250b57cec5SDimitry Andric else if (!m_options.m_function_name.empty()) { 426*fe013be4SDimitry Andric std::string function_signature = m_options.m_function_name; 427*fe013be4SDimitry Andric function_signature += "(frame, wp, internal_dict)"; 428480093f4SDimitry Andric script_interp->SetWatchpointCommandCallback( 429*fe013be4SDimitry Andric wp_options, function_signature.c_str(), /*is_callback=*/true); 4300b57cec5SDimitry Andric } else { 431480093f4SDimitry Andric script_interp->CollectDataForWatchpointCommandCallback(wp_options, 432480093f4SDimitry Andric result); 4330b57cec5SDimitry Andric } 4340b57cec5SDimitry Andric } else { 4350b57cec5SDimitry Andric // Special handling for one-liner specified inline. 4360b57cec5SDimitry Andric if (m_options.m_use_one_liner) 4370b57cec5SDimitry Andric SetWatchpointCommandCallback(wp_options, 4380b57cec5SDimitry Andric m_options.m_one_liner.c_str()); 4390b57cec5SDimitry Andric else 4400b57cec5SDimitry Andric CollectDataForWatchpointCommandCallback(wp_options, result); 4410b57cec5SDimitry Andric } 4420b57cec5SDimitry Andric } 4430b57cec5SDimitry Andric } 4440b57cec5SDimitry Andric 4450b57cec5SDimitry Andric return result.Succeeded(); 4460b57cec5SDimitry Andric } 4470b57cec5SDimitry Andric 4480b57cec5SDimitry Andric private: 4490b57cec5SDimitry Andric CommandOptions m_options; 4500b57cec5SDimitry Andric }; 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric // CommandObjectWatchpointCommandDelete 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric class CommandObjectWatchpointCommandDelete : public CommandObjectParsed { 4550b57cec5SDimitry Andric public: 4560b57cec5SDimitry Andric CommandObjectWatchpointCommandDelete(CommandInterpreter &interpreter) 4570b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "delete", 4580b57cec5SDimitry Andric "Delete the set of commands from a watchpoint.", 4599dba64beSDimitry Andric nullptr, eCommandRequiresTarget) { 4600b57cec5SDimitry Andric CommandArgumentEntry arg; 4610b57cec5SDimitry Andric CommandArgumentData wp_id_arg; 4620b57cec5SDimitry Andric 4630b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 4640b57cec5SDimitry Andric wp_id_arg.arg_type = eArgTypeWatchpointID; 4650b57cec5SDimitry Andric wp_id_arg.arg_repetition = eArgRepeatPlain; 4660b57cec5SDimitry Andric 4670b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 4680b57cec5SDimitry Andric // argument entry. 4690b57cec5SDimitry Andric arg.push_back(wp_id_arg); 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 4720b57cec5SDimitry Andric m_arguments.push_back(arg); 4730b57cec5SDimitry Andric } 4740b57cec5SDimitry Andric 4750b57cec5SDimitry Andric ~CommandObjectWatchpointCommandDelete() override = default; 4760b57cec5SDimitry Andric 4770b57cec5SDimitry Andric protected: 4780b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 4799dba64beSDimitry Andric Target *target = &GetSelectedTarget(); 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andric const WatchpointList &watchpoints = target->GetWatchpointList(); 4820b57cec5SDimitry Andric size_t num_watchpoints = watchpoints.GetSize(); 4830b57cec5SDimitry Andric 4840b57cec5SDimitry Andric if (num_watchpoints == 0) { 4850b57cec5SDimitry Andric result.AppendError("No watchpoints exist to have commands deleted"); 4860b57cec5SDimitry Andric return false; 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric if (command.GetArgumentCount() == 0) { 4900b57cec5SDimitry Andric result.AppendError( 4910b57cec5SDimitry Andric "No watchpoint specified from which to delete the commands"); 4920b57cec5SDimitry Andric return false; 4930b57cec5SDimitry Andric } 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andric std::vector<uint32_t> valid_wp_ids; 4960b57cec5SDimitry Andric if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, 4970b57cec5SDimitry Andric valid_wp_ids)) { 4980b57cec5SDimitry Andric result.AppendError("Invalid watchpoints specification."); 4990b57cec5SDimitry Andric return false; 5000b57cec5SDimitry Andric } 5010b57cec5SDimitry Andric 5020b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 5030b57cec5SDimitry Andric const size_t count = valid_wp_ids.size(); 5040b57cec5SDimitry Andric for (size_t i = 0; i < count; ++i) { 5050b57cec5SDimitry Andric uint32_t cur_wp_id = valid_wp_ids.at(i); 5060b57cec5SDimitry Andric if (cur_wp_id != LLDB_INVALID_WATCH_ID) { 5070b57cec5SDimitry Andric Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get(); 5080b57cec5SDimitry Andric if (wp) 5090b57cec5SDimitry Andric wp->ClearCallback(); 5100b57cec5SDimitry Andric } else { 5110b57cec5SDimitry Andric result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", cur_wp_id); 5120b57cec5SDimitry Andric return false; 5130b57cec5SDimitry Andric } 5140b57cec5SDimitry Andric } 5150b57cec5SDimitry Andric return result.Succeeded(); 5160b57cec5SDimitry Andric } 5170b57cec5SDimitry Andric }; 5180b57cec5SDimitry Andric 5190b57cec5SDimitry Andric // CommandObjectWatchpointCommandList 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric class CommandObjectWatchpointCommandList : public CommandObjectParsed { 5220b57cec5SDimitry Andric public: 5230b57cec5SDimitry Andric CommandObjectWatchpointCommandList(CommandInterpreter &interpreter) 5249dba64beSDimitry Andric : CommandObjectParsed(interpreter, "list", 5259dba64beSDimitry Andric "List the script or set of commands to be executed " 5269dba64beSDimitry Andric "when the watchpoint is hit.", 5279dba64beSDimitry Andric nullptr, eCommandRequiresTarget) { 5280b57cec5SDimitry Andric CommandArgumentEntry arg; 5290b57cec5SDimitry Andric CommandArgumentData wp_id_arg; 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 5320b57cec5SDimitry Andric wp_id_arg.arg_type = eArgTypeWatchpointID; 5330b57cec5SDimitry Andric wp_id_arg.arg_repetition = eArgRepeatPlain; 5340b57cec5SDimitry Andric 5350b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 5360b57cec5SDimitry Andric // argument entry. 5370b57cec5SDimitry Andric arg.push_back(wp_id_arg); 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 5400b57cec5SDimitry Andric m_arguments.push_back(arg); 5410b57cec5SDimitry Andric } 5420b57cec5SDimitry Andric 5430b57cec5SDimitry Andric ~CommandObjectWatchpointCommandList() override = default; 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric protected: 5460b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 5479dba64beSDimitry Andric Target *target = &GetSelectedTarget(); 5480b57cec5SDimitry Andric 5490b57cec5SDimitry Andric const WatchpointList &watchpoints = target->GetWatchpointList(); 5500b57cec5SDimitry Andric size_t num_watchpoints = watchpoints.GetSize(); 5510b57cec5SDimitry Andric 5520b57cec5SDimitry Andric if (num_watchpoints == 0) { 5530b57cec5SDimitry Andric result.AppendError("No watchpoints exist for which to list commands"); 5540b57cec5SDimitry Andric return false; 5550b57cec5SDimitry Andric } 5560b57cec5SDimitry Andric 5570b57cec5SDimitry Andric if (command.GetArgumentCount() == 0) { 5580b57cec5SDimitry Andric result.AppendError( 5590b57cec5SDimitry Andric "No watchpoint specified for which to list the commands"); 5600b57cec5SDimitry Andric return false; 5610b57cec5SDimitry Andric } 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric std::vector<uint32_t> valid_wp_ids; 5640b57cec5SDimitry Andric if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, 5650b57cec5SDimitry Andric valid_wp_ids)) { 5660b57cec5SDimitry Andric result.AppendError("Invalid watchpoints specification."); 5670b57cec5SDimitry Andric return false; 5680b57cec5SDimitry Andric } 5690b57cec5SDimitry Andric 5700b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 5710b57cec5SDimitry Andric const size_t count = valid_wp_ids.size(); 5720b57cec5SDimitry Andric for (size_t i = 0; i < count; ++i) { 5730b57cec5SDimitry Andric uint32_t cur_wp_id = valid_wp_ids.at(i); 5740b57cec5SDimitry Andric if (cur_wp_id != LLDB_INVALID_WATCH_ID) { 5750b57cec5SDimitry Andric Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get(); 5760b57cec5SDimitry Andric 5770b57cec5SDimitry Andric if (wp) { 5780b57cec5SDimitry Andric const WatchpointOptions *wp_options = wp->GetOptions(); 5790b57cec5SDimitry Andric if (wp_options) { 5800b57cec5SDimitry Andric // Get the callback baton associated with the current watchpoint. 5810b57cec5SDimitry Andric const Baton *baton = wp_options->GetBaton(); 5820b57cec5SDimitry Andric if (baton) { 5830b57cec5SDimitry Andric result.GetOutputStream().Printf("Watchpoint %u:\n", cur_wp_id); 584480093f4SDimitry Andric baton->GetDescription(result.GetOutputStream().AsRawOstream(), 585480093f4SDimitry Andric eDescriptionLevelFull, 586480093f4SDimitry Andric result.GetOutputStream().GetIndentLevel() + 587480093f4SDimitry Andric 2); 5880b57cec5SDimitry Andric } else { 5890b57cec5SDimitry Andric result.AppendMessageWithFormat( 5900b57cec5SDimitry Andric "Watchpoint %u does not have an associated command.\n", 5910b57cec5SDimitry Andric cur_wp_id); 5920b57cec5SDimitry Andric } 5930b57cec5SDimitry Andric } 5940b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 5950b57cec5SDimitry Andric } else { 5960b57cec5SDimitry Andric result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", 5970b57cec5SDimitry Andric cur_wp_id); 5980b57cec5SDimitry Andric } 5990b57cec5SDimitry Andric } 6000b57cec5SDimitry Andric } 6010b57cec5SDimitry Andric 6020b57cec5SDimitry Andric return result.Succeeded(); 6030b57cec5SDimitry Andric } 6040b57cec5SDimitry Andric }; 6050b57cec5SDimitry Andric 6060b57cec5SDimitry Andric // CommandObjectWatchpointCommand 6070b57cec5SDimitry Andric 6080b57cec5SDimitry Andric CommandObjectWatchpointCommand::CommandObjectWatchpointCommand( 6090b57cec5SDimitry Andric CommandInterpreter &interpreter) 6100b57cec5SDimitry Andric : CommandObjectMultiword( 6110b57cec5SDimitry Andric interpreter, "command", 6120b57cec5SDimitry Andric "Commands for adding, removing and examining LLDB commands " 6130b57cec5SDimitry Andric "executed when the watchpoint is hit (watchpoint 'commands').", 6140b57cec5SDimitry Andric "command <sub-command> [<sub-command-options>] <watchpoint-id>") { 6150b57cec5SDimitry Andric CommandObjectSP add_command_object( 6160b57cec5SDimitry Andric new CommandObjectWatchpointCommandAdd(interpreter)); 6170b57cec5SDimitry Andric CommandObjectSP delete_command_object( 6180b57cec5SDimitry Andric new CommandObjectWatchpointCommandDelete(interpreter)); 6190b57cec5SDimitry Andric CommandObjectSP list_command_object( 6200b57cec5SDimitry Andric new CommandObjectWatchpointCommandList(interpreter)); 6210b57cec5SDimitry Andric 6220b57cec5SDimitry Andric add_command_object->SetCommandName("watchpoint command add"); 6230b57cec5SDimitry Andric delete_command_object->SetCommandName("watchpoint command delete"); 6240b57cec5SDimitry Andric list_command_object->SetCommandName("watchpoint command list"); 6250b57cec5SDimitry Andric 6260b57cec5SDimitry Andric LoadSubCommand("add", add_command_object); 6270b57cec5SDimitry Andric LoadSubCommand("delete", delete_command_object); 6280b57cec5SDimitry Andric LoadSubCommand("list", list_command_object); 6290b57cec5SDimitry Andric } 6300b57cec5SDimitry Andric 6310b57cec5SDimitry Andric CommandObjectWatchpointCommand::~CommandObjectWatchpointCommand() = default; 632