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