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
10d891f9b872103235cfd2ed452c6f14a4394d9b3aDaniel Malea#include "lldb/lldb-python.h"
11d891f9b872103235cfd2ed452c6f14a4394d9b3aDaniel Malea
1224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// C Includes
1324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// C++ Includes
1424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "CommandObjectBreakpointCommand.h"
1724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "CommandObjectBreakpoint.h"
1824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Interpreter/CommandInterpreter.h"
2024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Interpreter/CommandReturnObject.h"
2124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Target/Target.h"
2224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Target/Thread.h"
2324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Breakpoint/BreakpointIDList.h"
2424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Breakpoint/Breakpoint.h"
2524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Breakpoint/BreakpointLocation.h"
2624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Breakpoint/StoppointCallbackContext.h"
2724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Core/State.h"
2824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
2924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerusing namespace lldb;
3024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerusing namespace lldb_private;
3124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
3224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
3324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// CommandObjectBreakpointCommandAdd
3424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
3524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
3624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
37da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectBreakpointCommandAdd : public CommandObjectParsed
3824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
39da26bd203cbb104291b39891febf7481794f205fJim Inghampublic:
40da26bd203cbb104291b39891febf7481794f205fJim Ingham
41da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandObjectBreakpointCommandAdd (CommandInterpreter &interpreter) :
42da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectParsed (interpreter,
43da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "add",
44da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "Add a set of commands to a breakpoint, to be executed whenever the breakpoint is hit.",
45da26bd203cbb104291b39891febf7481794f205fJim Ingham                             NULL),
46da26bd203cbb104291b39891febf7481794f205fJim Ingham        m_options (interpreter)
47da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
48da26bd203cbb104291b39891febf7481794f205fJim Ingham        SetHelpLong (
49b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton"\nGeneral information about entering breakpoint commands\n\
50b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton------------------------------------------------------\n\
51b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
52b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonThis command will cause you to be prompted to enter the command or set of\n\
53b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytoncommands you wish to be executed when the specified breakpoint is hit. You\n\
54b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonwill be told to enter your command(s), and will see a '> 'prompt. Because\n\
55b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonyou can enter one or many commands to be executed when a breakpoint is hit,\n\
56b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonyou will continue to be prompted after each new-line that you enter, until you\n\
57b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonenter the word 'DONE', which will cause the commands you have entered to be\n\
58b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonstored with the breakpoint and executed when the breakpoint is hit.\n\
59b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
60b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonSyntax checking is not necessarily done when breakpoint commands are entered.\n\
61b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonAn improperly written breakpoint command will attempt to get executed when the\n\
62b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonbreakpoint gets hit, and usually silently fail.  If your breakpoint command does\n\
63b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonnot appear to be getting executed, go back and check your syntax.\n\
64b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
65b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonSpecial information about PYTHON breakpoint commands\n\
66b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton----------------------------------------------------\n\
67b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
68b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonYou may enter either one line of Python, multiple lines of Python (including\n\
69b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonfunction definitions), or specify a Python function in a module that has already,\n\
70b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonor will be imported.  If you enter a single line of Python, that will be passed\n\
71b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonto the Python interpreter 'as is' when the breakpoint gets hit.  If you enter\n\
72b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonfunction definitions, they will be passed to the Python interpreter as soon as\n\
73b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonyou finish entering the breakpoint command, and they can be called later (don't\n\
74b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonforget to add calls to them, if you want them called when the breakpoint is\n\
75b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonhit).  If you enter multiple lines of Python that are not function definitions,\n\
76b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonthey will be collected into a new, automatically generated Python function, and\n\
77b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytona call to the newly generated function will be attached to the breakpoint.\n\
78b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
79b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
80b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonThis auto-generated function is passed in three arguments:\n\
81b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
82b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton    frame:  a lldb.SBFrame object for the frame which hit breakpoint.\n\
83b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton    bp_loc: a lldb.SBBreakpointLocation object that represents the breakpoint\n\
84b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton            location that was hit.\n\
85b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton    dict:   the python session dictionary hit.\n\
86b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
87b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonWhen specifying a python function with the --python-function option, you need\n\
88b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonto supply the function name prepended by the module name. So if you import a\n\
89b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonmodule named 'myutils' that contains a 'breakpoint_callback' function, you would\n\
90b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonspecify the option as:\n\
91b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
92b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton    --python-function myutils.breakpoint_callback\n\
93b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
94b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonThe function itself must have the following prototype:\n\
95b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
96b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytondef breakpoint_callback(frame, bp_loc, dict):\n\
97b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton  # Your code goes here\n\
98b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
99b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonThe arguments are the same as the 3 auto generation function arguments listed\n\
100b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonabove. Note that the global variable 'lldb.frame' will NOT be setup when this\n\
101b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonfunction is called, so be sure to use the 'frame' argument. The 'frame' argument\n\
102b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytoncan get you to the thread (frame.GetThread()), the thread can get you to the\n\
103b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonprocess (thread.GetProcess()), and the process can get you back to the target\n\
104b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton(process.GetTarget()).\n\
105b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
106b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonImportant Note: Because loose Python code gets collected into functions, if you\n\
107b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonwant to access global variables in the 'loose' code, you need to specify that\n\
108b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonthey are global, using the 'global' keyword.  Be sure to use correct Python\n\
109b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonsyntax, including indentation, when entering Python breakpoint commands.\n\
110b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
111b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonAs a third option, you can pass the name of an already existing Python function\n\
112b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonand that function will be attached to the breakpoint. It will get passed the\n\
113b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonframe and bp_loc arguments mentioned above.\n\
114b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
115b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonExample Python one-line breakpoint command:\n\
116b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
117b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton(lldb) breakpoint command add -s python 1\n\
118b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonEnter your Python command(s). Type 'DONE' to end.\n\
119b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton> print \"Hit this breakpoint!\"\n\
120b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton> DONE\n\
121b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
122b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonAs a convenience, this also works for a short Python one-liner:\n\
123b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton(lldb) breakpoint command add -s python 1 -o \"import time; print time.asctime()\"\n\
124b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton(lldb) run\n\
125b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonLaunching '.../a.out'  (x86_64)\n\
126b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton(lldb) Fri Sep 10 12:17:45 2010\n\
127b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonProcess 21778 Stopped\n\
128b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton* thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = breakpoint 1.1, queue = com.apple.main-thread\n\
1294c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen  36   	\n\
1304c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen  37   	int c(int val)\n\
1314c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen  38   	{\n\
1324c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen  39 ->	    return val + 3;\n\
1334c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen  40   	}\n\
1344c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen  41   	\n\
1354c1b218fa2b7fb340bc91b127fafc5c992c00220Johnny Chen  42   	int main (int argc, char const *argv[])\n\
136b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton(lldb)\n\
137b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
138b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonExample multiple line Python breakpoint command, using function definition:\n\
139b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
140b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton(lldb) breakpoint command add -s python 1\n\
141b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonEnter your Python command(s). Type 'DONE' to end.\n\
142b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton> def breakpoint_output (bp_no):\n\
143b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton>     out_string = \"Hit breakpoint number \" + repr (bp_no)\n\
144b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton>     print out_string\n\
145b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton>     return True\n\
146b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton> breakpoint_output (1)\n\
147b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton> DONE\n\
148b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
149b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
150b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonExample multiple line Python breakpoint command, using 'loose' Python:\n\
151b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
152b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton(lldb) breakpoint command add -s p 1\n\
153b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonEnter your Python command(s). Type 'DONE' to end.\n\
154b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton> global bp_count\n\
155b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton> bp_count = bp_count + 1\n\
156b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton> print \"Hit this breakpoint \" + repr(bp_count) + \" times!\"\n\
157b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton> DONE\n\
158b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
159b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonIn this case, since there is a reference to a global variable,\n\
160b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton'bp_count', you will also need to make sure 'bp_count' exists and is\n\
161b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytoninitialized:\n\
162b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
163b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton(lldb) script\n\
164b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton>>> bp_count = 0\n\
165b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton>>> quit()\n\
166b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
167b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton(lldb)\n\
168b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
169b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
170114b81f9b3e48d40e935982a6622ac70e1f2bf82Enrico GranataYour Python code, however organized, can optionally return a value.\n\
171114b81f9b3e48d40e935982a6622ac70e1f2bf82Enrico GranataIf the returned value is False, that tells LLDB not to stop at the breakpoint\n\
172114b81f9b3e48d40e935982a6622ac70e1f2bf82Enrico Granatato which the code is associated. Returning anything other than False, or even\n\
173114b81f9b3e48d40e935982a6622ac70e1f2bf82Enrico Granatareturning None, or even omitting a return statement entirely, will cause\n\
174114b81f9b3e48d40e935982a6622ac70e1f2bf82Enrico GranataLLDB to stop.\n\
175114b81f9b3e48d40e935982a6622ac70e1f2bf82Enrico Granata\n\
176b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonFinal Note:  If you get a warning that no breakpoint command was generated, but\n\
177b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonyou did not get any syntax errors, you probably forgot to add a call to your\n\
178b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonfunctions.\n\
179b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
180b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonSpecial information about debugger command breakpoint commands\n\
181b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton--------------------------------------------------------------\n\
182b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Clayton\n\
183b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonYou may enter any debugger command, exactly as you would at the debugger prompt.\n\
184b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg ClaytonYou may enter as many debugger commands as you like, but do NOT enter more than\n\
185b34ca857b4e16f5239c1092ba16b1a493a52d81aGreg Claytonone command per line.\n" );
18643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
187da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentEntry arg;
188da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentData bp_id_arg;
18943b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
190da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Define the first (and only) variant of this arg.
191da26bd203cbb104291b39891febf7481794f205fJim Ingham        bp_id_arg.arg_type = eArgTypeBreakpointID;
192da26bd203cbb104291b39891febf7481794f205fJim Ingham        bp_id_arg.arg_repetition = eArgRepeatPlain;
19343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
194da26bd203cbb104291b39891febf7481794f205fJim Ingham        // There is only one variant this argument could be; put it into the argument entry.
195da26bd203cbb104291b39891febf7481794f205fJim Ingham        arg.push_back (bp_id_arg);
19643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
197da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Push the data for the first argument into the m_arguments vector.
198da26bd203cbb104291b39891febf7481794f205fJim Ingham        m_arguments.push_back (arg);
19924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
20024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
201da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual
202da26bd203cbb104291b39891febf7481794f205fJim Ingham    ~CommandObjectBreakpointCommandAdd () {}
20324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
204da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual Options *
205da26bd203cbb104291b39891febf7481794f205fJim Ingham    GetOptions ()
20624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
207da26bd203cbb104291b39891febf7481794f205fJim Ingham        return &m_options;
20824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
20924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
210da26bd203cbb104291b39891febf7481794f205fJim Ingham    void
211da26bd203cbb104291b39891febf7481794f205fJim Ingham    CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
212da26bd203cbb104291b39891febf7481794f205fJim Ingham                                             CommandReturnObject &result)
2138ad21a888d8185b929c68fec52b571d823377b34Enrico Granata    {
214da26bd203cbb104291b39891febf7481794f205fJim Ingham        InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
215102b2c2681c9a830afe25bfea35557421905e42cGreg Clayton        std::unique_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
216da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (reader_sp && data_ap.get())
217da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
218da26bd203cbb104291b39891febf7481794f205fJim Ingham            BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
219da26bd203cbb104291b39891febf7481794f205fJim Ingham            bp_options->SetCallback (BreakpointOptionsCallbackFunction, baton_sp);
220da26bd203cbb104291b39891febf7481794f205fJim Ingham
221da26bd203cbb104291b39891febf7481794f205fJim Ingham            Error err (reader_sp->Initialize (CommandObjectBreakpointCommandAdd::GenerateBreakpointCommandCallback,
222da26bd203cbb104291b39891febf7481794f205fJim Ingham                                              bp_options,                   // baton
223da26bd203cbb104291b39891febf7481794f205fJim Ingham                                              eInputReaderGranularityLine,  // token size, to pass to callback function
224da26bd203cbb104291b39891febf7481794f205fJim Ingham                                              "DONE",                       // end token
225da26bd203cbb104291b39891febf7481794f205fJim Ingham                                              "> ",                         // prompt
226da26bd203cbb104291b39891febf7481794f205fJim Ingham                                              true));                       // echo input
227da26bd203cbb104291b39891febf7481794f205fJim Ingham            if (err.Success())
228da26bd203cbb104291b39891febf7481794f205fJim Ingham            {
229da26bd203cbb104291b39891febf7481794f205fJim Ingham                m_interpreter.GetDebugger().PushInputReader (reader_sp);
230da26bd203cbb104291b39891febf7481794f205fJim Ingham                result.SetStatus (eReturnStatusSuccessFinishNoResult);
231da26bd203cbb104291b39891febf7481794f205fJim Ingham            }
232da26bd203cbb104291b39891febf7481794f205fJim Ingham            else
233da26bd203cbb104291b39891febf7481794f205fJim Ingham            {
234da26bd203cbb104291b39891febf7481794f205fJim Ingham                result.AppendError (err.AsCString());
235da26bd203cbb104291b39891febf7481794f205fJim Ingham                result.SetStatus (eReturnStatusFailed);
236da26bd203cbb104291b39891febf7481794f205fJim Ingham            }
237da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
238da26bd203cbb104291b39891febf7481794f205fJim Ingham        else
239da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
240da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError("out of memory");
241da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
242da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
243da26bd203cbb104291b39891febf7481794f205fJim Ingham
2448ad21a888d8185b929c68fec52b571d823377b34Enrico Granata    }
2458ad21a888d8185b929c68fec52b571d823377b34Enrico Granata
246da26bd203cbb104291b39891febf7481794f205fJim Ingham    /// Set a one-liner as the callback for the breakpoint.
247da26bd203cbb104291b39891febf7481794f205fJim Ingham    void
248da26bd203cbb104291b39891febf7481794f205fJim Ingham    SetBreakpointCommandCallback (BreakpointOptions *bp_options,
249da26bd203cbb104291b39891febf7481794f205fJim Ingham                                  const char *oneliner)
250da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
251102b2c2681c9a830afe25bfea35557421905e42cGreg Clayton        std::unique_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
252da26bd203cbb104291b39891febf7481794f205fJim Ingham
253da26bd203cbb104291b39891febf7481794f205fJim Ingham        // It's necessary to set both user_source and script_source to the oneliner.
254da26bd203cbb104291b39891febf7481794f205fJim Ingham        // The former is used to generate callback description (as in breakpoint command list)
255da26bd203cbb104291b39891febf7481794f205fJim Ingham        // while the latter is used for Python to interpret during the actual callback.
256da26bd203cbb104291b39891febf7481794f205fJim Ingham        data_ap->user_source.AppendString (oneliner);
257da26bd203cbb104291b39891febf7481794f205fJim Ingham        data_ap->script_source.assign (oneliner);
258da26bd203cbb104291b39891febf7481794f205fJim Ingham        data_ap->stop_on_error = m_options.m_stop_on_error;
259da26bd203cbb104291b39891febf7481794f205fJim Ingham
260da26bd203cbb104291b39891febf7481794f205fJim Ingham        BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
261da26bd203cbb104291b39891febf7481794f205fJim Ingham        bp_options->SetCallback (BreakpointOptionsCallbackFunction, baton_sp);
262da26bd203cbb104291b39891febf7481794f205fJim Ingham
263da26bd203cbb104291b39891febf7481794f205fJim Ingham        return;
264da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
26524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
266da26bd203cbb104291b39891febf7481794f205fJim Ingham    static size_t
267da26bd203cbb104291b39891febf7481794f205fJim Ingham    GenerateBreakpointCommandCallback (void *baton,
268da26bd203cbb104291b39891febf7481794f205fJim Ingham                                       InputReader &reader,
269da26bd203cbb104291b39891febf7481794f205fJim Ingham                                       lldb::InputReaderAction notification,
270da26bd203cbb104291b39891febf7481794f205fJim Ingham                                       const char *bytes,
271da26bd203cbb104291b39891febf7481794f205fJim Ingham                                       size_t bytes_len)
27224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
273da26bd203cbb104291b39891febf7481794f205fJim Ingham        StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
274da26bd203cbb104291b39891febf7481794f205fJim Ingham        bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
275da26bd203cbb104291b39891febf7481794f205fJim Ingham
276da26bd203cbb104291b39891febf7481794f205fJim Ingham        switch (notification)
27724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
278da26bd203cbb104291b39891febf7481794f205fJim Ingham        case eInputReaderActivate:
279da26bd203cbb104291b39891febf7481794f205fJim Ingham            if (!batch_mode)
28024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            {
281da26bd203cbb104291b39891febf7481794f205fJim Ingham                out_stream->Printf ("%s\n", g_reader_instructions);
282da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (reader.GetPrompt())
283da26bd203cbb104291b39891febf7481794f205fJim Ingham                    out_stream->Printf ("%s", reader.GetPrompt());
284da26bd203cbb104291b39891febf7481794f205fJim Ingham                out_stream->Flush();
285da26bd203cbb104291b39891febf7481794f205fJim Ingham            }
286da26bd203cbb104291b39891febf7481794f205fJim Ingham            break;
287b81ed0d482fa374fd90e8bb591d0d9c28180559cJohnny Chen
288da26bd203cbb104291b39891febf7481794f205fJim Ingham        case eInputReaderDeactivate:
289da26bd203cbb104291b39891febf7481794f205fJim Ingham            break;
290b81ed0d482fa374fd90e8bb591d0d9c28180559cJohnny Chen
291da26bd203cbb104291b39891febf7481794f205fJim Ingham        case eInputReaderReactivate:
292da26bd203cbb104291b39891febf7481794f205fJim Ingham            if (reader.GetPrompt() && !batch_mode)
293da26bd203cbb104291b39891febf7481794f205fJim Ingham            {
294da26bd203cbb104291b39891febf7481794f205fJim Ingham                out_stream->Printf ("%s", reader.GetPrompt());
295da26bd203cbb104291b39891febf7481794f205fJim Ingham                out_stream->Flush();
296da26bd203cbb104291b39891febf7481794f205fJim Ingham            }
297da26bd203cbb104291b39891febf7481794f205fJim Ingham            break;
298da26bd203cbb104291b39891febf7481794f205fJim Ingham
299da26bd203cbb104291b39891febf7481794f205fJim Ingham        case eInputReaderAsynchronousOutputWritten:
300da26bd203cbb104291b39891febf7481794f205fJim Ingham            break;
301da26bd203cbb104291b39891febf7481794f205fJim Ingham
302da26bd203cbb104291b39891febf7481794f205fJim Ingham        case eInputReaderGotToken:
303da26bd203cbb104291b39891febf7481794f205fJim Ingham            if (bytes && bytes_len && baton)
304da26bd203cbb104291b39891febf7481794f205fJim Ingham            {
305da26bd203cbb104291b39891febf7481794f205fJim Ingham                BreakpointOptions *bp_options = (BreakpointOptions *) baton;
306da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (bp_options)
307b81ed0d482fa374fd90e8bb591d0d9c28180559cJohnny Chen                {
308da26bd203cbb104291b39891febf7481794f205fJim Ingham                    Baton *bp_options_baton = bp_options->GetBaton();
309da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (bp_options_baton)
310da26bd203cbb104291b39891febf7481794f205fJim Ingham                        ((BreakpointOptions::CommandData *)bp_options_baton->m_data)->user_source.AppendString (bytes, bytes_len);
311da26bd203cbb104291b39891febf7481794f205fJim Ingham                }
312da26bd203cbb104291b39891febf7481794f205fJim Ingham            }
313da26bd203cbb104291b39891febf7481794f205fJim Ingham            if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
314da26bd203cbb104291b39891febf7481794f205fJim Ingham            {
315da26bd203cbb104291b39891febf7481794f205fJim Ingham                out_stream->Printf ("%s", reader.GetPrompt());
316da26bd203cbb104291b39891febf7481794f205fJim Ingham                out_stream->Flush();
317da26bd203cbb104291b39891febf7481794f205fJim Ingham            }
318da26bd203cbb104291b39891febf7481794f205fJim Ingham            break;
319da26bd203cbb104291b39891febf7481794f205fJim Ingham
320da26bd203cbb104291b39891febf7481794f205fJim Ingham        case eInputReaderInterrupt:
321da26bd203cbb104291b39891febf7481794f205fJim Ingham            {
322da26bd203cbb104291b39891febf7481794f205fJim Ingham                // Finish, and cancel the breakpoint command.
323da26bd203cbb104291b39891febf7481794f205fJim Ingham                reader.SetIsDone (true);
324da26bd203cbb104291b39891febf7481794f205fJim Ingham                BreakpointOptions *bp_options = (BreakpointOptions *) baton;
325da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (bp_options)
326da26bd203cbb104291b39891febf7481794f205fJim Ingham                {
327da26bd203cbb104291b39891febf7481794f205fJim Ingham                    Baton *bp_options_baton = bp_options->GetBaton ();
328da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (bp_options_baton)
3298ad21a888d8185b929c68fec52b571d823377b34Enrico Granata                    {
330da26bd203cbb104291b39891febf7481794f205fJim Ingham                        ((BreakpointOptions::CommandData *) bp_options_baton->m_data)->user_source.Clear();
331da26bd203cbb104291b39891febf7481794f205fJim Ingham                        ((BreakpointOptions::CommandData *) bp_options_baton->m_data)->script_source.clear();
3328ad21a888d8185b929c68fec52b571d823377b34Enrico Granata                    }
33324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
334da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (!batch_mode)
33524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
336da26bd203cbb104291b39891febf7481794f205fJim Ingham                    out_stream->Printf ("Warning: No command attached to breakpoint.\n");
337da26bd203cbb104291b39891febf7481794f205fJim Ingham                    out_stream->Flush();
33824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
33924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            }
340da26bd203cbb104291b39891febf7481794f205fJim Ingham            break;
341da26bd203cbb104291b39891febf7481794f205fJim Ingham
342da26bd203cbb104291b39891febf7481794f205fJim Ingham        case eInputReaderEndOfFile:
343da26bd203cbb104291b39891febf7481794f205fJim Ingham            reader.SetIsDone (true);
344da26bd203cbb104291b39891febf7481794f205fJim Ingham            break;
345da26bd203cbb104291b39891febf7481794f205fJim Ingham
346da26bd203cbb104291b39891febf7481794f205fJim Ingham        case eInputReaderDone:
347da26bd203cbb104291b39891febf7481794f205fJim Ingham            break;
34824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
349da26bd203cbb104291b39891febf7481794f205fJim Ingham
350da26bd203cbb104291b39891febf7481794f205fJim Ingham        return bytes_len;
35124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
352da26bd203cbb104291b39891febf7481794f205fJim Ingham
353da26bd203cbb104291b39891febf7481794f205fJim Ingham    static bool
354da26bd203cbb104291b39891febf7481794f205fJim Ingham    BreakpointOptionsCallbackFunction (void *baton,
355da26bd203cbb104291b39891febf7481794f205fJim Ingham                                       StoppointCallbackContext *context,
356da26bd203cbb104291b39891febf7481794f205fJim Ingham                                       lldb::user_id_t break_id,
357da26bd203cbb104291b39891febf7481794f205fJim Ingham                                       lldb::user_id_t break_loc_id)
358da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
359da26bd203cbb104291b39891febf7481794f205fJim Ingham        bool ret_value = true;
360da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (baton == NULL)
361da26bd203cbb104291b39891febf7481794f205fJim Ingham            return true;
362da26bd203cbb104291b39891febf7481794f205fJim Ingham
363da26bd203cbb104291b39891febf7481794f205fJim Ingham
364da26bd203cbb104291b39891febf7481794f205fJim Ingham        BreakpointOptions::CommandData *data = (BreakpointOptions::CommandData *) baton;
365da26bd203cbb104291b39891febf7481794f205fJim Ingham        StringList &commands = data->user_source;
366da26bd203cbb104291b39891febf7481794f205fJim Ingham
367da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (commands.GetSize() > 0)
368da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
369da26bd203cbb104291b39891febf7481794f205fJim Ingham            ExecutionContext exe_ctx (context->exe_ctx_ref);
370da26bd203cbb104291b39891febf7481794f205fJim Ingham            Target *target = exe_ctx.GetTargetPtr();
371da26bd203cbb104291b39891febf7481794f205fJim Ingham            if (target)
372da26bd203cbb104291b39891febf7481794f205fJim Ingham            {
373da26bd203cbb104291b39891febf7481794f205fJim Ingham                CommandReturnObject result;
374da26bd203cbb104291b39891febf7481794f205fJim Ingham                Debugger &debugger = target->GetDebugger();
375da26bd203cbb104291b39891febf7481794f205fJim Ingham                // Rig up the results secondary output stream to the debugger's, so the output will come out synchronously
376da26bd203cbb104291b39891febf7481794f205fJim Ingham                // if the debugger is set up that way.
377da26bd203cbb104291b39891febf7481794f205fJim Ingham
378da26bd203cbb104291b39891febf7481794f205fJim Ingham                StreamSP output_stream (debugger.GetAsyncOutputStream());
379da26bd203cbb104291b39891febf7481794f205fJim Ingham                StreamSP error_stream (debugger.GetAsyncErrorStream());
380da26bd203cbb104291b39891febf7481794f205fJim Ingham                result.SetImmediateOutputStream (output_stream);
381da26bd203cbb104291b39891febf7481794f205fJim Ingham                result.SetImmediateErrorStream (error_stream);
382da26bd203cbb104291b39891febf7481794f205fJim Ingham
383da26bd203cbb104291b39891febf7481794f205fJim Ingham                bool stop_on_continue = true;
384da26bd203cbb104291b39891febf7481794f205fJim Ingham                bool echo_commands    = false;
385da26bd203cbb104291b39891febf7481794f205fJim Ingham                bool print_results    = true;
386da26bd203cbb104291b39891febf7481794f205fJim Ingham
387da26bd203cbb104291b39891febf7481794f205fJim Ingham                debugger.GetCommandInterpreter().HandleCommands (commands,
388da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 &exe_ctx,
389da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 stop_on_continue,
390da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 data->stop_on_error,
391da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 echo_commands,
392da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 print_results,
393da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 eLazyBoolNo,
394da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 result);
395da26bd203cbb104291b39891febf7481794f205fJim Ingham                result.GetImmediateOutputStream()->Flush();
396da26bd203cbb104291b39891febf7481794f205fJim Ingham                result.GetImmediateErrorStream()->Flush();
397da26bd203cbb104291b39891febf7481794f205fJim Ingham           }
398da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
399da26bd203cbb104291b39891febf7481794f205fJim Ingham        return ret_value;
400da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
40124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
402da26bd203cbb104291b39891febf7481794f205fJim Ingham    class CommandOptions : public Options
403da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
404da26bd203cbb104291b39891febf7481794f205fJim Ingham    public:
405da26bd203cbb104291b39891febf7481794f205fJim Ingham
406da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandOptions (CommandInterpreter &interpreter) :
407da26bd203cbb104291b39891febf7481794f205fJim Ingham            Options (interpreter),
408da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_use_commands (false),
409da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_use_script_language (false),
410da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_script_language (eScriptLanguageNone),
411da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_use_one_liner (false),
412da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_one_liner(),
413da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_function_name()
414da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
415da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
41624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
417da26bd203cbb104291b39891febf7481794f205fJim Ingham        virtual
418da26bd203cbb104291b39891febf7481794f205fJim Ingham        ~CommandOptions () {}
419da26bd203cbb104291b39891febf7481794f205fJim Ingham
420da26bd203cbb104291b39891febf7481794f205fJim Ingham        virtual Error
421da26bd203cbb104291b39891febf7481794f205fJim Ingham        SetOptionValue (uint32_t option_idx, const char *option_arg)
422da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
423da26bd203cbb104291b39891febf7481794f205fJim Ingham            Error error;
4246475c42148a8ea1ca86e5db465db7eca742d897dGreg Clayton            const int short_option = m_getopt_table[option_idx].val;
42524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
426da26bd203cbb104291b39891febf7481794f205fJim Ingham            switch (short_option)
427da26bd203cbb104291b39891febf7481794f205fJim Ingham            {
428da26bd203cbb104291b39891febf7481794f205fJim Ingham            case 'o':
429da26bd203cbb104291b39891febf7481794f205fJim Ingham                m_use_one_liner = true;
430da26bd203cbb104291b39891febf7481794f205fJim Ingham                m_one_liner = option_arg;
431da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
432da26bd203cbb104291b39891febf7481794f205fJim Ingham
433da26bd203cbb104291b39891febf7481794f205fJim Ingham            case 's':
434da26bd203cbb104291b39891febf7481794f205fJim Ingham                m_script_language = (lldb::ScriptLanguage) Args::StringToOptionEnum (option_arg,
435da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                                     g_option_table[option_idx].enum_values,
436da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                                     eScriptLanguageNone,
437da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                                     error);
438da26bd203cbb104291b39891febf7481794f205fJim Ingham
439da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (m_script_language == eScriptLanguagePython || m_script_language == eScriptLanguageDefault)
440da26bd203cbb104291b39891febf7481794f205fJim Ingham                {
441da26bd203cbb104291b39891febf7481794f205fJim Ingham                    m_use_script_language = true;
442da26bd203cbb104291b39891febf7481794f205fJim Ingham                }
443da26bd203cbb104291b39891febf7481794f205fJim Ingham                else
444da26bd203cbb104291b39891febf7481794f205fJim Ingham                {
445da26bd203cbb104291b39891febf7481794f205fJim Ingham                    m_use_script_language = false;
446da26bd203cbb104291b39891febf7481794f205fJim Ingham                }
447da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
44824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
449da26bd203cbb104291b39891febf7481794f205fJim Ingham            case 'e':
450da26bd203cbb104291b39891febf7481794f205fJim Ingham                {
451da26bd203cbb104291b39891febf7481794f205fJim Ingham                    bool success = false;
452da26bd203cbb104291b39891febf7481794f205fJim Ingham                    m_stop_on_error = Args::StringToBoolean(option_arg, false, &success);
453da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (!success)
454da26bd203cbb104291b39891febf7481794f205fJim Ingham                        error.SetErrorStringWithFormat("invalid value for stop-on-error: \"%s\"", option_arg);
455da26bd203cbb104291b39891febf7481794f205fJim Ingham                }
456da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
457da26bd203cbb104291b39891febf7481794f205fJim Ingham
458da26bd203cbb104291b39891febf7481794f205fJim Ingham            case 'F':
459da26bd203cbb104291b39891febf7481794f205fJim Ingham                {
460da26bd203cbb104291b39891febf7481794f205fJim Ingham                    m_use_one_liner = false;
461da26bd203cbb104291b39891febf7481794f205fJim Ingham                    m_use_script_language = true;
462da26bd203cbb104291b39891febf7481794f205fJim Ingham                    m_function_name.assign(option_arg);
463da26bd203cbb104291b39891febf7481794f205fJim Ingham                }
464da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
465da26bd203cbb104291b39891febf7481794f205fJim Ingham
466da26bd203cbb104291b39891febf7481794f205fJim Ingham            default:
467da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
468da26bd203cbb104291b39891febf7481794f205fJim Ingham            }
469da26bd203cbb104291b39891febf7481794f205fJim Ingham            return error;
470da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
471da26bd203cbb104291b39891febf7481794f205fJim Ingham        void
472da26bd203cbb104291b39891febf7481794f205fJim Ingham        OptionParsingStarting ()
47324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
474da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_use_commands = true;
475da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_use_script_language = false;
476da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_script_language = eScriptLanguageNone;
477da26bd203cbb104291b39891febf7481794f205fJim Ingham
478da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_use_one_liner = false;
479da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_stop_on_error = true;
480da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_one_liner.clear();
481da26bd203cbb104291b39891febf7481794f205fJim Ingham            m_function_name.clear();
48224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
483da26bd203cbb104291b39891febf7481794f205fJim Ingham
484da26bd203cbb104291b39891febf7481794f205fJim Ingham        const OptionDefinition*
485da26bd203cbb104291b39891febf7481794f205fJim Ingham        GetDefinitions ()
48624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
487da26bd203cbb104291b39891febf7481794f205fJim Ingham            return g_option_table;
48824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
48924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
490da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Options table: Required for subclasses of Options.
49124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
492da26bd203cbb104291b39891febf7481794f205fJim Ingham        static OptionDefinition g_option_table[];
493b81ed0d482fa374fd90e8bb591d0d9c28180559cJohnny Chen
494da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Instance variables to hold the values for command options.
495b81ed0d482fa374fd90e8bb591d0d9c28180559cJohnny Chen
496da26bd203cbb104291b39891febf7481794f205fJim Ingham        bool m_use_commands;
497da26bd203cbb104291b39891febf7481794f205fJim Ingham        bool m_use_script_language;
498da26bd203cbb104291b39891febf7481794f205fJim Ingham        lldb::ScriptLanguage m_script_language;
499b81ed0d482fa374fd90e8bb591d0d9c28180559cJohnny Chen
500da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Instance variables to hold the values for one_liner options.
501da26bd203cbb104291b39891febf7481794f205fJim Ingham        bool m_use_one_liner;
502da26bd203cbb104291b39891febf7481794f205fJim Ingham        std::string m_one_liner;
503da26bd203cbb104291b39891febf7481794f205fJim Ingham        bool m_stop_on_error;
504da26bd203cbb104291b39891febf7481794f205fJim Ingham        std::string m_function_name;
505da26bd203cbb104291b39891febf7481794f205fJim Ingham    };
506b81ed0d482fa374fd90e8bb591d0d9c28180559cJohnny Chen
507da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
508da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual bool
509da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (Args& command, CommandReturnObject &result)
51024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
511da26bd203cbb104291b39891febf7481794f205fJim Ingham        Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
512da26bd203cbb104291b39891febf7481794f205fJim Ingham
513da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (target == NULL)
514892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice        {
515da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("There is not a current executable; there are no breakpoints to which to add commands");
516da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
517da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
518892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice        }
51924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
520da26bd203cbb104291b39891febf7481794f205fJim Ingham        const BreakpointList &breakpoints = target->GetBreakpointList();
521da26bd203cbb104291b39891febf7481794f205fJim Ingham        size_t num_breakpoints = breakpoints.GetSize();
52224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
523da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (num_breakpoints == 0)
524f81b4c5c3f8500aa9be9734f676608464ecb5472Caroline Tice        {
525da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("No breakpoints exist to have commands added");
526da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
527da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
528f81b4c5c3f8500aa9be9734f676608464ecb5472Caroline Tice        }
52924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
530da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (m_options.m_use_script_language == false && m_options.m_function_name.size())
531f81b4c5c3f8500aa9be9734f676608464ecb5472Caroline Tice        {
532da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("need to enable scripting to have a function run as a breakpoint command");
533da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
534da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
535f81b4c5c3f8500aa9be9734f676608464ecb5472Caroline Tice        }
53624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
537da26bd203cbb104291b39891febf7481794f205fJim Ingham        BreakpointIDList valid_bp_ids;
538da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
539da26bd203cbb104291b39891febf7481794f205fJim Ingham
540da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (result.Succeeded())
541c4f55fee15b66ea53da092ca50400ac5d8b0692dCaroline Tice        {
542da26bd203cbb104291b39891febf7481794f205fJim Ingham            const size_t count = valid_bp_ids.GetSize();
54397b18f892c7e8ef13d07c938537467736267ef56Jim Ingham            if (count > 1)
54497b18f892c7e8ef13d07c938537467736267ef56Jim Ingham            {
54597b18f892c7e8ef13d07c938537467736267ef56Jim Ingham                result.AppendError ("can only add commands to one breakpoint at a time.");
54697b18f892c7e8ef13d07c938537467736267ef56Jim Ingham                result.SetStatus (eReturnStatusFailed);
54797b18f892c7e8ef13d07c938537467736267ef56Jim Ingham                return false;
54897b18f892c7e8ef13d07c938537467736267ef56Jim Ingham            }
54997b18f892c7e8ef13d07c938537467736267ef56Jim Ingham
550da26bd203cbb104291b39891febf7481794f205fJim Ingham            for (size_t i = 0; i < count; ++i)
551c4f55fee15b66ea53da092ca50400ac5d8b0692dCaroline Tice            {
552da26bd203cbb104291b39891febf7481794f205fJim Ingham                BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
553da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
554c4f55fee15b66ea53da092ca50400ac5d8b0692dCaroline Tice                {
555da26bd203cbb104291b39891febf7481794f205fJim Ingham                    Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
556da26bd203cbb104291b39891febf7481794f205fJim Ingham                    BreakpointOptions *bp_options = NULL;
557da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID)
558da26bd203cbb104291b39891febf7481794f205fJim Ingham                    {
559da26bd203cbb104291b39891febf7481794f205fJim Ingham                        // This breakpoint does not have an associated location.
560da26bd203cbb104291b39891febf7481794f205fJim Ingham                        bp_options = bp->GetOptions();
561da26bd203cbb104291b39891febf7481794f205fJim Ingham                    }
562da26bd203cbb104291b39891febf7481794f205fJim Ingham                    else
563da26bd203cbb104291b39891febf7481794f205fJim Ingham                    {
564da26bd203cbb104291b39891febf7481794f205fJim Ingham                        BreakpointLocationSP bp_loc_sp(bp->FindLocationByID (cur_bp_id.GetLocationID()));
565da26bd203cbb104291b39891febf7481794f205fJim Ingham                        // This breakpoint does have an associated location.
566da26bd203cbb104291b39891febf7481794f205fJim Ingham                        // Get its breakpoint options.
567da26bd203cbb104291b39891febf7481794f205fJim Ingham                        if (bp_loc_sp)
568da26bd203cbb104291b39891febf7481794f205fJim Ingham                            bp_options = bp_loc_sp->GetLocationOptions();
569da26bd203cbb104291b39891febf7481794f205fJim Ingham                    }
570da26bd203cbb104291b39891febf7481794f205fJim Ingham
571da26bd203cbb104291b39891febf7481794f205fJim Ingham                    // Skip this breakpoint if bp_options is not good.
572da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (bp_options == NULL) continue;
573da26bd203cbb104291b39891febf7481794f205fJim Ingham
574da26bd203cbb104291b39891febf7481794f205fJim Ingham                    // If we are using script language, get the script interpreter
575da26bd203cbb104291b39891febf7481794f205fJim Ingham                    // in order to set or collect command callback.  Otherwise, call
576da26bd203cbb104291b39891febf7481794f205fJim Ingham                    // the methods associated with this object.
577da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (m_options.m_use_script_language)
578da26bd203cbb104291b39891febf7481794f205fJim Ingham                    {
579da26bd203cbb104291b39891febf7481794f205fJim Ingham                        // Special handling for one-liner specified inline.
580da26bd203cbb104291b39891febf7481794f205fJim Ingham                        if (m_options.m_use_one_liner)
581da26bd203cbb104291b39891febf7481794f205fJim Ingham                        {
582da26bd203cbb104291b39891febf7481794f205fJim Ingham                            m_interpreter.GetScriptInterpreter()->SetBreakpointCommandCallback (bp_options,
583da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                                                m_options.m_one_liner.c_str());
584da26bd203cbb104291b39891febf7481794f205fJim Ingham                        }
585da26bd203cbb104291b39891febf7481794f205fJim Ingham                        // Special handling for using a Python function by name
586da26bd203cbb104291b39891febf7481794f205fJim Ingham                        // instead of extending the breakpoint callback data structures, we just automatize
587da26bd203cbb104291b39891febf7481794f205fJim Ingham                        // what the user would do manually: make their breakpoint command be a function call
588da26bd203cbb104291b39891febf7481794f205fJim Ingham                        else if (m_options.m_function_name.size())
589da26bd203cbb104291b39891febf7481794f205fJim Ingham                        {
590d0cd7a045bd6170153d5e7d0735535014a4fa0feEnrico Granata                            std::string oneliner("return ");
591d0cd7a045bd6170153d5e7d0735535014a4fa0feEnrico Granata                            oneliner += m_options.m_function_name;
592c1ca9dcd0e8a1c7c5a882281afdd2a5145026fd0Enrico Granata                            oneliner += "(frame, bp_loc, internal_dict)";
593da26bd203cbb104291b39891febf7481794f205fJim Ingham                            m_interpreter.GetScriptInterpreter()->SetBreakpointCommandCallback (bp_options,
594da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                                                oneliner.c_str());
595da26bd203cbb104291b39891febf7481794f205fJim Ingham                        }
596da26bd203cbb104291b39891febf7481794f205fJim Ingham                        else
597da26bd203cbb104291b39891febf7481794f205fJim Ingham                        {
598da26bd203cbb104291b39891febf7481794f205fJim Ingham                            m_interpreter.GetScriptInterpreter()->CollectDataForBreakpointCommandCallback (bp_options,
599da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                                                           result);
600da26bd203cbb104291b39891febf7481794f205fJim Ingham                        }
601da26bd203cbb104291b39891febf7481794f205fJim Ingham                    }
602da26bd203cbb104291b39891febf7481794f205fJim Ingham                    else
603da26bd203cbb104291b39891febf7481794f205fJim Ingham                    {
604da26bd203cbb104291b39891febf7481794f205fJim Ingham                        // Special handling for one-liner specified inline.
605da26bd203cbb104291b39891febf7481794f205fJim Ingham                        if (m_options.m_use_one_liner)
606da26bd203cbb104291b39891febf7481794f205fJim Ingham                            SetBreakpointCommandCallback (bp_options,
607da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                          m_options.m_one_liner.c_str());
608da26bd203cbb104291b39891febf7481794f205fJim Ingham                        else
609da26bd203cbb104291b39891febf7481794f205fJim Ingham                            CollectDataForBreakpointCommandCallback (bp_options,
610da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                     result);
611da26bd203cbb104291b39891febf7481794f205fJim Ingham                    }
612c4f55fee15b66ea53da092ca50400ac5d8b0692dCaroline Tice                }
613c4f55fee15b66ea53da092ca50400ac5d8b0692dCaroline Tice            }
614c4f55fee15b66ea53da092ca50400ac5d8b0692dCaroline Tice        }
615da26bd203cbb104291b39891febf7481794f205fJim Ingham
616da26bd203cbb104291b39891febf7481794f205fJim Ingham        return result.Succeeded();
61724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
61824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
619da26bd203cbb104291b39891febf7481794f205fJim Inghamprivate:
620da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandOptions m_options;
621da26bd203cbb104291b39891febf7481794f205fJim Ingham    static const char *g_reader_instructions;
62224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
623da26bd203cbb104291b39891febf7481794f205fJim Ingham};
624da26bd203cbb104291b39891febf7481794f205fJim Ingham
625da26bd203cbb104291b39891febf7481794f205fJim Inghamconst char *
626da26bd203cbb104291b39891febf7481794f205fJim InghamCommandObjectBreakpointCommandAdd::g_reader_instructions = "Enter your debugger command(s).  Type 'DONE' to end.";
627da26bd203cbb104291b39891febf7481794f205fJim Ingham
628da26bd203cbb104291b39891febf7481794f205fJim Ingham// FIXME: "script-type" needs to have its contents determined dynamically, so somebody can add a new scripting
629da26bd203cbb104291b39891febf7481794f205fJim Ingham// language to lldb and have it pickable here without having to change this enumeration by hand and rebuild lldb proper.
630da26bd203cbb104291b39891febf7481794f205fJim Ingham
631da26bd203cbb104291b39891febf7481794f205fJim Inghamstatic OptionEnumValueElement
632da26bd203cbb104291b39891febf7481794f205fJim Inghamg_script_option_enumeration[4] =
633da26bd203cbb104291b39891febf7481794f205fJim Ingham{
634da26bd203cbb104291b39891febf7481794f205fJim Ingham    { eScriptLanguageNone,    "command",         "Commands are in the lldb command interpreter language"},
635da26bd203cbb104291b39891febf7481794f205fJim Ingham    { eScriptLanguagePython,  "python",          "Commands are in the Python language."},
636da26bd203cbb104291b39891febf7481794f205fJim Ingham    { eSortOrderByName,       "default-script",  "Commands are in the default scripting language."},
637da26bd203cbb104291b39891febf7481794f205fJim Ingham    { 0,                      NULL,              NULL }
638da26bd203cbb104291b39891febf7481794f205fJim Ingham};
639da26bd203cbb104291b39891febf7481794f205fJim Ingham
640da26bd203cbb104291b39891febf7481794f205fJim InghamOptionDefinition
641da26bd203cbb104291b39891febf7481794f205fJim InghamCommandObjectBreakpointCommandAdd::CommandOptions::g_option_table[] =
642da26bd203cbb104291b39891febf7481794f205fJim Ingham{
643560c51483461697b2f78c145d70d0cd285012c4dFilipe Cabecinhas    { LLDB_OPT_SET_1, false, "one-liner", 'o', required_argument, NULL, 0, eArgTypeOneLiner,
644da26bd203cbb104291b39891febf7481794f205fJim Ingham        "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
645da26bd203cbb104291b39891febf7481794f205fJim Ingham
646560c51483461697b2f78c145d70d0cd285012c4dFilipe Cabecinhas    { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', required_argument, NULL, 0, eArgTypeBoolean,
647da26bd203cbb104291b39891febf7481794f205fJim Ingham        "Specify whether breakpoint command execution should terminate on error." },
648da26bd203cbb104291b39891febf7481794f205fJim Ingham
649560c51483461697b2f78c145d70d0cd285012c4dFilipe Cabecinhas    { LLDB_OPT_SET_ALL,   false, "script-type",     's', required_argument, g_script_option_enumeration, 0, eArgTypeNone,
650da26bd203cbb104291b39891febf7481794f205fJim Ingham        "Specify the language for the commands - if none is specified, the lldb command interpreter will be used."},
651da26bd203cbb104291b39891febf7481794f205fJim Ingham
652560c51483461697b2f78c145d70d0cd285012c4dFilipe Cabecinhas    { LLDB_OPT_SET_2,   false, "python-function",     'F', required_argument, NULL, 0, eArgTypePythonFunction,
653da26bd203cbb104291b39891febf7481794f205fJim Ingham        "Give the name of a Python function to run as command for this breakpoint. Be sure to give a module name if appropriate."},
654da26bd203cbb104291b39891febf7481794f205fJim Ingham
655da26bd203cbb104291b39891febf7481794f205fJim Ingham    { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
656da26bd203cbb104291b39891febf7481794f205fJim Ingham};
65724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
65824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
659b2203889ab9b743f04638cdd9e34aac2ab02e99dCaroline Tice// CommandObjectBreakpointCommandDelete
66024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
66124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
662da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectBreakpointCommandDelete : public CommandObjectParsed
66324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
664da26bd203cbb104291b39891febf7481794f205fJim Inghampublic:
665da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandObjectBreakpointCommandDelete (CommandInterpreter &interpreter) :
666da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectParsed (interpreter,
667da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "delete",
668da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "Delete the set of commands from a breakpoint.",
669da26bd203cbb104291b39891febf7481794f205fJim Ingham                             NULL)
670da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
671da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentEntry arg;
672da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentData bp_id_arg;
67343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
674da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Define the first (and only) variant of this arg.
675da26bd203cbb104291b39891febf7481794f205fJim Ingham        bp_id_arg.arg_type = eArgTypeBreakpointID;
676da26bd203cbb104291b39891febf7481794f205fJim Ingham        bp_id_arg.arg_repetition = eArgRepeatPlain;
67743b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
678da26bd203cbb104291b39891febf7481794f205fJim Ingham        // There is only one variant this argument could be; put it into the argument entry.
679da26bd203cbb104291b39891febf7481794f205fJim Ingham        arg.push_back (bp_id_arg);
68043b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
681da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Push the data for the first argument into the m_arguments vector.
682da26bd203cbb104291b39891febf7481794f205fJim Ingham        m_arguments.push_back (arg);
683da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
68424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
68524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
686da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual
687da26bd203cbb104291b39891febf7481794f205fJim Ingham    ~CommandObjectBreakpointCommandDelete () {}
68824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
689da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
690da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual bool
691da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (Args& command, CommandReturnObject &result)
69224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
693da26bd203cbb104291b39891febf7481794f205fJim Ingham        Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
69424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
695da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (target == NULL)
696da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
697da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("There is not a current executable; there are no breakpoints from which to delete commands");
698da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
699da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
700da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
70124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
702da26bd203cbb104291b39891febf7481794f205fJim Ingham        const BreakpointList &breakpoints = target->GetBreakpointList();
703da26bd203cbb104291b39891febf7481794f205fJim Ingham        size_t num_breakpoints = breakpoints.GetSize();
70424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
705da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (num_breakpoints == 0)
706da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
707da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("No breakpoints exist to have commands deleted");
708da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
709da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
710da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
71124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
712da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (command.GetArgumentCount() == 0)
713da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
714da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("No breakpoint specified from which to delete the commands");
715da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
716da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
717da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
71824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
719da26bd203cbb104291b39891febf7481794f205fJim Ingham        BreakpointIDList valid_bp_ids;
720da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
721da26bd203cbb104291b39891febf7481794f205fJim Ingham
722da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (result.Succeeded())
72324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
724da26bd203cbb104291b39891febf7481794f205fJim Ingham            const size_t count = valid_bp_ids.GetSize();
725da26bd203cbb104291b39891febf7481794f205fJim Ingham            for (size_t i = 0; i < count; ++i)
72624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            {
727da26bd203cbb104291b39891febf7481794f205fJim Ingham                BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
728da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
72924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
730da26bd203cbb104291b39891febf7481794f205fJim Ingham                    Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
731da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
732da26bd203cbb104291b39891febf7481794f205fJim Ingham                    {
733da26bd203cbb104291b39891febf7481794f205fJim Ingham                        BreakpointLocationSP bp_loc_sp (bp->FindLocationByID (cur_bp_id.GetLocationID()));
734da26bd203cbb104291b39891febf7481794f205fJim Ingham                        if (bp_loc_sp)
735da26bd203cbb104291b39891febf7481794f205fJim Ingham                            bp_loc_sp->ClearCallback();
736da26bd203cbb104291b39891febf7481794f205fJim Ingham                        else
737da26bd203cbb104291b39891febf7481794f205fJim Ingham                        {
738da26bd203cbb104291b39891febf7481794f205fJim Ingham                            result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
739da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                         cur_bp_id.GetBreakpointID(),
740da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                         cur_bp_id.GetLocationID());
741da26bd203cbb104291b39891febf7481794f205fJim Ingham                            result.SetStatus (eReturnStatusFailed);
742da26bd203cbb104291b39891febf7481794f205fJim Ingham                            return false;
743da26bd203cbb104291b39891febf7481794f205fJim Ingham                        }
744da26bd203cbb104291b39891febf7481794f205fJim Ingham                    }
74524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    else
74624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    {
747da26bd203cbb104291b39891febf7481794f205fJim Ingham                        bp->ClearCallback();
74824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    }
74924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
75024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            }
75124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
752da26bd203cbb104291b39891febf7481794f205fJim Ingham        return result.Succeeded();
75324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
754da26bd203cbb104291b39891febf7481794f205fJim Ingham};
75524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
75624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
75724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// CommandObjectBreakpointCommandList
75824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
75924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
760da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectBreakpointCommandList : public CommandObjectParsed
76124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
762da26bd203cbb104291b39891febf7481794f205fJim Inghampublic:
763da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandObjectBreakpointCommandList (CommandInterpreter &interpreter) :
764da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectParsed (interpreter,
765da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "list",
766da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "List the script or set of commands to be executed when the breakpoint is hit.",
767da26bd203cbb104291b39891febf7481794f205fJim Ingham                              NULL)
768da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
769da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentEntry arg;
770da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentData bp_id_arg;
77143b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
772da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Define the first (and only) variant of this arg.
773da26bd203cbb104291b39891febf7481794f205fJim Ingham        bp_id_arg.arg_type = eArgTypeBreakpointID;
774da26bd203cbb104291b39891febf7481794f205fJim Ingham        bp_id_arg.arg_repetition = eArgRepeatPlain;
77543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
776da26bd203cbb104291b39891febf7481794f205fJim Ingham        // There is only one variant this argument could be; put it into the argument entry.
777da26bd203cbb104291b39891febf7481794f205fJim Ingham        arg.push_back (bp_id_arg);
77824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
779da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Push the data for the first argument into the m_arguments vector.
780da26bd203cbb104291b39891febf7481794f205fJim Ingham        m_arguments.push_back (arg);
781da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
78224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
783da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual
784da26bd203cbb104291b39891febf7481794f205fJim Ingham    ~CommandObjectBreakpointCommandList () {}
78524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
786da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
787da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual bool
788da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (Args& command,
789da26bd203cbb104291b39891febf7481794f205fJim Ingham             CommandReturnObject &result)
79024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
791da26bd203cbb104291b39891febf7481794f205fJim Ingham        Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
792da26bd203cbb104291b39891febf7481794f205fJim Ingham
793da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (target == NULL)
794da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
795da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("There is not a current executable; there are no breakpoints for which to list commands");
796da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
797da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
798da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
79924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
800da26bd203cbb104291b39891febf7481794f205fJim Ingham        const BreakpointList &breakpoints = target->GetBreakpointList();
801da26bd203cbb104291b39891febf7481794f205fJim Ingham        size_t num_breakpoints = breakpoints.GetSize();
80224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
803da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (num_breakpoints == 0)
804da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
805da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("No breakpoints exist for which to list commands");
806da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
807da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
808da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
80924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
810da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (command.GetArgumentCount() == 0)
811da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
812da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.AppendError ("No breakpoint specified for which to list the commands");
813da26bd203cbb104291b39891febf7481794f205fJim Ingham            result.SetStatus (eReturnStatusFailed);
814da26bd203cbb104291b39891febf7481794f205fJim Ingham            return false;
815da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
81624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
817da26bd203cbb104291b39891febf7481794f205fJim Ingham        BreakpointIDList valid_bp_ids;
818da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
81924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
820da26bd203cbb104291b39891febf7481794f205fJim Ingham        if (result.Succeeded())
82124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
822da26bd203cbb104291b39891febf7481794f205fJim Ingham            const size_t count = valid_bp_ids.GetSize();
823da26bd203cbb104291b39891febf7481794f205fJim Ingham            for (size_t i = 0; i < count; ++i)
82424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            {
825da26bd203cbb104291b39891febf7481794f205fJim Ingham                BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
826da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
82724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
828da26bd203cbb104291b39891febf7481794f205fJim Ingham                    Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
829da26bd203cbb104291b39891febf7481794f205fJim Ingham
830da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (bp)
83124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    {
832da26bd203cbb104291b39891febf7481794f205fJim Ingham                        const BreakpointOptions *bp_options = NULL;
833da26bd203cbb104291b39891febf7481794f205fJim Ingham                        if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
834da26bd203cbb104291b39891febf7481794f205fJim Ingham                        {
835da26bd203cbb104291b39891febf7481794f205fJim Ingham                            BreakpointLocationSP bp_loc_sp(bp->FindLocationByID (cur_bp_id.GetLocationID()));
836da26bd203cbb104291b39891febf7481794f205fJim Ingham                            if (bp_loc_sp)
837da26bd203cbb104291b39891febf7481794f205fJim Ingham                                bp_options = bp_loc_sp->GetOptionsNoCreate();
838da26bd203cbb104291b39891febf7481794f205fJim Ingham                            else
839da26bd203cbb104291b39891febf7481794f205fJim Ingham                            {
840da26bd203cbb104291b39891febf7481794f205fJim Ingham                                result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
841da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             cur_bp_id.GetBreakpointID(),
842da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             cur_bp_id.GetLocationID());
843da26bd203cbb104291b39891febf7481794f205fJim Ingham                                result.SetStatus (eReturnStatusFailed);
844da26bd203cbb104291b39891febf7481794f205fJim Ingham                                return false;
845da26bd203cbb104291b39891febf7481794f205fJim Ingham                            }
846da26bd203cbb104291b39891febf7481794f205fJim Ingham                        }
84724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                        else
84824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                        {
849da26bd203cbb104291b39891febf7481794f205fJim Ingham                            bp_options = bp->GetOptions();
850da26bd203cbb104291b39891febf7481794f205fJim Ingham                        }
851da26bd203cbb104291b39891febf7481794f205fJim Ingham
852da26bd203cbb104291b39891febf7481794f205fJim Ingham                        if (bp_options)
853da26bd203cbb104291b39891febf7481794f205fJim Ingham                        {
854da26bd203cbb104291b39891febf7481794f205fJim Ingham                            StreamString id_str;
855da26bd203cbb104291b39891febf7481794f205fJim Ingham                            BreakpointID::GetCanonicalReference (&id_str,
856da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 cur_bp_id.GetBreakpointID(),
857da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                 cur_bp_id.GetLocationID());
858da26bd203cbb104291b39891febf7481794f205fJim Ingham                            const Baton *baton = bp_options->GetBaton();
859da26bd203cbb104291b39891febf7481794f205fJim Ingham                            if (baton)
860da26bd203cbb104291b39891febf7481794f205fJim Ingham                            {
861da26bd203cbb104291b39891febf7481794f205fJim Ingham                                result.GetOutputStream().Printf ("Breakpoint %s:\n", id_str.GetData());
862da26bd203cbb104291b39891febf7481794f205fJim Ingham                                result.GetOutputStream().IndentMore ();
863da26bd203cbb104291b39891febf7481794f205fJim Ingham                                baton->GetDescription(&result.GetOutputStream(), eDescriptionLevelFull);
864da26bd203cbb104291b39891febf7481794f205fJim Ingham                                result.GetOutputStream().IndentLess ();
865da26bd203cbb104291b39891febf7481794f205fJim Ingham                            }
866da26bd203cbb104291b39891febf7481794f205fJim Ingham                            else
867da26bd203cbb104291b39891febf7481794f205fJim Ingham                            {
868da26bd203cbb104291b39891febf7481794f205fJim Ingham                                result.AppendMessageWithFormat ("Breakpoint %s does not have an associated command.\n",
869da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                                id_str.GetData());
870da26bd203cbb104291b39891febf7481794f205fJim Ingham                            }
87124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                        }
872da26bd203cbb104291b39891febf7481794f205fJim Ingham                        result.SetStatus (eReturnStatusSuccessFinishResult);
87324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    }
87424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    else
87524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    {
876da26bd203cbb104291b39891febf7481794f205fJim Ingham                        result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n", cur_bp_id.GetBreakpointID());
877da26bd203cbb104291b39891febf7481794f205fJim Ingham                        result.SetStatus (eReturnStatusFailed);
87824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    }
87924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
88024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
88124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            }
88224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
88324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
884da26bd203cbb104291b39891febf7481794f205fJim Ingham        return result.Succeeded();
885da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
886da26bd203cbb104291b39891febf7481794f205fJim Ingham};
88724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
88824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
88924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// CommandObjectBreakpointCommand
89024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
89124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
89263094e0bb161580564954dee512955c1c79d3476Greg ClaytonCommandObjectBreakpointCommand::CommandObjectBreakpointCommand (CommandInterpreter &interpreter) :
893238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    CommandObjectMultiword (interpreter,
894238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                            "command",
89524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                            "A set of commands for adding, removing and examining bits of code to be executed when the breakpoint is hit (breakpoint 'commmands').",
89624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                            "command <sub-command> [<sub-command-options>] <breakpoint-id>")
89724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
898238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    CommandObjectSP add_command_object (new CommandObjectBreakpointCommandAdd (interpreter));
899b2203889ab9b743f04638cdd9e34aac2ab02e99dCaroline Tice    CommandObjectSP delete_command_object (new CommandObjectBreakpointCommandDelete (interpreter));
900238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    CommandObjectSP list_command_object (new CommandObjectBreakpointCommandList (interpreter));
90124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
90224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    add_command_object->SetCommandName ("breakpoint command add");
903b2203889ab9b743f04638cdd9e34aac2ab02e99dCaroline Tice    delete_command_object->SetCommandName ("breakpoint command delete");
90424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    list_command_object->SetCommandName ("breakpoint command list");
90524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
9064a379b1194f3e6b308cd6e80b45d6ca5dd0aafd7Greg Clayton    LoadSubCommand ("add",    add_command_object);
9074a379b1194f3e6b308cd6e80b45d6ca5dd0aafd7Greg Clayton    LoadSubCommand ("delete", delete_command_object);
9084a379b1194f3e6b308cd6e80b45d6ca5dd0aafd7Greg Clayton    LoadSubCommand ("list",   list_command_object);
90924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
91024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
91124943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerCommandObjectBreakpointCommand::~CommandObjectBreakpointCommand ()
91224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
91324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
91424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
91524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
916