5 PyErr_Cleaner(bool print = false) : m_print(print) {}
8 if (PyErr_Occurred()) {
9 if (m_print && !PyErr_ExceptionMatches(PyExc_SystemExit))
19 llvm::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) {
26 lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
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);
34 unsigned max_positional_args;
35 if (auto arg_info = pfunc.GetArgInfo())
36 max_positional_args = arg_info.get().max_positional_args;
38 return arg_info.takeError();
40 PythonObject frame_arg = SWIGBridge::ToSWIGWrapper(frame_sp);
41 PythonObject bp_loc_arg = SWIGBridge::ToSWIGWrapper(bp_loc_sp);
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);
49 return result.takeError();
51 // Only False counts as false!
52 return result.get().get() != Py_False;
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
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.
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.
68 bool 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) {
72 bool stop_at_watchpoint = true;
74 PyErr_Cleaner py_err_cleaner(true);
76 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
77 session_dictionary_name);
78 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
79 python_function_name, dict);
81 if (!pfunc.IsAllocated())
82 return stop_at_watchpoint;
85 pfunc(SWIGBridge::ToSWIGWrapper(frame_sp), SWIGBridge::ToSWIGWrapper(wp_sp), dict);
87 if (result.get() == Py_False)
88 stop_at_watchpoint = false;
90 return stop_at_watchpoint;
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.
97 bool lldb_private::python::SWIGBridge::LLDBSwigPythonFormatterCallbackFunction(
98 const char *python_function_name, const char *session_dictionary_name,
99 lldb::TypeImplSP type_impl_sp) {
101 PyErr_Cleaner py_err_cleaner(true);
103 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
104 session_dictionary_name);
105 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
106 python_function_name, dict);
108 if (!pfunc.IsAllocated())
111 PythonObject result =
112 pfunc(SWIGBridge::ToSWIGWrapper(type_impl_sp), dict);
114 // Only if everything goes okay and the function returns True we'll consider
116 return result.get() == Py_True;
119 bool 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) {
126 if (!python_function_name || !session_dictionary)
129 PyObject *pfunc_impl = nullptr;
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);
140 PyObject *py_dict = (PyObject *)session_dictionary;
141 if (!PythonDictionary::Check(py_dict))
144 PythonDictionary dict(PyRefType::Borrowed, py_dict);
146 PyErr_Cleaner pyerr_cleanup(true); // show Python errors
148 PythonCallable pfunc(PyRefType::Borrowed, pfunc_impl);
150 if (!pfunc.IsAllocated()) {
151 pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
152 python_function_name, dict);
153 if (!pfunc.IsAllocated())
156 if (pyfunct_wrapper) {
157 *pyfunct_wrapper = pfunc.get();
158 Py_XINCREF(pfunc.get());
163 auto argc = pfunc.GetArgInfo();
165 llvm::consumeError(argc.takeError());
169 PythonObject value_arg = SWIGBridge::ToSWIGWrapper(valobj_sp);
171 if (argc.get().max_positional_args < 3)
172 result = pfunc(value_arg, dict);
174 result = pfunc(value_arg, dict, SWIGBridge::ToSWIGWrapper(*options_sp));
176 retval = result.Str().GetString().str();
181 PythonObject 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();
188 PyErr_Cleaner py_err_cleaner(true);
190 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
191 session_dictionary_name);
192 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
193 python_class_name, dict);
195 if (!pfunc.IsAllocated())
196 return PythonObject();
198 auto sb_value = std::unique_ptr<lldb::SBValue>(new lldb::SBValue(valobj_sp));
199 sb_value->SetPreferSyntheticValue(false);
201 PythonObject val_arg = SWIGBridge::ToSWIGWrapper(std::move(sb_value));
202 if (!val_arg.IsAllocated())
203 return PythonObject();
205 PythonObject result = pfunc(val_arg, dict);
207 if (result.IsAllocated())
210 return PythonObject();
213 PythonObject 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();
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);
226 if (!pfunc.IsAllocated())
227 return PythonObject();
229 return pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger_sp)), dict);
232 PythonObject 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) {
237 if (python_class_name == NULL || python_class_name[0] == '\0' ||
238 !session_dictionary_name)
239 return PythonObject();
241 PyErr_Cleaner py_err_cleaner(true);
243 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
244 session_dictionary_name);
245 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
246 python_class_name, dict);
248 if (!pfunc.IsAllocated())
249 return PythonObject();
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.
256 if (result.IsAllocated()) {
257 // Check that __callback__ is defined:
258 auto callback_func = result.ResolveName<PythonCallable>("__callback__");
259 if (callback_func.IsAllocated())
262 return PythonObject();
265 unsigned 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);
272 if (!pfunc.IsAllocated())
275 PythonObject result = sym_ctx ? pfunc(SWIGBridge::ToSWIGWrapper(*sym_ctx)) : pfunc();
277 if (PyErr_Occurred()) {
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)
293 long long ret_val = unwrapOrSetPythonException(As<long long>(result));
295 if (PyErr_Occurred()) {
304 // wrapper that calls an optional instance member of an object taking no
306 static 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);
311 PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
312 auto pfunc = self.ResolveName<PythonCallable>(callee_name);
314 if (!pfunc.IsAllocated()) {
317 Py_XINCREF(ret_if_not_found);
318 return ret_if_not_found;
324 PythonObject result = pfunc();
325 return result.release();
328 size_t lldb_private::python::SWIGBridge::LLDBSwigPython_CalculateNumChildren(PyObject * implementor,
330 PythonObject self(PyRefType::Borrowed, implementor);
331 auto pfunc = self.ResolveName<PythonCallable>("num_children");
333 if (!pfunc.IsAllocated())
336 auto arg_info = pfunc.GetArgInfo();
338 llvm::consumeError(arg_info.takeError());
343 if (arg_info.get().max_positional_args < 1)
344 ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call()));
346 ret_val = unwrapOrSetPythonException(
347 As<long long>(pfunc.Call(PythonInteger(max))));
349 if (PyErr_Occurred()) {
355 if (arg_info.get().max_positional_args < 1)
356 ret_val = std::min(ret_val, static_cast<size_t>(max));
361 PyObject *lldb_private::python::SWIGBridge::LLDBSwigPython_GetChildAtIndex(PyObject * implementor,
363 PyErr_Cleaner py_err_cleaner(true);
365 PythonObject self(PyRefType::Borrowed, implementor);
366 auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index");
368 if (!pfunc.IsAllocated())
371 PythonObject result = pfunc(PythonInteger(idx));
373 if (!result.IsAllocated())
376 lldb::SBValue *sbvalue_ptr = nullptr;
377 if (SWIG_ConvertPtr(result.get(), (void **)&sbvalue_ptr,
378 SWIGTYPE_p_lldb__SBValue, 0) == -1)
381 if (sbvalue_ptr == nullptr)
384 return result.release();
387 int lldb_private::python::SWIGBridge::LLDBSwigPython_GetIndexOfChildWithName(
388 PyObject * implementor, const char *child_name) {
389 PyErr_Cleaner py_err_cleaner(true);
391 PythonObject self(PyRefType::Borrowed, implementor);
392 auto pfunc = self.ResolveName<PythonCallable>("get_child_index");
394 if (!pfunc.IsAllocated())
397 llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name));
400 unwrapOrSetPythonException(As<long long>(std::move(result)));
402 if (PyErr_Occurred()) {
403 PyErr_Clear(); // FIXME print this? do something else
408 return (uint32_t)retval;
413 bool lldb_private::python::SWIGBridge::LLDBSwigPython_UpdateSynthProviderInstance(PyObject *
415 bool ret_val = false;
417 static char callee_name[] = "update";
419 PyObject *py_return =
420 LLDBSwigPython_CallOptionalMember(implementor, callee_name);
422 if (py_return == Py_True)
425 Py_XDECREF(py_return);
430 bool lldb_private::python::SWIGBridge::LLDBSwigPython_MightHaveChildrenSynthProviderInstance(
431 PyObject * implementor) {
432 bool ret_val = false;
434 static char callee_name[] = "has_children";
436 PyObject *py_return =
437 LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_True);
439 if (py_return == Py_True)
442 Py_XDECREF(py_return);
447 PyObject *lldb_private::python::SWIGBridge::LLDBSwigPython_GetValueSynthProviderInstance(
448 PyObject * implementor) {
449 PyObject *ret_val = nullptr;
451 static char callee_name[] = "get_value";
453 PyObject *py_return =
454 LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_None);
456 if (py_return == Py_None || py_return == nullptr)
459 lldb::SBValue *sbvalue_ptr = NULL;
461 if (SWIG_ConvertPtr(py_return, (void **)&sbvalue_ptr,
462 SWIGTYPE_p_lldb__SBValue, 0) == -1)
464 else if (sbvalue_ptr == NULL)
469 Py_XDECREF(py_return);
473 void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBData(PyObject * data) {
474 lldb::SBData *sb_ptr = nullptr;
477 SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0);
479 if (valid_cast == -1)
485 void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(PyObject * data) {
486 lldb::SBBreakpoint *sb_ptr = nullptr;
489 SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBBreakpoint, 0);
491 if (valid_cast == -1)
497 void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(PyObject * data) {
498 lldb::SBAttachInfo *sb_ptr = nullptr;
501 SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBAttachInfo, 0);
503 if (valid_cast == -1)
509 void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(PyObject * data) {
510 lldb::SBLaunchInfo *sb_ptr = nullptr;
513 SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBLaunchInfo, 0);
515 if (valid_cast == -1)
521 void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBError(PyObject * data) {
522 lldb::SBError *sb_ptr = nullptr;
525 SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0);
527 if (valid_cast == -1)
533 void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBEvent(PyObject * data) {
534 lldb::SBEvent *sb_ptr = nullptr;
537 SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBEvent, 0);
539 if (valid_cast == -1)
545 void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBStream(PyObject * data) {
546 lldb::SBStream *sb_ptr = nullptr;
549 SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBStream, 0);
551 if (valid_cast == -1)
557 void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBValue(PyObject * data) {
558 lldb::SBValue *sb_ptr = NULL;
561 SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
563 if (valid_cast == -1)
569 void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *
571 lldb::SBMemoryRegionInfo *sb_ptr = NULL;
573 int valid_cast = SWIG_ConvertPtr(data, (void **)&sb_ptr,
574 SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0);
576 if (valid_cast == -1)
582 void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBExecutionContext(PyObject *
584 lldb::SBExecutionContext *sb_ptr = NULL;
586 int valid_cast = SWIG_ConvertPtr(data, (void **)&sb_ptr,
587 SWIGTYPE_p_lldb__SBExecutionContext, 0);
589 if (valid_cast == -1)
595 bool 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) {
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);
607 if (!pfunc.IsAllocated())
610 auto argc = pfunc.GetArgInfo();
612 llvm::consumeError(argc.takeError());
615 PythonObject debugger_arg = SWIGBridge::ToSWIGWrapper(std::move(debugger));
616 auto cmd_retobj_arg = SWIGBridge::ToSWIGWrapper(cmd_retobj);
618 if (argc.get().max_positional_args < 5u)
619 pfunc(debugger_arg, PythonString(args), cmd_retobj_arg.obj(), dict);
621 pfunc(debugger_arg, PythonString(args),
622 SWIGBridge::ToSWIGWrapper(std::move(exe_ctx_ref_sp)), cmd_retobj_arg.obj(), dict);
627 bool 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) {
632 PyErr_Cleaner py_err_cleaner(true);
634 PythonObject self(PyRefType::Borrowed, implementor);
635 auto pfunc = self.ResolveName<PythonCallable>("__call__");
637 if (!pfunc.IsAllocated())
640 auto cmd_retobj_arg = SWIGBridge::ToSWIGWrapper(cmd_retobj);
642 pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger)), PythonString(args),
643 SWIGBridge::ToSWIGWrapper(exe_ctx_ref_sp), cmd_retobj_arg.obj());
648 std::optional<std::string>
649 lldb_private::python::SWIGBridge::LLDBSwigPythonGetRepeatCommandForScriptedCommand(PyObject *implementor,
650 std::string &command) {
651 PyErr_Cleaner py_err_cleaner(true);
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())
659 PythonString command_str(command);
660 PythonObject result = pfunc(command_str);
662 // A return of None is the equivalent of nullopt - means repeat
663 // the command as is:
667 return result.Str().GetString().str();
670 StructuredData::DictionarySP
671 lldb_private::python::SWIGBridge::LLDBSwigPythonHandleArgumentCompletionForScriptedCommand(PyObject *implementor,
672 std::vector<llvm::StringRef> &args_vec, size_t args_pos, size_t pos_in_arg) {
674 PyErr_Cleaner py_err_cleaner(true);
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())
682 PythonList args_list(PyInitialValue::Empty);
683 for (auto elem : args_vec)
684 args_list.AppendItem(PythonString(elem));
686 PythonObject result = pfunc(args_list, PythonInteger(args_pos), PythonInteger(pos_in_arg));
687 // Returning None means do the ordinary completion
691 // Convert the return dictionary to a DictionarySP.
692 StructuredData::ObjectSP result_obj_sp = result.CreateStructuredObject();
696 StructuredData::DictionarySP dict_sp(new StructuredData::Dictionary(result_obj_sp));
697 if (dict_sp->GetType() == lldb::eStructuredDataTypeInvalid)
702 StructuredData::DictionarySP
703 lldb_private::python::SWIGBridge::LLDBSwigPythonHandleOptionArgumentCompletionForScriptedCommand(PyObject *implementor,
704 llvm::StringRef &long_option, size_t pos_in_arg) {
706 PyErr_Cleaner py_err_cleaner(true);
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())
714 PythonObject result = pfunc(PythonString(long_option), PythonInteger(pos_in_arg));
715 // Returning None means do the ordinary completion
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())
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);
732 // Convert the return dictionary to a DictionarySP.
733 StructuredData::ObjectSP result_obj_sp = result.CreateStructuredObject();
737 StructuredData::DictionarySP dict_sp(new StructuredData::Dictionary(result_obj_sp));
738 if (dict_sp->GetType() == lldb::eStructuredDataTypeInvalid)
743 #include "lldb/Interpreter/CommandReturnObject.h"
745 bool 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) {
750 PyErr_Cleaner py_err_cleaner(true);
752 PythonObject self(PyRefType::Borrowed, implementor);
753 auto pfunc = self.ResolveName<PythonCallable>("__call__");
755 if (!pfunc.IsAllocated()) {
756 cmd_retobj.AppendError("Could not find '__call__' method in implementation class");
760 pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger)), SWIGBridge::ToSWIGWrapper(args_impl),
761 SWIGBridge::ToSWIGWrapper(exe_ctx_ref_sp), SWIGBridge::ToSWIGWrapper(cmd_retobj).obj());
766 PythonObject 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();
773 PyErr_Cleaner py_err_cleaner(true);
775 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
776 session_dictionary_name);
777 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
778 python_class_name, dict);
780 if (!pfunc.IsAllocated())
781 return PythonObject();
783 return pfunc(SWIGBridge::ToSWIGWrapper(process_sp));
786 PythonObject 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();
792 PyErr_Cleaner py_err_cleaner(true);
794 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
795 session_dictionary_name);
796 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
797 python_class_name, dict);
799 if (!pfunc.IsAllocated())
800 return PythonObject();
805 PyObject *lldb_private::python::SWIGBridge::LLDBSwigPython_GetRecognizedArguments(
806 PyObject *implementor, const lldb::StackFrameSP &frame_sp) {
807 static char callee_name[] = "get_recognized_arguments";
809 PythonObject arg = SWIGBridge::ToSWIGWrapper(frame_sp);
811 PythonString str(callee_name);
813 PyObject_CallMethodObjArgs(implementor, str.get(), arg.get(), NULL);
817 bool lldb_private::python::SWIGBridge::LLDBSwigPython_ShouldHide(
818 PyObject *implementor, const lldb::StackFrameSP &frame_sp) {
819 static char callee_name[] = "should_hide";
821 PythonObject arg = SWIGBridge::ToSWIGWrapper(frame_sp);
823 PythonString str(callee_name);
826 PyObject_CallMethodObjArgs(implementor, str.get(), arg.get(), NULL);
827 bool ret_val = result ? PyObject_IsTrue(result) : false;
833 void *lldb_private::python::SWIGBridge::LLDBSWIGPython_GetDynamicSetting(
834 void *module, const char *setting, const lldb::TargetSP &target_sp) {
835 if (!module || !setting)
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");
842 if (!pfunc.IsAllocated())
845 auto result = pfunc(SWIGBridge::ToSWIGWrapper(target_sp), PythonString(setting));
847 return result.release();
850 bool lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordProcess(
851 const char *python_function_name, const char *session_dictionary_name,
852 const lldb::ProcessSP &process, std::string &output) {
854 if (python_function_name == NULL || python_function_name[0] == '\0' ||
855 !session_dictionary_name)
858 PyErr_Cleaner py_err_cleaner(true);
860 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
861 session_dictionary_name);
862 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
863 python_function_name, dict);
865 if (!pfunc.IsAllocated())
868 auto result = pfunc(SWIGBridge::ToSWIGWrapper(process), dict);
870 output = result.Str().GetString().str();
875 std::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)
882 PyErr_Cleaner py_err_cleaner(true);
884 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
885 session_dictionary_name);
886 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
887 python_function_name, dict);
889 if (!pfunc.IsAllocated())
892 auto result = pfunc(SWIGBridge::ToSWIGWrapper(std::move(thread)), dict);
894 return result.Str().GetString().str();
897 bool lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordTarget(
898 const char *python_function_name, const char *session_dictionary_name,
899 const lldb::TargetSP &target, std::string &output) {
901 if (python_function_name == NULL || python_function_name[0] == '\0' ||
902 !session_dictionary_name)
905 PyErr_Cleaner py_err_cleaner(true);
907 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
908 session_dictionary_name);
909 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
910 python_function_name, dict);
912 if (!pfunc.IsAllocated())
915 auto result = pfunc(SWIGBridge::ToSWIGWrapper(target), dict);
917 output = result.Str().GetString().str();
922 std::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)
929 PyErr_Cleaner py_err_cleaner(true);
931 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
932 session_dictionary_name);
933 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
934 python_function_name, dict);
936 if (!pfunc.IsAllocated())
939 auto result = pfunc(SWIGBridge::ToSWIGWrapper(std::move(frame)), dict);
941 return result.Str().GetString().str();
944 bool lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordValue(
945 const char *python_function_name, const char *session_dictionary_name,
946 const lldb::ValueObjectSP &value, std::string &output) {
948 if (python_function_name == NULL || python_function_name[0] == '\0' ||
949 !session_dictionary_name)
952 PyErr_Cleaner py_err_cleaner(true);
954 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
955 session_dictionary_name);
956 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
957 python_function_name, dict);
959 if (!pfunc.IsAllocated())
962 auto result = pfunc(SWIGBridge::ToSWIGWrapper(value), dict);
964 output = result.Str().GetString().str();
969 bool 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();
976 PyErr_Cleaner py_err_cleaner(true);
978 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
979 session_dictionary_name);
980 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
981 python_function_name, dict);
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())
988 pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger)), dict);
993 lldb::ValueObjectSP lldb_private::python::SWIGBridge::LLDBSWIGPython_GetValueObjectSPFromSBValue(
995 lldb::ValueObjectSP valobj_sp;
997 lldb::SBValue *sb_ptr = (lldb::SBValue *)data;
998 valobj_sp = sb_ptr->GetSP();
1003 // For the LogOutputCallback functions
1004 static void LLDBSwigPythonCallPythonLogOutputCallback(const char *str,
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);
1011 SWIG_PYTHON_THREAD_END_BLOCK;
1015 // For DebuggerTerminateCallback functions
1016 static void LLDBSwigPythonCallPythonSBDebuggerTerminateCallback(lldb::user_id_t debugger_id,
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);
1023 SWIG_PYTHON_THREAD_END_BLOCK;
1027 static 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
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);
1040 PyObject *result = PyObject_CallObject(
1041 reinterpret_cast<PyObject *>(baton), PyList_AsTuple(py_argv));
1042 ret_val = result ? PyObject_IsTrue(result) : false;
1044 SWIG_PYTHON_THREAD_END_BLOCK;
1049 static 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;
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));
1062 PythonCallable callable =
1063 Retain<PythonCallable>(reinterpret_cast<PyObject *>(callback_baton));
1064 if (!callable.IsValid()) {
1065 return SBError("The callback callable is not valid.");
1068 PythonObject result = callable(module_spec_arg, module_file_spec_arg,
1069 symbol_file_spec_arg);
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.");
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.");
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.");
1092 module_file_spec_sb = *sb_module_file_spec_ptr;
1093 symbol_file_spec_sb = *sb_symbol_file_spec_ptr;
1096 return *sb_error_ptr;