130fdc8d8SChris Lattner //===-- CommandObjectBreakpointCommand.cpp ----------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner // C Includes
1130fdc8d8SChris Lattner // C++ Includes
12c8ecc2a9SEugene Zelenko // Other libraries and framework includes
13c8ecc2a9SEugene Zelenko // Project includes
1430fdc8d8SChris Lattner #include "CommandObjectBreakpointCommand.h"
1530fdc8d8SChris Lattner #include "CommandObjectBreakpoint.h"
16*b9c1b51eSKate Stone #include "lldb/Breakpoint/Breakpoint.h"
17*b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointIDList.h"
18*b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointLocation.h"
19*b9c1b51eSKate Stone #include "lldb/Breakpoint/StoppointCallbackContext.h"
2044d93782SGreg Clayton #include "lldb/Core/IOHandler.h"
21*b9c1b51eSKate Stone #include "lldb/Core/State.h"
2230fdc8d8SChris Lattner #include "lldb/Interpreter/CommandInterpreter.h"
2330fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h"
2430fdc8d8SChris Lattner #include "lldb/Target/Target.h"
2530fdc8d8SChris Lattner #include "lldb/Target/Thread.h"
2630fdc8d8SChris Lattner 
2730fdc8d8SChris Lattner using namespace lldb;
2830fdc8d8SChris Lattner using namespace lldb_private;
2930fdc8d8SChris Lattner 
3030fdc8d8SChris Lattner //-------------------------------------------------------------------------
3130fdc8d8SChris Lattner // CommandObjectBreakpointCommandAdd
3230fdc8d8SChris Lattner //-------------------------------------------------------------------------
3330fdc8d8SChris Lattner 
34*b9c1b51eSKate Stone class CommandObjectBreakpointCommandAdd : public CommandObjectParsed,
35*b9c1b51eSKate Stone                                           public IOHandlerDelegateMultiline {
365a988416SJim Ingham public:
377428a18cSKate Stone   CommandObjectBreakpointCommandAdd(CommandInterpreter &interpreter)
387428a18cSKate Stone       : CommandObjectParsed(interpreter, "add",
39*b9c1b51eSKate Stone                             "Add LLDB commands to a breakpoint, to be executed "
40*b9c1b51eSKate Stone                             "whenever the breakpoint is hit."
41*b9c1b51eSKate Stone                             "  If no breakpoint is specified, adds the "
42*b9c1b51eSKate Stone                             "commands to the last created breakpoint.",
43c8ecc2a9SEugene Zelenko                             nullptr),
44*b9c1b51eSKate Stone         IOHandlerDelegateMultiline("DONE",
45*b9c1b51eSKate Stone                                    IOHandlerDelegate::Completion::LLDBCommand),
46*b9c1b51eSKate Stone         m_options() {
4730fdc8d8SChris Lattner     SetHelpLong(
48ea671fbdSKate Stone         R"(
49ea671fbdSKate Stone General information about entering breakpoint commands
50ea671fbdSKate Stone ------------------------------------------------------
51ea671fbdSKate Stone 
52*b9c1b51eSKate Stone )"
53*b9c1b51eSKate Stone         "This command will prompt for commands to be executed when the specified \
54ea671fbdSKate Stone breakpoint is hit.  Each command is typed on its own line following the '> ' \
55*b9c1b51eSKate Stone prompt until 'DONE' is entered."
56*b9c1b51eSKate Stone         R"(
57ea671fbdSKate Stone 
58*b9c1b51eSKate Stone )"
59*b9c1b51eSKate Stone         "Syntactic errors may not be detected when initially entered, and many \
60ea671fbdSKate Stone malformed commands can silently fail when executed.  If your breakpoint commands \
61*b9c1b51eSKate Stone do not appear to be executing, double-check the command syntax."
62*b9c1b51eSKate Stone         R"(
63ea671fbdSKate Stone 
64*b9c1b51eSKate Stone )"
65*b9c1b51eSKate Stone         "Note: You may enter any debugger command exactly as you would at the debugger \
66ea671fbdSKate Stone prompt.  There is no limit to the number of commands supplied, but do NOT enter \
67*b9c1b51eSKate Stone more than one command per line."
68*b9c1b51eSKate Stone         R"(
69ea671fbdSKate Stone 
70ea671fbdSKate Stone Special information about PYTHON breakpoint commands
71ea671fbdSKate Stone ----------------------------------------------------
72ea671fbdSKate Stone 
73*b9c1b51eSKate Stone )"
74*b9c1b51eSKate Stone         "You may enter either one or more lines of Python, including function \
75ea671fbdSKate Stone definitions or calls to functions that will have been imported by the time \
76ea671fbdSKate Stone the code executes.  Single line breakpoint commands will be interpreted 'as is' \
77ea671fbdSKate Stone when the breakpoint is hit.  Multiple lines of Python will be wrapped in a \
78*b9c1b51eSKate Stone generated function, and a call to the function will be attached to the breakpoint."
79*b9c1b51eSKate Stone         R"(
80ea671fbdSKate Stone 
81ea671fbdSKate Stone This auto-generated function is passed in three arguments:
82ea671fbdSKate Stone 
83ea671fbdSKate Stone     frame:  an lldb.SBFrame object for the frame which hit breakpoint.
84ea671fbdSKate Stone 
85ea671fbdSKate Stone     bp_loc: an lldb.SBBreakpointLocation object that represents the breakpoint location that was hit.
86ea671fbdSKate Stone 
87ea671fbdSKate Stone     dict:   the python session dictionary hit.
88ea671fbdSKate Stone 
89*b9c1b51eSKate Stone )"
90*b9c1b51eSKate Stone         "When specifying a python function with the --python-function option, you need \
91*b9c1b51eSKate Stone to supply the function name prepended by the module name:"
92*b9c1b51eSKate Stone         R"(
93ea671fbdSKate Stone 
94ea671fbdSKate Stone     --python-function myutils.breakpoint_callback
95ea671fbdSKate Stone 
96ea671fbdSKate Stone The function itself must have the following prototype:
97ea671fbdSKate Stone 
98ea671fbdSKate Stone def breakpoint_callback(frame, bp_loc, dict):
99ea671fbdSKate Stone   # Your code goes here
100ea671fbdSKate Stone 
101*b9c1b51eSKate Stone )"
102*b9c1b51eSKate Stone         "The arguments are the same as the arguments passed to generated functions as \
103ea671fbdSKate Stone described above.  Note that the global variable 'lldb.frame' will NOT be updated when \
104ea671fbdSKate Stone this function is called, so be sure to use the 'frame' argument. The 'frame' argument \
105ea671fbdSKate Stone can get you to the thread via frame.GetThread(), the thread can get you to the \
106ea671fbdSKate Stone process via thread.GetProcess(), and the process can get you back to the target \
107*b9c1b51eSKate Stone via process.GetTarget()."
108*b9c1b51eSKate Stone         R"(
109ea671fbdSKate Stone 
110*b9c1b51eSKate Stone )"
111*b9c1b51eSKate Stone         "Important Note: As Python code gets collected into functions, access to global \
112ea671fbdSKate Stone variables requires explicit scoping using the 'global' keyword.  Be sure to use correct \
113*b9c1b51eSKate Stone Python syntax, including indentation, when entering Python breakpoint commands."
114*b9c1b51eSKate Stone         R"(
115ea671fbdSKate Stone 
116ea671fbdSKate Stone Example Python one-line breakpoint command:
117ea671fbdSKate Stone 
118ea671fbdSKate Stone (lldb) breakpoint command add -s python 1
119ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end.
120ea671fbdSKate Stone > print "Hit this breakpoint!"
121ea671fbdSKate Stone > DONE
122ea671fbdSKate Stone 
123ea671fbdSKate Stone As a convenience, this also works for a short Python one-liner:
124ea671fbdSKate Stone 
125ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 -o 'import time; print time.asctime()'
126ea671fbdSKate Stone (lldb) run
127ea671fbdSKate Stone Launching '.../a.out'  (x86_64)
128ea671fbdSKate Stone (lldb) Fri Sep 10 12:17:45 2010
129ea671fbdSKate Stone Process 21778 Stopped
130ea671fbdSKate Stone * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = breakpoint 1.1, queue = com.apple.main-thread
131ea671fbdSKate Stone   36
132ea671fbdSKate Stone   37   	int c(int val)
133ea671fbdSKate Stone   38   	{
134ea671fbdSKate Stone   39 ->	    return val + 3;
135ea671fbdSKate Stone   40   	}
136ea671fbdSKate Stone   41
137ea671fbdSKate Stone   42   	int main (int argc, char const *argv[])
138ea671fbdSKate Stone 
139ea671fbdSKate Stone Example multiple line Python breakpoint command:
140ea671fbdSKate Stone 
141ea671fbdSKate Stone (lldb) breakpoint command add -s p 1
142ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end.
143ea671fbdSKate Stone > global bp_count
144ea671fbdSKate Stone > bp_count = bp_count + 1
145ea671fbdSKate Stone > print "Hit this breakpoint " + repr(bp_count) + " times!"
146ea671fbdSKate Stone > DONE
147ea671fbdSKate Stone 
148ea671fbdSKate Stone Example multiple line Python breakpoint command, using function definition:
149ea671fbdSKate Stone 
150ea671fbdSKate Stone (lldb) breakpoint command add -s python 1
151ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end.
152ea671fbdSKate Stone > def breakpoint_output (bp_no):
153ea671fbdSKate Stone >     out_string = "Hit breakpoint number " + repr (bp_no)
154ea671fbdSKate Stone >     print out_string
155ea671fbdSKate Stone >     return True
156ea671fbdSKate Stone > breakpoint_output (1)
157ea671fbdSKate Stone > DONE
158ea671fbdSKate Stone 
159*b9c1b51eSKate Stone )"
160*b9c1b51eSKate Stone         "In this case, since there is a reference to a global variable, \
161ea671fbdSKate Stone 'bp_count', you will also need to make sure 'bp_count' exists and is \
162*b9c1b51eSKate Stone initialized:"
163*b9c1b51eSKate Stone         R"(
164ea671fbdSKate Stone 
165ea671fbdSKate Stone (lldb) script
166ea671fbdSKate Stone >>> bp_count = 0
167ea671fbdSKate Stone >>> quit()
168ea671fbdSKate Stone 
169*b9c1b51eSKate Stone )"
170*b9c1b51eSKate Stone         "Your Python code, however organized, can optionally return a value.  \
171ea671fbdSKate Stone If the returned value is False, that tells LLDB not to stop at the breakpoint \
172ea671fbdSKate Stone to which the code is associated. Returning anything other than False, or even \
173ea671fbdSKate Stone returning None, or even omitting a return statement entirely, will cause \
174*b9c1b51eSKate Stone LLDB to stop."
175*b9c1b51eSKate Stone         R"(
176ea671fbdSKate Stone 
177*b9c1b51eSKate Stone )"
178*b9c1b51eSKate Stone         "Final Note: A warning that no breakpoint command was generated when there \
179*b9c1b51eSKate Stone are no syntax errors may indicate that a function was declared but never called.");
180405fe67fSCaroline Tice 
181405fe67fSCaroline Tice     CommandArgumentEntry arg;
182405fe67fSCaroline Tice     CommandArgumentData bp_id_arg;
183405fe67fSCaroline Tice 
184405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
185405fe67fSCaroline Tice     bp_id_arg.arg_type = eArgTypeBreakpointID;
1861bb0750bSJim Ingham     bp_id_arg.arg_repetition = eArgRepeatOptional;
187405fe67fSCaroline Tice 
188*b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
189*b9c1b51eSKate Stone     // argument entry.
190405fe67fSCaroline Tice     arg.push_back(bp_id_arg);
191405fe67fSCaroline Tice 
192405fe67fSCaroline Tice     // Push the data for the first argument into the m_arguments vector.
193405fe67fSCaroline Tice     m_arguments.push_back(arg);
19430fdc8d8SChris Lattner   }
19530fdc8d8SChris Lattner 
196c8ecc2a9SEugene Zelenko   ~CommandObjectBreakpointCommandAdd() override = default;
1975a988416SJim Ingham 
198*b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
1995a988416SJim Ingham 
200*b9c1b51eSKate Stone   void IOHandlerActivated(IOHandler &io_handler) override {
20144d93782SGreg Clayton     StreamFileSP output_sp(io_handler.GetOutputStreamFile());
202*b9c1b51eSKate Stone     if (output_sp) {
20344d93782SGreg Clayton       output_sp->PutCString(g_reader_instructions);
20444d93782SGreg Clayton       output_sp->Flush();
20544d93782SGreg Clayton     }
20644d93782SGreg Clayton   }
20744d93782SGreg Clayton 
208*b9c1b51eSKate Stone   void IOHandlerInputComplete(IOHandler &io_handler,
209*b9c1b51eSKate Stone                               std::string &line) override {
21044d93782SGreg Clayton     io_handler.SetIsDone(true);
21144d93782SGreg Clayton 
212*b9c1b51eSKate Stone     std::vector<BreakpointOptions *> *bp_options_vec =
213*b9c1b51eSKate Stone         (std::vector<BreakpointOptions *> *)io_handler.GetUserData();
214*b9c1b51eSKate Stone     for (BreakpointOptions *bp_options : *bp_options_vec) {
215b5796cb4SJim Ingham       if (!bp_options)
216b5796cb4SJim Ingham         continue;
217b5796cb4SJim Ingham 
218*b9c1b51eSKate Stone       std::unique_ptr<BreakpointOptions::CommandData> data_ap(
219*b9c1b51eSKate Stone           new BreakpointOptions::CommandData());
220*b9c1b51eSKate Stone       if (data_ap) {
22144d93782SGreg Clayton         data_ap->user_source.SplitIntoLines(line.c_str(), line.size());
222*b9c1b51eSKate Stone         BatonSP baton_sp(
223*b9c1b51eSKate Stone             new BreakpointOptions::CommandBaton(data_ap.release()));
22444d93782SGreg Clayton         bp_options->SetCallback(BreakpointOptionsCallbackFunction, baton_sp);
22544d93782SGreg Clayton       }
22644d93782SGreg Clayton     }
22744d93782SGreg Clayton   }
22844d93782SGreg Clayton 
229*b9c1b51eSKate Stone   void CollectDataForBreakpointCommandCallback(
230*b9c1b51eSKate Stone       std::vector<BreakpointOptions *> &bp_options_vec,
231*b9c1b51eSKate Stone       CommandReturnObject &result) {
232*b9c1b51eSKate Stone     m_interpreter.GetLLDBCommandsFromIOHandler(
233*b9c1b51eSKate Stone         "> ",             // Prompt
23444d93782SGreg Clayton         *this,            // IOHandlerDelegate
23544d93782SGreg Clayton         true,             // Run IOHandler in async mode
236*b9c1b51eSKate Stone         &bp_options_vec); // Baton for the "io_handler" that will be passed back
237*b9c1b51eSKate Stone                           // into our IOHandlerDelegate functions
2385a988416SJim Ingham   }
2395a988416SJim Ingham 
2405a988416SJim Ingham   /// Set a one-liner as the callback for the breakpoint.
2415a988416SJim Ingham   void
242b5796cb4SJim Ingham   SetBreakpointCommandCallback(std::vector<BreakpointOptions *> &bp_options_vec,
243*b9c1b51eSKate Stone                                const char *oneliner) {
244*b9c1b51eSKate Stone     for (auto bp_options : bp_options_vec) {
245*b9c1b51eSKate Stone       std::unique_ptr<BreakpointOptions::CommandData> data_ap(
246*b9c1b51eSKate Stone           new BreakpointOptions::CommandData());
2475a988416SJim Ingham 
248*b9c1b51eSKate Stone       // It's necessary to set both user_source and script_source to the
249*b9c1b51eSKate Stone       // oneliner.
250*b9c1b51eSKate Stone       // The former is used to generate callback description (as in breakpoint
251*b9c1b51eSKate Stone       // command list)
252*b9c1b51eSKate Stone       // while the latter is used for Python to interpret during the actual
253*b9c1b51eSKate Stone       // callback.
2545a988416SJim Ingham       data_ap->user_source.AppendString(oneliner);
2555a988416SJim Ingham       data_ap->script_source.assign(oneliner);
2565a988416SJim Ingham       data_ap->stop_on_error = m_options.m_stop_on_error;
2575a988416SJim Ingham 
2585a988416SJim Ingham       BatonSP baton_sp(new BreakpointOptions::CommandBaton(data_ap.release()));
2595a988416SJim Ingham       bp_options->SetCallback(BreakpointOptionsCallbackFunction, baton_sp);
260b5796cb4SJim Ingham     }
2615a988416SJim Ingham   }
2625a988416SJim Ingham 
263*b9c1b51eSKate Stone   static bool BreakpointOptionsCallbackFunction(
264*b9c1b51eSKate Stone       void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id,
265*b9c1b51eSKate Stone       lldb::user_id_t break_loc_id) {
2665a988416SJim Ingham     bool ret_value = true;
267c8ecc2a9SEugene Zelenko     if (baton == nullptr)
2685a988416SJim Ingham       return true;
2695a988416SJim Ingham 
270*b9c1b51eSKate Stone     BreakpointOptions::CommandData *data =
271*b9c1b51eSKate Stone         (BreakpointOptions::CommandData *)baton;
2725a988416SJim Ingham     StringList &commands = data->user_source;
2735a988416SJim Ingham 
274*b9c1b51eSKate Stone     if (commands.GetSize() > 0) {
2755a988416SJim Ingham       ExecutionContext exe_ctx(context->exe_ctx_ref);
2765a988416SJim Ingham       Target *target = exe_ctx.GetTargetPtr();
277*b9c1b51eSKate Stone       if (target) {
2785a988416SJim Ingham         CommandReturnObject result;
2795a988416SJim Ingham         Debugger &debugger = target->GetDebugger();
280*b9c1b51eSKate Stone         // Rig up the results secondary output stream to the debugger's, so the
281*b9c1b51eSKate Stone         // output will come out synchronously
2825a988416SJim Ingham         // if the debugger is set up that way.
2835a988416SJim Ingham 
2845a988416SJim Ingham         StreamSP output_stream(debugger.GetAsyncOutputStream());
2855a988416SJim Ingham         StreamSP error_stream(debugger.GetAsyncErrorStream());
2865a988416SJim Ingham         result.SetImmediateOutputStream(output_stream);
2875a988416SJim Ingham         result.SetImmediateErrorStream(error_stream);
2885a988416SJim Ingham 
28926c7bf93SJim Ingham         CommandInterpreterRunOptions options;
29026c7bf93SJim Ingham         options.SetStopOnContinue(true);
29126c7bf93SJim Ingham         options.SetStopOnError(data->stop_on_error);
29226c7bf93SJim Ingham         options.SetEchoCommands(true);
29326c7bf93SJim Ingham         options.SetPrintResults(true);
29426c7bf93SJim Ingham         options.SetAddToHistory(false);
2955a988416SJim Ingham 
296*b9c1b51eSKate Stone         debugger.GetCommandInterpreter().HandleCommands(commands, &exe_ctx,
297*b9c1b51eSKate Stone                                                         options, result);
2985a988416SJim Ingham         result.GetImmediateOutputStream()->Flush();
2995a988416SJim Ingham         result.GetImmediateErrorStream()->Flush();
3005a988416SJim Ingham       }
3015a988416SJim Ingham     }
3025a988416SJim Ingham     return ret_value;
3035a988416SJim Ingham   }
3045a988416SJim Ingham 
305*b9c1b51eSKate Stone   class CommandOptions : public Options {
3065a988416SJim Ingham   public:
307*b9c1b51eSKate Stone     CommandOptions()
308*b9c1b51eSKate Stone         : Options(), m_use_commands(false), m_use_script_language(false),
309*b9c1b51eSKate Stone           m_script_language(eScriptLanguageNone), m_use_one_liner(false),
310*b9c1b51eSKate Stone           m_one_liner(), m_function_name() {}
31130fdc8d8SChris Lattner 
312c8ecc2a9SEugene Zelenko     ~CommandOptions() override = default;
3135a988416SJim Ingham 
314*b9c1b51eSKate Stone     Error SetOptionValue(uint32_t option_idx, const char *option_arg,
315*b9c1b51eSKate Stone                          ExecutionContext *execution_context) override {
3165a988416SJim Ingham       Error error;
3173bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
3185a988416SJim Ingham 
319*b9c1b51eSKate Stone       switch (short_option) {
3205a988416SJim Ingham       case 'o':
3215a988416SJim Ingham         m_use_one_liner = true;
3225a988416SJim Ingham         m_one_liner = option_arg;
3235a988416SJim Ingham         break;
3245a988416SJim Ingham 
3255a988416SJim Ingham       case 's':
326*b9c1b51eSKate Stone         m_script_language = (lldb::ScriptLanguage)Args::StringToOptionEnum(
327*b9c1b51eSKate Stone             option_arg, g_option_table[option_idx].enum_values,
328*b9c1b51eSKate Stone             eScriptLanguageNone, error);
3295a988416SJim Ingham 
330*b9c1b51eSKate Stone         if (m_script_language == eScriptLanguagePython ||
331*b9c1b51eSKate Stone             m_script_language == eScriptLanguageDefault) {
3325a988416SJim Ingham           m_use_script_language = true;
333*b9c1b51eSKate Stone         } else {
3345a988416SJim Ingham           m_use_script_language = false;
3355a988416SJim Ingham         }
3365a988416SJim Ingham         break;
3375a988416SJim Ingham 
338*b9c1b51eSKate Stone       case 'e': {
3395a988416SJim Ingham         bool success = false;
3405a988416SJim Ingham         m_stop_on_error = Args::StringToBoolean(option_arg, false, &success);
3415a988416SJim Ingham         if (!success)
342*b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
343*b9c1b51eSKate Stone               "invalid value for stop-on-error: \"%s\"", option_arg);
344*b9c1b51eSKate Stone       } break;
3455a988416SJim Ingham 
3465a988416SJim Ingham       case 'F':
3475a988416SJim Ingham         m_use_one_liner = false;
3485a988416SJim Ingham         m_use_script_language = true;
3495a988416SJim Ingham         m_function_name.assign(option_arg);
3505a988416SJim Ingham         break;
3515a988416SJim Ingham 
35233df7cd3SJim Ingham       case 'D':
35333df7cd3SJim Ingham         m_use_dummy = true;
35433df7cd3SJim Ingham         break;
35533df7cd3SJim Ingham 
3565a988416SJim Ingham       default:
3575a988416SJim Ingham         break;
3585a988416SJim Ingham       }
3595a988416SJim Ingham       return error;
3605a988416SJim Ingham     }
361c8ecc2a9SEugene Zelenko 
362*b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
3635a988416SJim Ingham       m_use_commands = true;
3645a988416SJim Ingham       m_use_script_language = false;
3655a988416SJim Ingham       m_script_language = eScriptLanguageNone;
3665a988416SJim Ingham 
3675a988416SJim Ingham       m_use_one_liner = false;
3685a988416SJim Ingham       m_stop_on_error = true;
3695a988416SJim Ingham       m_one_liner.clear();
3705a988416SJim Ingham       m_function_name.clear();
37133df7cd3SJim Ingham       m_use_dummy = false;
3725a988416SJim Ingham     }
3735a988416SJim Ingham 
374*b9c1b51eSKate Stone     const OptionDefinition *GetDefinitions() override { return g_option_table; }
3755a988416SJim Ingham 
3765a988416SJim Ingham     // Options table: Required for subclasses of Options.
3775a988416SJim Ingham 
3785a988416SJim Ingham     static OptionDefinition g_option_table[];
3795a988416SJim Ingham 
3805a988416SJim Ingham     // Instance variables to hold the values for command options.
3815a988416SJim Ingham 
3825a988416SJim Ingham     bool m_use_commands;
3835a988416SJim Ingham     bool m_use_script_language;
3845a988416SJim Ingham     lldb::ScriptLanguage m_script_language;
3855a988416SJim Ingham 
3865a988416SJim Ingham     // Instance variables to hold the values for one_liner options.
3875a988416SJim Ingham     bool m_use_one_liner;
3885a988416SJim Ingham     std::string m_one_liner;
3895a988416SJim Ingham     bool m_stop_on_error;
3905a988416SJim Ingham     std::string m_function_name;
39133df7cd3SJim Ingham     bool m_use_dummy;
3925a988416SJim Ingham   };
3935a988416SJim Ingham 
3945a988416SJim Ingham protected:
395*b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
39633df7cd3SJim Ingham     Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
39730fdc8d8SChris Lattner 
398*b9c1b51eSKate Stone     if (target == nullptr) {
399*b9c1b51eSKate Stone       result.AppendError("There is not a current executable; there are no "
400*b9c1b51eSKate Stone                          "breakpoints to which to add commands");
40130fdc8d8SChris Lattner       result.SetStatus(eReturnStatusFailed);
40230fdc8d8SChris Lattner       return false;
40330fdc8d8SChris Lattner     }
40430fdc8d8SChris Lattner 
40530fdc8d8SChris Lattner     const BreakpointList &breakpoints = target->GetBreakpointList();
40630fdc8d8SChris Lattner     size_t num_breakpoints = breakpoints.GetSize();
40730fdc8d8SChris Lattner 
408*b9c1b51eSKate Stone     if (num_breakpoints == 0) {
40930fdc8d8SChris Lattner       result.AppendError("No breakpoints exist to have commands added");
41030fdc8d8SChris Lattner       result.SetStatus(eReturnStatusFailed);
41130fdc8d8SChris Lattner       return false;
41230fdc8d8SChris Lattner     }
41330fdc8d8SChris Lattner 
414*b9c1b51eSKate Stone     if (!m_options.m_use_script_language &&
415*b9c1b51eSKate Stone         !m_options.m_function_name.empty()) {
416*b9c1b51eSKate Stone       result.AppendError("need to enable scripting to have a function run as a "
417*b9c1b51eSKate Stone                          "breakpoint command");
4188d4a8010SEnrico Granata       result.SetStatus(eReturnStatusFailed);
4198d4a8010SEnrico Granata       return false;
4208d4a8010SEnrico Granata     }
4218d4a8010SEnrico Granata 
42230fdc8d8SChris Lattner     BreakpointIDList valid_bp_ids;
423*b9c1b51eSKate Stone     CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
424*b9c1b51eSKate Stone         command, target, result, &valid_bp_ids);
42530fdc8d8SChris Lattner 
426b5796cb4SJim Ingham     m_bp_options_vec.clear();
427b5796cb4SJim Ingham 
428*b9c1b51eSKate Stone     if (result.Succeeded()) {
429c982c768SGreg Clayton       const size_t count = valid_bp_ids.GetSize();
430d9916eaeSJim Ingham 
431*b9c1b51eSKate Stone       for (size_t i = 0; i < count; ++i) {
43230fdc8d8SChris Lattner         BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
433*b9c1b51eSKate Stone         if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
434*b9c1b51eSKate Stone           Breakpoint *bp =
435*b9c1b51eSKate Stone               target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
436c8ecc2a9SEugene Zelenko           BreakpointOptions *bp_options = nullptr;
437*b9c1b51eSKate Stone           if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID) {
43839d7d4f0SJohnny Chen             // This breakpoint does not have an associated location.
43939d7d4f0SJohnny Chen             bp_options = bp->GetOptions();
440*b9c1b51eSKate Stone           } else {
441*b9c1b51eSKate Stone             BreakpointLocationSP bp_loc_sp(
442*b9c1b51eSKate Stone                 bp->FindLocationByID(cur_bp_id.GetLocationID()));
44339d7d4f0SJohnny Chen             // This breakpoint does have an associated location.
44439d7d4f0SJohnny Chen             // Get its breakpoint options.
44530fdc8d8SChris Lattner             if (bp_loc_sp)
44639d7d4f0SJohnny Chen               bp_options = bp_loc_sp->GetLocationOptions();
44739d7d4f0SJohnny Chen           }
448b5796cb4SJim Ingham           if (bp_options)
449b5796cb4SJim Ingham             m_bp_options_vec.push_back(bp_options);
450b5796cb4SJim Ingham         }
451b5796cb4SJim Ingham       }
45239d7d4f0SJohnny Chen 
45339d7d4f0SJohnny Chen       // If we are using script language, get the script interpreter
45439d7d4f0SJohnny Chen       // in order to set or collect command callback.  Otherwise, call
45539d7d4f0SJohnny Chen       // the methods associated with this object.
456*b9c1b51eSKate Stone       if (m_options.m_use_script_language) {
457b5796cb4SJim Ingham         ScriptInterpreter *script_interp = m_interpreter.GetScriptInterpreter();
45839d7d4f0SJohnny Chen         // Special handling for one-liner specified inline.
459*b9c1b51eSKate Stone         if (m_options.m_use_one_liner) {
460*b9c1b51eSKate Stone           script_interp->SetBreakpointCommandCallback(
461*b9c1b51eSKate Stone               m_bp_options_vec, m_options.m_one_liner.c_str());
462*b9c1b51eSKate Stone         } else if (!m_options.m_function_name.empty()) {
463*b9c1b51eSKate Stone           script_interp->SetBreakpointCommandCallbackFunction(
464*b9c1b51eSKate Stone               m_bp_options_vec, m_options.m_function_name.c_str());
465*b9c1b51eSKate Stone         } else {
466*b9c1b51eSKate Stone           script_interp->CollectDataForBreakpointCommandCallback(
467*b9c1b51eSKate Stone               m_bp_options_vec, result);
4688d4a8010SEnrico Granata         }
469*b9c1b51eSKate Stone       } else {
47039d7d4f0SJohnny Chen         // Special handling for one-liner specified inline.
47139d7d4f0SJohnny Chen         if (m_options.m_use_one_liner)
472b5796cb4SJim Ingham           SetBreakpointCommandCallback(m_bp_options_vec,
47339d7d4f0SJohnny Chen                                        m_options.m_one_liner.c_str());
47439d7d4f0SJohnny Chen         else
475*b9c1b51eSKate Stone           CollectDataForBreakpointCommandCallback(m_bp_options_vec, result);
47630fdc8d8SChris Lattner       }
47730fdc8d8SChris Lattner     }
47830fdc8d8SChris Lattner 
47930fdc8d8SChris Lattner     return result.Succeeded();
48030fdc8d8SChris Lattner   }
48130fdc8d8SChris Lattner 
4825a988416SJim Ingham private:
4835a988416SJim Ingham   CommandOptions m_options;
484*b9c1b51eSKate Stone   std::vector<BreakpointOptions *> m_bp_options_vec; // This stores the
485*b9c1b51eSKate Stone                                                      // breakpoint options that
486*b9c1b51eSKate Stone                                                      // we are currently
487b5796cb4SJim Ingham   // collecting commands for.  In the CollectData... calls we need
488b5796cb4SJim Ingham   // to hand this off to the IOHandler, which may run asynchronously.
489b5796cb4SJim Ingham   // So we have to have some way to keep it alive, and not leak it.
490b5796cb4SJim Ingham   // Making it an ivar of the command object, which never goes away
491b5796cb4SJim Ingham   // achieves this.  Note that if we were able to run
492b5796cb4SJim Ingham   // the same command concurrently in one interpreter we'd have to
493b5796cb4SJim Ingham   // make this "per invocation".  But there are many more reasons
494b5796cb4SJim Ingham   // why it is not in general safe to do that in lldb at present,
495b5796cb4SJim Ingham   // so it isn't worthwhile to come up with a more complex mechanism
496b5796cb4SJim Ingham   // to address this particular weakness right now.
4975a988416SJim Ingham   static const char *g_reader_instructions;
4985a988416SJim Ingham };
4995a988416SJim Ingham 
500*b9c1b51eSKate Stone const char *CommandObjectBreakpointCommandAdd::g_reader_instructions =
501*b9c1b51eSKate Stone     "Enter your debugger command(s).  Type 'DONE' to end.\n";
5025a988416SJim Ingham 
503*b9c1b51eSKate Stone // FIXME: "script-type" needs to have its contents determined dynamically, so
504*b9c1b51eSKate Stone // somebody can add a new scripting
505*b9c1b51eSKate Stone // language to lldb and have it pickable here without having to change this
506*b9c1b51eSKate Stone // enumeration by hand and rebuild lldb proper.
5075a988416SJim Ingham 
508*b9c1b51eSKate Stone static OptionEnumValueElement g_script_option_enumeration[4] = {
509*b9c1b51eSKate Stone     {eScriptLanguageNone, "command",
510*b9c1b51eSKate Stone      "Commands are in the lldb command interpreter language"},
5115a988416SJim Ingham     {eScriptLanguagePython, "python", "Commands are in the Python language."},
512*b9c1b51eSKate Stone     {eSortOrderByName, "default-script",
513*b9c1b51eSKate Stone      "Commands are in the default scripting language."},
514*b9c1b51eSKate Stone     {0, nullptr, nullptr}};
51530fdc8d8SChris Lattner 
5165a988416SJim Ingham OptionDefinition
517*b9c1b51eSKate Stone     CommandObjectBreakpointCommandAdd::CommandOptions::g_option_table[] = {
518ac9c3a62SKate Stone         // clang-format off
519ac9c3a62SKate Stone   {LLDB_OPT_SET_1,   false, "one-liner",         'o', OptionParser::eRequiredArgument, nullptr, nullptr,                     0, eArgTypeOneLiner,       "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
520ac9c3a62SKate Stone   {LLDB_OPT_SET_ALL, false, "stop-on-error",     'e', OptionParser::eRequiredArgument, nullptr, nullptr,                     0, eArgTypeBoolean,        "Specify whether breakpoint command execution should terminate on error." },
521ac9c3a62SKate Stone   {LLDB_OPT_SET_ALL, false, "script-type",       's', OptionParser::eRequiredArgument, nullptr, g_script_option_enumeration, 0, eArgTypeNone,           "Specify the language for the commands - if none is specified, the lldb command interpreter will be used."},
522ac9c3a62SKate Stone   {LLDB_OPT_SET_2,   false, "python-function",   'F', OptionParser::eRequiredArgument, nullptr, nullptr,                     0, eArgTypePythonFunction, "Give the name of a Python function to run as command for this breakpoint. Be sure to give a module name if appropriate."},
523ac9c3a62SKate Stone   {LLDB_OPT_SET_ALL, false, "dummy-breakpoints", 'D', OptionParser::eNoArgument,       nullptr, nullptr,                     0, eArgTypeNone,           "Sets Dummy breakpoints - i.e. breakpoints set before a file is provided, which prime new targets."},
524c8ecc2a9SEugene Zelenko   {0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr}
525ac9c3a62SKate Stone         // clang-format on
5265a988416SJim Ingham };
52730fdc8d8SChris Lattner 
52830fdc8d8SChris Lattner //-------------------------------------------------------------------------
52993e0f19fSCaroline Tice // CommandObjectBreakpointCommandDelete
53030fdc8d8SChris Lattner //-------------------------------------------------------------------------
53130fdc8d8SChris Lattner 
532*b9c1b51eSKate Stone class CommandObjectBreakpointCommandDelete : public CommandObjectParsed {
5335a988416SJim Ingham public:
534*b9c1b51eSKate Stone   CommandObjectBreakpointCommandDelete(CommandInterpreter &interpreter)
535*b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "delete",
53693e0f19fSCaroline Tice                             "Delete the set of commands from a breakpoint.",
537c8ecc2a9SEugene Zelenko                             nullptr),
538*b9c1b51eSKate Stone         m_options() {
539405fe67fSCaroline Tice     CommandArgumentEntry arg;
540405fe67fSCaroline Tice     CommandArgumentData bp_id_arg;
541405fe67fSCaroline Tice 
542405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
543405fe67fSCaroline Tice     bp_id_arg.arg_type = eArgTypeBreakpointID;
544405fe67fSCaroline Tice     bp_id_arg.arg_repetition = eArgRepeatPlain;
545405fe67fSCaroline Tice 
546*b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
547*b9c1b51eSKate Stone     // argument entry.
548405fe67fSCaroline Tice     arg.push_back(bp_id_arg);
549405fe67fSCaroline Tice 
550405fe67fSCaroline Tice     // Push the data for the first argument into the m_arguments vector.
551405fe67fSCaroline Tice     m_arguments.push_back(arg);
55230fdc8d8SChris Lattner   }
55330fdc8d8SChris Lattner 
554c8ecc2a9SEugene Zelenko   ~CommandObjectBreakpointCommandDelete() override = default;
5555a988416SJim Ingham 
556*b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
55733df7cd3SJim Ingham 
558*b9c1b51eSKate Stone   class CommandOptions : public Options {
55933df7cd3SJim Ingham   public:
560*b9c1b51eSKate Stone     CommandOptions() : Options(), m_use_dummy(false) {}
56133df7cd3SJim Ingham 
562c8ecc2a9SEugene Zelenko     ~CommandOptions() override = default;
56333df7cd3SJim Ingham 
564*b9c1b51eSKate Stone     Error SetOptionValue(uint32_t option_idx, const char *option_arg,
565*b9c1b51eSKate Stone                          ExecutionContext *execution_context) override {
56633df7cd3SJim Ingham       Error error;
56733df7cd3SJim Ingham       const int short_option = m_getopt_table[option_idx].val;
56833df7cd3SJim Ingham 
569*b9c1b51eSKate Stone       switch (short_option) {
57033df7cd3SJim Ingham       case 'D':
57133df7cd3SJim Ingham         m_use_dummy = true;
57233df7cd3SJim Ingham         break;
57333df7cd3SJim Ingham 
57433df7cd3SJim Ingham       default:
575*b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unrecognized option '%c'",
576*b9c1b51eSKate Stone                                        short_option);
57733df7cd3SJim Ingham         break;
57833df7cd3SJim Ingham       }
57933df7cd3SJim Ingham 
58033df7cd3SJim Ingham       return error;
58133df7cd3SJim Ingham     }
58233df7cd3SJim Ingham 
583*b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
58433df7cd3SJim Ingham       m_use_dummy = false;
58533df7cd3SJim Ingham     }
58633df7cd3SJim Ingham 
587*b9c1b51eSKate Stone     const OptionDefinition *GetDefinitions() override { return g_option_table; }
58833df7cd3SJim Ingham 
58933df7cd3SJim Ingham     // Options table: Required for subclasses of Options.
59033df7cd3SJim Ingham 
59133df7cd3SJim Ingham     static OptionDefinition g_option_table[];
59233df7cd3SJim Ingham 
59333df7cd3SJim Ingham     // Instance variables to hold the values for command options.
59433df7cd3SJim Ingham     bool m_use_dummy;
59533df7cd3SJim Ingham   };
59633df7cd3SJim Ingham 
5975a988416SJim Ingham protected:
598*b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
59933df7cd3SJim Ingham     Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
60030fdc8d8SChris Lattner 
601*b9c1b51eSKate Stone     if (target == nullptr) {
602*b9c1b51eSKate Stone       result.AppendError("There is not a current executable; there are no "
603*b9c1b51eSKate Stone                          "breakpoints from which to delete commands");
60430fdc8d8SChris Lattner       result.SetStatus(eReturnStatusFailed);
60530fdc8d8SChris Lattner       return false;
60630fdc8d8SChris Lattner     }
60730fdc8d8SChris Lattner 
60830fdc8d8SChris Lattner     const BreakpointList &breakpoints = target->GetBreakpointList();
60930fdc8d8SChris Lattner     size_t num_breakpoints = breakpoints.GetSize();
61030fdc8d8SChris Lattner 
611*b9c1b51eSKate Stone     if (num_breakpoints == 0) {
61293e0f19fSCaroline Tice       result.AppendError("No breakpoints exist to have commands deleted");
61330fdc8d8SChris Lattner       result.SetStatus(eReturnStatusFailed);
61430fdc8d8SChris Lattner       return false;
61530fdc8d8SChris Lattner     }
61630fdc8d8SChris Lattner 
617*b9c1b51eSKate Stone     if (command.GetArgumentCount() == 0) {
618*b9c1b51eSKate Stone       result.AppendError(
619*b9c1b51eSKate Stone           "No breakpoint specified from which to delete the commands");
62030fdc8d8SChris Lattner       result.SetStatus(eReturnStatusFailed);
62130fdc8d8SChris Lattner       return false;
62230fdc8d8SChris Lattner     }
62330fdc8d8SChris Lattner 
62430fdc8d8SChris Lattner     BreakpointIDList valid_bp_ids;
625*b9c1b51eSKate Stone     CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
626*b9c1b51eSKate Stone         command, target, result, &valid_bp_ids);
62730fdc8d8SChris Lattner 
628*b9c1b51eSKate Stone     if (result.Succeeded()) {
629c982c768SGreg Clayton       const size_t count = valid_bp_ids.GetSize();
630*b9c1b51eSKate Stone       for (size_t i = 0; i < count; ++i) {
63130fdc8d8SChris Lattner         BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
632*b9c1b51eSKate Stone         if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
633*b9c1b51eSKate Stone           Breakpoint *bp =
634*b9c1b51eSKate Stone               target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
635*b9c1b51eSKate Stone           if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
636*b9c1b51eSKate Stone             BreakpointLocationSP bp_loc_sp(
637*b9c1b51eSKate Stone                 bp->FindLocationByID(cur_bp_id.GetLocationID()));
63830fdc8d8SChris Lattner             if (bp_loc_sp)
63930fdc8d8SChris Lattner               bp_loc_sp->ClearCallback();
640*b9c1b51eSKate Stone             else {
64130fdc8d8SChris Lattner               result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
64230fdc8d8SChris Lattner                                            cur_bp_id.GetBreakpointID(),
64330fdc8d8SChris Lattner                                            cur_bp_id.GetLocationID());
64430fdc8d8SChris Lattner               result.SetStatus(eReturnStatusFailed);
64530fdc8d8SChris Lattner               return false;
64630fdc8d8SChris Lattner             }
647*b9c1b51eSKate Stone           } else {
64830fdc8d8SChris Lattner             bp->ClearCallback();
64930fdc8d8SChris Lattner           }
65030fdc8d8SChris Lattner         }
65130fdc8d8SChris Lattner       }
65230fdc8d8SChris Lattner     }
65330fdc8d8SChris Lattner     return result.Succeeded();
65430fdc8d8SChris Lattner   }
655c8ecc2a9SEugene Zelenko 
65633df7cd3SJim Ingham private:
65733df7cd3SJim Ingham   CommandOptions m_options;
6585a988416SJim Ingham };
65930fdc8d8SChris Lattner 
66033df7cd3SJim Ingham OptionDefinition
661*b9c1b51eSKate Stone     CommandObjectBreakpointCommandDelete::CommandOptions::g_option_table[] = {
662ac9c3a62SKate Stone         // clang-format off
663ac9c3a62SKate Stone   {LLDB_OPT_SET_1, false, "dummy-breakpoints", 'D', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Delete commands from Dummy breakpoints - i.e. breakpoints set before a file is provided, which prime new targets."},
664c8ecc2a9SEugene Zelenko   {0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr}
665ac9c3a62SKate Stone         // clang-format on
66633df7cd3SJim Ingham };
66733df7cd3SJim Ingham 
66830fdc8d8SChris Lattner //-------------------------------------------------------------------------
66930fdc8d8SChris Lattner // CommandObjectBreakpointCommandList
67030fdc8d8SChris Lattner //-------------------------------------------------------------------------
67130fdc8d8SChris Lattner 
672*b9c1b51eSKate Stone class CommandObjectBreakpointCommandList : public CommandObjectParsed {
6735a988416SJim Ingham public:
674*b9c1b51eSKate Stone   CommandObjectBreakpointCommandList(CommandInterpreter &interpreter)
675*b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "list", "List the script or set of "
676*b9c1b51eSKate Stone                                                  "commands to be executed when "
677*b9c1b51eSKate Stone                                                  "the breakpoint is hit.",
678*b9c1b51eSKate Stone                             nullptr) {
679405fe67fSCaroline Tice     CommandArgumentEntry arg;
680405fe67fSCaroline Tice     CommandArgumentData bp_id_arg;
681405fe67fSCaroline Tice 
682405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
683405fe67fSCaroline Tice     bp_id_arg.arg_type = eArgTypeBreakpointID;
684405fe67fSCaroline Tice     bp_id_arg.arg_repetition = eArgRepeatPlain;
685405fe67fSCaroline Tice 
686*b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
687*b9c1b51eSKate Stone     // argument entry.
688405fe67fSCaroline Tice     arg.push_back(bp_id_arg);
689405fe67fSCaroline Tice 
690405fe67fSCaroline Tice     // Push the data for the first argument into the m_arguments vector.
691405fe67fSCaroline Tice     m_arguments.push_back(arg);
69230fdc8d8SChris Lattner   }
69330fdc8d8SChris Lattner 
694c8ecc2a9SEugene Zelenko   ~CommandObjectBreakpointCommandList() override = default;
69530fdc8d8SChris Lattner 
6965a988416SJim Ingham protected:
697*b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
698a7015092SGreg Clayton     Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
69930fdc8d8SChris Lattner 
700*b9c1b51eSKate Stone     if (target == nullptr) {
701*b9c1b51eSKate Stone       result.AppendError("There is not a current executable; there are no "
702*b9c1b51eSKate Stone                          "breakpoints for which to list commands");
70330fdc8d8SChris Lattner       result.SetStatus(eReturnStatusFailed);
70430fdc8d8SChris Lattner       return false;
70530fdc8d8SChris Lattner     }
70630fdc8d8SChris Lattner 
70730fdc8d8SChris Lattner     const BreakpointList &breakpoints = target->GetBreakpointList();
70830fdc8d8SChris Lattner     size_t num_breakpoints = breakpoints.GetSize();
70930fdc8d8SChris Lattner 
710*b9c1b51eSKate Stone     if (num_breakpoints == 0) {
71130fdc8d8SChris Lattner       result.AppendError("No breakpoints exist for which to list commands");
71230fdc8d8SChris Lattner       result.SetStatus(eReturnStatusFailed);
71330fdc8d8SChris Lattner       return false;
71430fdc8d8SChris Lattner     }
71530fdc8d8SChris Lattner 
716*b9c1b51eSKate Stone     if (command.GetArgumentCount() == 0) {
717*b9c1b51eSKate Stone       result.AppendError(
718*b9c1b51eSKate Stone           "No breakpoint specified for which to list the commands");
71930fdc8d8SChris Lattner       result.SetStatus(eReturnStatusFailed);
72030fdc8d8SChris Lattner       return false;
72130fdc8d8SChris Lattner     }
72230fdc8d8SChris Lattner 
72330fdc8d8SChris Lattner     BreakpointIDList valid_bp_ids;
724*b9c1b51eSKate Stone     CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
725*b9c1b51eSKate Stone         command, target, result, &valid_bp_ids);
72630fdc8d8SChris Lattner 
727*b9c1b51eSKate Stone     if (result.Succeeded()) {
728c982c768SGreg Clayton       const size_t count = valid_bp_ids.GetSize();
729*b9c1b51eSKate Stone       for (size_t i = 0; i < count; ++i) {
73030fdc8d8SChris Lattner         BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
731*b9c1b51eSKate Stone         if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
732*b9c1b51eSKate Stone           Breakpoint *bp =
733*b9c1b51eSKate Stone               target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
73430fdc8d8SChris Lattner 
735*b9c1b51eSKate Stone           if (bp) {
736c8ecc2a9SEugene Zelenko             const BreakpointOptions *bp_options = nullptr;
737*b9c1b51eSKate Stone             if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
738*b9c1b51eSKate Stone               BreakpointLocationSP bp_loc_sp(
739*b9c1b51eSKate Stone                   bp->FindLocationByID(cur_bp_id.GetLocationID()));
74030fdc8d8SChris Lattner               if (bp_loc_sp)
74105407f6bSJim Ingham                 bp_options = bp_loc_sp->GetOptionsNoCreate();
742*b9c1b51eSKate Stone               else {
74330fdc8d8SChris Lattner                 result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
74430fdc8d8SChris Lattner                                              cur_bp_id.GetBreakpointID(),
74530fdc8d8SChris Lattner                                              cur_bp_id.GetLocationID());
74630fdc8d8SChris Lattner                 result.SetStatus(eReturnStatusFailed);
74730fdc8d8SChris Lattner                 return false;
74830fdc8d8SChris Lattner               }
749*b9c1b51eSKate Stone             } else {
75030fdc8d8SChris Lattner               bp_options = bp->GetOptions();
75130fdc8d8SChris Lattner             }
75230fdc8d8SChris Lattner 
753*b9c1b51eSKate Stone             if (bp_options) {
75430fdc8d8SChris Lattner               StreamString id_str;
755e16c50a1SJim Ingham               BreakpointID::GetCanonicalReference(&id_str,
756e16c50a1SJim Ingham                                                   cur_bp_id.GetBreakpointID(),
757e16c50a1SJim Ingham                                                   cur_bp_id.GetLocationID());
7581b54c88cSJim Ingham               const Baton *baton = bp_options->GetBaton();
759*b9c1b51eSKate Stone               if (baton) {
760*b9c1b51eSKate Stone                 result.GetOutputStream().Printf("Breakpoint %s:\n",
761*b9c1b51eSKate Stone                                                 id_str.GetData());
76230fdc8d8SChris Lattner                 result.GetOutputStream().IndentMore();
763*b9c1b51eSKate Stone                 baton->GetDescription(&result.GetOutputStream(),
764*b9c1b51eSKate Stone                                       eDescriptionLevelFull);
76530fdc8d8SChris Lattner                 result.GetOutputStream().IndentLess();
766*b9c1b51eSKate Stone               } else {
767*b9c1b51eSKate Stone                 result.AppendMessageWithFormat(
768*b9c1b51eSKate Stone                     "Breakpoint %s does not have an associated command.\n",
769e16c50a1SJim Ingham                     id_str.GetData());
77030fdc8d8SChris Lattner               }
77130fdc8d8SChris Lattner             }
77230fdc8d8SChris Lattner             result.SetStatus(eReturnStatusSuccessFinishResult);
773*b9c1b51eSKate Stone           } else {
774*b9c1b51eSKate Stone             result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n",
775*b9c1b51eSKate Stone                                          cur_bp_id.GetBreakpointID());
77630fdc8d8SChris Lattner             result.SetStatus(eReturnStatusFailed);
77730fdc8d8SChris Lattner           }
77830fdc8d8SChris Lattner         }
77930fdc8d8SChris Lattner       }
78030fdc8d8SChris Lattner     }
78130fdc8d8SChris Lattner 
78230fdc8d8SChris Lattner     return result.Succeeded();
78330fdc8d8SChris Lattner   }
7845a988416SJim Ingham };
78530fdc8d8SChris Lattner 
78630fdc8d8SChris Lattner //-------------------------------------------------------------------------
78730fdc8d8SChris Lattner // CommandObjectBreakpointCommand
78830fdc8d8SChris Lattner //-------------------------------------------------------------------------
78930fdc8d8SChris Lattner 
790*b9c1b51eSKate Stone CommandObjectBreakpointCommand::CommandObjectBreakpointCommand(
791*b9c1b51eSKate Stone     CommandInterpreter &interpreter)
7927428a18cSKate Stone     : CommandObjectMultiword(
793*b9c1b51eSKate Stone           interpreter, "command", "Commands for adding, removing and listing "
794*b9c1b51eSKate Stone                                   "LLDB commands executed when a breakpoint is "
795*b9c1b51eSKate Stone                                   "hit.",
796*b9c1b51eSKate Stone           "command <sub-command> [<sub-command-options>] <breakpoint-id>") {
797*b9c1b51eSKate Stone   CommandObjectSP add_command_object(
798*b9c1b51eSKate Stone       new CommandObjectBreakpointCommandAdd(interpreter));
799*b9c1b51eSKate Stone   CommandObjectSP delete_command_object(
800*b9c1b51eSKate Stone       new CommandObjectBreakpointCommandDelete(interpreter));
801*b9c1b51eSKate Stone   CommandObjectSP list_command_object(
802*b9c1b51eSKate Stone       new CommandObjectBreakpointCommandList(interpreter));
80330fdc8d8SChris Lattner 
80430fdc8d8SChris Lattner   add_command_object->SetCommandName("breakpoint command add");
80593e0f19fSCaroline Tice   delete_command_object->SetCommandName("breakpoint command delete");
80630fdc8d8SChris Lattner   list_command_object->SetCommandName("breakpoint command list");
80730fdc8d8SChris Lattner 
80823f59509SGreg Clayton   LoadSubCommand("add", add_command_object);
80923f59509SGreg Clayton   LoadSubCommand("delete", delete_command_object);
81023f59509SGreg Clayton   LoadSubCommand("list", list_command_object);
81130fdc8d8SChris Lattner }
81230fdc8d8SChris Lattner 
813c8ecc2a9SEugene Zelenko CommandObjectBreakpointCommand::~CommandObjectBreakpointCommand() = default;
814