16498aff2SJonas Devlieghere%header %{
26498aff2SJonas Devlieghere
36498aff2SJonas Devlieghereclass PyErr_Cleaner
46498aff2SJonas Devlieghere{
56498aff2SJonas Devliegherepublic:
66498aff2SJonas Devlieghere    PyErr_Cleaner(bool print=false) :
76498aff2SJonas Devlieghere    m_print(print)
86498aff2SJonas Devlieghere    {
96498aff2SJonas Devlieghere    }
106498aff2SJonas Devlieghere
116498aff2SJonas Devlieghere    ~PyErr_Cleaner()
126498aff2SJonas Devlieghere    {
136498aff2SJonas Devlieghere        if (PyErr_Occurred())
146498aff2SJonas Devlieghere        {
156498aff2SJonas Devlieghere            if(m_print && !PyErr_ExceptionMatches(PyExc_SystemExit))
166498aff2SJonas Devlieghere                PyErr_Print();
176498aff2SJonas Devlieghere            PyErr_Clear();
186498aff2SJonas Devlieghere        }
196498aff2SJonas Devlieghere    }
206498aff2SJonas Devlieghere
216498aff2SJonas Devlieghereprivate:
226498aff2SJonas Devlieghere    bool m_print;
236498aff2SJonas Devlieghere};
246498aff2SJonas Devlieghere
256498aff2SJonas Devlieghere%}
266498aff2SJonas Devlieghere
276498aff2SJonas Devlieghere%wrapper %{
286498aff2SJonas Devlieghere
296498aff2SJonas Devlieghere// resolve a dotted Python name in the form
306498aff2SJonas Devlieghere// foo.bar.baz.Foobar to an actual Python object
316498aff2SJonas Devlieghere// if pmodule is NULL, the __main__ module will be used
326498aff2SJonas Devlieghere// as the starting point for the search
336498aff2SJonas Devlieghere
346498aff2SJonas Devlieghere
356498aff2SJonas Devlieghere// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...)
366498aff2SJonas Devlieghere// and is used when a script command is attached to a breakpoint for execution.
376498aff2SJonas Devlieghere
38daf36998SDave Lee#pragma clang diagnostic push
39daf36998SDave Lee#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
40daf36998SDave Lee
41daf36998SDave Lee// Disable warning C4190: 'LLDBSwigPythonBreakpointCallbackFunction' has
42daf36998SDave Lee// C-linkage specified, but returns UDT 'llvm::Expected<bool>' which is
43daf36998SDave Lee// incompatible with C
44daf36998SDave Lee#if _MSC_VER
45daf36998SDave Lee#pragma warning (push)
46daf36998SDave Lee#pragma warning (disable : 4190)
47daf36998SDave Lee#endif
48daf36998SDave Lee
496498aff2SJonas DevlieghereSWIGEXPORT llvm::Expected<bool>
506498aff2SJonas DevlieghereLLDBSwigPythonBreakpointCallbackFunction
516498aff2SJonas Devlieghere(
526498aff2SJonas Devlieghere    const char *python_function_name,
536498aff2SJonas Devlieghere    const char *session_dictionary_name,
546498aff2SJonas Devlieghere    const lldb::StackFrameSP& frame_sp,
556498aff2SJonas Devlieghere    const lldb::BreakpointLocationSP& bp_loc_sp,
566498aff2SJonas Devlieghere    lldb_private::StructuredDataImpl *args_impl
576498aff2SJonas Devlieghere)
586498aff2SJonas Devlieghere{
596498aff2SJonas Devlieghere    using namespace llvm;
606498aff2SJonas Devlieghere
616498aff2SJonas Devlieghere    lldb::SBFrame sb_frame (frame_sp);
626498aff2SJonas Devlieghere    lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
636498aff2SJonas Devlieghere
646498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
656498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
666498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
676498aff2SJonas Devlieghere
686498aff2SJonas Devlieghere    unsigned max_positional_args;
696498aff2SJonas Devlieghere    if (auto arg_info = pfunc.GetArgInfo())
706498aff2SJonas Devlieghere        max_positional_args = arg_info.get().max_positional_args;
716498aff2SJonas Devlieghere    else
726498aff2SJonas Devlieghere        return arg_info.takeError();
736498aff2SJonas Devlieghere
746498aff2SJonas Devlieghere    PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame));
756498aff2SJonas Devlieghere    PythonObject bp_loc_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_bp_loc));
766498aff2SJonas Devlieghere
776498aff2SJonas Devlieghere    auto result = [&] () -> Expected<PythonObject> {
786498aff2SJonas Devlieghere        // If the called function doesn't take extra_args, drop them here:
796498aff2SJonas Devlieghere        if (max_positional_args < 4) {
806498aff2SJonas Devlieghere            return pfunc.Call(frame_arg, bp_loc_arg, dict);
816498aff2SJonas Devlieghere        } else {
82f1127914SPavel Labath            // FIXME: SBStructuredData leaked here
836498aff2SJonas Devlieghere            lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl);
84f1127914SPavel Labath            PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*args_value));
856498aff2SJonas Devlieghere            return pfunc.Call(frame_arg, bp_loc_arg, args_arg, dict);
866498aff2SJonas Devlieghere        }
876498aff2SJonas Devlieghere    } ();
886498aff2SJonas Devlieghere
896498aff2SJonas Devlieghere    if (!result)
906498aff2SJonas Devlieghere        return result.takeError();
916498aff2SJonas Devlieghere
926498aff2SJonas Devlieghere    // Only False counts as false!
936498aff2SJonas Devlieghere    return result.get().get() != Py_False;
946498aff2SJonas Devlieghere}
956498aff2SJonas Devlieghere
96daf36998SDave Lee#if _MSC_VER
97daf36998SDave Lee#pragma warning (pop)
98daf36998SDave Lee#endif
99daf36998SDave Lee
100daf36998SDave Lee#pragma clang diagnostic pop
101daf36998SDave Lee
1026498aff2SJonas Devlieghere// This function is called by lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...)
1036498aff2SJonas Devlieghere// and is used when a script command is attached to a watchpoint for execution.
1046498aff2SJonas Devlieghere
1056498aff2SJonas DevlieghereSWIGEXPORT bool
1066498aff2SJonas DevlieghereLLDBSwigPythonWatchpointCallbackFunction
1076498aff2SJonas Devlieghere(
1086498aff2SJonas Devlieghere    const char *python_function_name,
1096498aff2SJonas Devlieghere    const char *session_dictionary_name,
1106498aff2SJonas Devlieghere    const lldb::StackFrameSP& frame_sp,
1116498aff2SJonas Devlieghere    const lldb::WatchpointSP& wp_sp
1126498aff2SJonas Devlieghere)
1136498aff2SJonas Devlieghere{
1146498aff2SJonas Devlieghere    lldb::SBFrame sb_frame (frame_sp);
1156498aff2SJonas Devlieghere    lldb::SBWatchpoint sb_wp(wp_sp);
1166498aff2SJonas Devlieghere
1176498aff2SJonas Devlieghere    bool stop_at_watchpoint = true;
1186498aff2SJonas Devlieghere
1196498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
1206498aff2SJonas Devlieghere
1216498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
1226498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
1236498aff2SJonas Devlieghere
1246498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
1256498aff2SJonas Devlieghere        return stop_at_watchpoint;
1266498aff2SJonas Devlieghere
1276498aff2SJonas Devlieghere    PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame));
1286498aff2SJonas Devlieghere    PythonObject wp_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_wp));
1296498aff2SJonas Devlieghere    PythonObject result = pfunc(frame_arg, wp_arg, dict);
1306498aff2SJonas Devlieghere
1316498aff2SJonas Devlieghere    if (result.get() == Py_False)
1326498aff2SJonas Devlieghere        stop_at_watchpoint = false;
1336498aff2SJonas Devlieghere
1346498aff2SJonas Devlieghere    return stop_at_watchpoint;
1356498aff2SJonas Devlieghere}
1366498aff2SJonas Devlieghere
1376498aff2SJonas DevlieghereSWIGEXPORT bool
1386498aff2SJonas DevlieghereLLDBSwigPythonCallTypeScript
1396498aff2SJonas Devlieghere(
1406498aff2SJonas Devlieghere    const char *python_function_name,
1416498aff2SJonas Devlieghere    const void *session_dictionary,
1426498aff2SJonas Devlieghere    const lldb::ValueObjectSP& valobj_sp,
1436498aff2SJonas Devlieghere    void** pyfunct_wrapper,
1446498aff2SJonas Devlieghere    const lldb::TypeSummaryOptionsSP& options_sp,
1456498aff2SJonas Devlieghere    std::string& retval
1466498aff2SJonas Devlieghere)
1476498aff2SJonas Devlieghere{
1486498aff2SJonas Devlieghere    lldb::SBTypeSummaryOptions sb_options(options_sp.get());
1496498aff2SJonas Devlieghere
1506498aff2SJonas Devlieghere    retval.clear();
1516498aff2SJonas Devlieghere
1526498aff2SJonas Devlieghere    if (!python_function_name || !session_dictionary)
1536498aff2SJonas Devlieghere        return false;
1546498aff2SJonas Devlieghere
1556498aff2SJonas Devlieghere    PyObject *pfunc_impl = nullptr;
1566498aff2SJonas Devlieghere
1576498aff2SJonas Devlieghere    if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper))
1586498aff2SJonas Devlieghere    {
1596498aff2SJonas Devlieghere        pfunc_impl = (PyObject*)(*pyfunct_wrapper);
1606498aff2SJonas Devlieghere        if (pfunc_impl->ob_refcnt == 1)
1616498aff2SJonas Devlieghere        {
1626498aff2SJonas Devlieghere            Py_XDECREF(pfunc_impl);
1636498aff2SJonas Devlieghere            pfunc_impl = NULL;
1646498aff2SJonas Devlieghere        }
1656498aff2SJonas Devlieghere    }
1666498aff2SJonas Devlieghere
1676498aff2SJonas Devlieghere    PyObject *py_dict = (PyObject*)session_dictionary;
1686498aff2SJonas Devlieghere    if (!PythonDictionary::Check(py_dict))
1696498aff2SJonas Devlieghere        return true;
1706498aff2SJonas Devlieghere
1716498aff2SJonas Devlieghere    PythonDictionary dict(PyRefType::Borrowed, py_dict);
1726498aff2SJonas Devlieghere
1736498aff2SJonas Devlieghere    PyErr_Cleaner pyerr_cleanup(true);  // show Python errors
1746498aff2SJonas Devlieghere
1756498aff2SJonas Devlieghere    PythonCallable pfunc(PyRefType::Borrowed, pfunc_impl);
1766498aff2SJonas Devlieghere
1776498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
1786498aff2SJonas Devlieghere    {
1796498aff2SJonas Devlieghere        pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
1806498aff2SJonas Devlieghere        if (!pfunc.IsAllocated())
1816498aff2SJonas Devlieghere            return false;
1826498aff2SJonas Devlieghere
1836498aff2SJonas Devlieghere        if (pyfunct_wrapper)
1846498aff2SJonas Devlieghere        {
1856498aff2SJonas Devlieghere            *pyfunct_wrapper = pfunc.get();
1866498aff2SJonas Devlieghere            Py_XINCREF(pfunc.get());
1876498aff2SJonas Devlieghere        }
1886498aff2SJonas Devlieghere    }
1896498aff2SJonas Devlieghere
1906498aff2SJonas Devlieghere    PythonObject result;
1916498aff2SJonas Devlieghere    auto argc = pfunc.GetArgInfo();
1926498aff2SJonas Devlieghere    if (!argc) {
1936498aff2SJonas Devlieghere        llvm::consumeError(argc.takeError());
1946498aff2SJonas Devlieghere        return false;
1956498aff2SJonas Devlieghere    }
1966498aff2SJonas Devlieghere
197*7f09ab08SPavel Labath    PythonObject value_arg = ToSWIGWrapper(valobj_sp);
1986498aff2SJonas Devlieghere    PythonObject options_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_options));
1996498aff2SJonas Devlieghere
2006498aff2SJonas Devlieghere    if (argc.get().max_positional_args < 3)
2016498aff2SJonas Devlieghere        result = pfunc(value_arg,dict);
2026498aff2SJonas Devlieghere    else
2036498aff2SJonas Devlieghere        result = pfunc(value_arg,dict,options_arg);
2046498aff2SJonas Devlieghere
2056498aff2SJonas Devlieghere    retval = result.Str().GetString().str();
2066498aff2SJonas Devlieghere
2076498aff2SJonas Devlieghere    return true;
2086498aff2SJonas Devlieghere}
2096498aff2SJonas Devlieghere
2106498aff2SJonas DevlieghereSWIGEXPORT void*
2116498aff2SJonas DevlieghereLLDBSwigPythonCreateSyntheticProvider
2126498aff2SJonas Devlieghere(
2136498aff2SJonas Devlieghere    const char *python_class_name,
2146498aff2SJonas Devlieghere    const char *session_dictionary_name,
2156498aff2SJonas Devlieghere    const lldb::ValueObjectSP& valobj_sp
2166498aff2SJonas Devlieghere)
2176498aff2SJonas Devlieghere{
2186498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
2196498aff2SJonas Devlieghere        Py_RETURN_NONE;
2206498aff2SJonas Devlieghere
2216498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
2226498aff2SJonas Devlieghere
2236498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
2246498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name,dict);
2256498aff2SJonas Devlieghere
2266498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
2276498aff2SJonas Devlieghere        Py_RETURN_NONE;
2286498aff2SJonas Devlieghere
229*7f09ab08SPavel Labath    auto sb_value = std::make_unique<lldb::SBValue>(valobj_sp);
2306498aff2SJonas Devlieghere    sb_value->SetPreferSyntheticValue(false);
2316498aff2SJonas Devlieghere
232*7f09ab08SPavel Labath    PythonObject val_arg = ToSWIGWrapper(std::move(sb_value));
2336498aff2SJonas Devlieghere    if (!val_arg.IsAllocated())
2346498aff2SJonas Devlieghere        Py_RETURN_NONE;
2356498aff2SJonas Devlieghere
2366498aff2SJonas Devlieghere    PythonObject result = pfunc(val_arg, dict);
2376498aff2SJonas Devlieghere
2386498aff2SJonas Devlieghere    if (result.IsAllocated())
2396498aff2SJonas Devlieghere        return result.release();
2406498aff2SJonas Devlieghere
2416498aff2SJonas Devlieghere    Py_RETURN_NONE;
2426498aff2SJonas Devlieghere}
2436498aff2SJonas Devlieghere
2446498aff2SJonas DevlieghereSWIGEXPORT void*
2456498aff2SJonas DevlieghereLLDBSwigPythonCreateCommandObject
2466498aff2SJonas Devlieghere(
2476498aff2SJonas Devlieghere    const char *python_class_name,
2486498aff2SJonas Devlieghere    const char *session_dictionary_name,
2496498aff2SJonas Devlieghere    const lldb::DebuggerSP debugger_sp
2506498aff2SJonas Devlieghere)
2516498aff2SJonas Devlieghere{
2526498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
2536498aff2SJonas Devlieghere        Py_RETURN_NONE;
2546498aff2SJonas Devlieghere
2556498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
2566498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
2576498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
2586498aff2SJonas Devlieghere
2596498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
2606498aff2SJonas Devlieghere        return nullptr;
2616498aff2SJonas Devlieghere
2626498aff2SJonas Devlieghere    lldb::SBDebugger debugger_sb(debugger_sp);
2636498aff2SJonas Devlieghere    PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
2646498aff2SJonas Devlieghere    PythonObject result = pfunc(debugger_arg, dict);
2656498aff2SJonas Devlieghere
2666498aff2SJonas Devlieghere    if (result.IsAllocated())
2676498aff2SJonas Devlieghere        return result.release();
2686498aff2SJonas Devlieghere
2696498aff2SJonas Devlieghere    Py_RETURN_NONE;
2706498aff2SJonas Devlieghere}
2716498aff2SJonas Devlieghere
2726498aff2SJonas DevlieghereSWIGEXPORT void*
2731f6a57c1SMed Ismail BennaniLLDBSwigPythonCreateScriptedProcess
2741f6a57c1SMed Ismail Bennani(
2751f6a57c1SMed Ismail Bennani    const char *python_class_name,
2761f6a57c1SMed Ismail Bennani    const char *session_dictionary_name,
2771f6a57c1SMed Ismail Bennani    const lldb::TargetSP& target_sp,
2781f6a57c1SMed Ismail Bennani    lldb_private::StructuredDataImpl *args_impl,
2791f6a57c1SMed Ismail Bennani    std::string &error_string
2801f6a57c1SMed Ismail Bennani)
2811f6a57c1SMed Ismail Bennani{
2821f6a57c1SMed Ismail Bennani    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
2831f6a57c1SMed Ismail Bennani        Py_RETURN_NONE;
2841f6a57c1SMed Ismail Bennani
2851f6a57c1SMed Ismail Bennani    PyErr_Cleaner py_err_cleaner(true);
2861f6a57c1SMed Ismail Bennani
2871f6a57c1SMed Ismail Bennani    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
2881f6a57c1SMed Ismail Bennani    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
2891f6a57c1SMed Ismail Bennani
2901f6a57c1SMed Ismail Bennani    if (!pfunc.IsAllocated()) {
2911f6a57c1SMed Ismail Bennani        error_string.append("could not find script class: ");
2921f6a57c1SMed Ismail Bennani        error_string.append(python_class_name);
2931f6a57c1SMed Ismail Bennani        return nullptr;
2941f6a57c1SMed Ismail Bennani    }
2951f6a57c1SMed Ismail Bennani
296*7f09ab08SPavel Labath    PythonObject target_arg = ToSWIGWrapper(target_sp);
2971f6a57c1SMed Ismail Bennani
2981f6a57c1SMed Ismail Bennani    llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
2991f6a57c1SMed Ismail Bennani    if (!arg_info) {
3001f6a57c1SMed Ismail Bennani        llvm::handleAllErrors(
3011f6a57c1SMed Ismail Bennani            arg_info.takeError(),
3021f6a57c1SMed Ismail Bennani            [&](PythonException &E) {
3031f6a57c1SMed Ismail Bennani                error_string.append(E.ReadBacktrace());
3041f6a57c1SMed Ismail Bennani            },
3051f6a57c1SMed Ismail Bennani            [&](const llvm::ErrorInfoBase &E) {
3061f6a57c1SMed Ismail Bennani                error_string.append(E.message());
3071f6a57c1SMed Ismail Bennani            });
3081f6a57c1SMed Ismail Bennani        Py_RETURN_NONE;
3091f6a57c1SMed Ismail Bennani    }
3101f6a57c1SMed Ismail Bennani
3111f6a57c1SMed Ismail Bennani    PythonObject result = {};
3121f6a57c1SMed Ismail Bennani    if (arg_info.get().max_positional_args == 2) {
313f1127914SPavel Labath        // FIXME: SBStructuredData leaked here
314f1127914SPavel Labath        PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*new lldb::SBStructuredData(args_impl)));
315738621d0SMed Ismail Bennani        result = pfunc(target_arg, args_arg);
3161f6a57c1SMed Ismail Bennani    } else {
317738621d0SMed Ismail Bennani        error_string.assign("wrong number of arguments in __init__, should be 2 (not including self)");
3181f6a57c1SMed Ismail Bennani        Py_RETURN_NONE;
3191f6a57c1SMed Ismail Bennani    }
3201f6a57c1SMed Ismail Bennani
3211f6a57c1SMed Ismail Bennani    if (result.IsAllocated())
3221f6a57c1SMed Ismail Bennani        return result.release();
3231f6a57c1SMed Ismail Bennani    Py_RETURN_NONE;
3241f6a57c1SMed Ismail Bennani}
3251f6a57c1SMed Ismail Bennani
3261f6a57c1SMed Ismail BennaniSWIGEXPORT void*
32759d8dd79SMed Ismail BennaniLLDBSwigPythonCreateScriptedThread
32859d8dd79SMed Ismail Bennani(
32959d8dd79SMed Ismail Bennani    const char *python_class_name,
33059d8dd79SMed Ismail Bennani    const char *session_dictionary_name,
331738621d0SMed Ismail Bennani    const lldb::ProcessSP& process_sp,
332738621d0SMed Ismail Bennani    lldb_private::StructuredDataImpl *args_impl,
33359d8dd79SMed Ismail Bennani    std::string &error_string
33459d8dd79SMed Ismail Bennani)
33559d8dd79SMed Ismail Bennani{
33659d8dd79SMed Ismail Bennani    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
33759d8dd79SMed Ismail Bennani        Py_RETURN_NONE;
33859d8dd79SMed Ismail Bennani
33959d8dd79SMed Ismail Bennani    PyErr_Cleaner py_err_cleaner(true);
34059d8dd79SMed Ismail Bennani
34159d8dd79SMed Ismail Bennani    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
34259d8dd79SMed Ismail Bennani    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
34359d8dd79SMed Ismail Bennani
34459d8dd79SMed Ismail Bennani    if (!pfunc.IsAllocated()) {
34559d8dd79SMed Ismail Bennani        error_string.append("could not find script class: ");
34659d8dd79SMed Ismail Bennani        error_string.append(python_class_name);
34759d8dd79SMed Ismail Bennani        return nullptr;
34859d8dd79SMed Ismail Bennani    }
34959d8dd79SMed Ismail Bennani
35059d8dd79SMed Ismail Bennani    llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
35159d8dd79SMed Ismail Bennani    if (!arg_info) {
35259d8dd79SMed Ismail Bennani        llvm::handleAllErrors(
35359d8dd79SMed Ismail Bennani            arg_info.takeError(),
35459d8dd79SMed Ismail Bennani            [&](PythonException &E) {
35559d8dd79SMed Ismail Bennani                error_string.append(E.ReadBacktrace());
35659d8dd79SMed Ismail Bennani            },
35759d8dd79SMed Ismail Bennani            [&](const llvm::ErrorInfoBase &E) {
35859d8dd79SMed Ismail Bennani                error_string.append(E.message());
35959d8dd79SMed Ismail Bennani            });
36059d8dd79SMed Ismail Bennani        Py_RETURN_NONE;
36159d8dd79SMed Ismail Bennani    }
36259d8dd79SMed Ismail Bennani
36359d8dd79SMed Ismail Bennani    PythonObject result = {};
364738621d0SMed Ismail Bennani    if (arg_info.get().max_positional_args == 2) {
365f1127914SPavel Labath        // FIXME: SBStructuredData leaked here
366f1127914SPavel Labath        PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*new lldb::SBStructuredData(args_impl)));
367*7f09ab08SPavel Labath        result = pfunc(ToSWIGWrapper(process_sp), args_arg);
36859d8dd79SMed Ismail Bennani    } else {
369738621d0SMed Ismail Bennani        error_string.assign("wrong number of arguments in __init__, should be 2 (not including self)");
37059d8dd79SMed Ismail Bennani        Py_RETURN_NONE;
37159d8dd79SMed Ismail Bennani    }
37259d8dd79SMed Ismail Bennani
37359d8dd79SMed Ismail Bennani    if (result.IsAllocated())
37459d8dd79SMed Ismail Bennani        return result.release();
37559d8dd79SMed Ismail Bennani    Py_RETURN_NONE;
37659d8dd79SMed Ismail Bennani}
37759d8dd79SMed Ismail Bennani
37859d8dd79SMed Ismail BennaniSWIGEXPORT void*
3796498aff2SJonas DevlieghereLLDBSwigPythonCreateScriptedThreadPlan
3806498aff2SJonas Devlieghere(
3816498aff2SJonas Devlieghere    const char *python_class_name,
3826498aff2SJonas Devlieghere    const char *session_dictionary_name,
3836498aff2SJonas Devlieghere    lldb_private::StructuredDataImpl *args_impl,
3846498aff2SJonas Devlieghere    std::string &error_string,
3856498aff2SJonas Devlieghere    const lldb::ThreadPlanSP& thread_plan_sp
3866498aff2SJonas Devlieghere)
3876498aff2SJonas Devlieghere{
3886498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
3896498aff2SJonas Devlieghere        Py_RETURN_NONE;
3906498aff2SJonas Devlieghere
3916498aff2SJonas Devlieghere
3926498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
3936498aff2SJonas Devlieghere
3946498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
3956498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
3966498aff2SJonas Devlieghere
3976498aff2SJonas Devlieghere    if (!pfunc.IsAllocated()) {
3986498aff2SJonas Devlieghere        error_string.append("could not find script class: ");
3996498aff2SJonas Devlieghere        error_string.append(python_class_name);
4006498aff2SJonas Devlieghere        return nullptr;
4016498aff2SJonas Devlieghere    }
4026498aff2SJonas Devlieghere
403*7f09ab08SPavel Labath    PythonObject tp_arg = ToSWIGWrapper(thread_plan_sp);
4046498aff2SJonas Devlieghere
4056498aff2SJonas Devlieghere    llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
4066498aff2SJonas Devlieghere    if (!arg_info) {
4076498aff2SJonas Devlieghere        llvm::handleAllErrors(
4086498aff2SJonas Devlieghere            arg_info.takeError(),
4096498aff2SJonas Devlieghere            [&](PythonException &E) {
4106498aff2SJonas Devlieghere                error_string.append(E.ReadBacktrace());
4116498aff2SJonas Devlieghere            },
4126498aff2SJonas Devlieghere            [&](const llvm::ErrorInfoBase &E) {
4136498aff2SJonas Devlieghere                error_string.append(E.message());
4146498aff2SJonas Devlieghere            });
4156498aff2SJonas Devlieghere        Py_RETURN_NONE;
4166498aff2SJonas Devlieghere    }
4176498aff2SJonas Devlieghere
4186498aff2SJonas Devlieghere    PythonObject result = {};
4196498aff2SJonas Devlieghere    if (arg_info.get().max_positional_args == 2) {
4206498aff2SJonas Devlieghere        if (args_impl != nullptr) {
4216498aff2SJonas Devlieghere           error_string.assign("args passed, but __init__ does not take an args dictionary");
4226498aff2SJonas Devlieghere           Py_RETURN_NONE;
4236498aff2SJonas Devlieghere        }
4246498aff2SJonas Devlieghere        result = pfunc(tp_arg, dict);
4256498aff2SJonas Devlieghere    } else if (arg_info.get().max_positional_args >= 3) {
426f1127914SPavel Labath        // FIXME: SBStructuredData leaked here
427f1127914SPavel Labath        PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*new lldb::SBStructuredData(args_impl)));
4286498aff2SJonas Devlieghere        result = pfunc(tp_arg, args_arg, dict);
4296498aff2SJonas Devlieghere    } else {
4306498aff2SJonas Devlieghere        error_string.assign("wrong number of arguments in __init__, should be 2 or 3 (not including self)");
4316498aff2SJonas Devlieghere        Py_RETURN_NONE;
4326498aff2SJonas Devlieghere    }
4336498aff2SJonas Devlieghere
4346498aff2SJonas Devlieghere    // FIXME: At this point we should check that the class we found supports all the methods
4356498aff2SJonas Devlieghere    // that we need.
4366498aff2SJonas Devlieghere
4376498aff2SJonas Devlieghere    if (result.IsAllocated())
4386498aff2SJonas Devlieghere        return result.release();
4396498aff2SJonas Devlieghere    Py_RETURN_NONE;
4406498aff2SJonas Devlieghere}
4416498aff2SJonas Devlieghere
4426498aff2SJonas DevlieghereSWIGEXPORT bool
4436498aff2SJonas DevlieghereLLDBSWIGPythonCallThreadPlan
4446498aff2SJonas Devlieghere(
4456498aff2SJonas Devlieghere    void *implementor,
4466498aff2SJonas Devlieghere    const char *method_name,
4476498aff2SJonas Devlieghere    lldb_private::Event *event,
4486498aff2SJonas Devlieghere    bool &got_error
4496498aff2SJonas Devlieghere)
4506498aff2SJonas Devlieghere{
4516498aff2SJonas Devlieghere    got_error = false;
4526498aff2SJonas Devlieghere
4536498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(false);
4546498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor));
4556498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>(method_name);
4566498aff2SJonas Devlieghere
4576498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
4586498aff2SJonas Devlieghere        return false;
4596498aff2SJonas Devlieghere
4606498aff2SJonas Devlieghere    PythonObject result;
4616498aff2SJonas Devlieghere    if (event != nullptr)
4626498aff2SJonas Devlieghere    {
4636498aff2SJonas Devlieghere        lldb::SBEvent sb_event(event);
4646498aff2SJonas Devlieghere        PythonObject event_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_event));
4656498aff2SJonas Devlieghere        result = pfunc(event_arg);
4666498aff2SJonas Devlieghere    }
4676498aff2SJonas Devlieghere    else
4686498aff2SJonas Devlieghere        result = pfunc();
4696498aff2SJonas Devlieghere
4706498aff2SJonas Devlieghere    if (PyErr_Occurred())
4716498aff2SJonas Devlieghere    {
4726498aff2SJonas Devlieghere        got_error = true;
4736498aff2SJonas Devlieghere        printf ("Return value was neither false nor true for call to %s.\n", method_name);
4746498aff2SJonas Devlieghere        PyErr_Print();
4756498aff2SJonas Devlieghere        return false;
4766498aff2SJonas Devlieghere    }
4776498aff2SJonas Devlieghere
4786498aff2SJonas Devlieghere    if (result.get() == Py_True)
4796498aff2SJonas Devlieghere        return true;
4806498aff2SJonas Devlieghere    else if (result.get() == Py_False)
4816498aff2SJonas Devlieghere        return false;
4826498aff2SJonas Devlieghere
4836498aff2SJonas Devlieghere    // Somebody returned the wrong thing...
4846498aff2SJonas Devlieghere    got_error = true;
4856498aff2SJonas Devlieghere    printf ("Wrong return value type for call to %s.\n", method_name);
4866498aff2SJonas Devlieghere    return false;
4876498aff2SJonas Devlieghere}
4886498aff2SJonas Devlieghere
489*7f09ab08SPavel LabathSWIGEXPORT void *LLDBSwigPythonCreateScriptedBreakpointResolver(
490*7f09ab08SPavel Labath    const char *python_class_name, const char *session_dictionary_name,
4916498aff2SJonas Devlieghere    lldb_private::StructuredDataImpl *args_impl,
492*7f09ab08SPavel Labath    const lldb::BreakpointSP &breakpoint_sp) {
493*7f09ab08SPavel Labath
4946498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
4956498aff2SJonas Devlieghere        Py_RETURN_NONE;
4966498aff2SJonas Devlieghere
4976498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
4986498aff2SJonas Devlieghere
4996498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
5006498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
5016498aff2SJonas Devlieghere
5026498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
5036498aff2SJonas Devlieghere        return nullptr;
5046498aff2SJonas Devlieghere
505f1127914SPavel Labath    // FIXME: SBStructuredData leaked here
5066498aff2SJonas Devlieghere    lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl);
507f1127914SPavel Labath    PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*args_value));
5086498aff2SJonas Devlieghere
509*7f09ab08SPavel Labath    PythonObject result = pfunc(ToSWIGWrapper(breakpoint_sp), args_arg, dict);
5106498aff2SJonas Devlieghere    // FIXME: At this point we should check that the class we found supports all the methods
5116498aff2SJonas Devlieghere    // that we need.
5126498aff2SJonas Devlieghere
5136498aff2SJonas Devlieghere    if (result.IsAllocated())
5146498aff2SJonas Devlieghere    {
5156498aff2SJonas Devlieghere        // Check that __callback__ is defined:
5166498aff2SJonas Devlieghere        auto callback_func = result.ResolveName<PythonCallable>("__callback__");
5176498aff2SJonas Devlieghere        if (callback_func.IsAllocated())
5186498aff2SJonas Devlieghere            return result.release();
5196498aff2SJonas Devlieghere        else
5206498aff2SJonas Devlieghere            result.release();
5216498aff2SJonas Devlieghere    }
5226498aff2SJonas Devlieghere    Py_RETURN_NONE;
5236498aff2SJonas Devlieghere}
5246498aff2SJonas Devlieghere
5256498aff2SJonas DevlieghereSWIGEXPORT unsigned int
5266498aff2SJonas DevlieghereLLDBSwigPythonCallBreakpointResolver
5276498aff2SJonas Devlieghere(
5286498aff2SJonas Devlieghere    void *implementor,
5296498aff2SJonas Devlieghere    const char *method_name,
5306498aff2SJonas Devlieghere    lldb_private::SymbolContext *sym_ctx
5316498aff2SJonas Devlieghere)
5326498aff2SJonas Devlieghere{
5336498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(false);
5346498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor));
5356498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>(method_name);
5366498aff2SJonas Devlieghere
5376498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
5386498aff2SJonas Devlieghere        return 0;
5396498aff2SJonas Devlieghere
5406498aff2SJonas Devlieghere    PythonObject result;
5416498aff2SJonas Devlieghere    if (sym_ctx != nullptr) {
5426498aff2SJonas Devlieghere      lldb::SBSymbolContext sb_sym_ctx(sym_ctx);
5436498aff2SJonas Devlieghere      PythonObject sym_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_sym_ctx));
5446498aff2SJonas Devlieghere      result = pfunc(sym_ctx_arg);
5456498aff2SJonas Devlieghere    } else
5466498aff2SJonas Devlieghere      result = pfunc();
5476498aff2SJonas Devlieghere
5486498aff2SJonas Devlieghere    if (PyErr_Occurred())
5496498aff2SJonas Devlieghere    {
5506498aff2SJonas Devlieghere        PyErr_Print();
55152712d3fSLawrence D'Anna        PyErr_Clear();
5526498aff2SJonas Devlieghere        return 0;
5536498aff2SJonas Devlieghere    }
5546498aff2SJonas Devlieghere
5556498aff2SJonas Devlieghere    // The callback will return a bool, but we're need to also return ints
5566498aff2SJonas Devlieghere    // so we're squirrelling the bool through as an int...  And if you return
5576498aff2SJonas Devlieghere    // nothing, we'll continue.
5586498aff2SJonas Devlieghere    if (strcmp(method_name, "__callback__") == 0) {
5596498aff2SJonas Devlieghere        if (result.get() == Py_False)
5606498aff2SJonas Devlieghere          return 0;
5616498aff2SJonas Devlieghere        else
5626498aff2SJonas Devlieghere          return 1;
5636498aff2SJonas Devlieghere    }
5646498aff2SJonas Devlieghere
56552712d3fSLawrence D'Anna    long long ret_val = unwrapOrSetPythonException(As<long long>(result));
566478619cfSMuhammad Omair Javaid
56752712d3fSLawrence D'Anna    if (PyErr_Occurred()) {
56852712d3fSLawrence D'Anna        PyErr_Print();
56952712d3fSLawrence D'Anna        PyErr_Clear();
57052712d3fSLawrence D'Anna        return 0;
57152712d3fSLawrence D'Anna    }
5726498aff2SJonas Devlieghere
5736498aff2SJonas Devlieghere    return ret_val;
5746498aff2SJonas Devlieghere}
5756498aff2SJonas Devlieghere
5761b1d9815SJim InghamSWIGEXPORT void *
5771b1d9815SJim InghamLLDBSwigPythonCreateScriptedStopHook
5781b1d9815SJim Ingham(
5791b1d9815SJim Ingham    lldb::TargetSP target_sp,
5801b1d9815SJim Ingham    const char *python_class_name,
5811b1d9815SJim Ingham    const char *session_dictionary_name,
5821b1d9815SJim Ingham    lldb_private::StructuredDataImpl *args_impl,
5831b1d9815SJim Ingham    Status &error
5841b1d9815SJim Ingham)
5851b1d9815SJim Ingham{
5861b1d9815SJim Ingham    if (python_class_name == NULL || python_class_name[0] == '\0') {
5871b1d9815SJim Ingham        error.SetErrorString("Empty class name.");
5881b1d9815SJim Ingham        Py_RETURN_NONE;
5891b1d9815SJim Ingham    }
5901b1d9815SJim Ingham    if (!session_dictionary_name) {
5911b1d9815SJim Ingham      error.SetErrorString("No session dictionary");
5921b1d9815SJim Ingham      Py_RETURN_NONE;
5931b1d9815SJim Ingham    }
5941b1d9815SJim Ingham
5951b1d9815SJim Ingham    PyErr_Cleaner py_err_cleaner(true);
5961b1d9815SJim Ingham
5971b1d9815SJim Ingham    auto dict =
5981b1d9815SJim Ingham        PythonModule::MainModule().ResolveName<PythonDictionary>(
5991b1d9815SJim Ingham            session_dictionary_name);
6001b1d9815SJim Ingham    auto pfunc =
6011b1d9815SJim Ingham        PythonObject::ResolveNameWithDictionary<PythonCallable>(
6021b1d9815SJim Ingham            python_class_name, dict);
6031b1d9815SJim Ingham
6041b1d9815SJim Ingham    if (!pfunc.IsAllocated()) {
6051b1d9815SJim Ingham        error.SetErrorStringWithFormat("Could not find class: %s.",
6061b1d9815SJim Ingham                                       python_class_name);
6071b1d9815SJim Ingham        return nullptr;
6081b1d9815SJim Ingham    }
6091b1d9815SJim Ingham
610f1127914SPavel Labath    // FIXME: SBStructuredData leaked here
6111b1d9815SJim Ingham    lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl);
612f1127914SPavel Labath    PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*args_value));
6131b1d9815SJim Ingham
614*7f09ab08SPavel Labath    PythonObject result = pfunc(ToSWIGWrapper(target_sp), args_arg, dict);
6151b1d9815SJim Ingham
6161b1d9815SJim Ingham    if (result.IsAllocated())
6171b1d9815SJim Ingham    {
6181b1d9815SJim Ingham        // Check that the handle_stop callback is defined:
6191b1d9815SJim Ingham        auto callback_func = result.ResolveName<PythonCallable>("handle_stop");
6201b1d9815SJim Ingham        if (callback_func.IsAllocated()) {
6211b1d9815SJim Ingham          if (auto args_info = callback_func.GetArgInfo()) {
6221b1d9815SJim Ingham            size_t num_args = (*args_info).max_positional_args;
6231b1d9815SJim Ingham            if (num_args != 2) {
6241b1d9815SJim Ingham              error.SetErrorStringWithFormat("Wrong number of args for "
6252f95c50aSRichard Smith              "handle_stop callback, should be 2 (excluding self), got: %zu",
6261b1d9815SJim Ingham              num_args);
6271b1d9815SJim Ingham              Py_RETURN_NONE;
6281b1d9815SJim Ingham            } else
6291b1d9815SJim Ingham              return result.release();
6301b1d9815SJim Ingham          } else {
6311b1d9815SJim Ingham            error.SetErrorString("Couldn't get num arguments for handle_stop "
6321b1d9815SJim Ingham                                 "callback.");
6331b1d9815SJim Ingham            Py_RETURN_NONE;
6341b1d9815SJim Ingham          }
6351b1d9815SJim Ingham          return result.release();
6361b1d9815SJim Ingham        }
6371b1d9815SJim Ingham        else {
6381b1d9815SJim Ingham          error.SetErrorStringWithFormat("Class \"%s\" is missing the required "
6391b1d9815SJim Ingham                                         "handle_stop callback.",
6401b1d9815SJim Ingham                                         python_class_name);
6411b1d9815SJim Ingham          result.release();
6421b1d9815SJim Ingham        }
6431b1d9815SJim Ingham    }
6441b1d9815SJim Ingham    Py_RETURN_NONE;
6451b1d9815SJim Ingham}
6461b1d9815SJim Ingham
6471b1d9815SJim InghamSWIGEXPORT bool
6481b1d9815SJim InghamLLDBSwigPythonStopHookCallHandleStop
6491b1d9815SJim Ingham(
6501b1d9815SJim Ingham    void *implementor,
6511b1d9815SJim Ingham    lldb::ExecutionContextRefSP exc_ctx_sp,
6521b1d9815SJim Ingham    lldb::StreamSP stream
6531b1d9815SJim Ingham)
6541b1d9815SJim Ingham{
6551b1d9815SJim Ingham    // handle_stop will return a bool with the meaning "should_stop"...
6561b1d9815SJim Ingham    // If you return nothing we'll assume we are going to stop.
6571b1d9815SJim Ingham    // Also any errors should return true, since we should stop on error.
6581b1d9815SJim Ingham
6591b1d9815SJim Ingham    PyErr_Cleaner py_err_cleaner(false);
6601b1d9815SJim Ingham    PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor));
6611b1d9815SJim Ingham    auto pfunc = self.ResolveName<PythonCallable>("handle_stop");
6621b1d9815SJim Ingham
6631b1d9815SJim Ingham    if (!pfunc.IsAllocated())
6641b1d9815SJim Ingham        return true;
6651b1d9815SJim Ingham
6661b1d9815SJim Ingham    PythonObject result;
6671b1d9815SJim Ingham    lldb::SBExecutionContext sb_exc_ctx(exc_ctx_sp);
6681b1d9815SJim Ingham    PythonObject exc_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_exc_ctx));
6691b1d9815SJim Ingham    lldb::SBStream sb_stream;
6701b1d9815SJim Ingham    PythonObject sb_stream_arg(PyRefType::Owned,
6711b1d9815SJim Ingham                               SBTypeToSWIGWrapper(sb_stream));
6721b1d9815SJim Ingham    result = pfunc(exc_ctx_arg, sb_stream_arg);
6731b1d9815SJim Ingham
6741b1d9815SJim Ingham    if (PyErr_Occurred())
6751b1d9815SJim Ingham    {
6761b1d9815SJim Ingham        stream->PutCString("Python error occurred handling stop-hook.");
6771b1d9815SJim Ingham        PyErr_Print();
6781b1d9815SJim Ingham        PyErr_Clear();
6791b1d9815SJim Ingham        return true;
6801b1d9815SJim Ingham    }
6811b1d9815SJim Ingham
6821b1d9815SJim Ingham    // Now add the result to the output stream.  SBStream only
6831b1d9815SJim Ingham    // makes an internally help StreamString which I can't interpose, so I
6841b1d9815SJim Ingham    // have to copy it over here.
6851b1d9815SJim Ingham    stream->PutCString(sb_stream.GetData());
6861b1d9815SJim Ingham
6871b1d9815SJim Ingham    if (result.get() == Py_False)
6881b1d9815SJim Ingham      return false;
6891b1d9815SJim Ingham    else
6901b1d9815SJim Ingham      return true;
6911b1d9815SJim Ingham}
6921b1d9815SJim Ingham
6936498aff2SJonas Devlieghere// wrapper that calls an optional instance member of an object taking no arguments
6946498aff2SJonas Devliegherestatic PyObject*
6956498aff2SJonas DevlieghereLLDBSwigPython_CallOptionalMember
6966498aff2SJonas Devlieghere(
6976498aff2SJonas Devlieghere    PyObject* implementor,
6986498aff2SJonas Devlieghere    char* callee_name,
6996498aff2SJonas Devlieghere    PyObject* ret_if_not_found = Py_None,
7006498aff2SJonas Devlieghere    bool* was_found = NULL
7016498aff2SJonas Devlieghere)
7026498aff2SJonas Devlieghere{
7036498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(false);
7046498aff2SJonas Devlieghere
7056498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor));
7066498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>(callee_name);
7076498aff2SJonas Devlieghere
7086498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
7096498aff2SJonas Devlieghere    {
7106498aff2SJonas Devlieghere        if (was_found)
7116498aff2SJonas Devlieghere            *was_found = false;
7126498aff2SJonas Devlieghere        Py_XINCREF(ret_if_not_found);
7136498aff2SJonas Devlieghere        return ret_if_not_found;
7146498aff2SJonas Devlieghere    }
7156498aff2SJonas Devlieghere
7166498aff2SJonas Devlieghere    if (was_found)
7176498aff2SJonas Devlieghere        *was_found = true;
7186498aff2SJonas Devlieghere
7196498aff2SJonas Devlieghere    PythonObject result = pfunc();
7206498aff2SJonas Devlieghere    return result.release();
7216498aff2SJonas Devlieghere}
7226498aff2SJonas Devlieghere
7236498aff2SJonas DevlieghereSWIGEXPORT size_t
7246498aff2SJonas DevlieghereLLDBSwigPython_CalculateNumChildren
7256498aff2SJonas Devlieghere(
7266498aff2SJonas Devlieghere    PyObject *implementor,
7276498aff2SJonas Devlieghere    uint32_t max
7286498aff2SJonas Devlieghere)
7296498aff2SJonas Devlieghere{
7306498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, implementor);
7316498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>("num_children");
7326498aff2SJonas Devlieghere
7336498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
7346498aff2SJonas Devlieghere        return 0;
7356498aff2SJonas Devlieghere
7366498aff2SJonas Devlieghere    auto arg_info = pfunc.GetArgInfo();
7376498aff2SJonas Devlieghere    if (!arg_info) {
7386498aff2SJonas Devlieghere        llvm::consumeError(arg_info.takeError());
7396498aff2SJonas Devlieghere        return 0;
7406498aff2SJonas Devlieghere    }
7416498aff2SJonas Devlieghere
74252712d3fSLawrence D'Anna    size_t ret_val;
743478619cfSMuhammad Omair Javaid    if (arg_info.get().max_positional_args < 1)
74452712d3fSLawrence D'Anna        ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call()));
745478619cfSMuhammad Omair Javaid    else
74652712d3fSLawrence D'Anna        ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call(PythonInteger(max))));
747478619cfSMuhammad Omair Javaid
74852712d3fSLawrence D'Anna    if (PyErr_Occurred())
7496498aff2SJonas Devlieghere    {
7506498aff2SJonas Devlieghere        PyErr_Print();
7516498aff2SJonas Devlieghere        PyErr_Clear();
75252712d3fSLawrence D'Anna        return 0;
7536498aff2SJonas Devlieghere    }
7546498aff2SJonas Devlieghere
7556498aff2SJonas Devlieghere    if (arg_info.get().max_positional_args < 1)
7566498aff2SJonas Devlieghere        ret_val = std::min(ret_val, static_cast<size_t>(max));
7576498aff2SJonas Devlieghere
7586498aff2SJonas Devlieghere    return ret_val;
7596498aff2SJonas Devlieghere}
7606498aff2SJonas Devlieghere
7616498aff2SJonas DevlieghereSWIGEXPORT PyObject*
7626498aff2SJonas DevlieghereLLDBSwigPython_GetChildAtIndex
7636498aff2SJonas Devlieghere(
7646498aff2SJonas Devlieghere    PyObject *implementor,
7656498aff2SJonas Devlieghere    uint32_t idx
7666498aff2SJonas Devlieghere)
7676498aff2SJonas Devlieghere{
7686498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
7696498aff2SJonas Devlieghere
7706498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, implementor);
7716498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index");
7726498aff2SJonas Devlieghere
7736498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
7746498aff2SJonas Devlieghere        return nullptr;
7756498aff2SJonas Devlieghere
7766498aff2SJonas Devlieghere    PythonObject result = pfunc(PythonInteger(idx));
7776498aff2SJonas Devlieghere
7786498aff2SJonas Devlieghere    if (!result.IsAllocated())
7796498aff2SJonas Devlieghere        return nullptr;
7806498aff2SJonas Devlieghere
7816498aff2SJonas Devlieghere    lldb::SBValue* sbvalue_ptr = nullptr;
7826498aff2SJonas Devlieghere    if (SWIG_ConvertPtr(result.get(), (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
7836498aff2SJonas Devlieghere        return nullptr;
7846498aff2SJonas Devlieghere
7856498aff2SJonas Devlieghere    if (sbvalue_ptr == nullptr)
7866498aff2SJonas Devlieghere        return nullptr;
7876498aff2SJonas Devlieghere
7886498aff2SJonas Devlieghere    return result.release();
7896498aff2SJonas Devlieghere}
7906498aff2SJonas Devlieghere
7916498aff2SJonas DevlieghereSWIGEXPORT int
7926498aff2SJonas DevlieghereLLDBSwigPython_GetIndexOfChildWithName
7936498aff2SJonas Devlieghere(
7946498aff2SJonas Devlieghere    PyObject *implementor,
7956498aff2SJonas Devlieghere    const char* child_name
7966498aff2SJonas Devlieghere)
7976498aff2SJonas Devlieghere{
7986498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
7996498aff2SJonas Devlieghere
8006498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, implementor);
8016498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>("get_child_index");
8026498aff2SJonas Devlieghere
8036498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
8046498aff2SJonas Devlieghere        return UINT32_MAX;
8056498aff2SJonas Devlieghere
80652712d3fSLawrence D'Anna    llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name));
8076498aff2SJonas Devlieghere
80852712d3fSLawrence D'Anna    long long retval = unwrapOrSetPythonException(As<long long>(std::move(result)));
80952712d3fSLawrence D'Anna
81052712d3fSLawrence D'Anna    if (PyErr_Occurred()) {
81152712d3fSLawrence D'Anna        PyErr_Clear(); // FIXME print this? do something else
8126498aff2SJonas Devlieghere        return UINT32_MAX;
81352712d3fSLawrence D'Anna    }
8146498aff2SJonas Devlieghere
8156498aff2SJonas Devlieghere    if (retval >= 0)
8166498aff2SJonas Devlieghere        return (uint32_t)retval;
8176498aff2SJonas Devlieghere
8186498aff2SJonas Devlieghere    return UINT32_MAX;
8196498aff2SJonas Devlieghere}
8206498aff2SJonas Devlieghere
8216498aff2SJonas DevlieghereSWIGEXPORT bool
8226498aff2SJonas DevlieghereLLDBSwigPython_UpdateSynthProviderInstance
8236498aff2SJonas Devlieghere(
8246498aff2SJonas Devlieghere    PyObject *implementor
8256498aff2SJonas Devlieghere)
8266498aff2SJonas Devlieghere{
8276498aff2SJonas Devlieghere    bool ret_val = false;
8286498aff2SJonas Devlieghere
8296498aff2SJonas Devlieghere    static char callee_name[] = "update";
8306498aff2SJonas Devlieghere
8316498aff2SJonas Devlieghere    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name);
8326498aff2SJonas Devlieghere
8336498aff2SJonas Devlieghere    if (py_return == Py_True)
8346498aff2SJonas Devlieghere        ret_val = true;
8356498aff2SJonas Devlieghere
8366498aff2SJonas Devlieghere    Py_XDECREF(py_return);
8376498aff2SJonas Devlieghere
8386498aff2SJonas Devlieghere    return ret_val;
8396498aff2SJonas Devlieghere}
8406498aff2SJonas Devlieghere
8416498aff2SJonas DevlieghereSWIGEXPORT bool
8426498aff2SJonas DevlieghereLLDBSwigPython_MightHaveChildrenSynthProviderInstance
8436498aff2SJonas Devlieghere(
8446498aff2SJonas Devlieghere    PyObject *implementor
8456498aff2SJonas Devlieghere)
8466498aff2SJonas Devlieghere{
8476498aff2SJonas Devlieghere    bool ret_val = false;
8486498aff2SJonas Devlieghere
8496498aff2SJonas Devlieghere    static char callee_name[] = "has_children";
8506498aff2SJonas Devlieghere
8516498aff2SJonas Devlieghere    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True);
8526498aff2SJonas Devlieghere
8536498aff2SJonas Devlieghere    if (py_return == Py_True)
8546498aff2SJonas Devlieghere        ret_val = true;
8556498aff2SJonas Devlieghere
8566498aff2SJonas Devlieghere    Py_XDECREF(py_return);
8576498aff2SJonas Devlieghere
8586498aff2SJonas Devlieghere    return ret_val;
8596498aff2SJonas Devlieghere}
8606498aff2SJonas Devlieghere
8616498aff2SJonas DevlieghereSWIGEXPORT PyObject*
8626498aff2SJonas DevlieghereLLDBSwigPython_GetValueSynthProviderInstance
8636498aff2SJonas Devlieghere(
8646498aff2SJonas Devlieghere    PyObject *implementor
8656498aff2SJonas Devlieghere)
8666498aff2SJonas Devlieghere{
8676498aff2SJonas Devlieghere    PyObject* ret_val = nullptr;
8686498aff2SJonas Devlieghere
8696498aff2SJonas Devlieghere    static char callee_name[] = "get_value";
8706498aff2SJonas Devlieghere
8716498aff2SJonas Devlieghere    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_None);
8726498aff2SJonas Devlieghere
8736498aff2SJonas Devlieghere    if (py_return == Py_None || py_return == nullptr)
8746498aff2SJonas Devlieghere        ret_val = nullptr;
8756498aff2SJonas Devlieghere
8766498aff2SJonas Devlieghere    lldb::SBValue* sbvalue_ptr = NULL;
8776498aff2SJonas Devlieghere
8786498aff2SJonas Devlieghere    if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
8796498aff2SJonas Devlieghere        ret_val = nullptr;
8806498aff2SJonas Devlieghere    else if (sbvalue_ptr == NULL)
8816498aff2SJonas Devlieghere        ret_val = nullptr;
8826498aff2SJonas Devlieghere    else
8836498aff2SJonas Devlieghere        ret_val = py_return;
8846498aff2SJonas Devlieghere
8856498aff2SJonas Devlieghere    Py_XDECREF(py_return);
8866498aff2SJonas Devlieghere    return ret_val;
8876498aff2SJonas Devlieghere}
8886498aff2SJonas Devlieghere
8896498aff2SJonas DevlieghereSWIGEXPORT void*
8901f6a57c1SMed Ismail BennaniLLDBSWIGPython_CastPyObjectToSBData
8911f6a57c1SMed Ismail Bennani(
8921f6a57c1SMed Ismail Bennani    PyObject* data
8931f6a57c1SMed Ismail Bennani)
8941f6a57c1SMed Ismail Bennani{
8951f6a57c1SMed Ismail Bennani    lldb::SBData* sb_ptr = nullptr;
8961f6a57c1SMed Ismail Bennani
8971f6a57c1SMed Ismail Bennani    int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0);
8981f6a57c1SMed Ismail Bennani
8991f6a57c1SMed Ismail Bennani    if (valid_cast == -1)
9001f6a57c1SMed Ismail Bennani        return NULL;
9011f6a57c1SMed Ismail Bennani
9021f6a57c1SMed Ismail Bennani    return sb_ptr;
9031f6a57c1SMed Ismail Bennani}
9041f6a57c1SMed Ismail Bennani
9051f6a57c1SMed Ismail Bennani
9061f6a57c1SMed Ismail BennaniSWIGEXPORT void*
9071f6a57c1SMed Ismail BennaniLLDBSWIGPython_CastPyObjectToSBError
9081f6a57c1SMed Ismail Bennani(
9091f6a57c1SMed Ismail Bennani    PyObject* data
9101f6a57c1SMed Ismail Bennani)
9111f6a57c1SMed Ismail Bennani{
9121f6a57c1SMed Ismail Bennani    lldb::SBError* sb_ptr = nullptr;
9131f6a57c1SMed Ismail Bennani
9141f6a57c1SMed Ismail Bennani    int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0);
9151f6a57c1SMed Ismail Bennani
9161f6a57c1SMed Ismail Bennani    if (valid_cast == -1)
9171f6a57c1SMed Ismail Bennani        return NULL;
9181f6a57c1SMed Ismail Bennani
9191f6a57c1SMed Ismail Bennani    return sb_ptr;
9201f6a57c1SMed Ismail Bennani}
9211f6a57c1SMed Ismail Bennani
9221f6a57c1SMed Ismail Bennani
9231f6a57c1SMed Ismail BennaniSWIGEXPORT void*
9246498aff2SJonas DevlieghereLLDBSWIGPython_CastPyObjectToSBValue
9256498aff2SJonas Devlieghere(
9266498aff2SJonas Devlieghere    PyObject* data
9276498aff2SJonas Devlieghere)
9286498aff2SJonas Devlieghere{
9296498aff2SJonas Devlieghere    lldb::SBValue* sb_ptr = NULL;
9306498aff2SJonas Devlieghere
9316498aff2SJonas Devlieghere    int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
9326498aff2SJonas Devlieghere
9336498aff2SJonas Devlieghere    if (valid_cast == -1)
9346498aff2SJonas Devlieghere        return NULL;
9356498aff2SJonas Devlieghere
9366498aff2SJonas Devlieghere    return sb_ptr;
9376498aff2SJonas Devlieghere}
9386498aff2SJonas Devlieghere
939a758c9f7SMed Ismail BennaniSWIGEXPORT void*
940a758c9f7SMed Ismail BennaniLLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo
941a758c9f7SMed Ismail Bennani(
942a758c9f7SMed Ismail Bennani    PyObject* data
943a758c9f7SMed Ismail Bennani)
944a758c9f7SMed Ismail Bennani{
945a758c9f7SMed Ismail Bennani    lldb::SBMemoryRegionInfo* sb_ptr = NULL;
946a758c9f7SMed Ismail Bennani
947a758c9f7SMed Ismail Bennani    int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0);
948a758c9f7SMed Ismail Bennani
949a758c9f7SMed Ismail Bennani    if (valid_cast == -1)
950a758c9f7SMed Ismail Bennani        return NULL;
951a758c9f7SMed Ismail Bennani
952a758c9f7SMed Ismail Bennani    return sb_ptr;
953a758c9f7SMed Ismail Bennani}
954a758c9f7SMed Ismail Bennani
9556498aff2SJonas DevlieghereSWIGEXPORT bool
9566498aff2SJonas DevlieghereLLDBSwigPythonCallCommand
9576498aff2SJonas Devlieghere(
9586498aff2SJonas Devlieghere    const char *python_function_name,
9596498aff2SJonas Devlieghere    const char *session_dictionary_name,
9606498aff2SJonas Devlieghere    lldb::DebuggerSP& debugger,
9616498aff2SJonas Devlieghere    const char* args,
9626498aff2SJonas Devlieghere    lldb_private::CommandReturnObject& cmd_retobj,
9636498aff2SJonas Devlieghere    lldb::ExecutionContextRefSP exe_ctx_ref_sp
9646498aff2SJonas Devlieghere)
9656498aff2SJonas Devlieghere{
9666498aff2SJonas Devlieghere    lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
9676498aff2SJonas Devlieghere    lldb::SBDebugger debugger_sb(debugger);
9686498aff2SJonas Devlieghere    lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
9696498aff2SJonas Devlieghere
9706498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
9716498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
9726498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
9736498aff2SJonas Devlieghere
9746498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
9756498aff2SJonas Devlieghere        return false;
9766498aff2SJonas Devlieghere
9776498aff2SJonas Devlieghere    auto argc = pfunc.GetArgInfo();
9786498aff2SJonas Devlieghere    if (!argc) {
9796498aff2SJonas Devlieghere        llvm::consumeError(argc.takeError());
9806498aff2SJonas Devlieghere        return false;
9816498aff2SJonas Devlieghere    }
9826498aff2SJonas Devlieghere    PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
9836498aff2SJonas Devlieghere    PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb));
984f1127914SPavel Labath    PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(cmd_retobj_sb));
9856498aff2SJonas Devlieghere
9866498aff2SJonas Devlieghere    if (argc.get().max_positional_args < 5u)
9876498aff2SJonas Devlieghere        pfunc(debugger_arg, PythonString(args), cmd_retobj_arg, dict);
9886498aff2SJonas Devlieghere    else
9896498aff2SJonas Devlieghere        pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg, dict);
9906498aff2SJonas Devlieghere
9916498aff2SJonas Devlieghere    return true;
9926498aff2SJonas Devlieghere}
9936498aff2SJonas Devlieghere
9946498aff2SJonas DevlieghereSWIGEXPORT bool
9956498aff2SJonas DevlieghereLLDBSwigPythonCallCommandObject
9966498aff2SJonas Devlieghere(
9976498aff2SJonas Devlieghere    PyObject *implementor,
9986498aff2SJonas Devlieghere    lldb::DebuggerSP& debugger,
9996498aff2SJonas Devlieghere    const char* args,
10006498aff2SJonas Devlieghere    lldb_private::CommandReturnObject& cmd_retobj,
10016498aff2SJonas Devlieghere    lldb::ExecutionContextRefSP exe_ctx_ref_sp
10026498aff2SJonas Devlieghere)
10036498aff2SJonas Devlieghere{
10046498aff2SJonas Devlieghere    lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
10056498aff2SJonas Devlieghere    lldb::SBDebugger debugger_sb(debugger);
10066498aff2SJonas Devlieghere    lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
10076498aff2SJonas Devlieghere
10086498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
10096498aff2SJonas Devlieghere
10106498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, implementor);
10116498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>("__call__");
10126498aff2SJonas Devlieghere
10136498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
10146498aff2SJonas Devlieghere        return false;
10156498aff2SJonas Devlieghere
10166498aff2SJonas Devlieghere    PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
10176498aff2SJonas Devlieghere    PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb));
1018f1127914SPavel Labath    PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(cmd_retobj_sb));
10196498aff2SJonas Devlieghere
10206498aff2SJonas Devlieghere    pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg);
10216498aff2SJonas Devlieghere
10226498aff2SJonas Devlieghere    return true;
10236498aff2SJonas Devlieghere}
10246498aff2SJonas Devlieghere
10256498aff2SJonas DevlieghereSWIGEXPORT void*
10266498aff2SJonas DevlieghereLLDBSWIGPythonCreateOSPlugin
10276498aff2SJonas Devlieghere(
10286498aff2SJonas Devlieghere    const char *python_class_name,
10296498aff2SJonas Devlieghere    const char *session_dictionary_name,
10306498aff2SJonas Devlieghere    const lldb::ProcessSP& process_sp
10316498aff2SJonas Devlieghere)
10326498aff2SJonas Devlieghere{
10336498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
10346498aff2SJonas Devlieghere        Py_RETURN_NONE;
10356498aff2SJonas Devlieghere
10366498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
10376498aff2SJonas Devlieghere
10386498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
10396498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
10406498aff2SJonas Devlieghere
10416498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
10426498aff2SJonas Devlieghere        Py_RETURN_NONE;
10436498aff2SJonas Devlieghere
1044*7f09ab08SPavel Labath    auto result = pfunc(ToSWIGWrapper(process_sp));
10456498aff2SJonas Devlieghere
10466498aff2SJonas Devlieghere    if (result.IsAllocated())
10476498aff2SJonas Devlieghere        return result.release();
10486498aff2SJonas Devlieghere
10496498aff2SJonas Devlieghere    Py_RETURN_NONE;
10506498aff2SJonas Devlieghere}
10516498aff2SJonas Devlieghere
10526498aff2SJonas DevlieghereSWIGEXPORT void*
10536498aff2SJonas DevlieghereLLDBSWIGPython_CreateFrameRecognizer
10546498aff2SJonas Devlieghere(
10556498aff2SJonas Devlieghere    const char *python_class_name,
10566498aff2SJonas Devlieghere    const char *session_dictionary_name
10576498aff2SJonas Devlieghere)
10586498aff2SJonas Devlieghere{
10596498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
10606498aff2SJonas Devlieghere        Py_RETURN_NONE;
10616498aff2SJonas Devlieghere
10626498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
10636498aff2SJonas Devlieghere
10646498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
10656498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
10666498aff2SJonas Devlieghere
10676498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
10686498aff2SJonas Devlieghere        Py_RETURN_NONE;
10696498aff2SJonas Devlieghere
10706498aff2SJonas Devlieghere    auto result = pfunc();
10716498aff2SJonas Devlieghere
10726498aff2SJonas Devlieghere    if (result.IsAllocated())
10736498aff2SJonas Devlieghere        return result.release();
10746498aff2SJonas Devlieghere
10756498aff2SJonas Devlieghere    Py_RETURN_NONE;
10766498aff2SJonas Devlieghere}
10776498aff2SJonas Devlieghere
10786498aff2SJonas DevlieghereSWIGEXPORT PyObject*
10796498aff2SJonas DevlieghereLLDBSwigPython_GetRecognizedArguments
10806498aff2SJonas Devlieghere(
10816498aff2SJonas Devlieghere    PyObject *implementor,
10826498aff2SJonas Devlieghere    const lldb::StackFrameSP& frame_sp
10836498aff2SJonas Devlieghere)
10846498aff2SJonas Devlieghere{
10856498aff2SJonas Devlieghere    static char callee_name[] = "get_recognized_arguments";
10866498aff2SJonas Devlieghere
10876498aff2SJonas Devlieghere    lldb::SBFrame frame_sb(frame_sp);
10886498aff2SJonas Devlieghere    PyObject *arg = SBTypeToSWIGWrapper(frame_sb);
10896498aff2SJonas Devlieghere
10906498aff2SJonas Devlieghere    PythonString str(callee_name);
10916498aff2SJonas Devlieghere    PyObject* result = PyObject_CallMethodObjArgs(implementor, str.get(), arg,
10926498aff2SJonas Devlieghere                                                  NULL);
10936498aff2SJonas Devlieghere    return result;
10946498aff2SJonas Devlieghere}
10956498aff2SJonas Devlieghere
10966498aff2SJonas DevlieghereSWIGEXPORT void*
10976498aff2SJonas DevlieghereLLDBSWIGPython_GetDynamicSetting (void* module, const char* setting, const lldb::TargetSP& target_sp)
10986498aff2SJonas Devlieghere{
10996498aff2SJonas Devlieghere    if (!module || !setting)
11006498aff2SJonas Devlieghere        Py_RETURN_NONE;
11016498aff2SJonas Devlieghere
11026498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
11036498aff2SJonas Devlieghere    PythonObject py_module(PyRefType::Borrowed, (PyObject *)module);
11046498aff2SJonas Devlieghere    auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting");
11056498aff2SJonas Devlieghere
11066498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
11076498aff2SJonas Devlieghere        Py_RETURN_NONE;
11086498aff2SJonas Devlieghere
1109*7f09ab08SPavel Labath    auto result = pfunc(ToSWIGWrapper(target_sp), PythonString(setting));
11106498aff2SJonas Devlieghere
11116498aff2SJonas Devlieghere    return result.release();
11126498aff2SJonas Devlieghere}
11136498aff2SJonas Devlieghere
1114*7f09ab08SPavel LabathSWIGEXPORT bool LLDBSWIGPythonRunScriptKeywordProcess(
1115*7f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
1116*7f09ab08SPavel Labath    const lldb::ProcessSP &process, std::string &output) {
11176498aff2SJonas Devlieghere
11186498aff2SJonas Devlieghere    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
11196498aff2SJonas Devlieghere        return false;
11206498aff2SJonas Devlieghere
11216498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
11226498aff2SJonas Devlieghere
11236498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
11246498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
11256498aff2SJonas Devlieghere
11266498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
11276498aff2SJonas Devlieghere        return false;
11286498aff2SJonas Devlieghere
1129*7f09ab08SPavel Labath    auto result = pfunc(ToSWIGWrapper(process), dict);
11306498aff2SJonas Devlieghere
11316498aff2SJonas Devlieghere    output = result.Str().GetString().str();
11326498aff2SJonas Devlieghere
11336498aff2SJonas Devlieghere    return true;
11346498aff2SJonas Devlieghere}
11356498aff2SJonas Devlieghere
11366498aff2SJonas DevlieghereSWIGEXPORT bool
11376498aff2SJonas DevlieghereLLDBSWIGPythonRunScriptKeywordThread
11386498aff2SJonas Devlieghere(const char* python_function_name,
11396498aff2SJonas Devlieghereconst char* session_dictionary_name,
11406498aff2SJonas Devliegherelldb::ThreadSP& thread,
11416498aff2SJonas Devliegherestd::string& output)
11426498aff2SJonas Devlieghere
11436498aff2SJonas Devlieghere{
11446498aff2SJonas Devlieghere    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
11456498aff2SJonas Devlieghere        return false;
11466498aff2SJonas Devlieghere
11476498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
11486498aff2SJonas Devlieghere
11496498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
11506498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
11516498aff2SJonas Devlieghere
11526498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
11536498aff2SJonas Devlieghere        return false;
11546498aff2SJonas Devlieghere
11556498aff2SJonas Devlieghere    lldb::SBThread thread_sb(thread);
11566498aff2SJonas Devlieghere    PythonObject thread_arg(PyRefType::Owned, SBTypeToSWIGWrapper(thread_sb));
11576498aff2SJonas Devlieghere    auto result = pfunc(thread_arg, dict);
11586498aff2SJonas Devlieghere
11596498aff2SJonas Devlieghere    output = result.Str().GetString().str();
11606498aff2SJonas Devlieghere
11616498aff2SJonas Devlieghere    return true;
11626498aff2SJonas Devlieghere}
11636498aff2SJonas Devlieghere
1164*7f09ab08SPavel LabathSWIGEXPORT bool LLDBSWIGPythonRunScriptKeywordTarget(
1165*7f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
1166*7f09ab08SPavel Labath    const lldb::TargetSP &target, std::string &output) {
11676498aff2SJonas Devlieghere
11686498aff2SJonas Devlieghere    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
11696498aff2SJonas Devlieghere        return false;
11706498aff2SJonas Devlieghere
11716498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
11726498aff2SJonas Devlieghere
11736498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
11746498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name,dict);
11756498aff2SJonas Devlieghere
11766498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
11776498aff2SJonas Devlieghere        return false;
11786498aff2SJonas Devlieghere
1179*7f09ab08SPavel Labath    auto result = pfunc(ToSWIGWrapper(target), dict);
11806498aff2SJonas Devlieghere
11816498aff2SJonas Devlieghere    output = result.Str().GetString().str();
11826498aff2SJonas Devlieghere
11836498aff2SJonas Devlieghere    return true;
11846498aff2SJonas Devlieghere}
11856498aff2SJonas Devlieghere
11866498aff2SJonas DevlieghereSWIGEXPORT bool
11876498aff2SJonas DevlieghereLLDBSWIGPythonRunScriptKeywordFrame
11886498aff2SJonas Devlieghere(const char* python_function_name,
11896498aff2SJonas Devlieghereconst char* session_dictionary_name,
11906498aff2SJonas Devliegherelldb::StackFrameSP& frame,
11916498aff2SJonas Devliegherestd::string& output)
11926498aff2SJonas Devlieghere
11936498aff2SJonas Devlieghere{
11946498aff2SJonas Devlieghere    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
11956498aff2SJonas Devlieghere        return false;
11966498aff2SJonas Devlieghere
11976498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
11986498aff2SJonas Devlieghere
11996498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
12006498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name,dict);
12016498aff2SJonas Devlieghere
12026498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
12036498aff2SJonas Devlieghere        return false;
12046498aff2SJonas Devlieghere
12056498aff2SJonas Devlieghere    lldb::SBFrame frame_sb(frame);
12066498aff2SJonas Devlieghere    PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(frame_sb));
12076498aff2SJonas Devlieghere    auto result = pfunc(frame_arg, dict);
12086498aff2SJonas Devlieghere
12096498aff2SJonas Devlieghere    output = result.Str().GetString().str();
12106498aff2SJonas Devlieghere
12116498aff2SJonas Devlieghere    return true;
12126498aff2SJonas Devlieghere}
12136498aff2SJonas Devlieghere
1214*7f09ab08SPavel LabathSWIGEXPORT bool LLDBSWIGPythonRunScriptKeywordValue(
1215*7f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
1216*7f09ab08SPavel Labath    const lldb::ValueObjectSP &value, std::string &output) {
12176498aff2SJonas Devlieghere
12186498aff2SJonas Devlieghere    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
12196498aff2SJonas Devlieghere        return false;
12206498aff2SJonas Devlieghere
12216498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
12226498aff2SJonas Devlieghere
12236498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
12246498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
12256498aff2SJonas Devlieghere
12266498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
12276498aff2SJonas Devlieghere        return false;
12286498aff2SJonas Devlieghere
1229*7f09ab08SPavel Labath    auto result = pfunc(ToSWIGWrapper(value), dict);
12306498aff2SJonas Devlieghere
12316498aff2SJonas Devlieghere    output = result.Str().GetString().str();
12326498aff2SJonas Devlieghere
12336498aff2SJonas Devlieghere    return true;
12346498aff2SJonas Devlieghere}
12356498aff2SJonas Devlieghere
12366498aff2SJonas DevlieghereSWIGEXPORT bool
12376498aff2SJonas DevlieghereLLDBSwigPythonCallModuleInit
12386498aff2SJonas Devlieghere(
12396498aff2SJonas Devlieghere    const char *python_module_name,
12406498aff2SJonas Devlieghere    const char *session_dictionary_name,
12416498aff2SJonas Devlieghere    lldb::DebuggerSP& debugger
12426498aff2SJonas Devlieghere)
12436498aff2SJonas Devlieghere{
12446498aff2SJonas Devlieghere    std::string python_function_name_string = python_module_name;
12456498aff2SJonas Devlieghere    python_function_name_string += ".__lldb_init_module";
12466498aff2SJonas Devlieghere    const char* python_function_name = python_function_name_string.c_str();
12476498aff2SJonas Devlieghere
12486498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
12496498aff2SJonas Devlieghere
12506498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
12516498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
12526498aff2SJonas Devlieghere
12536498aff2SJonas Devlieghere    // This method is optional and need not exist.  So if we don't find it,
12546498aff2SJonas Devlieghere    // it's actually a success, not a failure.
12556498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
12566498aff2SJonas Devlieghere        return true;
12576498aff2SJonas Devlieghere
12586498aff2SJonas Devlieghere    lldb::SBDebugger debugger_sb(debugger);
12596498aff2SJonas Devlieghere    PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
12606498aff2SJonas Devlieghere    pfunc(debugger_arg, dict);
12616498aff2SJonas Devlieghere
12626498aff2SJonas Devlieghere    return true;
12636498aff2SJonas Devlieghere}
12646498aff2SJonas Devlieghere%}
12656498aff2SJonas Devlieghere
12666498aff2SJonas Devlieghere
12676498aff2SJonas Devlieghere%runtime %{
12686498aff2SJonas Devlieghere// Forward declaration to be inserted at the start of LLDBWrapPython.h
12696498aff2SJonas Devlieghere#include "lldb/API/SBDebugger.h"
12706498aff2SJonas Devlieghere#include "lldb/API/SBValue.h"
12716498aff2SJonas Devlieghere
12726498aff2SJonas DevlieghereSWIGEXPORT lldb::ValueObjectSP
12736498aff2SJonas DevlieghereLLDBSWIGPython_GetValueObjectSPFromSBValue (void* data)
12746498aff2SJonas Devlieghere{
12756498aff2SJonas Devlieghere    lldb::ValueObjectSP valobj_sp;
12766498aff2SJonas Devlieghere    if (data)
12776498aff2SJonas Devlieghere    {
12786498aff2SJonas Devlieghere        lldb::SBValue* sb_ptr = (lldb::SBValue *)data;
12796498aff2SJonas Devlieghere        valobj_sp = sb_ptr->GetSP();
12806498aff2SJonas Devlieghere    }
12816498aff2SJonas Devlieghere    return valobj_sp;
12826498aff2SJonas Devlieghere}
12836498aff2SJonas Devlieghere
12846498aff2SJonas Devlieghere#ifdef __cplusplus
12856498aff2SJonas Devlieghereextern "C" {
12866498aff2SJonas Devlieghere#endif
12876498aff2SJonas Devlieghere
12886498aff2SJonas Devliegherevoid LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton);
12896498aff2SJonas Devlieghere
12906498aff2SJonas Devlieghere#ifdef __cplusplus
12916498aff2SJonas Devlieghere}
12926498aff2SJonas Devlieghere#endif
12936498aff2SJonas Devlieghere%}
12946498aff2SJonas Devlieghere
12956498aff2SJonas Devlieghere%wrapper %{
12966498aff2SJonas Devlieghere
12976498aff2SJonas Devlieghere
12986498aff2SJonas Devlieghere// For the LogOutputCallback functions
12996498aff2SJonas Devliegherevoid LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) {
13006498aff2SJonas Devlieghere    if (baton != Py_None) {
13016498aff2SJonas Devlieghere      SWIG_PYTHON_THREAD_BEGIN_BLOCK;
13026498aff2SJonas Devlieghere      PyObject *result = PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str);
13036498aff2SJonas Devlieghere	  Py_XDECREF(result);
13046498aff2SJonas Devlieghere      SWIG_PYTHON_THREAD_END_BLOCK;
13056498aff2SJonas Devlieghere    }
13066498aff2SJonas Devlieghere}
13076498aff2SJonas Devlieghere%}
1308