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 
10*93a64300SDaniel Malea #include "lldb/lldb-python.h"
11*93a64300SDaniel Malea 
1230fdc8d8SChris Lattner // C Includes
1330fdc8d8SChris Lattner // C++ Includes
1430fdc8d8SChris Lattner 
1530fdc8d8SChris Lattner 
1630fdc8d8SChris Lattner #include "CommandObjectBreakpointCommand.h"
1730fdc8d8SChris Lattner #include "CommandObjectBreakpoint.h"
1830fdc8d8SChris Lattner 
1930fdc8d8SChris Lattner #include "lldb/Interpreter/CommandInterpreter.h"
2030fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h"
2130fdc8d8SChris Lattner #include "lldb/Target/Target.h"
2230fdc8d8SChris Lattner #include "lldb/Target/Thread.h"
2330fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointIDList.h"
2430fdc8d8SChris Lattner #include "lldb/Breakpoint/Breakpoint.h"
2530fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointLocation.h"
2630fdc8d8SChris Lattner #include "lldb/Breakpoint/StoppointCallbackContext.h"
2730fdc8d8SChris Lattner #include "lldb/Core/State.h"
2830fdc8d8SChris Lattner 
2930fdc8d8SChris Lattner using namespace lldb;
3030fdc8d8SChris Lattner using namespace lldb_private;
3130fdc8d8SChris Lattner 
3230fdc8d8SChris Lattner //-------------------------------------------------------------------------
3330fdc8d8SChris Lattner // CommandObjectBreakpointCommandAdd
3430fdc8d8SChris Lattner //-------------------------------------------------------------------------
3530fdc8d8SChris Lattner 
3630fdc8d8SChris Lattner 
375a988416SJim Ingham class CommandObjectBreakpointCommandAdd : public CommandObjectParsed
385a988416SJim Ingham {
395a988416SJim Ingham public:
405a988416SJim Ingham 
415a988416SJim Ingham     CommandObjectBreakpointCommandAdd (CommandInterpreter &interpreter) :
425a988416SJim Ingham         CommandObjectParsed (interpreter,
43a7015092SGreg Clayton                              "add",
44e3d26315SCaroline Tice                              "Add a set of commands to a breakpoint, to be executed whenever the breakpoint is hit.",
45eb0103f2SGreg Clayton                              NULL),
46eb0103f2SGreg Clayton         m_options (interpreter)
4730fdc8d8SChris Lattner     {
4830fdc8d8SChris Lattner         SetHelpLong (
4930fdc8d8SChris Lattner "\nGeneral information about entering breakpoint commands\n\
5030fdc8d8SChris Lattner ------------------------------------------------------\n\
5130fdc8d8SChris Lattner \n\
5213b1213dSGreg Clayton This command will cause you to be prompted to enter the command or set of\n\
5313b1213dSGreg Clayton commands you wish to be executed when the specified breakpoint is hit. You\n\
5413b1213dSGreg Clayton will be told to enter your command(s), and will see a '> 'prompt. Because\n\
5513b1213dSGreg Clayton you can enter one or many commands to be executed when a breakpoint is hit,\n\
5613b1213dSGreg Clayton you will continue to be prompted after each new-line that you enter, until you\n\
5713b1213dSGreg Clayton enter the word 'DONE', which will cause the commands you have entered to be\n\
5813b1213dSGreg Clayton stored with the breakpoint and executed when the breakpoint is hit.\n\
5930fdc8d8SChris Lattner \n\
6013b1213dSGreg Clayton Syntax checking is not necessarily done when breakpoint commands are entered.\n\
6113b1213dSGreg Clayton An improperly written breakpoint command will attempt to get executed when the\n\
6213b1213dSGreg Clayton breakpoint gets hit, and usually silently fail.  If your breakpoint command does\n\
6313b1213dSGreg Clayton not appear to be getting executed, go back and check your syntax.\n\
6430fdc8d8SChris Lattner \n\
6530fdc8d8SChris Lattner Special information about PYTHON breakpoint commands\n\
6630fdc8d8SChris Lattner ----------------------------------------------------\n\
6730fdc8d8SChris Lattner \n\
6813b1213dSGreg Clayton You may enter either one line of Python, multiple lines of Python (including\n\
6913b1213dSGreg Clayton function definitions), or specify a Python function in a module that has already,\n\
7013b1213dSGreg Clayton or will be imported.  If you enter a single line of Python, that will be passed\n\
7113b1213dSGreg Clayton to the Python interpreter 'as is' when the breakpoint gets hit.  If you enter\n\
7213b1213dSGreg Clayton function definitions, they will be passed to the Python interpreter as soon as\n\
7313b1213dSGreg Clayton you finish entering the breakpoint command, and they can be called later (don't\n\
7413b1213dSGreg Clayton forget to add calls to them, if you want them called when the breakpoint is\n\
7513b1213dSGreg Clayton hit).  If you enter multiple lines of Python that are not function definitions,\n\
7613b1213dSGreg Clayton they will be collected into a new, automatically generated Python function, and\n\
7713b1213dSGreg Clayton a call to the newly generated function will be attached to the breakpoint.\n\
78c9efdbb0SJim Ingham \n\
79c9efdbb0SJim Ingham \n\
8013b1213dSGreg Clayton This auto-generated function is passed in three arguments:\n\
81c9efdbb0SJim Ingham \n\
8213b1213dSGreg Clayton     frame:  a lldb.SBFrame object for the frame which hit breakpoint.\n\
8313b1213dSGreg Clayton     bp_loc: a lldb.SBBreakpointLocation object that represents the breakpoint\n\
8413b1213dSGreg Clayton             location that was hit.\n\
8513b1213dSGreg Clayton     dict:   the python session dictionary hit.\n\
8613b1213dSGreg Clayton \n\
8713b1213dSGreg Clayton When specifying a python function with the --python-function option, you need\n\
8813b1213dSGreg Clayton to supply the function name prepended by the module name. So if you import a\n\
8913b1213dSGreg Clayton module named 'myutils' that contains a 'breakpoint_callback' function, you would\n\
9013b1213dSGreg Clayton specify the option as:\n\
9113b1213dSGreg Clayton \n\
9213b1213dSGreg Clayton     --python-function myutils.breakpoint_callback\n\
9313b1213dSGreg Clayton \n\
9413b1213dSGreg Clayton The function itself must have the following prototype:\n\
9513b1213dSGreg Clayton \n\
9613b1213dSGreg Clayton def breakpoint_callback(frame, bp_loc, dict):\n\
9713b1213dSGreg Clayton   # Your code goes here\n\
9813b1213dSGreg Clayton \n\
9913b1213dSGreg Clayton The arguments are the same as the 3 auto generation function arguments listed\n\
10013b1213dSGreg Clayton above. Note that the global variable 'lldb.frame' will NOT be setup when this\n\
10113b1213dSGreg Clayton function is called, so be sure to use the 'frame' argument. The 'frame' argument\n\
10213b1213dSGreg Clayton can get you to the thread (frame.GetThread()), the thread can get you to the\n\
10313b1213dSGreg Clayton process (thread.GetProcess()), and the process can get you back to the target\n\
10413b1213dSGreg Clayton (process.GetTarget()).\n\
10513b1213dSGreg Clayton \n\
10613b1213dSGreg Clayton Important Note: Because loose Python code gets collected into functions, if you\n\
10713b1213dSGreg Clayton want to access global variables in the 'loose' code, you need to specify that\n\
10813b1213dSGreg Clayton they are global, using the 'global' keyword.  Be sure to use correct Python\n\
10913b1213dSGreg Clayton syntax, including indentation, when entering Python breakpoint commands.\n\
110c9efdbb0SJim Ingham \n\
111c9efdbb0SJim Ingham As a third option, you can pass the name of an already existing Python function\n\
112c9efdbb0SJim Ingham and that function will be attached to the breakpoint. It will get passed the\n\
113c9efdbb0SJim Ingham frame and bp_loc arguments mentioned above.\n\
11430fdc8d8SChris Lattner \n\
11530fdc8d8SChris Lattner Example Python one-line breakpoint command:\n\
11630fdc8d8SChris Lattner \n\
117e16c50a1SJim Ingham (lldb) breakpoint command add -s python 1\n\
11830fdc8d8SChris Lattner Enter your Python command(s). Type 'DONE' to end.\n\
11930fdc8d8SChris Lattner > print \"Hit this breakpoint!\"\n\
12030fdc8d8SChris Lattner > DONE\n\
12130fdc8d8SChris Lattner \n\
1223495f25aSJohnny Chen As a convenience, this also works for a short Python one-liner:\n\
123e16c50a1SJim Ingham (lldb) breakpoint command add -s python 1 -o \"import time; print time.asctime()\"\n\
1243495f25aSJohnny Chen (lldb) run\n\
1253495f25aSJohnny Chen Launching '.../a.out'  (x86_64)\n\
1263495f25aSJohnny Chen (lldb) Fri Sep 10 12:17:45 2010\n\
1273495f25aSJohnny Chen Process 21778 Stopped\n\
1283495f25aSJohnny 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\
1293495f25aSJohnny Chen   36   	\n\
1303495f25aSJohnny Chen   37   	int c(int val)\n\
1313495f25aSJohnny Chen   38   	{\n\
1323495f25aSJohnny Chen   39 ->	    return val + 3;\n\
1333495f25aSJohnny Chen   40   	}\n\
1343495f25aSJohnny Chen   41   	\n\
1353495f25aSJohnny Chen   42   	int main (int argc, char const *argv[])\n\
1363495f25aSJohnny Chen (lldb)\n\
1373495f25aSJohnny Chen \n\
13830fdc8d8SChris Lattner Example multiple line Python breakpoint command, using function definition:\n\
13930fdc8d8SChris Lattner \n\
140e16c50a1SJim Ingham (lldb) breakpoint command add -s python 1\n\
14130fdc8d8SChris Lattner Enter your Python command(s). Type 'DONE' to end.\n\
14230fdc8d8SChris Lattner > def breakpoint_output (bp_no):\n\
14330fdc8d8SChris Lattner >     out_string = \"Hit breakpoint number \" + repr (bp_no)\n\
14430fdc8d8SChris Lattner >     print out_string\n\
14530fdc8d8SChris Lattner >     return True\n\
14630fdc8d8SChris Lattner > breakpoint_output (1)\n\
14730fdc8d8SChris Lattner > DONE\n\
14830fdc8d8SChris Lattner \n\
14930fdc8d8SChris Lattner \n\
15030fdc8d8SChris Lattner Example multiple line Python breakpoint command, using 'loose' Python:\n\
15130fdc8d8SChris Lattner \n\
152e16c50a1SJim Ingham (lldb) breakpoint command add -s p 1\n\
15330fdc8d8SChris Lattner Enter your Python command(s). Type 'DONE' to end.\n\
15430fdc8d8SChris Lattner > global bp_count\n\
15530fdc8d8SChris Lattner > bp_count = bp_count + 1\n\
15630fdc8d8SChris Lattner > print \"Hit this breakpoint \" + repr(bp_count) + \" times!\"\n\
15730fdc8d8SChris Lattner > DONE\n\
15830fdc8d8SChris Lattner \n\
15930fdc8d8SChris Lattner In this case, since there is a reference to a global variable,\n\
16030fdc8d8SChris Lattner 'bp_count', you will also need to make sure 'bp_count' exists and is\n\
16130fdc8d8SChris Lattner initialized:\n\
16230fdc8d8SChris Lattner \n\
16330fdc8d8SChris Lattner (lldb) script\n\
16430fdc8d8SChris Lattner >>> bp_count = 0\n\
16530fdc8d8SChris Lattner >>> quit()\n\
16630fdc8d8SChris Lattner \n\
16730fdc8d8SChris Lattner (lldb)\n\
16830fdc8d8SChris Lattner \n\
169650b9268SCaroline Tice \n\
17013b1213dSGreg Clayton Final Note:  If you get a warning that no breakpoint command was generated, but\n\
17113b1213dSGreg Clayton you did not get any syntax errors, you probably forgot to add a call to your\n\
17213b1213dSGreg Clayton functions.\n\
173650b9268SCaroline Tice \n\
174e3d26315SCaroline Tice Special information about debugger command breakpoint commands\n\
175e3d26315SCaroline Tice --------------------------------------------------------------\n\
17630fdc8d8SChris Lattner \n\
17713b1213dSGreg Clayton You may enter any debugger command, exactly as you would at the debugger prompt.\n\
17813b1213dSGreg Clayton You may enter as many debugger commands as you like, but do NOT enter more than\n\
17913b1213dSGreg Clayton one command per line.\n" );
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;
186405fe67fSCaroline Tice         bp_id_arg.arg_repetition = eArgRepeatPlain;
187405fe67fSCaroline Tice 
188405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
189405fe67fSCaroline Tice         arg.push_back (bp_id_arg);
190405fe67fSCaroline Tice 
191405fe67fSCaroline Tice         // Push the data for the first argument into the m_arguments vector.
192405fe67fSCaroline Tice         m_arguments.push_back (arg);
19330fdc8d8SChris Lattner     }
19430fdc8d8SChris Lattner 
1955a988416SJim Ingham     virtual
1965a988416SJim Ingham     ~CommandObjectBreakpointCommandAdd () {}
1975a988416SJim Ingham 
1985a988416SJim Ingham     virtual Options *
1995a988416SJim Ingham     GetOptions ()
2005a988416SJim Ingham     {
2015a988416SJim Ingham         return &m_options;
2025a988416SJim Ingham     }
2035a988416SJim Ingham 
2045a988416SJim Ingham     void
2055a988416SJim Ingham     CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
2065a988416SJim Ingham                                              CommandReturnObject &result)
2075a988416SJim Ingham     {
2085a988416SJim Ingham         InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
2095a988416SJim Ingham         std::auto_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
2105a988416SJim Ingham         if (reader_sp && data_ap.get())
2115a988416SJim Ingham         {
2125a988416SJim Ingham             BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
2135a988416SJim Ingham             bp_options->SetCallback (BreakpointOptionsCallbackFunction, baton_sp);
2145a988416SJim Ingham 
2155a988416SJim Ingham             Error err (reader_sp->Initialize (CommandObjectBreakpointCommandAdd::GenerateBreakpointCommandCallback,
2165a988416SJim Ingham                                               bp_options,                   // baton
2175a988416SJim Ingham                                               eInputReaderGranularityLine,  // token size, to pass to callback function
2185a988416SJim Ingham                                               "DONE",                       // end token
2195a988416SJim Ingham                                               "> ",                         // prompt
2205a988416SJim Ingham                                               true));                       // echo input
2215a988416SJim Ingham             if (err.Success())
2225a988416SJim Ingham             {
2235a988416SJim Ingham                 m_interpreter.GetDebugger().PushInputReader (reader_sp);
2245a988416SJim Ingham                 result.SetStatus (eReturnStatusSuccessFinishNoResult);
2255a988416SJim Ingham             }
2265a988416SJim Ingham             else
2275a988416SJim Ingham             {
2285a988416SJim Ingham                 result.AppendError (err.AsCString());
2295a988416SJim Ingham                 result.SetStatus (eReturnStatusFailed);
2305a988416SJim Ingham             }
2315a988416SJim Ingham         }
2325a988416SJim Ingham         else
2335a988416SJim Ingham         {
2345a988416SJim Ingham             result.AppendError("out of memory");
2355a988416SJim Ingham             result.SetStatus (eReturnStatusFailed);
2365a988416SJim Ingham         }
2375a988416SJim Ingham 
2385a988416SJim Ingham     }
2395a988416SJim Ingham 
2405a988416SJim Ingham     /// Set a one-liner as the callback for the breakpoint.
2415a988416SJim Ingham     void
2425a988416SJim Ingham     SetBreakpointCommandCallback (BreakpointOptions *bp_options,
2435a988416SJim Ingham                                   const char *oneliner)
2445a988416SJim Ingham     {
2455a988416SJim Ingham         std::auto_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
2465a988416SJim Ingham 
2475a988416SJim Ingham         // It's necessary to set both user_source and script_source to the oneliner.
2485a988416SJim Ingham         // The former is used to generate callback description (as in breakpoint command list)
2495a988416SJim Ingham         // while the latter is used for Python to interpret during the actual callback.
2505a988416SJim Ingham         data_ap->user_source.AppendString (oneliner);
2515a988416SJim Ingham         data_ap->script_source.assign (oneliner);
2525a988416SJim Ingham         data_ap->stop_on_error = m_options.m_stop_on_error;
2535a988416SJim Ingham 
2545a988416SJim Ingham         BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
2555a988416SJim Ingham         bp_options->SetCallback (BreakpointOptionsCallbackFunction, baton_sp);
2565a988416SJim Ingham 
2575a988416SJim Ingham         return;
2585a988416SJim Ingham     }
2595a988416SJim Ingham 
2605a988416SJim Ingham     static size_t
2615a988416SJim Ingham     GenerateBreakpointCommandCallback (void *baton,
2625a988416SJim Ingham                                        InputReader &reader,
2635a988416SJim Ingham                                        lldb::InputReaderAction notification,
2645a988416SJim Ingham                                        const char *bytes,
2655a988416SJim Ingham                                        size_t bytes_len)
2665a988416SJim Ingham     {
2675a988416SJim Ingham         StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
2685a988416SJim Ingham         bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
2695a988416SJim Ingham 
2705a988416SJim Ingham         switch (notification)
2715a988416SJim Ingham         {
2725a988416SJim Ingham         case eInputReaderActivate:
2735a988416SJim Ingham             if (!batch_mode)
2745a988416SJim Ingham             {
2755a988416SJim Ingham                 out_stream->Printf ("%s\n", g_reader_instructions);
2765a988416SJim Ingham                 if (reader.GetPrompt())
2775a988416SJim Ingham                     out_stream->Printf ("%s", reader.GetPrompt());
2785a988416SJim Ingham                 out_stream->Flush();
2795a988416SJim Ingham             }
2805a988416SJim Ingham             break;
2815a988416SJim Ingham 
2825a988416SJim Ingham         case eInputReaderDeactivate:
2835a988416SJim Ingham             break;
2845a988416SJim Ingham 
2855a988416SJim Ingham         case eInputReaderReactivate:
2865a988416SJim Ingham             if (reader.GetPrompt() && !batch_mode)
2875a988416SJim Ingham             {
2885a988416SJim Ingham                 out_stream->Printf ("%s", reader.GetPrompt());
2895a988416SJim Ingham                 out_stream->Flush();
2905a988416SJim Ingham             }
2915a988416SJim Ingham             break;
2925a988416SJim Ingham 
2935a988416SJim Ingham         case eInputReaderAsynchronousOutputWritten:
2945a988416SJim Ingham             break;
2955a988416SJim Ingham 
2965a988416SJim Ingham         case eInputReaderGotToken:
2975a988416SJim Ingham             if (bytes && bytes_len && baton)
2985a988416SJim Ingham             {
2995a988416SJim Ingham                 BreakpointOptions *bp_options = (BreakpointOptions *) baton;
3005a988416SJim Ingham                 if (bp_options)
3015a988416SJim Ingham                 {
3025a988416SJim Ingham                     Baton *bp_options_baton = bp_options->GetBaton();
3035a988416SJim Ingham                     if (bp_options_baton)
3045a988416SJim Ingham                         ((BreakpointOptions::CommandData *)bp_options_baton->m_data)->user_source.AppendString (bytes, bytes_len);
3055a988416SJim Ingham                 }
3065a988416SJim Ingham             }
3075a988416SJim Ingham             if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
3085a988416SJim Ingham             {
3095a988416SJim Ingham                 out_stream->Printf ("%s", reader.GetPrompt());
3105a988416SJim Ingham                 out_stream->Flush();
3115a988416SJim Ingham             }
3125a988416SJim Ingham             break;
3135a988416SJim Ingham 
3145a988416SJim Ingham         case eInputReaderInterrupt:
3155a988416SJim Ingham             {
3165a988416SJim Ingham                 // Finish, and cancel the breakpoint command.
3175a988416SJim Ingham                 reader.SetIsDone (true);
3185a988416SJim Ingham                 BreakpointOptions *bp_options = (BreakpointOptions *) baton;
3195a988416SJim Ingham                 if (bp_options)
3205a988416SJim Ingham                 {
3215a988416SJim Ingham                     Baton *bp_options_baton = bp_options->GetBaton ();
3225a988416SJim Ingham                     if (bp_options_baton)
3235a988416SJim Ingham                     {
3245a988416SJim Ingham                         ((BreakpointOptions::CommandData *) bp_options_baton->m_data)->user_source.Clear();
3255a988416SJim Ingham                         ((BreakpointOptions::CommandData *) bp_options_baton->m_data)->script_source.clear();
3265a988416SJim Ingham                     }
3275a988416SJim Ingham                 }
3285a988416SJim Ingham                 if (!batch_mode)
3295a988416SJim Ingham                 {
3305a988416SJim Ingham                     out_stream->Printf ("Warning: No command attached to breakpoint.\n");
3315a988416SJim Ingham                     out_stream->Flush();
3325a988416SJim Ingham                 }
3335a988416SJim Ingham             }
3345a988416SJim Ingham             break;
3355a988416SJim Ingham 
3365a988416SJim Ingham         case eInputReaderEndOfFile:
3375a988416SJim Ingham             reader.SetIsDone (true);
3385a988416SJim Ingham             break;
3395a988416SJim Ingham 
3405a988416SJim Ingham         case eInputReaderDone:
3415a988416SJim Ingham             break;
3425a988416SJim Ingham         }
3435a988416SJim Ingham 
3445a988416SJim Ingham         return bytes_len;
3455a988416SJim Ingham     }
3465a988416SJim Ingham 
3475a988416SJim Ingham     static bool
3485a988416SJim Ingham     BreakpointOptionsCallbackFunction (void *baton,
3495a988416SJim Ingham                                        StoppointCallbackContext *context,
3505a988416SJim Ingham                                        lldb::user_id_t break_id,
3515a988416SJim Ingham                                        lldb::user_id_t break_loc_id)
3525a988416SJim Ingham     {
3535a988416SJim Ingham         bool ret_value = true;
3545a988416SJim Ingham         if (baton == NULL)
3555a988416SJim Ingham             return true;
3565a988416SJim Ingham 
3575a988416SJim Ingham 
3585a988416SJim Ingham         BreakpointOptions::CommandData *data = (BreakpointOptions::CommandData *) baton;
3595a988416SJim Ingham         StringList &commands = data->user_source;
3605a988416SJim Ingham 
3615a988416SJim Ingham         if (commands.GetSize() > 0)
3625a988416SJim Ingham         {
3635a988416SJim Ingham             ExecutionContext exe_ctx (context->exe_ctx_ref);
3645a988416SJim Ingham             Target *target = exe_ctx.GetTargetPtr();
3655a988416SJim Ingham             if (target)
3665a988416SJim Ingham             {
3675a988416SJim Ingham                 CommandReturnObject result;
3685a988416SJim Ingham                 Debugger &debugger = target->GetDebugger();
3695a988416SJim Ingham                 // Rig up the results secondary output stream to the debugger's, so the output will come out synchronously
3705a988416SJim Ingham                 // if the debugger is set up that way.
3715a988416SJim Ingham 
3725a988416SJim Ingham                 StreamSP output_stream (debugger.GetAsyncOutputStream());
3735a988416SJim Ingham                 StreamSP error_stream (debugger.GetAsyncErrorStream());
3745a988416SJim Ingham                 result.SetImmediateOutputStream (output_stream);
3755a988416SJim Ingham                 result.SetImmediateErrorStream (error_stream);
3765a988416SJim Ingham 
3775a988416SJim Ingham                 bool stop_on_continue = true;
3785a988416SJim Ingham                 bool echo_commands    = false;
3795a988416SJim Ingham                 bool print_results    = true;
3805a988416SJim Ingham 
3815a988416SJim Ingham                 debugger.GetCommandInterpreter().HandleCommands (commands,
3825a988416SJim Ingham                                                                  &exe_ctx,
3835a988416SJim Ingham                                                                  stop_on_continue,
3845a988416SJim Ingham                                                                  data->stop_on_error,
3855a988416SJim Ingham                                                                  echo_commands,
3865a988416SJim Ingham                                                                  print_results,
3875a988416SJim Ingham                                                                  eLazyBoolNo,
3885a988416SJim Ingham                                                                  result);
3895a988416SJim Ingham                 result.GetImmediateOutputStream()->Flush();
3905a988416SJim Ingham                 result.GetImmediateErrorStream()->Flush();
3915a988416SJim Ingham            }
3925a988416SJim Ingham         }
3935a988416SJim Ingham         return ret_value;
3945a988416SJim Ingham     }
3955a988416SJim Ingham 
3965a988416SJim Ingham     class CommandOptions : public Options
3975a988416SJim Ingham     {
3985a988416SJim Ingham     public:
3995a988416SJim Ingham 
4005a988416SJim Ingham         CommandOptions (CommandInterpreter &interpreter) :
4015a988416SJim Ingham             Options (interpreter),
4025a988416SJim Ingham             m_use_commands (false),
4035a988416SJim Ingham             m_use_script_language (false),
4045a988416SJim Ingham             m_script_language (eScriptLanguageNone),
4055a988416SJim Ingham             m_use_one_liner (false),
4065a988416SJim Ingham             m_one_liner(),
4075a988416SJim Ingham             m_function_name()
40830fdc8d8SChris Lattner         {
40930fdc8d8SChris Lattner         }
41030fdc8d8SChris Lattner 
4115a988416SJim Ingham         virtual
4125a988416SJim Ingham         ~CommandOptions () {}
4135a988416SJim Ingham 
4145a988416SJim Ingham         virtual Error
4155a988416SJim Ingham         SetOptionValue (uint32_t option_idx, const char *option_arg)
4165a988416SJim Ingham         {
4175a988416SJim Ingham             Error error;
4183bcdfc0eSGreg Clayton             const int short_option = m_getopt_table[option_idx].val;
4195a988416SJim Ingham 
4205a988416SJim Ingham             switch (short_option)
4215a988416SJim Ingham             {
4225a988416SJim Ingham             case 'o':
4235a988416SJim Ingham                 m_use_one_liner = true;
4245a988416SJim Ingham                 m_one_liner = option_arg;
4255a988416SJim Ingham                 break;
4265a988416SJim Ingham 
4275a988416SJim Ingham             case 's':
4285a988416SJim Ingham                 m_script_language = (lldb::ScriptLanguage) Args::StringToOptionEnum (option_arg,
4295a988416SJim Ingham                                                                                      g_option_table[option_idx].enum_values,
4305a988416SJim Ingham                                                                                      eScriptLanguageNone,
4315a988416SJim Ingham                                                                                      error);
4325a988416SJim Ingham 
4335a988416SJim Ingham                 if (m_script_language == eScriptLanguagePython || m_script_language == eScriptLanguageDefault)
4345a988416SJim Ingham                 {
4355a988416SJim Ingham                     m_use_script_language = true;
4365a988416SJim Ingham                 }
4375a988416SJim Ingham                 else
4385a988416SJim Ingham                 {
4395a988416SJim Ingham                     m_use_script_language = false;
4405a988416SJim Ingham                 }
4415a988416SJim Ingham                 break;
4425a988416SJim Ingham 
4435a988416SJim Ingham             case 'e':
4445a988416SJim Ingham                 {
4455a988416SJim Ingham                     bool success = false;
4465a988416SJim Ingham                     m_stop_on_error = Args::StringToBoolean(option_arg, false, &success);
4475a988416SJim Ingham                     if (!success)
4485a988416SJim Ingham                         error.SetErrorStringWithFormat("invalid value for stop-on-error: \"%s\"", option_arg);
4495a988416SJim Ingham                 }
4505a988416SJim Ingham                 break;
4515a988416SJim Ingham 
4525a988416SJim Ingham             case 'F':
4535a988416SJim Ingham                 {
4545a988416SJim Ingham                     m_use_one_liner = false;
4555a988416SJim Ingham                     m_use_script_language = true;
4565a988416SJim Ingham                     m_function_name.assign(option_arg);
4575a988416SJim Ingham                 }
4585a988416SJim Ingham                 break;
4595a988416SJim Ingham 
4605a988416SJim Ingham             default:
4615a988416SJim Ingham                 break;
4625a988416SJim Ingham             }
4635a988416SJim Ingham             return error;
4645a988416SJim Ingham         }
4655a988416SJim Ingham         void
4665a988416SJim Ingham         OptionParsingStarting ()
4675a988416SJim Ingham         {
4685a988416SJim Ingham             m_use_commands = true;
4695a988416SJim Ingham             m_use_script_language = false;
4705a988416SJim Ingham             m_script_language = eScriptLanguageNone;
4715a988416SJim Ingham 
4725a988416SJim Ingham             m_use_one_liner = false;
4735a988416SJim Ingham             m_stop_on_error = true;
4745a988416SJim Ingham             m_one_liner.clear();
4755a988416SJim Ingham             m_function_name.clear();
4765a988416SJim Ingham         }
4775a988416SJim Ingham 
4785a988416SJim Ingham         const OptionDefinition*
4795a988416SJim Ingham         GetDefinitions ()
4805a988416SJim Ingham         {
4815a988416SJim Ingham             return g_option_table;
4825a988416SJim Ingham         }
4835a988416SJim Ingham 
4845a988416SJim Ingham         // Options table: Required for subclasses of Options.
4855a988416SJim Ingham 
4865a988416SJim Ingham         static OptionDefinition g_option_table[];
4875a988416SJim Ingham 
4885a988416SJim Ingham         // Instance variables to hold the values for command options.
4895a988416SJim Ingham 
4905a988416SJim Ingham         bool m_use_commands;
4915a988416SJim Ingham         bool m_use_script_language;
4925a988416SJim Ingham         lldb::ScriptLanguage m_script_language;
4935a988416SJim Ingham 
4945a988416SJim Ingham         // Instance variables to hold the values for one_liner options.
4955a988416SJim Ingham         bool m_use_one_liner;
4965a988416SJim Ingham         std::string m_one_liner;
4975a988416SJim Ingham         bool m_stop_on_error;
4985a988416SJim Ingham         std::string m_function_name;
4995a988416SJim Ingham     };
5005a988416SJim Ingham 
5015a988416SJim Ingham protected:
5025a988416SJim Ingham     virtual bool
5035a988416SJim Ingham     DoExecute (Args& command, CommandReturnObject &result)
50430fdc8d8SChris Lattner     {
505a7015092SGreg Clayton         Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
50630fdc8d8SChris Lattner 
50730fdc8d8SChris Lattner         if (target == NULL)
50830fdc8d8SChris Lattner         {
50930fdc8d8SChris Lattner             result.AppendError ("There is not a current executable; there are no breakpoints to which to add commands");
51030fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
51130fdc8d8SChris Lattner             return false;
51230fdc8d8SChris Lattner         }
51330fdc8d8SChris Lattner 
51430fdc8d8SChris Lattner         const BreakpointList &breakpoints = target->GetBreakpointList();
51530fdc8d8SChris Lattner         size_t num_breakpoints = breakpoints.GetSize();
51630fdc8d8SChris Lattner 
51730fdc8d8SChris Lattner         if (num_breakpoints == 0)
51830fdc8d8SChris Lattner         {
51930fdc8d8SChris Lattner             result.AppendError ("No breakpoints exist to have commands added");
52030fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
52130fdc8d8SChris Lattner             return false;
52230fdc8d8SChris Lattner         }
52330fdc8d8SChris Lattner 
5248d4a8010SEnrico Granata         if (m_options.m_use_script_language == false && m_options.m_function_name.size())
5258d4a8010SEnrico Granata         {
5268d4a8010SEnrico Granata             result.AppendError ("need to enable scripting to have a function run as a breakpoint command");
5278d4a8010SEnrico Granata             result.SetStatus (eReturnStatusFailed);
5288d4a8010SEnrico Granata             return false;
5298d4a8010SEnrico Granata         }
5308d4a8010SEnrico Granata 
53130fdc8d8SChris Lattner         BreakpointIDList valid_bp_ids;
53230fdc8d8SChris Lattner         CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
53330fdc8d8SChris Lattner 
53430fdc8d8SChris Lattner         if (result.Succeeded())
53530fdc8d8SChris Lattner         {
536c982c768SGreg Clayton             const size_t count = valid_bp_ids.GetSize();
537c982c768SGreg Clayton             for (size_t i = 0; i < count; ++i)
53830fdc8d8SChris Lattner             {
53930fdc8d8SChris Lattner                 BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
54030fdc8d8SChris Lattner                 if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
54130fdc8d8SChris Lattner                 {
54230fdc8d8SChris Lattner                     Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
54339d7d4f0SJohnny Chen                     BreakpointOptions *bp_options = NULL;
54439d7d4f0SJohnny Chen                     if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID)
54539d7d4f0SJohnny Chen                     {
54639d7d4f0SJohnny Chen                         // This breakpoint does not have an associated location.
54739d7d4f0SJohnny Chen                         bp_options = bp->GetOptions();
54839d7d4f0SJohnny Chen                     }
54939d7d4f0SJohnny Chen                     else
55030fdc8d8SChris Lattner                     {
55130fdc8d8SChris Lattner                         BreakpointLocationSP bp_loc_sp(bp->FindLocationByID (cur_bp_id.GetLocationID()));
55239d7d4f0SJohnny Chen                         // This breakpoint does have an associated location.
55339d7d4f0SJohnny Chen                         // Get its breakpoint options.
55430fdc8d8SChris Lattner                         if (bp_loc_sp)
55539d7d4f0SJohnny Chen                             bp_options = bp_loc_sp->GetLocationOptions();
55639d7d4f0SJohnny Chen                     }
55739d7d4f0SJohnny Chen 
558e16c50a1SJim Ingham                     // Skip this breakpoint if bp_options is not good.
55939d7d4f0SJohnny Chen                     if (bp_options == NULL) continue;
56039d7d4f0SJohnny Chen 
56139d7d4f0SJohnny Chen                     // If we are using script language, get the script interpreter
56239d7d4f0SJohnny Chen                     // in order to set or collect command callback.  Otherwise, call
56339d7d4f0SJohnny Chen                     // the methods associated with this object.
56430fdc8d8SChris Lattner                     if (m_options.m_use_script_language)
56530fdc8d8SChris Lattner                     {
56639d7d4f0SJohnny Chen                         // Special handling for one-liner specified inline.
56739d7d4f0SJohnny Chen                         if (m_options.m_use_one_liner)
5688d4a8010SEnrico Granata                         {
569a7015092SGreg Clayton                             m_interpreter.GetScriptInterpreter()->SetBreakpointCommandCallback (bp_options,
57039d7d4f0SJohnny Chen                                                                                                 m_options.m_one_liner.c_str());
5718d4a8010SEnrico Granata                         }
5728d4a8010SEnrico Granata                         // Special handling for using a Python function by name
5738d4a8010SEnrico Granata                         // instead of extending the breakpoint callback data structures, we just automatize
5748d4a8010SEnrico Granata                         // what the user would do manually: make their breakpoint command be a function call
5758d4a8010SEnrico Granata                         else if (m_options.m_function_name.size())
5768d4a8010SEnrico Granata                         {
5778d4a8010SEnrico Granata                             std::string oneliner(m_options.m_function_name);
57840d55710SEnrico Granata                             oneliner += "(frame, bp_loc, internal_dict)";
5798d4a8010SEnrico Granata                             m_interpreter.GetScriptInterpreter()->SetBreakpointCommandCallback (bp_options,
5808d4a8010SEnrico Granata                                                                                                 oneliner.c_str());
5818d4a8010SEnrico Granata                         }
58294de55d5SJohnny Chen                         else
5838d4a8010SEnrico Granata                         {
584a7015092SGreg Clayton                             m_interpreter.GetScriptInterpreter()->CollectDataForBreakpointCommandCallback (bp_options,
58530fdc8d8SChris Lattner                                                                                                            result);
58630fdc8d8SChris Lattner                         }
5878d4a8010SEnrico Granata                     }
58830fdc8d8SChris Lattner                     else
58930fdc8d8SChris Lattner                     {
59039d7d4f0SJohnny Chen                         // Special handling for one-liner specified inline.
59139d7d4f0SJohnny Chen                         if (m_options.m_use_one_liner)
592a7015092SGreg Clayton                             SetBreakpointCommandCallback (bp_options,
59339d7d4f0SJohnny Chen                                                           m_options.m_one_liner.c_str());
59439d7d4f0SJohnny Chen                         else
595a7015092SGreg Clayton                             CollectDataForBreakpointCommandCallback (bp_options,
596b132097bSGreg Clayton                                                                      result);
59730fdc8d8SChris Lattner                     }
59830fdc8d8SChris Lattner                 }
59930fdc8d8SChris Lattner             }
60030fdc8d8SChris Lattner         }
60130fdc8d8SChris Lattner 
60230fdc8d8SChris Lattner         return result.Succeeded();
60330fdc8d8SChris Lattner     }
60430fdc8d8SChris Lattner 
6055a988416SJim Ingham private:
6065a988416SJim Ingham     CommandOptions m_options;
6075a988416SJim Ingham     static const char *g_reader_instructions;
6085a988416SJim Ingham 
6095a988416SJim Ingham };
6105a988416SJim Ingham 
6115a988416SJim Ingham const char *
6125a988416SJim Ingham CommandObjectBreakpointCommandAdd::g_reader_instructions = "Enter your debugger command(s).  Type 'DONE' to end.";
6135a988416SJim Ingham 
6145a988416SJim Ingham // FIXME: "script-type" needs to have its contents determined dynamically, so somebody can add a new scripting
6155a988416SJim Ingham // language to lldb and have it pickable here without having to change this enumeration by hand and rebuild lldb proper.
6165a988416SJim Ingham 
6175a988416SJim Ingham static OptionEnumValueElement
6185a988416SJim Ingham g_script_option_enumeration[4] =
61930fdc8d8SChris Lattner {
6205a988416SJim Ingham     { eScriptLanguageNone,    "command",         "Commands are in the lldb command interpreter language"},
6215a988416SJim Ingham     { eScriptLanguagePython,  "python",          "Commands are in the Python language."},
6225a988416SJim Ingham     { eSortOrderByName,       "default-script",  "Commands are in the default scripting language."},
6235a988416SJim Ingham     { 0,                      NULL,              NULL }
6245a988416SJim Ingham };
62530fdc8d8SChris Lattner 
6265a988416SJim Ingham OptionDefinition
6275a988416SJim Ingham CommandObjectBreakpointCommandAdd::CommandOptions::g_option_table[] =
62830fdc8d8SChris Lattner {
629bc6e85cbSFilipe Cabecinhas     { LLDB_OPT_SET_1, false, "one-liner", 'o', required_argument, NULL, 0, eArgTypeOneLiner,
6305a988416SJim Ingham         "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
63130fdc8d8SChris Lattner 
632bc6e85cbSFilipe Cabecinhas     { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', required_argument, NULL, 0, eArgTypeBoolean,
6335a988416SJim Ingham         "Specify whether breakpoint command execution should terminate on error." },
63430fdc8d8SChris Lattner 
635bc6e85cbSFilipe Cabecinhas     { LLDB_OPT_SET_ALL,   false, "script-type",     's', required_argument, g_script_option_enumeration, 0, eArgTypeNone,
6365a988416SJim Ingham         "Specify the language for the commands - if none is specified, the lldb command interpreter will be used."},
63730fdc8d8SChris Lattner 
638bc6e85cbSFilipe Cabecinhas     { LLDB_OPT_SET_2,   false, "python-function",     'F', required_argument, NULL, 0, eArgTypePythonFunction,
6395a988416SJim Ingham         "Give the name of a Python function to run as command for this breakpoint. Be sure to give a module name if appropriate."},
64039d7d4f0SJohnny Chen 
6415a988416SJim Ingham     { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
6425a988416SJim Ingham };
64330fdc8d8SChris Lattner 
64430fdc8d8SChris Lattner //-------------------------------------------------------------------------
64593e0f19fSCaroline Tice // CommandObjectBreakpointCommandDelete
64630fdc8d8SChris Lattner //-------------------------------------------------------------------------
64730fdc8d8SChris Lattner 
6485a988416SJim Ingham class CommandObjectBreakpointCommandDelete : public CommandObjectParsed
6495a988416SJim Ingham {
6505a988416SJim Ingham public:
6515a988416SJim Ingham     CommandObjectBreakpointCommandDelete (CommandInterpreter &interpreter) :
6525a988416SJim Ingham         CommandObjectParsed (interpreter,
65393e0f19fSCaroline Tice                              "delete",
65493e0f19fSCaroline Tice                              "Delete the set of commands from a breakpoint.",
655405fe67fSCaroline Tice                              NULL)
65630fdc8d8SChris Lattner     {
657405fe67fSCaroline Tice         CommandArgumentEntry arg;
658405fe67fSCaroline Tice         CommandArgumentData bp_id_arg;
659405fe67fSCaroline Tice 
660405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
661405fe67fSCaroline Tice         bp_id_arg.arg_type = eArgTypeBreakpointID;
662405fe67fSCaroline Tice         bp_id_arg.arg_repetition = eArgRepeatPlain;
663405fe67fSCaroline Tice 
664405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
665405fe67fSCaroline Tice         arg.push_back (bp_id_arg);
666405fe67fSCaroline Tice 
667405fe67fSCaroline Tice         // Push the data for the first argument into the m_arguments vector.
668405fe67fSCaroline Tice         m_arguments.push_back (arg);
66930fdc8d8SChris Lattner     }
67030fdc8d8SChris Lattner 
67130fdc8d8SChris Lattner 
6725a988416SJim Ingham     virtual
6735a988416SJim Ingham     ~CommandObjectBreakpointCommandDelete () {}
6745a988416SJim Ingham 
6755a988416SJim Ingham protected:
6765a988416SJim Ingham     virtual bool
6775a988416SJim Ingham     DoExecute (Args& command, CommandReturnObject &result)
67830fdc8d8SChris Lattner     {
679a7015092SGreg Clayton         Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
68030fdc8d8SChris Lattner 
68130fdc8d8SChris Lattner         if (target == NULL)
68230fdc8d8SChris Lattner         {
68393e0f19fSCaroline Tice             result.AppendError ("There is not a current executable; there are no breakpoints from which to delete commands");
68430fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
68530fdc8d8SChris Lattner             return false;
68630fdc8d8SChris Lattner         }
68730fdc8d8SChris Lattner 
68830fdc8d8SChris Lattner         const BreakpointList &breakpoints = target->GetBreakpointList();
68930fdc8d8SChris Lattner         size_t num_breakpoints = breakpoints.GetSize();
69030fdc8d8SChris Lattner 
69130fdc8d8SChris Lattner         if (num_breakpoints == 0)
69230fdc8d8SChris Lattner         {
69393e0f19fSCaroline Tice             result.AppendError ("No breakpoints exist to have commands deleted");
69430fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
69530fdc8d8SChris Lattner             return false;
69630fdc8d8SChris Lattner         }
69730fdc8d8SChris Lattner 
69830fdc8d8SChris Lattner         if (command.GetArgumentCount() == 0)
69930fdc8d8SChris Lattner         {
70093e0f19fSCaroline Tice             result.AppendError ("No breakpoint specified from which to delete the commands");
70130fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
70230fdc8d8SChris Lattner             return false;
70330fdc8d8SChris Lattner         }
70430fdc8d8SChris Lattner 
70530fdc8d8SChris Lattner         BreakpointIDList valid_bp_ids;
70630fdc8d8SChris Lattner         CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
70730fdc8d8SChris Lattner 
70830fdc8d8SChris Lattner         if (result.Succeeded())
70930fdc8d8SChris Lattner         {
710c982c768SGreg Clayton             const size_t count = valid_bp_ids.GetSize();
711c982c768SGreg Clayton             for (size_t i = 0; i < count; ++i)
71230fdc8d8SChris Lattner             {
71330fdc8d8SChris Lattner                 BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
71430fdc8d8SChris Lattner                 if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
71530fdc8d8SChris Lattner                 {
71630fdc8d8SChris Lattner                     Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
71730fdc8d8SChris Lattner                     if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
71830fdc8d8SChris Lattner                     {
71930fdc8d8SChris Lattner                         BreakpointLocationSP bp_loc_sp (bp->FindLocationByID (cur_bp_id.GetLocationID()));
72030fdc8d8SChris Lattner                         if (bp_loc_sp)
72130fdc8d8SChris Lattner                             bp_loc_sp->ClearCallback();
72230fdc8d8SChris Lattner                         else
72330fdc8d8SChris Lattner                         {
72430fdc8d8SChris Lattner                             result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
72530fdc8d8SChris Lattner                                                          cur_bp_id.GetBreakpointID(),
72630fdc8d8SChris Lattner                                                          cur_bp_id.GetLocationID());
72730fdc8d8SChris Lattner                             result.SetStatus (eReturnStatusFailed);
72830fdc8d8SChris Lattner                             return false;
72930fdc8d8SChris Lattner                         }
73030fdc8d8SChris Lattner                     }
73130fdc8d8SChris Lattner                     else
73230fdc8d8SChris Lattner                     {
73330fdc8d8SChris Lattner                         bp->ClearCallback();
73430fdc8d8SChris Lattner                     }
73530fdc8d8SChris Lattner                 }
73630fdc8d8SChris Lattner             }
73730fdc8d8SChris Lattner         }
73830fdc8d8SChris Lattner         return result.Succeeded();
73930fdc8d8SChris Lattner     }
7405a988416SJim Ingham };
74130fdc8d8SChris Lattner 
74230fdc8d8SChris Lattner //-------------------------------------------------------------------------
74330fdc8d8SChris Lattner // CommandObjectBreakpointCommandList
74430fdc8d8SChris Lattner //-------------------------------------------------------------------------
74530fdc8d8SChris Lattner 
7465a988416SJim Ingham class CommandObjectBreakpointCommandList : public CommandObjectParsed
7475a988416SJim Ingham {
7485a988416SJim Ingham public:
7495a988416SJim Ingham     CommandObjectBreakpointCommandList (CommandInterpreter &interpreter) :
7505a988416SJim Ingham         CommandObjectParsed (interpreter,
751a7015092SGreg Clayton                              "list",
75230fdc8d8SChris Lattner                              "List the script or set of commands to be executed when the breakpoint is hit.",
753405fe67fSCaroline Tice                               NULL)
75430fdc8d8SChris Lattner     {
755405fe67fSCaroline Tice         CommandArgumentEntry arg;
756405fe67fSCaroline Tice         CommandArgumentData bp_id_arg;
757405fe67fSCaroline Tice 
758405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
759405fe67fSCaroline Tice         bp_id_arg.arg_type = eArgTypeBreakpointID;
760405fe67fSCaroline Tice         bp_id_arg.arg_repetition = eArgRepeatPlain;
761405fe67fSCaroline Tice 
762405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
763405fe67fSCaroline Tice         arg.push_back (bp_id_arg);
764405fe67fSCaroline Tice 
765405fe67fSCaroline Tice         // Push the data for the first argument into the m_arguments vector.
766405fe67fSCaroline Tice         m_arguments.push_back (arg);
76730fdc8d8SChris Lattner     }
76830fdc8d8SChris Lattner 
7695a988416SJim Ingham     virtual
7705a988416SJim Ingham     ~CommandObjectBreakpointCommandList () {}
77130fdc8d8SChris Lattner 
7725a988416SJim Ingham protected:
7735a988416SJim Ingham     virtual bool
7745a988416SJim Ingham     DoExecute (Args& command,
7755a988416SJim Ingham              CommandReturnObject &result)
77630fdc8d8SChris Lattner     {
777a7015092SGreg Clayton         Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
77830fdc8d8SChris Lattner 
77930fdc8d8SChris Lattner         if (target == NULL)
78030fdc8d8SChris Lattner         {
78130fdc8d8SChris Lattner             result.AppendError ("There is not a current executable; there are no breakpoints for which to list commands");
78230fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
78330fdc8d8SChris Lattner             return false;
78430fdc8d8SChris Lattner         }
78530fdc8d8SChris Lattner 
78630fdc8d8SChris Lattner         const BreakpointList &breakpoints = target->GetBreakpointList();
78730fdc8d8SChris Lattner         size_t num_breakpoints = breakpoints.GetSize();
78830fdc8d8SChris Lattner 
78930fdc8d8SChris Lattner         if (num_breakpoints == 0)
79030fdc8d8SChris Lattner         {
79130fdc8d8SChris Lattner             result.AppendError ("No breakpoints exist for which to list commands");
79230fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
79330fdc8d8SChris Lattner             return false;
79430fdc8d8SChris Lattner         }
79530fdc8d8SChris Lattner 
79630fdc8d8SChris Lattner         if (command.GetArgumentCount() == 0)
79730fdc8d8SChris Lattner         {
79830fdc8d8SChris Lattner             result.AppendError ("No breakpoint specified for which to list the commands");
79930fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
80030fdc8d8SChris Lattner             return false;
80130fdc8d8SChris Lattner         }
80230fdc8d8SChris Lattner 
80330fdc8d8SChris Lattner         BreakpointIDList valid_bp_ids;
80430fdc8d8SChris Lattner         CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
80530fdc8d8SChris Lattner 
80630fdc8d8SChris Lattner         if (result.Succeeded())
80730fdc8d8SChris Lattner         {
808c982c768SGreg Clayton             const size_t count = valid_bp_ids.GetSize();
809c982c768SGreg Clayton             for (size_t i = 0; i < count; ++i)
81030fdc8d8SChris Lattner             {
81130fdc8d8SChris Lattner                 BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
81230fdc8d8SChris Lattner                 if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
81330fdc8d8SChris Lattner                 {
81430fdc8d8SChris Lattner                     Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
81530fdc8d8SChris Lattner 
81630fdc8d8SChris Lattner                     if (bp)
81730fdc8d8SChris Lattner                     {
8181b54c88cSJim Ingham                         const BreakpointOptions *bp_options = NULL;
81930fdc8d8SChris Lattner                         if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
82030fdc8d8SChris Lattner                         {
82130fdc8d8SChris Lattner                             BreakpointLocationSP bp_loc_sp(bp->FindLocationByID (cur_bp_id.GetLocationID()));
82230fdc8d8SChris Lattner                             if (bp_loc_sp)
82305407f6bSJim Ingham                                 bp_options = bp_loc_sp->GetOptionsNoCreate();
82430fdc8d8SChris Lattner                             else
82530fdc8d8SChris Lattner                             {
82630fdc8d8SChris Lattner                                 result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
82730fdc8d8SChris Lattner                                                              cur_bp_id.GetBreakpointID(),
82830fdc8d8SChris Lattner                                                              cur_bp_id.GetLocationID());
82930fdc8d8SChris Lattner                                 result.SetStatus (eReturnStatusFailed);
83030fdc8d8SChris Lattner                                 return false;
83130fdc8d8SChris Lattner                             }
83230fdc8d8SChris Lattner                         }
83330fdc8d8SChris Lattner                         else
83430fdc8d8SChris Lattner                         {
83530fdc8d8SChris Lattner                             bp_options = bp->GetOptions();
83630fdc8d8SChris Lattner                         }
83730fdc8d8SChris Lattner 
83830fdc8d8SChris Lattner                         if (bp_options)
83930fdc8d8SChris Lattner                         {
84030fdc8d8SChris Lattner                             StreamString id_str;
841e16c50a1SJim Ingham                             BreakpointID::GetCanonicalReference (&id_str,
842e16c50a1SJim Ingham                                                                  cur_bp_id.GetBreakpointID(),
843e16c50a1SJim Ingham                                                                  cur_bp_id.GetLocationID());
8441b54c88cSJim Ingham                             const Baton *baton = bp_options->GetBaton();
84530fdc8d8SChris Lattner                             if (baton)
84630fdc8d8SChris Lattner                             {
84730fdc8d8SChris Lattner                                 result.GetOutputStream().Printf ("Breakpoint %s:\n", id_str.GetData());
84830fdc8d8SChris Lattner                                 result.GetOutputStream().IndentMore ();
84930fdc8d8SChris Lattner                                 baton->GetDescription(&result.GetOutputStream(), eDescriptionLevelFull);
85030fdc8d8SChris Lattner                                 result.GetOutputStream().IndentLess ();
85130fdc8d8SChris Lattner                             }
85230fdc8d8SChris Lattner                             else
85330fdc8d8SChris Lattner                             {
854e16c50a1SJim Ingham                                 result.AppendMessageWithFormat ("Breakpoint %s does not have an associated command.\n",
855e16c50a1SJim Ingham                                                                 id_str.GetData());
85630fdc8d8SChris Lattner                             }
85730fdc8d8SChris Lattner                         }
85830fdc8d8SChris Lattner                         result.SetStatus (eReturnStatusSuccessFinishResult);
85930fdc8d8SChris Lattner                     }
86030fdc8d8SChris Lattner                     else
86130fdc8d8SChris Lattner                     {
86230fdc8d8SChris Lattner                         result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n", cur_bp_id.GetBreakpointID());
86330fdc8d8SChris Lattner                         result.SetStatus (eReturnStatusFailed);
86430fdc8d8SChris Lattner                     }
86530fdc8d8SChris Lattner 
86630fdc8d8SChris Lattner                 }
86730fdc8d8SChris Lattner             }
86830fdc8d8SChris Lattner         }
86930fdc8d8SChris Lattner 
87030fdc8d8SChris Lattner         return result.Succeeded();
87130fdc8d8SChris Lattner     }
8725a988416SJim Ingham };
87330fdc8d8SChris Lattner 
87430fdc8d8SChris Lattner //-------------------------------------------------------------------------
87530fdc8d8SChris Lattner // CommandObjectBreakpointCommand
87630fdc8d8SChris Lattner //-------------------------------------------------------------------------
87730fdc8d8SChris Lattner 
8786611103cSGreg Clayton CommandObjectBreakpointCommand::CommandObjectBreakpointCommand (CommandInterpreter &interpreter) :
879a7015092SGreg Clayton     CommandObjectMultiword (interpreter,
880a7015092SGreg Clayton                             "command",
88130fdc8d8SChris Lattner                             "A set of commands for adding, removing and examining bits of code to be executed when the breakpoint is hit (breakpoint 'commmands').",
88230fdc8d8SChris Lattner                             "command <sub-command> [<sub-command-options>] <breakpoint-id>")
88330fdc8d8SChris Lattner {
884a7015092SGreg Clayton     CommandObjectSP add_command_object (new CommandObjectBreakpointCommandAdd (interpreter));
88593e0f19fSCaroline Tice     CommandObjectSP delete_command_object (new CommandObjectBreakpointCommandDelete (interpreter));
886a7015092SGreg Clayton     CommandObjectSP list_command_object (new CommandObjectBreakpointCommandList (interpreter));
88730fdc8d8SChris Lattner 
88830fdc8d8SChris Lattner     add_command_object->SetCommandName ("breakpoint command add");
88993e0f19fSCaroline Tice     delete_command_object->SetCommandName ("breakpoint command delete");
89030fdc8d8SChris Lattner     list_command_object->SetCommandName ("breakpoint command list");
89130fdc8d8SChris Lattner 
89223f59509SGreg Clayton     LoadSubCommand ("add",    add_command_object);
89323f59509SGreg Clayton     LoadSubCommand ("delete", delete_command_object);
89423f59509SGreg Clayton     LoadSubCommand ("list",   list_command_object);
89530fdc8d8SChris Lattner }
89630fdc8d8SChris Lattner 
89730fdc8d8SChris Lattner CommandObjectBreakpointCommand::~CommandObjectBreakpointCommand ()
89830fdc8d8SChris Lattner {
89930fdc8d8SChris Lattner }
90030fdc8d8SChris Lattner 
90130fdc8d8SChris Lattner 
902