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::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 = ToSWIGWrapper(frame_sp);
41 PythonObject bp_loc_arg = 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, 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::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(ToSWIGWrapper(frame_sp), ToSWIGWrapper(wp_sp), dict);
87 if (result.get() == Py_False)
88 stop_at_watchpoint = false;
90 return stop_at_watchpoint;
93 bool lldb_private::LLDBSwigPythonCallTypeScript(
94 const char *python_function_name, const void *session_dictionary,
95 const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper,
96 const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval) {
100 if (!python_function_name || !session_dictionary)
103 PyObject *pfunc_impl = nullptr;
105 if (pyfunct_wrapper && *pyfunct_wrapper &&
106 PyFunction_Check(*pyfunct_wrapper)) {
107 pfunc_impl = (PyObject *)(*pyfunct_wrapper);
108 if (pfunc_impl->ob_refcnt == 1) {
109 Py_XDECREF(pfunc_impl);
114 PyObject *py_dict = (PyObject *)session_dictionary;
115 if (!PythonDictionary::Check(py_dict))
118 PythonDictionary dict(PyRefType::Borrowed, py_dict);
120 PyErr_Cleaner pyerr_cleanup(true); // show Python errors
122 PythonCallable pfunc(PyRefType::Borrowed, pfunc_impl);
124 if (!pfunc.IsAllocated()) {
125 pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
126 python_function_name, dict);
127 if (!pfunc.IsAllocated())
130 if (pyfunct_wrapper) {
131 *pyfunct_wrapper = pfunc.get();
132 Py_XINCREF(pfunc.get());
137 auto argc = pfunc.GetArgInfo();
139 llvm::consumeError(argc.takeError());
143 PythonObject value_arg = ToSWIGWrapper(valobj_sp);
145 if (argc.get().max_positional_args < 3)
146 result = pfunc(value_arg, dict);
148 result = pfunc(value_arg, dict, ToSWIGWrapper(*options_sp));
150 retval = result.Str().GetString().str();
155 PythonObject lldb_private::LLDBSwigPythonCreateSyntheticProvider(
156 const char *python_class_name, const char *session_dictionary_name,
157 const lldb::ValueObjectSP &valobj_sp) {
158 if (python_class_name == NULL || python_class_name[0] == '\0' ||
159 !session_dictionary_name)
160 return PythonObject();
162 PyErr_Cleaner py_err_cleaner(true);
164 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
165 session_dictionary_name);
166 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
167 python_class_name, dict);
169 if (!pfunc.IsAllocated())
170 return PythonObject();
172 auto sb_value = std::make_unique<lldb::SBValue>(valobj_sp);
173 sb_value->SetPreferSyntheticValue(false);
175 PythonObject val_arg = ToSWIGWrapper(std::move(sb_value));
176 if (!val_arg.IsAllocated())
177 return PythonObject();
179 PythonObject result = pfunc(val_arg, dict);
181 if (result.IsAllocated())
184 return PythonObject();
187 PythonObject lldb_private::LLDBSwigPythonCreateCommandObject(
188 const char *python_class_name, const char *session_dictionary_name,
189 lldb::DebuggerSP debugger_sp) {
190 if (python_class_name == NULL || python_class_name[0] == '\0' ||
191 !session_dictionary_name)
192 return PythonObject();
194 PyErr_Cleaner py_err_cleaner(true);
195 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
196 session_dictionary_name);
197 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
198 python_class_name, dict);
200 if (!pfunc.IsAllocated())
201 return PythonObject();
203 return pfunc(ToSWIGWrapper(std::move(debugger_sp)), dict);
206 PythonObject lldb_private::LLDBSwigPythonCreateScriptedProcess(
207 const char *python_class_name, const char *session_dictionary_name,
208 const lldb::TargetSP &target_sp,
209 const lldb_private::StructuredDataImpl &args_impl,
210 std::string &error_string) {
211 if (python_class_name == NULL || python_class_name[0] == '\0' ||
212 !session_dictionary_name)
213 return PythonObject();
215 PyErr_Cleaner py_err_cleaner(true);
217 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
218 session_dictionary_name);
219 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
220 python_class_name, dict);
222 if (!pfunc.IsAllocated()) {
223 error_string.append("could not find script class: ");
224 error_string.append(python_class_name);
225 return PythonObject();
228 PythonObject target_arg = ToSWIGWrapper(target_sp);
230 llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
232 llvm::handleAllErrors(
233 arg_info.takeError(),
234 [&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
235 [&](const llvm::ErrorInfoBase &E) {
236 error_string.append(E.message());
238 return PythonObject();
241 PythonObject result = {};
242 if (arg_info.get().max_positional_args == 2) {
243 result = pfunc(target_arg, ToSWIGWrapper(args_impl));
245 error_string.assign("wrong number of arguments in __init__, should be 2 "
246 "(not including self)");
251 PythonObject lldb_private::LLDBSwigPythonCreateScriptedThread(
252 const char *python_class_name, const char *session_dictionary_name,
253 const lldb::ProcessSP &process_sp, const StructuredDataImpl &args_impl,
254 std::string &error_string) {
255 if (python_class_name == NULL || python_class_name[0] == '\0' ||
256 !session_dictionary_name)
257 return PythonObject();
259 PyErr_Cleaner py_err_cleaner(true);
261 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
262 session_dictionary_name);
263 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
264 python_class_name, dict);
266 if (!pfunc.IsAllocated()) {
267 error_string.append("could not find script class: ");
268 error_string.append(python_class_name);
269 return PythonObject();
272 llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
274 llvm::handleAllErrors(
275 arg_info.takeError(),
276 [&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
277 [&](const llvm::ErrorInfoBase &E) {
278 error_string.append(E.message());
280 return PythonObject();
283 if (arg_info.get().max_positional_args == 2)
284 return pfunc(ToSWIGWrapper(process_sp), ToSWIGWrapper(args_impl));
286 error_string.assign("wrong number of arguments in __init__, should be 2 "
287 "(not including self)");
288 return PythonObject();
291 PythonObject lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
292 const char *python_class_name, const char *session_dictionary_name,
293 const lldb_private::StructuredDataImpl &args_impl,
294 std::string &error_string, const lldb::ThreadPlanSP &thread_plan_sp) {
295 if (python_class_name == NULL || python_class_name[0] == '\0' ||
296 !session_dictionary_name)
297 return PythonObject();
299 PyErr_Cleaner py_err_cleaner(true);
301 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
302 session_dictionary_name);
303 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
304 python_class_name, dict);
306 if (!pfunc.IsAllocated()) {
307 error_string.append("could not find script class: ");
308 error_string.append(python_class_name);
309 return PythonObject();
312 PythonObject tp_arg = ToSWIGWrapper(thread_plan_sp);
314 llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
316 llvm::handleAllErrors(
317 arg_info.takeError(),
318 [&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
319 [&](const llvm::ErrorInfoBase &E) {
320 error_string.append(E.message());
322 return PythonObject();
325 PythonObject result = {};
326 auto args_sb = std::make_unique<lldb::SBStructuredData>(args_impl);
327 if (arg_info.get().max_positional_args == 2) {
328 if (args_sb->IsValid()) {
330 "args passed, but __init__ does not take an args dictionary");
331 return PythonObject();
333 result = pfunc(tp_arg, dict);
334 } else if (arg_info.get().max_positional_args >= 3) {
335 result = pfunc(tp_arg, ToSWIGWrapper(std::move(args_sb)), dict);
337 error_string.assign("wrong number of arguments in __init__, should be 2 or "
338 "3 (not including self)");
339 return PythonObject();
342 // FIXME: At this point we should check that the class we found supports all
343 // the methods that we need.
348 bool lldb_private::LLDBSWIGPythonCallThreadPlan(
349 void *implementor, const char *method_name, lldb_private::Event *event,
353 PyErr_Cleaner py_err_cleaner(false);
354 PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
355 auto pfunc = self.ResolveName<PythonCallable>(method_name);
357 if (!pfunc.IsAllocated())
361 if (event != nullptr) {
362 ScopedPythonObject<SBEvent> event_arg = ToSWIGWrapper(event);
363 result = pfunc(event_arg.obj());
367 if (PyErr_Occurred()) {
369 printf("Return value was neither false nor true for call to %s.\n",
375 if (result.get() == Py_True)
377 else if (result.get() == Py_False)
380 // Somebody returned the wrong thing...
382 printf("Wrong return value type for call to %s.\n", method_name);
386 PythonObject lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
387 const char *python_class_name, const char *session_dictionary_name,
388 const StructuredDataImpl &args_impl,
389 const lldb::BreakpointSP &breakpoint_sp) {
391 if (python_class_name == NULL || python_class_name[0] == '\0' ||
392 !session_dictionary_name)
393 return PythonObject();
395 PyErr_Cleaner py_err_cleaner(true);
397 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
398 session_dictionary_name);
399 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
400 python_class_name, dict);
402 if (!pfunc.IsAllocated())
403 return PythonObject();
405 PythonObject result =
406 pfunc(ToSWIGWrapper(breakpoint_sp), ToSWIGWrapper(args_impl), dict);
407 // FIXME: At this point we should check that the class we found supports all
408 // the methods that we need.
410 if (result.IsAllocated()) {
411 // Check that __callback__ is defined:
412 auto callback_func = result.ResolveName<PythonCallable>("__callback__");
413 if (callback_func.IsAllocated())
416 return PythonObject();
419 unsigned int lldb_private::LLDBSwigPythonCallBreakpointResolver(
420 void *implementor, const char *method_name,
421 lldb_private::SymbolContext *sym_ctx) {
422 PyErr_Cleaner py_err_cleaner(false);
423 PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
424 auto pfunc = self.ResolveName<PythonCallable>(method_name);
426 if (!pfunc.IsAllocated())
429 PythonObject result = sym_ctx ? pfunc(ToSWIGWrapper(*sym_ctx)) : pfunc();
431 if (PyErr_Occurred()) {
437 // The callback will return a bool, but we're need to also return ints
438 // so we're squirrelling the bool through as an int... And if you return
439 // nothing, we'll continue.
440 if (strcmp(method_name, "__callback__") == 0) {
441 if (result.get() == Py_False)
447 long long ret_val = unwrapOrSetPythonException(As<long long>(result));
449 if (PyErr_Occurred()) {
458 PythonObject lldb_private::LLDBSwigPythonCreateScriptedStopHook(
459 lldb::TargetSP target_sp, const char *python_class_name,
460 const char *session_dictionary_name, const StructuredDataImpl &args_impl,
462 if (python_class_name == NULL || python_class_name[0] == '\0') {
463 error.SetErrorString("Empty class name.");
464 return PythonObject();
466 if (!session_dictionary_name) {
467 error.SetErrorString("No session dictionary");
468 return PythonObject();
471 PyErr_Cleaner py_err_cleaner(true);
473 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
474 session_dictionary_name);
475 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
476 python_class_name, dict);
478 if (!pfunc.IsAllocated()) {
479 error.SetErrorStringWithFormat("Could not find class: %s.",
481 return PythonObject();
484 PythonObject result =
485 pfunc(ToSWIGWrapper(target_sp), ToSWIGWrapper(args_impl), dict);
487 if (result.IsAllocated()) {
488 // Check that the handle_stop callback is defined:
489 auto callback_func = result.ResolveName<PythonCallable>("handle_stop");
490 if (callback_func.IsAllocated()) {
491 if (auto args_info = callback_func.GetArgInfo()) {
492 size_t num_args = (*args_info).max_positional_args;
494 error.SetErrorStringWithFormat(
495 "Wrong number of args for "
496 "handle_stop callback, should be 2 (excluding self), got: %zu",
498 return PythonObject();
502 error.SetErrorString("Couldn't get num arguments for handle_stop "
504 return PythonObject();
508 error.SetErrorStringWithFormat("Class \"%s\" is missing the required "
509 "handle_stop callback.",
513 return PythonObject();
516 bool lldb_private::LLDBSwigPythonStopHookCallHandleStop(
517 void *implementor, lldb::ExecutionContextRefSP exc_ctx_sp,
518 lldb::StreamSP stream) {
519 // handle_stop will return a bool with the meaning "should_stop"...
520 // If you return nothing we'll assume we are going to stop.
521 // Also any errors should return true, since we should stop on error.
523 PyErr_Cleaner py_err_cleaner(false);
524 PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
525 auto pfunc = self.ResolveName<PythonCallable>("handle_stop");
527 if (!pfunc.IsAllocated())
530 auto *sb_stream = new lldb::SBStream();
531 PythonObject sb_stream_arg =
532 ToSWIGWrapper(std::unique_ptr<lldb::SBStream>(sb_stream));
533 PythonObject result =
534 pfunc(ToSWIGWrapper(std::move(exc_ctx_sp)), sb_stream_arg);
536 if (PyErr_Occurred()) {
537 stream->PutCString("Python error occurred handling stop-hook.");
543 // Now add the result to the output stream. SBStream only
544 // makes an internally help StreamString which I can't interpose, so I
545 // have to copy it over here.
546 stream->PutCString(sb_stream->GetData());
548 if (result.get() == Py_False)
554 // wrapper that calls an optional instance member of an object taking no
556 static PyObject *LLDBSwigPython_CallOptionalMember(
557 PyObject * implementor, char *callee_name,
558 PyObject *ret_if_not_found = Py_None, bool *was_found = NULL) {
559 PyErr_Cleaner py_err_cleaner(false);
561 PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
562 auto pfunc = self.ResolveName<PythonCallable>(callee_name);
564 if (!pfunc.IsAllocated()) {
567 Py_XINCREF(ret_if_not_found);
568 return ret_if_not_found;
574 PythonObject result = pfunc();
575 return result.release();
578 size_t lldb_private::LLDBSwigPython_CalculateNumChildren(PyObject * implementor,
580 PythonObject self(PyRefType::Borrowed, implementor);
581 auto pfunc = self.ResolveName<PythonCallable>("num_children");
583 if (!pfunc.IsAllocated())
586 auto arg_info = pfunc.GetArgInfo();
588 llvm::consumeError(arg_info.takeError());
593 if (arg_info.get().max_positional_args < 1)
594 ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call()));
596 ret_val = unwrapOrSetPythonException(
597 As<long long>(pfunc.Call(PythonInteger(max))));
599 if (PyErr_Occurred()) {
605 if (arg_info.get().max_positional_args < 1)
606 ret_val = std::min(ret_val, static_cast<size_t>(max));
611 PyObject *lldb_private::LLDBSwigPython_GetChildAtIndex(PyObject * implementor,
613 PyErr_Cleaner py_err_cleaner(true);
615 PythonObject self(PyRefType::Borrowed, implementor);
616 auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index");
618 if (!pfunc.IsAllocated())
621 PythonObject result = pfunc(PythonInteger(idx));
623 if (!result.IsAllocated())
626 lldb::SBValue *sbvalue_ptr = nullptr;
627 if (SWIG_ConvertPtr(result.get(), (void **)&sbvalue_ptr,
628 SWIGTYPE_p_lldb__SBValue, 0) == -1)
631 if (sbvalue_ptr == nullptr)
634 return result.release();
637 int lldb_private::LLDBSwigPython_GetIndexOfChildWithName(
638 PyObject * implementor, const char *child_name) {
639 PyErr_Cleaner py_err_cleaner(true);
641 PythonObject self(PyRefType::Borrowed, implementor);
642 auto pfunc = self.ResolveName<PythonCallable>("get_child_index");
644 if (!pfunc.IsAllocated())
647 llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name));
650 unwrapOrSetPythonException(As<long long>(std::move(result)));
652 if (PyErr_Occurred()) {
653 PyErr_Clear(); // FIXME print this? do something else
658 return (uint32_t)retval;
663 bool lldb_private::LLDBSwigPython_UpdateSynthProviderInstance(PyObject *
665 bool ret_val = false;
667 static char callee_name[] = "update";
669 PyObject *py_return =
670 LLDBSwigPython_CallOptionalMember(implementor, callee_name);
672 if (py_return == Py_True)
675 Py_XDECREF(py_return);
680 bool lldb_private::LLDBSwigPython_MightHaveChildrenSynthProviderInstance(
681 PyObject * implementor) {
682 bool ret_val = false;
684 static char callee_name[] = "has_children";
686 PyObject *py_return =
687 LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_True);
689 if (py_return == Py_True)
692 Py_XDECREF(py_return);
697 PyObject *lldb_private::LLDBSwigPython_GetValueSynthProviderInstance(
698 PyObject * implementor) {
699 PyObject *ret_val = nullptr;
701 static char callee_name[] = "get_value";
703 PyObject *py_return =
704 LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_None);
706 if (py_return == Py_None || py_return == nullptr)
709 lldb::SBValue *sbvalue_ptr = NULL;
711 if (SWIG_ConvertPtr(py_return, (void **)&sbvalue_ptr,
712 SWIGTYPE_p_lldb__SBValue, 0) == -1)
714 else if (sbvalue_ptr == NULL)
719 Py_XDECREF(py_return);
723 void *lldb_private::LLDBSWIGPython_CastPyObjectToSBData(PyObject * data) {
724 lldb::SBData *sb_ptr = nullptr;
727 SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0);
729 if (valid_cast == -1)
735 void *lldb_private::LLDBSWIGPython_CastPyObjectToSBError(PyObject * data) {
736 lldb::SBError *sb_ptr = nullptr;
739 SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0);
741 if (valid_cast == -1)
747 void *lldb_private::LLDBSWIGPython_CastPyObjectToSBValue(PyObject * data) {
748 lldb::SBValue *sb_ptr = NULL;
751 SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
753 if (valid_cast == -1)
759 void *lldb_private::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *
761 lldb::SBMemoryRegionInfo *sb_ptr = NULL;
763 int valid_cast = SWIG_ConvertPtr(data, (void **)&sb_ptr,
764 SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0);
766 if (valid_cast == -1)
772 bool lldb_private::LLDBSwigPythonCallCommand(
773 const char *python_function_name, const char *session_dictionary_name,
774 lldb::DebuggerSP debugger, const char *args,
775 lldb_private::CommandReturnObject &cmd_retobj,
776 lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
778 PyErr_Cleaner py_err_cleaner(true);
779 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
780 session_dictionary_name);
781 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
782 python_function_name, dict);
784 if (!pfunc.IsAllocated())
787 auto argc = pfunc.GetArgInfo();
789 llvm::consumeError(argc.takeError());
792 PythonObject debugger_arg = ToSWIGWrapper(std::move(debugger));
793 auto cmd_retobj_arg = ToSWIGWrapper(cmd_retobj);
795 if (argc.get().max_positional_args < 5u)
796 pfunc(debugger_arg, PythonString(args), cmd_retobj_arg.obj(), dict);
798 pfunc(debugger_arg, PythonString(args),
799 ToSWIGWrapper(std::move(exe_ctx_ref_sp)), cmd_retobj_arg.obj(), dict);
804 bool lldb_private::LLDBSwigPythonCallCommandObject(
805 PyObject *implementor, lldb::DebuggerSP debugger, const char *args,
806 lldb_private::CommandReturnObject &cmd_retobj,
807 lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
809 PyErr_Cleaner py_err_cleaner(true);
811 PythonObject self(PyRefType::Borrowed, implementor);
812 auto pfunc = self.ResolveName<PythonCallable>("__call__");
814 if (!pfunc.IsAllocated())
817 auto cmd_retobj_arg = ToSWIGWrapper(cmd_retobj);
819 pfunc(ToSWIGWrapper(std::move(debugger)), PythonString(args),
820 ToSWIGWrapper(exe_ctx_ref_sp), cmd_retobj_arg.obj());
825 PythonObject lldb_private::LLDBSWIGPythonCreateOSPlugin(
826 const char *python_class_name, const char *session_dictionary_name,
827 const lldb::ProcessSP &process_sp) {
828 if (python_class_name == NULL || python_class_name[0] == '\0' ||
829 !session_dictionary_name)
830 return PythonObject();
832 PyErr_Cleaner py_err_cleaner(true);
834 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
835 session_dictionary_name);
836 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
837 python_class_name, dict);
839 if (!pfunc.IsAllocated())
840 return PythonObject();
842 return pfunc(ToSWIGWrapper(process_sp));
845 PythonObject lldb_private::LLDBSWIGPython_CreateFrameRecognizer(
846 const char *python_class_name, const char *session_dictionary_name) {
847 if (python_class_name == NULL || python_class_name[0] == '\0' ||
848 !session_dictionary_name)
849 return PythonObject();
851 PyErr_Cleaner py_err_cleaner(true);
853 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
854 session_dictionary_name);
855 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
856 python_class_name, dict);
858 if (!pfunc.IsAllocated())
859 return PythonObject();
864 PyObject *lldb_private::LLDBSwigPython_GetRecognizedArguments(
865 PyObject * implementor, const lldb::StackFrameSP &frame_sp) {
866 static char callee_name[] = "get_recognized_arguments";
868 PythonObject arg = ToSWIGWrapper(frame_sp);
870 PythonString str(callee_name);
872 PyObject_CallMethodObjArgs(implementor, str.get(), arg.get(), NULL);
876 void *lldb_private::LLDBSWIGPython_GetDynamicSetting(
877 void *module, const char *setting, const lldb::TargetSP &target_sp) {
878 if (!module || !setting)
881 PyErr_Cleaner py_err_cleaner(true);
882 PythonObject py_module(PyRefType::Borrowed, (PyObject *)module);
883 auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting");
885 if (!pfunc.IsAllocated())
888 auto result = pfunc(ToSWIGWrapper(target_sp), PythonString(setting));
890 return result.release();
893 bool lldb_private::LLDBSWIGPythonRunScriptKeywordProcess(
894 const char *python_function_name, const char *session_dictionary_name,
895 const lldb::ProcessSP &process, std::string &output) {
897 if (python_function_name == NULL || python_function_name[0] == '\0' ||
898 !session_dictionary_name)
901 PyErr_Cleaner py_err_cleaner(true);
903 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
904 session_dictionary_name);
905 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
906 python_function_name, dict);
908 if (!pfunc.IsAllocated())
911 auto result = pfunc(ToSWIGWrapper(process), dict);
913 output = result.Str().GetString().str();
918 llvm::Optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordThread(
919 const char *python_function_name, const char *session_dictionary_name,
920 lldb::ThreadSP thread) {
921 if (python_function_name == NULL || python_function_name[0] == '\0' ||
922 !session_dictionary_name)
925 PyErr_Cleaner py_err_cleaner(true);
927 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
928 session_dictionary_name);
929 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
930 python_function_name, dict);
932 if (!pfunc.IsAllocated())
935 auto result = pfunc(ToSWIGWrapper(std::move(thread)), dict);
937 return result.Str().GetString().str();
940 bool lldb_private::LLDBSWIGPythonRunScriptKeywordTarget(
941 const char *python_function_name, const char *session_dictionary_name,
942 const lldb::TargetSP &target, std::string &output) {
944 if (python_function_name == NULL || python_function_name[0] == '\0' ||
945 !session_dictionary_name)
948 PyErr_Cleaner py_err_cleaner(true);
950 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
951 session_dictionary_name);
952 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
953 python_function_name, dict);
955 if (!pfunc.IsAllocated())
958 auto result = pfunc(ToSWIGWrapper(target), dict);
960 output = result.Str().GetString().str();
965 llvm::Optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordFrame(
966 const char *python_function_name, const char *session_dictionary_name,
967 lldb::StackFrameSP frame) {
968 if (python_function_name == NULL || python_function_name[0] == '\0' ||
969 !session_dictionary_name)
972 PyErr_Cleaner py_err_cleaner(true);
974 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
975 session_dictionary_name);
976 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
977 python_function_name, dict);
979 if (!pfunc.IsAllocated())
982 auto result = pfunc(ToSWIGWrapper(std::move(frame)), dict);
984 return result.Str().GetString().str();
987 bool lldb_private::LLDBSWIGPythonRunScriptKeywordValue(
988 const char *python_function_name, const char *session_dictionary_name,
989 const lldb::ValueObjectSP &value, std::string &output) {
991 if (python_function_name == NULL || python_function_name[0] == '\0' ||
992 !session_dictionary_name)
995 PyErr_Cleaner py_err_cleaner(true);
997 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
998 session_dictionary_name);
999 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
1000 python_function_name, dict);
1002 if (!pfunc.IsAllocated())
1005 auto result = pfunc(ToSWIGWrapper(value), dict);
1007 output = result.Str().GetString().str();
1012 bool lldb_private::LLDBSwigPythonCallModuleInit(
1013 const char *python_module_name, const char *session_dictionary_name,
1014 lldb::DebuggerSP debugger) {
1015 std::string python_function_name_string = python_module_name;
1016 python_function_name_string += ".__lldb_init_module";
1017 const char *python_function_name = python_function_name_string.c_str();
1019 PyErr_Cleaner py_err_cleaner(true);
1021 auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
1022 session_dictionary_name);
1023 auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
1024 python_function_name, dict);
1026 // This method is optional and need not exist. So if we don't find it,
1027 // it's actually a success, not a failure.
1028 if (!pfunc.IsAllocated())
1031 pfunc(ToSWIGWrapper(std::move(debugger)), dict);
1036 lldb::ValueObjectSP lldb_private::LLDBSWIGPython_GetValueObjectSPFromSBValue(
1038 lldb::ValueObjectSP valobj_sp;
1040 lldb::SBValue *sb_ptr = (lldb::SBValue *)data;
1041 valobj_sp = sb_ptr->GetSP();
1046 // For the LogOutputCallback functions
1047 static void LLDBSwigPythonCallPythonLogOutputCallback(const char *str,
1049 if (baton != Py_None) {
1050 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
1051 PyObject *result = PyObject_CallFunction(
1052 reinterpret_cast<PyObject *>(baton), const_cast<char *>("s"), str);
1054 SWIG_PYTHON_THREAD_END_BLOCK;