1 //===-- CommandObjectBreakpointCommand.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 // Other libraries and framework includes
13 // Project includes
14 #include "CommandObjectBreakpointCommand.h"
15 #include "CommandObjectBreakpoint.h"
16 #include "lldb/Core/IOHandler.h"
17 #include "lldb/Interpreter/CommandInterpreter.h"
18 #include "lldb/Interpreter/CommandReturnObject.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Target/Thread.h"
21 #include "lldb/Breakpoint/BreakpointIDList.h"
22 #include "lldb/Breakpoint/Breakpoint.h"
23 #include "lldb/Breakpoint/BreakpointLocation.h"
24 #include "lldb/Breakpoint/StoppointCallbackContext.h"
25 #include "lldb/Core/State.h"
26 
27 using namespace lldb;
28 using namespace lldb_private;
29 
30 //-------------------------------------------------------------------------
31 // CommandObjectBreakpointCommandAdd
32 //-------------------------------------------------------------------------
33 
34 class CommandObjectBreakpointCommandAdd :
35     public CommandObjectParsed,
36     public IOHandlerDelegateMultiline
37 {
38 public:
39     CommandObjectBreakpointCommandAdd (CommandInterpreter &interpreter) :
40         CommandObjectParsed(interpreter,
41                             "add",
42                             "Add a set of commands to a breakpoint, to be executed whenever the breakpoint is hit."
43                             "  If no breakpoint is specified, adds the commands to the last created breakpoint.",
44                             nullptr),
45         IOHandlerDelegateMultiline ("DONE", IOHandlerDelegate::Completion::LLDBCommand),
46         m_options (interpreter)
47     {
48         SetHelpLong (
49 R"(
50 General information about entering breakpoint commands
51 ------------------------------------------------------
52 
53 )" "This command will prompt for commands to be executed when the specified \
54 breakpoint is hit.  Each command is typed on its own line following the '> ' \
55 prompt until 'DONE' is entered." R"(
56 
57 )" "Syntactic errors may not be detected when initially entered, and many \
58 malformed commands can silently fail when executed.  If your breakpoint commands \
59 do not appear to be executing, double-check the command syntax." R"(
60 
61 )" "Note: You may enter any debugger command exactly as you would at the debugger \
62 prompt.  There is no limit to the number of commands supplied, but do NOT enter \
63 more than one command per line." R"(
64 
65 Special information about PYTHON breakpoint commands
66 ----------------------------------------------------
67 
68 )" "You may enter either one or more lines of Python, including function \
69 definitions or calls to functions that will have been imported by the time \
70 the code executes.  Single line breakpoint commands will be interpreted 'as is' \
71 when the breakpoint is hit.  Multiple lines of Python will be wrapped in a \
72 generated function, and a call to the function will be attached to the breakpoint." R"(
73 
74 This auto-generated function is passed in three arguments:
75 
76     frame:  an lldb.SBFrame object for the frame which hit breakpoint.
77 
78     bp_loc: an lldb.SBBreakpointLocation object that represents the breakpoint location that was hit.
79 
80     dict:   the python session dictionary hit.
81 
82 )" "When specifying a python function with the --python-function option, you need \
83 to supply the function name prepended by the module name:" R"(
84 
85     --python-function myutils.breakpoint_callback
86 
87 The function itself must have the following prototype:
88 
89 def breakpoint_callback(frame, bp_loc, dict):
90   # Your code goes here
91 
92 )" "The arguments are the same as the arguments passed to generated functions as \
93 described above.  Note that the global variable 'lldb.frame' will NOT be updated when \
94 this function is called, so be sure to use the 'frame' argument. The 'frame' argument \
95 can get you to the thread via frame.GetThread(), the thread can get you to the \
96 process via thread.GetProcess(), and the process can get you back to the target \
97 via process.GetTarget()." R"(
98 
99 )" "Important Note: As Python code gets collected into functions, access to global \
100 variables requires explicit scoping using the 'global' keyword.  Be sure to use correct \
101 Python syntax, including indentation, when entering Python breakpoint commands." R"(
102 
103 Example Python one-line breakpoint command:
104 
105 (lldb) breakpoint command add -s python 1
106 Enter your Python command(s). Type 'DONE' to end.
107 > print "Hit this breakpoint!"
108 > DONE
109 
110 As a convenience, this also works for a short Python one-liner:
111 
112 (lldb) breakpoint command add -s python 1 -o 'import time; print time.asctime()'
113 (lldb) run
114 Launching '.../a.out'  (x86_64)
115 (lldb) Fri Sep 10 12:17:45 2010
116 Process 21778 Stopped
117 * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = breakpoint 1.1, queue = com.apple.main-thread
118   36
119   37   	int c(int val)
120   38   	{
121   39 ->	    return val + 3;
122   40   	}
123   41
124   42   	int main (int argc, char const *argv[])
125 
126 Example multiple line Python breakpoint command:
127 
128 (lldb) breakpoint command add -s p 1
129 Enter your Python command(s). Type 'DONE' to end.
130 > global bp_count
131 > bp_count = bp_count + 1
132 > print "Hit this breakpoint " + repr(bp_count) + " times!"
133 > DONE
134 
135 Example multiple line Python breakpoint command, using function definition:
136 
137 (lldb) breakpoint command add -s python 1
138 Enter your Python command(s). Type 'DONE' to end.
139 > def breakpoint_output (bp_no):
140 >     out_string = "Hit breakpoint number " + repr (bp_no)
141 >     print out_string
142 >     return True
143 > breakpoint_output (1)
144 > DONE
145 
146 )" "In this case, since there is a reference to a global variable, \
147 'bp_count', you will also need to make sure 'bp_count' exists and is \
148 initialized:" R"(
149 
150 (lldb) script
151 >>> bp_count = 0
152 >>> quit()
153 
154 )" "Your Python code, however organized, can optionally return a value.  \
155 If the returned value is False, that tells LLDB not to stop at the breakpoint \
156 to which the code is associated. Returning anything other than False, or even \
157 returning None, or even omitting a return statement entirely, will cause \
158 LLDB to stop." R"(
159 
160 )" "Final Note: A warning that no breakpoint command was generated when there \
161 are no syntax errors may indicate that a function was declared but never called."
162         );
163 
164         CommandArgumentEntry arg;
165         CommandArgumentData bp_id_arg;
166 
167         // Define the first (and only) variant of this arg.
168         bp_id_arg.arg_type = eArgTypeBreakpointID;
169         bp_id_arg.arg_repetition = eArgRepeatOptional;
170 
171         // There is only one variant this argument could be; put it into the argument entry.
172         arg.push_back (bp_id_arg);
173 
174         // Push the data for the first argument into the m_arguments vector.
175         m_arguments.push_back (arg);
176     }
177 
178     ~CommandObjectBreakpointCommandAdd() override = default;
179 
180     Options *
181     GetOptions () override
182     {
183         return &m_options;
184     }
185 
186     void
187     IOHandlerActivated (IOHandler &io_handler) override
188     {
189         StreamFileSP output_sp(io_handler.GetOutputStreamFile());
190         if (output_sp)
191         {
192             output_sp->PutCString(g_reader_instructions);
193             output_sp->Flush();
194         }
195     }
196 
197     void
198     IOHandlerInputComplete (IOHandler &io_handler, std::string &line) override
199     {
200         io_handler.SetIsDone(true);
201 
202         std::vector<BreakpointOptions *> *bp_options_vec = (std::vector<BreakpointOptions *> *)io_handler.GetUserData();
203         for (BreakpointOptions *bp_options : *bp_options_vec)
204         {
205             if (!bp_options)
206                 continue;
207 
208             std::unique_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
209             if (data_ap)
210             {
211                 data_ap->user_source.SplitIntoLines (line.c_str(), line.size());
212                 BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
213                 bp_options->SetCallback (BreakpointOptionsCallbackFunction, baton_sp);
214             }
215         }
216     }
217 
218     void
219     CollectDataForBreakpointCommandCallback (std::vector<BreakpointOptions *> &bp_options_vec,
220                                              CommandReturnObject &result)
221     {
222         m_interpreter.GetLLDBCommandsFromIOHandler ("> ",           // Prompt
223                                                     *this,          // IOHandlerDelegate
224                                                     true,           // Run IOHandler in async mode
225                                                     &bp_options_vec);    // Baton for the "io_handler" that will be passed back into our IOHandlerDelegate functions
226     }
227 
228     /// Set a one-liner as the callback for the breakpoint.
229     void
230     SetBreakpointCommandCallback (std::vector<BreakpointOptions *> &bp_options_vec,
231                                   const char *oneliner)
232     {
233         for (auto bp_options : bp_options_vec)
234         {
235             std::unique_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
236 
237             // It's necessary to set both user_source and script_source to the oneliner.
238             // The former is used to generate callback description (as in breakpoint command list)
239             // while the latter is used for Python to interpret during the actual callback.
240             data_ap->user_source.AppendString (oneliner);
241             data_ap->script_source.assign (oneliner);
242             data_ap->stop_on_error = m_options.m_stop_on_error;
243 
244             BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
245             bp_options->SetCallback (BreakpointOptionsCallbackFunction, baton_sp);
246         }
247     }
248 
249     static bool
250     BreakpointOptionsCallbackFunction (void *baton,
251                                        StoppointCallbackContext *context,
252                                        lldb::user_id_t break_id,
253                                        lldb::user_id_t break_loc_id)
254     {
255         bool ret_value = true;
256         if (baton == nullptr)
257             return true;
258 
259         BreakpointOptions::CommandData *data = (BreakpointOptions::CommandData *) baton;
260         StringList &commands = data->user_source;
261 
262         if (commands.GetSize() > 0)
263         {
264             ExecutionContext exe_ctx (context->exe_ctx_ref);
265             Target *target = exe_ctx.GetTargetPtr();
266             if (target)
267             {
268                 CommandReturnObject result;
269                 Debugger &debugger = target->GetDebugger();
270                 // Rig up the results secondary output stream to the debugger's, so the output will come out synchronously
271                 // if the debugger is set up that way.
272 
273                 StreamSP output_stream (debugger.GetAsyncOutputStream());
274                 StreamSP error_stream (debugger.GetAsyncErrorStream());
275                 result.SetImmediateOutputStream (output_stream);
276                 result.SetImmediateErrorStream (error_stream);
277 
278                 CommandInterpreterRunOptions options;
279                 options.SetStopOnContinue(true);
280                 options.SetStopOnError (data->stop_on_error);
281                 options.SetEchoCommands (true);
282                 options.SetPrintResults (true);
283                 options.SetAddToHistory (false);
284 
285                 debugger.GetCommandInterpreter().HandleCommands (commands,
286                                                                  &exe_ctx,
287                                                                  options,
288                                                                  result);
289                 result.GetImmediateOutputStream()->Flush();
290                 result.GetImmediateErrorStream()->Flush();
291            }
292         }
293         return ret_value;
294     }
295 
296     class CommandOptions : public Options
297     {
298     public:
299         CommandOptions (CommandInterpreter &interpreter) :
300             Options (interpreter),
301             m_use_commands (false),
302             m_use_script_language (false),
303             m_script_language (eScriptLanguageNone),
304             m_use_one_liner (false),
305             m_one_liner(),
306             m_function_name()
307         {
308         }
309 
310         ~CommandOptions() override = default;
311 
312         Error
313         SetOptionValue (uint32_t option_idx, const char *option_arg) override
314         {
315             Error error;
316             const int short_option = m_getopt_table[option_idx].val;
317 
318             switch (short_option)
319             {
320             case 'o':
321                 m_use_one_liner = true;
322                 m_one_liner = option_arg;
323                 break;
324 
325             case 's':
326                 m_script_language = (lldb::ScriptLanguage) Args::StringToOptionEnum (option_arg,
327                                                                                      g_option_table[option_idx].enum_values,
328                                                                                      eScriptLanguageNone,
329                                                                                      error);
330 
331                 if (m_script_language == eScriptLanguagePython || m_script_language == eScriptLanguageDefault)
332                 {
333                     m_use_script_language = true;
334                 }
335                 else
336                 {
337                     m_use_script_language = false;
338                 }
339                 break;
340 
341             case 'e':
342                 {
343                     bool success = false;
344                     m_stop_on_error = Args::StringToBoolean(option_arg, false, &success);
345                     if (!success)
346                         error.SetErrorStringWithFormat("invalid value for stop-on-error: \"%s\"", option_arg);
347                 }
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             case 'D':
357                 m_use_dummy = true;
358                 break;
359 
360             default:
361                 break;
362             }
363             return error;
364         }
365 
366         void
367         OptionParsingStarting () override
368         {
369             m_use_commands = true;
370             m_use_script_language = false;
371             m_script_language = eScriptLanguageNone;
372 
373             m_use_one_liner = false;
374             m_stop_on_error = true;
375             m_one_liner.clear();
376             m_function_name.clear();
377             m_use_dummy = false;
378         }
379 
380         const OptionDefinition*
381         GetDefinitions () override
382         {
383             return g_option_table;
384         }
385 
386         // Options table: Required for subclasses of Options.
387 
388         static OptionDefinition g_option_table[];
389 
390         // Instance variables to hold the values for command options.
391 
392         bool m_use_commands;
393         bool m_use_script_language;
394         lldb::ScriptLanguage m_script_language;
395 
396         // Instance variables to hold the values for one_liner options.
397         bool m_use_one_liner;
398         std::string m_one_liner;
399         bool m_stop_on_error;
400         std::string m_function_name;
401         bool m_use_dummy;
402     };
403 
404 protected:
405     bool
406     DoExecute (Args& command, CommandReturnObject &result) override
407     {
408         Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
409 
410         if (target == nullptr)
411         {
412             result.AppendError ("There is not a current executable; there are no breakpoints to which to add commands");
413             result.SetStatus (eReturnStatusFailed);
414             return false;
415         }
416 
417         const BreakpointList &breakpoints = target->GetBreakpointList();
418         size_t num_breakpoints = breakpoints.GetSize();
419 
420         if (num_breakpoints == 0)
421         {
422             result.AppendError ("No breakpoints exist to have commands added");
423             result.SetStatus (eReturnStatusFailed);
424             return false;
425         }
426 
427         if (!m_options.m_use_script_language && !m_options.m_function_name.empty())
428         {
429             result.AppendError ("need to enable scripting to have a function run as a breakpoint command");
430             result.SetStatus (eReturnStatusFailed);
431             return false;
432         }
433 
434         BreakpointIDList valid_bp_ids;
435         CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs (command, target, result, &valid_bp_ids);
436 
437         m_bp_options_vec.clear();
438 
439         if (result.Succeeded())
440         {
441             const size_t count = valid_bp_ids.GetSize();
442 
443             for (size_t i = 0; i < count; ++i)
444             {
445                 BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
446                 if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
447                 {
448                     Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
449                     BreakpointOptions *bp_options = nullptr;
450                     if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID)
451                     {
452                         // This breakpoint does not have an associated location.
453                         bp_options = bp->GetOptions();
454                     }
455                     else
456                     {
457                         BreakpointLocationSP bp_loc_sp(bp->FindLocationByID (cur_bp_id.GetLocationID()));
458                         // This breakpoint does have an associated location.
459                         // Get its breakpoint options.
460                         if (bp_loc_sp)
461                             bp_options = bp_loc_sp->GetLocationOptions();
462                     }
463                     if (bp_options)
464                         m_bp_options_vec.push_back (bp_options);
465                 }
466             }
467 
468             // If we are using script language, get the script interpreter
469             // in order to set or collect command callback.  Otherwise, call
470             // the methods associated with this object.
471             if (m_options.m_use_script_language)
472             {
473                 ScriptInterpreter *script_interp = m_interpreter.GetScriptInterpreter();
474                 // Special handling for one-liner specified inline.
475                 if (m_options.m_use_one_liner)
476                 {
477                     script_interp->SetBreakpointCommandCallback (m_bp_options_vec,
478                                                                  m_options.m_one_liner.c_str());
479                 }
480                 else if (!m_options.m_function_name.empty())
481                 {
482                     script_interp->SetBreakpointCommandCallbackFunction (m_bp_options_vec,
483                                                                          m_options.m_function_name.c_str());
484                 }
485                 else
486                 {
487                     script_interp->CollectDataForBreakpointCommandCallback (m_bp_options_vec,
488                                                                             result);
489                 }
490             }
491             else
492             {
493                 // Special handling for one-liner specified inline.
494                 if (m_options.m_use_one_liner)
495                     SetBreakpointCommandCallback (m_bp_options_vec,
496                                                   m_options.m_one_liner.c_str());
497                 else
498                     CollectDataForBreakpointCommandCallback (m_bp_options_vec,
499                                                              result);
500             }
501         }
502 
503         return result.Succeeded();
504     }
505 
506 private:
507     CommandOptions m_options;
508     std::vector<BreakpointOptions *> m_bp_options_vec;  // This stores the breakpoint options that we are currently
509                                                         // collecting commands for.  In the CollectData... calls we need
510                                                         // to hand this off to the IOHandler, which may run asynchronously.
511                                                         // So we have to have some way to keep it alive, and not leak it.
512                                                         // Making it an ivar of the command object, which never goes away
513                                                         // achieves this.  Note that if we were able to run
514                                                         // the same command concurrently in one interpreter we'd have to
515                                                         // make this "per invocation".  But there are many more reasons
516                                                         // why it is not in general safe to do that in lldb at present,
517                                                         // so it isn't worthwhile to come up with a more complex mechanism
518                                                         // to address this particular weakness right now.
519     static const char *g_reader_instructions;
520 };
521 
522 const char *
523 CommandObjectBreakpointCommandAdd::g_reader_instructions = "Enter your debugger command(s).  Type 'DONE' to end.\n";
524 
525 // FIXME: "script-type" needs to have its contents determined dynamically, so somebody can add a new scripting
526 // language to lldb and have it pickable here without having to change this enumeration by hand and rebuild lldb proper.
527 
528 static OptionEnumValueElement
529 g_script_option_enumeration[4] =
530 {
531     { eScriptLanguageNone,    "command",         "Commands are in the lldb command interpreter language"},
532     { eScriptLanguagePython,  "python",          "Commands are in the Python language."},
533     { eSortOrderByName,       "default-script",  "Commands are in the default scripting language."},
534     { 0,                      nullptr,           nullptr }
535 };
536 
537 OptionDefinition
538 CommandObjectBreakpointCommandAdd::CommandOptions::g_option_table[] =
539 {
540     { LLDB_OPT_SET_1, false, "one-liner", 'o', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeOneLiner,
541         "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
542 
543     { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean,
544         "Specify whether breakpoint command execution should terminate on error." },
545 
546     { LLDB_OPT_SET_ALL,   false, "script-type",     's', OptionParser::eRequiredArgument, nullptr, g_script_option_enumeration, 0, eArgTypeNone,
547         "Specify the language for the commands - if none is specified, the lldb command interpreter will be used."},
548 
549     { LLDB_OPT_SET_2,   false, "python-function",     'F', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePythonFunction,
550         "Give the name of a Python function to run as command for this breakpoint. Be sure to give a module name if appropriate."},
551 
552     { LLDB_OPT_SET_ALL, false, "dummy-breakpoints", 'D', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone,
553         "Sets Dummy breakpoints - i.e. breakpoints set before a file is provided, which prime new targets."},
554 
555     { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
556 };
557 
558 //-------------------------------------------------------------------------
559 // CommandObjectBreakpointCommandDelete
560 //-------------------------------------------------------------------------
561 
562 class CommandObjectBreakpointCommandDelete : public CommandObjectParsed
563 {
564 public:
565     CommandObjectBreakpointCommandDelete (CommandInterpreter &interpreter) :
566         CommandObjectParsed(interpreter,
567                             "delete",
568                             "Delete the set of commands from a breakpoint.",
569                             nullptr),
570         m_options (interpreter)
571     {
572         CommandArgumentEntry arg;
573         CommandArgumentData bp_id_arg;
574 
575         // Define the first (and only) variant of this arg.
576         bp_id_arg.arg_type = eArgTypeBreakpointID;
577         bp_id_arg.arg_repetition = eArgRepeatPlain;
578 
579         // There is only one variant this argument could be; put it into the argument entry.
580         arg.push_back (bp_id_arg);
581 
582         // Push the data for the first argument into the m_arguments vector.
583         m_arguments.push_back (arg);
584     }
585 
586     ~CommandObjectBreakpointCommandDelete() override = default;
587 
588     Options *
589     GetOptions () override
590     {
591         return &m_options;
592     }
593 
594     class CommandOptions : public Options
595     {
596     public:
597         CommandOptions (CommandInterpreter &interpreter) :
598             Options (interpreter),
599             m_use_dummy (false)
600         {
601         }
602 
603         ~CommandOptions() override = default;
604 
605         Error
606         SetOptionValue (uint32_t option_idx, const char *option_arg) override
607         {
608             Error error;
609             const int short_option = m_getopt_table[option_idx].val;
610 
611             switch (short_option)
612             {
613                 case 'D':
614                     m_use_dummy = true;
615                     break;
616 
617                 default:
618                     error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
619                     break;
620             }
621 
622             return error;
623         }
624 
625         void
626         OptionParsingStarting () override
627         {
628             m_use_dummy = false;
629         }
630 
631         const OptionDefinition*
632         GetDefinitions () override
633         {
634             return g_option_table;
635         }
636 
637         // Options table: Required for subclasses of Options.
638 
639         static OptionDefinition g_option_table[];
640 
641         // Instance variables to hold the values for command options.
642         bool m_use_dummy;
643     };
644 
645 protected:
646     bool
647     DoExecute (Args& command, CommandReturnObject &result) override
648     {
649         Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
650 
651         if (target == nullptr)
652         {
653             result.AppendError ("There is not a current executable; there are no breakpoints from which to delete commands");
654             result.SetStatus (eReturnStatusFailed);
655             return false;
656         }
657 
658         const BreakpointList &breakpoints = target->GetBreakpointList();
659         size_t num_breakpoints = breakpoints.GetSize();
660 
661         if (num_breakpoints == 0)
662         {
663             result.AppendError ("No breakpoints exist to have commands deleted");
664             result.SetStatus (eReturnStatusFailed);
665             return false;
666         }
667 
668         if (command.GetArgumentCount() == 0)
669         {
670             result.AppendError ("No breakpoint specified from which to delete the commands");
671             result.SetStatus (eReturnStatusFailed);
672             return false;
673         }
674 
675         BreakpointIDList valid_bp_ids;
676         CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs (command, target, result, &valid_bp_ids);
677 
678         if (result.Succeeded())
679         {
680             const size_t count = valid_bp_ids.GetSize();
681             for (size_t i = 0; i < count; ++i)
682             {
683                 BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
684                 if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
685                 {
686                     Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
687                     if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
688                     {
689                         BreakpointLocationSP bp_loc_sp (bp->FindLocationByID (cur_bp_id.GetLocationID()));
690                         if (bp_loc_sp)
691                             bp_loc_sp->ClearCallback();
692                         else
693                         {
694                             result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
695                                                          cur_bp_id.GetBreakpointID(),
696                                                          cur_bp_id.GetLocationID());
697                             result.SetStatus (eReturnStatusFailed);
698                             return false;
699                         }
700                     }
701                     else
702                     {
703                         bp->ClearCallback();
704                     }
705                 }
706             }
707         }
708         return result.Succeeded();
709     }
710 
711 private:
712     CommandOptions m_options;
713 };
714 
715 OptionDefinition
716 CommandObjectBreakpointCommandDelete::CommandOptions::g_option_table[] =
717 {
718     { LLDB_OPT_SET_1, false, "dummy-breakpoints", 'D', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone,
719         "Delete commands from Dummy breakpoints - i.e. breakpoints set before a file is provided, which prime new targets."},
720 
721     { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
722 };
723 
724 //-------------------------------------------------------------------------
725 // CommandObjectBreakpointCommandList
726 //-------------------------------------------------------------------------
727 
728 class CommandObjectBreakpointCommandList : public CommandObjectParsed
729 {
730 public:
731     CommandObjectBreakpointCommandList (CommandInterpreter &interpreter) :
732         CommandObjectParsed(interpreter,
733                             "list",
734                             "List the script or set of commands to be executed when the breakpoint is hit.",
735                             nullptr)
736     {
737         CommandArgumentEntry arg;
738         CommandArgumentData bp_id_arg;
739 
740         // Define the first (and only) variant of this arg.
741         bp_id_arg.arg_type = eArgTypeBreakpointID;
742         bp_id_arg.arg_repetition = eArgRepeatPlain;
743 
744         // There is only one variant this argument could be; put it into the argument entry.
745         arg.push_back (bp_id_arg);
746 
747         // Push the data for the first argument into the m_arguments vector.
748         m_arguments.push_back (arg);
749     }
750 
751     ~CommandObjectBreakpointCommandList() override = default;
752 
753 protected:
754     bool
755     DoExecute (Args& command,
756              CommandReturnObject &result) override
757     {
758         Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
759 
760         if (target == nullptr)
761         {
762             result.AppendError ("There is not a current executable; there are no breakpoints for which to list commands");
763             result.SetStatus (eReturnStatusFailed);
764             return false;
765         }
766 
767         const BreakpointList &breakpoints = target->GetBreakpointList();
768         size_t num_breakpoints = breakpoints.GetSize();
769 
770         if (num_breakpoints == 0)
771         {
772             result.AppendError ("No breakpoints exist for which to list commands");
773             result.SetStatus (eReturnStatusFailed);
774             return false;
775         }
776 
777         if (command.GetArgumentCount() == 0)
778         {
779             result.AppendError ("No breakpoint specified for which to list the commands");
780             result.SetStatus (eReturnStatusFailed);
781             return false;
782         }
783 
784         BreakpointIDList valid_bp_ids;
785         CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs (command, target, result, &valid_bp_ids);
786 
787         if (result.Succeeded())
788         {
789             const size_t count = valid_bp_ids.GetSize();
790             for (size_t i = 0; i < count; ++i)
791             {
792                 BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
793                 if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
794                 {
795                     Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
796 
797                     if (bp)
798                     {
799                         const BreakpointOptions *bp_options = nullptr;
800                         if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
801                         {
802                             BreakpointLocationSP bp_loc_sp(bp->FindLocationByID (cur_bp_id.GetLocationID()));
803                             if (bp_loc_sp)
804                                 bp_options = bp_loc_sp->GetOptionsNoCreate();
805                             else
806                             {
807                                 result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
808                                                              cur_bp_id.GetBreakpointID(),
809                                                              cur_bp_id.GetLocationID());
810                                 result.SetStatus (eReturnStatusFailed);
811                                 return false;
812                             }
813                         }
814                         else
815                         {
816                             bp_options = bp->GetOptions();
817                         }
818 
819                         if (bp_options)
820                         {
821                             StreamString id_str;
822                             BreakpointID::GetCanonicalReference (&id_str,
823                                                                  cur_bp_id.GetBreakpointID(),
824                                                                  cur_bp_id.GetLocationID());
825                             const Baton *baton = bp_options->GetBaton();
826                             if (baton)
827                             {
828                                 result.GetOutputStream().Printf ("Breakpoint %s:\n", id_str.GetData());
829                                 result.GetOutputStream().IndentMore ();
830                                 baton->GetDescription(&result.GetOutputStream(), eDescriptionLevelFull);
831                                 result.GetOutputStream().IndentLess ();
832                             }
833                             else
834                             {
835                                 result.AppendMessageWithFormat ("Breakpoint %s does not have an associated command.\n",
836                                                                 id_str.GetData());
837                             }
838                         }
839                         result.SetStatus (eReturnStatusSuccessFinishResult);
840                     }
841                     else
842                     {
843                         result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n", cur_bp_id.GetBreakpointID());
844                         result.SetStatus (eReturnStatusFailed);
845                     }
846                 }
847             }
848         }
849 
850         return result.Succeeded();
851     }
852 };
853 
854 //-------------------------------------------------------------------------
855 // CommandObjectBreakpointCommand
856 //-------------------------------------------------------------------------
857 
858 CommandObjectBreakpointCommand::CommandObjectBreakpointCommand (CommandInterpreter &interpreter) :
859     CommandObjectMultiword (interpreter,
860                             "command",
861                             "A set of commands for adding, removing and examining bits of code to be executed when the breakpoint is hit (breakpoint 'commands').",
862                             "command <sub-command> [<sub-command-options>] <breakpoint-id>")
863 {
864     CommandObjectSP add_command_object (new CommandObjectBreakpointCommandAdd (interpreter));
865     CommandObjectSP delete_command_object (new CommandObjectBreakpointCommandDelete (interpreter));
866     CommandObjectSP list_command_object (new CommandObjectBreakpointCommandList (interpreter));
867 
868     add_command_object->SetCommandName ("breakpoint command add");
869     delete_command_object->SetCommandName ("breakpoint command delete");
870     list_command_object->SetCommandName ("breakpoint command list");
871 
872     LoadSubCommand ("add",    add_command_object);
873     LoadSubCommand ("delete", delete_command_object);
874     LoadSubCommand ("list",   list_command_object);
875 }
876 
877 CommandObjectBreakpointCommand::~CommandObjectBreakpointCommand() = default;
878