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);
41*2efc6892SPavel Labath  PythonObject bp_loc_arg = ToSWIGWrapper(bp_loc_sp);
426498aff2SJonas Devlieghere
43*2efc6892SPavel Labath  auto result =
44*2efc6892SPavel Labath      max_positional_args < 4
45*2efc6892SPavel Labath          ? pfunc.Call(frame_arg, bp_loc_arg, dict)
46*2efc6892SPavel 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
84*2efc6892SPavel Labath  PythonObject result =
85*2efc6892SPavel 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
148*2efc6892SPavel 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) {
3796498aff2SJonas Devlieghere    lldb::SBEvent sb_event(event);
3806498aff2SJonas Devlieghere    PythonObject event_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_event));
3816498aff2SJonas Devlieghere    result = pfunc(event_arg);
3829d5e37edSPavel Labath  } else
3836498aff2SJonas Devlieghere    result = pfunc();
3846498aff2SJonas Devlieghere
3859d5e37edSPavel Labath  if (PyErr_Occurred()) {
3866498aff2SJonas Devlieghere    got_error = true;
3879d5e37edSPavel Labath    printf("Return value was neither false nor true for call to %s.\n",
3889d5e37edSPavel Labath           method_name);
3896498aff2SJonas Devlieghere    PyErr_Print();
3906498aff2SJonas Devlieghere    return false;
3916498aff2SJonas Devlieghere  }
3926498aff2SJonas Devlieghere
3936498aff2SJonas Devlieghere  if (result.get() == Py_True)
3946498aff2SJonas Devlieghere    return true;
3956498aff2SJonas Devlieghere  else if (result.get() == Py_False)
3966498aff2SJonas Devlieghere    return false;
3976498aff2SJonas Devlieghere
3986498aff2SJonas Devlieghere  // Somebody returned the wrong thing...
3996498aff2SJonas Devlieghere  got_error = true;
4006498aff2SJonas Devlieghere  printf("Wrong return value type for call to %s.\n", method_name);
4016498aff2SJonas Devlieghere  return false;
4026498aff2SJonas Devlieghere}
4036498aff2SJonas Devlieghere
4049a14adeaSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
4057f09ab08SPavel Labath    const char *python_class_name, const char *session_dictionary_name,
40682de8df2SPavel Labath    const StructuredDataImpl &args_impl,
4077f09ab08SPavel Labath    const lldb::BreakpointSP &breakpoint_sp) {
4087f09ab08SPavel Labath
4099d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
4109d5e37edSPavel Labath      !session_dictionary_name)
4116498aff2SJonas Devlieghere    Py_RETURN_NONE;
4126498aff2SJonas Devlieghere
4136498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
4146498aff2SJonas Devlieghere
4159d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
4169d5e37edSPavel Labath      session_dictionary_name);
4179d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
4189d5e37edSPavel Labath      python_class_name, dict);
4196498aff2SJonas Devlieghere
4206498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
4216498aff2SJonas Devlieghere    return nullptr;
4226498aff2SJonas Devlieghere
423ebb6bb72SPavel Labath  PythonObject result =
424ebb6bb72SPavel Labath      pfunc(ToSWIGWrapper(breakpoint_sp), ToSWIGWrapper(args_impl), dict);
4259d5e37edSPavel Labath  // FIXME: At this point we should check that the class we found supports all
4269d5e37edSPavel Labath  // the methods that we need.
4276498aff2SJonas Devlieghere
4289d5e37edSPavel Labath  if (result.IsAllocated()) {
4296498aff2SJonas Devlieghere    // Check that __callback__ is defined:
4306498aff2SJonas Devlieghere    auto callback_func = result.ResolveName<PythonCallable>("__callback__");
4316498aff2SJonas Devlieghere    if (callback_func.IsAllocated())
4326498aff2SJonas Devlieghere      return result.release();
4336498aff2SJonas Devlieghere    else
4346498aff2SJonas Devlieghere      result.release();
4356498aff2SJonas Devlieghere  }
4366498aff2SJonas Devlieghere  Py_RETURN_NONE;
4376498aff2SJonas Devlieghere}
4386498aff2SJonas Devlieghere
4399d5e37edSPavel Labathunsigned int lldb_private::LLDBSwigPythonCallBreakpointResolver(
4409d5e37edSPavel Labath    void *implementor, const char *method_name,
4419d5e37edSPavel Labath    lldb_private::SymbolContext *sym_ctx) {
4426498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(false);
4436498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
4446498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>(method_name);
4456498aff2SJonas Devlieghere
4466498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
4476498aff2SJonas Devlieghere    return 0;
4486498aff2SJonas Devlieghere
449*2efc6892SPavel Labath  PythonObject result = sym_ctx ? pfunc(ToSWIGWrapper(*sym_ctx)) : pfunc();
4506498aff2SJonas Devlieghere
4519d5e37edSPavel Labath  if (PyErr_Occurred()) {
4526498aff2SJonas Devlieghere    PyErr_Print();
45352712d3fSLawrence D'Anna    PyErr_Clear();
4546498aff2SJonas Devlieghere    return 0;
4556498aff2SJonas Devlieghere  }
4566498aff2SJonas Devlieghere
4576498aff2SJonas Devlieghere  // The callback will return a bool, but we're need to also return ints
4586498aff2SJonas Devlieghere  // so we're squirrelling the bool through as an int...  And if you return
4596498aff2SJonas Devlieghere  // nothing, we'll continue.
4606498aff2SJonas Devlieghere  if (strcmp(method_name, "__callback__") == 0) {
4616498aff2SJonas Devlieghere    if (result.get() == Py_False)
4626498aff2SJonas Devlieghere      return 0;
4636498aff2SJonas Devlieghere    else
4646498aff2SJonas Devlieghere      return 1;
4656498aff2SJonas Devlieghere  }
4666498aff2SJonas Devlieghere
46752712d3fSLawrence D'Anna  long long ret_val = unwrapOrSetPythonException(As<long long>(result));
468478619cfSMuhammad Omair Javaid
46952712d3fSLawrence D'Anna  if (PyErr_Occurred()) {
47052712d3fSLawrence D'Anna    PyErr_Print();
47152712d3fSLawrence D'Anna    PyErr_Clear();
47252712d3fSLawrence D'Anna    return 0;
47352712d3fSLawrence D'Anna  }
4746498aff2SJonas Devlieghere
4756498aff2SJonas Devlieghere  return ret_val;
4766498aff2SJonas Devlieghere}
4776498aff2SJonas Devlieghere
4789d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedStopHook(
4799d5e37edSPavel Labath    lldb::TargetSP target_sp, const char *python_class_name,
4809d5e37edSPavel Labath    const char *session_dictionary_name, const StructuredDataImpl &args_impl,
4819d5e37edSPavel Labath    Status &error) {
4821b1d9815SJim Ingham  if (python_class_name == NULL || python_class_name[0] == '\0') {
4831b1d9815SJim Ingham    error.SetErrorString("Empty class name.");
4841b1d9815SJim Ingham    Py_RETURN_NONE;
4851b1d9815SJim Ingham  }
4861b1d9815SJim Ingham  if (!session_dictionary_name) {
4871b1d9815SJim Ingham    error.SetErrorString("No session dictionary");
4881b1d9815SJim Ingham    Py_RETURN_NONE;
4891b1d9815SJim Ingham  }
4901b1d9815SJim Ingham
4911b1d9815SJim Ingham  PyErr_Cleaner py_err_cleaner(true);
4921b1d9815SJim Ingham
4939d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
4941b1d9815SJim Ingham      session_dictionary_name);
4959d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
4961b1d9815SJim Ingham      python_class_name, dict);
4971b1d9815SJim Ingham
4981b1d9815SJim Ingham  if (!pfunc.IsAllocated()) {
4991b1d9815SJim Ingham    error.SetErrorStringWithFormat("Could not find class: %s.",
5001b1d9815SJim Ingham                                   python_class_name);
5011b1d9815SJim Ingham    return nullptr;
5021b1d9815SJim Ingham  }
5031b1d9815SJim Ingham
504ebb6bb72SPavel Labath  PythonObject result =
505ebb6bb72SPavel Labath      pfunc(ToSWIGWrapper(target_sp), ToSWIGWrapper(args_impl), dict);
5061b1d9815SJim Ingham
5079d5e37edSPavel Labath  if (result.IsAllocated()) {
5081b1d9815SJim Ingham    // Check that the handle_stop callback is defined:
5091b1d9815SJim Ingham    auto callback_func = result.ResolveName<PythonCallable>("handle_stop");
5101b1d9815SJim Ingham    if (callback_func.IsAllocated()) {
5111b1d9815SJim Ingham      if (auto args_info = callback_func.GetArgInfo()) {
5121b1d9815SJim Ingham        size_t num_args = (*args_info).max_positional_args;
5131b1d9815SJim Ingham        if (num_args != 2) {
5149d5e37edSPavel Labath          error.SetErrorStringWithFormat(
5159d5e37edSPavel Labath              "Wrong number of args for "
5162f95c50aSRichard Smith              "handle_stop callback, should be 2 (excluding self), got: %zu",
5171b1d9815SJim Ingham              num_args);
5181b1d9815SJim Ingham          Py_RETURN_NONE;
5191b1d9815SJim Ingham        } else
5201b1d9815SJim Ingham          return result.release();
5211b1d9815SJim Ingham      } else {
5221b1d9815SJim Ingham        error.SetErrorString("Couldn't get num arguments for handle_stop "
5231b1d9815SJim Ingham                             "callback.");
5241b1d9815SJim Ingham        Py_RETURN_NONE;
5251b1d9815SJim Ingham      }
5261b1d9815SJim Ingham      return result.release();
5279d5e37edSPavel Labath    } else {
5281b1d9815SJim Ingham      error.SetErrorStringWithFormat("Class \"%s\" is missing the required "
5291b1d9815SJim Ingham                                     "handle_stop callback.",
5301b1d9815SJim Ingham                                     python_class_name);
5311b1d9815SJim Ingham      result.release();
5321b1d9815SJim Ingham    }
5331b1d9815SJim Ingham  }
5341b1d9815SJim Ingham  Py_RETURN_NONE;
5351b1d9815SJim Ingham}
5361b1d9815SJim Ingham
5379d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonStopHookCallHandleStop(
5389d5e37edSPavel Labath    void *implementor, lldb::ExecutionContextRefSP exc_ctx_sp,
5399d5e37edSPavel Labath    lldb::StreamSP stream) {
5401b1d9815SJim Ingham  // handle_stop will return a bool with the meaning "should_stop"...
5411b1d9815SJim Ingham  // If you return nothing we'll assume we are going to stop.
5421b1d9815SJim Ingham  // Also any errors should return true, since we should stop on error.
5431b1d9815SJim Ingham
5441b1d9815SJim Ingham  PyErr_Cleaner py_err_cleaner(false);
5451b1d9815SJim Ingham  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
5461b1d9815SJim Ingham  auto pfunc = self.ResolveName<PythonCallable>("handle_stop");
5471b1d9815SJim Ingham
5481b1d9815SJim Ingham  if (!pfunc.IsAllocated())
5491b1d9815SJim Ingham    return true;
5501b1d9815SJim Ingham
551*2efc6892SPavel Labath  auto *sb_stream = new lldb::SBStream();
552*2efc6892SPavel Labath  PythonObject sb_stream_arg =
553*2efc6892SPavel Labath      ToSWIGWrapper(std::unique_ptr<lldb::SBStream>(sb_stream));
554*2efc6892SPavel Labath  PythonObject result =
555*2efc6892SPavel Labath      pfunc(ToSWIGWrapper(std::move(exc_ctx_sp)), sb_stream_arg);
5561b1d9815SJim Ingham
5579d5e37edSPavel Labath  if (PyErr_Occurred()) {
5581b1d9815SJim Ingham    stream->PutCString("Python error occurred handling stop-hook.");
5591b1d9815SJim Ingham    PyErr_Print();
5601b1d9815SJim Ingham    PyErr_Clear();
5611b1d9815SJim Ingham    return true;
5621b1d9815SJim Ingham  }
5631b1d9815SJim Ingham
5641b1d9815SJim Ingham  // Now add the result to the output stream.  SBStream only
5651b1d9815SJim Ingham  // makes an internally help StreamString which I can't interpose, so I
5661b1d9815SJim Ingham  // have to copy it over here.
567*2efc6892SPavel Labath  stream->PutCString(sb_stream->GetData());
5681b1d9815SJim Ingham
5691b1d9815SJim Ingham  if (result.get() == Py_False)
5701b1d9815SJim Ingham    return false;
5711b1d9815SJim Ingham  else
5721b1d9815SJim Ingham    return true;
5731b1d9815SJim Ingham}
5741b1d9815SJim Ingham
5759d5e37edSPavel Labath// wrapper that calls an optional instance member of an object taking no
5769d5e37edSPavel Labath// arguments
5779d5e37edSPavel Labathstatic PyObject *LLDBSwigPython_CallOptionalMember(
5789d5e37edSPavel Labath    PyObject * implementor, char *callee_name,
5799d5e37edSPavel Labath    PyObject *ret_if_not_found = Py_None, bool *was_found = NULL) {
5806498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(false);
5816498aff2SJonas Devlieghere
5826498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
5836498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>(callee_name);
5846498aff2SJonas Devlieghere
5859d5e37edSPavel Labath  if (!pfunc.IsAllocated()) {
5866498aff2SJonas Devlieghere    if (was_found)
5876498aff2SJonas Devlieghere      *was_found = false;
5886498aff2SJonas Devlieghere    Py_XINCREF(ret_if_not_found);
5896498aff2SJonas Devlieghere    return ret_if_not_found;
5906498aff2SJonas Devlieghere  }
5916498aff2SJonas Devlieghere
5926498aff2SJonas Devlieghere  if (was_found)
5936498aff2SJonas Devlieghere    *was_found = true;
5946498aff2SJonas Devlieghere
5956498aff2SJonas Devlieghere  PythonObject result = pfunc();
5966498aff2SJonas Devlieghere  return result.release();
5976498aff2SJonas Devlieghere}
5986498aff2SJonas Devlieghere
5999d5e37edSPavel Labathsize_t lldb_private::LLDBSwigPython_CalculateNumChildren(PyObject * implementor,
6009d5e37edSPavel Labath                                                         uint32_t max) {
6016498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, implementor);
6026498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>("num_children");
6036498aff2SJonas Devlieghere
6046498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
6056498aff2SJonas Devlieghere    return 0;
6066498aff2SJonas Devlieghere
6076498aff2SJonas Devlieghere  auto arg_info = pfunc.GetArgInfo();
6086498aff2SJonas Devlieghere  if (!arg_info) {
6096498aff2SJonas Devlieghere    llvm::consumeError(arg_info.takeError());
6106498aff2SJonas Devlieghere    return 0;
6116498aff2SJonas Devlieghere  }
6126498aff2SJonas Devlieghere
61352712d3fSLawrence D'Anna  size_t ret_val;
614478619cfSMuhammad Omair Javaid  if (arg_info.get().max_positional_args < 1)
61552712d3fSLawrence D'Anna    ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call()));
616478619cfSMuhammad Omair Javaid  else
6179d5e37edSPavel Labath    ret_val = unwrapOrSetPythonException(
6189d5e37edSPavel Labath        As<long long>(pfunc.Call(PythonInteger(max))));
619478619cfSMuhammad Omair Javaid
6209d5e37edSPavel Labath  if (PyErr_Occurred()) {
6216498aff2SJonas Devlieghere    PyErr_Print();
6226498aff2SJonas Devlieghere    PyErr_Clear();
62352712d3fSLawrence D'Anna    return 0;
6246498aff2SJonas Devlieghere  }
6256498aff2SJonas Devlieghere
6266498aff2SJonas Devlieghere  if (arg_info.get().max_positional_args < 1)
6276498aff2SJonas Devlieghere    ret_val = std::min(ret_val, static_cast<size_t>(max));
6286498aff2SJonas Devlieghere
6296498aff2SJonas Devlieghere  return ret_val;
6306498aff2SJonas Devlieghere}
6316498aff2SJonas Devlieghere
6329d5e37edSPavel LabathPyObject *lldb_private::LLDBSwigPython_GetChildAtIndex(PyObject * implementor,
6339d5e37edSPavel Labath                                                       uint32_t idx) {
6346498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
6356498aff2SJonas Devlieghere
6366498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, implementor);
6376498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index");
6386498aff2SJonas Devlieghere
6396498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
6406498aff2SJonas Devlieghere    return nullptr;
6416498aff2SJonas Devlieghere
6426498aff2SJonas Devlieghere  PythonObject result = pfunc(PythonInteger(idx));
6436498aff2SJonas Devlieghere
6446498aff2SJonas Devlieghere  if (!result.IsAllocated())
6456498aff2SJonas Devlieghere    return nullptr;
6466498aff2SJonas Devlieghere
6476498aff2SJonas Devlieghere  lldb::SBValue *sbvalue_ptr = nullptr;
6489d5e37edSPavel Labath  if (SWIG_ConvertPtr(result.get(), (void **)&sbvalue_ptr,
6499d5e37edSPavel Labath                      SWIGTYPE_p_lldb__SBValue, 0) == -1)
6506498aff2SJonas Devlieghere    return nullptr;
6516498aff2SJonas Devlieghere
6526498aff2SJonas Devlieghere  if (sbvalue_ptr == nullptr)
6536498aff2SJonas Devlieghere    return nullptr;
6546498aff2SJonas Devlieghere
6556498aff2SJonas Devlieghere  return result.release();
6566498aff2SJonas Devlieghere}
6576498aff2SJonas Devlieghere
6589d5e37edSPavel Labathint lldb_private::LLDBSwigPython_GetIndexOfChildWithName(
6599d5e37edSPavel Labath    PyObject * implementor, const char *child_name) {
6606498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
6616498aff2SJonas Devlieghere
6626498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, implementor);
6636498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>("get_child_index");
6646498aff2SJonas Devlieghere
6656498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
6666498aff2SJonas Devlieghere    return UINT32_MAX;
6676498aff2SJonas Devlieghere
66852712d3fSLawrence D'Anna  llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name));
6696498aff2SJonas Devlieghere
6709d5e37edSPavel Labath  long long retval =
6719d5e37edSPavel Labath      unwrapOrSetPythonException(As<long long>(std::move(result)));
67252712d3fSLawrence D'Anna
67352712d3fSLawrence D'Anna  if (PyErr_Occurred()) {
67452712d3fSLawrence D'Anna    PyErr_Clear(); // FIXME print this? do something else
6756498aff2SJonas Devlieghere    return UINT32_MAX;
67652712d3fSLawrence D'Anna  }
6776498aff2SJonas Devlieghere
6786498aff2SJonas Devlieghere  if (retval >= 0)
6796498aff2SJonas Devlieghere    return (uint32_t)retval;
6806498aff2SJonas Devlieghere
6816498aff2SJonas Devlieghere  return UINT32_MAX;
6826498aff2SJonas Devlieghere}
6836498aff2SJonas Devlieghere
6849d5e37edSPavel Labathbool lldb_private::LLDBSwigPython_UpdateSynthProviderInstance(PyObject *
6859d5e37edSPavel Labath                                                              implementor) {
6866498aff2SJonas Devlieghere  bool ret_val = false;
6876498aff2SJonas Devlieghere
6886498aff2SJonas Devlieghere  static char callee_name[] = "update";
6896498aff2SJonas Devlieghere
6909d5e37edSPavel Labath  PyObject *py_return =
6919d5e37edSPavel Labath      LLDBSwigPython_CallOptionalMember(implementor, callee_name);
6926498aff2SJonas Devlieghere
6936498aff2SJonas Devlieghere  if (py_return == Py_True)
6946498aff2SJonas Devlieghere    ret_val = true;
6956498aff2SJonas Devlieghere
6966498aff2SJonas Devlieghere  Py_XDECREF(py_return);
6976498aff2SJonas Devlieghere
6986498aff2SJonas Devlieghere  return ret_val;
6996498aff2SJonas Devlieghere}
7006498aff2SJonas Devlieghere
7019d5e37edSPavel Labathbool lldb_private::LLDBSwigPython_MightHaveChildrenSynthProviderInstance(
7029d5e37edSPavel Labath    PyObject * implementor) {
7036498aff2SJonas Devlieghere  bool ret_val = false;
7046498aff2SJonas Devlieghere
7056498aff2SJonas Devlieghere  static char callee_name[] = "has_children";
7066498aff2SJonas Devlieghere
7079d5e37edSPavel Labath  PyObject *py_return =
7089d5e37edSPavel Labath      LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_True);
7096498aff2SJonas Devlieghere
7106498aff2SJonas Devlieghere  if (py_return == Py_True)
7116498aff2SJonas Devlieghere    ret_val = true;
7126498aff2SJonas Devlieghere
7136498aff2SJonas Devlieghere  Py_XDECREF(py_return);
7146498aff2SJonas Devlieghere
7156498aff2SJonas Devlieghere  return ret_val;
7166498aff2SJonas Devlieghere}
7176498aff2SJonas Devlieghere
7189d5e37edSPavel LabathPyObject *lldb_private::LLDBSwigPython_GetValueSynthProviderInstance(
7199d5e37edSPavel Labath    PyObject * implementor) {
7206498aff2SJonas Devlieghere  PyObject *ret_val = nullptr;
7216498aff2SJonas Devlieghere
7226498aff2SJonas Devlieghere  static char callee_name[] = "get_value";
7236498aff2SJonas Devlieghere
7249d5e37edSPavel Labath  PyObject *py_return =
7259d5e37edSPavel Labath      LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_None);
7266498aff2SJonas Devlieghere
7276498aff2SJonas Devlieghere  if (py_return == Py_None || py_return == nullptr)
7286498aff2SJonas Devlieghere    ret_val = nullptr;
7296498aff2SJonas Devlieghere
7306498aff2SJonas Devlieghere  lldb::SBValue *sbvalue_ptr = NULL;
7316498aff2SJonas Devlieghere
7329d5e37edSPavel Labath  if (SWIG_ConvertPtr(py_return, (void **)&sbvalue_ptr,
7339d5e37edSPavel Labath                      SWIGTYPE_p_lldb__SBValue, 0) == -1)
7346498aff2SJonas Devlieghere    ret_val = nullptr;
7356498aff2SJonas Devlieghere  else if (sbvalue_ptr == NULL)
7366498aff2SJonas Devlieghere    ret_val = nullptr;
7376498aff2SJonas Devlieghere  else
7386498aff2SJonas Devlieghere    ret_val = py_return;
7396498aff2SJonas Devlieghere
7406498aff2SJonas Devlieghere  Py_XDECREF(py_return);
7416498aff2SJonas Devlieghere  return ret_val;
7426498aff2SJonas Devlieghere}
7436498aff2SJonas Devlieghere
7449d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBData(PyObject * data) {
7451f6a57c1SMed Ismail Bennani  lldb::SBData *sb_ptr = nullptr;
7461f6a57c1SMed Ismail Bennani
7479d5e37edSPavel Labath  int valid_cast =
7489d5e37edSPavel Labath      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0);
7491f6a57c1SMed Ismail Bennani
7501f6a57c1SMed Ismail Bennani  if (valid_cast == -1)
7511f6a57c1SMed Ismail Bennani    return NULL;
7521f6a57c1SMed Ismail Bennani
7531f6a57c1SMed Ismail Bennani  return sb_ptr;
7541f6a57c1SMed Ismail Bennani}
7551f6a57c1SMed Ismail Bennani
7569d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBError(PyObject * data) {
7571f6a57c1SMed Ismail Bennani  lldb::SBError *sb_ptr = nullptr;
7581f6a57c1SMed Ismail Bennani
7599d5e37edSPavel Labath  int valid_cast =
7609d5e37edSPavel Labath      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0);
7611f6a57c1SMed Ismail Bennani
7621f6a57c1SMed Ismail Bennani  if (valid_cast == -1)
7631f6a57c1SMed Ismail Bennani    return NULL;
7641f6a57c1SMed Ismail Bennani
7651f6a57c1SMed Ismail Bennani  return sb_ptr;
7661f6a57c1SMed Ismail Bennani}
7671f6a57c1SMed Ismail Bennani
7689d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBValue(PyObject * data) {
7696498aff2SJonas Devlieghere  lldb::SBValue *sb_ptr = NULL;
7706498aff2SJonas Devlieghere
7719d5e37edSPavel Labath  int valid_cast =
7729d5e37edSPavel Labath      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
7736498aff2SJonas Devlieghere
7746498aff2SJonas Devlieghere  if (valid_cast == -1)
7756498aff2SJonas Devlieghere    return NULL;
7766498aff2SJonas Devlieghere
7776498aff2SJonas Devlieghere  return sb_ptr;
7786498aff2SJonas Devlieghere}
7796498aff2SJonas Devlieghere
7809d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *
7819d5e37edSPavel Labath                                                                    data) {
782a758c9f7SMed Ismail Bennani  lldb::SBMemoryRegionInfo *sb_ptr = NULL;
783a758c9f7SMed Ismail Bennani
7849d5e37edSPavel Labath  int valid_cast = SWIG_ConvertPtr(data, (void **)&sb_ptr,
7859d5e37edSPavel Labath                                   SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0);
786a758c9f7SMed Ismail Bennani
787a758c9f7SMed Ismail Bennani  if (valid_cast == -1)
788a758c9f7SMed Ismail Bennani    return NULL;
789a758c9f7SMed Ismail Bennani
790a758c9f7SMed Ismail Bennani  return sb_ptr;
791a758c9f7SMed Ismail Bennani}
792a758c9f7SMed Ismail Bennani
7939d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonCallCommand(
7949d5e37edSPavel Labath    const char *python_function_name, const char *session_dictionary_name,
7957406d236SPavel Labath    lldb::DebuggerSP debugger, const char *args,
7966498aff2SJonas Devlieghere    lldb_private::CommandReturnObject &cmd_retobj,
7979d5e37edSPavel Labath    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
7986498aff2SJonas Devlieghere  lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
7996498aff2SJonas Devlieghere
8006498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
8019d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
8029d5e37edSPavel Labath      session_dictionary_name);
8039d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
8049d5e37edSPavel Labath      python_function_name, dict);
8056498aff2SJonas Devlieghere
8066498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
8076498aff2SJonas Devlieghere    return false;
8086498aff2SJonas Devlieghere
8096498aff2SJonas Devlieghere  auto argc = pfunc.GetArgInfo();
8106498aff2SJonas Devlieghere  if (!argc) {
8116498aff2SJonas Devlieghere    llvm::consumeError(argc.takeError());
8126498aff2SJonas Devlieghere    return false;
8136498aff2SJonas Devlieghere  }
8147406d236SPavel Labath  PythonObject debugger_arg = ToSWIGWrapper(std::move(debugger));
8159d5e37edSPavel Labath  PythonObject cmd_retobj_arg(PyRefType::Owned,
8169d5e37edSPavel Labath                              SBTypeToSWIGWrapper(cmd_retobj_sb));
8176498aff2SJonas Devlieghere
8186498aff2SJonas Devlieghere  if (argc.get().max_positional_args < 5u)
8196498aff2SJonas Devlieghere    pfunc(debugger_arg, PythonString(args), cmd_retobj_arg, dict);
8206498aff2SJonas Devlieghere  else
821*2efc6892SPavel Labath    pfunc(debugger_arg, PythonString(args),
822*2efc6892SPavel Labath          ToSWIGWrapper(std::move(exe_ctx_ref_sp)), cmd_retobj_arg, dict);
8236498aff2SJonas Devlieghere
8246498aff2SJonas Devlieghere  return true;
8256498aff2SJonas Devlieghere}
8266498aff2SJonas Devlieghere
8279d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonCallCommandObject(
8287406d236SPavel Labath    PyObject *implementor, lldb::DebuggerSP debugger, const char *args,
8296498aff2SJonas Devlieghere    lldb_private::CommandReturnObject &cmd_retobj,
8309d5e37edSPavel Labath    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
8316498aff2SJonas Devlieghere  lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
8326498aff2SJonas Devlieghere
8336498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
8346498aff2SJonas Devlieghere
8356498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, implementor);
8366498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>("__call__");
8376498aff2SJonas Devlieghere
8386498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
8396498aff2SJonas Devlieghere    return false;
8406498aff2SJonas Devlieghere
8419d5e37edSPavel Labath  PythonObject cmd_retobj_arg(PyRefType::Owned,
8429d5e37edSPavel Labath                              SBTypeToSWIGWrapper(cmd_retobj_sb));
8436498aff2SJonas Devlieghere
844*2efc6892SPavel Labath  pfunc(ToSWIGWrapper(std::move(debugger)), PythonString(args),
845*2efc6892SPavel Labath        ToSWIGWrapper(exe_ctx_ref_sp), cmd_retobj_arg);
8466498aff2SJonas Devlieghere
8476498aff2SJonas Devlieghere  return true;
8486498aff2SJonas Devlieghere}
8496498aff2SJonas Devlieghere
8509d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPythonCreateOSPlugin(
8519d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
8529d5e37edSPavel Labath    const lldb::ProcessSP &process_sp) {
8539d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
8549d5e37edSPavel Labath      !session_dictionary_name)
8556498aff2SJonas Devlieghere    Py_RETURN_NONE;
8566498aff2SJonas Devlieghere
8576498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
8586498aff2SJonas Devlieghere
8599d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
8609d5e37edSPavel Labath      session_dictionary_name);
8619d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
8629d5e37edSPavel Labath      python_class_name, dict);
8636498aff2SJonas Devlieghere
8646498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
8656498aff2SJonas Devlieghere    Py_RETURN_NONE;
8666498aff2SJonas Devlieghere
8677f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(process_sp));
8686498aff2SJonas Devlieghere
8696498aff2SJonas Devlieghere  if (result.IsAllocated())
8706498aff2SJonas Devlieghere    return result.release();
8716498aff2SJonas Devlieghere
8726498aff2SJonas Devlieghere  Py_RETURN_NONE;
8736498aff2SJonas Devlieghere}
8746498aff2SJonas Devlieghere
8759d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CreateFrameRecognizer(
8769d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name) {
8779d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
8789d5e37edSPavel Labath      !session_dictionary_name)
8796498aff2SJonas Devlieghere    Py_RETURN_NONE;
8806498aff2SJonas Devlieghere
8816498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
8826498aff2SJonas Devlieghere
8839d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
8849d5e37edSPavel Labath      session_dictionary_name);
8859d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
8869d5e37edSPavel Labath      python_class_name, dict);
8876498aff2SJonas Devlieghere
8886498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
8896498aff2SJonas Devlieghere    Py_RETURN_NONE;
8906498aff2SJonas Devlieghere
8916498aff2SJonas Devlieghere  auto result = pfunc();
8926498aff2SJonas Devlieghere
8936498aff2SJonas Devlieghere  if (result.IsAllocated())
8946498aff2SJonas Devlieghere    return result.release();
8956498aff2SJonas Devlieghere
8966498aff2SJonas Devlieghere  Py_RETURN_NONE;
8976498aff2SJonas Devlieghere}
8986498aff2SJonas Devlieghere
8999d5e37edSPavel LabathPyObject *lldb_private::LLDBSwigPython_GetRecognizedArguments(
9009d5e37edSPavel Labath    PyObject * implementor, const lldb::StackFrameSP &frame_sp) {
9016498aff2SJonas Devlieghere  static char callee_name[] = "get_recognized_arguments";
9026498aff2SJonas Devlieghere
9037406d236SPavel Labath  PythonObject arg = ToSWIGWrapper(frame_sp);
9046498aff2SJonas Devlieghere
9056498aff2SJonas Devlieghere  PythonString str(callee_name);
9069d5e37edSPavel Labath  PyObject *result =
9076c2bf012SPavel Labath      PyObject_CallMethodObjArgs(implementor, str.get(), arg.get(), NULL);
9086498aff2SJonas Devlieghere  return result;
9096498aff2SJonas Devlieghere}
9106498aff2SJonas Devlieghere
9119d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_GetDynamicSetting(
9129d5e37edSPavel Labath    void *module, const char *setting, const lldb::TargetSP &target_sp) {
9136498aff2SJonas Devlieghere  if (!module || !setting)
9146498aff2SJonas Devlieghere    Py_RETURN_NONE;
9156498aff2SJonas Devlieghere
9166498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
9176498aff2SJonas Devlieghere  PythonObject py_module(PyRefType::Borrowed, (PyObject *)module);
9186498aff2SJonas Devlieghere  auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting");
9196498aff2SJonas Devlieghere
9206498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
9216498aff2SJonas Devlieghere    Py_RETURN_NONE;
9226498aff2SJonas Devlieghere
9237f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(target_sp), PythonString(setting));
9246498aff2SJonas Devlieghere
9256498aff2SJonas Devlieghere  return result.release();
9266498aff2SJonas Devlieghere}
9276498aff2SJonas Devlieghere
9289a14adeaSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordProcess(
9297f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
9307f09ab08SPavel Labath    const lldb::ProcessSP &process, std::string &output) {
9316498aff2SJonas Devlieghere
9329d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9339d5e37edSPavel Labath      !session_dictionary_name)
9346498aff2SJonas Devlieghere    return false;
9356498aff2SJonas Devlieghere
9366498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
9376498aff2SJonas Devlieghere
9389d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9399d5e37edSPavel Labath      session_dictionary_name);
9409d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9419d5e37edSPavel Labath      python_function_name, dict);
9426498aff2SJonas Devlieghere
9436498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
9446498aff2SJonas Devlieghere    return false;
9456498aff2SJonas Devlieghere
9467f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(process), dict);
9476498aff2SJonas Devlieghere
9486498aff2SJonas Devlieghere  output = result.Str().GetString().str();
9496498aff2SJonas Devlieghere
9506498aff2SJonas Devlieghere  return true;
9516498aff2SJonas Devlieghere}
9526498aff2SJonas Devlieghere
9537406d236SPavel Labathllvm::Optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordThread(
9549d5e37edSPavel Labath    const char *python_function_name, const char *session_dictionary_name,
9557406d236SPavel Labath    lldb::ThreadSP thread) {
9569d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9579d5e37edSPavel Labath      !session_dictionary_name)
9587406d236SPavel Labath    return llvm::None;
9596498aff2SJonas Devlieghere
9606498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
9616498aff2SJonas Devlieghere
9629d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9639d5e37edSPavel Labath      session_dictionary_name);
9649d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9659d5e37edSPavel Labath      python_function_name, dict);
9666498aff2SJonas Devlieghere
9676498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
9687406d236SPavel Labath    return llvm::None;
9696498aff2SJonas Devlieghere
9707406d236SPavel Labath  auto result = pfunc(ToSWIGWrapper(std::move(thread)), dict);
9716498aff2SJonas Devlieghere
9727406d236SPavel Labath  return result.Str().GetString().str();
9736498aff2SJonas Devlieghere}
9746498aff2SJonas Devlieghere
9759a14adeaSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordTarget(
9767f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
9777f09ab08SPavel Labath    const lldb::TargetSP &target, std::string &output) {
9786498aff2SJonas Devlieghere
9799d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9809d5e37edSPavel Labath      !session_dictionary_name)
9816498aff2SJonas Devlieghere    return false;
9826498aff2SJonas Devlieghere
9836498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
9846498aff2SJonas Devlieghere
9859d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9869d5e37edSPavel Labath      session_dictionary_name);
9879d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9889d5e37edSPavel Labath      python_function_name, dict);
9896498aff2SJonas Devlieghere
9906498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
9916498aff2SJonas Devlieghere    return false;
9926498aff2SJonas Devlieghere
9937f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(target), dict);
9946498aff2SJonas Devlieghere
9956498aff2SJonas Devlieghere  output = result.Str().GetString().str();
9966498aff2SJonas Devlieghere
9976498aff2SJonas Devlieghere  return true;
9986498aff2SJonas Devlieghere}
9996498aff2SJonas Devlieghere
10007406d236SPavel Labathllvm::Optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordFrame(
10019d5e37edSPavel Labath    const char *python_function_name, const char *session_dictionary_name,
10027406d236SPavel Labath    lldb::StackFrameSP frame) {
10039d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
10049d5e37edSPavel Labath      !session_dictionary_name)
10057406d236SPavel Labath    return llvm::None;
10066498aff2SJonas Devlieghere
10076498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
10086498aff2SJonas Devlieghere
10099d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10109d5e37edSPavel Labath      session_dictionary_name);
10119d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10129d5e37edSPavel Labath      python_function_name, dict);
10136498aff2SJonas Devlieghere
10146498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
10157406d236SPavel Labath    return llvm::None;
10166498aff2SJonas Devlieghere
10177406d236SPavel Labath  auto result = pfunc(ToSWIGWrapper(std::move(frame)), dict);
10186498aff2SJonas Devlieghere
10197406d236SPavel Labath  return result.Str().GetString().str();
10206498aff2SJonas Devlieghere}
10216498aff2SJonas Devlieghere
10229a14adeaSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordValue(
10237f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
10247f09ab08SPavel Labath    const lldb::ValueObjectSP &value, std::string &output) {
10256498aff2SJonas Devlieghere
10269d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
10279d5e37edSPavel Labath      !session_dictionary_name)
10286498aff2SJonas Devlieghere    return false;
10296498aff2SJonas Devlieghere
10306498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
10316498aff2SJonas Devlieghere
10329d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10339d5e37edSPavel Labath      session_dictionary_name);
10349d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10359d5e37edSPavel Labath      python_function_name, dict);
10366498aff2SJonas Devlieghere
10376498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
10386498aff2SJonas Devlieghere    return false;
10396498aff2SJonas Devlieghere
10407f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(value), dict);
10416498aff2SJonas Devlieghere
10426498aff2SJonas Devlieghere  output = result.Str().GetString().str();
10436498aff2SJonas Devlieghere
10446498aff2SJonas Devlieghere  return true;
10456498aff2SJonas Devlieghere}
10466498aff2SJonas Devlieghere
10479d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonCallModuleInit(
10489d5e37edSPavel Labath    const char *python_module_name, const char *session_dictionary_name,
10497406d236SPavel Labath    lldb::DebuggerSP debugger) {
10506498aff2SJonas Devlieghere  std::string python_function_name_string = python_module_name;
10516498aff2SJonas Devlieghere  python_function_name_string += ".__lldb_init_module";
10526498aff2SJonas Devlieghere  const char *python_function_name = python_function_name_string.c_str();
10536498aff2SJonas Devlieghere
10546498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
10556498aff2SJonas Devlieghere
10569d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10579d5e37edSPavel Labath      session_dictionary_name);
10589d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10599d5e37edSPavel Labath      python_function_name, dict);
10606498aff2SJonas Devlieghere
10616498aff2SJonas Devlieghere  // This method is optional and need not exist.  So if we don't find it,
10626498aff2SJonas Devlieghere  // it's actually a success, not a failure.
10636498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
10646498aff2SJonas Devlieghere    return true;
10656498aff2SJonas Devlieghere
10667406d236SPavel Labath  pfunc(ToSWIGWrapper(std::move(debugger)), dict);
10676498aff2SJonas Devlieghere
10686498aff2SJonas Devlieghere  return true;
10696498aff2SJonas Devlieghere}
10706498aff2SJonas Devlieghere
10719d5e37edSPavel Labathlldb::ValueObjectSP lldb_private::LLDBSWIGPython_GetValueObjectSPFromSBValue(
10729d5e37edSPavel Labath    void *data) {
10736498aff2SJonas Devlieghere  lldb::ValueObjectSP valobj_sp;
10749d5e37edSPavel Labath  if (data) {
10756498aff2SJonas Devlieghere    lldb::SBValue *sb_ptr = (lldb::SBValue *)data;
10766498aff2SJonas Devlieghere    valobj_sp = sb_ptr->GetSP();
10776498aff2SJonas Devlieghere  }
10786498aff2SJonas Devlieghere  return valobj_sp;
10796498aff2SJonas Devlieghere}
10806498aff2SJonas Devlieghere
10816498aff2SJonas Devlieghere// For the LogOutputCallback functions
10829d5e37edSPavel Labathstatic void LLDBSwigPythonCallPythonLogOutputCallback(const char *str,
10839d5e37edSPavel Labath                                                      void *baton) {
10846498aff2SJonas Devlieghere  if (baton != Py_None) {
10856498aff2SJonas Devlieghere    SWIG_PYTHON_THREAD_BEGIN_BLOCK;
10869d5e37edSPavel Labath    PyObject *result = PyObject_CallFunction(
10879d5e37edSPavel Labath        reinterpret_cast<PyObject *>(baton), const_cast<char *>("s"), str);
10886498aff2SJonas Devlieghere    Py_XDECREF(result);
10896498aff2SJonas Devlieghere    SWIG_PYTHON_THREAD_END_BLOCK;
10906498aff2SJonas Devlieghere  }
10916498aff2SJonas Devlieghere}
10926498aff2SJonas Devlieghere%}
1093