blob: 89fb7e46664d66e3d4ba0debdd6afc9736e43dc4 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001/*
2 lldb.swig
3
Chris Lattner24943d22010-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 Lattner24943d22010-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 Clayton0d62dfd2011-01-14 04:54:56 +000021 $1 = (char **) malloc((size+1) * sizeof(char*));
Chris Lattner24943d22010-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 Clayton0d62dfd2011-01-14 04:54:56 +000025 $1[i] = PyString_AsString(o);
Chris Lattner24943d22010-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 Tice558be582010-06-30 16:22:25 +000033 } else if ($input == Py_None) {
34 $1 = NULL;
Chris Lattner24943d22010-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 Chen586a3e62011-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);
Johnny Chen562ed0e2011-03-23 00:26:08 +000066 if ($2 <= 0) {
Johnny Chen586a3e62011-03-07 21:28:57 +000067 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 Lattner24943d22010-06-08 16:52:24 +000080
Greg Clayton0d62dfd2011-01-14 04:54:56 +000081
82// typemap for an outgoing buffer
Johnny Chen586a3e62011-03-07 21:28:57 +000083// See also SBProcess::WriteMemory.
Johnny Chen36a0f742011-03-01 02:20:14 +000084%typemap(in) (const void *buf, size_t size) {
Greg Clayton0d62dfd2011-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 Chen586a3e62011-03-07 21:28:57 +000094// See also SBProcess::ReadMemory.
Johnny Chen36a0f742011-03-01 02:20:14 +000095%typemap(in) (void *buf, size_t size) {
Greg Clayton0d62dfd2011-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);
Johnny Chen562ed0e2011-03-23 00:26:08 +0000101 if ($2 <= 0) {
Greg Clayton0d62dfd2011-01-14 04:54:56 +0000102 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 Chen586a3e62011-03-07 21:28:57 +0000109// See also SBProcess::ReadMemory.
Johnny Chen36a0f742011-03-01 02:20:14 +0000110%typemap(argout) (void *buf, size_t size) {
Greg Clayton0d62dfd2011-01-14 04:54:56 +0000111 Py_XDECREF($result); /* Blow away any previous result */
Johnny Chen36a0f742011-03-01 02:20:14 +0000112 $result = PyString_FromStringAndSize(static_cast<const char*>($1),result);
Greg Clayton0d62dfd2011-01-14 04:54:56 +0000113 free($1);
114}
115
Greg Clayton461a4372010-06-12 15:34:20 +0000116/* The liblldb header files to be included. */
Chris Lattner24943d22010-06-08 16:52:24 +0000117
118%{
Greg Claytonb3448432011-03-24 21:19:54 +0000119#include "lldb/lldb-public.h"
Chris Lattner24943d22010-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 Lattner24943d22010-06-08 16:52:24 +0000125#include "lldb/API/SBCommandInterpreter.h"
126#include "lldb/API/SBCommandReturnObject.h"
Greg Claytond8c62532010-10-07 04:19:01 +0000127#include "lldb/API/SBCommunication.h"
Chris Lattner24943d22010-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 Chen4ead2e92010-08-27 22:35:26 +0000132#include "lldb/API/SBFileSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +0000133#include "lldb/API/SBFrame.h"
134#include "lldb/API/SBFunction.h"
Greg Claytond8c62532010-10-07 04:19:01 +0000135#include "lldb/API/SBHostOS.h"
136#include "lldb/API/SBInputReader.h"
Greg Clayton5c4c7462010-10-06 03:09:58 +0000137#include "lldb/API/SBInstruction.h"
138#include "lldb/API/SBInstructionList.h"
Chris Lattner24943d22010-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 Tice98f930f2010-09-20 05:20:02 +0000144#include "lldb/API/SBStream.h"
Chris Lattner24943d22010-06-08 16:52:24 +0000145#include "lldb/API/SBStringList.h"
146#include "lldb/API/SBSymbol.h"
147#include "lldb/API/SBSymbolContext.h"
Greg Claytond8c62532010-10-07 04:19:01 +0000148#include "lldb/API/SBSymbolContextList.h"
Chris Lattner24943d22010-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 Tice1ca48b02010-09-22 16:41:52 +0000153#include "lldb/API/SBValueList.h"
Chris Lattner24943d22010-06-08 16:52:24 +0000154%}
155
156/* Various liblldb typedefs that SWIG needs to know about. */
Johnny Chenc1955f22010-12-16 00:01:06 +0000157#define __extension__ /* Undefine GCC keyword to make Swig happy when processing glibc's stdint.h. */
Greg Claytond8c62532010-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 Lattner24943d22010-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 Lattner24943d22010-06-08 16:52:24 +0000169%include "lldb/API/SBCommandInterpreter.h"
170%include "lldb/API/SBCommandReturnObject.h"
Greg Claytond8c62532010-10-07 04:19:01 +0000171%include "lldb/API/SBCommunication.h"
Chris Lattner24943d22010-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 Chen4ead2e92010-08-27 22:35:26 +0000176%include "lldb/API/SBFileSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +0000177%include "lldb/API/SBFrame.h"
178%include "lldb/API/SBFunction.h"
Greg Claytond8c62532010-10-07 04:19:01 +0000179%include "lldb/API/SBHostOS.h"
180%include "lldb/API/SBInputReader.h"
Greg Clayton5c4c7462010-10-06 03:09:58 +0000181%include "lldb/API/SBInstruction.h"
182%include "lldb/API/SBInstructionList.h"
Chris Lattner24943d22010-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 Tice98f930f2010-09-20 05:20:02 +0000188%include "lldb/API/SBStream.h"
Chris Lattner24943d22010-06-08 16:52:24 +0000189%include "lldb/API/SBStringList.h"
190%include "lldb/API/SBSymbol.h"
191%include "lldb/API/SBSymbolContext.h"
Greg Claytond8c62532010-10-07 04:19:01 +0000192%include "lldb/API/SBSymbolContextList.h"
Chris Lattner24943d22010-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 Tice1ca48b02010-09-22 16:41:52 +0000197%include "lldb/API/SBValueList.h"
Chris Lattner24943d22010-06-08 16:52:24 +0000198
Caroline Ticee49ec182010-09-22 23:01:29 +0000199%include "./Python/python-extensions.swig"
Caroline Tice59c5d5d2010-09-27 18:00:20 +0000200
201
202%wrapper %{
203
Greg Clayton5144f382010-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 Tice59c5d5d2010-09-27 18:00:20 +0000206
Greg Clayton5144f382010-10-07 17:14:24 +0000207SWIGEXPORT bool
Greg Claytone86cbb92011-03-22 01:14:58 +0000208LLDBSwigPythonBreakpointCallbackFunction
Greg Claytond8c62532010-10-07 04:19:01 +0000209(
Greg Clayton5144f382010-10-07 17:14:24 +0000210 const char *python_function_name,
Caroline Tice0aa2e552011-01-14 00:29:16 +0000211 const char *session_dictionary_name,
Greg Claytone86cbb92011-03-22 01:14:58 +0000212 const lldb::StackFrameSP& frame_sp,
213 const lldb::BreakpointLocationSP& bp_loc_sp
Greg Claytond8c62532010-10-07 04:19:01 +0000214)
Caroline Tice59c5d5d2010-09-27 18:00:20 +0000215{
Greg Claytone86cbb92011-03-22 01:14:58 +0000216 lldb::SBFrame sb_frame (frame_sp);
217 lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
218
Greg Clayton5144f382010-10-07 17:14:24 +0000219 bool stop_at_breakpoint = true;
220 PyObject *Frame_PyObj = SWIG_NewPointerObj((void *) &sb_frame, SWIGTYPE_p_lldb__SBFrame, 0);
221 PyObject *Bp_Loc_PyObj = SWIG_NewPointerObj ((void *) &sb_bp_loc, SWIGTYPE_p_lldb__SBBreakpointLocation, 0);
Caroline Tice59c5d5d2010-09-27 18:00:20 +0000222
Greg Clayton5144f382010-10-07 17:14:24 +0000223 if (Frame_PyObj == NULL || Bp_Loc_PyObj == NULL)
224 return stop_at_breakpoint;
Caroline Tice0aa2e552011-01-14 00:29:16 +0000225
226 if (!python_function_name || !session_dictionary_name)
227 return stop_at_breakpoint;
Greg Clayton5144f382010-10-07 17:14:24 +0000228
Caroline Tice0aa2e552011-01-14 00:29:16 +0000229 PyObject *pmodule, *main_dict, *session_dict, *pfunc;
Greg Clayton5144f382010-10-07 17:14:24 +0000230 PyObject *pargs, *pvalue;
Caroline Tice59c5d5d2010-09-27 18:00:20 +0000231
Greg Clayton5144f382010-10-07 17:14:24 +0000232 pmodule = PyImport_AddModule ("__main__");
233 if (pmodule != NULL)
Caroline Tice59c5d5d2010-09-27 18:00:20 +0000234 {
Caroline Tice0aa2e552011-01-14 00:29:16 +0000235 main_dict = PyModule_GetDict (pmodule);
236 if (main_dict != NULL)
Caroline Tice59c5d5d2010-09-27 18:00:20 +0000237 {
Caroline Tice0aa2e552011-01-14 00:29:16 +0000238 PyObject *key, *value;
239 Py_ssize_t pos = 0;
240
241 // Find the current session's dictionary in the main module's dictionary.
242
243 if (PyDict_Check (main_dict))
244
245 {
246 session_dict = NULL;
247 while (PyDict_Next (main_dict, &pos, &key, &value))
248 {
249 // We have stolen references to the key and value objects in the dictionary; we need to increment
250 // them now so that Python's garbage collector doesn't collect them out from under us.
251 Py_INCREF (key);
252 Py_INCREF (value);
253 if (strcmp (PyString_AsString (key), session_dictionary_name) == 0)
254 {
255 session_dict = value;
256 break;
257 }
258 }
259 }
260
261 if (!session_dict || !PyDict_Check (session_dict))
262 return stop_at_breakpoint;
263
264 // Find the function we need to call in the current session's dictionary.
265
266 pos = 0;
267 pfunc = NULL;
268 while (PyDict_Next (session_dict, &pos, &key, &value))
269 {
270 if (PyString_Check (key))
271 {
272 // We have stolen references to the key and value objects in the dictionary; we need to increment
273 // them now so that Python's garbage collector doesn't collect them out from under us.
274 Py_INCREF (key);
275 Py_INCREF (value);
276 if (strcmp (PyString_AsString (key), python_function_name) == 0)
277 {
278 pfunc = value;
279 break;
280 }
281 }
282 }
283
284 // Set up the arguments and call the function.
285
Greg Clayton5144f382010-10-07 17:14:24 +0000286 if (pfunc && PyCallable_Check (pfunc))
Caroline Tice59c5d5d2010-09-27 18:00:20 +0000287 {
Caroline Tice0aa2e552011-01-14 00:29:16 +0000288 pargs = PyTuple_New (3);
Greg Clayton5144f382010-10-07 17:14:24 +0000289 if (pargs == NULL)
Caroline Tice59c5d5d2010-09-27 18:00:20 +0000290 {
Greg Clayton5144f382010-10-07 17:14:24 +0000291 if (PyErr_Occurred())
Caroline Tice59c5d5d2010-09-27 18:00:20 +0000292 PyErr_Clear();
Greg Clayton5144f382010-10-07 17:14:24 +0000293 return stop_at_breakpoint;
Caroline Tice59c5d5d2010-09-27 18:00:20 +0000294 }
Greg Clayton5144f382010-10-07 17:14:24 +0000295
296 PyTuple_SetItem (pargs, 0, Frame_PyObj); // This "steals" a reference to Frame_PyObj
297 PyTuple_SetItem (pargs, 1, Bp_Loc_PyObj); // This "steals" a reference to Bp_Loc_PyObj
Caroline Tice0aa2e552011-01-14 00:29:16 +0000298 PyTuple_SetItem (pargs, 2, session_dict); // This "steals" a reference to session_dict
Greg Clayton5144f382010-10-07 17:14:24 +0000299 pvalue = PyObject_CallObject (pfunc, pargs);
300 Py_DECREF (pargs);
301
302 if (pvalue != NULL)
303 {
304 Py_DECREF (pvalue);
305 }
306 else if (PyErr_Occurred ())
Caroline Tice59c5d5d2010-09-27 18:00:20 +0000307 {
308 PyErr_Clear();
309 }
Caroline Tice0aa2e552011-01-14 00:29:16 +0000310 Py_INCREF (session_dict);
Caroline Tice59c5d5d2010-09-27 18:00:20 +0000311 }
312 else if (PyErr_Occurred())
313 {
314 PyErr_Clear();
315 }
316 }
Greg Clayton5144f382010-10-07 17:14:24 +0000317 else if (PyErr_Occurred())
Caroline Tice59c5d5d2010-09-27 18:00:20 +0000318 {
Greg Clayton5144f382010-10-07 17:14:24 +0000319 PyErr_Clear();
Caroline Tice59c5d5d2010-09-27 18:00:20 +0000320 }
321 }
Greg Clayton5144f382010-10-07 17:14:24 +0000322 else if (PyErr_Occurred ())
323 {
324 PyErr_Clear ();
325 }
326 return stop_at_breakpoint;
Caroline Tice59c5d5d2010-09-27 18:00:20 +0000327}
328
329%}