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 471SWIGEXPORT void * 472LLDBSwigPythonCreateScriptedStopHook 473( 474 lldb::TargetSP target_sp, 475 const char *python_class_name, 476 const char *session_dictionary_name, 477 lldb_private::StructuredDataImpl *args_impl, 478 Status &error 479) 480{ 481 if (python_class_name == NULL || python_class_name[0] == '\0') { 482 error.SetErrorString("Empty class name."); 483 Py_RETURN_NONE; 484 } 485 if (!session_dictionary_name) { 486 error.SetErrorString("No session dictionary"); 487 Py_RETURN_NONE; 488 } 489 490 PyErr_Cleaner py_err_cleaner(true); 491 492 auto dict = 493 PythonModule::MainModule().ResolveName<PythonDictionary>( 494 session_dictionary_name); 495 auto pfunc = 496 PythonObject::ResolveNameWithDictionary<PythonCallable>( 497 python_class_name, dict); 498 499 if (!pfunc.IsAllocated()) { 500 error.SetErrorStringWithFormat("Could not find class: %s.", 501 python_class_name); 502 return nullptr; 503 } 504 505 lldb::SBTarget *target_val 506 = new lldb::SBTarget(target_sp); 507 508 PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(target_val)); 509 510 lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl); 511 PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(args_value)); 512 513 PythonObject result = pfunc(target_arg, args_arg, dict); 514 515 if (result.IsAllocated()) 516 { 517 // Check that the handle_stop callback is defined: 518 auto callback_func = result.ResolveName<PythonCallable>("handle_stop"); 519 if (callback_func.IsAllocated()) { 520 if (auto args_info = callback_func.GetArgInfo()) { 521 if ((*args_info).max_positional_args < 2) { 522 error.SetErrorStringWithFormat("Wrong number of args for " 523 "handle_stop callback, should be 2 (excluding self), got: %d", 524 (*args_info).max_positional_args); 525 } else 526 return result.release(); 527 } else { 528 error.SetErrorString("Couldn't get num arguments for handle_stop " 529 "callback."); 530 } 531 return result.release(); 532 } 533 else { 534 error.SetErrorStringWithFormat("Class \"%s\" is missing the required " 535 "handle_stop callback.", 536 python_class_name); 537 result.release(); 538 } 539 } 540 Py_RETURN_NONE; 541} 542 543SWIGEXPORT bool 544LLDBSwigPythonStopHookCallHandleStop 545( 546 void *implementor, 547 lldb::ExecutionContextRefSP exc_ctx_sp, 548 lldb::StreamSP stream 549) 550{ 551 // handle_stop will return a bool with the meaning "should_stop"... 552 // If you return nothing we'll assume we are going to stop. 553 // Also any errors should return true, since we should stop on error. 554 555 PyErr_Cleaner py_err_cleaner(false); 556 PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor)); 557 auto pfunc = self.ResolveName<PythonCallable>("handle_stop"); 558 559 if (!pfunc.IsAllocated()) 560 return true; 561 562 PythonObject result; 563 lldb::SBExecutionContext sb_exc_ctx(exc_ctx_sp); 564 PythonObject exc_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_exc_ctx)); 565 lldb::SBStream sb_stream; 566 PythonObject sb_stream_arg(PyRefType::Owned, 567 SBTypeToSWIGWrapper(sb_stream)); 568 result = pfunc(exc_ctx_arg, sb_stream_arg); 569 570 if (PyErr_Occurred()) 571 { 572 stream->PutCString("Python error occurred handling stop-hook."); 573 PyErr_Print(); 574 PyErr_Clear(); 575 return true; 576 } 577 578 // Now add the result to the output stream. SBStream only 579 // makes an internally help StreamString which I can't interpose, so I 580 // have to copy it over here. 581 stream->PutCString(sb_stream.GetData()); 582 583 if (result.get() == Py_False) 584 return false; 585 else 586 return true; 587} 588 589// wrapper that calls an optional instance member of an object taking no arguments 590static PyObject* 591LLDBSwigPython_CallOptionalMember 592( 593 PyObject* implementor, 594 char* callee_name, 595 PyObject* ret_if_not_found = Py_None, 596 bool* was_found = NULL 597) 598{ 599 PyErr_Cleaner py_err_cleaner(false); 600 601 PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor)); 602 auto pfunc = self.ResolveName<PythonCallable>(callee_name); 603 604 if (!pfunc.IsAllocated()) 605 { 606 if (was_found) 607 *was_found = false; 608 Py_XINCREF(ret_if_not_found); 609 return ret_if_not_found; 610 } 611 612 if (was_found) 613 *was_found = true; 614 615 PythonObject result = pfunc(); 616 return result.release(); 617} 618 619SWIGEXPORT size_t 620LLDBSwigPython_CalculateNumChildren 621( 622 PyObject *implementor, 623 uint32_t max 624) 625{ 626 PythonObject self(PyRefType::Borrowed, implementor); 627 auto pfunc = self.ResolveName<PythonCallable>("num_children"); 628 629 if (!pfunc.IsAllocated()) 630 return 0; 631 632 auto arg_info = pfunc.GetArgInfo(); 633 if (!arg_info) { 634 llvm::consumeError(arg_info.takeError()); 635 return 0; 636 } 637 638 size_t ret_val; 639 if (arg_info.get().max_positional_args < 1) 640 ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call())); 641 else 642 ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call(PythonInteger(max)))); 643 644 if (PyErr_Occurred()) 645 { 646 PyErr_Print(); 647 PyErr_Clear(); 648 return 0; 649 } 650 651 if (arg_info.get().max_positional_args < 1) 652 ret_val = std::min(ret_val, static_cast<size_t>(max)); 653 654 return ret_val; 655} 656 657SWIGEXPORT PyObject* 658LLDBSwigPython_GetChildAtIndex 659( 660 PyObject *implementor, 661 uint32_t idx 662) 663{ 664 PyErr_Cleaner py_err_cleaner(true); 665 666 PythonObject self(PyRefType::Borrowed, implementor); 667 auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index"); 668 669 if (!pfunc.IsAllocated()) 670 return nullptr; 671 672 PythonObject result = pfunc(PythonInteger(idx)); 673 674 if (!result.IsAllocated()) 675 return nullptr; 676 677 lldb::SBValue* sbvalue_ptr = nullptr; 678 if (SWIG_ConvertPtr(result.get(), (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1) 679 return nullptr; 680 681 if (sbvalue_ptr == nullptr) 682 return nullptr; 683 684 return result.release(); 685} 686 687SWIGEXPORT int 688LLDBSwigPython_GetIndexOfChildWithName 689( 690 PyObject *implementor, 691 const char* child_name 692) 693{ 694 PyErr_Cleaner py_err_cleaner(true); 695 696 PythonObject self(PyRefType::Borrowed, implementor); 697 auto pfunc = self.ResolveName<PythonCallable>("get_child_index"); 698 699 if (!pfunc.IsAllocated()) 700 return UINT32_MAX; 701 702 llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name)); 703 704 long long retval = unwrapOrSetPythonException(As<long long>(std::move(result))); 705 706 if (PyErr_Occurred()) { 707 PyErr_Clear(); // FIXME print this? do something else 708 return UINT32_MAX; 709 } 710 711 if (retval >= 0) 712 return (uint32_t)retval; 713 714 return UINT32_MAX; 715} 716 717SWIGEXPORT bool 718LLDBSwigPython_UpdateSynthProviderInstance 719( 720 PyObject *implementor 721) 722{ 723 bool ret_val = false; 724 725 static char callee_name[] = "update"; 726 727 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name); 728 729 if (py_return == Py_True) 730 ret_val = true; 731 732 Py_XDECREF(py_return); 733 734 return ret_val; 735} 736 737SWIGEXPORT bool 738LLDBSwigPython_MightHaveChildrenSynthProviderInstance 739( 740 PyObject *implementor 741) 742{ 743 bool ret_val = false; 744 745 static char callee_name[] = "has_children"; 746 747 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True); 748 749 if (py_return == Py_True) 750 ret_val = true; 751 752 Py_XDECREF(py_return); 753 754 return ret_val; 755} 756 757SWIGEXPORT PyObject* 758LLDBSwigPython_GetValueSynthProviderInstance 759( 760 PyObject *implementor 761) 762{ 763 PyObject* ret_val = nullptr; 764 765 static char callee_name[] = "get_value"; 766 767 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_None); 768 769 if (py_return == Py_None || py_return == nullptr) 770 ret_val = nullptr; 771 772 lldb::SBValue* sbvalue_ptr = NULL; 773 774 if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1) 775 ret_val = nullptr; 776 else if (sbvalue_ptr == NULL) 777 ret_val = nullptr; 778 else 779 ret_val = py_return; 780 781 Py_XDECREF(py_return); 782 return ret_val; 783} 784 785SWIGEXPORT void* 786LLDBSWIGPython_CastPyObjectToSBValue 787( 788 PyObject* data 789) 790{ 791 lldb::SBValue* sb_ptr = NULL; 792 793 int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0); 794 795 if (valid_cast == -1) 796 return NULL; 797 798 return sb_ptr; 799} 800 801SWIGEXPORT bool 802LLDBSwigPythonCallCommand 803( 804 const char *python_function_name, 805 const char *session_dictionary_name, 806 lldb::DebuggerSP& debugger, 807 const char* args, 808 lldb_private::CommandReturnObject& cmd_retobj, 809 lldb::ExecutionContextRefSP exe_ctx_ref_sp 810) 811{ 812 lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj); 813 lldb::SBDebugger debugger_sb(debugger); 814 lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp); 815 816 PyErr_Cleaner py_err_cleaner(true); 817 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 818 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 819 820 if (!pfunc.IsAllocated()) 821 return false; 822 823 // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you 824 // see comment above for SBCommandReturnObjectReleaser for further details 825 auto argc = pfunc.GetArgInfo(); 826 if (!argc) { 827 llvm::consumeError(argc.takeError()); 828 return false; 829 } 830 PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); 831 PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb)); 832 PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(&cmd_retobj_sb)); 833 834 if (argc.get().max_positional_args < 5u) 835 pfunc(debugger_arg, PythonString(args), cmd_retobj_arg, dict); 836 else 837 pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg, dict); 838 839 return true; 840} 841 842SWIGEXPORT bool 843LLDBSwigPythonCallCommandObject 844( 845 PyObject *implementor, 846 lldb::DebuggerSP& debugger, 847 const char* args, 848 lldb_private::CommandReturnObject& cmd_retobj, 849 lldb::ExecutionContextRefSP exe_ctx_ref_sp 850) 851{ 852 lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj); 853 lldb::SBDebugger debugger_sb(debugger); 854 lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp); 855 856 PyErr_Cleaner py_err_cleaner(true); 857 858 PythonObject self(PyRefType::Borrowed, implementor); 859 auto pfunc = self.ResolveName<PythonCallable>("__call__"); 860 861 if (!pfunc.IsAllocated()) 862 return false; 863 864 // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you 865 // see comment above for SBCommandReturnObjectReleaser for further details 866 PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); 867 PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb)); 868 PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(&cmd_retobj_sb)); 869 870 pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg); 871 872 return true; 873} 874 875SWIGEXPORT void* 876LLDBSWIGPythonCreateOSPlugin 877( 878 const char *python_class_name, 879 const char *session_dictionary_name, 880 const lldb::ProcessSP& process_sp 881) 882{ 883 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) 884 Py_RETURN_NONE; 885 886 PyErr_Cleaner py_err_cleaner(true); 887 888 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 889 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict); 890 891 if (!pfunc.IsAllocated()) 892 Py_RETURN_NONE; 893 894 // I do not want the SBProcess to be deallocated when going out of scope because python 895 // has ownership of it and will manage memory for this object by itself 896 lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp); 897 PythonObject process_arg(PyRefType::Owned, SBTypeToSWIGWrapper(process_sb)); 898 if (!process_arg.IsAllocated()) 899 Py_RETURN_NONE; 900 901 auto result = pfunc(process_arg); 902 903 if (result.IsAllocated()) 904 return result.release(); 905 906 Py_RETURN_NONE; 907} 908 909SWIGEXPORT void* 910LLDBSWIGPython_CreateFrameRecognizer 911( 912 const char *python_class_name, 913 const char *session_dictionary_name 914) 915{ 916 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) 917 Py_RETURN_NONE; 918 919 PyErr_Cleaner py_err_cleaner(true); 920 921 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 922 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict); 923 924 if (!pfunc.IsAllocated()) 925 Py_RETURN_NONE; 926 927 auto result = pfunc(); 928 929 if (result.IsAllocated()) 930 return result.release(); 931 932 Py_RETURN_NONE; 933} 934 935SWIGEXPORT PyObject* 936LLDBSwigPython_GetRecognizedArguments 937( 938 PyObject *implementor, 939 const lldb::StackFrameSP& frame_sp 940) 941{ 942 static char callee_name[] = "get_recognized_arguments"; 943 944 lldb::SBFrame frame_sb(frame_sp); 945 PyObject *arg = SBTypeToSWIGWrapper(frame_sb); 946 947 PythonString str(callee_name); 948 PyObject* result = PyObject_CallMethodObjArgs(implementor, str.get(), arg, 949 NULL); 950 return result; 951} 952 953SWIGEXPORT void* 954LLDBSWIGPython_GetDynamicSetting (void* module, const char* setting, const lldb::TargetSP& target_sp) 955{ 956 if (!module || !setting) 957 Py_RETURN_NONE; 958 959 PyErr_Cleaner py_err_cleaner(true); 960 PythonObject py_module(PyRefType::Borrowed, (PyObject *)module); 961 auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting"); 962 963 if (!pfunc.IsAllocated()) 964 Py_RETURN_NONE; 965 966 lldb::SBTarget target_sb(target_sp); 967 PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(target_sb)); 968 auto result = pfunc(target_arg, PythonString(setting)); 969 970 return result.release(); 971} 972 973SWIGEXPORT bool 974LLDBSWIGPythonRunScriptKeywordProcess 975(const char* python_function_name, 976const char* session_dictionary_name, 977lldb::ProcessSP& process, 978std::string& output) 979 980{ 981 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) 982 return false; 983 984 PyErr_Cleaner py_err_cleaner(true); 985 986 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 987 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 988 989 if (!pfunc.IsAllocated()) 990 return false; 991 992 lldb::SBProcess process_sb(process); 993 PythonObject process_arg(PyRefType::Owned, SBTypeToSWIGWrapper(process_sb)); 994 auto result = pfunc(process_arg, dict); 995 996 output = result.Str().GetString().str(); 997 998 return true; 999} 1000 1001SWIGEXPORT bool 1002LLDBSWIGPythonRunScriptKeywordThread 1003(const char* python_function_name, 1004const char* session_dictionary_name, 1005lldb::ThreadSP& thread, 1006std::string& output) 1007 1008{ 1009 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) 1010 return false; 1011 1012 PyErr_Cleaner py_err_cleaner(true); 1013 1014 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 1015 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 1016 1017 if (!pfunc.IsAllocated()) 1018 return false; 1019 1020 lldb::SBThread thread_sb(thread); 1021 PythonObject thread_arg(PyRefType::Owned, SBTypeToSWIGWrapper(thread_sb)); 1022 auto result = pfunc(thread_arg, dict); 1023 1024 output = result.Str().GetString().str(); 1025 1026 return true; 1027} 1028 1029SWIGEXPORT bool 1030LLDBSWIGPythonRunScriptKeywordTarget 1031(const char* python_function_name, 1032const char* session_dictionary_name, 1033lldb::TargetSP& target, 1034std::string& output) 1035 1036{ 1037 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) 1038 return false; 1039 1040 PyErr_Cleaner py_err_cleaner(true); 1041 1042 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 1043 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name,dict); 1044 1045 if (!pfunc.IsAllocated()) 1046 return false; 1047 1048 lldb::SBTarget target_sb(target); 1049 PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(target_sb)); 1050 auto result = pfunc(target_arg, dict); 1051 1052 output = result.Str().GetString().str(); 1053 1054 return true; 1055} 1056 1057SWIGEXPORT bool 1058LLDBSWIGPythonRunScriptKeywordFrame 1059(const char* python_function_name, 1060const char* session_dictionary_name, 1061lldb::StackFrameSP& frame, 1062std::string& output) 1063 1064{ 1065 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) 1066 return false; 1067 1068 PyErr_Cleaner py_err_cleaner(true); 1069 1070 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 1071 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name,dict); 1072 1073 if (!pfunc.IsAllocated()) 1074 return false; 1075 1076 lldb::SBFrame frame_sb(frame); 1077 PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(frame_sb)); 1078 auto result = pfunc(frame_arg, dict); 1079 1080 output = result.Str().GetString().str(); 1081 1082 return true; 1083} 1084 1085SWIGEXPORT bool 1086LLDBSWIGPythonRunScriptKeywordValue 1087(const char* python_function_name, 1088const char* session_dictionary_name, 1089lldb::ValueObjectSP& value, 1090std::string& output) 1091 1092{ 1093 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) 1094 return false; 1095 1096 PyErr_Cleaner py_err_cleaner(true); 1097 1098 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 1099 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 1100 1101 if (!pfunc.IsAllocated()) 1102 return false; 1103 1104 lldb::SBValue value_sb(value); 1105 PythonObject value_arg(PyRefType::Owned, SBTypeToSWIGWrapper(value_sb)); 1106 auto result = pfunc(value_arg, dict); 1107 1108 output = result.Str().GetString().str(); 1109 1110 return true; 1111} 1112 1113SWIGEXPORT bool 1114LLDBSwigPythonCallModuleInit 1115( 1116 const char *python_module_name, 1117 const char *session_dictionary_name, 1118 lldb::DebuggerSP& debugger 1119) 1120{ 1121 std::string python_function_name_string = python_module_name; 1122 python_function_name_string += ".__lldb_init_module"; 1123 const char* python_function_name = python_function_name_string.c_str(); 1124 1125 PyErr_Cleaner py_err_cleaner(true); 1126 1127 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 1128 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 1129 1130 // This method is optional and need not exist. So if we don't find it, 1131 // it's actually a success, not a failure. 1132 if (!pfunc.IsAllocated()) 1133 return true; 1134 1135 lldb::SBDebugger debugger_sb(debugger); 1136 PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); 1137 pfunc(debugger_arg, dict); 1138 1139 return true; 1140} 1141%} 1142 1143 1144%runtime %{ 1145// Forward declaration to be inserted at the start of LLDBWrapPython.h 1146#include "lldb/API/SBDebugger.h" 1147#include "lldb/API/SBValue.h" 1148 1149SWIGEXPORT lldb::ValueObjectSP 1150LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data) 1151{ 1152 lldb::ValueObjectSP valobj_sp; 1153 if (data) 1154 { 1155 lldb::SBValue* sb_ptr = (lldb::SBValue *)data; 1156 valobj_sp = sb_ptr->GetSP(); 1157 } 1158 return valobj_sp; 1159} 1160 1161#ifdef __cplusplus 1162extern "C" { 1163#endif 1164 1165void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton); 1166 1167#ifdef __cplusplus 1168} 1169#endif 1170%} 1171 1172%wrapper %{ 1173 1174 1175// For the LogOutputCallback functions 1176void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) { 1177 if (baton != Py_None) { 1178 SWIG_PYTHON_THREAD_BEGIN_BLOCK; 1179 PyObject *result = PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str); 1180 Py_XDECREF(result); 1181 SWIG_PYTHON_THREAD_END_BLOCK; 1182 } 1183} 1184%} 1185