Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 1 | %wrapper %{ |
| 2 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 3 | class PyErr_Cleaner |
| 4 | { |
| 5 | public: |
| 6 | PyErr_Cleaner(bool print=false) : |
| 7 | m_print(print) |
| 8 | { |
| 9 | } |
| 10 | |
| 11 | ~PyErr_Cleaner() |
| 12 | { |
| 13 | if (PyErr_Occurred()) |
| 14 | { |
| 15 | if(m_print) |
| 16 | PyErr_Print(); |
| 17 | PyErr_Clear(); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | private: |
| 22 | bool m_print; |
| 23 | }; |
| 24 | |
| 25 | // resolve a dotted Python name in the form |
| 26 | // foo.bar.baz.Foobar to an actual Python object |
| 27 | // if pmodule is NULL, the __main__ module will be used |
| 28 | // as the starting point for the search |
| 29 | |
| 30 | static PyObject* |
| 31 | ResolvePythonName(const char* name, |
| 32 | PyObject* pmodule = NULL) |
| 33 | { |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 34 | if (!name) |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 35 | return pmodule; |
| 36 | |
| 37 | PyErr_Cleaner pyerr_cleanup(true); // show Python errors |
| 38 | |
| 39 | PyObject* main_dict; |
| 40 | |
| 41 | if (!pmodule) |
| 42 | { |
| 43 | pmodule = PyImport_AddModule ("__main__"); |
| 44 | if (!pmodule) |
| 45 | return NULL; |
| 46 | } |
| 47 | |
| 48 | if (!PyDict_Check(pmodule)) |
| 49 | { |
| 50 | main_dict = PyModule_GetDict (pmodule); |
| 51 | if (!main_dict) |
| 52 | return NULL; |
| 53 | } |
| 54 | else |
| 55 | main_dict = pmodule; |
| 56 | |
| 57 | const char* dot_pos = ::strchr(name, '.'); |
| 58 | |
| 59 | PyObject *dest_object; |
| 60 | PyObject *key, *value; |
| 61 | Py_ssize_t pos = 0; |
| 62 | |
| 63 | if (!dot_pos) |
| 64 | { |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 65 | dest_object = NULL; |
| 66 | while (PyDict_Next (main_dict, &pos, &key, &value)) |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 67 | { |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 68 | // We have stolen references to the key and value objects in the dictionary; we need to increment |
| 69 | // them now so that Python's garbage collector doesn't collect them out from under us. |
| 70 | Py_INCREF (key); |
| 71 | Py_INCREF (value); |
| 72 | if (strcmp (PyString_AsString (key), name) == 0) |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 73 | { |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 74 | dest_object = value; |
| 75 | break; |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 76 | } |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 77 | } |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 78 | if (!dest_object || dest_object == Py_None) |
| 79 | return NULL; |
| 80 | return dest_object; |
| 81 | } |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 82 | else |
| 83 | { |
| 84 | size_t len = dot_pos - name; |
| 85 | std::string piece(name,len); |
| 86 | pmodule = ResolvePythonName(piece.c_str(), main_dict); |
| 87 | if (!pmodule) |
| 88 | return NULL; |
| 89 | name = dot_pos+1; |
| 90 | return ResolvePythonName(dot_pos+1,pmodule); // tail recursion.. should be optimized by the compiler |
| 91 | } |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static PyObject* |
| 95 | FindSessionDictionary(const char *session_dictionary_name) |
| 96 | { |
| 97 | return ResolvePythonName(session_dictionary_name, NULL); |
| 98 | } |
| 99 | |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 100 | // This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...) |
| 101 | // and is used when a script command is attached to a breakpoint for execution. |
| 102 | |
| 103 | SWIGEXPORT bool |
| 104 | LLDBSwigPythonBreakpointCallbackFunction |
| 105 | ( |
| 106 | const char *python_function_name, |
| 107 | const char *session_dictionary_name, |
| 108 | const lldb::StackFrameSP& frame_sp, |
| 109 | const lldb::BreakpointLocationSP& bp_loc_sp |
| 110 | ) |
| 111 | { |
| 112 | lldb::SBFrame sb_frame (frame_sp); |
| 113 | lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp); |
| 114 | |
| 115 | bool stop_at_breakpoint = true; |
| 116 | PyObject *Frame_PyObj = SWIG_NewPointerObj((void *) &sb_frame, SWIGTYPE_p_lldb__SBFrame, 0); |
| 117 | PyObject *Bp_Loc_PyObj = SWIG_NewPointerObj ((void *) &sb_bp_loc, SWIGTYPE_p_lldb__SBBreakpointLocation, 0); |
| 118 | |
| 119 | if (Frame_PyObj == NULL || Bp_Loc_PyObj == NULL) |
| 120 | return stop_at_breakpoint; |
| 121 | |
| 122 | if (!python_function_name || !session_dictionary_name) |
| 123 | return stop_at_breakpoint; |
| 124 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 125 | PyObject *session_dict, *pfunc; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 126 | PyObject *pargs, *pvalue; |
| 127 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 128 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 129 | if (session_dict != NULL) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 130 | { |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 131 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 132 | if (pfunc != NULL) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 133 | { |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 134 | // Set up the arguments and call the function. |
| 135 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 136 | if (PyCallable_Check (pfunc)) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 137 | { |
| 138 | pargs = PyTuple_New (3); |
| 139 | if (pargs == NULL) |
| 140 | { |
| 141 | if (PyErr_Occurred()) |
| 142 | PyErr_Clear(); |
| 143 | return stop_at_breakpoint; |
| 144 | } |
| 145 | |
| 146 | PyTuple_SetItem (pargs, 0, Frame_PyObj); // This "steals" a reference to Frame_PyObj |
| 147 | PyTuple_SetItem (pargs, 1, Bp_Loc_PyObj); // This "steals" a reference to Bp_Loc_PyObj |
| 148 | PyTuple_SetItem (pargs, 2, session_dict); // This "steals" a reference to session_dict |
| 149 | pvalue = PyObject_CallObject (pfunc, pargs); |
| 150 | Py_DECREF (pargs); |
| 151 | |
| 152 | if (pvalue != NULL) |
| 153 | { |
| 154 | Py_DECREF (pvalue); |
| 155 | } |
| 156 | else if (PyErr_Occurred ()) |
| 157 | { |
| 158 | PyErr_Clear(); |
| 159 | } |
| 160 | Py_INCREF (session_dict); |
| 161 | } |
| 162 | else if (PyErr_Occurred()) |
| 163 | { |
| 164 | PyErr_Clear(); |
| 165 | } |
| 166 | } |
| 167 | else if (PyErr_Occurred()) |
| 168 | { |
| 169 | PyErr_Clear(); |
| 170 | } |
| 171 | } |
| 172 | else if (PyErr_Occurred ()) |
| 173 | { |
| 174 | PyErr_Clear (); |
| 175 | } |
| 176 | return stop_at_breakpoint; |
| 177 | } |
| 178 | |
Johnny Chen | f3ec461 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 179 | // This function is called by lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...) |
| 180 | // and is used when a script command is attached to a watchpoint for execution. |
| 181 | |
| 182 | SWIGEXPORT bool |
| 183 | LLDBSwigPythonWatchpointCallbackFunction |
| 184 | ( |
| 185 | const char *python_function_name, |
| 186 | const char *session_dictionary_name, |
| 187 | const lldb::StackFrameSP& frame_sp, |
| 188 | const lldb::WatchpointSP& wp_sp |
| 189 | ) |
| 190 | { |
| 191 | lldb::SBFrame sb_frame (frame_sp); |
| 192 | lldb::SBWatchpoint sb_wp(wp_sp); |
| 193 | |
| 194 | bool stop_at_watchpoint = true; |
| 195 | PyObject *Frame_PyObj = SWIG_NewPointerObj((void *) &sb_frame, SWIGTYPE_p_lldb__SBFrame, 0); |
| 196 | PyObject *Wp_PyObj = SWIG_NewPointerObj ((void *) &sb_wp, SWIGTYPE_p_lldb__SBWatchpoint, 0); |
| 197 | |
| 198 | if (Frame_PyObj == NULL || Wp_PyObj == NULL) |
| 199 | return stop_at_watchpoint; |
| 200 | |
| 201 | if (!python_function_name || !session_dictionary_name) |
| 202 | return stop_at_watchpoint; |
| 203 | |
| 204 | PyObject *session_dict, *pfunc; |
| 205 | PyObject *pargs, *pvalue; |
| 206 | |
| 207 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 208 | if (session_dict != NULL) |
| 209 | { |
| 210 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 211 | if (pfunc != NULL) |
| 212 | { |
| 213 | // Set up the arguments and call the function. |
| 214 | |
| 215 | if (PyCallable_Check (pfunc)) |
| 216 | { |
| 217 | pargs = PyTuple_New (3); |
| 218 | if (pargs == NULL) |
| 219 | { |
| 220 | if (PyErr_Occurred()) |
| 221 | PyErr_Clear(); |
| 222 | return stop_at_watchpoint; |
| 223 | } |
| 224 | |
| 225 | PyTuple_SetItem (pargs, 0, Frame_PyObj); // This "steals" a reference to Frame_PyObj |
| 226 | PyTuple_SetItem (pargs, 1, Wp_PyObj); // This "steals" a reference to Wp_PyObj |
| 227 | PyTuple_SetItem (pargs, 2, session_dict); // This "steals" a reference to session_dict |
| 228 | pvalue = PyObject_CallObject (pfunc, pargs); |
| 229 | Py_DECREF (pargs); |
| 230 | |
| 231 | if (pvalue != NULL) |
| 232 | { |
| 233 | Py_DECREF (pvalue); |
| 234 | } |
| 235 | else if (PyErr_Occurred ()) |
| 236 | { |
| 237 | PyErr_Clear(); |
| 238 | } |
| 239 | Py_INCREF (session_dict); |
| 240 | } |
| 241 | else if (PyErr_Occurred()) |
| 242 | { |
| 243 | PyErr_Clear(); |
| 244 | } |
| 245 | } |
| 246 | else if (PyErr_Occurred()) |
| 247 | { |
| 248 | PyErr_Clear(); |
| 249 | } |
| 250 | } |
| 251 | else if (PyErr_Occurred ()) |
| 252 | { |
| 253 | PyErr_Clear (); |
| 254 | } |
| 255 | return stop_at_watchpoint; |
| 256 | } |
| 257 | |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 258 | SWIGEXPORT bool |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 259 | LLDBSwigPythonCallTypeScript |
| 260 | ( |
| 261 | const char *python_function_name, |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 262 | const void *session_dictionary, |
| 263 | const lldb::ValueObjectSP& valobj_sp, |
| 264 | void** pyfunct_wrapper, |
| 265 | std::string& retval |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 266 | ) |
| 267 | { |
| 268 | lldb::SBValue sb_value (valobj_sp); |
| 269 | |
Greg Clayton | ec1e823 | 2013-01-16 19:53:55 +0000 | [diff] [blame^] | 270 | retval.clear(); |
| 271 | |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 272 | PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *) &sb_value, SWIGTYPE_p_lldb__SBValue, 0); |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 273 | |
| 274 | if (ValObj_PyObj == NULL) |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 275 | return false; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 276 | |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 277 | if (!python_function_name || !session_dictionary) |
| 278 | return false; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 279 | |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 280 | PyObject *session_dict = (PyObject*)session_dictionary, *pfunc = NULL, *pargs = NULL, *pvalue = NULL; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 281 | |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 282 | if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper)) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 283 | { |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 284 | pfunc = (PyObject*)(*pyfunct_wrapper); |
| 285 | if (pfunc->ob_refcnt == 1) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 286 | { |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 287 | Py_XDECREF(pfunc); |
| 288 | pfunc = NULL; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 291 | |
| 292 | if (PyDict_Check(session_dict)) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 293 | { |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 294 | PyErr_Cleaner pyerr_cleanup(true); // show Python errors |
| 295 | |
| 296 | if (!pfunc) |
| 297 | { |
| 298 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 299 | if (!pfunc || !PyFunction_Check (pfunc)) |
| 300 | return false; |
| 301 | else |
| 302 | { |
| 303 | if (pyfunct_wrapper) |
| 304 | *pyfunct_wrapper = pfunc; |
| 305 | } |
| 306 | } |
| 307 | /*else |
| 308 | printf("caching works!!!!\n");*/ |
| 309 | |
| 310 | pargs = PyTuple_Pack(2, ValObj_PyObj, session_dict); |
| 311 | if (pargs == NULL) |
| 312 | return false; |
| 313 | |
| 314 | pvalue = PyObject_CallObject (pfunc, pargs); |
| 315 | Py_DECREF (pargs); |
| 316 | |
| 317 | if (pvalue != NULL && pvalue != Py_None && PyString_Check(pvalue)) |
| 318 | retval.assign(PyString_AsString(pvalue)); |
| 319 | Py_XDECREF (pvalue); |
| 320 | Py_INCREF (session_dict); |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 321 | } |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 322 | return true; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | SWIGEXPORT void* |
| 326 | LLDBSwigPythonCreateSyntheticProvider |
| 327 | ( |
Greg Clayton | ec1e823 | 2013-01-16 19:53:55 +0000 | [diff] [blame^] | 328 | const char *python_class_name, |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 329 | const char *session_dictionary_name, |
| 330 | const lldb::ValueObjectSP& valobj_sp |
| 331 | ) |
| 332 | { |
| 333 | PyObject* retval = NULL; |
| 334 | |
Greg Clayton | ec1e823 | 2013-01-16 19:53:55 +0000 | [diff] [blame^] | 335 | if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 336 | Py_RETURN_NONE; |
| 337 | |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 338 | // I do not want the SBValue to be deallocated when going out of scope because python |
| 339 | // has ownership of it and will manage memory for this object by itself |
| 340 | lldb::SBValue *valobj_sb = new lldb::SBValue(valobj_sp); |
Enrico Granata | 3d656c7 | 2012-10-22 18:18:36 +0000 | [diff] [blame] | 341 | valobj_sb->SetPreferSyntheticValue(false); |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 342 | |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 343 | PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *)valobj_sb, SWIGTYPE_p_lldb__SBValue, 0); |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 344 | |
| 345 | if (ValObj_PyObj == NULL) |
| 346 | Py_RETURN_NONE; |
| 347 | |
Greg Clayton | ec1e823 | 2013-01-16 19:53:55 +0000 | [diff] [blame^] | 348 | const char* python_function_name = python_class_name; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 349 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 350 | PyObject *session_dict, *pfunc; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 351 | PyObject *pvalue; |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 352 | |
| 353 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 354 | if (session_dict != NULL) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 355 | { |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 356 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 357 | if (pfunc != NULL) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 358 | { |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 359 | // Set up the arguments and call the function. |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 360 | |
| 361 | if (PyCallable_Check (pfunc)) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 362 | { |
| 363 | PyObject *argList = Py_BuildValue("SS", ValObj_PyObj, session_dict); |
| 364 | |
| 365 | if (PyErr_Occurred ()) |
| 366 | { |
| 367 | PyErr_Print(); |
| 368 | PyErr_Clear(); |
| 369 | return retval; |
| 370 | } |
| 371 | |
| 372 | if (argList == NULL) |
| 373 | { |
| 374 | return retval; |
| 375 | } |
| 376 | |
| 377 | Py_INCREF(ValObj_PyObj); |
| 378 | |
| 379 | pvalue = PyObject_CallObject(pfunc, argList); |
| 380 | |
| 381 | Py_DECREF(argList); |
| 382 | |
| 383 | if (pvalue != NULL) |
| 384 | { |
| 385 | if (pvalue != Py_None) |
| 386 | retval = pvalue; |
| 387 | else |
| 388 | { |
| 389 | retval = Py_None; |
| 390 | Py_INCREF(retval); |
| 391 | } |
| 392 | } |
| 393 | else if (PyErr_Occurred ()) |
| 394 | { |
| 395 | PyErr_Print(); |
| 396 | PyErr_Clear(); |
| 397 | } |
| 398 | Py_INCREF (session_dict); |
| 399 | } |
| 400 | else if (PyErr_Occurred()) |
| 401 | { |
| 402 | PyErr_Print(); |
| 403 | PyErr_Clear(); |
| 404 | } |
| 405 | } |
| 406 | else if (PyErr_Occurred()) |
| 407 | { |
| 408 | PyErr_Print(); |
| 409 | PyErr_Clear(); |
| 410 | } |
| 411 | } |
| 412 | else if (PyErr_Occurred ()) |
| 413 | { |
| 414 | PyErr_Print(); |
| 415 | PyErr_Clear (); |
| 416 | } |
| 417 | if (retval) |
| 418 | return retval; |
| 419 | else |
| 420 | Py_RETURN_NONE; |
| 421 | } |
| 422 | |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 423 | // wrapper that calls an optional instance member of an object taking no arguments |
| 424 | static PyObject* |
| 425 | LLDBSwigPython_CallOptionalMember |
| 426 | ( |
| 427 | PyObject* self, |
| 428 | char* callee_name, |
| 429 | PyObject* ret_if_not_found = Py_None, |
| 430 | bool* was_found = NULL |
| 431 | ) |
| 432 | { |
| 433 | if (self == NULL || self == Py_None) |
| 434 | { |
| 435 | if (was_found) |
| 436 | *was_found = false; |
| 437 | Py_XINCREF(ret_if_not_found); |
| 438 | return ret_if_not_found; |
| 439 | } |
| 440 | |
| 441 | PyObject* pmeth = PyObject_GetAttrString(self, callee_name); |
| 442 | |
| 443 | if (PyErr_Occurred()) |
| 444 | { |
| 445 | PyErr_Clear(); |
| 446 | } |
| 447 | |
| 448 | if (pmeth == NULL || pmeth == Py_None) |
| 449 | { |
| 450 | if (was_found) |
| 451 | *was_found = false; |
| 452 | Py_XDECREF(pmeth); |
| 453 | Py_XINCREF(ret_if_not_found); |
| 454 | return ret_if_not_found; |
| 455 | } |
| 456 | |
| 457 | if (PyCallable_Check(pmeth) == 0) |
| 458 | { |
| 459 | if (PyErr_Occurred()) |
| 460 | { |
| 461 | PyErr_Clear(); |
| 462 | } |
| 463 | |
| 464 | Py_XDECREF(pmeth); |
| 465 | if (was_found) |
| 466 | *was_found = false; |
| 467 | Py_XINCREF(ret_if_not_found); |
| 468 | return ret_if_not_found; |
| 469 | } |
| 470 | |
| 471 | if (was_found) |
| 472 | *was_found = true; |
| 473 | |
| 474 | if (PyErr_Occurred()) |
| 475 | { |
| 476 | PyErr_Clear(); |
| 477 | } |
| 478 | |
| 479 | Py_XDECREF(pmeth); |
| 480 | |
| 481 | // right now we know this function exists and is callable.. |
| 482 | PyObject* py_return = PyObject_CallMethod(self, callee_name, NULL); |
| 483 | |
| 484 | // if it fails, print the error but otherwise go on |
| 485 | if (PyErr_Occurred()) |
| 486 | { |
| 487 | PyErr_Print(); |
| 488 | PyErr_Clear(); |
| 489 | } |
| 490 | |
| 491 | return py_return; |
| 492 | } |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 493 | |
| 494 | SWIGEXPORT uint32_t |
| 495 | LLDBSwigPython_CalculateNumChildren |
| 496 | ( |
| 497 | PyObject *implementor |
| 498 | ) |
| 499 | { |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 500 | uint32_t ret_val = UINT32_MAX; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 501 | |
| 502 | static char callee_name[] = "num_children"; |
| 503 | |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 504 | PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, NULL); |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 505 | |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 506 | if (!py_return) |
| 507 | return ret_val; |
| 508 | |
| 509 | if (PyInt_Check(py_return)) |
| 510 | ret_val = PyInt_AsLong(py_return); |
| 511 | |
| 512 | Py_XDECREF(py_return); |
| 513 | |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 514 | if (PyErr_Occurred()) |
| 515 | { |
| 516 | PyErr_Print(); |
| 517 | PyErr_Clear(); |
| 518 | } |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 519 | |
| 520 | return ret_val; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | SWIGEXPORT PyObject* |
| 524 | LLDBSwigPython_GetChildAtIndex |
| 525 | ( |
| 526 | PyObject *implementor, |
| 527 | uint32_t idx |
| 528 | ) |
| 529 | { |
| 530 | |
| 531 | static char callee_name[] = "get_child_at_index"; |
| 532 | static char param_format[] = "i"; |
| 533 | |
| 534 | if (implementor == NULL || implementor == Py_None) |
| 535 | return NULL; |
| 536 | PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, idx); |
| 537 | if (PyErr_Occurred()) |
| 538 | { |
| 539 | PyErr_Print(); |
| 540 | PyErr_Clear(); |
| 541 | } |
| 542 | |
| 543 | if (py_return == NULL || py_return == Py_None) |
| 544 | { |
| 545 | Py_XDECREF(py_return); |
| 546 | return NULL; |
| 547 | } |
| 548 | |
| 549 | lldb::SBValue* sbvalue_ptr = NULL; |
| 550 | |
| 551 | if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1) |
| 552 | { |
| 553 | Py_DECREF(py_return); |
| 554 | return NULL; |
| 555 | } |
| 556 | |
| 557 | if (sbvalue_ptr == NULL) |
| 558 | return NULL; |
| 559 | |
| 560 | return py_return; |
| 561 | } |
| 562 | |
| 563 | SWIGEXPORT int |
| 564 | LLDBSwigPython_GetIndexOfChildWithName |
| 565 | ( |
| 566 | PyObject *implementor, |
| 567 | const char* child_name |
| 568 | ) |
| 569 | { |
| 570 | static char callee_name[] = "get_child_index"; |
| 571 | static char param_format[] = "s"; |
| 572 | |
| 573 | if (implementor == NULL || implementor == Py_None) |
| 574 | return 0; |
| 575 | PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, child_name); |
| 576 | if (PyErr_Occurred()) |
| 577 | { |
| 578 | PyErr_Print(); |
| 579 | PyErr_Clear(); |
| 580 | } |
| 581 | |
| 582 | if (py_return == NULL || py_return == Py_None) |
| 583 | { |
| 584 | Py_XDECREF(py_return); |
| 585 | return UINT32_MAX; |
| 586 | } |
| 587 | long retval = PyInt_AsLong(py_return); |
| 588 | Py_DECREF(py_return); |
| 589 | if (retval >= 0) |
| 590 | return (uint32_t)retval; |
| 591 | if (PyErr_Occurred()) |
| 592 | { |
| 593 | PyErr_Print(); |
| 594 | PyErr_Clear(); |
| 595 | } |
| 596 | return 0; |
| 597 | } |
| 598 | |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 599 | SWIGEXPORT bool |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 600 | LLDBSwigPython_UpdateSynthProviderInstance |
| 601 | ( |
| 602 | PyObject *implementor |
| 603 | ) |
| 604 | { |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 605 | bool ret_val = false; |
| 606 | |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 607 | static char callee_name[] = "update"; |
| 608 | |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 609 | PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name); |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 610 | |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 611 | if (py_return == Py_True) |
| 612 | ret_val = true; |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 613 | |
| 614 | Py_XDECREF(py_return); |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 615 | |
| 616 | return ret_val; |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 617 | } |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 618 | |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 619 | SWIGEXPORT bool |
| 620 | LLDBSwigPython_MightHaveChildrenSynthProviderInstance |
| 621 | ( |
| 622 | PyObject *implementor |
| 623 | ) |
| 624 | { |
| 625 | bool ret_val = false; |
| 626 | |
| 627 | static char callee_name[] = "has_children"; |
| 628 | |
| 629 | PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True); |
| 630 | |
| 631 | if (py_return == Py_True) |
| 632 | ret_val = true; |
| 633 | |
| 634 | Py_XDECREF(py_return); |
| 635 | |
| 636 | return ret_val; |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 639 | SWIGEXPORT void* |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 640 | LLDBSWIGPython_CastPyObjectToSBValue |
| 641 | ( |
| 642 | PyObject* data |
| 643 | ) |
| 644 | { |
| 645 | lldb::SBValue* sb_ptr = NULL; |
| 646 | |
| 647 | int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0); |
| 648 | |
| 649 | if (valid_cast == -1) |
| 650 | return NULL; |
| 651 | |
| 652 | return sb_ptr; |
| 653 | } |
| 654 | |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 655 | // Currently, SBCommandReturnObjectReleaser wraps an std::auto_ptr to an |
| 656 | // lldb_private::CommandReturnObject. This means that the destructor for the |
| 657 | // SB object will deallocate its contained CommandReturnObject. Because that |
| 658 | // object is used as the real return object for Python-based commands, we want |
| 659 | // it to stay around. Thus, we release the auto_ptr before returning from |
| 660 | // LLDBSwigPythonCallCommand, and to guarantee that the release will occur no |
| 661 | // matter how we exit from the function, we have a releaser object whose |
| 662 | // destructor does the right thing for us |
| 663 | class SBCommandReturnObjectReleaser |
| 664 | { |
| 665 | public: |
| 666 | SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) : |
| 667 | m_command_return_object_ref (obj) |
| 668 | { |
| 669 | } |
| 670 | |
| 671 | ~SBCommandReturnObjectReleaser () |
| 672 | { |
| 673 | m_command_return_object_ref.Release(); |
| 674 | } |
| 675 | private: |
| 676 | lldb::SBCommandReturnObject &m_command_return_object_ref; |
| 677 | }; |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 678 | |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 679 | SWIGEXPORT bool |
| 680 | LLDBSwigPythonCallCommand |
| 681 | ( |
| 682 | const char *python_function_name, |
| 683 | const char *session_dictionary_name, |
| 684 | lldb::DebuggerSP& debugger, |
| 685 | const char* args, |
| 686 | std::string& err_msg, |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 687 | lldb_private::CommandReturnObject& cmd_retobj |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 688 | ) |
| 689 | { |
| 690 | |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 691 | lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj); |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 692 | SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb); |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 693 | lldb::SBDebugger debugger_sb(debugger); |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 694 | |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 695 | bool retval = false; |
| 696 | |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 697 | PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0); |
| 698 | PyObject *CmdRetObj_PyObj = SWIG_NewPointerObj((void *) &cmd_retobj_sb, SWIGTYPE_p_lldb__SBCommandReturnObject, 0); |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 699 | |
| 700 | if (DebuggerObj_PyObj == NULL) |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 701 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 702 | |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 703 | if (CmdRetObj_PyObj == NULL) |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 704 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 705 | |
| 706 | if (!python_function_name || !session_dictionary_name) |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 707 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 708 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 709 | PyObject *session_dict, *pfunc; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 710 | PyObject *pargs, *pvalue; |
| 711 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 712 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 713 | if (session_dict != NULL) |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 714 | { |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 715 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 716 | if (pfunc != NULL) |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 717 | { |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 718 | // Set up the arguments and call the function. |
| 719 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 720 | if (PyCallable_Check (pfunc)) |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 721 | { |
| 722 | pargs = PyTuple_New (4); |
| 723 | if (pargs == NULL) |
| 724 | { |
| 725 | if (PyErr_Occurred()) |
| 726 | PyErr_Clear(); |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 727 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj |
| 731 | PyTuple_SetItem (pargs, 1, PyString_FromString(args)); |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 732 | PyTuple_SetItem (pargs, 2, CmdRetObj_PyObj); // This "steals" a reference to CmdRetObj_PyObj |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 733 | PyTuple_SetItem (pargs, 3, session_dict); // This "steals" a reference to session_dict |
| 734 | pvalue = PyObject_CallObject (pfunc, pargs); |
| 735 | Py_DECREF (pargs); |
| 736 | |
| 737 | if (pvalue != NULL) |
| 738 | { |
| 739 | if (pvalue == Py_None) // no error |
| 740 | { |
| 741 | err_msg.clear(); |
| 742 | retval = true; |
| 743 | } |
Johnny Chen | 2fcf412 | 2011-12-14 20:40:27 +0000 | [diff] [blame] | 744 | else |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 745 | { |
Johnny Chen | 2fcf412 | 2011-12-14 20:40:27 +0000 | [diff] [blame] | 746 | // return value is an error string |
| 747 | if (PyString_CheckExact(pvalue)) |
| 748 | err_msg.assign(PyString_AsString(pvalue)); |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 749 | retval = false; |
| 750 | } |
| 751 | Py_DECREF (pvalue); |
| 752 | } |
| 753 | else if (PyErr_Occurred ()) |
| 754 | { |
| 755 | PyErr_Print(); |
| 756 | PyErr_Clear(); |
| 757 | } |
| 758 | Py_INCREF (session_dict); |
| 759 | } |
| 760 | else if (PyErr_Occurred()) |
| 761 | { |
| 762 | PyErr_Print(); |
| 763 | PyErr_Clear(); |
| 764 | } |
| 765 | } |
| 766 | else if (PyErr_Occurred()) |
| 767 | { |
| 768 | PyErr_Print(); |
| 769 | PyErr_Clear(); |
| 770 | } |
| 771 | } |
| 772 | else if (PyErr_Occurred ()) |
| 773 | { |
| 774 | PyErr_Print(); |
| 775 | PyErr_Clear (); |
| 776 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 777 | return retval; |
| 778 | } |
| 779 | |
Enrico Granata | 155ee91 | 2012-08-24 00:30:47 +0000 | [diff] [blame] | 780 | SWIGEXPORT void* |
| 781 | LLDBSWIGPythonCreateOSPlugin |
| 782 | ( |
Greg Clayton | ec1e823 | 2013-01-16 19:53:55 +0000 | [diff] [blame^] | 783 | const char *python_class_name, |
Enrico Granata | 155ee91 | 2012-08-24 00:30:47 +0000 | [diff] [blame] | 784 | const char *session_dictionary_name, |
| 785 | const lldb::ProcessSP& process_sp |
| 786 | ) |
| 787 | { |
| 788 | PyObject* retval = NULL; |
| 789 | |
Greg Clayton | ec1e823 | 2013-01-16 19:53:55 +0000 | [diff] [blame^] | 790 | if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) |
Enrico Granata | 155ee91 | 2012-08-24 00:30:47 +0000 | [diff] [blame] | 791 | Py_RETURN_NONE; |
| 792 | |
| 793 | // I do not want the SBValue to be deallocated when going out of scope because python |
| 794 | // has ownership of it and will manage memory for this object by itself |
| 795 | lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp); |
| 796 | |
| 797 | PyObject *SBProc_PyObj = SWIG_NewPointerObj((void *)process_sb, SWIGTYPE_p_lldb__SBProcess, 0); |
| 798 | |
| 799 | if (SBProc_PyObj == NULL) |
| 800 | Py_RETURN_NONE; |
| 801 | |
Greg Clayton | ec1e823 | 2013-01-16 19:53:55 +0000 | [diff] [blame^] | 802 | const char* python_function_name = python_class_name; |
Enrico Granata | 155ee91 | 2012-08-24 00:30:47 +0000 | [diff] [blame] | 803 | |
| 804 | PyObject *session_dict, *pfunc; |
| 805 | PyObject *pvalue; |
| 806 | |
| 807 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 808 | if (session_dict != NULL) |
| 809 | { |
| 810 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 811 | if (pfunc != NULL) |
| 812 | { |
| 813 | // Set up the arguments and call the function. |
| 814 | |
| 815 | if (PyCallable_Check (pfunc)) |
| 816 | { |
Enrico Granata | cec963a | 2012-08-24 01:34:39 +0000 | [diff] [blame] | 817 | PyObject *argList = Py_BuildValue("(O)", SBProc_PyObj); |
Enrico Granata | 155ee91 | 2012-08-24 00:30:47 +0000 | [diff] [blame] | 818 | |
| 819 | if (PyErr_Occurred ()) |
| 820 | { |
| 821 | PyErr_Print(); |
| 822 | PyErr_Clear(); |
| 823 | return retval; |
| 824 | } |
| 825 | |
| 826 | if (argList == NULL) |
| 827 | { |
| 828 | return retval; |
| 829 | } |
| 830 | |
| 831 | Py_INCREF(SBProc_PyObj); |
| 832 | |
| 833 | pvalue = PyObject_CallObject(pfunc, argList); |
| 834 | |
| 835 | Py_DECREF(argList); |
| 836 | |
| 837 | if (pvalue != NULL) |
| 838 | { |
| 839 | if (pvalue != Py_None) |
| 840 | retval = pvalue; |
| 841 | else |
| 842 | { |
| 843 | retval = Py_None; |
| 844 | Py_INCREF(retval); |
| 845 | } |
| 846 | } |
| 847 | else if (PyErr_Occurred ()) |
| 848 | { |
| 849 | PyErr_Print(); |
| 850 | PyErr_Clear(); |
| 851 | } |
| 852 | Py_INCREF (session_dict); |
| 853 | } |
| 854 | else if (PyErr_Occurred()) |
| 855 | { |
| 856 | PyErr_Print(); |
| 857 | PyErr_Clear(); |
| 858 | } |
| 859 | } |
| 860 | else if (PyErr_Occurred()) |
| 861 | { |
| 862 | PyErr_Print(); |
| 863 | PyErr_Clear(); |
| 864 | } |
| 865 | } |
| 866 | else if (PyErr_Occurred ()) |
| 867 | { |
| 868 | PyErr_Print(); |
| 869 | PyErr_Clear (); |
| 870 | } |
| 871 | if (retval) |
| 872 | return retval; |
| 873 | else |
| 874 | Py_RETURN_NONE; |
| 875 | } |
| 876 | |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 877 | SWIGEXPORT bool |
| 878 | LLDBSwigPythonCallModuleInit |
| 879 | ( |
Greg Clayton | ec1e823 | 2013-01-16 19:53:55 +0000 | [diff] [blame^] | 880 | const char *python_module_name, |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 881 | const char *session_dictionary_name, |
| 882 | lldb::DebuggerSP& debugger |
| 883 | ) |
| 884 | { |
| 885 | |
| 886 | lldb::SBDebugger debugger_sb(debugger); |
| 887 | |
| 888 | bool retval = false; |
| 889 | |
| 890 | PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0); |
| 891 | |
| 892 | if (DebuggerObj_PyObj == NULL) |
| 893 | return retval; |
| 894 | |
Greg Clayton | ec1e823 | 2013-01-16 19:53:55 +0000 | [diff] [blame^] | 895 | if (python_module_name == NULL || python_module_name[0] == '\0' || !session_dictionary_name) |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 896 | return retval; |
| 897 | |
| 898 | PyObject *session_dict, *pfunc; |
| 899 | PyObject *pargs, *pvalue; |
| 900 | |
| 901 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 902 | |
Greg Clayton | ec1e823 | 2013-01-16 19:53:55 +0000 | [diff] [blame^] | 903 | std::string python_function_name_string = python_module_name; |
| 904 | python_function_name_string += ".__lldb_init_module"; |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 905 | const char* python_function_name = python_function_name_string.c_str(); |
| 906 | |
| 907 | if (session_dict != NULL) |
| 908 | { |
| 909 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 910 | |
| 911 | if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that |
| 912 | { |
| 913 | PyErr_Clear(); |
| 914 | return true; |
| 915 | } |
| 916 | |
| 917 | if (pfunc == NULL) |
| 918 | return true; |
| 919 | else |
| 920 | { |
| 921 | // Set up the arguments and call the function. |
| 922 | |
| 923 | if (PyCallable_Check (pfunc)) |
| 924 | { |
| 925 | pargs = PyTuple_New (2); |
| 926 | if (pargs == NULL) |
| 927 | { |
| 928 | if (PyErr_Occurred()) |
| 929 | PyErr_Clear(); |
| 930 | return retval; |
| 931 | } |
| 932 | |
| 933 | PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj |
| 934 | PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict |
| 935 | pvalue = PyObject_CallObject (pfunc, pargs); |
| 936 | Py_DECREF (pargs); |
| 937 | |
| 938 | if (PyErr_Occurred ()) |
| 939 | { |
| 940 | PyErr_Print(); |
| 941 | PyErr_Clear(); |
| 942 | } |
| 943 | else |
| 944 | { |
| 945 | retval = true; |
| 946 | Py_XDECREF (pvalue); |
| 947 | } |
| 948 | Py_INCREF (session_dict); |
| 949 | } |
| 950 | else if (PyErr_Occurred()) |
| 951 | { |
| 952 | PyErr_Print(); |
| 953 | PyErr_Clear(); |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 958 | } |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame] | 959 | %} |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 960 | |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame] | 961 | |
| 962 | %runtime %{ |
| 963 | // Forward declaration to be inserted at the start of LLDBWrapPython.h |
| 964 | // I used runtime as a hack to make SWIG place it where it's needed. |
| 965 | // This is needed to use LLDBSwigPythonCallSBInputReaderCallback in the |
| 966 | // typemaps and in the extensions (SBInputReader.__del__()). |
Filipe Cabecinhas | 85fbe9d | 2012-08-22 18:10:45 +0000 | [diff] [blame] | 967 | #include "lldb/API/SBInputReader.h" |
Filipe Cabecinhas | 43898d7 | 2012-08-25 00:29:07 +0000 | [diff] [blame] | 968 | #include "lldb/API/SBDebugger.h" |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame] | 969 | |
Daniel Malea | 37459fc | 2012-11-29 16:38:44 +0000 | [diff] [blame] | 970 | #ifdef __cplusplus |
| 971 | extern "C" { |
| 972 | #endif |
| 973 | |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame] | 974 | size_t |
| 975 | LLDBSwigPythonCallSBInputReaderCallback(void *baton, |
| 976 | lldb::SBInputReader *reader, |
| 977 | lldb::InputReaderAction notification, |
| 978 | const char*bytes, |
| 979 | size_t bytes_len); |
Filipe Cabecinhas | 43898d7 | 2012-08-25 00:29:07 +0000 | [diff] [blame] | 980 | |
| 981 | void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton); |
Daniel Malea | 37459fc | 2012-11-29 16:38:44 +0000 | [diff] [blame] | 982 | |
| 983 | #ifdef __cplusplus |
| 984 | } |
| 985 | #endif |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame] | 986 | %} |
| 987 | |
| 988 | %wrapper %{ |
| 989 | // For the InputReader Callback functions |
| 990 | SWIGEXPORT size_t |
| 991 | LLDBSwigPythonCallSBInputReaderCallback(void *baton, |
| 992 | lldb::SBInputReader *reader, |
| 993 | lldb::InputReaderAction notification, |
| 994 | const char*bytes, |
| 995 | size_t bytes_len) { |
Filipe Cabecinhas | 43898d7 | 2012-08-25 00:29:07 +0000 | [diff] [blame] | 996 | if (baton != Py_None) { |
| 997 | SWIG_PYTHON_THREAD_BEGIN_BLOCK; |
| 998 | |
| 999 | PyObject *py_InputReader = SWIG_NewPointerObj(reader, SWIGTYPE_p_lldb__SBInputReader, false); |
| 1000 | PyObject *py_Notification = PyInt_FromLong(notification); |
| 1001 | PyObject *py_Bytes = PyBytes_FromStringAndSize(bytes, bytes_len); |
| 1002 | |
| 1003 | PyObject *tuple = PyTuple_Pack(3, py_InputReader, py_Notification, py_Bytes); |
| 1004 | PyObject *res = PyObject_Call(reinterpret_cast<PyObject*>(baton), tuple, NULL); |
| 1005 | Py_DECREF(tuple); |
| 1006 | Py_DECREF(py_InputReader); |
| 1007 | Py_DECREF(py_Notification); |
| 1008 | Py_DECREF(py_Bytes); |
| 1009 | |
| 1010 | if (res == NULL) { |
| 1011 | PyObject *exc = PyErr_Occurred(); |
| 1012 | if (exc) { |
| 1013 | ::puts("\nErroring out at LLDBSwigPythonCallSBInputReaderCallback"); |
| 1014 | PyErr_Print(); |
| 1015 | } |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | size_t result = 0; |
| 1020 | // If the callback misbehaves and returns Py_None, assume it returned 0 |
| 1021 | if (res != Py_None) |
| 1022 | result = static_cast<size_t>(PyInt_AsSsize_t(res)); |
| 1023 | |
| 1024 | Py_DECREF(res); |
| 1025 | SWIG_PYTHON_THREAD_END_BLOCK; |
| 1026 | return result; |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame] | 1027 | } |
Enrico Granata | e25c631 | 2012-08-27 18:30:45 +0000 | [diff] [blame] | 1028 | return 0; |
Filipe Cabecinhas | 43898d7 | 2012-08-25 00:29:07 +0000 | [diff] [blame] | 1029 | } |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame] | 1030 | |
Filipe Cabecinhas | 43898d7 | 2012-08-25 00:29:07 +0000 | [diff] [blame] | 1031 | // For the LogOutputCallback functions |
| 1032 | void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) { |
| 1033 | if (baton != Py_None) { |
| 1034 | SWIG_PYTHON_THREAD_BEGIN_BLOCK; |
| 1035 | PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str); |
| 1036 | SWIG_PYTHON_THREAD_END_BLOCK; |
| 1037 | } |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame] | 1038 | } |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 1039 | %} |