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 165*0fca6ea1SDimitry Andric AddSimpleArgumentList(eArgTypeWatchpointID); 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric ~CommandObjectWatchpointCommandAdd() override = default; 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric void IOHandlerActivated(IOHandler &io_handler, bool interactive) override { 1739dba64beSDimitry Andric StreamFileSP output_sp(io_handler.GetOutputStreamFileSP()); 1740b57cec5SDimitry Andric if (output_sp && interactive) { 1750b57cec5SDimitry Andric output_sp->PutCString( 1760b57cec5SDimitry Andric "Enter your debugger command(s). Type 'DONE' to end.\n"); 1770b57cec5SDimitry Andric output_sp->Flush(); 1780b57cec5SDimitry Andric } 1790b57cec5SDimitry Andric } 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric void IOHandlerInputComplete(IOHandler &io_handler, 1820b57cec5SDimitry Andric std::string &line) override { 1830b57cec5SDimitry Andric io_handler.SetIsDone(true); 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric // The WatchpointOptions object is owned by the watchpoint or watchpoint 1860b57cec5SDimitry Andric // location 1870b57cec5SDimitry Andric WatchpointOptions *wp_options = 1880b57cec5SDimitry Andric (WatchpointOptions *)io_handler.GetUserData(); 1890b57cec5SDimitry Andric if (wp_options) { 1900b57cec5SDimitry Andric std::unique_ptr<WatchpointOptions::CommandData> data_up( 1910b57cec5SDimitry Andric new WatchpointOptions::CommandData()); 1920b57cec5SDimitry Andric if (data_up) { 1930b57cec5SDimitry Andric data_up->user_source.SplitIntoLines(line); 1940b57cec5SDimitry Andric auto baton_sp = std::make_shared<WatchpointOptions::CommandBaton>( 1950b57cec5SDimitry Andric std::move(data_up)); 1960b57cec5SDimitry Andric wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp); 1970b57cec5SDimitry Andric } 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric } 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, 2020b57cec5SDimitry Andric CommandReturnObject &result) { 2030b57cec5SDimitry Andric m_interpreter.GetLLDBCommandsFromIOHandler( 2040b57cec5SDimitry Andric "> ", // Prompt 2050b57cec5SDimitry Andric *this, // IOHandlerDelegate 2060b57cec5SDimitry Andric wp_options); // Baton for the "io_handler" that will be passed back into 2070b57cec5SDimitry Andric // our IOHandlerDelegate functions 2080b57cec5SDimitry Andric } 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric /// Set a one-liner as the callback for the watchpoint. 2110b57cec5SDimitry Andric void SetWatchpointCommandCallback(WatchpointOptions *wp_options, 2120b57cec5SDimitry Andric const char *oneliner) { 2130b57cec5SDimitry Andric std::unique_ptr<WatchpointOptions::CommandData> data_up( 2140b57cec5SDimitry Andric new WatchpointOptions::CommandData()); 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andric // It's necessary to set both user_source and script_source to the 2170b57cec5SDimitry Andric // oneliner. The former is used to generate callback description (as in 2180b57cec5SDimitry Andric // watchpoint command list) while the latter is used for Python to 2190b57cec5SDimitry Andric // interpret during the actual callback. 2200b57cec5SDimitry Andric data_up->user_source.AppendString(oneliner); 2210b57cec5SDimitry Andric data_up->script_source.assign(oneliner); 2220b57cec5SDimitry Andric data_up->stop_on_error = m_options.m_stop_on_error; 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric auto baton_sp = 2250b57cec5SDimitry Andric std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up)); 2260b57cec5SDimitry Andric wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp); 2270b57cec5SDimitry Andric } 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric static bool 2300b57cec5SDimitry Andric WatchpointOptionsCallbackFunction(void *baton, 2310b57cec5SDimitry Andric StoppointCallbackContext *context, 2320b57cec5SDimitry Andric lldb::user_id_t watch_id) { 2330b57cec5SDimitry Andric bool ret_value = true; 2340b57cec5SDimitry Andric if (baton == nullptr) 2350b57cec5SDimitry Andric return true; 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric WatchpointOptions::CommandData *data = 2380b57cec5SDimitry Andric (WatchpointOptions::CommandData *)baton; 2390b57cec5SDimitry Andric StringList &commands = data->user_source; 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric if (commands.GetSize() > 0) { 2420b57cec5SDimitry Andric ExecutionContext exe_ctx(context->exe_ctx_ref); 2430b57cec5SDimitry Andric Target *target = exe_ctx.GetTargetPtr(); 2440b57cec5SDimitry Andric if (target) { 2450b57cec5SDimitry Andric Debugger &debugger = target->GetDebugger(); 2465ffd83dbSDimitry Andric CommandReturnObject result(debugger.GetUseColor()); 2475ffd83dbSDimitry Andric 2480b57cec5SDimitry Andric // Rig up the results secondary output stream to the debugger's, so the 2490b57cec5SDimitry Andric // output will come out synchronously if the debugger is set up that 2500b57cec5SDimitry Andric // way. 2510b57cec5SDimitry Andric StreamSP output_stream(debugger.GetAsyncOutputStream()); 2520b57cec5SDimitry Andric StreamSP error_stream(debugger.GetAsyncErrorStream()); 2530b57cec5SDimitry Andric result.SetImmediateOutputStream(output_stream); 2540b57cec5SDimitry Andric result.SetImmediateErrorStream(error_stream); 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric CommandInterpreterRunOptions options; 2570b57cec5SDimitry Andric options.SetStopOnContinue(true); 2580b57cec5SDimitry Andric options.SetStopOnError(data->stop_on_error); 2590b57cec5SDimitry Andric options.SetEchoCommands(false); 2600b57cec5SDimitry Andric options.SetPrintResults(true); 2610b57cec5SDimitry Andric options.SetPrintErrors(true); 2620b57cec5SDimitry Andric options.SetAddToHistory(false); 2630b57cec5SDimitry Andric 264fe6060f1SDimitry Andric debugger.GetCommandInterpreter().HandleCommands(commands, exe_ctx, 2650b57cec5SDimitry Andric options, result); 2660b57cec5SDimitry Andric result.GetImmediateOutputStream()->Flush(); 2670b57cec5SDimitry Andric result.GetImmediateErrorStream()->Flush(); 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric } 2700b57cec5SDimitry Andric return ret_value; 2710b57cec5SDimitry Andric } 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric class CommandOptions : public Options { 2740b57cec5SDimitry Andric public: 27581ad6265SDimitry Andric CommandOptions() = default; 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric ~CommandOptions() override = default; 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 2800b57cec5SDimitry Andric ExecutionContext *execution_context) override { 2810b57cec5SDimitry Andric Status error; 2820b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric switch (short_option) { 2850b57cec5SDimitry Andric case 'o': 2860b57cec5SDimitry Andric m_use_one_liner = true; 2875ffd83dbSDimitry Andric m_one_liner = std::string(option_arg); 2880b57cec5SDimitry Andric break; 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric case 's': 2910b57cec5SDimitry Andric m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum( 2920b57cec5SDimitry Andric option_arg, GetDefinitions()[option_idx].enum_values, 2930b57cec5SDimitry Andric eScriptLanguageNone, error); 2940b57cec5SDimitry Andric 295480093f4SDimitry Andric switch (m_script_language) { 296480093f4SDimitry Andric case eScriptLanguagePython: 297480093f4SDimitry Andric case eScriptLanguageLua: 298480093f4SDimitry Andric m_use_script_language = true; 299480093f4SDimitry Andric break; 300480093f4SDimitry Andric case eScriptLanguageNone: 301480093f4SDimitry Andric case eScriptLanguageUnknown: 302480093f4SDimitry Andric m_use_script_language = false; 303480093f4SDimitry Andric break; 304480093f4SDimitry Andric } 3050b57cec5SDimitry Andric break; 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andric case 'e': { 3080b57cec5SDimitry Andric bool success = false; 3090b57cec5SDimitry Andric m_stop_on_error = 3100b57cec5SDimitry Andric OptionArgParser::ToBoolean(option_arg, false, &success); 3110b57cec5SDimitry Andric if (!success) 3120b57cec5SDimitry Andric error.SetErrorStringWithFormat( 3130b57cec5SDimitry Andric "invalid value for stop-on-error: \"%s\"", 3140b57cec5SDimitry Andric option_arg.str().c_str()); 3150b57cec5SDimitry Andric } break; 3160b57cec5SDimitry Andric 3170b57cec5SDimitry Andric case 'F': 3180b57cec5SDimitry Andric m_use_one_liner = false; 3195ffd83dbSDimitry Andric m_function_name.assign(std::string(option_arg)); 3200b57cec5SDimitry Andric break; 3210b57cec5SDimitry Andric 3220b57cec5SDimitry Andric default: 3239dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric return error; 3260b57cec5SDimitry Andric } 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 3290b57cec5SDimitry Andric m_use_commands = true; 3300b57cec5SDimitry Andric m_use_script_language = false; 3310b57cec5SDimitry Andric m_script_language = eScriptLanguageNone; 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric m_use_one_liner = false; 3340b57cec5SDimitry Andric m_stop_on_error = true; 3350b57cec5SDimitry Andric m_one_liner.clear(); 3360b57cec5SDimitry Andric m_function_name.clear(); 3370b57cec5SDimitry Andric } 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 340bdd1243dSDimitry Andric return llvm::ArrayRef(g_watchpoint_command_add_options); 3410b57cec5SDimitry Andric } 3420b57cec5SDimitry Andric 3430b57cec5SDimitry Andric // Instance variables to hold the values for command options. 3440b57cec5SDimitry Andric 345fe6060f1SDimitry Andric bool m_use_commands = false; 346fe6060f1SDimitry Andric bool m_use_script_language = false; 347fe6060f1SDimitry Andric lldb::ScriptLanguage m_script_language = eScriptLanguageNone; 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric // Instance variables to hold the values for one_liner options. 350fe6060f1SDimitry Andric bool m_use_one_liner = false; 3510b57cec5SDimitry Andric std::string m_one_liner; 3520b57cec5SDimitry Andric bool m_stop_on_error; 3530b57cec5SDimitry Andric std::string m_function_name; 3540b57cec5SDimitry Andric }; 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric protected: 3575f757f3fSDimitry Andric void DoExecute(Args &command, CommandReturnObject &result) override { 3589dba64beSDimitry Andric Target *target = &GetSelectedTarget(); 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric const WatchpointList &watchpoints = target->GetWatchpointList(); 3610b57cec5SDimitry Andric size_t num_watchpoints = watchpoints.GetSize(); 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric if (num_watchpoints == 0) { 3640b57cec5SDimitry Andric result.AppendError("No watchpoints exist to have commands added"); 3655f757f3fSDimitry Andric return; 3660b57cec5SDimitry Andric } 3670b57cec5SDimitry Andric 368480093f4SDimitry Andric if (!m_options.m_function_name.empty()) { 369480093f4SDimitry Andric if (!m_options.m_use_script_language) { 370480093f4SDimitry Andric m_options.m_script_language = GetDebugger().GetScriptLanguage(); 371480093f4SDimitry Andric m_options.m_use_script_language = true; 372480093f4SDimitry Andric } 3730b57cec5SDimitry Andric } 3740b57cec5SDimitry Andric 3750b57cec5SDimitry Andric std::vector<uint32_t> valid_wp_ids; 3760b57cec5SDimitry Andric if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, 3770b57cec5SDimitry Andric valid_wp_ids)) { 3780b57cec5SDimitry Andric result.AppendError("Invalid watchpoints specification."); 3795f757f3fSDimitry Andric return; 3800b57cec5SDimitry Andric } 3810b57cec5SDimitry Andric 3820b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 3830b57cec5SDimitry Andric const size_t count = valid_wp_ids.size(); 3840b57cec5SDimitry Andric for (size_t i = 0; i < count; ++i) { 3850b57cec5SDimitry Andric uint32_t cur_wp_id = valid_wp_ids.at(i); 3860b57cec5SDimitry Andric if (cur_wp_id != LLDB_INVALID_WATCH_ID) { 3870b57cec5SDimitry Andric Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get(); 3880b57cec5SDimitry Andric // Sanity check wp first. 3890b57cec5SDimitry Andric if (wp == nullptr) 3900b57cec5SDimitry Andric continue; 3910b57cec5SDimitry Andric 3920b57cec5SDimitry Andric WatchpointOptions *wp_options = wp->GetOptions(); 3930b57cec5SDimitry Andric // Skip this watchpoint if wp_options is not good. 3940b57cec5SDimitry Andric if (wp_options == nullptr) 3950b57cec5SDimitry Andric continue; 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andric // If we are using script language, get the script interpreter in order 3980b57cec5SDimitry Andric // to set or collect command callback. Otherwise, call the methods 3990b57cec5SDimitry Andric // associated with this object. 4000b57cec5SDimitry Andric if (m_options.m_use_script_language) { 401480093f4SDimitry Andric ScriptInterpreter *script_interp = GetDebugger().GetScriptInterpreter( 402480093f4SDimitry Andric /*can_create=*/true, m_options.m_script_language); 4030b57cec5SDimitry Andric // Special handling for one-liner specified inline. 4040b57cec5SDimitry Andric if (m_options.m_use_one_liner) { 405480093f4SDimitry Andric script_interp->SetWatchpointCommandCallback( 40606c3fb27SDimitry Andric wp_options, m_options.m_one_liner.c_str(), 40706c3fb27SDimitry Andric /*is_callback=*/false); 4080b57cec5SDimitry Andric } 4090b57cec5SDimitry Andric // Special handling for using a Python function by name instead of 4100b57cec5SDimitry Andric // extending the watchpoint callback data structures, we just 4110b57cec5SDimitry Andric // automatize what the user would do manually: make their watchpoint 4120b57cec5SDimitry Andric // command be a function call 4130b57cec5SDimitry Andric else if (!m_options.m_function_name.empty()) { 41406c3fb27SDimitry Andric std::string function_signature = m_options.m_function_name; 41506c3fb27SDimitry Andric function_signature += "(frame, wp, internal_dict)"; 416480093f4SDimitry Andric script_interp->SetWatchpointCommandCallback( 41706c3fb27SDimitry Andric wp_options, function_signature.c_str(), /*is_callback=*/true); 4180b57cec5SDimitry Andric } else { 419480093f4SDimitry Andric script_interp->CollectDataForWatchpointCommandCallback(wp_options, 420480093f4SDimitry Andric result); 4210b57cec5SDimitry Andric } 4220b57cec5SDimitry Andric } else { 4230b57cec5SDimitry Andric // Special handling for one-liner specified inline. 4240b57cec5SDimitry Andric if (m_options.m_use_one_liner) 4250b57cec5SDimitry Andric SetWatchpointCommandCallback(wp_options, 4260b57cec5SDimitry Andric m_options.m_one_liner.c_str()); 4270b57cec5SDimitry Andric else 4280b57cec5SDimitry Andric CollectDataForWatchpointCommandCallback(wp_options, result); 4290b57cec5SDimitry Andric } 4300b57cec5SDimitry Andric } 4310b57cec5SDimitry Andric } 4320b57cec5SDimitry Andric } 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric private: 4350b57cec5SDimitry Andric CommandOptions m_options; 4360b57cec5SDimitry Andric }; 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric // CommandObjectWatchpointCommandDelete 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric class CommandObjectWatchpointCommandDelete : public CommandObjectParsed { 4410b57cec5SDimitry Andric public: 4420b57cec5SDimitry Andric CommandObjectWatchpointCommandDelete(CommandInterpreter &interpreter) 4430b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "delete", 4440b57cec5SDimitry Andric "Delete the set of commands from a watchpoint.", 4459dba64beSDimitry Andric nullptr, eCommandRequiresTarget) { 446*0fca6ea1SDimitry Andric AddSimpleArgumentList(eArgTypeWatchpointID); 4470b57cec5SDimitry Andric } 4480b57cec5SDimitry Andric 4490b57cec5SDimitry Andric ~CommandObjectWatchpointCommandDelete() override = default; 4500b57cec5SDimitry Andric 4510b57cec5SDimitry Andric protected: 4525f757f3fSDimitry Andric void DoExecute(Args &command, CommandReturnObject &result) override { 4539dba64beSDimitry Andric Target *target = &GetSelectedTarget(); 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric const WatchpointList &watchpoints = target->GetWatchpointList(); 4560b57cec5SDimitry Andric size_t num_watchpoints = watchpoints.GetSize(); 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric if (num_watchpoints == 0) { 4590b57cec5SDimitry Andric result.AppendError("No watchpoints exist to have commands deleted"); 4605f757f3fSDimitry Andric return; 4610b57cec5SDimitry Andric } 4620b57cec5SDimitry Andric 4630b57cec5SDimitry Andric if (command.GetArgumentCount() == 0) { 4640b57cec5SDimitry Andric result.AppendError( 4650b57cec5SDimitry Andric "No watchpoint specified from which to delete the commands"); 4665f757f3fSDimitry Andric return; 4670b57cec5SDimitry Andric } 4680b57cec5SDimitry Andric 4690b57cec5SDimitry Andric std::vector<uint32_t> valid_wp_ids; 4700b57cec5SDimitry Andric if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, 4710b57cec5SDimitry Andric valid_wp_ids)) { 4720b57cec5SDimitry Andric result.AppendError("Invalid watchpoints specification."); 4735f757f3fSDimitry Andric return; 4740b57cec5SDimitry Andric } 4750b57cec5SDimitry Andric 4760b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 4770b57cec5SDimitry Andric const size_t count = valid_wp_ids.size(); 4780b57cec5SDimitry Andric for (size_t i = 0; i < count; ++i) { 4790b57cec5SDimitry Andric uint32_t cur_wp_id = valid_wp_ids.at(i); 4800b57cec5SDimitry Andric if (cur_wp_id != LLDB_INVALID_WATCH_ID) { 4810b57cec5SDimitry Andric Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get(); 4820b57cec5SDimitry Andric if (wp) 4830b57cec5SDimitry Andric wp->ClearCallback(); 4840b57cec5SDimitry Andric } else { 4850b57cec5SDimitry Andric result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", cur_wp_id); 4865f757f3fSDimitry Andric return; 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric } 4890b57cec5SDimitry Andric } 4900b57cec5SDimitry Andric }; 4910b57cec5SDimitry Andric 4920b57cec5SDimitry Andric // CommandObjectWatchpointCommandList 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andric class CommandObjectWatchpointCommandList : public CommandObjectParsed { 4950b57cec5SDimitry Andric public: 4960b57cec5SDimitry Andric CommandObjectWatchpointCommandList(CommandInterpreter &interpreter) 4979dba64beSDimitry Andric : CommandObjectParsed(interpreter, "list", 4989dba64beSDimitry Andric "List the script or set of commands to be executed " 4999dba64beSDimitry Andric "when the watchpoint is hit.", 5009dba64beSDimitry Andric nullptr, eCommandRequiresTarget) { 501*0fca6ea1SDimitry Andric AddSimpleArgumentList(eArgTypeWatchpointID); 5020b57cec5SDimitry Andric } 5030b57cec5SDimitry Andric 5040b57cec5SDimitry Andric ~CommandObjectWatchpointCommandList() override = default; 5050b57cec5SDimitry Andric 5060b57cec5SDimitry Andric protected: 5075f757f3fSDimitry Andric void DoExecute(Args &command, CommandReturnObject &result) override { 5089dba64beSDimitry Andric Target *target = &GetSelectedTarget(); 5090b57cec5SDimitry Andric 5100b57cec5SDimitry Andric const WatchpointList &watchpoints = target->GetWatchpointList(); 5110b57cec5SDimitry Andric size_t num_watchpoints = watchpoints.GetSize(); 5120b57cec5SDimitry Andric 5130b57cec5SDimitry Andric if (num_watchpoints == 0) { 5140b57cec5SDimitry Andric result.AppendError("No watchpoints exist for which to list commands"); 5155f757f3fSDimitry Andric return; 5160b57cec5SDimitry Andric } 5170b57cec5SDimitry Andric 5180b57cec5SDimitry Andric if (command.GetArgumentCount() == 0) { 5190b57cec5SDimitry Andric result.AppendError( 5200b57cec5SDimitry Andric "No watchpoint specified for which to list the commands"); 5215f757f3fSDimitry Andric return; 5220b57cec5SDimitry Andric } 5230b57cec5SDimitry Andric 5240b57cec5SDimitry Andric std::vector<uint32_t> valid_wp_ids; 5250b57cec5SDimitry Andric if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, 5260b57cec5SDimitry Andric valid_wp_ids)) { 5270b57cec5SDimitry Andric result.AppendError("Invalid watchpoints specification."); 5285f757f3fSDimitry Andric return; 5290b57cec5SDimitry Andric } 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 5320b57cec5SDimitry Andric const size_t count = valid_wp_ids.size(); 5330b57cec5SDimitry Andric for (size_t i = 0; i < count; ++i) { 5340b57cec5SDimitry Andric uint32_t cur_wp_id = valid_wp_ids.at(i); 5350b57cec5SDimitry Andric if (cur_wp_id != LLDB_INVALID_WATCH_ID) { 5360b57cec5SDimitry Andric Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get(); 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andric if (wp) { 5390b57cec5SDimitry Andric const WatchpointOptions *wp_options = wp->GetOptions(); 5400b57cec5SDimitry Andric if (wp_options) { 5410b57cec5SDimitry Andric // Get the callback baton associated with the current watchpoint. 5420b57cec5SDimitry Andric const Baton *baton = wp_options->GetBaton(); 5430b57cec5SDimitry Andric if (baton) { 5440b57cec5SDimitry Andric result.GetOutputStream().Printf("Watchpoint %u:\n", cur_wp_id); 545480093f4SDimitry Andric baton->GetDescription(result.GetOutputStream().AsRawOstream(), 546480093f4SDimitry Andric eDescriptionLevelFull, 547480093f4SDimitry Andric result.GetOutputStream().GetIndentLevel() + 548480093f4SDimitry Andric 2); 5490b57cec5SDimitry Andric } else { 5500b57cec5SDimitry Andric result.AppendMessageWithFormat( 5510b57cec5SDimitry Andric "Watchpoint %u does not have an associated command.\n", 5520b57cec5SDimitry Andric cur_wp_id); 5530b57cec5SDimitry Andric } 5540b57cec5SDimitry Andric } 5550b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 5560b57cec5SDimitry Andric } else { 5570b57cec5SDimitry Andric result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", 5580b57cec5SDimitry Andric cur_wp_id); 5590b57cec5SDimitry Andric } 5600b57cec5SDimitry Andric } 5610b57cec5SDimitry Andric } 5620b57cec5SDimitry Andric } 5630b57cec5SDimitry Andric }; 5640b57cec5SDimitry Andric 5650b57cec5SDimitry Andric // CommandObjectWatchpointCommand 5660b57cec5SDimitry Andric 5670b57cec5SDimitry Andric CommandObjectWatchpointCommand::CommandObjectWatchpointCommand( 5680b57cec5SDimitry Andric CommandInterpreter &interpreter) 5690b57cec5SDimitry Andric : CommandObjectMultiword( 5700b57cec5SDimitry Andric interpreter, "command", 5710b57cec5SDimitry Andric "Commands for adding, removing and examining LLDB commands " 5720b57cec5SDimitry Andric "executed when the watchpoint is hit (watchpoint 'commands').", 5730b57cec5SDimitry Andric "command <sub-command> [<sub-command-options>] <watchpoint-id>") { 5740b57cec5SDimitry Andric CommandObjectSP add_command_object( 5750b57cec5SDimitry Andric new CommandObjectWatchpointCommandAdd(interpreter)); 5760b57cec5SDimitry Andric CommandObjectSP delete_command_object( 5770b57cec5SDimitry Andric new CommandObjectWatchpointCommandDelete(interpreter)); 5780b57cec5SDimitry Andric CommandObjectSP list_command_object( 5790b57cec5SDimitry Andric new CommandObjectWatchpointCommandList(interpreter)); 5800b57cec5SDimitry Andric 5810b57cec5SDimitry Andric add_command_object->SetCommandName("watchpoint command add"); 5820b57cec5SDimitry Andric delete_command_object->SetCommandName("watchpoint command delete"); 5830b57cec5SDimitry Andric list_command_object->SetCommandName("watchpoint command list"); 5840b57cec5SDimitry Andric 5850b57cec5SDimitry Andric LoadSubCommand("add", add_command_object); 5860b57cec5SDimitry Andric LoadSubCommand("delete", delete_command_object); 5870b57cec5SDimitry Andric LoadSubCommand("list", list_command_object); 5880b57cec5SDimitry Andric } 5890b57cec5SDimitry Andric 5900b57cec5SDimitry Andric CommandObjectWatchpointCommand::~CommandObjectWatchpointCommand() = default; 591