blob: ff0463305d6d803c60ddcd2c15348018c17d68ac [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.
7
8*/
9
10/* The name of the module to be created. */
11
12%module lldb
13
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014/* Typemap definitions, to allow SWIG to properly handle 'char**' data types. */
15
16%typemap(in) char ** {
17 /* Check if is a list */
18 if (PyList_Check($input)) {
19 int size = PyList_Size($input);
20 int i = 0;
Greg Claytonca512b32011-01-14 04:54:56 +000021 $1 = (char **) malloc((size+1) * sizeof(char*));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022 for (i = 0; i < size; i++) {
23 PyObject *o = PyList_GetItem($input,i);
24 if (PyString_Check(o))
Greg Claytonca512b32011-01-14 04:54:56 +000025 $1[i] = PyString_AsString(o);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026 else {
27 PyErr_SetString(PyExc_TypeError,"list must contain strings");
28 free($1);
29 return NULL;
30 }
31 }
32 $1[i] = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000033 } else if ($input == Py_None) {
34 $1 = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035 } else {
36 PyErr_SetString(PyExc_TypeError,"not a list");
37 return NULL;
38 }
39}
40
41%typemap(freearg) char** {
42 free((char *) $1);
43}
44
45%typemap(out) char** {
46 int len;
47 int i;
48 len = 0;
49 while ($1[len]) len++;
50 $result = PyList_New(len);
51 for (i = 0; i < len; i++) {
52 PyList_SetItem($result, i, PyString_FromString($1[i]));
53 }
54}
55
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000056/* Typemap definitions to allow SWIG to properly handle char buffer. */
57
58// typemap for a char buffer
59// See also SBThread::GetStopDescription.
60%typemap(in) (char *dst, size_t dst_len) {
61 if (!PyInt_Check($input)) {
62 PyErr_SetString(PyExc_ValueError, "Expecting an integer");
63 return NULL;
64 }
65 $2 = PyInt_AsLong($input);
66 if ($2 < 0) {
67 PyErr_SetString(PyExc_ValueError, "Positive integer expected");
68 return NULL;
69 }
70 $1 = (char *) malloc($2);
71}
72
73// Return the char buffer. Discarding any previous return result
74// See also SBThread::GetStopDescription.
75%typemap(argout) (char *dst, size_t dst_len) {
76 Py_XDECREF($result); /* Blow away any previous result */
77 $result = PyString_FromStringAndSize(($1),result);
78 free($1);
79}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080
Greg Claytonca512b32011-01-14 04:54:56 +000081
82// typemap for an outgoing buffer
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000083// See also SBProcess::WriteMemory.
Johnny Chen37f99fd2011-03-01 02:20:14 +000084%typemap(in) (const void *buf, size_t size) {
Greg Claytonca512b32011-01-14 04:54:56 +000085 if (!PyString_Check($input)) {
86 PyErr_SetString(PyExc_ValueError, "Expecting a string");
87 return NULL;
88 }
89 $1 = (void *) PyString_AsString($input);
90 $2 = PyString_Size($input);
91}
92
93// typemap for an incoming buffer
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000094// See also SBProcess::ReadMemory.
Johnny Chen37f99fd2011-03-01 02:20:14 +000095%typemap(in) (void *buf, size_t size) {
Greg Claytonca512b32011-01-14 04:54:56 +000096 if (!PyInt_Check($input)) {
97 PyErr_SetString(PyExc_ValueError, "Expecting an integer");
98 return NULL;
99 }
100 $2 = PyInt_AsLong($input);
101 if ($2 < 0) {
102 PyErr_SetString(PyExc_ValueError, "Positive integer expected");
103 return NULL;
104 }
105 $1 = (void *) malloc($2);
106}
107
108// Return the buffer. Discarding any previous return result
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000109// See also SBProcess::ReadMemory.
Johnny Chen37f99fd2011-03-01 02:20:14 +0000110%typemap(argout) (void *buf, size_t size) {
Greg Claytonca512b32011-01-14 04:54:56 +0000111 Py_XDECREF($result); /* Blow away any previous result */
Johnny Chen37f99fd2011-03-01 02:20:14 +0000112 $result = PyString_FromStringAndSize(static_cast<const char*>($1),result);
Greg Claytonca512b32011-01-14 04:54:56 +0000113 free($1);
114}
115
Greg Claytonc0cc73e2010-06-12 15:34:20 +0000116/* The liblldb header files to be included. */
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117
118%{
Greg Clayton05faeb72010-10-07 04:19:01 +0000119#include "lldb/lldb-include.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120#include "lldb/API/SBAddress.h"
121#include "lldb/API/SBBlock.h"
122#include "lldb/API/SBBreakpoint.h"
123#include "lldb/API/SBBreakpointLocation.h"
124#include "lldb/API/SBBroadcaster.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125#include "lldb/API/SBCommandInterpreter.h"
126#include "lldb/API/SBCommandReturnObject.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000127#include "lldb/API/SBCommunication.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128#include "lldb/API/SBCompileUnit.h"
129#include "lldb/API/SBDebugger.h"
130#include "lldb/API/SBError.h"
131#include "lldb/API/SBEvent.h"
Johnny Chen23fd10c2010-08-27 22:35:26 +0000132#include "lldb/API/SBFileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133#include "lldb/API/SBFrame.h"
134#include "lldb/API/SBFunction.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000135#include "lldb/API/SBHostOS.h"
136#include "lldb/API/SBInputReader.h"
Greg Clayton1d273162010-10-06 03:09:58 +0000137#include "lldb/API/SBInstruction.h"
138#include "lldb/API/SBInstructionList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139#include "lldb/API/SBLineEntry.h"
140#include "lldb/API/SBListener.h"
141#include "lldb/API/SBModule.h"
142#include "lldb/API/SBProcess.h"
143#include "lldb/API/SBSourceManager.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +0000144#include "lldb/API/SBStream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145#include "lldb/API/SBStringList.h"
146#include "lldb/API/SBSymbol.h"
147#include "lldb/API/SBSymbolContext.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000148#include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149#include "lldb/API/SBTarget.h"
150#include "lldb/API/SBThread.h"
151#include "lldb/API/SBType.h"
152#include "lldb/API/SBValue.h"
Caroline Tice77404122010-09-22 16:41:52 +0000153#include "lldb/API/SBValueList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154%}
155
156/* Various liblldb typedefs that SWIG needs to know about. */
Johnny Chen4b332092010-12-16 00:01:06 +0000157#define __extension__ /* Undefine GCC keyword to make Swig happy when processing glibc's stdint.h. */
Greg Clayton05faeb72010-10-07 04:19:01 +0000158%include <stdint.h>
159%include "lldb/lldb-defines.h"
160%include "lldb/lldb-enumerations.h"
161%include "lldb/lldb-forward.h"
162%include "lldb/lldb-forward-rtti.h"
163%include "lldb/lldb-types.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164%include "lldb/API/SBAddress.h"
165%include "lldb/API/SBBlock.h"
166%include "lldb/API/SBBreakpoint.h"
167%include "lldb/API/SBBreakpointLocation.h"
168%include "lldb/API/SBBroadcaster.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169%include "lldb/API/SBCommandInterpreter.h"
170%include "lldb/API/SBCommandReturnObject.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000171%include "lldb/API/SBCommunication.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172%include "lldb/API/SBCompileUnit.h"
173%include "lldb/API/SBDebugger.h"
174%include "lldb/API/SBError.h"
175%include "lldb/API/SBEvent.h"
Johnny Chen23fd10c2010-08-27 22:35:26 +0000176%include "lldb/API/SBFileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177%include "lldb/API/SBFrame.h"
178%include "lldb/API/SBFunction.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000179%include "lldb/API/SBHostOS.h"
180%include "lldb/API/SBInputReader.h"
Greg Clayton1d273162010-10-06 03:09:58 +0000181%include "lldb/API/SBInstruction.h"
182%include "lldb/API/SBInstructionList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183%include "lldb/API/SBLineEntry.h"
184%include "lldb/API/SBListener.h"
185%include "lldb/API/SBModule.h"
186%include "lldb/API/SBProcess.h"
187%include "lldb/API/SBSourceManager.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +0000188%include "lldb/API/SBStream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189%include "lldb/API/SBStringList.h"
190%include "lldb/API/SBSymbol.h"
191%include "lldb/API/SBSymbolContext.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000192%include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193%include "lldb/API/SBTarget.h"
194%include "lldb/API/SBThread.h"
195%include "lldb/API/SBType.h"
196%include "lldb/API/SBValue.h"
Caroline Tice77404122010-09-22 16:41:52 +0000197%include "lldb/API/SBValueList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198
Caroline Ticedac97f32010-09-22 23:01:29 +0000199%include "./Python/python-extensions.swig"
Caroline Tice18474c92010-09-27 18:00:20 +0000200
201
202%wrapper %{
203
Greg Claytonc6ed5422010-10-07 17:14:24 +0000204// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...)
205// and is used when a script command is attached to a breakpoint for execution.
Caroline Tice18474c92010-09-27 18:00:20 +0000206
Greg Claytonc6ed5422010-10-07 17:14:24 +0000207SWIGEXPORT bool
208LLDBSWIGPythonBreakpointCallbackFunction
Greg Clayton05faeb72010-10-07 04:19:01 +0000209(
Greg Claytonc6ed5422010-10-07 17:14:24 +0000210 const char *python_function_name,
Caroline Tice2f88aad2011-01-14 00:29:16 +0000211 const char *session_dictionary_name,
Greg Claytonc6ed5422010-10-07 17:14:24 +0000212 lldb::SBFrame& sb_frame,
213 lldb::SBBreakpointLocation& sb_bp_loc
Greg Clayton05faeb72010-10-07 04:19:01 +0000214)
Caroline Tice18474c92010-09-27 18:00:20 +0000215{
Greg Claytonc6ed5422010-10-07 17:14:24 +0000216 bool stop_at_breakpoint = true;
217 PyObject *Frame_PyObj = SWIG_NewPointerObj((void *) &sb_frame, SWIGTYPE_p_lldb__SBFrame, 0);
218 PyObject *Bp_Loc_PyObj = SWIG_NewPointerObj ((void *) &sb_bp_loc, SWIGTYPE_p_lldb__SBBreakpointLocation, 0);
Caroline Tice18474c92010-09-27 18:00:20 +0000219
Greg Claytonc6ed5422010-10-07 17:14:24 +0000220 if (Frame_PyObj == NULL || Bp_Loc_PyObj == NULL)
221 return stop_at_breakpoint;
Caroline Tice2f88aad2011-01-14 00:29:16 +0000222
223 if (!python_function_name || !session_dictionary_name)
224 return stop_at_breakpoint;
Greg Claytonc6ed5422010-10-07 17:14:24 +0000225
Caroline Tice2f88aad2011-01-14 00:29:16 +0000226 PyObject *pmodule, *main_dict, *session_dict, *pfunc;
Greg Claytonc6ed5422010-10-07 17:14:24 +0000227 PyObject *pargs, *pvalue;
Caroline Tice18474c92010-09-27 18:00:20 +0000228
Greg Claytonc6ed5422010-10-07 17:14:24 +0000229 pmodule = PyImport_AddModule ("__main__");
230 if (pmodule != NULL)
Caroline Tice18474c92010-09-27 18:00:20 +0000231 {
Caroline Tice2f88aad2011-01-14 00:29:16 +0000232 main_dict = PyModule_GetDict (pmodule);
233 if (main_dict != NULL)
Caroline Tice18474c92010-09-27 18:00:20 +0000234 {
Caroline Tice2f88aad2011-01-14 00:29:16 +0000235 PyObject *key, *value;
236 Py_ssize_t pos = 0;
237
238 // Find the current session's dictionary in the main module's dictionary.
239
240 if (PyDict_Check (main_dict))
241
242 {
243 session_dict = NULL;
244 while (PyDict_Next (main_dict, &pos, &key, &value))
245 {
246 // We have stolen references to the key and value objects in the dictionary; we need to increment
247 // them now so that Python's garbage collector doesn't collect them out from under us.
248 Py_INCREF (key);
249 Py_INCREF (value);
250 if (strcmp (PyString_AsString (key), session_dictionary_name) == 0)
251 {
252 session_dict = value;
253 break;
254 }
255 }
256 }
257
258 if (!session_dict || !PyDict_Check (session_dict))
259 return stop_at_breakpoint;
260
261 // Find the function we need to call in the current session's dictionary.
262
263 pos = 0;
264 pfunc = NULL;
265 while (PyDict_Next (session_dict, &pos, &key, &value))
266 {
267 if (PyString_Check (key))
268 {
269 // We have stolen references to the key and value objects in the dictionary; we need to increment
270 // them now so that Python's garbage collector doesn't collect them out from under us.
271 Py_INCREF (key);
272 Py_INCREF (value);
273 if (strcmp (PyString_AsString (key), python_function_name) == 0)
274 {
275 pfunc = value;
276 break;
277 }
278 }
279 }
280
281 // Set up the arguments and call the function.
282
Greg Claytonc6ed5422010-10-07 17:14:24 +0000283 if (pfunc && PyCallable_Check (pfunc))
Caroline Tice18474c92010-09-27 18:00:20 +0000284 {
Caroline Tice2f88aad2011-01-14 00:29:16 +0000285 pargs = PyTuple_New (3);
Greg Claytonc6ed5422010-10-07 17:14:24 +0000286 if (pargs == NULL)
Caroline Tice18474c92010-09-27 18:00:20 +0000287 {
Greg Claytonc6ed5422010-10-07 17:14:24 +0000288 if (PyErr_Occurred())
Caroline Tice18474c92010-09-27 18:00:20 +0000289 PyErr_Clear();
Greg Claytonc6ed5422010-10-07 17:14:24 +0000290 return stop_at_breakpoint;
Caroline Tice18474c92010-09-27 18:00:20 +0000291 }
Greg Claytonc6ed5422010-10-07 17:14:24 +0000292
293 PyTuple_SetItem (pargs, 0, Frame_PyObj); // This "steals" a reference to Frame_PyObj
294 PyTuple_SetItem (pargs, 1, Bp_Loc_PyObj); // This "steals" a reference to Bp_Loc_PyObj
Caroline Tice2f88aad2011-01-14 00:29:16 +0000295 PyTuple_SetItem (pargs, 2, session_dict); // This "steals" a reference to session_dict
Greg Claytonc6ed5422010-10-07 17:14:24 +0000296 pvalue = PyObject_CallObject (pfunc, pargs);
297 Py_DECREF (pargs);
298
299 if (pvalue != NULL)
300 {
301 Py_DECREF (pvalue);
302 }
303 else if (PyErr_Occurred ())
Caroline Tice18474c92010-09-27 18:00:20 +0000304 {
305 PyErr_Clear();
306 }
Caroline Tice2f88aad2011-01-14 00:29:16 +0000307 Py_INCREF (session_dict);
Caroline Tice18474c92010-09-27 18:00:20 +0000308 }
309 else if (PyErr_Occurred())
310 {
311 PyErr_Clear();
312 }
313 }
Greg Claytonc6ed5422010-10-07 17:14:24 +0000314 else if (PyErr_Occurred())
Caroline Tice18474c92010-09-27 18:00:20 +0000315 {
Greg Claytonc6ed5422010-10-07 17:14:24 +0000316 PyErr_Clear();
Caroline Tice18474c92010-09-27 18:00:20 +0000317 }
318 }
Greg Claytonc6ed5422010-10-07 17:14:24 +0000319 else if (PyErr_Occurred ())
320 {
321 PyErr_Clear ();
322 }
323 return stop_at_breakpoint;
Caroline Tice18474c92010-09-27 18:00:20 +0000324}
325
326%}