130fdc8d8SChris Lattner //===-- CommandObjectBreakpointCommand.cpp ----------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner // C Includes
1130fdc8d8SChris Lattner // C++ Includes
1230fdc8d8SChris Lattner 
1330fdc8d8SChris Lattner 
1430fdc8d8SChris Lattner #include "CommandObjectBreakpointCommand.h"
1530fdc8d8SChris Lattner #include "CommandObjectBreakpoint.h"
1630fdc8d8SChris Lattner 
1730fdc8d8SChris Lattner #include "lldb/Interpreter/CommandInterpreter.h"
1830fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h"
1930fdc8d8SChris Lattner #include "lldb/Target/Target.h"
2030fdc8d8SChris Lattner #include "lldb/Target/Thread.h"
2130fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointIDList.h"
2230fdc8d8SChris Lattner #include "lldb/Breakpoint/Breakpoint.h"
2330fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointLocation.h"
2430fdc8d8SChris Lattner #include "lldb/Breakpoint/StoppointCallbackContext.h"
2530fdc8d8SChris Lattner #include "lldb/Core/State.h"
2630fdc8d8SChris Lattner 
2730fdc8d8SChris Lattner using namespace lldb;
2830fdc8d8SChris Lattner using namespace lldb_private;
2930fdc8d8SChris Lattner 
3030fdc8d8SChris Lattner //-------------------------------------------------------------------------
3130fdc8d8SChris Lattner // CommandObjectBreakpointCommandAdd::CommandOptions
3230fdc8d8SChris Lattner //-------------------------------------------------------------------------
3330fdc8d8SChris Lattner 
34eb0103f2SGreg Clayton CommandObjectBreakpointCommandAdd::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
35eb0103f2SGreg Clayton     Options (interpreter),
3639d7d4f0SJohnny Chen     m_use_commands (false),
3739d7d4f0SJohnny Chen     m_use_script_language (false),
3839d7d4f0SJohnny Chen     m_script_language (eScriptLanguageNone),
3939d7d4f0SJohnny Chen     m_use_one_liner (false),
40*8d4a8010SEnrico Granata     m_one_liner(),
41*8d4a8010SEnrico Granata     m_function_name()
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 {
64*8d4a8010SEnrico Granata     { LLDB_OPT_SET_1, 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 
73*8d4a8010SEnrico Granata     { LLDB_OPT_SET_2,   false, "python-function",     'F', required_argument, NULL, NULL, eArgTypePythonFunction,
74*8d4a8010SEnrico Granata         "Give the name of a Python function to run as command for this breakpoint. Be sure to give a module name if appropriate."},
75*8d4a8010SEnrico Granata 
76deaab222SCaroline Tice     { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
7730fdc8d8SChris Lattner };
7830fdc8d8SChris Lattner 
79e0d378b3SGreg Clayton const OptionDefinition*
8030fdc8d8SChris Lattner CommandObjectBreakpointCommandAdd::CommandOptions::GetDefinitions ()
8130fdc8d8SChris Lattner {
8230fdc8d8SChris Lattner     return g_option_table;
8330fdc8d8SChris Lattner }
8430fdc8d8SChris Lattner 
8530fdc8d8SChris Lattner 
8630fdc8d8SChris Lattner Error
8730fdc8d8SChris Lattner CommandObjectBreakpointCommandAdd::CommandOptions::SetOptionValue
8830fdc8d8SChris Lattner (
89f6b8b581SGreg Clayton     uint32_t option_idx,
9030fdc8d8SChris Lattner     const char *option_arg
9130fdc8d8SChris Lattner )
9230fdc8d8SChris Lattner {
9330fdc8d8SChris Lattner     Error error;
9430fdc8d8SChris Lattner     char short_option = (char) m_getopt_table[option_idx].val;
9530fdc8d8SChris Lattner 
9630fdc8d8SChris Lattner     switch (short_option)
9730fdc8d8SChris Lattner     {
9839d7d4f0SJohnny Chen     case 'o':
9939d7d4f0SJohnny Chen         m_use_one_liner = true;
10039d7d4f0SJohnny Chen         m_one_liner = option_arg;
10139d7d4f0SJohnny Chen         break;
102cf0e4f0dSGreg Clayton 
10330fdc8d8SChris Lattner     case 's':
104e16c50a1SJim Ingham         m_script_language = (lldb::ScriptLanguage) Args::StringToOptionEnum (option_arg,
105e16c50a1SJim Ingham                                                                              g_option_table[option_idx].enum_values,
106e16c50a1SJim Ingham                                                                              eScriptLanguageNone,
107cf0e4f0dSGreg Clayton                                                                              error);
108e16c50a1SJim Ingham 
109e16c50a1SJim Ingham         if (m_script_language == eScriptLanguagePython || m_script_language == eScriptLanguageDefault)
110e16c50a1SJim Ingham         {
11130fdc8d8SChris Lattner             m_use_script_language = true;
112e16c50a1SJim Ingham         }
113e16c50a1SJim Ingham         else
114e16c50a1SJim Ingham         {
11530fdc8d8SChris Lattner             m_use_script_language = false;
116e16c50a1SJim Ingham         }
117cf0e4f0dSGreg Clayton         break;
118cf0e4f0dSGreg Clayton 
119cf0e4f0dSGreg Clayton     case 'e':
120cf0e4f0dSGreg Clayton         {
121cf0e4f0dSGreg Clayton             bool success = false;
122cf0e4f0dSGreg Clayton             m_stop_on_error = Args::StringToBoolean(option_arg, false, &success);
123cf0e4f0dSGreg Clayton             if (!success)
12486edbf41SGreg Clayton                 error.SetErrorStringWithFormat("invalid value for stop-on-error: \"%s\"", option_arg);
125e16c50a1SJim Ingham         }
126e16c50a1SJim Ingham         break;
127cf0e4f0dSGreg Clayton 
128*8d4a8010SEnrico Granata     case 'F':
129*8d4a8010SEnrico Granata         {
130*8d4a8010SEnrico Granata             m_use_one_liner = false;
131*8d4a8010SEnrico Granata             m_use_script_language = true;
132*8d4a8010SEnrico Granata             m_function_name.assign(option_arg);
133*8d4a8010SEnrico Granata         }
134*8d4a8010SEnrico Granata         break;
135*8d4a8010SEnrico Granata 
13630fdc8d8SChris Lattner     default:
13730fdc8d8SChris Lattner         break;
13830fdc8d8SChris Lattner     }
13930fdc8d8SChris Lattner     return error;
14030fdc8d8SChris Lattner }
14130fdc8d8SChris Lattner 
14230fdc8d8SChris Lattner void
143f6b8b581SGreg Clayton CommandObjectBreakpointCommandAdd::CommandOptions::OptionParsingStarting ()
14430fdc8d8SChris Lattner {
145e16c50a1SJim Ingham     m_use_commands = true;
14630fdc8d8SChris Lattner     m_use_script_language = false;
14730fdc8d8SChris Lattner     m_script_language = eScriptLanguageNone;
14839d7d4f0SJohnny Chen 
14939d7d4f0SJohnny Chen     m_use_one_liner = false;
150e16c50a1SJim Ingham     m_stop_on_error = true;
15139d7d4f0SJohnny Chen     m_one_liner.clear();
152*8d4a8010SEnrico Granata     m_function_name.clear();
15330fdc8d8SChris Lattner }
15430fdc8d8SChris Lattner 
15530fdc8d8SChris Lattner //-------------------------------------------------------------------------
15630fdc8d8SChris Lattner // CommandObjectBreakpointCommandAdd
15730fdc8d8SChris Lattner //-------------------------------------------------------------------------
15830fdc8d8SChris Lattner 
15930fdc8d8SChris Lattner 
160a7015092SGreg Clayton CommandObjectBreakpointCommandAdd::CommandObjectBreakpointCommandAdd (CommandInterpreter &interpreter) :
161a7015092SGreg Clayton     CommandObject (interpreter,
162a7015092SGreg Clayton                    "add",
163e3d26315SCaroline Tice                    "Add a set of commands to a breakpoint, to be executed whenever the breakpoint is hit.",
164eb0103f2SGreg Clayton                    NULL),
165eb0103f2SGreg Clayton     m_options (interpreter)
16630fdc8d8SChris Lattner {
16730fdc8d8SChris Lattner     SetHelpLong (
16830fdc8d8SChris Lattner "\nGeneral information about entering breakpoint commands \n\
16930fdc8d8SChris Lattner ------------------------------------------------------ \n\
17030fdc8d8SChris Lattner  \n\
17130fdc8d8SChris Lattner This command will cause you to be prompted to enter the command or set \n\
17230fdc8d8SChris Lattner of commands you wish to be executed when the specified breakpoint is \n\
17330fdc8d8SChris Lattner hit.  You will be told to enter your command(s), and will see a '> ' \n\
17430fdc8d8SChris Lattner prompt. Because you can enter one or many commands to be executed when \n\
17530fdc8d8SChris Lattner a breakpoint is hit, you will continue to be prompted after each \n\
17630fdc8d8SChris Lattner new-line that you enter, until you enter the word 'DONE', which will \n\
17730fdc8d8SChris Lattner cause the commands you have entered to be stored with the breakpoint \n\
17830fdc8d8SChris Lattner and executed when the breakpoint is hit. \n\
17930fdc8d8SChris Lattner  \n\
18030fdc8d8SChris Lattner Syntax checking is not necessarily done when breakpoint commands are \n\
18130fdc8d8SChris Lattner entered.  An improperly written breakpoint command will attempt to get \n\
18230fdc8d8SChris Lattner executed when the breakpoint gets hit, and usually silently fail.  If \n\
18330fdc8d8SChris Lattner your breakpoint command does not appear to be getting executed, go \n\
18430fdc8d8SChris Lattner back and check your syntax. \n\
18530fdc8d8SChris Lattner  \n\
18630fdc8d8SChris Lattner  \n\
18730fdc8d8SChris Lattner Special information about PYTHON breakpoint commands \n\
18830fdc8d8SChris Lattner ---------------------------------------------------- \n\
18930fdc8d8SChris Lattner  \n\
19030fdc8d8SChris Lattner You may enter either one line of Python or multiple lines of Python \n\
19130fdc8d8SChris Lattner (including defining whole functions, if desired).  If you enter a \n\
19230fdc8d8SChris Lattner single line of Python, that will be passed to the Python interpreter \n\
19330fdc8d8SChris Lattner 'as is' when the breakpoint gets hit.  If you enter function \n\
19430fdc8d8SChris Lattner definitions, they will be passed to the Python interpreter as soon as \n\
19530fdc8d8SChris Lattner you finish entering the breakpoint command, and they can be called \n\
19630fdc8d8SChris Lattner later (don't forget to add calls to them, if you want them called when \n\
19730fdc8d8SChris Lattner the breakpoint is hit).  If you enter multiple lines of Python that \n\
19830fdc8d8SChris Lattner are not function definitions, they will be collected into a new, \n\
19930fdc8d8SChris Lattner automatically generated Python function, and a call to the newly \n\
20030fdc8d8SChris Lattner generated function will be attached to the breakpoint.  Important \n\
20130fdc8d8SChris Lattner Note: Because loose Python code gets collected into functions, if you \n\
20230fdc8d8SChris Lattner want to access global variables in the 'loose' code, you need to \n\
20330fdc8d8SChris Lattner specify that they are global, using the 'global' keyword.  Be sure to \n\
20430fdc8d8SChris Lattner use correct Python syntax, including indentation, when entering Python \n\
205*8d4a8010SEnrico Granata breakpoint commands. \nAs a third option, you can pass the name of an already \
206*8d4a8010SEnrico Granata existing Python function and that function will be attached to the breakpoint. \n\
20730fdc8d8SChris Lattner  \n\
20830fdc8d8SChris Lattner Example Python one-line breakpoint command: \n\
20930fdc8d8SChris Lattner  \n\
210e16c50a1SJim Ingham (lldb) breakpoint command add -s python 1 \n\
21130fdc8d8SChris Lattner Enter your Python command(s). Type 'DONE' to end. \n\
21230fdc8d8SChris Lattner > print \"Hit this breakpoint!\" \n\
21330fdc8d8SChris Lattner > DONE \n\
21430fdc8d8SChris Lattner  \n\
2153495f25aSJohnny Chen As a convenience, this also works for a short Python one-liner: \n\
216e16c50a1SJim Ingham (lldb) breakpoint command add -s python 1 -o \"import time; print time.asctime()\" \n\
2173495f25aSJohnny Chen (lldb) run \n\
2183495f25aSJohnny Chen Launching '.../a.out'  (x86_64) \n\
2193495f25aSJohnny Chen (lldb) Fri Sep 10 12:17:45 2010 \n\
2203495f25aSJohnny Chen Process 21778 Stopped \n\
2213495f25aSJohnny 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\
2223495f25aSJohnny Chen   36   	\n\
2233495f25aSJohnny Chen   37   	int c(int val)\n\
2243495f25aSJohnny Chen   38   	{\n\
2253495f25aSJohnny Chen   39 ->	    return val + 3;\n\
2263495f25aSJohnny Chen   40   	}\n\
2273495f25aSJohnny Chen   41   	\n\
2283495f25aSJohnny Chen   42   	int main (int argc, char const *argv[])\n\
2293495f25aSJohnny Chen (lldb) \n\
2303495f25aSJohnny Chen  \n\
23130fdc8d8SChris Lattner Example multiple line Python breakpoint command, using function definition: \n\
23230fdc8d8SChris Lattner  \n\
233e16c50a1SJim Ingham (lldb) breakpoint command add -s python 1 \n\
23430fdc8d8SChris Lattner Enter your Python command(s). Type 'DONE' to end. \n\
23530fdc8d8SChris Lattner > def breakpoint_output (bp_no): \n\
23630fdc8d8SChris Lattner >     out_string = \"Hit breakpoint number \" + repr (bp_no) \n\
23730fdc8d8SChris Lattner >     print out_string \n\
23830fdc8d8SChris Lattner >     return True \n\
23930fdc8d8SChris Lattner > breakpoint_output (1) \n\
24030fdc8d8SChris Lattner > DONE \n\
24130fdc8d8SChris Lattner  \n\
24230fdc8d8SChris Lattner  \n\
24330fdc8d8SChris Lattner Example multiple line Python breakpoint command, using 'loose' Python: \n\
24430fdc8d8SChris Lattner  \n\
245e16c50a1SJim Ingham (lldb) breakpoint command add -s p 1 \n\
24630fdc8d8SChris Lattner Enter your Python command(s). Type 'DONE' to end. \n\
24730fdc8d8SChris Lattner > global bp_count \n\
24830fdc8d8SChris Lattner > bp_count = bp_count + 1 \n\
24930fdc8d8SChris Lattner > print \"Hit this breakpoint \" + repr(bp_count) + \" times!\" \n\
25030fdc8d8SChris Lattner > DONE \n\
25130fdc8d8SChris Lattner  \n\
25230fdc8d8SChris Lattner In this case, since there is a reference to a global variable, \n\
25330fdc8d8SChris Lattner 'bp_count', you will also need to make sure 'bp_count' exists and is \n\
25430fdc8d8SChris Lattner initialized: \n\
25530fdc8d8SChris Lattner  \n\
25630fdc8d8SChris Lattner (lldb) script \n\
25730fdc8d8SChris Lattner >>> bp_count = 0 \n\
25830fdc8d8SChris Lattner >>> quit() \n\
25930fdc8d8SChris Lattner  \n\
26030fdc8d8SChris Lattner (lldb)  \n\
26130fdc8d8SChris Lattner  \n\
262650b9268SCaroline Tice  \n\
263650b9268SCaroline Tice Final Note:  If you get a warning that no breakpoint command was generated, \n\
264650b9268SCaroline Tice but you did not get any syntax errors, you probably forgot to add a call \n\
265650b9268SCaroline Tice to your functions. \n\
266650b9268SCaroline Tice  \n\
267e3d26315SCaroline Tice Special information about debugger command breakpoint commands \n\
268e3d26315SCaroline Tice -------------------------------------------------------------- \n\
26930fdc8d8SChris Lattner  \n\
27030fdc8d8SChris Lattner You may enter any debugger command, exactly as you would at the \n\
27130fdc8d8SChris Lattner debugger prompt.  You may enter as many debugger commands as you like, \n\
27230fdc8d8SChris Lattner but do NOT enter more than one command per line. \n" );
273405fe67fSCaroline Tice 
274405fe67fSCaroline Tice 
275405fe67fSCaroline Tice     CommandArgumentEntry arg;
276405fe67fSCaroline Tice     CommandArgumentData bp_id_arg;
277405fe67fSCaroline Tice 
278405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
279405fe67fSCaroline Tice     bp_id_arg.arg_type = eArgTypeBreakpointID;
280405fe67fSCaroline Tice     bp_id_arg.arg_repetition = eArgRepeatPlain;
281405fe67fSCaroline Tice 
282405fe67fSCaroline Tice     // There is only one variant this argument could be; put it into the argument entry.
283405fe67fSCaroline Tice     arg.push_back (bp_id_arg);
284405fe67fSCaroline Tice 
285405fe67fSCaroline Tice     // Push the data for the first argument into the m_arguments vector.
286405fe67fSCaroline Tice     m_arguments.push_back (arg);
28730fdc8d8SChris Lattner }
28830fdc8d8SChris Lattner 
28930fdc8d8SChris Lattner CommandObjectBreakpointCommandAdd::~CommandObjectBreakpointCommandAdd ()
29030fdc8d8SChris Lattner {
29130fdc8d8SChris Lattner }
29230fdc8d8SChris Lattner 
29330fdc8d8SChris Lattner bool
29430fdc8d8SChris Lattner CommandObjectBreakpointCommandAdd::Execute
29530fdc8d8SChris Lattner (
29630fdc8d8SChris Lattner     Args& command,
29730fdc8d8SChris Lattner     CommandReturnObject &result
29830fdc8d8SChris Lattner )
29930fdc8d8SChris Lattner {
300a7015092SGreg Clayton     Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
30130fdc8d8SChris Lattner 
30230fdc8d8SChris Lattner     if (target == NULL)
30330fdc8d8SChris Lattner     {
30430fdc8d8SChris Lattner         result.AppendError ("There is not a current executable; there are no breakpoints to which to add commands");
30530fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
30630fdc8d8SChris Lattner         return false;
30730fdc8d8SChris Lattner     }
30830fdc8d8SChris Lattner 
30930fdc8d8SChris Lattner     const BreakpointList &breakpoints = target->GetBreakpointList();
31030fdc8d8SChris Lattner     size_t num_breakpoints = breakpoints.GetSize();
31130fdc8d8SChris Lattner 
31230fdc8d8SChris Lattner     if (num_breakpoints == 0)
31330fdc8d8SChris Lattner     {
31430fdc8d8SChris Lattner         result.AppendError ("No breakpoints exist to have commands added");
31530fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
31630fdc8d8SChris Lattner         return false;
31730fdc8d8SChris Lattner     }
31830fdc8d8SChris Lattner 
319*8d4a8010SEnrico Granata     if (m_options.m_use_script_language == false && m_options.m_function_name.size())
320*8d4a8010SEnrico Granata     {
321*8d4a8010SEnrico Granata         result.AppendError ("need to enable scripting to have a function run as a breakpoint command");
322*8d4a8010SEnrico Granata         result.SetStatus (eReturnStatusFailed);
323*8d4a8010SEnrico Granata         return false;
324*8d4a8010SEnrico Granata     }
325*8d4a8010SEnrico Granata 
32630fdc8d8SChris Lattner     BreakpointIDList valid_bp_ids;
32730fdc8d8SChris Lattner     CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
32830fdc8d8SChris Lattner 
32930fdc8d8SChris Lattner     if (result.Succeeded())
33030fdc8d8SChris Lattner     {
331c982c768SGreg Clayton         const size_t count = valid_bp_ids.GetSize();
332c982c768SGreg Clayton         for (size_t i = 0; i < count; ++i)
33330fdc8d8SChris Lattner         {
33430fdc8d8SChris Lattner             BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
33530fdc8d8SChris Lattner             if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
33630fdc8d8SChris Lattner             {
33730fdc8d8SChris Lattner                 Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
33839d7d4f0SJohnny Chen                 BreakpointOptions *bp_options = NULL;
33939d7d4f0SJohnny Chen                 if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID)
34039d7d4f0SJohnny Chen                 {
34139d7d4f0SJohnny Chen                     // This breakpoint does not have an associated location.
34239d7d4f0SJohnny Chen                     bp_options = bp->GetOptions();
34339d7d4f0SJohnny Chen                 }
34439d7d4f0SJohnny Chen                 else
34530fdc8d8SChris Lattner                 {
34630fdc8d8SChris Lattner                     BreakpointLocationSP bp_loc_sp(bp->FindLocationByID (cur_bp_id.GetLocationID()));
34739d7d4f0SJohnny Chen                     // This breakpoint does have an associated location.
34839d7d4f0SJohnny Chen                     // Get its breakpoint options.
34930fdc8d8SChris Lattner                     if (bp_loc_sp)
35039d7d4f0SJohnny Chen                         bp_options = bp_loc_sp->GetLocationOptions();
35139d7d4f0SJohnny Chen                 }
35239d7d4f0SJohnny Chen 
353e16c50a1SJim Ingham                 // Skip this breakpoint if bp_options is not good.
35439d7d4f0SJohnny Chen                 if (bp_options == NULL) continue;
35539d7d4f0SJohnny Chen 
35639d7d4f0SJohnny Chen                 // If we are using script language, get the script interpreter
35739d7d4f0SJohnny Chen                 // in order to set or collect command callback.  Otherwise, call
35839d7d4f0SJohnny Chen                 // the methods associated with this object.
35930fdc8d8SChris Lattner                 if (m_options.m_use_script_language)
36030fdc8d8SChris Lattner                 {
36139d7d4f0SJohnny Chen                     // Special handling for one-liner specified inline.
36239d7d4f0SJohnny Chen                     if (m_options.m_use_one_liner)
363*8d4a8010SEnrico Granata                     {
364a7015092SGreg Clayton                         m_interpreter.GetScriptInterpreter()->SetBreakpointCommandCallback (bp_options,
36539d7d4f0SJohnny Chen                                                                                             m_options.m_one_liner.c_str());
366*8d4a8010SEnrico Granata                     }
367*8d4a8010SEnrico Granata                     // Special handling for using a Python function by name
368*8d4a8010SEnrico Granata                     // instead of extending the breakpoint callback data structures, we just automatize
369*8d4a8010SEnrico Granata                     // what the user would do manually: make their breakpoint command be a function call
370*8d4a8010SEnrico Granata                     else if (m_options.m_function_name.size())
371*8d4a8010SEnrico Granata                     {
372*8d4a8010SEnrico Granata                         std::string oneliner(m_options.m_function_name);
373*8d4a8010SEnrico Granata                         oneliner += "(frame, bp_loc, dict)";
374*8d4a8010SEnrico Granata                         m_interpreter.GetScriptInterpreter()->SetBreakpointCommandCallback (bp_options,
375*8d4a8010SEnrico Granata                                                                                             oneliner.c_str());
376*8d4a8010SEnrico Granata                     }
37794de55d5SJohnny Chen                     else
378*8d4a8010SEnrico Granata                     {
379a7015092SGreg Clayton                         m_interpreter.GetScriptInterpreter()->CollectDataForBreakpointCommandCallback (bp_options,
38030fdc8d8SChris Lattner                                                                                                        result);
38130fdc8d8SChris Lattner                     }
382*8d4a8010SEnrico Granata                 }
38330fdc8d8SChris Lattner                 else
38430fdc8d8SChris Lattner                 {
38539d7d4f0SJohnny Chen                     // Special handling for one-liner specified inline.
38639d7d4f0SJohnny Chen                     if (m_options.m_use_one_liner)
387a7015092SGreg Clayton                         SetBreakpointCommandCallback (bp_options,
38839d7d4f0SJohnny Chen                                                       m_options.m_one_liner.c_str());
38939d7d4f0SJohnny Chen                     else
390a7015092SGreg Clayton                         CollectDataForBreakpointCommandCallback (bp_options,
391b132097bSGreg Clayton                                                                  result);
39230fdc8d8SChris Lattner                 }
39330fdc8d8SChris Lattner             }
39430fdc8d8SChris Lattner         }
39530fdc8d8SChris Lattner     }
39630fdc8d8SChris Lattner 
39730fdc8d8SChris Lattner     return result.Succeeded();
39830fdc8d8SChris Lattner }
39930fdc8d8SChris Lattner 
40030fdc8d8SChris Lattner Options *
40130fdc8d8SChris Lattner CommandObjectBreakpointCommandAdd::GetOptions ()
40230fdc8d8SChris Lattner {
40330fdc8d8SChris Lattner     return &m_options;
40430fdc8d8SChris Lattner }
40530fdc8d8SChris Lattner 
40630fdc8d8SChris Lattner const char *g_reader_instructions = "Enter your debugger command(s).  Type 'DONE' to end.";
40730fdc8d8SChris Lattner 
40830fdc8d8SChris Lattner void
40930fdc8d8SChris Lattner CommandObjectBreakpointCommandAdd::CollectDataForBreakpointCommandCallback
41030fdc8d8SChris Lattner (
41130fdc8d8SChris Lattner     BreakpointOptions *bp_options,
41230fdc8d8SChris Lattner     CommandReturnObject &result
41330fdc8d8SChris Lattner )
41430fdc8d8SChris Lattner {
415a7015092SGreg Clayton     InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
41630fdc8d8SChris Lattner     std::auto_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
41730fdc8d8SChris Lattner     if (reader_sp && data_ap.get())
41830fdc8d8SChris Lattner     {
41930fdc8d8SChris Lattner         BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
42030fdc8d8SChris Lattner         bp_options->SetCallback (CommandObjectBreakpointCommand::BreakpointOptionsCallbackFunction, baton_sp);
42130fdc8d8SChris Lattner 
42230fdc8d8SChris Lattner         Error err (reader_sp->Initialize (CommandObjectBreakpointCommandAdd::GenerateBreakpointCommandCallback,
42330fdc8d8SChris Lattner                                           bp_options,                   // baton
42430fdc8d8SChris Lattner                                           eInputReaderGranularityLine,  // token size, to pass to callback function
42530fdc8d8SChris Lattner                                           "DONE",                       // end token
42630fdc8d8SChris Lattner                                           "> ",                         // prompt
42730fdc8d8SChris Lattner                                           true));                       // echo input
42830fdc8d8SChris Lattner         if (err.Success())
42930fdc8d8SChris Lattner         {
430a7015092SGreg Clayton             m_interpreter.GetDebugger().PushInputReader (reader_sp);
43130fdc8d8SChris Lattner             result.SetStatus (eReturnStatusSuccessFinishNoResult);
43230fdc8d8SChris Lattner         }
43330fdc8d8SChris Lattner         else
43430fdc8d8SChris Lattner         {
43530fdc8d8SChris Lattner             result.AppendError (err.AsCString());
43630fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
43730fdc8d8SChris Lattner         }
43830fdc8d8SChris Lattner     }
43930fdc8d8SChris Lattner     else
44030fdc8d8SChris Lattner     {
44130fdc8d8SChris Lattner         result.AppendError("out of memory");
44230fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
44330fdc8d8SChris Lattner     }
44430fdc8d8SChris Lattner 
44530fdc8d8SChris Lattner }
44630fdc8d8SChris Lattner 
4474550154dSJohnny Chen // Set a one-liner as the callback for the breakpoint.
44839d7d4f0SJohnny Chen void
449a7015092SGreg Clayton CommandObjectBreakpointCommandAdd::SetBreakpointCommandCallback (BreakpointOptions *bp_options,
45039d7d4f0SJohnny Chen                                                                  const char *oneliner)
45139d7d4f0SJohnny Chen {
45239d7d4f0SJohnny Chen     std::auto_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
45339d7d4f0SJohnny Chen 
45439d7d4f0SJohnny Chen     // It's necessary to set both user_source and script_source to the oneliner.
45539d7d4f0SJohnny Chen     // The former is used to generate callback description (as in breakpoint command list)
45639d7d4f0SJohnny Chen     // while the latter is used for Python to interpret during the actual callback.
45739d7d4f0SJohnny Chen     data_ap->user_source.AppendString (oneliner);
458a73b7df7SEnrico Granata     data_ap->script_source.assign (oneliner);
459e16c50a1SJim Ingham     data_ap->stop_on_error = m_options.m_stop_on_error;
46039d7d4f0SJohnny Chen 
46139d7d4f0SJohnny Chen     BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
46239d7d4f0SJohnny Chen     bp_options->SetCallback (CommandObjectBreakpointCommand::BreakpointOptionsCallbackFunction, baton_sp);
46339d7d4f0SJohnny Chen 
46439d7d4f0SJohnny Chen     return;
46539d7d4f0SJohnny Chen }
46639d7d4f0SJohnny Chen 
46730fdc8d8SChris Lattner size_t
46830fdc8d8SChris Lattner CommandObjectBreakpointCommandAdd::GenerateBreakpointCommandCallback
46930fdc8d8SChris Lattner (
47030fdc8d8SChris Lattner     void *baton,
4716611103cSGreg Clayton     InputReader &reader,
47230fdc8d8SChris Lattner     lldb::InputReaderAction notification,
47330fdc8d8SChris Lattner     const char *bytes,
47430fdc8d8SChris Lattner     size_t bytes_len
47530fdc8d8SChris Lattner )
47630fdc8d8SChris Lattner {
477d61c10bcSCaroline Tice     StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
478d61c10bcSCaroline Tice     bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
47930fdc8d8SChris Lattner 
48030fdc8d8SChris Lattner     switch (notification)
48130fdc8d8SChris Lattner     {
48230fdc8d8SChris Lattner     case eInputReaderActivate:
483d61c10bcSCaroline Tice         if (!batch_mode)
484d61c10bcSCaroline Tice         {
485d61c10bcSCaroline Tice             out_stream->Printf ("%s\n", g_reader_instructions);
4866611103cSGreg Clayton             if (reader.GetPrompt())
487d61c10bcSCaroline Tice                 out_stream->Printf ("%s", reader.GetPrompt());
488d61c10bcSCaroline Tice             out_stream->Flush();
489d61c10bcSCaroline Tice         }
49030fdc8d8SChris Lattner         break;
49130fdc8d8SChris Lattner 
49230fdc8d8SChris Lattner     case eInputReaderDeactivate:
49330fdc8d8SChris Lattner         break;
49430fdc8d8SChris Lattner 
49530fdc8d8SChris Lattner     case eInputReaderReactivate:
496d61c10bcSCaroline Tice         if (reader.GetPrompt() && !batch_mode)
49704a339a0SCaroline Tice         {
498d61c10bcSCaroline Tice             out_stream->Printf ("%s", reader.GetPrompt());
499d61c10bcSCaroline Tice             out_stream->Flush();
50004a339a0SCaroline Tice         }
50130fdc8d8SChris Lattner         break;
50230fdc8d8SChris Lattner 
503969ed3d1SCaroline Tice     case eInputReaderAsynchronousOutputWritten:
504969ed3d1SCaroline Tice         break;
505969ed3d1SCaroline Tice 
50630fdc8d8SChris Lattner     case eInputReaderGotToken:
50730fdc8d8SChris Lattner         if (bytes && bytes_len && baton)
50830fdc8d8SChris Lattner         {
50930fdc8d8SChris Lattner             BreakpointOptions *bp_options = (BreakpointOptions *) baton;
51030fdc8d8SChris Lattner             if (bp_options)
51130fdc8d8SChris Lattner             {
51230fdc8d8SChris Lattner                 Baton *bp_options_baton = bp_options->GetBaton();
51330fdc8d8SChris Lattner                 if (bp_options_baton)
51430fdc8d8SChris Lattner                     ((BreakpointOptions::CommandData *)bp_options_baton->m_data)->user_source.AppendString (bytes, bytes_len);
51530fdc8d8SChris Lattner             }
51630fdc8d8SChris Lattner         }
517d61c10bcSCaroline Tice         if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
51804a339a0SCaroline Tice         {
519d61c10bcSCaroline Tice             out_stream->Printf ("%s", reader.GetPrompt());
520d61c10bcSCaroline Tice             out_stream->Flush();
52104a339a0SCaroline Tice         }
52230fdc8d8SChris Lattner         break;
52330fdc8d8SChris Lattner 
524efed6131SCaroline Tice     case eInputReaderInterrupt:
525efed6131SCaroline Tice         {
526efed6131SCaroline Tice             // Finish, and cancel the breakpoint command.
527efed6131SCaroline Tice             reader.SetIsDone (true);
528efed6131SCaroline Tice             BreakpointOptions *bp_options = (BreakpointOptions *) baton;
529efed6131SCaroline Tice             if (bp_options)
530efed6131SCaroline Tice             {
531efed6131SCaroline Tice                 Baton *bp_options_baton = bp_options->GetBaton ();
532efed6131SCaroline Tice                 if (bp_options_baton)
533efed6131SCaroline Tice                 {
534efed6131SCaroline Tice                     ((BreakpointOptions::CommandData *) bp_options_baton->m_data)->user_source.Clear();
535a73b7df7SEnrico Granata                     ((BreakpointOptions::CommandData *) bp_options_baton->m_data)->script_source.clear();
536efed6131SCaroline Tice                 }
537efed6131SCaroline Tice             }
538d61c10bcSCaroline Tice             if (!batch_mode)
539d61c10bcSCaroline Tice             {
540d61c10bcSCaroline Tice                 out_stream->Printf ("Warning: No command attached to breakpoint.\n");
541d61c10bcSCaroline Tice                 out_stream->Flush();
542d61c10bcSCaroline Tice             }
543efed6131SCaroline Tice         }
544efed6131SCaroline Tice         break;
545efed6131SCaroline Tice 
546efed6131SCaroline Tice     case eInputReaderEndOfFile:
547efed6131SCaroline Tice         reader.SetIsDone (true);
548efed6131SCaroline Tice         break;
549efed6131SCaroline Tice 
55030fdc8d8SChris Lattner     case eInputReaderDone:
55130fdc8d8SChris Lattner         break;
55230fdc8d8SChris Lattner     }
55330fdc8d8SChris Lattner 
55430fdc8d8SChris Lattner     return bytes_len;
55530fdc8d8SChris Lattner }
55630fdc8d8SChris Lattner 
55730fdc8d8SChris Lattner 
55830fdc8d8SChris Lattner //-------------------------------------------------------------------------
55993e0f19fSCaroline Tice // CommandObjectBreakpointCommandDelete
56030fdc8d8SChris Lattner //-------------------------------------------------------------------------
56130fdc8d8SChris Lattner 
56293e0f19fSCaroline Tice CommandObjectBreakpointCommandDelete::CommandObjectBreakpointCommandDelete (CommandInterpreter &interpreter) :
563a7015092SGreg Clayton     CommandObject (interpreter,
56493e0f19fSCaroline Tice                    "delete",
56593e0f19fSCaroline Tice                    "Delete the set of commands from a breakpoint.",
566405fe67fSCaroline Tice                    NULL)
56730fdc8d8SChris Lattner {
568405fe67fSCaroline Tice     CommandArgumentEntry arg;
569405fe67fSCaroline Tice     CommandArgumentData bp_id_arg;
570405fe67fSCaroline Tice 
571405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
572405fe67fSCaroline Tice     bp_id_arg.arg_type = eArgTypeBreakpointID;
573405fe67fSCaroline Tice     bp_id_arg.arg_repetition = eArgRepeatPlain;
574405fe67fSCaroline Tice 
575405fe67fSCaroline Tice     // There is only one variant this argument could be; put it into the argument entry.
576405fe67fSCaroline Tice     arg.push_back (bp_id_arg);
577405fe67fSCaroline Tice 
578405fe67fSCaroline Tice     // Push the data for the first argument into the m_arguments vector.
579405fe67fSCaroline Tice     m_arguments.push_back (arg);
58030fdc8d8SChris Lattner }
58130fdc8d8SChris Lattner 
58293e0f19fSCaroline Tice CommandObjectBreakpointCommandDelete::~CommandObjectBreakpointCommandDelete ()
58330fdc8d8SChris Lattner {
58430fdc8d8SChris Lattner }
58530fdc8d8SChris Lattner 
58630fdc8d8SChris Lattner bool
58793e0f19fSCaroline Tice CommandObjectBreakpointCommandDelete::Execute
5886611103cSGreg Clayton (
5896611103cSGreg Clayton     Args& command,
5906611103cSGreg Clayton     CommandReturnObject &result
5916611103cSGreg Clayton )
59230fdc8d8SChris Lattner {
593a7015092SGreg Clayton     Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
59430fdc8d8SChris Lattner 
59530fdc8d8SChris Lattner     if (target == NULL)
59630fdc8d8SChris Lattner     {
59793e0f19fSCaroline Tice         result.AppendError ("There is not a current executable; there are no breakpoints from which to delete commands");
59830fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
59930fdc8d8SChris Lattner         return false;
60030fdc8d8SChris Lattner     }
60130fdc8d8SChris Lattner 
60230fdc8d8SChris Lattner     const BreakpointList &breakpoints = target->GetBreakpointList();
60330fdc8d8SChris Lattner     size_t num_breakpoints = breakpoints.GetSize();
60430fdc8d8SChris Lattner 
60530fdc8d8SChris Lattner     if (num_breakpoints == 0)
60630fdc8d8SChris Lattner     {
60793e0f19fSCaroline Tice         result.AppendError ("No breakpoints exist to have commands deleted");
60830fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
60930fdc8d8SChris Lattner         return false;
61030fdc8d8SChris Lattner     }
61130fdc8d8SChris Lattner 
61230fdc8d8SChris Lattner     if (command.GetArgumentCount() == 0)
61330fdc8d8SChris Lattner     {
61493e0f19fSCaroline Tice         result.AppendError ("No breakpoint specified from which to delete the commands");
61530fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
61630fdc8d8SChris Lattner         return false;
61730fdc8d8SChris Lattner     }
61830fdc8d8SChris Lattner 
61930fdc8d8SChris Lattner     BreakpointIDList valid_bp_ids;
62030fdc8d8SChris Lattner     CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
62130fdc8d8SChris Lattner 
62230fdc8d8SChris Lattner     if (result.Succeeded())
62330fdc8d8SChris Lattner     {
624c982c768SGreg Clayton         const size_t count = valid_bp_ids.GetSize();
625c982c768SGreg Clayton         for (size_t i = 0; i < count; ++i)
62630fdc8d8SChris Lattner         {
62730fdc8d8SChris Lattner             BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
62830fdc8d8SChris Lattner             if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
62930fdc8d8SChris Lattner             {
63030fdc8d8SChris Lattner                 Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
63130fdc8d8SChris Lattner                 if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
63230fdc8d8SChris Lattner                 {
63330fdc8d8SChris Lattner                     BreakpointLocationSP bp_loc_sp (bp->FindLocationByID (cur_bp_id.GetLocationID()));
63430fdc8d8SChris Lattner                     if (bp_loc_sp)
63530fdc8d8SChris Lattner                         bp_loc_sp->ClearCallback();
63630fdc8d8SChris Lattner                     else
63730fdc8d8SChris Lattner                     {
63830fdc8d8SChris Lattner                         result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
63930fdc8d8SChris Lattner                                                      cur_bp_id.GetBreakpointID(),
64030fdc8d8SChris Lattner                                                      cur_bp_id.GetLocationID());
64130fdc8d8SChris Lattner                         result.SetStatus (eReturnStatusFailed);
64230fdc8d8SChris Lattner                         return false;
64330fdc8d8SChris Lattner                     }
64430fdc8d8SChris Lattner                 }
64530fdc8d8SChris Lattner                 else
64630fdc8d8SChris Lattner                 {
64730fdc8d8SChris Lattner                     bp->ClearCallback();
64830fdc8d8SChris Lattner                 }
64930fdc8d8SChris Lattner             }
65030fdc8d8SChris Lattner         }
65130fdc8d8SChris Lattner     }
65230fdc8d8SChris Lattner     return result.Succeeded();
65330fdc8d8SChris Lattner }
65430fdc8d8SChris Lattner 
65530fdc8d8SChris Lattner 
65630fdc8d8SChris Lattner //-------------------------------------------------------------------------
65730fdc8d8SChris Lattner // CommandObjectBreakpointCommandList
65830fdc8d8SChris Lattner //-------------------------------------------------------------------------
65930fdc8d8SChris Lattner 
660a7015092SGreg Clayton CommandObjectBreakpointCommandList::CommandObjectBreakpointCommandList (CommandInterpreter &interpreter) :
661a7015092SGreg Clayton     CommandObject (interpreter,
662a7015092SGreg Clayton                    "list",
66330fdc8d8SChris Lattner                    "List the script or set of commands to be executed when the breakpoint is hit.",
664405fe67fSCaroline Tice                     NULL)
66530fdc8d8SChris Lattner {
666405fe67fSCaroline Tice     CommandArgumentEntry arg;
667405fe67fSCaroline Tice     CommandArgumentData bp_id_arg;
668405fe67fSCaroline Tice 
669405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
670405fe67fSCaroline Tice     bp_id_arg.arg_type = eArgTypeBreakpointID;
671405fe67fSCaroline Tice     bp_id_arg.arg_repetition = eArgRepeatPlain;
672405fe67fSCaroline Tice 
673405fe67fSCaroline Tice     // There is only one variant this argument could be; put it into the argument entry.
674405fe67fSCaroline Tice     arg.push_back (bp_id_arg);
675405fe67fSCaroline Tice 
676405fe67fSCaroline Tice     // Push the data for the first argument into the m_arguments vector.
677405fe67fSCaroline Tice     m_arguments.push_back (arg);
67830fdc8d8SChris Lattner }
67930fdc8d8SChris Lattner 
68030fdc8d8SChris Lattner CommandObjectBreakpointCommandList::~CommandObjectBreakpointCommandList ()
68130fdc8d8SChris Lattner {
68230fdc8d8SChris Lattner }
68330fdc8d8SChris Lattner 
68430fdc8d8SChris Lattner bool
6856611103cSGreg Clayton CommandObjectBreakpointCommandList::Execute
6866611103cSGreg Clayton (
6876611103cSGreg Clayton     Args& command,
6886611103cSGreg Clayton     CommandReturnObject &result
6896611103cSGreg Clayton )
69030fdc8d8SChris Lattner {
691a7015092SGreg Clayton     Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
69230fdc8d8SChris Lattner 
69330fdc8d8SChris Lattner     if (target == NULL)
69430fdc8d8SChris Lattner     {
69530fdc8d8SChris Lattner         result.AppendError ("There is not a current executable; there are no breakpoints for which to list commands");
69630fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
69730fdc8d8SChris Lattner         return false;
69830fdc8d8SChris Lattner     }
69930fdc8d8SChris Lattner 
70030fdc8d8SChris Lattner     const BreakpointList &breakpoints = target->GetBreakpointList();
70130fdc8d8SChris Lattner     size_t num_breakpoints = breakpoints.GetSize();
70230fdc8d8SChris Lattner 
70330fdc8d8SChris Lattner     if (num_breakpoints == 0)
70430fdc8d8SChris Lattner     {
70530fdc8d8SChris Lattner         result.AppendError ("No breakpoints exist for which to list commands");
70630fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
70730fdc8d8SChris Lattner         return false;
70830fdc8d8SChris Lattner     }
70930fdc8d8SChris Lattner 
71030fdc8d8SChris Lattner     if (command.GetArgumentCount() == 0)
71130fdc8d8SChris Lattner     {
71230fdc8d8SChris Lattner         result.AppendError ("No breakpoint specified for which to list the commands");
71330fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
71430fdc8d8SChris Lattner         return false;
71530fdc8d8SChris Lattner     }
71630fdc8d8SChris Lattner 
71730fdc8d8SChris Lattner     BreakpointIDList valid_bp_ids;
71830fdc8d8SChris Lattner     CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
71930fdc8d8SChris Lattner 
72030fdc8d8SChris Lattner     if (result.Succeeded())
72130fdc8d8SChris Lattner     {
722c982c768SGreg Clayton         const size_t count = valid_bp_ids.GetSize();
723c982c768SGreg Clayton         for (size_t i = 0; i < count; ++i)
72430fdc8d8SChris Lattner         {
72530fdc8d8SChris Lattner             BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
72630fdc8d8SChris Lattner             if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
72730fdc8d8SChris Lattner             {
72830fdc8d8SChris Lattner                 Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
72930fdc8d8SChris Lattner 
73030fdc8d8SChris Lattner                 if (bp)
73130fdc8d8SChris Lattner                 {
7321b54c88cSJim Ingham                     const BreakpointOptions *bp_options = NULL;
73330fdc8d8SChris Lattner                     if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
73430fdc8d8SChris Lattner                     {
73530fdc8d8SChris Lattner                         BreakpointLocationSP bp_loc_sp(bp->FindLocationByID (cur_bp_id.GetLocationID()));
73630fdc8d8SChris Lattner                         if (bp_loc_sp)
73705407f6bSJim Ingham                             bp_options = bp_loc_sp->GetOptionsNoCreate();
73830fdc8d8SChris Lattner                         else
73930fdc8d8SChris Lattner                         {
74030fdc8d8SChris Lattner                             result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
74130fdc8d8SChris Lattner                                                          cur_bp_id.GetBreakpointID(),
74230fdc8d8SChris Lattner                                                          cur_bp_id.GetLocationID());
74330fdc8d8SChris Lattner                             result.SetStatus (eReturnStatusFailed);
74430fdc8d8SChris Lattner                             return false;
74530fdc8d8SChris Lattner                         }
74630fdc8d8SChris Lattner                     }
74730fdc8d8SChris Lattner                     else
74830fdc8d8SChris Lattner                     {
74930fdc8d8SChris Lattner                         bp_options = bp->GetOptions();
75030fdc8d8SChris Lattner                     }
75130fdc8d8SChris Lattner 
75230fdc8d8SChris Lattner                     if (bp_options)
75330fdc8d8SChris Lattner                     {
75430fdc8d8SChris Lattner                         StreamString id_str;
755e16c50a1SJim Ingham                         BreakpointID::GetCanonicalReference (&id_str,
756e16c50a1SJim Ingham                                                              cur_bp_id.GetBreakpointID(),
757e16c50a1SJim Ingham                                                              cur_bp_id.GetLocationID());
7581b54c88cSJim Ingham                         const Baton *baton = bp_options->GetBaton();
75930fdc8d8SChris Lattner                         if (baton)
76030fdc8d8SChris Lattner                         {
76130fdc8d8SChris Lattner                             result.GetOutputStream().Printf ("Breakpoint %s:\n", id_str.GetData());
76230fdc8d8SChris Lattner                             result.GetOutputStream().IndentMore ();
76330fdc8d8SChris Lattner                             baton->GetDescription(&result.GetOutputStream(), eDescriptionLevelFull);
76430fdc8d8SChris Lattner                             result.GetOutputStream().IndentLess ();
76530fdc8d8SChris Lattner                         }
76630fdc8d8SChris Lattner                         else
76730fdc8d8SChris Lattner                         {
768e16c50a1SJim Ingham                             result.AppendMessageWithFormat ("Breakpoint %s does not have an associated command.\n",
769e16c50a1SJim Ingham                                                             id_str.GetData());
77030fdc8d8SChris Lattner                         }
77130fdc8d8SChris Lattner                     }
77230fdc8d8SChris Lattner                     result.SetStatus (eReturnStatusSuccessFinishResult);
77330fdc8d8SChris Lattner                 }
77430fdc8d8SChris Lattner                 else
77530fdc8d8SChris Lattner                 {
77630fdc8d8SChris Lattner                     result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n", cur_bp_id.GetBreakpointID());
77730fdc8d8SChris Lattner                     result.SetStatus (eReturnStatusFailed);
77830fdc8d8SChris Lattner                 }
77930fdc8d8SChris Lattner 
78030fdc8d8SChris Lattner             }
78130fdc8d8SChris Lattner         }
78230fdc8d8SChris Lattner     }
78330fdc8d8SChris Lattner 
78430fdc8d8SChris Lattner     return result.Succeeded();
78530fdc8d8SChris Lattner }
78630fdc8d8SChris Lattner 
78730fdc8d8SChris Lattner //-------------------------------------------------------------------------
78830fdc8d8SChris Lattner // CommandObjectBreakpointCommand
78930fdc8d8SChris Lattner //-------------------------------------------------------------------------
79030fdc8d8SChris Lattner 
7916611103cSGreg Clayton CommandObjectBreakpointCommand::CommandObjectBreakpointCommand (CommandInterpreter &interpreter) :
792a7015092SGreg Clayton     CommandObjectMultiword (interpreter,
793a7015092SGreg Clayton                             "command",
79430fdc8d8SChris Lattner                             "A set of commands for adding, removing and examining bits of code to be executed when the breakpoint is hit (breakpoint 'commmands').",
79530fdc8d8SChris Lattner                             "command <sub-command> [<sub-command-options>] <breakpoint-id>")
79630fdc8d8SChris Lattner {
79730fdc8d8SChris Lattner     bool status;
798a7015092SGreg Clayton     CommandObjectSP add_command_object (new CommandObjectBreakpointCommandAdd (interpreter));
79993e0f19fSCaroline Tice     CommandObjectSP delete_command_object (new CommandObjectBreakpointCommandDelete (interpreter));
800a7015092SGreg Clayton     CommandObjectSP list_command_object (new CommandObjectBreakpointCommandList (interpreter));
80130fdc8d8SChris Lattner 
80230fdc8d8SChris Lattner     add_command_object->SetCommandName ("breakpoint command add");
80393e0f19fSCaroline Tice     delete_command_object->SetCommandName ("breakpoint command delete");
80430fdc8d8SChris Lattner     list_command_object->SetCommandName ("breakpoint command list");
80530fdc8d8SChris Lattner 
806a7015092SGreg Clayton     status = LoadSubCommand ("add",    add_command_object);
80793e0f19fSCaroline Tice     status = LoadSubCommand ("delete", delete_command_object);
808a7015092SGreg Clayton     status = LoadSubCommand ("list",   list_command_object);
80930fdc8d8SChris Lattner }
81030fdc8d8SChris Lattner 
81130fdc8d8SChris Lattner 
81230fdc8d8SChris Lattner CommandObjectBreakpointCommand::~CommandObjectBreakpointCommand ()
81330fdc8d8SChris Lattner {
81430fdc8d8SChris Lattner }
81530fdc8d8SChris Lattner 
81630fdc8d8SChris Lattner bool
81730fdc8d8SChris Lattner CommandObjectBreakpointCommand::BreakpointOptionsCallbackFunction
81830fdc8d8SChris Lattner (
81930fdc8d8SChris Lattner     void *baton,
82030fdc8d8SChris Lattner     StoppointCallbackContext *context,
82130fdc8d8SChris Lattner     lldb::user_id_t break_id,
82230fdc8d8SChris Lattner     lldb::user_id_t break_loc_id
82330fdc8d8SChris Lattner )
82430fdc8d8SChris Lattner {
82530fdc8d8SChris Lattner     bool ret_value = true;
82630fdc8d8SChris Lattner     if (baton == NULL)
82730fdc8d8SChris Lattner         return true;
82830fdc8d8SChris Lattner 
82930fdc8d8SChris Lattner 
83030fdc8d8SChris Lattner     BreakpointOptions::CommandData *data = (BreakpointOptions::CommandData *) baton;
83130fdc8d8SChris Lattner     StringList &commands = data->user_source;
83230fdc8d8SChris Lattner 
83330fdc8d8SChris Lattner     if (commands.GetSize() > 0)
83430fdc8d8SChris Lattner     {
8351ac04c30SGreg Clayton         ExecutionContext exe_ctx (context->exe_ctx_ref);
8361ac04c30SGreg Clayton         Target *target = exe_ctx.GetTargetPtr();
837c14ee32dSGreg Clayton         if (target)
8386611103cSGreg Clayton         {
83985e8b814SJim Ingham             CommandReturnObject result;
840c14ee32dSGreg Clayton             Debugger &debugger = target->GetDebugger();
84185e8b814SJim Ingham             // Rig up the results secondary output stream to the debugger's, so the output will come out synchronously
84285e8b814SJim Ingham             // if the debugger is set up that way.
84385e8b814SJim Ingham 
8445b52f0c7SJim Ingham             StreamSP output_stream (debugger.GetAsyncOutputStream());
8455b52f0c7SJim Ingham             StreamSP error_stream (debugger.GetAsyncErrorStream());
846b5059accSCaroline Tice             result.SetImmediateOutputStream (output_stream);
847b5059accSCaroline Tice             result.SetImmediateErrorStream (error_stream);
84830fdc8d8SChris Lattner 
849e16c50a1SJim Ingham             bool stop_on_continue = true;
850e16c50a1SJim Ingham             bool echo_commands    = false;
851e16c50a1SJim Ingham             bool print_results    = true;
85230fdc8d8SChris Lattner 
853e16c50a1SJim Ingham             debugger.GetCommandInterpreter().HandleCommands (commands,
8541ac04c30SGreg Clayton                                                              &exe_ctx,
855e16c50a1SJim Ingham                                                              stop_on_continue,
856e16c50a1SJim Ingham                                                              data->stop_on_error,
857e16c50a1SJim Ingham                                                              echo_commands,
858e16c50a1SJim Ingham                                                              print_results,
859e16c50a1SJim Ingham                                                              result);
860b5059accSCaroline Tice             result.GetImmediateOutputStream()->Flush();
861b5059accSCaroline Tice             result.GetImmediateErrorStream()->Flush();
86230fdc8d8SChris Lattner        }
8636611103cSGreg Clayton     }
86430fdc8d8SChris Lattner     return ret_value;
86530fdc8d8SChris Lattner }
86630fdc8d8SChris Lattner 
867