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
259a14adeaSPavel Labathllvm::Expected<bool>
269a14adeaSPavel Labathlldb_private::LLDBSwigPythonBreakpointCallbackFunction
276498aff2SJonas Devlieghere(
286498aff2SJonas Devlieghere    const char *python_function_name,
296498aff2SJonas Devlieghere    const char *session_dictionary_name,
306498aff2SJonas Devlieghere    const lldb::StackFrameSP& frame_sp,
316498aff2SJonas Devlieghere    const lldb::BreakpointLocationSP& bp_loc_sp,
3282de8df2SPavel Labath    const lldb_private::StructuredDataImpl &args_impl
336498aff2SJonas Devlieghere)
346498aff2SJonas Devlieghere{
356498aff2SJonas Devlieghere    using namespace llvm;
366498aff2SJonas Devlieghere
376498aff2SJonas Devlieghere    lldb::SBFrame sb_frame (frame_sp);
386498aff2SJonas Devlieghere    lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
396498aff2SJonas Devlieghere
406498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
416498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
426498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
436498aff2SJonas Devlieghere
446498aff2SJonas Devlieghere    unsigned max_positional_args;
456498aff2SJonas Devlieghere    if (auto arg_info = pfunc.GetArgInfo())
466498aff2SJonas Devlieghere        max_positional_args = arg_info.get().max_positional_args;
476498aff2SJonas Devlieghere    else
486498aff2SJonas Devlieghere        return arg_info.takeError();
496498aff2SJonas Devlieghere
506498aff2SJonas Devlieghere    PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame));
516498aff2SJonas Devlieghere    PythonObject bp_loc_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_bp_loc));
526498aff2SJonas Devlieghere
536498aff2SJonas Devlieghere    auto result = [&] () -> Expected<PythonObject> {
546498aff2SJonas Devlieghere        // If the called function doesn't take extra_args, drop them here:
55*ebb6bb72SPavel Labath        if (max_positional_args < 4)
566498aff2SJonas Devlieghere            return pfunc.Call(frame_arg, bp_loc_arg, dict);
57*ebb6bb72SPavel Labath        return pfunc.Call(frame_arg, bp_loc_arg, ToSWIGWrapper(args_impl), dict);
586498aff2SJonas Devlieghere    } ();
596498aff2SJonas Devlieghere
606498aff2SJonas Devlieghere    if (!result)
616498aff2SJonas Devlieghere        return result.takeError();
626498aff2SJonas Devlieghere
636498aff2SJonas Devlieghere    // Only False counts as false!
646498aff2SJonas Devlieghere    return result.get().get() != Py_False;
656498aff2SJonas Devlieghere}
666498aff2SJonas Devlieghere
679a14adeaSPavel Labath// resolve a dotted Python name in the form
689a14adeaSPavel Labath// foo.bar.baz.Foobar to an actual Python object
699a14adeaSPavel Labath// if pmodule is NULL, the __main__ module will be used
709a14adeaSPavel Labath// as the starting point for the search
71daf36998SDave Lee
729a14adeaSPavel Labath
739a14adeaSPavel Labath// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...)
749a14adeaSPavel Labath// and is used when a script command is attached to a breakpoint for execution.
75daf36998SDave Lee
766498aff2SJonas Devlieghere// This function is called by lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...)
776498aff2SJonas Devlieghere// and is used when a script command is attached to a watchpoint for execution.
786498aff2SJonas Devlieghere
799a14adeaSPavel Labathbool
809a14adeaSPavel Labathlldb_private::LLDBSwigPythonWatchpointCallbackFunction
816498aff2SJonas Devlieghere(
826498aff2SJonas Devlieghere    const char *python_function_name,
836498aff2SJonas Devlieghere    const char *session_dictionary_name,
846498aff2SJonas Devlieghere    const lldb::StackFrameSP& frame_sp,
856498aff2SJonas Devlieghere    const lldb::WatchpointSP& wp_sp
866498aff2SJonas Devlieghere)
876498aff2SJonas Devlieghere{
886498aff2SJonas Devlieghere    lldb::SBFrame sb_frame (frame_sp);
896498aff2SJonas Devlieghere    lldb::SBWatchpoint sb_wp(wp_sp);
906498aff2SJonas Devlieghere
916498aff2SJonas Devlieghere    bool stop_at_watchpoint = true;
926498aff2SJonas Devlieghere
936498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
946498aff2SJonas Devlieghere
956498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
966498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
976498aff2SJonas Devlieghere
986498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
996498aff2SJonas Devlieghere        return stop_at_watchpoint;
1006498aff2SJonas Devlieghere
1016498aff2SJonas Devlieghere    PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame));
1026498aff2SJonas Devlieghere    PythonObject wp_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_wp));
1036498aff2SJonas Devlieghere    PythonObject result = pfunc(frame_arg, wp_arg, dict);
1046498aff2SJonas Devlieghere
1056498aff2SJonas Devlieghere    if (result.get() == Py_False)
1066498aff2SJonas Devlieghere        stop_at_watchpoint = false;
1076498aff2SJonas Devlieghere
1086498aff2SJonas Devlieghere    return stop_at_watchpoint;
1096498aff2SJonas Devlieghere}
1106498aff2SJonas Devlieghere
1119a14adeaSPavel Labathbool
1129a14adeaSPavel Labathlldb_private::LLDBSwigPythonCallTypeScript
1136498aff2SJonas Devlieghere(
1146498aff2SJonas Devlieghere    const char *python_function_name,
1156498aff2SJonas Devlieghere    const void *session_dictionary,
1166498aff2SJonas Devlieghere    const lldb::ValueObjectSP& valobj_sp,
1176498aff2SJonas Devlieghere    void** pyfunct_wrapper,
1186498aff2SJonas Devlieghere    const lldb::TypeSummaryOptionsSP& options_sp,
1196498aff2SJonas Devlieghere    std::string& retval
1206498aff2SJonas Devlieghere)
1216498aff2SJonas Devlieghere{
1226498aff2SJonas Devlieghere    lldb::SBTypeSummaryOptions sb_options(options_sp.get());
1236498aff2SJonas Devlieghere
1246498aff2SJonas Devlieghere    retval.clear();
1256498aff2SJonas Devlieghere
1266498aff2SJonas Devlieghere    if (!python_function_name || !session_dictionary)
1276498aff2SJonas Devlieghere        return false;
1286498aff2SJonas Devlieghere
1296498aff2SJonas Devlieghere    PyObject *pfunc_impl = nullptr;
1306498aff2SJonas Devlieghere
1316498aff2SJonas Devlieghere    if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper))
1326498aff2SJonas Devlieghere    {
1336498aff2SJonas Devlieghere        pfunc_impl = (PyObject*)(*pyfunct_wrapper);
1346498aff2SJonas Devlieghere        if (pfunc_impl->ob_refcnt == 1)
1356498aff2SJonas Devlieghere        {
1366498aff2SJonas Devlieghere            Py_XDECREF(pfunc_impl);
1376498aff2SJonas Devlieghere            pfunc_impl = NULL;
1386498aff2SJonas Devlieghere        }
1396498aff2SJonas Devlieghere    }
1406498aff2SJonas Devlieghere
1416498aff2SJonas Devlieghere    PyObject *py_dict = (PyObject*)session_dictionary;
1426498aff2SJonas Devlieghere    if (!PythonDictionary::Check(py_dict))
1436498aff2SJonas Devlieghere        return true;
1446498aff2SJonas Devlieghere
1456498aff2SJonas Devlieghere    PythonDictionary dict(PyRefType::Borrowed, py_dict);
1466498aff2SJonas Devlieghere
1476498aff2SJonas Devlieghere    PyErr_Cleaner pyerr_cleanup(true);  // show Python errors
1486498aff2SJonas Devlieghere
1496498aff2SJonas Devlieghere    PythonCallable pfunc(PyRefType::Borrowed, pfunc_impl);
1506498aff2SJonas Devlieghere
1516498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
1526498aff2SJonas Devlieghere    {
1536498aff2SJonas Devlieghere        pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
1546498aff2SJonas Devlieghere        if (!pfunc.IsAllocated())
1556498aff2SJonas Devlieghere            return false;
1566498aff2SJonas Devlieghere
1576498aff2SJonas Devlieghere        if (pyfunct_wrapper)
1586498aff2SJonas Devlieghere        {
1596498aff2SJonas Devlieghere            *pyfunct_wrapper = pfunc.get();
1606498aff2SJonas Devlieghere            Py_XINCREF(pfunc.get());
1616498aff2SJonas Devlieghere        }
1626498aff2SJonas Devlieghere    }
1636498aff2SJonas Devlieghere
1646498aff2SJonas Devlieghere    PythonObject result;
1656498aff2SJonas Devlieghere    auto argc = pfunc.GetArgInfo();
1666498aff2SJonas Devlieghere    if (!argc) {
1676498aff2SJonas Devlieghere        llvm::consumeError(argc.takeError());
1686498aff2SJonas Devlieghere        return false;
1696498aff2SJonas Devlieghere    }
1706498aff2SJonas Devlieghere
1717f09ab08SPavel Labath    PythonObject value_arg = ToSWIGWrapper(valobj_sp);
1726498aff2SJonas Devlieghere    PythonObject options_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_options));
1736498aff2SJonas Devlieghere
1746498aff2SJonas Devlieghere    if (argc.get().max_positional_args < 3)
1756498aff2SJonas Devlieghere        result = pfunc(value_arg,dict);
1766498aff2SJonas Devlieghere    else
1776498aff2SJonas Devlieghere        result = pfunc(value_arg,dict,options_arg);
1786498aff2SJonas Devlieghere
1796498aff2SJonas Devlieghere    retval = result.Str().GetString().str();
1806498aff2SJonas Devlieghere
1816498aff2SJonas Devlieghere    return true;
1826498aff2SJonas Devlieghere}
1836498aff2SJonas Devlieghere
1849a14adeaSPavel Labathvoid*
1859a14adeaSPavel Labathlldb_private::LLDBSwigPythonCreateSyntheticProvider
1866498aff2SJonas Devlieghere(
1876498aff2SJonas Devlieghere    const char *python_class_name,
1886498aff2SJonas Devlieghere    const char *session_dictionary_name,
1896498aff2SJonas Devlieghere    const lldb::ValueObjectSP& valobj_sp
1906498aff2SJonas Devlieghere)
1916498aff2SJonas Devlieghere{
1926498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
1936498aff2SJonas Devlieghere        Py_RETURN_NONE;
1946498aff2SJonas Devlieghere
1956498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
1966498aff2SJonas Devlieghere
1976498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
1986498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name,dict);
1996498aff2SJonas Devlieghere
2006498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
2016498aff2SJonas Devlieghere        Py_RETURN_NONE;
2026498aff2SJonas Devlieghere
2037f09ab08SPavel Labath    auto sb_value = std::make_unique<lldb::SBValue>(valobj_sp);
2046498aff2SJonas Devlieghere    sb_value->SetPreferSyntheticValue(false);
2056498aff2SJonas Devlieghere
2067f09ab08SPavel Labath    PythonObject val_arg = ToSWIGWrapper(std::move(sb_value));
2076498aff2SJonas Devlieghere    if (!val_arg.IsAllocated())
2086498aff2SJonas Devlieghere        Py_RETURN_NONE;
2096498aff2SJonas Devlieghere
2106498aff2SJonas Devlieghere    PythonObject result = pfunc(val_arg, dict);
2116498aff2SJonas Devlieghere
2126498aff2SJonas Devlieghere    if (result.IsAllocated())
2136498aff2SJonas Devlieghere        return result.release();
2146498aff2SJonas Devlieghere
2156498aff2SJonas Devlieghere    Py_RETURN_NONE;
2166498aff2SJonas Devlieghere}
2176498aff2SJonas Devlieghere
2189a14adeaSPavel Labathvoid*
2199a14adeaSPavel Labathlldb_private::LLDBSwigPythonCreateCommandObject
2206498aff2SJonas Devlieghere(
2216498aff2SJonas Devlieghere    const char *python_class_name,
2226498aff2SJonas Devlieghere    const char *session_dictionary_name,
2236498aff2SJonas Devlieghere    const lldb::DebuggerSP debugger_sp
2246498aff2SJonas Devlieghere)
2256498aff2SJonas Devlieghere{
2266498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
2276498aff2SJonas Devlieghere        Py_RETURN_NONE;
2286498aff2SJonas Devlieghere
2296498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
2306498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
2316498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
2326498aff2SJonas Devlieghere
2336498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
2346498aff2SJonas Devlieghere        return nullptr;
2356498aff2SJonas Devlieghere
2366498aff2SJonas Devlieghere    lldb::SBDebugger debugger_sb(debugger_sp);
2376498aff2SJonas Devlieghere    PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
2386498aff2SJonas Devlieghere    PythonObject result = pfunc(debugger_arg, dict);
2396498aff2SJonas Devlieghere
2406498aff2SJonas Devlieghere    if (result.IsAllocated())
2416498aff2SJonas Devlieghere        return result.release();
2426498aff2SJonas Devlieghere
2436498aff2SJonas Devlieghere    Py_RETURN_NONE;
2446498aff2SJonas Devlieghere}
2456498aff2SJonas Devlieghere
2469a14adeaSPavel Labathvoid*
2479a14adeaSPavel Labathlldb_private::LLDBSwigPythonCreateScriptedProcess
2481f6a57c1SMed Ismail Bennani(
2491f6a57c1SMed Ismail Bennani    const char *python_class_name,
2501f6a57c1SMed Ismail Bennani    const char *session_dictionary_name,
2511f6a57c1SMed Ismail Bennani    const lldb::TargetSP& target_sp,
25282de8df2SPavel Labath    const lldb_private::StructuredDataImpl &args_impl,
2531f6a57c1SMed Ismail Bennani    std::string &error_string
2541f6a57c1SMed Ismail Bennani)
2551f6a57c1SMed Ismail Bennani{
2561f6a57c1SMed Ismail Bennani    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
2571f6a57c1SMed Ismail Bennani        Py_RETURN_NONE;
2581f6a57c1SMed Ismail Bennani
2591f6a57c1SMed Ismail Bennani    PyErr_Cleaner py_err_cleaner(true);
2601f6a57c1SMed Ismail Bennani
2611f6a57c1SMed Ismail Bennani    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
2621f6a57c1SMed Ismail Bennani    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
2631f6a57c1SMed Ismail Bennani
2641f6a57c1SMed Ismail Bennani    if (!pfunc.IsAllocated()) {
2651f6a57c1SMed Ismail Bennani        error_string.append("could not find script class: ");
2661f6a57c1SMed Ismail Bennani        error_string.append(python_class_name);
2671f6a57c1SMed Ismail Bennani        return nullptr;
2681f6a57c1SMed Ismail Bennani    }
2691f6a57c1SMed Ismail Bennani
2707f09ab08SPavel Labath    PythonObject target_arg = ToSWIGWrapper(target_sp);
2711f6a57c1SMed Ismail Bennani
2721f6a57c1SMed Ismail Bennani    llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
2731f6a57c1SMed Ismail Bennani    if (!arg_info) {
2741f6a57c1SMed Ismail Bennani        llvm::handleAllErrors(
2751f6a57c1SMed Ismail Bennani            arg_info.takeError(),
2761f6a57c1SMed Ismail Bennani            [&](PythonException &E) {
2771f6a57c1SMed Ismail Bennani                error_string.append(E.ReadBacktrace());
2781f6a57c1SMed Ismail Bennani            },
2791f6a57c1SMed Ismail Bennani            [&](const llvm::ErrorInfoBase &E) {
2801f6a57c1SMed Ismail Bennani                error_string.append(E.message());
2811f6a57c1SMed Ismail Bennani            });
2821f6a57c1SMed Ismail Bennani        Py_RETURN_NONE;
2831f6a57c1SMed Ismail Bennani    }
2841f6a57c1SMed Ismail Bennani
2851f6a57c1SMed Ismail Bennani    PythonObject result = {};
2861f6a57c1SMed Ismail Bennani    if (arg_info.get().max_positional_args == 2) {
287*ebb6bb72SPavel Labath        result = pfunc(target_arg, ToSWIGWrapper(args_impl));
2881f6a57c1SMed Ismail Bennani    } else {
289738621d0SMed Ismail Bennani        error_string.assign("wrong number of arguments in __init__, should be 2 (not including self)");
2901f6a57c1SMed Ismail Bennani        Py_RETURN_NONE;
2911f6a57c1SMed Ismail Bennani    }
2921f6a57c1SMed Ismail Bennani
2931f6a57c1SMed Ismail Bennani    if (result.IsAllocated())
2941f6a57c1SMed Ismail Bennani        return result.release();
2951f6a57c1SMed Ismail Bennani    Py_RETURN_NONE;
2961f6a57c1SMed Ismail Bennani}
2971f6a57c1SMed Ismail Bennani
2989a14adeaSPavel Labathvoid*
2999a14adeaSPavel Labathlldb_private::LLDBSwigPythonCreateScriptedThread
30059d8dd79SMed Ismail Bennani(
30159d8dd79SMed Ismail Bennani    const char *python_class_name,
30259d8dd79SMed Ismail Bennani    const char *session_dictionary_name,
303738621d0SMed Ismail Bennani    const lldb::ProcessSP& process_sp,
30482de8df2SPavel Labath    const StructuredDataImpl &args_impl,
30559d8dd79SMed Ismail Bennani    std::string &error_string
30659d8dd79SMed Ismail Bennani)
30759d8dd79SMed Ismail Bennani{
30859d8dd79SMed Ismail Bennani    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
30959d8dd79SMed Ismail Bennani        Py_RETURN_NONE;
31059d8dd79SMed Ismail Bennani
31159d8dd79SMed Ismail Bennani    PyErr_Cleaner py_err_cleaner(true);
31259d8dd79SMed Ismail Bennani
31359d8dd79SMed Ismail Bennani    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
31459d8dd79SMed Ismail Bennani    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
31559d8dd79SMed Ismail Bennani
31659d8dd79SMed Ismail Bennani    if (!pfunc.IsAllocated()) {
31759d8dd79SMed Ismail Bennani        error_string.append("could not find script class: ");
31859d8dd79SMed Ismail Bennani        error_string.append(python_class_name);
31959d8dd79SMed Ismail Bennani        return nullptr;
32059d8dd79SMed Ismail Bennani    }
32159d8dd79SMed Ismail Bennani
32259d8dd79SMed Ismail Bennani    llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
32359d8dd79SMed Ismail Bennani    if (!arg_info) {
32459d8dd79SMed Ismail Bennani        llvm::handleAllErrors(
32559d8dd79SMed Ismail Bennani            arg_info.takeError(),
32659d8dd79SMed Ismail Bennani            [&](PythonException &E) {
32759d8dd79SMed Ismail Bennani                error_string.append(E.ReadBacktrace());
32859d8dd79SMed Ismail Bennani            },
32959d8dd79SMed Ismail Bennani            [&](const llvm::ErrorInfoBase &E) {
33059d8dd79SMed Ismail Bennani                error_string.append(E.message());
33159d8dd79SMed Ismail Bennani            });
33259d8dd79SMed Ismail Bennani        Py_RETURN_NONE;
33359d8dd79SMed Ismail Bennani    }
33459d8dd79SMed Ismail Bennani
33559d8dd79SMed Ismail Bennani    PythonObject result = {};
336738621d0SMed Ismail Bennani    if (arg_info.get().max_positional_args == 2) {
337*ebb6bb72SPavel Labath        result = pfunc(ToSWIGWrapper(process_sp), ToSWIGWrapper(args_impl));
33859d8dd79SMed Ismail Bennani    } else {
339738621d0SMed Ismail Bennani        error_string.assign("wrong number of arguments in __init__, should be 2 (not including self)");
34059d8dd79SMed Ismail Bennani        Py_RETURN_NONE;
34159d8dd79SMed Ismail Bennani    }
34259d8dd79SMed Ismail Bennani
34359d8dd79SMed Ismail Bennani    if (result.IsAllocated())
34459d8dd79SMed Ismail Bennani        return result.release();
34559d8dd79SMed Ismail Bennani    Py_RETURN_NONE;
34659d8dd79SMed Ismail Bennani}
34759d8dd79SMed Ismail Bennani
3489a14adeaSPavel Labathvoid*
3499a14adeaSPavel Labathlldb_private::LLDBSwigPythonCreateScriptedThreadPlan
3506498aff2SJonas Devlieghere(
3516498aff2SJonas Devlieghere    const char *python_class_name,
3526498aff2SJonas Devlieghere    const char *session_dictionary_name,
35382de8df2SPavel Labath    const lldb_private::StructuredDataImpl &args_impl,
3546498aff2SJonas Devlieghere    std::string &error_string,
3556498aff2SJonas Devlieghere    const lldb::ThreadPlanSP& thread_plan_sp
3566498aff2SJonas Devlieghere)
3576498aff2SJonas Devlieghere{
3586498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
3596498aff2SJonas Devlieghere        Py_RETURN_NONE;
3606498aff2SJonas Devlieghere
3616498aff2SJonas Devlieghere
3626498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
3636498aff2SJonas Devlieghere
3646498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
3656498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
3666498aff2SJonas Devlieghere
3676498aff2SJonas Devlieghere    if (!pfunc.IsAllocated()) {
3686498aff2SJonas Devlieghere        error_string.append("could not find script class: ");
3696498aff2SJonas Devlieghere        error_string.append(python_class_name);
3706498aff2SJonas Devlieghere        return nullptr;
3716498aff2SJonas Devlieghere    }
3726498aff2SJonas Devlieghere
3737f09ab08SPavel Labath    PythonObject tp_arg = ToSWIGWrapper(thread_plan_sp);
3746498aff2SJonas Devlieghere
3756498aff2SJonas Devlieghere    llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
3766498aff2SJonas Devlieghere    if (!arg_info) {
3776498aff2SJonas Devlieghere        llvm::handleAllErrors(
3786498aff2SJonas Devlieghere            arg_info.takeError(),
3796498aff2SJonas Devlieghere            [&](PythonException &E) {
3806498aff2SJonas Devlieghere                error_string.append(E.ReadBacktrace());
3816498aff2SJonas Devlieghere            },
3826498aff2SJonas Devlieghere            [&](const llvm::ErrorInfoBase &E) {
3836498aff2SJonas Devlieghere                error_string.append(E.message());
3846498aff2SJonas Devlieghere            });
3856498aff2SJonas Devlieghere        Py_RETURN_NONE;
3866498aff2SJonas Devlieghere    }
3876498aff2SJonas Devlieghere
3886498aff2SJonas Devlieghere    PythonObject result = {};
389*ebb6bb72SPavel Labath    auto args_sb = std::make_unique<lldb::SBStructuredData>(args_impl);
3906498aff2SJonas Devlieghere    if (arg_info.get().max_positional_args == 2) {
39182de8df2SPavel Labath        if (args_sb->IsValid()) {
3926498aff2SJonas Devlieghere           error_string.assign("args passed, but __init__ does not take an args dictionary");
3936498aff2SJonas Devlieghere           Py_RETURN_NONE;
3946498aff2SJonas Devlieghere        }
3956498aff2SJonas Devlieghere        result = pfunc(tp_arg, dict);
3966498aff2SJonas Devlieghere    } else if (arg_info.get().max_positional_args >= 3) {
397*ebb6bb72SPavel Labath        result = pfunc(tp_arg, ToSWIGWrapper(std::move(args_sb)), dict);
3986498aff2SJonas Devlieghere    } else {
3996498aff2SJonas Devlieghere        error_string.assign("wrong number of arguments in __init__, should be 2 or 3 (not including self)");
4006498aff2SJonas Devlieghere        Py_RETURN_NONE;
4016498aff2SJonas Devlieghere    }
4026498aff2SJonas Devlieghere
4036498aff2SJonas Devlieghere    // FIXME: At this point we should check that the class we found supports all the methods
4046498aff2SJonas Devlieghere    // that we need.
4056498aff2SJonas Devlieghere
4066498aff2SJonas Devlieghere    if (result.IsAllocated())
4076498aff2SJonas Devlieghere        return result.release();
4086498aff2SJonas Devlieghere    Py_RETURN_NONE;
4096498aff2SJonas Devlieghere}
4106498aff2SJonas Devlieghere
4119a14adeaSPavel Labathbool
4129a14adeaSPavel Labathlldb_private::LLDBSWIGPythonCallThreadPlan
4136498aff2SJonas Devlieghere(
4146498aff2SJonas Devlieghere    void *implementor,
4156498aff2SJonas Devlieghere    const char *method_name,
4166498aff2SJonas Devlieghere    lldb_private::Event *event,
4176498aff2SJonas Devlieghere    bool &got_error
4186498aff2SJonas Devlieghere)
4196498aff2SJonas Devlieghere{
4206498aff2SJonas Devlieghere    got_error = false;
4216498aff2SJonas Devlieghere
4226498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(false);
4236498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor));
4246498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>(method_name);
4256498aff2SJonas Devlieghere
4266498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
4276498aff2SJonas Devlieghere        return false;
4286498aff2SJonas Devlieghere
4296498aff2SJonas Devlieghere    PythonObject result;
4306498aff2SJonas Devlieghere    if (event != nullptr)
4316498aff2SJonas Devlieghere    {
4326498aff2SJonas Devlieghere        lldb::SBEvent sb_event(event);
4336498aff2SJonas Devlieghere        PythonObject event_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_event));
4346498aff2SJonas Devlieghere        result = pfunc(event_arg);
4356498aff2SJonas Devlieghere    }
4366498aff2SJonas Devlieghere    else
4376498aff2SJonas Devlieghere        result = pfunc();
4386498aff2SJonas Devlieghere
4396498aff2SJonas Devlieghere    if (PyErr_Occurred())
4406498aff2SJonas Devlieghere    {
4416498aff2SJonas Devlieghere        got_error = true;
4426498aff2SJonas Devlieghere        printf ("Return value was neither false nor true for call to %s.\n", method_name);
4436498aff2SJonas Devlieghere        PyErr_Print();
4446498aff2SJonas Devlieghere        return false;
4456498aff2SJonas Devlieghere    }
4466498aff2SJonas Devlieghere
4476498aff2SJonas Devlieghere    if (result.get() == Py_True)
4486498aff2SJonas Devlieghere        return true;
4496498aff2SJonas Devlieghere    else if (result.get() == Py_False)
4506498aff2SJonas Devlieghere        return false;
4516498aff2SJonas Devlieghere
4526498aff2SJonas Devlieghere    // Somebody returned the wrong thing...
4536498aff2SJonas Devlieghere    got_error = true;
4546498aff2SJonas Devlieghere    printf ("Wrong return value type for call to %s.\n", method_name);
4556498aff2SJonas Devlieghere    return false;
4566498aff2SJonas Devlieghere}
4576498aff2SJonas Devlieghere
4589a14adeaSPavel Labathvoid *lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
4597f09ab08SPavel Labath    const char *python_class_name, const char *session_dictionary_name,
46082de8df2SPavel Labath    const StructuredDataImpl &args_impl,
4617f09ab08SPavel Labath    const lldb::BreakpointSP &breakpoint_sp) {
4627f09ab08SPavel Labath
4636498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
4646498aff2SJonas Devlieghere        Py_RETURN_NONE;
4656498aff2SJonas Devlieghere
4666498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
4676498aff2SJonas Devlieghere
4686498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
4696498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
4706498aff2SJonas Devlieghere
4716498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
4726498aff2SJonas Devlieghere        return nullptr;
4736498aff2SJonas Devlieghere
474*ebb6bb72SPavel Labath    PythonObject result =
475*ebb6bb72SPavel Labath        pfunc(ToSWIGWrapper(breakpoint_sp), ToSWIGWrapper(args_impl), dict);
4766498aff2SJonas Devlieghere    // FIXME: At this point we should check that the class we found supports all the methods
4776498aff2SJonas Devlieghere    // that we need.
4786498aff2SJonas Devlieghere
4796498aff2SJonas Devlieghere    if (result.IsAllocated())
4806498aff2SJonas Devlieghere    {
4816498aff2SJonas Devlieghere        // Check that __callback__ is defined:
4826498aff2SJonas Devlieghere        auto callback_func = result.ResolveName<PythonCallable>("__callback__");
4836498aff2SJonas Devlieghere        if (callback_func.IsAllocated())
4846498aff2SJonas Devlieghere            return result.release();
4856498aff2SJonas Devlieghere        else
4866498aff2SJonas Devlieghere            result.release();
4876498aff2SJonas Devlieghere    }
4886498aff2SJonas Devlieghere    Py_RETURN_NONE;
4896498aff2SJonas Devlieghere}
4906498aff2SJonas Devlieghere
4919a14adeaSPavel Labathunsigned int
4929a14adeaSPavel Labathlldb_private::LLDBSwigPythonCallBreakpointResolver
4936498aff2SJonas Devlieghere(
4946498aff2SJonas Devlieghere    void *implementor,
4956498aff2SJonas Devlieghere    const char *method_name,
4966498aff2SJonas Devlieghere    lldb_private::SymbolContext *sym_ctx
4976498aff2SJonas Devlieghere)
4986498aff2SJonas Devlieghere{
4996498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(false);
5006498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor));
5016498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>(method_name);
5026498aff2SJonas Devlieghere
5036498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
5046498aff2SJonas Devlieghere        return 0;
5056498aff2SJonas Devlieghere
5066498aff2SJonas Devlieghere    PythonObject result;
5076498aff2SJonas Devlieghere    if (sym_ctx != nullptr) {
5086498aff2SJonas Devlieghere      lldb::SBSymbolContext sb_sym_ctx(sym_ctx);
5096498aff2SJonas Devlieghere      PythonObject sym_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_sym_ctx));
5106498aff2SJonas Devlieghere      result = pfunc(sym_ctx_arg);
5116498aff2SJonas Devlieghere    } else
5126498aff2SJonas Devlieghere      result = pfunc();
5136498aff2SJonas Devlieghere
5146498aff2SJonas Devlieghere    if (PyErr_Occurred())
5156498aff2SJonas Devlieghere    {
5166498aff2SJonas Devlieghere        PyErr_Print();
51752712d3fSLawrence D'Anna        PyErr_Clear();
5186498aff2SJonas Devlieghere        return 0;
5196498aff2SJonas Devlieghere    }
5206498aff2SJonas Devlieghere
5216498aff2SJonas Devlieghere    // The callback will return a bool, but we're need to also return ints
5226498aff2SJonas Devlieghere    // so we're squirrelling the bool through as an int...  And if you return
5236498aff2SJonas Devlieghere    // nothing, we'll continue.
5246498aff2SJonas Devlieghere    if (strcmp(method_name, "__callback__") == 0) {
5256498aff2SJonas Devlieghere        if (result.get() == Py_False)
5266498aff2SJonas Devlieghere          return 0;
5276498aff2SJonas Devlieghere        else
5286498aff2SJonas Devlieghere          return 1;
5296498aff2SJonas Devlieghere    }
5306498aff2SJonas Devlieghere
53152712d3fSLawrence D'Anna    long long ret_val = unwrapOrSetPythonException(As<long long>(result));
532478619cfSMuhammad Omair Javaid
53352712d3fSLawrence D'Anna    if (PyErr_Occurred()) {
53452712d3fSLawrence D'Anna        PyErr_Print();
53552712d3fSLawrence D'Anna        PyErr_Clear();
53652712d3fSLawrence D'Anna        return 0;
53752712d3fSLawrence D'Anna    }
5386498aff2SJonas Devlieghere
5396498aff2SJonas Devlieghere    return ret_val;
5406498aff2SJonas Devlieghere}
5416498aff2SJonas Devlieghere
5429a14adeaSPavel Labathvoid *
5439a14adeaSPavel Labathlldb_private::LLDBSwigPythonCreateScriptedStopHook
5441b1d9815SJim Ingham(
5451b1d9815SJim Ingham    lldb::TargetSP target_sp,
5461b1d9815SJim Ingham    const char *python_class_name,
5471b1d9815SJim Ingham    const char *session_dictionary_name,
54882de8df2SPavel Labath    const StructuredDataImpl &args_impl,
5491b1d9815SJim Ingham    Status &error
5501b1d9815SJim Ingham)
5511b1d9815SJim Ingham{
5521b1d9815SJim Ingham    if (python_class_name == NULL || python_class_name[0] == '\0') {
5531b1d9815SJim Ingham        error.SetErrorString("Empty class name.");
5541b1d9815SJim Ingham        Py_RETURN_NONE;
5551b1d9815SJim Ingham    }
5561b1d9815SJim Ingham    if (!session_dictionary_name) {
5571b1d9815SJim Ingham      error.SetErrorString("No session dictionary");
5581b1d9815SJim Ingham      Py_RETURN_NONE;
5591b1d9815SJim Ingham    }
5601b1d9815SJim Ingham
5611b1d9815SJim Ingham    PyErr_Cleaner py_err_cleaner(true);
5621b1d9815SJim Ingham
5631b1d9815SJim Ingham    auto dict =
5641b1d9815SJim Ingham        PythonModule::MainModule().ResolveName<PythonDictionary>(
5651b1d9815SJim Ingham            session_dictionary_name);
5661b1d9815SJim Ingham    auto pfunc =
5671b1d9815SJim Ingham        PythonObject::ResolveNameWithDictionary<PythonCallable>(
5681b1d9815SJim Ingham            python_class_name, dict);
5691b1d9815SJim Ingham
5701b1d9815SJim Ingham    if (!pfunc.IsAllocated()) {
5711b1d9815SJim Ingham        error.SetErrorStringWithFormat("Could not find class: %s.",
5721b1d9815SJim Ingham                                       python_class_name);
5731b1d9815SJim Ingham        return nullptr;
5741b1d9815SJim Ingham    }
5751b1d9815SJim Ingham
576*ebb6bb72SPavel Labath    PythonObject result =
577*ebb6bb72SPavel Labath        pfunc(ToSWIGWrapper(target_sp), ToSWIGWrapper(args_impl), dict);
5781b1d9815SJim Ingham
5791b1d9815SJim Ingham    if (result.IsAllocated())
5801b1d9815SJim Ingham    {
5811b1d9815SJim Ingham        // Check that the handle_stop callback is defined:
5821b1d9815SJim Ingham        auto callback_func = result.ResolveName<PythonCallable>("handle_stop");
5831b1d9815SJim Ingham        if (callback_func.IsAllocated()) {
5841b1d9815SJim Ingham          if (auto args_info = callback_func.GetArgInfo()) {
5851b1d9815SJim Ingham            size_t num_args = (*args_info).max_positional_args;
5861b1d9815SJim Ingham            if (num_args != 2) {
5871b1d9815SJim Ingham              error.SetErrorStringWithFormat("Wrong number of args for "
5882f95c50aSRichard Smith              "handle_stop callback, should be 2 (excluding self), got: %zu",
5891b1d9815SJim Ingham              num_args);
5901b1d9815SJim Ingham              Py_RETURN_NONE;
5911b1d9815SJim Ingham            } else
5921b1d9815SJim Ingham              return result.release();
5931b1d9815SJim Ingham          } else {
5941b1d9815SJim Ingham            error.SetErrorString("Couldn't get num arguments for handle_stop "
5951b1d9815SJim Ingham                                 "callback.");
5961b1d9815SJim Ingham            Py_RETURN_NONE;
5971b1d9815SJim Ingham          }
5981b1d9815SJim Ingham          return result.release();
5991b1d9815SJim Ingham        }
6001b1d9815SJim Ingham        else {
6011b1d9815SJim Ingham          error.SetErrorStringWithFormat("Class \"%s\" is missing the required "
6021b1d9815SJim Ingham                                         "handle_stop callback.",
6031b1d9815SJim Ingham                                         python_class_name);
6041b1d9815SJim Ingham          result.release();
6051b1d9815SJim Ingham        }
6061b1d9815SJim Ingham    }
6071b1d9815SJim Ingham    Py_RETURN_NONE;
6081b1d9815SJim Ingham}
6091b1d9815SJim Ingham
6109a14adeaSPavel Labathbool
6119a14adeaSPavel Labathlldb_private::LLDBSwigPythonStopHookCallHandleStop
6121b1d9815SJim Ingham(
6131b1d9815SJim Ingham    void *implementor,
6141b1d9815SJim Ingham    lldb::ExecutionContextRefSP exc_ctx_sp,
6151b1d9815SJim Ingham    lldb::StreamSP stream
6161b1d9815SJim Ingham)
6171b1d9815SJim Ingham{
6181b1d9815SJim Ingham    // handle_stop will return a bool with the meaning "should_stop"...
6191b1d9815SJim Ingham    // If you return nothing we'll assume we are going to stop.
6201b1d9815SJim Ingham    // Also any errors should return true, since we should stop on error.
6211b1d9815SJim Ingham
6221b1d9815SJim Ingham    PyErr_Cleaner py_err_cleaner(false);
6231b1d9815SJim Ingham    PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor));
6241b1d9815SJim Ingham    auto pfunc = self.ResolveName<PythonCallable>("handle_stop");
6251b1d9815SJim Ingham
6261b1d9815SJim Ingham    if (!pfunc.IsAllocated())
6271b1d9815SJim Ingham        return true;
6281b1d9815SJim Ingham
6291b1d9815SJim Ingham    PythonObject result;
6301b1d9815SJim Ingham    lldb::SBExecutionContext sb_exc_ctx(exc_ctx_sp);
6311b1d9815SJim Ingham    PythonObject exc_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_exc_ctx));
6321b1d9815SJim Ingham    lldb::SBStream sb_stream;
6331b1d9815SJim Ingham    PythonObject sb_stream_arg(PyRefType::Owned,
6341b1d9815SJim Ingham                               SBTypeToSWIGWrapper(sb_stream));
6351b1d9815SJim Ingham    result = pfunc(exc_ctx_arg, sb_stream_arg);
6361b1d9815SJim Ingham
6371b1d9815SJim Ingham    if (PyErr_Occurred())
6381b1d9815SJim Ingham    {
6391b1d9815SJim Ingham        stream->PutCString("Python error occurred handling stop-hook.");
6401b1d9815SJim Ingham        PyErr_Print();
6411b1d9815SJim Ingham        PyErr_Clear();
6421b1d9815SJim Ingham        return true;
6431b1d9815SJim Ingham    }
6441b1d9815SJim Ingham
6451b1d9815SJim Ingham    // Now add the result to the output stream.  SBStream only
6461b1d9815SJim Ingham    // makes an internally help StreamString which I can't interpose, so I
6471b1d9815SJim Ingham    // have to copy it over here.
6481b1d9815SJim Ingham    stream->PutCString(sb_stream.GetData());
6491b1d9815SJim Ingham
6501b1d9815SJim Ingham    if (result.get() == Py_False)
6511b1d9815SJim Ingham      return false;
6521b1d9815SJim Ingham    else
6531b1d9815SJim Ingham      return true;
6541b1d9815SJim Ingham}
6551b1d9815SJim Ingham
6566498aff2SJonas Devlieghere// wrapper that calls an optional instance member of an object taking no arguments
6576498aff2SJonas Devliegherestatic PyObject*
6586498aff2SJonas DevlieghereLLDBSwigPython_CallOptionalMember
6596498aff2SJonas Devlieghere(
6606498aff2SJonas Devlieghere    PyObject* implementor,
6616498aff2SJonas Devlieghere    char* callee_name,
6626498aff2SJonas Devlieghere    PyObject* ret_if_not_found = Py_None,
6636498aff2SJonas Devlieghere    bool* was_found = NULL
6646498aff2SJonas Devlieghere)
6656498aff2SJonas Devlieghere{
6666498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(false);
6676498aff2SJonas Devlieghere
6686498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor));
6696498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>(callee_name);
6706498aff2SJonas Devlieghere
6716498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
6726498aff2SJonas Devlieghere    {
6736498aff2SJonas Devlieghere        if (was_found)
6746498aff2SJonas Devlieghere            *was_found = false;
6756498aff2SJonas Devlieghere        Py_XINCREF(ret_if_not_found);
6766498aff2SJonas Devlieghere        return ret_if_not_found;
6776498aff2SJonas Devlieghere    }
6786498aff2SJonas Devlieghere
6796498aff2SJonas Devlieghere    if (was_found)
6806498aff2SJonas Devlieghere        *was_found = true;
6816498aff2SJonas Devlieghere
6826498aff2SJonas Devlieghere    PythonObject result = pfunc();
6836498aff2SJonas Devlieghere    return result.release();
6846498aff2SJonas Devlieghere}
6856498aff2SJonas Devlieghere
6869a14adeaSPavel Labathsize_t
6879a14adeaSPavel Labathlldb_private::LLDBSwigPython_CalculateNumChildren
6886498aff2SJonas Devlieghere(
6896498aff2SJonas Devlieghere    PyObject *implementor,
6906498aff2SJonas Devlieghere    uint32_t max
6916498aff2SJonas Devlieghere)
6926498aff2SJonas Devlieghere{
6936498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, implementor);
6946498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>("num_children");
6956498aff2SJonas Devlieghere
6966498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
6976498aff2SJonas Devlieghere        return 0;
6986498aff2SJonas Devlieghere
6996498aff2SJonas Devlieghere    auto arg_info = pfunc.GetArgInfo();
7006498aff2SJonas Devlieghere    if (!arg_info) {
7016498aff2SJonas Devlieghere        llvm::consumeError(arg_info.takeError());
7026498aff2SJonas Devlieghere        return 0;
7036498aff2SJonas Devlieghere    }
7046498aff2SJonas Devlieghere
70552712d3fSLawrence D'Anna    size_t ret_val;
706478619cfSMuhammad Omair Javaid    if (arg_info.get().max_positional_args < 1)
70752712d3fSLawrence D'Anna        ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call()));
708478619cfSMuhammad Omair Javaid    else
70952712d3fSLawrence D'Anna        ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call(PythonInteger(max))));
710478619cfSMuhammad Omair Javaid
71152712d3fSLawrence D'Anna    if (PyErr_Occurred())
7126498aff2SJonas Devlieghere    {
7136498aff2SJonas Devlieghere        PyErr_Print();
7146498aff2SJonas Devlieghere        PyErr_Clear();
71552712d3fSLawrence D'Anna        return 0;
7166498aff2SJonas Devlieghere    }
7176498aff2SJonas Devlieghere
7186498aff2SJonas Devlieghere    if (arg_info.get().max_positional_args < 1)
7196498aff2SJonas Devlieghere        ret_val = std::min(ret_val, static_cast<size_t>(max));
7206498aff2SJonas Devlieghere
7216498aff2SJonas Devlieghere    return ret_val;
7226498aff2SJonas Devlieghere}
7236498aff2SJonas Devlieghere
7249a14adeaSPavel LabathPyObject*
7259a14adeaSPavel Labathlldb_private::LLDBSwigPython_GetChildAtIndex
7266498aff2SJonas Devlieghere(
7276498aff2SJonas Devlieghere    PyObject *implementor,
7286498aff2SJonas Devlieghere    uint32_t idx
7296498aff2SJonas Devlieghere)
7306498aff2SJonas Devlieghere{
7316498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
7326498aff2SJonas Devlieghere
7336498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, implementor);
7346498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index");
7356498aff2SJonas Devlieghere
7366498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
7376498aff2SJonas Devlieghere        return nullptr;
7386498aff2SJonas Devlieghere
7396498aff2SJonas Devlieghere    PythonObject result = pfunc(PythonInteger(idx));
7406498aff2SJonas Devlieghere
7416498aff2SJonas Devlieghere    if (!result.IsAllocated())
7426498aff2SJonas Devlieghere        return nullptr;
7436498aff2SJonas Devlieghere
7446498aff2SJonas Devlieghere    lldb::SBValue* sbvalue_ptr = nullptr;
7456498aff2SJonas Devlieghere    if (SWIG_ConvertPtr(result.get(), (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
7466498aff2SJonas Devlieghere        return nullptr;
7476498aff2SJonas Devlieghere
7486498aff2SJonas Devlieghere    if (sbvalue_ptr == nullptr)
7496498aff2SJonas Devlieghere        return nullptr;
7506498aff2SJonas Devlieghere
7516498aff2SJonas Devlieghere    return result.release();
7526498aff2SJonas Devlieghere}
7536498aff2SJonas Devlieghere
7549a14adeaSPavel Labathint
7559a14adeaSPavel Labathlldb_private::LLDBSwigPython_GetIndexOfChildWithName
7566498aff2SJonas Devlieghere(
7576498aff2SJonas Devlieghere    PyObject *implementor,
7586498aff2SJonas Devlieghere    const char* child_name
7596498aff2SJonas Devlieghere)
7606498aff2SJonas Devlieghere{
7616498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
7626498aff2SJonas Devlieghere
7636498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, implementor);
7646498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>("get_child_index");
7656498aff2SJonas Devlieghere
7666498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
7676498aff2SJonas Devlieghere        return UINT32_MAX;
7686498aff2SJonas Devlieghere
76952712d3fSLawrence D'Anna    llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name));
7706498aff2SJonas Devlieghere
77152712d3fSLawrence D'Anna    long long retval = unwrapOrSetPythonException(As<long long>(std::move(result)));
77252712d3fSLawrence D'Anna
77352712d3fSLawrence D'Anna    if (PyErr_Occurred()) {
77452712d3fSLawrence D'Anna        PyErr_Clear(); // FIXME print this? do something else
7756498aff2SJonas Devlieghere        return UINT32_MAX;
77652712d3fSLawrence D'Anna    }
7776498aff2SJonas Devlieghere
7786498aff2SJonas Devlieghere    if (retval >= 0)
7796498aff2SJonas Devlieghere        return (uint32_t)retval;
7806498aff2SJonas Devlieghere
7816498aff2SJonas Devlieghere    return UINT32_MAX;
7826498aff2SJonas Devlieghere}
7836498aff2SJonas Devlieghere
7849a14adeaSPavel Labathbool
7859a14adeaSPavel Labathlldb_private::LLDBSwigPython_UpdateSynthProviderInstance
7866498aff2SJonas Devlieghere(
7876498aff2SJonas Devlieghere    PyObject *implementor
7886498aff2SJonas Devlieghere)
7896498aff2SJonas Devlieghere{
7906498aff2SJonas Devlieghere    bool ret_val = false;
7916498aff2SJonas Devlieghere
7926498aff2SJonas Devlieghere    static char callee_name[] = "update";
7936498aff2SJonas Devlieghere
7946498aff2SJonas Devlieghere    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name);
7956498aff2SJonas Devlieghere
7966498aff2SJonas Devlieghere    if (py_return == Py_True)
7976498aff2SJonas Devlieghere        ret_val = true;
7986498aff2SJonas Devlieghere
7996498aff2SJonas Devlieghere    Py_XDECREF(py_return);
8006498aff2SJonas Devlieghere
8016498aff2SJonas Devlieghere    return ret_val;
8026498aff2SJonas Devlieghere}
8036498aff2SJonas Devlieghere
8049a14adeaSPavel Labathbool
8059a14adeaSPavel Labathlldb_private::LLDBSwigPython_MightHaveChildrenSynthProviderInstance
8066498aff2SJonas Devlieghere(
8076498aff2SJonas Devlieghere    PyObject *implementor
8086498aff2SJonas Devlieghere)
8096498aff2SJonas Devlieghere{
8106498aff2SJonas Devlieghere    bool ret_val = false;
8116498aff2SJonas Devlieghere
8126498aff2SJonas Devlieghere    static char callee_name[] = "has_children";
8136498aff2SJonas Devlieghere
8146498aff2SJonas Devlieghere    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True);
8156498aff2SJonas Devlieghere
8166498aff2SJonas Devlieghere    if (py_return == Py_True)
8176498aff2SJonas Devlieghere        ret_val = true;
8186498aff2SJonas Devlieghere
8196498aff2SJonas Devlieghere    Py_XDECREF(py_return);
8206498aff2SJonas Devlieghere
8216498aff2SJonas Devlieghere    return ret_val;
8226498aff2SJonas Devlieghere}
8236498aff2SJonas Devlieghere
8249a14adeaSPavel LabathPyObject*
8259a14adeaSPavel Labathlldb_private::LLDBSwigPython_GetValueSynthProviderInstance
8266498aff2SJonas Devlieghere(
8276498aff2SJonas Devlieghere    PyObject *implementor
8286498aff2SJonas Devlieghere)
8296498aff2SJonas Devlieghere{
8306498aff2SJonas Devlieghere    PyObject* ret_val = nullptr;
8316498aff2SJonas Devlieghere
8326498aff2SJonas Devlieghere    static char callee_name[] = "get_value";
8336498aff2SJonas Devlieghere
8346498aff2SJonas Devlieghere    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_None);
8356498aff2SJonas Devlieghere
8366498aff2SJonas Devlieghere    if (py_return == Py_None || py_return == nullptr)
8376498aff2SJonas Devlieghere        ret_val = nullptr;
8386498aff2SJonas Devlieghere
8396498aff2SJonas Devlieghere    lldb::SBValue* sbvalue_ptr = NULL;
8406498aff2SJonas Devlieghere
8416498aff2SJonas Devlieghere    if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
8426498aff2SJonas Devlieghere        ret_val = nullptr;
8436498aff2SJonas Devlieghere    else if (sbvalue_ptr == NULL)
8446498aff2SJonas Devlieghere        ret_val = nullptr;
8456498aff2SJonas Devlieghere    else
8466498aff2SJonas Devlieghere        ret_val = py_return;
8476498aff2SJonas Devlieghere
8486498aff2SJonas Devlieghere    Py_XDECREF(py_return);
8496498aff2SJonas Devlieghere    return ret_val;
8506498aff2SJonas Devlieghere}
8516498aff2SJonas Devlieghere
8529a14adeaSPavel Labathvoid*
8539a14adeaSPavel Labathlldb_private::LLDBSWIGPython_CastPyObjectToSBData
8541f6a57c1SMed Ismail Bennani(
8551f6a57c1SMed Ismail Bennani    PyObject* data
8561f6a57c1SMed Ismail Bennani)
8571f6a57c1SMed Ismail Bennani{
8581f6a57c1SMed Ismail Bennani    lldb::SBData* sb_ptr = nullptr;
8591f6a57c1SMed Ismail Bennani
8601f6a57c1SMed Ismail Bennani    int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0);
8611f6a57c1SMed Ismail Bennani
8621f6a57c1SMed Ismail Bennani    if (valid_cast == -1)
8631f6a57c1SMed Ismail Bennani        return NULL;
8641f6a57c1SMed Ismail Bennani
8651f6a57c1SMed Ismail Bennani    return sb_ptr;
8661f6a57c1SMed Ismail Bennani}
8671f6a57c1SMed Ismail Bennani
8681f6a57c1SMed Ismail Bennani
8699a14adeaSPavel Labathvoid*
8709a14adeaSPavel Labathlldb_private::LLDBSWIGPython_CastPyObjectToSBError
8711f6a57c1SMed Ismail Bennani(
8721f6a57c1SMed Ismail Bennani    PyObject* data
8731f6a57c1SMed Ismail Bennani)
8741f6a57c1SMed Ismail Bennani{
8751f6a57c1SMed Ismail Bennani    lldb::SBError* sb_ptr = nullptr;
8761f6a57c1SMed Ismail Bennani
8771f6a57c1SMed Ismail Bennani    int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0);
8781f6a57c1SMed Ismail Bennani
8791f6a57c1SMed Ismail Bennani    if (valid_cast == -1)
8801f6a57c1SMed Ismail Bennani        return NULL;
8811f6a57c1SMed Ismail Bennani
8821f6a57c1SMed Ismail Bennani    return sb_ptr;
8831f6a57c1SMed Ismail Bennani}
8841f6a57c1SMed Ismail Bennani
8851f6a57c1SMed Ismail Bennani
8869a14adeaSPavel Labathvoid*
8879a14adeaSPavel Labathlldb_private::LLDBSWIGPython_CastPyObjectToSBValue
8886498aff2SJonas Devlieghere(
8896498aff2SJonas Devlieghere    PyObject* data
8906498aff2SJonas Devlieghere)
8916498aff2SJonas Devlieghere{
8926498aff2SJonas Devlieghere    lldb::SBValue* sb_ptr = NULL;
8936498aff2SJonas Devlieghere
8946498aff2SJonas Devlieghere    int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
8956498aff2SJonas Devlieghere
8966498aff2SJonas Devlieghere    if (valid_cast == -1)
8976498aff2SJonas Devlieghere        return NULL;
8986498aff2SJonas Devlieghere
8996498aff2SJonas Devlieghere    return sb_ptr;
9006498aff2SJonas Devlieghere}
9016498aff2SJonas Devlieghere
9029a14adeaSPavel Labathvoid*
9039a14adeaSPavel Labathlldb_private::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo
904a758c9f7SMed Ismail Bennani(
905a758c9f7SMed Ismail Bennani    PyObject* data
906a758c9f7SMed Ismail Bennani)
907a758c9f7SMed Ismail Bennani{
908a758c9f7SMed Ismail Bennani    lldb::SBMemoryRegionInfo* sb_ptr = NULL;
909a758c9f7SMed Ismail Bennani
910a758c9f7SMed Ismail Bennani    int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0);
911a758c9f7SMed Ismail Bennani
912a758c9f7SMed Ismail Bennani    if (valid_cast == -1)
913a758c9f7SMed Ismail Bennani        return NULL;
914a758c9f7SMed Ismail Bennani
915a758c9f7SMed Ismail Bennani    return sb_ptr;
916a758c9f7SMed Ismail Bennani}
917a758c9f7SMed Ismail Bennani
9189a14adeaSPavel Labathbool
9199a14adeaSPavel Labathlldb_private::LLDBSwigPythonCallCommand
9206498aff2SJonas Devlieghere(
9216498aff2SJonas Devlieghere    const char *python_function_name,
9226498aff2SJonas Devlieghere    const char *session_dictionary_name,
9236498aff2SJonas Devlieghere    lldb::DebuggerSP& debugger,
9246498aff2SJonas Devlieghere    const char* args,
9256498aff2SJonas Devlieghere    lldb_private::CommandReturnObject& cmd_retobj,
9266498aff2SJonas Devlieghere    lldb::ExecutionContextRefSP exe_ctx_ref_sp
9276498aff2SJonas Devlieghere)
9286498aff2SJonas Devlieghere{
9296498aff2SJonas Devlieghere    lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
9306498aff2SJonas Devlieghere    lldb::SBDebugger debugger_sb(debugger);
9316498aff2SJonas Devlieghere    lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
9326498aff2SJonas Devlieghere
9336498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
9346498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
9356498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
9366498aff2SJonas Devlieghere
9376498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
9386498aff2SJonas Devlieghere        return false;
9396498aff2SJonas Devlieghere
9406498aff2SJonas Devlieghere    auto argc = pfunc.GetArgInfo();
9416498aff2SJonas Devlieghere    if (!argc) {
9426498aff2SJonas Devlieghere        llvm::consumeError(argc.takeError());
9436498aff2SJonas Devlieghere        return false;
9446498aff2SJonas Devlieghere    }
9456498aff2SJonas Devlieghere    PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
9466498aff2SJonas Devlieghere    PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb));
947f1127914SPavel Labath    PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(cmd_retobj_sb));
9486498aff2SJonas Devlieghere
9496498aff2SJonas Devlieghere    if (argc.get().max_positional_args < 5u)
9506498aff2SJonas Devlieghere        pfunc(debugger_arg, PythonString(args), cmd_retobj_arg, dict);
9516498aff2SJonas Devlieghere    else
9526498aff2SJonas Devlieghere        pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg, dict);
9536498aff2SJonas Devlieghere
9546498aff2SJonas Devlieghere    return true;
9556498aff2SJonas Devlieghere}
9566498aff2SJonas Devlieghere
9579a14adeaSPavel Labathbool
9589a14adeaSPavel Labathlldb_private::LLDBSwigPythonCallCommandObject
9596498aff2SJonas Devlieghere(
9606498aff2SJonas Devlieghere    PyObject *implementor,
9616498aff2SJonas Devlieghere    lldb::DebuggerSP& debugger,
9626498aff2SJonas Devlieghere    const char* args,
9636498aff2SJonas Devlieghere    lldb_private::CommandReturnObject& cmd_retobj,
9646498aff2SJonas Devlieghere    lldb::ExecutionContextRefSP exe_ctx_ref_sp
9656498aff2SJonas Devlieghere)
9666498aff2SJonas Devlieghere{
9676498aff2SJonas Devlieghere    lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
9686498aff2SJonas Devlieghere    lldb::SBDebugger debugger_sb(debugger);
9696498aff2SJonas Devlieghere    lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
9706498aff2SJonas Devlieghere
9716498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
9726498aff2SJonas Devlieghere
9736498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, implementor);
9746498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>("__call__");
9756498aff2SJonas Devlieghere
9766498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
9776498aff2SJonas Devlieghere        return false;
9786498aff2SJonas Devlieghere
9796498aff2SJonas Devlieghere    PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
9806498aff2SJonas Devlieghere    PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb));
981f1127914SPavel Labath    PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(cmd_retobj_sb));
9826498aff2SJonas Devlieghere
9836498aff2SJonas Devlieghere    pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg);
9846498aff2SJonas Devlieghere
9856498aff2SJonas Devlieghere    return true;
9866498aff2SJonas Devlieghere}
9876498aff2SJonas Devlieghere
9889a14adeaSPavel Labathvoid*
9899a14adeaSPavel Labathlldb_private::LLDBSWIGPythonCreateOSPlugin
9906498aff2SJonas Devlieghere(
9916498aff2SJonas Devlieghere    const char *python_class_name,
9926498aff2SJonas Devlieghere    const char *session_dictionary_name,
9936498aff2SJonas Devlieghere    const lldb::ProcessSP& process_sp
9946498aff2SJonas Devlieghere)
9956498aff2SJonas Devlieghere{
9966498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
9976498aff2SJonas Devlieghere        Py_RETURN_NONE;
9986498aff2SJonas Devlieghere
9996498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
10006498aff2SJonas Devlieghere
10016498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
10026498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
10036498aff2SJonas Devlieghere
10046498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
10056498aff2SJonas Devlieghere        Py_RETURN_NONE;
10066498aff2SJonas Devlieghere
10077f09ab08SPavel Labath    auto result = pfunc(ToSWIGWrapper(process_sp));
10086498aff2SJonas Devlieghere
10096498aff2SJonas Devlieghere    if (result.IsAllocated())
10106498aff2SJonas Devlieghere        return result.release();
10116498aff2SJonas Devlieghere
10126498aff2SJonas Devlieghere    Py_RETURN_NONE;
10136498aff2SJonas Devlieghere}
10146498aff2SJonas Devlieghere
10159a14adeaSPavel Labathvoid*
10169a14adeaSPavel Labathlldb_private::LLDBSWIGPython_CreateFrameRecognizer
10176498aff2SJonas Devlieghere(
10186498aff2SJonas Devlieghere    const char *python_class_name,
10196498aff2SJonas Devlieghere    const char *session_dictionary_name
10206498aff2SJonas Devlieghere)
10216498aff2SJonas Devlieghere{
10226498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
10236498aff2SJonas Devlieghere        Py_RETURN_NONE;
10246498aff2SJonas Devlieghere
10256498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
10266498aff2SJonas Devlieghere
10276498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
10286498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
10296498aff2SJonas Devlieghere
10306498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
10316498aff2SJonas Devlieghere        Py_RETURN_NONE;
10326498aff2SJonas Devlieghere
10336498aff2SJonas Devlieghere    auto result = pfunc();
10346498aff2SJonas Devlieghere
10356498aff2SJonas Devlieghere    if (result.IsAllocated())
10366498aff2SJonas Devlieghere        return result.release();
10376498aff2SJonas Devlieghere
10386498aff2SJonas Devlieghere    Py_RETURN_NONE;
10396498aff2SJonas Devlieghere}
10406498aff2SJonas Devlieghere
10419a14adeaSPavel LabathPyObject*
10429a14adeaSPavel Labathlldb_private::LLDBSwigPython_GetRecognizedArguments
10436498aff2SJonas Devlieghere(
10446498aff2SJonas Devlieghere    PyObject *implementor,
10456498aff2SJonas Devlieghere    const lldb::StackFrameSP& frame_sp
10466498aff2SJonas Devlieghere)
10476498aff2SJonas Devlieghere{
10486498aff2SJonas Devlieghere    static char callee_name[] = "get_recognized_arguments";
10496498aff2SJonas Devlieghere
10506498aff2SJonas Devlieghere    lldb::SBFrame frame_sb(frame_sp);
10516498aff2SJonas Devlieghere    PyObject *arg = SBTypeToSWIGWrapper(frame_sb);
10526498aff2SJonas Devlieghere
10536498aff2SJonas Devlieghere    PythonString str(callee_name);
10546498aff2SJonas Devlieghere    PyObject* result = PyObject_CallMethodObjArgs(implementor, str.get(), arg,
10556498aff2SJonas Devlieghere                                                  NULL);
10566498aff2SJonas Devlieghere    return result;
10576498aff2SJonas Devlieghere}
10586498aff2SJonas Devlieghere
10599a14adeaSPavel Labathvoid*
10609a14adeaSPavel Labathlldb_private::LLDBSWIGPython_GetDynamicSetting (void* module, const char* setting, const lldb::TargetSP& target_sp)
10616498aff2SJonas Devlieghere{
10626498aff2SJonas Devlieghere    if (!module || !setting)
10636498aff2SJonas Devlieghere        Py_RETURN_NONE;
10646498aff2SJonas Devlieghere
10656498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
10666498aff2SJonas Devlieghere    PythonObject py_module(PyRefType::Borrowed, (PyObject *)module);
10676498aff2SJonas Devlieghere    auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting");
10686498aff2SJonas Devlieghere
10696498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
10706498aff2SJonas Devlieghere        Py_RETURN_NONE;
10716498aff2SJonas Devlieghere
10727f09ab08SPavel Labath    auto result = pfunc(ToSWIGWrapper(target_sp), PythonString(setting));
10736498aff2SJonas Devlieghere
10746498aff2SJonas Devlieghere    return result.release();
10756498aff2SJonas Devlieghere}
10766498aff2SJonas Devlieghere
10779a14adeaSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordProcess(
10787f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
10797f09ab08SPavel Labath    const lldb::ProcessSP &process, std::string &output) {
10806498aff2SJonas Devlieghere
10816498aff2SJonas Devlieghere    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
10826498aff2SJonas Devlieghere        return false;
10836498aff2SJonas Devlieghere
10846498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
10856498aff2SJonas Devlieghere
10866498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
10876498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
10886498aff2SJonas Devlieghere
10896498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
10906498aff2SJonas Devlieghere        return false;
10916498aff2SJonas Devlieghere
10927f09ab08SPavel Labath    auto result = pfunc(ToSWIGWrapper(process), dict);
10936498aff2SJonas Devlieghere
10946498aff2SJonas Devlieghere    output = result.Str().GetString().str();
10956498aff2SJonas Devlieghere
10966498aff2SJonas Devlieghere    return true;
10976498aff2SJonas Devlieghere}
10986498aff2SJonas Devlieghere
10999a14adeaSPavel Labathbool
11009a14adeaSPavel Labathlldb_private::LLDBSWIGPythonRunScriptKeywordThread
11016498aff2SJonas Devlieghere(const char* python_function_name,
11026498aff2SJonas Devlieghereconst char* session_dictionary_name,
11036498aff2SJonas Devliegherelldb::ThreadSP& thread,
11046498aff2SJonas Devliegherestd::string& output)
11056498aff2SJonas Devlieghere
11066498aff2SJonas Devlieghere{
11076498aff2SJonas Devlieghere    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
11086498aff2SJonas Devlieghere        return false;
11096498aff2SJonas Devlieghere
11106498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
11116498aff2SJonas Devlieghere
11126498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
11136498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
11146498aff2SJonas Devlieghere
11156498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
11166498aff2SJonas Devlieghere        return false;
11176498aff2SJonas Devlieghere
11186498aff2SJonas Devlieghere    lldb::SBThread thread_sb(thread);
11196498aff2SJonas Devlieghere    PythonObject thread_arg(PyRefType::Owned, SBTypeToSWIGWrapper(thread_sb));
11206498aff2SJonas Devlieghere    auto result = pfunc(thread_arg, dict);
11216498aff2SJonas Devlieghere
11226498aff2SJonas Devlieghere    output = result.Str().GetString().str();
11236498aff2SJonas Devlieghere
11246498aff2SJonas Devlieghere    return true;
11256498aff2SJonas Devlieghere}
11266498aff2SJonas Devlieghere
11279a14adeaSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordTarget(
11287f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
11297f09ab08SPavel Labath    const lldb::TargetSP &target, std::string &output) {
11306498aff2SJonas Devlieghere
11316498aff2SJonas Devlieghere    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
11326498aff2SJonas Devlieghere        return false;
11336498aff2SJonas Devlieghere
11346498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
11356498aff2SJonas Devlieghere
11366498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
11376498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name,dict);
11386498aff2SJonas Devlieghere
11396498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
11406498aff2SJonas Devlieghere        return false;
11416498aff2SJonas Devlieghere
11427f09ab08SPavel Labath    auto result = pfunc(ToSWIGWrapper(target), dict);
11436498aff2SJonas Devlieghere
11446498aff2SJonas Devlieghere    output = result.Str().GetString().str();
11456498aff2SJonas Devlieghere
11466498aff2SJonas Devlieghere    return true;
11476498aff2SJonas Devlieghere}
11486498aff2SJonas Devlieghere
11499a14adeaSPavel Labathbool
11509a14adeaSPavel Labathlldb_private::LLDBSWIGPythonRunScriptKeywordFrame
11516498aff2SJonas Devlieghere(const char* python_function_name,
11526498aff2SJonas Devlieghereconst char* session_dictionary_name,
11536498aff2SJonas Devliegherelldb::StackFrameSP& frame,
11546498aff2SJonas Devliegherestd::string& output)
11556498aff2SJonas Devlieghere
11566498aff2SJonas Devlieghere{
11576498aff2SJonas Devlieghere    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
11586498aff2SJonas Devlieghere        return false;
11596498aff2SJonas Devlieghere
11606498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
11616498aff2SJonas Devlieghere
11626498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
11636498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name,dict);
11646498aff2SJonas Devlieghere
11656498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
11666498aff2SJonas Devlieghere        return false;
11676498aff2SJonas Devlieghere
11686498aff2SJonas Devlieghere    lldb::SBFrame frame_sb(frame);
11696498aff2SJonas Devlieghere    PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(frame_sb));
11706498aff2SJonas Devlieghere    auto result = pfunc(frame_arg, dict);
11716498aff2SJonas Devlieghere
11726498aff2SJonas Devlieghere    output = result.Str().GetString().str();
11736498aff2SJonas Devlieghere
11746498aff2SJonas Devlieghere    return true;
11756498aff2SJonas Devlieghere}
11766498aff2SJonas Devlieghere
11779a14adeaSPavel Labathbool lldb_private::LLDBSWIGPythonRunScriptKeywordValue(
11787f09ab08SPavel Labath    const char *python_function_name, const char *session_dictionary_name,
11797f09ab08SPavel Labath    const lldb::ValueObjectSP &value, std::string &output) {
11806498aff2SJonas Devlieghere
11816498aff2SJonas Devlieghere    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
11826498aff2SJonas Devlieghere        return false;
11836498aff2SJonas Devlieghere
11846498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
11856498aff2SJonas Devlieghere
11866498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
11876498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
11886498aff2SJonas Devlieghere
11896498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
11906498aff2SJonas Devlieghere        return false;
11916498aff2SJonas Devlieghere
11927f09ab08SPavel Labath    auto result = pfunc(ToSWIGWrapper(value), dict);
11936498aff2SJonas Devlieghere
11946498aff2SJonas Devlieghere    output = result.Str().GetString().str();
11956498aff2SJonas Devlieghere
11966498aff2SJonas Devlieghere    return true;
11976498aff2SJonas Devlieghere}
11986498aff2SJonas Devlieghere
11999a14adeaSPavel Labathbool
12009a14adeaSPavel Labathlldb_private::LLDBSwigPythonCallModuleInit
12016498aff2SJonas Devlieghere(
12026498aff2SJonas Devlieghere    const char *python_module_name,
12036498aff2SJonas Devlieghere    const char *session_dictionary_name,
12046498aff2SJonas Devlieghere    lldb::DebuggerSP& debugger
12056498aff2SJonas Devlieghere)
12066498aff2SJonas Devlieghere{
12076498aff2SJonas Devlieghere    std::string python_function_name_string = python_module_name;
12086498aff2SJonas Devlieghere    python_function_name_string += ".__lldb_init_module";
12096498aff2SJonas Devlieghere    const char* python_function_name = python_function_name_string.c_str();
12106498aff2SJonas Devlieghere
12116498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
12126498aff2SJonas Devlieghere
12136498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
12146498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
12156498aff2SJonas Devlieghere
12166498aff2SJonas Devlieghere    // This method is optional and need not exist.  So if we don't find it,
12176498aff2SJonas Devlieghere    // it's actually a success, not a failure.
12186498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
12196498aff2SJonas Devlieghere        return true;
12206498aff2SJonas Devlieghere
12216498aff2SJonas Devlieghere    lldb::SBDebugger debugger_sb(debugger);
12226498aff2SJonas Devlieghere    PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
12236498aff2SJonas Devlieghere    pfunc(debugger_arg, dict);
12246498aff2SJonas Devlieghere
12256498aff2SJonas Devlieghere    return true;
12266498aff2SJonas Devlieghere}
12276498aff2SJonas Devlieghere
12289a14adeaSPavel Labathlldb::ValueObjectSP
12299a14adeaSPavel Labathlldb_private::LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data)
12306498aff2SJonas Devlieghere{
12316498aff2SJonas Devlieghere    lldb::ValueObjectSP valobj_sp;
12326498aff2SJonas Devlieghere    if (data)
12336498aff2SJonas Devlieghere    {
12346498aff2SJonas Devlieghere        lldb::SBValue* sb_ptr = (lldb::SBValue *)data;
12356498aff2SJonas Devlieghere        valobj_sp = sb_ptr->GetSP();
12366498aff2SJonas Devlieghere    }
12376498aff2SJonas Devlieghere    return valobj_sp;
12386498aff2SJonas Devlieghere}
12396498aff2SJonas Devlieghere
12406498aff2SJonas Devlieghere// For the LogOutputCallback functions
12419a14adeaSPavel Labathstatic void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) {
12426498aff2SJonas Devlieghere    if (baton != Py_None) {
12436498aff2SJonas Devlieghere      SWIG_PYTHON_THREAD_BEGIN_BLOCK;
12446498aff2SJonas Devlieghere      PyObject *result = PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str);
12456498aff2SJonas Devlieghere	  Py_XDECREF(result);
12466498aff2SJonas Devlieghere      SWIG_PYTHON_THREAD_END_BLOCK;
12476498aff2SJonas Devlieghere    }
12486498aff2SJonas Devlieghere}
12496498aff2SJonas Devlieghere%}
1250