xref: /freebsd-src/contrib/llvm-project/lldb/bindings/python/python-wrapper.swig (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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
190eae32dcSDimitry Andricllvm::Expected<bool> lldb_private::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
400eae32dcSDimitry Andric  PythonObject frame_arg = ToSWIGWrapper(frame_sp);
410eae32dcSDimitry Andric  PythonObject bp_loc_arg = 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)
460eae32dcSDimitry Andric          : pfunc.Call(frame_arg, bp_loc_arg, 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
680eae32dcSDimitry Andricbool lldb_private::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 =
850eae32dcSDimitry Andric      pfunc(ToSWIGWrapper(frame_sp), 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
93*bdd1243dSDimitry Andric// This function is called by
94*bdd1243dSDimitry Andric// ScriptInterpreterPython::FormatterMatchingCallbackFunction and it's used when
95*bdd1243dSDimitry Andric// a data formatter provides the name of a callback to inspect a candidate type
96*bdd1243dSDimitry Andric// before considering a match.
97*bdd1243dSDimitry Andricbool lldb_private::LLDBSwigPythonFormatterCallbackFunction(
98*bdd1243dSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
99*bdd1243dSDimitry Andric    lldb::TypeImplSP type_impl_sp) {
100*bdd1243dSDimitry Andric
101*bdd1243dSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
102*bdd1243dSDimitry Andric
103*bdd1243dSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
104*bdd1243dSDimitry Andric      session_dictionary_name);
105*bdd1243dSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
106*bdd1243dSDimitry Andric      python_function_name, dict);
107*bdd1243dSDimitry Andric
108*bdd1243dSDimitry Andric  if (!pfunc.IsAllocated())
109*bdd1243dSDimitry Andric    return false;
110*bdd1243dSDimitry Andric
111*bdd1243dSDimitry Andric  PythonObject result =
112*bdd1243dSDimitry Andric      pfunc(ToSWIGWrapper(type_impl_sp), dict);
113*bdd1243dSDimitry Andric
114*bdd1243dSDimitry Andric  // Only if everything goes okay and the function returns True we'll consider
115*bdd1243dSDimitry Andric  // it a match.
116*bdd1243dSDimitry Andric  return result.get() == Py_True;
117*bdd1243dSDimitry Andric}
118*bdd1243dSDimitry Andric
1190eae32dcSDimitry Andricbool lldb_private::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
1694824e7fdSDimitry Andric  PythonObject value_arg = ToSWIGWrapper(valobj_sp);
170130d950cSDimitry Andric
171130d950cSDimitry Andric  if (argc.get().max_positional_args < 3)
172130d950cSDimitry Andric    result = pfunc(value_arg, dict);
173130d950cSDimitry Andric  else
1740eae32dcSDimitry Andric    result = pfunc(value_arg, dict, ToSWIGWrapper(*options_sp));
175130d950cSDimitry Andric
176130d950cSDimitry Andric  retval = result.Str().GetString().str();
177130d950cSDimitry Andric
178130d950cSDimitry Andric  return true;
179130d950cSDimitry Andric}
180130d950cSDimitry Andric
18104eeddc0SDimitry AndricPythonObject lldb_private::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
1984824e7fdSDimitry Andric  auto sb_value = std::make_unique<lldb::SBValue>(valobj_sp);
199130d950cSDimitry Andric  sb_value->SetPreferSyntheticValue(false);
200130d950cSDimitry Andric
2014824e7fdSDimitry Andric  PythonObject val_arg = 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
21304eeddc0SDimitry AndricPythonObject lldb_private::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
22904eeddc0SDimitry Andric  return pfunc(ToSWIGWrapper(std::move(debugger_sp)), dict);
230130d950cSDimitry Andric}
231130d950cSDimitry Andric
232*bdd1243dSDimitry AndricPythonObject lldb_private::LLDBSwigPythonCreateScriptedObject(
2330eae32dcSDimitry Andric    const char *python_class_name, const char *session_dictionary_name,
234*bdd1243dSDimitry 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*bdd1243dSDimitry Andric      result = pfunc(ToSWIGWrapper(exe_ctx_sp), 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
27504eeddc0SDimitry AndricPythonObject lldb_private::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
2964824e7fdSDimitry Andric  PythonObject tp_arg = 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 = {};
3100eae32dcSDimitry Andric  auto args_sb = std::make_unique<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) {
3190eae32dcSDimitry Andric    result = pfunc(tp_arg, 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
3320eae32dcSDimitry Andricbool lldb_private::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) {
34604eeddc0SDimitry Andric    ScopedPythonObject<SBEvent> event_arg = 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
37004eeddc0SDimitry AndricPythonObject lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
3714824e7fdSDimitry Andric    const char *python_class_name, const char *session_dictionary_name,
3720eae32dcSDimitry Andric    const StructuredDataImpl &args_impl,
3734824e7fdSDimitry Andric    const lldb::BreakpointSP &breakpoint_sp) {
3744824e7fdSDimitry Andric
3750eae32dcSDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
3760eae32dcSDimitry Andric      !session_dictionary_name)
37704eeddc0SDimitry Andric    return PythonObject();
378130d950cSDimitry Andric
379130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
380130d950cSDimitry Andric
3810eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
3820eae32dcSDimitry Andric      session_dictionary_name);
3830eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
3840eae32dcSDimitry Andric      python_class_name, dict);
385130d950cSDimitry Andric
386130d950cSDimitry Andric  if (!pfunc.IsAllocated())
38704eeddc0SDimitry Andric    return PythonObject();
388130d950cSDimitry Andric
3890eae32dcSDimitry Andric  PythonObject result =
3900eae32dcSDimitry Andric      pfunc(ToSWIGWrapper(breakpoint_sp), ToSWIGWrapper(args_impl), dict);
3910eae32dcSDimitry Andric  // FIXME: At this point we should check that the class we found supports all
3920eae32dcSDimitry Andric  // the methods that we need.
393130d950cSDimitry Andric
3940eae32dcSDimitry Andric  if (result.IsAllocated()) {
395130d950cSDimitry Andric    // Check that __callback__ is defined:
396130d950cSDimitry Andric    auto callback_func = result.ResolveName<PythonCallable>("__callback__");
397130d950cSDimitry Andric    if (callback_func.IsAllocated())
39804eeddc0SDimitry Andric      return result;
399130d950cSDimitry Andric  }
40004eeddc0SDimitry Andric  return PythonObject();
401130d950cSDimitry Andric}
402130d950cSDimitry Andric
4030eae32dcSDimitry Andricunsigned int lldb_private::LLDBSwigPythonCallBreakpointResolver(
4040eae32dcSDimitry Andric    void *implementor, const char *method_name,
4050eae32dcSDimitry Andric    lldb_private::SymbolContext *sym_ctx) {
406130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(false);
407130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
408130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>(method_name);
409130d950cSDimitry Andric
410130d950cSDimitry Andric  if (!pfunc.IsAllocated())
411130d950cSDimitry Andric    return 0;
412130d950cSDimitry Andric
4130eae32dcSDimitry Andric  PythonObject result = sym_ctx ? pfunc(ToSWIGWrapper(*sym_ctx)) : pfunc();
414130d950cSDimitry Andric
4150eae32dcSDimitry Andric  if (PyErr_Occurred()) {
416130d950cSDimitry Andric    PyErr_Print();
4175ffd83dbSDimitry Andric    PyErr_Clear();
418130d950cSDimitry Andric    return 0;
419130d950cSDimitry Andric  }
420130d950cSDimitry Andric
421130d950cSDimitry Andric  // The callback will return a bool, but we're need to also return ints
422130d950cSDimitry Andric  // so we're squirrelling the bool through as an int...  And if you return
423130d950cSDimitry Andric  // nothing, we'll continue.
424130d950cSDimitry Andric  if (strcmp(method_name, "__callback__") == 0) {
425130d950cSDimitry Andric    if (result.get() == Py_False)
426130d950cSDimitry Andric      return 0;
427130d950cSDimitry Andric    else
428130d950cSDimitry Andric      return 1;
429130d950cSDimitry Andric  }
430130d950cSDimitry Andric
4315ffd83dbSDimitry Andric  long long ret_val = unwrapOrSetPythonException(As<long long>(result));
432130d950cSDimitry Andric
4335ffd83dbSDimitry Andric  if (PyErr_Occurred()) {
4345ffd83dbSDimitry Andric    PyErr_Print();
4355ffd83dbSDimitry Andric    PyErr_Clear();
4365ffd83dbSDimitry Andric    return 0;
4375ffd83dbSDimitry Andric  }
438130d950cSDimitry Andric
439130d950cSDimitry Andric  return ret_val;
440130d950cSDimitry Andric}
441130d950cSDimitry Andric
44204eeddc0SDimitry AndricPythonObject lldb_private::LLDBSwigPythonCreateScriptedStopHook(
4430eae32dcSDimitry Andric    lldb::TargetSP target_sp, const char *python_class_name,
4440eae32dcSDimitry Andric    const char *session_dictionary_name, const StructuredDataImpl &args_impl,
4450eae32dcSDimitry Andric    Status &error) {
446e8d8bef9SDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0') {
447e8d8bef9SDimitry Andric    error.SetErrorString("Empty class name.");
44804eeddc0SDimitry Andric    return PythonObject();
449e8d8bef9SDimitry Andric  }
450e8d8bef9SDimitry Andric  if (!session_dictionary_name) {
451e8d8bef9SDimitry Andric    error.SetErrorString("No session dictionary");
45204eeddc0SDimitry Andric    return PythonObject();
453e8d8bef9SDimitry Andric  }
454e8d8bef9SDimitry Andric
455e8d8bef9SDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
456e8d8bef9SDimitry Andric
4570eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
458e8d8bef9SDimitry Andric      session_dictionary_name);
4590eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
460e8d8bef9SDimitry Andric      python_class_name, dict);
461e8d8bef9SDimitry Andric
462e8d8bef9SDimitry Andric  if (!pfunc.IsAllocated()) {
463e8d8bef9SDimitry Andric    error.SetErrorStringWithFormat("Could not find class: %s.",
464e8d8bef9SDimitry Andric                                   python_class_name);
46504eeddc0SDimitry Andric    return PythonObject();
466e8d8bef9SDimitry Andric  }
467e8d8bef9SDimitry Andric
4680eae32dcSDimitry Andric  PythonObject result =
4690eae32dcSDimitry Andric      pfunc(ToSWIGWrapper(target_sp), ToSWIGWrapper(args_impl), dict);
470e8d8bef9SDimitry Andric
4710eae32dcSDimitry Andric  if (result.IsAllocated()) {
472e8d8bef9SDimitry Andric    // Check that the handle_stop callback is defined:
473e8d8bef9SDimitry Andric    auto callback_func = result.ResolveName<PythonCallable>("handle_stop");
474e8d8bef9SDimitry Andric    if (callback_func.IsAllocated()) {
475e8d8bef9SDimitry Andric      if (auto args_info = callback_func.GetArgInfo()) {
476e8d8bef9SDimitry Andric        size_t num_args = (*args_info).max_positional_args;
477e8d8bef9SDimitry Andric        if (num_args != 2) {
4780eae32dcSDimitry Andric          error.SetErrorStringWithFormat(
4790eae32dcSDimitry Andric              "Wrong number of args for "
480e8d8bef9SDimitry Andric              "handle_stop callback, should be 2 (excluding self), got: %zu",
481e8d8bef9SDimitry Andric              num_args);
48204eeddc0SDimitry Andric          return PythonObject();
483e8d8bef9SDimitry Andric        } else
48404eeddc0SDimitry Andric          return result;
485e8d8bef9SDimitry Andric      } else {
486e8d8bef9SDimitry Andric        error.SetErrorString("Couldn't get num arguments for handle_stop "
487e8d8bef9SDimitry Andric                             "callback.");
48804eeddc0SDimitry Andric        return PythonObject();
489e8d8bef9SDimitry Andric      }
49004eeddc0SDimitry Andric      return result;
4910eae32dcSDimitry Andric    } else {
492e8d8bef9SDimitry Andric      error.SetErrorStringWithFormat("Class \"%s\" is missing the required "
493e8d8bef9SDimitry Andric                                     "handle_stop callback.",
494e8d8bef9SDimitry Andric                                     python_class_name);
495e8d8bef9SDimitry Andric    }
496e8d8bef9SDimitry Andric  }
49704eeddc0SDimitry Andric  return PythonObject();
498e8d8bef9SDimitry Andric}
499e8d8bef9SDimitry Andric
5000eae32dcSDimitry Andricbool lldb_private::LLDBSwigPythonStopHookCallHandleStop(
5010eae32dcSDimitry Andric    void *implementor, lldb::ExecutionContextRefSP exc_ctx_sp,
5020eae32dcSDimitry Andric    lldb::StreamSP stream) {
503e8d8bef9SDimitry Andric  // handle_stop will return a bool with the meaning "should_stop"...
504e8d8bef9SDimitry Andric  // If you return nothing we'll assume we are going to stop.
505e8d8bef9SDimitry Andric  // Also any errors should return true, since we should stop on error.
506e8d8bef9SDimitry Andric
507e8d8bef9SDimitry Andric  PyErr_Cleaner py_err_cleaner(false);
508e8d8bef9SDimitry Andric  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
509e8d8bef9SDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("handle_stop");
510e8d8bef9SDimitry Andric
511e8d8bef9SDimitry Andric  if (!pfunc.IsAllocated())
512e8d8bef9SDimitry Andric    return true;
513e8d8bef9SDimitry Andric
5140eae32dcSDimitry Andric  auto *sb_stream = new lldb::SBStream();
5150eae32dcSDimitry Andric  PythonObject sb_stream_arg =
5160eae32dcSDimitry Andric      ToSWIGWrapper(std::unique_ptr<lldb::SBStream>(sb_stream));
5170eae32dcSDimitry Andric  PythonObject result =
5180eae32dcSDimitry Andric      pfunc(ToSWIGWrapper(std::move(exc_ctx_sp)), sb_stream_arg);
519e8d8bef9SDimitry Andric
5200eae32dcSDimitry Andric  if (PyErr_Occurred()) {
521e8d8bef9SDimitry Andric    stream->PutCString("Python error occurred handling stop-hook.");
522e8d8bef9SDimitry Andric    PyErr_Print();
523e8d8bef9SDimitry Andric    PyErr_Clear();
524e8d8bef9SDimitry Andric    return true;
525e8d8bef9SDimitry Andric  }
526e8d8bef9SDimitry Andric
527e8d8bef9SDimitry Andric  // Now add the result to the output stream.  SBStream only
528e8d8bef9SDimitry Andric  // makes an internally help StreamString which I can't interpose, so I
529e8d8bef9SDimitry Andric  // have to copy it over here.
5300eae32dcSDimitry Andric  stream->PutCString(sb_stream->GetData());
531e8d8bef9SDimitry Andric
532e8d8bef9SDimitry Andric  if (result.get() == Py_False)
533e8d8bef9SDimitry Andric    return false;
534e8d8bef9SDimitry Andric  else
535e8d8bef9SDimitry Andric    return true;
536e8d8bef9SDimitry Andric}
537e8d8bef9SDimitry Andric
5380eae32dcSDimitry Andric// wrapper that calls an optional instance member of an object taking no
5390eae32dcSDimitry Andric// arguments
5400eae32dcSDimitry Andricstatic PyObject *LLDBSwigPython_CallOptionalMember(
5410eae32dcSDimitry Andric    PyObject * implementor, char *callee_name,
5420eae32dcSDimitry Andric    PyObject *ret_if_not_found = Py_None, bool *was_found = NULL) {
543130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(false);
544130d950cSDimitry Andric
545130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
546130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>(callee_name);
547130d950cSDimitry Andric
5480eae32dcSDimitry Andric  if (!pfunc.IsAllocated()) {
549130d950cSDimitry Andric    if (was_found)
550130d950cSDimitry Andric      *was_found = false;
551130d950cSDimitry Andric    Py_XINCREF(ret_if_not_found);
552130d950cSDimitry Andric    return ret_if_not_found;
553130d950cSDimitry Andric  }
554130d950cSDimitry Andric
555130d950cSDimitry Andric  if (was_found)
556130d950cSDimitry Andric    *was_found = true;
557130d950cSDimitry Andric
558130d950cSDimitry Andric  PythonObject result = pfunc();
559130d950cSDimitry Andric  return result.release();
560130d950cSDimitry Andric}
561130d950cSDimitry Andric
5620eae32dcSDimitry Andricsize_t lldb_private::LLDBSwigPython_CalculateNumChildren(PyObject * implementor,
5630eae32dcSDimitry Andric                                                         uint32_t max) {
564130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
565130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("num_children");
566130d950cSDimitry Andric
567130d950cSDimitry Andric  if (!pfunc.IsAllocated())
568130d950cSDimitry Andric    return 0;
569130d950cSDimitry Andric
570130d950cSDimitry Andric  auto arg_info = pfunc.GetArgInfo();
571130d950cSDimitry Andric  if (!arg_info) {
572130d950cSDimitry Andric    llvm::consumeError(arg_info.takeError());
573130d950cSDimitry Andric    return 0;
574130d950cSDimitry Andric  }
575130d950cSDimitry Andric
5765ffd83dbSDimitry Andric  size_t ret_val;
577130d950cSDimitry Andric  if (arg_info.get().max_positional_args < 1)
5785ffd83dbSDimitry Andric    ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call()));
579130d950cSDimitry Andric  else
5800eae32dcSDimitry Andric    ret_val = unwrapOrSetPythonException(
5810eae32dcSDimitry Andric        As<long long>(pfunc.Call(PythonInteger(max))));
582130d950cSDimitry Andric
5830eae32dcSDimitry Andric  if (PyErr_Occurred()) {
584130d950cSDimitry Andric    PyErr_Print();
585130d950cSDimitry Andric    PyErr_Clear();
5865ffd83dbSDimitry Andric    return 0;
587130d950cSDimitry Andric  }
588130d950cSDimitry Andric
589130d950cSDimitry Andric  if (arg_info.get().max_positional_args < 1)
590130d950cSDimitry Andric    ret_val = std::min(ret_val, static_cast<size_t>(max));
591130d950cSDimitry Andric
592130d950cSDimitry Andric  return ret_val;
593130d950cSDimitry Andric}
594130d950cSDimitry Andric
5950eae32dcSDimitry AndricPyObject *lldb_private::LLDBSwigPython_GetChildAtIndex(PyObject * implementor,
5960eae32dcSDimitry Andric                                                       uint32_t idx) {
597130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
598130d950cSDimitry Andric
599130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
600130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index");
601130d950cSDimitry Andric
602130d950cSDimitry Andric  if (!pfunc.IsAllocated())
603130d950cSDimitry Andric    return nullptr;
604130d950cSDimitry Andric
605130d950cSDimitry Andric  PythonObject result = pfunc(PythonInteger(idx));
606130d950cSDimitry Andric
607130d950cSDimitry Andric  if (!result.IsAllocated())
608130d950cSDimitry Andric    return nullptr;
609130d950cSDimitry Andric
610130d950cSDimitry Andric  lldb::SBValue *sbvalue_ptr = nullptr;
6110eae32dcSDimitry Andric  if (SWIG_ConvertPtr(result.get(), (void **)&sbvalue_ptr,
6120eae32dcSDimitry Andric                      SWIGTYPE_p_lldb__SBValue, 0) == -1)
613130d950cSDimitry Andric    return nullptr;
614130d950cSDimitry Andric
615130d950cSDimitry Andric  if (sbvalue_ptr == nullptr)
616130d950cSDimitry Andric    return nullptr;
617130d950cSDimitry Andric
618130d950cSDimitry Andric  return result.release();
619130d950cSDimitry Andric}
620130d950cSDimitry Andric
6210eae32dcSDimitry Andricint lldb_private::LLDBSwigPython_GetIndexOfChildWithName(
6220eae32dcSDimitry Andric    PyObject * implementor, const char *child_name) {
623130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
624130d950cSDimitry Andric
625130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
626130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("get_child_index");
627130d950cSDimitry Andric
628130d950cSDimitry Andric  if (!pfunc.IsAllocated())
629130d950cSDimitry Andric    return UINT32_MAX;
630130d950cSDimitry Andric
6315ffd83dbSDimitry Andric  llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name));
632130d950cSDimitry Andric
6330eae32dcSDimitry Andric  long long retval =
6340eae32dcSDimitry Andric      unwrapOrSetPythonException(As<long long>(std::move(result)));
6355ffd83dbSDimitry Andric
6365ffd83dbSDimitry Andric  if (PyErr_Occurred()) {
6375ffd83dbSDimitry Andric    PyErr_Clear(); // FIXME print this? do something else
638130d950cSDimitry Andric    return UINT32_MAX;
6395ffd83dbSDimitry Andric  }
640130d950cSDimitry Andric
641130d950cSDimitry Andric  if (retval >= 0)
642130d950cSDimitry Andric    return (uint32_t)retval;
643130d950cSDimitry Andric
644130d950cSDimitry Andric  return UINT32_MAX;
645130d950cSDimitry Andric}
646130d950cSDimitry Andric
6470eae32dcSDimitry Andricbool lldb_private::LLDBSwigPython_UpdateSynthProviderInstance(PyObject *
6480eae32dcSDimitry Andric                                                              implementor) {
649130d950cSDimitry Andric  bool ret_val = false;
650130d950cSDimitry Andric
651130d950cSDimitry Andric  static char callee_name[] = "update";
652130d950cSDimitry Andric
6530eae32dcSDimitry Andric  PyObject *py_return =
6540eae32dcSDimitry Andric      LLDBSwigPython_CallOptionalMember(implementor, callee_name);
655130d950cSDimitry Andric
656130d950cSDimitry Andric  if (py_return == Py_True)
657130d950cSDimitry Andric    ret_val = true;
658130d950cSDimitry Andric
659130d950cSDimitry Andric  Py_XDECREF(py_return);
660130d950cSDimitry Andric
661130d950cSDimitry Andric  return ret_val;
662130d950cSDimitry Andric}
663130d950cSDimitry Andric
6640eae32dcSDimitry Andricbool lldb_private::LLDBSwigPython_MightHaveChildrenSynthProviderInstance(
6650eae32dcSDimitry Andric    PyObject * implementor) {
666130d950cSDimitry Andric  bool ret_val = false;
667130d950cSDimitry Andric
668130d950cSDimitry Andric  static char callee_name[] = "has_children";
669130d950cSDimitry Andric
6700eae32dcSDimitry Andric  PyObject *py_return =
6710eae32dcSDimitry Andric      LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_True);
672130d950cSDimitry Andric
673130d950cSDimitry Andric  if (py_return == Py_True)
674130d950cSDimitry Andric    ret_val = true;
675130d950cSDimitry Andric
676130d950cSDimitry Andric  Py_XDECREF(py_return);
677130d950cSDimitry Andric
678130d950cSDimitry Andric  return ret_val;
679130d950cSDimitry Andric}
680130d950cSDimitry Andric
6810eae32dcSDimitry AndricPyObject *lldb_private::LLDBSwigPython_GetValueSynthProviderInstance(
6820eae32dcSDimitry Andric    PyObject * implementor) {
683130d950cSDimitry Andric  PyObject *ret_val = nullptr;
684130d950cSDimitry Andric
685130d950cSDimitry Andric  static char callee_name[] = "get_value";
686130d950cSDimitry Andric
6870eae32dcSDimitry Andric  PyObject *py_return =
6880eae32dcSDimitry Andric      LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_None);
689130d950cSDimitry Andric
690130d950cSDimitry Andric  if (py_return == Py_None || py_return == nullptr)
691130d950cSDimitry Andric    ret_val = nullptr;
692130d950cSDimitry Andric
693130d950cSDimitry Andric  lldb::SBValue *sbvalue_ptr = NULL;
694130d950cSDimitry Andric
6950eae32dcSDimitry Andric  if (SWIG_ConvertPtr(py_return, (void **)&sbvalue_ptr,
6960eae32dcSDimitry Andric                      SWIGTYPE_p_lldb__SBValue, 0) == -1)
697130d950cSDimitry Andric    ret_val = nullptr;
698130d950cSDimitry Andric  else if (sbvalue_ptr == NULL)
699130d950cSDimitry Andric    ret_val = nullptr;
700130d950cSDimitry Andric  else
701130d950cSDimitry Andric    ret_val = py_return;
702130d950cSDimitry Andric
703130d950cSDimitry Andric  Py_XDECREF(py_return);
704130d950cSDimitry Andric  return ret_val;
705130d950cSDimitry Andric}
706130d950cSDimitry Andric
7070eae32dcSDimitry Andricvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBData(PyObject * data) {
708fe6060f1SDimitry Andric  lldb::SBData *sb_ptr = nullptr;
709fe6060f1SDimitry Andric
7100eae32dcSDimitry Andric  int valid_cast =
7110eae32dcSDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0);
712fe6060f1SDimitry Andric
713fe6060f1SDimitry Andric  if (valid_cast == -1)
714fe6060f1SDimitry Andric    return NULL;
715fe6060f1SDimitry Andric
716fe6060f1SDimitry Andric  return sb_ptr;
717fe6060f1SDimitry Andric}
718fe6060f1SDimitry Andric
7190eae32dcSDimitry Andricvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBError(PyObject * data) {
720fe6060f1SDimitry Andric  lldb::SBError *sb_ptr = nullptr;
721fe6060f1SDimitry Andric
7220eae32dcSDimitry Andric  int valid_cast =
7230eae32dcSDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0);
724fe6060f1SDimitry Andric
725fe6060f1SDimitry Andric  if (valid_cast == -1)
726fe6060f1SDimitry Andric    return NULL;
727fe6060f1SDimitry Andric
728fe6060f1SDimitry Andric  return sb_ptr;
729fe6060f1SDimitry Andric}
730fe6060f1SDimitry Andric
7310eae32dcSDimitry Andricvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBValue(PyObject * data) {
732130d950cSDimitry Andric  lldb::SBValue *sb_ptr = NULL;
733130d950cSDimitry Andric
7340eae32dcSDimitry Andric  int valid_cast =
7350eae32dcSDimitry Andric      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
736130d950cSDimitry Andric
737130d950cSDimitry Andric  if (valid_cast == -1)
738130d950cSDimitry Andric    return NULL;
739130d950cSDimitry Andric
740130d950cSDimitry Andric  return sb_ptr;
741130d950cSDimitry Andric}
742130d950cSDimitry Andric
7430eae32dcSDimitry Andricvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *
7440eae32dcSDimitry Andric                                                                    data) {
745349cc55cSDimitry Andric  lldb::SBMemoryRegionInfo *sb_ptr = NULL;
746349cc55cSDimitry Andric
7470eae32dcSDimitry Andric  int valid_cast = SWIG_ConvertPtr(data, (void **)&sb_ptr,
7480eae32dcSDimitry Andric                                   SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0);
749349cc55cSDimitry Andric
750349cc55cSDimitry Andric  if (valid_cast == -1)
751349cc55cSDimitry Andric    return NULL;
752349cc55cSDimitry Andric
753349cc55cSDimitry Andric  return sb_ptr;
754349cc55cSDimitry Andric}
755349cc55cSDimitry Andric
7560eae32dcSDimitry Andricbool lldb_private::LLDBSwigPythonCallCommand(
7570eae32dcSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
7580eae32dcSDimitry Andric    lldb::DebuggerSP debugger, const char *args,
759130d950cSDimitry Andric    lldb_private::CommandReturnObject &cmd_retobj,
7600eae32dcSDimitry Andric    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
761130d950cSDimitry Andric
762130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
7630eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
7640eae32dcSDimitry Andric      session_dictionary_name);
7650eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
7660eae32dcSDimitry Andric      python_function_name, dict);
767130d950cSDimitry Andric
768130d950cSDimitry Andric  if (!pfunc.IsAllocated())
769130d950cSDimitry Andric    return false;
770130d950cSDimitry Andric
771130d950cSDimitry Andric  auto argc = pfunc.GetArgInfo();
772130d950cSDimitry Andric  if (!argc) {
773130d950cSDimitry Andric    llvm::consumeError(argc.takeError());
774130d950cSDimitry Andric    return false;
775130d950cSDimitry Andric  }
7760eae32dcSDimitry Andric  PythonObject debugger_arg = ToSWIGWrapper(std::move(debugger));
77704eeddc0SDimitry Andric  auto cmd_retobj_arg = ToSWIGWrapper(cmd_retobj);
778130d950cSDimitry Andric
779130d950cSDimitry Andric  if (argc.get().max_positional_args < 5u)
78004eeddc0SDimitry Andric    pfunc(debugger_arg, PythonString(args), cmd_retobj_arg.obj(), dict);
781130d950cSDimitry Andric  else
7820eae32dcSDimitry Andric    pfunc(debugger_arg, PythonString(args),
78304eeddc0SDimitry Andric          ToSWIGWrapper(std::move(exe_ctx_ref_sp)), cmd_retobj_arg.obj(), dict);
784130d950cSDimitry Andric
785130d950cSDimitry Andric  return true;
786130d950cSDimitry Andric}
787130d950cSDimitry Andric
7880eae32dcSDimitry Andricbool lldb_private::LLDBSwigPythonCallCommandObject(
7890eae32dcSDimitry Andric    PyObject *implementor, lldb::DebuggerSP debugger, const char *args,
790130d950cSDimitry Andric    lldb_private::CommandReturnObject &cmd_retobj,
7910eae32dcSDimitry Andric    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
792130d950cSDimitry Andric
793130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
794130d950cSDimitry Andric
795130d950cSDimitry Andric  PythonObject self(PyRefType::Borrowed, implementor);
796130d950cSDimitry Andric  auto pfunc = self.ResolveName<PythonCallable>("__call__");
797130d950cSDimitry Andric
798130d950cSDimitry Andric  if (!pfunc.IsAllocated())
799130d950cSDimitry Andric    return false;
800130d950cSDimitry Andric
80104eeddc0SDimitry Andric  auto cmd_retobj_arg = ToSWIGWrapper(cmd_retobj);
802130d950cSDimitry Andric
8030eae32dcSDimitry Andric  pfunc(ToSWIGWrapper(std::move(debugger)), PythonString(args),
80404eeddc0SDimitry Andric        ToSWIGWrapper(exe_ctx_ref_sp), cmd_retobj_arg.obj());
805130d950cSDimitry Andric
806130d950cSDimitry Andric  return true;
807130d950cSDimitry Andric}
808130d950cSDimitry Andric
80904eeddc0SDimitry AndricPythonObject lldb_private::LLDBSWIGPythonCreateOSPlugin(
8100eae32dcSDimitry Andric    const char *python_class_name, const char *session_dictionary_name,
8110eae32dcSDimitry Andric    const lldb::ProcessSP &process_sp) {
8120eae32dcSDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
8130eae32dcSDimitry Andric      !session_dictionary_name)
81404eeddc0SDimitry Andric    return PythonObject();
815130d950cSDimitry Andric
816130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
817130d950cSDimitry Andric
8180eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
8190eae32dcSDimitry Andric      session_dictionary_name);
8200eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
8210eae32dcSDimitry Andric      python_class_name, dict);
822130d950cSDimitry Andric
823130d950cSDimitry Andric  if (!pfunc.IsAllocated())
82404eeddc0SDimitry Andric    return PythonObject();
825130d950cSDimitry Andric
82604eeddc0SDimitry Andric  return pfunc(ToSWIGWrapper(process_sp));
827130d950cSDimitry Andric}
828130d950cSDimitry Andric
82904eeddc0SDimitry AndricPythonObject lldb_private::LLDBSWIGPython_CreateFrameRecognizer(
8300eae32dcSDimitry Andric    const char *python_class_name, const char *session_dictionary_name) {
8310eae32dcSDimitry Andric  if (python_class_name == NULL || python_class_name[0] == '\0' ||
8320eae32dcSDimitry Andric      !session_dictionary_name)
83304eeddc0SDimitry Andric    return PythonObject();
834130d950cSDimitry Andric
835130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
836130d950cSDimitry Andric
8370eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
8380eae32dcSDimitry Andric      session_dictionary_name);
8390eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
8400eae32dcSDimitry Andric      python_class_name, dict);
841130d950cSDimitry Andric
842130d950cSDimitry Andric  if (!pfunc.IsAllocated())
84304eeddc0SDimitry Andric    return PythonObject();
844130d950cSDimitry Andric
84504eeddc0SDimitry Andric  return pfunc();
846130d950cSDimitry Andric}
847130d950cSDimitry Andric
8480eae32dcSDimitry AndricPyObject *lldb_private::LLDBSwigPython_GetRecognizedArguments(
8490eae32dcSDimitry Andric    PyObject * implementor, const lldb::StackFrameSP &frame_sp) {
850130d950cSDimitry Andric  static char callee_name[] = "get_recognized_arguments";
851130d950cSDimitry Andric
8520eae32dcSDimitry Andric  PythonObject arg = ToSWIGWrapper(frame_sp);
853130d950cSDimitry Andric
854130d950cSDimitry Andric  PythonString str(callee_name);
8550eae32dcSDimitry Andric  PyObject *result =
8560eae32dcSDimitry Andric      PyObject_CallMethodObjArgs(implementor, str.get(), arg.get(), NULL);
857130d950cSDimitry Andric  return result;
858130d950cSDimitry Andric}
859130d950cSDimitry Andric
8600eae32dcSDimitry Andricvoid *lldb_private::LLDBSWIGPython_GetDynamicSetting(
8610eae32dcSDimitry Andric    void *module, const char *setting, const lldb::TargetSP &target_sp) {
862130d950cSDimitry Andric  if (!module || !setting)
863130d950cSDimitry Andric    Py_RETURN_NONE;
864130d950cSDimitry Andric
865130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
866130d950cSDimitry Andric  PythonObject py_module(PyRefType::Borrowed, (PyObject *)module);
867130d950cSDimitry Andric  auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting");
868130d950cSDimitry Andric
869130d950cSDimitry Andric  if (!pfunc.IsAllocated())
870130d950cSDimitry Andric    Py_RETURN_NONE;
871130d950cSDimitry Andric
8724824e7fdSDimitry Andric  auto result = pfunc(ToSWIGWrapper(target_sp), PythonString(setting));
873130d950cSDimitry Andric
874130d950cSDimitry Andric  return result.release();
875130d950cSDimitry Andric}
876130d950cSDimitry Andric
8774824e7fdSDimitry Andricbool lldb_private::LLDBSWIGPythonRunScriptKeywordProcess(
8784824e7fdSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
8794824e7fdSDimitry Andric    const lldb::ProcessSP &process, std::string &output) {
880130d950cSDimitry Andric
8810eae32dcSDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
8820eae32dcSDimitry Andric      !session_dictionary_name)
883130d950cSDimitry Andric    return false;
884130d950cSDimitry Andric
885130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
886130d950cSDimitry Andric
8870eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
8880eae32dcSDimitry Andric      session_dictionary_name);
8890eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
8900eae32dcSDimitry Andric      python_function_name, dict);
891130d950cSDimitry Andric
892130d950cSDimitry Andric  if (!pfunc.IsAllocated())
893130d950cSDimitry Andric    return false;
894130d950cSDimitry Andric
8954824e7fdSDimitry Andric  auto result = pfunc(ToSWIGWrapper(process), dict);
896130d950cSDimitry Andric
897130d950cSDimitry Andric  output = result.Str().GetString().str();
898130d950cSDimitry Andric
899130d950cSDimitry Andric  return true;
900130d950cSDimitry Andric}
901130d950cSDimitry Andric
902*bdd1243dSDimitry Andricstd::optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordThread(
9030eae32dcSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
9040eae32dcSDimitry Andric    lldb::ThreadSP thread) {
9050eae32dcSDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9060eae32dcSDimitry Andric      !session_dictionary_name)
907*bdd1243dSDimitry Andric    return std::nullopt;
908130d950cSDimitry Andric
909130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
910130d950cSDimitry Andric
9110eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9120eae32dcSDimitry Andric      session_dictionary_name);
9130eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9140eae32dcSDimitry Andric      python_function_name, dict);
915130d950cSDimitry Andric
916130d950cSDimitry Andric  if (!pfunc.IsAllocated())
917*bdd1243dSDimitry Andric    return std::nullopt;
918130d950cSDimitry Andric
9190eae32dcSDimitry Andric  auto result = pfunc(ToSWIGWrapper(std::move(thread)), dict);
920130d950cSDimitry Andric
9210eae32dcSDimitry Andric  return result.Str().GetString().str();
922130d950cSDimitry Andric}
923130d950cSDimitry Andric
9244824e7fdSDimitry Andricbool lldb_private::LLDBSWIGPythonRunScriptKeywordTarget(
9254824e7fdSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
9264824e7fdSDimitry Andric    const lldb::TargetSP &target, std::string &output) {
927130d950cSDimitry Andric
9280eae32dcSDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9290eae32dcSDimitry Andric      !session_dictionary_name)
930130d950cSDimitry Andric    return false;
931130d950cSDimitry Andric
932130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
933130d950cSDimitry Andric
9340eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9350eae32dcSDimitry Andric      session_dictionary_name);
9360eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9370eae32dcSDimitry Andric      python_function_name, dict);
938130d950cSDimitry Andric
939130d950cSDimitry Andric  if (!pfunc.IsAllocated())
940130d950cSDimitry Andric    return false;
941130d950cSDimitry Andric
9424824e7fdSDimitry Andric  auto result = pfunc(ToSWIGWrapper(target), dict);
943130d950cSDimitry Andric
944130d950cSDimitry Andric  output = result.Str().GetString().str();
945130d950cSDimitry Andric
946130d950cSDimitry Andric  return true;
947130d950cSDimitry Andric}
948130d950cSDimitry Andric
949*bdd1243dSDimitry Andricstd::optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordFrame(
9500eae32dcSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
9510eae32dcSDimitry Andric    lldb::StackFrameSP frame) {
9520eae32dcSDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9530eae32dcSDimitry Andric      !session_dictionary_name)
954*bdd1243dSDimitry Andric    return std::nullopt;
955130d950cSDimitry Andric
956130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
957130d950cSDimitry Andric
9580eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9590eae32dcSDimitry Andric      session_dictionary_name);
9600eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9610eae32dcSDimitry Andric      python_function_name, dict);
962130d950cSDimitry Andric
963130d950cSDimitry Andric  if (!pfunc.IsAllocated())
964*bdd1243dSDimitry Andric    return std::nullopt;
965130d950cSDimitry Andric
9660eae32dcSDimitry Andric  auto result = pfunc(ToSWIGWrapper(std::move(frame)), dict);
967130d950cSDimitry Andric
9680eae32dcSDimitry Andric  return result.Str().GetString().str();
969130d950cSDimitry Andric}
970130d950cSDimitry Andric
9714824e7fdSDimitry Andricbool lldb_private::LLDBSWIGPythonRunScriptKeywordValue(
9724824e7fdSDimitry Andric    const char *python_function_name, const char *session_dictionary_name,
9734824e7fdSDimitry Andric    const lldb::ValueObjectSP &value, std::string &output) {
974130d950cSDimitry Andric
9750eae32dcSDimitry Andric  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9760eae32dcSDimitry Andric      !session_dictionary_name)
977130d950cSDimitry Andric    return false;
978130d950cSDimitry Andric
979130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
980130d950cSDimitry Andric
9810eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9820eae32dcSDimitry Andric      session_dictionary_name);
9830eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9840eae32dcSDimitry Andric      python_function_name, dict);
985130d950cSDimitry Andric
986130d950cSDimitry Andric  if (!pfunc.IsAllocated())
987130d950cSDimitry Andric    return false;
988130d950cSDimitry Andric
9894824e7fdSDimitry Andric  auto result = pfunc(ToSWIGWrapper(value), dict);
990130d950cSDimitry Andric
991130d950cSDimitry Andric  output = result.Str().GetString().str();
992130d950cSDimitry Andric
993130d950cSDimitry Andric  return true;
994130d950cSDimitry Andric}
995130d950cSDimitry Andric
9960eae32dcSDimitry Andricbool lldb_private::LLDBSwigPythonCallModuleInit(
9970eae32dcSDimitry Andric    const char *python_module_name, const char *session_dictionary_name,
9980eae32dcSDimitry Andric    lldb::DebuggerSP debugger) {
999130d950cSDimitry Andric  std::string python_function_name_string = python_module_name;
1000130d950cSDimitry Andric  python_function_name_string += ".__lldb_init_module";
1001130d950cSDimitry Andric  const char *python_function_name = python_function_name_string.c_str();
1002130d950cSDimitry Andric
1003130d950cSDimitry Andric  PyErr_Cleaner py_err_cleaner(true);
1004130d950cSDimitry Andric
10050eae32dcSDimitry Andric  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10060eae32dcSDimitry Andric      session_dictionary_name);
10070eae32dcSDimitry Andric  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10080eae32dcSDimitry Andric      python_function_name, dict);
1009130d950cSDimitry Andric
1010130d950cSDimitry Andric  // This method is optional and need not exist.  So if we don't find it,
1011130d950cSDimitry Andric  // it's actually a success, not a failure.
1012130d950cSDimitry Andric  if (!pfunc.IsAllocated())
1013130d950cSDimitry Andric    return true;
1014130d950cSDimitry Andric
10150eae32dcSDimitry Andric  pfunc(ToSWIGWrapper(std::move(debugger)), dict);
1016130d950cSDimitry Andric
1017130d950cSDimitry Andric  return true;
1018130d950cSDimitry Andric}
1019130d950cSDimitry Andric
10200eae32dcSDimitry Andriclldb::ValueObjectSP lldb_private::LLDBSWIGPython_GetValueObjectSPFromSBValue(
10210eae32dcSDimitry Andric    void *data) {
1022130d950cSDimitry Andric  lldb::ValueObjectSP valobj_sp;
10230eae32dcSDimitry Andric  if (data) {
1024130d950cSDimitry Andric    lldb::SBValue *sb_ptr = (lldb::SBValue *)data;
1025130d950cSDimitry Andric    valobj_sp = sb_ptr->GetSP();
1026130d950cSDimitry Andric  }
1027130d950cSDimitry Andric  return valobj_sp;
1028130d950cSDimitry Andric}
1029130d950cSDimitry Andric
1030130d950cSDimitry Andric// For the LogOutputCallback functions
10310eae32dcSDimitry Andricstatic void LLDBSwigPythonCallPythonLogOutputCallback(const char *str,
10320eae32dcSDimitry Andric                                                      void *baton) {
1033130d950cSDimitry Andric  if (baton != Py_None) {
1034130d950cSDimitry Andric    SWIG_PYTHON_THREAD_BEGIN_BLOCK;
10350eae32dcSDimitry Andric    PyObject *result = PyObject_CallFunction(
10360eae32dcSDimitry Andric        reinterpret_cast<PyObject *>(baton), const_cast<char *>("s"), str);
1037130d950cSDimitry Andric    Py_XDECREF(result);
1038130d950cSDimitry Andric    SWIG_PYTHON_THREAD_END_BLOCK;
1039130d950cSDimitry Andric  }
1040130d950cSDimitry Andric}
1041130d950cSDimitry Andric%}
1042