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"
26*b5059accSCaroline Tice #include "lldb/Core/StreamAsynchronousIO.h"
2730fdc8d8SChris Lattner 
2830fdc8d8SChris Lattner using namespace lldb;
2930fdc8d8SChris Lattner using namespace lldb_private;
3030fdc8d8SChris Lattner 
3130fdc8d8SChris Lattner //-------------------------------------------------------------------------
3230fdc8d8SChris Lattner // CommandObjectBreakpointCommandAdd::CommandOptions
3330fdc8d8SChris Lattner //-------------------------------------------------------------------------
3430fdc8d8SChris Lattner 
35eb0103f2SGreg Clayton CommandObjectBreakpointCommandAdd::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
36eb0103f2SGreg Clayton     Options (interpreter),
3739d7d4f0SJohnny Chen     m_use_commands (false),
3839d7d4f0SJohnny Chen     m_use_script_language (false),
3939d7d4f0SJohnny Chen     m_script_language (eScriptLanguageNone),
4039d7d4f0SJohnny Chen     m_use_one_liner (false),
4139d7d4f0SJohnny Chen     m_one_liner()
4230fdc8d8SChris Lattner {
4330fdc8d8SChris Lattner }
4430fdc8d8SChris Lattner 
4530fdc8d8SChris Lattner CommandObjectBreakpointCommandAdd::CommandOptions::~CommandOptions ()
4630fdc8d8SChris Lattner {
4730fdc8d8SChris Lattner }
4830fdc8d8SChris Lattner 
49e16c50a1SJim Ingham // FIXME: "script-type" needs to have its contents determined dynamically, so somebody can add a new scripting
50e16c50a1SJim Ingham // language to lldb and have it pickable here without having to change this enumeration by hand and rebuild lldb proper.
51e16c50a1SJim Ingham 
52e0d378b3SGreg Clayton static OptionEnumValueElement
53e16c50a1SJim Ingham g_script_option_enumeration[4] =
54e16c50a1SJim Ingham {
55e16c50a1SJim Ingham     { eScriptLanguageNone,    "command",         "Commands are in the lldb command interpreter language"},
56e16c50a1SJim Ingham     { eScriptLanguagePython,  "python",          "Commands are in the Python language."},
57e16c50a1SJim Ingham     { eSortOrderByName,       "default-script",  "Commands are in the default scripting language."},
58e16c50a1SJim Ingham     { 0,                      NULL,              NULL }
59e16c50a1SJim Ingham };
60e16c50a1SJim Ingham 
61e0d378b3SGreg Clayton OptionDefinition
6230fdc8d8SChris Lattner CommandObjectBreakpointCommandAdd::CommandOptions::g_option_table[] =
6330fdc8d8SChris Lattner {
64deaab222SCaroline Tice     { LLDB_OPT_SET_ALL, false, "one-liner", 'o', required_argument, NULL, NULL, eArgTypeOneLiner,
65867b185dSCaroline Tice         "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
6639d7d4f0SJohnny Chen 
67e16c50a1SJim Ingham     { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', required_argument, NULL, NULL, eArgTypeBoolean,
68e16c50a1SJim Ingham         "Specify whether breakpoint command execution should terminate on error." },
6930fdc8d8SChris Lattner 
70e16c50a1SJim Ingham     { LLDB_OPT_SET_ALL,   false, "script-type",     's', required_argument, g_script_option_enumeration, NULL, eArgTypeNone,
71e16c50a1SJim Ingham         "Specify the language for the commands - if none is specified, the lldb command interpreter will be used."},
7230fdc8d8SChris Lattner 
73deaab222SCaroline Tice     { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
7430fdc8d8SChris Lattner };
7530fdc8d8SChris Lattner 
76e0d378b3SGreg Clayton const OptionDefinition*
7730fdc8d8SChris Lattner CommandObjectBreakpointCommandAdd::CommandOptions::GetDefinitions ()
7830fdc8d8SChris Lattner {
7930fdc8d8SChris Lattner     return g_option_table;
8030fdc8d8SChris Lattner }
8130fdc8d8SChris Lattner 
8230fdc8d8SChris Lattner 
8330fdc8d8SChris Lattner Error
8430fdc8d8SChris Lattner CommandObjectBreakpointCommandAdd::CommandOptions::SetOptionValue
8530fdc8d8SChris Lattner (
86f6b8b581SGreg Clayton     uint32_t option_idx,
8730fdc8d8SChris Lattner     const char *option_arg
8830fdc8d8SChris Lattner )
8930fdc8d8SChris Lattner {
9030fdc8d8SChris Lattner     Error error;
9130fdc8d8SChris Lattner     char short_option = (char) m_getopt_table[option_idx].val;
9230fdc8d8SChris Lattner 
9330fdc8d8SChris Lattner     switch (short_option)
9430fdc8d8SChris Lattner       {
9539d7d4f0SJohnny Chen       case 'o':
9639d7d4f0SJohnny Chen           m_use_one_liner = true;
9739d7d4f0SJohnny Chen           m_one_liner = option_arg;
9839d7d4f0SJohnny Chen           break;
99e16c50a1SJim Ingham           break;
10030fdc8d8SChris Lattner       case 's':
101e16c50a1SJim Ingham           {
102e16c50a1SJim Ingham               bool found_one = false;
103e16c50a1SJim Ingham               m_script_language = (lldb::ScriptLanguage) Args::StringToOptionEnum (option_arg,
104e16c50a1SJim Ingham                                                                          g_option_table[option_idx].enum_values,
105e16c50a1SJim Ingham                                                                          eScriptLanguageNone,
106e16c50a1SJim Ingham                                                                          &found_one);
107e16c50a1SJim Ingham               if (!found_one)
108e16c50a1SJim Ingham                   error.SetErrorStringWithFormat("Invalid enumeration value '%s' for option '%c'.\n",
109e16c50a1SJim Ingham                                                  option_arg,
110e16c50a1SJim Ingham                                                  short_option);
111e16c50a1SJim Ingham 
112e16c50a1SJim Ingham               if (m_script_language == eScriptLanguagePython || m_script_language == eScriptLanguageDefault)
113e16c50a1SJim Ingham               {
11430fdc8d8SChris Lattner                   m_use_commands = false;
11530fdc8d8SChris Lattner                   m_use_script_language = true;
116e16c50a1SJim Ingham               }
117e16c50a1SJim Ingham               else
118e16c50a1SJim Ingham               {
11930fdc8d8SChris Lattner                   m_use_commands = true;
12030fdc8d8SChris Lattner                   m_use_script_language = false;
121e16c50a1SJim Ingham               }
122e16c50a1SJim Ingham           }
123e16c50a1SJim Ingham       break;
124e16c50a1SJim Ingham       case 'e':
125e16c50a1SJim Ingham           bool success_ptr;
126e16c50a1SJim Ingham           m_stop_on_error = Args::StringToBoolean(option_arg, false, &success_ptr);
127e16c50a1SJim Ingham           if (!success_ptr)
128e16c50a1SJim Ingham               error.SetErrorStringWithFormat("Invalid value for stop-on-error: \"%s\".\n", option_arg);
12930fdc8d8SChris Lattner           break;
13030fdc8d8SChris Lattner       default:
13130fdc8d8SChris Lattner           break;
13230fdc8d8SChris Lattner       }
13330fdc8d8SChris Lattner     return error;
13430fdc8d8SChris Lattner }
13530fdc8d8SChris Lattner 
13630fdc8d8SChris Lattner void
137f6b8b581SGreg Clayton CommandObjectBreakpointCommandAdd::CommandOptions::OptionParsingStarting ()
13830fdc8d8SChris Lattner {
139e16c50a1SJim Ingham     m_use_commands = true;
14030fdc8d8SChris Lattner     m_use_script_language = false;
14130fdc8d8SChris Lattner     m_script_language = eScriptLanguageNone;
14239d7d4f0SJohnny Chen 
14339d7d4f0SJohnny Chen     m_use_one_liner = false;
144e16c50a1SJim Ingham     m_stop_on_error = true;
14539d7d4f0SJohnny Chen     m_one_liner.clear();
14630fdc8d8SChris Lattner }
14730fdc8d8SChris Lattner 
14830fdc8d8SChris Lattner //-------------------------------------------------------------------------
14930fdc8d8SChris Lattner // CommandObjectBreakpointCommandAdd
15030fdc8d8SChris Lattner //-------------------------------------------------------------------------
15130fdc8d8SChris Lattner 
15230fdc8d8SChris Lattner 
153a7015092SGreg Clayton CommandObjectBreakpointCommandAdd::CommandObjectBreakpointCommandAdd (CommandInterpreter &interpreter) :
154a7015092SGreg Clayton     CommandObject (interpreter,
155a7015092SGreg Clayton                    "add",
156e3d26315SCaroline Tice                    "Add a set of commands to a breakpoint, to be executed whenever the breakpoint is hit.",
157eb0103f2SGreg Clayton                    NULL),
158eb0103f2SGreg Clayton     m_options (interpreter)
15930fdc8d8SChris Lattner {
16030fdc8d8SChris Lattner     SetHelpLong (
16130fdc8d8SChris Lattner "\nGeneral information about entering breakpoint commands \n\
16230fdc8d8SChris Lattner ------------------------------------------------------ \n\
16330fdc8d8SChris Lattner  \n\
16430fdc8d8SChris Lattner This command will cause you to be prompted to enter the command or set \n\
16530fdc8d8SChris Lattner of commands you wish to be executed when the specified breakpoint is \n\
16630fdc8d8SChris Lattner hit.  You will be told to enter your command(s), and will see a '> ' \n\
16730fdc8d8SChris Lattner prompt. Because you can enter one or many commands to be executed when \n\
16830fdc8d8SChris Lattner a breakpoint is hit, you will continue to be prompted after each \n\
16930fdc8d8SChris Lattner new-line that you enter, until you enter the word 'DONE', which will \n\
17030fdc8d8SChris Lattner cause the commands you have entered to be stored with the breakpoint \n\
17130fdc8d8SChris Lattner and executed when the breakpoint is hit. \n\
17230fdc8d8SChris Lattner  \n\
17330fdc8d8SChris Lattner Syntax checking is not necessarily done when breakpoint commands are \n\
17430fdc8d8SChris Lattner entered.  An improperly written breakpoint command will attempt to get \n\
17530fdc8d8SChris Lattner executed when the breakpoint gets hit, and usually silently fail.  If \n\
17630fdc8d8SChris Lattner your breakpoint command does not appear to be getting executed, go \n\
17730fdc8d8SChris Lattner back and check your syntax. \n\
17830fdc8d8SChris Lattner  \n\
17930fdc8d8SChris Lattner  \n\
18030fdc8d8SChris Lattner Special information about PYTHON breakpoint commands \n\
18130fdc8d8SChris Lattner ---------------------------------------------------- \n\
18230fdc8d8SChris Lattner  \n\
18330fdc8d8SChris Lattner You may enter either one line of Python or multiple lines of Python \n\
18430fdc8d8SChris Lattner (including defining whole functions, if desired).  If you enter a \n\
18530fdc8d8SChris Lattner single line of Python, that will be passed to the Python interpreter \n\
18630fdc8d8SChris Lattner 'as is' when the breakpoint gets hit.  If you enter function \n\
18730fdc8d8SChris Lattner definitions, they will be passed to the Python interpreter as soon as \n\
18830fdc8d8SChris Lattner you finish entering the breakpoint command, and they can be called \n\
18930fdc8d8SChris Lattner later (don't forget to add calls to them, if you want them called when \n\
19030fdc8d8SChris Lattner the breakpoint is hit).  If you enter multiple lines of Python that \n\
19130fdc8d8SChris Lattner are not function definitions, they will be collected into a new, \n\
19230fdc8d8SChris Lattner automatically generated Python function, and a call to the newly \n\
19330fdc8d8SChris Lattner generated function will be attached to the breakpoint.  Important \n\
19430fdc8d8SChris Lattner Note: Because loose Python code gets collected into functions, if you \n\
19530fdc8d8SChris Lattner want to access global variables in the 'loose' code, you need to \n\
19630fdc8d8SChris Lattner specify that they are global, using the 'global' keyword.  Be sure to \n\
19730fdc8d8SChris Lattner use correct Python syntax, including indentation, when entering Python \n\
19830fdc8d8SChris Lattner breakpoint commands. \n\
19930fdc8d8SChris Lattner  \n\
20030fdc8d8SChris Lattner Example Python one-line breakpoint command: \n\
20130fdc8d8SChris Lattner  \n\
202e16c50a1SJim Ingham (lldb) breakpoint command add -s python 1 \n\
20330fdc8d8SChris Lattner Enter your Python command(s). Type 'DONE' to end. \n\
20430fdc8d8SChris Lattner > print \"Hit this breakpoint!\" \n\
20530fdc8d8SChris Lattner > DONE \n\
20630fdc8d8SChris Lattner  \n\
2073495f25aSJohnny Chen As a convenience, this also works for a short Python one-liner: \n\
208e16c50a1SJim Ingham (lldb) breakpoint command add -s python 1 -o \"import time; print time.asctime()\" \n\
2093495f25aSJohnny Chen (lldb) run \n\
2103495f25aSJohnny Chen Launching '.../a.out'  (x86_64) \n\
2113495f25aSJohnny Chen (lldb) Fri Sep 10 12:17:45 2010 \n\
2123495f25aSJohnny Chen Process 21778 Stopped \n\
2133495f25aSJohnny 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\
2143495f25aSJohnny Chen   36   	\n\
2153495f25aSJohnny Chen   37   	int c(int val)\n\
2163495f25aSJohnny Chen   38   	{\n\
2173495f25aSJohnny Chen   39 ->	    return val + 3;\n\
2183495f25aSJohnny Chen   40   	}\n\
2193495f25aSJohnny Chen   41   	\n\
2203495f25aSJohnny Chen   42   	int main (int argc, char const *argv[])\n\
2213495f25aSJohnny Chen (lldb) \n\
2223495f25aSJohnny Chen  \n\
22330fdc8d8SChris Lattner Example multiple line Python breakpoint command, using function definition: \n\
22430fdc8d8SChris Lattner  \n\
225e16c50a1SJim Ingham (lldb) breakpoint command add -s python 1 \n\
22630fdc8d8SChris Lattner Enter your Python command(s). Type 'DONE' to end. \n\
22730fdc8d8SChris Lattner > def breakpoint_output (bp_no): \n\
22830fdc8d8SChris Lattner >     out_string = \"Hit breakpoint number \" + repr (bp_no) \n\
22930fdc8d8SChris Lattner >     print out_string \n\
23030fdc8d8SChris Lattner >     return True \n\
23130fdc8d8SChris Lattner > breakpoint_output (1) \n\
23230fdc8d8SChris Lattner > DONE \n\
23330fdc8d8SChris Lattner  \n\
23430fdc8d8SChris Lattner  \n\
23530fdc8d8SChris Lattner Example multiple line Python breakpoint command, using 'loose' Python: \n\
23630fdc8d8SChris Lattner  \n\
237e16c50a1SJim Ingham (lldb) breakpoint command add -s p 1 \n\
23830fdc8d8SChris Lattner Enter your Python command(s). Type 'DONE' to end. \n\
23930fdc8d8SChris Lattner > global bp_count \n\
24030fdc8d8SChris Lattner > bp_count = bp_count + 1 \n\
24130fdc8d8SChris Lattner > print \"Hit this breakpoint \" + repr(bp_count) + \" times!\" \n\
24230fdc8d8SChris Lattner > DONE \n\
24330fdc8d8SChris Lattner  \n\
24430fdc8d8SChris Lattner In this case, since there is a reference to a global variable, \n\
24530fdc8d8SChris Lattner 'bp_count', you will also need to make sure 'bp_count' exists and is \n\
24630fdc8d8SChris Lattner initialized: \n\
24730fdc8d8SChris Lattner  \n\
24830fdc8d8SChris Lattner (lldb) script \n\
24930fdc8d8SChris Lattner >>> bp_count = 0 \n\
25030fdc8d8SChris Lattner >>> quit() \n\
25130fdc8d8SChris Lattner  \n\
25230fdc8d8SChris Lattner (lldb)  \n\
25330fdc8d8SChris Lattner  \n\
254650b9268SCaroline Tice  \n\
255650b9268SCaroline Tice Final Note:  If you get a warning that no breakpoint command was generated, \n\
256650b9268SCaroline Tice but you did not get any syntax errors, you probably forgot to add a call \n\
257650b9268SCaroline Tice to your functions. \n\
258650b9268SCaroline Tice  \n\
259e3d26315SCaroline Tice Special information about debugger command breakpoint commands \n\
260e3d26315SCaroline Tice -------------------------------------------------------------- \n\
26130fdc8d8SChris Lattner  \n\
26230fdc8d8SChris Lattner You may enter any debugger command, exactly as you would at the \n\
26330fdc8d8SChris Lattner debugger prompt.  You may enter as many debugger commands as you like, \n\
26430fdc8d8SChris Lattner but do NOT enter more than one command per line. \n" );
265405fe67fSCaroline Tice 
266405fe67fSCaroline Tice 
267405fe67fSCaroline Tice     CommandArgumentEntry arg;
268405fe67fSCaroline Tice     CommandArgumentData bp_id_arg;
269405fe67fSCaroline Tice 
270405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
271405fe67fSCaroline Tice     bp_id_arg.arg_type = eArgTypeBreakpointID;
272405fe67fSCaroline Tice     bp_id_arg.arg_repetition = eArgRepeatPlain;
273405fe67fSCaroline Tice 
274405fe67fSCaroline Tice     // There is only one variant this argument could be; put it into the argument entry.
275405fe67fSCaroline Tice     arg.push_back (bp_id_arg);
276405fe67fSCaroline Tice 
277405fe67fSCaroline Tice     // Push the data for the first argument into the m_arguments vector.
278405fe67fSCaroline Tice     m_arguments.push_back (arg);
27930fdc8d8SChris Lattner }
28030fdc8d8SChris Lattner 
28130fdc8d8SChris Lattner CommandObjectBreakpointCommandAdd::~CommandObjectBreakpointCommandAdd ()
28230fdc8d8SChris Lattner {
28330fdc8d8SChris Lattner }
28430fdc8d8SChris Lattner 
28530fdc8d8SChris Lattner bool
28630fdc8d8SChris Lattner CommandObjectBreakpointCommandAdd::Execute
28730fdc8d8SChris Lattner (
28830fdc8d8SChris Lattner     Args& command,
28930fdc8d8SChris Lattner     CommandReturnObject &result
29030fdc8d8SChris Lattner )
29130fdc8d8SChris Lattner {
292a7015092SGreg Clayton     Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
29330fdc8d8SChris Lattner 
29430fdc8d8SChris Lattner     if (target == NULL)
29530fdc8d8SChris Lattner     {
29630fdc8d8SChris Lattner         result.AppendError ("There is not a current executable; there are no breakpoints to which to add commands");
29730fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
29830fdc8d8SChris Lattner         return false;
29930fdc8d8SChris Lattner     }
30030fdc8d8SChris Lattner 
30130fdc8d8SChris Lattner     const BreakpointList &breakpoints = target->GetBreakpointList();
30230fdc8d8SChris Lattner     size_t num_breakpoints = breakpoints.GetSize();
30330fdc8d8SChris Lattner 
30430fdc8d8SChris Lattner     if (num_breakpoints == 0)
30530fdc8d8SChris Lattner     {
30630fdc8d8SChris Lattner         result.AppendError ("No breakpoints exist to have commands added");
30730fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
30830fdc8d8SChris Lattner         return false;
30930fdc8d8SChris Lattner     }
31030fdc8d8SChris Lattner 
31130fdc8d8SChris Lattner     BreakpointIDList valid_bp_ids;
31230fdc8d8SChris Lattner     CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
31330fdc8d8SChris Lattner 
31430fdc8d8SChris Lattner     if (result.Succeeded())
31530fdc8d8SChris Lattner     {
316c982c768SGreg Clayton         const size_t count = valid_bp_ids.GetSize();
317c982c768SGreg Clayton         for (size_t i = 0; i < count; ++i)
31830fdc8d8SChris Lattner         {
31930fdc8d8SChris Lattner             BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
32030fdc8d8SChris Lattner             if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
32130fdc8d8SChris Lattner             {
32230fdc8d8SChris Lattner                 Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
32339d7d4f0SJohnny Chen                 BreakpointOptions *bp_options = NULL;
32439d7d4f0SJohnny Chen                 if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID)
32539d7d4f0SJohnny Chen                 {
32639d7d4f0SJohnny Chen                     // This breakpoint does not have an associated location.
32739d7d4f0SJohnny Chen                     bp_options = bp->GetOptions();
32839d7d4f0SJohnny Chen                 }
32939d7d4f0SJohnny Chen                 else
33030fdc8d8SChris Lattner                 {
33130fdc8d8SChris Lattner                     BreakpointLocationSP bp_loc_sp(bp->FindLocationByID (cur_bp_id.GetLocationID()));
33239d7d4f0SJohnny Chen                     // This breakpoint does have an associated location.
33339d7d4f0SJohnny Chen                     // Get its breakpoint options.
33430fdc8d8SChris Lattner                     if (bp_loc_sp)
33539d7d4f0SJohnny Chen                         bp_options = bp_loc_sp->GetLocationOptions();
33639d7d4f0SJohnny Chen                 }
33739d7d4f0SJohnny Chen 
338e16c50a1SJim Ingham                 // Skip this breakpoint if bp_options is not good.
33939d7d4f0SJohnny Chen                 if (bp_options == NULL) continue;
34039d7d4f0SJohnny Chen 
34139d7d4f0SJohnny Chen                 // If we are using script language, get the script interpreter
34239d7d4f0SJohnny Chen                 // in order to set or collect command callback.  Otherwise, call
34339d7d4f0SJohnny Chen                 // the methods associated with this object.
34430fdc8d8SChris Lattner                 if (m_options.m_use_script_language)
34530fdc8d8SChris Lattner                 {
34639d7d4f0SJohnny Chen                     // Special handling for one-liner specified inline.
34739d7d4f0SJohnny Chen                     if (m_options.m_use_one_liner)
348a7015092SGreg Clayton                         m_interpreter.GetScriptInterpreter()->SetBreakpointCommandCallback (bp_options,
34939d7d4f0SJohnny Chen                                                                                             m_options.m_one_liner.c_str());
35094de55d5SJohnny Chen                     else
351a7015092SGreg Clayton                         m_interpreter.GetScriptInterpreter()->CollectDataForBreakpointCommandCallback (bp_options,
35230fdc8d8SChris Lattner                                                                                                        result);
35330fdc8d8SChris Lattner                 }
35430fdc8d8SChris Lattner                 else
35530fdc8d8SChris Lattner                 {
35639d7d4f0SJohnny Chen                     // Special handling for one-liner specified inline.
35739d7d4f0SJohnny Chen                     if (m_options.m_use_one_liner)
358a7015092SGreg Clayton                         SetBreakpointCommandCallback (bp_options,
35939d7d4f0SJohnny Chen                                                       m_options.m_one_liner.c_str());
36039d7d4f0SJohnny Chen                     else
361a7015092SGreg Clayton                         CollectDataForBreakpointCommandCallback (bp_options,
362b132097bSGreg Clayton                                                                  result);
36330fdc8d8SChris Lattner                 }
36430fdc8d8SChris Lattner             }
36530fdc8d8SChris Lattner         }
36630fdc8d8SChris Lattner     }
36730fdc8d8SChris Lattner 
36830fdc8d8SChris Lattner     return result.Succeeded();
36930fdc8d8SChris Lattner }
37030fdc8d8SChris Lattner 
37130fdc8d8SChris Lattner Options *
37230fdc8d8SChris Lattner CommandObjectBreakpointCommandAdd::GetOptions ()
37330fdc8d8SChris Lattner {
37430fdc8d8SChris Lattner     return &m_options;
37530fdc8d8SChris Lattner }
37630fdc8d8SChris Lattner 
37730fdc8d8SChris Lattner const char *g_reader_instructions = "Enter your debugger command(s).  Type 'DONE' to end.";
37830fdc8d8SChris Lattner 
37930fdc8d8SChris Lattner void
38030fdc8d8SChris Lattner CommandObjectBreakpointCommandAdd::CollectDataForBreakpointCommandCallback
38130fdc8d8SChris Lattner (
38230fdc8d8SChris Lattner     BreakpointOptions *bp_options,
38330fdc8d8SChris Lattner     CommandReturnObject &result
38430fdc8d8SChris Lattner )
38530fdc8d8SChris Lattner {
386a7015092SGreg Clayton     InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
38730fdc8d8SChris Lattner     std::auto_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
38830fdc8d8SChris Lattner     if (reader_sp && data_ap.get())
38930fdc8d8SChris Lattner     {
39030fdc8d8SChris Lattner         BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
39130fdc8d8SChris Lattner         bp_options->SetCallback (CommandObjectBreakpointCommand::BreakpointOptionsCallbackFunction, baton_sp);
39230fdc8d8SChris Lattner 
39330fdc8d8SChris Lattner         Error err (reader_sp->Initialize (CommandObjectBreakpointCommandAdd::GenerateBreakpointCommandCallback,
39430fdc8d8SChris Lattner                                           bp_options,                   // baton
39530fdc8d8SChris Lattner                                           eInputReaderGranularityLine,  // token size, to pass to callback function
39630fdc8d8SChris Lattner                                           "DONE",                       // end token
39730fdc8d8SChris Lattner                                           "> ",                         // prompt
39830fdc8d8SChris Lattner                                           true));                       // echo input
39930fdc8d8SChris Lattner         if (err.Success())
40030fdc8d8SChris Lattner         {
401a7015092SGreg Clayton             m_interpreter.GetDebugger().PushInputReader (reader_sp);
40230fdc8d8SChris Lattner             result.SetStatus (eReturnStatusSuccessFinishNoResult);
40330fdc8d8SChris Lattner         }
40430fdc8d8SChris Lattner         else
40530fdc8d8SChris Lattner         {
40630fdc8d8SChris Lattner             result.AppendError (err.AsCString());
40730fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
40830fdc8d8SChris Lattner         }
40930fdc8d8SChris Lattner     }
41030fdc8d8SChris Lattner     else
41130fdc8d8SChris Lattner     {
41230fdc8d8SChris Lattner         result.AppendError("out of memory");
41330fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
41430fdc8d8SChris Lattner     }
41530fdc8d8SChris Lattner 
41630fdc8d8SChris Lattner }
41730fdc8d8SChris Lattner 
4184550154dSJohnny Chen // Set a one-liner as the callback for the breakpoint.
41939d7d4f0SJohnny Chen void
420a7015092SGreg Clayton CommandObjectBreakpointCommandAdd::SetBreakpointCommandCallback (BreakpointOptions *bp_options,
42139d7d4f0SJohnny Chen                                                                  const char *oneliner)
42239d7d4f0SJohnny Chen {
42339d7d4f0SJohnny Chen     std::auto_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
42439d7d4f0SJohnny Chen 
42539d7d4f0SJohnny Chen     // It's necessary to set both user_source and script_source to the oneliner.
42639d7d4f0SJohnny Chen     // The former is used to generate callback description (as in breakpoint command list)
42739d7d4f0SJohnny Chen     // while the latter is used for Python to interpret during the actual callback.
42839d7d4f0SJohnny Chen     data_ap->user_source.AppendString (oneliner);
42939d7d4f0SJohnny Chen     data_ap->script_source.AppendString (oneliner);
430e16c50a1SJim Ingham     data_ap->stop_on_error = m_options.m_stop_on_error;
43139d7d4f0SJohnny Chen 
43239d7d4f0SJohnny Chen     BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
43339d7d4f0SJohnny Chen     bp_options->SetCallback (CommandObjectBreakpointCommand::BreakpointOptionsCallbackFunction, baton_sp);
43439d7d4f0SJohnny Chen 
43539d7d4f0SJohnny Chen     return;
43639d7d4f0SJohnny Chen }
43739d7d4f0SJohnny Chen 
43830fdc8d8SChris Lattner size_t
43930fdc8d8SChris Lattner CommandObjectBreakpointCommandAdd::GenerateBreakpointCommandCallback
44030fdc8d8SChris Lattner (
44130fdc8d8SChris Lattner     void *baton,
4426611103cSGreg Clayton     InputReader &reader,
44330fdc8d8SChris Lattner     lldb::InputReaderAction notification,
44430fdc8d8SChris Lattner     const char *bytes,
44530fdc8d8SChris Lattner     size_t bytes_len
44630fdc8d8SChris Lattner )
44730fdc8d8SChris Lattner {
44851b1e2d2SGreg Clayton     File &out_file = reader.GetDebugger().GetOutputFile();
44930fdc8d8SChris Lattner 
45030fdc8d8SChris Lattner     switch (notification)
45130fdc8d8SChris Lattner     {
45230fdc8d8SChris Lattner     case eInputReaderActivate:
45351b1e2d2SGreg Clayton         out_file.Printf ("%s\n", g_reader_instructions);
4546611103cSGreg Clayton         if (reader.GetPrompt())
45551b1e2d2SGreg Clayton             out_file.Printf ("%s", reader.GetPrompt());
45651b1e2d2SGreg Clayton         out_file.Flush();
45730fdc8d8SChris Lattner         break;
45830fdc8d8SChris Lattner 
45930fdc8d8SChris Lattner     case eInputReaderDeactivate:
46030fdc8d8SChris Lattner         break;
46130fdc8d8SChris Lattner 
46230fdc8d8SChris Lattner     case eInputReaderReactivate:
46351b1e2d2SGreg Clayton         if (reader.GetPrompt())
46404a339a0SCaroline Tice         {
46551b1e2d2SGreg Clayton             out_file.Printf ("%s", reader.GetPrompt());
46651b1e2d2SGreg Clayton             out_file.Flush();
46704a339a0SCaroline Tice         }
46830fdc8d8SChris Lattner         break;
46930fdc8d8SChris Lattner 
470969ed3d1SCaroline Tice     case eInputReaderAsynchronousOutputWritten:
471969ed3d1SCaroline Tice         break;
472969ed3d1SCaroline Tice 
47330fdc8d8SChris Lattner     case eInputReaderGotToken:
47430fdc8d8SChris Lattner         if (bytes && bytes_len && baton)
47530fdc8d8SChris Lattner         {
47630fdc8d8SChris Lattner             BreakpointOptions *bp_options = (BreakpointOptions *) baton;
47730fdc8d8SChris Lattner             if (bp_options)
47830fdc8d8SChris Lattner             {
47930fdc8d8SChris Lattner                 Baton *bp_options_baton = bp_options->GetBaton();
48030fdc8d8SChris Lattner                 if (bp_options_baton)
48130fdc8d8SChris Lattner                     ((BreakpointOptions::CommandData *)bp_options_baton->m_data)->user_source.AppendString (bytes, bytes_len);
48230fdc8d8SChris Lattner             }
48330fdc8d8SChris Lattner         }
48451b1e2d2SGreg Clayton         if (!reader.IsDone() && reader.GetPrompt())
48504a339a0SCaroline Tice         {
48651b1e2d2SGreg Clayton             out_file.Printf ("%s", reader.GetPrompt());
48751b1e2d2SGreg Clayton             out_file.Flush();
48804a339a0SCaroline Tice         }
48930fdc8d8SChris Lattner         break;
49030fdc8d8SChris Lattner 
491efed6131SCaroline Tice     case eInputReaderInterrupt:
492efed6131SCaroline Tice         {
493efed6131SCaroline Tice             // Finish, and cancel the breakpoint command.
494efed6131SCaroline Tice             reader.SetIsDone (true);
495efed6131SCaroline Tice             BreakpointOptions *bp_options = (BreakpointOptions *) baton;
496efed6131SCaroline Tice             if (bp_options)
497efed6131SCaroline Tice             {
498efed6131SCaroline Tice                 Baton *bp_options_baton = bp_options->GetBaton ();
499efed6131SCaroline Tice                 if (bp_options_baton)
500efed6131SCaroline Tice                 {
501efed6131SCaroline Tice                     ((BreakpointOptions::CommandData *) bp_options_baton->m_data)->user_source.Clear();
502efed6131SCaroline Tice                     ((BreakpointOptions::CommandData *) bp_options_baton->m_data)->script_source.Clear();
503efed6131SCaroline Tice                 }
504efed6131SCaroline Tice             }
50551b1e2d2SGreg Clayton             out_file.Printf ("Warning: No command attached to breakpoint.\n");
50651b1e2d2SGreg Clayton             out_file.Flush();
507efed6131SCaroline Tice         }
508efed6131SCaroline Tice         break;
509efed6131SCaroline Tice 
510efed6131SCaroline Tice     case eInputReaderEndOfFile:
511efed6131SCaroline Tice         reader.SetIsDone (true);
512efed6131SCaroline Tice         break;
513efed6131SCaroline Tice 
51430fdc8d8SChris Lattner     case eInputReaderDone:
51530fdc8d8SChris Lattner         break;
51630fdc8d8SChris Lattner     }
51730fdc8d8SChris Lattner 
51830fdc8d8SChris Lattner     return bytes_len;
51930fdc8d8SChris Lattner }
52030fdc8d8SChris Lattner 
52130fdc8d8SChris Lattner 
52230fdc8d8SChris Lattner //-------------------------------------------------------------------------
52330fdc8d8SChris Lattner // CommandObjectBreakpointCommandRemove
52430fdc8d8SChris Lattner //-------------------------------------------------------------------------
52530fdc8d8SChris Lattner 
526a7015092SGreg Clayton CommandObjectBreakpointCommandRemove::CommandObjectBreakpointCommandRemove (CommandInterpreter &interpreter) :
527a7015092SGreg Clayton     CommandObject (interpreter,
528a7015092SGreg Clayton                    "remove",
52930fdc8d8SChris Lattner                    "Remove the set of commands from a breakpoint.",
530405fe67fSCaroline Tice                    NULL)
53130fdc8d8SChris Lattner {
532405fe67fSCaroline Tice     CommandArgumentEntry arg;
533405fe67fSCaroline Tice     CommandArgumentData bp_id_arg;
534405fe67fSCaroline Tice 
535405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
536405fe67fSCaroline Tice     bp_id_arg.arg_type = eArgTypeBreakpointID;
537405fe67fSCaroline Tice     bp_id_arg.arg_repetition = eArgRepeatPlain;
538405fe67fSCaroline Tice 
539405fe67fSCaroline Tice     // There is only one variant this argument could be; put it into the argument entry.
540405fe67fSCaroline Tice     arg.push_back (bp_id_arg);
541405fe67fSCaroline Tice 
542405fe67fSCaroline Tice     // Push the data for the first argument into the m_arguments vector.
543405fe67fSCaroline Tice     m_arguments.push_back (arg);
54430fdc8d8SChris Lattner }
54530fdc8d8SChris Lattner 
54630fdc8d8SChris Lattner CommandObjectBreakpointCommandRemove::~CommandObjectBreakpointCommandRemove ()
54730fdc8d8SChris Lattner {
54830fdc8d8SChris Lattner }
54930fdc8d8SChris Lattner 
55030fdc8d8SChris Lattner bool
5516611103cSGreg Clayton CommandObjectBreakpointCommandRemove::Execute
5526611103cSGreg Clayton (
5536611103cSGreg Clayton     Args& command,
5546611103cSGreg Clayton     CommandReturnObject &result
5556611103cSGreg Clayton )
55630fdc8d8SChris Lattner {
557a7015092SGreg Clayton     Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
55830fdc8d8SChris Lattner 
55930fdc8d8SChris Lattner     if (target == NULL)
56030fdc8d8SChris Lattner     {
56130fdc8d8SChris Lattner         result.AppendError ("There is not a current executable; there are no breakpoints from which to remove commands");
56230fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
56330fdc8d8SChris Lattner         return false;
56430fdc8d8SChris Lattner     }
56530fdc8d8SChris Lattner 
56630fdc8d8SChris Lattner     const BreakpointList &breakpoints = target->GetBreakpointList();
56730fdc8d8SChris Lattner     size_t num_breakpoints = breakpoints.GetSize();
56830fdc8d8SChris Lattner 
56930fdc8d8SChris Lattner     if (num_breakpoints == 0)
57030fdc8d8SChris Lattner     {
57130fdc8d8SChris Lattner         result.AppendError ("No breakpoints exist to have commands removed");
57230fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
57330fdc8d8SChris Lattner         return false;
57430fdc8d8SChris Lattner     }
57530fdc8d8SChris Lattner 
57630fdc8d8SChris Lattner     if (command.GetArgumentCount() == 0)
57730fdc8d8SChris Lattner     {
57830fdc8d8SChris Lattner         result.AppendError ("No breakpoint specified from which to remove the commands");
57930fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
58030fdc8d8SChris Lattner         return false;
58130fdc8d8SChris Lattner     }
58230fdc8d8SChris Lattner 
58330fdc8d8SChris Lattner     BreakpointIDList valid_bp_ids;
58430fdc8d8SChris Lattner     CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
58530fdc8d8SChris Lattner 
58630fdc8d8SChris Lattner     if (result.Succeeded())
58730fdc8d8SChris Lattner     {
588c982c768SGreg Clayton         const size_t count = valid_bp_ids.GetSize();
589c982c768SGreg Clayton         for (size_t i = 0; i < count; ++i)
59030fdc8d8SChris Lattner         {
59130fdc8d8SChris Lattner             BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
59230fdc8d8SChris Lattner             if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
59330fdc8d8SChris Lattner             {
59430fdc8d8SChris Lattner                 Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
59530fdc8d8SChris Lattner                 if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
59630fdc8d8SChris Lattner                 {
59730fdc8d8SChris Lattner                     BreakpointLocationSP bp_loc_sp (bp->FindLocationByID (cur_bp_id.GetLocationID()));
59830fdc8d8SChris Lattner                     if (bp_loc_sp)
59930fdc8d8SChris Lattner                         bp_loc_sp->ClearCallback();
60030fdc8d8SChris Lattner                     else
60130fdc8d8SChris Lattner                     {
60230fdc8d8SChris Lattner                         result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
60330fdc8d8SChris Lattner                                                      cur_bp_id.GetBreakpointID(),
60430fdc8d8SChris Lattner                                                      cur_bp_id.GetLocationID());
60530fdc8d8SChris Lattner                         result.SetStatus (eReturnStatusFailed);
60630fdc8d8SChris Lattner                         return false;
60730fdc8d8SChris Lattner                     }
60830fdc8d8SChris Lattner                 }
60930fdc8d8SChris Lattner                 else
61030fdc8d8SChris Lattner                 {
61130fdc8d8SChris Lattner                     bp->ClearCallback();
61230fdc8d8SChris Lattner                 }
61330fdc8d8SChris Lattner             }
61430fdc8d8SChris Lattner         }
61530fdc8d8SChris Lattner     }
61630fdc8d8SChris Lattner     return result.Succeeded();
61730fdc8d8SChris Lattner }
61830fdc8d8SChris Lattner 
61930fdc8d8SChris Lattner 
62030fdc8d8SChris Lattner //-------------------------------------------------------------------------
62130fdc8d8SChris Lattner // CommandObjectBreakpointCommandList
62230fdc8d8SChris Lattner //-------------------------------------------------------------------------
62330fdc8d8SChris Lattner 
624a7015092SGreg Clayton CommandObjectBreakpointCommandList::CommandObjectBreakpointCommandList (CommandInterpreter &interpreter) :
625a7015092SGreg Clayton     CommandObject (interpreter,
626a7015092SGreg Clayton                    "list",
62730fdc8d8SChris Lattner                    "List the script or set of commands to be executed when the breakpoint is hit.",
628405fe67fSCaroline Tice                     NULL)
62930fdc8d8SChris Lattner {
630405fe67fSCaroline Tice     CommandArgumentEntry arg;
631405fe67fSCaroline Tice     CommandArgumentData bp_id_arg;
632405fe67fSCaroline Tice 
633405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
634405fe67fSCaroline Tice     bp_id_arg.arg_type = eArgTypeBreakpointID;
635405fe67fSCaroline Tice     bp_id_arg.arg_repetition = eArgRepeatPlain;
636405fe67fSCaroline Tice 
637405fe67fSCaroline Tice     // There is only one variant this argument could be; put it into the argument entry.
638405fe67fSCaroline Tice     arg.push_back (bp_id_arg);
639405fe67fSCaroline Tice 
640405fe67fSCaroline Tice     // Push the data for the first argument into the m_arguments vector.
641405fe67fSCaroline Tice     m_arguments.push_back (arg);
64230fdc8d8SChris Lattner }
64330fdc8d8SChris Lattner 
64430fdc8d8SChris Lattner CommandObjectBreakpointCommandList::~CommandObjectBreakpointCommandList ()
64530fdc8d8SChris Lattner {
64630fdc8d8SChris Lattner }
64730fdc8d8SChris Lattner 
64830fdc8d8SChris Lattner bool
6496611103cSGreg Clayton CommandObjectBreakpointCommandList::Execute
6506611103cSGreg Clayton (
6516611103cSGreg Clayton     Args& command,
6526611103cSGreg Clayton     CommandReturnObject &result
6536611103cSGreg Clayton )
65430fdc8d8SChris Lattner {
655a7015092SGreg Clayton     Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
65630fdc8d8SChris Lattner 
65730fdc8d8SChris Lattner     if (target == NULL)
65830fdc8d8SChris Lattner     {
65930fdc8d8SChris Lattner         result.AppendError ("There is not a current executable; there are no breakpoints for which to list commands");
66030fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
66130fdc8d8SChris Lattner         return false;
66230fdc8d8SChris Lattner     }
66330fdc8d8SChris Lattner 
66430fdc8d8SChris Lattner     const BreakpointList &breakpoints = target->GetBreakpointList();
66530fdc8d8SChris Lattner     size_t num_breakpoints = breakpoints.GetSize();
66630fdc8d8SChris Lattner 
66730fdc8d8SChris Lattner     if (num_breakpoints == 0)
66830fdc8d8SChris Lattner     {
66930fdc8d8SChris Lattner         result.AppendError ("No breakpoints exist for which to list commands");
67030fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
67130fdc8d8SChris Lattner         return false;
67230fdc8d8SChris Lattner     }
67330fdc8d8SChris Lattner 
67430fdc8d8SChris Lattner     if (command.GetArgumentCount() == 0)
67530fdc8d8SChris Lattner     {
67630fdc8d8SChris Lattner         result.AppendError ("No breakpoint specified for which to list the commands");
67730fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
67830fdc8d8SChris Lattner         return false;
67930fdc8d8SChris Lattner     }
68030fdc8d8SChris Lattner 
68130fdc8d8SChris Lattner     BreakpointIDList valid_bp_ids;
68230fdc8d8SChris Lattner     CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
68330fdc8d8SChris Lattner 
68430fdc8d8SChris Lattner     if (result.Succeeded())
68530fdc8d8SChris Lattner     {
686c982c768SGreg Clayton         const size_t count = valid_bp_ids.GetSize();
687c982c768SGreg Clayton         for (size_t i = 0; i < count; ++i)
68830fdc8d8SChris Lattner         {
68930fdc8d8SChris Lattner             BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
69030fdc8d8SChris Lattner             if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
69130fdc8d8SChris Lattner             {
69230fdc8d8SChris Lattner                 Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
69330fdc8d8SChris Lattner 
69430fdc8d8SChris Lattner                 if (bp)
69530fdc8d8SChris Lattner                 {
6961b54c88cSJim Ingham                     const BreakpointOptions *bp_options = NULL;
69730fdc8d8SChris Lattner                     if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
69830fdc8d8SChris Lattner                     {
69930fdc8d8SChris Lattner                         BreakpointLocationSP bp_loc_sp(bp->FindLocationByID (cur_bp_id.GetLocationID()));
70030fdc8d8SChris Lattner                         if (bp_loc_sp)
70105407f6bSJim Ingham                             bp_options = bp_loc_sp->GetOptionsNoCreate();
70230fdc8d8SChris Lattner                         else
70330fdc8d8SChris Lattner                         {
70430fdc8d8SChris Lattner                             result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
70530fdc8d8SChris Lattner                                                          cur_bp_id.GetBreakpointID(),
70630fdc8d8SChris Lattner                                                          cur_bp_id.GetLocationID());
70730fdc8d8SChris Lattner                             result.SetStatus (eReturnStatusFailed);
70830fdc8d8SChris Lattner                             return false;
70930fdc8d8SChris Lattner                         }
71030fdc8d8SChris Lattner                     }
71130fdc8d8SChris Lattner                     else
71230fdc8d8SChris Lattner                     {
71330fdc8d8SChris Lattner                         bp_options = bp->GetOptions();
71430fdc8d8SChris Lattner                     }
71530fdc8d8SChris Lattner 
71630fdc8d8SChris Lattner                     if (bp_options)
71730fdc8d8SChris Lattner                     {
71830fdc8d8SChris Lattner                         StreamString id_str;
719e16c50a1SJim Ingham                         BreakpointID::GetCanonicalReference (&id_str,
720e16c50a1SJim Ingham                                                              cur_bp_id.GetBreakpointID(),
721e16c50a1SJim Ingham                                                              cur_bp_id.GetLocationID());
7221b54c88cSJim Ingham                         const Baton *baton = bp_options->GetBaton();
72330fdc8d8SChris Lattner                         if (baton)
72430fdc8d8SChris Lattner                         {
72530fdc8d8SChris Lattner                             result.GetOutputStream().Printf ("Breakpoint %s:\n", id_str.GetData());
72630fdc8d8SChris Lattner                             result.GetOutputStream().IndentMore ();
72730fdc8d8SChris Lattner                             baton->GetDescription(&result.GetOutputStream(), eDescriptionLevelFull);
72830fdc8d8SChris Lattner                             result.GetOutputStream().IndentLess ();
72930fdc8d8SChris Lattner                         }
73030fdc8d8SChris Lattner                         else
73130fdc8d8SChris Lattner                         {
732e16c50a1SJim Ingham                             result.AppendMessageWithFormat ("Breakpoint %s does not have an associated command.\n",
733e16c50a1SJim Ingham                                                             id_str.GetData());
73430fdc8d8SChris Lattner                         }
73530fdc8d8SChris Lattner                     }
73630fdc8d8SChris Lattner                     result.SetStatus (eReturnStatusSuccessFinishResult);
73730fdc8d8SChris Lattner                 }
73830fdc8d8SChris Lattner                 else
73930fdc8d8SChris Lattner                 {
74030fdc8d8SChris Lattner                     result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n", cur_bp_id.GetBreakpointID());
74130fdc8d8SChris Lattner                     result.SetStatus (eReturnStatusFailed);
74230fdc8d8SChris Lattner                 }
74330fdc8d8SChris Lattner 
74430fdc8d8SChris Lattner             }
74530fdc8d8SChris Lattner         }
74630fdc8d8SChris Lattner     }
74730fdc8d8SChris Lattner 
74830fdc8d8SChris Lattner     return result.Succeeded();
74930fdc8d8SChris Lattner }
75030fdc8d8SChris Lattner 
75130fdc8d8SChris Lattner //-------------------------------------------------------------------------
75230fdc8d8SChris Lattner // CommandObjectBreakpointCommand
75330fdc8d8SChris Lattner //-------------------------------------------------------------------------
75430fdc8d8SChris Lattner 
7556611103cSGreg Clayton CommandObjectBreakpointCommand::CommandObjectBreakpointCommand (CommandInterpreter &interpreter) :
756a7015092SGreg Clayton     CommandObjectMultiword (interpreter,
757a7015092SGreg Clayton                             "command",
75830fdc8d8SChris Lattner                             "A set of commands for adding, removing and examining bits of code to be executed when the breakpoint is hit (breakpoint 'commmands').",
75930fdc8d8SChris Lattner                             "command <sub-command> [<sub-command-options>] <breakpoint-id>")
76030fdc8d8SChris Lattner {
76130fdc8d8SChris Lattner     bool status;
762a7015092SGreg Clayton     CommandObjectSP add_command_object (new CommandObjectBreakpointCommandAdd (interpreter));
763a7015092SGreg Clayton     CommandObjectSP remove_command_object (new CommandObjectBreakpointCommandRemove (interpreter));
764a7015092SGreg Clayton     CommandObjectSP list_command_object (new CommandObjectBreakpointCommandList (interpreter));
76530fdc8d8SChris Lattner 
76630fdc8d8SChris Lattner     add_command_object->SetCommandName ("breakpoint command add");
76730fdc8d8SChris Lattner     remove_command_object->SetCommandName ("breakpoint command remove");
76830fdc8d8SChris Lattner     list_command_object->SetCommandName ("breakpoint command list");
76930fdc8d8SChris Lattner 
770a7015092SGreg Clayton     status = LoadSubCommand ("add",    add_command_object);
771a7015092SGreg Clayton     status = LoadSubCommand ("remove", remove_command_object);
772a7015092SGreg Clayton     status = LoadSubCommand ("list",   list_command_object);
77330fdc8d8SChris Lattner }
77430fdc8d8SChris Lattner 
77530fdc8d8SChris Lattner 
77630fdc8d8SChris Lattner CommandObjectBreakpointCommand::~CommandObjectBreakpointCommand ()
77730fdc8d8SChris Lattner {
77830fdc8d8SChris Lattner }
77930fdc8d8SChris Lattner 
78030fdc8d8SChris Lattner bool
78130fdc8d8SChris Lattner CommandObjectBreakpointCommand::BreakpointOptionsCallbackFunction
78230fdc8d8SChris Lattner (
78330fdc8d8SChris Lattner     void *baton,
78430fdc8d8SChris Lattner     StoppointCallbackContext *context,
78530fdc8d8SChris Lattner     lldb::user_id_t break_id,
78630fdc8d8SChris Lattner     lldb::user_id_t break_loc_id
78730fdc8d8SChris Lattner )
78830fdc8d8SChris Lattner {
78930fdc8d8SChris Lattner     bool ret_value = true;
79030fdc8d8SChris Lattner     if (baton == NULL)
79130fdc8d8SChris Lattner         return true;
79230fdc8d8SChris Lattner 
79330fdc8d8SChris Lattner 
79430fdc8d8SChris Lattner     BreakpointOptions::CommandData *data = (BreakpointOptions::CommandData *) baton;
79530fdc8d8SChris Lattner     StringList &commands = data->user_source;
79630fdc8d8SChris Lattner 
79730fdc8d8SChris Lattner     if (commands.GetSize() > 0)
79830fdc8d8SChris Lattner     {
7996611103cSGreg Clayton         if (context->exe_ctx.target)
8006611103cSGreg Clayton         {
80185e8b814SJim Ingham             CommandReturnObject result;
8026611103cSGreg Clayton             Debugger &debugger = context->exe_ctx.target->GetDebugger();
80385e8b814SJim Ingham             // Rig up the results secondary output stream to the debugger's, so the output will come out synchronously
80485e8b814SJim Ingham             // if the debugger is set up that way.
80585e8b814SJim Ingham 
806*b5059accSCaroline Tice             StreamSP output_stream (new StreamAsynchronousIO (debugger.GetCommandInterpreter(),
807*b5059accSCaroline Tice                                                               CommandInterpreter::eBroadcastBitAsynchronousOutputData));
808*b5059accSCaroline Tice             StreamSP error_stream (new StreamAsynchronousIO (debugger.GetCommandInterpreter(),
809*b5059accSCaroline Tice                                                              CommandInterpreter::eBroadcastBitAsynchronousErrorData));
810*b5059accSCaroline Tice             result.SetImmediateOutputStream (output_stream);
811*b5059accSCaroline Tice             result.SetImmediateErrorStream (error_stream);
81230fdc8d8SChris Lattner 
813e16c50a1SJim Ingham             bool stop_on_continue = true;
814e16c50a1SJim Ingham             bool echo_commands    = false;
815e16c50a1SJim Ingham             bool print_results    = true;
81630fdc8d8SChris Lattner 
817e16c50a1SJim Ingham             debugger.GetCommandInterpreter().HandleCommands (commands,
818e16c50a1SJim Ingham                                                              &(context->exe_ctx),
819e16c50a1SJim Ingham                                                              stop_on_continue,
820e16c50a1SJim Ingham                                                              data->stop_on_error,
821e16c50a1SJim Ingham                                                              echo_commands,
822e16c50a1SJim Ingham                                                              print_results,
823e16c50a1SJim Ingham                                                              result);
824*b5059accSCaroline Tice             result.GetImmediateOutputStream()->Flush();
825*b5059accSCaroline Tice             result.GetImmediateErrorStream()->Flush();
82630fdc8d8SChris Lattner        }
8276611103cSGreg Clayton     }
82830fdc8d8SChris Lattner     return ret_value;
82930fdc8d8SChris Lattner }
83030fdc8d8SChris Lattner 
831