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"
180b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
190b57cec5SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
200b57cec5SDimitry Andric #include "lldb/Target/Target.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace lldb;
230b57cec5SDimitry Andric using namespace lldb_private;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric // FIXME: "script-type" needs to have its contents determined dynamically, so
269dba64beSDimitry Andric // somebody can add a new scripting language to lldb and have it pickable here
279dba64beSDimitry Andric // without having to change this enumeration by hand and rebuild lldb proper.
280b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_script_option_enumeration[] = {
299dba64beSDimitry Andric     {
309dba64beSDimitry Andric         eScriptLanguageNone,
319dba64beSDimitry Andric         "command",
329dba64beSDimitry Andric         "Commands are in the lldb command interpreter language",
339dba64beSDimitry Andric     },
349dba64beSDimitry Andric     {
359dba64beSDimitry Andric         eScriptLanguagePython,
369dba64beSDimitry Andric         "python",
379dba64beSDimitry Andric         "Commands are in the Python language.",
389dba64beSDimitry Andric     },
399dba64beSDimitry Andric     {
40480093f4SDimitry Andric         eScriptLanguageLua,
41480093f4SDimitry Andric         "lua",
425ffd83dbSDimitry Andric         "Commands are in the Lua language.",
43480093f4SDimitry Andric     },
44480093f4SDimitry Andric     {
459dba64beSDimitry Andric         eSortOrderByName,
469dba64beSDimitry Andric         "default-script",
479dba64beSDimitry Andric         "Commands are in the default scripting language.",
489dba64beSDimitry Andric     },
499dba64beSDimitry Andric };
500b57cec5SDimitry Andric 
ScriptOptionEnum()510b57cec5SDimitry Andric static constexpr OptionEnumValues ScriptOptionEnum() {
520b57cec5SDimitry Andric   return OptionEnumValues(g_script_option_enumeration);
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric #define LLDB_OPTIONS_watchpoint_command_add
560b57cec5SDimitry Andric #include "CommandOptions.inc"
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric class CommandObjectWatchpointCommandAdd : public CommandObjectParsed,
590b57cec5SDimitry Andric                                           public IOHandlerDelegateMultiline {
600b57cec5SDimitry Andric public:
CommandObjectWatchpointCommandAdd(CommandInterpreter & interpreter)610b57cec5SDimitry Andric   CommandObjectWatchpointCommandAdd(CommandInterpreter &interpreter)
620b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "add",
630b57cec5SDimitry Andric                             "Add a set of LLDB commands to a watchpoint, to be "
64*5f7ddb14SDimitry Andric                             "executed whenever the watchpoint is hit.  "
65*5f7ddb14SDimitry Andric                             "The commands added to the watchpoint replace any "
66*5f7ddb14SDimitry Andric                             "commands previously added to it.",
679dba64beSDimitry Andric                             nullptr, eCommandRequiresTarget),
680b57cec5SDimitry Andric         IOHandlerDelegateMultiline("DONE",
690b57cec5SDimitry Andric                                    IOHandlerDelegate::Completion::LLDBCommand),
700b57cec5SDimitry Andric         m_options() {
710b57cec5SDimitry Andric     SetHelpLong(
720b57cec5SDimitry Andric         R"(
730b57cec5SDimitry Andric General information about entering watchpoint commands
740b57cec5SDimitry Andric ------------------------------------------------------
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric )"
770b57cec5SDimitry Andric         "This command will prompt for commands to be executed when the specified \
780b57cec5SDimitry Andric watchpoint is hit.  Each command is typed on its own line following the '> ' \
790b57cec5SDimitry Andric prompt until 'DONE' is entered."
800b57cec5SDimitry Andric         R"(
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric )"
830b57cec5SDimitry Andric         "Syntactic errors may not be detected when initially entered, and many \
840b57cec5SDimitry Andric malformed commands can silently fail when executed.  If your watchpoint commands \
850b57cec5SDimitry Andric do not appear to be executing, double-check the command syntax."
860b57cec5SDimitry Andric         R"(
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric )"
890b57cec5SDimitry Andric         "Note: You may enter any debugger command exactly as you would at the debugger \
900b57cec5SDimitry Andric prompt.  There is no limit to the number of commands supplied, but do NOT enter \
910b57cec5SDimitry Andric more than one command per line."
920b57cec5SDimitry Andric         R"(
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric Special information about PYTHON watchpoint commands
950b57cec5SDimitry Andric ----------------------------------------------------
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric )"
980b57cec5SDimitry Andric         "You may enter either one or more lines of Python, including function \
990b57cec5SDimitry Andric definitions or calls to functions that will have been imported by the time \
1000b57cec5SDimitry Andric the code executes.  Single line watchpoint commands will be interpreted 'as is' \
1010b57cec5SDimitry Andric when the watchpoint is hit.  Multiple lines of Python will be wrapped in a \
1020b57cec5SDimitry Andric generated function, and a call to the function will be attached to the watchpoint."
1030b57cec5SDimitry Andric         R"(
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric This auto-generated function is passed in three arguments:
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric     frame:  an lldb.SBFrame object for the frame which hit the watchpoint.
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric     wp:     the watchpoint that was hit.
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric )"
1120b57cec5SDimitry Andric         "When specifying a python function with the --python-function option, you need \
1130b57cec5SDimitry Andric to supply the function name prepended by the module name:"
1140b57cec5SDimitry Andric         R"(
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric     --python-function myutils.watchpoint_callback
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric The function itself must have the following prototype:
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric def watchpoint_callback(frame, wp):
1210b57cec5SDimitry Andric   # Your code goes here
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric )"
1240b57cec5SDimitry Andric         "The arguments are the same as the arguments passed to generated functions as \
1250b57cec5SDimitry Andric described above.  Note that the global variable 'lldb.frame' will NOT be updated when \
1260b57cec5SDimitry Andric this function is called, so be sure to use the 'frame' argument. The 'frame' argument \
1270b57cec5SDimitry Andric can get you to the thread via frame.GetThread(), the thread can get you to the \
1280b57cec5SDimitry Andric process via thread.GetProcess(), and the process can get you back to the target \
1290b57cec5SDimitry Andric via process.GetTarget()."
1300b57cec5SDimitry Andric         R"(
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric )"
1330b57cec5SDimitry Andric         "Important Note: As Python code gets collected into functions, access to global \
1340b57cec5SDimitry Andric variables requires explicit scoping using the 'global' keyword.  Be sure to use correct \
1350b57cec5SDimitry Andric Python syntax, including indentation, when entering Python watchpoint commands."
1360b57cec5SDimitry Andric         R"(
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric Example Python one-line watchpoint command:
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric (lldb) watchpoint command add -s python 1
1410b57cec5SDimitry Andric Enter your Python command(s). Type 'DONE' to end.
1420b57cec5SDimitry Andric > print "Hit this watchpoint!"
1430b57cec5SDimitry Andric > DONE
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric As a convenience, this also works for a short Python one-liner:
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric (lldb) watchpoint command add -s python 1 -o 'import time; print time.asctime()'
1480b57cec5SDimitry Andric (lldb) run
1490b57cec5SDimitry Andric Launching '.../a.out'  (x86_64)
1500b57cec5SDimitry Andric (lldb) Fri Sep 10 12:17:45 2010
1510b57cec5SDimitry Andric Process 21778 Stopped
1520b57cec5SDimitry Andric * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = watchpoint 1.1, queue = com.apple.main-thread
1530b57cec5SDimitry Andric   36
1540b57cec5SDimitry Andric   37   	int c(int val)
1550b57cec5SDimitry Andric   38   	{
1560b57cec5SDimitry Andric   39 ->	    return val + 3;
1570b57cec5SDimitry Andric   40   	}
1580b57cec5SDimitry Andric   41
1590b57cec5SDimitry Andric   42   	int main (int argc, char const *argv[])
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric Example multiple line Python watchpoint command, using function definition:
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric (lldb) watchpoint command add -s python 1
1640b57cec5SDimitry Andric Enter your Python command(s). Type 'DONE' to end.
1650b57cec5SDimitry Andric > def watchpoint_output (wp_no):
1660b57cec5SDimitry Andric >     out_string = "Hit watchpoint number " + repr (wp_no)
1670b57cec5SDimitry Andric >     print out_string
1680b57cec5SDimitry Andric >     return True
1690b57cec5SDimitry Andric > watchpoint_output (1)
1700b57cec5SDimitry Andric > DONE
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric Example multiple line Python watchpoint command, using 'loose' Python:
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric (lldb) watchpoint command add -s p 1
1750b57cec5SDimitry Andric Enter your Python command(s). Type 'DONE' to end.
1760b57cec5SDimitry Andric > global wp_count
1770b57cec5SDimitry Andric > wp_count = wp_count + 1
1780b57cec5SDimitry Andric > print "Hit this watchpoint " + repr(wp_count) + " times!"
1790b57cec5SDimitry Andric > DONE
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric )"
1820b57cec5SDimitry Andric         "In this case, since there is a reference to a global variable, \
1830b57cec5SDimitry Andric 'wp_count', you will also need to make sure 'wp_count' exists and is \
1840b57cec5SDimitry Andric initialized:"
1850b57cec5SDimitry Andric         R"(
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric (lldb) script
1880b57cec5SDimitry Andric >>> wp_count = 0
1890b57cec5SDimitry Andric >>> quit()
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric )"
1920b57cec5SDimitry Andric         "Final Note: A warning that no watchpoint command was generated when there \
1930b57cec5SDimitry Andric are no syntax errors may indicate that a function was declared but never called.");
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric     CommandArgumentEntry arg;
1960b57cec5SDimitry Andric     CommandArgumentData wp_id_arg;
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
1990b57cec5SDimitry Andric     wp_id_arg.arg_type = eArgTypeWatchpointID;
2000b57cec5SDimitry Andric     wp_id_arg.arg_repetition = eArgRepeatPlain;
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
2030b57cec5SDimitry Andric     // argument entry.
2040b57cec5SDimitry Andric     arg.push_back(wp_id_arg);
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
2070b57cec5SDimitry Andric     m_arguments.push_back(arg);
2080b57cec5SDimitry Andric   }
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric   ~CommandObjectWatchpointCommandAdd() override = default;
2110b57cec5SDimitry Andric 
GetOptions()2120b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
2130b57cec5SDimitry Andric 
IOHandlerActivated(IOHandler & io_handler,bool interactive)2140b57cec5SDimitry Andric   void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
2159dba64beSDimitry Andric     StreamFileSP output_sp(io_handler.GetOutputStreamFileSP());
2160b57cec5SDimitry Andric     if (output_sp && interactive) {
2170b57cec5SDimitry Andric       output_sp->PutCString(
2180b57cec5SDimitry Andric           "Enter your debugger command(s).  Type 'DONE' to end.\n");
2190b57cec5SDimitry Andric       output_sp->Flush();
2200b57cec5SDimitry Andric     }
2210b57cec5SDimitry Andric   }
2220b57cec5SDimitry Andric 
IOHandlerInputComplete(IOHandler & io_handler,std::string & line)2230b57cec5SDimitry Andric   void IOHandlerInputComplete(IOHandler &io_handler,
2240b57cec5SDimitry Andric                               std::string &line) override {
2250b57cec5SDimitry Andric     io_handler.SetIsDone(true);
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric     // The WatchpointOptions object is owned by the watchpoint or watchpoint
2280b57cec5SDimitry Andric     // location
2290b57cec5SDimitry Andric     WatchpointOptions *wp_options =
2300b57cec5SDimitry Andric         (WatchpointOptions *)io_handler.GetUserData();
2310b57cec5SDimitry Andric     if (wp_options) {
2320b57cec5SDimitry Andric       std::unique_ptr<WatchpointOptions::CommandData> data_up(
2330b57cec5SDimitry Andric           new WatchpointOptions::CommandData());
2340b57cec5SDimitry Andric       if (data_up) {
2350b57cec5SDimitry Andric         data_up->user_source.SplitIntoLines(line);
2360b57cec5SDimitry Andric         auto baton_sp = std::make_shared<WatchpointOptions::CommandBaton>(
2370b57cec5SDimitry Andric             std::move(data_up));
2380b57cec5SDimitry Andric         wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp);
2390b57cec5SDimitry Andric       }
2400b57cec5SDimitry Andric     }
2410b57cec5SDimitry Andric   }
2420b57cec5SDimitry Andric 
CollectDataForWatchpointCommandCallback(WatchpointOptions * wp_options,CommandReturnObject & result)2430b57cec5SDimitry Andric   void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
2440b57cec5SDimitry Andric                                                CommandReturnObject &result) {
2450b57cec5SDimitry Andric     m_interpreter.GetLLDBCommandsFromIOHandler(
2460b57cec5SDimitry Andric         "> ",        // Prompt
2470b57cec5SDimitry Andric         *this,       // IOHandlerDelegate
2480b57cec5SDimitry Andric         wp_options); // Baton for the "io_handler" that will be passed back into
2490b57cec5SDimitry Andric                      // our IOHandlerDelegate functions
2500b57cec5SDimitry Andric   }
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric   /// Set a one-liner as the callback for the watchpoint.
SetWatchpointCommandCallback(WatchpointOptions * wp_options,const char * oneliner)2530b57cec5SDimitry Andric   void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
2540b57cec5SDimitry Andric                                     const char *oneliner) {
2550b57cec5SDimitry Andric     std::unique_ptr<WatchpointOptions::CommandData> data_up(
2560b57cec5SDimitry Andric         new WatchpointOptions::CommandData());
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric     // It's necessary to set both user_source and script_source to the
2590b57cec5SDimitry Andric     // oneliner. The former is used to generate callback description (as in
2600b57cec5SDimitry Andric     // watchpoint command list) while the latter is used for Python to
2610b57cec5SDimitry Andric     // interpret during the actual callback.
2620b57cec5SDimitry Andric     data_up->user_source.AppendString(oneliner);
2630b57cec5SDimitry Andric     data_up->script_source.assign(oneliner);
2640b57cec5SDimitry Andric     data_up->stop_on_error = m_options.m_stop_on_error;
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric     auto baton_sp =
2670b57cec5SDimitry Andric         std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up));
2680b57cec5SDimitry Andric     wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp);
2690b57cec5SDimitry Andric   }
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric   static bool
WatchpointOptionsCallbackFunction(void * baton,StoppointCallbackContext * context,lldb::user_id_t watch_id)2720b57cec5SDimitry Andric   WatchpointOptionsCallbackFunction(void *baton,
2730b57cec5SDimitry Andric                                     StoppointCallbackContext *context,
2740b57cec5SDimitry Andric                                     lldb::user_id_t watch_id) {
2750b57cec5SDimitry Andric     bool ret_value = true;
2760b57cec5SDimitry Andric     if (baton == nullptr)
2770b57cec5SDimitry Andric       return true;
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric     WatchpointOptions::CommandData *data =
2800b57cec5SDimitry Andric         (WatchpointOptions::CommandData *)baton;
2810b57cec5SDimitry Andric     StringList &commands = data->user_source;
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric     if (commands.GetSize() > 0) {
2840b57cec5SDimitry Andric       ExecutionContext exe_ctx(context->exe_ctx_ref);
2850b57cec5SDimitry Andric       Target *target = exe_ctx.GetTargetPtr();
2860b57cec5SDimitry Andric       if (target) {
2870b57cec5SDimitry Andric         Debugger &debugger = target->GetDebugger();
2885ffd83dbSDimitry Andric         CommandReturnObject result(debugger.GetUseColor());
2895ffd83dbSDimitry Andric 
2900b57cec5SDimitry Andric         // Rig up the results secondary output stream to the debugger's, so the
2910b57cec5SDimitry Andric         // output will come out synchronously if the debugger is set up that
2920b57cec5SDimitry Andric         // way.
2930b57cec5SDimitry Andric         StreamSP output_stream(debugger.GetAsyncOutputStream());
2940b57cec5SDimitry Andric         StreamSP error_stream(debugger.GetAsyncErrorStream());
2950b57cec5SDimitry Andric         result.SetImmediateOutputStream(output_stream);
2960b57cec5SDimitry Andric         result.SetImmediateErrorStream(error_stream);
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric         CommandInterpreterRunOptions options;
2990b57cec5SDimitry Andric         options.SetStopOnContinue(true);
3000b57cec5SDimitry Andric         options.SetStopOnError(data->stop_on_error);
3010b57cec5SDimitry Andric         options.SetEchoCommands(false);
3020b57cec5SDimitry Andric         options.SetPrintResults(true);
3030b57cec5SDimitry Andric         options.SetPrintErrors(true);
3040b57cec5SDimitry Andric         options.SetAddToHistory(false);
3050b57cec5SDimitry Andric 
306*5f7ddb14SDimitry Andric         debugger.GetCommandInterpreter().HandleCommands(commands, exe_ctx,
3070b57cec5SDimitry Andric                                                         options, result);
3080b57cec5SDimitry Andric         result.GetImmediateOutputStream()->Flush();
3090b57cec5SDimitry Andric         result.GetImmediateErrorStream()->Flush();
3100b57cec5SDimitry Andric       }
3110b57cec5SDimitry Andric     }
3120b57cec5SDimitry Andric     return ret_value;
3130b57cec5SDimitry Andric   }
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric   class CommandOptions : public Options {
3160b57cec5SDimitry Andric   public:
CommandOptions()317*5f7ddb14SDimitry Andric     CommandOptions() : Options(), m_one_liner(), m_function_name() {}
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric     ~CommandOptions() override = default;
3200b57cec5SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)3210b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
3220b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
3230b57cec5SDimitry Andric       Status error;
3240b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric       switch (short_option) {
3270b57cec5SDimitry Andric       case 'o':
3280b57cec5SDimitry Andric         m_use_one_liner = true;
3295ffd83dbSDimitry Andric         m_one_liner = std::string(option_arg);
3300b57cec5SDimitry Andric         break;
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric       case 's':
3330b57cec5SDimitry Andric         m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum(
3340b57cec5SDimitry Andric             option_arg, GetDefinitions()[option_idx].enum_values,
3350b57cec5SDimitry Andric             eScriptLanguageNone, error);
3360b57cec5SDimitry Andric 
337480093f4SDimitry Andric         switch (m_script_language) {
338480093f4SDimitry Andric         case eScriptLanguagePython:
339480093f4SDimitry Andric         case eScriptLanguageLua:
340480093f4SDimitry Andric           m_use_script_language = true;
341480093f4SDimitry Andric           break;
342480093f4SDimitry Andric         case eScriptLanguageNone:
343480093f4SDimitry Andric         case eScriptLanguageUnknown:
344480093f4SDimitry Andric           m_use_script_language = false;
345480093f4SDimitry Andric           break;
346480093f4SDimitry Andric         }
3470b57cec5SDimitry Andric         break;
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric       case 'e': {
3500b57cec5SDimitry Andric         bool success = false;
3510b57cec5SDimitry Andric         m_stop_on_error =
3520b57cec5SDimitry Andric             OptionArgParser::ToBoolean(option_arg, false, &success);
3530b57cec5SDimitry Andric         if (!success)
3540b57cec5SDimitry Andric           error.SetErrorStringWithFormat(
3550b57cec5SDimitry Andric               "invalid value for stop-on-error: \"%s\"",
3560b57cec5SDimitry Andric               option_arg.str().c_str());
3570b57cec5SDimitry Andric       } break;
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric       case 'F':
3600b57cec5SDimitry Andric         m_use_one_liner = false;
3615ffd83dbSDimitry Andric         m_function_name.assign(std::string(option_arg));
3620b57cec5SDimitry Andric         break;
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric       default:
3659dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
3660b57cec5SDimitry Andric       }
3670b57cec5SDimitry Andric       return error;
3680b57cec5SDimitry Andric     }
3690b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)3700b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
3710b57cec5SDimitry Andric       m_use_commands = true;
3720b57cec5SDimitry Andric       m_use_script_language = false;
3730b57cec5SDimitry Andric       m_script_language = eScriptLanguageNone;
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric       m_use_one_liner = false;
3760b57cec5SDimitry Andric       m_stop_on_error = true;
3770b57cec5SDimitry Andric       m_one_liner.clear();
3780b57cec5SDimitry Andric       m_function_name.clear();
3790b57cec5SDimitry Andric     }
3800b57cec5SDimitry Andric 
GetDefinitions()3810b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
3820b57cec5SDimitry Andric       return llvm::makeArrayRef(g_watchpoint_command_add_options);
3830b57cec5SDimitry Andric     }
3840b57cec5SDimitry Andric 
3850b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
3860b57cec5SDimitry Andric 
387*5f7ddb14SDimitry Andric     bool m_use_commands = false;
388*5f7ddb14SDimitry Andric     bool m_use_script_language = false;
389*5f7ddb14SDimitry Andric     lldb::ScriptLanguage m_script_language = eScriptLanguageNone;
3900b57cec5SDimitry Andric 
3910b57cec5SDimitry Andric     // Instance variables to hold the values for one_liner options.
392*5f7ddb14SDimitry Andric     bool m_use_one_liner = false;
3930b57cec5SDimitry Andric     std::string m_one_liner;
3940b57cec5SDimitry Andric     bool m_stop_on_error;
3950b57cec5SDimitry Andric     std::string m_function_name;
3960b57cec5SDimitry Andric   };
3970b57cec5SDimitry Andric 
3980b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)3990b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
4009dba64beSDimitry Andric     Target *target = &GetSelectedTarget();
4010b57cec5SDimitry Andric 
4020b57cec5SDimitry Andric     const WatchpointList &watchpoints = target->GetWatchpointList();
4030b57cec5SDimitry Andric     size_t num_watchpoints = watchpoints.GetSize();
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric     if (num_watchpoints == 0) {
4060b57cec5SDimitry Andric       result.AppendError("No watchpoints exist to have commands added");
4070b57cec5SDimitry Andric       return false;
4080b57cec5SDimitry Andric     }
4090b57cec5SDimitry Andric 
410480093f4SDimitry Andric     if (!m_options.m_function_name.empty()) {
411480093f4SDimitry Andric       if (!m_options.m_use_script_language) {
412480093f4SDimitry Andric         m_options.m_script_language = GetDebugger().GetScriptLanguage();
413480093f4SDimitry Andric         m_options.m_use_script_language = true;
414480093f4SDimitry Andric       }
4150b57cec5SDimitry Andric     }
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric     std::vector<uint32_t> valid_wp_ids;
4180b57cec5SDimitry Andric     if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
4190b57cec5SDimitry Andric                                                                valid_wp_ids)) {
4200b57cec5SDimitry Andric       result.AppendError("Invalid watchpoints specification.");
4210b57cec5SDimitry Andric       return false;
4220b57cec5SDimitry Andric     }
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishNoResult);
4250b57cec5SDimitry Andric     const size_t count = valid_wp_ids.size();
4260b57cec5SDimitry Andric     for (size_t i = 0; i < count; ++i) {
4270b57cec5SDimitry Andric       uint32_t cur_wp_id = valid_wp_ids.at(i);
4280b57cec5SDimitry Andric       if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
4290b57cec5SDimitry Andric         Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
4300b57cec5SDimitry Andric         // Sanity check wp first.
4310b57cec5SDimitry Andric         if (wp == nullptr)
4320b57cec5SDimitry Andric           continue;
4330b57cec5SDimitry Andric 
4340b57cec5SDimitry Andric         WatchpointOptions *wp_options = wp->GetOptions();
4350b57cec5SDimitry Andric         // Skip this watchpoint if wp_options is not good.
4360b57cec5SDimitry Andric         if (wp_options == nullptr)
4370b57cec5SDimitry Andric           continue;
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric         // If we are using script language, get the script interpreter in order
4400b57cec5SDimitry Andric         // to set or collect command callback.  Otherwise, call the methods
4410b57cec5SDimitry Andric         // associated with this object.
4420b57cec5SDimitry Andric         if (m_options.m_use_script_language) {
443480093f4SDimitry Andric           ScriptInterpreter *script_interp = GetDebugger().GetScriptInterpreter(
444480093f4SDimitry Andric               /*can_create=*/true, m_options.m_script_language);
4450b57cec5SDimitry Andric           // Special handling for one-liner specified inline.
4460b57cec5SDimitry Andric           if (m_options.m_use_one_liner) {
447480093f4SDimitry Andric             script_interp->SetWatchpointCommandCallback(
4480b57cec5SDimitry Andric                 wp_options, m_options.m_one_liner.c_str());
4490b57cec5SDimitry Andric           }
4500b57cec5SDimitry Andric           // Special handling for using a Python function by name instead of
4510b57cec5SDimitry Andric           // extending the watchpoint callback data structures, we just
4520b57cec5SDimitry Andric           // automatize what the user would do manually: make their watchpoint
4530b57cec5SDimitry Andric           // command be a function call
4540b57cec5SDimitry Andric           else if (!m_options.m_function_name.empty()) {
4550b57cec5SDimitry Andric             std::string oneliner(m_options.m_function_name);
4560b57cec5SDimitry Andric             oneliner += "(frame, wp, internal_dict)";
457480093f4SDimitry Andric             script_interp->SetWatchpointCommandCallback(
4580b57cec5SDimitry Andric                 wp_options, oneliner.c_str());
4590b57cec5SDimitry Andric           } else {
460480093f4SDimitry Andric             script_interp->CollectDataForWatchpointCommandCallback(wp_options,
461480093f4SDimitry Andric                                                                    result);
4620b57cec5SDimitry Andric           }
4630b57cec5SDimitry Andric         } else {
4640b57cec5SDimitry Andric           // Special handling for one-liner specified inline.
4650b57cec5SDimitry Andric           if (m_options.m_use_one_liner)
4660b57cec5SDimitry Andric             SetWatchpointCommandCallback(wp_options,
4670b57cec5SDimitry Andric                                          m_options.m_one_liner.c_str());
4680b57cec5SDimitry Andric           else
4690b57cec5SDimitry Andric             CollectDataForWatchpointCommandCallback(wp_options, result);
4700b57cec5SDimitry Andric         }
4710b57cec5SDimitry Andric       }
4720b57cec5SDimitry Andric     }
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric     return result.Succeeded();
4750b57cec5SDimitry Andric   }
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric private:
4780b57cec5SDimitry Andric   CommandOptions m_options;
4790b57cec5SDimitry Andric };
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric // CommandObjectWatchpointCommandDelete
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric class CommandObjectWatchpointCommandDelete : public CommandObjectParsed {
4840b57cec5SDimitry Andric public:
CommandObjectWatchpointCommandDelete(CommandInterpreter & interpreter)4850b57cec5SDimitry Andric   CommandObjectWatchpointCommandDelete(CommandInterpreter &interpreter)
4860b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "delete",
4870b57cec5SDimitry Andric                             "Delete the set of commands from a watchpoint.",
4889dba64beSDimitry Andric                             nullptr, eCommandRequiresTarget) {
4890b57cec5SDimitry Andric     CommandArgumentEntry arg;
4900b57cec5SDimitry Andric     CommandArgumentData wp_id_arg;
4910b57cec5SDimitry Andric 
4920b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
4930b57cec5SDimitry Andric     wp_id_arg.arg_type = eArgTypeWatchpointID;
4940b57cec5SDimitry Andric     wp_id_arg.arg_repetition = eArgRepeatPlain;
4950b57cec5SDimitry Andric 
4960b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
4970b57cec5SDimitry Andric     // argument entry.
4980b57cec5SDimitry Andric     arg.push_back(wp_id_arg);
4990b57cec5SDimitry Andric 
5000b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
5010b57cec5SDimitry Andric     m_arguments.push_back(arg);
5020b57cec5SDimitry Andric   }
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric   ~CommandObjectWatchpointCommandDelete() override = default;
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)5070b57cec5SDimitry Andric   bool 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 to have commands deleted");
5150b57cec5SDimitry Andric       return false;
5160b57cec5SDimitry Andric     }
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric     if (command.GetArgumentCount() == 0) {
5190b57cec5SDimitry Andric       result.AppendError(
5200b57cec5SDimitry Andric           "No watchpoint specified from which to delete the commands");
5210b57cec5SDimitry Andric       return false;
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.");
5280b57cec5SDimitry Andric       return false;
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         if (wp)
5380b57cec5SDimitry Andric           wp->ClearCallback();
5390b57cec5SDimitry Andric       } else {
5400b57cec5SDimitry Andric         result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", cur_wp_id);
5410b57cec5SDimitry Andric         return false;
5420b57cec5SDimitry Andric       }
5430b57cec5SDimitry Andric     }
5440b57cec5SDimitry Andric     return result.Succeeded();
5450b57cec5SDimitry Andric   }
5460b57cec5SDimitry Andric };
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric // CommandObjectWatchpointCommandList
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric class CommandObjectWatchpointCommandList : public CommandObjectParsed {
5510b57cec5SDimitry Andric public:
CommandObjectWatchpointCommandList(CommandInterpreter & interpreter)5520b57cec5SDimitry Andric   CommandObjectWatchpointCommandList(CommandInterpreter &interpreter)
5539dba64beSDimitry Andric       : CommandObjectParsed(interpreter, "list",
5549dba64beSDimitry Andric                             "List the script or set of commands to be executed "
5559dba64beSDimitry Andric                             "when the watchpoint is hit.",
5569dba64beSDimitry Andric                             nullptr, eCommandRequiresTarget) {
5570b57cec5SDimitry Andric     CommandArgumentEntry arg;
5580b57cec5SDimitry Andric     CommandArgumentData wp_id_arg;
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
5610b57cec5SDimitry Andric     wp_id_arg.arg_type = eArgTypeWatchpointID;
5620b57cec5SDimitry Andric     wp_id_arg.arg_repetition = eArgRepeatPlain;
5630b57cec5SDimitry Andric 
5640b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
5650b57cec5SDimitry Andric     // argument entry.
5660b57cec5SDimitry Andric     arg.push_back(wp_id_arg);
5670b57cec5SDimitry Andric 
5680b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
5690b57cec5SDimitry Andric     m_arguments.push_back(arg);
5700b57cec5SDimitry Andric   }
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric   ~CommandObjectWatchpointCommandList() override = default;
5730b57cec5SDimitry Andric 
5740b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)5750b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
5769dba64beSDimitry Andric     Target *target = &GetSelectedTarget();
5770b57cec5SDimitry Andric 
5780b57cec5SDimitry Andric     const WatchpointList &watchpoints = target->GetWatchpointList();
5790b57cec5SDimitry Andric     size_t num_watchpoints = watchpoints.GetSize();
5800b57cec5SDimitry Andric 
5810b57cec5SDimitry Andric     if (num_watchpoints == 0) {
5820b57cec5SDimitry Andric       result.AppendError("No watchpoints exist for which to list commands");
5830b57cec5SDimitry Andric       return false;
5840b57cec5SDimitry Andric     }
5850b57cec5SDimitry Andric 
5860b57cec5SDimitry Andric     if (command.GetArgumentCount() == 0) {
5870b57cec5SDimitry Andric       result.AppendError(
5880b57cec5SDimitry Andric           "No watchpoint specified for which to list the commands");
5890b57cec5SDimitry Andric       return false;
5900b57cec5SDimitry Andric     }
5910b57cec5SDimitry Andric 
5920b57cec5SDimitry Andric     std::vector<uint32_t> valid_wp_ids;
5930b57cec5SDimitry Andric     if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
5940b57cec5SDimitry Andric                                                                valid_wp_ids)) {
5950b57cec5SDimitry Andric       result.AppendError("Invalid watchpoints specification.");
5960b57cec5SDimitry Andric       return false;
5970b57cec5SDimitry Andric     }
5980b57cec5SDimitry Andric 
5990b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishNoResult);
6000b57cec5SDimitry Andric     const size_t count = valid_wp_ids.size();
6010b57cec5SDimitry Andric     for (size_t i = 0; i < count; ++i) {
6020b57cec5SDimitry Andric       uint32_t cur_wp_id = valid_wp_ids.at(i);
6030b57cec5SDimitry Andric       if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
6040b57cec5SDimitry Andric         Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric         if (wp) {
6070b57cec5SDimitry Andric           const WatchpointOptions *wp_options = wp->GetOptions();
6080b57cec5SDimitry Andric           if (wp_options) {
6090b57cec5SDimitry Andric             // Get the callback baton associated with the current watchpoint.
6100b57cec5SDimitry Andric             const Baton *baton = wp_options->GetBaton();
6110b57cec5SDimitry Andric             if (baton) {
6120b57cec5SDimitry Andric               result.GetOutputStream().Printf("Watchpoint %u:\n", cur_wp_id);
613480093f4SDimitry Andric               baton->GetDescription(result.GetOutputStream().AsRawOstream(),
614480093f4SDimitry Andric                                     eDescriptionLevelFull,
615480093f4SDimitry Andric                                     result.GetOutputStream().GetIndentLevel() +
616480093f4SDimitry Andric                                         2);
6170b57cec5SDimitry Andric             } else {
6180b57cec5SDimitry Andric               result.AppendMessageWithFormat(
6190b57cec5SDimitry Andric                   "Watchpoint %u does not have an associated command.\n",
6200b57cec5SDimitry Andric                   cur_wp_id);
6210b57cec5SDimitry Andric             }
6220b57cec5SDimitry Andric           }
6230b57cec5SDimitry Andric           result.SetStatus(eReturnStatusSuccessFinishResult);
6240b57cec5SDimitry Andric         } else {
6250b57cec5SDimitry Andric           result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n",
6260b57cec5SDimitry Andric                                        cur_wp_id);
6270b57cec5SDimitry Andric         }
6280b57cec5SDimitry Andric       }
6290b57cec5SDimitry Andric     }
6300b57cec5SDimitry Andric 
6310b57cec5SDimitry Andric     return result.Succeeded();
6320b57cec5SDimitry Andric   }
6330b57cec5SDimitry Andric };
6340b57cec5SDimitry Andric 
6350b57cec5SDimitry Andric // CommandObjectWatchpointCommand
6360b57cec5SDimitry Andric 
CommandObjectWatchpointCommand(CommandInterpreter & interpreter)6370b57cec5SDimitry Andric CommandObjectWatchpointCommand::CommandObjectWatchpointCommand(
6380b57cec5SDimitry Andric     CommandInterpreter &interpreter)
6390b57cec5SDimitry Andric     : CommandObjectMultiword(
6400b57cec5SDimitry Andric           interpreter, "command",
6410b57cec5SDimitry Andric           "Commands for adding, removing and examining LLDB commands "
6420b57cec5SDimitry Andric           "executed when the watchpoint is hit (watchpoint 'commands').",
6430b57cec5SDimitry Andric           "command <sub-command> [<sub-command-options>] <watchpoint-id>") {
6440b57cec5SDimitry Andric   CommandObjectSP add_command_object(
6450b57cec5SDimitry Andric       new CommandObjectWatchpointCommandAdd(interpreter));
6460b57cec5SDimitry Andric   CommandObjectSP delete_command_object(
6470b57cec5SDimitry Andric       new CommandObjectWatchpointCommandDelete(interpreter));
6480b57cec5SDimitry Andric   CommandObjectSP list_command_object(
6490b57cec5SDimitry Andric       new CommandObjectWatchpointCommandList(interpreter));
6500b57cec5SDimitry Andric 
6510b57cec5SDimitry Andric   add_command_object->SetCommandName("watchpoint command add");
6520b57cec5SDimitry Andric   delete_command_object->SetCommandName("watchpoint command delete");
6530b57cec5SDimitry Andric   list_command_object->SetCommandName("watchpoint command list");
6540b57cec5SDimitry Andric 
6550b57cec5SDimitry Andric   LoadSubCommand("add", add_command_object);
6560b57cec5SDimitry Andric   LoadSubCommand("delete", delete_command_object);
6570b57cec5SDimitry Andric   LoadSubCommand("list", list_command_object);
6580b57cec5SDimitry Andric }
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric CommandObjectWatchpointCommand::~CommandObjectWatchpointCommand() = default;
661