lldb.swig revision 33b4be1d7ca14b25d0d57b5a2032971294980996
1/*
2   lldb.swig
3
4   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/* Define our module docstring. */
10%define DOCSTRING
11"The lldb module contains the public APIs for Python binding.
12
13Some 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.
24o SBSymbol: Represents the symbol associated with a stack frame.
25o 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
29      and a source file location. SBCompileUnit contains SBLineEntry(s)."
30%enddef
31
32/*  The name of the module to be created.  */
33%module(docstring=DOCSTRING) lldb
34
35
36/* 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;
43    $1 = (char **) malloc((size+1) * sizeof(char*));
44    for (i = 0; i < size; i++) {
45      PyObject *o = PyList_GetItem($input,i);
46      if (PyString_Check(o))
47        $1[i] = PyString_AsString(o);
48      else {
49        PyErr_SetString(PyExc_TypeError,"list must contain strings");
50        free($1);
51        return NULL;
52      }
53    }
54    $1[i] = 0;
55  } else if ($input == Py_None) {
56    $1 =  NULL;
57  } 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
78/* 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);
88   if ($2 <= 0) {
89       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}
102
103
104// typemap for an outgoing buffer
105// 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.
115%typemap(in) (const void *buf, size_t size) {
116   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
125// See also SBProcess::ReadMemory.
126%typemap(in) (void *buf, size_t size) {
127   if (!PyInt_Check($input)) {
128       PyErr_SetString(PyExc_ValueError, "Expecting an integer");
129       return NULL;
130   }
131   $2 = PyInt_AsLong($input);
132   if ($2 <= 0) {
133       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
140// See also SBProcess::ReadMemory.
141%typemap(argout) (void *buf, size_t size) {
142   Py_XDECREF($result);   /* Blow away any previous result */
143   $result = PyString_FromStringAndSize(static_cast<const char*>($1),result);
144   free($1);
145}
146
147/* The liblldb header files to be included. */
148
149%{
150#include "lldb/lldb-public.h"
151#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"
156#include "lldb/API/SBCommandInterpreter.h"
157#include "lldb/API/SBCommandReturnObject.h"
158#include "lldb/API/SBCommunication.h"
159#include "lldb/API/SBCompileUnit.h"
160#include "lldb/API/SBDebugger.h"
161#include "lldb/API/SBError.h"
162#include "lldb/API/SBEvent.h"
163#include "lldb/API/SBFileSpec.h"
164#include "lldb/API/SBFrame.h"
165#include "lldb/API/SBFunction.h"
166#include "lldb/API/SBHostOS.h"
167#include "lldb/API/SBInputReader.h"
168#include "lldb/API/SBInstruction.h"
169#include "lldb/API/SBInstructionList.h"
170#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"
175#include "lldb/API/SBStream.h"
176#include "lldb/API/SBStringList.h"
177#include "lldb/API/SBSymbol.h"
178#include "lldb/API/SBSymbolContext.h"
179#include "lldb/API/SBSymbolContextList.h"
180#include "lldb/API/SBTarget.h"
181#include "lldb/API/SBThread.h"
182#include "lldb/API/SBType.h"
183#include "lldb/API/SBValue.h"
184#include "lldb/API/SBValueList.h"
185%}
186
187/* Various liblldb typedefs that SWIG needs to know about.  */
188#define __extension__ /* Undefine GCC keyword to make Swig happy when processing glibc's stdint.h. */
189%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"
195%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"
200%include "lldb/API/SBCommandInterpreter.h"
201%include "lldb/API/SBCommandReturnObject.h"
202%include "lldb/API/SBCommunication.h"
203%include "lldb/API/SBCompileUnit.h"
204%include "lldb/API/SBDebugger.h"
205%include "lldb/API/SBError.h"
206%include "lldb/API/SBEvent.h"
207%include "lldb/API/SBFileSpec.h"
208%include "lldb/API/SBFrame.h"
209%include "lldb/API/SBFunction.h"
210%include "lldb/API/SBHostOS.h"
211%include "lldb/API/SBInputReader.h"
212%include "lldb/API/SBInstruction.h"
213%include "lldb/API/SBInstructionList.h"
214%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"
219%include "lldb/API/SBStream.h"
220%include "lldb/API/SBStringList.h"
221%include "lldb/API/SBSymbol.h"
222%include "lldb/API/SBSymbolContext.h"
223%include "lldb/API/SBSymbolContextList.h"
224%include "lldb/API/SBTarget.h"
225%include "lldb/API/SBThread.h"
226%include "lldb/API/SBType.h"
227%include "lldb/API/SBValue.h"
228%include "lldb/API/SBValueList.h"
229
230%include "./Python/python-extensions.swig"
231
232
233%wrapper %{
234
235// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...)
236// and is used when a script command is attached to a breakpoint for execution.
237
238SWIGEXPORT bool
239LLDBSwigPythonBreakpointCallbackFunction
240(
241    const char *python_function_name,
242    const char *session_dictionary_name,
243    const lldb::StackFrameSP& frame_sp,
244    const lldb::BreakpointLocationSP& bp_loc_sp
245)
246{
247    lldb::SBFrame sb_frame (frame_sp);
248    lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
249
250    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);
253
254    if (Frame_PyObj == NULL || Bp_Loc_PyObj == NULL)
255        return stop_at_breakpoint;
256
257    if (!python_function_name || !session_dictionary_name)
258        return stop_at_breakpoint;
259
260    PyObject *pmodule, *main_dict, *session_dict, *pfunc;
261    PyObject *pargs, *pvalue;
262
263    pmodule = PyImport_AddModule ("__main__");
264    if (pmodule != NULL)
265    {
266        main_dict = PyModule_GetDict (pmodule);
267        if (main_dict != NULL)
268        {
269            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
317            if (pfunc && PyCallable_Check (pfunc))
318            {
319                pargs = PyTuple_New (3);
320                if (pargs == NULL)
321                {
322                    if (PyErr_Occurred())
323                        PyErr_Clear();
324                    return stop_at_breakpoint;
325                }
326
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
329                PyTuple_SetItem (pargs, 2, session_dict); // This "steals" a reference to session_dict
330                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 ())
338                {
339                    PyErr_Clear();
340                }
341                Py_INCREF (session_dict);
342            }
343            else if (PyErr_Occurred())
344            {
345                PyErr_Clear();
346            }
347        }
348        else if (PyErr_Occurred())
349        {
350            PyErr_Clear();
351        }
352    }
353    else if (PyErr_Occurred ())
354    {
355        PyErr_Clear ();
356    }
357    return stop_at_breakpoint;
358}
359
360%}
361