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