1130d950cSDimitry Andric%header %{
2130d950cSDimitry Andric
30eae32dcSDimitry Andricclass PyErr_Cleaner {
4130d950cSDimitry Andricpublic:
50eae32dcSDimitry Andric  PyErr_Cleaner(bool print = false) : m_print(print) {}
6130d950cSDimitry Andric
70eae32dcSDimitry Andric  ~PyErr_Cleaner() {
80eae32dcSDimitry Andric    if (PyErr_Occurred()) {
9130d950cSDimitry Andric      if (m_print && !PyErr_ExceptionMatches(PyExc_SystemExit))
10130d950cSDimitry Andric        PyErr_Print();
11130d950cSDimitry Andric      PyErr_Clear();
12130d950cSDimitry Andric    }
13130d950cSDimitry Andric  }
14130d950cSDimitry Andric
15130d950cSDimitry Andricprivate:
16130d950cSDimitry Andric  bool m_print;
17130d950cSDimitry Andric};
18130d950cSDimitry Andric
19*fe013be4SDimitry Andricllvm::Expected<bool> lldb_private::python::SWIGBridge::LLDBSwigPythonBreakpointCallbackFunction(
200eae32dcSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
21130d950cSDimitry Andric    const lldb::StackFrameSP &frame_sp,
22130d950cSDimitry Andric    const lldb::BreakpointLocationSP &bp_loc_sp,
230eae32dcSDimitry Andric    const lldb_private::StructuredDataImpl &args_impl) {
24130d950cSDimitry Andric  using namespace llvm;
25130d950cSDimitry Andric
26130d950cSDimitry Andric  lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
27130d950cSDimitry Andric
28130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
290eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
300eae32dcSDimitry Andric      session_dictionary_name);
310eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
320eae32dcSDimitry Andric      python_function_name, dict);
33130d950cSDimitry Andric
34130d950cSDimitry Andric  unsigned max_positional_args;
35130d950cSDimitry Andric  if (auto arg_info = pfunc.GetArgInfo())
36130d950cSDimitry Andric    max_positional_args = arg_info.get().max_positional_args;
37130d950cSDimitry Andric  else
38130d950cSDimitry Andric    return arg_info.takeError();
39130d950cSDimitry Andric
40*fe013be4SDimitry Andric  PythonObject frame_arg = SWIGBridge::ToSWIGWrapper(frame_sp);
41*fe013be4SDimitry Andric  PythonObject bp_loc_arg = SWIGBridge::ToSWIGWrapper(bp_loc_sp);
42130d950cSDimitry Andric
430eae32dcSDimitry Andric  auto result =
440eae32dcSDimitry Andric      max_positional_args < 4
450eae32dcSDimitry Andric          ? pfunc.Call(frame_arg, bp_loc_arg, dict)
46*fe013be4SDimitry Andric          : pfunc.Call(frame_arg, bp_loc_arg, SWIGBridge::ToSWIGWrapper(args_impl), dict);
47130d950cSDimitry Andric
48130d950cSDimitry Andric  if (!result)
49130d950cSDimitry Andric    return result.takeError();
50130d950cSDimitry Andric
51130d950cSDimitry Andric  // Only False counts as false!
52130d950cSDimitry Andric  return result.get().get() != Py_False;
53130d950cSDimitry Andric}
54130d950cSDimitry Andric
554824e7fdSDimitry Andric// resolve a dotted Python name in the form
564824e7fdSDimitry Andric// foo.bar.baz.Foobar to an actual Python object
574824e7fdSDimitry Andric// if pmodule is NULL, the __main__ module will be used
584824e7fdSDimitry Andric// as the starting point for the search
59fe6060f1SDimitry Andric
600eae32dcSDimitry Andric// This function is called by
610eae32dcSDimitry Andric// lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...) and is
620eae32dcSDimitry Andric// used when a script command is attached to a breakpoint for execution.
634824e7fdSDimitry Andric
640eae32dcSDimitry Andric// This function is called by
650eae32dcSDimitry Andric// lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...) and is
660eae32dcSDimitry Andric// used when a script command is attached to a watchpoint for execution.
67fe6060f1SDimitry Andric
68*fe013be4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonWatchpointCallbackFunction(
690eae32dcSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
700eae32dcSDimitry Andric    const lldb::StackFrameSP &frame_sp, const lldb::WatchpointSP &wp_sp) {
71130d950cSDimitry Andric
72130d950cSDimitry Andric  bool stop_at_watchpoint = true;
73130d950cSDimitry Andric
74130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
75130d950cSDimitry Andric
760eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
770eae32dcSDimitry Andric      session_dictionary_name);
780eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
790eae32dcSDimitry Andric      python_function_name, dict);
80130d950cSDimitry Andric
81130d950cSDimitry Andric  if (!pfunc.IsAllocated())
82130d950cSDimitry Andric    return stop_at_watchpoint;
83130d950cSDimitry Andric
840eae32dcSDimitry Andric  PythonObject result =
85*fe013be4SDimitry Andric      pfunc(SWIGBridge::ToSWIGWrapper(frame_sp), SWIGBridge::ToSWIGWrapper(wp_sp), dict);
86130d950cSDimitry Andric
87130d950cSDimitry Andric  if (result.get() == Py_False)
88130d950cSDimitry Andric    stop_at_watchpoint = false;
89130d950cSDimitry Andric
90130d950cSDimitry Andric  return stop_at_watchpoint;
91130d950cSDimitry Andric}
92130d950cSDimitry Andric
93bdd1243dSDimitry Andric// This function is called by
94bdd1243dSDimitry Andric// ScriptInterpreterPython::FormatterMatchingCallbackFunction and it's used when
95bdd1243dSDimitry Andric// a data formatter provides the name of a callback to inspect a candidate type
96bdd1243dSDimitry Andric// before considering a match.
97*fe013be4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonFormatterCallbackFunction(
98bdd1243dSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
99bdd1243dSDimitry Andric    lldb::TypeImplSP type_impl_sp) {
100bdd1243dSDimitry Andric
101bdd1243dSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
102bdd1243dSDimitry Andric
103bdd1243dSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
104bdd1243dSDimitry Andric      session_dictionary_name);
105bdd1243dSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
106bdd1243dSDimitry Andric      python_function_name, dict);
107bdd1243dSDimitry Andric
108bdd1243dSDimitry Andric  if (!pfunc.IsAllocated())
109bdd1243dSDimitry Andric    return false;
110bdd1243dSDimitry Andric
111bdd1243dSDimitry Andric  PythonObject result =
112*fe013be4SDimitry Andric      pfunc(SWIGBridge::ToSWIGWrapper(type_impl_sp), dict);
113bdd1243dSDimitry Andric
114bdd1243dSDimitry Andric  // Only if everything goes okay and the function returns True we'll consider
115bdd1243dSDimitry Andric  // it a match.
116bdd1243dSDimitry Andric  return result.get() == Py_True;
117bdd1243dSDimitry Andric}
118bdd1243dSDimitry Andric
119*fe013be4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonCallTypeScript(
1200eae32dcSDimitry Andric    const char *python_function_name, const void *session_dictionary,
1210eae32dcSDimitry Andric    const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper,
1220eae32dcSDimitry Andric    const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval) {
123130d950cSDimitry Andric
124130d950cSDimitry Andric  retval.clear();
125130d950cSDimitry Andric
126130d950cSDimitry Andric  if (!python_function_name || !session_dictionary)
127130d950cSDimitry Andric    return false;
128130d950cSDimitry Andric
129130d950cSDimitry Andric  PyObject *pfunc_impl = nullptr;
130130d950cSDimitry Andric
1310eae32dcSDimitry Andric  if (pyfunct_wrapper && *pyfunct_wrapper &&
1320eae32dcSDimitry Andric      PyFunction_Check(*pyfunct_wrapper)) {
133130d950cSDimitry Andric    pfunc_impl = (PyObject *)(*pyfunct_wrapper);
1340eae32dcSDimitry Andric    if (pfunc_impl->ob_refcnt == 1) {
135130d950cSDimitry Andric      Py_XDECREF(pfunc_impl);
136130d950cSDimitry Andric      pfunc_impl = NULL;
137130d950cSDimitry Andric    }
138130d950cSDimitry Andric  }
139130d950cSDimitry Andric
140130d950cSDimitry Andric  PyObject *py_dict = (PyObject *)session_dictionary;
141130d950cSDimitry Andric  if (!PythonDictionary::Check(py_dict))
142130d950cSDimitry Andric    return true;
143130d950cSDimitry Andric
144130d950cSDimitry Andric  PythonDictionary dict(PyRefType::Borrowed, py_dict);
145130d950cSDimitry Andric
146130d950cSDimitry Andric  PyErr_Cleaner pyerr_cleanup(true); // show Python errors
147130d950cSDimitry Andric
148130d950cSDimitry Andric  PythonCallable pfunc(PyRefType::Borrowed, pfunc_impl);
149130d950cSDimitry Andric
1500eae32dcSDimitry Andric  if (!pfunc.IsAllocated()) {
1510eae32dcSDimitry Andric    pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
1520eae32dcSDimitry Andric        python_function_name, dict);
153130d950cSDimitry Andric    if (!pfunc.IsAllocated())
154130d950cSDimitry Andric      return false;
155130d950cSDimitry Andric
1560eae32dcSDimitry Andric    if (pyfunct_wrapper) {
157130d950cSDimitry Andric      *pyfunct_wrapper = pfunc.get();
158130d950cSDimitry Andric      Py_XINCREF(pfunc.get());
159130d950cSDimitry Andric    }
160130d950cSDimitry Andric  }
161130d950cSDimitry Andric
162130d950cSDimitry Andric  PythonObject result;
163130d950cSDimitry Andric  auto argc = pfunc.GetArgInfo();
164130d950cSDimitry Andric  if (!argc) {
165130d950cSDimitry Andric    llvm::consumeError(argc.takeError());
166130d950cSDimitry Andric    return false;
167130d950cSDimitry Andric  }
168130d950cSDimitry Andric
169*fe013be4SDimitry Andric  PythonObject value_arg = SWIGBridge::ToSWIGWrapper(valobj_sp);
170130d950cSDimitry Andric
171130d950cSDimitry Andric  if (argc.get().max_positional_args < 3)
172130d950cSDimitry Andric    result = pfunc(value_arg, dict);
173130d950cSDimitry Andric  else
174*fe013be4SDimitry Andric    result = pfunc(value_arg, dict, SWIGBridge::ToSWIGWrapper(*options_sp));
175130d950cSDimitry Andric
176130d950cSDimitry Andric  retval = result.Str().GetString().str();
177130d950cSDimitry Andric
178130d950cSDimitry Andric  return true;
179130d950cSDimitry Andric}
180130d950cSDimitry Andric
181*fe013be4SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateSyntheticProvider(
1820eae32dcSDimitry Andric    const char *python_class_name, const char *session_dictionary_name,
1830eae32dcSDimitry Andric    const lldb::ValueObjectSP &valobj_sp) {
1840eae32dcSDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
1850eae32dcSDimitry Andric      !session_dictionary_name)
18604eeddc0SDimitry Andric    return PythonObject();
187130d950cSDimitry Andric
188130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
189130d950cSDimitry Andric
1900eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
1910eae32dcSDimitry Andric      session_dictionary_name);
1920eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
1930eae32dcSDimitry Andric      python_class_name, dict);
194130d950cSDimitry Andric
195130d950cSDimitry Andric  if (!pfunc.IsAllocated())
19604eeddc0SDimitry Andric    return PythonObject();
197130d950cSDimitry Andric
198*fe013be4SDimitry Andric  auto sb_value = std::unique_ptr<lldb::SBValue>(new lldb::SBValue(valobj_sp));
199130d950cSDimitry Andric  sb_value->SetPreferSyntheticValue(false);
200130d950cSDimitry Andric
201*fe013be4SDimitry Andric  PythonObject val_arg = SWIGBridge::ToSWIGWrapper(std::move(sb_value));
202130d950cSDimitry Andric  if (!val_arg.IsAllocated())
20304eeddc0SDimitry Andric    return PythonObject();
204130d950cSDimitry Andric
205130d950cSDimitry Andric  PythonObject result = pfunc(val_arg, dict);
206130d950cSDimitry Andric
207130d950cSDimitry Andric  if (result.IsAllocated())
20804eeddc0SDimitry Andric    return result;
209130d950cSDimitry Andric
21004eeddc0SDimitry Andric  return PythonObject();
211130d950cSDimitry Andric}
212130d950cSDimitry Andric
213*fe013be4SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateCommandObject(
2140eae32dcSDimitry Andric    const char *python_class_name, const char *session_dictionary_name,
2150eae32dcSDimitry Andric    lldb::DebuggerSP debugger_sp) {
2160eae32dcSDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
2170eae32dcSDimitry Andric      !session_dictionary_name)
21804eeddc0SDimitry Andric    return PythonObject();
219130d950cSDimitry Andric
220130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
2210eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
2220eae32dcSDimitry Andric      session_dictionary_name);
2230eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
2240eae32dcSDimitry Andric      python_class_name, dict);
225130d950cSDimitry Andric
226130d950cSDimitry Andric  if (!pfunc.IsAllocated())
22704eeddc0SDimitry Andric    return PythonObject();
228130d950cSDimitry Andric
229*fe013be4SDimitry Andric  return pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger_sp)), dict);
230130d950cSDimitry Andric}
231130d950cSDimitry Andric
232*fe013be4SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedThreadPlan(
2330eae32dcSDimitry Andric    const char *python_class_name, const char *session_dictionary_name,
2340eae32dcSDimitry Andric    const lldb_private::StructuredDataImpl &args_impl,
2350eae32dcSDimitry Andric    std::string &error_string, const lldb::ThreadPlanSP &thread_plan_sp) {
2360eae32dcSDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
2370eae32dcSDimitry Andric      !session_dictionary_name)
23804eeddc0SDimitry Andric    return PythonObject();
239130d950cSDimitry Andric
240130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
241130d950cSDimitry Andric
2420eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
2430eae32dcSDimitry Andric      session_dictionary_name);
2440eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
2450eae32dcSDimitry Andric      python_class_name, dict);
246130d950cSDimitry Andric
247130d950cSDimitry Andric  if (!pfunc.IsAllocated()) {
248130d950cSDimitry Andric    error_string.append("could not find script class: ");
249130d950cSDimitry Andric    error_string.append(python_class_name);
25004eeddc0SDimitry Andric    return PythonObject();
251130d950cSDimitry Andric  }
252130d950cSDimitry Andric
253*fe013be4SDimitry Andric  PythonObject tp_arg = SWIGBridge::ToSWIGWrapper(thread_plan_sp);
254130d950cSDimitry Andric
255130d950cSDimitry Andric  llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
256130d950cSDimitry Andric  if (!arg_info) {
257130d950cSDimitry Andric    llvm::handleAllErrors(
258130d950cSDimitry Andric        arg_info.takeError(),
2590eae32dcSDimitry Andric        [&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
260130d950cSDimitry Andric        [&](const llvm::ErrorInfoBase &E) {
261130d950cSDimitry Andric          error_string.append(E.message());
262130d950cSDimitry Andric        });
26304eeddc0SDimitry Andric    return PythonObject();
264130d950cSDimitry Andric  }
265130d950cSDimitry Andric
266130d950cSDimitry Andric  PythonObject result = {};
267*fe013be4SDimitry Andric  auto args_sb = std::unique_ptr<lldb::SBStructuredData>(new lldb::SBStructuredData(args_impl));
268130d950cSDimitry Andric  if (arg_info.get().max_positional_args == 2) {
2690eae32dcSDimitry Andric    if (args_sb->IsValid()) {
2700eae32dcSDimitry Andric      error_string.assign(
2710eae32dcSDimitry Andric          "args passed, but __init__ does not take an args dictionary");
27204eeddc0SDimitry Andric      return PythonObject();
273130d950cSDimitry Andric    }
274130d950cSDimitry Andric    result = pfunc(tp_arg, dict);
275130d950cSDimitry Andric  } else if (arg_info.get().max_positional_args >= 3) {
276*fe013be4SDimitry Andric    result = pfunc(tp_arg, SWIGBridge::ToSWIGWrapper(std::move(args_sb)), dict);
277130d950cSDimitry Andric  } else {
2780eae32dcSDimitry Andric    error_string.assign("wrong number of arguments in __init__, should be 2 or "
2790eae32dcSDimitry Andric                        "3 (not including self)");
28004eeddc0SDimitry Andric    return PythonObject();
281130d950cSDimitry Andric  }
282130d950cSDimitry Andric
2830eae32dcSDimitry Andric  // FIXME: At this point we should check that the class we found supports all
2840eae32dcSDimitry Andric  // the methods that we need.
285130d950cSDimitry Andric
28604eeddc0SDimitry Andric  return result;
287130d950cSDimitry Andric}
288130d950cSDimitry Andric
289*fe013be4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSWIGPythonCallThreadPlan(
2900eae32dcSDimitry Andric    void *implementor, const char *method_name, lldb_private::Event *event,
2910eae32dcSDimitry Andric    bool &got_error) {
292130d950cSDimitry Andric  got_error = false;
293130d950cSDimitry Andric
294130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(false);
295130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
296130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>(method_name);
297130d950cSDimitry Andric
298130d950cSDimitry Andric  if (!pfunc.IsAllocated())
299130d950cSDimitry Andric    return false;
300130d950cSDimitry Andric
301130d950cSDimitry Andric  PythonObject result;
3020eae32dcSDimitry Andric  if (event != nullptr) {
303*fe013be4SDimitry Andric    ScopedPythonObject<SBEvent> event_arg = SWIGBridge::ToSWIGWrapper(event);
30404eeddc0SDimitry Andric    result = pfunc(event_arg.obj());
3050eae32dcSDimitry Andric  } else
306130d950cSDimitry Andric    result = pfunc();
307130d950cSDimitry Andric
3080eae32dcSDimitry Andric  if (PyErr_Occurred()) {
309130d950cSDimitry Andric    got_error = true;
3100eae32dcSDimitry Andric    printf("Return value was neither false nor true for call to %s.\n",
3110eae32dcSDimitry Andric           method_name);
312130d950cSDimitry Andric    PyErr_Print();
313130d950cSDimitry Andric    return false;
314130d950cSDimitry Andric  }
315130d950cSDimitry Andric
316130d950cSDimitry Andric  if (result.get() == Py_True)
317130d950cSDimitry Andric    return true;
318130d950cSDimitry Andric  else if (result.get() == Py_False)
319130d950cSDimitry Andric    return false;
320130d950cSDimitry Andric
321130d950cSDimitry Andric  // Somebody returned the wrong thing...
322130d950cSDimitry Andric  got_error = true;
323130d950cSDimitry Andric  printf("Wrong return value type for call to %s.\n", method_name);
324130d950cSDimitry Andric  return false;
325130d950cSDimitry Andric}
326130d950cSDimitry Andric
327*fe013be4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSWIGPythonCallThreadPlan(
328*fe013be4SDimitry Andric    void *implementor, const char *method_name, lldb_private::Stream *stream,
329*fe013be4SDimitry Andric    bool &got_error) {
330*fe013be4SDimitry Andric  got_error = false;
331*fe013be4SDimitry Andric
332*fe013be4SDimitry Andric  PyErr_Cleaner py_err_cleaner(false);
333*fe013be4SDimitry Andric  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
334*fe013be4SDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>(method_name);
335*fe013be4SDimitry Andric
336*fe013be4SDimitry Andric  if (!pfunc.IsAllocated())
337*fe013be4SDimitry Andric    return false;
338*fe013be4SDimitry Andric
339*fe013be4SDimitry Andric  auto *sb_stream = new lldb::SBStream();
340*fe013be4SDimitry Andric  PythonObject sb_stream_arg =
341*fe013be4SDimitry Andric      SWIGBridge::ToSWIGWrapper(std::unique_ptr<lldb::SBStream>(sb_stream));
342*fe013be4SDimitry Andric
343*fe013be4SDimitry Andric  PythonObject result;
344*fe013be4SDimitry Andric  result = pfunc(sb_stream_arg);
345*fe013be4SDimitry Andric
346*fe013be4SDimitry Andric  if (PyErr_Occurred()) {
347*fe013be4SDimitry Andric    printf("Error occured for call to %s.\n",
348*fe013be4SDimitry Andric           method_name);
349*fe013be4SDimitry Andric    PyErr_Print();
350*fe013be4SDimitry Andric    got_error = true;
351*fe013be4SDimitry Andric    return false;
352*fe013be4SDimitry Andric  }
353*fe013be4SDimitry Andric  if (stream)
354*fe013be4SDimitry Andric    stream->PutCString(sb_stream->GetData());
355*fe013be4SDimitry Andric  return true;
356*fe013be4SDimitry Andric
357*fe013be4SDimitry Andric}
358*fe013be4SDimitry Andric
359*fe013be4SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedBreakpointResolver(
3604824e7fdSDimitry Andric    const char *python_class_name, const char *session_dictionary_name,
3610eae32dcSDimitry Andric    const StructuredDataImpl &args_impl,
3624824e7fdSDimitry Andric    const lldb::BreakpointSP &breakpoint_sp) {
3634824e7fdSDimitry Andric
3640eae32dcSDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
3650eae32dcSDimitry Andric      !session_dictionary_name)
36604eeddc0SDimitry Andric    return PythonObject();
367130d950cSDimitry Andric
368130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
369130d950cSDimitry Andric
3700eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
3710eae32dcSDimitry Andric      session_dictionary_name);
3720eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
3730eae32dcSDimitry Andric      python_class_name, dict);
374130d950cSDimitry Andric
375130d950cSDimitry Andric  if (!pfunc.IsAllocated())
37604eeddc0SDimitry Andric    return PythonObject();
377130d950cSDimitry Andric
3780eae32dcSDimitry Andric  PythonObject result =
379*fe013be4SDimitry Andric      pfunc(SWIGBridge::ToSWIGWrapper(breakpoint_sp), SWIGBridge::ToSWIGWrapper(args_impl), dict);
3800eae32dcSDimitry Andric  // FIXME: At this point we should check that the class we found supports all
3810eae32dcSDimitry Andric  // the methods that we need.
382130d950cSDimitry Andric
3830eae32dcSDimitry Andric  if (result.IsAllocated()) {
384130d950cSDimitry Andric    // Check that __callback__ is defined:
385130d950cSDimitry Andric    auto callback_func = result.ResolveName<PythonCallable>("__callback__");
386130d950cSDimitry Andric    if (callback_func.IsAllocated())
38704eeddc0SDimitry Andric      return result;
388130d950cSDimitry Andric  }
38904eeddc0SDimitry Andric  return PythonObject();
390130d950cSDimitry Andric}
391130d950cSDimitry Andric
392*fe013be4SDimitry Andricunsigned int lldb_private::python::SWIGBridge::LLDBSwigPythonCallBreakpointResolver(
3930eae32dcSDimitry Andric    void *implementor, const char *method_name,
3940eae32dcSDimitry Andric    lldb_private::SymbolContext *sym_ctx) {
395130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(false);
396130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
397130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>(method_name);
398130d950cSDimitry Andric
399130d950cSDimitry Andric  if (!pfunc.IsAllocated())
400130d950cSDimitry Andric    return 0;
401130d950cSDimitry Andric
402*fe013be4SDimitry Andric  PythonObject result = sym_ctx ? pfunc(SWIGBridge::ToSWIGWrapper(*sym_ctx)) : pfunc();
403130d950cSDimitry Andric
4040eae32dcSDimitry Andric  if (PyErr_Occurred()) {
405130d950cSDimitry Andric    PyErr_Print();
4065ffd83dbSDimitry Andric    PyErr_Clear();
407130d950cSDimitry Andric    return 0;
408130d950cSDimitry Andric  }
409130d950cSDimitry Andric
410130d950cSDimitry Andric  // The callback will return a bool, but we're need to also return ints
411130d950cSDimitry Andric  // so we're squirrelling the bool through as an int...  And if you return
412130d950cSDimitry Andric  // nothing, we'll continue.
413130d950cSDimitry Andric  if (strcmp(method_name, "__callback__") == 0) {
414130d950cSDimitry Andric    if (result.get() == Py_False)
415130d950cSDimitry Andric      return 0;
416130d950cSDimitry Andric    else
417130d950cSDimitry Andric      return 1;
418130d950cSDimitry Andric  }
419130d950cSDimitry Andric
4205ffd83dbSDimitry Andric  long long ret_val = unwrapOrSetPythonException(As<long long>(result));
421130d950cSDimitry Andric
4225ffd83dbSDimitry Andric  if (PyErr_Occurred()) {
4235ffd83dbSDimitry Andric    PyErr_Print();
4245ffd83dbSDimitry Andric    PyErr_Clear();
4255ffd83dbSDimitry Andric    return 0;
4265ffd83dbSDimitry Andric  }
427130d950cSDimitry Andric
428130d950cSDimitry Andric  return ret_val;
429130d950cSDimitry Andric}
430130d950cSDimitry Andric
431*fe013be4SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedStopHook(
4320eae32dcSDimitry Andric    lldb::TargetSP target_sp, const char *python_class_name,
4330eae32dcSDimitry Andric    const char *session_dictionary_name, const StructuredDataImpl &args_impl,
4340eae32dcSDimitry Andric    Status &error) {
435e8d8bef9SDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0') {
436e8d8bef9SDimitry Andric    error.SetErrorString("Empty class name.");
43704eeddc0SDimitry Andric    return PythonObject();
438e8d8bef9SDimitry Andric  }
439e8d8bef9SDimitry Andric  if (!session_dictionary_name) {
440e8d8bef9SDimitry Andric    error.SetErrorString("No session dictionary");
44104eeddc0SDimitry Andric    return PythonObject();
442e8d8bef9SDimitry Andric  }
443e8d8bef9SDimitry Andric
444e8d8bef9SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
445e8d8bef9SDimitry Andric
4460eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
447e8d8bef9SDimitry Andric      session_dictionary_name);
4480eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
449e8d8bef9SDimitry Andric      python_class_name, dict);
450e8d8bef9SDimitry Andric
451e8d8bef9SDimitry Andric  if (!pfunc.IsAllocated()) {
452e8d8bef9SDimitry Andric    error.SetErrorStringWithFormat("Could not find class: %s.",
453e8d8bef9SDimitry Andric                                   python_class_name);
45404eeddc0SDimitry Andric    return PythonObject();
455e8d8bef9SDimitry Andric  }
456e8d8bef9SDimitry Andric
4570eae32dcSDimitry Andric  PythonObject result =
458*fe013be4SDimitry Andric      pfunc(SWIGBridge::ToSWIGWrapper(target_sp), SWIGBridge::ToSWIGWrapper(args_impl), dict);
459e8d8bef9SDimitry Andric
4600eae32dcSDimitry Andric  if (result.IsAllocated()) {
461e8d8bef9SDimitry Andric    // Check that the handle_stop callback is defined:
462e8d8bef9SDimitry Andric    auto callback_func = result.ResolveName<PythonCallable>("handle_stop");
463e8d8bef9SDimitry Andric    if (callback_func.IsAllocated()) {
464e8d8bef9SDimitry Andric      if (auto args_info = callback_func.GetArgInfo()) {
465e8d8bef9SDimitry Andric        size_t num_args = (*args_info).max_positional_args;
466e8d8bef9SDimitry Andric        if (num_args != 2) {
4670eae32dcSDimitry Andric          error.SetErrorStringWithFormat(
4680eae32dcSDimitry Andric              "Wrong number of args for "
469e8d8bef9SDimitry Andric              "handle_stop callback, should be 2 (excluding self), got: %zu",
470e8d8bef9SDimitry Andric              num_args);
47104eeddc0SDimitry Andric          return PythonObject();
472e8d8bef9SDimitry Andric        } else
47304eeddc0SDimitry Andric          return result;
474e8d8bef9SDimitry Andric      } else {
475e8d8bef9SDimitry Andric        error.SetErrorString("Couldn't get num arguments for handle_stop "
476e8d8bef9SDimitry Andric                             "callback.");
47704eeddc0SDimitry Andric        return PythonObject();
478e8d8bef9SDimitry Andric      }
47904eeddc0SDimitry Andric      return result;
4800eae32dcSDimitry Andric    } else {
481e8d8bef9SDimitry Andric      error.SetErrorStringWithFormat("Class \"%s\" is missing the required "
482e8d8bef9SDimitry Andric                                     "handle_stop callback.",
483e8d8bef9SDimitry Andric                                     python_class_name);
484e8d8bef9SDimitry Andric    }
485e8d8bef9SDimitry Andric  }
48604eeddc0SDimitry Andric  return PythonObject();
487e8d8bef9SDimitry Andric}
488e8d8bef9SDimitry Andric
489*fe013be4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonStopHookCallHandleStop(
4900eae32dcSDimitry Andric    void *implementor, lldb::ExecutionContextRefSP exc_ctx_sp,
4910eae32dcSDimitry Andric    lldb::StreamSP stream) {
492e8d8bef9SDimitry Andric  // handle_stop will return a bool with the meaning "should_stop"...
493e8d8bef9SDimitry Andric  // If you return nothing we'll assume we are going to stop.
494e8d8bef9SDimitry Andric  // Also any errors should return true, since we should stop on error.
495e8d8bef9SDimitry Andric
496e8d8bef9SDimitry Andric  PyErr_Cleaner py_err_cleaner(false);
497e8d8bef9SDimitry Andric  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
498e8d8bef9SDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("handle_stop");
499e8d8bef9SDimitry Andric
500e8d8bef9SDimitry Andric  if (!pfunc.IsAllocated())
501e8d8bef9SDimitry Andric    return true;
502e8d8bef9SDimitry Andric
5030eae32dcSDimitry Andric  auto *sb_stream = new lldb::SBStream();
5040eae32dcSDimitry Andric  PythonObject sb_stream_arg =
505*fe013be4SDimitry Andric      SWIGBridge::ToSWIGWrapper(std::unique_ptr<lldb::SBStream>(sb_stream));
5060eae32dcSDimitry Andric  PythonObject result =
507*fe013be4SDimitry Andric      pfunc(SWIGBridge::ToSWIGWrapper(std::move(exc_ctx_sp)), sb_stream_arg);
508e8d8bef9SDimitry Andric
5090eae32dcSDimitry Andric  if (PyErr_Occurred()) {
510e8d8bef9SDimitry Andric    stream->PutCString("Python error occurred handling stop-hook.");
511e8d8bef9SDimitry Andric    PyErr_Print();
512e8d8bef9SDimitry Andric    PyErr_Clear();
513e8d8bef9SDimitry Andric    return true;
514e8d8bef9SDimitry Andric  }
515e8d8bef9SDimitry Andric
516e8d8bef9SDimitry Andric  // Now add the result to the output stream.  SBStream only
517e8d8bef9SDimitry Andric  // makes an internally help StreamString which I can't interpose, so I
518e8d8bef9SDimitry Andric  // have to copy it over here.
5190eae32dcSDimitry Andric  stream->PutCString(sb_stream->GetData());
520e8d8bef9SDimitry Andric
521e8d8bef9SDimitry Andric  if (result.get() == Py_False)
522e8d8bef9SDimitry Andric    return false;
523e8d8bef9SDimitry Andric  else
524e8d8bef9SDimitry Andric    return true;
525e8d8bef9SDimitry Andric}
526e8d8bef9SDimitry Andric
5270eae32dcSDimitry Andric// wrapper that calls an optional instance member of an object taking no
5280eae32dcSDimitry Andric// arguments
5290eae32dcSDimitry Andricstatic PyObject *LLDBSwigPython_CallOptionalMember(
5300eae32dcSDimitry Andric    PyObject * implementor, char *callee_name,
5310eae32dcSDimitry Andric    PyObject *ret_if_not_found = Py_None, bool *was_found = NULL) {
532130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(false);
533130d950cSDimitry Andric
534130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
535130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>(callee_name);
536130d950cSDimitry Andric
5370eae32dcSDimitry Andric  if (!pfunc.IsAllocated()) {
538130d950cSDimitry Andric    if (was_found)
539130d950cSDimitry Andric      *was_found = false;
540130d950cSDimitry Andric    Py_XINCREF(ret_if_not_found);
541130d950cSDimitry Andric    return ret_if_not_found;
542130d950cSDimitry Andric  }
543130d950cSDimitry Andric
544130d950cSDimitry Andric  if (was_found)
545130d950cSDimitry Andric    *was_found = true;
546130d950cSDimitry Andric
547130d950cSDimitry Andric  PythonObject result = pfunc();
548130d950cSDimitry Andric  return result.release();
549130d950cSDimitry Andric}
550130d950cSDimitry Andric
551*fe013be4SDimitry Andricsize_t lldb_private::python::SWIGBridge::LLDBSwigPython_CalculateNumChildren(PyObject * implementor,
5520eae32dcSDimitry Andric                                                         uint32_t max) {
553130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
554130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("num_children");
555130d950cSDimitry Andric
556130d950cSDimitry Andric  if (!pfunc.IsAllocated())
557130d950cSDimitry Andric    return 0;
558130d950cSDimitry Andric
559130d950cSDimitry Andric  auto arg_info = pfunc.GetArgInfo();
560130d950cSDimitry Andric  if (!arg_info) {
561130d950cSDimitry Andric    llvm::consumeError(arg_info.takeError());
562130d950cSDimitry Andric    return 0;
563130d950cSDimitry Andric  }
564130d950cSDimitry Andric
5655ffd83dbSDimitry Andric  size_t ret_val;
566130d950cSDimitry Andric  if (arg_info.get().max_positional_args < 1)
5675ffd83dbSDimitry Andric    ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call()));
568130d950cSDimitry Andric  else
5690eae32dcSDimitry Andric    ret_val = unwrapOrSetPythonException(
5700eae32dcSDimitry Andric        As<long long>(pfunc.Call(PythonInteger(max))));
571130d950cSDimitry Andric
5720eae32dcSDimitry Andric  if (PyErr_Occurred()) {
573130d950cSDimitry Andric    PyErr_Print();
574130d950cSDimitry Andric    PyErr_Clear();
5755ffd83dbSDimitry Andric    return 0;
576130d950cSDimitry Andric  }
577130d950cSDimitry Andric
578130d950cSDimitry Andric  if (arg_info.get().max_positional_args < 1)
579130d950cSDimitry Andric    ret_val = std::min(ret_val, static_cast<size_t>(max));
580130d950cSDimitry Andric
581130d950cSDimitry Andric  return ret_val;
582130d950cSDimitry Andric}
583130d950cSDimitry Andric
584*fe013be4SDimitry AndricPyObject *lldb_private::python::SWIGBridge::LLDBSwigPython_GetChildAtIndex(PyObject * implementor,
5850eae32dcSDimitry Andric                                                       uint32_t idx) {
586130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
587130d950cSDimitry Andric
588130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
589130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index");
590130d950cSDimitry Andric
591130d950cSDimitry Andric  if (!pfunc.IsAllocated())
592130d950cSDimitry Andric    return nullptr;
593130d950cSDimitry Andric
594130d950cSDimitry Andric  PythonObject result = pfunc(PythonInteger(idx));
595130d950cSDimitry Andric
596130d950cSDimitry Andric  if (!result.IsAllocated())
597130d950cSDimitry Andric    return nullptr;
598130d950cSDimitry Andric
599130d950cSDimitry Andric  lldb::SBValue *sbvalue_ptr = nullptr;
6000eae32dcSDimitry Andric  if (SWIG_ConvertPtr(result.get(), (void **)&sbvalue_ptr,
6010eae32dcSDimitry Andric                      SWIGTYPE_p_lldb__SBValue, 0) == -1)
602130d950cSDimitry Andric    return nullptr;
603130d950cSDimitry Andric
604130d950cSDimitry Andric  if (sbvalue_ptr == nullptr)
605130d950cSDimitry Andric    return nullptr;
606130d950cSDimitry Andric
607130d950cSDimitry Andric  return result.release();
608130d950cSDimitry Andric}
609130d950cSDimitry Andric
610*fe013be4SDimitry Andricint lldb_private::python::SWIGBridge::LLDBSwigPython_GetIndexOfChildWithName(
6110eae32dcSDimitry Andric    PyObject * implementor, const char *child_name) {
612130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
613130d950cSDimitry Andric
614130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
615130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("get_child_index");
616130d950cSDimitry Andric
617130d950cSDimitry Andric  if (!pfunc.IsAllocated())
618130d950cSDimitry Andric    return UINT32_MAX;
619130d950cSDimitry Andric
6205ffd83dbSDimitry Andric  llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name));
621130d950cSDimitry Andric
6220eae32dcSDimitry Andric  long long retval =
6230eae32dcSDimitry Andric      unwrapOrSetPythonException(As<long long>(std::move(result)));
6245ffd83dbSDimitry Andric
6255ffd83dbSDimitry Andric  if (PyErr_Occurred()) {
6265ffd83dbSDimitry Andric    PyErr_Clear(); // FIXME print this? do something else
627130d950cSDimitry Andric    return UINT32_MAX;
6285ffd83dbSDimitry Andric  }
629130d950cSDimitry Andric
630130d950cSDimitry Andric  if (retval >= 0)
631130d950cSDimitry Andric    return (uint32_t)retval;
632130d950cSDimitry Andric
633130d950cSDimitry Andric  return UINT32_MAX;
634130d950cSDimitry Andric}
635130d950cSDimitry Andric
636*fe013be4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPython_UpdateSynthProviderInstance(PyObject *
6370eae32dcSDimitry Andric                                                              implementor) {
638130d950cSDimitry Andric  bool ret_val = false;
639130d950cSDimitry Andric
640130d950cSDimitry Andric  static char callee_name[] = "update";
641130d950cSDimitry Andric
6420eae32dcSDimitry Andric  PyObject *py_return =
6430eae32dcSDimitry Andric      LLDBSwigPython_CallOptionalMember(implementor, callee_name);
644130d950cSDimitry Andric
645130d950cSDimitry Andric  if (py_return == Py_True)
646130d950cSDimitry Andric    ret_val = true;
647130d950cSDimitry Andric
648130d950cSDimitry Andric  Py_XDECREF(py_return);
649130d950cSDimitry Andric
650130d950cSDimitry Andric  return ret_val;
651130d950cSDimitry Andric}
652130d950cSDimitry Andric
653*fe013be4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPython_MightHaveChildrenSynthProviderInstance(
6540eae32dcSDimitry Andric    PyObject * implementor) {
655130d950cSDimitry Andric  bool ret_val = false;
656130d950cSDimitry Andric
657130d950cSDimitry Andric  static char callee_name[] = "has_children";
658130d950cSDimitry Andric
6590eae32dcSDimitry Andric  PyObject *py_return =
6600eae32dcSDimitry Andric      LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_True);
661130d950cSDimitry Andric
662130d950cSDimitry Andric  if (py_return == Py_True)
663130d950cSDimitry Andric    ret_val = true;
664130d950cSDimitry Andric
665130d950cSDimitry Andric  Py_XDECREF(py_return);
666130d950cSDimitry Andric
667130d950cSDimitry Andric  return ret_val;
668130d950cSDimitry Andric}
669130d950cSDimitry Andric
670*fe013be4SDimitry AndricPyObject *lldb_private::python::SWIGBridge::LLDBSwigPython_GetValueSynthProviderInstance(
6710eae32dcSDimitry Andric    PyObject * implementor) {
672130d950cSDimitry Andric  PyObject *ret_val = nullptr;
673130d950cSDimitry Andric
674130d950cSDimitry Andric  static char callee_name[] = "get_value";
675130d950cSDimitry Andric
6760eae32dcSDimitry Andric  PyObject *py_return =
6770eae32dcSDimitry Andric      LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_None);
678130d950cSDimitry Andric
679130d950cSDimitry Andric  if (py_return == Py_None || py_return == nullptr)
680130d950cSDimitry Andric    ret_val = nullptr;
681130d950cSDimitry Andric
682130d950cSDimitry Andric  lldb::SBValue *sbvalue_ptr = NULL;
683130d950cSDimitry Andric
6840eae32dcSDimitry Andric  if (SWIG_ConvertPtr(py_return, (void **)&sbvalue_ptr,
6850eae32dcSDimitry Andric                      SWIGTYPE_p_lldb__SBValue, 0) == -1)
686130d950cSDimitry Andric    ret_val = nullptr;
687130d950cSDimitry Andric  else if (sbvalue_ptr == NULL)
688130d950cSDimitry Andric    ret_val = nullptr;
689130d950cSDimitry Andric  else
690130d950cSDimitry Andric    ret_val = py_return;
691130d950cSDimitry Andric
692130d950cSDimitry Andric  Py_XDECREF(py_return);
693130d950cSDimitry Andric  return ret_val;
694130d950cSDimitry Andric}
695130d950cSDimitry Andric
696*fe013be4SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBData(PyObject * data) {
697fe6060f1SDimitry Andric  lldb::SBData *sb_ptr = nullptr;
698fe6060f1SDimitry Andric
6990eae32dcSDimitry Andric  int valid_cast =
7000eae32dcSDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0);
701fe6060f1SDimitry Andric
702fe6060f1SDimitry Andric  if (valid_cast == -1)
703fe6060f1SDimitry Andric    return NULL;
704fe6060f1SDimitry Andric
705fe6060f1SDimitry Andric  return sb_ptr;
706fe6060f1SDimitry Andric}
707fe6060f1SDimitry Andric
708*fe013be4SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(PyObject * data) {
709*fe013be4SDimitry Andric  lldb::SBBreakpoint *sb_ptr = nullptr;
710*fe013be4SDimitry Andric
711*fe013be4SDimitry Andric  int valid_cast =
712*fe013be4SDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBBreakpoint, 0);
713*fe013be4SDimitry Andric
714*fe013be4SDimitry Andric  if (valid_cast == -1)
715*fe013be4SDimitry Andric    return NULL;
716*fe013be4SDimitry Andric
717*fe013be4SDimitry Andric  return sb_ptr;
718*fe013be4SDimitry Andric}
719*fe013be4SDimitry Andric
720*fe013be4SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(PyObject * data) {
721*fe013be4SDimitry Andric  lldb::SBAttachInfo *sb_ptr = nullptr;
722*fe013be4SDimitry Andric
723*fe013be4SDimitry Andric  int valid_cast =
724*fe013be4SDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBAttachInfo, 0);
725*fe013be4SDimitry Andric
726*fe013be4SDimitry Andric  if (valid_cast == -1)
727*fe013be4SDimitry Andric    return NULL;
728*fe013be4SDimitry Andric
729*fe013be4SDimitry Andric  return sb_ptr;
730*fe013be4SDimitry Andric}
731*fe013be4SDimitry Andric
732*fe013be4SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(PyObject * data) {
733*fe013be4SDimitry Andric  lldb::SBLaunchInfo *sb_ptr = nullptr;
734*fe013be4SDimitry Andric
735*fe013be4SDimitry Andric  int valid_cast =
736*fe013be4SDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBLaunchInfo, 0);
737*fe013be4SDimitry Andric
738*fe013be4SDimitry Andric  if (valid_cast == -1)
739*fe013be4SDimitry Andric    return NULL;
740*fe013be4SDimitry Andric
741*fe013be4SDimitry Andric  return sb_ptr;
742*fe013be4SDimitry Andric}
743*fe013be4SDimitry Andric
744*fe013be4SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBError(PyObject * data) {
745fe6060f1SDimitry Andric  lldb::SBError *sb_ptr = nullptr;
746fe6060f1SDimitry Andric
7470eae32dcSDimitry Andric  int valid_cast =
7480eae32dcSDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0);
749fe6060f1SDimitry Andric
750fe6060f1SDimitry Andric  if (valid_cast == -1)
751fe6060f1SDimitry Andric    return NULL;
752fe6060f1SDimitry Andric
753fe6060f1SDimitry Andric  return sb_ptr;
754fe6060f1SDimitry Andric}
755fe6060f1SDimitry Andric
756*fe013be4SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBValue(PyObject * data) {
757130d950cSDimitry Andric  lldb::SBValue *sb_ptr = NULL;
758130d950cSDimitry Andric
7590eae32dcSDimitry Andric  int valid_cast =
7600eae32dcSDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
761130d950cSDimitry Andric
762130d950cSDimitry Andric  if (valid_cast == -1)
763130d950cSDimitry Andric    return NULL;
764130d950cSDimitry Andric
765130d950cSDimitry Andric  return sb_ptr;
766130d950cSDimitry Andric}
767130d950cSDimitry Andric
768*fe013be4SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *
7690eae32dcSDimitry Andric                                                                    data) {
770349cc55cSDimitry Andric  lldb::SBMemoryRegionInfo *sb_ptr = NULL;
771349cc55cSDimitry Andric
7720eae32dcSDimitry Andric  int valid_cast = SWIG_ConvertPtr(data, (void **)&sb_ptr,
7730eae32dcSDimitry Andric                                   SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0);
774349cc55cSDimitry Andric
775349cc55cSDimitry Andric  if (valid_cast == -1)
776349cc55cSDimitry Andric    return NULL;
777349cc55cSDimitry Andric
778349cc55cSDimitry Andric  return sb_ptr;
779349cc55cSDimitry Andric}
780349cc55cSDimitry Andric
781*fe013be4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonCallCommand(
7820eae32dcSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
7830eae32dcSDimitry Andric    lldb::DebuggerSP debugger, const char *args,
784130d950cSDimitry Andric    lldb_private::CommandReturnObject &cmd_retobj,
7850eae32dcSDimitry Andric    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
786130d950cSDimitry Andric
787130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
7880eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
7890eae32dcSDimitry Andric      session_dictionary_name);
7900eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
7910eae32dcSDimitry Andric      python_function_name, dict);
792130d950cSDimitry Andric
793130d950cSDimitry Andric  if (!pfunc.IsAllocated())
794130d950cSDimitry Andric    return false;
795130d950cSDimitry Andric
796130d950cSDimitry Andric  auto argc = pfunc.GetArgInfo();
797130d950cSDimitry Andric  if (!argc) {
798130d950cSDimitry Andric    llvm::consumeError(argc.takeError());
799130d950cSDimitry Andric    return false;
800130d950cSDimitry Andric  }
801*fe013be4SDimitry Andric  PythonObject debugger_arg = SWIGBridge::ToSWIGWrapper(std::move(debugger));
802*fe013be4SDimitry Andric  auto cmd_retobj_arg = SWIGBridge::ToSWIGWrapper(cmd_retobj);
803130d950cSDimitry Andric
804130d950cSDimitry Andric  if (argc.get().max_positional_args < 5u)
80504eeddc0SDimitry Andric    pfunc(debugger_arg, PythonString(args), cmd_retobj_arg.obj(), dict);
806130d950cSDimitry Andric  else
8070eae32dcSDimitry Andric    pfunc(debugger_arg, PythonString(args),
808*fe013be4SDimitry Andric          SWIGBridge::ToSWIGWrapper(std::move(exe_ctx_ref_sp)), cmd_retobj_arg.obj(), dict);
809130d950cSDimitry Andric
810130d950cSDimitry Andric  return true;
811130d950cSDimitry Andric}
812130d950cSDimitry Andric
813*fe013be4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonCallCommandObject(
8140eae32dcSDimitry Andric    PyObject *implementor, lldb::DebuggerSP debugger, const char *args,
815130d950cSDimitry Andric    lldb_private::CommandReturnObject &cmd_retobj,
8160eae32dcSDimitry Andric    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
817130d950cSDimitry Andric
818130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
819130d950cSDimitry Andric
820130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
821130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("__call__");
822130d950cSDimitry Andric
823130d950cSDimitry Andric  if (!pfunc.IsAllocated())
824130d950cSDimitry Andric    return false;
825130d950cSDimitry Andric
826*fe013be4SDimitry Andric  auto cmd_retobj_arg = SWIGBridge::ToSWIGWrapper(cmd_retobj);
827130d950cSDimitry Andric
828*fe013be4SDimitry Andric  pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger)), PythonString(args),
829*fe013be4SDimitry Andric        SWIGBridge::ToSWIGWrapper(exe_ctx_ref_sp), cmd_retobj_arg.obj());
830130d950cSDimitry Andric
831130d950cSDimitry Andric  return true;
832130d950cSDimitry Andric}
833130d950cSDimitry Andric
834*fe013be4SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSWIGPythonCreateOSPlugin(
8350eae32dcSDimitry Andric    const char *python_class_name, const char *session_dictionary_name,
8360eae32dcSDimitry Andric    const lldb::ProcessSP &process_sp) {
8370eae32dcSDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
8380eae32dcSDimitry Andric      !session_dictionary_name)
83904eeddc0SDimitry Andric    return PythonObject();
840130d950cSDimitry Andric
841130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
842130d950cSDimitry Andric
8430eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
8440eae32dcSDimitry Andric      session_dictionary_name);
8450eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
8460eae32dcSDimitry Andric      python_class_name, dict);
847130d950cSDimitry Andric
848130d950cSDimitry Andric  if (!pfunc.IsAllocated())
84904eeddc0SDimitry Andric    return PythonObject();
850130d950cSDimitry Andric
851*fe013be4SDimitry Andric  return pfunc(SWIGBridge::ToSWIGWrapper(process_sp));
852130d950cSDimitry Andric}
853130d950cSDimitry Andric
854*fe013be4SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSWIGPython_CreateFrameRecognizer(
8550eae32dcSDimitry Andric    const char *python_class_name, const char *session_dictionary_name) {
8560eae32dcSDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
8570eae32dcSDimitry Andric      !session_dictionary_name)
85804eeddc0SDimitry Andric    return PythonObject();
859130d950cSDimitry Andric
860130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
861130d950cSDimitry Andric
8620eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
8630eae32dcSDimitry Andric      session_dictionary_name);
8640eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
8650eae32dcSDimitry Andric      python_class_name, dict);
866130d950cSDimitry Andric
867130d950cSDimitry Andric  if (!pfunc.IsAllocated())
86804eeddc0SDimitry Andric    return PythonObject();
869130d950cSDimitry Andric
87004eeddc0SDimitry Andric  return pfunc();
871130d950cSDimitry Andric}
872130d950cSDimitry Andric
873*fe013be4SDimitry AndricPyObject *lldb_private::python::SWIGBridge::LLDBSwigPython_GetRecognizedArguments(
8740eae32dcSDimitry Andric    PyObject * implementor, const lldb::StackFrameSP &frame_sp) {
875130d950cSDimitry Andric  static char callee_name[] = "get_recognized_arguments";
876130d950cSDimitry Andric
877*fe013be4SDimitry Andric  PythonObject arg = SWIGBridge::ToSWIGWrapper(frame_sp);
878130d950cSDimitry Andric
879130d950cSDimitry Andric  PythonString str(callee_name);
8800eae32dcSDimitry Andric  PyObject *result =
8810eae32dcSDimitry Andric      PyObject_CallMethodObjArgs(implementor, str.get(), arg.get(), NULL);
882130d950cSDimitry Andric  return result;
883130d950cSDimitry Andric}
884130d950cSDimitry Andric
885*fe013be4SDimitry Andricvoid *lldb_private::python::SWIGBridge::LLDBSWIGPython_GetDynamicSetting(
8860eae32dcSDimitry Andric    void *module, const char *setting, const lldb::TargetSP &target_sp) {
887130d950cSDimitry Andric  if (!module || !setting)
888130d950cSDimitry Andric    Py_RETURN_NONE;
889130d950cSDimitry Andric
890130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
891130d950cSDimitry Andric  PythonObject py_module(PyRefType::Borrowed, (PyObject *)module);
892130d950cSDimitry Andric  auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting");
893130d950cSDimitry Andric
894130d950cSDimitry Andric  if (!pfunc.IsAllocated())
895130d950cSDimitry Andric    Py_RETURN_NONE;
896130d950cSDimitry Andric
897*fe013be4SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(target_sp), PythonString(setting));
898130d950cSDimitry Andric
899130d950cSDimitry Andric  return result.release();
900130d950cSDimitry Andric}
901130d950cSDimitry Andric
902*fe013be4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordProcess(
9034824e7fdSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
9044824e7fdSDimitry Andric    const lldb::ProcessSP &process, std::string &output) {
905130d950cSDimitry Andric
9060eae32dcSDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9070eae32dcSDimitry Andric      !session_dictionary_name)
908130d950cSDimitry Andric    return false;
909130d950cSDimitry Andric
910130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
911130d950cSDimitry Andric
9120eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9130eae32dcSDimitry Andric      session_dictionary_name);
9140eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9150eae32dcSDimitry Andric      python_function_name, dict);
916130d950cSDimitry Andric
917130d950cSDimitry Andric  if (!pfunc.IsAllocated())
918130d950cSDimitry Andric    return false;
919130d950cSDimitry Andric
920*fe013be4SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(process), dict);
921130d950cSDimitry Andric
922130d950cSDimitry Andric  output = result.Str().GetString().str();
923130d950cSDimitry Andric
924130d950cSDimitry Andric  return true;
925130d950cSDimitry Andric}
926130d950cSDimitry Andric
927*fe013be4SDimitry Andricstd::optional<std::string> lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordThread(
9280eae32dcSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
9290eae32dcSDimitry Andric    lldb::ThreadSP thread) {
9300eae32dcSDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9310eae32dcSDimitry Andric      !session_dictionary_name)
932bdd1243dSDimitry Andric    return std::nullopt;
933130d950cSDimitry Andric
934130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
935130d950cSDimitry Andric
9360eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9370eae32dcSDimitry Andric      session_dictionary_name);
9380eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9390eae32dcSDimitry Andric      python_function_name, dict);
940130d950cSDimitry Andric
941130d950cSDimitry Andric  if (!pfunc.IsAllocated())
942bdd1243dSDimitry Andric    return std::nullopt;
943130d950cSDimitry Andric
944*fe013be4SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(std::move(thread)), dict);
945130d950cSDimitry Andric
9460eae32dcSDimitry Andric  return result.Str().GetString().str();
947130d950cSDimitry Andric}
948130d950cSDimitry Andric
949*fe013be4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordTarget(
9504824e7fdSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
9514824e7fdSDimitry Andric    const lldb::TargetSP &target, std::string &output) {
952130d950cSDimitry Andric
9530eae32dcSDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9540eae32dcSDimitry Andric      !session_dictionary_name)
955130d950cSDimitry Andric    return false;
956130d950cSDimitry Andric
957130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
958130d950cSDimitry Andric
9590eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9600eae32dcSDimitry Andric      session_dictionary_name);
9610eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9620eae32dcSDimitry Andric      python_function_name, dict);
963130d950cSDimitry Andric
964130d950cSDimitry Andric  if (!pfunc.IsAllocated())
965130d950cSDimitry Andric    return false;
966130d950cSDimitry Andric
967*fe013be4SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(target), dict);
968130d950cSDimitry Andric
969130d950cSDimitry Andric  output = result.Str().GetString().str();
970130d950cSDimitry Andric
971130d950cSDimitry Andric  return true;
972130d950cSDimitry Andric}
973130d950cSDimitry Andric
974*fe013be4SDimitry Andricstd::optional<std::string> lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordFrame(
9750eae32dcSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
9760eae32dcSDimitry Andric    lldb::StackFrameSP frame) {
9770eae32dcSDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9780eae32dcSDimitry Andric      !session_dictionary_name)
979bdd1243dSDimitry Andric    return std::nullopt;
980130d950cSDimitry Andric
981130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
982130d950cSDimitry Andric
9830eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9840eae32dcSDimitry Andric      session_dictionary_name);
9850eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9860eae32dcSDimitry Andric      python_function_name, dict);
987130d950cSDimitry Andric
988130d950cSDimitry Andric  if (!pfunc.IsAllocated())
989bdd1243dSDimitry Andric    return std::nullopt;
990130d950cSDimitry Andric
991*fe013be4SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(std::move(frame)), dict);
992130d950cSDimitry Andric
9930eae32dcSDimitry Andric  return result.Str().GetString().str();
994130d950cSDimitry Andric}
995130d950cSDimitry Andric
996*fe013be4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordValue(
9974824e7fdSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
9984824e7fdSDimitry Andric    const lldb::ValueObjectSP &value, std::string &output) {
999130d950cSDimitry Andric
10000eae32dcSDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
10010eae32dcSDimitry Andric      !session_dictionary_name)
1002130d950cSDimitry Andric    return false;
1003130d950cSDimitry Andric
1004130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
1005130d950cSDimitry Andric
10060eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10070eae32dcSDimitry Andric      session_dictionary_name);
10080eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10090eae32dcSDimitry Andric      python_function_name, dict);
1010130d950cSDimitry Andric
1011130d950cSDimitry Andric  if (!pfunc.IsAllocated())
1012130d950cSDimitry Andric    return false;
1013130d950cSDimitry Andric
1014*fe013be4SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(value), dict);
1015130d950cSDimitry Andric
1016130d950cSDimitry Andric  output = result.Str().GetString().str();
1017130d950cSDimitry Andric
1018130d950cSDimitry Andric  return true;
1019130d950cSDimitry Andric}
1020130d950cSDimitry Andric
1021*fe013be4SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonCallModuleInit(
10220eae32dcSDimitry Andric    const char *python_module_name, const char *session_dictionary_name,
10230eae32dcSDimitry Andric    lldb::DebuggerSP debugger) {
1024130d950cSDimitry Andric  std::string python_function_name_string = python_module_name;
1025130d950cSDimitry Andric  python_function_name_string += ".__lldb_init_module";
1026130d950cSDimitry Andric  const char *python_function_name = python_function_name_string.c_str();
1027130d950cSDimitry Andric
1028130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
1029130d950cSDimitry Andric
10300eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10310eae32dcSDimitry Andric      session_dictionary_name);
10320eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10330eae32dcSDimitry Andric      python_function_name, dict);
1034130d950cSDimitry Andric
1035130d950cSDimitry Andric  // This method is optional and need not exist.  So if we don't find it,
1036130d950cSDimitry Andric  // it's actually a success, not a failure.
1037130d950cSDimitry Andric  if (!pfunc.IsAllocated())
1038130d950cSDimitry Andric    return true;
1039130d950cSDimitry Andric
1040*fe013be4SDimitry Andric  pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger)), dict);
1041130d950cSDimitry Andric
1042130d950cSDimitry Andric  return true;
1043130d950cSDimitry Andric}
1044130d950cSDimitry Andric
1045*fe013be4SDimitry Andriclldb::ValueObjectSP lldb_private::python::SWIGBridge::LLDBSWIGPython_GetValueObjectSPFromSBValue(
10460eae32dcSDimitry Andric    void *data) {
1047130d950cSDimitry Andric  lldb::ValueObjectSP valobj_sp;
10480eae32dcSDimitry Andric  if (data) {
1049130d950cSDimitry Andric    lldb::SBValue *sb_ptr = (lldb::SBValue *)data;
1050130d950cSDimitry Andric    valobj_sp = sb_ptr->GetSP();
1051130d950cSDimitry Andric  }
1052130d950cSDimitry Andric  return valobj_sp;
1053130d950cSDimitry Andric}
1054130d950cSDimitry Andric
1055130d950cSDimitry Andric// For the LogOutputCallback functions
10560eae32dcSDimitry Andricstatic void LLDBSwigPythonCallPythonLogOutputCallback(const char *str,
10570eae32dcSDimitry Andric                                                      void *baton) {
1058130d950cSDimitry Andric  if (baton != Py_None) {
1059130d950cSDimitry Andric    SWIG_PYTHON_THREAD_BEGIN_BLOCK;
10600eae32dcSDimitry Andric    PyObject *result = PyObject_CallFunction(
10610eae32dcSDimitry Andric        reinterpret_cast<PyObject *>(baton), const_cast<char *>("s"), str);
1062130d950cSDimitry Andric    Py_XDECREF(result);
1063130d950cSDimitry Andric    SWIG_PYTHON_THREAD_END_BLOCK;
1064130d950cSDimitry Andric  }
1065130d950cSDimitry Andric}
1066*fe013be4SDimitry Andric
1067*fe013be4SDimitry Andric// For DebuggerTerminateCallback functions
1068*fe013be4SDimitry Andricstatic void LLDBSwigPythonCallPythonSBDebuggerTerminateCallback(lldb::user_id_t debugger_id,
1069*fe013be4SDimitry Andric                                                      void *baton) {
1070*fe013be4SDimitry Andric  if (baton != Py_None) {
1071*fe013be4SDimitry Andric    SWIG_PYTHON_THREAD_BEGIN_BLOCK;
1072*fe013be4SDimitry Andric    PyObject *result = PyObject_CallFunction(
1073*fe013be4SDimitry Andric        reinterpret_cast<PyObject *>(baton), const_cast<char *>("l"), debugger_id);
1074*fe013be4SDimitry Andric    Py_XDECREF(result);
1075*fe013be4SDimitry Andric    SWIG_PYTHON_THREAD_END_BLOCK;
1076*fe013be4SDimitry Andric  }
1077*fe013be4SDimitry Andric}
1078*fe013be4SDimitry Andric
1079*fe013be4SDimitry Andricstatic SBError LLDBSwigPythonCallLocateModuleCallback(
1080*fe013be4SDimitry Andric    void *callback_baton, const SBModuleSpec &module_spec_sb,
1081*fe013be4SDimitry Andric    SBFileSpec &module_file_spec_sb, SBFileSpec &symbol_file_spec_sb) {
1082*fe013be4SDimitry Andric  SWIG_Python_Thread_Block swig_thread_block;
1083*fe013be4SDimitry Andric
1084*fe013be4SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
1085*fe013be4SDimitry Andric  PythonObject module_spec_arg = SWIGBridge::ToSWIGWrapper(
1086*fe013be4SDimitry Andric      std::make_unique<SBModuleSpec>(module_spec_sb));
1087*fe013be4SDimitry Andric  PythonObject module_file_spec_arg = SWIGBridge::ToSWIGWrapper(
1088*fe013be4SDimitry Andric      std::make_unique<SBFileSpec>(module_file_spec_sb));
1089*fe013be4SDimitry Andric  PythonObject symbol_file_spec_arg = SWIGBridge::ToSWIGWrapper(
1090*fe013be4SDimitry Andric      std::make_unique<SBFileSpec>(symbol_file_spec_sb));
1091*fe013be4SDimitry Andric
1092*fe013be4SDimitry Andric  PythonCallable callable =
1093*fe013be4SDimitry Andric      Retain<PythonCallable>(reinterpret_cast<PyObject *>(callback_baton));
1094*fe013be4SDimitry Andric  if (!callable.IsValid()) {
1095*fe013be4SDimitry Andric    return SBError("The callback callable is not valid.");
1096*fe013be4SDimitry Andric  }
1097*fe013be4SDimitry Andric
1098*fe013be4SDimitry Andric  PythonObject result = callable(module_spec_arg, module_file_spec_arg,
1099*fe013be4SDimitry Andric                                 symbol_file_spec_arg);
1100*fe013be4SDimitry Andric
1101*fe013be4SDimitry Andric  if (!result.IsAllocated())
1102*fe013be4SDimitry Andric    return SBError("No result.");
1103*fe013be4SDimitry Andric  lldb::SBError *sb_error_ptr = nullptr;
1104*fe013be4SDimitry Andric  if (SWIG_ConvertPtr(result.get(), (void **)&sb_error_ptr,
1105*fe013be4SDimitry Andric                      SWIGTYPE_p_lldb__SBError, 0) == -1) {
1106*fe013be4SDimitry Andric    return SBError("Result is not SBError.");
1107*fe013be4SDimitry Andric  }
1108*fe013be4SDimitry Andric
1109*fe013be4SDimitry Andric  if (sb_error_ptr->Success()) {
1110*fe013be4SDimitry Andric    lldb::SBFileSpec *sb_module_file_spec_ptr = nullptr;
1111*fe013be4SDimitry Andric    if (SWIG_ConvertPtr(module_file_spec_arg.get(),
1112*fe013be4SDimitry Andric                        (void **)&sb_module_file_spec_ptr,
1113*fe013be4SDimitry Andric                        SWIGTYPE_p_lldb__SBFileSpec, 0) == -1)
1114*fe013be4SDimitry Andric      return SBError("module_file_spec is not SBFileSpec.");
1115*fe013be4SDimitry Andric
1116*fe013be4SDimitry Andric    lldb::SBFileSpec *sb_symbol_file_spec_ptr = nullptr;
1117*fe013be4SDimitry Andric    if (SWIG_ConvertPtr(symbol_file_spec_arg.get(),
1118*fe013be4SDimitry Andric                        (void **)&sb_symbol_file_spec_ptr,
1119*fe013be4SDimitry Andric                        SWIGTYPE_p_lldb__SBFileSpec, 0) == -1)
1120*fe013be4SDimitry Andric      return SBError("symbol_file_spec is not SBFileSpec.");
1121*fe013be4SDimitry Andric
1122*fe013be4SDimitry Andric    module_file_spec_sb = *sb_module_file_spec_ptr;
1123*fe013be4SDimitry Andric    symbol_file_spec_sb = *sb_symbol_file_spec_ptr;
1124*fe013be4SDimitry Andric  }
1125*fe013be4SDimitry Andric
1126*fe013be4SDimitry Andric  return *sb_error_ptr;
1127*fe013be4SDimitry Andric}
1128130d950cSDimitry Andric%}
1129