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