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