xref: /openbsd-src/gnu/llvm/lldb/source/Commands/CommandObjectWatchpointCommand.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1dda28197Spatrick //===-- CommandObjectWatchpointCommand.cpp --------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include <vector>
10061da546Spatrick 
11061da546Spatrick #include "CommandObjectWatchpoint.h"
12061da546Spatrick #include "CommandObjectWatchpointCommand.h"
13061da546Spatrick #include "lldb/Breakpoint/StoppointCallbackContext.h"
14061da546Spatrick #include "lldb/Breakpoint/Watchpoint.h"
15061da546Spatrick #include "lldb/Core/IOHandler.h"
16061da546Spatrick #include "lldb/Host/OptionParser.h"
17061da546Spatrick #include "lldb/Interpreter/CommandInterpreter.h"
18*f6aab3d8Srobert #include "lldb/Interpreter/CommandOptionArgumentTable.h"
19061da546Spatrick #include "lldb/Interpreter/CommandReturnObject.h"
20061da546Spatrick #include "lldb/Interpreter/OptionArgParser.h"
21061da546Spatrick #include "lldb/Target/Target.h"
22061da546Spatrick 
23061da546Spatrick using namespace lldb;
24061da546Spatrick using namespace lldb_private;
25061da546Spatrick 
26061da546Spatrick #define LLDB_OPTIONS_watchpoint_command_add
27061da546Spatrick #include "CommandOptions.inc"
28061da546Spatrick 
29061da546Spatrick class CommandObjectWatchpointCommandAdd : public CommandObjectParsed,
30061da546Spatrick                                           public IOHandlerDelegateMultiline {
31061da546Spatrick public:
CommandObjectWatchpointCommandAdd(CommandInterpreter & interpreter)32061da546Spatrick   CommandObjectWatchpointCommandAdd(CommandInterpreter &interpreter)
33061da546Spatrick       : CommandObjectParsed(interpreter, "add",
34061da546Spatrick                             "Add a set of LLDB commands to a watchpoint, to be "
35be691f3bSpatrick                             "executed whenever the watchpoint is hit.  "
36be691f3bSpatrick                             "The commands added to the watchpoint replace any "
37be691f3bSpatrick                             "commands previously added to it.",
38061da546Spatrick                             nullptr, eCommandRequiresTarget),
39061da546Spatrick         IOHandlerDelegateMultiline("DONE",
40*f6aab3d8Srobert                                    IOHandlerDelegate::Completion::LLDBCommand) {
41061da546Spatrick     SetHelpLong(
42061da546Spatrick         R"(
43061da546Spatrick General information about entering watchpoint commands
44061da546Spatrick ------------------------------------------------------
45061da546Spatrick 
46061da546Spatrick )"
47061da546Spatrick         "This command will prompt for commands to be executed when the specified \
48061da546Spatrick watchpoint is hit.  Each command is typed on its own line following the '> ' \
49061da546Spatrick prompt until 'DONE' is entered."
50061da546Spatrick         R"(
51061da546Spatrick 
52061da546Spatrick )"
53061da546Spatrick         "Syntactic errors may not be detected when initially entered, and many \
54061da546Spatrick malformed commands can silently fail when executed.  If your watchpoint commands \
55061da546Spatrick do not appear to be executing, double-check the command syntax."
56061da546Spatrick         R"(
57061da546Spatrick 
58061da546Spatrick )"
59061da546Spatrick         "Note: You may enter any debugger command exactly as you would at the debugger \
60061da546Spatrick prompt.  There is no limit to the number of commands supplied, but do NOT enter \
61061da546Spatrick more than one command per line."
62061da546Spatrick         R"(
63061da546Spatrick 
64061da546Spatrick Special information about PYTHON watchpoint commands
65061da546Spatrick ----------------------------------------------------
66061da546Spatrick 
67061da546Spatrick )"
68061da546Spatrick         "You may enter either one or more lines of Python, including function \
69061da546Spatrick definitions or calls to functions that will have been imported by the time \
70061da546Spatrick the code executes.  Single line watchpoint commands will be interpreted 'as is' \
71061da546Spatrick when the watchpoint is hit.  Multiple lines of Python will be wrapped in a \
72061da546Spatrick generated function, and a call to the function will be attached to the watchpoint."
73061da546Spatrick         R"(
74061da546Spatrick 
75061da546Spatrick This auto-generated function is passed in three arguments:
76061da546Spatrick 
77061da546Spatrick     frame:  an lldb.SBFrame object for the frame which hit the watchpoint.
78061da546Spatrick 
79061da546Spatrick     wp:     the watchpoint that was hit.
80061da546Spatrick 
81061da546Spatrick )"
82061da546Spatrick         "When specifying a python function with the --python-function option, you need \
83061da546Spatrick to supply the function name prepended by the module name:"
84061da546Spatrick         R"(
85061da546Spatrick 
86061da546Spatrick     --python-function myutils.watchpoint_callback
87061da546Spatrick 
88061da546Spatrick The function itself must have the following prototype:
89061da546Spatrick 
90061da546Spatrick def watchpoint_callback(frame, wp):
91061da546Spatrick   # Your code goes here
92061da546Spatrick 
93061da546Spatrick )"
94061da546Spatrick         "The arguments are the same as the arguments passed to generated functions as \
95061da546Spatrick described above.  Note that the global variable 'lldb.frame' will NOT be updated when \
96061da546Spatrick this function is called, so be sure to use the 'frame' argument. The 'frame' argument \
97061da546Spatrick can get you to the thread via frame.GetThread(), the thread can get you to the \
98061da546Spatrick process via thread.GetProcess(), and the process can get you back to the target \
99061da546Spatrick via process.GetTarget()."
100061da546Spatrick         R"(
101061da546Spatrick 
102061da546Spatrick )"
103061da546Spatrick         "Important Note: As Python code gets collected into functions, access to global \
104061da546Spatrick variables requires explicit scoping using the 'global' keyword.  Be sure to use correct \
105061da546Spatrick Python syntax, including indentation, when entering Python watchpoint commands."
106061da546Spatrick         R"(
107061da546Spatrick 
108061da546Spatrick Example Python one-line watchpoint command:
109061da546Spatrick 
110061da546Spatrick (lldb) watchpoint command add -s python 1
111061da546Spatrick Enter your Python command(s). Type 'DONE' to end.
112061da546Spatrick > print "Hit this watchpoint!"
113061da546Spatrick > DONE
114061da546Spatrick 
115061da546Spatrick As a convenience, this also works for a short Python one-liner:
116061da546Spatrick 
117061da546Spatrick (lldb) watchpoint command add -s python 1 -o 'import time; print time.asctime()'
118061da546Spatrick (lldb) run
119061da546Spatrick Launching '.../a.out'  (x86_64)
120061da546Spatrick (lldb) Fri Sep 10 12:17:45 2010
121061da546Spatrick Process 21778 Stopped
122061da546Spatrick * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = watchpoint 1.1, queue = com.apple.main-thread
123061da546Spatrick   36
124061da546Spatrick   37   	int c(int val)
125061da546Spatrick   38   	{
126061da546Spatrick   39 ->	    return val + 3;
127061da546Spatrick   40   	}
128061da546Spatrick   41
129061da546Spatrick   42   	int main (int argc, char const *argv[])
130061da546Spatrick 
131061da546Spatrick Example multiple line Python watchpoint command, using function definition:
132061da546Spatrick 
133061da546Spatrick (lldb) watchpoint command add -s python 1
134061da546Spatrick Enter your Python command(s). Type 'DONE' to end.
135061da546Spatrick > def watchpoint_output (wp_no):
136061da546Spatrick >     out_string = "Hit watchpoint number " + repr (wp_no)
137061da546Spatrick >     print out_string
138061da546Spatrick >     return True
139061da546Spatrick > watchpoint_output (1)
140061da546Spatrick > DONE
141061da546Spatrick 
142061da546Spatrick Example multiple line Python watchpoint command, using 'loose' Python:
143061da546Spatrick 
144061da546Spatrick (lldb) watchpoint command add -s p 1
145061da546Spatrick Enter your Python command(s). Type 'DONE' to end.
146061da546Spatrick > global wp_count
147061da546Spatrick > wp_count = wp_count + 1
148061da546Spatrick > print "Hit this watchpoint " + repr(wp_count) + " times!"
149061da546Spatrick > DONE
150061da546Spatrick 
151061da546Spatrick )"
152061da546Spatrick         "In this case, since there is a reference to a global variable, \
153061da546Spatrick 'wp_count', you will also need to make sure 'wp_count' exists and is \
154061da546Spatrick initialized:"
155061da546Spatrick         R"(
156061da546Spatrick 
157061da546Spatrick (lldb) script
158061da546Spatrick >>> wp_count = 0
159061da546Spatrick >>> quit()
160061da546Spatrick 
161061da546Spatrick )"
162061da546Spatrick         "Final Note: A warning that no watchpoint command was generated when there \
163061da546Spatrick are no syntax errors may indicate that a function was declared but never called.");
164061da546Spatrick 
165061da546Spatrick     CommandArgumentEntry arg;
166061da546Spatrick     CommandArgumentData wp_id_arg;
167061da546Spatrick 
168061da546Spatrick     // Define the first (and only) variant of this arg.
169061da546Spatrick     wp_id_arg.arg_type = eArgTypeWatchpointID;
170061da546Spatrick     wp_id_arg.arg_repetition = eArgRepeatPlain;
171061da546Spatrick 
172061da546Spatrick     // There is only one variant this argument could be; put it into the
173061da546Spatrick     // argument entry.
174061da546Spatrick     arg.push_back(wp_id_arg);
175061da546Spatrick 
176061da546Spatrick     // Push the data for the first argument into the m_arguments vector.
177061da546Spatrick     m_arguments.push_back(arg);
178061da546Spatrick   }
179061da546Spatrick 
180061da546Spatrick   ~CommandObjectWatchpointCommandAdd() override = default;
181061da546Spatrick 
GetOptions()182061da546Spatrick   Options *GetOptions() override { return &m_options; }
183061da546Spatrick 
IOHandlerActivated(IOHandler & io_handler,bool interactive)184061da546Spatrick   void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
185061da546Spatrick     StreamFileSP output_sp(io_handler.GetOutputStreamFileSP());
186061da546Spatrick     if (output_sp && interactive) {
187061da546Spatrick       output_sp->PutCString(
188061da546Spatrick           "Enter your debugger command(s).  Type 'DONE' to end.\n");
189061da546Spatrick       output_sp->Flush();
190061da546Spatrick     }
191061da546Spatrick   }
192061da546Spatrick 
IOHandlerInputComplete(IOHandler & io_handler,std::string & line)193061da546Spatrick   void IOHandlerInputComplete(IOHandler &io_handler,
194061da546Spatrick                               std::string &line) override {
195061da546Spatrick     io_handler.SetIsDone(true);
196061da546Spatrick 
197061da546Spatrick     // The WatchpointOptions object is owned by the watchpoint or watchpoint
198061da546Spatrick     // location
199061da546Spatrick     WatchpointOptions *wp_options =
200061da546Spatrick         (WatchpointOptions *)io_handler.GetUserData();
201061da546Spatrick     if (wp_options) {
202061da546Spatrick       std::unique_ptr<WatchpointOptions::CommandData> data_up(
203061da546Spatrick           new WatchpointOptions::CommandData());
204061da546Spatrick       if (data_up) {
205061da546Spatrick         data_up->user_source.SplitIntoLines(line);
206061da546Spatrick         auto baton_sp = std::make_shared<WatchpointOptions::CommandBaton>(
207061da546Spatrick             std::move(data_up));
208061da546Spatrick         wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp);
209061da546Spatrick       }
210061da546Spatrick     }
211061da546Spatrick   }
212061da546Spatrick 
CollectDataForWatchpointCommandCallback(WatchpointOptions * wp_options,CommandReturnObject & result)213061da546Spatrick   void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
214061da546Spatrick                                                CommandReturnObject &result) {
215061da546Spatrick     m_interpreter.GetLLDBCommandsFromIOHandler(
216061da546Spatrick         "> ",        // Prompt
217061da546Spatrick         *this,       // IOHandlerDelegate
218061da546Spatrick         wp_options); // Baton for the "io_handler" that will be passed back into
219061da546Spatrick                      // our IOHandlerDelegate functions
220061da546Spatrick   }
221061da546Spatrick 
222061da546Spatrick   /// Set a one-liner as the callback for the watchpoint.
SetWatchpointCommandCallback(WatchpointOptions * wp_options,const char * oneliner)223061da546Spatrick   void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
224061da546Spatrick                                     const char *oneliner) {
225061da546Spatrick     std::unique_ptr<WatchpointOptions::CommandData> data_up(
226061da546Spatrick         new WatchpointOptions::CommandData());
227061da546Spatrick 
228061da546Spatrick     // It's necessary to set both user_source and script_source to the
229061da546Spatrick     // oneliner. The former is used to generate callback description (as in
230061da546Spatrick     // watchpoint command list) while the latter is used for Python to
231061da546Spatrick     // interpret during the actual callback.
232061da546Spatrick     data_up->user_source.AppendString(oneliner);
233061da546Spatrick     data_up->script_source.assign(oneliner);
234061da546Spatrick     data_up->stop_on_error = m_options.m_stop_on_error;
235061da546Spatrick 
236061da546Spatrick     auto baton_sp =
237061da546Spatrick         std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up));
238061da546Spatrick     wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp);
239061da546Spatrick   }
240061da546Spatrick 
241061da546Spatrick   static bool
WatchpointOptionsCallbackFunction(void * baton,StoppointCallbackContext * context,lldb::user_id_t watch_id)242061da546Spatrick   WatchpointOptionsCallbackFunction(void *baton,
243061da546Spatrick                                     StoppointCallbackContext *context,
244061da546Spatrick                                     lldb::user_id_t watch_id) {
245061da546Spatrick     bool ret_value = true;
246061da546Spatrick     if (baton == nullptr)
247061da546Spatrick       return true;
248061da546Spatrick 
249061da546Spatrick     WatchpointOptions::CommandData *data =
250061da546Spatrick         (WatchpointOptions::CommandData *)baton;
251061da546Spatrick     StringList &commands = data->user_source;
252061da546Spatrick 
253061da546Spatrick     if (commands.GetSize() > 0) {
254061da546Spatrick       ExecutionContext exe_ctx(context->exe_ctx_ref);
255061da546Spatrick       Target *target = exe_ctx.GetTargetPtr();
256061da546Spatrick       if (target) {
257061da546Spatrick         Debugger &debugger = target->GetDebugger();
258dda28197Spatrick         CommandReturnObject result(debugger.GetUseColor());
259dda28197Spatrick 
260061da546Spatrick         // Rig up the results secondary output stream to the debugger's, so the
261061da546Spatrick         // output will come out synchronously if the debugger is set up that
262061da546Spatrick         // way.
263061da546Spatrick         StreamSP output_stream(debugger.GetAsyncOutputStream());
264061da546Spatrick         StreamSP error_stream(debugger.GetAsyncErrorStream());
265061da546Spatrick         result.SetImmediateOutputStream(output_stream);
266061da546Spatrick         result.SetImmediateErrorStream(error_stream);
267061da546Spatrick 
268061da546Spatrick         CommandInterpreterRunOptions options;
269061da546Spatrick         options.SetStopOnContinue(true);
270061da546Spatrick         options.SetStopOnError(data->stop_on_error);
271061da546Spatrick         options.SetEchoCommands(false);
272061da546Spatrick         options.SetPrintResults(true);
273061da546Spatrick         options.SetPrintErrors(true);
274061da546Spatrick         options.SetAddToHistory(false);
275061da546Spatrick 
276be691f3bSpatrick         debugger.GetCommandInterpreter().HandleCommands(commands, exe_ctx,
277061da546Spatrick                                                         options, result);
278061da546Spatrick         result.GetImmediateOutputStream()->Flush();
279061da546Spatrick         result.GetImmediateErrorStream()->Flush();
280061da546Spatrick       }
281061da546Spatrick     }
282061da546Spatrick     return ret_value;
283061da546Spatrick   }
284061da546Spatrick 
285061da546Spatrick   class CommandOptions : public Options {
286061da546Spatrick   public:
287*f6aab3d8Srobert     CommandOptions() = default;
288061da546Spatrick 
289061da546Spatrick     ~CommandOptions() override = default;
290061da546Spatrick 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)291061da546Spatrick     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
292061da546Spatrick                           ExecutionContext *execution_context) override {
293061da546Spatrick       Status error;
294061da546Spatrick       const int short_option = m_getopt_table[option_idx].val;
295061da546Spatrick 
296061da546Spatrick       switch (short_option) {
297061da546Spatrick       case 'o':
298061da546Spatrick         m_use_one_liner = true;
299dda28197Spatrick         m_one_liner = std::string(option_arg);
300061da546Spatrick         break;
301061da546Spatrick 
302061da546Spatrick       case 's':
303061da546Spatrick         m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum(
304061da546Spatrick             option_arg, GetDefinitions()[option_idx].enum_values,
305061da546Spatrick             eScriptLanguageNone, error);
306061da546Spatrick 
307061da546Spatrick         switch (m_script_language) {
308061da546Spatrick         case eScriptLanguagePython:
309061da546Spatrick         case eScriptLanguageLua:
310061da546Spatrick           m_use_script_language = true;
311061da546Spatrick           break;
312061da546Spatrick         case eScriptLanguageNone:
313061da546Spatrick         case eScriptLanguageUnknown:
314061da546Spatrick           m_use_script_language = false;
315061da546Spatrick           break;
316061da546Spatrick         }
317061da546Spatrick         break;
318061da546Spatrick 
319061da546Spatrick       case 'e': {
320061da546Spatrick         bool success = false;
321061da546Spatrick         m_stop_on_error =
322061da546Spatrick             OptionArgParser::ToBoolean(option_arg, false, &success);
323061da546Spatrick         if (!success)
324061da546Spatrick           error.SetErrorStringWithFormat(
325061da546Spatrick               "invalid value for stop-on-error: \"%s\"",
326061da546Spatrick               option_arg.str().c_str());
327061da546Spatrick       } break;
328061da546Spatrick 
329061da546Spatrick       case 'F':
330061da546Spatrick         m_use_one_liner = false;
331dda28197Spatrick         m_function_name.assign(std::string(option_arg));
332061da546Spatrick         break;
333061da546Spatrick 
334061da546Spatrick       default:
335061da546Spatrick         llvm_unreachable("Unimplemented option");
336061da546Spatrick       }
337061da546Spatrick       return error;
338061da546Spatrick     }
339061da546Spatrick 
OptionParsingStarting(ExecutionContext * execution_context)340061da546Spatrick     void OptionParsingStarting(ExecutionContext *execution_context) override {
341061da546Spatrick       m_use_commands = true;
342061da546Spatrick       m_use_script_language = false;
343061da546Spatrick       m_script_language = eScriptLanguageNone;
344061da546Spatrick 
345061da546Spatrick       m_use_one_liner = false;
346061da546Spatrick       m_stop_on_error = true;
347061da546Spatrick       m_one_liner.clear();
348061da546Spatrick       m_function_name.clear();
349061da546Spatrick     }
350061da546Spatrick 
GetDefinitions()351061da546Spatrick     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
352*f6aab3d8Srobert       return llvm::ArrayRef(g_watchpoint_command_add_options);
353061da546Spatrick     }
354061da546Spatrick 
355061da546Spatrick     // Instance variables to hold the values for command options.
356061da546Spatrick 
357be691f3bSpatrick     bool m_use_commands = false;
358be691f3bSpatrick     bool m_use_script_language = false;
359be691f3bSpatrick     lldb::ScriptLanguage m_script_language = eScriptLanguageNone;
360061da546Spatrick 
361061da546Spatrick     // Instance variables to hold the values for one_liner options.
362be691f3bSpatrick     bool m_use_one_liner = false;
363061da546Spatrick     std::string m_one_liner;
364061da546Spatrick     bool m_stop_on_error;
365061da546Spatrick     std::string m_function_name;
366061da546Spatrick   };
367061da546Spatrick 
368061da546Spatrick protected:
DoExecute(Args & command,CommandReturnObject & result)369061da546Spatrick   bool DoExecute(Args &command, CommandReturnObject &result) override {
370061da546Spatrick     Target *target = &GetSelectedTarget();
371061da546Spatrick 
372061da546Spatrick     const WatchpointList &watchpoints = target->GetWatchpointList();
373061da546Spatrick     size_t num_watchpoints = watchpoints.GetSize();
374061da546Spatrick 
375061da546Spatrick     if (num_watchpoints == 0) {
376061da546Spatrick       result.AppendError("No watchpoints exist to have commands added");
377061da546Spatrick       return false;
378061da546Spatrick     }
379061da546Spatrick 
380061da546Spatrick     if (!m_options.m_function_name.empty()) {
381061da546Spatrick       if (!m_options.m_use_script_language) {
382061da546Spatrick         m_options.m_script_language = GetDebugger().GetScriptLanguage();
383061da546Spatrick         m_options.m_use_script_language = true;
384061da546Spatrick       }
385061da546Spatrick     }
386061da546Spatrick 
387061da546Spatrick     std::vector<uint32_t> valid_wp_ids;
388061da546Spatrick     if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
389061da546Spatrick                                                                valid_wp_ids)) {
390061da546Spatrick       result.AppendError("Invalid watchpoints specification.");
391061da546Spatrick       return false;
392061da546Spatrick     }
393061da546Spatrick 
394061da546Spatrick     result.SetStatus(eReturnStatusSuccessFinishNoResult);
395061da546Spatrick     const size_t count = valid_wp_ids.size();
396061da546Spatrick     for (size_t i = 0; i < count; ++i) {
397061da546Spatrick       uint32_t cur_wp_id = valid_wp_ids.at(i);
398061da546Spatrick       if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
399061da546Spatrick         Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
400061da546Spatrick         // Sanity check wp first.
401061da546Spatrick         if (wp == nullptr)
402061da546Spatrick           continue;
403061da546Spatrick 
404061da546Spatrick         WatchpointOptions *wp_options = wp->GetOptions();
405061da546Spatrick         // Skip this watchpoint if wp_options is not good.
406061da546Spatrick         if (wp_options == nullptr)
407061da546Spatrick           continue;
408061da546Spatrick 
409061da546Spatrick         // If we are using script language, get the script interpreter in order
410061da546Spatrick         // to set or collect command callback.  Otherwise, call the methods
411061da546Spatrick         // associated with this object.
412061da546Spatrick         if (m_options.m_use_script_language) {
413061da546Spatrick           ScriptInterpreter *script_interp = GetDebugger().GetScriptInterpreter(
414061da546Spatrick               /*can_create=*/true, m_options.m_script_language);
415061da546Spatrick           // Special handling for one-liner specified inline.
416061da546Spatrick           if (m_options.m_use_one_liner) {
417061da546Spatrick             script_interp->SetWatchpointCommandCallback(
418061da546Spatrick                 wp_options, m_options.m_one_liner.c_str());
419061da546Spatrick           }
420061da546Spatrick           // Special handling for using a Python function by name instead of
421061da546Spatrick           // extending the watchpoint callback data structures, we just
422061da546Spatrick           // automatize what the user would do manually: make their watchpoint
423061da546Spatrick           // command be a function call
424061da546Spatrick           else if (!m_options.m_function_name.empty()) {
425061da546Spatrick             std::string oneliner(m_options.m_function_name);
426061da546Spatrick             oneliner += "(frame, wp, internal_dict)";
427061da546Spatrick             script_interp->SetWatchpointCommandCallback(
428061da546Spatrick                 wp_options, oneliner.c_str());
429061da546Spatrick           } else {
430061da546Spatrick             script_interp->CollectDataForWatchpointCommandCallback(wp_options,
431061da546Spatrick                                                                    result);
432061da546Spatrick           }
433061da546Spatrick         } else {
434061da546Spatrick           // Special handling for one-liner specified inline.
435061da546Spatrick           if (m_options.m_use_one_liner)
436061da546Spatrick             SetWatchpointCommandCallback(wp_options,
437061da546Spatrick                                          m_options.m_one_liner.c_str());
438061da546Spatrick           else
439061da546Spatrick             CollectDataForWatchpointCommandCallback(wp_options, result);
440061da546Spatrick         }
441061da546Spatrick       }
442061da546Spatrick     }
443061da546Spatrick 
444061da546Spatrick     return result.Succeeded();
445061da546Spatrick   }
446061da546Spatrick 
447061da546Spatrick private:
448061da546Spatrick   CommandOptions m_options;
449061da546Spatrick };
450061da546Spatrick 
451061da546Spatrick // CommandObjectWatchpointCommandDelete
452061da546Spatrick 
453061da546Spatrick class CommandObjectWatchpointCommandDelete : public CommandObjectParsed {
454061da546Spatrick public:
CommandObjectWatchpointCommandDelete(CommandInterpreter & interpreter)455061da546Spatrick   CommandObjectWatchpointCommandDelete(CommandInterpreter &interpreter)
456061da546Spatrick       : CommandObjectParsed(interpreter, "delete",
457061da546Spatrick                             "Delete the set of commands from a watchpoint.",
458061da546Spatrick                             nullptr, eCommandRequiresTarget) {
459061da546Spatrick     CommandArgumentEntry arg;
460061da546Spatrick     CommandArgumentData wp_id_arg;
461061da546Spatrick 
462061da546Spatrick     // Define the first (and only) variant of this arg.
463061da546Spatrick     wp_id_arg.arg_type = eArgTypeWatchpointID;
464061da546Spatrick     wp_id_arg.arg_repetition = eArgRepeatPlain;
465061da546Spatrick 
466061da546Spatrick     // There is only one variant this argument could be; put it into the
467061da546Spatrick     // argument entry.
468061da546Spatrick     arg.push_back(wp_id_arg);
469061da546Spatrick 
470061da546Spatrick     // Push the data for the first argument into the m_arguments vector.
471061da546Spatrick     m_arguments.push_back(arg);
472061da546Spatrick   }
473061da546Spatrick 
474061da546Spatrick   ~CommandObjectWatchpointCommandDelete() override = default;
475061da546Spatrick 
476061da546Spatrick protected:
DoExecute(Args & command,CommandReturnObject & result)477061da546Spatrick   bool DoExecute(Args &command, CommandReturnObject &result) override {
478061da546Spatrick     Target *target = &GetSelectedTarget();
479061da546Spatrick 
480061da546Spatrick     const WatchpointList &watchpoints = target->GetWatchpointList();
481061da546Spatrick     size_t num_watchpoints = watchpoints.GetSize();
482061da546Spatrick 
483061da546Spatrick     if (num_watchpoints == 0) {
484061da546Spatrick       result.AppendError("No watchpoints exist to have commands deleted");
485061da546Spatrick       return false;
486061da546Spatrick     }
487061da546Spatrick 
488061da546Spatrick     if (command.GetArgumentCount() == 0) {
489061da546Spatrick       result.AppendError(
490061da546Spatrick           "No watchpoint specified from which to delete the commands");
491061da546Spatrick       return false;
492061da546Spatrick     }
493061da546Spatrick 
494061da546Spatrick     std::vector<uint32_t> valid_wp_ids;
495061da546Spatrick     if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
496061da546Spatrick                                                                valid_wp_ids)) {
497061da546Spatrick       result.AppendError("Invalid watchpoints specification.");
498061da546Spatrick       return false;
499061da546Spatrick     }
500061da546Spatrick 
501061da546Spatrick     result.SetStatus(eReturnStatusSuccessFinishNoResult);
502061da546Spatrick     const size_t count = valid_wp_ids.size();
503061da546Spatrick     for (size_t i = 0; i < count; ++i) {
504061da546Spatrick       uint32_t cur_wp_id = valid_wp_ids.at(i);
505061da546Spatrick       if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
506061da546Spatrick         Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
507061da546Spatrick         if (wp)
508061da546Spatrick           wp->ClearCallback();
509061da546Spatrick       } else {
510061da546Spatrick         result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", cur_wp_id);
511061da546Spatrick         return false;
512061da546Spatrick       }
513061da546Spatrick     }
514061da546Spatrick     return result.Succeeded();
515061da546Spatrick   }
516061da546Spatrick };
517061da546Spatrick 
518061da546Spatrick // CommandObjectWatchpointCommandList
519061da546Spatrick 
520061da546Spatrick class CommandObjectWatchpointCommandList : public CommandObjectParsed {
521061da546Spatrick public:
CommandObjectWatchpointCommandList(CommandInterpreter & interpreter)522061da546Spatrick   CommandObjectWatchpointCommandList(CommandInterpreter &interpreter)
523061da546Spatrick       : CommandObjectParsed(interpreter, "list",
524061da546Spatrick                             "List the script or set of commands to be executed "
525061da546Spatrick                             "when the watchpoint is hit.",
526061da546Spatrick                             nullptr, eCommandRequiresTarget) {
527061da546Spatrick     CommandArgumentEntry arg;
528061da546Spatrick     CommandArgumentData wp_id_arg;
529061da546Spatrick 
530061da546Spatrick     // Define the first (and only) variant of this arg.
531061da546Spatrick     wp_id_arg.arg_type = eArgTypeWatchpointID;
532061da546Spatrick     wp_id_arg.arg_repetition = eArgRepeatPlain;
533061da546Spatrick 
534061da546Spatrick     // There is only one variant this argument could be; put it into the
535061da546Spatrick     // argument entry.
536061da546Spatrick     arg.push_back(wp_id_arg);
537061da546Spatrick 
538061da546Spatrick     // Push the data for the first argument into the m_arguments vector.
539061da546Spatrick     m_arguments.push_back(arg);
540061da546Spatrick   }
541061da546Spatrick 
542061da546Spatrick   ~CommandObjectWatchpointCommandList() override = default;
543061da546Spatrick 
544061da546Spatrick protected:
DoExecute(Args & command,CommandReturnObject & result)545061da546Spatrick   bool DoExecute(Args &command, CommandReturnObject &result) override {
546061da546Spatrick     Target *target = &GetSelectedTarget();
547061da546Spatrick 
548061da546Spatrick     const WatchpointList &watchpoints = target->GetWatchpointList();
549061da546Spatrick     size_t num_watchpoints = watchpoints.GetSize();
550061da546Spatrick 
551061da546Spatrick     if (num_watchpoints == 0) {
552061da546Spatrick       result.AppendError("No watchpoints exist for which to list commands");
553061da546Spatrick       return false;
554061da546Spatrick     }
555061da546Spatrick 
556061da546Spatrick     if (command.GetArgumentCount() == 0) {
557061da546Spatrick       result.AppendError(
558061da546Spatrick           "No watchpoint specified for which to list the commands");
559061da546Spatrick       return false;
560061da546Spatrick     }
561061da546Spatrick 
562061da546Spatrick     std::vector<uint32_t> valid_wp_ids;
563061da546Spatrick     if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
564061da546Spatrick                                                                valid_wp_ids)) {
565061da546Spatrick       result.AppendError("Invalid watchpoints specification.");
566061da546Spatrick       return false;
567061da546Spatrick     }
568061da546Spatrick 
569061da546Spatrick     result.SetStatus(eReturnStatusSuccessFinishNoResult);
570061da546Spatrick     const size_t count = valid_wp_ids.size();
571061da546Spatrick     for (size_t i = 0; i < count; ++i) {
572061da546Spatrick       uint32_t cur_wp_id = valid_wp_ids.at(i);
573061da546Spatrick       if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
574061da546Spatrick         Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
575061da546Spatrick 
576061da546Spatrick         if (wp) {
577061da546Spatrick           const WatchpointOptions *wp_options = wp->GetOptions();
578061da546Spatrick           if (wp_options) {
579061da546Spatrick             // Get the callback baton associated with the current watchpoint.
580061da546Spatrick             const Baton *baton = wp_options->GetBaton();
581061da546Spatrick             if (baton) {
582061da546Spatrick               result.GetOutputStream().Printf("Watchpoint %u:\n", cur_wp_id);
583061da546Spatrick               baton->GetDescription(result.GetOutputStream().AsRawOstream(),
584061da546Spatrick                                     eDescriptionLevelFull,
585061da546Spatrick                                     result.GetOutputStream().GetIndentLevel() +
586061da546Spatrick                                         2);
587061da546Spatrick             } else {
588061da546Spatrick               result.AppendMessageWithFormat(
589061da546Spatrick                   "Watchpoint %u does not have an associated command.\n",
590061da546Spatrick                   cur_wp_id);
591061da546Spatrick             }
592061da546Spatrick           }
593061da546Spatrick           result.SetStatus(eReturnStatusSuccessFinishResult);
594061da546Spatrick         } else {
595061da546Spatrick           result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n",
596061da546Spatrick                                        cur_wp_id);
597061da546Spatrick         }
598061da546Spatrick       }
599061da546Spatrick     }
600061da546Spatrick 
601061da546Spatrick     return result.Succeeded();
602061da546Spatrick   }
603061da546Spatrick };
604061da546Spatrick 
605061da546Spatrick // CommandObjectWatchpointCommand
606061da546Spatrick 
CommandObjectWatchpointCommand(CommandInterpreter & interpreter)607061da546Spatrick CommandObjectWatchpointCommand::CommandObjectWatchpointCommand(
608061da546Spatrick     CommandInterpreter &interpreter)
609061da546Spatrick     : CommandObjectMultiword(
610061da546Spatrick           interpreter, "command",
611061da546Spatrick           "Commands for adding, removing and examining LLDB commands "
612061da546Spatrick           "executed when the watchpoint is hit (watchpoint 'commands').",
613061da546Spatrick           "command <sub-command> [<sub-command-options>] <watchpoint-id>") {
614061da546Spatrick   CommandObjectSP add_command_object(
615061da546Spatrick       new CommandObjectWatchpointCommandAdd(interpreter));
616061da546Spatrick   CommandObjectSP delete_command_object(
617061da546Spatrick       new CommandObjectWatchpointCommandDelete(interpreter));
618061da546Spatrick   CommandObjectSP list_command_object(
619061da546Spatrick       new CommandObjectWatchpointCommandList(interpreter));
620061da546Spatrick 
621061da546Spatrick   add_command_object->SetCommandName("watchpoint command add");
622061da546Spatrick   delete_command_object->SetCommandName("watchpoint command delete");
623061da546Spatrick   list_command_object->SetCommandName("watchpoint command list");
624061da546Spatrick 
625061da546Spatrick   LoadSubCommand("add", add_command_object);
626061da546Spatrick   LoadSubCommand("delete", delete_command_object);
627061da546Spatrick   LoadSubCommand("list", list_command_object);
628061da546Spatrick }
629061da546Spatrick 
630061da546Spatrick CommandObjectWatchpointCommand::~CommandObjectWatchpointCommand() = default;
631