xref: /llvm-project/lldb/source/Commands/CommandObjectWatchpointCommand.cpp (revision 8fe53c490a567d3e9337b974057a239477dbe685)
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/Host/OptionParser.h"
22 #include "lldb/Interpreter/CommandInterpreter.h"
23 #include "lldb/Interpreter/CommandReturnObject.h"
24 #include "lldb/Interpreter/OptionArgParser.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Target/Thread.h"
27 #include "lldb/Utility/State.h"
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 
32 //-------------------------------------------------------------------------
33 // CommandObjectWatchpointCommandAdd
34 //-------------------------------------------------------------------------
35 
36 // FIXME: "script-type" needs to have its contents determined dynamically, so
37 // somebody can add a new scripting
38 // language to lldb and have it pickable here without having to change this
39 // enumeration by hand and rebuild lldb proper.
40 
41 static constexpr OptionEnumValueElement g_script_option_enumeration[] = {
42     {eScriptLanguageNone, "command",
43      "Commands are in the lldb command interpreter language"},
44     {eScriptLanguagePython, "python", "Commands are in the Python language."},
45     {eSortOrderByName, "default-script",
46      "Commands are in the default scripting language."} };
47 
48 static constexpr OptionEnumValues ScriptOptionEnum() {
49   return OptionEnumValues(g_script_option_enumeration);
50 }
51 
52 static constexpr OptionDefinition g_watchpoint_command_add_options[] = {
53     // clang-format off
54   { 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." },
55   { LLDB_OPT_SET_ALL, false, "stop-on-error",   'e', OptionParser::eRequiredArgument, nullptr, {},                 0, eArgTypeBoolean,        "Specify whether watchpoint command execution should terminate on error." },
56   { 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." },
57   { 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." }
58     // clang-format on
59 };
60 
61 class CommandObjectWatchpointCommandAdd : public CommandObjectParsed,
62                                           public IOHandlerDelegateMultiline {
63 public:
64   CommandObjectWatchpointCommandAdd(CommandInterpreter &interpreter)
65       : CommandObjectParsed(interpreter, "add",
66                             "Add a set of LLDB commands to a watchpoint, to be "
67                             "executed whenever the watchpoint is hit.",
68                             nullptr),
69         IOHandlerDelegateMultiline("DONE",
70                                    IOHandlerDelegate::Completion::LLDBCommand),
71         m_options() {
72     SetHelpLong(
73         R"(
74 General information about entering watchpoint commands
75 ------------------------------------------------------
76 
77 )"
78         "This command will prompt for commands to be executed when the specified \
79 watchpoint is hit.  Each command is typed on its own line following the '> ' \
80 prompt until 'DONE' is entered."
81         R"(
82 
83 )"
84         "Syntactic errors may not be detected when initially entered, and many \
85 malformed commands can silently fail when executed.  If your watchpoint commands \
86 do not appear to be executing, double-check the command syntax."
87         R"(
88 
89 )"
90         "Note: You may enter any debugger command exactly as you would at the debugger \
91 prompt.  There is no limit to the number of commands supplied, but do NOT enter \
92 more than one command per line."
93         R"(
94 
95 Special information about PYTHON watchpoint commands
96 ----------------------------------------------------
97 
98 )"
99         "You may enter either one or more lines of Python, including function \
100 definitions or calls to functions that will have been imported by the time \
101 the code executes.  Single line watchpoint commands will be interpreted 'as is' \
102 when the watchpoint is hit.  Multiple lines of Python will be wrapped in a \
103 generated function, and a call to the function will be attached to the watchpoint."
104         R"(
105 
106 This auto-generated function is passed in three arguments:
107 
108     frame:  an lldb.SBFrame object for the frame which hit the watchpoint.
109 
110     wp:     the watchpoint that was hit.
111 
112 )"
113         "When specifying a python function with the --python-function option, you need \
114 to supply the function name prepended by the module name:"
115         R"(
116 
117     --python-function myutils.watchpoint_callback
118 
119 The function itself must have the following prototype:
120 
121 def watchpoint_callback(frame, wp):
122   # Your code goes here
123 
124 )"
125         "The arguments are the same as the arguments passed to generated functions as \
126 described above.  Note that the global variable 'lldb.frame' will NOT be updated when \
127 this function is called, so be sure to use the 'frame' argument. The 'frame' argument \
128 can get you to the thread via frame.GetThread(), the thread can get you to the \
129 process via thread.GetProcess(), and the process can get you back to the target \
130 via process.GetTarget()."
131         R"(
132 
133 )"
134         "Important Note: As Python code gets collected into functions, access to global \
135 variables requires explicit scoping using the 'global' keyword.  Be sure to use correct \
136 Python syntax, including indentation, when entering Python watchpoint commands."
137         R"(
138 
139 Example Python one-line watchpoint command:
140 
141 (lldb) watchpoint command add -s python 1
142 Enter your Python command(s). Type 'DONE' to end.
143 > print "Hit this watchpoint!"
144 > DONE
145 
146 As a convenience, this also works for a short Python one-liner:
147 
148 (lldb) watchpoint command add -s python 1 -o 'import time; print time.asctime()'
149 (lldb) run
150 Launching '.../a.out'  (x86_64)
151 (lldb) Fri Sep 10 12:17:45 2010
152 Process 21778 Stopped
153 * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = watchpoint 1.1, queue = com.apple.main-thread
154   36
155   37   	int c(int val)
156   38   	{
157   39 ->	    return val + 3;
158   40   	}
159   41
160   42   	int main (int argc, char const *argv[])
161 
162 Example multiple line Python watchpoint command, using function definition:
163 
164 (lldb) watchpoint command add -s python 1
165 Enter your Python command(s). Type 'DONE' to end.
166 > def watchpoint_output (wp_no):
167 >     out_string = "Hit watchpoint number " + repr (wp_no)
168 >     print out_string
169 >     return True
170 > watchpoint_output (1)
171 > DONE
172 
173 Example multiple line Python watchpoint command, using 'loose' Python:
174 
175 (lldb) watchpoint command add -s p 1
176 Enter your Python command(s). Type 'DONE' to end.
177 > global wp_count
178 > wp_count = wp_count + 1
179 > print "Hit this watchpoint " + repr(wp_count) + " times!"
180 > DONE
181 
182 )"
183         "In this case, since there is a reference to a global variable, \
184 'wp_count', you will also need to make sure 'wp_count' exists and is \
185 initialized:"
186         R"(
187 
188 (lldb) script
189 >>> wp_count = 0
190 >>> quit()
191 
192 )"
193         "Final Note: A warning that no watchpoint command was generated when there \
194 are no syntax errors may indicate that a function was declared but never called.");
195 
196     CommandArgumentEntry arg;
197     CommandArgumentData wp_id_arg;
198 
199     // Define the first (and only) variant of this arg.
200     wp_id_arg.arg_type = eArgTypeWatchpointID;
201     wp_id_arg.arg_repetition = eArgRepeatPlain;
202 
203     // There is only one variant this argument could be; put it into the
204     // argument entry.
205     arg.push_back(wp_id_arg);
206 
207     // Push the data for the first argument into the m_arguments vector.
208     m_arguments.push_back(arg);
209   }
210 
211   ~CommandObjectWatchpointCommandAdd() override = default;
212 
213   Options *GetOptions() override { return &m_options; }
214 
215   void IOHandlerActivated(IOHandler &io_handler) override {
216     StreamFileSP output_sp(io_handler.GetOutputStreamFile());
217     if (output_sp) {
218       output_sp->PutCString(
219           "Enter your debugger command(s).  Type 'DONE' to end.\n");
220       output_sp->Flush();
221     }
222   }
223 
224   void IOHandlerInputComplete(IOHandler &io_handler,
225                               std::string &line) override {
226     io_handler.SetIsDone(true);
227 
228     // The WatchpointOptions object is owned by the watchpoint or watchpoint
229     // location
230     WatchpointOptions *wp_options =
231         (WatchpointOptions *)io_handler.GetUserData();
232     if (wp_options) {
233       std::unique_ptr<WatchpointOptions::CommandData> data_ap(
234           new WatchpointOptions::CommandData());
235       if (data_ap) {
236         data_ap->user_source.SplitIntoLines(line);
237         auto baton_sp = std::make_shared<WatchpointOptions::CommandBaton>(
238             std::move(data_ap));
239         wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp);
240       }
241     }
242   }
243 
244   void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
245                                                CommandReturnObject &result) {
246     m_interpreter.GetLLDBCommandsFromIOHandler(
247         "> ",        // Prompt
248         *this,       // IOHandlerDelegate
249         true,        // Run IOHandler in async mode
250         wp_options); // Baton for the "io_handler" that will be passed back into
251                      // our IOHandlerDelegate functions
252   }
253 
254   /// Set a one-liner as the callback for the watchpoint.
255   void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
256                                     const char *oneliner) {
257     std::unique_ptr<WatchpointOptions::CommandData> data_ap(
258         new WatchpointOptions::CommandData());
259 
260     // It's necessary to set both user_source and script_source to the
261     // oneliner. The former is used to generate callback description (as in
262     // watchpoint command list) while the latter is used for Python to
263     // interpret during the actual callback.
264     data_ap->user_source.AppendString(oneliner);
265     data_ap->script_source.assign(oneliner);
266     data_ap->stop_on_error = m_options.m_stop_on_error;
267 
268     auto baton_sp =
269         std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_ap));
270     wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp);
271   }
272 
273   static bool
274   WatchpointOptionsCallbackFunction(void *baton,
275                                     StoppointCallbackContext *context,
276                                     lldb::user_id_t watch_id) {
277     bool ret_value = true;
278     if (baton == nullptr)
279       return true;
280 
281     WatchpointOptions::CommandData *data =
282         (WatchpointOptions::CommandData *)baton;
283     StringList &commands = data->user_source;
284 
285     if (commands.GetSize() > 0) {
286       ExecutionContext exe_ctx(context->exe_ctx_ref);
287       Target *target = exe_ctx.GetTargetPtr();
288       if (target) {
289         CommandReturnObject result;
290         Debugger &debugger = target->GetDebugger();
291         // Rig up the results secondary output stream to the debugger's, so the
292         // output will come out synchronously if the debugger is set up that
293         // way.
294 
295         StreamSP output_stream(debugger.GetAsyncOutputStream());
296         StreamSP error_stream(debugger.GetAsyncErrorStream());
297         result.SetImmediateOutputStream(output_stream);
298         result.SetImmediateErrorStream(error_stream);
299 
300         CommandInterpreterRunOptions options;
301         options.SetStopOnContinue(true);
302         options.SetStopOnError(data->stop_on_error);
303         options.SetEchoCommands(false);
304         options.SetPrintResults(true);
305         options.SetAddToHistory(false);
306 
307         debugger.GetCommandInterpreter().HandleCommands(commands, &exe_ctx,
308                                                         options, result);
309         result.GetImmediateOutputStream()->Flush();
310         result.GetImmediateErrorStream()->Flush();
311       }
312     }
313     return ret_value;
314   }
315 
316   class CommandOptions : public Options {
317   public:
318     CommandOptions()
319         : Options(), m_use_commands(false), m_use_script_language(false),
320           m_script_language(eScriptLanguageNone), m_use_one_liner(false),
321           m_one_liner(), m_function_name() {}
322 
323     ~CommandOptions() override = default;
324 
325     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
326                           ExecutionContext *execution_context) override {
327       Status error;
328       const int short_option = m_getopt_table[option_idx].val;
329 
330       switch (short_option) {
331       case 'o':
332         m_use_one_liner = true;
333         m_one_liner = option_arg;
334         break;
335 
336       case 's':
337         m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum(
338             option_arg, GetDefinitions()[option_idx].enum_values,
339             eScriptLanguageNone, error);
340 
341         m_use_script_language = (m_script_language == eScriptLanguagePython ||
342                                  m_script_language == eScriptLanguageDefault);
343         break;
344 
345       case 'e': {
346         bool success = false;
347         m_stop_on_error =
348             OptionArgParser::ToBoolean(option_arg, false, &success);
349         if (!success)
350           error.SetErrorStringWithFormat(
351               "invalid value for stop-on-error: \"%s\"",
352               option_arg.str().c_str());
353       } break;
354 
355       case 'F':
356         m_use_one_liner = false;
357         m_use_script_language = true;
358         m_function_name.assign(option_arg);
359         break;
360 
361       default:
362         break;
363       }
364       return error;
365     }
366 
367     void OptionParsingStarting(ExecutionContext *execution_context) override {
368       m_use_commands = true;
369       m_use_script_language = false;
370       m_script_language = eScriptLanguageNone;
371 
372       m_use_one_liner = false;
373       m_stop_on_error = true;
374       m_one_liner.clear();
375       m_function_name.clear();
376     }
377 
378     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
379       return llvm::makeArrayRef(g_watchpoint_command_add_options);
380     }
381 
382     // Instance variables to hold the values for command options.
383 
384     bool m_use_commands;
385     bool m_use_script_language;
386     lldb::ScriptLanguage m_script_language;
387 
388     // Instance variables to hold the values for one_liner options.
389     bool m_use_one_liner;
390     std::string m_one_liner;
391     bool m_stop_on_error;
392     std::string m_function_name;
393   };
394 
395 protected:
396   bool DoExecute(Args &command, CommandReturnObject &result) override {
397     Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
398 
399     if (target == nullptr) {
400       result.AppendError("There is not a current executable; there are no "
401                          "watchpoints to which to add commands");
402       result.SetStatus(eReturnStatusFailed);
403       return false;
404     }
405 
406     const WatchpointList &watchpoints = target->GetWatchpointList();
407     size_t num_watchpoints = watchpoints.GetSize();
408 
409     if (num_watchpoints == 0) {
410       result.AppendError("No watchpoints exist to have commands added");
411       result.SetStatus(eReturnStatusFailed);
412       return false;
413     }
414 
415     if (!m_options.m_use_script_language &&
416         !m_options.m_function_name.empty()) {
417       result.AppendError("need to enable scripting to have a function run as a "
418                          "watchpoint command");
419       result.SetStatus(eReturnStatusFailed);
420       return false;
421     }
422 
423     std::vector<uint32_t> valid_wp_ids;
424     if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
425                                                                valid_wp_ids)) {
426       result.AppendError("Invalid watchpoints specification.");
427       result.SetStatus(eReturnStatusFailed);
428       return false;
429     }
430 
431     result.SetStatus(eReturnStatusSuccessFinishNoResult);
432     const size_t count = valid_wp_ids.size();
433     for (size_t i = 0; i < count; ++i) {
434       uint32_t cur_wp_id = valid_wp_ids.at(i);
435       if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
436         Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
437         // Sanity check wp first.
438         if (wp == nullptr)
439           continue;
440 
441         WatchpointOptions *wp_options = wp->GetOptions();
442         // Skip this watchpoint if wp_options is not good.
443         if (wp_options == nullptr)
444           continue;
445 
446         // If we are using script language, get the script interpreter in order
447         // to set or collect command callback.  Otherwise, call the methods
448         // associated with this object.
449         if (m_options.m_use_script_language) {
450           // Special handling for one-liner specified inline.
451           if (m_options.m_use_one_liner) {
452             m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback(
453                 wp_options, m_options.m_one_liner.c_str());
454           }
455           // Special handling for using a Python function by name instead of
456           // extending the watchpoint callback data structures, we just
457           // automatize what the user would do manually: make their watchpoint
458           // command be a function call
459           else if (!m_options.m_function_name.empty()) {
460             std::string oneliner(m_options.m_function_name);
461             oneliner += "(frame, wp, internal_dict)";
462             m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback(
463                 wp_options, oneliner.c_str());
464           } else {
465             m_interpreter.GetScriptInterpreter()
466                 ->CollectDataForWatchpointCommandCallback(wp_options, result);
467           }
468         } else {
469           // Special handling for one-liner specified inline.
470           if (m_options.m_use_one_liner)
471             SetWatchpointCommandCallback(wp_options,
472                                          m_options.m_one_liner.c_str());
473           else
474             CollectDataForWatchpointCommandCallback(wp_options, result);
475         }
476       }
477     }
478 
479     return result.Succeeded();
480   }
481 
482 private:
483   CommandOptions m_options;
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 'commands').",
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