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