CommandObjectBreakpointCommand.cpp revision c1ca9dcd0e8a1c7c5a882281afdd2a5145026fd0
124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//===-- CommandObjectBreakpointCommand.cpp ----------------------*- C++ -*-===//
224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//
324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//                     The LLVM Compiler Infrastructure
424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//
524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// This file is distributed under the University of Illinois Open Source
624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// License. See LICENSE.TXT for details.
724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//
824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//===----------------------------------------------------------------------===//
924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// C Includes
1124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// C++ Includes
1224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "CommandObjectBreakpointCommand.h"
1524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "CommandObjectBreakpoint.h"
1624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Interpreter/CommandInterpreter.h"
1824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Interpreter/CommandReturnObject.h"
1924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Target/Target.h"
2024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Target/Thread.h"
2124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Breakpoint/BreakpointIDList.h"
2224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Breakpoint/Breakpoint.h"
2324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Breakpoint/BreakpointLocation.h"
2424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Breakpoint/StoppointCallbackContext.h"
2524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Core/State.h"
2624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
2724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerusing namespace lldb;
2824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerusing namespace lldb_private;
2924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
3024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
3124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// CommandObjectBreakpointCommandAdd
3224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
3324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
3424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
35da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectBreakpointCommandAdd : public CommandObjectParsed
3624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
37da26bd203cbb104291b39891febf7481794f205fJim Inghampublic:
38da26bd203cbb104291b39891febf7481794f205fJim Ingham
39da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandObjectBreakpointCommandAdd (CommandInterpreter &interpreter) :
40da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectParsed (interpreter,
41da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "add",
42da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "Add a set of commands to a breakpoint, to be executed whenever the breakpoint is hit.",
43da26bd203cbb104291b39891febf7481794f205fJim Ingham                             NULL),
44da26bd203cbb104291b39891febf7481794f205fJim Ingham        m_options (interpreter)
45da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
46da26bd203cbb104291b39891febf7481794f205fJim Ingham        SetHelpLong (
4724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner"\nGeneral information about entering breakpoint commands \n\
4824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner------------------------------------------------------ \n\
4924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner \n\
5024943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerThis command will cause you to be prompted to enter the command or set \n\
5124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerof commands you wish to be executed when the specified breakpoint is \n\
5224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerhit.  You will be told to enter your command(s), and will see a '> ' \n\
5324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerprompt. Because you can enter one or many commands to be executed when \n\
5424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnera breakpoint is hit, you will continue to be prompted after each \n\
5524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnernew-line that you enter, until you enter the word 'DONE', which will \n\
5624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnercause the commands you have entered to be stored with the breakpoint \n\
5724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerand executed when the breakpoint is hit. \n\
5824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner \n\
5924943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerSyntax checking is not necessarily done when breakpoint commands are \n\
6024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerentered.  An improperly written breakpoint command will attempt to get \n\
6124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerexecuted when the breakpoint gets hit, and usually silently fail.  If \n\
6224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattneryour breakpoint command does not appear to be getting executed, go \n\
6324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerback and check your syntax. \n\
6424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner \n\
6524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner \n\
6673260dafe670e2ab6c993ef3518fb49caf382108Jim InghamSpecial information about PYTHON breakpoint commands                            \n\
6773260dafe670e2ab6c993ef3518fb49caf382108Jim Ingham----------------------------------------------------                            \n\
6873260dafe670e2ab6c993ef3518fb49caf382108Jim Ingham                                                                                \n\
6973260dafe670e2ab6c993ef3518fb49caf382108Jim InghamYou may enter either one line of Python or multiple lines of Python             \n\
7073260dafe670e2ab6c993ef3518fb49caf382108Jim Ingham(including defining whole functions, if desired).  If you enter a               \n\
7173260dafe670e2ab6c993ef3518fb49caf382108Jim Inghamsingle line of Python, that will be passed to the Python interpreter            \n\
7273260dafe670e2ab6c993ef3518fb49caf382108Jim Ingham'as is' when the breakpoint gets hit.  If you enter function                    \n\
7373260dafe670e2ab6c993ef3518fb49caf382108Jim Inghamdefinitions, they will be passed to the Python interpreter as soon as           \n\
7473260dafe670e2ab6c993ef3518fb49caf382108Jim Inghamyou finish entering the breakpoint command, and they can be called              \n\
7573260dafe670e2ab6c993ef3518fb49caf382108Jim Inghamlater (don't forget to add calls to them, if you want them called when          \n\
7673260dafe670e2ab6c993ef3518fb49caf382108Jim Inghamthe breakpoint is hit).  If you enter multiple lines of Python that             \n\
7773260dafe670e2ab6c993ef3518fb49caf382108Jim Inghamare not function definitions, they will be collected into a new,                \n\
7873260dafe670e2ab6c993ef3518fb49caf382108Jim Inghamautomatically generated Python function, and a call to the newly                \n\
7973260dafe670e2ab6c993ef3518fb49caf382108Jim Inghamgenerated function will be attached to the breakpoint.                          \n\
8073260dafe670e2ab6c993ef3518fb49caf382108Jim Ingham                                                                                \n\
8173260dafe670e2ab6c993ef3518fb49caf382108Jim InghamThis auto-generated function is passed in two arguments:                        \n\
8273260dafe670e2ab6c993ef3518fb49caf382108Jim Ingham                                                                                \n\
8373260dafe670e2ab6c993ef3518fb49caf382108Jim Ingham    frame:  an SBFrame object representing the frame which hit the breakpoint.  \n\
8473260dafe670e2ab6c993ef3518fb49caf382108Jim Ingham            From the frame you can get back to the thread and process.          \n\
8573260dafe670e2ab6c993ef3518fb49caf382108Jim Ingham    bp_loc: the number of the breakpoint location that was hit.                 \n\
8673260dafe670e2ab6c993ef3518fb49caf382108Jim Ingham            This is useful since one breakpoint can have many locations.        \n\
8773260dafe670e2ab6c993ef3518fb49caf382108Jim Ingham                                                                                \n\
8873260dafe670e2ab6c993ef3518fb49caf382108Jim InghamImportant Note: Because loose Python code gets collected into functions,        \n\
8973260dafe670e2ab6c993ef3518fb49caf382108Jim Inghamif you want to access global variables in the 'loose' code, you need to         \n\
9073260dafe670e2ab6c993ef3518fb49caf382108Jim Inghamspecify that they are global, using the 'global' keyword.  Be sure to           \n\
9173260dafe670e2ab6c993ef3518fb49caf382108Jim Inghamuse correct Python syntax, including indentation, when entering Python          \n\
9273260dafe670e2ab6c993ef3518fb49caf382108Jim Inghambreakpoint commands.                                                            \n\
9373260dafe670e2ab6c993ef3518fb49caf382108Jim Ingham                                                                                \n\
9473260dafe670e2ab6c993ef3518fb49caf382108Jim InghamAs a third option, you can pass the name of an already existing Python function \n\
9573260dafe670e2ab6c993ef3518fb49caf382108Jim Inghamand that function will be attached to the breakpoint. It will get passed the    \n\
9673260dafe670e2ab6c993ef3518fb49caf382108Jim Inghamframe and bp_loc arguments mentioned above.                                     \n\
9773260dafe670e2ab6c993ef3518fb49caf382108Jim Ingham                                                                                \n\
9824943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerExample Python one-line breakpoint command: \n\
9924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner \n\
100949d5acde6684b5824a26034457410cdf3823dfeJim Ingham(lldb) breakpoint command add -s python 1 \n\
10124943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerEnter your Python command(s). Type 'DONE' to end. \n\
10224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner> print \"Hit this breakpoint!\" \n\
10324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner> DONE \n\
10424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner \n\
1054c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny ChenAs a convenience, this also works for a short Python one-liner: \n\
106949d5acde6684b5824a26034457410cdf3823dfeJim Ingham(lldb) breakpoint command add -s python 1 -o \"import time; print time.asctime()\" \n\
1074c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen(lldb) run \n\
1084c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny ChenLaunching '.../a.out'  (x86_64) \n\
1094c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen(lldb) Fri Sep 10 12:17:45 2010 \n\
1104c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny ChenProcess 21778 Stopped \n\
1114c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny 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\
1124c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen  36   	\n\
1134c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen  37   	int c(int val)\n\
1144c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen  38   	{\n\
1154c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen  39 ->	    return val + 3;\n\
1164c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen  40   	}\n\
1174c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen  41   	\n\
1184c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen  42   	int main (int argc, char const *argv[])\n\
1194c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen(lldb) \n\
1204c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen \n\
12124943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerExample multiple line Python breakpoint command, using function definition: \n\
12224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner \n\
123949d5acde6684b5824a26034457410cdf3823dfeJim Ingham(lldb) breakpoint command add -s python 1 \n\
12424943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerEnter your Python command(s). Type 'DONE' to end. \n\
12524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner> def breakpoint_output (bp_no): \n\
12624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner>     out_string = \"Hit breakpoint number \" + repr (bp_no) \n\
12724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner>     print out_string \n\
12824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner>     return True \n\
12924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner> breakpoint_output (1) \n\
13024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner> DONE \n\
13124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner \n\
13224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner \n\
13324943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerExample multiple line Python breakpoint command, using 'loose' Python: \n\
13424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner \n\
135949d5acde6684b5824a26034457410cdf3823dfeJim Ingham(lldb) breakpoint command add -s p 1 \n\
13624943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerEnter your Python command(s). Type 'DONE' to end. \n\
13724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner> global bp_count \n\
13824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner> bp_count = bp_count + 1 \n\
13924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner> print \"Hit this breakpoint \" + repr(bp_count) + \" times!\" \n\
14024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner> DONE \n\
14124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner \n\
14224943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerIn this case, since there is a reference to a global variable, \n\
14324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner'bp_count', you will also need to make sure 'bp_count' exists and is \n\
14424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerinitialized: \n\
14524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner \n\
14624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner(lldb) script \n\
14724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner>>> bp_count = 0 \n\
14824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner>>> quit() \n\
14924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner \n\
15024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner(lldb)  \n\
15124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner \n\
152b447e84828dda036b599108e0ebdb29fd505571dCaroline Tice \n\
153b447e84828dda036b599108e0ebdb29fd505571dCaroline TiceFinal Note:  If you get a warning that no breakpoint command was generated, \n\
154b447e84828dda036b599108e0ebdb29fd505571dCaroline Ticebut you did not get any syntax errors, you probably forgot to add a call \n\
155b447e84828dda036b599108e0ebdb29fd505571dCaroline Ticeto your functions. \n\
156b447e84828dda036b599108e0ebdb29fd505571dCaroline Tice \n\
157abb507ae79e20ab2dd8365049921df73a24c95a5Caroline TiceSpecial information about debugger command breakpoint commands \n\
158abb507ae79e20ab2dd8365049921df73a24c95a5Caroline Tice-------------------------------------------------------------- \n\
15924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner \n\
16024943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerYou may enter any debugger command, exactly as you would at the \n\
16124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerdebugger prompt.  You may enter as many debugger commands as you like, \n\
16224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerbut do NOT enter more than one command per line. \n" );
16343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
164da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentEntry arg;
165da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentData bp_id_arg;
16643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
167da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Define the first (and only) variant of this arg.
168da26bd203cbb104291b39891febf7481794f205fJim Ingham        bp_id_arg.arg_type = eArgTypeBreakpointID;
169da26bd203cbb104291b39891febf7481794f205fJim Ingham        bp_id_arg.arg_repetition = eArgRepeatPlain;
17043b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
171da26bd203cbb104291b39891febf7481794f205fJim Ingham        // There is only one variant this argument could be; put it into the argument entry.
172da26bd203cbb104291b39891febf7481794f205fJim Ingham        arg.push_back (bp_id_arg);
17343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
174da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Push the data for the first argument into the m_arguments vector.
175da26bd203cbb104291b39891febf7481794f205fJim Ingham        m_arguments.push_back (arg);
17624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
17724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
178da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual
179da26bd203cbb104291b39891febf7481794f205fJim Ingham    ~CommandObjectBreakpointCommandAdd () {}
18024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
181da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual Options *
182da26bd203cbb104291b39891febf7481794f205fJim Ingham    GetOptions ()
18324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
184da26bd203cbb104291b39891febf7481794f205fJim Ingham        return &m_options;
18524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
18624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
187da26bd203cbb104291b39891febf7481794f205fJim Ingham    void
188da26bd203cbb104291b39891febf7481794f205fJim Ingham    CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
189da26bd203cbb104291b39891febf7481794f205fJim Ingham                                             CommandReturnObject &result)
1908ad21a888d8185b929c68fec52b571d823377b34Enrico Granata    {
191da26bd203cbb104291b39891febf7481794f205fJim Ingham        InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
192da26bd203cbb104291b39891febf7481794f205fJim Ingham        std::auto_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
193da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (reader_sp && data_ap.get())
194da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
195da26bd203cbb104291b39891febf7481794f205fJim Ingham            BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
196da26bd203cbb104291b39891febf7481794f205fJim Ingham            bp_options->SetCallback (BreakpointOptionsCallbackFunction, baton_sp);
197da26bd203cbb104291b39891febf7481794f205fJim Ingham
198da26bd203cbb104291b39891febf7481794f205fJim Ingham            Error err (reader_sp->Initialize (CommandObjectBreakpointCommandAdd::GenerateBreakpointCommandCallback,
199da26bd203cbb104291b39891febf7481794f205fJim Ingham                                              bp_options,                   // baton
200da26bd203cbb104291b39891febf7481794f205fJim Ingham                                              eInputReaderGranularityLine,  // token size, to pass to callback function
201da26bd203cbb104291b39891febf7481794f205fJim Ingham                                              "DONE",                       // end token
202da26bd203cbb104291b39891febf7481794f205fJim Ingham                                              "> ",                         // prompt
203da26bd203cbb104291b39891febf7481794f205fJim Ingham                                              true));                       // echo input
204da26bd203cbb104291b39891febf7481794f205fJim Ingham            if (err.Success())
205da26bd203cbb104291b39891febf7481794f205fJim Ingham            {
206da26bd203cbb104291b39891febf7481794f205fJim Ingham                m_interpreter.GetDebugger().PushInputReader (reader_sp);
207da26bd203cbb104291b39891febf7481794f205fJim Ingham                result.SetStatus (eReturnStatusSuccessFinishNoResult);
208da26bd203cbb104291b39891febf7481794f205fJim Ingham            }
209da26bd203cbb104291b39891febf7481794f205fJim Ingham            else
210da26bd203cbb104291b39891febf7481794f205fJim Ingham            {
211da26bd203cbb104291b39891febf7481794f205fJim Ingham                result.AppendError (err.AsCString());
212da26bd203cbb104291b39891febf7481794f205fJim Ingham                result.SetStatus (eReturnStatusFailed);
213da26bd203cbb104291b39891febf7481794f205fJim Ingham            }
214da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
215da26bd203cbb104291b39891febf7481794f205fJim Ingham        else
216da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
217da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError("out of memory");
218da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
219da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
220da26bd203cbb104291b39891febf7481794f205fJim Ingham
2218ad21a888d8185b929c68fec52b571d823377b34Enrico Granata    }
2228ad21a888d8185b929c68fec52b571d823377b34Enrico Granata
223da26bd203cbb104291b39891febf7481794f205fJim Ingham    /// Set a one-liner as the callback for the breakpoint.
224da26bd203cbb104291b39891febf7481794f205fJim Ingham    void
225da26bd203cbb104291b39891febf7481794f205fJim Ingham    SetBreakpointCommandCallback (BreakpointOptions *bp_options,
226da26bd203cbb104291b39891febf7481794f205fJim Ingham                                  const char *oneliner)
227da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
228da26bd203cbb104291b39891febf7481794f205fJim Ingham        std::auto_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
229da26bd203cbb104291b39891febf7481794f205fJim Ingham
230da26bd203cbb104291b39891febf7481794f205fJim Ingham        // It's necessary to set both user_source and script_source to the oneliner.
231da26bd203cbb104291b39891febf7481794f205fJim Ingham        // The former is used to generate callback description (as in breakpoint command list)
232da26bd203cbb104291b39891febf7481794f205fJim Ingham        // while the latter is used for Python to interpret during the actual callback.
233da26bd203cbb104291b39891febf7481794f205fJim Ingham        data_ap->user_source.AppendString (oneliner);
234da26bd203cbb104291b39891febf7481794f205fJim Ingham        data_ap->script_source.assign (oneliner);
235da26bd203cbb104291b39891febf7481794f205fJim Ingham        data_ap->stop_on_error = m_options.m_stop_on_error;
236da26bd203cbb104291b39891febf7481794f205fJim Ingham
237da26bd203cbb104291b39891febf7481794f205fJim Ingham        BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
238da26bd203cbb104291b39891febf7481794f205fJim Ingham        bp_options->SetCallback (BreakpointOptionsCallbackFunction, baton_sp);
239da26bd203cbb104291b39891febf7481794f205fJim Ingham
240da26bd203cbb104291b39891febf7481794f205fJim Ingham        return;
241da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
24224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
243da26bd203cbb104291b39891febf7481794f205fJim Ingham    static size_t
244da26bd203cbb104291b39891febf7481794f205fJim Ingham    GenerateBreakpointCommandCallback (void *baton,
245da26bd203cbb104291b39891febf7481794f205fJim Ingham                                       InputReader &reader,
246da26bd203cbb104291b39891febf7481794f205fJim Ingham                                       lldb::InputReaderAction notification,
247da26bd203cbb104291b39891febf7481794f205fJim Ingham                                       const char *bytes,
248da26bd203cbb104291b39891febf7481794f205fJim Ingham                                       size_t bytes_len)
24924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
250da26bd203cbb104291b39891febf7481794f205fJim Ingham        StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
251da26bd203cbb104291b39891febf7481794f205fJim Ingham        bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
252da26bd203cbb104291b39891febf7481794f205fJim Ingham
253da26bd203cbb104291b39891febf7481794f205fJim Ingham        switch (notification)
25424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
255da26bd203cbb104291b39891febf7481794f205fJim Ingham        case eInputReaderActivate:
256da26bd203cbb104291b39891febf7481794f205fJim Ingham            if (!batch_mode)
25724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            {
258da26bd203cbb104291b39891febf7481794f205fJim Ingham                out_stream->Printf ("%s\n", g_reader_instructions);
259da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (reader.GetPrompt())
260da26bd203cbb104291b39891febf7481794f205fJim Ingham                    out_stream->Printf ("%s", reader.GetPrompt());
261da26bd203cbb104291b39891febf7481794f205fJim Ingham                out_stream->Flush();
262da26bd203cbb104291b39891febf7481794f205fJim Ingham            }
263da26bd203cbb104291b39891febf7481794f205fJim Ingham            break;
264b81ed0d482fa374fd90e8bb591d0d9c28180559cJohnny Chen
265da26bd203cbb104291b39891febf7481794f205fJim Ingham        case eInputReaderDeactivate:
266da26bd203cbb104291b39891febf7481794f205fJim Ingham            break;
267b81ed0d482fa374fd90e8bb591d0d9c28180559cJohnny Chen
268da26bd203cbb104291b39891febf7481794f205fJim Ingham        case eInputReaderReactivate:
269da26bd203cbb104291b39891febf7481794f205fJim Ingham            if (reader.GetPrompt() && !batch_mode)
270da26bd203cbb104291b39891febf7481794f205fJim Ingham            {
271da26bd203cbb104291b39891febf7481794f205fJim Ingham                out_stream->Printf ("%s", reader.GetPrompt());
272da26bd203cbb104291b39891febf7481794f205fJim Ingham                out_stream->Flush();
273da26bd203cbb104291b39891febf7481794f205fJim Ingham            }
274da26bd203cbb104291b39891febf7481794f205fJim Ingham            break;
275da26bd203cbb104291b39891febf7481794f205fJim Ingham
276da26bd203cbb104291b39891febf7481794f205fJim Ingham        case eInputReaderAsynchronousOutputWritten:
277da26bd203cbb104291b39891febf7481794f205fJim Ingham            break;
278da26bd203cbb104291b39891febf7481794f205fJim Ingham
279da26bd203cbb104291b39891febf7481794f205fJim Ingham        case eInputReaderGotToken:
280da26bd203cbb104291b39891febf7481794f205fJim Ingham            if (bytes && bytes_len && baton)
281da26bd203cbb104291b39891febf7481794f205fJim Ingham            {
282da26bd203cbb104291b39891febf7481794f205fJim Ingham                BreakpointOptions *bp_options = (BreakpointOptions *) baton;
283da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (bp_options)
284b81ed0d482fa374fd90e8bb591d0d9c28180559cJohnny Chen                {
285da26bd203cbb104291b39891febf7481794f205fJim Ingham                    Baton *bp_options_baton = bp_options->GetBaton();
286da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (bp_options_baton)
287da26bd203cbb104291b39891febf7481794f205fJim Ingham                        ((BreakpointOptions::CommandData *)bp_options_baton->m_data)->user_source.AppendString (bytes, bytes_len);
288da26bd203cbb104291b39891febf7481794f205fJim Ingham                }
289da26bd203cbb104291b39891febf7481794f205fJim Ingham            }
290da26bd203cbb104291b39891febf7481794f205fJim Ingham            if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
291da26bd203cbb104291b39891febf7481794f205fJim Ingham            {
292da26bd203cbb104291b39891febf7481794f205fJim Ingham                out_stream->Printf ("%s", reader.GetPrompt());
293da26bd203cbb104291b39891febf7481794f205fJim Ingham                out_stream->Flush();
294da26bd203cbb104291b39891febf7481794f205fJim Ingham            }
295da26bd203cbb104291b39891febf7481794f205fJim Ingham            break;
296da26bd203cbb104291b39891febf7481794f205fJim Ingham
297da26bd203cbb104291b39891febf7481794f205fJim Ingham        case eInputReaderInterrupt:
298da26bd203cbb104291b39891febf7481794f205fJim Ingham            {
299da26bd203cbb104291b39891febf7481794f205fJim Ingham                // Finish, and cancel the breakpoint command.
300da26bd203cbb104291b39891febf7481794f205fJim Ingham                reader.SetIsDone (true);
301da26bd203cbb104291b39891febf7481794f205fJim Ingham                BreakpointOptions *bp_options = (BreakpointOptions *) baton;
302da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (bp_options)
303da26bd203cbb104291b39891febf7481794f205fJim Ingham                {
304da26bd203cbb104291b39891febf7481794f205fJim Ingham                    Baton *bp_options_baton = bp_options->GetBaton ();
305da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (bp_options_baton)
3068ad21a888d8185b929c68fec52b571d823377b34Enrico Granata                    {
307da26bd203cbb104291b39891febf7481794f205fJim Ingham                        ((BreakpointOptions::CommandData *) bp_options_baton->m_data)->user_source.Clear();
308da26bd203cbb104291b39891febf7481794f205fJim Ingham                        ((BreakpointOptions::CommandData *) bp_options_baton->m_data)->script_source.clear();
3098ad21a888d8185b929c68fec52b571d823377b34Enrico Granata                    }
31024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
311da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (!batch_mode)
31224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
313da26bd203cbb104291b39891febf7481794f205fJim Ingham                    out_stream->Printf ("Warning: No command attached to breakpoint.\n");
314da26bd203cbb104291b39891febf7481794f205fJim Ingham                    out_stream->Flush();
31524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
31624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            }
317da26bd203cbb104291b39891febf7481794f205fJim Ingham            break;
318da26bd203cbb104291b39891febf7481794f205fJim Ingham
319da26bd203cbb104291b39891febf7481794f205fJim Ingham        case eInputReaderEndOfFile:
320da26bd203cbb104291b39891febf7481794f205fJim Ingham            reader.SetIsDone (true);
321da26bd203cbb104291b39891febf7481794f205fJim Ingham            break;
322da26bd203cbb104291b39891febf7481794f205fJim Ingham
323da26bd203cbb104291b39891febf7481794f205fJim Ingham        case eInputReaderDone:
324da26bd203cbb104291b39891febf7481794f205fJim Ingham            break;
32524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
326da26bd203cbb104291b39891febf7481794f205fJim Ingham
327da26bd203cbb104291b39891febf7481794f205fJim Ingham        return bytes_len;
32824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
329da26bd203cbb104291b39891febf7481794f205fJim Ingham
330da26bd203cbb104291b39891febf7481794f205fJim Ingham    static bool
331da26bd203cbb104291b39891febf7481794f205fJim Ingham    BreakpointOptionsCallbackFunction (void *baton,
332da26bd203cbb104291b39891febf7481794f205fJim Ingham                                       StoppointCallbackContext *context,
333da26bd203cbb104291b39891febf7481794f205fJim Ingham                                       lldb::user_id_t break_id,
334da26bd203cbb104291b39891febf7481794f205fJim Ingham                                       lldb::user_id_t break_loc_id)
335da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
336da26bd203cbb104291b39891febf7481794f205fJim Ingham        bool ret_value = true;
337da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (baton == NULL)
338da26bd203cbb104291b39891febf7481794f205fJim Ingham            return true;
339da26bd203cbb104291b39891febf7481794f205fJim Ingham
340da26bd203cbb104291b39891febf7481794f205fJim Ingham
341da26bd203cbb104291b39891febf7481794f205fJim Ingham        BreakpointOptions::CommandData *data = (BreakpointOptions::CommandData *) baton;
342da26bd203cbb104291b39891febf7481794f205fJim Ingham        StringList &commands = data->user_source;
343da26bd203cbb104291b39891febf7481794f205fJim Ingham
344da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (commands.GetSize() > 0)
345da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
346da26bd203cbb104291b39891febf7481794f205fJim Ingham            ExecutionContext exe_ctx (context->exe_ctx_ref);
347da26bd203cbb104291b39891febf7481794f205fJim Ingham            Target *target = exe_ctx.GetTargetPtr();
348da26bd203cbb104291b39891febf7481794f205fJim Ingham            if (target)
349da26bd203cbb104291b39891febf7481794f205fJim Ingham            {
350da26bd203cbb104291b39891febf7481794f205fJim Ingham                CommandReturnObject result;
351da26bd203cbb104291b39891febf7481794f205fJim Ingham                Debugger &debugger = target->GetDebugger();
352da26bd203cbb104291b39891febf7481794f205fJim Ingham                // Rig up the results secondary output stream to the debugger's, so the output will come out synchronously
353da26bd203cbb104291b39891febf7481794f205fJim Ingham                // if the debugger is set up that way.
354da26bd203cbb104291b39891febf7481794f205fJim Ingham
355da26bd203cbb104291b39891febf7481794f205fJim Ingham                StreamSP output_stream (debugger.GetAsyncOutputStream());
356da26bd203cbb104291b39891febf7481794f205fJim Ingham                StreamSP error_stream (debugger.GetAsyncErrorStream());
357da26bd203cbb104291b39891febf7481794f205fJim Ingham                result.SetImmediateOutputStream (output_stream);
358da26bd203cbb104291b39891febf7481794f205fJim Ingham                result.SetImmediateErrorStream (error_stream);
359da26bd203cbb104291b39891febf7481794f205fJim Ingham
360da26bd203cbb104291b39891febf7481794f205fJim Ingham                bool stop_on_continue = true;
361da26bd203cbb104291b39891febf7481794f205fJim Ingham                bool echo_commands    = false;
362da26bd203cbb104291b39891febf7481794f205fJim Ingham                bool print_results    = true;
363da26bd203cbb104291b39891febf7481794f205fJim Ingham
364da26bd203cbb104291b39891febf7481794f205fJim Ingham                debugger.GetCommandInterpreter().HandleCommands (commands,
365da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 &exe_ctx,
366da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 stop_on_continue,
367da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 data->stop_on_error,
368da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 echo_commands,
369da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 print_results,
370da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 eLazyBoolNo,
371da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 result);
372da26bd203cbb104291b39891febf7481794f205fJim Ingham                result.GetImmediateOutputStream()->Flush();
373da26bd203cbb104291b39891febf7481794f205fJim Ingham                result.GetImmediateErrorStream()->Flush();
374da26bd203cbb104291b39891febf7481794f205fJim Ingham           }
375da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
376da26bd203cbb104291b39891febf7481794f205fJim Ingham        return ret_value;
377da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
37824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
379da26bd203cbb104291b39891febf7481794f205fJim Ingham    class CommandOptions : public Options
380da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
381da26bd203cbb104291b39891febf7481794f205fJim Ingham    public:
382da26bd203cbb104291b39891febf7481794f205fJim Ingham
383da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandOptions (CommandInterpreter &interpreter) :
384da26bd203cbb104291b39891febf7481794f205fJim Ingham            Options (interpreter),
385da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_use_commands (false),
386da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_use_script_language (false),
387da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_script_language (eScriptLanguageNone),
388da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_use_one_liner (false),
389da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_one_liner(),
390da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_function_name()
391da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
392da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
39324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
394da26bd203cbb104291b39891febf7481794f205fJim Ingham        virtual
395da26bd203cbb104291b39891febf7481794f205fJim Ingham        ~CommandOptions () {}
396da26bd203cbb104291b39891febf7481794f205fJim Ingham
397da26bd203cbb104291b39891febf7481794f205fJim Ingham        virtual Error
398da26bd203cbb104291b39891febf7481794f205fJim Ingham        SetOptionValue (uint32_t option_idx, const char *option_arg)
399da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
400da26bd203cbb104291b39891febf7481794f205fJim Ingham            Error error;
401da26bd203cbb104291b39891febf7481794f205fJim Ingham            char short_option = (char) m_getopt_table[option_idx].val;
40224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
403da26bd203cbb104291b39891febf7481794f205fJim Ingham            switch (short_option)
404da26bd203cbb104291b39891febf7481794f205fJim Ingham            {
405da26bd203cbb104291b39891febf7481794f205fJim Ingham            case 'o':
406da26bd203cbb104291b39891febf7481794f205fJim Ingham                m_use_one_liner = true;
407da26bd203cbb104291b39891febf7481794f205fJim Ingham                m_one_liner = option_arg;
408da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
409da26bd203cbb104291b39891febf7481794f205fJim Ingham
410da26bd203cbb104291b39891febf7481794f205fJim Ingham            case 's':
411da26bd203cbb104291b39891febf7481794f205fJim Ingham                m_script_language = (lldb::ScriptLanguage) Args::StringToOptionEnum (option_arg,
412da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                                     g_option_table[option_idx].enum_values,
413da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                                     eScriptLanguageNone,
414da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                                     error);
415da26bd203cbb104291b39891febf7481794f205fJim Ingham
416da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (m_script_language == eScriptLanguagePython || m_script_language == eScriptLanguageDefault)
417da26bd203cbb104291b39891febf7481794f205fJim Ingham                {
418da26bd203cbb104291b39891febf7481794f205fJim Ingham                    m_use_script_language = true;
419da26bd203cbb104291b39891febf7481794f205fJim Ingham                }
420da26bd203cbb104291b39891febf7481794f205fJim Ingham                else
421da26bd203cbb104291b39891febf7481794f205fJim Ingham                {
422da26bd203cbb104291b39891febf7481794f205fJim Ingham                    m_use_script_language = false;
423da26bd203cbb104291b39891febf7481794f205fJim Ingham                }
424da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
42524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
426da26bd203cbb104291b39891febf7481794f205fJim Ingham            case 'e':
427da26bd203cbb104291b39891febf7481794f205fJim Ingham                {
428da26bd203cbb104291b39891febf7481794f205fJim Ingham                    bool success = false;
429da26bd203cbb104291b39891febf7481794f205fJim Ingham                    m_stop_on_error = Args::StringToBoolean(option_arg, false, &success);
430da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (!success)
431da26bd203cbb104291b39891febf7481794f205fJim Ingham                        error.SetErrorStringWithFormat("invalid value for stop-on-error: \"%s\"", option_arg);
432da26bd203cbb104291b39891febf7481794f205fJim Ingham                }
433da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
434da26bd203cbb104291b39891febf7481794f205fJim Ingham
435da26bd203cbb104291b39891febf7481794f205fJim Ingham            case 'F':
436da26bd203cbb104291b39891febf7481794f205fJim Ingham                {
437da26bd203cbb104291b39891febf7481794f205fJim Ingham                    m_use_one_liner = false;
438da26bd203cbb104291b39891febf7481794f205fJim Ingham                    m_use_script_language = true;
439da26bd203cbb104291b39891febf7481794f205fJim Ingham                    m_function_name.assign(option_arg);
440da26bd203cbb104291b39891febf7481794f205fJim Ingham                }
441da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
442da26bd203cbb104291b39891febf7481794f205fJim Ingham
443da26bd203cbb104291b39891febf7481794f205fJim Ingham            default:
444da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
445da26bd203cbb104291b39891febf7481794f205fJim Ingham            }
446da26bd203cbb104291b39891febf7481794f205fJim Ingham            return error;
447da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
448da26bd203cbb104291b39891febf7481794f205fJim Ingham        void
449da26bd203cbb104291b39891febf7481794f205fJim Ingham        OptionParsingStarting ()
45024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
451da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_use_commands = true;
452da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_use_script_language = false;
453da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_script_language = eScriptLanguageNone;
454da26bd203cbb104291b39891febf7481794f205fJim Ingham
455da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_use_one_liner = false;
456da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_stop_on_error = true;
457da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_one_liner.clear();
458da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_function_name.clear();
45924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
460da26bd203cbb104291b39891febf7481794f205fJim Ingham
461da26bd203cbb104291b39891febf7481794f205fJim Ingham        const OptionDefinition*
462da26bd203cbb104291b39891febf7481794f205fJim Ingham        GetDefinitions ()
46324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
464da26bd203cbb104291b39891febf7481794f205fJim Ingham            return g_option_table;
46524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
46624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
467da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Options table: Required for subclasses of Options.
46824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
469da26bd203cbb104291b39891febf7481794f205fJim Ingham        static OptionDefinition g_option_table[];
470b81ed0d482fa374fd90e8bb591d0d9c28180559cJohnny Chen
471da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Instance variables to hold the values for command options.
472b81ed0d482fa374fd90e8bb591d0d9c28180559cJohnny Chen
473da26bd203cbb104291b39891febf7481794f205fJim Ingham        bool m_use_commands;
474da26bd203cbb104291b39891febf7481794f205fJim Ingham        bool m_use_script_language;
475da26bd203cbb104291b39891febf7481794f205fJim Ingham        lldb::ScriptLanguage m_script_language;
476b81ed0d482fa374fd90e8bb591d0d9c28180559cJohnny Chen
477da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Instance variables to hold the values for one_liner options.
478da26bd203cbb104291b39891febf7481794f205fJim Ingham        bool m_use_one_liner;
479da26bd203cbb104291b39891febf7481794f205fJim Ingham        std::string m_one_liner;
480da26bd203cbb104291b39891febf7481794f205fJim Ingham        bool m_stop_on_error;
481da26bd203cbb104291b39891febf7481794f205fJim Ingham        std::string m_function_name;
482da26bd203cbb104291b39891febf7481794f205fJim Ingham    };
483b81ed0d482fa374fd90e8bb591d0d9c28180559cJohnny Chen
484da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
485da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual bool
486da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (Args& command, CommandReturnObject &result)
48724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
488da26bd203cbb104291b39891febf7481794f205fJim Ingham        Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
489da26bd203cbb104291b39891febf7481794f205fJim Ingham
490da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (target == NULL)
491892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice        {
492da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("There is not a current executable; there are no breakpoints to which to add commands");
493da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
494da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
495892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice        }
49624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
497da26bd203cbb104291b39891febf7481794f205fJim Ingham        const BreakpointList &breakpoints = target->GetBreakpointList();
498da26bd203cbb104291b39891febf7481794f205fJim Ingham        size_t num_breakpoints = breakpoints.GetSize();
49924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
500da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (num_breakpoints == 0)
501f81b4c5c3f8500aa9be9734f676608464ecb5472Caroline Tice        {
502da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("No breakpoints exist to have commands added");
503da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
504da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
505f81b4c5c3f8500aa9be9734f676608464ecb5472Caroline Tice        }
50624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
507da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (m_options.m_use_script_language == false && m_options.m_function_name.size())
508f81b4c5c3f8500aa9be9734f676608464ecb5472Caroline Tice        {
509da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("need to enable scripting to have a function run as a breakpoint command");
510da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
511da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
512f81b4c5c3f8500aa9be9734f676608464ecb5472Caroline Tice        }
51324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
514da26bd203cbb104291b39891febf7481794f205fJim Ingham        BreakpointIDList valid_bp_ids;
515da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
516da26bd203cbb104291b39891febf7481794f205fJim Ingham
517da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (result.Succeeded())
518c4f55fee15b66ea53da092ca50400ac5d8b0692dCaroline Tice        {
519da26bd203cbb104291b39891febf7481794f205fJim Ingham            const size_t count = valid_bp_ids.GetSize();
520da26bd203cbb104291b39891febf7481794f205fJim Ingham            for (size_t i = 0; i < count; ++i)
521c4f55fee15b66ea53da092ca50400ac5d8b0692dCaroline Tice            {
522da26bd203cbb104291b39891febf7481794f205fJim Ingham                BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
523da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
524c4f55fee15b66ea53da092ca50400ac5d8b0692dCaroline Tice                {
525da26bd203cbb104291b39891febf7481794f205fJim Ingham                    Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
526da26bd203cbb104291b39891febf7481794f205fJim Ingham                    BreakpointOptions *bp_options = NULL;
527da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID)
528da26bd203cbb104291b39891febf7481794f205fJim Ingham                    {
529da26bd203cbb104291b39891febf7481794f205fJim Ingham                        // This breakpoint does not have an associated location.
530da26bd203cbb104291b39891febf7481794f205fJim Ingham                        bp_options = bp->GetOptions();
531da26bd203cbb104291b39891febf7481794f205fJim Ingham                    }
532da26bd203cbb104291b39891febf7481794f205fJim Ingham                    else
533da26bd203cbb104291b39891febf7481794f205fJim Ingham                    {
534da26bd203cbb104291b39891febf7481794f205fJim Ingham                        BreakpointLocationSP bp_loc_sp(bp->FindLocationByID (cur_bp_id.GetLocationID()));
535da26bd203cbb104291b39891febf7481794f205fJim Ingham                        // This breakpoint does have an associated location.
536da26bd203cbb104291b39891febf7481794f205fJim Ingham                        // Get its breakpoint options.
537da26bd203cbb104291b39891febf7481794f205fJim Ingham                        if (bp_loc_sp)
538da26bd203cbb104291b39891febf7481794f205fJim Ingham                            bp_options = bp_loc_sp->GetLocationOptions();
539da26bd203cbb104291b39891febf7481794f205fJim Ingham                    }
540da26bd203cbb104291b39891febf7481794f205fJim Ingham
541da26bd203cbb104291b39891febf7481794f205fJim Ingham                    // Skip this breakpoint if bp_options is not good.
542da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (bp_options == NULL) continue;
543da26bd203cbb104291b39891febf7481794f205fJim Ingham
544da26bd203cbb104291b39891febf7481794f205fJim Ingham                    // If we are using script language, get the script interpreter
545da26bd203cbb104291b39891febf7481794f205fJim Ingham                    // in order to set or collect command callback.  Otherwise, call
546da26bd203cbb104291b39891febf7481794f205fJim Ingham                    // the methods associated with this object.
547da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (m_options.m_use_script_language)
548da26bd203cbb104291b39891febf7481794f205fJim Ingham                    {
549da26bd203cbb104291b39891febf7481794f205fJim Ingham                        // Special handling for one-liner specified inline.
550da26bd203cbb104291b39891febf7481794f205fJim Ingham                        if (m_options.m_use_one_liner)
551da26bd203cbb104291b39891febf7481794f205fJim Ingham                        {
552da26bd203cbb104291b39891febf7481794f205fJim Ingham                            m_interpreter.GetScriptInterpreter()->SetBreakpointCommandCallback (bp_options,
553da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                                                m_options.m_one_liner.c_str());
554da26bd203cbb104291b39891febf7481794f205fJim Ingham                        }
555da26bd203cbb104291b39891febf7481794f205fJim Ingham                        // Special handling for using a Python function by name
556da26bd203cbb104291b39891febf7481794f205fJim Ingham                        // instead of extending the breakpoint callback data structures, we just automatize
557da26bd203cbb104291b39891febf7481794f205fJim Ingham                        // what the user would do manually: make their breakpoint command be a function call
558da26bd203cbb104291b39891febf7481794f205fJim Ingham                        else if (m_options.m_function_name.size())
559da26bd203cbb104291b39891febf7481794f205fJim Ingham                        {
560da26bd203cbb104291b39891febf7481794f205fJim Ingham                            std::string oneliner(m_options.m_function_name);
561c1ca9dcd0e8a1c7c5a882281afdd2a5145026fd0Enrico Granata                            oneliner += "(frame, bp_loc, internal_dict)";
562da26bd203cbb104291b39891febf7481794f205fJim Ingham                            m_interpreter.GetScriptInterpreter()->SetBreakpointCommandCallback (bp_options,
563da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                                                oneliner.c_str());
564da26bd203cbb104291b39891febf7481794f205fJim Ingham                        }
565da26bd203cbb104291b39891febf7481794f205fJim Ingham                        else
566da26bd203cbb104291b39891febf7481794f205fJim Ingham                        {
567da26bd203cbb104291b39891febf7481794f205fJim Ingham                            m_interpreter.GetScriptInterpreter()->CollectDataForBreakpointCommandCallback (bp_options,
568da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                                                           result);
569da26bd203cbb104291b39891febf7481794f205fJim Ingham                        }
570da26bd203cbb104291b39891febf7481794f205fJim Ingham                    }
571da26bd203cbb104291b39891febf7481794f205fJim Ingham                    else
572da26bd203cbb104291b39891febf7481794f205fJim Ingham                    {
573da26bd203cbb104291b39891febf7481794f205fJim Ingham                        // Special handling for one-liner specified inline.
574da26bd203cbb104291b39891febf7481794f205fJim Ingham                        if (m_options.m_use_one_liner)
575da26bd203cbb104291b39891febf7481794f205fJim Ingham                            SetBreakpointCommandCallback (bp_options,
576da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                          m_options.m_one_liner.c_str());
577da26bd203cbb104291b39891febf7481794f205fJim Ingham                        else
578da26bd203cbb104291b39891febf7481794f205fJim Ingham                            CollectDataForBreakpointCommandCallback (bp_options,
579da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                     result);
580da26bd203cbb104291b39891febf7481794f205fJim Ingham                    }
581c4f55fee15b66ea53da092ca50400ac5d8b0692dCaroline Tice                }
582c4f55fee15b66ea53da092ca50400ac5d8b0692dCaroline Tice            }
583c4f55fee15b66ea53da092ca50400ac5d8b0692dCaroline Tice        }
584da26bd203cbb104291b39891febf7481794f205fJim Ingham
585da26bd203cbb104291b39891febf7481794f205fJim Ingham        return result.Succeeded();
58624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
58724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
588da26bd203cbb104291b39891febf7481794f205fJim Inghamprivate:
589da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandOptions m_options;
590da26bd203cbb104291b39891febf7481794f205fJim Ingham    static const char *g_reader_instructions;
59124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
592da26bd203cbb104291b39891febf7481794f205fJim Ingham};
593da26bd203cbb104291b39891febf7481794f205fJim Ingham
594da26bd203cbb104291b39891febf7481794f205fJim Inghamconst char *
595da26bd203cbb104291b39891febf7481794f205fJim InghamCommandObjectBreakpointCommandAdd::g_reader_instructions = "Enter your debugger command(s).  Type 'DONE' to end.";
596da26bd203cbb104291b39891febf7481794f205fJim Ingham
597da26bd203cbb104291b39891febf7481794f205fJim Ingham// FIXME: "script-type" needs to have its contents determined dynamically, so somebody can add a new scripting
598da26bd203cbb104291b39891febf7481794f205fJim Ingham// language to lldb and have it pickable here without having to change this enumeration by hand and rebuild lldb proper.
599da26bd203cbb104291b39891febf7481794f205fJim Ingham
600da26bd203cbb104291b39891febf7481794f205fJim Inghamstatic OptionEnumValueElement
601da26bd203cbb104291b39891febf7481794f205fJim Inghamg_script_option_enumeration[4] =
602da26bd203cbb104291b39891febf7481794f205fJim Ingham{
603da26bd203cbb104291b39891febf7481794f205fJim Ingham    { eScriptLanguageNone,    "command",         "Commands are in the lldb command interpreter language"},
604da26bd203cbb104291b39891febf7481794f205fJim Ingham    { eScriptLanguagePython,  "python",          "Commands are in the Python language."},
605da26bd203cbb104291b39891febf7481794f205fJim Ingham    { eSortOrderByName,       "default-script",  "Commands are in the default scripting language."},
606da26bd203cbb104291b39891febf7481794f205fJim Ingham    { 0,                      NULL,              NULL }
607da26bd203cbb104291b39891febf7481794f205fJim Ingham};
608da26bd203cbb104291b39891febf7481794f205fJim Ingham
609da26bd203cbb104291b39891febf7481794f205fJim InghamOptionDefinition
610da26bd203cbb104291b39891febf7481794f205fJim InghamCommandObjectBreakpointCommandAdd::CommandOptions::g_option_table[] =
611da26bd203cbb104291b39891febf7481794f205fJim Ingham{
612da26bd203cbb104291b39891febf7481794f205fJim Ingham    { LLDB_OPT_SET_1, false, "one-liner", 'o', required_argument, NULL, NULL, eArgTypeOneLiner,
613da26bd203cbb104291b39891febf7481794f205fJim Ingham        "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
614da26bd203cbb104291b39891febf7481794f205fJim Ingham
615da26bd203cbb104291b39891febf7481794f205fJim Ingham    { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', required_argument, NULL, NULL, eArgTypeBoolean,
616da26bd203cbb104291b39891febf7481794f205fJim Ingham        "Specify whether breakpoint command execution should terminate on error." },
617da26bd203cbb104291b39891febf7481794f205fJim Ingham
618da26bd203cbb104291b39891febf7481794f205fJim Ingham    { LLDB_OPT_SET_ALL,   false, "script-type",     's', required_argument, g_script_option_enumeration, NULL, eArgTypeNone,
619da26bd203cbb104291b39891febf7481794f205fJim Ingham        "Specify the language for the commands - if none is specified, the lldb command interpreter will be used."},
620da26bd203cbb104291b39891febf7481794f205fJim Ingham
621da26bd203cbb104291b39891febf7481794f205fJim Ingham    { LLDB_OPT_SET_2,   false, "python-function",     'F', required_argument, NULL, NULL, eArgTypePythonFunction,
622da26bd203cbb104291b39891febf7481794f205fJim Ingham        "Give the name of a Python function to run as command for this breakpoint. Be sure to give a module name if appropriate."},
623da26bd203cbb104291b39891febf7481794f205fJim Ingham
624da26bd203cbb104291b39891febf7481794f205fJim Ingham    { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
625da26bd203cbb104291b39891febf7481794f205fJim Ingham};
62624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
62724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
628b2203889ab9b743f04638cdd9e34aac2ab02e99dCaroline Tice// CommandObjectBreakpointCommandDelete
62924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
63024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
631da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectBreakpointCommandDelete : public CommandObjectParsed
63224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
633da26bd203cbb104291b39891febf7481794f205fJim Inghampublic:
634da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandObjectBreakpointCommandDelete (CommandInterpreter &interpreter) :
635da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectParsed (interpreter,
636da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "delete",
637da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "Delete the set of commands from a breakpoint.",
638da26bd203cbb104291b39891febf7481794f205fJim Ingham                             NULL)
639da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
640da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentEntry arg;
641da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentData bp_id_arg;
64243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
643da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Define the first (and only) variant of this arg.
644da26bd203cbb104291b39891febf7481794f205fJim Ingham        bp_id_arg.arg_type = eArgTypeBreakpointID;
645da26bd203cbb104291b39891febf7481794f205fJim Ingham        bp_id_arg.arg_repetition = eArgRepeatPlain;
64643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
647da26bd203cbb104291b39891febf7481794f205fJim Ingham        // There is only one variant this argument could be; put it into the argument entry.
648da26bd203cbb104291b39891febf7481794f205fJim Ingham        arg.push_back (bp_id_arg);
64943b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
650da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Push the data for the first argument into the m_arguments vector.
651da26bd203cbb104291b39891febf7481794f205fJim Ingham        m_arguments.push_back (arg);
652da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
65324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
65424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
655da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual
656da26bd203cbb104291b39891febf7481794f205fJim Ingham    ~CommandObjectBreakpointCommandDelete () {}
65724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
658da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
659da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual bool
660da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (Args& command, CommandReturnObject &result)
66124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
662da26bd203cbb104291b39891febf7481794f205fJim Ingham        Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
66324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
664da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (target == NULL)
665da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
666da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("There is not a current executable; there are no breakpoints from which to delete commands");
667da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
668da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
669da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
67024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
671da26bd203cbb104291b39891febf7481794f205fJim Ingham        const BreakpointList &breakpoints = target->GetBreakpointList();
672da26bd203cbb104291b39891febf7481794f205fJim Ingham        size_t num_breakpoints = breakpoints.GetSize();
67324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
674da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (num_breakpoints == 0)
675da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
676da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("No breakpoints exist to have commands deleted");
677da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
678da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
679da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
68024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
681da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (command.GetArgumentCount() == 0)
682da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
683da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("No breakpoint specified from which to delete the commands");
684da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
685da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
686da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
68724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
688da26bd203cbb104291b39891febf7481794f205fJim Ingham        BreakpointIDList valid_bp_ids;
689da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
690da26bd203cbb104291b39891febf7481794f205fJim Ingham
691da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (result.Succeeded())
69224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
693da26bd203cbb104291b39891febf7481794f205fJim Ingham            const size_t count = valid_bp_ids.GetSize();
694da26bd203cbb104291b39891febf7481794f205fJim Ingham            for (size_t i = 0; i < count; ++i)
69524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            {
696da26bd203cbb104291b39891febf7481794f205fJim Ingham                BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
697da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
69824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
699da26bd203cbb104291b39891febf7481794f205fJim Ingham                    Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
700da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
701da26bd203cbb104291b39891febf7481794f205fJim Ingham                    {
702da26bd203cbb104291b39891febf7481794f205fJim Ingham                        BreakpointLocationSP bp_loc_sp (bp->FindLocationByID (cur_bp_id.GetLocationID()));
703da26bd203cbb104291b39891febf7481794f205fJim Ingham                        if (bp_loc_sp)
704da26bd203cbb104291b39891febf7481794f205fJim Ingham                            bp_loc_sp->ClearCallback();
705da26bd203cbb104291b39891febf7481794f205fJim Ingham                        else
706da26bd203cbb104291b39891febf7481794f205fJim Ingham                        {
707da26bd203cbb104291b39891febf7481794f205fJim Ingham                            result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
708da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                         cur_bp_id.GetBreakpointID(),
709da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                         cur_bp_id.GetLocationID());
710da26bd203cbb104291b39891febf7481794f205fJim Ingham                            result.SetStatus (eReturnStatusFailed);
711da26bd203cbb104291b39891febf7481794f205fJim Ingham                            return false;
712da26bd203cbb104291b39891febf7481794f205fJim Ingham                        }
713da26bd203cbb104291b39891febf7481794f205fJim Ingham                    }
71424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    else
71524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    {
716da26bd203cbb104291b39891febf7481794f205fJim Ingham                        bp->ClearCallback();
71724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    }
71824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
71924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            }
72024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
721da26bd203cbb104291b39891febf7481794f205fJim Ingham        return result.Succeeded();
72224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
723da26bd203cbb104291b39891febf7481794f205fJim Ingham};
72424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
72524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
72624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// CommandObjectBreakpointCommandList
72724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
72824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
729da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectBreakpointCommandList : public CommandObjectParsed
73024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
731da26bd203cbb104291b39891febf7481794f205fJim Inghampublic:
732da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandObjectBreakpointCommandList (CommandInterpreter &interpreter) :
733da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectParsed (interpreter,
734da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "list",
735da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "List the script or set of commands to be executed when the breakpoint is hit.",
736da26bd203cbb104291b39891febf7481794f205fJim Ingham                              NULL)
737da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
738da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentEntry arg;
739da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentData bp_id_arg;
74043b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
741da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Define the first (and only) variant of this arg.
742da26bd203cbb104291b39891febf7481794f205fJim Ingham        bp_id_arg.arg_type = eArgTypeBreakpointID;
743da26bd203cbb104291b39891febf7481794f205fJim Ingham        bp_id_arg.arg_repetition = eArgRepeatPlain;
74443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
745da26bd203cbb104291b39891febf7481794f205fJim Ingham        // There is only one variant this argument could be; put it into the argument entry.
746da26bd203cbb104291b39891febf7481794f205fJim Ingham        arg.push_back (bp_id_arg);
74724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
748da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Push the data for the first argument into the m_arguments vector.
749da26bd203cbb104291b39891febf7481794f205fJim Ingham        m_arguments.push_back (arg);
750da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
75124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
752da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual
753da26bd203cbb104291b39891febf7481794f205fJim Ingham    ~CommandObjectBreakpointCommandList () {}
75424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
755da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
756da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual bool
757da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (Args& command,
758da26bd203cbb104291b39891febf7481794f205fJim Ingham             CommandReturnObject &result)
75924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
760da26bd203cbb104291b39891febf7481794f205fJim Ingham        Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
761da26bd203cbb104291b39891febf7481794f205fJim Ingham
762da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (target == NULL)
763da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
764da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("There is not a current executable; there are no breakpoints for which to list commands");
765da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
766da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
767da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
76824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
769da26bd203cbb104291b39891febf7481794f205fJim Ingham        const BreakpointList &breakpoints = target->GetBreakpointList();
770da26bd203cbb104291b39891febf7481794f205fJim Ingham        size_t num_breakpoints = breakpoints.GetSize();
77124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
772da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (num_breakpoints == 0)
773da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
774da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("No breakpoints exist for which to list commands");
775da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
776da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
777da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
77824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
779da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (command.GetArgumentCount() == 0)
780da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
781da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("No breakpoint specified for which to list the commands");
782da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
783da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
784da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
78524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
786da26bd203cbb104291b39891febf7481794f205fJim Ingham        BreakpointIDList valid_bp_ids;
787da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
78824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
789da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (result.Succeeded())
79024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
791da26bd203cbb104291b39891febf7481794f205fJim Ingham            const size_t count = valid_bp_ids.GetSize();
792da26bd203cbb104291b39891febf7481794f205fJim Ingham            for (size_t i = 0; i < count; ++i)
79324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            {
794da26bd203cbb104291b39891febf7481794f205fJim Ingham                BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
795da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
79624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
797da26bd203cbb104291b39891febf7481794f205fJim Ingham                    Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
798da26bd203cbb104291b39891febf7481794f205fJim Ingham
799da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (bp)
80024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    {
801da26bd203cbb104291b39891febf7481794f205fJim Ingham                        const BreakpointOptions *bp_options = NULL;
802da26bd203cbb104291b39891febf7481794f205fJim Ingham                        if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
803da26bd203cbb104291b39891febf7481794f205fJim Ingham                        {
804da26bd203cbb104291b39891febf7481794f205fJim Ingham                            BreakpointLocationSP bp_loc_sp(bp->FindLocationByID (cur_bp_id.GetLocationID()));
805da26bd203cbb104291b39891febf7481794f205fJim Ingham                            if (bp_loc_sp)
806da26bd203cbb104291b39891febf7481794f205fJim Ingham                                bp_options = bp_loc_sp->GetOptionsNoCreate();
807da26bd203cbb104291b39891febf7481794f205fJim Ingham                            else
808da26bd203cbb104291b39891febf7481794f205fJim Ingham                            {
809da26bd203cbb104291b39891febf7481794f205fJim Ingham                                result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
810da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             cur_bp_id.GetBreakpointID(),
811da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             cur_bp_id.GetLocationID());
812da26bd203cbb104291b39891febf7481794f205fJim Ingham                                result.SetStatus (eReturnStatusFailed);
813da26bd203cbb104291b39891febf7481794f205fJim Ingham                                return false;
814da26bd203cbb104291b39891febf7481794f205fJim Ingham                            }
815da26bd203cbb104291b39891febf7481794f205fJim Ingham                        }
81624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                        else
81724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                        {
818da26bd203cbb104291b39891febf7481794f205fJim Ingham                            bp_options = bp->GetOptions();
819da26bd203cbb104291b39891febf7481794f205fJim Ingham                        }
820da26bd203cbb104291b39891febf7481794f205fJim Ingham
821da26bd203cbb104291b39891febf7481794f205fJim Ingham                        if (bp_options)
822da26bd203cbb104291b39891febf7481794f205fJim Ingham                        {
823da26bd203cbb104291b39891febf7481794f205fJim Ingham                            StreamString id_str;
824da26bd203cbb104291b39891febf7481794f205fJim Ingham                            BreakpointID::GetCanonicalReference (&id_str,
825da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 cur_bp_id.GetBreakpointID(),
826da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 cur_bp_id.GetLocationID());
827da26bd203cbb104291b39891febf7481794f205fJim Ingham                            const Baton *baton = bp_options->GetBaton();
828da26bd203cbb104291b39891febf7481794f205fJim Ingham                            if (baton)
829da26bd203cbb104291b39891febf7481794f205fJim Ingham                            {
830da26bd203cbb104291b39891febf7481794f205fJim Ingham                                result.GetOutputStream().Printf ("Breakpoint %s:\n", id_str.GetData());
831da26bd203cbb104291b39891febf7481794f205fJim Ingham                                result.GetOutputStream().IndentMore ();
832da26bd203cbb104291b39891febf7481794f205fJim Ingham                                baton->GetDescription(&result.GetOutputStream(), eDescriptionLevelFull);
833da26bd203cbb104291b39891febf7481794f205fJim Ingham                                result.GetOutputStream().IndentLess ();
834da26bd203cbb104291b39891febf7481794f205fJim Ingham                            }
835da26bd203cbb104291b39891febf7481794f205fJim Ingham                            else
836da26bd203cbb104291b39891febf7481794f205fJim Ingham                            {
837da26bd203cbb104291b39891febf7481794f205fJim Ingham                                result.AppendMessageWithFormat ("Breakpoint %s does not have an associated command.\n",
838da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                id_str.GetData());
839da26bd203cbb104291b39891febf7481794f205fJim Ingham                            }
84024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                        }
841da26bd203cbb104291b39891febf7481794f205fJim Ingham                        result.SetStatus (eReturnStatusSuccessFinishResult);
84224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    }
84324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    else
84424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    {
845da26bd203cbb104291b39891febf7481794f205fJim Ingham                        result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n", cur_bp_id.GetBreakpointID());
846da26bd203cbb104291b39891febf7481794f205fJim Ingham                        result.SetStatus (eReturnStatusFailed);
84724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    }
84824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
84924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
85024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            }
85124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
85224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
853da26bd203cbb104291b39891febf7481794f205fJim Ingham        return result.Succeeded();
854da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
855da26bd203cbb104291b39891febf7481794f205fJim Ingham};
85624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
85724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
85824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// CommandObjectBreakpointCommand
85924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
86024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
86163094e0bb161580564954dee512955c1c79d3476Greg ClaytonCommandObjectBreakpointCommand::CommandObjectBreakpointCommand (CommandInterpreter &interpreter) :
862238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    CommandObjectMultiword (interpreter,
863238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                            "command",
86424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                            "A set of commands for adding, removing and examining bits of code to be executed when the breakpoint is hit (breakpoint 'commmands').",
86524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                            "command <sub-command> [<sub-command-options>] <breakpoint-id>")
86624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
867238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    CommandObjectSP add_command_object (new CommandObjectBreakpointCommandAdd (interpreter));
868b2203889ab9b743f04638cdd9e34aac2ab02e99dCaroline Tice    CommandObjectSP delete_command_object (new CommandObjectBreakpointCommandDelete (interpreter));
869238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    CommandObjectSP list_command_object (new CommandObjectBreakpointCommandList (interpreter));
87024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
87124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    add_command_object->SetCommandName ("breakpoint command add");
872b2203889ab9b743f04638cdd9e34aac2ab02e99dCaroline Tice    delete_command_object->SetCommandName ("breakpoint command delete");
87324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    list_command_object->SetCommandName ("breakpoint command list");
87424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
8754a379b1194f3e6b308cd6e80b45d6ca5dd0aafd7Greg Clayton    LoadSubCommand ("add",    add_command_object);
8764a379b1194f3e6b308cd6e80b45d6ca5dd0aafd7Greg Clayton    LoadSubCommand ("delete", delete_command_object);
8774a379b1194f3e6b308cd6e80b45d6ca5dd0aafd7Greg Clayton    LoadSubCommand ("list",   list_command_object);
87824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
87924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
88024943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerCommandObjectBreakpointCommand::~CommandObjectBreakpointCommand ()
88124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
88224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
88324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
88424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
885