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