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