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 | |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 270 | 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] | 271 | |
| 272 | if (ValObj_PyObj == NULL) |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 273 | return false; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 274 | |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 275 | if (!python_function_name || !session_dictionary) |
| 276 | return false; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 277 | |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 278 | PyObject *session_dict = (PyObject*)session_dictionary, *pfunc = NULL, *pargs = NULL, *pvalue = NULL; |
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 | if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper)) |
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 | pfunc = (PyObject*)(*pyfunct_wrapper); |
| 283 | if (pfunc->ob_refcnt == 1) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 284 | { |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 285 | Py_XDECREF(pfunc); |
| 286 | pfunc = NULL; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 289 | |
| 290 | if (PyDict_Check(session_dict)) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 291 | { |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 292 | PyErr_Cleaner pyerr_cleanup(true); // show Python errors |
| 293 | |
| 294 | if (!pfunc) |
| 295 | { |
| 296 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 297 | if (!pfunc || !PyFunction_Check (pfunc)) |
| 298 | return false; |
| 299 | else |
| 300 | { |
| 301 | if (pyfunct_wrapper) |
| 302 | *pyfunct_wrapper = pfunc; |
| 303 | } |
| 304 | } |
| 305 | /*else |
| 306 | printf("caching works!!!!\n");*/ |
| 307 | |
| 308 | pargs = PyTuple_Pack(2, ValObj_PyObj, session_dict); |
| 309 | if (pargs == NULL) |
| 310 | return false; |
| 311 | |
| 312 | pvalue = PyObject_CallObject (pfunc, pargs); |
| 313 | Py_DECREF (pargs); |
| 314 | |
| 315 | if (pvalue != NULL && pvalue != Py_None && PyString_Check(pvalue)) |
| 316 | retval.assign(PyString_AsString(pvalue)); |
| 317 | Py_XDECREF (pvalue); |
| 318 | Py_INCREF (session_dict); |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 319 | } |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 320 | return true; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | SWIGEXPORT void* |
| 324 | LLDBSwigPythonCreateSyntheticProvider |
| 325 | ( |
| 326 | const std::string python_class_name, |
| 327 | const char *session_dictionary_name, |
| 328 | const lldb::ValueObjectSP& valobj_sp |
| 329 | ) |
| 330 | { |
| 331 | PyObject* retval = NULL; |
| 332 | |
| 333 | if (python_class_name.empty() || !session_dictionary_name) |
| 334 | Py_RETURN_NONE; |
| 335 | |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 336 | // I do not want the SBValue to be deallocated when going out of scope because python |
| 337 | // has ownership of it and will manage memory for this object by itself |
| 338 | lldb::SBValue *valobj_sb = new lldb::SBValue(valobj_sp); |
Enrico Granata | 3d656c7 | 2012-10-22 18:18:36 +0000 | [diff] [blame] | 339 | valobj_sb->SetPreferSyntheticValue(false); |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 340 | |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 341 | 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] | 342 | |
| 343 | if (ValObj_PyObj == NULL) |
| 344 | Py_RETURN_NONE; |
| 345 | |
| 346 | const char* python_function_name = python_class_name.c_str(); |
| 347 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 348 | PyObject *session_dict, *pfunc; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 349 | PyObject *pvalue; |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 350 | |
| 351 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 352 | if (session_dict != NULL) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 353 | { |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 354 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 355 | if (pfunc != NULL) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 356 | { |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 357 | // Set up the arguments and call the function. |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 358 | |
| 359 | if (PyCallable_Check (pfunc)) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 360 | { |
| 361 | PyObject *argList = Py_BuildValue("SS", ValObj_PyObj, session_dict); |
| 362 | |
| 363 | if (PyErr_Occurred ()) |
| 364 | { |
| 365 | PyErr_Print(); |
| 366 | PyErr_Clear(); |
| 367 | return retval; |
| 368 | } |
| 369 | |
| 370 | if (argList == NULL) |
| 371 | { |
| 372 | return retval; |
| 373 | } |
| 374 | |
| 375 | Py_INCREF(ValObj_PyObj); |
| 376 | |
| 377 | pvalue = PyObject_CallObject(pfunc, argList); |
| 378 | |
| 379 | Py_DECREF(argList); |
| 380 | |
| 381 | if (pvalue != NULL) |
| 382 | { |
| 383 | if (pvalue != Py_None) |
| 384 | retval = pvalue; |
| 385 | else |
| 386 | { |
| 387 | retval = Py_None; |
| 388 | Py_INCREF(retval); |
| 389 | } |
| 390 | } |
| 391 | else if (PyErr_Occurred ()) |
| 392 | { |
| 393 | PyErr_Print(); |
| 394 | PyErr_Clear(); |
| 395 | } |
| 396 | Py_INCREF (session_dict); |
| 397 | } |
| 398 | else if (PyErr_Occurred()) |
| 399 | { |
| 400 | PyErr_Print(); |
| 401 | PyErr_Clear(); |
| 402 | } |
| 403 | } |
| 404 | else if (PyErr_Occurred()) |
| 405 | { |
| 406 | PyErr_Print(); |
| 407 | PyErr_Clear(); |
| 408 | } |
| 409 | } |
| 410 | else if (PyErr_Occurred ()) |
| 411 | { |
| 412 | PyErr_Print(); |
| 413 | PyErr_Clear (); |
| 414 | } |
| 415 | if (retval) |
| 416 | return retval; |
| 417 | else |
| 418 | Py_RETURN_NONE; |
| 419 | } |
| 420 | |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 421 | // wrapper that calls an optional instance member of an object taking no arguments |
| 422 | static PyObject* |
| 423 | LLDBSwigPython_CallOptionalMember |
| 424 | ( |
| 425 | PyObject* self, |
| 426 | char* callee_name, |
| 427 | PyObject* ret_if_not_found = Py_None, |
| 428 | bool* was_found = NULL |
| 429 | ) |
| 430 | { |
| 431 | if (self == NULL || self == Py_None) |
| 432 | { |
| 433 | if (was_found) |
| 434 | *was_found = false; |
| 435 | Py_XINCREF(ret_if_not_found); |
| 436 | return ret_if_not_found; |
| 437 | } |
| 438 | |
| 439 | PyObject* pmeth = PyObject_GetAttrString(self, callee_name); |
| 440 | |
| 441 | if (PyErr_Occurred()) |
| 442 | { |
| 443 | PyErr_Clear(); |
| 444 | } |
| 445 | |
| 446 | if (pmeth == NULL || pmeth == Py_None) |
| 447 | { |
| 448 | if (was_found) |
| 449 | *was_found = false; |
| 450 | Py_XDECREF(pmeth); |
| 451 | Py_XINCREF(ret_if_not_found); |
| 452 | return ret_if_not_found; |
| 453 | } |
| 454 | |
| 455 | if (PyCallable_Check(pmeth) == 0) |
| 456 | { |
| 457 | if (PyErr_Occurred()) |
| 458 | { |
| 459 | PyErr_Clear(); |
| 460 | } |
| 461 | |
| 462 | Py_XDECREF(pmeth); |
| 463 | if (was_found) |
| 464 | *was_found = false; |
| 465 | Py_XINCREF(ret_if_not_found); |
| 466 | return ret_if_not_found; |
| 467 | } |
| 468 | |
| 469 | if (was_found) |
| 470 | *was_found = true; |
| 471 | |
| 472 | if (PyErr_Occurred()) |
| 473 | { |
| 474 | PyErr_Clear(); |
| 475 | } |
| 476 | |
| 477 | Py_XDECREF(pmeth); |
| 478 | |
| 479 | // right now we know this function exists and is callable.. |
| 480 | PyObject* py_return = PyObject_CallMethod(self, callee_name, NULL); |
| 481 | |
| 482 | // if it fails, print the error but otherwise go on |
| 483 | if (PyErr_Occurred()) |
| 484 | { |
| 485 | PyErr_Print(); |
| 486 | PyErr_Clear(); |
| 487 | } |
| 488 | |
| 489 | return py_return; |
| 490 | } |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 491 | |
| 492 | SWIGEXPORT uint32_t |
| 493 | LLDBSwigPython_CalculateNumChildren |
| 494 | ( |
| 495 | PyObject *implementor |
| 496 | ) |
| 497 | { |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 498 | uint32_t ret_val = UINT32_MAX; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 499 | |
| 500 | static char callee_name[] = "num_children"; |
| 501 | |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 502 | PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, NULL); |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 503 | |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 504 | if (!py_return) |
| 505 | return ret_val; |
| 506 | |
| 507 | if (PyInt_Check(py_return)) |
| 508 | ret_val = PyInt_AsLong(py_return); |
| 509 | |
| 510 | Py_XDECREF(py_return); |
| 511 | |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 512 | if (PyErr_Occurred()) |
| 513 | { |
| 514 | PyErr_Print(); |
| 515 | PyErr_Clear(); |
| 516 | } |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 517 | |
| 518 | return ret_val; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | SWIGEXPORT PyObject* |
| 522 | LLDBSwigPython_GetChildAtIndex |
| 523 | ( |
| 524 | PyObject *implementor, |
| 525 | uint32_t idx |
| 526 | ) |
| 527 | { |
| 528 | |
| 529 | static char callee_name[] = "get_child_at_index"; |
| 530 | static char param_format[] = "i"; |
| 531 | |
| 532 | if (implementor == NULL || implementor == Py_None) |
| 533 | return NULL; |
| 534 | PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, idx); |
| 535 | if (PyErr_Occurred()) |
| 536 | { |
| 537 | PyErr_Print(); |
| 538 | PyErr_Clear(); |
| 539 | } |
| 540 | |
| 541 | if (py_return == NULL || py_return == Py_None) |
| 542 | { |
| 543 | Py_XDECREF(py_return); |
| 544 | return NULL; |
| 545 | } |
| 546 | |
| 547 | lldb::SBValue* sbvalue_ptr = NULL; |
| 548 | |
| 549 | if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1) |
| 550 | { |
| 551 | Py_DECREF(py_return); |
| 552 | return NULL; |
| 553 | } |
| 554 | |
| 555 | if (sbvalue_ptr == NULL) |
| 556 | return NULL; |
| 557 | |
| 558 | return py_return; |
| 559 | } |
| 560 | |
| 561 | SWIGEXPORT int |
| 562 | LLDBSwigPython_GetIndexOfChildWithName |
| 563 | ( |
| 564 | PyObject *implementor, |
| 565 | const char* child_name |
| 566 | ) |
| 567 | { |
| 568 | static char callee_name[] = "get_child_index"; |
| 569 | static char param_format[] = "s"; |
| 570 | |
| 571 | if (implementor == NULL || implementor == Py_None) |
| 572 | return 0; |
| 573 | PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, child_name); |
| 574 | if (PyErr_Occurred()) |
| 575 | { |
| 576 | PyErr_Print(); |
| 577 | PyErr_Clear(); |
| 578 | } |
| 579 | |
| 580 | if (py_return == NULL || py_return == Py_None) |
| 581 | { |
| 582 | Py_XDECREF(py_return); |
| 583 | return UINT32_MAX; |
| 584 | } |
| 585 | long retval = PyInt_AsLong(py_return); |
| 586 | Py_DECREF(py_return); |
| 587 | if (retval >= 0) |
| 588 | return (uint32_t)retval; |
| 589 | if (PyErr_Occurred()) |
| 590 | { |
| 591 | PyErr_Print(); |
| 592 | PyErr_Clear(); |
| 593 | } |
| 594 | return 0; |
| 595 | } |
| 596 | |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 597 | SWIGEXPORT bool |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 598 | LLDBSwigPython_UpdateSynthProviderInstance |
| 599 | ( |
| 600 | PyObject *implementor |
| 601 | ) |
| 602 | { |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 603 | bool ret_val = false; |
| 604 | |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 605 | static char callee_name[] = "update"; |
| 606 | |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 607 | PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name); |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 608 | |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 609 | if (py_return == Py_True) |
| 610 | ret_val = true; |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 611 | |
| 612 | Py_XDECREF(py_return); |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 613 | |
| 614 | return ret_val; |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 615 | } |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 616 | |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 617 | SWIGEXPORT bool |
| 618 | LLDBSwigPython_MightHaveChildrenSynthProviderInstance |
| 619 | ( |
| 620 | PyObject *implementor |
| 621 | ) |
| 622 | { |
| 623 | bool ret_val = false; |
| 624 | |
| 625 | static char callee_name[] = "has_children"; |
| 626 | |
| 627 | PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True); |
| 628 | |
| 629 | if (py_return == Py_True) |
| 630 | ret_val = true; |
| 631 | |
| 632 | Py_XDECREF(py_return); |
| 633 | |
| 634 | return ret_val; |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 637 | SWIGEXPORT void* |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 638 | LLDBSWIGPython_CastPyObjectToSBValue |
| 639 | ( |
| 640 | PyObject* data |
| 641 | ) |
| 642 | { |
| 643 | lldb::SBValue* sb_ptr = NULL; |
| 644 | |
| 645 | int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0); |
| 646 | |
| 647 | if (valid_cast == -1) |
| 648 | return NULL; |
| 649 | |
| 650 | return sb_ptr; |
| 651 | } |
| 652 | |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 653 | // Currently, SBCommandReturnObjectReleaser wraps an std::auto_ptr to an |
| 654 | // lldb_private::CommandReturnObject. This means that the destructor for the |
| 655 | // SB object will deallocate its contained CommandReturnObject. Because that |
| 656 | // object is used as the real return object for Python-based commands, we want |
| 657 | // it to stay around. Thus, we release the auto_ptr before returning from |
| 658 | // LLDBSwigPythonCallCommand, and to guarantee that the release will occur no |
| 659 | // matter how we exit from the function, we have a releaser object whose |
| 660 | // destructor does the right thing for us |
| 661 | class SBCommandReturnObjectReleaser |
| 662 | { |
| 663 | public: |
| 664 | SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) : |
| 665 | m_command_return_object_ref (obj) |
| 666 | { |
| 667 | } |
| 668 | |
| 669 | ~SBCommandReturnObjectReleaser () |
| 670 | { |
| 671 | m_command_return_object_ref.Release(); |
| 672 | } |
| 673 | private: |
| 674 | lldb::SBCommandReturnObject &m_command_return_object_ref; |
| 675 | }; |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 676 | |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 677 | SWIGEXPORT bool |
| 678 | LLDBSwigPythonCallCommand |
| 679 | ( |
| 680 | const char *python_function_name, |
| 681 | const char *session_dictionary_name, |
| 682 | lldb::DebuggerSP& debugger, |
| 683 | const char* args, |
| 684 | std::string& err_msg, |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 685 | lldb_private::CommandReturnObject& cmd_retobj |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 686 | ) |
| 687 | { |
| 688 | |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 689 | lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj); |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 690 | SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb); |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 691 | lldb::SBDebugger debugger_sb(debugger); |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 692 | |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 693 | bool retval = false; |
| 694 | |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 695 | PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0); |
| 696 | 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] | 697 | |
| 698 | if (DebuggerObj_PyObj == NULL) |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 699 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 700 | |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 701 | if (CmdRetObj_PyObj == NULL) |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 702 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 703 | |
| 704 | if (!python_function_name || !session_dictionary_name) |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 705 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 706 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 707 | PyObject *session_dict, *pfunc; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 708 | PyObject *pargs, *pvalue; |
| 709 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 710 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 711 | if (session_dict != NULL) |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 712 | { |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 713 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 714 | if (pfunc != NULL) |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 715 | { |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 716 | // Set up the arguments and call the function. |
| 717 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 718 | if (PyCallable_Check (pfunc)) |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 719 | { |
| 720 | pargs = PyTuple_New (4); |
| 721 | if (pargs == NULL) |
| 722 | { |
| 723 | if (PyErr_Occurred()) |
| 724 | PyErr_Clear(); |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 725 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj |
| 729 | PyTuple_SetItem (pargs, 1, PyString_FromString(args)); |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 730 | 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] | 731 | PyTuple_SetItem (pargs, 3, session_dict); // This "steals" a reference to session_dict |
| 732 | pvalue = PyObject_CallObject (pfunc, pargs); |
| 733 | Py_DECREF (pargs); |
| 734 | |
| 735 | if (pvalue != NULL) |
| 736 | { |
| 737 | if (pvalue == Py_None) // no error |
| 738 | { |
| 739 | err_msg.clear(); |
| 740 | retval = true; |
| 741 | } |
Johnny Chen | 2fcf412 | 2011-12-14 20:40:27 +0000 | [diff] [blame] | 742 | else |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 743 | { |
Johnny Chen | 2fcf412 | 2011-12-14 20:40:27 +0000 | [diff] [blame] | 744 | // return value is an error string |
| 745 | if (PyString_CheckExact(pvalue)) |
| 746 | err_msg.assign(PyString_AsString(pvalue)); |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 747 | retval = false; |
| 748 | } |
| 749 | Py_DECREF (pvalue); |
| 750 | } |
| 751 | else if (PyErr_Occurred ()) |
| 752 | { |
| 753 | PyErr_Print(); |
| 754 | PyErr_Clear(); |
| 755 | } |
| 756 | Py_INCREF (session_dict); |
| 757 | } |
| 758 | else if (PyErr_Occurred()) |
| 759 | { |
| 760 | PyErr_Print(); |
| 761 | PyErr_Clear(); |
| 762 | } |
| 763 | } |
| 764 | else if (PyErr_Occurred()) |
| 765 | { |
| 766 | PyErr_Print(); |
| 767 | PyErr_Clear(); |
| 768 | } |
| 769 | } |
| 770 | else if (PyErr_Occurred ()) |
| 771 | { |
| 772 | PyErr_Print(); |
| 773 | PyErr_Clear (); |
| 774 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 775 | return retval; |
| 776 | } |
| 777 | |
Enrico Granata | 155ee91 | 2012-08-24 00:30:47 +0000 | [diff] [blame] | 778 | SWIGEXPORT void* |
| 779 | LLDBSWIGPythonCreateOSPlugin |
| 780 | ( |
| 781 | const std::string python_class_name, |
| 782 | const char *session_dictionary_name, |
| 783 | const lldb::ProcessSP& process_sp |
| 784 | ) |
| 785 | { |
| 786 | PyObject* retval = NULL; |
| 787 | |
| 788 | if (python_class_name.empty() || !session_dictionary_name) |
| 789 | Py_RETURN_NONE; |
| 790 | |
| 791 | // I do not want the SBValue to be deallocated when going out of scope because python |
| 792 | // has ownership of it and will manage memory for this object by itself |
| 793 | lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp); |
| 794 | |
| 795 | PyObject *SBProc_PyObj = SWIG_NewPointerObj((void *)process_sb, SWIGTYPE_p_lldb__SBProcess, 0); |
| 796 | |
| 797 | if (SBProc_PyObj == NULL) |
| 798 | Py_RETURN_NONE; |
| 799 | |
| 800 | const char* python_function_name = python_class_name.c_str(); |
| 801 | |
| 802 | PyObject *session_dict, *pfunc; |
| 803 | PyObject *pvalue; |
| 804 | |
| 805 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 806 | if (session_dict != NULL) |
| 807 | { |
| 808 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 809 | if (pfunc != NULL) |
| 810 | { |
| 811 | // Set up the arguments and call the function. |
| 812 | |
| 813 | if (PyCallable_Check (pfunc)) |
| 814 | { |
Enrico Granata | cec963a | 2012-08-24 01:34:39 +0000 | [diff] [blame] | 815 | PyObject *argList = Py_BuildValue("(O)", SBProc_PyObj); |
Enrico Granata | 155ee91 | 2012-08-24 00:30:47 +0000 | [diff] [blame] | 816 | |
| 817 | if (PyErr_Occurred ()) |
| 818 | { |
| 819 | PyErr_Print(); |
| 820 | PyErr_Clear(); |
| 821 | return retval; |
| 822 | } |
| 823 | |
| 824 | if (argList == NULL) |
| 825 | { |
| 826 | return retval; |
| 827 | } |
| 828 | |
| 829 | Py_INCREF(SBProc_PyObj); |
| 830 | |
| 831 | pvalue = PyObject_CallObject(pfunc, argList); |
| 832 | |
| 833 | Py_DECREF(argList); |
| 834 | |
| 835 | if (pvalue != NULL) |
| 836 | { |
| 837 | if (pvalue != Py_None) |
| 838 | retval = pvalue; |
| 839 | else |
| 840 | { |
| 841 | retval = Py_None; |
| 842 | Py_INCREF(retval); |
| 843 | } |
| 844 | } |
| 845 | else if (PyErr_Occurred ()) |
| 846 | { |
| 847 | PyErr_Print(); |
| 848 | PyErr_Clear(); |
| 849 | } |
| 850 | Py_INCREF (session_dict); |
| 851 | } |
| 852 | else if (PyErr_Occurred()) |
| 853 | { |
| 854 | PyErr_Print(); |
| 855 | PyErr_Clear(); |
| 856 | } |
| 857 | } |
| 858 | else if (PyErr_Occurred()) |
| 859 | { |
| 860 | PyErr_Print(); |
| 861 | PyErr_Clear(); |
| 862 | } |
| 863 | } |
| 864 | else if (PyErr_Occurred ()) |
| 865 | { |
| 866 | PyErr_Print(); |
| 867 | PyErr_Clear (); |
| 868 | } |
| 869 | if (retval) |
| 870 | return retval; |
| 871 | else |
| 872 | Py_RETURN_NONE; |
| 873 | } |
| 874 | |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 875 | SWIGEXPORT bool |
| 876 | LLDBSwigPythonCallModuleInit |
| 877 | ( |
| 878 | const std::string python_module_name, |
| 879 | const char *session_dictionary_name, |
| 880 | lldb::DebuggerSP& debugger |
| 881 | ) |
| 882 | { |
| 883 | |
| 884 | lldb::SBDebugger debugger_sb(debugger); |
| 885 | |
| 886 | bool retval = false; |
| 887 | |
| 888 | PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0); |
| 889 | |
| 890 | if (DebuggerObj_PyObj == NULL) |
| 891 | return retval; |
| 892 | |
| 893 | if (!(python_module_name.length()) || !session_dictionary_name) |
| 894 | return retval; |
| 895 | |
| 896 | PyObject *session_dict, *pfunc; |
| 897 | PyObject *pargs, *pvalue; |
| 898 | |
| 899 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 900 | |
| 901 | std::string python_function_name_string = python_module_name + (".__lldb_init_module"); |
| 902 | const char* python_function_name = python_function_name_string.c_str(); |
| 903 | |
| 904 | if (session_dict != NULL) |
| 905 | { |
| 906 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 907 | |
| 908 | if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that |
| 909 | { |
| 910 | PyErr_Clear(); |
| 911 | return true; |
| 912 | } |
| 913 | |
| 914 | if (pfunc == NULL) |
| 915 | return true; |
| 916 | else |
| 917 | { |
| 918 | // Set up the arguments and call the function. |
| 919 | |
| 920 | if (PyCallable_Check (pfunc)) |
| 921 | { |
| 922 | pargs = PyTuple_New (2); |
| 923 | if (pargs == NULL) |
| 924 | { |
| 925 | if (PyErr_Occurred()) |
| 926 | PyErr_Clear(); |
| 927 | return retval; |
| 928 | } |
| 929 | |
| 930 | PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj |
| 931 | PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict |
| 932 | pvalue = PyObject_CallObject (pfunc, pargs); |
| 933 | Py_DECREF (pargs); |
| 934 | |
| 935 | if (PyErr_Occurred ()) |
| 936 | { |
| 937 | PyErr_Print(); |
| 938 | PyErr_Clear(); |
| 939 | } |
| 940 | else |
| 941 | { |
| 942 | retval = true; |
| 943 | Py_XDECREF (pvalue); |
| 944 | } |
| 945 | Py_INCREF (session_dict); |
| 946 | } |
| 947 | else if (PyErr_Occurred()) |
| 948 | { |
| 949 | PyErr_Print(); |
| 950 | PyErr_Clear(); |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 955 | } |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame] | 956 | %} |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 957 | |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame] | 958 | |
| 959 | %runtime %{ |
| 960 | // Forward declaration to be inserted at the start of LLDBWrapPython.h |
| 961 | // I used runtime as a hack to make SWIG place it where it's needed. |
| 962 | // This is needed to use LLDBSwigPythonCallSBInputReaderCallback in the |
| 963 | // typemaps and in the extensions (SBInputReader.__del__()). |
Filipe Cabecinhas | 85fbe9d | 2012-08-22 18:10:45 +0000 | [diff] [blame] | 964 | #include "lldb/API/SBInputReader.h" |
Filipe Cabecinhas | 43898d7 | 2012-08-25 00:29:07 +0000 | [diff] [blame] | 965 | #include "lldb/API/SBDebugger.h" |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame] | 966 | |
Daniel Malea | 37459fc | 2012-11-29 16:38:44 +0000 | [diff] [blame^] | 967 | #ifdef __cplusplus |
| 968 | extern "C" { |
| 969 | #endif |
| 970 | |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame] | 971 | size_t |
| 972 | LLDBSwigPythonCallSBInputReaderCallback(void *baton, |
| 973 | lldb::SBInputReader *reader, |
| 974 | lldb::InputReaderAction notification, |
| 975 | const char*bytes, |
| 976 | size_t bytes_len); |
Filipe Cabecinhas | 43898d7 | 2012-08-25 00:29:07 +0000 | [diff] [blame] | 977 | |
| 978 | void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton); |
Daniel Malea | 37459fc | 2012-11-29 16:38:44 +0000 | [diff] [blame^] | 979 | |
| 980 | #ifdef __cplusplus |
| 981 | } |
| 982 | #endif |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame] | 983 | %} |
| 984 | |
| 985 | %wrapper %{ |
| 986 | // For the InputReader Callback functions |
| 987 | SWIGEXPORT size_t |
| 988 | LLDBSwigPythonCallSBInputReaderCallback(void *baton, |
| 989 | lldb::SBInputReader *reader, |
| 990 | lldb::InputReaderAction notification, |
| 991 | const char*bytes, |
| 992 | size_t bytes_len) { |
Filipe Cabecinhas | 43898d7 | 2012-08-25 00:29:07 +0000 | [diff] [blame] | 993 | if (baton != Py_None) { |
| 994 | SWIG_PYTHON_THREAD_BEGIN_BLOCK; |
| 995 | |
| 996 | PyObject *py_InputReader = SWIG_NewPointerObj(reader, SWIGTYPE_p_lldb__SBInputReader, false); |
| 997 | PyObject *py_Notification = PyInt_FromLong(notification); |
| 998 | PyObject *py_Bytes = PyBytes_FromStringAndSize(bytes, bytes_len); |
| 999 | |
| 1000 | PyObject *tuple = PyTuple_Pack(3, py_InputReader, py_Notification, py_Bytes); |
| 1001 | PyObject *res = PyObject_Call(reinterpret_cast<PyObject*>(baton), tuple, NULL); |
| 1002 | Py_DECREF(tuple); |
| 1003 | Py_DECREF(py_InputReader); |
| 1004 | Py_DECREF(py_Notification); |
| 1005 | Py_DECREF(py_Bytes); |
| 1006 | |
| 1007 | if (res == NULL) { |
| 1008 | PyObject *exc = PyErr_Occurred(); |
| 1009 | if (exc) { |
| 1010 | ::puts("\nErroring out at LLDBSwigPythonCallSBInputReaderCallback"); |
| 1011 | PyErr_Print(); |
| 1012 | } |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | size_t result = 0; |
| 1017 | // If the callback misbehaves and returns Py_None, assume it returned 0 |
| 1018 | if (res != Py_None) |
| 1019 | result = static_cast<size_t>(PyInt_AsSsize_t(res)); |
| 1020 | |
| 1021 | Py_DECREF(res); |
| 1022 | SWIG_PYTHON_THREAD_END_BLOCK; |
| 1023 | return result; |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame] | 1024 | } |
Enrico Granata | e25c631 | 2012-08-27 18:30:45 +0000 | [diff] [blame] | 1025 | return 0; |
Filipe Cabecinhas | 43898d7 | 2012-08-25 00:29:07 +0000 | [diff] [blame] | 1026 | } |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame] | 1027 | |
Filipe Cabecinhas | 43898d7 | 2012-08-25 00:29:07 +0000 | [diff] [blame] | 1028 | // For the LogOutputCallback functions |
| 1029 | void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) { |
| 1030 | if (baton != Py_None) { |
| 1031 | SWIG_PYTHON_THREAD_BEGIN_BLOCK; |
| 1032 | PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str); |
| 1033 | SWIG_PYTHON_THREAD_END_BLOCK; |
| 1034 | } |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame] | 1035 | } |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 1036 | %} |