xref: /freebsd-src/contrib/llvm-project/lldb/bindings/python/python-wrapper.swig (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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*06c3fb27SDimitry 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*06c3fb27SDimitry Andric  PythonObject frame_arg = SWIGBridge::ToSWIGWrapper(frame_sp);
41*06c3fb27SDimitry 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*06c3fb27SDimitry 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*06c3fb27SDimitry 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*06c3fb27SDimitry 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*06c3fb27SDimitry 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*06c3fb27SDimitry 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*06c3fb27SDimitry 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*06c3fb27SDimitry 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*06c3fb27SDimitry 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*06c3fb27SDimitry 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*06c3fb27SDimitry Andric  auto sb_value = std::unique_ptr<lldb::SBValue>(new lldb::SBValue(valobj_sp));
199130d950cSDimitry Andric  sb_value->SetPreferSyntheticValue(false);
200130d950cSDimitry Andric
201*06c3fb27SDimitry 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*06c3fb27SDimitry 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*06c3fb27SDimitry Andric  return pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger_sp)), dict);
230130d950cSDimitry Andric}
231130d950cSDimitry Andric
232*06c3fb27SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedObject(
2330eae32dcSDimitry Andric    const char *python_class_name, const char *session_dictionary_name,
234bdd1243dSDimitry Andric    lldb::ExecutionContextRefSP exe_ctx_sp,
2350eae32dcSDimitry Andric    const lldb_private::StructuredDataImpl &args_impl,
2360eae32dcSDimitry Andric    std::string &error_string) {
2370eae32dcSDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
2380eae32dcSDimitry Andric      !session_dictionary_name)
23904eeddc0SDimitry Andric    return PythonObject();
240fe6060f1SDimitry Andric
241fe6060f1SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
242fe6060f1SDimitry Andric
2430eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
2440eae32dcSDimitry Andric      session_dictionary_name);
2450eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
2460eae32dcSDimitry Andric      python_class_name, dict);
247fe6060f1SDimitry Andric
248fe6060f1SDimitry Andric  if (!pfunc.IsAllocated()) {
249fe6060f1SDimitry Andric    error_string.append("could not find script class: ");
250fe6060f1SDimitry Andric    error_string.append(python_class_name);
25104eeddc0SDimitry Andric    return PythonObject();
252fe6060f1SDimitry Andric  }
253fe6060f1SDimitry Andric
254fe6060f1SDimitry Andric  llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
255fe6060f1SDimitry Andric  if (!arg_info) {
256fe6060f1SDimitry Andric    llvm::handleAllErrors(
257fe6060f1SDimitry Andric        arg_info.takeError(),
2580eae32dcSDimitry Andric        [&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
259fe6060f1SDimitry Andric        [&](const llvm::ErrorInfoBase &E) {
260fe6060f1SDimitry Andric          error_string.append(E.message());
261fe6060f1SDimitry Andric        });
26204eeddc0SDimitry Andric    return PythonObject();
263fe6060f1SDimitry Andric  }
264fe6060f1SDimitry Andric
265fe6060f1SDimitry Andric  PythonObject result = {};
266fe6060f1SDimitry Andric  if (arg_info.get().max_positional_args == 2) {
267*06c3fb27SDimitry Andric      result = pfunc(SWIGBridge::ToSWIGWrapper(exe_ctx_sp), SWIGBridge::ToSWIGWrapper(args_impl));
268349cc55cSDimitry Andric  } else {
2690eae32dcSDimitry Andric    error_string.assign("wrong number of arguments in __init__, should be 2 "
2700eae32dcSDimitry Andric                        "(not including self)");
27104eeddc0SDimitry Andric  }
27204eeddc0SDimitry Andric  return result;
273fe6060f1SDimitry Andric}
274349cc55cSDimitry Andric
275*06c3fb27SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedThreadPlan(
2760eae32dcSDimitry Andric    const char *python_class_name, const char *session_dictionary_name,
2770eae32dcSDimitry Andric    const lldb_private::StructuredDataImpl &args_impl,
2780eae32dcSDimitry Andric    std::string &error_string, const lldb::ThreadPlanSP &thread_plan_sp) {
2790eae32dcSDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
2800eae32dcSDimitry Andric      !session_dictionary_name)
28104eeddc0SDimitry Andric    return PythonObject();
282130d950cSDimitry Andric
283130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
284130d950cSDimitry Andric
2850eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
2860eae32dcSDimitry Andric      session_dictionary_name);
2870eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
2880eae32dcSDimitry Andric      python_class_name, dict);
289130d950cSDimitry Andric
290130d950cSDimitry Andric  if (!pfunc.IsAllocated()) {
291130d950cSDimitry Andric    error_string.append("could not find script class: ");
292130d950cSDimitry Andric    error_string.append(python_class_name);
29304eeddc0SDimitry Andric    return PythonObject();
294130d950cSDimitry Andric  }
295130d950cSDimitry Andric
296*06c3fb27SDimitry Andric  PythonObject tp_arg = SWIGBridge::ToSWIGWrapper(thread_plan_sp);
297130d950cSDimitry Andric
298130d950cSDimitry Andric  llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
299130d950cSDimitry Andric  if (!arg_info) {
300130d950cSDimitry Andric    llvm::handleAllErrors(
301130d950cSDimitry Andric        arg_info.takeError(),
3020eae32dcSDimitry Andric        [&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
303130d950cSDimitry Andric        [&](const llvm::ErrorInfoBase &E) {
304130d950cSDimitry Andric          error_string.append(E.message());
305130d950cSDimitry Andric        });
30604eeddc0SDimitry Andric    return PythonObject();
307130d950cSDimitry Andric  }
308130d950cSDimitry Andric
309130d950cSDimitry Andric  PythonObject result = {};
310*06c3fb27SDimitry Andric  auto args_sb = std::unique_ptr<lldb::SBStructuredData>(new lldb::SBStructuredData(args_impl));
311130d950cSDimitry Andric  if (arg_info.get().max_positional_args == 2) {
3120eae32dcSDimitry Andric    if (args_sb->IsValid()) {
3130eae32dcSDimitry Andric      error_string.assign(
3140eae32dcSDimitry Andric          "args passed, but __init__ does not take an args dictionary");
31504eeddc0SDimitry Andric      return PythonObject();
316130d950cSDimitry Andric    }
317130d950cSDimitry Andric    result = pfunc(tp_arg, dict);
318130d950cSDimitry Andric  } else if (arg_info.get().max_positional_args >= 3) {
319*06c3fb27SDimitry Andric    result = pfunc(tp_arg, SWIGBridge::ToSWIGWrapper(std::move(args_sb)), dict);
320130d950cSDimitry Andric  } else {
3210eae32dcSDimitry Andric    error_string.assign("wrong number of arguments in __init__, should be 2 or "
3220eae32dcSDimitry Andric                        "3 (not including self)");
32304eeddc0SDimitry Andric    return PythonObject();
324130d950cSDimitry Andric  }
325130d950cSDimitry Andric
3260eae32dcSDimitry Andric  // FIXME: At this point we should check that the class we found supports all
3270eae32dcSDimitry Andric  // the methods that we need.
328130d950cSDimitry Andric
32904eeddc0SDimitry Andric  return result;
330130d950cSDimitry Andric}
331130d950cSDimitry Andric
332*06c3fb27SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSWIGPythonCallThreadPlan(
3330eae32dcSDimitry Andric    void *implementor, const char *method_name, lldb_private::Event *event,
3340eae32dcSDimitry Andric    bool &got_error) {
335130d950cSDimitry Andric  got_error = false;
336130d950cSDimitry Andric
337130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(false);
338130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
339130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>(method_name);
340130d950cSDimitry Andric
341130d950cSDimitry Andric  if (!pfunc.IsAllocated())
342130d950cSDimitry Andric    return false;
343130d950cSDimitry Andric
344130d950cSDimitry Andric  PythonObject result;
3450eae32dcSDimitry Andric  if (event != nullptr) {
346*06c3fb27SDimitry Andric    ScopedPythonObject<SBEvent> event_arg = SWIGBridge::ToSWIGWrapper(event);
34704eeddc0SDimitry Andric    result = pfunc(event_arg.obj());
3480eae32dcSDimitry Andric  } else
349130d950cSDimitry Andric    result = pfunc();
350130d950cSDimitry Andric
3510eae32dcSDimitry Andric  if (PyErr_Occurred()) {
352130d950cSDimitry Andric    got_error = true;
3530eae32dcSDimitry Andric    printf("Return value was neither false nor true for call to %s.\n",
3540eae32dcSDimitry Andric           method_name);
355130d950cSDimitry Andric    PyErr_Print();
356130d950cSDimitry Andric    return false;
357130d950cSDimitry Andric  }
358130d950cSDimitry Andric
359130d950cSDimitry Andric  if (result.get() == Py_True)
360130d950cSDimitry Andric    return true;
361130d950cSDimitry Andric  else if (result.get() == Py_False)
362130d950cSDimitry Andric    return false;
363130d950cSDimitry Andric
364130d950cSDimitry Andric  // Somebody returned the wrong thing...
365130d950cSDimitry Andric  got_error = true;
366130d950cSDimitry Andric  printf("Wrong return value type for call to %s.\n", method_name);
367130d950cSDimitry Andric  return false;
368130d950cSDimitry Andric}
369130d950cSDimitry Andric
370*06c3fb27SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSWIGPythonCallThreadPlan(
371*06c3fb27SDimitry Andric    void *implementor, const char *method_name, lldb_private::Stream *stream,
372*06c3fb27SDimitry Andric    bool &got_error) {
373*06c3fb27SDimitry Andric  got_error = false;
374*06c3fb27SDimitry Andric
375*06c3fb27SDimitry Andric  PyErr_Cleaner py_err_cleaner(false);
376*06c3fb27SDimitry Andric  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
377*06c3fb27SDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>(method_name);
378*06c3fb27SDimitry Andric
379*06c3fb27SDimitry Andric  if (!pfunc.IsAllocated())
380*06c3fb27SDimitry Andric    return false;
381*06c3fb27SDimitry Andric
382*06c3fb27SDimitry Andric  auto *sb_stream = new lldb::SBStream();
383*06c3fb27SDimitry Andric  PythonObject sb_stream_arg =
384*06c3fb27SDimitry Andric      SWIGBridge::ToSWIGWrapper(std::unique_ptr<lldb::SBStream>(sb_stream));
385*06c3fb27SDimitry Andric
386*06c3fb27SDimitry Andric  PythonObject result;
387*06c3fb27SDimitry Andric  result = pfunc(sb_stream_arg);
388*06c3fb27SDimitry Andric
389*06c3fb27SDimitry Andric  if (PyErr_Occurred()) {
390*06c3fb27SDimitry Andric    printf("Error occured for call to %s.\n",
391*06c3fb27SDimitry Andric           method_name);
392*06c3fb27SDimitry Andric    PyErr_Print();
393*06c3fb27SDimitry Andric    got_error = true;
394*06c3fb27SDimitry Andric    return false;
395*06c3fb27SDimitry Andric  }
396*06c3fb27SDimitry Andric  if (stream)
397*06c3fb27SDimitry Andric    stream->PutCString(sb_stream->GetData());
398*06c3fb27SDimitry Andric  return true;
399*06c3fb27SDimitry Andric
400*06c3fb27SDimitry Andric}
401*06c3fb27SDimitry Andric
402*06c3fb27SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedBreakpointResolver(
4034824e7fdSDimitry Andric    const char *python_class_name, const char *session_dictionary_name,
4040eae32dcSDimitry Andric    const StructuredDataImpl &args_impl,
4054824e7fdSDimitry Andric    const lldb::BreakpointSP &breakpoint_sp) {
4064824e7fdSDimitry Andric
4070eae32dcSDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
4080eae32dcSDimitry Andric      !session_dictionary_name)
40904eeddc0SDimitry Andric    return PythonObject();
410130d950cSDimitry Andric
411130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
412130d950cSDimitry Andric
4130eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
4140eae32dcSDimitry Andric      session_dictionary_name);
4150eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
4160eae32dcSDimitry Andric      python_class_name, dict);
417130d950cSDimitry Andric
418130d950cSDimitry Andric  if (!pfunc.IsAllocated())
41904eeddc0SDimitry Andric    return PythonObject();
420130d950cSDimitry Andric
4210eae32dcSDimitry Andric  PythonObject result =
422*06c3fb27SDimitry Andric      pfunc(SWIGBridge::ToSWIGWrapper(breakpoint_sp), SWIGBridge::ToSWIGWrapper(args_impl), dict);
4230eae32dcSDimitry Andric  // FIXME: At this point we should check that the class we found supports all
4240eae32dcSDimitry Andric  // the methods that we need.
425130d950cSDimitry Andric
4260eae32dcSDimitry Andric  if (result.IsAllocated()) {
427130d950cSDimitry Andric    // Check that __callback__ is defined:
428130d950cSDimitry Andric    auto callback_func = result.ResolveName<PythonCallable>("__callback__");
429130d950cSDimitry Andric    if (callback_func.IsAllocated())
43004eeddc0SDimitry Andric      return result;
431130d950cSDimitry Andric  }
43204eeddc0SDimitry Andric  return PythonObject();
433130d950cSDimitry Andric}
434130d950cSDimitry Andric
435*06c3fb27SDimitry Andricunsigned int lldb_private::python::SWIGBridge::LLDBSwigPythonCallBreakpointResolver(
4360eae32dcSDimitry Andric    void *implementor, const char *method_name,
4370eae32dcSDimitry Andric    lldb_private::SymbolContext *sym_ctx) {
438130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(false);
439130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
440130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>(method_name);
441130d950cSDimitry Andric
442130d950cSDimitry Andric  if (!pfunc.IsAllocated())
443130d950cSDimitry Andric    return 0;
444130d950cSDimitry Andric
445*06c3fb27SDimitry Andric  PythonObject result = sym_ctx ? pfunc(SWIGBridge::ToSWIGWrapper(*sym_ctx)) : pfunc();
446130d950cSDimitry Andric
4470eae32dcSDimitry Andric  if (PyErr_Occurred()) {
448130d950cSDimitry Andric    PyErr_Print();
4495ffd83dbSDimitry Andric    PyErr_Clear();
450130d950cSDimitry Andric    return 0;
451130d950cSDimitry Andric  }
452130d950cSDimitry Andric
453130d950cSDimitry Andric  // The callback will return a bool, but we're need to also return ints
454130d950cSDimitry Andric  // so we're squirrelling the bool through as an int...  And if you return
455130d950cSDimitry Andric  // nothing, we'll continue.
456130d950cSDimitry Andric  if (strcmp(method_name, "__callback__") == 0) {
457130d950cSDimitry Andric    if (result.get() == Py_False)
458130d950cSDimitry Andric      return 0;
459130d950cSDimitry Andric    else
460130d950cSDimitry Andric      return 1;
461130d950cSDimitry Andric  }
462130d950cSDimitry Andric
4635ffd83dbSDimitry Andric  long long ret_val = unwrapOrSetPythonException(As<long long>(result));
464130d950cSDimitry Andric
4655ffd83dbSDimitry Andric  if (PyErr_Occurred()) {
4665ffd83dbSDimitry Andric    PyErr_Print();
4675ffd83dbSDimitry Andric    PyErr_Clear();
4685ffd83dbSDimitry Andric    return 0;
4695ffd83dbSDimitry Andric  }
470130d950cSDimitry Andric
471130d950cSDimitry Andric  return ret_val;
472130d950cSDimitry Andric}
473130d950cSDimitry Andric
474*06c3fb27SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedStopHook(
4750eae32dcSDimitry Andric    lldb::TargetSP target_sp, const char *python_class_name,
4760eae32dcSDimitry Andric    const char *session_dictionary_name, const StructuredDataImpl &args_impl,
4770eae32dcSDimitry Andric    Status &error) {
478e8d8bef9SDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0') {
479e8d8bef9SDimitry Andric    error.SetErrorString("Empty class name.");
48004eeddc0SDimitry Andric    return PythonObject();
481e8d8bef9SDimitry Andric  }
482e8d8bef9SDimitry Andric  if (!session_dictionary_name) {
483e8d8bef9SDimitry Andric    error.SetErrorString("No session dictionary");
48404eeddc0SDimitry Andric    return PythonObject();
485e8d8bef9SDimitry Andric  }
486e8d8bef9SDimitry Andric
487e8d8bef9SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
488e8d8bef9SDimitry Andric
4890eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
490e8d8bef9SDimitry Andric      session_dictionary_name);
4910eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
492e8d8bef9SDimitry Andric      python_class_name, dict);
493e8d8bef9SDimitry Andric
494e8d8bef9SDimitry Andric  if (!pfunc.IsAllocated()) {
495e8d8bef9SDimitry Andric    error.SetErrorStringWithFormat("Could not find class: %s.",
496e8d8bef9SDimitry Andric                                   python_class_name);
49704eeddc0SDimitry Andric    return PythonObject();
498e8d8bef9SDimitry Andric  }
499e8d8bef9SDimitry Andric
5000eae32dcSDimitry Andric  PythonObject result =
501*06c3fb27SDimitry Andric      pfunc(SWIGBridge::ToSWIGWrapper(target_sp), SWIGBridge::ToSWIGWrapper(args_impl), dict);
502e8d8bef9SDimitry Andric
5030eae32dcSDimitry Andric  if (result.IsAllocated()) {
504e8d8bef9SDimitry Andric    // Check that the handle_stop callback is defined:
505e8d8bef9SDimitry Andric    auto callback_func = result.ResolveName<PythonCallable>("handle_stop");
506e8d8bef9SDimitry Andric    if (callback_func.IsAllocated()) {
507e8d8bef9SDimitry Andric      if (auto args_info = callback_func.GetArgInfo()) {
508e8d8bef9SDimitry Andric        size_t num_args = (*args_info).max_positional_args;
509e8d8bef9SDimitry Andric        if (num_args != 2) {
5100eae32dcSDimitry Andric          error.SetErrorStringWithFormat(
5110eae32dcSDimitry Andric              "Wrong number of args for "
512e8d8bef9SDimitry Andric              "handle_stop callback, should be 2 (excluding self), got: %zu",
513e8d8bef9SDimitry Andric              num_args);
51404eeddc0SDimitry Andric          return PythonObject();
515e8d8bef9SDimitry Andric        } else
51604eeddc0SDimitry Andric          return result;
517e8d8bef9SDimitry Andric      } else {
518e8d8bef9SDimitry Andric        error.SetErrorString("Couldn't get num arguments for handle_stop "
519e8d8bef9SDimitry Andric                             "callback.");
52004eeddc0SDimitry Andric        return PythonObject();
521e8d8bef9SDimitry Andric      }
52204eeddc0SDimitry Andric      return result;
5230eae32dcSDimitry Andric    } else {
524e8d8bef9SDimitry Andric      error.SetErrorStringWithFormat("Class \"%s\" is missing the required "
525e8d8bef9SDimitry Andric                                     "handle_stop callback.",
526e8d8bef9SDimitry Andric                                     python_class_name);
527e8d8bef9SDimitry Andric    }
528e8d8bef9SDimitry Andric  }
52904eeddc0SDimitry Andric  return PythonObject();
530e8d8bef9SDimitry Andric}
531e8d8bef9SDimitry Andric
532*06c3fb27SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonStopHookCallHandleStop(
5330eae32dcSDimitry Andric    void *implementor, lldb::ExecutionContextRefSP exc_ctx_sp,
5340eae32dcSDimitry Andric    lldb::StreamSP stream) {
535e8d8bef9SDimitry Andric  // handle_stop will return a bool with the meaning "should_stop"...
536e8d8bef9SDimitry Andric  // If you return nothing we'll assume we are going to stop.
537e8d8bef9SDimitry Andric  // Also any errors should return true, since we should stop on error.
538e8d8bef9SDimitry Andric
539e8d8bef9SDimitry Andric  PyErr_Cleaner py_err_cleaner(false);
540e8d8bef9SDimitry Andric  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
541e8d8bef9SDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("handle_stop");
542e8d8bef9SDimitry Andric
543e8d8bef9SDimitry Andric  if (!pfunc.IsAllocated())
544e8d8bef9SDimitry Andric    return true;
545e8d8bef9SDimitry Andric
5460eae32dcSDimitry Andric  auto *sb_stream = new lldb::SBStream();
5470eae32dcSDimitry Andric  PythonObject sb_stream_arg =
548*06c3fb27SDimitry Andric      SWIGBridge::ToSWIGWrapper(std::unique_ptr<lldb::SBStream>(sb_stream));
5490eae32dcSDimitry Andric  PythonObject result =
550*06c3fb27SDimitry Andric      pfunc(SWIGBridge::ToSWIGWrapper(std::move(exc_ctx_sp)), sb_stream_arg);
551e8d8bef9SDimitry Andric
5520eae32dcSDimitry Andric  if (PyErr_Occurred()) {
553e8d8bef9SDimitry Andric    stream->PutCString("Python error occurred handling stop-hook.");
554e8d8bef9SDimitry Andric    PyErr_Print();
555e8d8bef9SDimitry Andric    PyErr_Clear();
556e8d8bef9SDimitry Andric    return true;
557e8d8bef9SDimitry Andric  }
558e8d8bef9SDimitry Andric
559e8d8bef9SDimitry Andric  // Now add the result to the output stream.  SBStream only
560e8d8bef9SDimitry Andric  // makes an internally help StreamString which I can't interpose, so I
561e8d8bef9SDimitry Andric  // have to copy it over here.
5620eae32dcSDimitry Andric  stream->PutCString(sb_stream->GetData());
563e8d8bef9SDimitry Andric
564e8d8bef9SDimitry Andric  if (result.get() == Py_False)
565e8d8bef9SDimitry Andric    return false;
566e8d8bef9SDimitry Andric  else
567e8d8bef9SDimitry Andric    return true;
568e8d8bef9SDimitry Andric}
569e8d8bef9SDimitry Andric
5700eae32dcSDimitry Andric// wrapper that calls an optional instance member of an object taking no
5710eae32dcSDimitry Andric// arguments
5720eae32dcSDimitry Andricstatic PyObject *LLDBSwigPython_CallOptionalMember(
5730eae32dcSDimitry Andric    PyObject * implementor, char *callee_name,
5740eae32dcSDimitry Andric    PyObject *ret_if_not_found = Py_None, bool *was_found = NULL) {
575130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(false);
576130d950cSDimitry Andric
577130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
578130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>(callee_name);
579130d950cSDimitry Andric
5800eae32dcSDimitry Andric  if (!pfunc.IsAllocated()) {
581130d950cSDimitry Andric    if (was_found)
582130d950cSDimitry Andric      *was_found = false;
583130d950cSDimitry Andric    Py_XINCREF(ret_if_not_found);
584130d950cSDimitry Andric    return ret_if_not_found;
585130d950cSDimitry Andric  }
586130d950cSDimitry Andric
587130d950cSDimitry Andric  if (was_found)
588130d950cSDimitry Andric    *was_found = true;
589130d950cSDimitry Andric
590130d950cSDimitry Andric  PythonObject result = pfunc();
591130d950cSDimitry Andric  return result.release();
592130d950cSDimitry Andric}
593130d950cSDimitry Andric
594*06c3fb27SDimitry Andricsize_t lldb_private::python::SWIGBridge::LLDBSwigPython_CalculateNumChildren(PyObject * implementor,
5950eae32dcSDimitry Andric                                                         uint32_t max) {
596130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
597130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("num_children");
598130d950cSDimitry Andric
599130d950cSDimitry Andric  if (!pfunc.IsAllocated())
600130d950cSDimitry Andric    return 0;
601130d950cSDimitry Andric
602130d950cSDimitry Andric  auto arg_info = pfunc.GetArgInfo();
603130d950cSDimitry Andric  if (!arg_info) {
604130d950cSDimitry Andric    llvm::consumeError(arg_info.takeError());
605130d950cSDimitry Andric    return 0;
606130d950cSDimitry Andric  }
607130d950cSDimitry Andric
6085ffd83dbSDimitry Andric  size_t ret_val;
609130d950cSDimitry Andric  if (arg_info.get().max_positional_args < 1)
6105ffd83dbSDimitry Andric    ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call()));
611130d950cSDimitry Andric  else
6120eae32dcSDimitry Andric    ret_val = unwrapOrSetPythonException(
6130eae32dcSDimitry Andric        As<long long>(pfunc.Call(PythonInteger(max))));
614130d950cSDimitry Andric
6150eae32dcSDimitry Andric  if (PyErr_Occurred()) {
616130d950cSDimitry Andric    PyErr_Print();
617130d950cSDimitry Andric    PyErr_Clear();
6185ffd83dbSDimitry Andric    return 0;
619130d950cSDimitry Andric  }
620130d950cSDimitry Andric
621130d950cSDimitry Andric  if (arg_info.get().max_positional_args < 1)
622130d950cSDimitry Andric    ret_val = std::min(ret_val, static_cast<size_t>(max));
623130d950cSDimitry Andric
624130d950cSDimitry Andric  return ret_val;
625130d950cSDimitry Andric}
626130d950cSDimitry Andric
627*06c3fb27SDimitry AndricPyObject *lldb_private::python::SWIGBridge::LLDBSwigPython_GetChildAtIndex(PyObject * implementor,
6280eae32dcSDimitry Andric                                                       uint32_t idx) {
629130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
630130d950cSDimitry Andric
631130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
632130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index");
633130d950cSDimitry Andric
634130d950cSDimitry Andric  if (!pfunc.IsAllocated())
635130d950cSDimitry Andric    return nullptr;
636130d950cSDimitry Andric
637130d950cSDimitry Andric  PythonObject result = pfunc(PythonInteger(idx));
638130d950cSDimitry Andric
639130d950cSDimitry Andric  if (!result.IsAllocated())
640130d950cSDimitry Andric    return nullptr;
641130d950cSDimitry Andric
642130d950cSDimitry Andric  lldb::SBValue *sbvalue_ptr = nullptr;
6430eae32dcSDimitry Andric  if (SWIG_ConvertPtr(result.get(), (void **)&sbvalue_ptr,
6440eae32dcSDimitry Andric                      SWIGTYPE_p_lldb__SBValue, 0) == -1)
645130d950cSDimitry Andric    return nullptr;
646130d950cSDimitry Andric
647130d950cSDimitry Andric  if (sbvalue_ptr == nullptr)
648130d950cSDimitry Andric    return nullptr;
649130d950cSDimitry Andric
650130d950cSDimitry Andric  return result.release();
651130d950cSDimitry Andric}
652130d950cSDimitry Andric
653*06c3fb27SDimitry Andricint lldb_private::python::SWIGBridge::LLDBSwigPython_GetIndexOfChildWithName(
6540eae32dcSDimitry Andric    PyObject * implementor, const char *child_name) {
655130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
656130d950cSDimitry Andric
657130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
658130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("get_child_index");
659130d950cSDimitry Andric
660130d950cSDimitry Andric  if (!pfunc.IsAllocated())
661130d950cSDimitry Andric    return UINT32_MAX;
662130d950cSDimitry Andric
6635ffd83dbSDimitry Andric  llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name));
664130d950cSDimitry Andric
6650eae32dcSDimitry Andric  long long retval =
6660eae32dcSDimitry Andric      unwrapOrSetPythonException(As<long long>(std::move(result)));
6675ffd83dbSDimitry Andric
6685ffd83dbSDimitry Andric  if (PyErr_Occurred()) {
6695ffd83dbSDimitry Andric    PyErr_Clear(); // FIXME print this? do something else
670130d950cSDimitry Andric    return UINT32_MAX;
6715ffd83dbSDimitry Andric  }
672130d950cSDimitry Andric
673130d950cSDimitry Andric  if (retval >= 0)
674130d950cSDimitry Andric    return (uint32_t)retval;
675130d950cSDimitry Andric
676130d950cSDimitry Andric  return UINT32_MAX;
677130d950cSDimitry Andric}
678130d950cSDimitry Andric
679*06c3fb27SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPython_UpdateSynthProviderInstance(PyObject *
6800eae32dcSDimitry Andric                                                              implementor) {
681130d950cSDimitry Andric  bool ret_val = false;
682130d950cSDimitry Andric
683130d950cSDimitry Andric  static char callee_name[] = "update";
684130d950cSDimitry Andric
6850eae32dcSDimitry Andric  PyObject *py_return =
6860eae32dcSDimitry Andric      LLDBSwigPython_CallOptionalMember(implementor, callee_name);
687130d950cSDimitry Andric
688130d950cSDimitry Andric  if (py_return == Py_True)
689130d950cSDimitry Andric    ret_val = true;
690130d950cSDimitry Andric
691130d950cSDimitry Andric  Py_XDECREF(py_return);
692130d950cSDimitry Andric
693130d950cSDimitry Andric  return ret_val;
694130d950cSDimitry Andric}
695130d950cSDimitry Andric
696*06c3fb27SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPython_MightHaveChildrenSynthProviderInstance(
6970eae32dcSDimitry Andric    PyObject * implementor) {
698130d950cSDimitry Andric  bool ret_val = false;
699130d950cSDimitry Andric
700130d950cSDimitry Andric  static char callee_name[] = "has_children";
701130d950cSDimitry Andric
7020eae32dcSDimitry Andric  PyObject *py_return =
7030eae32dcSDimitry Andric      LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_True);
704130d950cSDimitry Andric
705130d950cSDimitry Andric  if (py_return == Py_True)
706130d950cSDimitry Andric    ret_val = true;
707130d950cSDimitry Andric
708130d950cSDimitry Andric  Py_XDECREF(py_return);
709130d950cSDimitry Andric
710130d950cSDimitry Andric  return ret_val;
711130d950cSDimitry Andric}
712130d950cSDimitry Andric
713*06c3fb27SDimitry AndricPyObject *lldb_private::python::SWIGBridge::LLDBSwigPython_GetValueSynthProviderInstance(
7140eae32dcSDimitry Andric    PyObject * implementor) {
715130d950cSDimitry Andric  PyObject *ret_val = nullptr;
716130d950cSDimitry Andric
717130d950cSDimitry Andric  static char callee_name[] = "get_value";
718130d950cSDimitry Andric
7190eae32dcSDimitry Andric  PyObject *py_return =
7200eae32dcSDimitry Andric      LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_None);
721130d950cSDimitry Andric
722130d950cSDimitry Andric  if (py_return == Py_None || py_return == nullptr)
723130d950cSDimitry Andric    ret_val = nullptr;
724130d950cSDimitry Andric
725130d950cSDimitry Andric  lldb::SBValue *sbvalue_ptr = NULL;
726130d950cSDimitry Andric
7270eae32dcSDimitry Andric  if (SWIG_ConvertPtr(py_return, (void **)&sbvalue_ptr,
7280eae32dcSDimitry Andric                      SWIGTYPE_p_lldb__SBValue, 0) == -1)
729130d950cSDimitry Andric    ret_val = nullptr;
730130d950cSDimitry Andric  else if (sbvalue_ptr == NULL)
731130d950cSDimitry Andric    ret_val = nullptr;
732130d950cSDimitry Andric  else
733130d950cSDimitry Andric    ret_val = py_return;
734130d950cSDimitry Andric
735130d950cSDimitry Andric  Py_XDECREF(py_return);
736130d950cSDimitry Andric  return ret_val;
737130d950cSDimitry Andric}
738130d950cSDimitry Andric
739*06c3fb27SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBData(PyObject * data) {
740fe6060f1SDimitry Andric  lldb::SBData *sb_ptr = nullptr;
741fe6060f1SDimitry Andric
7420eae32dcSDimitry Andric  int valid_cast =
7430eae32dcSDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0);
744fe6060f1SDimitry Andric
745fe6060f1SDimitry Andric  if (valid_cast == -1)
746fe6060f1SDimitry Andric    return NULL;
747fe6060f1SDimitry Andric
748fe6060f1SDimitry Andric  return sb_ptr;
749fe6060f1SDimitry Andric}
750fe6060f1SDimitry Andric
751*06c3fb27SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(PyObject * data) {
752*06c3fb27SDimitry Andric  lldb::SBBreakpoint *sb_ptr = nullptr;
753*06c3fb27SDimitry Andric
754*06c3fb27SDimitry Andric  int valid_cast =
755*06c3fb27SDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBBreakpoint, 0);
756*06c3fb27SDimitry Andric
757*06c3fb27SDimitry Andric  if (valid_cast == -1)
758*06c3fb27SDimitry Andric    return NULL;
759*06c3fb27SDimitry Andric
760*06c3fb27SDimitry Andric  return sb_ptr;
761*06c3fb27SDimitry Andric}
762*06c3fb27SDimitry Andric
763*06c3fb27SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(PyObject * data) {
764*06c3fb27SDimitry Andric  lldb::SBAttachInfo *sb_ptr = nullptr;
765*06c3fb27SDimitry Andric
766*06c3fb27SDimitry Andric  int valid_cast =
767*06c3fb27SDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBAttachInfo, 0);
768*06c3fb27SDimitry Andric
769*06c3fb27SDimitry Andric  if (valid_cast == -1)
770*06c3fb27SDimitry Andric    return NULL;
771*06c3fb27SDimitry Andric
772*06c3fb27SDimitry Andric  return sb_ptr;
773*06c3fb27SDimitry Andric}
774*06c3fb27SDimitry Andric
775*06c3fb27SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(PyObject * data) {
776*06c3fb27SDimitry Andric  lldb::SBLaunchInfo *sb_ptr = nullptr;
777*06c3fb27SDimitry Andric
778*06c3fb27SDimitry Andric  int valid_cast =
779*06c3fb27SDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBLaunchInfo, 0);
780*06c3fb27SDimitry Andric
781*06c3fb27SDimitry Andric  if (valid_cast == -1)
782*06c3fb27SDimitry Andric    return NULL;
783*06c3fb27SDimitry Andric
784*06c3fb27SDimitry Andric  return sb_ptr;
785*06c3fb27SDimitry Andric}
786*06c3fb27SDimitry Andric
787*06c3fb27SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBError(PyObject * data) {
788fe6060f1SDimitry Andric  lldb::SBError *sb_ptr = nullptr;
789fe6060f1SDimitry Andric
7900eae32dcSDimitry Andric  int valid_cast =
7910eae32dcSDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0);
792fe6060f1SDimitry Andric
793fe6060f1SDimitry Andric  if (valid_cast == -1)
794fe6060f1SDimitry Andric    return NULL;
795fe6060f1SDimitry Andric
796fe6060f1SDimitry Andric  return sb_ptr;
797fe6060f1SDimitry Andric}
798fe6060f1SDimitry Andric
799*06c3fb27SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBValue(PyObject * data) {
800130d950cSDimitry Andric  lldb::SBValue *sb_ptr = NULL;
801130d950cSDimitry Andric
8020eae32dcSDimitry Andric  int valid_cast =
8030eae32dcSDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
804130d950cSDimitry Andric
805130d950cSDimitry Andric  if (valid_cast == -1)
806130d950cSDimitry Andric    return NULL;
807130d950cSDimitry Andric
808130d950cSDimitry Andric  return sb_ptr;
809130d950cSDimitry Andric}
810130d950cSDimitry Andric
811*06c3fb27SDimitry Andricvoid *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *
8120eae32dcSDimitry Andric                                                                    data) {
813349cc55cSDimitry Andric  lldb::SBMemoryRegionInfo *sb_ptr = NULL;
814349cc55cSDimitry Andric
8150eae32dcSDimitry Andric  int valid_cast = SWIG_ConvertPtr(data, (void **)&sb_ptr,
8160eae32dcSDimitry Andric                                   SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0);
817349cc55cSDimitry Andric
818349cc55cSDimitry Andric  if (valid_cast == -1)
819349cc55cSDimitry Andric    return NULL;
820349cc55cSDimitry Andric
821349cc55cSDimitry Andric  return sb_ptr;
822349cc55cSDimitry Andric}
823349cc55cSDimitry Andric
824*06c3fb27SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonCallCommand(
8250eae32dcSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
8260eae32dcSDimitry Andric    lldb::DebuggerSP debugger, const char *args,
827130d950cSDimitry Andric    lldb_private::CommandReturnObject &cmd_retobj,
8280eae32dcSDimitry Andric    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
829130d950cSDimitry Andric
830130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
8310eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
8320eae32dcSDimitry Andric      session_dictionary_name);
8330eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
8340eae32dcSDimitry Andric      python_function_name, dict);
835130d950cSDimitry Andric
836130d950cSDimitry Andric  if (!pfunc.IsAllocated())
837130d950cSDimitry Andric    return false;
838130d950cSDimitry Andric
839130d950cSDimitry Andric  auto argc = pfunc.GetArgInfo();
840130d950cSDimitry Andric  if (!argc) {
841130d950cSDimitry Andric    llvm::consumeError(argc.takeError());
842130d950cSDimitry Andric    return false;
843130d950cSDimitry Andric  }
844*06c3fb27SDimitry Andric  PythonObject debugger_arg = SWIGBridge::ToSWIGWrapper(std::move(debugger));
845*06c3fb27SDimitry Andric  auto cmd_retobj_arg = SWIGBridge::ToSWIGWrapper(cmd_retobj);
846130d950cSDimitry Andric
847130d950cSDimitry Andric  if (argc.get().max_positional_args < 5u)
84804eeddc0SDimitry Andric    pfunc(debugger_arg, PythonString(args), cmd_retobj_arg.obj(), dict);
849130d950cSDimitry Andric  else
8500eae32dcSDimitry Andric    pfunc(debugger_arg, PythonString(args),
851*06c3fb27SDimitry Andric          SWIGBridge::ToSWIGWrapper(std::move(exe_ctx_ref_sp)), cmd_retobj_arg.obj(), dict);
852130d950cSDimitry Andric
853130d950cSDimitry Andric  return true;
854130d950cSDimitry Andric}
855130d950cSDimitry Andric
856*06c3fb27SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonCallCommandObject(
8570eae32dcSDimitry Andric    PyObject *implementor, lldb::DebuggerSP debugger, const char *args,
858130d950cSDimitry Andric    lldb_private::CommandReturnObject &cmd_retobj,
8590eae32dcSDimitry Andric    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
860130d950cSDimitry Andric
861130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
862130d950cSDimitry Andric
863130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
864130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("__call__");
865130d950cSDimitry Andric
866130d950cSDimitry Andric  if (!pfunc.IsAllocated())
867130d950cSDimitry Andric    return false;
868130d950cSDimitry Andric
869*06c3fb27SDimitry Andric  auto cmd_retobj_arg = SWIGBridge::ToSWIGWrapper(cmd_retobj);
870130d950cSDimitry Andric
871*06c3fb27SDimitry Andric  pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger)), PythonString(args),
872*06c3fb27SDimitry Andric        SWIGBridge::ToSWIGWrapper(exe_ctx_ref_sp), cmd_retobj_arg.obj());
873130d950cSDimitry Andric
874130d950cSDimitry Andric  return true;
875130d950cSDimitry Andric}
876130d950cSDimitry Andric
877*06c3fb27SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSWIGPythonCreateOSPlugin(
8780eae32dcSDimitry Andric    const char *python_class_name, const char *session_dictionary_name,
8790eae32dcSDimitry Andric    const lldb::ProcessSP &process_sp) {
8800eae32dcSDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
8810eae32dcSDimitry Andric      !session_dictionary_name)
88204eeddc0SDimitry Andric    return PythonObject();
883130d950cSDimitry Andric
884130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
885130d950cSDimitry Andric
8860eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
8870eae32dcSDimitry Andric      session_dictionary_name);
8880eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
8890eae32dcSDimitry Andric      python_class_name, dict);
890130d950cSDimitry Andric
891130d950cSDimitry Andric  if (!pfunc.IsAllocated())
89204eeddc0SDimitry Andric    return PythonObject();
893130d950cSDimitry Andric
894*06c3fb27SDimitry Andric  return pfunc(SWIGBridge::ToSWIGWrapper(process_sp));
895130d950cSDimitry Andric}
896130d950cSDimitry Andric
897*06c3fb27SDimitry AndricPythonObject lldb_private::python::SWIGBridge::LLDBSWIGPython_CreateFrameRecognizer(
8980eae32dcSDimitry Andric    const char *python_class_name, const char *session_dictionary_name) {
8990eae32dcSDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
9000eae32dcSDimitry Andric      !session_dictionary_name)
90104eeddc0SDimitry Andric    return PythonObject();
902130d950cSDimitry Andric
903130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
904130d950cSDimitry Andric
9050eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9060eae32dcSDimitry Andric      session_dictionary_name);
9070eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9080eae32dcSDimitry Andric      python_class_name, dict);
909130d950cSDimitry Andric
910130d950cSDimitry Andric  if (!pfunc.IsAllocated())
91104eeddc0SDimitry Andric    return PythonObject();
912130d950cSDimitry Andric
91304eeddc0SDimitry Andric  return pfunc();
914130d950cSDimitry Andric}
915130d950cSDimitry Andric
916*06c3fb27SDimitry AndricPyObject *lldb_private::python::SWIGBridge::LLDBSwigPython_GetRecognizedArguments(
9170eae32dcSDimitry Andric    PyObject * implementor, const lldb::StackFrameSP &frame_sp) {
918130d950cSDimitry Andric  static char callee_name[] = "get_recognized_arguments";
919130d950cSDimitry Andric
920*06c3fb27SDimitry Andric  PythonObject arg = SWIGBridge::ToSWIGWrapper(frame_sp);
921130d950cSDimitry Andric
922130d950cSDimitry Andric  PythonString str(callee_name);
9230eae32dcSDimitry Andric  PyObject *result =
9240eae32dcSDimitry Andric      PyObject_CallMethodObjArgs(implementor, str.get(), arg.get(), NULL);
925130d950cSDimitry Andric  return result;
926130d950cSDimitry Andric}
927130d950cSDimitry Andric
928*06c3fb27SDimitry Andricvoid *lldb_private::python::SWIGBridge::LLDBSWIGPython_GetDynamicSetting(
9290eae32dcSDimitry Andric    void *module, const char *setting, const lldb::TargetSP &target_sp) {
930130d950cSDimitry Andric  if (!module || !setting)
931130d950cSDimitry Andric    Py_RETURN_NONE;
932130d950cSDimitry Andric
933130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
934130d950cSDimitry Andric  PythonObject py_module(PyRefType::Borrowed, (PyObject *)module);
935130d950cSDimitry Andric  auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting");
936130d950cSDimitry Andric
937130d950cSDimitry Andric  if (!pfunc.IsAllocated())
938130d950cSDimitry Andric    Py_RETURN_NONE;
939130d950cSDimitry Andric
940*06c3fb27SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(target_sp), PythonString(setting));
941130d950cSDimitry Andric
942130d950cSDimitry Andric  return result.release();
943130d950cSDimitry Andric}
944130d950cSDimitry Andric
945*06c3fb27SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordProcess(
9464824e7fdSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
9474824e7fdSDimitry Andric    const lldb::ProcessSP &process, std::string &output) {
948130d950cSDimitry Andric
9490eae32dcSDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9500eae32dcSDimitry Andric      !session_dictionary_name)
951130d950cSDimitry Andric    return false;
952130d950cSDimitry Andric
953130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
954130d950cSDimitry Andric
9550eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9560eae32dcSDimitry Andric      session_dictionary_name);
9570eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9580eae32dcSDimitry Andric      python_function_name, dict);
959130d950cSDimitry Andric
960130d950cSDimitry Andric  if (!pfunc.IsAllocated())
961130d950cSDimitry Andric    return false;
962130d950cSDimitry Andric
963*06c3fb27SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(process), dict);
964130d950cSDimitry Andric
965130d950cSDimitry Andric  output = result.Str().GetString().str();
966130d950cSDimitry Andric
967130d950cSDimitry Andric  return true;
968130d950cSDimitry Andric}
969130d950cSDimitry Andric
970*06c3fb27SDimitry Andricstd::optional<std::string> lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordThread(
9710eae32dcSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
9720eae32dcSDimitry Andric    lldb::ThreadSP thread) {
9730eae32dcSDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9740eae32dcSDimitry Andric      !session_dictionary_name)
975bdd1243dSDimitry Andric    return std::nullopt;
976130d950cSDimitry Andric
977130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
978130d950cSDimitry Andric
9790eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9800eae32dcSDimitry Andric      session_dictionary_name);
9810eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9820eae32dcSDimitry Andric      python_function_name, dict);
983130d950cSDimitry Andric
984130d950cSDimitry Andric  if (!pfunc.IsAllocated())
985bdd1243dSDimitry Andric    return std::nullopt;
986130d950cSDimitry Andric
987*06c3fb27SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(std::move(thread)), dict);
988130d950cSDimitry Andric
9890eae32dcSDimitry Andric  return result.Str().GetString().str();
990130d950cSDimitry Andric}
991130d950cSDimitry Andric
992*06c3fb27SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordTarget(
9934824e7fdSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
9944824e7fdSDimitry Andric    const lldb::TargetSP &target, std::string &output) {
995130d950cSDimitry Andric
9960eae32dcSDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9970eae32dcSDimitry Andric      !session_dictionary_name)
998130d950cSDimitry Andric    return false;
999130d950cSDimitry Andric
1000130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
1001130d950cSDimitry Andric
10020eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10030eae32dcSDimitry Andric      session_dictionary_name);
10040eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10050eae32dcSDimitry Andric      python_function_name, dict);
1006130d950cSDimitry Andric
1007130d950cSDimitry Andric  if (!pfunc.IsAllocated())
1008130d950cSDimitry Andric    return false;
1009130d950cSDimitry Andric
1010*06c3fb27SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(target), dict);
1011130d950cSDimitry Andric
1012130d950cSDimitry Andric  output = result.Str().GetString().str();
1013130d950cSDimitry Andric
1014130d950cSDimitry Andric  return true;
1015130d950cSDimitry Andric}
1016130d950cSDimitry Andric
1017*06c3fb27SDimitry Andricstd::optional<std::string> lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordFrame(
10180eae32dcSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
10190eae32dcSDimitry Andric    lldb::StackFrameSP frame) {
10200eae32dcSDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
10210eae32dcSDimitry Andric      !session_dictionary_name)
1022bdd1243dSDimitry Andric    return std::nullopt;
1023130d950cSDimitry Andric
1024130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
1025130d950cSDimitry Andric
10260eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10270eae32dcSDimitry Andric      session_dictionary_name);
10280eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10290eae32dcSDimitry Andric      python_function_name, dict);
1030130d950cSDimitry Andric
1031130d950cSDimitry Andric  if (!pfunc.IsAllocated())
1032bdd1243dSDimitry Andric    return std::nullopt;
1033130d950cSDimitry Andric
1034*06c3fb27SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(std::move(frame)), dict);
1035130d950cSDimitry Andric
10360eae32dcSDimitry Andric  return result.Str().GetString().str();
1037130d950cSDimitry Andric}
1038130d950cSDimitry Andric
1039*06c3fb27SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSWIGPythonRunScriptKeywordValue(
10404824e7fdSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
10414824e7fdSDimitry Andric    const lldb::ValueObjectSP &value, std::string &output) {
1042130d950cSDimitry Andric
10430eae32dcSDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
10440eae32dcSDimitry Andric      !session_dictionary_name)
1045130d950cSDimitry Andric    return false;
1046130d950cSDimitry Andric
1047130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
1048130d950cSDimitry Andric
10490eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10500eae32dcSDimitry Andric      session_dictionary_name);
10510eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10520eae32dcSDimitry Andric      python_function_name, dict);
1053130d950cSDimitry Andric
1054130d950cSDimitry Andric  if (!pfunc.IsAllocated())
1055130d950cSDimitry Andric    return false;
1056130d950cSDimitry Andric
1057*06c3fb27SDimitry Andric  auto result = pfunc(SWIGBridge::ToSWIGWrapper(value), dict);
1058130d950cSDimitry Andric
1059130d950cSDimitry Andric  output = result.Str().GetString().str();
1060130d950cSDimitry Andric
1061130d950cSDimitry Andric  return true;
1062130d950cSDimitry Andric}
1063130d950cSDimitry Andric
1064*06c3fb27SDimitry Andricbool lldb_private::python::SWIGBridge::LLDBSwigPythonCallModuleInit(
10650eae32dcSDimitry Andric    const char *python_module_name, const char *session_dictionary_name,
10660eae32dcSDimitry Andric    lldb::DebuggerSP debugger) {
1067130d950cSDimitry Andric  std::string python_function_name_string = python_module_name;
1068130d950cSDimitry Andric  python_function_name_string += ".__lldb_init_module";
1069130d950cSDimitry Andric  const char *python_function_name = python_function_name_string.c_str();
1070130d950cSDimitry Andric
1071130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
1072130d950cSDimitry Andric
10730eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10740eae32dcSDimitry Andric      session_dictionary_name);
10750eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10760eae32dcSDimitry Andric      python_function_name, dict);
1077130d950cSDimitry Andric
1078130d950cSDimitry Andric  // This method is optional and need not exist.  So if we don't find it,
1079130d950cSDimitry Andric  // it's actually a success, not a failure.
1080130d950cSDimitry Andric  if (!pfunc.IsAllocated())
1081130d950cSDimitry Andric    return true;
1082130d950cSDimitry Andric
1083*06c3fb27SDimitry Andric  pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger)), dict);
1084130d950cSDimitry Andric
1085130d950cSDimitry Andric  return true;
1086130d950cSDimitry Andric}
1087130d950cSDimitry Andric
1088*06c3fb27SDimitry Andriclldb::ValueObjectSP lldb_private::python::SWIGBridge::LLDBSWIGPython_GetValueObjectSPFromSBValue(
10890eae32dcSDimitry Andric    void *data) {
1090130d950cSDimitry Andric  lldb::ValueObjectSP valobj_sp;
10910eae32dcSDimitry Andric  if (data) {
1092130d950cSDimitry Andric    lldb::SBValue *sb_ptr = (lldb::SBValue *)data;
1093130d950cSDimitry Andric    valobj_sp = sb_ptr->GetSP();
1094130d950cSDimitry Andric  }
1095130d950cSDimitry Andric  return valobj_sp;
1096130d950cSDimitry Andric}
1097130d950cSDimitry Andric
1098130d950cSDimitry Andric// For the LogOutputCallback functions
10990eae32dcSDimitry Andricstatic void LLDBSwigPythonCallPythonLogOutputCallback(const char *str,
11000eae32dcSDimitry Andric                                                      void *baton) {
1101130d950cSDimitry Andric  if (baton != Py_None) {
1102130d950cSDimitry Andric    SWIG_PYTHON_THREAD_BEGIN_BLOCK;
11030eae32dcSDimitry Andric    PyObject *result = PyObject_CallFunction(
11040eae32dcSDimitry Andric        reinterpret_cast<PyObject *>(baton), const_cast<char *>("s"), str);
1105130d950cSDimitry Andric    Py_XDECREF(result);
1106130d950cSDimitry Andric    SWIG_PYTHON_THREAD_END_BLOCK;
1107130d950cSDimitry Andric  }
1108130d950cSDimitry Andric}
1109*06c3fb27SDimitry Andric
1110*06c3fb27SDimitry Andric// For DebuggerTerminateCallback functions
1111*06c3fb27SDimitry Andricstatic void LLDBSwigPythonCallPythonSBDebuggerTerminateCallback(lldb::user_id_t debugger_id,
1112*06c3fb27SDimitry Andric                                                      void *baton) {
1113*06c3fb27SDimitry Andric  if (baton != Py_None) {
1114*06c3fb27SDimitry Andric    SWIG_PYTHON_THREAD_BEGIN_BLOCK;
1115*06c3fb27SDimitry Andric    PyObject *result = PyObject_CallFunction(
1116*06c3fb27SDimitry Andric        reinterpret_cast<PyObject *>(baton), const_cast<char *>("l"), debugger_id);
1117*06c3fb27SDimitry Andric    Py_XDECREF(result);
1118*06c3fb27SDimitry Andric    SWIG_PYTHON_THREAD_END_BLOCK;
1119*06c3fb27SDimitry Andric  }
1120*06c3fb27SDimitry Andric}
1121*06c3fb27SDimitry Andric
1122*06c3fb27SDimitry Andricstatic SBError LLDBSwigPythonCallLocateModuleCallback(
1123*06c3fb27SDimitry Andric    void *callback_baton, const SBModuleSpec &module_spec_sb,
1124*06c3fb27SDimitry Andric    SBFileSpec &module_file_spec_sb, SBFileSpec &symbol_file_spec_sb) {
1125*06c3fb27SDimitry Andric  SWIG_Python_Thread_Block swig_thread_block;
1126*06c3fb27SDimitry Andric
1127*06c3fb27SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
1128*06c3fb27SDimitry Andric  PythonObject module_spec_arg = SWIGBridge::ToSWIGWrapper(
1129*06c3fb27SDimitry Andric      std::make_unique<SBModuleSpec>(module_spec_sb));
1130*06c3fb27SDimitry Andric  PythonObject module_file_spec_arg = SWIGBridge::ToSWIGWrapper(
1131*06c3fb27SDimitry Andric      std::make_unique<SBFileSpec>(module_file_spec_sb));
1132*06c3fb27SDimitry Andric  PythonObject symbol_file_spec_arg = SWIGBridge::ToSWIGWrapper(
1133*06c3fb27SDimitry Andric      std::make_unique<SBFileSpec>(symbol_file_spec_sb));
1134*06c3fb27SDimitry Andric
1135*06c3fb27SDimitry Andric  PythonCallable callable =
1136*06c3fb27SDimitry Andric      Retain<PythonCallable>(reinterpret_cast<PyObject *>(callback_baton));
1137*06c3fb27SDimitry Andric  if (!callable.IsValid()) {
1138*06c3fb27SDimitry Andric    return SBError("The callback callable is not valid.");
1139*06c3fb27SDimitry Andric  }
1140*06c3fb27SDimitry Andric
1141*06c3fb27SDimitry Andric  PythonObject result = callable(module_spec_arg, module_file_spec_arg,
1142*06c3fb27SDimitry Andric                                 symbol_file_spec_arg);
1143*06c3fb27SDimitry Andric
1144*06c3fb27SDimitry Andric  if (!result.IsAllocated())
1145*06c3fb27SDimitry Andric    return SBError("No result.");
1146*06c3fb27SDimitry Andric  lldb::SBError *sb_error_ptr = nullptr;
1147*06c3fb27SDimitry Andric  if (SWIG_ConvertPtr(result.get(), (void **)&sb_error_ptr,
1148*06c3fb27SDimitry Andric                      SWIGTYPE_p_lldb__SBError, 0) == -1) {
1149*06c3fb27SDimitry Andric    return SBError("Result is not SBError.");
1150*06c3fb27SDimitry Andric  }
1151*06c3fb27SDimitry Andric
1152*06c3fb27SDimitry Andric  if (sb_error_ptr->Success()) {
1153*06c3fb27SDimitry Andric    lldb::SBFileSpec *sb_module_file_spec_ptr = nullptr;
1154*06c3fb27SDimitry Andric    if (SWIG_ConvertPtr(module_file_spec_arg.get(),
1155*06c3fb27SDimitry Andric                        (void **)&sb_module_file_spec_ptr,
1156*06c3fb27SDimitry Andric                        SWIGTYPE_p_lldb__SBFileSpec, 0) == -1)
1157*06c3fb27SDimitry Andric      return SBError("module_file_spec is not SBFileSpec.");
1158*06c3fb27SDimitry Andric
1159*06c3fb27SDimitry Andric    lldb::SBFileSpec *sb_symbol_file_spec_ptr = nullptr;
1160*06c3fb27SDimitry Andric    if (SWIG_ConvertPtr(symbol_file_spec_arg.get(),
1161*06c3fb27SDimitry Andric                        (void **)&sb_symbol_file_spec_ptr,
1162*06c3fb27SDimitry Andric                        SWIGTYPE_p_lldb__SBFileSpec, 0) == -1)
1163*06c3fb27SDimitry Andric      return SBError("symbol_file_spec is not SBFileSpec.");
1164*06c3fb27SDimitry Andric
1165*06c3fb27SDimitry Andric    module_file_spec_sb = *sb_module_file_spec_ptr;
1166*06c3fb27SDimitry Andric    symbol_file_spec_sb = *sb_symbol_file_spec_ptr;
1167*06c3fb27SDimitry Andric  }
1168*06c3fb27SDimitry Andric
1169*06c3fb27SDimitry Andric  return *sb_error_ptr;
1170*06c3fb27SDimitry Andric}
1171130d950cSDimitry Andric%}
1172