1 //===-- CommandObjectWatchpointCommand.cpp ----------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <vector>
10 
11 #include "CommandObjectWatchpoint.h"
12 #include "CommandObjectWatchpointCommand.h"
13 #include "lldb/Breakpoint/StoppointCallbackContext.h"
14 #include "lldb/Breakpoint/Watchpoint.h"
15 #include "lldb/Core/IOHandler.h"
16 #include "lldb/Host/OptionParser.h"
17 #include "lldb/Interpreter/CommandInterpreter.h"
18 #include "lldb/Interpreter/CommandReturnObject.h"
19 #include "lldb/Interpreter/OptionArgParser.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Target/Thread.h"
22 #include "lldb/Utility/State.h"
23 
24 using namespace lldb;
25 using namespace lldb_private;
26 
27 // CommandObjectWatchpointCommandAdd
28 
29 // FIXME: "script-type" needs to have its contents determined dynamically, so
30 // somebody can add a new scripting
31 // language to lldb and have it pickable here without having to change this
32 // enumeration by hand and rebuild lldb proper.
33 
34 static constexpr OptionEnumValueElement g_script_option_enumeration[] = {
35     {eScriptLanguageNone, "command",
36      "Commands are in the lldb command interpreter language"},
37     {eScriptLanguagePython, "python", "Commands are in the Python language."},
38     {eSortOrderByName, "default-script",
39      "Commands are in the default scripting language."} };
40 
41 static constexpr OptionEnumValues ScriptOptionEnum() {
42   return OptionEnumValues(g_script_option_enumeration);
43 }
44 
45 static constexpr OptionDefinition g_watchpoint_command_add_options[] = {
46     // clang-format off
47   { LLDB_OPT_SET_1,   false, "one-liner",       'o', OptionParser::eRequiredArgument, nullptr, {},                 0, eArgTypeOneLiner,       "Specify a one-line watchpoint command inline. Be sure to surround it with quotes." },
48   { LLDB_OPT_SET_ALL, false, "stop-on-error",   'e', OptionParser::eRequiredArgument, nullptr, {},                 0, eArgTypeBoolean,        "Specify whether watchpoint command execution should terminate on error." },
49   { LLDB_OPT_SET_ALL, false, "script-type",     's', OptionParser::eRequiredArgument, nullptr, ScriptOptionEnum(), 0, eArgTypeNone,           "Specify the language for the commands - if none is specified, the lldb command interpreter will be used." },
50   { LLDB_OPT_SET_2,   false, "python-function", 'F', OptionParser::eRequiredArgument, nullptr, {},                 0, eArgTypePythonFunction, "Give the name of a Python function to run as command for this watchpoint. Be sure to give a module name if appropriate." }
51     // clang-format on
52 };
53 
54 class CommandObjectWatchpointCommandAdd : public CommandObjectParsed,
55                                           public IOHandlerDelegateMultiline {
56 public:
57   CommandObjectWatchpointCommandAdd(CommandInterpreter &interpreter)
58       : CommandObjectParsed(interpreter, "add",
59                             "Add a set of LLDB commands to a watchpoint, to be "
60                             "executed whenever the watchpoint is hit.",
61                             nullptr),
62         IOHandlerDelegateMultiline("DONE",
63                                    IOHandlerDelegate::Completion::LLDBCommand),
64         m_options() {
65     SetHelpLong(
66         R"(
67 General information about entering watchpoint commands
68 ------------------------------------------------------
69 
70 )"
71         "This command will prompt for commands to be executed when the specified \
72 watchpoint is hit.  Each command is typed on its own line following the '> ' \
73 prompt until 'DONE' is entered."
74         R"(
75 
76 )"
77         "Syntactic errors may not be detected when initially entered, and many \
78 malformed commands can silently fail when executed.  If your watchpoint commands \
79 do not appear to be executing, double-check the command syntax."
80         R"(
81 
82 )"
83         "Note: You may enter any debugger command exactly as you would at the debugger \
84 prompt.  There is no limit to the number of commands supplied, but do NOT enter \
85 more than one command per line."
86         R"(
87 
88 Special information about PYTHON watchpoint commands
89 ----------------------------------------------------
90 
91 )"
92         "You may enter either one or more lines of Python, including function \
93 definitions or calls to functions that will have been imported by the time \
94 the code executes.  Single line watchpoint commands will be interpreted 'as is' \
95 when the watchpoint is hit.  Multiple lines of Python will be wrapped in a \
96 generated function, and a call to the function will be attached to the watchpoint."
97         R"(
98 
99 This auto-generated function is passed in three arguments:
100 
101     frame:  an lldb.SBFrame object for the frame which hit the watchpoint.
102 
103     wp:     the watchpoint that was hit.
104 
105 )"
106         "When specifying a python function with the --python-function option, you need \
107 to supply the function name prepended by the module name:"
108         R"(
109 
110     --python-function myutils.watchpoint_callback
111 
112 The function itself must have the following prototype:
113 
114 def watchpoint_callback(frame, wp):
115   # Your code goes here
116 
117 )"
118         "The arguments are the same as the arguments passed to generated functions as \
119 described above.  Note that the global variable 'lldb.frame' will NOT be updated when \
120 this function is called, so be sure to use the 'frame' argument. The 'frame' argument \
121 can get you to the thread via frame.GetThread(), the thread can get you to the \
122 process via thread.GetProcess(), and the process can get you back to the target \
123 via process.GetTarget()."
124         R"(
125 
126 )"
127         "Important Note: As Python code gets collected into functions, access to global \
128 variables requires explicit scoping using the 'global' keyword.  Be sure to use correct \
129 Python syntax, including indentation, when entering Python watchpoint commands."
130         R"(
131 
132 Example Python one-line watchpoint command:
133 
134 (lldb) watchpoint command add -s python 1
135 Enter your Python command(s). Type 'DONE' to end.
136 > print "Hit this watchpoint!"
137 > DONE
138 
139 As a convenience, this also works for a short Python one-liner:
140 
141 (lldb) watchpoint command add -s python 1 -o 'import time; print time.asctime()'
142 (lldb) run
143 Launching '.../a.out'  (x86_64)
144 (lldb) Fri Sep 10 12:17:45 2010
145 Process 21778 Stopped
146 * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = watchpoint 1.1, queue = com.apple.main-thread
147   36
148   37   	int c(int val)
149   38   	{
150   39 ->	    return val + 3;
151   40   	}
152   41
153   42   	int main (int argc, char const *argv[])
154 
155 Example multiple line Python watchpoint command, using function definition:
156 
157 (lldb) watchpoint command add -s python 1
158 Enter your Python command(s). Type 'DONE' to end.
159 > def watchpoint_output (wp_no):
160 >     out_string = "Hit watchpoint number " + repr (wp_no)
161 >     print out_string
162 >     return True
163 > watchpoint_output (1)
164 > DONE
165 
166 Example multiple line Python watchpoint command, using 'loose' Python:
167 
168 (lldb) watchpoint command add -s p 1
169 Enter your Python command(s). Type 'DONE' to end.
170 > global wp_count
171 > wp_count = wp_count + 1
172 > print "Hit this watchpoint " + repr(wp_count) + " times!"
173 > DONE
174 
175 )"
176         "In this case, since there is a reference to a global variable, \
177 'wp_count', you will also need to make sure 'wp_count' exists and is \
178 initialized:"
179         R"(
180 
181 (lldb) script
182 >>> wp_count = 0
183 >>> quit()
184 
185 )"
186         "Final Note: A warning that no watchpoint command was generated when there \
187 are no syntax errors may indicate that a function was declared but never called.");
188 
189     CommandArgumentEntry arg;
190     CommandArgumentData wp_id_arg;
191 
192     // Define the first (and only) variant of this arg.
193     wp_id_arg.arg_type = eArgTypeWatchpointID;
194     wp_id_arg.arg_repetition = eArgRepeatPlain;
195 
196     // There is only one variant this argument could be; put it into the
197     // argument entry.
198     arg.push_back(wp_id_arg);
199 
200     // Push the data for the first argument into the m_arguments vector.
201     m_arguments.push_back(arg);
202   }
203 
204   ~CommandObjectWatchpointCommandAdd() override = default;
205 
206   Options *GetOptions() override { return &m_options; }
207 
208   void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
209     StreamFileSP output_sp(io_handler.GetOutputStreamFile());
210     if (output_sp && interactive) {
211       output_sp->PutCString(
212           "Enter your debugger command(s).  Type 'DONE' to end.\n");
213       output_sp->Flush();
214     }
215   }
216 
217   void IOHandlerInputComplete(IOHandler &io_handler,
218                               std::string &line) override {
219     io_handler.SetIsDone(true);
220 
221     // The WatchpointOptions object is owned by the watchpoint or watchpoint
222     // location
223     WatchpointOptions *wp_options =
224         (WatchpointOptions *)io_handler.GetUserData();
225     if (wp_options) {
226       std::unique_ptr<WatchpointOptions::CommandData> data_up(
227           new WatchpointOptions::CommandData());
228       if (data_up) {
229         data_up->user_source.SplitIntoLines(line);
230         auto baton_sp = std::make_shared<WatchpointOptions::CommandBaton>(
231             std::move(data_up));
232         wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp);
233       }
234     }
235   }
236 
237   void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
238                                                CommandReturnObject &result) {
239     m_interpreter.GetLLDBCommandsFromIOHandler(
240         "> ",        // Prompt
241         *this,       // IOHandlerDelegate
242         true,        // Run IOHandler in async mode
243         wp_options); // Baton for the "io_handler" that will be passed back into
244                      // our IOHandlerDelegate functions
245   }
246 
247   /// Set a one-liner as the callback for the watchpoint.
248   void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
249                                     const char *oneliner) {
250     std::unique_ptr<WatchpointOptions::CommandData> data_up(
251         new WatchpointOptions::CommandData());
252 
253     // It's necessary to set both user_source and script_source to the
254     // oneliner. The former is used to generate callback description (as in
255     // watchpoint command list) while the latter is used for Python to
256     // interpret during the actual callback.
257     data_up->user_source.AppendString(oneliner);
258     data_up->script_source.assign(oneliner);
259     data_up->stop_on_error = m_options.m_stop_on_error;
260 
261     auto baton_sp =
262         std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up));
263     wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp);
264   }
265 
266   static bool
267   WatchpointOptionsCallbackFunction(void *baton,
268                                     StoppointCallbackContext *context,
269                                     lldb::user_id_t watch_id) {
270     bool ret_value = true;
271     if (baton == nullptr)
272       return true;
273 
274     WatchpointOptions::CommandData *data =
275         (WatchpointOptions::CommandData *)baton;
276     StringList &commands = data->user_source;
277 
278     if (commands.GetSize() > 0) {
279       ExecutionContext exe_ctx(context->exe_ctx_ref);
280       Target *target = exe_ctx.GetTargetPtr();
281       if (target) {
282         CommandReturnObject result;
283         Debugger &debugger = target->GetDebugger();
284         // Rig up the results secondary output stream to the debugger's, so the
285         // output will come out synchronously if the debugger is set up that
286         // way.
287 
288         StreamSP output_stream(debugger.GetAsyncOutputStream());
289         StreamSP error_stream(debugger.GetAsyncErrorStream());
290         result.SetImmediateOutputStream(output_stream);
291         result.SetImmediateErrorStream(error_stream);
292 
293         CommandInterpreterRunOptions options;
294         options.SetStopOnContinue(true);
295         options.SetStopOnError(data->stop_on_error);
296         options.SetEchoCommands(false);
297         options.SetPrintResults(true);
298         options.SetPrintErrors(true);
299         options.SetAddToHistory(false);
300 
301         debugger.GetCommandInterpreter().HandleCommands(commands, &exe_ctx,
302                                                         options, result);
303         result.GetImmediateOutputStream()->Flush();
304         result.GetImmediateErrorStream()->Flush();
305       }
306     }
307     return ret_value;
308   }
309 
310   class CommandOptions : public Options {
311   public:
312     CommandOptions()
313         : Options(), m_use_commands(false), m_use_script_language(false),
314           m_script_language(eScriptLanguageNone), m_use_one_liner(false),
315           m_one_liner(), m_function_name() {}
316 
317     ~CommandOptions() override = default;
318 
319     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
320                           ExecutionContext *execution_context) override {
321       Status error;
322       const int short_option = m_getopt_table[option_idx].val;
323 
324       switch (short_option) {
325       case 'o':
326         m_use_one_liner = true;
327         m_one_liner = option_arg;
328         break;
329 
330       case 's':
331         m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum(
332             option_arg, GetDefinitions()[option_idx].enum_values,
333             eScriptLanguageNone, error);
334 
335         m_use_script_language = (m_script_language == eScriptLanguagePython ||
336                                  m_script_language == eScriptLanguageDefault);
337         break;
338 
339       case 'e': {
340         bool success = false;
341         m_stop_on_error =
342             OptionArgParser::ToBoolean(option_arg, false, &success);
343         if (!success)
344           error.SetErrorStringWithFormat(
345               "invalid value for stop-on-error: \"%s\"",
346               option_arg.str().c_str());
347       } break;
348 
349       case 'F':
350         m_use_one_liner = false;
351         m_use_script_language = true;
352         m_function_name.assign(option_arg);
353         break;
354 
355       default:
356         break;
357       }
358       return error;
359     }
360 
361     void OptionParsingStarting(ExecutionContext *execution_context) override {
362       m_use_commands = true;
363       m_use_script_language = false;
364       m_script_language = eScriptLanguageNone;
365 
366       m_use_one_liner = false;
367       m_stop_on_error = true;
368       m_one_liner.clear();
369       m_function_name.clear();
370     }
371 
372     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
373       return llvm::makeArrayRef(g_watchpoint_command_add_options);
374     }
375 
376     // Instance variables to hold the values for command options.
377 
378     bool m_use_commands;
379     bool m_use_script_language;
380     lldb::ScriptLanguage m_script_language;
381 
382     // Instance variables to hold the values for one_liner options.
383     bool m_use_one_liner;
384     std::string m_one_liner;
385     bool m_stop_on_error;
386     std::string m_function_name;
387   };
388 
389 protected:
390   bool DoExecute(Args &command, CommandReturnObject &result) override {
391     Target *target = GetDebugger().GetSelectedTarget().get();
392 
393     if (target == nullptr) {
394       result.AppendError("There is not a current executable; there are no "
395                          "watchpoints to which to add commands");
396       result.SetStatus(eReturnStatusFailed);
397       return false;
398     }
399 
400     const WatchpointList &watchpoints = target->GetWatchpointList();
401     size_t num_watchpoints = watchpoints.GetSize();
402 
403     if (num_watchpoints == 0) {
404       result.AppendError("No watchpoints exist to have commands added");
405       result.SetStatus(eReturnStatusFailed);
406       return false;
407     }
408 
409     if (!m_options.m_use_script_language &&
410         !m_options.m_function_name.empty()) {
411       result.AppendError("need to enable scripting to have a function run as a "
412                          "watchpoint command");
413       result.SetStatus(eReturnStatusFailed);
414       return false;
415     }
416 
417     std::vector<uint32_t> valid_wp_ids;
418     if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
419                                                                valid_wp_ids)) {
420       result.AppendError("Invalid watchpoints specification.");
421       result.SetStatus(eReturnStatusFailed);
422       return false;
423     }
424 
425     result.SetStatus(eReturnStatusSuccessFinishNoResult);
426     const size_t count = valid_wp_ids.size();
427     for (size_t i = 0; i < count; ++i) {
428       uint32_t cur_wp_id = valid_wp_ids.at(i);
429       if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
430         Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
431         // Sanity check wp first.
432         if (wp == nullptr)
433           continue;
434 
435         WatchpointOptions *wp_options = wp->GetOptions();
436         // Skip this watchpoint if wp_options is not good.
437         if (wp_options == nullptr)
438           continue;
439 
440         // If we are using script language, get the script interpreter in order
441         // to set or collect command callback.  Otherwise, call the methods
442         // associated with this object.
443         if (m_options.m_use_script_language) {
444           // Special handling for one-liner specified inline.
445           if (m_options.m_use_one_liner) {
446             GetDebugger().GetScriptInterpreter()->SetWatchpointCommandCallback(
447                 wp_options, m_options.m_one_liner.c_str());
448           }
449           // Special handling for using a Python function by name instead of
450           // extending the watchpoint callback data structures, we just
451           // automatize what the user would do manually: make their watchpoint
452           // command be a function call
453           else if (!m_options.m_function_name.empty()) {
454             std::string oneliner(m_options.m_function_name);
455             oneliner += "(frame, wp, internal_dict)";
456             GetDebugger().GetScriptInterpreter()->SetWatchpointCommandCallback(
457                 wp_options, oneliner.c_str());
458           } else {
459             GetDebugger()
460                 .GetScriptInterpreter()
461                 ->CollectDataForWatchpointCommandCallback(wp_options, result);
462           }
463         } else {
464           // Special handling for one-liner specified inline.
465           if (m_options.m_use_one_liner)
466             SetWatchpointCommandCallback(wp_options,
467                                          m_options.m_one_liner.c_str());
468           else
469             CollectDataForWatchpointCommandCallback(wp_options, result);
470         }
471       }
472     }
473 
474     return result.Succeeded();
475   }
476 
477 private:
478   CommandOptions m_options;
479 };
480 
481 // CommandObjectWatchpointCommandDelete
482 
483 class CommandObjectWatchpointCommandDelete : public CommandObjectParsed {
484 public:
485   CommandObjectWatchpointCommandDelete(CommandInterpreter &interpreter)
486       : CommandObjectParsed(interpreter, "delete",
487                             "Delete the set of commands from a watchpoint.",
488                             nullptr) {
489     CommandArgumentEntry arg;
490     CommandArgumentData wp_id_arg;
491 
492     // Define the first (and only) variant of this arg.
493     wp_id_arg.arg_type = eArgTypeWatchpointID;
494     wp_id_arg.arg_repetition = eArgRepeatPlain;
495 
496     // There is only one variant this argument could be; put it into the
497     // argument entry.
498     arg.push_back(wp_id_arg);
499 
500     // Push the data for the first argument into the m_arguments vector.
501     m_arguments.push_back(arg);
502   }
503 
504   ~CommandObjectWatchpointCommandDelete() override = default;
505 
506 protected:
507   bool DoExecute(Args &command, CommandReturnObject &result) override {
508     Target *target = GetDebugger().GetSelectedTarget().get();
509 
510     if (target == nullptr) {
511       result.AppendError("There is not a current executable; there are no "
512                          "watchpoints from which to delete commands");
513       result.SetStatus(eReturnStatusFailed);
514       return false;
515     }
516 
517     const WatchpointList &watchpoints = target->GetWatchpointList();
518     size_t num_watchpoints = watchpoints.GetSize();
519 
520     if (num_watchpoints == 0) {
521       result.AppendError("No watchpoints exist to have commands deleted");
522       result.SetStatus(eReturnStatusFailed);
523       return false;
524     }
525 
526     if (command.GetArgumentCount() == 0) {
527       result.AppendError(
528           "No watchpoint specified from which to delete the commands");
529       result.SetStatus(eReturnStatusFailed);
530       return false;
531     }
532 
533     std::vector<uint32_t> valid_wp_ids;
534     if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
535                                                                valid_wp_ids)) {
536       result.AppendError("Invalid watchpoints specification.");
537       result.SetStatus(eReturnStatusFailed);
538       return false;
539     }
540 
541     result.SetStatus(eReturnStatusSuccessFinishNoResult);
542     const size_t count = valid_wp_ids.size();
543     for (size_t i = 0; i < count; ++i) {
544       uint32_t cur_wp_id = valid_wp_ids.at(i);
545       if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
546         Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
547         if (wp)
548           wp->ClearCallback();
549       } else {
550         result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", cur_wp_id);
551         result.SetStatus(eReturnStatusFailed);
552         return false;
553       }
554     }
555     return result.Succeeded();
556   }
557 };
558 
559 // CommandObjectWatchpointCommandList
560 
561 class CommandObjectWatchpointCommandList : public CommandObjectParsed {
562 public:
563   CommandObjectWatchpointCommandList(CommandInterpreter &interpreter)
564       : CommandObjectParsed(interpreter, "list", "List the script or set of "
565                                                  "commands to be executed when "
566                                                  "the watchpoint is hit.",
567                             nullptr) {
568     CommandArgumentEntry arg;
569     CommandArgumentData wp_id_arg;
570 
571     // Define the first (and only) variant of this arg.
572     wp_id_arg.arg_type = eArgTypeWatchpointID;
573     wp_id_arg.arg_repetition = eArgRepeatPlain;
574 
575     // There is only one variant this argument could be; put it into the
576     // argument entry.
577     arg.push_back(wp_id_arg);
578 
579     // Push the data for the first argument into the m_arguments vector.
580     m_arguments.push_back(arg);
581   }
582 
583   ~CommandObjectWatchpointCommandList() override = default;
584 
585 protected:
586   bool DoExecute(Args &command, CommandReturnObject &result) override {
587     Target *target = GetDebugger().GetSelectedTarget().get();
588 
589     if (target == nullptr) {
590       result.AppendError("There is not a current executable; there are no "
591                          "watchpoints for which to list commands");
592       result.SetStatus(eReturnStatusFailed);
593       return false;
594     }
595 
596     const WatchpointList &watchpoints = target->GetWatchpointList();
597     size_t num_watchpoints = watchpoints.GetSize();
598 
599     if (num_watchpoints == 0) {
600       result.AppendError("No watchpoints exist for which to list commands");
601       result.SetStatus(eReturnStatusFailed);
602       return false;
603     }
604 
605     if (command.GetArgumentCount() == 0) {
606       result.AppendError(
607           "No watchpoint specified for which to list the commands");
608       result.SetStatus(eReturnStatusFailed);
609       return false;
610     }
611 
612     std::vector<uint32_t> valid_wp_ids;
613     if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
614                                                                valid_wp_ids)) {
615       result.AppendError("Invalid watchpoints specification.");
616       result.SetStatus(eReturnStatusFailed);
617       return false;
618     }
619 
620     result.SetStatus(eReturnStatusSuccessFinishNoResult);
621     const size_t count = valid_wp_ids.size();
622     for (size_t i = 0; i < count; ++i) {
623       uint32_t cur_wp_id = valid_wp_ids.at(i);
624       if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
625         Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
626 
627         if (wp) {
628           const WatchpointOptions *wp_options = wp->GetOptions();
629           if (wp_options) {
630             // Get the callback baton associated with the current watchpoint.
631             const Baton *baton = wp_options->GetBaton();
632             if (baton) {
633               result.GetOutputStream().Printf("Watchpoint %u:\n", cur_wp_id);
634               result.GetOutputStream().IndentMore();
635               baton->GetDescription(&result.GetOutputStream(),
636                                     eDescriptionLevelFull);
637               result.GetOutputStream().IndentLess();
638             } else {
639               result.AppendMessageWithFormat(
640                   "Watchpoint %u does not have an associated command.\n",
641                   cur_wp_id);
642             }
643           }
644           result.SetStatus(eReturnStatusSuccessFinishResult);
645         } else {
646           result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n",
647                                        cur_wp_id);
648           result.SetStatus(eReturnStatusFailed);
649         }
650       }
651     }
652 
653     return result.Succeeded();
654   }
655 };
656 
657 // CommandObjectWatchpointCommand
658 
659 CommandObjectWatchpointCommand::CommandObjectWatchpointCommand(
660     CommandInterpreter &interpreter)
661     : CommandObjectMultiword(
662           interpreter, "command",
663           "Commands for adding, removing and examining LLDB commands "
664           "executed when the watchpoint is hit (watchpoint 'commands').",
665           "command <sub-command> [<sub-command-options>] <watchpoint-id>") {
666   CommandObjectSP add_command_object(
667       new CommandObjectWatchpointCommandAdd(interpreter));
668   CommandObjectSP delete_command_object(
669       new CommandObjectWatchpointCommandDelete(interpreter));
670   CommandObjectSP list_command_object(
671       new CommandObjectWatchpointCommandList(interpreter));
672 
673   add_command_object->SetCommandName("watchpoint command add");
674   delete_command_object->SetCommandName("watchpoint command delete");
675   list_command_object->SetCommandName("watchpoint command list");
676 
677   LoadSubCommand("add", add_command_object);
678   LoadSubCommand("delete", delete_command_object);
679   LoadSubCommand("list", list_command_object);
680 }
681 
682 CommandObjectWatchpointCommand::~CommandObjectWatchpointCommand() = default;
683