1 //===-- CommandObjectBreakpointCommand.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 "CommandObjectBreakpointCommand.h"
10 #include "CommandObjectBreakpoint.h"
11 #include "lldb/Breakpoint/Breakpoint.h"
12 #include "lldb/Breakpoint/BreakpointIDList.h"
13 #include "lldb/Breakpoint/BreakpointLocation.h"
14 #include "lldb/Breakpoint/StoppointCallbackContext.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 #include "llvm/ADT/STLExtras.h"
25 
26 using namespace lldb;
27 using namespace lldb_private;
28 
29 // FIXME: "script-type" needs to have its contents determined dynamically, so
30 // somebody can add a new scripting language to lldb and have it pickable here
31 // without having to change this enumeration by hand and rebuild lldb proper.
32 static constexpr OptionEnumValueElement g_script_option_enumeration[] = {
33     {
34         eScriptLanguageNone,
35         "command",
36         "Commands are in the lldb command interpreter language",
37     },
38     {
39         eScriptLanguagePython,
40         "python",
41         "Commands are in the Python language.",
42     },
43     {
44         eSortOrderByName,
45         "default-script",
46         "Commands are in the default scripting language.",
47     },
48 };
49 
50 static constexpr OptionEnumValues ScriptOptionEnum() {
51   return OptionEnumValues(g_script_option_enumeration);
52 }
53 
54 #define LLDB_OPTIONS_breakpoint_command_add
55 #include "CommandOptions.inc"
56 
57 class CommandObjectBreakpointCommandAdd : public CommandObjectParsed,
58                                           public IOHandlerDelegateMultiline {
59 public:
60   CommandObjectBreakpointCommandAdd(CommandInterpreter &interpreter)
61       : CommandObjectParsed(interpreter, "add",
62                             "Add LLDB commands to a breakpoint, to be executed "
63                             "whenever the breakpoint is hit."
64                             "  If no breakpoint is specified, adds the "
65                             "commands to the last created breakpoint.",
66                             nullptr),
67         IOHandlerDelegateMultiline("DONE",
68                                    IOHandlerDelegate::Completion::LLDBCommand),
69         m_options() {
70     SetHelpLong(
71         R"(
72 General information about entering breakpoint commands
73 ------------------------------------------------------
74 
75 )"
76         "This command will prompt for commands to be executed when the specified \
77 breakpoint is hit.  Each command is typed on its own line following the '> ' \
78 prompt until 'DONE' is entered."
79         R"(
80 
81 )"
82         "Syntactic errors may not be detected when initially entered, and many \
83 malformed commands can silently fail when executed.  If your breakpoint commands \
84 do not appear to be executing, double-check the command syntax."
85         R"(
86 
87 )"
88         "Note: You may enter any debugger command exactly as you would at the debugger \
89 prompt.  There is no limit to the number of commands supplied, but do NOT enter \
90 more than one command per line."
91         R"(
92 
93 Special information about PYTHON breakpoint commands
94 ----------------------------------------------------
95 
96 )"
97         "You may enter either one or more lines of Python, including function \
98 definitions or calls to functions that will have been imported by the time \
99 the code executes.  Single line breakpoint commands will be interpreted 'as is' \
100 when the breakpoint is hit.  Multiple lines of Python will be wrapped in a \
101 generated function, and a call to the function will be attached to the breakpoint."
102         R"(
103 
104 This auto-generated function is passed in three arguments:
105 
106     frame:  an lldb.SBFrame object for the frame which hit breakpoint.
107 
108     bp_loc: an lldb.SBBreakpointLocation object that represents the breakpoint location that was hit.
109 
110     dict:   the python session dictionary 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.breakpoint_callback
118 
119 The function itself must have the following prototype:
120 
121 def breakpoint_callback(frame, bp_loc, dict):
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 breakpoint commands."
137         R"(
138 
139 Example Python one-line breakpoint command:
140 
141 (lldb) breakpoint command add -s python 1
142 Enter your Python command(s). Type 'DONE' to end.
143 > print "Hit this breakpoint!"
144 > DONE
145 
146 As a convenience, this also works for a short Python one-liner:
147 
148 (lldb) breakpoint 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 = breakpoint 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 breakpoint command:
163 
164 (lldb) breakpoint command add -s p 1
165 Enter your Python command(s). Type 'DONE' to end.
166 > global bp_count
167 > bp_count = bp_count + 1
168 > print "Hit this breakpoint " + repr(bp_count) + " times!"
169 > DONE
170 
171 Example multiple line Python breakpoint command, using function definition:
172 
173 (lldb) breakpoint command add -s python 1
174 Enter your Python command(s). Type 'DONE' to end.
175 > def breakpoint_output (bp_no):
176 >     out_string = "Hit breakpoint number " + repr (bp_no)
177 >     print out_string
178 >     return True
179 > breakpoint_output (1)
180 > DONE
181 
182 )"
183         "In this case, since there is a reference to a global variable, \
184 'bp_count', you will also need to make sure 'bp_count' exists and is \
185 initialized:"
186         R"(
187 
188 (lldb) script
189 >>> bp_count = 0
190 >>> quit()
191 
192 )"
193         "Your Python code, however organized, can optionally return a value.  \
194 If the returned value is False, that tells LLDB not to stop at the breakpoint \
195 to which the code is associated. Returning anything other than False, or even \
196 returning None, or even omitting a return statement entirely, will cause \
197 LLDB to stop."
198         R"(
199 
200 )"
201         "Final Note: A warning that no breakpoint command was generated when there \
202 are no syntax errors may indicate that a function was declared but never called.");
203 
204     CommandArgumentEntry arg;
205     CommandArgumentData bp_id_arg;
206 
207     // Define the first (and only) variant of this arg.
208     bp_id_arg.arg_type = eArgTypeBreakpointID;
209     bp_id_arg.arg_repetition = eArgRepeatOptional;
210 
211     // There is only one variant this argument could be; put it into the
212     // argument entry.
213     arg.push_back(bp_id_arg);
214 
215     // Push the data for the first argument into the m_arguments vector.
216     m_arguments.push_back(arg);
217   }
218 
219   ~CommandObjectBreakpointCommandAdd() override = default;
220 
221   Options *GetOptions() override { return &m_options; }
222 
223   void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
224     StreamFileSP output_sp(io_handler.GetOutputStreamFile());
225     if (output_sp && interactive) {
226       output_sp->PutCString(g_reader_instructions);
227       output_sp->Flush();
228     }
229   }
230 
231   void IOHandlerInputComplete(IOHandler &io_handler,
232                               std::string &line) override {
233     io_handler.SetIsDone(true);
234 
235     std::vector<BreakpointOptions *> *bp_options_vec =
236         (std::vector<BreakpointOptions *> *)io_handler.GetUserData();
237     for (BreakpointOptions *bp_options : *bp_options_vec) {
238       if (!bp_options)
239         continue;
240 
241       auto cmd_data = std::make_unique<BreakpointOptions::CommandData>();
242       cmd_data->user_source.SplitIntoLines(line.c_str(), line.size());
243       bp_options->SetCommandDataCallback(cmd_data);
244     }
245   }
246 
247   void CollectDataForBreakpointCommandCallback(
248       std::vector<BreakpointOptions *> &bp_options_vec,
249       CommandReturnObject &result) {
250     m_interpreter.GetLLDBCommandsFromIOHandler(
251         "> ",             // Prompt
252         *this,            // IOHandlerDelegate
253         true,             // Run IOHandler in async mode
254         &bp_options_vec); // Baton for the "io_handler" that will be passed back
255                           // into our IOHandlerDelegate functions
256   }
257 
258   /// Set a one-liner as the callback for the breakpoint.
259   void
260   SetBreakpointCommandCallback(std::vector<BreakpointOptions *> &bp_options_vec,
261                                const char *oneliner) {
262     for (auto bp_options : bp_options_vec) {
263       auto cmd_data = std::make_unique<BreakpointOptions::CommandData>();
264 
265       cmd_data->user_source.AppendString(oneliner);
266       cmd_data->stop_on_error = m_options.m_stop_on_error;
267 
268       bp_options->SetCommandDataCallback(cmd_data);
269     }
270   }
271 
272   class CommandOptions : public Options {
273   public:
274     CommandOptions()
275         : Options(), m_use_commands(false), m_use_script_language(false),
276           m_script_language(eScriptLanguageNone), m_use_one_liner(false),
277           m_one_liner(), m_function_name() {}
278 
279     ~CommandOptions() override = default;
280 
281     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
282                           ExecutionContext *execution_context) override {
283       Status error;
284       const int short_option = m_getopt_table[option_idx].val;
285 
286       switch (short_option) {
287       case 'o':
288         m_use_one_liner = true;
289         m_one_liner = option_arg;
290         break;
291 
292       case 's':
293         m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum(
294             option_arg,
295             g_breakpoint_command_add_options[option_idx].enum_values,
296             eScriptLanguageNone, error);
297 
298         if (m_script_language == eScriptLanguagePython ||
299             m_script_language == eScriptLanguageDefault) {
300           m_use_script_language = true;
301         } else {
302           m_use_script_language = false;
303         }
304         break;
305 
306       case 'e': {
307         bool success = false;
308         m_stop_on_error =
309             OptionArgParser::ToBoolean(option_arg, false, &success);
310         if (!success)
311           error.SetErrorStringWithFormat(
312               "invalid value for stop-on-error: \"%s\"",
313               option_arg.str().c_str());
314       } break;
315 
316       case 'F':
317         m_use_one_liner = false;
318         m_use_script_language = true;
319         m_function_name.assign(option_arg);
320         break;
321 
322       case 'D':
323         m_use_dummy = true;
324         break;
325 
326       default:
327         break;
328       }
329       return error;
330     }
331 
332     void OptionParsingStarting(ExecutionContext *execution_context) override {
333       m_use_commands = true;
334       m_use_script_language = false;
335       m_script_language = eScriptLanguageNone;
336 
337       m_use_one_liner = false;
338       m_stop_on_error = true;
339       m_one_liner.clear();
340       m_function_name.clear();
341       m_use_dummy = false;
342     }
343 
344     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
345       return llvm::makeArrayRef(g_breakpoint_command_add_options);
346     }
347 
348     // Instance variables to hold the values for command options.
349 
350     bool m_use_commands;
351     bool m_use_script_language;
352     lldb::ScriptLanguage m_script_language;
353 
354     // Instance variables to hold the values for one_liner options.
355     bool m_use_one_liner;
356     std::string m_one_liner;
357     bool m_stop_on_error;
358     std::string m_function_name;
359     bool m_use_dummy;
360   };
361 
362 protected:
363   bool DoExecute(Args &command, CommandReturnObject &result) override {
364     Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
365 
366     if (target == nullptr) {
367       result.AppendError("There is not a current executable; there are no "
368                          "breakpoints to which to add commands");
369       result.SetStatus(eReturnStatusFailed);
370       return false;
371     }
372 
373     const BreakpointList &breakpoints = target->GetBreakpointList();
374     size_t num_breakpoints = breakpoints.GetSize();
375 
376     if (num_breakpoints == 0) {
377       result.AppendError("No breakpoints exist to have commands added");
378       result.SetStatus(eReturnStatusFailed);
379       return false;
380     }
381 
382     if (!m_options.m_use_script_language &&
383         !m_options.m_function_name.empty()) {
384       result.AppendError("need to enable scripting to have a function run as a "
385                          "breakpoint command");
386       result.SetStatus(eReturnStatusFailed);
387       return false;
388     }
389 
390     BreakpointIDList valid_bp_ids;
391     CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
392         command, target, result, &valid_bp_ids,
393         BreakpointName::Permissions::PermissionKinds::listPerm);
394 
395     m_bp_options_vec.clear();
396 
397     if (result.Succeeded()) {
398       const size_t count = valid_bp_ids.GetSize();
399 
400       for (size_t i = 0; i < count; ++i) {
401         BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
402         if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
403           Breakpoint *bp =
404               target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
405           BreakpointOptions *bp_options = nullptr;
406           if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID) {
407             // This breakpoint does not have an associated location.
408             bp_options = bp->GetOptions();
409           } else {
410             BreakpointLocationSP bp_loc_sp(
411                 bp->FindLocationByID(cur_bp_id.GetLocationID()));
412             // This breakpoint does have an associated location. Get its
413             // breakpoint options.
414             if (bp_loc_sp)
415               bp_options = bp_loc_sp->GetLocationOptions();
416           }
417           if (bp_options)
418             m_bp_options_vec.push_back(bp_options);
419         }
420       }
421 
422       // If we are using script language, get the script interpreter in order
423       // to set or collect command callback.  Otherwise, call the methods
424       // associated with this object.
425       if (m_options.m_use_script_language) {
426         ScriptInterpreter *script_interp = GetDebugger().GetScriptInterpreter();
427         // Special handling for one-liner specified inline.
428         if (m_options.m_use_one_liner) {
429           script_interp->SetBreakpointCommandCallback(
430               m_bp_options_vec, m_options.m_one_liner.c_str());
431         } else if (!m_options.m_function_name.empty()) {
432           script_interp->SetBreakpointCommandCallbackFunction(
433               m_bp_options_vec, m_options.m_function_name.c_str());
434         } else {
435           script_interp->CollectDataForBreakpointCommandCallback(
436               m_bp_options_vec, result);
437         }
438       } else {
439         // Special handling for one-liner specified inline.
440         if (m_options.m_use_one_liner)
441           SetBreakpointCommandCallback(m_bp_options_vec,
442                                        m_options.m_one_liner.c_str());
443         else
444           CollectDataForBreakpointCommandCallback(m_bp_options_vec, result);
445       }
446     }
447 
448     return result.Succeeded();
449   }
450 
451 private:
452   CommandOptions m_options;
453   std::vector<BreakpointOptions *> m_bp_options_vec; // This stores the
454                                                      // breakpoint options that
455                                                      // we are currently
456   // collecting commands for.  In the CollectData... calls we need to hand this
457   // off to the IOHandler, which may run asynchronously. So we have to have
458   // some way to keep it alive, and not leak it. Making it an ivar of the
459   // command object, which never goes away achieves this.  Note that if we were
460   // able to run the same command concurrently in one interpreter we'd have to
461   // make this "per invocation".  But there are many more reasons why it is not
462   // in general safe to do that in lldb at present, so it isn't worthwhile to
463   // come up with a more complex mechanism to address this particular weakness
464   // right now.
465   static const char *g_reader_instructions;
466 };
467 
468 const char *CommandObjectBreakpointCommandAdd::g_reader_instructions =
469     "Enter your debugger command(s).  Type 'DONE' to end.\n";
470 
471 // CommandObjectBreakpointCommandDelete
472 
473 #define LLDB_OPTIONS_breakpoint_command_delete
474 #include "CommandOptions.inc"
475 
476 class CommandObjectBreakpointCommandDelete : public CommandObjectParsed {
477 public:
478   CommandObjectBreakpointCommandDelete(CommandInterpreter &interpreter)
479       : CommandObjectParsed(interpreter, "delete",
480                             "Delete the set of commands from a breakpoint.",
481                             nullptr),
482         m_options() {
483     CommandArgumentEntry arg;
484     CommandArgumentData bp_id_arg;
485 
486     // Define the first (and only) variant of this arg.
487     bp_id_arg.arg_type = eArgTypeBreakpointID;
488     bp_id_arg.arg_repetition = eArgRepeatPlain;
489 
490     // There is only one variant this argument could be; put it into the
491     // argument entry.
492     arg.push_back(bp_id_arg);
493 
494     // Push the data for the first argument into the m_arguments vector.
495     m_arguments.push_back(arg);
496   }
497 
498   ~CommandObjectBreakpointCommandDelete() override = default;
499 
500   Options *GetOptions() override { return &m_options; }
501 
502   class CommandOptions : public Options {
503   public:
504     CommandOptions() : Options(), m_use_dummy(false) {}
505 
506     ~CommandOptions() override = default;
507 
508     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
509                           ExecutionContext *execution_context) override {
510       Status error;
511       const int short_option = m_getopt_table[option_idx].val;
512 
513       switch (short_option) {
514       case 'D':
515         m_use_dummy = true;
516         break;
517 
518       default:
519         error.SetErrorStringWithFormat("unrecognized option '%c'",
520                                        short_option);
521         break;
522       }
523 
524       return error;
525     }
526 
527     void OptionParsingStarting(ExecutionContext *execution_context) override {
528       m_use_dummy = false;
529     }
530 
531     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
532       return llvm::makeArrayRef(g_breakpoint_command_delete_options);
533     }
534 
535     // Instance variables to hold the values for command options.
536     bool m_use_dummy;
537   };
538 
539 protected:
540   bool DoExecute(Args &command, CommandReturnObject &result) override {
541     Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
542 
543     if (target == nullptr) {
544       result.AppendError("There is not a current executable; there are no "
545                          "breakpoints from which to delete commands");
546       result.SetStatus(eReturnStatusFailed);
547       return false;
548     }
549 
550     const BreakpointList &breakpoints = target->GetBreakpointList();
551     size_t num_breakpoints = breakpoints.GetSize();
552 
553     if (num_breakpoints == 0) {
554       result.AppendError("No breakpoints exist to have commands deleted");
555       result.SetStatus(eReturnStatusFailed);
556       return false;
557     }
558 
559     if (command.empty()) {
560       result.AppendError(
561           "No breakpoint specified from which to delete the commands");
562       result.SetStatus(eReturnStatusFailed);
563       return false;
564     }
565 
566     BreakpointIDList valid_bp_ids;
567     CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
568         command, target, result, &valid_bp_ids,
569         BreakpointName::Permissions::PermissionKinds::listPerm);
570 
571     if (result.Succeeded()) {
572       const size_t count = valid_bp_ids.GetSize();
573       for (size_t i = 0; i < count; ++i) {
574         BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
575         if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
576           Breakpoint *bp =
577               target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
578           if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
579             BreakpointLocationSP bp_loc_sp(
580                 bp->FindLocationByID(cur_bp_id.GetLocationID()));
581             if (bp_loc_sp)
582               bp_loc_sp->ClearCallback();
583             else {
584               result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
585                                            cur_bp_id.GetBreakpointID(),
586                                            cur_bp_id.GetLocationID());
587               result.SetStatus(eReturnStatusFailed);
588               return false;
589             }
590           } else {
591             bp->ClearCallback();
592           }
593         }
594       }
595     }
596     return result.Succeeded();
597   }
598 
599 private:
600   CommandOptions m_options;
601 };
602 
603 // CommandObjectBreakpointCommandList
604 
605 class CommandObjectBreakpointCommandList : public CommandObjectParsed {
606 public:
607   CommandObjectBreakpointCommandList(CommandInterpreter &interpreter)
608       : CommandObjectParsed(interpreter, "list", "List the script or set of "
609                                                  "commands to be executed when "
610                                                  "the breakpoint is hit.",
611                             nullptr) {
612     CommandArgumentEntry arg;
613     CommandArgumentData bp_id_arg;
614 
615     // Define the first (and only) variant of this arg.
616     bp_id_arg.arg_type = eArgTypeBreakpointID;
617     bp_id_arg.arg_repetition = eArgRepeatPlain;
618 
619     // There is only one variant this argument could be; put it into the
620     // argument entry.
621     arg.push_back(bp_id_arg);
622 
623     // Push the data for the first argument into the m_arguments vector.
624     m_arguments.push_back(arg);
625   }
626 
627   ~CommandObjectBreakpointCommandList() override = default;
628 
629 protected:
630   bool DoExecute(Args &command, CommandReturnObject &result) override {
631     Target *target = GetDebugger().GetSelectedTarget().get();
632 
633     if (target == nullptr) {
634       result.AppendError("There is not a current executable; there are no "
635                          "breakpoints for which to list commands");
636       result.SetStatus(eReturnStatusFailed);
637       return false;
638     }
639 
640     const BreakpointList &breakpoints = target->GetBreakpointList();
641     size_t num_breakpoints = breakpoints.GetSize();
642 
643     if (num_breakpoints == 0) {
644       result.AppendError("No breakpoints exist for which to list commands");
645       result.SetStatus(eReturnStatusFailed);
646       return false;
647     }
648 
649     if (command.empty()) {
650       result.AppendError(
651           "No breakpoint specified for which to list the commands");
652       result.SetStatus(eReturnStatusFailed);
653       return false;
654     }
655 
656     BreakpointIDList valid_bp_ids;
657     CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
658         command, target, result, &valid_bp_ids,
659         BreakpointName::Permissions::PermissionKinds::listPerm);
660 
661     if (result.Succeeded()) {
662       const size_t count = valid_bp_ids.GetSize();
663       for (size_t i = 0; i < count; ++i) {
664         BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
665         if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
666           Breakpoint *bp =
667               target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
668 
669           if (bp) {
670             BreakpointLocationSP bp_loc_sp;
671             if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
672                   bp_loc_sp = bp->FindLocationByID(cur_bp_id.GetLocationID());
673               if (!bp_loc_sp)
674               {
675                 result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
676                                              cur_bp_id.GetBreakpointID(),
677                                              cur_bp_id.GetLocationID());
678                 result.SetStatus(eReturnStatusFailed);
679                 return false;
680               }
681             }
682 
683             StreamString id_str;
684             BreakpointID::GetCanonicalReference(&id_str,
685                                                 cur_bp_id.GetBreakpointID(),
686                                                 cur_bp_id.GetLocationID());
687             const Baton *baton = nullptr;
688             if (bp_loc_sp)
689               baton = bp_loc_sp
690                ->GetOptionsSpecifyingKind(BreakpointOptions::eCallback)
691                ->GetBaton();
692             else
693               baton = bp->GetOptions()->GetBaton();
694 
695             if (baton) {
696               result.GetOutputStream().Printf("Breakpoint %s:\n",
697                                               id_str.GetData());
698               result.GetOutputStream().IndentMore();
699               baton->GetDescription(&result.GetOutputStream(),
700                                     eDescriptionLevelFull);
701               result.GetOutputStream().IndentLess();
702             } else {
703               result.AppendMessageWithFormat(
704                   "Breakpoint %s does not have an associated command.\n",
705                   id_str.GetData());
706             }
707           }
708           result.SetStatus(eReturnStatusSuccessFinishResult);
709         } else {
710           result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n",
711                                        cur_bp_id.GetBreakpointID());
712           result.SetStatus(eReturnStatusFailed);
713         }
714       }
715     }
716 
717     return result.Succeeded();
718   }
719 };
720 
721 // CommandObjectBreakpointCommand
722 
723 CommandObjectBreakpointCommand::CommandObjectBreakpointCommand(
724     CommandInterpreter &interpreter)
725     : CommandObjectMultiword(
726           interpreter, "command", "Commands for adding, removing and listing "
727                                   "LLDB commands executed when a breakpoint is "
728                                   "hit.",
729           "command <sub-command> [<sub-command-options>] <breakpoint-id>") {
730   CommandObjectSP add_command_object(
731       new CommandObjectBreakpointCommandAdd(interpreter));
732   CommandObjectSP delete_command_object(
733       new CommandObjectBreakpointCommandDelete(interpreter));
734   CommandObjectSP list_command_object(
735       new CommandObjectBreakpointCommandList(interpreter));
736 
737   add_command_object->SetCommandName("breakpoint command add");
738   delete_command_object->SetCommandName("breakpoint command delete");
739   list_command_object->SetCommandName("breakpoint command list");
740 
741   LoadSubCommand("add", add_command_object);
742   LoadSubCommand("delete", delete_command_object);
743   LoadSubCommand("list", list_command_object);
744 }
745 
746 CommandObjectBreakpointCommand::~CommandObjectBreakpointCommand() = default;
747