16498aff2SJonas Devlieghere%header %{
26498aff2SJonas Devlieghere
3*9d5e37edSPavel Labathclass PyErr_Cleaner {
46498aff2SJonas Devliegherepublic:
5*9d5e37edSPavel Labath  PyErr_Cleaner(bool print = false) : m_print(print) {}
66498aff2SJonas Devlieghere
7*9d5e37edSPavel Labath  ~PyErr_Cleaner() {
8*9d5e37edSPavel 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
19*9d5e37edSPavel Labathllvm::Expected<bool> lldb_private::LLDBSwigPythonBreakpointCallbackFunction(
20*9d5e37edSPavel 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,
23*9d5e37edSPavel Labath    const lldb_private::StructuredDataImpl &args_impl) {
246498aff2SJonas Devlieghere  using namespace llvm;
256498aff2SJonas Devlieghere
266498aff2SJonas Devlieghere  lldb::SBFrame sb_frame(frame_sp);
276498aff2SJonas Devlieghere  lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
286498aff2SJonas Devlieghere
296498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
30*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
31*9d5e37edSPavel Labath      session_dictionary_name);
32*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
33*9d5e37edSPavel Labath      python_function_name, dict);
346498aff2SJonas Devlieghere
356498aff2SJonas Devlieghere  unsigned max_positional_args;
366498aff2SJonas Devlieghere  if (auto arg_info = pfunc.GetArgInfo())
376498aff2SJonas Devlieghere    max_positional_args = arg_info.get().max_positional_args;
386498aff2SJonas Devlieghere  else
396498aff2SJonas Devlieghere    return arg_info.takeError();
406498aff2SJonas Devlieghere
416498aff2SJonas Devlieghere  PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame));
426498aff2SJonas Devlieghere  PythonObject bp_loc_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_bp_loc));
436498aff2SJonas Devlieghere
446498aff2SJonas Devlieghere  auto result = [&]() -> Expected<PythonObject> {
456498aff2SJonas Devlieghere    // If the called function doesn't take extra_args, drop them here:
46ebb6bb72SPavel Labath    if (max_positional_args < 4)
476498aff2SJonas Devlieghere      return pfunc.Call(frame_arg, bp_loc_arg, dict);
48ebb6bb72SPavel Labath    return pfunc.Call(frame_arg, bp_loc_arg, ToSWIGWrapper(args_impl), dict);
496498aff2SJonas Devlieghere  }();
506498aff2SJonas Devlieghere
516498aff2SJonas Devlieghere  if (!result)
526498aff2SJonas Devlieghere    return result.takeError();
536498aff2SJonas Devlieghere
546498aff2SJonas Devlieghere  // Only False counts as false!
556498aff2SJonas Devlieghere  return result.get().get() != Py_False;
566498aff2SJonas Devlieghere}
576498aff2SJonas Devlieghere
589a14adeaSPavel Labath// resolve a dotted Python name in the form
599a14adeaSPavel Labath// foo.bar.baz.Foobar to an actual Python object
609a14adeaSPavel Labath// if pmodule is NULL, the __main__ module will be used
619a14adeaSPavel Labath// as the starting point for the search
62daf36998SDave Lee
63*9d5e37edSPavel Labath// This function is called by
64*9d5e37edSPavel Labath// lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...) and is
65*9d5e37edSPavel Labath// used when a script command is attached to a breakpoint for execution.
669a14adeaSPavel Labath
67*9d5e37edSPavel Labath// This function is called by
68*9d5e37edSPavel Labath// lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...) and is
69*9d5e37edSPavel Labath// used when a script command is attached to a watchpoint for execution.
70daf36998SDave Lee
71*9d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonWatchpointCallbackFunction(
72*9d5e37edSPavel Labath    const char *python_function_name, const char *session_dictionary_name,
73*9d5e37edSPavel Labath    const lldb::StackFrameSP &frame_sp, const lldb::WatchpointSP &wp_sp) {
746498aff2SJonas Devlieghere  lldb::SBFrame sb_frame(frame_sp);
756498aff2SJonas Devlieghere  lldb::SBWatchpoint sb_wp(wp_sp);
766498aff2SJonas Devlieghere
776498aff2SJonas Devlieghere  bool stop_at_watchpoint = true;
786498aff2SJonas Devlieghere
796498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
806498aff2SJonas Devlieghere
81*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
82*9d5e37edSPavel Labath      session_dictionary_name);
83*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
84*9d5e37edSPavel Labath      python_function_name, dict);
856498aff2SJonas Devlieghere
866498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
876498aff2SJonas Devlieghere    return stop_at_watchpoint;
886498aff2SJonas Devlieghere
896498aff2SJonas Devlieghere  PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame));
906498aff2SJonas Devlieghere  PythonObject wp_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_wp));
916498aff2SJonas Devlieghere  PythonObject result = pfunc(frame_arg, wp_arg, dict);
926498aff2SJonas Devlieghere
936498aff2SJonas Devlieghere  if (result.get() == Py_False)
946498aff2SJonas Devlieghere    stop_at_watchpoint = false;
956498aff2SJonas Devlieghere
966498aff2SJonas Devlieghere  return stop_at_watchpoint;
976498aff2SJonas Devlieghere}
986498aff2SJonas Devlieghere
99*9d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonCallTypeScript(
100*9d5e37edSPavel Labath    const char *python_function_name, const void *session_dictionary,
101*9d5e37edSPavel Labath    const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper,
102*9d5e37edSPavel Labath    const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval) {
1036498aff2SJonas Devlieghere  lldb::SBTypeSummaryOptions sb_options(options_sp.get());
1046498aff2SJonas Devlieghere
1056498aff2SJonas Devlieghere  retval.clear();
1066498aff2SJonas Devlieghere
1076498aff2SJonas Devlieghere  if (!python_function_name || !session_dictionary)
1086498aff2SJonas Devlieghere    return false;
1096498aff2SJonas Devlieghere
1106498aff2SJonas Devlieghere  PyObject *pfunc_impl = nullptr;
1116498aff2SJonas Devlieghere
112*9d5e37edSPavel Labath  if (pyfunct_wrapper && *pyfunct_wrapper &&
113*9d5e37edSPavel Labath      PyFunction_Check(*pyfunct_wrapper)) {
1146498aff2SJonas Devlieghere    pfunc_impl = (PyObject *)(*pyfunct_wrapper);
115*9d5e37edSPavel Labath    if (pfunc_impl->ob_refcnt == 1) {
1166498aff2SJonas Devlieghere      Py_XDECREF(pfunc_impl);
1176498aff2SJonas Devlieghere      pfunc_impl = NULL;
1186498aff2SJonas Devlieghere    }
1196498aff2SJonas Devlieghere  }
1206498aff2SJonas Devlieghere
1216498aff2SJonas Devlieghere  PyObject *py_dict = (PyObject *)session_dictionary;
1226498aff2SJonas Devlieghere  if (!PythonDictionary::Check(py_dict))
1236498aff2SJonas Devlieghere    return true;
1246498aff2SJonas Devlieghere
1256498aff2SJonas Devlieghere  PythonDictionary dict(PyRefType::Borrowed, py_dict);
1266498aff2SJonas Devlieghere
1276498aff2SJonas Devlieghere  PyErr_Cleaner pyerr_cleanup(true); // show Python errors
1286498aff2SJonas Devlieghere
1296498aff2SJonas Devlieghere  PythonCallable pfunc(PyRefType::Borrowed, pfunc_impl);
1306498aff2SJonas Devlieghere
131*9d5e37edSPavel Labath  if (!pfunc.IsAllocated()) {
132*9d5e37edSPavel Labath    pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
133*9d5e37edSPavel Labath        python_function_name, dict);
1346498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
1356498aff2SJonas Devlieghere      return false;
1366498aff2SJonas Devlieghere
137*9d5e37edSPavel Labath    if (pyfunct_wrapper) {
1386498aff2SJonas Devlieghere      *pyfunct_wrapper = pfunc.get();
1396498aff2SJonas Devlieghere      Py_XINCREF(pfunc.get());
1406498aff2SJonas Devlieghere    }
1416498aff2SJonas Devlieghere  }
1426498aff2SJonas Devlieghere
1436498aff2SJonas Devlieghere  PythonObject result;
1446498aff2SJonas Devlieghere  auto argc = pfunc.GetArgInfo();
1456498aff2SJonas Devlieghere  if (!argc) {
1466498aff2SJonas Devlieghere    llvm::consumeError(argc.takeError());
1476498aff2SJonas Devlieghere    return false;
1486498aff2SJonas Devlieghere  }
1496498aff2SJonas Devlieghere
1507f09ab08SPavel Labath  PythonObject value_arg = ToSWIGWrapper(valobj_sp);
1516498aff2SJonas Devlieghere  PythonObject options_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_options));
1526498aff2SJonas Devlieghere
1536498aff2SJonas Devlieghere  if (argc.get().max_positional_args < 3)
1546498aff2SJonas Devlieghere    result = pfunc(value_arg, dict);
1556498aff2SJonas Devlieghere  else
1566498aff2SJonas Devlieghere    result = pfunc(value_arg, dict, options_arg);
1576498aff2SJonas Devlieghere
1586498aff2SJonas Devlieghere  retval = result.Str().GetString().str();
1596498aff2SJonas Devlieghere
1606498aff2SJonas Devlieghere  return true;
1616498aff2SJonas Devlieghere}
1626498aff2SJonas Devlieghere
163*9d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateSyntheticProvider(
164*9d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
165*9d5e37edSPavel Labath    const lldb::ValueObjectSP &valobj_sp) {
166*9d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
167*9d5e37edSPavel Labath      !session_dictionary_name)
1686498aff2SJonas Devlieghere    Py_RETURN_NONE;
1696498aff2SJonas Devlieghere
1706498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
1716498aff2SJonas Devlieghere
172*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
173*9d5e37edSPavel Labath      session_dictionary_name);
174*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
175*9d5e37edSPavel Labath      python_class_name, dict);
1766498aff2SJonas Devlieghere
1776498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
1786498aff2SJonas Devlieghere    Py_RETURN_NONE;
1796498aff2SJonas Devlieghere
1807f09ab08SPavel Labath  auto sb_value = std::make_unique<lldb::SBValue>(valobj_sp);
1816498aff2SJonas Devlieghere  sb_value->SetPreferSyntheticValue(false);
1826498aff2SJonas Devlieghere
1837f09ab08SPavel Labath  PythonObject val_arg = ToSWIGWrapper(std::move(sb_value));
1846498aff2SJonas Devlieghere  if (!val_arg.IsAllocated())
1856498aff2SJonas Devlieghere    Py_RETURN_NONE;
1866498aff2SJonas Devlieghere
1876498aff2SJonas Devlieghere  PythonObject result = pfunc(val_arg, dict);
1886498aff2SJonas Devlieghere
1896498aff2SJonas Devlieghere  if (result.IsAllocated())
1906498aff2SJonas Devlieghere    return result.release();
1916498aff2SJonas Devlieghere
1926498aff2SJonas Devlieghere  Py_RETURN_NONE;
1936498aff2SJonas Devlieghere}
1946498aff2SJonas Devlieghere
195*9d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateCommandObject(
196*9d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
197*9d5e37edSPavel Labath    const lldb::DebuggerSP debugger_sp) {
198*9d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
199*9d5e37edSPavel Labath      !session_dictionary_name)
2006498aff2SJonas Devlieghere    Py_RETURN_NONE;
2016498aff2SJonas Devlieghere
2026498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
203*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
204*9d5e37edSPavel Labath      session_dictionary_name);
205*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
206*9d5e37edSPavel Labath      python_class_name, dict);
2076498aff2SJonas Devlieghere
2086498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
2096498aff2SJonas Devlieghere    return nullptr;
2106498aff2SJonas Devlieghere
2116498aff2SJonas Devlieghere  lldb::SBDebugger debugger_sb(debugger_sp);
2126498aff2SJonas Devlieghere  PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
2136498aff2SJonas Devlieghere  PythonObject result = pfunc(debugger_arg, dict);
2146498aff2SJonas Devlieghere
2156498aff2SJonas Devlieghere  if (result.IsAllocated())
2166498aff2SJonas Devlieghere    return result.release();
2176498aff2SJonas Devlieghere
2186498aff2SJonas Devlieghere  Py_RETURN_NONE;
2196498aff2SJonas Devlieghere}
2206498aff2SJonas Devlieghere
221*9d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedProcess(
222*9d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
2231f6a57c1SMed Ismail Bennani    const lldb::TargetSP &target_sp,
22482de8df2SPavel Labath    const lldb_private::StructuredDataImpl &args_impl,
225*9d5e37edSPavel Labath    std::string &error_string) {
226*9d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
227*9d5e37edSPavel Labath      !session_dictionary_name)
2281f6a57c1SMed Ismail Bennani    Py_RETURN_NONE;
2291f6a57c1SMed Ismail Bennani
2301f6a57c1SMed Ismail Bennani  PyErr_Cleaner py_err_cleaner(true);
2311f6a57c1SMed Ismail Bennani
232*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
233*9d5e37edSPavel Labath      session_dictionary_name);
234*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
235*9d5e37edSPavel Labath      python_class_name, dict);
2361f6a57c1SMed Ismail Bennani
2371f6a57c1SMed Ismail Bennani  if (!pfunc.IsAllocated()) {
2381f6a57c1SMed Ismail Bennani    error_string.append("could not find script class: ");
2391f6a57c1SMed Ismail Bennani    error_string.append(python_class_name);
2401f6a57c1SMed Ismail Bennani    return nullptr;
2411f6a57c1SMed Ismail Bennani  }
2421f6a57c1SMed Ismail Bennani
2437f09ab08SPavel Labath  PythonObject target_arg = ToSWIGWrapper(target_sp);
2441f6a57c1SMed Ismail Bennani
2451f6a57c1SMed Ismail Bennani  llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
2461f6a57c1SMed Ismail Bennani  if (!arg_info) {
2471f6a57c1SMed Ismail Bennani    llvm::handleAllErrors(
2481f6a57c1SMed Ismail Bennani        arg_info.takeError(),
249*9d5e37edSPavel Labath        [&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
2501f6a57c1SMed Ismail Bennani        [&](const llvm::ErrorInfoBase &E) {
2511f6a57c1SMed Ismail Bennani          error_string.append(E.message());
2521f6a57c1SMed Ismail Bennani        });
2531f6a57c1SMed Ismail Bennani    Py_RETURN_NONE;
2541f6a57c1SMed Ismail Bennani  }
2551f6a57c1SMed Ismail Bennani
2561f6a57c1SMed Ismail Bennani  PythonObject result = {};
2571f6a57c1SMed Ismail Bennani  if (arg_info.get().max_positional_args == 2) {
258ebb6bb72SPavel Labath    result = pfunc(target_arg, ToSWIGWrapper(args_impl));
2591f6a57c1SMed Ismail Bennani  } else {
260*9d5e37edSPavel Labath    error_string.assign("wrong number of arguments in __init__, should be 2 "
261*9d5e37edSPavel Labath                        "(not including self)");
2621f6a57c1SMed Ismail Bennani    Py_RETURN_NONE;
2631f6a57c1SMed Ismail Bennani  }
2641f6a57c1SMed Ismail Bennani
2651f6a57c1SMed Ismail Bennani  if (result.IsAllocated())
2661f6a57c1SMed Ismail Bennani    return result.release();
2671f6a57c1SMed Ismail Bennani  Py_RETURN_NONE;
2681f6a57c1SMed Ismail Bennani}
2691f6a57c1SMed Ismail Bennani
270*9d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedThread(
271*9d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
272*9d5e37edSPavel Labath    const lldb::ProcessSP &process_sp, const StructuredDataImpl &args_impl,
273*9d5e37edSPavel Labath    std::string &error_string) {
274*9d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
275*9d5e37edSPavel Labath      !session_dictionary_name)
27659d8dd79SMed Ismail Bennani    Py_RETURN_NONE;
27759d8dd79SMed Ismail Bennani
27859d8dd79SMed Ismail Bennani  PyErr_Cleaner py_err_cleaner(true);
27959d8dd79SMed Ismail Bennani
280*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
281*9d5e37edSPavel Labath      session_dictionary_name);
282*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
283*9d5e37edSPavel Labath      python_class_name, dict);
28459d8dd79SMed Ismail Bennani
28559d8dd79SMed Ismail Bennani  if (!pfunc.IsAllocated()) {
28659d8dd79SMed Ismail Bennani    error_string.append("could not find script class: ");
28759d8dd79SMed Ismail Bennani    error_string.append(python_class_name);
28859d8dd79SMed Ismail Bennani    return nullptr;
28959d8dd79SMed Ismail Bennani  }
29059d8dd79SMed Ismail Bennani
29159d8dd79SMed Ismail Bennani  llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
29259d8dd79SMed Ismail Bennani  if (!arg_info) {
29359d8dd79SMed Ismail Bennani    llvm::handleAllErrors(
29459d8dd79SMed Ismail Bennani        arg_info.takeError(),
295*9d5e37edSPavel Labath        [&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
29659d8dd79SMed Ismail Bennani        [&](const llvm::ErrorInfoBase &E) {
29759d8dd79SMed Ismail Bennani          error_string.append(E.message());
29859d8dd79SMed Ismail Bennani        });
29959d8dd79SMed Ismail Bennani    Py_RETURN_NONE;
30059d8dd79SMed Ismail Bennani  }
30159d8dd79SMed Ismail Bennani
30259d8dd79SMed Ismail Bennani  PythonObject result = {};
303738621d0SMed Ismail Bennani  if (arg_info.get().max_positional_args == 2) {
304ebb6bb72SPavel Labath    result = pfunc(ToSWIGWrapper(process_sp), ToSWIGWrapper(args_impl));
30559d8dd79SMed Ismail Bennani  } else {
306*9d5e37edSPavel Labath    error_string.assign("wrong number of arguments in __init__, should be 2 "
307*9d5e37edSPavel Labath                        "(not including self)");
30859d8dd79SMed Ismail Bennani    Py_RETURN_NONE;
30959d8dd79SMed Ismail Bennani  }
31059d8dd79SMed Ismail Bennani
31159d8dd79SMed Ismail Bennani  if (result.IsAllocated())
31259d8dd79SMed Ismail Bennani    return result.release();
31359d8dd79SMed Ismail Bennani  Py_RETURN_NONE;
31459d8dd79SMed Ismail Bennani}
31559d8dd79SMed Ismail Bennani
316*9d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
317*9d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
31882de8df2SPavel Labath    const lldb_private::StructuredDataImpl &args_impl,
319*9d5e37edSPavel Labath    std::string &error_string, const lldb::ThreadPlanSP &thread_plan_sp) {
320*9d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
321*9d5e37edSPavel Labath      !session_dictionary_name)
3226498aff2SJonas Devlieghere    Py_RETURN_NONE;
3236498aff2SJonas Devlieghere
3246498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
3256498aff2SJonas Devlieghere
326*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
327*9d5e37edSPavel Labath      session_dictionary_name);
328*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
329*9d5e37edSPavel Labath      python_class_name, dict);
3306498aff2SJonas Devlieghere
3316498aff2SJonas Devlieghere  if (!pfunc.IsAllocated()) {
3326498aff2SJonas Devlieghere    error_string.append("could not find script class: ");
3336498aff2SJonas Devlieghere    error_string.append(python_class_name);
3346498aff2SJonas Devlieghere    return nullptr;
3356498aff2SJonas Devlieghere  }
3366498aff2SJonas Devlieghere
3377f09ab08SPavel Labath  PythonObject tp_arg = ToSWIGWrapper(thread_plan_sp);
3386498aff2SJonas Devlieghere
3396498aff2SJonas Devlieghere  llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
3406498aff2SJonas Devlieghere  if (!arg_info) {
3416498aff2SJonas Devlieghere    llvm::handleAllErrors(
3426498aff2SJonas Devlieghere        arg_info.takeError(),
343*9d5e37edSPavel Labath        [&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
3446498aff2SJonas Devlieghere        [&](const llvm::ErrorInfoBase &E) {
3456498aff2SJonas Devlieghere          error_string.append(E.message());
3466498aff2SJonas Devlieghere        });
3476498aff2SJonas Devlieghere    Py_RETURN_NONE;
3486498aff2SJonas Devlieghere  }
3496498aff2SJonas Devlieghere
3506498aff2SJonas Devlieghere  PythonObject result = {};
351ebb6bb72SPavel Labath  auto args_sb = std::make_unique<lldb::SBStructuredData>(args_impl);
3526498aff2SJonas Devlieghere  if (arg_info.get().max_positional_args == 2) {
35382de8df2SPavel Labath    if (args_sb->IsValid()) {
354*9d5e37edSPavel Labath      error_string.assign(
355*9d5e37edSPavel Labath          "args passed, but __init__ does not take an args dictionary");
3566498aff2SJonas Devlieghere      Py_RETURN_NONE;
3576498aff2SJonas Devlieghere    }
3586498aff2SJonas Devlieghere    result = pfunc(tp_arg, dict);
3596498aff2SJonas Devlieghere  } else if (arg_info.get().max_positional_args >= 3) {
360ebb6bb72SPavel Labath    result = pfunc(tp_arg, ToSWIGWrapper(std::move(args_sb)), dict);
3616498aff2SJonas Devlieghere  } else {
362*9d5e37edSPavel Labath    error_string.assign("wrong number of arguments in __init__, should be 2 or "
363*9d5e37edSPavel Labath                        "3 (not including self)");
3646498aff2SJonas Devlieghere    Py_RETURN_NONE;
3656498aff2SJonas Devlieghere  }
3666498aff2SJonas Devlieghere
367*9d5e37edSPavel Labath  // FIXME: At this point we should check that the class we found supports all
368*9d5e37edSPavel Labath  // the methods that we need.
3696498aff2SJonas Devlieghere
3706498aff2SJonas Devlieghere  if (result.IsAllocated())
3716498aff2SJonas Devlieghere    return result.release();
3726498aff2SJonas Devlieghere  Py_RETURN_NONE;
3736498aff2SJonas Devlieghere}
3746498aff2SJonas Devlieghere
375*9d5e37edSPavel Labathbool lldb_private::LLDBSWIGPythonCallThreadPlan(
376*9d5e37edSPavel Labath    void *implementor, const char *method_name, lldb_private::Event *event,
377*9d5e37edSPavel Labath    bool &got_error) {
3786498aff2SJonas Devlieghere  got_error = false;
3796498aff2SJonas Devlieghere
3806498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(false);
3816498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
3826498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>(method_name);
3836498aff2SJonas Devlieghere
3846498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
3856498aff2SJonas Devlieghere    return false;
3866498aff2SJonas Devlieghere
3876498aff2SJonas Devlieghere  PythonObject result;
388*9d5e37edSPavel Labath  if (event != nullptr) {
3896498aff2SJonas Devlieghere    lldb::SBEvent sb_event(event);
3906498aff2SJonas Devlieghere    PythonObject event_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_event));
3916498aff2SJonas Devlieghere    result = pfunc(event_arg);
392*9d5e37edSPavel Labath  } else
3936498aff2SJonas Devlieghere    result = pfunc();
3946498aff2SJonas Devlieghere
395*9d5e37edSPavel Labath  if (PyErr_Occurred()) {
3966498aff2SJonas Devlieghere    got_error = true;
397*9d5e37edSPavel Labath    printf("Return value was neither false nor true for call to %s.\n",
398*9d5e37edSPavel Labath           method_name);
3996498aff2SJonas Devlieghere    PyErr_Print();
4006498aff2SJonas Devlieghere    return false;
4016498aff2SJonas Devlieghere  }
4026498aff2SJonas Devlieghere
4036498aff2SJonas Devlieghere  if (result.get() == Py_True)
4046498aff2SJonas Devlieghere    return true;
4056498aff2SJonas Devlieghere  else if (result.get() == Py_False)
4066498aff2SJonas Devlieghere    return false;
4076498aff2SJonas Devlieghere
4086498aff2SJonas Devlieghere  // Somebody returned the wrong thing...
4096498aff2SJonas Devlieghere  got_error = true;
4106498aff2SJonas Devlieghere  printf("Wrong return value type for call to %s.\n", method_name);
4116498aff2SJonas Devlieghere  return false;
4126498aff2SJonas Devlieghere}
4136498aff2SJonas Devlieghere
4149a14adeaSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
4157f09ab08SPavel Labath    const char *python_class_name, const char *session_dictionary_name,
41682de8df2SPavel Labath    const StructuredDataImpl &args_impl,
4177f09ab08SPavel Labath    const lldb::BreakpointSP &breakpoint_sp) {
4187f09ab08SPavel Labath
419*9d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
420*9d5e37edSPavel Labath      !session_dictionary_name)
4216498aff2SJonas Devlieghere    Py_RETURN_NONE;
4226498aff2SJonas Devlieghere
4236498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
4246498aff2SJonas Devlieghere
425*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
426*9d5e37edSPavel Labath      session_dictionary_name);
427*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
428*9d5e37edSPavel Labath      python_class_name, dict);
4296498aff2SJonas Devlieghere
4306498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
4316498aff2SJonas Devlieghere    return nullptr;
4326498aff2SJonas Devlieghere
433ebb6bb72SPavel Labath  PythonObject result =
434ebb6bb72SPavel Labath      pfunc(ToSWIGWrapper(breakpoint_sp), ToSWIGWrapper(args_impl), dict);
435*9d5e37edSPavel Labath  // FIXME: At this point we should check that the class we found supports all
436*9d5e37edSPavel Labath  // the methods that we need.
4376498aff2SJonas Devlieghere
438*9d5e37edSPavel Labath  if (result.IsAllocated()) {
4396498aff2SJonas Devlieghere    // Check that __callback__ is defined:
4406498aff2SJonas Devlieghere    auto callback_func = result.ResolveName<PythonCallable>("__callback__");
4416498aff2SJonas Devlieghere    if (callback_func.IsAllocated())
4426498aff2SJonas Devlieghere      return result.release();
4436498aff2SJonas Devlieghere    else
4446498aff2SJonas Devlieghere      result.release();
4456498aff2SJonas Devlieghere  }
4466498aff2SJonas Devlieghere  Py_RETURN_NONE;
4476498aff2SJonas Devlieghere}
4486498aff2SJonas Devlieghere
449*9d5e37edSPavel Labathunsigned int lldb_private::LLDBSwigPythonCallBreakpointResolver(
450*9d5e37edSPavel Labath    void *implementor, const char *method_name,
451*9d5e37edSPavel Labath    lldb_private::SymbolContext *sym_ctx) {
4526498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(false);
4536498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
4546498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>(method_name);
4556498aff2SJonas Devlieghere
4566498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
4576498aff2SJonas Devlieghere    return 0;
4586498aff2SJonas Devlieghere
4596498aff2SJonas Devlieghere  PythonObject result;
4606498aff2SJonas Devlieghere  if (sym_ctx != nullptr) {
4616498aff2SJonas Devlieghere    lldb::SBSymbolContext sb_sym_ctx(sym_ctx);
4626498aff2SJonas Devlieghere    PythonObject sym_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_sym_ctx));
4636498aff2SJonas Devlieghere    result = pfunc(sym_ctx_arg);
4646498aff2SJonas Devlieghere  } else
4656498aff2SJonas Devlieghere    result = pfunc();
4666498aff2SJonas Devlieghere
467*9d5e37edSPavel Labath  if (PyErr_Occurred()) {
4686498aff2SJonas Devlieghere    PyErr_Print();
46952712d3fSLawrence D'Anna    PyErr_Clear();
4706498aff2SJonas Devlieghere    return 0;
4716498aff2SJonas Devlieghere  }
4726498aff2SJonas Devlieghere
4736498aff2SJonas Devlieghere  // The callback will return a bool, but we're need to also return ints
4746498aff2SJonas Devlieghere  // so we're squirrelling the bool through as an int...  And if you return
4756498aff2SJonas Devlieghere  // nothing, we'll continue.
4766498aff2SJonas Devlieghere  if (strcmp(method_name, "__callback__") == 0) {
4776498aff2SJonas Devlieghere    if (result.get() == Py_False)
4786498aff2SJonas Devlieghere      return 0;
4796498aff2SJonas Devlieghere    else
4806498aff2SJonas Devlieghere      return 1;
4816498aff2SJonas Devlieghere  }
4826498aff2SJonas Devlieghere
48352712d3fSLawrence D'Anna  long long ret_val = unwrapOrSetPythonException(As<long long>(result));
484478619cfSMuhammad Omair Javaid
48552712d3fSLawrence D'Anna  if (PyErr_Occurred()) {
48652712d3fSLawrence D'Anna    PyErr_Print();
48752712d3fSLawrence D'Anna    PyErr_Clear();
48852712d3fSLawrence D'Anna    return 0;
48952712d3fSLawrence D'Anna  }
4906498aff2SJonas Devlieghere
4916498aff2SJonas Devlieghere  return ret_val;
4926498aff2SJonas Devlieghere}
4936498aff2SJonas Devlieghere
494*9d5e37edSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedStopHook(
495*9d5e37edSPavel Labath    lldb::TargetSP target_sp, const char *python_class_name,
496*9d5e37edSPavel Labath    const char *session_dictionary_name, const StructuredDataImpl &args_impl,
497*9d5e37edSPavel Labath    Status &error) {
4981b1d9815SJim Ingham  if (python_class_name == NULL || python_class_name[0] == '\0') {
4991b1d9815SJim Ingham    error.SetErrorString("Empty class name.");
5001b1d9815SJim Ingham    Py_RETURN_NONE;
5011b1d9815SJim Ingham  }
5021b1d9815SJim Ingham  if (!session_dictionary_name) {
5031b1d9815SJim Ingham    error.SetErrorString("No session dictionary");
5041b1d9815SJim Ingham    Py_RETURN_NONE;
5051b1d9815SJim Ingham  }
5061b1d9815SJim Ingham
5071b1d9815SJim Ingham  PyErr_Cleaner py_err_cleaner(true);
5081b1d9815SJim Ingham
509*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
5101b1d9815SJim Ingham      session_dictionary_name);
511*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
5121b1d9815SJim Ingham      python_class_name, dict);
5131b1d9815SJim Ingham
5141b1d9815SJim Ingham  if (!pfunc.IsAllocated()) {
5151b1d9815SJim Ingham    error.SetErrorStringWithFormat("Could not find class: %s.",
5161b1d9815SJim Ingham                                   python_class_name);
5171b1d9815SJim Ingham    return nullptr;
5181b1d9815SJim Ingham  }
5191b1d9815SJim Ingham
520ebb6bb72SPavel Labath  PythonObject result =
521ebb6bb72SPavel Labath      pfunc(ToSWIGWrapper(target_sp), ToSWIGWrapper(args_impl), dict);
5221b1d9815SJim Ingham
523*9d5e37edSPavel Labath  if (result.IsAllocated()) {
5241b1d9815SJim Ingham    // Check that the handle_stop callback is defined:
5251b1d9815SJim Ingham    auto callback_func = result.ResolveName<PythonCallable>("handle_stop");
5261b1d9815SJim Ingham    if (callback_func.IsAllocated()) {
5271b1d9815SJim Ingham      if (auto args_info = callback_func.GetArgInfo()) {
5281b1d9815SJim Ingham        size_t num_args = (*args_info).max_positional_args;
5291b1d9815SJim Ingham        if (num_args != 2) {
530*9d5e37edSPavel Labath          error.SetErrorStringWithFormat(
531*9d5e37edSPavel Labath              "Wrong number of args for "
5322f95c50aSRichard Smith              "handle_stop callback, should be 2 (excluding self), got: %zu",
5331b1d9815SJim Ingham              num_args);
5341b1d9815SJim Ingham          Py_RETURN_NONE;
5351b1d9815SJim Ingham        } else
5361b1d9815SJim Ingham          return result.release();
5371b1d9815SJim Ingham      } else {
5381b1d9815SJim Ingham        error.SetErrorString("Couldn't get num arguments for handle_stop "
5391b1d9815SJim Ingham                             "callback.");
5401b1d9815SJim Ingham        Py_RETURN_NONE;
5411b1d9815SJim Ingham      }
5421b1d9815SJim Ingham      return result.release();
543*9d5e37edSPavel Labath    } else {
5441b1d9815SJim Ingham      error.SetErrorStringWithFormat("Class \"%s\" is missing the required "
5451b1d9815SJim Ingham                                     "handle_stop callback.",
5461b1d9815SJim Ingham                                     python_class_name);
5471b1d9815SJim Ingham      result.release();
5481b1d9815SJim Ingham    }
5491b1d9815SJim Ingham  }
5501b1d9815SJim Ingham  Py_RETURN_NONE;
5511b1d9815SJim Ingham}
5521b1d9815SJim Ingham
553*9d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonStopHookCallHandleStop(
554*9d5e37edSPavel Labath    void *implementor, lldb::ExecutionContextRefSP exc_ctx_sp,
555*9d5e37edSPavel Labath    lldb::StreamSP stream) {
5561b1d9815SJim Ingham  // handle_stop will return a bool with the meaning "should_stop"...
5571b1d9815SJim Ingham  // If you return nothing we'll assume we are going to stop.
5581b1d9815SJim Ingham  // Also any errors should return true, since we should stop on error.
5591b1d9815SJim Ingham
5601b1d9815SJim Ingham  PyErr_Cleaner py_err_cleaner(false);
5611b1d9815SJim Ingham  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
5621b1d9815SJim Ingham  auto pfunc = self.ResolveName<PythonCallable>("handle_stop");
5631b1d9815SJim Ingham
5641b1d9815SJim Ingham  if (!pfunc.IsAllocated())
5651b1d9815SJim Ingham    return true;
5661b1d9815SJim Ingham
5671b1d9815SJim Ingham  PythonObject result;
5681b1d9815SJim Ingham  lldb::SBExecutionContext sb_exc_ctx(exc_ctx_sp);
5691b1d9815SJim Ingham  PythonObject exc_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_exc_ctx));
5701b1d9815SJim Ingham  lldb::SBStream sb_stream;
571*9d5e37edSPavel Labath  PythonObject sb_stream_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_stream));
5721b1d9815SJim Ingham  result = pfunc(exc_ctx_arg, sb_stream_arg);
5731b1d9815SJim Ingham
574*9d5e37edSPavel Labath  if (PyErr_Occurred()) {
5751b1d9815SJim Ingham    stream->PutCString("Python error occurred handling stop-hook.");
5761b1d9815SJim Ingham    PyErr_Print();
5771b1d9815SJim Ingham    PyErr_Clear();
5781b1d9815SJim Ingham    return true;
5791b1d9815SJim Ingham  }
5801b1d9815SJim Ingham
5811b1d9815SJim Ingham  // Now add the result to the output stream.  SBStream only
5821b1d9815SJim Ingham  // makes an internally help StreamString which I can't interpose, so I
5831b1d9815SJim Ingham  // have to copy it over here.
5841b1d9815SJim Ingham  stream->PutCString(sb_stream.GetData());
5851b1d9815SJim Ingham
5861b1d9815SJim Ingham  if (result.get() == Py_False)
5871b1d9815SJim Ingham    return false;
5881b1d9815SJim Ingham  else
5891b1d9815SJim Ingham    return true;
5901b1d9815SJim Ingham}
5911b1d9815SJim Ingham
592*9d5e37edSPavel Labath// wrapper that calls an optional instance member of an object taking no
593*9d5e37edSPavel Labath// arguments
594*9d5e37edSPavel Labathstatic PyObject *LLDBSwigPython_CallOptionalMember(
595*9d5e37edSPavel Labath    PyObject * implementor, char *callee_name,
596*9d5e37edSPavel Labath    PyObject *ret_if_not_found = Py_None, bool *was_found = NULL) {
5976498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(false);
5986498aff2SJonas Devlieghere
5996498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
6006498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>(callee_name);
6016498aff2SJonas Devlieghere
602*9d5e37edSPavel Labath  if (!pfunc.IsAllocated()) {
6036498aff2SJonas Devlieghere    if (was_found)
6046498aff2SJonas Devlieghere      *was_found = false;
6056498aff2SJonas Devlieghere    Py_XINCREF(ret_if_not_found);
6066498aff2SJonas Devlieghere    return ret_if_not_found;
6076498aff2SJonas Devlieghere  }
6086498aff2SJonas Devlieghere
6096498aff2SJonas Devlieghere  if (was_found)
6106498aff2SJonas Devlieghere    *was_found = true;
6116498aff2SJonas Devlieghere
6126498aff2SJonas Devlieghere  PythonObject result = pfunc();
6136498aff2SJonas Devlieghere  return result.release();
6146498aff2SJonas Devlieghere}
6156498aff2SJonas Devlieghere
616*9d5e37edSPavel Labathsize_t lldb_private::LLDBSwigPython_CalculateNumChildren(PyObject * implementor,
617*9d5e37edSPavel Labath                                                         uint32_t max) {
6186498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, implementor);
6196498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>("num_children");
6206498aff2SJonas Devlieghere
6216498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
6226498aff2SJonas Devlieghere    return 0;
6236498aff2SJonas Devlieghere
6246498aff2SJonas Devlieghere  auto arg_info = pfunc.GetArgInfo();
6256498aff2SJonas Devlieghere  if (!arg_info) {
6266498aff2SJonas Devlieghere    llvm::consumeError(arg_info.takeError());
6276498aff2SJonas Devlieghere    return 0;
6286498aff2SJonas Devlieghere  }
6296498aff2SJonas Devlieghere
63052712d3fSLawrence D'Anna  size_t ret_val;
631478619cfSMuhammad Omair Javaid  if (arg_info.get().max_positional_args < 1)
63252712d3fSLawrence D'Anna    ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call()));
633478619cfSMuhammad Omair Javaid  else
634*9d5e37edSPavel Labath    ret_val = unwrapOrSetPythonException(
635*9d5e37edSPavel Labath        As<long long>(pfunc.Call(PythonInteger(max))));
636478619cfSMuhammad Omair Javaid
637*9d5e37edSPavel Labath  if (PyErr_Occurred()) {
6386498aff2SJonas Devlieghere    PyErr_Print();
6396498aff2SJonas Devlieghere    PyErr_Clear();
64052712d3fSLawrence D'Anna    return 0;
6416498aff2SJonas Devlieghere  }
6426498aff2SJonas Devlieghere
6436498aff2SJonas Devlieghere  if (arg_info.get().max_positional_args < 1)
6446498aff2SJonas Devlieghere    ret_val = std::min(ret_val, static_cast<size_t>(max));
6456498aff2SJonas Devlieghere
6466498aff2SJonas Devlieghere  return ret_val;
6476498aff2SJonas Devlieghere}
6486498aff2SJonas Devlieghere
649*9d5e37edSPavel LabathPyObject *lldb_private::LLDBSwigPython_GetChildAtIndex(PyObject * implementor,
650*9d5e37edSPavel Labath                                                       uint32_t idx) {
6516498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
6526498aff2SJonas Devlieghere
6536498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, implementor);
6546498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index");
6556498aff2SJonas Devlieghere
6566498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
6576498aff2SJonas Devlieghere    return nullptr;
6586498aff2SJonas Devlieghere
6596498aff2SJonas Devlieghere  PythonObject result = pfunc(PythonInteger(idx));
6606498aff2SJonas Devlieghere
6616498aff2SJonas Devlieghere  if (!result.IsAllocated())
6626498aff2SJonas Devlieghere    return nullptr;
6636498aff2SJonas Devlieghere
6646498aff2SJonas Devlieghere  lldb::SBValue *sbvalue_ptr = nullptr;
665*9d5e37edSPavel Labath  if (SWIG_ConvertPtr(result.get(), (void **)&sbvalue_ptr,
666*9d5e37edSPavel Labath                      SWIGTYPE_p_lldb__SBValue, 0) == -1)
6676498aff2SJonas Devlieghere    return nullptr;
6686498aff2SJonas Devlieghere
6696498aff2SJonas Devlieghere  if (sbvalue_ptr == nullptr)
6706498aff2SJonas Devlieghere    return nullptr;
6716498aff2SJonas Devlieghere
6726498aff2SJonas Devlieghere  return result.release();
6736498aff2SJonas Devlieghere}
6746498aff2SJonas Devlieghere
675*9d5e37edSPavel Labathint lldb_private::LLDBSwigPython_GetIndexOfChildWithName(
676*9d5e37edSPavel Labath    PyObject * implementor, const char *child_name) {
6776498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
6786498aff2SJonas Devlieghere
6796498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, implementor);
6806498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>("get_child_index");
6816498aff2SJonas Devlieghere
6826498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
6836498aff2SJonas Devlieghere    return UINT32_MAX;
6846498aff2SJonas Devlieghere
68552712d3fSLawrence D'Anna  llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name));
6866498aff2SJonas Devlieghere
687*9d5e37edSPavel Labath  long long retval =
688*9d5e37edSPavel Labath      unwrapOrSetPythonException(As<long long>(std::move(result)));
68952712d3fSLawrence D'Anna
69052712d3fSLawrence D'Anna  if (PyErr_Occurred()) {
69152712d3fSLawrence D'Anna    PyErr_Clear(); // FIXME print this? do something else
6926498aff2SJonas Devlieghere    return UINT32_MAX;
69352712d3fSLawrence D'Anna  }
6946498aff2SJonas Devlieghere
6956498aff2SJonas Devlieghere  if (retval >= 0)
6966498aff2SJonas Devlieghere    return (uint32_t)retval;
6976498aff2SJonas Devlieghere
6986498aff2SJonas Devlieghere  return UINT32_MAX;
6996498aff2SJonas Devlieghere}
7006498aff2SJonas Devlieghere
701*9d5e37edSPavel Labathbool lldb_private::LLDBSwigPython_UpdateSynthProviderInstance(PyObject *
702*9d5e37edSPavel Labath                                                              implementor) {
7036498aff2SJonas Devlieghere  bool ret_val = false;
7046498aff2SJonas Devlieghere
7056498aff2SJonas Devlieghere  static char callee_name[] = "update";
7066498aff2SJonas Devlieghere
707*9d5e37edSPavel Labath  PyObject *py_return =
708*9d5e37edSPavel Labath      LLDBSwigPython_CallOptionalMember(implementor, callee_name);
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
718*9d5e37edSPavel Labathbool lldb_private::LLDBSwigPython_MightHaveChildrenSynthProviderInstance(
719*9d5e37edSPavel Labath    PyObject * implementor) {
7206498aff2SJonas Devlieghere  bool ret_val = false;
7216498aff2SJonas Devlieghere
7226498aff2SJonas Devlieghere  static char callee_name[] = "has_children";
7236498aff2SJonas Devlieghere
724*9d5e37edSPavel Labath  PyObject *py_return =
725*9d5e37edSPavel Labath      LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_True);
7266498aff2SJonas Devlieghere
7276498aff2SJonas Devlieghere  if (py_return == Py_True)
7286498aff2SJonas Devlieghere    ret_val = true;
7296498aff2SJonas Devlieghere
7306498aff2SJonas Devlieghere  Py_XDECREF(py_return);
7316498aff2SJonas Devlieghere
7326498aff2SJonas Devlieghere  return ret_val;
7336498aff2SJonas Devlieghere}
7346498aff2SJonas Devlieghere
735*9d5e37edSPavel LabathPyObject *lldb_private::LLDBSwigPython_GetValueSynthProviderInstance(
736*9d5e37edSPavel Labath    PyObject * implementor) {
7376498aff2SJonas Devlieghere  PyObject *ret_val = nullptr;
7386498aff2SJonas Devlieghere
7396498aff2SJonas Devlieghere  static char callee_name[] = "get_value";
7406498aff2SJonas Devlieghere
741*9d5e37edSPavel Labath  PyObject *py_return =
742*9d5e37edSPavel Labath      LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_None);
7436498aff2SJonas Devlieghere
7446498aff2SJonas Devlieghere  if (py_return == Py_None || py_return == nullptr)
7456498aff2SJonas Devlieghere    ret_val = nullptr;
7466498aff2SJonas Devlieghere
7476498aff2SJonas Devlieghere  lldb::SBValue *sbvalue_ptr = NULL;
7486498aff2SJonas Devlieghere
749*9d5e37edSPavel Labath  if (SWIG_ConvertPtr(py_return, (void **)&sbvalue_ptr,
750*9d5e37edSPavel Labath                      SWIGTYPE_p_lldb__SBValue, 0) == -1)
7516498aff2SJonas Devlieghere    ret_val = nullptr;
7526498aff2SJonas Devlieghere  else if (sbvalue_ptr == NULL)
7536498aff2SJonas Devlieghere    ret_val = nullptr;
7546498aff2SJonas Devlieghere  else
7556498aff2SJonas Devlieghere    ret_val = py_return;
7566498aff2SJonas Devlieghere
7576498aff2SJonas Devlieghere  Py_XDECREF(py_return);
7586498aff2SJonas Devlieghere  return ret_val;
7596498aff2SJonas Devlieghere}
7606498aff2SJonas Devlieghere
761*9d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBData(PyObject * data) {
7621f6a57c1SMed Ismail Bennani  lldb::SBData *sb_ptr = nullptr;
7631f6a57c1SMed Ismail Bennani
764*9d5e37edSPavel Labath  int valid_cast =
765*9d5e37edSPavel Labath      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0);
7661f6a57c1SMed Ismail Bennani
7671f6a57c1SMed Ismail Bennani  if (valid_cast == -1)
7681f6a57c1SMed Ismail Bennani    return NULL;
7691f6a57c1SMed Ismail Bennani
7701f6a57c1SMed Ismail Bennani  return sb_ptr;
7711f6a57c1SMed Ismail Bennani}
7721f6a57c1SMed Ismail Bennani
773*9d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBError(PyObject * data) {
7741f6a57c1SMed Ismail Bennani  lldb::SBError *sb_ptr = nullptr;
7751f6a57c1SMed Ismail Bennani
776*9d5e37edSPavel Labath  int valid_cast =
777*9d5e37edSPavel Labath      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0);
7781f6a57c1SMed Ismail Bennani
7791f6a57c1SMed Ismail Bennani  if (valid_cast == -1)
7801f6a57c1SMed Ismail Bennani    return NULL;
7811f6a57c1SMed Ismail Bennani
7821f6a57c1SMed Ismail Bennani  return sb_ptr;
7831f6a57c1SMed Ismail Bennani}
7841f6a57c1SMed Ismail Bennani
785*9d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBValue(PyObject * data) {
7866498aff2SJonas Devlieghere  lldb::SBValue *sb_ptr = NULL;
7876498aff2SJonas Devlieghere
788*9d5e37edSPavel Labath  int valid_cast =
789*9d5e37edSPavel Labath      SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
7906498aff2SJonas Devlieghere
7916498aff2SJonas Devlieghere  if (valid_cast == -1)
7926498aff2SJonas Devlieghere    return NULL;
7936498aff2SJonas Devlieghere
7946498aff2SJonas Devlieghere  return sb_ptr;
7956498aff2SJonas Devlieghere}
7966498aff2SJonas Devlieghere
797*9d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *
798*9d5e37edSPavel Labath                                                                    data) {
799a758c9f7SMed Ismail Bennani  lldb::SBMemoryRegionInfo *sb_ptr = NULL;
800a758c9f7SMed Ismail Bennani
801*9d5e37edSPavel Labath  int valid_cast = SWIG_ConvertPtr(data, (void **)&sb_ptr,
802*9d5e37edSPavel Labath                                   SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0);
803a758c9f7SMed Ismail Bennani
804a758c9f7SMed Ismail Bennani  if (valid_cast == -1)
805a758c9f7SMed Ismail Bennani    return NULL;
806a758c9f7SMed Ismail Bennani
807a758c9f7SMed Ismail Bennani  return sb_ptr;
808a758c9f7SMed Ismail Bennani}
809a758c9f7SMed Ismail Bennani
810*9d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonCallCommand(
811*9d5e37edSPavel Labath    const char *python_function_name, const char *session_dictionary_name,
812*9d5e37edSPavel Labath    lldb::DebuggerSP &debugger, const char *args,
8136498aff2SJonas Devlieghere    lldb_private::CommandReturnObject &cmd_retobj,
814*9d5e37edSPavel Labath    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
8156498aff2SJonas Devlieghere  lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
8166498aff2SJonas Devlieghere  lldb::SBDebugger debugger_sb(debugger);
8176498aff2SJonas Devlieghere  lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
8186498aff2SJonas Devlieghere
8196498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
820*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
821*9d5e37edSPavel Labath      session_dictionary_name);
822*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
823*9d5e37edSPavel Labath      python_function_name, dict);
8246498aff2SJonas Devlieghere
8256498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
8266498aff2SJonas Devlieghere    return false;
8276498aff2SJonas Devlieghere
8286498aff2SJonas Devlieghere  auto argc = pfunc.GetArgInfo();
8296498aff2SJonas Devlieghere  if (!argc) {
8306498aff2SJonas Devlieghere    llvm::consumeError(argc.takeError());
8316498aff2SJonas Devlieghere    return false;
8326498aff2SJonas Devlieghere  }
8336498aff2SJonas Devlieghere  PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
8346498aff2SJonas Devlieghere  PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb));
835*9d5e37edSPavel Labath  PythonObject cmd_retobj_arg(PyRefType::Owned,
836*9d5e37edSPavel Labath                              SBTypeToSWIGWrapper(cmd_retobj_sb));
8376498aff2SJonas Devlieghere
8386498aff2SJonas Devlieghere  if (argc.get().max_positional_args < 5u)
8396498aff2SJonas Devlieghere    pfunc(debugger_arg, PythonString(args), cmd_retobj_arg, dict);
8406498aff2SJonas Devlieghere  else
8416498aff2SJonas Devlieghere    pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg, dict);
8426498aff2SJonas Devlieghere
8436498aff2SJonas Devlieghere  return true;
8446498aff2SJonas Devlieghere}
8456498aff2SJonas Devlieghere
846*9d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonCallCommandObject(
847*9d5e37edSPavel Labath    PyObject * implementor, lldb::DebuggerSP & debugger, const char *args,
8486498aff2SJonas Devlieghere    lldb_private::CommandReturnObject &cmd_retobj,
849*9d5e37edSPavel Labath    lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
8506498aff2SJonas Devlieghere  lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
8516498aff2SJonas Devlieghere  lldb::SBDebugger debugger_sb(debugger);
8526498aff2SJonas Devlieghere  lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
8536498aff2SJonas Devlieghere
8546498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
8556498aff2SJonas Devlieghere
8566498aff2SJonas Devlieghere  PythonObject self(PyRefType::Borrowed, implementor);
8576498aff2SJonas Devlieghere  auto pfunc = self.ResolveName<PythonCallable>("__call__");
8586498aff2SJonas Devlieghere
8596498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
8606498aff2SJonas Devlieghere    return false;
8616498aff2SJonas Devlieghere
8626498aff2SJonas Devlieghere  PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
8636498aff2SJonas Devlieghere  PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb));
864*9d5e37edSPavel Labath  PythonObject cmd_retobj_arg(PyRefType::Owned,
865*9d5e37edSPavel Labath                              SBTypeToSWIGWrapper(cmd_retobj_sb));
8666498aff2SJonas Devlieghere
8676498aff2SJonas Devlieghere  pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg);
8686498aff2SJonas Devlieghere
8696498aff2SJonas Devlieghere  return true;
8706498aff2SJonas Devlieghere}
8716498aff2SJonas Devlieghere
872*9d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPythonCreateOSPlugin(
873*9d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name,
874*9d5e37edSPavel Labath    const lldb::ProcessSP &process_sp) {
875*9d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
876*9d5e37edSPavel Labath      !session_dictionary_name)
8776498aff2SJonas Devlieghere    Py_RETURN_NONE;
8786498aff2SJonas Devlieghere
8796498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
8806498aff2SJonas Devlieghere
881*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
882*9d5e37edSPavel Labath      session_dictionary_name);
883*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
884*9d5e37edSPavel Labath      python_class_name, dict);
8856498aff2SJonas Devlieghere
8866498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
8876498aff2SJonas Devlieghere    Py_RETURN_NONE;
8886498aff2SJonas Devlieghere
8897f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(process_sp));
8906498aff2SJonas Devlieghere
8916498aff2SJonas Devlieghere  if (result.IsAllocated())
8926498aff2SJonas Devlieghere    return result.release();
8936498aff2SJonas Devlieghere
8946498aff2SJonas Devlieghere  Py_RETURN_NONE;
8956498aff2SJonas Devlieghere}
8966498aff2SJonas Devlieghere
897*9d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_CreateFrameRecognizer(
898*9d5e37edSPavel Labath    const char *python_class_name, const char *session_dictionary_name) {
899*9d5e37edSPavel Labath  if (python_class_name == NULL || python_class_name[0] == '\0' ||
900*9d5e37edSPavel Labath      !session_dictionary_name)
9016498aff2SJonas Devlieghere    Py_RETURN_NONE;
9026498aff2SJonas Devlieghere
9036498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
9046498aff2SJonas Devlieghere
905*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
906*9d5e37edSPavel Labath      session_dictionary_name);
907*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
908*9d5e37edSPavel Labath      python_class_name, dict);
9096498aff2SJonas Devlieghere
9106498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
9116498aff2SJonas Devlieghere    Py_RETURN_NONE;
9126498aff2SJonas Devlieghere
9136498aff2SJonas Devlieghere  auto result = pfunc();
9146498aff2SJonas Devlieghere
9156498aff2SJonas Devlieghere  if (result.IsAllocated())
9166498aff2SJonas Devlieghere    return result.release();
9176498aff2SJonas Devlieghere
9186498aff2SJonas Devlieghere  Py_RETURN_NONE;
9196498aff2SJonas Devlieghere}
9206498aff2SJonas Devlieghere
921*9d5e37edSPavel LabathPyObject *lldb_private::LLDBSwigPython_GetRecognizedArguments(
922*9d5e37edSPavel Labath    PyObject * implementor, const lldb::StackFrameSP &frame_sp) {
9236498aff2SJonas Devlieghere  static char callee_name[] = "get_recognized_arguments";
9246498aff2SJonas Devlieghere
9256498aff2SJonas Devlieghere  lldb::SBFrame frame_sb(frame_sp);
9266498aff2SJonas Devlieghere  PyObject *arg = SBTypeToSWIGWrapper(frame_sb);
9276498aff2SJonas Devlieghere
9286498aff2SJonas Devlieghere  PythonString str(callee_name);
929*9d5e37edSPavel Labath  PyObject *result =
930*9d5e37edSPavel Labath      PyObject_CallMethodObjArgs(implementor, str.get(), arg, NULL);
9316498aff2SJonas Devlieghere  return result;
9326498aff2SJonas Devlieghere}
9336498aff2SJonas Devlieghere
934*9d5e37edSPavel Labathvoid *lldb_private::LLDBSWIGPython_GetDynamicSetting(
935*9d5e37edSPavel Labath    void *module, const char *setting, const lldb::TargetSP &target_sp) {
9366498aff2SJonas Devlieghere  if (!module || !setting)
9376498aff2SJonas Devlieghere    Py_RETURN_NONE;
9386498aff2SJonas Devlieghere
9396498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
9406498aff2SJonas Devlieghere  PythonObject py_module(PyRefType::Borrowed, (PyObject *)module);
9416498aff2SJonas Devlieghere  auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting");
9426498aff2SJonas Devlieghere
9436498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
9446498aff2SJonas Devlieghere    Py_RETURN_NONE;
9456498aff2SJonas Devlieghere
9467f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(target_sp), PythonString(setting));
9476498aff2SJonas Devlieghere
9486498aff2SJonas Devlieghere  return result.release();
9496498aff2SJonas Devlieghere}
9506498aff2SJonas Devlieghere
9519a14adeaSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordProcess(
9527f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
9537f09ab08SPavel Labath    const lldb::ProcessSP &process, std::string &output) {
9546498aff2SJonas Devlieghere
955*9d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
956*9d5e37edSPavel Labath      !session_dictionary_name)
9576498aff2SJonas Devlieghere    return false;
9586498aff2SJonas Devlieghere
9596498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
9606498aff2SJonas Devlieghere
961*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
962*9d5e37edSPavel Labath      session_dictionary_name);
963*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
964*9d5e37edSPavel Labath      python_function_name, dict);
9656498aff2SJonas Devlieghere
9666498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
9676498aff2SJonas Devlieghere    return false;
9686498aff2SJonas Devlieghere
9697f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(process), dict);
9706498aff2SJonas Devlieghere
9716498aff2SJonas Devlieghere  output = result.Str().GetString().str();
9726498aff2SJonas Devlieghere
9736498aff2SJonas Devlieghere  return true;
9746498aff2SJonas Devlieghere}
9756498aff2SJonas Devlieghere
976*9d5e37edSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordThread(
977*9d5e37edSPavel Labath    const char *python_function_name, const char *session_dictionary_name,
978*9d5e37edSPavel Labath    lldb::ThreadSP &thread, std::string &output)
9796498aff2SJonas Devlieghere
9806498aff2SJonas Devlieghere{
981*9d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
982*9d5e37edSPavel Labath      !session_dictionary_name)
9836498aff2SJonas Devlieghere    return false;
9846498aff2SJonas Devlieghere
9856498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
9866498aff2SJonas Devlieghere
987*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
988*9d5e37edSPavel Labath      session_dictionary_name);
989*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
990*9d5e37edSPavel Labath      python_function_name, dict);
9916498aff2SJonas Devlieghere
9926498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
9936498aff2SJonas Devlieghere    return false;
9946498aff2SJonas Devlieghere
9956498aff2SJonas Devlieghere  lldb::SBThread thread_sb(thread);
9966498aff2SJonas Devlieghere  PythonObject thread_arg(PyRefType::Owned, SBTypeToSWIGWrapper(thread_sb));
9976498aff2SJonas Devlieghere  auto result = pfunc(thread_arg, dict);
9986498aff2SJonas Devlieghere
9996498aff2SJonas Devlieghere  output = result.Str().GetString().str();
10006498aff2SJonas Devlieghere
10016498aff2SJonas Devlieghere  return true;
10026498aff2SJonas Devlieghere}
10036498aff2SJonas Devlieghere
10049a14adeaSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordTarget(
10057f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
10067f09ab08SPavel Labath    const lldb::TargetSP &target, std::string &output) {
10076498aff2SJonas Devlieghere
1008*9d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
1009*9d5e37edSPavel Labath      !session_dictionary_name)
10106498aff2SJonas Devlieghere    return false;
10116498aff2SJonas Devlieghere
10126498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
10136498aff2SJonas Devlieghere
1014*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
1015*9d5e37edSPavel Labath      session_dictionary_name);
1016*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
1017*9d5e37edSPavel Labath      python_function_name, dict);
10186498aff2SJonas Devlieghere
10196498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
10206498aff2SJonas Devlieghere    return false;
10216498aff2SJonas Devlieghere
10227f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(target), dict);
10236498aff2SJonas Devlieghere
10246498aff2SJonas Devlieghere  output = result.Str().GetString().str();
10256498aff2SJonas Devlieghere
10266498aff2SJonas Devlieghere  return true;
10276498aff2SJonas Devlieghere}
10286498aff2SJonas Devlieghere
1029*9d5e37edSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordFrame(
1030*9d5e37edSPavel Labath    const char *python_function_name, const char *session_dictionary_name,
1031*9d5e37edSPavel Labath    lldb::StackFrameSP &frame, std::string &output)
10326498aff2SJonas Devlieghere
10336498aff2SJonas Devlieghere{
1034*9d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
1035*9d5e37edSPavel Labath      !session_dictionary_name)
10366498aff2SJonas Devlieghere    return false;
10376498aff2SJonas Devlieghere
10386498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
10396498aff2SJonas Devlieghere
1040*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
1041*9d5e37edSPavel Labath      session_dictionary_name);
1042*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
1043*9d5e37edSPavel Labath      python_function_name, dict);
10446498aff2SJonas Devlieghere
10456498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
10466498aff2SJonas Devlieghere    return false;
10476498aff2SJonas Devlieghere
10486498aff2SJonas Devlieghere  lldb::SBFrame frame_sb(frame);
10496498aff2SJonas Devlieghere  PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(frame_sb));
10506498aff2SJonas Devlieghere  auto result = pfunc(frame_arg, dict);
10516498aff2SJonas Devlieghere
10526498aff2SJonas Devlieghere  output = result.Str().GetString().str();
10536498aff2SJonas Devlieghere
10546498aff2SJonas Devlieghere  return true;
10556498aff2SJonas Devlieghere}
10566498aff2SJonas Devlieghere
10579a14adeaSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordValue(
10587f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
10597f09ab08SPavel Labath    const lldb::ValueObjectSP &value, std::string &output) {
10606498aff2SJonas Devlieghere
1061*9d5e37edSPavel Labath  if (python_function_name == NULL || python_function_name[0] == '\0' ||
1062*9d5e37edSPavel Labath      !session_dictionary_name)
10636498aff2SJonas Devlieghere    return false;
10646498aff2SJonas Devlieghere
10656498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
10666498aff2SJonas Devlieghere
1067*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
1068*9d5e37edSPavel Labath      session_dictionary_name);
1069*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
1070*9d5e37edSPavel Labath      python_function_name, dict);
10716498aff2SJonas Devlieghere
10726498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
10736498aff2SJonas Devlieghere    return false;
10746498aff2SJonas Devlieghere
10757f09ab08SPavel Labath  auto result = pfunc(ToSWIGWrapper(value), dict);
10766498aff2SJonas Devlieghere
10776498aff2SJonas Devlieghere  output = result.Str().GetString().str();
10786498aff2SJonas Devlieghere
10796498aff2SJonas Devlieghere  return true;
10806498aff2SJonas Devlieghere}
10816498aff2SJonas Devlieghere
1082*9d5e37edSPavel Labathbool lldb_private::LLDBSwigPythonCallModuleInit(
1083*9d5e37edSPavel Labath    const char *python_module_name, const char *session_dictionary_name,
1084*9d5e37edSPavel Labath    lldb::DebuggerSP &debugger) {
10856498aff2SJonas Devlieghere  std::string python_function_name_string = python_module_name;
10866498aff2SJonas Devlieghere  python_function_name_string += ".__lldb_init_module";
10876498aff2SJonas Devlieghere  const char *python_function_name = python_function_name_string.c_str();
10886498aff2SJonas Devlieghere
10896498aff2SJonas Devlieghere  PyErr_Cleaner py_err_cleaner(true);
10906498aff2SJonas Devlieghere
1091*9d5e37edSPavel Labath  auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
1092*9d5e37edSPavel Labath      session_dictionary_name);
1093*9d5e37edSPavel Labath  auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
1094*9d5e37edSPavel Labath      python_function_name, dict);
10956498aff2SJonas Devlieghere
10966498aff2SJonas Devlieghere  // This method is optional and need not exist.  So if we don't find it,
10976498aff2SJonas Devlieghere  // it's actually a success, not a failure.
10986498aff2SJonas Devlieghere  if (!pfunc.IsAllocated())
10996498aff2SJonas Devlieghere    return true;
11006498aff2SJonas Devlieghere
11016498aff2SJonas Devlieghere  lldb::SBDebugger debugger_sb(debugger);
11026498aff2SJonas Devlieghere  PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
11036498aff2SJonas Devlieghere  pfunc(debugger_arg, dict);
11046498aff2SJonas Devlieghere
11056498aff2SJonas Devlieghere  return true;
11066498aff2SJonas Devlieghere}
11076498aff2SJonas Devlieghere
1108*9d5e37edSPavel Labathlldb::ValueObjectSP lldb_private::LLDBSWIGPython_GetValueObjectSPFromSBValue(
1109*9d5e37edSPavel Labath    void *data) {
11106498aff2SJonas Devlieghere  lldb::ValueObjectSP valobj_sp;
1111*9d5e37edSPavel Labath  if (data) {
11126498aff2SJonas Devlieghere    lldb::SBValue *sb_ptr = (lldb::SBValue *)data;
11136498aff2SJonas Devlieghere    valobj_sp = sb_ptr->GetSP();
11146498aff2SJonas Devlieghere  }
11156498aff2SJonas Devlieghere  return valobj_sp;
11166498aff2SJonas Devlieghere}
11176498aff2SJonas Devlieghere
11186498aff2SJonas Devlieghere// For the LogOutputCallback functions
1119*9d5e37edSPavel Labathstatic void LLDBSwigPythonCallPythonLogOutputCallback(const char *str,
1120*9d5e37edSPavel Labath                                                      void *baton) {
11216498aff2SJonas Devlieghere  if (baton != Py_None) {
11226498aff2SJonas Devlieghere    SWIG_PYTHON_THREAD_BEGIN_BLOCK;
1123*9d5e37edSPavel Labath    PyObject *result = PyObject_CallFunction(
1124*9d5e37edSPavel Labath        reinterpret_cast<PyObject *>(baton), const_cast<char *>("s"), str);
11256498aff2SJonas Devlieghere    Py_XDECREF(result);
11266498aff2SJonas Devlieghere    SWIG_PYTHON_THREAD_END_BLOCK;
11276498aff2SJonas Devlieghere  }
11286498aff2SJonas Devlieghere}
11296498aff2SJonas Devlieghere%}
1130