blob: 8f0f4e409f5cc6a97b72a6e9d82f87d80269e3fd [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001/*
2 lldb.swig
3
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004 This is the input file for SWIG, to create the appropriate C++ wrappers and
5 functions for various scripting languages, to enable them to call the
6 liblldb Script Bridge functions.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007*/
8
Johnny Chenee481782011-06-30 21:29:50 +00009/* Define our module docstring. */
10%define DOCSTRING
11"The lldb module contains the public APIs for Python binding.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012
Johnny Chenee481782011-06-30 21:29:50 +000013Some of the important classes are describe here:
14
15o SBTarget: Represents the target program running under the debugger.
16o SBProcess: Represents the process associated with the target program.
17o SBThread: Represents a thread of execution. SBProcess contains SBThread(s).
18o SBFrame: Represents one of the stack frames associated with a thread. SBThread
19 contains SBFrame(s).
20o SBSymbolContext: A container that stores various debugger related info.
21o SBValue: Represents the value of a variable, a register, or an expression.
22o SBModule: Represents an executable image and its associated object and symbol
23 files.
Johnny Chen993f2b62011-07-14 21:23:24 +000024o SBSymbol: Represents the symbol possibly associated with a stack frame.
Johnny Chenee481782011-06-30 21:29:50 +000025o SBCompileUnit: Represents a compilation unit, or compiled source file.
26o SBFunction: Represents a generic function, which can be inlined or not.
27o SBBlock: Represents a lexical block. SBFunction contains SBBlock(s).
28o SBLineEntry: Specifies an association with a contiguous range of instructions
Johnny Chen61abb2a2011-07-01 18:39:47 +000029 and a source file location. SBCompileUnit contains SBLineEntry(s)."
Johnny Chenee481782011-06-30 21:29:50 +000030%enddef
31
32/* The name of the module to be created. */
33%module(docstring=DOCSTRING) lldb
34
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036/* Typemap definitions, to allow SWIG to properly handle 'char**' data types. */
37
38%typemap(in) char ** {
39 /* Check if is a list */
40 if (PyList_Check($input)) {
41 int size = PyList_Size($input);
42 int i = 0;
Greg Claytonca512b32011-01-14 04:54:56 +000043 $1 = (char **) malloc((size+1) * sizeof(char*));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044 for (i = 0; i < size; i++) {
45 PyObject *o = PyList_GetItem($input,i);
46 if (PyString_Check(o))
Greg Claytonca512b32011-01-14 04:54:56 +000047 $1[i] = PyString_AsString(o);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048 else {
49 PyErr_SetString(PyExc_TypeError,"list must contain strings");
50 free($1);
51 return NULL;
52 }
53 }
54 $1[i] = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000055 } else if ($input == Py_None) {
56 $1 = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057 } else {
58 PyErr_SetString(PyExc_TypeError,"not a list");
59 return NULL;
60 }
61}
62
63%typemap(freearg) char** {
64 free((char *) $1);
65}
66
67%typemap(out) char** {
68 int len;
69 int i;
70 len = 0;
71 while ($1[len]) len++;
72 $result = PyList_New(len);
73 for (i = 0; i < len; i++) {
74 PyList_SetItem($result, i, PyString_FromString($1[i]));
75 }
76}
77
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000078/* Typemap definitions to allow SWIG to properly handle char buffer. */
79
80// typemap for a char buffer
81// See also SBThread::GetStopDescription.
82%typemap(in) (char *dst, size_t dst_len) {
83 if (!PyInt_Check($input)) {
84 PyErr_SetString(PyExc_ValueError, "Expecting an integer");
85 return NULL;
86 }
87 $2 = PyInt_AsLong($input);
Johnny Chende8241c2011-03-23 00:26:08 +000088 if ($2 <= 0) {
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000089 PyErr_SetString(PyExc_ValueError, "Positive integer expected");
90 return NULL;
91 }
92 $1 = (char *) malloc($2);
93}
94
95// Return the char buffer. Discarding any previous return result
96// See also SBThread::GetStopDescription.
97%typemap(argout) (char *dst, size_t dst_len) {
98 Py_XDECREF($result); /* Blow away any previous result */
99 $result = PyString_FromStringAndSize(($1),result);
100 free($1);
101}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102
Greg Claytonca512b32011-01-14 04:54:56 +0000103
104// typemap for an outgoing buffer
Johnny Chen3a709ac2011-07-08 23:02:33 +0000105// See also SBEvent::SBEvent(uint32_t event, const char *cstr, uint32_t cstr_len).
106%typemap(in) (const char *cstr, uint32_t cstr_len) {
107 if (!PyString_Check($input)) {
108 PyErr_SetString(PyExc_ValueError, "Expecting a string");
109 return NULL;
110 }
111 $1 = (char *) PyString_AsString($input);
112 $2 = PyString_Size($input);
113}
114// And SBProcess::WriteMemory.
Johnny Chen37f99fd2011-03-01 02:20:14 +0000115%typemap(in) (const void *buf, size_t size) {
Greg Claytonca512b32011-01-14 04:54:56 +0000116 if (!PyString_Check($input)) {
117 PyErr_SetString(PyExc_ValueError, "Expecting a string");
118 return NULL;
119 }
120 $1 = (void *) PyString_AsString($input);
121 $2 = PyString_Size($input);
122}
123
124// typemap for an incoming buffer
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000125// See also SBProcess::ReadMemory.
Johnny Chen37f99fd2011-03-01 02:20:14 +0000126%typemap(in) (void *buf, size_t size) {
Greg Claytonca512b32011-01-14 04:54:56 +0000127 if (!PyInt_Check($input)) {
128 PyErr_SetString(PyExc_ValueError, "Expecting an integer");
129 return NULL;
130 }
131 $2 = PyInt_AsLong($input);
Johnny Chende8241c2011-03-23 00:26:08 +0000132 if ($2 <= 0) {
Greg Claytonca512b32011-01-14 04:54:56 +0000133 PyErr_SetString(PyExc_ValueError, "Positive integer expected");
134 return NULL;
135 }
136 $1 = (void *) malloc($2);
137}
138
139// Return the buffer. Discarding any previous return result
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000140// See also SBProcess::ReadMemory.
Johnny Chen37f99fd2011-03-01 02:20:14 +0000141%typemap(argout) (void *buf, size_t size) {
Greg Claytonca512b32011-01-14 04:54:56 +0000142 Py_XDECREF($result); /* Blow away any previous result */
Johnny Chen37f99fd2011-03-01 02:20:14 +0000143 $result = PyString_FromStringAndSize(static_cast<const char*>($1),result);
Greg Claytonca512b32011-01-14 04:54:56 +0000144 free($1);
145}
146
Greg Claytonc0cc73e2010-06-12 15:34:20 +0000147/* The liblldb header files to be included. */
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148
149%{
Greg Claytone0d378b2011-03-24 21:19:54 +0000150#include "lldb/lldb-public.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151#include "lldb/API/SBAddress.h"
152#include "lldb/API/SBBlock.h"
153#include "lldb/API/SBBreakpoint.h"
154#include "lldb/API/SBBreakpointLocation.h"
155#include "lldb/API/SBBroadcaster.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156#include "lldb/API/SBCommandInterpreter.h"
157#include "lldb/API/SBCommandReturnObject.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000158#include "lldb/API/SBCommunication.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159#include "lldb/API/SBCompileUnit.h"
160#include "lldb/API/SBDebugger.h"
161#include "lldb/API/SBError.h"
162#include "lldb/API/SBEvent.h"
Johnny Chen23fd10c2010-08-27 22:35:26 +0000163#include "lldb/API/SBFileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164#include "lldb/API/SBFrame.h"
165#include "lldb/API/SBFunction.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000166#include "lldb/API/SBHostOS.h"
167#include "lldb/API/SBInputReader.h"
Greg Clayton1d273162010-10-06 03:09:58 +0000168#include "lldb/API/SBInstruction.h"
169#include "lldb/API/SBInstructionList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170#include "lldb/API/SBLineEntry.h"
171#include "lldb/API/SBListener.h"
172#include "lldb/API/SBModule.h"
173#include "lldb/API/SBProcess.h"
174#include "lldb/API/SBSourceManager.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +0000175#include "lldb/API/SBStream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176#include "lldb/API/SBStringList.h"
177#include "lldb/API/SBSymbol.h"
178#include "lldb/API/SBSymbolContext.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000179#include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180#include "lldb/API/SBTarget.h"
181#include "lldb/API/SBThread.h"
182#include "lldb/API/SBType.h"
183#include "lldb/API/SBValue.h"
Caroline Tice77404122010-09-22 16:41:52 +0000184#include "lldb/API/SBValueList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185%}
186
187/* Various liblldb typedefs that SWIG needs to know about. */
Johnny Chen4b332092010-12-16 00:01:06 +0000188#define __extension__ /* Undefine GCC keyword to make Swig happy when processing glibc's stdint.h. */
Greg Clayton05faeb72010-10-07 04:19:01 +0000189%include <stdint.h>
190%include "lldb/lldb-defines.h"
191%include "lldb/lldb-enumerations.h"
192%include "lldb/lldb-forward.h"
193%include "lldb/lldb-forward-rtti.h"
194%include "lldb/lldb-types.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195%include "lldb/API/SBAddress.h"
196%include "lldb/API/SBBlock.h"
197%include "lldb/API/SBBreakpoint.h"
198%include "lldb/API/SBBreakpointLocation.h"
199%include "lldb/API/SBBroadcaster.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200%include "lldb/API/SBCommandInterpreter.h"
201%include "lldb/API/SBCommandReturnObject.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000202%include "lldb/API/SBCommunication.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203%include "lldb/API/SBCompileUnit.h"
204%include "lldb/API/SBDebugger.h"
205%include "lldb/API/SBError.h"
206%include "lldb/API/SBEvent.h"
Johnny Chen23fd10c2010-08-27 22:35:26 +0000207%include "lldb/API/SBFileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208%include "lldb/API/SBFrame.h"
209%include "lldb/API/SBFunction.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000210%include "lldb/API/SBHostOS.h"
211%include "lldb/API/SBInputReader.h"
Greg Clayton1d273162010-10-06 03:09:58 +0000212%include "lldb/API/SBInstruction.h"
213%include "lldb/API/SBInstructionList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214%include "lldb/API/SBLineEntry.h"
215%include "lldb/API/SBListener.h"
216%include "lldb/API/SBModule.h"
217%include "lldb/API/SBProcess.h"
218%include "lldb/API/SBSourceManager.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +0000219%include "lldb/API/SBStream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220%include "lldb/API/SBStringList.h"
221%include "lldb/API/SBSymbol.h"
222%include "lldb/API/SBSymbolContext.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000223%include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224%include "lldb/API/SBTarget.h"
225%include "lldb/API/SBThread.h"
226%include "lldb/API/SBType.h"
227%include "lldb/API/SBValue.h"
Caroline Tice77404122010-09-22 16:41:52 +0000228%include "lldb/API/SBValueList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229
Caroline Ticedac97f32010-09-22 23:01:29 +0000230%include "./Python/python-extensions.swig"
Caroline Tice18474c92010-09-27 18:00:20 +0000231
232
233%wrapper %{
234
Greg Claytonc6ed5422010-10-07 17:14:24 +0000235// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...)
236// and is used when a script command is attached to a breakpoint for execution.
Caroline Tice18474c92010-09-27 18:00:20 +0000237
Greg Claytonc6ed5422010-10-07 17:14:24 +0000238SWIGEXPORT bool
Greg Claytonfc36f7912011-03-22 01:14:58 +0000239LLDBSwigPythonBreakpointCallbackFunction
Greg Clayton05faeb72010-10-07 04:19:01 +0000240(
Greg Claytonc6ed5422010-10-07 17:14:24 +0000241 const char *python_function_name,
Caroline Tice2f88aad2011-01-14 00:29:16 +0000242 const char *session_dictionary_name,
Greg Claytonfc36f7912011-03-22 01:14:58 +0000243 const lldb::StackFrameSP& frame_sp,
244 const lldb::BreakpointLocationSP& bp_loc_sp
Greg Clayton05faeb72010-10-07 04:19:01 +0000245)
Caroline Tice18474c92010-09-27 18:00:20 +0000246{
Greg Claytonfc36f7912011-03-22 01:14:58 +0000247 lldb::SBFrame sb_frame (frame_sp);
248 lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
249
Greg Claytonc6ed5422010-10-07 17:14:24 +0000250 bool stop_at_breakpoint = true;
251 PyObject *Frame_PyObj = SWIG_NewPointerObj((void *) &sb_frame, SWIGTYPE_p_lldb__SBFrame, 0);
252 PyObject *Bp_Loc_PyObj = SWIG_NewPointerObj ((void *) &sb_bp_loc, SWIGTYPE_p_lldb__SBBreakpointLocation, 0);
Caroline Tice18474c92010-09-27 18:00:20 +0000253
Greg Claytonc6ed5422010-10-07 17:14:24 +0000254 if (Frame_PyObj == NULL || Bp_Loc_PyObj == NULL)
255 return stop_at_breakpoint;
Caroline Tice2f88aad2011-01-14 00:29:16 +0000256
257 if (!python_function_name || !session_dictionary_name)
258 return stop_at_breakpoint;
Greg Claytonc6ed5422010-10-07 17:14:24 +0000259
Caroline Tice2f88aad2011-01-14 00:29:16 +0000260 PyObject *pmodule, *main_dict, *session_dict, *pfunc;
Greg Claytonc6ed5422010-10-07 17:14:24 +0000261 PyObject *pargs, *pvalue;
Caroline Tice18474c92010-09-27 18:00:20 +0000262
Greg Claytonc6ed5422010-10-07 17:14:24 +0000263 pmodule = PyImport_AddModule ("__main__");
264 if (pmodule != NULL)
Caroline Tice18474c92010-09-27 18:00:20 +0000265 {
Caroline Tice2f88aad2011-01-14 00:29:16 +0000266 main_dict = PyModule_GetDict (pmodule);
267 if (main_dict != NULL)
Caroline Tice18474c92010-09-27 18:00:20 +0000268 {
Caroline Tice2f88aad2011-01-14 00:29:16 +0000269 PyObject *key, *value;
270 Py_ssize_t pos = 0;
271
272 // Find the current session's dictionary in the main module's dictionary.
273
274 if (PyDict_Check (main_dict))
275
276 {
277 session_dict = NULL;
278 while (PyDict_Next (main_dict, &pos, &key, &value))
279 {
280 // We have stolen references to the key and value objects in the dictionary; we need to increment
281 // them now so that Python's garbage collector doesn't collect them out from under us.
282 Py_INCREF (key);
283 Py_INCREF (value);
284 if (strcmp (PyString_AsString (key), session_dictionary_name) == 0)
285 {
286 session_dict = value;
287 break;
288 }
289 }
290 }
291
292 if (!session_dict || !PyDict_Check (session_dict))
293 return stop_at_breakpoint;
294
295 // Find the function we need to call in the current session's dictionary.
296
297 pos = 0;
298 pfunc = NULL;
299 while (PyDict_Next (session_dict, &pos, &key, &value))
300 {
301 if (PyString_Check (key))
302 {
303 // We have stolen references to the key and value objects in the dictionary; we need to increment
304 // them now so that Python's garbage collector doesn't collect them out from under us.
305 Py_INCREF (key);
306 Py_INCREF (value);
307 if (strcmp (PyString_AsString (key), python_function_name) == 0)
308 {
309 pfunc = value;
310 break;
311 }
312 }
313 }
314
315 // Set up the arguments and call the function.
316
Greg Claytonc6ed5422010-10-07 17:14:24 +0000317 if (pfunc && PyCallable_Check (pfunc))
Caroline Tice18474c92010-09-27 18:00:20 +0000318 {
Caroline Tice2f88aad2011-01-14 00:29:16 +0000319 pargs = PyTuple_New (3);
Greg Claytonc6ed5422010-10-07 17:14:24 +0000320 if (pargs == NULL)
Caroline Tice18474c92010-09-27 18:00:20 +0000321 {
Greg Claytonc6ed5422010-10-07 17:14:24 +0000322 if (PyErr_Occurred())
Caroline Tice18474c92010-09-27 18:00:20 +0000323 PyErr_Clear();
Greg Claytonc6ed5422010-10-07 17:14:24 +0000324 return stop_at_breakpoint;
Caroline Tice18474c92010-09-27 18:00:20 +0000325 }
Greg Claytonc6ed5422010-10-07 17:14:24 +0000326
327 PyTuple_SetItem (pargs, 0, Frame_PyObj); // This "steals" a reference to Frame_PyObj
328 PyTuple_SetItem (pargs, 1, Bp_Loc_PyObj); // This "steals" a reference to Bp_Loc_PyObj
Caroline Tice2f88aad2011-01-14 00:29:16 +0000329 PyTuple_SetItem (pargs, 2, session_dict); // This "steals" a reference to session_dict
Greg Claytonc6ed5422010-10-07 17:14:24 +0000330 pvalue = PyObject_CallObject (pfunc, pargs);
331 Py_DECREF (pargs);
332
333 if (pvalue != NULL)
334 {
335 Py_DECREF (pvalue);
336 }
337 else if (PyErr_Occurred ())
Caroline Tice18474c92010-09-27 18:00:20 +0000338 {
339 PyErr_Clear();
340 }
Caroline Tice2f88aad2011-01-14 00:29:16 +0000341 Py_INCREF (session_dict);
Caroline Tice18474c92010-09-27 18:00:20 +0000342 }
343 else if (PyErr_Occurred())
344 {
345 PyErr_Clear();
346 }
347 }
Greg Claytonc6ed5422010-10-07 17:14:24 +0000348 else if (PyErr_Occurred())
Caroline Tice18474c92010-09-27 18:00:20 +0000349 {
Greg Claytonc6ed5422010-10-07 17:14:24 +0000350 PyErr_Clear();
Caroline Tice18474c92010-09-27 18:00:20 +0000351 }
352 }
Greg Claytonc6ed5422010-10-07 17:14:24 +0000353 else if (PyErr_Occurred ())
354 {
355 PyErr_Clear ();
356 }
357 return stop_at_breakpoint;
Caroline Tice18474c92010-09-27 18:00:20 +0000358}
359
360%}