1%header %{ 2 3template <typename T> 4PyObject * 5SBTypeToSWIGWrapper (T* item); 6 7class PyErr_Cleaner 8{ 9public: 10 PyErr_Cleaner(bool print=false) : 11 m_print(print) 12 { 13 } 14 15 ~PyErr_Cleaner() 16 { 17 if (PyErr_Occurred()) 18 { 19 if(m_print && !PyErr_ExceptionMatches(PyExc_SystemExit)) 20 PyErr_Print(); 21 PyErr_Clear(); 22 } 23 } 24 25private: 26 bool m_print; 27}; 28 29%} 30 31%wrapper %{ 32 33// resolve a dotted Python name in the form 34// foo.bar.baz.Foobar to an actual Python object 35// if pmodule is NULL, the __main__ module will be used 36// as the starting point for the search 37 38 39// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...) 40// and is used when a script command is attached to a breakpoint for execution. 41 42SWIGEXPORT llvm::Expected<bool> 43LLDBSwigPythonBreakpointCallbackFunction 44( 45 const char *python_function_name, 46 const char *session_dictionary_name, 47 const lldb::StackFrameSP& frame_sp, 48 const lldb::BreakpointLocationSP& bp_loc_sp, 49 lldb_private::StructuredDataImpl *args_impl 50) 51{ 52 using namespace llvm; 53 54 lldb::SBFrame sb_frame (frame_sp); 55 lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp); 56 57 PyErr_Cleaner py_err_cleaner(true); 58 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 59 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 60 61 unsigned max_positional_args; 62 if (auto arg_info = pfunc.GetArgInfo()) 63 max_positional_args = arg_info.get().max_positional_args; 64 else 65 return arg_info.takeError(); 66 67 PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame)); 68 PythonObject bp_loc_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_bp_loc)); 69 70 auto result = [&] () -> Expected<PythonObject> { 71 // If the called function doesn't take extra_args, drop them here: 72 if (max_positional_args < 4) { 73 return pfunc.Call(frame_arg, bp_loc_arg, dict); 74 } else { 75 lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl); 76 PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(args_value)); 77 return pfunc.Call(frame_arg, bp_loc_arg, args_arg, dict); 78 } 79 } (); 80 81 if (!result) 82 return result.takeError(); 83 84 // Only False counts as false! 85 return result.get().get() != Py_False; 86} 87 88// This function is called by lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...) 89// and is used when a script command is attached to a watchpoint for execution. 90 91SWIGEXPORT bool 92LLDBSwigPythonWatchpointCallbackFunction 93( 94 const char *python_function_name, 95 const char *session_dictionary_name, 96 const lldb::StackFrameSP& frame_sp, 97 const lldb::WatchpointSP& wp_sp 98) 99{ 100 lldb::SBFrame sb_frame (frame_sp); 101 lldb::SBWatchpoint sb_wp(wp_sp); 102 103 bool stop_at_watchpoint = true; 104 105 PyErr_Cleaner py_err_cleaner(true); 106 107 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 108 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 109 110 if (!pfunc.IsAllocated()) 111 return stop_at_watchpoint; 112 113 PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame)); 114 PythonObject wp_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_wp)); 115 PythonObject result = pfunc(frame_arg, wp_arg, dict); 116 117 if (result.get() == Py_False) 118 stop_at_watchpoint = false; 119 120 return stop_at_watchpoint; 121} 122 123SWIGEXPORT bool 124LLDBSwigPythonCallTypeScript 125( 126 const char *python_function_name, 127 const void *session_dictionary, 128 const lldb::ValueObjectSP& valobj_sp, 129 void** pyfunct_wrapper, 130 const lldb::TypeSummaryOptionsSP& options_sp, 131 std::string& retval 132) 133{ 134 lldb::SBValue sb_value (valobj_sp); 135 lldb::SBTypeSummaryOptions sb_options(options_sp.get()); 136 137 retval.clear(); 138 139 if (!python_function_name || !session_dictionary) 140 return false; 141 142 PyObject *pfunc_impl = nullptr; 143 144 if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper)) 145 { 146 pfunc_impl = (PyObject*)(*pyfunct_wrapper); 147 if (pfunc_impl->ob_refcnt == 1) 148 { 149 Py_XDECREF(pfunc_impl); 150 pfunc_impl = NULL; 151 } 152 } 153 154 PyObject *py_dict = (PyObject*)session_dictionary; 155 if (!PythonDictionary::Check(py_dict)) 156 return true; 157 158 PythonDictionary dict(PyRefType::Borrowed, py_dict); 159 160 PyErr_Cleaner pyerr_cleanup(true); // show Python errors 161 162 PythonCallable pfunc(PyRefType::Borrowed, pfunc_impl); 163 164 if (!pfunc.IsAllocated()) 165 { 166 pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 167 if (!pfunc.IsAllocated()) 168 return false; 169 170 if (pyfunct_wrapper) 171 { 172 *pyfunct_wrapper = pfunc.get(); 173 Py_XINCREF(pfunc.get()); 174 } 175 } 176 177 PythonObject result; 178 auto argc = pfunc.GetArgInfo(); 179 if (!argc) { 180 llvm::consumeError(argc.takeError()); 181 return false; 182 } 183 184 PythonObject value_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_value)); 185 PythonObject options_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_options)); 186 187 if (argc.get().max_positional_args < 3) 188 result = pfunc(value_arg,dict); 189 else 190 result = pfunc(value_arg,dict,options_arg); 191 192 retval = result.Str().GetString().str(); 193 194 return true; 195} 196 197SWIGEXPORT void* 198LLDBSwigPythonCreateSyntheticProvider 199( 200 const char *python_class_name, 201 const char *session_dictionary_name, 202 const lldb::ValueObjectSP& valobj_sp 203) 204{ 205 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) 206 Py_RETURN_NONE; 207 208 PyErr_Cleaner py_err_cleaner(true); 209 210 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 211 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name,dict); 212 213 if (!pfunc.IsAllocated()) 214 Py_RETURN_NONE; 215 216 // I do not want the SBValue to be deallocated when going out of scope because python 217 // has ownership of it and will manage memory for this object by itself 218 lldb::SBValue *sb_value = new lldb::SBValue(valobj_sp); 219 sb_value->SetPreferSyntheticValue(false); 220 221 PythonObject val_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_value)); 222 if (!val_arg.IsAllocated()) 223 Py_RETURN_NONE; 224 225 PythonObject result = pfunc(val_arg, dict); 226 227 if (result.IsAllocated()) 228 return result.release(); 229 230 Py_RETURN_NONE; 231} 232 233SWIGEXPORT void* 234LLDBSwigPythonCreateCommandObject 235( 236 const char *python_class_name, 237 const char *session_dictionary_name, 238 const lldb::DebuggerSP debugger_sp 239) 240{ 241 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) 242 Py_RETURN_NONE; 243 244 PyErr_Cleaner py_err_cleaner(true); 245 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 246 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict); 247 248 if (!pfunc.IsAllocated()) 249 return nullptr; 250 251 lldb::SBDebugger debugger_sb(debugger_sp); 252 PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); 253 PythonObject result = pfunc(debugger_arg, dict); 254 255 if (result.IsAllocated()) 256 return result.release(); 257 258 Py_RETURN_NONE; 259} 260 261SWIGEXPORT void* 262LLDBSwigPythonCreateScriptedThreadPlan 263( 264 const char *python_class_name, 265 const char *session_dictionary_name, 266 lldb_private::StructuredDataImpl *args_impl, 267 std::string &error_string, 268 const lldb::ThreadPlanSP& thread_plan_sp 269) 270{ 271 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) 272 Py_RETURN_NONE; 273 274 275 PyErr_Cleaner py_err_cleaner(true); 276 277 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 278 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict); 279 280 if (!pfunc.IsAllocated()) { 281 error_string.append("could not find script class: "); 282 error_string.append(python_class_name); 283 return nullptr; 284 } 285 286 // I do not want the SBThreadPlan to be deallocated when going out of scope 287 // because python has ownership of it and will manage memory for this 288 // object by itself 289 PythonObject tp_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBThreadPlan(thread_plan_sp))); 290 291 if (!tp_arg.IsAllocated()) 292 Py_RETURN_NONE; 293 294 llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo(); 295 if (!arg_info) { 296 llvm::handleAllErrors( 297 arg_info.takeError(), 298 [&](PythonException &E) { 299 error_string.append(E.ReadBacktrace()); 300 }, 301 [&](const llvm::ErrorInfoBase &E) { 302 error_string.append(E.message()); 303 }); 304 Py_RETURN_NONE; 305 } 306 307 PythonObject result = {}; 308 if (arg_info.get().max_positional_args == 2) { 309 if (args_impl != nullptr) { 310 error_string.assign("args passed, but __init__ does not take an args dictionary"); 311 Py_RETURN_NONE; 312 } 313 result = pfunc(tp_arg, dict); 314 } else if (arg_info.get().max_positional_args >= 3) { 315 PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBStructuredData(args_impl))); 316 result = pfunc(tp_arg, args_arg, dict); 317 } else { 318 error_string.assign("wrong number of arguments in __init__, should be 2 or 3 (not including self)"); 319 Py_RETURN_NONE; 320 } 321 322 // FIXME: At this point we should check that the class we found supports all the methods 323 // that we need. 324 325 if (result.IsAllocated()) 326 return result.release(); 327 Py_RETURN_NONE; 328} 329 330SWIGEXPORT bool 331LLDBSWIGPythonCallThreadPlan 332( 333 void *implementor, 334 const char *method_name, 335 lldb_private::Event *event, 336 bool &got_error 337) 338{ 339 got_error = false; 340 341 PyErr_Cleaner py_err_cleaner(false); 342 PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor)); 343 auto pfunc = self.ResolveName<PythonCallable>(method_name); 344 345 if (!pfunc.IsAllocated()) 346 return false; 347 348 PythonObject result; 349 if (event != nullptr) 350 { 351 lldb::SBEvent sb_event(event); 352 PythonObject event_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_event)); 353 result = pfunc(event_arg); 354 } 355 else 356 result = pfunc(); 357 358 if (PyErr_Occurred()) 359 { 360 got_error = true; 361 printf ("Return value was neither false nor true for call to %s.\n", method_name); 362 PyErr_Print(); 363 return false; 364 } 365 366 if (result.get() == Py_True) 367 return true; 368 else if (result.get() == Py_False) 369 return false; 370 371 // Somebody returned the wrong thing... 372 got_error = true; 373 printf ("Wrong return value type for call to %s.\n", method_name); 374 return false; 375} 376 377SWIGEXPORT void * 378LLDBSwigPythonCreateScriptedBreakpointResolver 379( 380 const char *python_class_name, 381 const char *session_dictionary_name, 382 lldb_private::StructuredDataImpl *args_impl, 383 lldb::BreakpointSP &breakpoint_sp 384) 385{ 386 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) 387 Py_RETURN_NONE; 388 389 PyErr_Cleaner py_err_cleaner(true); 390 391 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 392 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict); 393 394 if (!pfunc.IsAllocated()) 395 return nullptr; 396 397 lldb::SBBreakpoint *bkpt_value = new lldb::SBBreakpoint(breakpoint_sp); 398 399 PythonObject bkpt_arg(PyRefType::Owned, SBTypeToSWIGWrapper(bkpt_value)); 400 401 lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl); 402 PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(args_value)); 403 404 PythonObject result = pfunc(bkpt_arg, args_arg, dict); 405 // FIXME: At this point we should check that the class we found supports all the methods 406 // that we need. 407 408 if (result.IsAllocated()) 409 { 410 // Check that __callback__ is defined: 411 auto callback_func = result.ResolveName<PythonCallable>("__callback__"); 412 if (callback_func.IsAllocated()) 413 return result.release(); 414 else 415 result.release(); 416 } 417 Py_RETURN_NONE; 418} 419 420SWIGEXPORT unsigned int 421LLDBSwigPythonCallBreakpointResolver 422( 423 void *implementor, 424 const char *method_name, 425 lldb_private::SymbolContext *sym_ctx 426) 427{ 428 PyErr_Cleaner py_err_cleaner(false); 429 PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor)); 430 auto pfunc = self.ResolveName<PythonCallable>(method_name); 431 432 if (!pfunc.IsAllocated()) 433 return 0; 434 435 PythonObject result; 436 if (sym_ctx != nullptr) { 437 lldb::SBSymbolContext sb_sym_ctx(sym_ctx); 438 PythonObject sym_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_sym_ctx)); 439 result = pfunc(sym_ctx_arg); 440 } else 441 result = pfunc(); 442 443 if (PyErr_Occurred()) 444 { 445 PyErr_Print(); 446 PyErr_Clear(); 447 return 0; 448 } 449 450 // The callback will return a bool, but we're need to also return ints 451 // so we're squirrelling the bool through as an int... And if you return 452 // nothing, we'll continue. 453 if (strcmp(method_name, "__callback__") == 0) { 454 if (result.get() == Py_False) 455 return 0; 456 else 457 return 1; 458 } 459 460 long long ret_val = unwrapOrSetPythonException(As<long long>(result)); 461 462 if (PyErr_Occurred()) { 463 PyErr_Print(); 464 PyErr_Clear(); 465 return 0; 466 } 467 468 return ret_val; 469} 470 471// wrapper that calls an optional instance member of an object taking no arguments 472static PyObject* 473LLDBSwigPython_CallOptionalMember 474( 475 PyObject* implementor, 476 char* callee_name, 477 PyObject* ret_if_not_found = Py_None, 478 bool* was_found = NULL 479) 480{ 481 PyErr_Cleaner py_err_cleaner(false); 482 483 PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor)); 484 auto pfunc = self.ResolveName<PythonCallable>(callee_name); 485 486 if (!pfunc.IsAllocated()) 487 { 488 if (was_found) 489 *was_found = false; 490 Py_XINCREF(ret_if_not_found); 491 return ret_if_not_found; 492 } 493 494 if (was_found) 495 *was_found = true; 496 497 PythonObject result = pfunc(); 498 return result.release(); 499} 500 501SWIGEXPORT size_t 502LLDBSwigPython_CalculateNumChildren 503( 504 PyObject *implementor, 505 uint32_t max 506) 507{ 508 PythonObject self(PyRefType::Borrowed, implementor); 509 auto pfunc = self.ResolveName<PythonCallable>("num_children"); 510 511 if (!pfunc.IsAllocated()) 512 return 0; 513 514 auto arg_info = pfunc.GetArgInfo(); 515 if (!arg_info) { 516 llvm::consumeError(arg_info.takeError()); 517 return 0; 518 } 519 520 size_t ret_val; 521 if (arg_info.get().max_positional_args < 1) 522 ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call())); 523 else 524 ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call(PythonInteger(max)))); 525 526 if (PyErr_Occurred()) 527 { 528 PyErr_Print(); 529 PyErr_Clear(); 530 return 0; 531 } 532 533 if (arg_info.get().max_positional_args < 1) 534 ret_val = std::min(ret_val, static_cast<size_t>(max)); 535 536 return ret_val; 537} 538 539SWIGEXPORT PyObject* 540LLDBSwigPython_GetChildAtIndex 541( 542 PyObject *implementor, 543 uint32_t idx 544) 545{ 546 PyErr_Cleaner py_err_cleaner(true); 547 548 PythonObject self(PyRefType::Borrowed, implementor); 549 auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index"); 550 551 if (!pfunc.IsAllocated()) 552 return nullptr; 553 554 PythonObject result = pfunc(PythonInteger(idx)); 555 556 if (!result.IsAllocated()) 557 return nullptr; 558 559 lldb::SBValue* sbvalue_ptr = nullptr; 560 if (SWIG_ConvertPtr(result.get(), (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1) 561 return nullptr; 562 563 if (sbvalue_ptr == nullptr) 564 return nullptr; 565 566 return result.release(); 567} 568 569SWIGEXPORT int 570LLDBSwigPython_GetIndexOfChildWithName 571( 572 PyObject *implementor, 573 const char* child_name 574) 575{ 576 PyErr_Cleaner py_err_cleaner(true); 577 578 PythonObject self(PyRefType::Borrowed, implementor); 579 auto pfunc = self.ResolveName<PythonCallable>("get_child_index"); 580 581 if (!pfunc.IsAllocated()) 582 return UINT32_MAX; 583 584 llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name)); 585 586 long long retval = unwrapOrSetPythonException(As<long long>(std::move(result))); 587 588 if (PyErr_Occurred()) { 589 PyErr_Clear(); // FIXME print this? do something else 590 return UINT32_MAX; 591 } 592 593 if (retval >= 0) 594 return (uint32_t)retval; 595 596 return UINT32_MAX; 597} 598 599SWIGEXPORT bool 600LLDBSwigPython_UpdateSynthProviderInstance 601( 602 PyObject *implementor 603) 604{ 605 bool ret_val = false; 606 607 static char callee_name[] = "update"; 608 609 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name); 610 611 if (py_return == Py_True) 612 ret_val = true; 613 614 Py_XDECREF(py_return); 615 616 return ret_val; 617} 618 619SWIGEXPORT bool 620LLDBSwigPython_MightHaveChildrenSynthProviderInstance 621( 622 PyObject *implementor 623) 624{ 625 bool ret_val = false; 626 627 static char callee_name[] = "has_children"; 628 629 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True); 630 631 if (py_return == Py_True) 632 ret_val = true; 633 634 Py_XDECREF(py_return); 635 636 return ret_val; 637} 638 639SWIGEXPORT PyObject* 640LLDBSwigPython_GetValueSynthProviderInstance 641( 642 PyObject *implementor 643) 644{ 645 PyObject* ret_val = nullptr; 646 647 static char callee_name[] = "get_value"; 648 649 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_None); 650 651 if (py_return == Py_None || py_return == nullptr) 652 ret_val = nullptr; 653 654 lldb::SBValue* sbvalue_ptr = NULL; 655 656 if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1) 657 ret_val = nullptr; 658 else if (sbvalue_ptr == NULL) 659 ret_val = nullptr; 660 else 661 ret_val = py_return; 662 663 Py_XDECREF(py_return); 664 return ret_val; 665} 666 667SWIGEXPORT void* 668LLDBSWIGPython_CastPyObjectToSBValue 669( 670 PyObject* data 671) 672{ 673 lldb::SBValue* sb_ptr = NULL; 674 675 int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0); 676 677 if (valid_cast == -1) 678 return NULL; 679 680 return sb_ptr; 681} 682 683SWIGEXPORT bool 684LLDBSwigPythonCallCommand 685( 686 const char *python_function_name, 687 const char *session_dictionary_name, 688 lldb::DebuggerSP& debugger, 689 const char* args, 690 lldb_private::CommandReturnObject& cmd_retobj, 691 lldb::ExecutionContextRefSP exe_ctx_ref_sp 692) 693{ 694 lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj); 695 lldb::SBDebugger debugger_sb(debugger); 696 lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp); 697 698 PyErr_Cleaner py_err_cleaner(true); 699 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 700 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 701 702 if (!pfunc.IsAllocated()) 703 return false; 704 705 // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you 706 // see comment above for SBCommandReturnObjectReleaser for further details 707 auto argc = pfunc.GetArgInfo(); 708 if (!argc) { 709 llvm::consumeError(argc.takeError()); 710 return false; 711 } 712 PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); 713 PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb)); 714 PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(&cmd_retobj_sb)); 715 716 if (argc.get().max_positional_args < 5u) 717 pfunc(debugger_arg, PythonString(args), cmd_retobj_arg, dict); 718 else 719 pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg, dict); 720 721 return true; 722} 723 724SWIGEXPORT bool 725LLDBSwigPythonCallCommandObject 726( 727 PyObject *implementor, 728 lldb::DebuggerSP& debugger, 729 const char* args, 730 lldb_private::CommandReturnObject& cmd_retobj, 731 lldb::ExecutionContextRefSP exe_ctx_ref_sp 732) 733{ 734 lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj); 735 lldb::SBDebugger debugger_sb(debugger); 736 lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp); 737 738 PyErr_Cleaner py_err_cleaner(true); 739 740 PythonObject self(PyRefType::Borrowed, implementor); 741 auto pfunc = self.ResolveName<PythonCallable>("__call__"); 742 743 if (!pfunc.IsAllocated()) 744 return false; 745 746 // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you 747 // see comment above for SBCommandReturnObjectReleaser for further details 748 PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); 749 PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb)); 750 PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(&cmd_retobj_sb)); 751 752 pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg); 753 754 return true; 755} 756 757SWIGEXPORT void* 758LLDBSWIGPythonCreateOSPlugin 759( 760 const char *python_class_name, 761 const char *session_dictionary_name, 762 const lldb::ProcessSP& process_sp 763) 764{ 765 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) 766 Py_RETURN_NONE; 767 768 PyErr_Cleaner py_err_cleaner(true); 769 770 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 771 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict); 772 773 if (!pfunc.IsAllocated()) 774 Py_RETURN_NONE; 775 776 // I do not want the SBProcess to be deallocated when going out of scope because python 777 // has ownership of it and will manage memory for this object by itself 778 lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp); 779 PythonObject process_arg(PyRefType::Owned, SBTypeToSWIGWrapper(process_sb)); 780 if (!process_arg.IsAllocated()) 781 Py_RETURN_NONE; 782 783 auto result = pfunc(process_arg); 784 785 if (result.IsAllocated()) 786 return result.release(); 787 788 Py_RETURN_NONE; 789} 790 791SWIGEXPORT void* 792LLDBSWIGPython_CreateFrameRecognizer 793( 794 const char *python_class_name, 795 const char *session_dictionary_name 796) 797{ 798 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) 799 Py_RETURN_NONE; 800 801 PyErr_Cleaner py_err_cleaner(true); 802 803 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 804 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict); 805 806 if (!pfunc.IsAllocated()) 807 Py_RETURN_NONE; 808 809 auto result = pfunc(); 810 811 if (result.IsAllocated()) 812 return result.release(); 813 814 Py_RETURN_NONE; 815} 816 817SWIGEXPORT PyObject* 818LLDBSwigPython_GetRecognizedArguments 819( 820 PyObject *implementor, 821 const lldb::StackFrameSP& frame_sp 822) 823{ 824 static char callee_name[] = "get_recognized_arguments"; 825 826 lldb::SBFrame frame_sb(frame_sp); 827 PyObject *arg = SBTypeToSWIGWrapper(frame_sb); 828 829 PythonString str(callee_name); 830 PyObject* result = PyObject_CallMethodObjArgs(implementor, str.get(), arg, 831 NULL); 832 return result; 833} 834 835SWIGEXPORT void* 836LLDBSWIGPython_GetDynamicSetting (void* module, const char* setting, const lldb::TargetSP& target_sp) 837{ 838 if (!module || !setting) 839 Py_RETURN_NONE; 840 841 PyErr_Cleaner py_err_cleaner(true); 842 PythonObject py_module(PyRefType::Borrowed, (PyObject *)module); 843 auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting"); 844 845 if (!pfunc.IsAllocated()) 846 Py_RETURN_NONE; 847 848 lldb::SBTarget target_sb(target_sp); 849 PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(target_sb)); 850 auto result = pfunc(target_arg, PythonString(setting)); 851 852 return result.release(); 853} 854 855SWIGEXPORT bool 856LLDBSWIGPythonRunScriptKeywordProcess 857(const char* python_function_name, 858const char* session_dictionary_name, 859lldb::ProcessSP& process, 860std::string& output) 861 862{ 863 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) 864 return false; 865 866 PyErr_Cleaner py_err_cleaner(true); 867 868 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 869 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 870 871 if (!pfunc.IsAllocated()) 872 return false; 873 874 lldb::SBProcess process_sb(process); 875 PythonObject process_arg(PyRefType::Owned, SBTypeToSWIGWrapper(process_sb)); 876 auto result = pfunc(process_arg, dict); 877 878 output = result.Str().GetString().str(); 879 880 return true; 881} 882 883SWIGEXPORT bool 884LLDBSWIGPythonRunScriptKeywordThread 885(const char* python_function_name, 886const char* session_dictionary_name, 887lldb::ThreadSP& thread, 888std::string& output) 889 890{ 891 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) 892 return false; 893 894 PyErr_Cleaner py_err_cleaner(true); 895 896 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 897 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 898 899 if (!pfunc.IsAllocated()) 900 return false; 901 902 lldb::SBThread thread_sb(thread); 903 PythonObject thread_arg(PyRefType::Owned, SBTypeToSWIGWrapper(thread_sb)); 904 auto result = pfunc(thread_arg, dict); 905 906 output = result.Str().GetString().str(); 907 908 return true; 909} 910 911SWIGEXPORT bool 912LLDBSWIGPythonRunScriptKeywordTarget 913(const char* python_function_name, 914const char* session_dictionary_name, 915lldb::TargetSP& target, 916std::string& output) 917 918{ 919 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) 920 return false; 921 922 PyErr_Cleaner py_err_cleaner(true); 923 924 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 925 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name,dict); 926 927 if (!pfunc.IsAllocated()) 928 return false; 929 930 lldb::SBTarget target_sb(target); 931 PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(target_sb)); 932 auto result = pfunc(target_arg, dict); 933 934 output = result.Str().GetString().str(); 935 936 return true; 937} 938 939SWIGEXPORT bool 940LLDBSWIGPythonRunScriptKeywordFrame 941(const char* python_function_name, 942const char* session_dictionary_name, 943lldb::StackFrameSP& frame, 944std::string& output) 945 946{ 947 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) 948 return false; 949 950 PyErr_Cleaner py_err_cleaner(true); 951 952 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 953 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name,dict); 954 955 if (!pfunc.IsAllocated()) 956 return false; 957 958 lldb::SBFrame frame_sb(frame); 959 PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(frame_sb)); 960 auto result = pfunc(frame_arg, dict); 961 962 output = result.Str().GetString().str(); 963 964 return true; 965} 966 967SWIGEXPORT bool 968LLDBSWIGPythonRunScriptKeywordValue 969(const char* python_function_name, 970const char* session_dictionary_name, 971lldb::ValueObjectSP& value, 972std::string& output) 973 974{ 975 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) 976 return false; 977 978 PyErr_Cleaner py_err_cleaner(true); 979 980 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 981 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 982 983 if (!pfunc.IsAllocated()) 984 return false; 985 986 lldb::SBValue value_sb(value); 987 PythonObject value_arg(PyRefType::Owned, SBTypeToSWIGWrapper(value_sb)); 988 auto result = pfunc(value_arg, dict); 989 990 output = result.Str().GetString().str(); 991 992 return true; 993} 994 995SWIGEXPORT bool 996LLDBSwigPythonCallModuleInit 997( 998 const char *python_module_name, 999 const char *session_dictionary_name, 1000 lldb::DebuggerSP& debugger 1001) 1002{ 1003 std::string python_function_name_string = python_module_name; 1004 python_function_name_string += ".__lldb_init_module"; 1005 const char* python_function_name = python_function_name_string.c_str(); 1006 1007 PyErr_Cleaner py_err_cleaner(true); 1008 1009 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 1010 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 1011 1012 // This method is optional and need not exist. So if we don't find it, 1013 // it's actually a success, not a failure. 1014 if (!pfunc.IsAllocated()) 1015 return true; 1016 1017 lldb::SBDebugger debugger_sb(debugger); 1018 PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); 1019 pfunc(debugger_arg, dict); 1020 1021 return true; 1022} 1023%} 1024 1025 1026%runtime %{ 1027// Forward declaration to be inserted at the start of LLDBWrapPython.h 1028#include "lldb/API/SBDebugger.h" 1029#include "lldb/API/SBValue.h" 1030 1031SWIGEXPORT lldb::ValueObjectSP 1032LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data) 1033{ 1034 lldb::ValueObjectSP valobj_sp; 1035 if (data) 1036 { 1037 lldb::SBValue* sb_ptr = (lldb::SBValue *)data; 1038 valobj_sp = sb_ptr->GetSP(); 1039 } 1040 return valobj_sp; 1041} 1042 1043#ifdef __cplusplus 1044extern "C" { 1045#endif 1046 1047void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton); 1048 1049#ifdef __cplusplus 1050} 1051#endif 1052%} 1053 1054%wrapper %{ 1055 1056 1057// For the LogOutputCallback functions 1058void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) { 1059 if (baton != Py_None) { 1060 SWIG_PYTHON_THREAD_BEGIN_BLOCK; 1061 PyObject *result = PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str); 1062 Py_XDECREF(result); 1063 SWIG_PYTHON_THREAD_END_BLOCK; 1064 } 1065} 1066%} 1067