Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | lldb.swig |
| 3 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Johnny Chen | ee48178 | 2011-06-30 21:29:50 +0000 | [diff] [blame] | 9 | /* Define our module docstring. */ |
| 10 | %define DOCSTRING |
| 11 | "The lldb module contains the public APIs for Python binding. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | |
Johnny Chen | ee48178 | 2011-06-30 21:29:50 +0000 | [diff] [blame] | 13 | Some of the important classes are describe here: |
| 14 | |
| 15 | o SBTarget: Represents the target program running under the debugger. |
| 16 | o SBProcess: Represents the process associated with the target program. |
| 17 | o SBThread: Represents a thread of execution. SBProcess contains SBThread(s). |
| 18 | o SBFrame: Represents one of the stack frames associated with a thread. SBThread |
| 19 | contains SBFrame(s). |
| 20 | o SBSymbolContext: A container that stores various debugger related info. |
| 21 | o SBValue: Represents the value of a variable, a register, or an expression. |
| 22 | o SBModule: Represents an executable image and its associated object and symbol |
Johnny Chen | 4036b58 | 2011-07-14 21:32:11 +0000 | [diff] [blame] | 23 | files. SBTarget conatins SBModule(s). |
| 24 | o SBBreakpoint: Represents a logical breakpoint and its associated settings. |
| 25 | SBTarget conatins SBBreakpoint(s). |
Johnny Chen | 993f2b6 | 2011-07-14 21:23:24 +0000 | [diff] [blame] | 26 | o SBSymbol: Represents the symbol possibly associated with a stack frame. |
Johnny Chen | ee48178 | 2011-06-30 21:29:50 +0000 | [diff] [blame] | 27 | o SBCompileUnit: Represents a compilation unit, or compiled source file. |
| 28 | o SBFunction: Represents a generic function, which can be inlined or not. |
| 29 | o SBBlock: Represents a lexical block. SBFunction contains SBBlock(s). |
| 30 | o SBLineEntry: Specifies an association with a contiguous range of instructions |
Johnny Chen | 61abb2a | 2011-07-01 18:39:47 +0000 | [diff] [blame] | 31 | and a source file location. SBCompileUnit contains SBLineEntry(s)." |
Johnny Chen | ee48178 | 2011-06-30 21:29:50 +0000 | [diff] [blame] | 32 | %enddef |
| 33 | |
| 34 | /* The name of the module to be created. */ |
| 35 | %module(docstring=DOCSTRING) lldb |
| 36 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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; |
Greg Clayton | ca512b3 | 2011-01-14 04:54:56 +0000 | [diff] [blame] | 45 | $1 = (char **) malloc((size+1) * sizeof(char*)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | for (i = 0; i < size; i++) { |
| 47 | PyObject *o = PyList_GetItem($input,i); |
| 48 | if (PyString_Check(o)) |
Greg Clayton | ca512b3 | 2011-01-14 04:54:56 +0000 | [diff] [blame] | 49 | $1[i] = PyString_AsString(o); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 50 | else { |
| 51 | PyErr_SetString(PyExc_TypeError,"list must contain strings"); |
| 52 | free($1); |
| 53 | return NULL; |
| 54 | } |
| 55 | } |
| 56 | $1[i] = 0; |
Caroline Tice | ebc1bb2 | 2010-06-30 16:22:25 +0000 | [diff] [blame] | 57 | } else if ($input == Py_None) { |
| 58 | $1 = NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
Johnny Chen | 2f6f7ba | 2011-03-07 21:28:57 +0000 | [diff] [blame] | 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); |
Johnny Chen | de8241c | 2011-03-23 00:26:08 +0000 | [diff] [blame] | 90 | if ($2 <= 0) { |
Johnny Chen | 2f6f7ba | 2011-03-07 21:28:57 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | |
Greg Clayton | ca512b3 | 2011-01-14 04:54:56 +0000 | [diff] [blame] | 105 | |
| 106 | // typemap for an outgoing buffer |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 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. |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 117 | %typemap(in) (const void *buf, size_t size) { |
Greg Clayton | ca512b3 | 2011-01-14 04:54:56 +0000 | [diff] [blame] | 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 |
Johnny Chen | 2f6f7ba | 2011-03-07 21:28:57 +0000 | [diff] [blame] | 127 | // See also SBProcess::ReadMemory. |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 128 | %typemap(in) (void *buf, size_t size) { |
Greg Clayton | ca512b3 | 2011-01-14 04:54:56 +0000 | [diff] [blame] | 129 | if (!PyInt_Check($input)) { |
| 130 | PyErr_SetString(PyExc_ValueError, "Expecting an integer"); |
| 131 | return NULL; |
| 132 | } |
| 133 | $2 = PyInt_AsLong($input); |
Johnny Chen | de8241c | 2011-03-23 00:26:08 +0000 | [diff] [blame] | 134 | if ($2 <= 0) { |
Greg Clayton | ca512b3 | 2011-01-14 04:54:56 +0000 | [diff] [blame] | 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 |
Johnny Chen | 2f6f7ba | 2011-03-07 21:28:57 +0000 | [diff] [blame] | 142 | // See also SBProcess::ReadMemory. |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 143 | %typemap(argout) (void *buf, size_t size) { |
Greg Clayton | ca512b3 | 2011-01-14 04:54:56 +0000 | [diff] [blame] | 144 | Py_XDECREF($result); /* Blow away any previous result */ |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 145 | $result = PyString_FromStringAndSize(static_cast<const char*>($1),result); |
Greg Clayton | ca512b3 | 2011-01-14 04:54:56 +0000 | [diff] [blame] | 146 | free($1); |
| 147 | } |
| 148 | |
Greg Clayton | c0cc73e | 2010-06-12 15:34:20 +0000 | [diff] [blame] | 149 | /* The liblldb header files to be included. */ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 150 | |
| 151 | %{ |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 152 | #include "lldb/lldb-public.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 158 | #include "lldb/API/SBCommandInterpreter.h" |
| 159 | #include "lldb/API/SBCommandReturnObject.h" |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 160 | #include "lldb/API/SBCommunication.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 161 | #include "lldb/API/SBCompileUnit.h" |
| 162 | #include "lldb/API/SBDebugger.h" |
| 163 | #include "lldb/API/SBError.h" |
| 164 | #include "lldb/API/SBEvent.h" |
Johnny Chen | 23fd10c | 2010-08-27 22:35:26 +0000 | [diff] [blame] | 165 | #include "lldb/API/SBFileSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 166 | #include "lldb/API/SBFrame.h" |
| 167 | #include "lldb/API/SBFunction.h" |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 168 | #include "lldb/API/SBHostOS.h" |
| 169 | #include "lldb/API/SBInputReader.h" |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 170 | #include "lldb/API/SBInstruction.h" |
| 171 | #include "lldb/API/SBInstructionList.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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" |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 177 | #include "lldb/API/SBStream.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 178 | #include "lldb/API/SBStringList.h" |
| 179 | #include "lldb/API/SBSymbol.h" |
| 180 | #include "lldb/API/SBSymbolContext.h" |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 181 | #include "lldb/API/SBSymbolContextList.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 182 | #include "lldb/API/SBTarget.h" |
| 183 | #include "lldb/API/SBThread.h" |
| 184 | #include "lldb/API/SBType.h" |
| 185 | #include "lldb/API/SBValue.h" |
Caroline Tice | 7740412 | 2010-09-22 16:41:52 +0000 | [diff] [blame] | 186 | #include "lldb/API/SBValueList.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 187 | %} |
| 188 | |
| 189 | /* Various liblldb typedefs that SWIG needs to know about. */ |
Johnny Chen | 4b33209 | 2010-12-16 00:01:06 +0000 | [diff] [blame] | 190 | #define __extension__ /* Undefine GCC keyword to make Swig happy when processing glibc's stdint.h. */ |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 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" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 202 | %include "lldb/API/SBCommandInterpreter.h" |
| 203 | %include "lldb/API/SBCommandReturnObject.h" |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 204 | %include "lldb/API/SBCommunication.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 205 | %include "lldb/API/SBCompileUnit.h" |
| 206 | %include "lldb/API/SBDebugger.h" |
| 207 | %include "lldb/API/SBError.h" |
| 208 | %include "lldb/API/SBEvent.h" |
Johnny Chen | 23fd10c | 2010-08-27 22:35:26 +0000 | [diff] [blame] | 209 | %include "lldb/API/SBFileSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 210 | %include "lldb/API/SBFrame.h" |
| 211 | %include "lldb/API/SBFunction.h" |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 212 | %include "lldb/API/SBHostOS.h" |
| 213 | %include "lldb/API/SBInputReader.h" |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 214 | %include "lldb/API/SBInstruction.h" |
| 215 | %include "lldb/API/SBInstructionList.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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" |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 221 | %include "lldb/API/SBStream.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 222 | %include "lldb/API/SBStringList.h" |
| 223 | %include "lldb/API/SBSymbol.h" |
| 224 | %include "lldb/API/SBSymbolContext.h" |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 225 | %include "lldb/API/SBSymbolContextList.h" |
Johnny Chen | dc7d3c1 | 2011-07-16 21:15:39 +0000 | [diff] [blame^] | 226 | %include "./Python/interface/SBTarget.i" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 227 | %include "lldb/API/SBThread.h" |
| 228 | %include "lldb/API/SBType.h" |
| 229 | %include "lldb/API/SBValue.h" |
Caroline Tice | 7740412 | 2010-09-22 16:41:52 +0000 | [diff] [blame] | 230 | %include "lldb/API/SBValueList.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 231 | |
Caroline Tice | dac97f3 | 2010-09-22 23:01:29 +0000 | [diff] [blame] | 232 | %include "./Python/python-extensions.swig" |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 233 | |
| 234 | |
| 235 | %wrapper %{ |
| 236 | |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 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. |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 239 | |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 240 | SWIGEXPORT bool |
Greg Clayton | fc36f791 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 241 | LLDBSwigPythonBreakpointCallbackFunction |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 242 | ( |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 243 | const char *python_function_name, |
Caroline Tice | 2f88aad | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 244 | const char *session_dictionary_name, |
Greg Clayton | fc36f791 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 245 | const lldb::StackFrameSP& frame_sp, |
| 246 | const lldb::BreakpointLocationSP& bp_loc_sp |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 247 | ) |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 248 | { |
Greg Clayton | fc36f791 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 249 | lldb::SBFrame sb_frame (frame_sp); |
| 250 | lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp); |
| 251 | |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 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); |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 255 | |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 256 | if (Frame_PyObj == NULL || Bp_Loc_PyObj == NULL) |
| 257 | return stop_at_breakpoint; |
Caroline Tice | 2f88aad | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 258 | |
| 259 | if (!python_function_name || !session_dictionary_name) |
| 260 | return stop_at_breakpoint; |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 261 | |
Caroline Tice | 2f88aad | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 262 | PyObject *pmodule, *main_dict, *session_dict, *pfunc; |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 263 | PyObject *pargs, *pvalue; |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 264 | |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 265 | pmodule = PyImport_AddModule ("__main__"); |
| 266 | if (pmodule != NULL) |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 267 | { |
Caroline Tice | 2f88aad | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 268 | main_dict = PyModule_GetDict (pmodule); |
| 269 | if (main_dict != NULL) |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 270 | { |
Caroline Tice | 2f88aad | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 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 | |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 319 | if (pfunc && PyCallable_Check (pfunc)) |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 320 | { |
Caroline Tice | 2f88aad | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 321 | pargs = PyTuple_New (3); |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 322 | if (pargs == NULL) |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 323 | { |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 324 | if (PyErr_Occurred()) |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 325 | PyErr_Clear(); |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 326 | return stop_at_breakpoint; |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 327 | } |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 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 |
Caroline Tice | 2f88aad | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 331 | PyTuple_SetItem (pargs, 2, session_dict); // This "steals" a reference to session_dict |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 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 ()) |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 340 | { |
| 341 | PyErr_Clear(); |
| 342 | } |
Caroline Tice | 2f88aad | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 343 | Py_INCREF (session_dict); |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 344 | } |
| 345 | else if (PyErr_Occurred()) |
| 346 | { |
| 347 | PyErr_Clear(); |
| 348 | } |
| 349 | } |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 350 | else if (PyErr_Occurred()) |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 351 | { |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 352 | PyErr_Clear(); |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
Greg Clayton | c6ed542 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 355 | else if (PyErr_Occurred ()) |
| 356 | { |
| 357 | PyErr_Clear (); |
| 358 | } |
| 359 | return stop_at_breakpoint; |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Enrico Granata | f2bbf71 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 362 | SWIGEXPORT std::string |
| 363 | LLDBSwigPythonCallTypeScript |
| 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 | |
Caroline Tice | 18474c9 | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 486 | %} |