xref: /src/contrib/llvm-project/lldb/bindings/python/python-wrapper.swig (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1cfca06d7SDimitry Andric%header %{
2cfca06d7SDimitry Andric
377fc4c14SDimitry Andricclass PyErr_Cleaner {
4cfca06d7SDimitry Andricpublic:
577fc4c14SDimitry Andric  PyErr_Cleaner(bool print = false) : m_print(print) {}
6cfca06d7SDimitry Andric
777fc4c14SDimitry Andric  ~PyErr_Cleaner() {
877fc4c14SDimitry Andric    if (PyErr_Occurred()) {
9cfca06d7SDimitry Andric      if (m_print && !PyErr_ExceptionMatches(PyExc_SystemExit))
10cfca06d7SDimitry Andric        PyErr_Print();
11cfca06d7SDimitry Andric      PyErr_Clear();
12cfca06d7SDimitry Andric    }
13cfca06d7SDimitry Andric  }
14cfca06d7SDimitry Andric
15cfca06d7SDimitry Andricprivate:
16cfca06d7SDimitry Andric  bool m_print;
17cfca06d7SDimitry Andric};
18cfca06d7SDimitry Andric
197fa27ce4SDimitry Andricllvm::Expected<bool> lldb_private::python::SWIGBridge::LLDBSwigPythonBreakpointCallbackFunction(
2077fc4c14SDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
21cfca06d7SDimitry Andric    const lldb::StackFrameSP &frame_sp,
22cfca06d7SDimitry Andric    const lldb::BreakpointLocationSP &bp_loc_sp,
2377fc4c14SDimitry Andric    const lldb_private::StructuredDataImpl &args_impl) {
24cfca06d7SDimitry Andric  using namespace llvm;
25cfca06d7SDimitry Andric
26cfca06d7SDimitry Andric  lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
27cfca06d7SDimitry Andric
28cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
2977fc4c14SDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
3077fc4c14SDimitry Andric      session_dictionary_name);
3177fc4c14SDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
3277fc4c14SDimitry Andric      python_function_name, dict);
33cfca06d7SDimitry Andric
34cfca06d7SDimitry Andric  unsigned max_positional_args;
35cfca06d7SDimitry Andric  if (auto arg_info = pfunc.GetArgInfo())
36cfca06d7SDimitry Andric    max_positional_args = arg_info.get().max_positional_args;
37cfca06d7SDimitry Andric  else
38cfca06d7SDimitry Andric    return arg_info.takeError();
39cfca06d7SDimitry Andric
407fa27ce4SDimitry Andric  PythonObject frame_arg = SWIGBridge::ToSWIGWrapper(frame_sp);
417fa27ce4SDimitry Andric  PythonObject bp_loc_arg = SWIGBridge::ToSWIGWrapper(bp_loc_sp);
42cfca06d7SDimitry Andric
4377fc4c14SDimitry Andric  auto result =
4477fc4c14SDimitry Andric      max_positional_args < 4
4577fc4c14SDimitry Andric          ? pfunc.Call(frame_arg, bp_loc_arg, dict)
467fa27ce4SDimitry Andric          : pfunc.Call(frame_arg, bp_loc_arg, SWIGBridge::ToSWIGWrapper(args_impl), dict);
47cfca06d7SDimitry Andric
48cfca06d7SDimitry Andric  if (!result)
49cfca06d7SDimitry Andric    return result.takeError();
50cfca06d7SDimitry Andric
51cfca06d7SDimitry Andric  // Only False counts as false!
52cfca06d7SDimitry Andric  return result.get().get() != Py_False;
53cfca06d7SDimitry Andric}
54cfca06d7SDimitry Andric
55f65dcba8SDimitry Andric// resolve a dotted Python name in the form
56f65dcba8SDimitry Andric// foo.bar.baz.Foobar to an actual Python object
57f65dcba8SDimitry Andric// if pmodule is NULL, the __main__ module will be used
58f65dcba8SDimitry Andric// as the starting point for the search
59344a3780SDimitry Andric
6077fc4c14SDimitry Andric// This function is called by
6177fc4c14SDimitry Andric// lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...) and is
6277fc4c14SDimitry Andric// used when a script command is attached to a breakpoint for execution.
63f65dcba8SDimitry Andric
6477fc4c14SDimitry Andric// This function is called by
6577fc4c14SDimitry Andric// lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...) and is
6677fc4c14SDimitry Andric// used when a script command is attached to a watchpoint for execution.
67344a3780SDimitry Andric
687fa27ce4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonWatchpointCallbackFunction(
6977fc4c14SDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
7077fc4c14SDimitry Andric    const lldb::StackFrameSP &frame_sp, const lldb::WatchpointSP &wp_sp) {
71cfca06d7SDimitry Andric
72cfca06d7SDimitry Andric  bool stop_at_watchpoint = true;
73cfca06d7SDimitry Andric
74cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
75cfca06d7SDimitry Andric
7677fc4c14SDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
7777fc4c14SDimitry Andric      session_dictionary_name);
7877fc4c14SDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
7977fc4c14SDimitry Andric      python_function_name, dict);
80cfca06d7SDimitry Andric
81cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
82cfca06d7SDimitry Andric    return stop_at_watchpoint;
83cfca06d7SDimitry Andric
8477fc4c14SDimitry Andric  PythonObject result =
857fa27ce4SDimitry Andric      pfunc(SWIGBridge::ToSWIGWrapper(frame_sp), SWIGBridge::ToSWIGWrapper(wp_sp), dict);
86cfca06d7SDimitry Andric
87cfca06d7SDimitry Andric  if (result.get() == Py_False)
88cfca06d7SDimitry Andric    stop_at_watchpoint = false;
89cfca06d7SDimitry Andric
90cfca06d7SDimitry Andric  return stop_at_watchpoint;
91cfca06d7SDimitry Andric}
92cfca06d7SDimitry Andric
93e3b55780SDimitry Andric// This function is called by
94e3b55780SDimitry Andric// ScriptInterpreterPython::FormatterMatchingCallbackFunction and it's used when
95e3b55780SDimitry Andric// a data formatter provides the name of a callback to inspect a candidate type
96e3b55780SDimitry Andric// before considering a match.
977fa27ce4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonFormatterCallbackFunction(
98e3b55780SDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
99e3b55780SDimitry Andric    lldb::TypeImplSP type_impl_sp) {
100e3b55780SDimitry Andric
101e3b55780SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
102e3b55780SDimitry Andric
103e3b55780SDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
104e3b55780SDimitry Andric      session_dictionary_name);
105e3b55780SDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
106e3b55780SDimitry Andric      python_function_name, dict);
107e3b55780SDimitry Andric
108e3b55780SDimitry Andric  if (!pfunc.IsAllocated())
109e3b55780SDimitry Andric    return false;
110e3b55780SDimitry Andric
111e3b55780SDimitry Andric  PythonObject result =
1127fa27ce4SDimitry Andric      pfunc(SWIGBridge::ToSWIGWrapper(type_impl_sp), dict);
113e3b55780SDimitry Andric
114e3b55780SDimitry Andric  // Only if everything goes okay and the function returns True we'll consider
115e3b55780SDimitry Andric  // it a match.
116e3b55780SDimitry Andric  return result.get() == Py_True;
117e3b55780SDimitry Andric}
118e3b55780SDimitry Andric
1197fa27ce4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonCallTypeScript(
12077fc4c14SDimitry Andric    const char *python_function_name, const void *session_dictionary,
12177fc4c14SDimitry Andric    const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper,
12277fc4c14SDimitry Andric    const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval) {
123cfca06d7SDimitry Andric
124cfca06d7SDimitry Andric  retval.clear();
125cfca06d7SDimitry Andric
126cfca06d7SDimitry Andric  if (!python_function_name || !session_dictionary)
127cfca06d7SDimitry Andric    return false;
128cfca06d7SDimitry Andric
129cfca06d7SDimitry Andric  PyObject *pfunc_impl = nullptr;
130cfca06d7SDimitry Andric
13177fc4c14SDimitry Andric  if (pyfunct_wrapper && *pyfunct_wrapper &&
13277fc4c14SDimitry Andric      PyFunction_Check(*pyfunct_wrapper)) {
133cfca06d7SDimitry Andric    pfunc_impl = (PyObject *)(*pyfunct_wrapper);
13477fc4c14SDimitry Andric    if (pfunc_impl->ob_refcnt == 1) {
135cfca06d7SDimitry Andric      Py_XDECREF(pfunc_impl);
136cfca06d7SDimitry Andric      pfunc_impl = NULL;
137cfca06d7SDimitry Andric    }
138cfca06d7SDimitry Andric  }
139cfca06d7SDimitry Andric
140cfca06d7SDimitry Andric  PyObject *py_dict = (PyObject *)session_dictionary;
141cfca06d7SDimitry Andric  if (!PythonDictionary::Check(py_dict))
142cfca06d7SDimitry Andric    return true;
143cfca06d7SDimitry Andric
144cfca06d7SDimitry Andric  PythonDictionary dict(PyRefType::Borrowed, py_dict);
145cfca06d7SDimitry Andric
146cfca06d7SDimitry Andric  PyErr_Cleaner pyerr_cleanup(true); // show Python errors
147cfca06d7SDimitry Andric
148cfca06d7SDimitry Andric  PythonCallable pfunc(PyRefType::Borrowed, pfunc_impl);
149cfca06d7SDimitry Andric
15077fc4c14SDimitry Andric  if (!pfunc.IsAllocated()) {
15177fc4c14SDimitry Andric    pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
15277fc4c14SDimitry Andric        python_function_name, dict);
153cfca06d7SDimitry Andric    if (!pfunc.IsAllocated())
154cfca06d7SDimitry Andric      return false;
155cfca06d7SDimitry Andric
15677fc4c14SDimitry Andric    if (pyfunct_wrapper) {
157cfca06d7SDimitry Andric      *pyfunct_wrapper = pfunc.get();
158cfca06d7SDimitry Andric      Py_XINCREF(pfunc.get());
159cfca06d7SDimitry Andric    }
160cfca06d7SDimitry Andric  }
161cfca06d7SDimitry Andric
162cfca06d7SDimitry Andric  PythonObject result;
163cfca06d7SDimitry Andric  auto argc = pfunc.GetArgInfo();
164cfca06d7SDimitry Andric  if (!argc) {
165cfca06d7SDimitry Andric    llvm::consumeError(argc.takeError());
166cfca06d7SDimitry Andric    return false;
167cfca06d7SDimitry Andric  }
168cfca06d7SDimitry Andric
1697fa27ce4SDimitry Andric  PythonObject value_arg = SWIGBridge::ToSWIGWrapper(valobj_sp);
170cfca06d7SDimitry Andric
171cfca06d7SDimitry Andric  if (argc.get().max_positional_args < 3)
172cfca06d7SDimitry Andric    result = pfunc(value_arg, dict);
173cfca06d7SDimitry Andric  else
1747fa27ce4SDimitry Andric    result = pfunc(value_arg, dict, SWIGBridge::ToSWIGWrapper(*options_sp));
175cfca06d7SDimitry Andric
176cfca06d7SDimitry Andric  retval = result.Str().GetString().str();
177cfca06d7SDimitry Andric
178cfca06d7SDimitry Andric  return true;
179cfca06d7SDimitry Andric}
180cfca06d7SDimitry Andric
1817fa27ce4SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateSyntheticProvider(
18277fc4c14SDimitry Andric    const char *python_class_name, const char *session_dictionary_name,
18377fc4c14SDimitry Andric    const lldb::ValueObjectSP &valobj_sp) {
18477fc4c14SDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
18577fc4c14SDimitry Andric      !session_dictionary_name)
1866f8fc217SDimitry Andric    return PythonObject();
187cfca06d7SDimitry Andric
188cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
189cfca06d7SDimitry Andric
19077fc4c14SDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
19177fc4c14SDimitry Andric      session_dictionary_name);
19277fc4c14SDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
19377fc4c14SDimitry Andric      python_class_name, dict);
194cfca06d7SDimitry Andric
195cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
1966f8fc217SDimitry Andric    return PythonObject();
197cfca06d7SDimitry Andric
1987fa27ce4SDimitry Andric  auto sb_value = std::unique_ptr<lldb::SBValue>(new lldb::SBValue(valobj_sp));
199cfca06d7SDimitry Andric  sb_value->SetPreferSyntheticValue(false);
200cfca06d7SDimitry Andric
2017fa27ce4SDimitry Andric  PythonObject val_arg = SWIGBridge::ToSWIGWrapper(std::move(sb_value));
202cfca06d7SDimitry Andric  if (!val_arg.IsAllocated())
2036f8fc217SDimitry Andric    return PythonObject();
204cfca06d7SDimitry Andric
205cfca06d7SDimitry Andric  PythonObject result = pfunc(val_arg, dict);
206cfca06d7SDimitry Andric
207cfca06d7SDimitry Andric  if (result.IsAllocated())
2086f8fc217SDimitry Andric    return result;
209cfca06d7SDimitry Andric
2106f8fc217SDimitry Andric  return PythonObject();
211cfca06d7SDimitry Andric}
212cfca06d7SDimitry Andric
2137fa27ce4SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateCommandObject(
21477fc4c14SDimitry Andric    const char *python_class_name, const char *session_dictionary_name,
21577fc4c14SDimitry Andric    lldb::DebuggerSP debugger_sp) {
21677fc4c14SDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
21777fc4c14SDimitry Andric      !session_dictionary_name)
2186f8fc217SDimitry Andric    return PythonObject();
219cfca06d7SDimitry Andric
220cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
22177fc4c14SDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
22277fc4c14SDimitry Andric      session_dictionary_name);
22377fc4c14SDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
22477fc4c14SDimitry Andric      python_class_name, dict);
225cfca06d7SDimitry Andric
226cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
2276f8fc217SDimitry Andric    return PythonObject();
228cfca06d7SDimitry Andric
2297fa27ce4SDimitry Andric  return pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger_sp)), dict);
230cfca06d7SDimitry Andric}
231cfca06d7SDimitry Andric
2327fa27ce4SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedBreakpointResolver(
233f65dcba8SDimitry Andric    const char *python_class_name, const char *session_dictionary_name,
23477fc4c14SDimitry Andric    const StructuredDataImpl &args_impl,
235f65dcba8SDimitry Andric    const lldb::BreakpointSP &breakpoint_sp) {
236f65dcba8SDimitry Andric
23777fc4c14SDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
23877fc4c14SDimitry Andric      !session_dictionary_name)
2396f8fc217SDimitry Andric    return PythonObject();
240cfca06d7SDimitry Andric
241cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
242cfca06d7SDimitry Andric
24377fc4c14SDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
24477fc4c14SDimitry Andric      session_dictionary_name);
24577fc4c14SDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
24677fc4c14SDimitry Andric      python_class_name, dict);
247cfca06d7SDimitry Andric
248cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
2496f8fc217SDimitry Andric    return PythonObject();
250cfca06d7SDimitry Andric
25177fc4c14SDimitry Andric  PythonObject result =
2527fa27ce4SDimitry Andric      pfunc(SWIGBridge::ToSWIGWrapper(breakpoint_sp), SWIGBridge::ToSWIGWrapper(args_impl), dict);
25377fc4c14SDimitry Andric  // FIXME: At this point we should check that the class we found supports all
25477fc4c14SDimitry Andric  // the methods that we need.
255cfca06d7SDimitry Andric
25677fc4c14SDimitry Andric  if (result.IsAllocated()) {
257cfca06d7SDimitry Andric    // Check that __callback__ is defined:
258cfca06d7SDimitry Andric    auto callback_func = result.ResolveName<PythonCallable>("__callback__");
259cfca06d7SDimitry Andric    if (callback_func.IsAllocated())
2606f8fc217SDimitry Andric      return result;
261cfca06d7SDimitry Andric  }
2626f8fc217SDimitry Andric  return PythonObject();
263cfca06d7SDimitry Andric}
264cfca06d7SDimitry Andric
2657fa27ce4SDimitry Andricunsigned int lldb_private::python::SWIGBridge::LLDBSwigPythonCallBreakpointResolver(
26677fc4c14SDimitry Andric    void *implementor, const char *method_name,
26777fc4c14SDimitry Andric    lldb_private::SymbolContext *sym_ctx) {
268cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(false);
269cfca06d7SDimitry Andric  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
270cfca06d7SDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>(method_name);
271cfca06d7SDimitry Andric
272cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
273cfca06d7SDimitry Andric    return 0;
274cfca06d7SDimitry Andric
2757fa27ce4SDimitry Andric  PythonObject result = sym_ctx ? pfunc(SWIGBridge::ToSWIGWrapper(*sym_ctx)) : pfunc();
276cfca06d7SDimitry Andric
27777fc4c14SDimitry Andric  if (PyErr_Occurred()) {
278cfca06d7SDimitry Andric    PyErr_Print();
279cfca06d7SDimitry Andric    PyErr_Clear();
280cfca06d7SDimitry Andric    return 0;
281cfca06d7SDimitry Andric  }
282cfca06d7SDimitry Andric
283cfca06d7SDimitry Andric  // The callback will return a bool, but we're need to also return ints
284cfca06d7SDimitry Andric  // so we're squirrelling the bool through as an int...  And if you return
285cfca06d7SDimitry Andric  // nothing, we'll continue.
286cfca06d7SDimitry Andric  if (strcmp(method_name, "__callback__") == 0) {
287cfca06d7SDimitry Andric    if (result.get() == Py_False)
288cfca06d7SDimitry Andric      return 0;
289cfca06d7SDimitry Andric    else
290cfca06d7SDimitry Andric      return 1;
291cfca06d7SDimitry Andric  }
292cfca06d7SDimitry Andric
293cfca06d7SDimitry Andric  long long ret_val = unwrapOrSetPythonException(As<long long>(result));
294cfca06d7SDimitry Andric
295cfca06d7SDimitry Andric  if (PyErr_Occurred()) {
296cfca06d7SDimitry Andric    PyErr_Print();
297cfca06d7SDimitry Andric    PyErr_Clear();
298cfca06d7SDimitry Andric    return 0;
299cfca06d7SDimitry Andric  }
300cfca06d7SDimitry Andric
301cfca06d7SDimitry Andric  return ret_val;
302cfca06d7SDimitry Andric}
303cfca06d7SDimitry Andric
3047fa27ce4SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedStopHook(
30577fc4c14SDimitry Andric    lldb::TargetSP target_sp, const char *python_class_name,
30677fc4c14SDimitry Andric    const char *session_dictionary_name, const StructuredDataImpl &args_impl,
30777fc4c14SDimitry Andric    Status &error) {
308b60736ecSDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0') {
309b60736ecSDimitry Andric    error.SetErrorString("Empty class name.");
3106f8fc217SDimitry Andric    return PythonObject();
311b60736ecSDimitry Andric  }
312b60736ecSDimitry Andric  if (!session_dictionary_name) {
313b60736ecSDimitry Andric    error.SetErrorString("No session dictionary");
3146f8fc217SDimitry Andric    return PythonObject();
315b60736ecSDimitry Andric  }
316b60736ecSDimitry Andric
317b60736ecSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
318b60736ecSDimitry Andric
31977fc4c14SDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
320b60736ecSDimitry Andric      session_dictionary_name);
32177fc4c14SDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
322b60736ecSDimitry Andric      python_class_name, dict);
323b60736ecSDimitry Andric
324b60736ecSDimitry Andric  if (!pfunc.IsAllocated()) {
325b60736ecSDimitry Andric    error.SetErrorStringWithFormat("Could not find class: %s.",
326b60736ecSDimitry Andric                                   python_class_name);
3276f8fc217SDimitry Andric    return PythonObject();
328b60736ecSDimitry Andric  }
329b60736ecSDimitry Andric
33077fc4c14SDimitry Andric  PythonObject result =
3317fa27ce4SDimitry Andric      pfunc(SWIGBridge::ToSWIGWrapper(target_sp), SWIGBridge::ToSWIGWrapper(args_impl), dict);
332b60736ecSDimitry Andric
33377fc4c14SDimitry Andric  if (result.IsAllocated()) {
334b60736ecSDimitry Andric    // Check that the handle_stop callback is defined:
335b60736ecSDimitry Andric    auto callback_func = result.ResolveName<PythonCallable>("handle_stop");
336b60736ecSDimitry Andric    if (callback_func.IsAllocated()) {
337b60736ecSDimitry Andric      if (auto args_info = callback_func.GetArgInfo()) {
338b60736ecSDimitry Andric        size_t num_args = (*args_info).max_positional_args;
339b60736ecSDimitry Andric        if (num_args != 2) {
34077fc4c14SDimitry Andric          error.SetErrorStringWithFormat(
34177fc4c14SDimitry Andric              "Wrong number of args for "
342b60736ecSDimitry Andric              "handle_stop callback, should be 2 (excluding self), got: %zu",
343b60736ecSDimitry Andric              num_args);
3446f8fc217SDimitry Andric          return PythonObject();
345b60736ecSDimitry Andric        } else
3466f8fc217SDimitry Andric          return result;
347b60736ecSDimitry Andric      } else {
348b60736ecSDimitry Andric        error.SetErrorString("Couldn't get num arguments for handle_stop "
349b60736ecSDimitry Andric                             "callback.");
3506f8fc217SDimitry Andric        return PythonObject();
351b60736ecSDimitry Andric      }
3526f8fc217SDimitry Andric      return result;
35377fc4c14SDimitry Andric    } else {
354b60736ecSDimitry Andric      error.SetErrorStringWithFormat("Class \"%s\" is missing the required "
355b60736ecSDimitry Andric                                     "handle_stop callback.",
356b60736ecSDimitry Andric                                     python_class_name);
357b60736ecSDimitry Andric    }
358b60736ecSDimitry Andric  }
3596f8fc217SDimitry Andric  return PythonObject();
360b60736ecSDimitry Andric}
361b60736ecSDimitry Andric
3627fa27ce4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonStopHookCallHandleStop(
36377fc4c14SDimitry Andric    void *implementor, lldb::ExecutionContextRefSP exc_ctx_sp,
36477fc4c14SDimitry Andric    lldb::StreamSP stream) {
365b60736ecSDimitry Andric  // handle_stop will return a bool with the meaning "should_stop"...
366b60736ecSDimitry Andric  // If you return nothing we'll assume we are going to stop.
367b60736ecSDimitry Andric  // Also any errors should return true, since we should stop on error.
368b60736ecSDimitry Andric
369b60736ecSDimitry Andric  PyErr_Cleaner py_err_cleaner(false);
370b60736ecSDimitry Andric  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
371b60736ecSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("handle_stop");
372b60736ecSDimitry Andric
373b60736ecSDimitry Andric  if (!pfunc.IsAllocated())
374b60736ecSDimitry Andric    return true;
375b60736ecSDimitry Andric
376ac9a064cSDimitry Andric  std::shared_ptr<lldb::SBStream> sb_stream = std::make_shared<lldb::SBStream>();
377ac9a064cSDimitry Andric  PythonObject sb_stream_arg = SWIGBridge::ToSWIGWrapper(sb_stream);
37877fc4c14SDimitry Andric  PythonObject result =
3797fa27ce4SDimitry Andric      pfunc(SWIGBridge::ToSWIGWrapper(std::move(exc_ctx_sp)), sb_stream_arg);
380b60736ecSDimitry Andric
38177fc4c14SDimitry Andric  if (PyErr_Occurred()) {
382b60736ecSDimitry Andric    stream->PutCString("Python error occurred handling stop-hook.");
383b60736ecSDimitry Andric    PyErr_Print();
384b60736ecSDimitry Andric    PyErr_Clear();
385b60736ecSDimitry Andric    return true;
386b60736ecSDimitry Andric  }
387b60736ecSDimitry Andric
388b60736ecSDimitry Andric  // Now add the result to the output stream.  SBStream only
389b60736ecSDimitry Andric  // makes an internally help StreamString which I can't interpose, so I
390b60736ecSDimitry Andric  // have to copy it over here.
39177fc4c14SDimitry Andric  stream->PutCString(sb_stream->GetData());
392ac9a064cSDimitry Andric  sb_stream_arg.release();
393b60736ecSDimitry Andric
394b60736ecSDimitry Andric  if (result.get() == Py_False)
395b60736ecSDimitry Andric    return false;
396b60736ecSDimitry Andric  else
397b60736ecSDimitry Andric    return true;
398b60736ecSDimitry Andric}
399b60736ecSDimitry Andric
40077fc4c14SDimitry Andric// wrapper that calls an optional instance member of an object taking no
40177fc4c14SDimitry Andric// arguments
40277fc4c14SDimitry Andricstatic PyObject *LLDBSwigPython_CallOptionalMember(
40377fc4c14SDimitry Andric    PyObject * implementor, char *callee_name,
40477fc4c14SDimitry Andric    PyObject *ret_if_not_found = Py_None, bool *was_found = NULL) {
405cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(false);
406cfca06d7SDimitry Andric
407cfca06d7SDimitry Andric  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
408cfca06d7SDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>(callee_name);
409cfca06d7SDimitry Andric
41077fc4c14SDimitry Andric  if (!pfunc.IsAllocated()) {
411cfca06d7SDimitry Andric    if (was_found)
412cfca06d7SDimitry Andric      *was_found = false;
413cfca06d7SDimitry Andric    Py_XINCREF(ret_if_not_found);
414cfca06d7SDimitry Andric    return ret_if_not_found;
415cfca06d7SDimitry Andric  }
416cfca06d7SDimitry Andric
417cfca06d7SDimitry Andric  if (was_found)
418cfca06d7SDimitry Andric    *was_found = true;
419cfca06d7SDimitry Andric
420cfca06d7SDimitry Andric  PythonObject result = pfunc();
421cfca06d7SDimitry Andric  return result.release();
422cfca06d7SDimitry Andric}
423cfca06d7SDimitry Andric
4247fa27ce4SDimitry Andricsize_t lldb_private::python::SWIGBridge::LLDBSwigPython_CalculateNumChildren(PyObject * implementor,
42577fc4c14SDimitry Andric                                                         uint32_t max) {
426cfca06d7SDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
427cfca06d7SDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("num_children");
428cfca06d7SDimitry Andric
429cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
430cfca06d7SDimitry Andric    return 0;
431cfca06d7SDimitry Andric
432cfca06d7SDimitry Andric  auto arg_info = pfunc.GetArgInfo();
433cfca06d7SDimitry Andric  if (!arg_info) {
434cfca06d7SDimitry Andric    llvm::consumeError(arg_info.takeError());
435cfca06d7SDimitry Andric    return 0;
436cfca06d7SDimitry Andric  }
437cfca06d7SDimitry Andric
438cfca06d7SDimitry Andric  size_t ret_val;
439cfca06d7SDimitry Andric  if (arg_info.get().max_positional_args < 1)
440cfca06d7SDimitry Andric    ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call()));
441cfca06d7SDimitry Andric  else
44277fc4c14SDimitry Andric    ret_val = unwrapOrSetPythonException(
44377fc4c14SDimitry Andric        As<long long>(pfunc.Call(PythonInteger(max))));
444cfca06d7SDimitry Andric
44577fc4c14SDimitry Andric  if (PyErr_Occurred()) {
446cfca06d7SDimitry Andric    PyErr_Print();
447cfca06d7SDimitry Andric    PyErr_Clear();
448cfca06d7SDimitry Andric    return 0;
449cfca06d7SDimitry Andric  }
450cfca06d7SDimitry Andric
451cfca06d7SDimitry Andric  if (arg_info.get().max_positional_args < 1)
452cfca06d7SDimitry Andric    ret_val = std::min(ret_val, static_cast<size_t>(max));
453cfca06d7SDimitry Andric
454cfca06d7SDimitry Andric  return ret_val;
455cfca06d7SDimitry Andric}
456cfca06d7SDimitry Andric
4577fa27ce4SDimitry AndricPyObject *lldb_private::python::SWIGBridge::LLDBSwigPython_GetChildAtIndex(PyObject * implementor,
45877fc4c14SDimitry Andric                                                       uint32_t idx) {
459cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
460cfca06d7SDimitry Andric
461cfca06d7SDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
462cfca06d7SDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index");
463cfca06d7SDimitry Andric
464cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
465cfca06d7SDimitry Andric    return nullptr;
466cfca06d7SDimitry Andric
467cfca06d7SDimitry Andric  PythonObject result = pfunc(PythonInteger(idx));
468cfca06d7SDimitry Andric
469cfca06d7SDimitry Andric  if (!result.IsAllocated())
470cfca06d7SDimitry Andric    return nullptr;
471cfca06d7SDimitry Andric
472cfca06d7SDimitry Andric  lldb::SBValue *sbvalue_ptr = nullptr;
47377fc4c14SDimitry Andric  if (SWIG_ConvertPtr(result.get(), (void **)&sbvalue_ptr,
47477fc4c14SDimitry Andric                      SWIGTYPE_p_lldb__SBValue, 0) == -1)
475cfca06d7SDimitry Andric    return nullptr;
476cfca06d7SDimitry Andric
477cfca06d7SDimitry Andric  if (sbvalue_ptr == nullptr)
478cfca06d7SDimitry Andric    return nullptr;
479cfca06d7SDimitry Andric
480cfca06d7SDimitry Andric  return result.release();
481cfca06d7SDimitry Andric}
482cfca06d7SDimitry Andric
4837fa27ce4SDimitry Andricint lldb_private::python::SWIGBridge::LLDBSwigPython_GetIndexOfChildWithName(
48477fc4c14SDimitry Andric    PyObject * implementor, const char *child_name) {
485cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
486cfca06d7SDimitry Andric
487cfca06d7SDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
488cfca06d7SDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("get_child_index");
489cfca06d7SDimitry Andric
490cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
491cfca06d7SDimitry Andric    return UINT32_MAX;
492cfca06d7SDimitry Andric
493cfca06d7SDimitry Andric  llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name));
494cfca06d7SDimitry Andric
49577fc4c14SDimitry Andric  long long retval =
49677fc4c14SDimitry Andric      unwrapOrSetPythonException(As<long long>(std::move(result)));
497cfca06d7SDimitry Andric
498cfca06d7SDimitry Andric  if (PyErr_Occurred()) {
499cfca06d7SDimitry Andric    PyErr_Clear(); // FIXME print this? do something else
500cfca06d7SDimitry Andric    return UINT32_MAX;
501cfca06d7SDimitry Andric  }
502cfca06d7SDimitry Andric
503cfca06d7SDimitry Andric  if (retval >= 0)
504cfca06d7SDimitry Andric    return (uint32_t)retval;
505cfca06d7SDimitry Andric
506cfca06d7SDimitry Andric  return UINT32_MAX;
507cfca06d7SDimitry Andric}
508cfca06d7SDimitry Andric
5097fa27ce4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPython_UpdateSynthProviderInstance(PyObject *
51077fc4c14SDimitry Andric                                                              implementor) {
511cfca06d7SDimitry Andric  bool ret_val = false;
512cfca06d7SDimitry Andric
513cfca06d7SDimitry Andric  static char callee_name[] = "update";
514cfca06d7SDimitry Andric
51577fc4c14SDimitry Andric  PyObject *py_return =
51677fc4c14SDimitry Andric      LLDBSwigPython_CallOptionalMember(implementor, callee_name);
517cfca06d7SDimitry Andric
518cfca06d7SDimitry Andric  if (py_return == Py_True)
519cfca06d7SDimitry Andric    ret_val = true;
520cfca06d7SDimitry Andric
521cfca06d7SDimitry Andric  Py_XDECREF(py_return);
522cfca06d7SDimitry Andric
523cfca06d7SDimitry Andric  return ret_val;
524cfca06d7SDimitry Andric}
525cfca06d7SDimitry Andric
5267fa27ce4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPython_MightHaveChildrenSynthProviderInstance(
52777fc4c14SDimitry Andric    PyObject * implementor) {
528cfca06d7SDimitry Andric  bool ret_val = false;
529cfca06d7SDimitry Andric
530cfca06d7SDimitry Andric  static char callee_name[] = "has_children";
531cfca06d7SDimitry Andric
53277fc4c14SDimitry Andric  PyObject *py_return =
53377fc4c14SDimitry Andric      LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_True);
534cfca06d7SDimitry Andric
535cfca06d7SDimitry Andric  if (py_return == Py_True)
536cfca06d7SDimitry Andric    ret_val = true;
537cfca06d7SDimitry Andric
538cfca06d7SDimitry Andric  Py_XDECREF(py_return);
539cfca06d7SDimitry Andric
540cfca06d7SDimitry Andric  return ret_val;
541cfca06d7SDimitry Andric}
542cfca06d7SDimitry Andric
5437fa27ce4SDimitry AndricPyObject *lldb_private::python::SWIGBridge::LLDBSwigPython_GetValueSynthProviderInstance(
54477fc4c14SDimitry Andric    PyObject * implementor) {
545cfca06d7SDimitry Andric  PyObject *ret_val = nullptr;
546cfca06d7SDimitry Andric
547cfca06d7SDimitry Andric  static char callee_name[] = "get_value";
548cfca06d7SDimitry Andric
54977fc4c14SDimitry Andric  PyObject *py_return =
55077fc4c14SDimitry Andric      LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_None);
551cfca06d7SDimitry Andric
552cfca06d7SDimitry Andric  if (py_return == Py_None || py_return == nullptr)
553cfca06d7SDimitry Andric    ret_val = nullptr;
554cfca06d7SDimitry Andric
555cfca06d7SDimitry Andric  lldb::SBValue *sbvalue_ptr = NULL;
556cfca06d7SDimitry Andric
55777fc4c14SDimitry Andric  if (SWIG_ConvertPtr(py_return, (void **)&sbvalue_ptr,
55877fc4c14SDimitry Andric                      SWIGTYPE_p_lldb__SBValue, 0) == -1)
559cfca06d7SDimitry Andric    ret_val = nullptr;
560cfca06d7SDimitry Andric  else if (sbvalue_ptr == NULL)
561cfca06d7SDimitry Andric    ret_val = nullptr;
562cfca06d7SDimitry Andric  else
563cfca06d7SDimitry Andric    ret_val = py_return;
564cfca06d7SDimitry Andric
565cfca06d7SDimitry Andric  Py_XDECREF(py_return);
566cfca06d7SDimitry Andric  return ret_val;
567cfca06d7SDimitry Andric}
568cfca06d7SDimitry Andric
5697fa27ce4SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBData(PyObject * data) {
570344a3780SDimitry Andric  lldb::SBData *sb_ptr = nullptr;
571344a3780SDimitry Andric
57277fc4c14SDimitry Andric  int valid_cast =
57377fc4c14SDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0);
574344a3780SDimitry Andric
575344a3780SDimitry Andric  if (valid_cast == -1)
576344a3780SDimitry Andric    return NULL;
577344a3780SDimitry Andric
578344a3780SDimitry Andric  return sb_ptr;
579344a3780SDimitry Andric}
580344a3780SDimitry Andric
5817fa27ce4SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(PyObject * data) {
5827fa27ce4SDimitry Andric  lldb::SBBreakpoint *sb_ptr = nullptr;
5837fa27ce4SDimitry Andric
5847fa27ce4SDimitry Andric  int valid_cast =
5857fa27ce4SDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBBreakpoint, 0);
5867fa27ce4SDimitry Andric
5877fa27ce4SDimitry Andric  if (valid_cast == -1)
5887fa27ce4SDimitry Andric    return NULL;
5897fa27ce4SDimitry Andric
5907fa27ce4SDimitry Andric  return sb_ptr;
5917fa27ce4SDimitry Andric}
5927fa27ce4SDimitry Andric
5937fa27ce4SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(PyObject * data) {
5947fa27ce4SDimitry Andric  lldb::SBAttachInfo *sb_ptr = nullptr;
5957fa27ce4SDimitry Andric
5967fa27ce4SDimitry Andric  int valid_cast =
5977fa27ce4SDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBAttachInfo, 0);
5987fa27ce4SDimitry Andric
5997fa27ce4SDimitry Andric  if (valid_cast == -1)
6007fa27ce4SDimitry Andric    return NULL;
6017fa27ce4SDimitry Andric
6027fa27ce4SDimitry Andric  return sb_ptr;
6037fa27ce4SDimitry Andric}
6047fa27ce4SDimitry Andric
6057fa27ce4SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(PyObject * data) {
6067fa27ce4SDimitry Andric  lldb::SBLaunchInfo *sb_ptr = nullptr;
6077fa27ce4SDimitry Andric
6087fa27ce4SDimitry Andric  int valid_cast =
6097fa27ce4SDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBLaunchInfo, 0);
6107fa27ce4SDimitry Andric
6117fa27ce4SDimitry Andric  if (valid_cast == -1)
6127fa27ce4SDimitry Andric    return NULL;
6137fa27ce4SDimitry Andric
6147fa27ce4SDimitry Andric  return sb_ptr;
6157fa27ce4SDimitry Andric}
6167fa27ce4SDimitry Andric
6177fa27ce4SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBError(PyObject * data) {
618344a3780SDimitry Andric  lldb::SBError *sb_ptr = nullptr;
619344a3780SDimitry Andric
62077fc4c14SDimitry Andric  int valid_cast =
62177fc4c14SDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0);
622344a3780SDimitry Andric
623344a3780SDimitry Andric  if (valid_cast == -1)
624344a3780SDimitry Andric    return NULL;
625344a3780SDimitry Andric
626344a3780SDimitry Andric  return sb_ptr;
627344a3780SDimitry Andric}
628344a3780SDimitry Andric
629ac9a064cSDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBEvent(PyObject * data) {
630ac9a064cSDimitry Andric  lldb::SBEvent *sb_ptr = nullptr;
631ac9a064cSDimitry Andric
632ac9a064cSDimitry Andric  int valid_cast =
633ac9a064cSDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBEvent, 0);
634ac9a064cSDimitry Andric
635ac9a064cSDimitry Andric  if (valid_cast == -1)
636ac9a064cSDimitry Andric    return NULL;
637ac9a064cSDimitry Andric
638ac9a064cSDimitry Andric  return sb_ptr;
639ac9a064cSDimitry Andric}
640ac9a064cSDimitry Andric
641ac9a064cSDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBStream(PyObject * data) {
642ac9a064cSDimitry Andric  lldb::SBStream *sb_ptr = nullptr;
643ac9a064cSDimitry Andric
644ac9a064cSDimitry Andric  int valid_cast =
645ac9a064cSDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBStream, 0);
646ac9a064cSDimitry Andric
647ac9a064cSDimitry Andric  if (valid_cast == -1)
648ac9a064cSDimitry Andric    return NULL;
649ac9a064cSDimitry Andric
650ac9a064cSDimitry Andric  return sb_ptr;
651ac9a064cSDimitry Andric}
652ac9a064cSDimitry Andric
6537fa27ce4SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBValue(PyObject * data) {
654cfca06d7SDimitry Andric  lldb::SBValue *sb_ptr = NULL;
655cfca06d7SDimitry Andric
65677fc4c14SDimitry Andric  int valid_cast =
65777fc4c14SDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
658cfca06d7SDimitry Andric
659cfca06d7SDimitry Andric  if (valid_cast == -1)
660cfca06d7SDimitry Andric    return NULL;
661cfca06d7SDimitry Andric
662cfca06d7SDimitry Andric  return sb_ptr;
663cfca06d7SDimitry Andric}
664cfca06d7SDimitry Andric
6657fa27ce4SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *
66677fc4c14SDimitry Andric                                                                    data) {
667c0981da4SDimitry Andric  lldb::SBMemoryRegionInfo *sb_ptr = NULL;
668c0981da4SDimitry Andric
66977fc4c14SDimitry Andric  int valid_cast = SWIG_ConvertPtr(data, (void **)&sb_ptr,
67077fc4c14SDimitry Andric                                   SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0);
671c0981da4SDimitry Andric
672c0981da4SDimitry Andric  if (valid_cast == -1)
673c0981da4SDimitry Andric    return NULL;
674c0981da4SDimitry Andric
675c0981da4SDimitry Andric  return sb_ptr;
676c0981da4SDimitry Andric}
677c0981da4SDimitry Andric
6787fa27ce4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonCallCommand(
67977fc4c14SDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
68077fc4c14SDimitry Andric    lldb::DebuggerSP debugger, const char *args,
681cfca06d7SDimitry Andric    lldb_private::CommandReturnObject &cmd_retobj,
68277fc4c14SDimitry Andric    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
683cfca06d7SDimitry Andric
684cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
68577fc4c14SDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
68677fc4c14SDimitry Andric      session_dictionary_name);
68777fc4c14SDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
68877fc4c14SDimitry Andric      python_function_name, dict);
689cfca06d7SDimitry Andric
690cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
691cfca06d7SDimitry Andric    return false;
692cfca06d7SDimitry Andric
693cfca06d7SDimitry Andric  auto argc = pfunc.GetArgInfo();
694cfca06d7SDimitry Andric  if (!argc) {
695cfca06d7SDimitry Andric    llvm::consumeError(argc.takeError());
696cfca06d7SDimitry Andric    return false;
697cfca06d7SDimitry Andric  }
6987fa27ce4SDimitry Andric  PythonObject debugger_arg = SWIGBridge::ToSWIGWrapper(std::move(debugger));
6997fa27ce4SDimitry Andric  auto cmd_retobj_arg = SWIGBridge::ToSWIGWrapper(cmd_retobj);
700cfca06d7SDimitry Andric
701cfca06d7SDimitry Andric  if (argc.get().max_positional_args < 5u)
7026f8fc217SDimitry Andric    pfunc(debugger_arg, PythonString(args), cmd_retobj_arg.obj(), dict);
703cfca06d7SDimitry Andric  else
70477fc4c14SDimitry Andric    pfunc(debugger_arg, PythonString(args),
7057fa27ce4SDimitry Andric          SWIGBridge::ToSWIGWrapper(std::move(exe_ctx_ref_sp)), cmd_retobj_arg.obj(), dict);
706cfca06d7SDimitry Andric
707cfca06d7SDimitry Andric  return true;
708cfca06d7SDimitry Andric}
709cfca06d7SDimitry Andric
7107fa27ce4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonCallCommandObject(
71177fc4c14SDimitry Andric    PyObject *implementor, lldb::DebuggerSP debugger, const char *args,
712cfca06d7SDimitry Andric    lldb_private::CommandReturnObject &cmd_retobj,
71377fc4c14SDimitry Andric    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
714cfca06d7SDimitry Andric
715cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
716cfca06d7SDimitry Andric
717cfca06d7SDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
718cfca06d7SDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("__call__");
719cfca06d7SDimitry Andric
720cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
721cfca06d7SDimitry Andric    return false;
722cfca06d7SDimitry Andric
7237fa27ce4SDimitry Andric  auto cmd_retobj_arg = SWIGBridge::ToSWIGWrapper(cmd_retobj);
724cfca06d7SDimitry Andric
7257fa27ce4SDimitry Andric  pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger)), PythonString(args),
7267fa27ce4SDimitry Andric        SWIGBridge::ToSWIGWrapper(exe_ctx_ref_sp), cmd_retobj_arg.obj());
727cfca06d7SDimitry Andric
728cfca06d7SDimitry Andric  return true;
729cfca06d7SDimitry Andric}
730cfca06d7SDimitry Andric
731ac9a064cSDimitry Andricstd::optional<std::string>
732ac9a064cSDimitry Andriclldb_private::python::SWIGBridge::LLDBSwigPythonGetRepeatCommandForScriptedCommand(PyObject *implementor,
733ac9a064cSDimitry Andric                                               std::string &command) {
734ac9a064cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
735ac9a064cSDimitry Andric
736ac9a064cSDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
737ac9a064cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("get_repeat_command");
738ac9a064cSDimitry Andric  // If not implemented, repeat the exact command.
739ac9a064cSDimitry Andric  if (!pfunc.IsAllocated())
740ac9a064cSDimitry Andric    return std::nullopt;
741ac9a064cSDimitry Andric
742ac9a064cSDimitry Andric  PythonString command_str(command);
743ac9a064cSDimitry Andric  PythonObject result = pfunc(command_str);
744ac9a064cSDimitry Andric
745ac9a064cSDimitry Andric  // A return of None is the equivalent of nullopt - means repeat
746ac9a064cSDimitry Andric  // the command as is:
747ac9a064cSDimitry Andric  if (result.IsNone())
748ac9a064cSDimitry Andric    return std::nullopt;
749ac9a064cSDimitry Andric
750ac9a064cSDimitry Andric  return result.Str().GetString().str();
751ac9a064cSDimitry Andric}
752ac9a064cSDimitry Andric
753ac9a064cSDimitry Andric#include "lldb/Interpreter/CommandReturnObject.h"
754ac9a064cSDimitry Andric
755ac9a064cSDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonCallParsedCommandObject(
756ac9a064cSDimitry Andric    PyObject *implementor, lldb::DebuggerSP debugger, lldb_private::StructuredDataImpl &args_impl,
757ac9a064cSDimitry Andric    lldb_private::CommandReturnObject &cmd_retobj,
758ac9a064cSDimitry Andric    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
759ac9a064cSDimitry Andric
760ac9a064cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
761ac9a064cSDimitry Andric
762ac9a064cSDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
763ac9a064cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("__call__");
764ac9a064cSDimitry Andric
765ac9a064cSDimitry Andric  if (!pfunc.IsAllocated()) {
766ac9a064cSDimitry Andric    cmd_retobj.AppendError("Could not find '__call__' method in implementation class");
767ac9a064cSDimitry Andric    return false;
768ac9a064cSDimitry Andric  }
769ac9a064cSDimitry Andric
770ac9a064cSDimitry Andric  pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger)), SWIGBridge::ToSWIGWrapper(args_impl),
771ac9a064cSDimitry Andric        SWIGBridge::ToSWIGWrapper(exe_ctx_ref_sp), SWIGBridge::ToSWIGWrapper(cmd_retobj).obj());
772ac9a064cSDimitry Andric
773ac9a064cSDimitry Andric  return true;
774ac9a064cSDimitry Andric}
775ac9a064cSDimitry Andric
7767fa27ce4SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSWIGPythonCreateOSPlugin(
77777fc4c14SDimitry Andric    const char *python_class_name, const char *session_dictionary_name,
77877fc4c14SDimitry Andric    const lldb::ProcessSP &process_sp) {
77977fc4c14SDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
78077fc4c14SDimitry Andric      !session_dictionary_name)
7816f8fc217SDimitry Andric    return PythonObject();
782cfca06d7SDimitry Andric
783cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
784cfca06d7SDimitry Andric
78577fc4c14SDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
78677fc4c14SDimitry Andric      session_dictionary_name);
78777fc4c14SDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
78877fc4c14SDimitry Andric      python_class_name, dict);
789cfca06d7SDimitry Andric
790cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
7916f8fc217SDimitry Andric    return PythonObject();
792cfca06d7SDimitry Andric
7937fa27ce4SDimitry Andric  return pfunc(SWIGBridge::ToSWIGWrapper(process_sp));
794cfca06d7SDimitry Andric}
795cfca06d7SDimitry Andric
7967fa27ce4SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSWIGPython_CreateFrameRecognizer(
79777fc4c14SDimitry Andric    const char *python_class_name, const char *session_dictionary_name) {
79877fc4c14SDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
79977fc4c14SDimitry Andric      !session_dictionary_name)
8006f8fc217SDimitry Andric    return PythonObject();
801cfca06d7SDimitry Andric
802cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
803cfca06d7SDimitry Andric
80477fc4c14SDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
80577fc4c14SDimitry Andric      session_dictionary_name);
80677fc4c14SDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
80777fc4c14SDimitry Andric      python_class_name, dict);
808cfca06d7SDimitry Andric
809cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
8106f8fc217SDimitry Andric    return PythonObject();
811cfca06d7SDimitry Andric
8126f8fc217SDimitry Andric  return pfunc();
813cfca06d7SDimitry Andric}
814cfca06d7SDimitry Andric
8157fa27ce4SDimitry AndricPyObject *lldb_private::python::SWIGBridge::LLDBSwigPython_GetRecognizedArguments(
81677fc4c14SDimitry Andric    PyObject * implementor, const lldb::StackFrameSP &frame_sp) {
817cfca06d7SDimitry Andric  static char callee_name[] = "get_recognized_arguments";
818cfca06d7SDimitry Andric
8197fa27ce4SDimitry Andric  PythonObject arg = SWIGBridge::ToSWIGWrapper(frame_sp);
820cfca06d7SDimitry Andric
821cfca06d7SDimitry Andric  PythonString str(callee_name);
82277fc4c14SDimitry Andric  PyObject *result =
82377fc4c14SDimitry Andric      PyObject_CallMethodObjArgs(implementor, str.get(), arg.get(), NULL);
824cfca06d7SDimitry Andric  return result;
825cfca06d7SDimitry Andric}
826cfca06d7SDimitry Andric
8277fa27ce4SDimitry Andricvoid *lldb_private::python::SWIGBridge::LLDBSWIGPython_GetDynamicSetting(
82877fc4c14SDimitry Andric    void *module, const char *setting, const lldb::TargetSP &target_sp) {
829cfca06d7SDimitry Andric  if (!module || !setting)
830cfca06d7SDimitry Andric    Py_RETURN_NONE;
831cfca06d7SDimitry Andric
832cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
833cfca06d7SDimitry Andric  PythonObject py_module(PyRefType::Borrowed, (PyObject *)module);
834cfca06d7SDimitry Andric  auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting");
835cfca06d7SDimitry Andric
836cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
837cfca06d7SDimitry Andric    Py_RETURN_NONE;
838cfca06d7SDimitry Andric
8397fa27ce4SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(target_sp), PythonString(setting));
840cfca06d7SDimitry Andric
841cfca06d7SDimitry Andric  return result.release();
842cfca06d7SDimitry Andric}
843cfca06d7SDimitry Andric
8447fa27ce4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordProcess(
845f65dcba8SDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
846f65dcba8SDimitry Andric    const lldb::ProcessSP &process, std::string &output) {
847cfca06d7SDimitry Andric
84877fc4c14SDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
84977fc4c14SDimitry Andric      !session_dictionary_name)
850cfca06d7SDimitry Andric    return false;
851cfca06d7SDimitry Andric
852cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
853cfca06d7SDimitry Andric
85477fc4c14SDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
85577fc4c14SDimitry Andric      session_dictionary_name);
85677fc4c14SDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
85777fc4c14SDimitry Andric      python_function_name, dict);
858cfca06d7SDimitry Andric
859cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
860cfca06d7SDimitry Andric    return false;
861cfca06d7SDimitry Andric
8627fa27ce4SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(process), dict);
863cfca06d7SDimitry Andric
864cfca06d7SDimitry Andric  output = result.Str().GetString().str();
865cfca06d7SDimitry Andric
866cfca06d7SDimitry Andric  return true;
867cfca06d7SDimitry Andric}
868cfca06d7SDimitry Andric
8697fa27ce4SDimitry Andricstd::optional<std::string> lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordThread(
87077fc4c14SDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
87177fc4c14SDimitry Andric    lldb::ThreadSP thread) {
87277fc4c14SDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
87377fc4c14SDimitry Andric      !session_dictionary_name)
874e3b55780SDimitry Andric    return std::nullopt;
875cfca06d7SDimitry Andric
876cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
877cfca06d7SDimitry Andric
87877fc4c14SDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
87977fc4c14SDimitry Andric      session_dictionary_name);
88077fc4c14SDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
88177fc4c14SDimitry Andric      python_function_name, dict);
882cfca06d7SDimitry Andric
883cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
884e3b55780SDimitry Andric    return std::nullopt;
885cfca06d7SDimitry Andric
8867fa27ce4SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(std::move(thread)), dict);
887cfca06d7SDimitry Andric
88877fc4c14SDimitry Andric  return result.Str().GetString().str();
889cfca06d7SDimitry Andric}
890cfca06d7SDimitry Andric
8917fa27ce4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordTarget(
892f65dcba8SDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
893f65dcba8SDimitry Andric    const lldb::TargetSP &target, std::string &output) {
894cfca06d7SDimitry Andric
89577fc4c14SDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
89677fc4c14SDimitry Andric      !session_dictionary_name)
897cfca06d7SDimitry Andric    return false;
898cfca06d7SDimitry Andric
899cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
900cfca06d7SDimitry Andric
90177fc4c14SDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
90277fc4c14SDimitry Andric      session_dictionary_name);
90377fc4c14SDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
90477fc4c14SDimitry Andric      python_function_name, dict);
905cfca06d7SDimitry Andric
906cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
907cfca06d7SDimitry Andric    return false;
908cfca06d7SDimitry Andric
9097fa27ce4SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(target), dict);
910cfca06d7SDimitry Andric
911cfca06d7SDimitry Andric  output = result.Str().GetString().str();
912cfca06d7SDimitry Andric
913cfca06d7SDimitry Andric  return true;
914cfca06d7SDimitry Andric}
915cfca06d7SDimitry Andric
9167fa27ce4SDimitry Andricstd::optional<std::string> lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordFrame(
91777fc4c14SDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
91877fc4c14SDimitry Andric    lldb::StackFrameSP frame) {
91977fc4c14SDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
92077fc4c14SDimitry Andric      !session_dictionary_name)
921e3b55780SDimitry Andric    return std::nullopt;
922cfca06d7SDimitry Andric
923cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
924cfca06d7SDimitry Andric
92577fc4c14SDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
92677fc4c14SDimitry Andric      session_dictionary_name);
92777fc4c14SDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
92877fc4c14SDimitry Andric      python_function_name, dict);
929cfca06d7SDimitry Andric
930cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
931e3b55780SDimitry Andric    return std::nullopt;
932cfca06d7SDimitry Andric
9337fa27ce4SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(std::move(frame)), dict);
934cfca06d7SDimitry Andric
93577fc4c14SDimitry Andric  return result.Str().GetString().str();
936cfca06d7SDimitry Andric}
937cfca06d7SDimitry Andric
9387fa27ce4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordValue(
939f65dcba8SDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
940f65dcba8SDimitry Andric    const lldb::ValueObjectSP &value, std::string &output) {
941cfca06d7SDimitry Andric
94277fc4c14SDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
94377fc4c14SDimitry Andric      !session_dictionary_name)
944cfca06d7SDimitry Andric    return false;
945cfca06d7SDimitry Andric
946cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
947cfca06d7SDimitry Andric
94877fc4c14SDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
94977fc4c14SDimitry Andric      session_dictionary_name);
95077fc4c14SDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
95177fc4c14SDimitry Andric      python_function_name, dict);
952cfca06d7SDimitry Andric
953cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
954cfca06d7SDimitry Andric    return false;
955cfca06d7SDimitry Andric
9567fa27ce4SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(value), dict);
957cfca06d7SDimitry Andric
958cfca06d7SDimitry Andric  output = result.Str().GetString().str();
959cfca06d7SDimitry Andric
960cfca06d7SDimitry Andric  return true;
961cfca06d7SDimitry Andric}
962cfca06d7SDimitry Andric
9637fa27ce4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonCallModuleInit(
96477fc4c14SDimitry Andric    const char *python_module_name, const char *session_dictionary_name,
96577fc4c14SDimitry Andric    lldb::DebuggerSP debugger) {
966cfca06d7SDimitry Andric  std::string python_function_name_string = python_module_name;
967cfca06d7SDimitry Andric  python_function_name_string += ".__lldb_init_module";
968cfca06d7SDimitry Andric  const char *python_function_name = python_function_name_string.c_str();
969cfca06d7SDimitry Andric
970cfca06d7SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
971cfca06d7SDimitry Andric
97277fc4c14SDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
97377fc4c14SDimitry Andric      session_dictionary_name);
97477fc4c14SDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
97577fc4c14SDimitry Andric      python_function_name, dict);
976cfca06d7SDimitry Andric
977cfca06d7SDimitry Andric  // This method is optional and need not exist.  So if we don't find it,
978cfca06d7SDimitry Andric  // it's actually a success, not a failure.
979cfca06d7SDimitry Andric  if (!pfunc.IsAllocated())
980cfca06d7SDimitry Andric    return true;
981cfca06d7SDimitry Andric
9827fa27ce4SDimitry Andric  pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger)), dict);
983cfca06d7SDimitry Andric
984cfca06d7SDimitry Andric  return true;
985cfca06d7SDimitry Andric}
986cfca06d7SDimitry Andric
9877fa27ce4SDimitry Andriclldb::ValueObjectSP lldb_private::python::SWIGBridge::LLDBSWIGPython_GetValueObjectSPFromSBValue(
98877fc4c14SDimitry Andric    void *data) {
989cfca06d7SDimitry Andric  lldb::ValueObjectSP valobj_sp;
99077fc4c14SDimitry Andric  if (data) {
991cfca06d7SDimitry Andric    lldb::SBValue *sb_ptr = (lldb::SBValue *)data;
992cfca06d7SDimitry Andric    valobj_sp = sb_ptr->GetSP();
993cfca06d7SDimitry Andric  }
994cfca06d7SDimitry Andric  return valobj_sp;
995cfca06d7SDimitry Andric}
996cfca06d7SDimitry Andric
997cfca06d7SDimitry Andric// For the LogOutputCallback functions
99877fc4c14SDimitry Andricstatic void LLDBSwigPythonCallPythonLogOutputCallback(const char *str,
99977fc4c14SDimitry Andric                                                      void *baton) {
1000cfca06d7SDimitry Andric  if (baton != Py_None) {
1001cfca06d7SDimitry Andric    SWIG_PYTHON_THREAD_BEGIN_BLOCK;
100277fc4c14SDimitry Andric    PyObject *result = PyObject_CallFunction(
100377fc4c14SDimitry Andric        reinterpret_cast<PyObject *>(baton), const_cast<char *>("s"), str);
1004cfca06d7SDimitry Andric    Py_XDECREF(result);
1005cfca06d7SDimitry Andric    SWIG_PYTHON_THREAD_END_BLOCK;
1006cfca06d7SDimitry Andric  }
1007cfca06d7SDimitry Andric}
10087fa27ce4SDimitry Andric
10097fa27ce4SDimitry Andric// For DebuggerTerminateCallback functions
10107fa27ce4SDimitry Andricstatic void LLDBSwigPythonCallPythonSBDebuggerTerminateCallback(lldb::user_id_t debugger_id,
10117fa27ce4SDimitry Andric                                                      void *baton) {
10127fa27ce4SDimitry Andric  if (baton != Py_None) {
10137fa27ce4SDimitry Andric    SWIG_PYTHON_THREAD_BEGIN_BLOCK;
10147fa27ce4SDimitry Andric    PyObject *result = PyObject_CallFunction(
10157fa27ce4SDimitry Andric        reinterpret_cast<PyObject *>(baton), const_cast<char *>("l"), debugger_id);
10167fa27ce4SDimitry Andric    Py_XDECREF(result);
10177fa27ce4SDimitry Andric    SWIG_PYTHON_THREAD_END_BLOCK;
10187fa27ce4SDimitry Andric  }
10197fa27ce4SDimitry Andric}
10207fa27ce4SDimitry Andric
1021ac9a064cSDimitry Andricstatic bool LLDBSwigPythonCallPythonSBCommandInterpreterSetCommandOverrideCallback(void *baton, const char **argv) {
1022ac9a064cSDimitry Andric  bool ret_val = false;
1023ac9a064cSDimitry Andric  if (baton != Py_None) {
1024ac9a064cSDimitry Andric    SWIG_PYTHON_THREAD_BEGIN_BLOCK;
1025ac9a064cSDimitry Andric    // Create a PyList of items since we're going to pass it to the callback as a tuple
1026ac9a064cSDimitry Andric    // of arguments.
1027ac9a064cSDimitry Andric    PyObject *py_argv = PyList_New(0);
1028ac9a064cSDimitry Andric    for (const char **arg = argv; arg && *arg; arg++) {
1029ac9a064cSDimitry Andric      std::string arg_string = *arg;
1030ac9a064cSDimitry Andric      PyObject *py_string = PyUnicode_FromStringAndSize(arg_string.c_str(), arg_string.size());
1031ac9a064cSDimitry Andric      PyList_Append(py_argv, py_string);
1032ac9a064cSDimitry Andric    }
1033ac9a064cSDimitry Andric
1034ac9a064cSDimitry Andric    PyObject *result = PyObject_CallObject(
1035ac9a064cSDimitry Andric        reinterpret_cast<PyObject *>(baton), PyList_AsTuple(py_argv));
1036ac9a064cSDimitry Andric    ret_val = result ? PyObject_IsTrue(result) : false;
1037ac9a064cSDimitry Andric    Py_XDECREF(result);
1038ac9a064cSDimitry Andric    SWIG_PYTHON_THREAD_END_BLOCK;
1039ac9a064cSDimitry Andric  }
1040ac9a064cSDimitry Andric  return ret_val;
1041ac9a064cSDimitry Andric}
1042ac9a064cSDimitry Andric
10437fa27ce4SDimitry Andricstatic SBError LLDBSwigPythonCallLocateModuleCallback(
10447fa27ce4SDimitry Andric    void *callback_baton, const SBModuleSpec &module_spec_sb,
10457fa27ce4SDimitry Andric    SBFileSpec &module_file_spec_sb, SBFileSpec &symbol_file_spec_sb) {
10467fa27ce4SDimitry Andric  SWIG_Python_Thread_Block swig_thread_block;
10477fa27ce4SDimitry Andric
10487fa27ce4SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
10497fa27ce4SDimitry Andric  PythonObject module_spec_arg = SWIGBridge::ToSWIGWrapper(
10507fa27ce4SDimitry Andric      std::make_unique<SBModuleSpec>(module_spec_sb));
10517fa27ce4SDimitry Andric  PythonObject module_file_spec_arg = SWIGBridge::ToSWIGWrapper(
10527fa27ce4SDimitry Andric      std::make_unique<SBFileSpec>(module_file_spec_sb));
10537fa27ce4SDimitry Andric  PythonObject symbol_file_spec_arg = SWIGBridge::ToSWIGWrapper(
10547fa27ce4SDimitry Andric      std::make_unique<SBFileSpec>(symbol_file_spec_sb));
10557fa27ce4SDimitry Andric
10567fa27ce4SDimitry Andric  PythonCallable callable =
10577fa27ce4SDimitry Andric      Retain<PythonCallable>(reinterpret_cast<PyObject *>(callback_baton));
10587fa27ce4SDimitry Andric  if (!callable.IsValid()) {
10597fa27ce4SDimitry Andric    return SBError("The callback callable is not valid.");
10607fa27ce4SDimitry Andric  }
10617fa27ce4SDimitry Andric
10627fa27ce4SDimitry Andric  PythonObject result = callable(module_spec_arg, module_file_spec_arg,
10637fa27ce4SDimitry Andric                                 symbol_file_spec_arg);
10647fa27ce4SDimitry Andric
10657fa27ce4SDimitry Andric  if (!result.IsAllocated())
10667fa27ce4SDimitry Andric    return SBError("No result.");
10677fa27ce4SDimitry Andric  lldb::SBError *sb_error_ptr = nullptr;
10687fa27ce4SDimitry Andric  if (SWIG_ConvertPtr(result.get(), (void **)&sb_error_ptr,
10697fa27ce4SDimitry Andric                      SWIGTYPE_p_lldb__SBError, 0) == -1) {
10707fa27ce4SDimitry Andric    return SBError("Result is not SBError.");
10717fa27ce4SDimitry Andric  }
10727fa27ce4SDimitry Andric
10737fa27ce4SDimitry Andric  if (sb_error_ptr->Success()) {
10747fa27ce4SDimitry Andric    lldb::SBFileSpec *sb_module_file_spec_ptr = nullptr;
10757fa27ce4SDimitry Andric    if (SWIG_ConvertPtr(module_file_spec_arg.get(),
10767fa27ce4SDimitry Andric                        (void **)&sb_module_file_spec_ptr,
10777fa27ce4SDimitry Andric                        SWIGTYPE_p_lldb__SBFileSpec, 0) == -1)
10787fa27ce4SDimitry Andric      return SBError("module_file_spec is not SBFileSpec.");
10797fa27ce4SDimitry Andric
10807fa27ce4SDimitry Andric    lldb::SBFileSpec *sb_symbol_file_spec_ptr = nullptr;
10817fa27ce4SDimitry Andric    if (SWIG_ConvertPtr(symbol_file_spec_arg.get(),
10827fa27ce4SDimitry Andric                        (void **)&sb_symbol_file_spec_ptr,
10837fa27ce4SDimitry Andric                        SWIGTYPE_p_lldb__SBFileSpec, 0) == -1)
10847fa27ce4SDimitry Andric      return SBError("symbol_file_spec is not SBFileSpec.");
10857fa27ce4SDimitry Andric
10867fa27ce4SDimitry Andric    module_file_spec_sb = *sb_module_file_spec_ptr;
10877fa27ce4SDimitry Andric    symbol_file_spec_sb = *sb_symbol_file_spec_ptr;
10887fa27ce4SDimitry Andric  }
10897fa27ce4SDimitry Andric
10907fa27ce4SDimitry Andric  return *sb_error_ptr;
10917fa27ce4SDimitry Andric}
1092cfca06d7SDimitry Andric%}
1093