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);
416498aff2SJonas Devlieghere  PythonObject bp_loc_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_bp_loc));
426498aff2SJonas Devlieghere
436498aff2SJonas Devlieghere  auto result = [&]() -> Expected<PythonObject> {
446498aff2SJonas Devlieghere    // If the called function doesn't take extra_args, drop them here:
45ebb6bb72SPavel Labath    if (max_positional_args < 4)
466498aff2SJonas Devlieghere      return pfunc.Call(frame_arg, bp_loc_arg, dict);
47ebb6bb72SPavel Labath    return pfunc.Call(frame_arg, bp_loc_arg, ToSWIGWrapper(args_impl), dict);
486498aff2SJonas Devlieghere  }();
496498aff2SJonas Devlieghere
506498aff2SJonas Devlieghere  if (!result)
516498aff2SJonas Devlieghere    return result.takeError();
526498aff2SJonas Devlieghere
536498aff2SJonas Devlieghere  // Only False counts as false!
546498aff2SJonas Devlieghere  return result.get().get() != Py_False;
556498aff2SJonas Devlieghere}
566498aff2SJonas Devlieghere
579a14adeaSPavel Labath// resolve a dotted Python name in the form
589a14adeaSPavel Labath// foo.bar.baz.Foobar to an actual Python object
599a14adeaSPavel Labath// if pmodule is NULL, the __main__ module will be used
609a14adeaSPavel Labath// as the starting point for the search
61daf36998SDave Lee
629d5e37edSPavel Labath// This function is called by
639d5e37edSPavel Labath// lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...) and is
649d5e37edSPavel Labath// used when a script command is attached to a breakpoint for execution.
659a14adeaSPavel Labath
669d5e37edSPavel Labath// This function is called by
679d5e37edSPavel Labath// lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...) and is
689d5e37edSPavel Labath// used when a script command is attached to a watchpoint for execution.
69daf36998SDave Lee
709d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonWatchpointCallbackFunction(
719d5e37edSPavel Labath    const char *python_function_name, const char *session_dictionary_name,
729d5e37edSPavel Labath    const lldb::StackFrameSP &frame_sp, const lldb::WatchpointSP &wp_sp) {
736498aff2SJonas Devlieghere  lldb::SBWatchpoint sb_wp(wp_sp);
746498aff2SJonas Devlieghere
756498aff2SJonas Devlieghere  bool stop_at_watchpoint = true;
766498aff2SJonas Devlieghere
776498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
786498aff2SJonas Devlieghere
799d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
809d5e37edSPavel Labath      session_dictionary_name);
819d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
829d5e37edSPavel Labath      python_function_name, dict);
836498aff2SJonas Devlieghere
846498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
856498aff2SJonas Devlieghere    return stop_at_watchpoint;
866498aff2SJonas Devlieghere
877406d236SPavel Labath  PythonObject frame_arg = ToSWIGWrapper(frame_sp);
886498aff2SJonas Devlieghere  PythonObject wp_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_wp));
896498aff2SJonas Devlieghere  PythonObject result = pfunc(frame_arg, wp_arg, dict);
906498aff2SJonas Devlieghere
916498aff2SJonas Devlieghere  if (result.get() == Py_False)
926498aff2SJonas Devlieghere    stop_at_watchpoint = false;
936498aff2SJonas Devlieghere
946498aff2SJonas Devlieghere  return stop_at_watchpoint;
956498aff2SJonas Devlieghere}
966498aff2SJonas Devlieghere
979d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonCallTypeScript(
989d5e37edSPavel Labath    const char *python_function_name, const void *session_dictionary,
999d5e37edSPavel Labath    const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper,
1009d5e37edSPavel Labath    const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval) {
1016498aff2SJonas Devlieghere  lldb::SBTypeSummaryOptions sb_options(options_sp.get());
1026498aff2SJonas Devlieghere
1036498aff2SJonas Devlieghere  retval.clear();
1046498aff2SJonas Devlieghere
1056498aff2SJonas Devlieghere  if (!python_function_name || !session_dictionary)
1066498aff2SJonas Devlieghere    return false;
1076498aff2SJonas Devlieghere
1086498aff2SJonas Devlieghere  PyObject *pfunc_impl = nullptr;
1096498aff2SJonas Devlieghere
1109d5e37edSPavel Labath  if (pyfunct_wrapper && *pyfunct_wrapper &&
1119d5e37edSPavel Labath      PyFunction_Check(*pyfunct_wrapper)) {
1126498aff2SJonas Devlieghere    pfunc_impl = (PyObject *)(*pyfunct_wrapper);
1139d5e37edSPavel Labath    if (pfunc_impl->ob_refcnt == 1) {
1146498aff2SJonas Devlieghere      Py_XDECREF(pfunc_impl);
1156498aff2SJonas Devlieghere      pfunc_impl = NULL;
1166498aff2SJonas Devlieghere    }
1176498aff2SJonas Devlieghere  }
1186498aff2SJonas Devlieghere
1196498aff2SJonas Devlieghere  PyObject *py_dict = (PyObject *)session_dictionary;
1206498aff2SJonas Devlieghere  if (!PythonDictionary::Check(py_dict))
1216498aff2SJonas Devlieghere    return true;
1226498aff2SJonas Devlieghere
1236498aff2SJonas Devlieghere  PythonDictionary dict(PyRefType::Borrowed, py_dict);
1246498aff2SJonas Devlieghere
1256498aff2SJonas Devlieghere  PyErr_Cleaner pyerr_cleanup(true); // show Python errors
1266498aff2SJonas Devlieghere
1276498aff2SJonas Devlieghere  PythonCallable pfunc(PyRefType::Borrowed, pfunc_impl);
1286498aff2SJonas Devlieghere
1299d5e37edSPavel Labath  if (!pfunc.IsAllocated()) {
1309d5e37edSPavel Labath    pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
1319d5e37edSPavel Labath        python_function_name, dict);
1326498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
1336498aff2SJonas Devlieghere      return false;
1346498aff2SJonas Devlieghere
1359d5e37edSPavel Labath    if (pyfunct_wrapper) {
1366498aff2SJonas Devlieghere      *pyfunct_wrapper = pfunc.get();
1376498aff2SJonas Devlieghere      Py_XINCREF(pfunc.get());
1386498aff2SJonas Devlieghere    }
1396498aff2SJonas Devlieghere  }
1406498aff2SJonas Devlieghere
1416498aff2SJonas Devlieghere  PythonObject result;
1426498aff2SJonas Devlieghere  auto argc = pfunc.GetArgInfo();
1436498aff2SJonas Devlieghere  if (!argc) {
1446498aff2SJonas Devlieghere    llvm::consumeError(argc.takeError());
1456498aff2SJonas Devlieghere    return false;
1466498aff2SJonas Devlieghere  }
1476498aff2SJonas Devlieghere
1487f09ab08SPavel Labath  PythonObject value_arg = ToSWIGWrapper(valobj_sp);
1496498aff2SJonas Devlieghere  PythonObject options_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_options));
1506498aff2SJonas Devlieghere
1516498aff2SJonas Devlieghere  if (argc.get().max_positional_args < 3)
1526498aff2SJonas Devlieghere    result = pfunc(value_arg, dict);
1536498aff2SJonas Devlieghere  else
1546498aff2SJonas Devlieghere    result = pfunc(value_arg, dict, options_arg);
1556498aff2SJonas Devlieghere
1566498aff2SJonas Devlieghere  retval = result.Str().GetString().str();
1576498aff2SJonas Devlieghere
1586498aff2SJonas Devlieghere  return true;
1596498aff2SJonas Devlieghere}
1606498aff2SJonas Devlieghere
1619d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateSyntheticProvider(
1629d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
1639d5e37edSPavel Labath    const lldb::ValueObjectSP &valobj_sp) {
1649d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
1659d5e37edSPavel Labath      !session_dictionary_name)
1666498aff2SJonas Devlieghere    Py_RETURN_NONE;
1676498aff2SJonas Devlieghere
1686498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
1696498aff2SJonas Devlieghere
1709d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
1719d5e37edSPavel Labath      session_dictionary_name);
1729d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
1739d5e37edSPavel Labath      python_class_name, dict);
1746498aff2SJonas Devlieghere
1756498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
1766498aff2SJonas Devlieghere    Py_RETURN_NONE;
1776498aff2SJonas Devlieghere
1787f09ab08SPavel Labath  auto sb_value = std::make_unique<lldb::SBValue>(valobj_sp);
1796498aff2SJonas Devlieghere  sb_value->SetPreferSyntheticValue(false);
1806498aff2SJonas Devlieghere
1817f09ab08SPavel Labath  PythonObject val_arg = ToSWIGWrapper(std::move(sb_value));
1826498aff2SJonas Devlieghere  if (!val_arg.IsAllocated())
1836498aff2SJonas Devlieghere    Py_RETURN_NONE;
1846498aff2SJonas Devlieghere
1856498aff2SJonas Devlieghere  PythonObject result = pfunc(val_arg, dict);
1866498aff2SJonas Devlieghere
1876498aff2SJonas Devlieghere  if (result.IsAllocated())
1886498aff2SJonas Devlieghere    return result.release();
1896498aff2SJonas Devlieghere
1906498aff2SJonas Devlieghere  Py_RETURN_NONE;
1916498aff2SJonas Devlieghere}
1926498aff2SJonas Devlieghere
1939d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateCommandObject(
1949d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
1957406d236SPavel Labath    lldb::DebuggerSP debugger_sp) {
1969d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
1979d5e37edSPavel Labath      !session_dictionary_name)
1986498aff2SJonas Devlieghere    Py_RETURN_NONE;
1996498aff2SJonas Devlieghere
2006498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
2019d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
2029d5e37edSPavel Labath      session_dictionary_name);
2039d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
2049d5e37edSPavel Labath      python_class_name, dict);
2056498aff2SJonas Devlieghere
2066498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
2076498aff2SJonas Devlieghere    return nullptr;
2086498aff2SJonas Devlieghere
2097406d236SPavel Labath  PythonObject result = pfunc(ToSWIGWrapper(std::move(debugger_sp)), dict);
2106498aff2SJonas Devlieghere
2116498aff2SJonas Devlieghere  if (result.IsAllocated())
2126498aff2SJonas Devlieghere    return result.release();
2136498aff2SJonas Devlieghere
2146498aff2SJonas Devlieghere  Py_RETURN_NONE;
2156498aff2SJonas Devlieghere}
2166498aff2SJonas Devlieghere
2179d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedProcess(
2189d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
2191f6a57c1SMed Ismail Bennani    const lldb::TargetSP &target_sp,
22082de8df2SPavel Labath    const lldb_private::StructuredDataImpl &args_impl,
2219d5e37edSPavel Labath    std::string &error_string) {
2229d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
2239d5e37edSPavel Labath      !session_dictionary_name)
2241f6a57c1SMed Ismail Bennani    Py_RETURN_NONE;
2251f6a57c1SMed Ismail Bennani
2261f6a57c1SMed Ismail Bennani  PyErr_Cleaner py_err_cleaner(true);
2271f6a57c1SMed Ismail Bennani
2289d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
2299d5e37edSPavel Labath      session_dictionary_name);
2309d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
2319d5e37edSPavel Labath      python_class_name, dict);
2321f6a57c1SMed Ismail Bennani
2331f6a57c1SMed Ismail Bennani  if (!pfunc.IsAllocated()) {
2341f6a57c1SMed Ismail Bennani    error_string.append("could not find script class: ");
2351f6a57c1SMed Ismail Bennani    error_string.append(python_class_name);
2361f6a57c1SMed Ismail Bennani    return nullptr;
2371f6a57c1SMed Ismail Bennani  }
2381f6a57c1SMed Ismail Bennani
2397f09ab08SPavel Labath  PythonObject target_arg = ToSWIGWrapper(target_sp);
2401f6a57c1SMed Ismail Bennani
2411f6a57c1SMed Ismail Bennani  llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
2421f6a57c1SMed Ismail Bennani  if (!arg_info) {
2431f6a57c1SMed Ismail Bennani    llvm::handleAllErrors(
2441f6a57c1SMed Ismail Bennani        arg_info.takeError(),
2459d5e37edSPavel Labath        [&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
2461f6a57c1SMed Ismail Bennani        [&](const llvm::ErrorInfoBase &E) {
2471f6a57c1SMed Ismail Bennani          error_string.append(E.message());
2481f6a57c1SMed Ismail Bennani        });
2491f6a57c1SMed Ismail Bennani    Py_RETURN_NONE;
2501f6a57c1SMed Ismail Bennani  }
2511f6a57c1SMed Ismail Bennani
2521f6a57c1SMed Ismail Bennani  PythonObject result = {};
2531f6a57c1SMed Ismail Bennani  if (arg_info.get().max_positional_args == 2) {
254ebb6bb72SPavel Labath    result = pfunc(target_arg, ToSWIGWrapper(args_impl));
2551f6a57c1SMed Ismail Bennani  } else {
2569d5e37edSPavel Labath    error_string.assign("wrong number of arguments in __init__, should be 2 "
2579d5e37edSPavel Labath                        "(not including self)");
2581f6a57c1SMed Ismail Bennani    Py_RETURN_NONE;
2591f6a57c1SMed Ismail Bennani  }
2601f6a57c1SMed Ismail Bennani
2611f6a57c1SMed Ismail Bennani  if (result.IsAllocated())
2621f6a57c1SMed Ismail Bennani    return result.release();
2631f6a57c1SMed Ismail Bennani  Py_RETURN_NONE;
2641f6a57c1SMed Ismail Bennani}
2651f6a57c1SMed Ismail Bennani
2669d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedThread(
2679d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
2689d5e37edSPavel Labath    const lldb::ProcessSP &process_sp, const StructuredDataImpl &args_impl,
2699d5e37edSPavel Labath    std::string &error_string) {
2709d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
2719d5e37edSPavel Labath      !session_dictionary_name)
27259d8dd79SMed Ismail Bennani    Py_RETURN_NONE;
27359d8dd79SMed Ismail Bennani
27459d8dd79SMed Ismail Bennani  PyErr_Cleaner py_err_cleaner(true);
27559d8dd79SMed Ismail Bennani
2769d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
2779d5e37edSPavel Labath      session_dictionary_name);
2789d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
2799d5e37edSPavel Labath      python_class_name, dict);
28059d8dd79SMed Ismail Bennani
28159d8dd79SMed Ismail Bennani  if (!pfunc.IsAllocated()) {
28259d8dd79SMed Ismail Bennani    error_string.append("could not find script class: ");
28359d8dd79SMed Ismail Bennani    error_string.append(python_class_name);
28459d8dd79SMed Ismail Bennani    return nullptr;
28559d8dd79SMed Ismail Bennani  }
28659d8dd79SMed Ismail Bennani
28759d8dd79SMed Ismail Bennani  llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
28859d8dd79SMed Ismail Bennani  if (!arg_info) {
28959d8dd79SMed Ismail Bennani    llvm::handleAllErrors(
29059d8dd79SMed Ismail Bennani        arg_info.takeError(),
2919d5e37edSPavel Labath        [&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
29259d8dd79SMed Ismail Bennani        [&](const llvm::ErrorInfoBase &E) {
29359d8dd79SMed Ismail Bennani          error_string.append(E.message());
29459d8dd79SMed Ismail Bennani        });
29559d8dd79SMed Ismail Bennani    Py_RETURN_NONE;
29659d8dd79SMed Ismail Bennani  }
29759d8dd79SMed Ismail Bennani
29859d8dd79SMed Ismail Bennani  PythonObject result = {};
299738621d0SMed Ismail Bennani  if (arg_info.get().max_positional_args == 2) {
300ebb6bb72SPavel Labath    result = pfunc(ToSWIGWrapper(process_sp), ToSWIGWrapper(args_impl));
30159d8dd79SMed Ismail Bennani  } else {
3029d5e37edSPavel Labath    error_string.assign("wrong number of arguments in __init__, should be 2 "
3039d5e37edSPavel Labath                        "(not including self)");
30459d8dd79SMed Ismail Bennani    Py_RETURN_NONE;
30559d8dd79SMed Ismail Bennani  }
30659d8dd79SMed Ismail Bennani
30759d8dd79SMed Ismail Bennani  if (result.IsAllocated())
30859d8dd79SMed Ismail Bennani    return result.release();
30959d8dd79SMed Ismail Bennani  Py_RETURN_NONE;
31059d8dd79SMed Ismail Bennani}
31159d8dd79SMed Ismail Bennani
3129d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
3139d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
31482de8df2SPavel Labath    const lldb_private::StructuredDataImpl &args_impl,
3159d5e37edSPavel Labath    std::string &error_string, const lldb::ThreadPlanSP &thread_plan_sp) {
3169d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
3179d5e37edSPavel Labath      !session_dictionary_name)
3186498aff2SJonas Devlieghere    Py_RETURN_NONE;
3196498aff2SJonas Devlieghere
3206498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
3216498aff2SJonas Devlieghere
3229d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
3239d5e37edSPavel Labath      session_dictionary_name);
3249d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
3259d5e37edSPavel Labath      python_class_name, dict);
3266498aff2SJonas Devlieghere
3276498aff2SJonas Devlieghere  if (!pfunc.IsAllocated()) {
3286498aff2SJonas Devlieghere    error_string.append("could not find script class: ");
3296498aff2SJonas Devlieghere    error_string.append(python_class_name);
3306498aff2SJonas Devlieghere    return nullptr;
3316498aff2SJonas Devlieghere  }
3326498aff2SJonas Devlieghere
3337f09ab08SPavel Labath  PythonObject tp_arg = ToSWIGWrapper(thread_plan_sp);
3346498aff2SJonas Devlieghere
3356498aff2SJonas Devlieghere  llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
3366498aff2SJonas Devlieghere  if (!arg_info) {
3376498aff2SJonas Devlieghere    llvm::handleAllErrors(
3386498aff2SJonas Devlieghere        arg_info.takeError(),
3399d5e37edSPavel Labath        [&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
3406498aff2SJonas Devlieghere        [&](const llvm::ErrorInfoBase &E) {
3416498aff2SJonas Devlieghere          error_string.append(E.message());
3426498aff2SJonas Devlieghere        });
3436498aff2SJonas Devlieghere    Py_RETURN_NONE;
3446498aff2SJonas Devlieghere  }
3456498aff2SJonas Devlieghere
3466498aff2SJonas Devlieghere  PythonObject result = {};
347ebb6bb72SPavel Labath  auto args_sb = std::make_unique<lldb::SBStructuredData>(args_impl);
3486498aff2SJonas Devlieghere  if (arg_info.get().max_positional_args == 2) {
34982de8df2SPavel Labath    if (args_sb->IsValid()) {
3509d5e37edSPavel Labath      error_string.assign(
3519d5e37edSPavel Labath          "args passed, but __init__ does not take an args dictionary");
3526498aff2SJonas Devlieghere      Py_RETURN_NONE;
3536498aff2SJonas Devlieghere    }
3546498aff2SJonas Devlieghere    result = pfunc(tp_arg, dict);
3556498aff2SJonas Devlieghere  } else if (arg_info.get().max_positional_args >= 3) {
356ebb6bb72SPavel Labath    result = pfunc(tp_arg, ToSWIGWrapper(std::move(args_sb)), dict);
3576498aff2SJonas Devlieghere  } else {
3589d5e37edSPavel Labath    error_string.assign("wrong number of arguments in __init__, should be 2 or "
3599d5e37edSPavel Labath                        "3 (not including self)");
3606498aff2SJonas Devlieghere    Py_RETURN_NONE;
3616498aff2SJonas Devlieghere  }
3626498aff2SJonas Devlieghere
3639d5e37edSPavel Labath  // FIXME: At this point we should check that the class we found supports all
3649d5e37edSPavel Labath  // the methods that we need.
3656498aff2SJonas Devlieghere
3666498aff2SJonas Devlieghere  if (result.IsAllocated())
3676498aff2SJonas Devlieghere    return result.release();
3686498aff2SJonas Devlieghere  Py_RETURN_NONE;
3696498aff2SJonas Devlieghere}
3706498aff2SJonas Devlieghere
3719d5e37edSPavel Labathbool lldb_private::LLDBSWIGPythonCallThreadPlan(
3729d5e37edSPavel Labath    void *implementor, const char *method_name, lldb_private::Event *event,
3739d5e37edSPavel Labath    bool &got_error) {
3746498aff2SJonas Devlieghere  got_error = false;
3756498aff2SJonas Devlieghere
3766498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(false);
3776498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
3786498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>(method_name);
3796498aff2SJonas Devlieghere
3806498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
3816498aff2SJonas Devlieghere    return false;
3826498aff2SJonas Devlieghere
3836498aff2SJonas Devlieghere  PythonObject result;
3849d5e37edSPavel Labath  if (event != nullptr) {
3856498aff2SJonas Devlieghere    lldb::SBEvent sb_event(event);
3866498aff2SJonas Devlieghere    PythonObject event_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_event));
3876498aff2SJonas Devlieghere    result = pfunc(event_arg);
3889d5e37edSPavel Labath  } else
3896498aff2SJonas Devlieghere    result = pfunc();
3906498aff2SJonas Devlieghere
3919d5e37edSPavel Labath  if (PyErr_Occurred()) {
3926498aff2SJonas Devlieghere    got_error = true;
3939d5e37edSPavel Labath    printf("Return value was neither false nor true for call to %s.\n",
3949d5e37edSPavel Labath           method_name);
3956498aff2SJonas Devlieghere    PyErr_Print();
3966498aff2SJonas Devlieghere    return false;
3976498aff2SJonas Devlieghere  }
3986498aff2SJonas Devlieghere
3996498aff2SJonas Devlieghere  if (result.get() == Py_True)
4006498aff2SJonas Devlieghere    return true;
4016498aff2SJonas Devlieghere  else if (result.get() == Py_False)
4026498aff2SJonas Devlieghere    return false;
4036498aff2SJonas Devlieghere
4046498aff2SJonas Devlieghere  // Somebody returned the wrong thing...
4056498aff2SJonas Devlieghere  got_error = true;
4066498aff2SJonas Devlieghere  printf("Wrong return value type for call to %s.\n", method_name);
4076498aff2SJonas Devlieghere  return false;
4086498aff2SJonas Devlieghere}
4096498aff2SJonas Devlieghere
4109a14adeaSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
4117f09ab08SPavel Labath    const char *python_class_name, const char *session_dictionary_name,
41282de8df2SPavel Labath    const StructuredDataImpl &args_impl,
4137f09ab08SPavel Labath    const lldb::BreakpointSP &breakpoint_sp) {
4147f09ab08SPavel Labath
4159d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
4169d5e37edSPavel Labath      !session_dictionary_name)
4176498aff2SJonas Devlieghere    Py_RETURN_NONE;
4186498aff2SJonas Devlieghere
4196498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
4206498aff2SJonas Devlieghere
4219d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
4229d5e37edSPavel Labath      session_dictionary_name);
4239d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
4249d5e37edSPavel Labath      python_class_name, dict);
4256498aff2SJonas Devlieghere
4266498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
4276498aff2SJonas Devlieghere    return nullptr;
4286498aff2SJonas Devlieghere
429ebb6bb72SPavel Labath  PythonObject result =
430ebb6bb72SPavel Labath      pfunc(ToSWIGWrapper(breakpoint_sp), ToSWIGWrapper(args_impl), dict);
4319d5e37edSPavel Labath  // FIXME: At this point we should check that the class we found supports all
4329d5e37edSPavel Labath  // the methods that we need.
4336498aff2SJonas Devlieghere
4349d5e37edSPavel Labath  if (result.IsAllocated()) {
4356498aff2SJonas Devlieghere    // Check that __callback__ is defined:
4366498aff2SJonas Devlieghere    auto callback_func = result.ResolveName<PythonCallable>("__callback__");
4376498aff2SJonas Devlieghere    if (callback_func.IsAllocated())
4386498aff2SJonas Devlieghere      return result.release();
4396498aff2SJonas Devlieghere    else
4406498aff2SJonas Devlieghere      result.release();
4416498aff2SJonas Devlieghere  }
4426498aff2SJonas Devlieghere  Py_RETURN_NONE;
4436498aff2SJonas Devlieghere}
4446498aff2SJonas Devlieghere
4459d5e37edSPavel Labathunsigned int lldb_private::LLDBSwigPythonCallBreakpointResolver(
4469d5e37edSPavel Labath    void *implementor, const char *method_name,
4479d5e37edSPavel Labath    lldb_private::SymbolContext *sym_ctx) {
4486498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(false);
4496498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
4506498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>(method_name);
4516498aff2SJonas Devlieghere
4526498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
4536498aff2SJonas Devlieghere    return 0;
4546498aff2SJonas Devlieghere
4556498aff2SJonas Devlieghere  PythonObject result;
4566498aff2SJonas Devlieghere  if (sym_ctx != nullptr) {
4576498aff2SJonas Devlieghere    lldb::SBSymbolContext sb_sym_ctx(sym_ctx);
4586498aff2SJonas Devlieghere    PythonObject sym_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_sym_ctx));
4596498aff2SJonas Devlieghere    result = pfunc(sym_ctx_arg);
4606498aff2SJonas Devlieghere  } else
4616498aff2SJonas Devlieghere    result = pfunc();
4626498aff2SJonas Devlieghere
4639d5e37edSPavel Labath  if (PyErr_Occurred()) {
4646498aff2SJonas Devlieghere    PyErr_Print();
46552712d3fSLawrence D'Anna    PyErr_Clear();
4666498aff2SJonas Devlieghere    return 0;
4676498aff2SJonas Devlieghere  }
4686498aff2SJonas Devlieghere
4696498aff2SJonas Devlieghere  // The callback will return a bool, but we're need to also return ints
4706498aff2SJonas Devlieghere  // so we're squirrelling the bool through as an int...  And if you return
4716498aff2SJonas Devlieghere  // nothing, we'll continue.
4726498aff2SJonas Devlieghere  if (strcmp(method_name, "__callback__") == 0) {
4736498aff2SJonas Devlieghere    if (result.get() == Py_False)
4746498aff2SJonas Devlieghere      return 0;
4756498aff2SJonas Devlieghere    else
4766498aff2SJonas Devlieghere      return 1;
4776498aff2SJonas Devlieghere  }
4786498aff2SJonas Devlieghere
47952712d3fSLawrence D'Anna  long long ret_val = unwrapOrSetPythonException(As<long long>(result));
480478619cfSMuhammad Omair Javaid
48152712d3fSLawrence D'Anna  if (PyErr_Occurred()) {
48252712d3fSLawrence D'Anna    PyErr_Print();
48352712d3fSLawrence D'Anna    PyErr_Clear();
48452712d3fSLawrence D'Anna    return 0;
48552712d3fSLawrence D'Anna  }
4866498aff2SJonas Devlieghere
4876498aff2SJonas Devlieghere  return ret_val;
4886498aff2SJonas Devlieghere}
4896498aff2SJonas Devlieghere
4909d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedStopHook(
4919d5e37edSPavel Labath    lldb::TargetSP target_sp, const char *python_class_name,
4929d5e37edSPavel Labath    const char *session_dictionary_name, const StructuredDataImpl &args_impl,
4939d5e37edSPavel Labath    Status &error) {
4941b1d9815SJim Ingham  if (python_class_name == NULL || python_class_name[0] == '\0') {
4951b1d9815SJim Ingham    error.SetErrorString("Empty class name.");
4961b1d9815SJim Ingham    Py_RETURN_NONE;
4971b1d9815SJim Ingham  }
4981b1d9815SJim Ingham  if (!session_dictionary_name) {
4991b1d9815SJim Ingham    error.SetErrorString("No session dictionary");
5001b1d9815SJim Ingham    Py_RETURN_NONE;
5011b1d9815SJim Ingham  }
5021b1d9815SJim Ingham
5031b1d9815SJim Ingham  PyErr_Cleaner py_err_cleaner(true);
5041b1d9815SJim Ingham
5059d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
5061b1d9815SJim Ingham      session_dictionary_name);
5079d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
5081b1d9815SJim Ingham      python_class_name, dict);
5091b1d9815SJim Ingham
5101b1d9815SJim Ingham  if (!pfunc.IsAllocated()) {
5111b1d9815SJim Ingham    error.SetErrorStringWithFormat("Could not find class: %s.",
5121b1d9815SJim Ingham                                   python_class_name);
5131b1d9815SJim Ingham    return nullptr;
5141b1d9815SJim Ingham  }
5151b1d9815SJim Ingham
516ebb6bb72SPavel Labath  PythonObject result =
517ebb6bb72SPavel Labath      pfunc(ToSWIGWrapper(target_sp), ToSWIGWrapper(args_impl), dict);
5181b1d9815SJim Ingham
5199d5e37edSPavel Labath  if (result.IsAllocated()) {
5201b1d9815SJim Ingham    // Check that the handle_stop callback is defined:
5211b1d9815SJim Ingham    auto callback_func = result.ResolveName<PythonCallable>("handle_stop");
5221b1d9815SJim Ingham    if (callback_func.IsAllocated()) {
5231b1d9815SJim Ingham      if (auto args_info = callback_func.GetArgInfo()) {
5241b1d9815SJim Ingham        size_t num_args = (*args_info).max_positional_args;
5251b1d9815SJim Ingham        if (num_args != 2) {
5269d5e37edSPavel Labath          error.SetErrorStringWithFormat(
5279d5e37edSPavel Labath              "Wrong number of args for "
5282f95c50aSRichard Smith              "handle_stop callback, should be 2 (excluding self), got: %zu",
5291b1d9815SJim Ingham              num_args);
5301b1d9815SJim Ingham          Py_RETURN_NONE;
5311b1d9815SJim Ingham        } else
5321b1d9815SJim Ingham          return result.release();
5331b1d9815SJim Ingham      } else {
5341b1d9815SJim Ingham        error.SetErrorString("Couldn't get num arguments for handle_stop "
5351b1d9815SJim Ingham                             "callback.");
5361b1d9815SJim Ingham        Py_RETURN_NONE;
5371b1d9815SJim Ingham      }
5381b1d9815SJim Ingham      return result.release();
5399d5e37edSPavel Labath    } else {
5401b1d9815SJim Ingham      error.SetErrorStringWithFormat("Class \"%s\" is missing the required "
5411b1d9815SJim Ingham                                     "handle_stop callback.",
5421b1d9815SJim Ingham                                     python_class_name);
5431b1d9815SJim Ingham      result.release();
5441b1d9815SJim Ingham    }
5451b1d9815SJim Ingham  }
5461b1d9815SJim Ingham  Py_RETURN_NONE;
5471b1d9815SJim Ingham}
5481b1d9815SJim Ingham
5499d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonStopHookCallHandleStop(
5509d5e37edSPavel Labath    void *implementor, lldb::ExecutionContextRefSP exc_ctx_sp,
5519d5e37edSPavel Labath    lldb::StreamSP stream) {
5521b1d9815SJim Ingham  // handle_stop will return a bool with the meaning "should_stop"...
5531b1d9815SJim Ingham  // If you return nothing we'll assume we are going to stop.
5541b1d9815SJim Ingham  // Also any errors should return true, since we should stop on error.
5551b1d9815SJim Ingham
5561b1d9815SJim Ingham  PyErr_Cleaner py_err_cleaner(false);
5571b1d9815SJim Ingham  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
5581b1d9815SJim Ingham  auto pfunc = self.ResolveName<PythonCallable>("handle_stop");
5591b1d9815SJim Ingham
5601b1d9815SJim Ingham  if (!pfunc.IsAllocated())
5611b1d9815SJim Ingham    return true;
5621b1d9815SJim Ingham
5631b1d9815SJim Ingham  PythonObject result;
5641b1d9815SJim Ingham  lldb::SBExecutionContext sb_exc_ctx(exc_ctx_sp);
5651b1d9815SJim Ingham  PythonObject exc_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_exc_ctx));
5661b1d9815SJim Ingham  lldb::SBStream sb_stream;
5679d5e37edSPavel Labath  PythonObject sb_stream_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_stream));
5681b1d9815SJim Ingham  result = pfunc(exc_ctx_arg, sb_stream_arg);
5691b1d9815SJim Ingham
5709d5e37edSPavel Labath  if (PyErr_Occurred()) {
5711b1d9815SJim Ingham    stream->PutCString("Python error occurred handling stop-hook.");
5721b1d9815SJim Ingham    PyErr_Print();
5731b1d9815SJim Ingham    PyErr_Clear();
5741b1d9815SJim Ingham    return true;
5751b1d9815SJim Ingham  }
5761b1d9815SJim Ingham
5771b1d9815SJim Ingham  // Now add the result to the output stream.  SBStream only
5781b1d9815SJim Ingham  // makes an internally help StreamString which I can't interpose, so I
5791b1d9815SJim Ingham  // have to copy it over here.
5801b1d9815SJim Ingham  stream->PutCString(sb_stream.GetData());
5811b1d9815SJim Ingham
5821b1d9815SJim Ingham  if (result.get() == Py_False)
5831b1d9815SJim Ingham    return false;
5841b1d9815SJim Ingham  else
5851b1d9815SJim Ingham    return true;
5861b1d9815SJim Ingham}
5871b1d9815SJim Ingham
5889d5e37edSPavel Labath// wrapper that calls an optional instance member of an object taking no
5899d5e37edSPavel Labath// arguments
5909d5e37edSPavel Labathstatic PyObject *LLDBSwigPython_CallOptionalMember(
5919d5e37edSPavel Labath    PyObject * implementor, char *callee_name,
5929d5e37edSPavel Labath    PyObject *ret_if_not_found = Py_None, bool *was_found = NULL) {
5936498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(false);
5946498aff2SJonas Devlieghere
5956498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
5966498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>(callee_name);
5976498aff2SJonas Devlieghere
5989d5e37edSPavel Labath  if (!pfunc.IsAllocated()) {
5996498aff2SJonas Devlieghere    if (was_found)
6006498aff2SJonas Devlieghere      *was_found = false;
6016498aff2SJonas Devlieghere    Py_XINCREF(ret_if_not_found);
6026498aff2SJonas Devlieghere    return ret_if_not_found;
6036498aff2SJonas Devlieghere  }
6046498aff2SJonas Devlieghere
6056498aff2SJonas Devlieghere  if (was_found)
6066498aff2SJonas Devlieghere    *was_found = true;
6076498aff2SJonas Devlieghere
6086498aff2SJonas Devlieghere  PythonObject result = pfunc();
6096498aff2SJonas Devlieghere  return result.release();
6106498aff2SJonas Devlieghere}
6116498aff2SJonas Devlieghere
6129d5e37edSPavel Labathsize_t lldb_private::LLDBSwigPython_CalculateNumChildren(PyObject * implementor,
6139d5e37edSPavel Labath                                                         uint32_t max) {
6146498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, implementor);
6156498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>("num_children");
6166498aff2SJonas Devlieghere
6176498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
6186498aff2SJonas Devlieghere    return 0;
6196498aff2SJonas Devlieghere
6206498aff2SJonas Devlieghere  auto arg_info = pfunc.GetArgInfo();
6216498aff2SJonas Devlieghere  if (!arg_info) {
6226498aff2SJonas Devlieghere    llvm::consumeError(arg_info.takeError());
6236498aff2SJonas Devlieghere    return 0;
6246498aff2SJonas Devlieghere  }
6256498aff2SJonas Devlieghere
62652712d3fSLawrence D'Anna  size_t ret_val;
627478619cfSMuhammad Omair Javaid  if (arg_info.get().max_positional_args < 1)
62852712d3fSLawrence D'Anna    ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call()));
629478619cfSMuhammad Omair Javaid  else
6309d5e37edSPavel Labath    ret_val = unwrapOrSetPythonException(
6319d5e37edSPavel Labath        As<long long>(pfunc.Call(PythonInteger(max))));
632478619cfSMuhammad Omair Javaid
6339d5e37edSPavel Labath  if (PyErr_Occurred()) {
6346498aff2SJonas Devlieghere    PyErr_Print();
6356498aff2SJonas Devlieghere    PyErr_Clear();
63652712d3fSLawrence D'Anna    return 0;
6376498aff2SJonas Devlieghere  }
6386498aff2SJonas Devlieghere
6396498aff2SJonas Devlieghere  if (arg_info.get().max_positional_args < 1)
6406498aff2SJonas Devlieghere    ret_val = std::min(ret_val, static_cast<size_t>(max));
6416498aff2SJonas Devlieghere
6426498aff2SJonas Devlieghere  return ret_val;
6436498aff2SJonas Devlieghere}
6446498aff2SJonas Devlieghere
6459d5e37edSPavel LabathPyObject *lldb_private::LLDBSwigPython_GetChildAtIndex(PyObject * implementor,
6469d5e37edSPavel Labath                                                       uint32_t idx) {
6476498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
6486498aff2SJonas Devlieghere
6496498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, implementor);
6506498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index");
6516498aff2SJonas Devlieghere
6526498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
6536498aff2SJonas Devlieghere    return nullptr;
6546498aff2SJonas Devlieghere
6556498aff2SJonas Devlieghere  PythonObject result = pfunc(PythonInteger(idx));
6566498aff2SJonas Devlieghere
6576498aff2SJonas Devlieghere  if (!result.IsAllocated())
6586498aff2SJonas Devlieghere    return nullptr;
6596498aff2SJonas Devlieghere
6606498aff2SJonas Devlieghere  lldb::SBValue *sbvalue_ptr = nullptr;
6619d5e37edSPavel Labath  if (SWIG_ConvertPtr(result.get(), (void **)&sbvalue_ptr,
6629d5e37edSPavel Labath                      SWIGTYPE_p_lldb__SBValue, 0) == -1)
6636498aff2SJonas Devlieghere    return nullptr;
6646498aff2SJonas Devlieghere
6656498aff2SJonas Devlieghere  if (sbvalue_ptr == nullptr)
6666498aff2SJonas Devlieghere    return nullptr;
6676498aff2SJonas Devlieghere
6686498aff2SJonas Devlieghere  return result.release();
6696498aff2SJonas Devlieghere}
6706498aff2SJonas Devlieghere
6719d5e37edSPavel Labathint lldb_private::LLDBSwigPython_GetIndexOfChildWithName(
6729d5e37edSPavel Labath    PyObject * implementor, const char *child_name) {
6736498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
6746498aff2SJonas Devlieghere
6756498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, implementor);
6766498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>("get_child_index");
6776498aff2SJonas Devlieghere
6786498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
6796498aff2SJonas Devlieghere    return UINT32_MAX;
6806498aff2SJonas Devlieghere
68152712d3fSLawrence D'Anna  llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name));
6826498aff2SJonas Devlieghere
6839d5e37edSPavel Labath  long long retval =
6849d5e37edSPavel Labath      unwrapOrSetPythonException(As<long long>(std::move(result)));
68552712d3fSLawrence D'Anna
68652712d3fSLawrence D'Anna  if (PyErr_Occurred()) {
68752712d3fSLawrence D'Anna    PyErr_Clear(); // FIXME print this? do something else
6886498aff2SJonas Devlieghere    return UINT32_MAX;
68952712d3fSLawrence D'Anna  }
6906498aff2SJonas Devlieghere
6916498aff2SJonas Devlieghere  if (retval >= 0)
6926498aff2SJonas Devlieghere    return (uint32_t)retval;
6936498aff2SJonas Devlieghere
6946498aff2SJonas Devlieghere  return UINT32_MAX;
6956498aff2SJonas Devlieghere}
6966498aff2SJonas Devlieghere
6979d5e37edSPavel Labathbool lldb_private::LLDBSwigPython_UpdateSynthProviderInstance(PyObject *
6989d5e37edSPavel Labath                                                              implementor) {
6996498aff2SJonas Devlieghere  bool ret_val = false;
7006498aff2SJonas Devlieghere
7016498aff2SJonas Devlieghere  static char callee_name[] = "update";
7026498aff2SJonas Devlieghere
7039d5e37edSPavel Labath  PyObject *py_return =
7049d5e37edSPavel Labath      LLDBSwigPython_CallOptionalMember(implementor, callee_name);
7056498aff2SJonas Devlieghere
7066498aff2SJonas Devlieghere  if (py_return == Py_True)
7076498aff2SJonas Devlieghere    ret_val = true;
7086498aff2SJonas Devlieghere
7096498aff2SJonas Devlieghere  Py_XDECREF(py_return);
7106498aff2SJonas Devlieghere
7116498aff2SJonas Devlieghere  return ret_val;
7126498aff2SJonas Devlieghere}
7136498aff2SJonas Devlieghere
7149d5e37edSPavel Labathbool lldb_private::LLDBSwigPython_MightHaveChildrenSynthProviderInstance(
7159d5e37edSPavel Labath    PyObject * implementor) {
7166498aff2SJonas Devlieghere  bool ret_val = false;
7176498aff2SJonas Devlieghere
7186498aff2SJonas Devlieghere  static char callee_name[] = "has_children";
7196498aff2SJonas Devlieghere
7209d5e37edSPavel Labath  PyObject *py_return =
7219d5e37edSPavel Labath      LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_True);
7226498aff2SJonas Devlieghere
7236498aff2SJonas Devlieghere  if (py_return == Py_True)
7246498aff2SJonas Devlieghere    ret_val = true;
7256498aff2SJonas Devlieghere
7266498aff2SJonas Devlieghere  Py_XDECREF(py_return);
7276498aff2SJonas Devlieghere
7286498aff2SJonas Devlieghere  return ret_val;
7296498aff2SJonas Devlieghere}
7306498aff2SJonas Devlieghere
7319d5e37edSPavel LabathPyObject *lldb_private::LLDBSwigPython_GetValueSynthProviderInstance(
7329d5e37edSPavel Labath    PyObject * implementor) {
7336498aff2SJonas Devlieghere  PyObject *ret_val = nullptr;
7346498aff2SJonas Devlieghere
7356498aff2SJonas Devlieghere  static char callee_name[] = "get_value";
7366498aff2SJonas Devlieghere
7379d5e37edSPavel Labath  PyObject *py_return =
7389d5e37edSPavel Labath      LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_None);
7396498aff2SJonas Devlieghere
7406498aff2SJonas Devlieghere  if (py_return == Py_None || py_return == nullptr)
7416498aff2SJonas Devlieghere    ret_val = nullptr;
7426498aff2SJonas Devlieghere
7436498aff2SJonas Devlieghere  lldb::SBValue *sbvalue_ptr = NULL;
7446498aff2SJonas Devlieghere
7459d5e37edSPavel Labath  if (SWIG_ConvertPtr(py_return, (void **)&sbvalue_ptr,
7469d5e37edSPavel Labath                      SWIGTYPE_p_lldb__SBValue, 0) == -1)
7476498aff2SJonas Devlieghere    ret_val = nullptr;
7486498aff2SJonas Devlieghere  else if (sbvalue_ptr == NULL)
7496498aff2SJonas Devlieghere    ret_val = nullptr;
7506498aff2SJonas Devlieghere  else
7516498aff2SJonas Devlieghere    ret_val = py_return;
7526498aff2SJonas Devlieghere
7536498aff2SJonas Devlieghere  Py_XDECREF(py_return);
7546498aff2SJonas Devlieghere  return ret_val;
7556498aff2SJonas Devlieghere}
7566498aff2SJonas Devlieghere
7579d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBData(PyObject * data) {
7581f6a57c1SMed Ismail Bennani  lldb::SBData *sb_ptr = nullptr;
7591f6a57c1SMed Ismail Bennani
7609d5e37edSPavel Labath  int valid_cast =
7619d5e37edSPavel Labath      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0);
7621f6a57c1SMed Ismail Bennani
7631f6a57c1SMed Ismail Bennani  if (valid_cast == -1)
7641f6a57c1SMed Ismail Bennani    return NULL;
7651f6a57c1SMed Ismail Bennani
7661f6a57c1SMed Ismail Bennani  return sb_ptr;
7671f6a57c1SMed Ismail Bennani}
7681f6a57c1SMed Ismail Bennani
7699d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBError(PyObject * data) {
7701f6a57c1SMed Ismail Bennani  lldb::SBError *sb_ptr = nullptr;
7711f6a57c1SMed Ismail Bennani
7729d5e37edSPavel Labath  int valid_cast =
7739d5e37edSPavel Labath      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0);
7741f6a57c1SMed Ismail Bennani
7751f6a57c1SMed Ismail Bennani  if (valid_cast == -1)
7761f6a57c1SMed Ismail Bennani    return NULL;
7771f6a57c1SMed Ismail Bennani
7781f6a57c1SMed Ismail Bennani  return sb_ptr;
7791f6a57c1SMed Ismail Bennani}
7801f6a57c1SMed Ismail Bennani
7819d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBValue(PyObject * data) {
7826498aff2SJonas Devlieghere  lldb::SBValue *sb_ptr = NULL;
7836498aff2SJonas Devlieghere
7849d5e37edSPavel Labath  int valid_cast =
7859d5e37edSPavel Labath      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
7866498aff2SJonas Devlieghere
7876498aff2SJonas Devlieghere  if (valid_cast == -1)
7886498aff2SJonas Devlieghere    return NULL;
7896498aff2SJonas Devlieghere
7906498aff2SJonas Devlieghere  return sb_ptr;
7916498aff2SJonas Devlieghere}
7926498aff2SJonas Devlieghere
7939d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *
7949d5e37edSPavel Labath                                                                    data) {
795a758c9f7SMed Ismail Bennani  lldb::SBMemoryRegionInfo *sb_ptr = NULL;
796a758c9f7SMed Ismail Bennani
7979d5e37edSPavel Labath  int valid_cast = SWIG_ConvertPtr(data, (void **)&sb_ptr,
7989d5e37edSPavel Labath                                   SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0);
799a758c9f7SMed Ismail Bennani
800a758c9f7SMed Ismail Bennani  if (valid_cast == -1)
801a758c9f7SMed Ismail Bennani    return NULL;
802a758c9f7SMed Ismail Bennani
803a758c9f7SMed Ismail Bennani  return sb_ptr;
804a758c9f7SMed Ismail Bennani}
805a758c9f7SMed Ismail Bennani
8069d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonCallCommand(
8079d5e37edSPavel Labath    const char *python_function_name, const char *session_dictionary_name,
8087406d236SPavel Labath    lldb::DebuggerSP debugger, const char *args,
8096498aff2SJonas Devlieghere    lldb_private::CommandReturnObject &cmd_retobj,
8109d5e37edSPavel Labath    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
8116498aff2SJonas Devlieghere  lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
8126498aff2SJonas Devlieghere  lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
8136498aff2SJonas Devlieghere
8146498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
8159d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
8169d5e37edSPavel Labath      session_dictionary_name);
8179d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
8189d5e37edSPavel Labath      python_function_name, dict);
8196498aff2SJonas Devlieghere
8206498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
8216498aff2SJonas Devlieghere    return false;
8226498aff2SJonas Devlieghere
8236498aff2SJonas Devlieghere  auto argc = pfunc.GetArgInfo();
8246498aff2SJonas Devlieghere  if (!argc) {
8256498aff2SJonas Devlieghere    llvm::consumeError(argc.takeError());
8266498aff2SJonas Devlieghere    return false;
8276498aff2SJonas Devlieghere  }
8287406d236SPavel Labath  PythonObject debugger_arg = ToSWIGWrapper(std::move(debugger));
8296498aff2SJonas Devlieghere  PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb));
8309d5e37edSPavel Labath  PythonObject cmd_retobj_arg(PyRefType::Owned,
8319d5e37edSPavel Labath                              SBTypeToSWIGWrapper(cmd_retobj_sb));
8326498aff2SJonas Devlieghere
8336498aff2SJonas Devlieghere  if (argc.get().max_positional_args < 5u)
8346498aff2SJonas Devlieghere    pfunc(debugger_arg, PythonString(args), cmd_retobj_arg, dict);
8356498aff2SJonas Devlieghere  else
8366498aff2SJonas Devlieghere    pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg, dict);
8376498aff2SJonas Devlieghere
8386498aff2SJonas Devlieghere  return true;
8396498aff2SJonas Devlieghere}
8406498aff2SJonas Devlieghere
8419d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonCallCommandObject(
8427406d236SPavel Labath    PyObject *implementor, lldb::DebuggerSP debugger, const char *args,
8436498aff2SJonas Devlieghere    lldb_private::CommandReturnObject &cmd_retobj,
8449d5e37edSPavel Labath    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
8456498aff2SJonas Devlieghere  lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
8466498aff2SJonas Devlieghere  lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
8476498aff2SJonas Devlieghere
8486498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
8496498aff2SJonas Devlieghere
8506498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, implementor);
8516498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>("__call__");
8526498aff2SJonas Devlieghere
8536498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
8546498aff2SJonas Devlieghere    return false;
8556498aff2SJonas Devlieghere
8566498aff2SJonas Devlieghere  PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb));
8579d5e37edSPavel Labath  PythonObject cmd_retobj_arg(PyRefType::Owned,
8589d5e37edSPavel Labath                              SBTypeToSWIGWrapper(cmd_retobj_sb));
8596498aff2SJonas Devlieghere
8607406d236SPavel Labath  pfunc(ToSWIGWrapper(std::move(debugger)), PythonString(args), exe_ctx_arg,
8617406d236SPavel Labath        cmd_retobj_arg);
8626498aff2SJonas Devlieghere
8636498aff2SJonas Devlieghere  return true;
8646498aff2SJonas Devlieghere}
8656498aff2SJonas Devlieghere
8669d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPythonCreateOSPlugin(
8679d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
8689d5e37edSPavel Labath    const lldb::ProcessSP &process_sp) {
8699d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
8709d5e37edSPavel Labath      !session_dictionary_name)
8716498aff2SJonas Devlieghere    Py_RETURN_NONE;
8726498aff2SJonas Devlieghere
8736498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
8746498aff2SJonas Devlieghere
8759d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
8769d5e37edSPavel Labath      session_dictionary_name);
8779d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
8789d5e37edSPavel Labath      python_class_name, dict);
8796498aff2SJonas Devlieghere
8806498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
8816498aff2SJonas Devlieghere    Py_RETURN_NONE;
8826498aff2SJonas Devlieghere
8837f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(process_sp));
8846498aff2SJonas Devlieghere
8856498aff2SJonas Devlieghere  if (result.IsAllocated())
8866498aff2SJonas Devlieghere    return result.release();
8876498aff2SJonas Devlieghere
8886498aff2SJonas Devlieghere  Py_RETURN_NONE;
8896498aff2SJonas Devlieghere}
8906498aff2SJonas Devlieghere
8919d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CreateFrameRecognizer(
8929d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name) {
8939d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
8949d5e37edSPavel Labath      !session_dictionary_name)
8956498aff2SJonas Devlieghere    Py_RETURN_NONE;
8966498aff2SJonas Devlieghere
8976498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
8986498aff2SJonas Devlieghere
8999d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9009d5e37edSPavel Labath      session_dictionary_name);
9019d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9029d5e37edSPavel Labath      python_class_name, dict);
9036498aff2SJonas Devlieghere
9046498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
9056498aff2SJonas Devlieghere    Py_RETURN_NONE;
9066498aff2SJonas Devlieghere
9076498aff2SJonas Devlieghere  auto result = pfunc();
9086498aff2SJonas Devlieghere
9096498aff2SJonas Devlieghere  if (result.IsAllocated())
9106498aff2SJonas Devlieghere    return result.release();
9116498aff2SJonas Devlieghere
9126498aff2SJonas Devlieghere  Py_RETURN_NONE;
9136498aff2SJonas Devlieghere}
9146498aff2SJonas Devlieghere
9159d5e37edSPavel LabathPyObject *lldb_private::LLDBSwigPython_GetRecognizedArguments(
9169d5e37edSPavel Labath    PyObject * implementor, const lldb::StackFrameSP &frame_sp) {
9176498aff2SJonas Devlieghere  static char callee_name[] = "get_recognized_arguments";
9186498aff2SJonas Devlieghere
9197406d236SPavel Labath  PythonObject arg = ToSWIGWrapper(frame_sp);
9206498aff2SJonas Devlieghere
9216498aff2SJonas Devlieghere  PythonString str(callee_name);
9229d5e37edSPavel Labath  PyObject *result =
923*6c2bf012SPavel Labath      PyObject_CallMethodObjArgs(implementor, str.get(), arg.get(), NULL);
9246498aff2SJonas Devlieghere  return result;
9256498aff2SJonas Devlieghere}
9266498aff2SJonas Devlieghere
9279d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_GetDynamicSetting(
9289d5e37edSPavel Labath    void *module, const char *setting, const lldb::TargetSP &target_sp) {
9296498aff2SJonas Devlieghere  if (!module || !setting)
9306498aff2SJonas Devlieghere    Py_RETURN_NONE;
9316498aff2SJonas Devlieghere
9326498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
9336498aff2SJonas Devlieghere  PythonObject py_module(PyRefType::Borrowed, (PyObject *)module);
9346498aff2SJonas Devlieghere  auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting");
9356498aff2SJonas Devlieghere
9366498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
9376498aff2SJonas Devlieghere    Py_RETURN_NONE;
9386498aff2SJonas Devlieghere
9397f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(target_sp), PythonString(setting));
9406498aff2SJonas Devlieghere
9416498aff2SJonas Devlieghere  return result.release();
9426498aff2SJonas Devlieghere}
9436498aff2SJonas Devlieghere
9449a14adeaSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordProcess(
9457f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
9467f09ab08SPavel Labath    const lldb::ProcessSP &process, std::string &output) {
9476498aff2SJonas Devlieghere
9489d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9499d5e37edSPavel Labath      !session_dictionary_name)
9506498aff2SJonas Devlieghere    return false;
9516498aff2SJonas Devlieghere
9526498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
9536498aff2SJonas Devlieghere
9549d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9559d5e37edSPavel Labath      session_dictionary_name);
9569d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9579d5e37edSPavel Labath      python_function_name, dict);
9586498aff2SJonas Devlieghere
9596498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
9606498aff2SJonas Devlieghere    return false;
9616498aff2SJonas Devlieghere
9627f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(process), dict);
9636498aff2SJonas Devlieghere
9646498aff2SJonas Devlieghere  output = result.Str().GetString().str();
9656498aff2SJonas Devlieghere
9666498aff2SJonas Devlieghere  return true;
9676498aff2SJonas Devlieghere}
9686498aff2SJonas Devlieghere
9697406d236SPavel Labathllvm::Optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordThread(
9709d5e37edSPavel Labath    const char *python_function_name, const char *session_dictionary_name,
9717406d236SPavel Labath    lldb::ThreadSP thread) {
9729d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9739d5e37edSPavel Labath      !session_dictionary_name)
9747406d236SPavel Labath    return llvm::None;
9756498aff2SJonas Devlieghere
9766498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
9776498aff2SJonas Devlieghere
9789d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
9799d5e37edSPavel Labath      session_dictionary_name);
9809d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
9819d5e37edSPavel Labath      python_function_name, dict);
9826498aff2SJonas Devlieghere
9836498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
9847406d236SPavel Labath    return llvm::None;
9856498aff2SJonas Devlieghere
9867406d236SPavel Labath  auto result = pfunc(ToSWIGWrapper(std::move(thread)), dict);
9876498aff2SJonas Devlieghere
9887406d236SPavel Labath  return result.Str().GetString().str();
9896498aff2SJonas Devlieghere}
9906498aff2SJonas Devlieghere
9919a14adeaSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordTarget(
9927f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
9937f09ab08SPavel Labath    const lldb::TargetSP &target, std::string &output) {
9946498aff2SJonas Devlieghere
9959d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
9969d5e37edSPavel Labath      !session_dictionary_name)
9976498aff2SJonas Devlieghere    return false;
9986498aff2SJonas Devlieghere
9996498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
10006498aff2SJonas Devlieghere
10019d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10029d5e37edSPavel Labath      session_dictionary_name);
10039d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10049d5e37edSPavel Labath      python_function_name, dict);
10056498aff2SJonas Devlieghere
10066498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
10076498aff2SJonas Devlieghere    return false;
10086498aff2SJonas Devlieghere
10097f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(target), dict);
10106498aff2SJonas Devlieghere
10116498aff2SJonas Devlieghere  output = result.Str().GetString().str();
10126498aff2SJonas Devlieghere
10136498aff2SJonas Devlieghere  return true;
10146498aff2SJonas Devlieghere}
10156498aff2SJonas Devlieghere
10167406d236SPavel Labathllvm::Optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordFrame(
10179d5e37edSPavel Labath    const char *python_function_name, const char *session_dictionary_name,
10187406d236SPavel Labath    lldb::StackFrameSP frame) {
10199d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
10209d5e37edSPavel Labath      !session_dictionary_name)
10217406d236SPavel Labath    return llvm::None;
10226498aff2SJonas Devlieghere
10236498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
10246498aff2SJonas Devlieghere
10259d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10269d5e37edSPavel Labath      session_dictionary_name);
10279d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10289d5e37edSPavel Labath      python_function_name, dict);
10296498aff2SJonas Devlieghere
10306498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
10317406d236SPavel Labath    return llvm::None;
10326498aff2SJonas Devlieghere
10337406d236SPavel Labath  auto result = pfunc(ToSWIGWrapper(std::move(frame)), dict);
10346498aff2SJonas Devlieghere
10357406d236SPavel Labath  return result.Str().GetString().str();
10366498aff2SJonas Devlieghere}
10376498aff2SJonas Devlieghere
10389a14adeaSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordValue(
10397f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
10407f09ab08SPavel Labath    const lldb::ValueObjectSP &value, std::string &output) {
10416498aff2SJonas Devlieghere
10429d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
10439d5e37edSPavel Labath      !session_dictionary_name)
10446498aff2SJonas Devlieghere    return false;
10456498aff2SJonas Devlieghere
10466498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
10476498aff2SJonas Devlieghere
10489d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10499d5e37edSPavel Labath      session_dictionary_name);
10509d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10519d5e37edSPavel Labath      python_function_name, dict);
10526498aff2SJonas Devlieghere
10536498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
10546498aff2SJonas Devlieghere    return false;
10556498aff2SJonas Devlieghere
10567f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(value), dict);
10576498aff2SJonas Devlieghere
10586498aff2SJonas Devlieghere  output = result.Str().GetString().str();
10596498aff2SJonas Devlieghere
10606498aff2SJonas Devlieghere  return true;
10616498aff2SJonas Devlieghere}
10626498aff2SJonas Devlieghere
10639d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonCallModuleInit(
10649d5e37edSPavel Labath    const char *python_module_name, const char *session_dictionary_name,
10657406d236SPavel Labath    lldb::DebuggerSP debugger) {
10666498aff2SJonas Devlieghere  std::string python_function_name_string = python_module_name;
10676498aff2SJonas Devlieghere  python_function_name_string += ".__lldb_init_module";
10686498aff2SJonas Devlieghere  const char *python_function_name = python_function_name_string.c_str();
10696498aff2SJonas Devlieghere
10706498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
10716498aff2SJonas Devlieghere
10729d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
10739d5e37edSPavel Labath      session_dictionary_name);
10749d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
10759d5e37edSPavel Labath      python_function_name, dict);
10766498aff2SJonas Devlieghere
10776498aff2SJonas Devlieghere  // This method is optional and need not exist.  So if we don't find it,
10786498aff2SJonas Devlieghere  // it's actually a success, not a failure.
10796498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
10806498aff2SJonas Devlieghere    return true;
10816498aff2SJonas Devlieghere
10827406d236SPavel Labath  pfunc(ToSWIGWrapper(std::move(debugger)), dict);
10836498aff2SJonas Devlieghere
10846498aff2SJonas Devlieghere  return true;
10856498aff2SJonas Devlieghere}
10866498aff2SJonas Devlieghere
10879d5e37edSPavel Labathlldb::ValueObjectSP lldb_private::LLDBSWIGPython_GetValueObjectSPFromSBValue(
10889d5e37edSPavel Labath    void *data) {
10896498aff2SJonas Devlieghere  lldb::ValueObjectSP valobj_sp;
10909d5e37edSPavel Labath  if (data) {
10916498aff2SJonas Devlieghere    lldb::SBValue *sb_ptr = (lldb::SBValue *)data;
10926498aff2SJonas Devlieghere    valobj_sp = sb_ptr->GetSP();
10936498aff2SJonas Devlieghere  }
10946498aff2SJonas Devlieghere  return valobj_sp;
10956498aff2SJonas Devlieghere}
10966498aff2SJonas Devlieghere
10976498aff2SJonas Devlieghere// For the LogOutputCallback functions
10989d5e37edSPavel Labathstatic void LLDBSwigPythonCallPythonLogOutputCallback(const char *str,
10999d5e37edSPavel Labath                                                      void *baton) {
11006498aff2SJonas Devlieghere  if (baton != Py_None) {
11016498aff2SJonas Devlieghere    SWIG_PYTHON_THREAD_BEGIN_BLOCK;
11029d5e37edSPavel Labath    PyObject *result = PyObject_CallFunction(
11039d5e37edSPavel Labath        reinterpret_cast<PyObject *>(baton), const_cast<char *>("s"), str);
11046498aff2SJonas Devlieghere    Py_XDECREF(result);
11056498aff2SJonas Devlieghere    SWIG_PYTHON_THREAD_END_BLOCK;
11066498aff2SJonas Devlieghere  }
11076498aff2SJonas Devlieghere}
11086498aff2SJonas Devlieghere%}
1109