lldb.swig revision ebd63b28c6b7574ed9fbd71e57677c25e57c6a5b
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.  SBTarget conatins SBModule(s).
24o SBBreakpoint: Represents a logical breakpoint and its associated settings.
25      SBTarget conatins SBBreakpoint(s).
26o SBSymbol: Represents the symbol possibly associated with a stack frame.
27o SBCompileUnit: Represents a compilation unit, or compiled source file.
28o SBFunction: Represents a generic function, which can be inlined or not.
29o SBBlock: Represents a lexical block. SBFunction contains SBBlock(s).
30o SBLineEntry: Specifies an association with a contiguous range of instructions
31      and a source file location. SBCompileUnit contains SBLineEntry(s)."
32%enddef
33
34/*  The name of the module to be created.  */
35%module(docstring=DOCSTRING) lldb
36
37
38/* Typemap definitions, to allow SWIG to properly handle 'char**' data types. */
39
40%typemap(in) char ** {
41  /* Check if is a list  */
42  if (PyList_Check($input)) {
43    int size = PyList_Size($input);
44    int i = 0;
45    $1 = (char **) malloc((size+1) * sizeof(char*));
46    for (i = 0; i < size; i++) {
47      PyObject *o = PyList_GetItem($input,i);
48      if (PyString_Check(o))
49        $1[i] = PyString_AsString(o);
50      else {
51        PyErr_SetString(PyExc_TypeError,"list must contain strings");
52        free($1);
53        return NULL;
54      }
55    }
56    $1[i] = 0;
57  } else if ($input == Py_None) {
58    $1 =  NULL;
59  } else {
60    PyErr_SetString(PyExc_TypeError,"not a list");
61    return NULL;
62  }
63}
64
65%typemap(freearg) char** {
66  free((char *) $1);
67}
68
69%typemap(out) char** {
70  int len;
71  int i;
72  len = 0;
73  while ($1[len]) len++;
74  $result = PyList_New(len);
75  for (i = 0; i < len; i++) {
76    PyList_SetItem($result, i, PyString_FromString($1[i]));
77  }
78}
79
80/* Typemap definitions to allow SWIG to properly handle char buffer. */
81
82// typemap for a char buffer
83// See also SBThread::GetStopDescription.
84%typemap(in) (char *dst, size_t dst_len) {
85   if (!PyInt_Check($input)) {
86       PyErr_SetString(PyExc_ValueError, "Expecting an integer");
87       return NULL;
88   }
89   $2 = PyInt_AsLong($input);
90   if ($2 <= 0) {
91       PyErr_SetString(PyExc_ValueError, "Positive integer expected");
92       return NULL;
93   }
94   $1 = (char *) malloc($2);
95}
96
97// Return the char buffer.  Discarding any previous return result
98// See also SBThread::GetStopDescription.
99%typemap(argout) (char *dst, size_t dst_len) {
100   Py_XDECREF($result);   /* Blow away any previous result */
101   $result = PyString_FromStringAndSize(($1),result);
102   free($1);
103}
104
105
106// typemap for an outgoing buffer
107// See also SBEvent::SBEvent(uint32_t event, const char *cstr, uint32_t cstr_len).
108%typemap(in) (const char *cstr, uint32_t cstr_len) {
109   if (!PyString_Check($input)) {
110       PyErr_SetString(PyExc_ValueError, "Expecting a string");
111       return NULL;
112   }
113   $1 = (char *) PyString_AsString($input);
114   $2 = PyString_Size($input);
115}
116// And SBProcess::WriteMemory.
117%typemap(in) (const void *buf, size_t size) {
118   if (!PyString_Check($input)) {
119       PyErr_SetString(PyExc_ValueError, "Expecting a string");
120       return NULL;
121   }
122   $1 = (void *) PyString_AsString($input);
123   $2 = PyString_Size($input);
124}
125
126// typemap for an incoming buffer
127// See also SBProcess::ReadMemory.
128%typemap(in) (void *buf, size_t size) {
129   if (!PyInt_Check($input)) {
130       PyErr_SetString(PyExc_ValueError, "Expecting an integer");
131       return NULL;
132   }
133   $2 = PyInt_AsLong($input);
134   if ($2 <= 0) {
135       PyErr_SetString(PyExc_ValueError, "Positive integer expected");
136       return NULL;
137   }
138   $1 = (void *) malloc($2);
139}
140
141// Return the buffer.  Discarding any previous return result
142// See also SBProcess::ReadMemory.
143%typemap(argout) (void *buf, size_t size) {
144   Py_XDECREF($result);   /* Blow away any previous result */
145   $result = PyString_FromStringAndSize(static_cast<const char*>($1),result);
146   free($1);
147}
148
149/* The liblldb header files to be included. */
150
151%{
152#include "lldb/lldb-public.h"
153#include "lldb/API/SBAddress.h"
154#include "lldb/API/SBBlock.h"
155#include "lldb/API/SBBreakpoint.h"
156#include "lldb/API/SBBreakpointLocation.h"
157#include "lldb/API/SBBroadcaster.h"
158#include "lldb/API/SBCommandInterpreter.h"
159#include "lldb/API/SBCommandReturnObject.h"
160#include "lldb/API/SBCommunication.h"
161#include "lldb/API/SBCompileUnit.h"
162#include "lldb/API/SBDebugger.h"
163#include "lldb/API/SBError.h"
164#include "lldb/API/SBEvent.h"
165#include "lldb/API/SBFileSpec.h"
166#include "lldb/API/SBFrame.h"
167#include "lldb/API/SBFunction.h"
168#include "lldb/API/SBHostOS.h"
169#include "lldb/API/SBInputReader.h"
170#include "lldb/API/SBInstruction.h"
171#include "lldb/API/SBInstructionList.h"
172#include "lldb/API/SBLineEntry.h"
173#include "lldb/API/SBListener.h"
174#include "lldb/API/SBModule.h"
175#include "lldb/API/SBProcess.h"
176#include "lldb/API/SBSourceManager.h"
177#include "lldb/API/SBStream.h"
178#include "lldb/API/SBStringList.h"
179#include "lldb/API/SBSymbol.h"
180#include "lldb/API/SBSymbolContext.h"
181#include "lldb/API/SBSymbolContextList.h"
182#include "lldb/API/SBTarget.h"
183#include "lldb/API/SBThread.h"
184#include "lldb/API/SBType.h"
185#include "lldb/API/SBValue.h"
186#include "lldb/API/SBValueList.h"
187%}
188
189/* Various liblldb typedefs that SWIG needs to know about.  */
190#define __extension__ /* Undefine GCC keyword to make Swig happy when processing glibc's stdint.h. */
191%include <stdint.h>
192%include "lldb/lldb-defines.h"
193%include "lldb/lldb-enumerations.h"
194%include "lldb/lldb-forward.h"
195%include "lldb/lldb-forward-rtti.h"
196%include "lldb/lldb-types.h"
197%include "lldb/API/SBAddress.h"
198%include "lldb/API/SBBlock.h"
199%include "lldb/API/SBBreakpoint.h"
200%include "lldb/API/SBBreakpointLocation.h"
201%include "lldb/API/SBBroadcaster.h"
202%include "lldb/API/SBCommandInterpreter.h"
203%include "lldb/API/SBCommandReturnObject.h"
204%include "lldb/API/SBCommunication.h"
205%include "lldb/API/SBCompileUnit.h"
206%include "lldb/API/SBDebugger.h"
207%include "lldb/API/SBError.h"
208%include "lldb/API/SBEvent.h"
209%include "lldb/API/SBFileSpec.h"
210%include "lldb/API/SBFrame.h"
211%include "lldb/API/SBFunction.h"
212%include "lldb/API/SBHostOS.h"
213%include "lldb/API/SBInputReader.h"
214%include "lldb/API/SBInstruction.h"
215%include "lldb/API/SBInstructionList.h"
216%include "lldb/API/SBLineEntry.h"
217%include "lldb/API/SBListener.h"
218%include "lldb/API/SBModule.h"
219%include "lldb/API/SBProcess.h"
220%include "lldb/API/SBSourceManager.h"
221%include "lldb/API/SBStream.h"
222%include "lldb/API/SBStringList.h"
223%include "lldb/API/SBSymbol.h"
224%include "lldb/API/SBSymbolContext.h"
225%include "lldb/API/SBSymbolContextList.h"
226%include "./Python/interface/SBTarget.i"
227%include "lldb/API/SBThread.h"
228%include "lldb/API/SBType.h"
229%include "lldb/API/SBValue.h"
230%include "lldb/API/SBValueList.h"
231
232%include "./Python/python-extensions.swig"
233
234
235%wrapper %{
236
237// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...)
238// and is used when a script command is attached to a breakpoint for execution.
239
240SWIGEXPORT bool
241LLDBSwigPythonBreakpointCallbackFunction
242(
243    const char *python_function_name,
244    const char *session_dictionary_name,
245    const lldb::StackFrameSP& frame_sp,
246    const lldb::BreakpointLocationSP& bp_loc_sp
247)
248{
249    lldb::SBFrame sb_frame (frame_sp);
250    lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
251
252    bool stop_at_breakpoint = true;
253    PyObject *Frame_PyObj = SWIG_NewPointerObj((void *) &sb_frame, SWIGTYPE_p_lldb__SBFrame, 0);
254    PyObject *Bp_Loc_PyObj = SWIG_NewPointerObj ((void *) &sb_bp_loc, SWIGTYPE_p_lldb__SBBreakpointLocation, 0);
255
256    if (Frame_PyObj == NULL || Bp_Loc_PyObj == NULL)
257        return stop_at_breakpoint;
258
259    if (!python_function_name || !session_dictionary_name)
260        return stop_at_breakpoint;
261
262    PyObject *pmodule, *main_dict, *session_dict, *pfunc;
263    PyObject *pargs, *pvalue;
264
265    pmodule = PyImport_AddModule ("__main__");
266    if (pmodule != NULL)
267    {
268        main_dict = PyModule_GetDict (pmodule);
269        if (main_dict != NULL)
270        {
271            PyObject *key, *value;
272            Py_ssize_t pos = 0;
273
274            // Find the current session's dictionary in the main module's dictionary.
275
276            if (PyDict_Check (main_dict))
277
278            {
279                session_dict = NULL;
280                while (PyDict_Next (main_dict, &pos, &key, &value))
281                {
282                    // We have stolen references to the key and value objects in the dictionary; we need to increment
283                    // them now so that Python's garbage collector doesn't collect them out from under us.
284                    Py_INCREF (key);
285                    Py_INCREF (value);
286                    if (strcmp (PyString_AsString (key), session_dictionary_name) == 0)
287                    {
288                        session_dict = value;
289                        break;
290                    }
291                }
292            }
293
294            if (!session_dict || !PyDict_Check (session_dict))
295                return stop_at_breakpoint;
296
297            // Find the function we need to call in the current session's dictionary.
298
299            pos = 0;
300            pfunc = NULL;
301            while (PyDict_Next (session_dict, &pos, &key, &value))
302            {
303                if (PyString_Check (key))
304                {
305                    // We have stolen references to the key and value objects in the dictionary; we need to increment
306                    // them now so that Python's garbage collector doesn't collect them out from under us.
307                    Py_INCREF (key);
308                    Py_INCREF (value);
309                    if (strcmp (PyString_AsString (key), python_function_name) == 0)
310                    {
311                        pfunc = value;
312                        break;
313                    }
314                }
315            }
316
317            // Set up the arguments and call the function.
318
319            if (pfunc && PyCallable_Check (pfunc))
320            {
321                pargs = PyTuple_New (3);
322                if (pargs == NULL)
323                {
324                    if (PyErr_Occurred())
325                        PyErr_Clear();
326                    return stop_at_breakpoint;
327                }
328
329                PyTuple_SetItem (pargs, 0, Frame_PyObj);  // This "steals" a reference to Frame_PyObj
330                PyTuple_SetItem (pargs, 1, Bp_Loc_PyObj); // This "steals" a reference to Bp_Loc_PyObj
331                PyTuple_SetItem (pargs, 2, session_dict); // This "steals" a reference to session_dict
332                pvalue = PyObject_CallObject (pfunc, pargs);
333                Py_DECREF (pargs);
334
335                if (pvalue != NULL)
336                {
337                    Py_DECREF (pvalue);
338                }
339                else if (PyErr_Occurred ())
340                {
341                    PyErr_Clear();
342                }
343                Py_INCREF (session_dict);
344            }
345            else if (PyErr_Occurred())
346            {
347                PyErr_Clear();
348            }
349        }
350        else if (PyErr_Occurred())
351        {
352            PyErr_Clear();
353        }
354    }
355    else if (PyErr_Occurred ())
356    {
357        PyErr_Clear ();
358    }
359    return stop_at_breakpoint;
360}
361
362SWIGEXPORT std::string
363LLDBSwigPythonCallTypeScript
364(
365    const char *python_function_name,
366    const char *session_dictionary_name,
367    const lldb::ValueObjectSP& valobj_sp
368)
369{
370    lldb::SBValue sb_value (valobj_sp);
371
372    std::string retval = "";
373
374    PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *) &valobj_sp, SWIGTYPE_p_lldb__SBValue, 0);
375
376    if (ValObj_PyObj == NULL)
377        return retval;
378
379    if (!python_function_name || !session_dictionary_name)
380        return retval;
381
382    PyObject *pmodule, *main_dict, *session_dict, *pfunc;
383    PyObject *pargs, *pvalue;
384
385    pmodule = PyImport_AddModule ("__main__");
386    if (pmodule != NULL)
387    {
388        main_dict = PyModule_GetDict (pmodule);
389        if (main_dict != NULL)
390        {
391            PyObject *key, *value;
392            Py_ssize_t pos = 0;
393
394            // Find the current session's dictionary in the main module's dictionary.
395
396            if (PyDict_Check (main_dict))
397
398            {
399                session_dict = NULL;
400                while (PyDict_Next (main_dict, &pos, &key, &value))
401                {
402                    // We have stolen references to the key and value objects in the dictionary; we need to increment
403                    // them now so that Python's garbage collector doesn't collect them out from under us.
404                    Py_INCREF (key);
405                    Py_INCREF (value);
406                    if (strcmp (PyString_AsString (key), session_dictionary_name) == 0)
407                    {
408                        session_dict = value;
409                        break;
410                    }
411                }
412            }
413
414            if (!session_dict || !PyDict_Check (session_dict))
415                return retval;
416
417            // Find the function we need to call in the current session's dictionary.
418
419            pos = 0;
420            pfunc = NULL;
421            while (PyDict_Next (session_dict, &pos, &key, &value))
422            {
423                if (PyString_Check (key))
424                {
425                    // We have stolen references to the key and value objects in the dictionary; we need to increment
426                    // them now so that Python's garbage collector doesn't collect them out from under us.
427                    Py_INCREF (key);
428                    Py_INCREF (value);
429                    if (strcmp (PyString_AsString (key), python_function_name) == 0)
430                    {
431                        pfunc = value;
432                        break;
433                    }
434                }
435            }
436
437            // Set up the arguments and call the function.
438
439            if (pfunc && PyCallable_Check (pfunc))
440            {
441                pargs = PyTuple_New (2);
442                if (pargs == NULL)
443                {
444                    if (PyErr_Occurred())
445                        PyErr_Clear();
446                    return retval;
447                }
448
449                PyTuple_SetItem (pargs, 0, ValObj_PyObj);  // This "steals" a reference to ValObj_PyObj
450                PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict
451                pvalue = PyObject_CallObject (pfunc, pargs);
452                Py_DECREF (pargs);
453
454                if (pvalue != NULL)
455                {
456                    if (pvalue != Py_None)
457                        retval = std::string(PyString_AsString(pvalue));
458                    else
459                        retval = "None";
460                    Py_DECREF (pvalue);
461                }
462                else if (PyErr_Occurred ())
463                {
464                    PyErr_Clear();
465                }
466                Py_INCREF (session_dict);
467            }
468            else if (PyErr_Occurred())
469            {
470                PyErr_Clear();
471            }
472        }
473        else if (PyErr_Occurred())
474        {
475            PyErr_Clear();
476        }
477    }
478    else if (PyErr_Occurred ())
479    {
480        PyErr_Clear ();
481    }
482    return retval;
483}
484
485
486%}
487