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
1230fdc8d8SChris Lattner 
1330fdc8d8SChris Lattner 
1430fdc8d8SChris Lattner #include "CommandObjectBreakpointCommand.h"
1530fdc8d8SChris Lattner #include "CommandObjectBreakpoint.h"
1630fdc8d8SChris Lattner 
1730fdc8d8SChris Lattner #include "lldb/Interpreter/CommandInterpreter.h"
1830fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h"
1930fdc8d8SChris Lattner #include "lldb/Target/Target.h"
2030fdc8d8SChris Lattner #include "lldb/Target/Thread.h"
2130fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointIDList.h"
2230fdc8d8SChris Lattner #include "lldb/Breakpoint/Breakpoint.h"
2330fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointLocation.h"
2430fdc8d8SChris Lattner #include "lldb/Breakpoint/StoppointCallbackContext.h"
2530fdc8d8SChris Lattner #include "lldb/Core/State.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 
3430fdc8d8SChris Lattner 
35*5a988416SJim Ingham class CommandObjectBreakpointCommandAdd : public CommandObjectParsed
36*5a988416SJim Ingham {
37*5a988416SJim Ingham public:
38*5a988416SJim Ingham 
39*5a988416SJim Ingham     CommandObjectBreakpointCommandAdd (CommandInterpreter &interpreter) :
40*5a988416SJim Ingham         CommandObjectParsed (interpreter,
41a7015092SGreg Clayton                              "add",
42e3d26315SCaroline Tice                              "Add a set of commands to a breakpoint, to be executed whenever the breakpoint is hit.",
43eb0103f2SGreg Clayton                              NULL),
44eb0103f2SGreg Clayton         m_options (interpreter)
4530fdc8d8SChris Lattner     {
4630fdc8d8SChris Lattner         SetHelpLong (
4730fdc8d8SChris Lattner "\nGeneral information about entering breakpoint commands \n\
4830fdc8d8SChris Lattner ------------------------------------------------------ \n\
4930fdc8d8SChris Lattner  \n\
5030fdc8d8SChris Lattner This command will cause you to be prompted to enter the command or set \n\
5130fdc8d8SChris Lattner of commands you wish to be executed when the specified breakpoint is \n\
5230fdc8d8SChris Lattner hit.  You will be told to enter your command(s), and will see a '> ' \n\
5330fdc8d8SChris Lattner prompt. Because you can enter one or many commands to be executed when \n\
5430fdc8d8SChris Lattner a breakpoint is hit, you will continue to be prompted after each \n\
5530fdc8d8SChris Lattner new-line that you enter, until you enter the word 'DONE', which will \n\
5630fdc8d8SChris Lattner cause the commands you have entered to be stored with the breakpoint \n\
5730fdc8d8SChris Lattner and executed when the breakpoint is hit. \n\
5830fdc8d8SChris Lattner  \n\
5930fdc8d8SChris Lattner Syntax checking is not necessarily done when breakpoint commands are \n\
6030fdc8d8SChris Lattner entered.  An improperly written breakpoint command will attempt to get \n\
6130fdc8d8SChris Lattner executed when the breakpoint gets hit, and usually silently fail.  If \n\
6230fdc8d8SChris Lattner your breakpoint command does not appear to be getting executed, go \n\
6330fdc8d8SChris Lattner back and check your syntax. \n\
6430fdc8d8SChris Lattner  \n\
6530fdc8d8SChris Lattner  \n\
6630fdc8d8SChris Lattner Special information about PYTHON breakpoint commands                            \n\
6730fdc8d8SChris Lattner ----------------------------------------------------                            \n\
6830fdc8d8SChris Lattner                                                                                 \n\
6930fdc8d8SChris Lattner You may enter either one line of Python or multiple lines of Python             \n\
7030fdc8d8SChris Lattner (including defining whole functions, if desired).  If you enter a               \n\
7130fdc8d8SChris Lattner single line of Python, that will be passed to the Python interpreter            \n\
7230fdc8d8SChris Lattner 'as is' when the breakpoint gets hit.  If you enter function                    \n\
7330fdc8d8SChris Lattner definitions, they will be passed to the Python interpreter as soon as           \n\
7430fdc8d8SChris Lattner you finish entering the breakpoint command, and they can be called              \n\
7530fdc8d8SChris Lattner later (don't forget to add calls to them, if you want them called when          \n\
7630fdc8d8SChris Lattner the breakpoint is hit).  If you enter multiple lines of Python that             \n\
7730fdc8d8SChris Lattner are not function definitions, they will be collected into a new,                \n\
7830fdc8d8SChris Lattner automatically generated Python function, and a call to the newly                \n\
79c9efdbb0SJim Ingham generated function will be attached to the breakpoint.                          \n\
80c9efdbb0SJim Ingham                                                                                 \n\
81c9efdbb0SJim Ingham This auto-generated function is passed in two arguments:                        \n\
82c9efdbb0SJim Ingham                                                                                 \n\
83c9efdbb0SJim Ingham     frame:  an SBFrame object representing the frame which hit the breakpoint.  \n\
84c9efdbb0SJim Ingham             From the frame you can get back to the thread and process.          \n\
85c9efdbb0SJim Ingham     bp_loc: the number of the breakpoint location that was hit.                 \n\
86c9efdbb0SJim Ingham             This is useful since one breakpoint can have many locations.        \n\
87c9efdbb0SJim Ingham                                                                                 \n\
88c9efdbb0SJim Ingham Important Note: Because loose Python code gets collected into functions,        \n\
89c9efdbb0SJim Ingham if you want to access global variables in the 'loose' code, you need to         \n\
9030fdc8d8SChris Lattner specify that they are global, using the 'global' keyword.  Be sure to           \n\
9130fdc8d8SChris Lattner use correct Python syntax, including indentation, when entering Python          \n\
92c9efdbb0SJim Ingham breakpoint commands.                                                            \n\
93c9efdbb0SJim Ingham                                                                                 \n\
94c9efdbb0SJim Ingham As a third option, you can pass the name of an already existing Python function \n\
95c9efdbb0SJim Ingham and that function will be attached to the breakpoint. It will get passed the    \n\
96c9efdbb0SJim Ingham frame and bp_loc arguments mentioned above.                                     \n\
9730fdc8d8SChris Lattner                                                                                 \n\
9830fdc8d8SChris Lattner Example Python one-line breakpoint command: \n\
9930fdc8d8SChris Lattner  \n\
100e16c50a1SJim Ingham (lldb) breakpoint command add -s python 1 \n\
10130fdc8d8SChris Lattner Enter your Python command(s). Type 'DONE' to end. \n\
10230fdc8d8SChris Lattner > print \"Hit this breakpoint!\" \n\
10330fdc8d8SChris Lattner > DONE \n\
10430fdc8d8SChris Lattner  \n\
1053495f25aSJohnny Chen As a convenience, this also works for a short Python one-liner: \n\
106e16c50a1SJim Ingham (lldb) breakpoint command add -s python 1 -o \"import time; print time.asctime()\" \n\
1073495f25aSJohnny Chen (lldb) run \n\
1083495f25aSJohnny Chen Launching '.../a.out'  (x86_64) \n\
1093495f25aSJohnny Chen (lldb) Fri Sep 10 12:17:45 2010 \n\
1103495f25aSJohnny Chen Process 21778 Stopped \n\
1113495f25aSJohnny Chen * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = breakpoint 1.1, queue = com.apple.main-thread \n\
1123495f25aSJohnny Chen   36   	\n\
1133495f25aSJohnny Chen   37   	int c(int val)\n\
1143495f25aSJohnny Chen   38   	{\n\
1153495f25aSJohnny Chen   39 ->	    return val + 3;\n\
1163495f25aSJohnny Chen   40   	}\n\
1173495f25aSJohnny Chen   41   	\n\
1183495f25aSJohnny Chen   42   	int main (int argc, char const *argv[])\n\
1193495f25aSJohnny Chen (lldb) \n\
1203495f25aSJohnny Chen  \n\
12130fdc8d8SChris Lattner Example multiple line Python breakpoint command, using function definition: \n\
12230fdc8d8SChris Lattner  \n\
123e16c50a1SJim Ingham (lldb) breakpoint command add -s python 1 \n\
12430fdc8d8SChris Lattner Enter your Python command(s). Type 'DONE' to end. \n\
12530fdc8d8SChris Lattner > def breakpoint_output (bp_no): \n\
12630fdc8d8SChris Lattner >     out_string = \"Hit breakpoint number \" + repr (bp_no) \n\
12730fdc8d8SChris Lattner >     print out_string \n\
12830fdc8d8SChris Lattner >     return True \n\
12930fdc8d8SChris Lattner > breakpoint_output (1) \n\
13030fdc8d8SChris Lattner > DONE \n\
13130fdc8d8SChris Lattner  \n\
13230fdc8d8SChris Lattner  \n\
13330fdc8d8SChris Lattner Example multiple line Python breakpoint command, using 'loose' Python: \n\
13430fdc8d8SChris Lattner  \n\
135e16c50a1SJim Ingham (lldb) breakpoint command add -s p 1 \n\
13630fdc8d8SChris Lattner Enter your Python command(s). Type 'DONE' to end. \n\
13730fdc8d8SChris Lattner > global bp_count \n\
13830fdc8d8SChris Lattner > bp_count = bp_count + 1 \n\
13930fdc8d8SChris Lattner > print \"Hit this breakpoint \" + repr(bp_count) + \" times!\" \n\
14030fdc8d8SChris Lattner > DONE \n\
14130fdc8d8SChris Lattner  \n\
14230fdc8d8SChris Lattner In this case, since there is a reference to a global variable, \n\
14330fdc8d8SChris Lattner 'bp_count', you will also need to make sure 'bp_count' exists and is \n\
14430fdc8d8SChris Lattner initialized: \n\
14530fdc8d8SChris Lattner  \n\
14630fdc8d8SChris Lattner (lldb) script \n\
14730fdc8d8SChris Lattner >>> bp_count = 0 \n\
14830fdc8d8SChris Lattner >>> quit() \n\
14930fdc8d8SChris Lattner  \n\
15030fdc8d8SChris Lattner (lldb)  \n\
15130fdc8d8SChris Lattner  \n\
152650b9268SCaroline Tice  \n\
153650b9268SCaroline Tice Final Note:  If you get a warning that no breakpoint command was generated, \n\
154650b9268SCaroline Tice but you did not get any syntax errors, you probably forgot to add a call \n\
155650b9268SCaroline Tice to your functions. \n\
156650b9268SCaroline Tice  \n\
157e3d26315SCaroline Tice Special information about debugger command breakpoint commands \n\
158e3d26315SCaroline Tice -------------------------------------------------------------- \n\
15930fdc8d8SChris Lattner  \n\
16030fdc8d8SChris Lattner You may enter any debugger command, exactly as you would at the \n\
16130fdc8d8SChris Lattner debugger prompt.  You may enter as many debugger commands as you like, \n\
16230fdc8d8SChris Lattner but do NOT enter more than one command per line. \n" );
163405fe67fSCaroline Tice 
164405fe67fSCaroline Tice         CommandArgumentEntry arg;
165405fe67fSCaroline Tice         CommandArgumentData bp_id_arg;
166405fe67fSCaroline Tice 
167405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
168405fe67fSCaroline Tice         bp_id_arg.arg_type = eArgTypeBreakpointID;
169405fe67fSCaroline Tice         bp_id_arg.arg_repetition = eArgRepeatPlain;
170405fe67fSCaroline Tice 
171405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
172405fe67fSCaroline Tice         arg.push_back (bp_id_arg);
173405fe67fSCaroline Tice 
174405fe67fSCaroline Tice         // Push the data for the first argument into the m_arguments vector.
175405fe67fSCaroline Tice         m_arguments.push_back (arg);
17630fdc8d8SChris Lattner     }
17730fdc8d8SChris Lattner 
178*5a988416SJim Ingham     virtual
179*5a988416SJim Ingham     ~CommandObjectBreakpointCommandAdd () {}
180*5a988416SJim Ingham 
181*5a988416SJim Ingham     virtual Options *
182*5a988416SJim Ingham     GetOptions ()
183*5a988416SJim Ingham     {
184*5a988416SJim Ingham         return &m_options;
185*5a988416SJim Ingham     }
186*5a988416SJim Ingham 
187*5a988416SJim Ingham     void
188*5a988416SJim Ingham     CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
189*5a988416SJim Ingham                                              CommandReturnObject &result)
190*5a988416SJim Ingham     {
191*5a988416SJim Ingham         InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
192*5a988416SJim Ingham         std::auto_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
193*5a988416SJim Ingham         if (reader_sp && data_ap.get())
194*5a988416SJim Ingham         {
195*5a988416SJim Ingham             BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
196*5a988416SJim Ingham             bp_options->SetCallback (BreakpointOptionsCallbackFunction, baton_sp);
197*5a988416SJim Ingham 
198*5a988416SJim Ingham             Error err (reader_sp->Initialize (CommandObjectBreakpointCommandAdd::GenerateBreakpointCommandCallback,
199*5a988416SJim Ingham                                               bp_options,                   // baton
200*5a988416SJim Ingham                                               eInputReaderGranularityLine,  // token size, to pass to callback function
201*5a988416SJim Ingham                                               "DONE",                       // end token
202*5a988416SJim Ingham                                               "> ",                         // prompt
203*5a988416SJim Ingham                                               true));                       // echo input
204*5a988416SJim Ingham             if (err.Success())
205*5a988416SJim Ingham             {
206*5a988416SJim Ingham                 m_interpreter.GetDebugger().PushInputReader (reader_sp);
207*5a988416SJim Ingham                 result.SetStatus (eReturnStatusSuccessFinishNoResult);
208*5a988416SJim Ingham             }
209*5a988416SJim Ingham             else
210*5a988416SJim Ingham             {
211*5a988416SJim Ingham                 result.AppendError (err.AsCString());
212*5a988416SJim Ingham                 result.SetStatus (eReturnStatusFailed);
213*5a988416SJim Ingham             }
214*5a988416SJim Ingham         }
215*5a988416SJim Ingham         else
216*5a988416SJim Ingham         {
217*5a988416SJim Ingham             result.AppendError("out of memory");
218*5a988416SJim Ingham             result.SetStatus (eReturnStatusFailed);
219*5a988416SJim Ingham         }
220*5a988416SJim Ingham 
221*5a988416SJim Ingham     }
222*5a988416SJim Ingham 
223*5a988416SJim Ingham     /// Set a one-liner as the callback for the breakpoint.
224*5a988416SJim Ingham     void
225*5a988416SJim Ingham     SetBreakpointCommandCallback (BreakpointOptions *bp_options,
226*5a988416SJim Ingham                                   const char *oneliner)
227*5a988416SJim Ingham     {
228*5a988416SJim Ingham         std::auto_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
229*5a988416SJim Ingham 
230*5a988416SJim Ingham         // It's necessary to set both user_source and script_source to the oneliner.
231*5a988416SJim Ingham         // The former is used to generate callback description (as in breakpoint command list)
232*5a988416SJim Ingham         // while the latter is used for Python to interpret during the actual callback.
233*5a988416SJim Ingham         data_ap->user_source.AppendString (oneliner);
234*5a988416SJim Ingham         data_ap->script_source.assign (oneliner);
235*5a988416SJim Ingham         data_ap->stop_on_error = m_options.m_stop_on_error;
236*5a988416SJim Ingham 
237*5a988416SJim Ingham         BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
238*5a988416SJim Ingham         bp_options->SetCallback (BreakpointOptionsCallbackFunction, baton_sp);
239*5a988416SJim Ingham 
240*5a988416SJim Ingham         return;
241*5a988416SJim Ingham     }
242*5a988416SJim Ingham 
243*5a988416SJim Ingham     static size_t
244*5a988416SJim Ingham     GenerateBreakpointCommandCallback (void *baton,
245*5a988416SJim Ingham                                        InputReader &reader,
246*5a988416SJim Ingham                                        lldb::InputReaderAction notification,
247*5a988416SJim Ingham                                        const char *bytes,
248*5a988416SJim Ingham                                        size_t bytes_len)
249*5a988416SJim Ingham     {
250*5a988416SJim Ingham         StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
251*5a988416SJim Ingham         bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
252*5a988416SJim Ingham 
253*5a988416SJim Ingham         switch (notification)
254*5a988416SJim Ingham         {
255*5a988416SJim Ingham         case eInputReaderActivate:
256*5a988416SJim Ingham             if (!batch_mode)
257*5a988416SJim Ingham             {
258*5a988416SJim Ingham                 out_stream->Printf ("%s\n", g_reader_instructions);
259*5a988416SJim Ingham                 if (reader.GetPrompt())
260*5a988416SJim Ingham                     out_stream->Printf ("%s", reader.GetPrompt());
261*5a988416SJim Ingham                 out_stream->Flush();
262*5a988416SJim Ingham             }
263*5a988416SJim Ingham             break;
264*5a988416SJim Ingham 
265*5a988416SJim Ingham         case eInputReaderDeactivate:
266*5a988416SJim Ingham             break;
267*5a988416SJim Ingham 
268*5a988416SJim Ingham         case eInputReaderReactivate:
269*5a988416SJim Ingham             if (reader.GetPrompt() && !batch_mode)
270*5a988416SJim Ingham             {
271*5a988416SJim Ingham                 out_stream->Printf ("%s", reader.GetPrompt());
272*5a988416SJim Ingham                 out_stream->Flush();
273*5a988416SJim Ingham             }
274*5a988416SJim Ingham             break;
275*5a988416SJim Ingham 
276*5a988416SJim Ingham         case eInputReaderAsynchronousOutputWritten:
277*5a988416SJim Ingham             break;
278*5a988416SJim Ingham 
279*5a988416SJim Ingham         case eInputReaderGotToken:
280*5a988416SJim Ingham             if (bytes && bytes_len && baton)
281*5a988416SJim Ingham             {
282*5a988416SJim Ingham                 BreakpointOptions *bp_options = (BreakpointOptions *) baton;
283*5a988416SJim Ingham                 if (bp_options)
284*5a988416SJim Ingham                 {
285*5a988416SJim Ingham                     Baton *bp_options_baton = bp_options->GetBaton();
286*5a988416SJim Ingham                     if (bp_options_baton)
287*5a988416SJim Ingham                         ((BreakpointOptions::CommandData *)bp_options_baton->m_data)->user_source.AppendString (bytes, bytes_len);
288*5a988416SJim Ingham                 }
289*5a988416SJim Ingham             }
290*5a988416SJim Ingham             if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
291*5a988416SJim Ingham             {
292*5a988416SJim Ingham                 out_stream->Printf ("%s", reader.GetPrompt());
293*5a988416SJim Ingham                 out_stream->Flush();
294*5a988416SJim Ingham             }
295*5a988416SJim Ingham             break;
296*5a988416SJim Ingham 
297*5a988416SJim Ingham         case eInputReaderInterrupt:
298*5a988416SJim Ingham             {
299*5a988416SJim Ingham                 // Finish, and cancel the breakpoint command.
300*5a988416SJim Ingham                 reader.SetIsDone (true);
301*5a988416SJim Ingham                 BreakpointOptions *bp_options = (BreakpointOptions *) baton;
302*5a988416SJim Ingham                 if (bp_options)
303*5a988416SJim Ingham                 {
304*5a988416SJim Ingham                     Baton *bp_options_baton = bp_options->GetBaton ();
305*5a988416SJim Ingham                     if (bp_options_baton)
306*5a988416SJim Ingham                     {
307*5a988416SJim Ingham                         ((BreakpointOptions::CommandData *) bp_options_baton->m_data)->user_source.Clear();
308*5a988416SJim Ingham                         ((BreakpointOptions::CommandData *) bp_options_baton->m_data)->script_source.clear();
309*5a988416SJim Ingham                     }
310*5a988416SJim Ingham                 }
311*5a988416SJim Ingham                 if (!batch_mode)
312*5a988416SJim Ingham                 {
313*5a988416SJim Ingham                     out_stream->Printf ("Warning: No command attached to breakpoint.\n");
314*5a988416SJim Ingham                     out_stream->Flush();
315*5a988416SJim Ingham                 }
316*5a988416SJim Ingham             }
317*5a988416SJim Ingham             break;
318*5a988416SJim Ingham 
319*5a988416SJim Ingham         case eInputReaderEndOfFile:
320*5a988416SJim Ingham             reader.SetIsDone (true);
321*5a988416SJim Ingham             break;
322*5a988416SJim Ingham 
323*5a988416SJim Ingham         case eInputReaderDone:
324*5a988416SJim Ingham             break;
325*5a988416SJim Ingham         }
326*5a988416SJim Ingham 
327*5a988416SJim Ingham         return bytes_len;
328*5a988416SJim Ingham     }
329*5a988416SJim Ingham 
330*5a988416SJim Ingham     static bool
331*5a988416SJim Ingham     BreakpointOptionsCallbackFunction (void *baton,
332*5a988416SJim Ingham                                        StoppointCallbackContext *context,
333*5a988416SJim Ingham                                        lldb::user_id_t break_id,
334*5a988416SJim Ingham                                        lldb::user_id_t break_loc_id)
335*5a988416SJim Ingham     {
336*5a988416SJim Ingham         bool ret_value = true;
337*5a988416SJim Ingham         if (baton == NULL)
338*5a988416SJim Ingham             return true;
339*5a988416SJim Ingham 
340*5a988416SJim Ingham 
341*5a988416SJim Ingham         BreakpointOptions::CommandData *data = (BreakpointOptions::CommandData *) baton;
342*5a988416SJim Ingham         StringList &commands = data->user_source;
343*5a988416SJim Ingham 
344*5a988416SJim Ingham         if (commands.GetSize() > 0)
345*5a988416SJim Ingham         {
346*5a988416SJim Ingham             ExecutionContext exe_ctx (context->exe_ctx_ref);
347*5a988416SJim Ingham             Target *target = exe_ctx.GetTargetPtr();
348*5a988416SJim Ingham             if (target)
349*5a988416SJim Ingham             {
350*5a988416SJim Ingham                 CommandReturnObject result;
351*5a988416SJim Ingham                 Debugger &debugger = target->GetDebugger();
352*5a988416SJim Ingham                 // Rig up the results secondary output stream to the debugger's, so the output will come out synchronously
353*5a988416SJim Ingham                 // if the debugger is set up that way.
354*5a988416SJim Ingham 
355*5a988416SJim Ingham                 StreamSP output_stream (debugger.GetAsyncOutputStream());
356*5a988416SJim Ingham                 StreamSP error_stream (debugger.GetAsyncErrorStream());
357*5a988416SJim Ingham                 result.SetImmediateOutputStream (output_stream);
358*5a988416SJim Ingham                 result.SetImmediateErrorStream (error_stream);
359*5a988416SJim Ingham 
360*5a988416SJim Ingham                 bool stop_on_continue = true;
361*5a988416SJim Ingham                 bool echo_commands    = false;
362*5a988416SJim Ingham                 bool print_results    = true;
363*5a988416SJim Ingham 
364*5a988416SJim Ingham                 debugger.GetCommandInterpreter().HandleCommands (commands,
365*5a988416SJim Ingham                                                                  &exe_ctx,
366*5a988416SJim Ingham                                                                  stop_on_continue,
367*5a988416SJim Ingham                                                                  data->stop_on_error,
368*5a988416SJim Ingham                                                                  echo_commands,
369*5a988416SJim Ingham                                                                  print_results,
370*5a988416SJim Ingham                                                                  eLazyBoolNo,
371*5a988416SJim Ingham                                                                  result);
372*5a988416SJim Ingham                 result.GetImmediateOutputStream()->Flush();
373*5a988416SJim Ingham                 result.GetImmediateErrorStream()->Flush();
374*5a988416SJim Ingham            }
375*5a988416SJim Ingham         }
376*5a988416SJim Ingham         return ret_value;
377*5a988416SJim Ingham     }
378*5a988416SJim Ingham 
379*5a988416SJim Ingham     class CommandOptions : public Options
380*5a988416SJim Ingham     {
381*5a988416SJim Ingham     public:
382*5a988416SJim Ingham 
383*5a988416SJim Ingham         CommandOptions (CommandInterpreter &interpreter) :
384*5a988416SJim Ingham             Options (interpreter),
385*5a988416SJim Ingham             m_use_commands (false),
386*5a988416SJim Ingham             m_use_script_language (false),
387*5a988416SJim Ingham             m_script_language (eScriptLanguageNone),
388*5a988416SJim Ingham             m_use_one_liner (false),
389*5a988416SJim Ingham             m_one_liner(),
390*5a988416SJim Ingham             m_function_name()
39130fdc8d8SChris Lattner         {
39230fdc8d8SChris Lattner         }
39330fdc8d8SChris Lattner 
394*5a988416SJim Ingham         virtual
395*5a988416SJim Ingham         ~CommandOptions () {}
396*5a988416SJim Ingham 
397*5a988416SJim Ingham         virtual Error
398*5a988416SJim Ingham         SetOptionValue (uint32_t option_idx, const char *option_arg)
399*5a988416SJim Ingham         {
400*5a988416SJim Ingham             Error error;
401*5a988416SJim Ingham             char short_option = (char) m_getopt_table[option_idx].val;
402*5a988416SJim Ingham 
403*5a988416SJim Ingham             switch (short_option)
404*5a988416SJim Ingham             {
405*5a988416SJim Ingham             case 'o':
406*5a988416SJim Ingham                 m_use_one_liner = true;
407*5a988416SJim Ingham                 m_one_liner = option_arg;
408*5a988416SJim Ingham                 break;
409*5a988416SJim Ingham 
410*5a988416SJim Ingham             case 's':
411*5a988416SJim Ingham                 m_script_language = (lldb::ScriptLanguage) Args::StringToOptionEnum (option_arg,
412*5a988416SJim Ingham                                                                                      g_option_table[option_idx].enum_values,
413*5a988416SJim Ingham                                                                                      eScriptLanguageNone,
414*5a988416SJim Ingham                                                                                      error);
415*5a988416SJim Ingham 
416*5a988416SJim Ingham                 if (m_script_language == eScriptLanguagePython || m_script_language == eScriptLanguageDefault)
417*5a988416SJim Ingham                 {
418*5a988416SJim Ingham                     m_use_script_language = true;
419*5a988416SJim Ingham                 }
420*5a988416SJim Ingham                 else
421*5a988416SJim Ingham                 {
422*5a988416SJim Ingham                     m_use_script_language = false;
423*5a988416SJim Ingham                 }
424*5a988416SJim Ingham                 break;
425*5a988416SJim Ingham 
426*5a988416SJim Ingham             case 'e':
427*5a988416SJim Ingham                 {
428*5a988416SJim Ingham                     bool success = false;
429*5a988416SJim Ingham                     m_stop_on_error = Args::StringToBoolean(option_arg, false, &success);
430*5a988416SJim Ingham                     if (!success)
431*5a988416SJim Ingham                         error.SetErrorStringWithFormat("invalid value for stop-on-error: \"%s\"", option_arg);
432*5a988416SJim Ingham                 }
433*5a988416SJim Ingham                 break;
434*5a988416SJim Ingham 
435*5a988416SJim Ingham             case 'F':
436*5a988416SJim Ingham                 {
437*5a988416SJim Ingham                     m_use_one_liner = false;
438*5a988416SJim Ingham                     m_use_script_language = true;
439*5a988416SJim Ingham                     m_function_name.assign(option_arg);
440*5a988416SJim Ingham                 }
441*5a988416SJim Ingham                 break;
442*5a988416SJim Ingham 
443*5a988416SJim Ingham             default:
444*5a988416SJim Ingham                 break;
445*5a988416SJim Ingham             }
446*5a988416SJim Ingham             return error;
447*5a988416SJim Ingham         }
448*5a988416SJim Ingham         void
449*5a988416SJim Ingham         OptionParsingStarting ()
450*5a988416SJim Ingham         {
451*5a988416SJim Ingham             m_use_commands = true;
452*5a988416SJim Ingham             m_use_script_language = false;
453*5a988416SJim Ingham             m_script_language = eScriptLanguageNone;
454*5a988416SJim Ingham 
455*5a988416SJim Ingham             m_use_one_liner = false;
456*5a988416SJim Ingham             m_stop_on_error = true;
457*5a988416SJim Ingham             m_one_liner.clear();
458*5a988416SJim Ingham             m_function_name.clear();
459*5a988416SJim Ingham         }
460*5a988416SJim Ingham 
461*5a988416SJim Ingham         const OptionDefinition*
462*5a988416SJim Ingham         GetDefinitions ()
463*5a988416SJim Ingham         {
464*5a988416SJim Ingham             return g_option_table;
465*5a988416SJim Ingham         }
466*5a988416SJim Ingham 
467*5a988416SJim Ingham         // Options table: Required for subclasses of Options.
468*5a988416SJim Ingham 
469*5a988416SJim Ingham         static OptionDefinition g_option_table[];
470*5a988416SJim Ingham 
471*5a988416SJim Ingham         // Instance variables to hold the values for command options.
472*5a988416SJim Ingham 
473*5a988416SJim Ingham         bool m_use_commands;
474*5a988416SJim Ingham         bool m_use_script_language;
475*5a988416SJim Ingham         lldb::ScriptLanguage m_script_language;
476*5a988416SJim Ingham 
477*5a988416SJim Ingham         // Instance variables to hold the values for one_liner options.
478*5a988416SJim Ingham         bool m_use_one_liner;
479*5a988416SJim Ingham         std::string m_one_liner;
480*5a988416SJim Ingham         bool m_stop_on_error;
481*5a988416SJim Ingham         std::string m_function_name;
482*5a988416SJim Ingham     };
483*5a988416SJim Ingham 
484*5a988416SJim Ingham protected:
485*5a988416SJim Ingham     virtual bool
486*5a988416SJim Ingham     DoExecute (Args& command, CommandReturnObject &result)
48730fdc8d8SChris Lattner     {
488a7015092SGreg Clayton         Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
48930fdc8d8SChris Lattner 
49030fdc8d8SChris Lattner         if (target == NULL)
49130fdc8d8SChris Lattner         {
49230fdc8d8SChris Lattner             result.AppendError ("There is not a current executable; there are no breakpoints to which to add commands");
49330fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
49430fdc8d8SChris Lattner             return false;
49530fdc8d8SChris Lattner         }
49630fdc8d8SChris Lattner 
49730fdc8d8SChris Lattner         const BreakpointList &breakpoints = target->GetBreakpointList();
49830fdc8d8SChris Lattner         size_t num_breakpoints = breakpoints.GetSize();
49930fdc8d8SChris Lattner 
50030fdc8d8SChris Lattner         if (num_breakpoints == 0)
50130fdc8d8SChris Lattner         {
50230fdc8d8SChris Lattner             result.AppendError ("No breakpoints exist to have commands added");
50330fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
50430fdc8d8SChris Lattner             return false;
50530fdc8d8SChris Lattner         }
50630fdc8d8SChris Lattner 
5078d4a8010SEnrico Granata         if (m_options.m_use_script_language == false && m_options.m_function_name.size())
5088d4a8010SEnrico Granata         {
5098d4a8010SEnrico Granata             result.AppendError ("need to enable scripting to have a function run as a breakpoint command");
5108d4a8010SEnrico Granata             result.SetStatus (eReturnStatusFailed);
5118d4a8010SEnrico Granata             return false;
5128d4a8010SEnrico Granata         }
5138d4a8010SEnrico Granata 
51430fdc8d8SChris Lattner         BreakpointIDList valid_bp_ids;
51530fdc8d8SChris Lattner         CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
51630fdc8d8SChris Lattner 
51730fdc8d8SChris Lattner         if (result.Succeeded())
51830fdc8d8SChris Lattner         {
519c982c768SGreg Clayton             const size_t count = valid_bp_ids.GetSize();
520c982c768SGreg Clayton             for (size_t i = 0; i < count; ++i)
52130fdc8d8SChris Lattner             {
52230fdc8d8SChris Lattner                 BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
52330fdc8d8SChris Lattner                 if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
52430fdc8d8SChris Lattner                 {
52530fdc8d8SChris Lattner                     Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
52639d7d4f0SJohnny Chen                     BreakpointOptions *bp_options = NULL;
52739d7d4f0SJohnny Chen                     if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID)
52839d7d4f0SJohnny Chen                     {
52939d7d4f0SJohnny Chen                         // This breakpoint does not have an associated location.
53039d7d4f0SJohnny Chen                         bp_options = bp->GetOptions();
53139d7d4f0SJohnny Chen                     }
53239d7d4f0SJohnny Chen                     else
53330fdc8d8SChris Lattner                     {
53430fdc8d8SChris Lattner                         BreakpointLocationSP bp_loc_sp(bp->FindLocationByID (cur_bp_id.GetLocationID()));
53539d7d4f0SJohnny Chen                         // This breakpoint does have an associated location.
53639d7d4f0SJohnny Chen                         // Get its breakpoint options.
53730fdc8d8SChris Lattner                         if (bp_loc_sp)
53839d7d4f0SJohnny Chen                             bp_options = bp_loc_sp->GetLocationOptions();
53939d7d4f0SJohnny Chen                     }
54039d7d4f0SJohnny Chen 
541e16c50a1SJim Ingham                     // Skip this breakpoint if bp_options is not good.
54239d7d4f0SJohnny Chen                     if (bp_options == NULL) continue;
54339d7d4f0SJohnny Chen 
54439d7d4f0SJohnny Chen                     // If we are using script language, get the script interpreter
54539d7d4f0SJohnny Chen                     // in order to set or collect command callback.  Otherwise, call
54639d7d4f0SJohnny Chen                     // the methods associated with this object.
54730fdc8d8SChris Lattner                     if (m_options.m_use_script_language)
54830fdc8d8SChris Lattner                     {
54939d7d4f0SJohnny Chen                         // Special handling for one-liner specified inline.
55039d7d4f0SJohnny Chen                         if (m_options.m_use_one_liner)
5518d4a8010SEnrico Granata                         {
552a7015092SGreg Clayton                             m_interpreter.GetScriptInterpreter()->SetBreakpointCommandCallback (bp_options,
55339d7d4f0SJohnny Chen                                                                                                 m_options.m_one_liner.c_str());
5548d4a8010SEnrico Granata                         }
5558d4a8010SEnrico Granata                         // Special handling for using a Python function by name
5568d4a8010SEnrico Granata                         // instead of extending the breakpoint callback data structures, we just automatize
5578d4a8010SEnrico Granata                         // what the user would do manually: make their breakpoint command be a function call
5588d4a8010SEnrico Granata                         else if (m_options.m_function_name.size())
5598d4a8010SEnrico Granata                         {
5608d4a8010SEnrico Granata                             std::string oneliner(m_options.m_function_name);
5618d4a8010SEnrico Granata                             oneliner += "(frame, bp_loc, dict)";
5628d4a8010SEnrico Granata                             m_interpreter.GetScriptInterpreter()->SetBreakpointCommandCallback (bp_options,
5638d4a8010SEnrico Granata                                                                                                 oneliner.c_str());
5648d4a8010SEnrico Granata                         }
56594de55d5SJohnny Chen                         else
5668d4a8010SEnrico Granata                         {
567a7015092SGreg Clayton                             m_interpreter.GetScriptInterpreter()->CollectDataForBreakpointCommandCallback (bp_options,
56830fdc8d8SChris Lattner                                                                                                            result);
56930fdc8d8SChris Lattner                         }
5708d4a8010SEnrico Granata                     }
57130fdc8d8SChris Lattner                     else
57230fdc8d8SChris Lattner                     {
57339d7d4f0SJohnny Chen                         // Special handling for one-liner specified inline.
57439d7d4f0SJohnny Chen                         if (m_options.m_use_one_liner)
575a7015092SGreg Clayton                             SetBreakpointCommandCallback (bp_options,
57639d7d4f0SJohnny Chen                                                           m_options.m_one_liner.c_str());
57739d7d4f0SJohnny Chen                         else
578a7015092SGreg Clayton                             CollectDataForBreakpointCommandCallback (bp_options,
579b132097bSGreg Clayton                                                                      result);
58030fdc8d8SChris Lattner                     }
58130fdc8d8SChris Lattner                 }
58230fdc8d8SChris Lattner             }
58330fdc8d8SChris Lattner         }
58430fdc8d8SChris Lattner 
58530fdc8d8SChris Lattner         return result.Succeeded();
58630fdc8d8SChris Lattner     }
58730fdc8d8SChris Lattner 
588*5a988416SJim Ingham private:
589*5a988416SJim Ingham     CommandOptions m_options;
590*5a988416SJim Ingham     static const char *g_reader_instructions;
591*5a988416SJim Ingham 
592*5a988416SJim Ingham };
593*5a988416SJim Ingham 
594*5a988416SJim Ingham const char *
595*5a988416SJim Ingham CommandObjectBreakpointCommandAdd::g_reader_instructions = "Enter your debugger command(s).  Type 'DONE' to end.";
596*5a988416SJim Ingham 
597*5a988416SJim Ingham // FIXME: "script-type" needs to have its contents determined dynamically, so somebody can add a new scripting
598*5a988416SJim Ingham // language to lldb and have it pickable here without having to change this enumeration by hand and rebuild lldb proper.
599*5a988416SJim Ingham 
600*5a988416SJim Ingham static OptionEnumValueElement
601*5a988416SJim Ingham g_script_option_enumeration[4] =
60230fdc8d8SChris Lattner {
603*5a988416SJim Ingham     { eScriptLanguageNone,    "command",         "Commands are in the lldb command interpreter language"},
604*5a988416SJim Ingham     { eScriptLanguagePython,  "python",          "Commands are in the Python language."},
605*5a988416SJim Ingham     { eSortOrderByName,       "default-script",  "Commands are in the default scripting language."},
606*5a988416SJim Ingham     { 0,                      NULL,              NULL }
607*5a988416SJim Ingham };
60830fdc8d8SChris Lattner 
609*5a988416SJim Ingham OptionDefinition
610*5a988416SJim Ingham CommandObjectBreakpointCommandAdd::CommandOptions::g_option_table[] =
61130fdc8d8SChris Lattner {
612*5a988416SJim Ingham     { LLDB_OPT_SET_1, false, "one-liner", 'o', required_argument, NULL, NULL, eArgTypeOneLiner,
613*5a988416SJim Ingham         "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
61430fdc8d8SChris Lattner 
615*5a988416SJim Ingham     { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', required_argument, NULL, NULL, eArgTypeBoolean,
616*5a988416SJim Ingham         "Specify whether breakpoint command execution should terminate on error." },
61730fdc8d8SChris Lattner 
618*5a988416SJim Ingham     { LLDB_OPT_SET_ALL,   false, "script-type",     's', required_argument, g_script_option_enumeration, NULL, eArgTypeNone,
619*5a988416SJim Ingham         "Specify the language for the commands - if none is specified, the lldb command interpreter will be used."},
62030fdc8d8SChris Lattner 
621*5a988416SJim Ingham     { LLDB_OPT_SET_2,   false, "python-function",     'F', required_argument, NULL, NULL, eArgTypePythonFunction,
622*5a988416SJim Ingham         "Give the name of a Python function to run as command for this breakpoint. Be sure to give a module name if appropriate."},
62339d7d4f0SJohnny Chen 
624*5a988416SJim Ingham     { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
625*5a988416SJim Ingham };
62630fdc8d8SChris Lattner 
62730fdc8d8SChris Lattner //-------------------------------------------------------------------------
62893e0f19fSCaroline Tice // CommandObjectBreakpointCommandDelete
62930fdc8d8SChris Lattner //-------------------------------------------------------------------------
63030fdc8d8SChris Lattner 
631*5a988416SJim Ingham class CommandObjectBreakpointCommandDelete : public CommandObjectParsed
632*5a988416SJim Ingham {
633*5a988416SJim Ingham public:
634*5a988416SJim Ingham     CommandObjectBreakpointCommandDelete (CommandInterpreter &interpreter) :
635*5a988416SJim Ingham         CommandObjectParsed (interpreter,
63693e0f19fSCaroline Tice                              "delete",
63793e0f19fSCaroline Tice                              "Delete the set of commands from a breakpoint.",
638405fe67fSCaroline Tice                              NULL)
63930fdc8d8SChris Lattner     {
640405fe67fSCaroline Tice         CommandArgumentEntry arg;
641405fe67fSCaroline Tice         CommandArgumentData bp_id_arg;
642405fe67fSCaroline Tice 
643405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
644405fe67fSCaroline Tice         bp_id_arg.arg_type = eArgTypeBreakpointID;
645405fe67fSCaroline Tice         bp_id_arg.arg_repetition = eArgRepeatPlain;
646405fe67fSCaroline Tice 
647405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
648405fe67fSCaroline Tice         arg.push_back (bp_id_arg);
649405fe67fSCaroline Tice 
650405fe67fSCaroline Tice         // Push the data for the first argument into the m_arguments vector.
651405fe67fSCaroline Tice         m_arguments.push_back (arg);
65230fdc8d8SChris Lattner     }
65330fdc8d8SChris Lattner 
65430fdc8d8SChris Lattner 
655*5a988416SJim Ingham     virtual
656*5a988416SJim Ingham     ~CommandObjectBreakpointCommandDelete () {}
657*5a988416SJim Ingham 
658*5a988416SJim Ingham protected:
659*5a988416SJim Ingham     virtual bool
660*5a988416SJim Ingham     DoExecute (Args& command, CommandReturnObject &result)
66130fdc8d8SChris Lattner     {
662a7015092SGreg Clayton         Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
66330fdc8d8SChris Lattner 
66430fdc8d8SChris Lattner         if (target == NULL)
66530fdc8d8SChris Lattner         {
66693e0f19fSCaroline Tice             result.AppendError ("There is not a current executable; there are no breakpoints from which to delete commands");
66730fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
66830fdc8d8SChris Lattner             return false;
66930fdc8d8SChris Lattner         }
67030fdc8d8SChris Lattner 
67130fdc8d8SChris Lattner         const BreakpointList &breakpoints = target->GetBreakpointList();
67230fdc8d8SChris Lattner         size_t num_breakpoints = breakpoints.GetSize();
67330fdc8d8SChris Lattner 
67430fdc8d8SChris Lattner         if (num_breakpoints == 0)
67530fdc8d8SChris Lattner         {
67693e0f19fSCaroline Tice             result.AppendError ("No breakpoints exist to have commands deleted");
67730fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
67830fdc8d8SChris Lattner             return false;
67930fdc8d8SChris Lattner         }
68030fdc8d8SChris Lattner 
68130fdc8d8SChris Lattner         if (command.GetArgumentCount() == 0)
68230fdc8d8SChris Lattner         {
68393e0f19fSCaroline Tice             result.AppendError ("No breakpoint specified from which to delete the commands");
68430fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
68530fdc8d8SChris Lattner             return false;
68630fdc8d8SChris Lattner         }
68730fdc8d8SChris Lattner 
68830fdc8d8SChris Lattner         BreakpointIDList valid_bp_ids;
68930fdc8d8SChris Lattner         CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
69030fdc8d8SChris Lattner 
69130fdc8d8SChris Lattner         if (result.Succeeded())
69230fdc8d8SChris Lattner         {
693c982c768SGreg Clayton             const size_t count = valid_bp_ids.GetSize();
694c982c768SGreg Clayton             for (size_t i = 0; i < count; ++i)
69530fdc8d8SChris Lattner             {
69630fdc8d8SChris Lattner                 BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
69730fdc8d8SChris Lattner                 if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
69830fdc8d8SChris Lattner                 {
69930fdc8d8SChris Lattner                     Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
70030fdc8d8SChris Lattner                     if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
70130fdc8d8SChris Lattner                     {
70230fdc8d8SChris Lattner                         BreakpointLocationSP bp_loc_sp (bp->FindLocationByID (cur_bp_id.GetLocationID()));
70330fdc8d8SChris Lattner                         if (bp_loc_sp)
70430fdc8d8SChris Lattner                             bp_loc_sp->ClearCallback();
70530fdc8d8SChris Lattner                         else
70630fdc8d8SChris Lattner                         {
70730fdc8d8SChris Lattner                             result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
70830fdc8d8SChris Lattner                                                          cur_bp_id.GetBreakpointID(),
70930fdc8d8SChris Lattner                                                          cur_bp_id.GetLocationID());
71030fdc8d8SChris Lattner                             result.SetStatus (eReturnStatusFailed);
71130fdc8d8SChris Lattner                             return false;
71230fdc8d8SChris Lattner                         }
71330fdc8d8SChris Lattner                     }
71430fdc8d8SChris Lattner                     else
71530fdc8d8SChris Lattner                     {
71630fdc8d8SChris Lattner                         bp->ClearCallback();
71730fdc8d8SChris Lattner                     }
71830fdc8d8SChris Lattner                 }
71930fdc8d8SChris Lattner             }
72030fdc8d8SChris Lattner         }
72130fdc8d8SChris Lattner         return result.Succeeded();
72230fdc8d8SChris Lattner     }
723*5a988416SJim Ingham };
72430fdc8d8SChris Lattner 
72530fdc8d8SChris Lattner //-------------------------------------------------------------------------
72630fdc8d8SChris Lattner // CommandObjectBreakpointCommandList
72730fdc8d8SChris Lattner //-------------------------------------------------------------------------
72830fdc8d8SChris Lattner 
729*5a988416SJim Ingham class CommandObjectBreakpointCommandList : public CommandObjectParsed
730*5a988416SJim Ingham {
731*5a988416SJim Ingham public:
732*5a988416SJim Ingham     CommandObjectBreakpointCommandList (CommandInterpreter &interpreter) :
733*5a988416SJim Ingham         CommandObjectParsed (interpreter,
734a7015092SGreg Clayton                              "list",
73530fdc8d8SChris Lattner                              "List the script or set of commands to be executed when the breakpoint is hit.",
736405fe67fSCaroline Tice                               NULL)
73730fdc8d8SChris Lattner     {
738405fe67fSCaroline Tice         CommandArgumentEntry arg;
739405fe67fSCaroline Tice         CommandArgumentData bp_id_arg;
740405fe67fSCaroline Tice 
741405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
742405fe67fSCaroline Tice         bp_id_arg.arg_type = eArgTypeBreakpointID;
743405fe67fSCaroline Tice         bp_id_arg.arg_repetition = eArgRepeatPlain;
744405fe67fSCaroline Tice 
745405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
746405fe67fSCaroline Tice         arg.push_back (bp_id_arg);
747405fe67fSCaroline Tice 
748405fe67fSCaroline Tice         // Push the data for the first argument into the m_arguments vector.
749405fe67fSCaroline Tice         m_arguments.push_back (arg);
75030fdc8d8SChris Lattner     }
75130fdc8d8SChris Lattner 
752*5a988416SJim Ingham     virtual
753*5a988416SJim Ingham     ~CommandObjectBreakpointCommandList () {}
75430fdc8d8SChris Lattner 
755*5a988416SJim Ingham protected:
756*5a988416SJim Ingham     virtual bool
757*5a988416SJim Ingham     DoExecute (Args& command,
758*5a988416SJim Ingham              CommandReturnObject &result)
75930fdc8d8SChris Lattner     {
760a7015092SGreg Clayton         Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
76130fdc8d8SChris Lattner 
76230fdc8d8SChris Lattner         if (target == NULL)
76330fdc8d8SChris Lattner         {
76430fdc8d8SChris Lattner             result.AppendError ("There is not a current executable; there are no breakpoints for which to list commands");
76530fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
76630fdc8d8SChris Lattner             return false;
76730fdc8d8SChris Lattner         }
76830fdc8d8SChris Lattner 
76930fdc8d8SChris Lattner         const BreakpointList &breakpoints = target->GetBreakpointList();
77030fdc8d8SChris Lattner         size_t num_breakpoints = breakpoints.GetSize();
77130fdc8d8SChris Lattner 
77230fdc8d8SChris Lattner         if (num_breakpoints == 0)
77330fdc8d8SChris Lattner         {
77430fdc8d8SChris Lattner             result.AppendError ("No breakpoints exist for which to list commands");
77530fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
77630fdc8d8SChris Lattner             return false;
77730fdc8d8SChris Lattner         }
77830fdc8d8SChris Lattner 
77930fdc8d8SChris Lattner         if (command.GetArgumentCount() == 0)
78030fdc8d8SChris Lattner         {
78130fdc8d8SChris Lattner             result.AppendError ("No breakpoint specified for which to list the commands");
78230fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
78330fdc8d8SChris Lattner             return false;
78430fdc8d8SChris Lattner         }
78530fdc8d8SChris Lattner 
78630fdc8d8SChris Lattner         BreakpointIDList valid_bp_ids;
78730fdc8d8SChris Lattner         CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
78830fdc8d8SChris Lattner 
78930fdc8d8SChris Lattner         if (result.Succeeded())
79030fdc8d8SChris Lattner         {
791c982c768SGreg Clayton             const size_t count = valid_bp_ids.GetSize();
792c982c768SGreg Clayton             for (size_t i = 0; i < count; ++i)
79330fdc8d8SChris Lattner             {
79430fdc8d8SChris Lattner                 BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
79530fdc8d8SChris Lattner                 if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
79630fdc8d8SChris Lattner                 {
79730fdc8d8SChris Lattner                     Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
79830fdc8d8SChris Lattner 
79930fdc8d8SChris Lattner                     if (bp)
80030fdc8d8SChris Lattner                     {
8011b54c88cSJim Ingham                         const BreakpointOptions *bp_options = NULL;
80230fdc8d8SChris Lattner                         if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
80330fdc8d8SChris Lattner                         {
80430fdc8d8SChris Lattner                             BreakpointLocationSP bp_loc_sp(bp->FindLocationByID (cur_bp_id.GetLocationID()));
80530fdc8d8SChris Lattner                             if (bp_loc_sp)
80605407f6bSJim Ingham                                 bp_options = bp_loc_sp->GetOptionsNoCreate();
80730fdc8d8SChris Lattner                             else
80830fdc8d8SChris Lattner                             {
80930fdc8d8SChris Lattner                                 result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
81030fdc8d8SChris Lattner                                                              cur_bp_id.GetBreakpointID(),
81130fdc8d8SChris Lattner                                                              cur_bp_id.GetLocationID());
81230fdc8d8SChris Lattner                                 result.SetStatus (eReturnStatusFailed);
81330fdc8d8SChris Lattner                                 return false;
81430fdc8d8SChris Lattner                             }
81530fdc8d8SChris Lattner                         }
81630fdc8d8SChris Lattner                         else
81730fdc8d8SChris Lattner                         {
81830fdc8d8SChris Lattner                             bp_options = bp->GetOptions();
81930fdc8d8SChris Lattner                         }
82030fdc8d8SChris Lattner 
82130fdc8d8SChris Lattner                         if (bp_options)
82230fdc8d8SChris Lattner                         {
82330fdc8d8SChris Lattner                             StreamString id_str;
824e16c50a1SJim Ingham                             BreakpointID::GetCanonicalReference (&id_str,
825e16c50a1SJim Ingham                                                                  cur_bp_id.GetBreakpointID(),
826e16c50a1SJim Ingham                                                                  cur_bp_id.GetLocationID());
8271b54c88cSJim Ingham                             const Baton *baton = bp_options->GetBaton();
82830fdc8d8SChris Lattner                             if (baton)
82930fdc8d8SChris Lattner                             {
83030fdc8d8SChris Lattner                                 result.GetOutputStream().Printf ("Breakpoint %s:\n", id_str.GetData());
83130fdc8d8SChris Lattner                                 result.GetOutputStream().IndentMore ();
83230fdc8d8SChris Lattner                                 baton->GetDescription(&result.GetOutputStream(), eDescriptionLevelFull);
83330fdc8d8SChris Lattner                                 result.GetOutputStream().IndentLess ();
83430fdc8d8SChris Lattner                             }
83530fdc8d8SChris Lattner                             else
83630fdc8d8SChris Lattner                             {
837e16c50a1SJim Ingham                                 result.AppendMessageWithFormat ("Breakpoint %s does not have an associated command.\n",
838e16c50a1SJim Ingham                                                                 id_str.GetData());
83930fdc8d8SChris Lattner                             }
84030fdc8d8SChris Lattner                         }
84130fdc8d8SChris Lattner                         result.SetStatus (eReturnStatusSuccessFinishResult);
84230fdc8d8SChris Lattner                     }
84330fdc8d8SChris Lattner                     else
84430fdc8d8SChris Lattner                     {
84530fdc8d8SChris Lattner                         result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n", cur_bp_id.GetBreakpointID());
84630fdc8d8SChris Lattner                         result.SetStatus (eReturnStatusFailed);
84730fdc8d8SChris Lattner                     }
84830fdc8d8SChris Lattner 
84930fdc8d8SChris Lattner                 }
85030fdc8d8SChris Lattner             }
85130fdc8d8SChris Lattner         }
85230fdc8d8SChris Lattner 
85330fdc8d8SChris Lattner         return result.Succeeded();
85430fdc8d8SChris Lattner     }
855*5a988416SJim Ingham };
85630fdc8d8SChris Lattner 
85730fdc8d8SChris Lattner //-------------------------------------------------------------------------
85830fdc8d8SChris Lattner // CommandObjectBreakpointCommand
85930fdc8d8SChris Lattner //-------------------------------------------------------------------------
86030fdc8d8SChris Lattner 
8616611103cSGreg Clayton CommandObjectBreakpointCommand::CommandObjectBreakpointCommand (CommandInterpreter &interpreter) :
862a7015092SGreg Clayton     CommandObjectMultiword (interpreter,
863a7015092SGreg Clayton                             "command",
86430fdc8d8SChris Lattner                             "A set of commands for adding, removing and examining bits of code to be executed when the breakpoint is hit (breakpoint 'commmands').",
86530fdc8d8SChris Lattner                             "command <sub-command> [<sub-command-options>] <breakpoint-id>")
86630fdc8d8SChris Lattner {
86730fdc8d8SChris Lattner     bool status;
868a7015092SGreg Clayton     CommandObjectSP add_command_object (new CommandObjectBreakpointCommandAdd (interpreter));
86993e0f19fSCaroline Tice     CommandObjectSP delete_command_object (new CommandObjectBreakpointCommandDelete (interpreter));
870a7015092SGreg Clayton     CommandObjectSP list_command_object (new CommandObjectBreakpointCommandList (interpreter));
87130fdc8d8SChris Lattner 
87230fdc8d8SChris Lattner     add_command_object->SetCommandName ("breakpoint command add");
87393e0f19fSCaroline Tice     delete_command_object->SetCommandName ("breakpoint command delete");
87430fdc8d8SChris Lattner     list_command_object->SetCommandName ("breakpoint command list");
87530fdc8d8SChris Lattner 
876a7015092SGreg Clayton     status = LoadSubCommand ("add",    add_command_object);
87793e0f19fSCaroline Tice     status = LoadSubCommand ("delete", delete_command_object);
878a7015092SGreg Clayton     status = LoadSubCommand ("list",   list_command_object);
87930fdc8d8SChris Lattner }
88030fdc8d8SChris Lattner 
88130fdc8d8SChris Lattner CommandObjectBreakpointCommand::~CommandObjectBreakpointCommand ()
88230fdc8d8SChris Lattner {
88330fdc8d8SChris Lattner }
88430fdc8d8SChris Lattner 
88530fdc8d8SChris Lattner 
886