16498aff2SJonas Devlieghere%header %{
26498aff2SJonas Devlieghere
39d5e37edSPavel Labathclass PyErr_Cleaner {
46498aff2SJonas Devliegherepublic:
59d5e37edSPavel Labath  PyErr_Cleaner(bool print = false) : m_print(print) {}
66498aff2SJonas Devlieghere
79d5e37edSPavel Labath  ~PyErr_Cleaner() {
89d5e37edSPavel Labath    if (PyErr_Occurred()) {
96498aff2SJonas Devlieghere      if (m_print && !PyErr_ExceptionMatches(PyExc_SystemExit))
106498aff2SJonas Devlieghere        PyErr_Print();
116498aff2SJonas Devlieghere      PyErr_Clear();
126498aff2SJonas Devlieghere    }
136498aff2SJonas Devlieghere  }
146498aff2SJonas Devlieghere
156498aff2SJonas Devlieghereprivate:
166498aff2SJonas Devlieghere  bool m_print;
176498aff2SJonas Devlieghere};
186498aff2SJonas Devlieghere
199d5e37edSPavel Labathllvm::Expected<bool> lldb_private::LLDBSwigPythonBreakpointCallbackFunction(
209d5e37edSPavel Labath    const char *python_function_name, const char *session_dictionary_name,
216498aff2SJonas Devlieghere    const lldb::StackFrameSP &frame_sp,
226498aff2SJonas Devlieghere    const lldb::BreakpointLocationSP &bp_loc_sp,
239d5e37edSPavel Labath    const lldb_private::StructuredDataImpl &args_impl) {
246498aff2SJonas Devlieghere  using namespace llvm;
256498aff2SJonas Devlieghere
266498aff2SJonas Devlieghere  lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
276498aff2SJonas Devlieghere
286498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
299d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
309d5e37edSPavel Labath      session_dictionary_name);
319d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
329d5e37edSPavel Labath      python_function_name, dict);
336498aff2SJonas Devlieghere
346498aff2SJonas Devlieghere  unsigned max_positional_args;
356498aff2SJonas Devlieghere  if (auto arg_info = pfunc.GetArgInfo())
366498aff2SJonas Devlieghere    max_positional_args = arg_info.get().max_positional_args;
376498aff2SJonas Devlieghere  else
386498aff2SJonas Devlieghere    return arg_info.takeError();
396498aff2SJonas Devlieghere
407406d236SPavel Labath  PythonObject frame_arg = ToSWIGWrapper(frame_sp);
412efc6892SPavel Labath  PythonObject bp_loc_arg = ToSWIGWrapper(bp_loc_sp);
426498aff2SJonas Devlieghere
432efc6892SPavel Labath  auto result =
442efc6892SPavel Labath      max_positional_args < 4
452efc6892SPavel Labath          ? pfunc.Call(frame_arg, bp_loc_arg, dict)
462efc6892SPavel Labath          : pfunc.Call(frame_arg, bp_loc_arg, ToSWIGWrapper(args_impl), dict);
476498aff2SJonas Devlieghere
486498aff2SJonas Devlieghere  if (!result)
496498aff2SJonas Devlieghere    return result.takeError();
506498aff2SJonas Devlieghere
516498aff2SJonas Devlieghere  // Only False counts as false!
526498aff2SJonas Devlieghere  return result.get().get() != Py_False;
536498aff2SJonas Devlieghere}
546498aff2SJonas Devlieghere
559a14adeaSPavel Labath// resolve a dotted Python name in the form
569a14adeaSPavel Labath// foo.bar.baz.Foobar to an actual Python object
579a14adeaSPavel Labath// if pmodule is NULL, the __main__ module will be used
589a14adeaSPavel Labath// as the starting point for the search
59daf36998SDave Lee
609d5e37edSPavel Labath// This function is called by
619d5e37edSPavel Labath// lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...) and is
629d5e37edSPavel Labath// used when a script command is attached to a breakpoint for execution.
639a14adeaSPavel Labath
649d5e37edSPavel Labath// This function is called by
659d5e37edSPavel Labath// lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...) and is
669d5e37edSPavel Labath// used when a script command is attached to a watchpoint for execution.
67daf36998SDave Lee
689d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonWatchpointCallbackFunction(
699d5e37edSPavel Labath    const char *python_function_name, const char *session_dictionary_name,
709d5e37edSPavel Labath    const lldb::StackFrameSP &frame_sp, const lldb::WatchpointSP &wp_sp) {
716498aff2SJonas Devlieghere
726498aff2SJonas Devlieghere  bool stop_at_watchpoint = true;
736498aff2SJonas Devlieghere
746498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
756498aff2SJonas Devlieghere
769d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
779d5e37edSPavel Labath      session_dictionary_name);
789d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
799d5e37edSPavel Labath      python_function_name, dict);
806498aff2SJonas Devlieghere
816498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
826498aff2SJonas Devlieghere    return stop_at_watchpoint;
836498aff2SJonas Devlieghere
842efc6892SPavel Labath  PythonObject result =
852efc6892SPavel Labath      pfunc(ToSWIGWrapper(frame_sp), ToSWIGWrapper(wp_sp), dict);
866498aff2SJonas Devlieghere
876498aff2SJonas Devlieghere  if (result.get() == Py_False)
886498aff2SJonas Devlieghere    stop_at_watchpoint = false;
896498aff2SJonas Devlieghere
906498aff2SJonas Devlieghere  return stop_at_watchpoint;
916498aff2SJonas Devlieghere}
926498aff2SJonas Devlieghere
939d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonCallTypeScript(
949d5e37edSPavel Labath    const char *python_function_name, const void *session_dictionary,
959d5e37edSPavel Labath    const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper,
969d5e37edSPavel Labath    const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval) {
976498aff2SJonas Devlieghere
986498aff2SJonas Devlieghere  retval.clear();
996498aff2SJonas Devlieghere
1006498aff2SJonas Devlieghere  if (!python_function_name || !session_dictionary)
1016498aff2SJonas Devlieghere    return false;
1026498aff2SJonas Devlieghere
1036498aff2SJonas Devlieghere  PyObject *pfunc_impl = nullptr;
1046498aff2SJonas Devlieghere
1059d5e37edSPavel Labath  if (pyfunct_wrapper && *pyfunct_wrapper &&
1069d5e37edSPavel Labath      PyFunction_Check(*pyfunct_wrapper)) {
1076498aff2SJonas Devlieghere    pfunc_impl = (PyObject *)(*pyfunct_wrapper);
1089d5e37edSPavel Labath    if (pfunc_impl->ob_refcnt == 1) {
1096498aff2SJonas Devlieghere      Py_XDECREF(pfunc_impl);
1106498aff2SJonas Devlieghere      pfunc_impl = NULL;
1116498aff2SJonas Devlieghere    }
1126498aff2SJonas Devlieghere  }
1136498aff2SJonas Devlieghere
1146498aff2SJonas Devlieghere  PyObject *py_dict = (PyObject *)session_dictionary;
1156498aff2SJonas Devlieghere  if (!PythonDictionary::Check(py_dict))
1166498aff2SJonas Devlieghere    return true;
1176498aff2SJonas Devlieghere
1186498aff2SJonas Devlieghere  PythonDictionary dict(PyRefType::Borrowed, py_dict);
1196498aff2SJonas Devlieghere
1206498aff2SJonas Devlieghere  PyErr_Cleaner pyerr_cleanup(true); // show Python errors
1216498aff2SJonas Devlieghere
1226498aff2SJonas Devlieghere  PythonCallable pfunc(PyRefType::Borrowed, pfunc_impl);
1236498aff2SJonas Devlieghere
1249d5e37edSPavel Labath  if (!pfunc.IsAllocated()) {
1259d5e37edSPavel Labath    pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
1269d5e37edSPavel Labath        python_function_name, dict);
1276498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
1286498aff2SJonas Devlieghere      return false;
1296498aff2SJonas Devlieghere
1309d5e37edSPavel Labath    if (pyfunct_wrapper) {
1316498aff2SJonas Devlieghere      *pyfunct_wrapper = pfunc.get();
1326498aff2SJonas Devlieghere      Py_XINCREF(pfunc.get());
1336498aff2SJonas Devlieghere    }
1346498aff2SJonas Devlieghere  }
1356498aff2SJonas Devlieghere
1366498aff2SJonas Devlieghere  PythonObject result;
1376498aff2SJonas Devlieghere  auto argc = pfunc.GetArgInfo();
1386498aff2SJonas Devlieghere  if (!argc) {
1396498aff2SJonas Devlieghere    llvm::consumeError(argc.takeError());
1406498aff2SJonas Devlieghere    return false;
1416498aff2SJonas Devlieghere  }
1426498aff2SJonas Devlieghere
1437f09ab08SPavel Labath  PythonObject value_arg = ToSWIGWrapper(valobj_sp);
1446498aff2SJonas Devlieghere
1456498aff2SJonas Devlieghere  if (argc.get().max_positional_args < 3)
1466498aff2SJonas Devlieghere    result = pfunc(value_arg, dict);
1476498aff2SJonas Devlieghere  else
1482efc6892SPavel Labath    result = pfunc(value_arg, dict, ToSWIGWrapper(*options_sp));
1496498aff2SJonas Devlieghere
1506498aff2SJonas Devlieghere  retval = result.Str().GetString().str();
1516498aff2SJonas Devlieghere
1526498aff2SJonas Devlieghere  return true;
1536498aff2SJonas Devlieghere}
1546498aff2SJonas Devlieghere
1559d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateSyntheticProvider(
1569d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
1579d5e37edSPavel Labath    const lldb::ValueObjectSP &valobj_sp) {
1589d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
1599d5e37edSPavel Labath      !session_dictionary_name)
1606498aff2SJonas Devlieghere    Py_RETURN_NONE;
1616498aff2SJonas Devlieghere
1626498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
1636498aff2SJonas Devlieghere
1649d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
1659d5e37edSPavel Labath      session_dictionary_name);
1669d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
1679d5e37edSPavel Labath      python_class_name, dict);
1686498aff2SJonas Devlieghere
1696498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
1706498aff2SJonas Devlieghere    Py_RETURN_NONE;
1716498aff2SJonas Devlieghere
1727f09ab08SPavel Labath  auto sb_value = std::make_unique<lldb::SBValue>(valobj_sp);
1736498aff2SJonas Devlieghere  sb_value->SetPreferSyntheticValue(false);
1746498aff2SJonas Devlieghere
1757f09ab08SPavel Labath  PythonObject val_arg = ToSWIGWrapper(std::move(sb_value));
1766498aff2SJonas Devlieghere  if (!val_arg.IsAllocated())
1776498aff2SJonas Devlieghere    Py_RETURN_NONE;
1786498aff2SJonas Devlieghere
1796498aff2SJonas Devlieghere  PythonObject result = pfunc(val_arg, dict);
1806498aff2SJonas Devlieghere
1816498aff2SJonas Devlieghere  if (result.IsAllocated())
1826498aff2SJonas Devlieghere    return result.release();
1836498aff2SJonas Devlieghere
1846498aff2SJonas Devlieghere  Py_RETURN_NONE;
1856498aff2SJonas Devlieghere}
1866498aff2SJonas Devlieghere
1879d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateCommandObject(
1889d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
1897406d236SPavel Labath    lldb::DebuggerSP debugger_sp) {
1909d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
1919d5e37edSPavel Labath      !session_dictionary_name)
1926498aff2SJonas Devlieghere    Py_RETURN_NONE;
1936498aff2SJonas Devlieghere
1946498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
1959d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
1969d5e37edSPavel Labath      session_dictionary_name);
1979d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
1989d5e37edSPavel Labath      python_class_name, dict);
1996498aff2SJonas Devlieghere
2006498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
2016498aff2SJonas Devlieghere    return nullptr;
2026498aff2SJonas Devlieghere
2037406d236SPavel Labath  PythonObject result = pfunc(ToSWIGWrapper(std::move(debugger_sp)), dict);
2046498aff2SJonas Devlieghere
2056498aff2SJonas Devlieghere  if (result.IsAllocated())
2066498aff2SJonas Devlieghere    return result.release();
2076498aff2SJonas Devlieghere
2086498aff2SJonas Devlieghere  Py_RETURN_NONE;
2096498aff2SJonas Devlieghere}
2106498aff2SJonas Devlieghere
2119d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedProcess(
2129d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
2131f6a57c1SMed Ismail Bennani    const lldb::TargetSP &target_sp,
21482de8df2SPavel Labath    const lldb_private::StructuredDataImpl &args_impl,
2159d5e37edSPavel Labath    std::string &error_string) {
2169d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
2179d5e37edSPavel Labath      !session_dictionary_name)
2181f6a57c1SMed Ismail Bennani    Py_RETURN_NONE;
2191f6a57c1SMed Ismail Bennani
2201f6a57c1SMed Ismail Bennani  PyErr_Cleaner py_err_cleaner(true);
2211f6a57c1SMed Ismail Bennani
2229d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
2239d5e37edSPavel Labath      session_dictionary_name);
2249d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
2259d5e37edSPavel Labath      python_class_name, dict);
2261f6a57c1SMed Ismail Bennani
2271f6a57c1SMed Ismail Bennani  if (!pfunc.IsAllocated()) {
2281f6a57c1SMed Ismail Bennani    error_string.append("could not find script class: ");
2291f6a57c1SMed Ismail Bennani    error_string.append(python_class_name);
2301f6a57c1SMed Ismail Bennani    return nullptr;
2311f6a57c1SMed Ismail Bennani  }
2321f6a57c1SMed Ismail Bennani
2337f09ab08SPavel Labath  PythonObject target_arg = ToSWIGWrapper(target_sp);
2341f6a57c1SMed Ismail Bennani
2351f6a57c1SMed Ismail Bennani  llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
2361f6a57c1SMed Ismail Bennani  if (!arg_info) {
2371f6a57c1SMed Ismail Bennani    llvm::handleAllErrors(
2381f6a57c1SMed Ismail Bennani        arg_info.takeError(),
2399d5e37edSPavel Labath        [&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
2401f6a57c1SMed Ismail Bennani        [&](const llvm::ErrorInfoBase &E) {
2411f6a57c1SMed Ismail Bennani          error_string.append(E.message());
2421f6a57c1SMed Ismail Bennani        });
2431f6a57c1SMed Ismail Bennani    Py_RETURN_NONE;
2441f6a57c1SMed Ismail Bennani  }
2451f6a57c1SMed Ismail Bennani
2461f6a57c1SMed Ismail Bennani  PythonObject result = {};
2471f6a57c1SMed Ismail Bennani  if (arg_info.get().max_positional_args == 2) {
248ebb6bb72SPavel Labath    result = pfunc(target_arg, ToSWIGWrapper(args_impl));
2491f6a57c1SMed Ismail Bennani  } else {
2509d5e37edSPavel Labath    error_string.assign("wrong number of arguments in __init__, should be 2 "
2519d5e37edSPavel Labath                        "(not including self)");
2521f6a57c1SMed Ismail Bennani    Py_RETURN_NONE;
2531f6a57c1SMed Ismail Bennani  }
2541f6a57c1SMed Ismail Bennani
2551f6a57c1SMed Ismail Bennani  if (result.IsAllocated())
2561f6a57c1SMed Ismail Bennani    return result.release();
2571f6a57c1SMed Ismail Bennani  Py_RETURN_NONE;
2581f6a57c1SMed Ismail Bennani}
2591f6a57c1SMed Ismail Bennani
2609d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedThread(
2619d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
2629d5e37edSPavel Labath    const lldb::ProcessSP &process_sp, const StructuredDataImpl &args_impl,
2639d5e37edSPavel Labath    std::string &error_string) {
2649d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
2659d5e37edSPavel Labath      !session_dictionary_name)
26659d8dd79SMed Ismail Bennani    Py_RETURN_NONE;
26759d8dd79SMed Ismail Bennani
26859d8dd79SMed Ismail Bennani  PyErr_Cleaner py_err_cleaner(true);
26959d8dd79SMed Ismail Bennani
2709d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
2719d5e37edSPavel Labath      session_dictionary_name);
2729d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
2739d5e37edSPavel Labath      python_class_name, dict);
27459d8dd79SMed Ismail Bennani
27559d8dd79SMed Ismail Bennani  if (!pfunc.IsAllocated()) {
27659d8dd79SMed Ismail Bennani    error_string.append("could not find script class: ");
27759d8dd79SMed Ismail Bennani    error_string.append(python_class_name);
27859d8dd79SMed Ismail Bennani    return nullptr;
27959d8dd79SMed Ismail Bennani  }
28059d8dd79SMed Ismail Bennani
28159d8dd79SMed Ismail Bennani  llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
28259d8dd79SMed Ismail Bennani  if (!arg_info) {
28359d8dd79SMed Ismail Bennani    llvm::handleAllErrors(
28459d8dd79SMed Ismail Bennani        arg_info.takeError(),
2859d5e37edSPavel Labath        [&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
28659d8dd79SMed Ismail Bennani        [&](const llvm::ErrorInfoBase &E) {
28759d8dd79SMed Ismail Bennani          error_string.append(E.message());
28859d8dd79SMed Ismail Bennani        });
28959d8dd79SMed Ismail Bennani    Py_RETURN_NONE;
29059d8dd79SMed Ismail Bennani  }
29159d8dd79SMed Ismail Bennani
29259d8dd79SMed Ismail Bennani  PythonObject result = {};
293738621d0SMed Ismail Bennani  if (arg_info.get().max_positional_args == 2) {
294ebb6bb72SPavel Labath    result = pfunc(ToSWIGWrapper(process_sp), ToSWIGWrapper(args_impl));
29559d8dd79SMed Ismail Bennani  } else {
2969d5e37edSPavel Labath    error_string.assign("wrong number of arguments in __init__, should be 2 "
2979d5e37edSPavel Labath                        "(not including self)");
29859d8dd79SMed Ismail Bennani    Py_RETURN_NONE;
29959d8dd79SMed Ismail Bennani  }
30059d8dd79SMed Ismail Bennani
30159d8dd79SMed Ismail Bennani  if (result.IsAllocated())
30259d8dd79SMed Ismail Bennani    return result.release();
30359d8dd79SMed Ismail Bennani  Py_RETURN_NONE;
30459d8dd79SMed Ismail Bennani}
30559d8dd79SMed Ismail Bennani
3069d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
3079d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
30882de8df2SPavel Labath    const lldb_private::StructuredDataImpl &args_impl,
3099d5e37edSPavel Labath    std::string &error_string, const lldb::ThreadPlanSP &thread_plan_sp) {
3109d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
3119d5e37edSPavel Labath      !session_dictionary_name)
3126498aff2SJonas Devlieghere    Py_RETURN_NONE;
3136498aff2SJonas Devlieghere
3146498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
3156498aff2SJonas Devlieghere
3169d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
3179d5e37edSPavel Labath      session_dictionary_name);
3189d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
3199d5e37edSPavel Labath      python_class_name, dict);
3206498aff2SJonas Devlieghere
3216498aff2SJonas Devlieghere  if (!pfunc.IsAllocated()) {
3226498aff2SJonas Devlieghere    error_string.append("could not find script class: ");
3236498aff2SJonas Devlieghere    error_string.append(python_class_name);
3246498aff2SJonas Devlieghere    return nullptr;
3256498aff2SJonas Devlieghere  }
3266498aff2SJonas Devlieghere
3277f09ab08SPavel Labath  PythonObject tp_arg = ToSWIGWrapper(thread_plan_sp);
3286498aff2SJonas Devlieghere
3296498aff2SJonas Devlieghere  llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
3306498aff2SJonas Devlieghere  if (!arg_info) {
3316498aff2SJonas Devlieghere    llvm::handleAllErrors(
3326498aff2SJonas Devlieghere        arg_info.takeError(),
3339d5e37edSPavel Labath        [&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
3346498aff2SJonas Devlieghere        [&](const llvm::ErrorInfoBase &E) {
3356498aff2SJonas Devlieghere          error_string.append(E.message());
3366498aff2SJonas Devlieghere        });
3376498aff2SJonas Devlieghere    Py_RETURN_NONE;
3386498aff2SJonas Devlieghere  }
3396498aff2SJonas Devlieghere
3406498aff2SJonas Devlieghere  PythonObject result = {};
341ebb6bb72SPavel Labath  auto args_sb = std::make_unique<lldb::SBStructuredData>(args_impl);
3426498aff2SJonas Devlieghere  if (arg_info.get().max_positional_args == 2) {
34382de8df2SPavel Labath    if (args_sb->IsValid()) {
3449d5e37edSPavel Labath      error_string.assign(
3459d5e37edSPavel Labath          "args passed, but __init__ does not take an args dictionary");
3466498aff2SJonas Devlieghere      Py_RETURN_NONE;
3476498aff2SJonas Devlieghere    }
3486498aff2SJonas Devlieghere    result = pfunc(tp_arg, dict);
3496498aff2SJonas Devlieghere  } else if (arg_info.get().max_positional_args >= 3) {
350ebb6bb72SPavel Labath    result = pfunc(tp_arg, ToSWIGWrapper(std::move(args_sb)), dict);
3516498aff2SJonas Devlieghere  } else {
3529d5e37edSPavel Labath    error_string.assign("wrong number of arguments in __init__, should be 2 or "
3539d5e37edSPavel Labath                        "3 (not including self)");
3546498aff2SJonas Devlieghere    Py_RETURN_NONE;
3556498aff2SJonas Devlieghere  }
3566498aff2SJonas Devlieghere
3579d5e37edSPavel Labath  // FIXME: At this point we should check that the class we found supports all
3589d5e37edSPavel Labath  // the methods that we need.
3596498aff2SJonas Devlieghere
3606498aff2SJonas Devlieghere  if (result.IsAllocated())
3616498aff2SJonas Devlieghere    return result.release();
3626498aff2SJonas Devlieghere  Py_RETURN_NONE;
3636498aff2SJonas Devlieghere}
3646498aff2SJonas Devlieghere
3659d5e37edSPavel Labathbool lldb_private::LLDBSWIGPythonCallThreadPlan(
3669d5e37edSPavel Labath    void *implementor, const char *method_name, lldb_private::Event *event,
3679d5e37edSPavel Labath    bool &got_error) {
3686498aff2SJonas Devlieghere  got_error = false;
3696498aff2SJonas Devlieghere
3706498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(false);
3716498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
3726498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>(method_name);
3736498aff2SJonas Devlieghere
3746498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
3756498aff2SJonas Devlieghere    return false;
3766498aff2SJonas Devlieghere
3776498aff2SJonas Devlieghere  PythonObject result;
3789d5e37edSPavel Labath  if (event != nullptr) {
379*0a07c966SPavel Labath    ScopedPythonObject<SBEvent> event_arg = ToSWIGWrapper(event);
380*0a07c966SPavel Labath    result = pfunc(event_arg.obj());
3819d5e37edSPavel Labath  } else
3826498aff2SJonas Devlieghere    result = pfunc();
3836498aff2SJonas Devlieghere
3849d5e37edSPavel Labath  if (PyErr_Occurred()) {
3856498aff2SJonas Devlieghere    got_error = true;
3869d5e37edSPavel Labath    printf("Return value was neither false nor true for call to %s.\n",
3879d5e37edSPavel Labath           method_name);
3886498aff2SJonas Devlieghere    PyErr_Print();
3896498aff2SJonas Devlieghere    return false;
3906498aff2SJonas Devlieghere  }
3916498aff2SJonas Devlieghere
3926498aff2SJonas Devlieghere  if (result.get() == Py_True)
3936498aff2SJonas Devlieghere    return true;
3946498aff2SJonas Devlieghere  else if (result.get() == Py_False)
3956498aff2SJonas Devlieghere    return false;
3966498aff2SJonas Devlieghere
3976498aff2SJonas Devlieghere  // Somebody returned the wrong thing...
3986498aff2SJonas Devlieghere  got_error = true;
3996498aff2SJonas Devlieghere  printf("Wrong return value type for call to %s.\n", method_name);
4006498aff2SJonas Devlieghere  return false;
4016498aff2SJonas Devlieghere}
4026498aff2SJonas Devlieghere
4039a14adeaSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
4047f09ab08SPavel Labath    const char *python_class_name, const char *session_dictionary_name,
40582de8df2SPavel Labath    const StructuredDataImpl &args_impl,
4067f09ab08SPavel Labath    const lldb::BreakpointSP &breakpoint_sp) {
4077f09ab08SPavel Labath
4089d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
4099d5e37edSPavel Labath      !session_dictionary_name)
4106498aff2SJonas Devlieghere    Py_RETURN_NONE;
4116498aff2SJonas Devlieghere
4126498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
4136498aff2SJonas Devlieghere
4149d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
4159d5e37edSPavel Labath      session_dictionary_name);
4169d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
4179d5e37edSPavel Labath      python_class_name, dict);
4186498aff2SJonas Devlieghere
4196498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
4206498aff2SJonas Devlieghere    return nullptr;
4216498aff2SJonas Devlieghere
422ebb6bb72SPavel Labath  PythonObject result =
423ebb6bb72SPavel Labath      pfunc(ToSWIGWrapper(breakpoint_sp), ToSWIGWrapper(args_impl), dict);
4249d5e37edSPavel Labath  // FIXME: At this point we should check that the class we found supports all
4259d5e37edSPavel Labath  // the methods that we need.
4266498aff2SJonas Devlieghere
4279d5e37edSPavel Labath  if (result.IsAllocated()) {
4286498aff2SJonas Devlieghere    // Check that __callback__ is defined:
4296498aff2SJonas Devlieghere    auto callback_func = result.ResolveName<PythonCallable>("__callback__");
4306498aff2SJonas Devlieghere    if (callback_func.IsAllocated())
4316498aff2SJonas Devlieghere      return result.release();
4326498aff2SJonas Devlieghere    else
4336498aff2SJonas Devlieghere      result.release();
4346498aff2SJonas Devlieghere  }
4356498aff2SJonas Devlieghere  Py_RETURN_NONE;
4366498aff2SJonas Devlieghere}
4376498aff2SJonas Devlieghere
4389d5e37edSPavel Labathunsigned int lldb_private::LLDBSwigPythonCallBreakpointResolver(
4399d5e37edSPavel Labath    void *implementor, const char *method_name,
4409d5e37edSPavel Labath    lldb_private::SymbolContext *sym_ctx) {
4416498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(false);
4426498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
4436498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>(method_name);
4446498aff2SJonas Devlieghere
4456498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
4466498aff2SJonas Devlieghere    return 0;
4476498aff2SJonas Devlieghere
4482efc6892SPavel Labath  PythonObject result = sym_ctx ? pfunc(ToSWIGWrapper(*sym_ctx)) : pfunc();
4496498aff2SJonas Devlieghere
4509d5e37edSPavel Labath  if (PyErr_Occurred()) {
4516498aff2SJonas Devlieghere    PyErr_Print();
45252712d3fSLawrence D'Anna    PyErr_Clear();
4536498aff2SJonas Devlieghere    return 0;
4546498aff2SJonas Devlieghere  }
4556498aff2SJonas Devlieghere
4566498aff2SJonas Devlieghere  // The callback will return a bool, but we're need to also return ints
4576498aff2SJonas Devlieghere  // so we're squirrelling the bool through as an int...  And if you return
4586498aff2SJonas Devlieghere  // nothing, we'll continue.
4596498aff2SJonas Devlieghere  if (strcmp(method_name, "__callback__") == 0) {
4606498aff2SJonas Devlieghere    if (result.get() == Py_False)
4616498aff2SJonas Devlieghere      return 0;
4626498aff2SJonas Devlieghere    else
4636498aff2SJonas Devlieghere      return 1;
4646498aff2SJonas Devlieghere  }
4656498aff2SJonas Devlieghere
46652712d3fSLawrence D'Anna  long long ret_val = unwrapOrSetPythonException(As<long long>(result));
467478619cfSMuhammad Omair Javaid
46852712d3fSLawrence D'Anna  if (PyErr_Occurred()) {
46952712d3fSLawrence D'Anna    PyErr_Print();
47052712d3fSLawrence D'Anna    PyErr_Clear();
47152712d3fSLawrence D'Anna    return 0;
47252712d3fSLawrence D'Anna  }
4736498aff2SJonas Devlieghere
4746498aff2SJonas Devlieghere  return ret_val;
4756498aff2SJonas Devlieghere}
4766498aff2SJonas Devlieghere
4779d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedStopHook(
4789d5e37edSPavel Labath    lldb::TargetSP target_sp, const char *python_class_name,
4799d5e37edSPavel Labath    const char *session_dictionary_name, const StructuredDataImpl &args_impl,
4809d5e37edSPavel Labath    Status &error) {
4811b1d9815SJim Ingham  if (python_class_name == NULL || python_class_name[0] == '\0') {
4821b1d9815SJim Ingham    error.SetErrorString("Empty class name.");
4831b1d9815SJim Ingham    Py_RETURN_NONE;
4841b1d9815SJim Ingham  }
4851b1d9815SJim Ingham  if (!session_dictionary_name) {
4861b1d9815SJim Ingham    error.SetErrorString("No session dictionary");
4871b1d9815SJim Ingham    Py_RETURN_NONE;
4881b1d9815SJim Ingham  }
4891b1d9815SJim Ingham
4901b1d9815SJim Ingham  PyErr_Cleaner py_err_cleaner(true);
4911b1d9815SJim Ingham
4929d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
4931b1d9815SJim Ingham      session_dictionary_name);
4949d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
4951b1d9815SJim Ingham      python_class_name, dict);
4961b1d9815SJim Ingham
4971b1d9815SJim Ingham  if (!pfunc.IsAllocated()) {
4981b1d9815SJim Ingham    error.SetErrorStringWithFormat("Could not find class: %s.",
4991b1d9815SJim Ingham                                   python_class_name);
5001b1d9815SJim Ingham    return nullptr;
5011b1d9815SJim Ingham  }
5021b1d9815SJim Ingham
503ebb6bb72SPavel Labath  PythonObject result =
504ebb6bb72SPavel Labath      pfunc(ToSWIGWrapper(target_sp), ToSWIGWrapper(args_impl), dict);
5051b1d9815SJim Ingham
5069d5e37edSPavel Labath  if (result.IsAllocated()) {
5071b1d9815SJim Ingham    // Check that the handle_stop callback is defined:
5081b1d9815SJim Ingham    auto callback_func = result.ResolveName<PythonCallable>("handle_stop");
5091b1d9815SJim Ingham    if (callback_func.IsAllocated()) {
5101b1d9815SJim Ingham      if (auto args_info = callback_func.GetArgInfo()) {
5111b1d9815SJim Ingham        size_t num_args = (*args_info).max_positional_args;
5121b1d9815SJim Ingham        if (num_args != 2) {
5139d5e37edSPavel Labath          error.SetErrorStringWithFormat(
5149d5e37edSPavel Labath              "Wrong number of args for "
5152f95c50aSRichard Smith              "handle_stop callback, should be 2 (excluding self), got: %zu",
5161b1d9815SJim Ingham              num_args);
5171b1d9815SJim Ingham          Py_RETURN_NONE;
5181b1d9815SJim Ingham        } else
5191b1d9815SJim Ingham          return result.release();
5201b1d9815SJim Ingham      } else {
5211b1d9815SJim Ingham        error.SetErrorString("Couldn't get num arguments for handle_stop "
5221b1d9815SJim Ingham                             "callback.");
5231b1d9815SJim Ingham        Py_RETURN_NONE;
5241b1d9815SJim Ingham      }
5251b1d9815SJim Ingham      return result.release();
5269d5e37edSPavel Labath    } else {
5271b1d9815SJim Ingham      error.SetErrorStringWithFormat("Class \"%s\" is missing the required "
5281b1d9815SJim Ingham                                     "handle_stop callback.",
5291b1d9815SJim Ingham                                     python_class_name);
5301b1d9815SJim Ingham      result.release();
5311b1d9815SJim Ingham    }
5321b1d9815SJim Ingham  }
5331b1d9815SJim Ingham  Py_RETURN_NONE;
5341b1d9815SJim Ingham}
5351b1d9815SJim Ingham
5369d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonStopHookCallHandleStop(
5379d5e37edSPavel Labath    void *implementor, lldb::ExecutionContextRefSP exc_ctx_sp,
5389d5e37edSPavel Labath    lldb::StreamSP stream) {
5391b1d9815SJim Ingham  // handle_stop will return a bool with the meaning "should_stop"...
5401b1d9815SJim Ingham  // If you return nothing we'll assume we are going to stop.
5411b1d9815SJim Ingham  // Also any errors should return true, since we should stop on error.
5421b1d9815SJim Ingham
5431b1d9815SJim Ingham  PyErr_Cleaner py_err_cleaner(false);
5441b1d9815SJim Ingham  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
5451b1d9815SJim Ingham  auto pfunc = self.ResolveName<PythonCallable>("handle_stop");
5461b1d9815SJim Ingham
5471b1d9815SJim Ingham  if (!pfunc.IsAllocated())
5481b1d9815SJim Ingham    return true;
5491b1d9815SJim Ingham
5502efc6892SPavel Labath  auto *sb_stream = new lldb::SBStream();
5512efc6892SPavel Labath  PythonObject sb_stream_arg =
5522efc6892SPavel Labath      ToSWIGWrapper(std::unique_ptr<lldb::SBStream>(sb_stream));
5532efc6892SPavel Labath  PythonObject result =
5542efc6892SPavel Labath      pfunc(ToSWIGWrapper(std::move(exc_ctx_sp)), sb_stream_arg);
5551b1d9815SJim Ingham
5569d5e37edSPavel Labath  if (PyErr_Occurred()) {
5571b1d9815SJim Ingham    stream->PutCString("Python error occurred handling stop-hook.");
5581b1d9815SJim Ingham    PyErr_Print();
5591b1d9815SJim Ingham    PyErr_Clear();
5601b1d9815SJim Ingham    return true;
5611b1d9815SJim Ingham  }
5621b1d9815SJim Ingham
5631b1d9815SJim Ingham  // Now add the result to the output stream.  SBStream only
5641b1d9815SJim Ingham  // makes an internally help StreamString which I can't interpose, so I
5651b1d9815SJim Ingham  // have to copy it over here.
5662efc6892SPavel Labath  stream->PutCString(sb_stream->GetData());
5671b1d9815SJim Ingham
5681b1d9815SJim Ingham  if (result.get() == Py_False)
5691b1d9815SJim Ingham    return false;
5701b1d9815SJim Ingham  else
5711b1d9815SJim Ingham    return true;
5721b1d9815SJim Ingham}
5731b1d9815SJim Ingham
5749d5e37edSPavel Labath// wrapper that calls an optional instance member of an object taking no
5759d5e37edSPavel Labath// arguments
5769d5e37edSPavel Labathstatic PyObject *LLDBSwigPython_CallOptionalMember(
5779d5e37edSPavel Labath    PyObject * implementor, char *callee_name,
5789d5e37edSPavel Labath    PyObject *ret_if_not_found = Py_None, bool *was_found = NULL) {
5796498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(false);
5806498aff2SJonas Devlieghere
5816498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
5826498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>(callee_name);
5836498aff2SJonas Devlieghere
5849d5e37edSPavel Labath  if (!pfunc.IsAllocated()) {
5856498aff2SJonas Devlieghere    if (was_found)
5866498aff2SJonas Devlieghere      *was_found = false;
5876498aff2SJonas Devlieghere    Py_XINCREF(ret_if_not_found);
5886498aff2SJonas Devlieghere    return ret_if_not_found;
5896498aff2SJonas Devlieghere  }
5906498aff2SJonas Devlieghere
5916498aff2SJonas Devlieghere  if (was_found)
5926498aff2SJonas Devlieghere    *was_found = true;
5936498aff2SJonas Devlieghere
5946498aff2SJonas Devlieghere  PythonObject result = pfunc();
5956498aff2SJonas Devlieghere  return result.release();
5966498aff2SJonas Devlieghere}
5976498aff2SJonas Devlieghere
5989d5e37edSPavel Labathsize_t lldb_private::LLDBSwigPython_CalculateNumChildren(PyObject * implementor,
5999d5e37edSPavel Labath                                                         uint32_t max) {
6006498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, implementor);
6016498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>("num_children");
6026498aff2SJonas Devlieghere
6036498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
6046498aff2SJonas Devlieghere    return 0;
6056498aff2SJonas Devlieghere
6066498aff2SJonas Devlieghere  auto arg_info = pfunc.GetArgInfo();
6076498aff2SJonas Devlieghere  if (!arg_info) {
6086498aff2SJonas Devlieghere    llvm::consumeError(arg_info.takeError());
6096498aff2SJonas Devlieghere    return 0;
6106498aff2SJonas Devlieghere  }
6116498aff2SJonas Devlieghere
61252712d3fSLawrence D'Anna  size_t ret_val;
613478619cfSMuhammad Omair Javaid  if (arg_info.get().max_positional_args < 1)
61452712d3fSLawrence D'Anna    ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call()));
615478619cfSMuhammad Omair Javaid  else
6169d5e37edSPavel Labath    ret_val = unwrapOrSetPythonException(
6179d5e37edSPavel Labath        As<long long>(pfunc.Call(PythonInteger(max))));
618478619cfSMuhammad Omair Javaid
6199d5e37edSPavel Labath  if (PyErr_Occurred()) {
6206498aff2SJonas Devlieghere    PyErr_Print();
6216498aff2SJonas Devlieghere    PyErr_Clear();
62252712d3fSLawrence D'Anna    return 0;
6236498aff2SJonas Devlieghere  }
6246498aff2SJonas Devlieghere
6256498aff2SJonas Devlieghere  if (arg_info.get().max_positional_args < 1)
6266498aff2SJonas Devlieghere    ret_val = std::min(ret_val, static_cast<size_t>(max));
6276498aff2SJonas Devlieghere
6286498aff2SJonas Devlieghere  return ret_val;
6296498aff2SJonas Devlieghere}
6306498aff2SJonas Devlieghere
6319d5e37edSPavel LabathPyObject *lldb_private::LLDBSwigPython_GetChildAtIndex(PyObject * implementor,
6329d5e37edSPavel Labath                                                       uint32_t idx) {
6336498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
6346498aff2SJonas Devlieghere
6356498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, implementor);
6366498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index");
6376498aff2SJonas Devlieghere
6386498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
6396498aff2SJonas Devlieghere    return nullptr;
6406498aff2SJonas Devlieghere
6416498aff2SJonas Devlieghere  PythonObject result = pfunc(PythonInteger(idx));
6426498aff2SJonas Devlieghere
6436498aff2SJonas Devlieghere  if (!result.IsAllocated())
6446498aff2SJonas Devlieghere    return nullptr;
6456498aff2SJonas Devlieghere
6466498aff2SJonas Devlieghere  lldb::SBValue *sbvalue_ptr = nullptr;
6479d5e37edSPavel Labath  if (SWIG_ConvertPtr(result.get(), (void **)&sbvalue_ptr,
6489d5e37edSPavel Labath                      SWIGTYPE_p_lldb__SBValue, 0) == -1)
6496498aff2SJonas Devlieghere    return nullptr;
6506498aff2SJonas Devlieghere
6516498aff2SJonas Devlieghere  if (sbvalue_ptr == nullptr)
6526498aff2SJonas Devlieghere    return nullptr;
6536498aff2SJonas Devlieghere
6546498aff2SJonas Devlieghere  return result.release();
6556498aff2SJonas Devlieghere}
6566498aff2SJonas Devlieghere
6579d5e37edSPavel Labathint lldb_private::LLDBSwigPython_GetIndexOfChildWithName(
6589d5e37edSPavel Labath    PyObject * implementor, const char *child_name) {
6596498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
6606498aff2SJonas Devlieghere
6616498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, implementor);
6626498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>("get_child_index");
6636498aff2SJonas Devlieghere
6646498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
6656498aff2SJonas Devlieghere    return UINT32_MAX;
6666498aff2SJonas Devlieghere
66752712d3fSLawrence D'Anna  llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name));
6686498aff2SJonas Devlieghere
6699d5e37edSPavel Labath  long long retval =
6709d5e37edSPavel Labath      unwrapOrSetPythonException(As<long long>(std::move(result)));
67152712d3fSLawrence D'Anna
67252712d3fSLawrence D'Anna  if (PyErr_Occurred()) {
67352712d3fSLawrence D'Anna    PyErr_Clear(); // FIXME print this? do something else
6746498aff2SJonas Devlieghere    return UINT32_MAX;
67552712d3fSLawrence D'Anna  }
6766498aff2SJonas Devlieghere
6776498aff2SJonas Devlieghere  if (retval >= 0)
6786498aff2SJonas Devlieghere    return (uint32_t)retval;
6796498aff2SJonas Devlieghere
6806498aff2SJonas Devlieghere  return UINT32_MAX;
6816498aff2SJonas Devlieghere}
6826498aff2SJonas Devlieghere
6839d5e37edSPavel Labathbool lldb_private::LLDBSwigPython_UpdateSynthProviderInstance(PyObject *
6849d5e37edSPavel Labath                                                              implementor) {
6856498aff2SJonas Devlieghere  bool ret_val = false;
6866498aff2SJonas Devlieghere
6876498aff2SJonas Devlieghere  static char callee_name[] = "update";
6886498aff2SJonas Devlieghere
6899d5e37edSPavel Labath  PyObject *py_return =
6909d5e37edSPavel Labath      LLDBSwigPython_CallOptionalMember(implementor, callee_name);
6916498aff2SJonas Devlieghere
6926498aff2SJonas Devlieghere  if (py_return == Py_True)
6936498aff2SJonas Devlieghere    ret_val = true;
6946498aff2SJonas Devlieghere
6956498aff2SJonas Devlieghere  Py_XDECREF(py_return);
6966498aff2SJonas Devlieghere
6976498aff2SJonas Devlieghere  return ret_val;
6986498aff2SJonas Devlieghere}
6996498aff2SJonas Devlieghere
7009d5e37edSPavel Labathbool lldb_private::LLDBSwigPython_MightHaveChildrenSynthProviderInstance(
7019d5e37edSPavel Labath    PyObject * implementor) {
7026498aff2SJonas Devlieghere  bool ret_val = false;
7036498aff2SJonas Devlieghere
7046498aff2SJonas Devlieghere  static char callee_name[] = "has_children";
7056498aff2SJonas Devlieghere
7069d5e37edSPavel Labath  PyObject *py_return =
7079d5e37edSPavel Labath      LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_True);
7086498aff2SJonas Devlieghere
7096498aff2SJonas Devlieghere  if (py_return == Py_True)
7106498aff2SJonas Devlieghere    ret_val = true;
7116498aff2SJonas Devlieghere
7126498aff2SJonas Devlieghere  Py_XDECREF(py_return);
7136498aff2SJonas Devlieghere
7146498aff2SJonas Devlieghere  return ret_val;
7156498aff2SJonas Devlieghere}
7166498aff2SJonas Devlieghere
7179d5e37edSPavel LabathPyObject *lldb_private::LLDBSwigPython_GetValueSynthProviderInstance(
7189d5e37edSPavel Labath    PyObject * implementor) {
7196498aff2SJonas Devlieghere  PyObject *ret_val = nullptr;
7206498aff2SJonas Devlieghere
7216498aff2SJonas Devlieghere  static char callee_name[] = "get_value";
7226498aff2SJonas Devlieghere
7239d5e37edSPavel Labath  PyObject *py_return =
7249d5e37edSPavel Labath      LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_None);
7256498aff2SJonas Devlieghere
7266498aff2SJonas Devlieghere  if (py_return == Py_None || py_return == nullptr)
7276498aff2SJonas Devlieghere    ret_val = nullptr;
7286498aff2SJonas Devlieghere
7296498aff2SJonas Devlieghere  lldb::SBValue *sbvalue_ptr = NULL;
7306498aff2SJonas Devlieghere
7319d5e37edSPavel Labath  if (SWIG_ConvertPtr(py_return, (void **)&sbvalue_ptr,
7329d5e37edSPavel Labath                      SWIGTYPE_p_lldb__SBValue, 0) == -1)
7336498aff2SJonas Devlieghere    ret_val = nullptr;
7346498aff2SJonas Devlieghere  else if (sbvalue_ptr == NULL)
7356498aff2SJonas Devlieghere    ret_val = nullptr;
7366498aff2SJonas Devlieghere  else
7376498aff2SJonas Devlieghere    ret_val = py_return;
7386498aff2SJonas Devlieghere
7396498aff2SJonas Devlieghere  Py_XDECREF(py_return);
7406498aff2SJonas Devlieghere  return ret_val;
7416498aff2SJonas Devlieghere}
7426498aff2SJonas Devlieghere
7439d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBData(PyObject * data) {
7441f6a57c1SMed Ismail Bennani  lldb::SBData *sb_ptr = nullptr;
7451f6a57c1SMed Ismail Bennani
7469d5e37edSPavel Labath  int valid_cast =
7479d5e37edSPavel Labath      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0);
7481f6a57c1SMed Ismail Bennani
7491f6a57c1SMed Ismail Bennani  if (valid_cast == -1)
7501f6a57c1SMed Ismail Bennani    return NULL;
7511f6a57c1SMed Ismail Bennani
7521f6a57c1SMed Ismail Bennani  return sb_ptr;
7531f6a57c1SMed Ismail Bennani}
7541f6a57c1SMed Ismail Bennani
7559d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBError(PyObject * data) {
7561f6a57c1SMed Ismail Bennani  lldb::SBError *sb_ptr = nullptr;
7571f6a57c1SMed Ismail Bennani
7589d5e37edSPavel Labath  int valid_cast =
7599d5e37edSPavel Labath      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0);
7601f6a57c1SMed Ismail Bennani
7611f6a57c1SMed Ismail Bennani  if (valid_cast == -1)
7621f6a57c1SMed Ismail Bennani    return NULL;
7631f6a57c1SMed Ismail Bennani
7641f6a57c1SMed Ismail Bennani  return sb_ptr;
7651f6a57c1SMed Ismail Bennani}
7661f6a57c1SMed Ismail Bennani
7679d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBValue(PyObject * data) {
7686498aff2SJonas Devlieghere  lldb::SBValue *sb_ptr = NULL;
7696498aff2SJonas Devlieghere
7709d5e37edSPavel Labath  int valid_cast =
7719d5e37edSPavel Labath      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
7726498aff2SJonas Devlieghere
7736498aff2SJonas Devlieghere  if (valid_cast == -1)
7746498aff2SJonas Devlieghere    return NULL;
7756498aff2SJonas Devlieghere
7766498aff2SJonas Devlieghere  return sb_ptr;
7776498aff2SJonas Devlieghere}
7786498aff2SJonas Devlieghere
7799d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *
7809d5e37edSPavel Labath                                                                    data) {
781a758c9f7SMed Ismail Bennani  lldb::SBMemoryRegionInfo *sb_ptr = NULL;
782a758c9f7SMed Ismail Bennani
7839d5e37edSPavel Labath  int valid_cast = SWIG_ConvertPtr(data, (void **)&sb_ptr,
7849d5e37edSPavel Labath                                   SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0);
785a758c9f7SMed Ismail Bennani
786a758c9f7SMed Ismail Bennani  if (valid_cast == -1)
787a758c9f7SMed Ismail Bennani    return NULL;
788a758c9f7SMed Ismail Bennani
789a758c9f7SMed Ismail Bennani  return sb_ptr;
790a758c9f7SMed Ismail Bennani}
791a758c9f7SMed Ismail Bennani
7929d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonCallCommand(
7939d5e37edSPavel Labath    const char *python_function_name, const char *session_dictionary_name,
7947406d236SPavel Labath    lldb::DebuggerSP debugger, const char *args,
7956498aff2SJonas Devlieghere    lldb_private::CommandReturnObject &cmd_retobj,
7969d5e37edSPavel Labath    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
7976498aff2SJonas Devlieghere
7986498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
7999d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
8009d5e37edSPavel Labath      session_dictionary_name);
8019d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
8029d5e37edSPavel Labath      python_function_name, dict);
8036498aff2SJonas Devlieghere
8046498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
8056498aff2SJonas Devlieghere    return false;
8066498aff2SJonas Devlieghere
8076498aff2SJonas Devlieghere  auto argc = pfunc.GetArgInfo();
8086498aff2SJonas Devlieghere  if (!argc) {
8096498aff2SJonas Devlieghere    llvm::consumeError(argc.takeError());
8106498aff2SJonas Devlieghere    return false;
8116498aff2SJonas Devlieghere  }
8127406d236SPavel Labath  PythonObject debugger_arg = ToSWIGWrapper(std::move(debugger));
813*0a07c966SPavel Labath  auto cmd_retobj_arg = ToSWIGWrapper(cmd_retobj);
8146498aff2SJonas Devlieghere
8156498aff2SJonas Devlieghere  if (argc.get().max_positional_args < 5u)
816*0a07c966SPavel Labath    pfunc(debugger_arg, PythonString(args), cmd_retobj_arg.obj(), dict);
8176498aff2SJonas Devlieghere  else
8182efc6892SPavel Labath    pfunc(debugger_arg, PythonString(args),
819*0a07c966SPavel Labath          ToSWIGWrapper(std::move(exe_ctx_ref_sp)), cmd_retobj_arg.obj(), dict);
8206498aff2SJonas Devlieghere
8216498aff2SJonas Devlieghere  return true;
8226498aff2SJonas Devlieghere}
8236498aff2SJonas Devlieghere
8249d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonCallCommandObject(
8257406d236SPavel Labath    PyObject *implementor, lldb::DebuggerSP debugger, const char *args,
8266498aff2SJonas Devlieghere    lldb_private::CommandReturnObject &cmd_retobj,
8279d5e37edSPavel Labath    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
8286498aff2SJonas Devlieghere
8296498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
8306498aff2SJonas Devlieghere
8316498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, implementor);
8326498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>("__call__");
8336498aff2SJonas Devlieghere
8346498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
8356498aff2SJonas Devlieghere    return false;
8366498aff2SJonas Devlieghere
837*0a07c966SPavel Labath  auto cmd_retobj_arg = ToSWIGWrapper(cmd_retobj);
8386498aff2SJonas Devlieghere
8392efc6892SPavel Labath  pfunc(ToSWIGWrapper(std::move(debugger)), PythonString(args),
840*0a07c966SPavel Labath        ToSWIGWrapper(exe_ctx_ref_sp), cmd_retobj_arg.obj());
8416498aff2SJonas Devlieghere
8426498aff2SJonas Devlieghere  return true;
8436498aff2SJonas Devlieghere}
8446498aff2SJonas Devlieghere
8459d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPythonCreateOSPlugin(
8469d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
8479d5e37edSPavel Labath    const lldb::ProcessSP &process_sp) {
8489d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
8499d5e37edSPavel Labath      !session_dictionary_name)
8506498aff2SJonas Devlieghere    Py_RETURN_NONE;
8516498aff2SJonas Devlieghere
8526498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
8536498aff2SJonas Devlieghere
8549d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
8559d5e37edSPavel Labath      session_dictionary_name);
8569d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
8579d5e37edSPavel Labath      python_class_name, dict);
8586498aff2SJonas Devlieghere
8596498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
8606498aff2SJonas Devlieghere    Py_RETURN_NONE;
8616498aff2SJonas Devlieghere
8627f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(process_sp));
8636498aff2SJonas Devlieghere
8646498aff2SJonas Devlieghere  if (result.IsAllocated())
8656498aff2SJonas Devlieghere    return result.release();
8666498aff2SJonas Devlieghere
8676498aff2SJonas Devlieghere  Py_RETURN_NONE;
8686498aff2SJonas Devlieghere}
8696498aff2SJonas Devlieghere
8709d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CreateFrameRecognizer(
8719d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name) {
8729d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
8739d5e37edSPavel Labath      !session_dictionary_name)
8746498aff2SJonas Devlieghere    Py_RETURN_NONE;
8756498aff2SJonas Devlieghere
8766498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
8776498aff2SJonas Devlieghere
8789d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
8799d5e37edSPavel Labath      session_dictionary_name);
8809d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
8819d5e37edSPavel Labath      python_class_name, dict);
8826498aff2SJonas Devlieghere
8836498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
8846498aff2SJonas Devlieghere    Py_RETURN_NONE;
8856498aff2SJonas Devlieghere
8866498aff2SJonas Devlieghere  auto result = pfunc();
8876498aff2SJonas Devlieghere
8886498aff2SJonas Devlieghere  if (result.IsAllocated())
8896498aff2SJonas Devlieghere    return result.release();
8906498aff2SJonas Devlieghere
8916498aff2SJonas Devlieghere  Py_RETURN_NONE;
8926498aff2SJonas Devlieghere}
8936498aff2SJonas Devlieghere
8949d5e37edSPavel LabathPyObject *lldb_private::LLDBSwigPython_GetRecognizedArguments(
8959d5e37edSPavel Labath    PyObject * implementor, const lldb::StackFrameSP &frame_sp) {
8966498aff2SJonas Devlieghere  static char callee_name[] = "get_recognized_arguments";
8976498aff2SJonas Devlieghere
8987406d236SPavel Labath  PythonObject arg = ToSWIGWrapper(frame_sp);
8996498aff2SJonas Devlieghere
9006498aff2SJonas Devlieghere  PythonString str(callee_name);
9019d5e37edSPavel Labath  PyObject *result =
9026c2bf012SPavel Labath      PyObject_CallMethodObjArgs(implementor, str.get(), arg.get(), NULL);
9036498aff2SJonas Devlieghere  return result;
9046498aff2SJonas Devlieghere}
9056498aff2SJonas Devlieghere
9069d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_GetDynamicSetting(
9079d5e37edSPavel Labath    void *module, const char *setting, const lldb::TargetSP &target_sp) {
9086498aff2SJonas Devlieghere  if (!module || !setting)
9096498aff2SJonas Devlieghere    Py_RETURN_NONE;
9106498aff2SJonas Devlieghere
9116498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
9126498aff2SJonas Devlieghere  PythonObject py_module(PyRefType::Borrowed, (PyObject *)module);
9136498aff2SJonas Devlieghere  auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting");
9146498aff2SJonas Devlieghere
9156498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
9166498aff2SJonas Devlieghere    Py_RETURN_NONE;
9176498aff2SJonas Devlieghere
9187f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(target_sp), PythonString(setting));
9196498aff2SJonas Devlieghere
9206498aff2SJonas Devlieghere  return result.release();
9216498aff2SJonas Devlieghere}
9226498aff2SJonas Devlieghere
9239a14adeaSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordProcess(
9247f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
9257f09ab08SPavel Labath    const lldb::ProcessSP &process, std::string &output) {
9266498aff2SJonas Devlieghere
9279d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9289d5e37edSPavel Labath      !session_dictionary_name)
9296498aff2SJonas Devlieghere    return false;
9306498aff2SJonas Devlieghere
9316498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
9326498aff2SJonas Devlieghere
9339d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9349d5e37edSPavel Labath      session_dictionary_name);
9359d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9369d5e37edSPavel Labath      python_function_name, dict);
9376498aff2SJonas Devlieghere
9386498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
9396498aff2SJonas Devlieghere    return false;
9406498aff2SJonas Devlieghere
9417f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(process), dict);
9426498aff2SJonas Devlieghere
9436498aff2SJonas Devlieghere  output = result.Str().GetString().str();
9446498aff2SJonas Devlieghere
9456498aff2SJonas Devlieghere  return true;
9466498aff2SJonas Devlieghere}
9476498aff2SJonas Devlieghere
9487406d236SPavel Labathllvm::Optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordThread(
9499d5e37edSPavel Labath    const char *python_function_name, const char *session_dictionary_name,
9507406d236SPavel Labath    lldb::ThreadSP thread) {
9519d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9529d5e37edSPavel Labath      !session_dictionary_name)
9537406d236SPavel Labath    return llvm::None;
9546498aff2SJonas Devlieghere
9556498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
9566498aff2SJonas Devlieghere
9579d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9589d5e37edSPavel Labath      session_dictionary_name);
9599d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9609d5e37edSPavel Labath      python_function_name, dict);
9616498aff2SJonas Devlieghere
9626498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
9637406d236SPavel Labath    return llvm::None;
9646498aff2SJonas Devlieghere
9657406d236SPavel Labath  auto result = pfunc(ToSWIGWrapper(std::move(thread)), dict);
9666498aff2SJonas Devlieghere
9677406d236SPavel Labath  return result.Str().GetString().str();
9686498aff2SJonas Devlieghere}
9696498aff2SJonas Devlieghere
9709a14adeaSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordTarget(
9717f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
9727f09ab08SPavel Labath    const lldb::TargetSP &target, std::string &output) {
9736498aff2SJonas Devlieghere
9749d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9759d5e37edSPavel Labath      !session_dictionary_name)
9766498aff2SJonas Devlieghere    return false;
9776498aff2SJonas Devlieghere
9786498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
9796498aff2SJonas Devlieghere
9809d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9819d5e37edSPavel Labath      session_dictionary_name);
9829d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9839d5e37edSPavel Labath      python_function_name, dict);
9846498aff2SJonas Devlieghere
9856498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
9866498aff2SJonas Devlieghere    return false;
9876498aff2SJonas Devlieghere
9887f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(target), dict);
9896498aff2SJonas Devlieghere
9906498aff2SJonas Devlieghere  output = result.Str().GetString().str();
9916498aff2SJonas Devlieghere
9926498aff2SJonas Devlieghere  return true;
9936498aff2SJonas Devlieghere}
9946498aff2SJonas Devlieghere
9957406d236SPavel Labathllvm::Optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordFrame(
9969d5e37edSPavel Labath    const char *python_function_name, const char *session_dictionary_name,
9977406d236SPavel Labath    lldb::StackFrameSP frame) {
9989d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9999d5e37edSPavel Labath      !session_dictionary_name)
10007406d236SPavel Labath    return llvm::None;
10016498aff2SJonas Devlieghere
10026498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
10036498aff2SJonas Devlieghere
10049d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10059d5e37edSPavel Labath      session_dictionary_name);
10069d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10079d5e37edSPavel Labath      python_function_name, dict);
10086498aff2SJonas Devlieghere
10096498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
10107406d236SPavel Labath    return llvm::None;
10116498aff2SJonas Devlieghere
10127406d236SPavel Labath  auto result = pfunc(ToSWIGWrapper(std::move(frame)), dict);
10136498aff2SJonas Devlieghere
10147406d236SPavel Labath  return result.Str().GetString().str();
10156498aff2SJonas Devlieghere}
10166498aff2SJonas Devlieghere
10179a14adeaSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordValue(
10187f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
10197f09ab08SPavel Labath    const lldb::ValueObjectSP &value, std::string &output) {
10206498aff2SJonas Devlieghere
10219d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
10229d5e37edSPavel Labath      !session_dictionary_name)
10236498aff2SJonas Devlieghere    return false;
10246498aff2SJonas Devlieghere
10256498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
10266498aff2SJonas Devlieghere
10279d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10289d5e37edSPavel Labath      session_dictionary_name);
10299d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10309d5e37edSPavel Labath      python_function_name, dict);
10316498aff2SJonas Devlieghere
10326498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
10336498aff2SJonas Devlieghere    return false;
10346498aff2SJonas Devlieghere
10357f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(value), dict);
10366498aff2SJonas Devlieghere
10376498aff2SJonas Devlieghere  output = result.Str().GetString().str();
10386498aff2SJonas Devlieghere
10396498aff2SJonas Devlieghere  return true;
10406498aff2SJonas Devlieghere}
10416498aff2SJonas Devlieghere
10429d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonCallModuleInit(
10439d5e37edSPavel Labath    const char *python_module_name, const char *session_dictionary_name,
10447406d236SPavel Labath    lldb::DebuggerSP debugger) {
10456498aff2SJonas Devlieghere  std::string python_function_name_string = python_module_name;
10466498aff2SJonas Devlieghere  python_function_name_string += ".__lldb_init_module";
10476498aff2SJonas Devlieghere  const char *python_function_name = python_function_name_string.c_str();
10486498aff2SJonas Devlieghere
10496498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
10506498aff2SJonas Devlieghere
10519d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10529d5e37edSPavel Labath      session_dictionary_name);
10539d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10549d5e37edSPavel Labath      python_function_name, dict);
10556498aff2SJonas Devlieghere
10566498aff2SJonas Devlieghere  // This method is optional and need not exist.  So if we don't find it,
10576498aff2SJonas Devlieghere  // it's actually a success, not a failure.
10586498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
10596498aff2SJonas Devlieghere    return true;
10606498aff2SJonas Devlieghere
10617406d236SPavel Labath  pfunc(ToSWIGWrapper(std::move(debugger)), dict);
10626498aff2SJonas Devlieghere
10636498aff2SJonas Devlieghere  return true;
10646498aff2SJonas Devlieghere}
10656498aff2SJonas Devlieghere
10669d5e37edSPavel Labathlldb::ValueObjectSP lldb_private::LLDBSWIGPython_GetValueObjectSPFromSBValue(
10679d5e37edSPavel Labath    void *data) {
10686498aff2SJonas Devlieghere  lldb::ValueObjectSP valobj_sp;
10699d5e37edSPavel Labath  if (data) {
10706498aff2SJonas Devlieghere    lldb::SBValue *sb_ptr = (lldb::SBValue *)data;
10716498aff2SJonas Devlieghere    valobj_sp = sb_ptr->GetSP();
10726498aff2SJonas Devlieghere  }
10736498aff2SJonas Devlieghere  return valobj_sp;
10746498aff2SJonas Devlieghere}
10756498aff2SJonas Devlieghere
10766498aff2SJonas Devlieghere// For the LogOutputCallback functions
10779d5e37edSPavel Labathstatic void LLDBSwigPythonCallPythonLogOutputCallback(const char *str,
10789d5e37edSPavel Labath                                                      void *baton) {
10796498aff2SJonas Devlieghere  if (baton != Py_None) {
10806498aff2SJonas Devlieghere    SWIG_PYTHON_THREAD_BEGIN_BLOCK;
10819d5e37edSPavel Labath    PyObject *result = PyObject_CallFunction(
10829d5e37edSPavel Labath        reinterpret_cast<PyObject *>(baton), const_cast<char *>("s"), str);
10836498aff2SJonas Devlieghere    Py_XDECREF(result);
10846498aff2SJonas Devlieghere    SWIG_PYTHON_THREAD_END_BLOCK;
10856498aff2SJonas Devlieghere  }
10866498aff2SJonas Devlieghere}
10876498aff2SJonas Devlieghere%}
1088