blob: 752be409b909d653b0624a25cfe6f1756b6f109d [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
56
Greg Claytonca512b32011-01-14 04:54:56 +000057
58// typemap for an outgoing buffer
Johnny Chen37f99fd2011-03-01 02:20:14 +000059%typemap(in) (const void *buf, size_t size) {
Greg Claytonca512b32011-01-14 04:54:56 +000060 if (!PyString_Check($input)) {
61 PyErr_SetString(PyExc_ValueError, "Expecting a string");
62 return NULL;
63 }
64 $1 = (void *) PyString_AsString($input);
65 $2 = PyString_Size($input);
66}
67
68// typemap for an incoming buffer
Johnny Chen37f99fd2011-03-01 02:20:14 +000069%typemap(in) (void *buf, size_t size) {
Greg Claytonca512b32011-01-14 04:54:56 +000070 if (!PyInt_Check($input)) {
71 PyErr_SetString(PyExc_ValueError, "Expecting an integer");
72 return NULL;
73 }
74 $2 = PyInt_AsLong($input);
75 if ($2 < 0) {
76 PyErr_SetString(PyExc_ValueError, "Positive integer expected");
77 return NULL;
78 }
79 $1 = (void *) malloc($2);
80}
81
82// Return the buffer. Discarding any previous return result
Johnny Chen37f99fd2011-03-01 02:20:14 +000083%typemap(argout) (void *buf, size_t size) {
Greg Claytonca512b32011-01-14 04:54:56 +000084 Py_XDECREF($result); /* Blow away any previous result */
85 if (result < 0) { /* Check for I/O error */
86 free($1);
87 PyErr_SetFromErrno(PyExc_IOError);
88 return NULL;
89 }
Johnny Chen37f99fd2011-03-01 02:20:14 +000090 $result = PyString_FromStringAndSize(static_cast<const char*>($1),result);
Greg Claytonca512b32011-01-14 04:54:56 +000091 free($1);
92}
93
Greg Claytonc0cc73e2010-06-12 15:34:20 +000094/* The liblldb header files to be included. */
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095
96%{
Greg Clayton05faeb72010-10-07 04:19:01 +000097#include "lldb/lldb-include.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098#include "lldb/API/SBAddress.h"
99#include "lldb/API/SBBlock.h"
100#include "lldb/API/SBBreakpoint.h"
101#include "lldb/API/SBBreakpointLocation.h"
102#include "lldb/API/SBBroadcaster.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103#include "lldb/API/SBCommandInterpreter.h"
104#include "lldb/API/SBCommandReturnObject.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000105#include "lldb/API/SBCommunication.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106#include "lldb/API/SBCompileUnit.h"
107#include "lldb/API/SBDebugger.h"
108#include "lldb/API/SBError.h"
109#include "lldb/API/SBEvent.h"
Johnny Chen23fd10c2010-08-27 22:35:26 +0000110#include "lldb/API/SBFileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111#include "lldb/API/SBFrame.h"
112#include "lldb/API/SBFunction.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000113#include "lldb/API/SBHostOS.h"
114#include "lldb/API/SBInputReader.h"
Greg Clayton1d273162010-10-06 03:09:58 +0000115#include "lldb/API/SBInstruction.h"
116#include "lldb/API/SBInstructionList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117#include "lldb/API/SBLineEntry.h"
118#include "lldb/API/SBListener.h"
119#include "lldb/API/SBModule.h"
120#include "lldb/API/SBProcess.h"
121#include "lldb/API/SBSourceManager.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +0000122#include "lldb/API/SBStream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123#include "lldb/API/SBStringList.h"
124#include "lldb/API/SBSymbol.h"
125#include "lldb/API/SBSymbolContext.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000126#include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127#include "lldb/API/SBTarget.h"
128#include "lldb/API/SBThread.h"
129#include "lldb/API/SBType.h"
130#include "lldb/API/SBValue.h"
Caroline Tice77404122010-09-22 16:41:52 +0000131#include "lldb/API/SBValueList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132%}
133
134/* Various liblldb typedefs that SWIG needs to know about. */
Johnny Chen4b332092010-12-16 00:01:06 +0000135#define __extension__ /* Undefine GCC keyword to make Swig happy when processing glibc's stdint.h. */
Greg Clayton05faeb72010-10-07 04:19:01 +0000136%include <stdint.h>
137%include "lldb/lldb-defines.h"
138%include "lldb/lldb-enumerations.h"
139%include "lldb/lldb-forward.h"
140%include "lldb/lldb-forward-rtti.h"
141%include "lldb/lldb-types.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142%include "lldb/API/SBAddress.h"
143%include "lldb/API/SBBlock.h"
144%include "lldb/API/SBBreakpoint.h"
145%include "lldb/API/SBBreakpointLocation.h"
146%include "lldb/API/SBBroadcaster.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147%include "lldb/API/SBCommandInterpreter.h"
148%include "lldb/API/SBCommandReturnObject.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000149%include "lldb/API/SBCommunication.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150%include "lldb/API/SBCompileUnit.h"
151%include "lldb/API/SBDebugger.h"
152%include "lldb/API/SBError.h"
153%include "lldb/API/SBEvent.h"
Johnny Chen23fd10c2010-08-27 22:35:26 +0000154%include "lldb/API/SBFileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155%include "lldb/API/SBFrame.h"
156%include "lldb/API/SBFunction.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000157%include "lldb/API/SBHostOS.h"
158%include "lldb/API/SBInputReader.h"
Greg Clayton1d273162010-10-06 03:09:58 +0000159%include "lldb/API/SBInstruction.h"
160%include "lldb/API/SBInstructionList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161%include "lldb/API/SBLineEntry.h"
162%include "lldb/API/SBListener.h"
163%include "lldb/API/SBModule.h"
164%include "lldb/API/SBProcess.h"
165%include "lldb/API/SBSourceManager.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +0000166%include "lldb/API/SBStream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167%include "lldb/API/SBStringList.h"
168%include "lldb/API/SBSymbol.h"
169%include "lldb/API/SBSymbolContext.h"
Greg Clayton05faeb72010-10-07 04:19:01 +0000170%include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171%include "lldb/API/SBTarget.h"
172%include "lldb/API/SBThread.h"
173%include "lldb/API/SBType.h"
174%include "lldb/API/SBValue.h"
Caroline Tice77404122010-09-22 16:41:52 +0000175%include "lldb/API/SBValueList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176
Caroline Ticedac97f32010-09-22 23:01:29 +0000177%include "./Python/python-extensions.swig"
Caroline Tice18474c92010-09-27 18:00:20 +0000178
179
180%wrapper %{
181
Greg Claytonc6ed5422010-10-07 17:14:24 +0000182// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...)
183// and is used when a script command is attached to a breakpoint for execution.
Caroline Tice18474c92010-09-27 18:00:20 +0000184
Greg Claytonc6ed5422010-10-07 17:14:24 +0000185SWIGEXPORT bool
186LLDBSWIGPythonBreakpointCallbackFunction
Greg Clayton05faeb72010-10-07 04:19:01 +0000187(
Greg Claytonc6ed5422010-10-07 17:14:24 +0000188 const char *python_function_name,
Caroline Tice2f88aad2011-01-14 00:29:16 +0000189 const char *session_dictionary_name,
Greg Claytonc6ed5422010-10-07 17:14:24 +0000190 lldb::SBFrame& sb_frame,
191 lldb::SBBreakpointLocation& sb_bp_loc
Greg Clayton05faeb72010-10-07 04:19:01 +0000192)
Caroline Tice18474c92010-09-27 18:00:20 +0000193{
Greg Claytonc6ed5422010-10-07 17:14:24 +0000194 bool stop_at_breakpoint = true;
195 PyObject *Frame_PyObj = SWIG_NewPointerObj((void *) &sb_frame, SWIGTYPE_p_lldb__SBFrame, 0);
196 PyObject *Bp_Loc_PyObj = SWIG_NewPointerObj ((void *) &sb_bp_loc, SWIGTYPE_p_lldb__SBBreakpointLocation, 0);
Caroline Tice18474c92010-09-27 18:00:20 +0000197
Greg Claytonc6ed5422010-10-07 17:14:24 +0000198 if (Frame_PyObj == NULL || Bp_Loc_PyObj == NULL)
199 return stop_at_breakpoint;
Caroline Tice2f88aad2011-01-14 00:29:16 +0000200
201 if (!python_function_name || !session_dictionary_name)
202 return stop_at_breakpoint;
Greg Claytonc6ed5422010-10-07 17:14:24 +0000203
Caroline Tice2f88aad2011-01-14 00:29:16 +0000204 PyObject *pmodule, *main_dict, *session_dict, *pfunc;
Greg Claytonc6ed5422010-10-07 17:14:24 +0000205 PyObject *pargs, *pvalue;
Caroline Tice18474c92010-09-27 18:00:20 +0000206
Greg Claytonc6ed5422010-10-07 17:14:24 +0000207 pmodule = PyImport_AddModule ("__main__");
208 if (pmodule != NULL)
Caroline Tice18474c92010-09-27 18:00:20 +0000209 {
Caroline Tice2f88aad2011-01-14 00:29:16 +0000210 main_dict = PyModule_GetDict (pmodule);
211 if (main_dict != NULL)
Caroline Tice18474c92010-09-27 18:00:20 +0000212 {
Caroline Tice2f88aad2011-01-14 00:29:16 +0000213 PyObject *key, *value;
214 Py_ssize_t pos = 0;
215
216 // Find the current session's dictionary in the main module's dictionary.
217
218 if (PyDict_Check (main_dict))
219
220 {
221 session_dict = NULL;
222 while (PyDict_Next (main_dict, &pos, &key, &value))
223 {
224 // We have stolen references to the key and value objects in the dictionary; we need to increment
225 // them now so that Python's garbage collector doesn't collect them out from under us.
226 Py_INCREF (key);
227 Py_INCREF (value);
228 if (strcmp (PyString_AsString (key), session_dictionary_name) == 0)
229 {
230 session_dict = value;
231 break;
232 }
233 }
234 }
235
236 if (!session_dict || !PyDict_Check (session_dict))
237 return stop_at_breakpoint;
238
239 // Find the function we need to call in the current session's dictionary.
240
241 pos = 0;
242 pfunc = NULL;
243 while (PyDict_Next (session_dict, &pos, &key, &value))
244 {
245 if (PyString_Check (key))
246 {
247 // We have stolen references to the key and value objects in the dictionary; we need to increment
248 // them now so that Python's garbage collector doesn't collect them out from under us.
249 Py_INCREF (key);
250 Py_INCREF (value);
251 if (strcmp (PyString_AsString (key), python_function_name) == 0)
252 {
253 pfunc = value;
254 break;
255 }
256 }
257 }
258
259 // Set up the arguments and call the function.
260
Greg Claytonc6ed5422010-10-07 17:14:24 +0000261 if (pfunc && PyCallable_Check (pfunc))
Caroline Tice18474c92010-09-27 18:00:20 +0000262 {
Caroline Tice2f88aad2011-01-14 00:29:16 +0000263 pargs = PyTuple_New (3);
Greg Claytonc6ed5422010-10-07 17:14:24 +0000264 if (pargs == NULL)
Caroline Tice18474c92010-09-27 18:00:20 +0000265 {
Greg Claytonc6ed5422010-10-07 17:14:24 +0000266 if (PyErr_Occurred())
Caroline Tice18474c92010-09-27 18:00:20 +0000267 PyErr_Clear();
Greg Claytonc6ed5422010-10-07 17:14:24 +0000268 return stop_at_breakpoint;
Caroline Tice18474c92010-09-27 18:00:20 +0000269 }
Greg Claytonc6ed5422010-10-07 17:14:24 +0000270
271 PyTuple_SetItem (pargs, 0, Frame_PyObj); // This "steals" a reference to Frame_PyObj
272 PyTuple_SetItem (pargs, 1, Bp_Loc_PyObj); // This "steals" a reference to Bp_Loc_PyObj
Caroline Tice2f88aad2011-01-14 00:29:16 +0000273 PyTuple_SetItem (pargs, 2, session_dict); // This "steals" a reference to session_dict
Greg Claytonc6ed5422010-10-07 17:14:24 +0000274 pvalue = PyObject_CallObject (pfunc, pargs);
275 Py_DECREF (pargs);
276
277 if (pvalue != NULL)
278 {
279 Py_DECREF (pvalue);
280 }
281 else if (PyErr_Occurred ())
Caroline Tice18474c92010-09-27 18:00:20 +0000282 {
283 PyErr_Clear();
284 }
Caroline Tice2f88aad2011-01-14 00:29:16 +0000285 Py_INCREF (session_dict);
Caroline Tice18474c92010-09-27 18:00:20 +0000286 }
287 else if (PyErr_Occurred())
288 {
289 PyErr_Clear();
290 }
291 }
Greg Claytonc6ed5422010-10-07 17:14:24 +0000292 else if (PyErr_Occurred())
Caroline Tice18474c92010-09-27 18:00:20 +0000293 {
Greg Claytonc6ed5422010-10-07 17:14:24 +0000294 PyErr_Clear();
Caroline Tice18474c92010-09-27 18:00:20 +0000295 }
296 }
Greg Claytonc6ed5422010-10-07 17:14:24 +0000297 else if (PyErr_Occurred ())
298 {
299 PyErr_Clear ();
300 }
301 return stop_at_breakpoint;
Caroline Tice18474c92010-09-27 18:00:20 +0000302}
303
304%}