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 {
82*f1127914SPavel Labath            // FIXME: SBStructuredData leaked here
836498aff2SJonas Devlieghere            lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl);
84*f1127914SPavel 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::SBValue sb_value (valobj_sp);
1496498aff2SJonas Devlieghere    lldb::SBTypeSummaryOptions sb_options(options_sp.get());
1506498aff2SJonas Devlieghere
1516498aff2SJonas Devlieghere    retval.clear();
1526498aff2SJonas Devlieghere
1536498aff2SJonas Devlieghere    if (!python_function_name || !session_dictionary)
1546498aff2SJonas Devlieghere        return false;
1556498aff2SJonas Devlieghere
1566498aff2SJonas Devlieghere    PyObject *pfunc_impl = nullptr;
1576498aff2SJonas Devlieghere
1586498aff2SJonas Devlieghere    if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper))
1596498aff2SJonas Devlieghere    {
1606498aff2SJonas Devlieghere        pfunc_impl = (PyObject*)(*pyfunct_wrapper);
1616498aff2SJonas Devlieghere        if (pfunc_impl->ob_refcnt == 1)
1626498aff2SJonas Devlieghere        {
1636498aff2SJonas Devlieghere            Py_XDECREF(pfunc_impl);
1646498aff2SJonas Devlieghere            pfunc_impl = NULL;
1656498aff2SJonas Devlieghere        }
1666498aff2SJonas Devlieghere    }
1676498aff2SJonas Devlieghere
1686498aff2SJonas Devlieghere    PyObject *py_dict = (PyObject*)session_dictionary;
1696498aff2SJonas Devlieghere    if (!PythonDictionary::Check(py_dict))
1706498aff2SJonas Devlieghere        return true;
1716498aff2SJonas Devlieghere
1726498aff2SJonas Devlieghere    PythonDictionary dict(PyRefType::Borrowed, py_dict);
1736498aff2SJonas Devlieghere
1746498aff2SJonas Devlieghere    PyErr_Cleaner pyerr_cleanup(true);  // show Python errors
1756498aff2SJonas Devlieghere
1766498aff2SJonas Devlieghere    PythonCallable pfunc(PyRefType::Borrowed, pfunc_impl);
1776498aff2SJonas Devlieghere
1786498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
1796498aff2SJonas Devlieghere    {
1806498aff2SJonas Devlieghere        pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
1816498aff2SJonas Devlieghere        if (!pfunc.IsAllocated())
1826498aff2SJonas Devlieghere            return false;
1836498aff2SJonas Devlieghere
1846498aff2SJonas Devlieghere        if (pyfunct_wrapper)
1856498aff2SJonas Devlieghere        {
1866498aff2SJonas Devlieghere            *pyfunct_wrapper = pfunc.get();
1876498aff2SJonas Devlieghere            Py_XINCREF(pfunc.get());
1886498aff2SJonas Devlieghere        }
1896498aff2SJonas Devlieghere    }
1906498aff2SJonas Devlieghere
1916498aff2SJonas Devlieghere    PythonObject result;
1926498aff2SJonas Devlieghere    auto argc = pfunc.GetArgInfo();
1936498aff2SJonas Devlieghere    if (!argc) {
1946498aff2SJonas Devlieghere        llvm::consumeError(argc.takeError());
1956498aff2SJonas Devlieghere        return false;
1966498aff2SJonas Devlieghere    }
1976498aff2SJonas Devlieghere
1986498aff2SJonas Devlieghere    PythonObject value_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_value));
1996498aff2SJonas Devlieghere    PythonObject options_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_options));
2006498aff2SJonas Devlieghere
2016498aff2SJonas Devlieghere    if (argc.get().max_positional_args < 3)
2026498aff2SJonas Devlieghere        result = pfunc(value_arg,dict);
2036498aff2SJonas Devlieghere    else
2046498aff2SJonas Devlieghere        result = pfunc(value_arg,dict,options_arg);
2056498aff2SJonas Devlieghere
2066498aff2SJonas Devlieghere    retval = result.Str().GetString().str();
2076498aff2SJonas Devlieghere
2086498aff2SJonas Devlieghere    return true;
2096498aff2SJonas Devlieghere}
2106498aff2SJonas Devlieghere
2116498aff2SJonas DevlieghereSWIGEXPORT void*
2126498aff2SJonas DevlieghereLLDBSwigPythonCreateSyntheticProvider
2136498aff2SJonas Devlieghere(
2146498aff2SJonas Devlieghere    const char *python_class_name,
2156498aff2SJonas Devlieghere    const char *session_dictionary_name,
2166498aff2SJonas Devlieghere    const lldb::ValueObjectSP& valobj_sp
2176498aff2SJonas Devlieghere)
2186498aff2SJonas Devlieghere{
2196498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
2206498aff2SJonas Devlieghere        Py_RETURN_NONE;
2216498aff2SJonas Devlieghere
2226498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
2236498aff2SJonas Devlieghere
2246498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
2256498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name,dict);
2266498aff2SJonas Devlieghere
2276498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
2286498aff2SJonas Devlieghere        Py_RETURN_NONE;
2296498aff2SJonas Devlieghere
230*f1127914SPavel Labath    // FIXME: SBValue leaked here
2316498aff2SJonas Devlieghere    lldb::SBValue *sb_value = new lldb::SBValue(valobj_sp);
2326498aff2SJonas Devlieghere    sb_value->SetPreferSyntheticValue(false);
2336498aff2SJonas Devlieghere
234*f1127914SPavel Labath    PythonObject val_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*sb_value));
2356498aff2SJonas Devlieghere    if (!val_arg.IsAllocated())
2366498aff2SJonas Devlieghere        Py_RETURN_NONE;
2376498aff2SJonas Devlieghere
2386498aff2SJonas Devlieghere    PythonObject result = pfunc(val_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
2466498aff2SJonas DevlieghereSWIGEXPORT void*
2476498aff2SJonas DevlieghereLLDBSwigPythonCreateCommandObject
2486498aff2SJonas Devlieghere(
2496498aff2SJonas Devlieghere    const char *python_class_name,
2506498aff2SJonas Devlieghere    const char *session_dictionary_name,
2516498aff2SJonas Devlieghere    const lldb::DebuggerSP debugger_sp
2526498aff2SJonas Devlieghere)
2536498aff2SJonas Devlieghere{
2546498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
2556498aff2SJonas Devlieghere        Py_RETURN_NONE;
2566498aff2SJonas Devlieghere
2576498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
2586498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
2596498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
2606498aff2SJonas Devlieghere
2616498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
2626498aff2SJonas Devlieghere        return nullptr;
2636498aff2SJonas Devlieghere
2646498aff2SJonas Devlieghere    lldb::SBDebugger debugger_sb(debugger_sp);
2656498aff2SJonas Devlieghere    PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
2666498aff2SJonas Devlieghere    PythonObject result = pfunc(debugger_arg, dict);
2676498aff2SJonas Devlieghere
2686498aff2SJonas Devlieghere    if (result.IsAllocated())
2696498aff2SJonas Devlieghere        return result.release();
2706498aff2SJonas Devlieghere
2716498aff2SJonas Devlieghere    Py_RETURN_NONE;
2726498aff2SJonas Devlieghere}
2736498aff2SJonas Devlieghere
2746498aff2SJonas DevlieghereSWIGEXPORT void*
2751f6a57c1SMed Ismail BennaniLLDBSwigPythonCreateScriptedProcess
2761f6a57c1SMed Ismail Bennani(
2771f6a57c1SMed Ismail Bennani    const char *python_class_name,
2781f6a57c1SMed Ismail Bennani    const char *session_dictionary_name,
2791f6a57c1SMed Ismail Bennani    const lldb::TargetSP& target_sp,
2801f6a57c1SMed Ismail Bennani    lldb_private::StructuredDataImpl *args_impl,
2811f6a57c1SMed Ismail Bennani    std::string &error_string
2821f6a57c1SMed Ismail Bennani)
2831f6a57c1SMed Ismail Bennani{
2841f6a57c1SMed Ismail Bennani    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
2851f6a57c1SMed Ismail Bennani        Py_RETURN_NONE;
2861f6a57c1SMed Ismail Bennani
2871f6a57c1SMed Ismail Bennani    PyErr_Cleaner py_err_cleaner(true);
2881f6a57c1SMed Ismail Bennani
2891f6a57c1SMed Ismail Bennani    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
2901f6a57c1SMed Ismail Bennani    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
2911f6a57c1SMed Ismail Bennani
2921f6a57c1SMed Ismail Bennani    if (!pfunc.IsAllocated()) {
2931f6a57c1SMed Ismail Bennani        error_string.append("could not find script class: ");
2941f6a57c1SMed Ismail Bennani        error_string.append(python_class_name);
2951f6a57c1SMed Ismail Bennani        return nullptr;
2961f6a57c1SMed Ismail Bennani    }
2971f6a57c1SMed Ismail Bennani
298*f1127914SPavel Labath    // FIXME: SBTarget leaked here
299*f1127914SPavel Labath    PythonObject target_arg(
300*f1127914SPavel Labath        PyRefType::Owned, SBTypeToSWIGWrapper(*new lldb::SBTarget(target_sp)));
3011f6a57c1SMed Ismail Bennani
3021f6a57c1SMed Ismail Bennani    if (!target_arg.IsAllocated())
3031f6a57c1SMed Ismail Bennani        Py_RETURN_NONE;
3041f6a57c1SMed Ismail Bennani
3051f6a57c1SMed Ismail Bennani    llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
3061f6a57c1SMed Ismail Bennani    if (!arg_info) {
3071f6a57c1SMed Ismail Bennani        llvm::handleAllErrors(
3081f6a57c1SMed Ismail Bennani            arg_info.takeError(),
3091f6a57c1SMed Ismail Bennani            [&](PythonException &E) {
3101f6a57c1SMed Ismail Bennani                error_string.append(E.ReadBacktrace());
3111f6a57c1SMed Ismail Bennani            },
3121f6a57c1SMed Ismail Bennani            [&](const llvm::ErrorInfoBase &E) {
3131f6a57c1SMed Ismail Bennani                error_string.append(E.message());
3141f6a57c1SMed Ismail Bennani            });
3151f6a57c1SMed Ismail Bennani        Py_RETURN_NONE;
3161f6a57c1SMed Ismail Bennani    }
3171f6a57c1SMed Ismail Bennani
3181f6a57c1SMed Ismail Bennani    PythonObject result = {};
3191f6a57c1SMed Ismail Bennani    if (arg_info.get().max_positional_args == 2) {
320*f1127914SPavel Labath        // FIXME: SBStructuredData leaked here
321*f1127914SPavel Labath        PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*new lldb::SBStructuredData(args_impl)));
322738621d0SMed Ismail Bennani        result = pfunc(target_arg, args_arg);
3231f6a57c1SMed Ismail Bennani    } else {
324738621d0SMed Ismail Bennani        error_string.assign("wrong number of arguments in __init__, should be 2 (not including self)");
3251f6a57c1SMed Ismail Bennani        Py_RETURN_NONE;
3261f6a57c1SMed Ismail Bennani    }
3271f6a57c1SMed Ismail Bennani
3281f6a57c1SMed Ismail Bennani    if (result.IsAllocated())
3291f6a57c1SMed Ismail Bennani        return result.release();
3301f6a57c1SMed Ismail Bennani    Py_RETURN_NONE;
3311f6a57c1SMed Ismail Bennani}
3321f6a57c1SMed Ismail Bennani
3331f6a57c1SMed Ismail BennaniSWIGEXPORT void*
33459d8dd79SMed Ismail BennaniLLDBSwigPythonCreateScriptedThread
33559d8dd79SMed Ismail Bennani(
33659d8dd79SMed Ismail Bennani    const char *python_class_name,
33759d8dd79SMed Ismail Bennani    const char *session_dictionary_name,
338738621d0SMed Ismail Bennani    const lldb::ProcessSP& process_sp,
339738621d0SMed Ismail Bennani    lldb_private::StructuredDataImpl *args_impl,
34059d8dd79SMed Ismail Bennani    std::string &error_string
34159d8dd79SMed Ismail Bennani)
34259d8dd79SMed Ismail Bennani{
34359d8dd79SMed Ismail Bennani    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
34459d8dd79SMed Ismail Bennani        Py_RETURN_NONE;
34559d8dd79SMed Ismail Bennani
34659d8dd79SMed Ismail Bennani    PyErr_Cleaner py_err_cleaner(true);
34759d8dd79SMed Ismail Bennani
34859d8dd79SMed Ismail Bennani    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
34959d8dd79SMed Ismail Bennani    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
35059d8dd79SMed Ismail Bennani
35159d8dd79SMed Ismail Bennani    if (!pfunc.IsAllocated()) {
35259d8dd79SMed Ismail Bennani        error_string.append("could not find script class: ");
35359d8dd79SMed Ismail Bennani        error_string.append(python_class_name);
35459d8dd79SMed Ismail Bennani        return nullptr;
35559d8dd79SMed Ismail Bennani    }
35659d8dd79SMed Ismail Bennani
357*f1127914SPavel Labath    // FIXME: This leaks the SBProcess object
358*f1127914SPavel Labath    PythonObject process_arg(
359*f1127914SPavel Labath        PyRefType::Owned,
360*f1127914SPavel Labath        SBTypeToSWIGWrapper(*new lldb::SBProcess(process_sp)));
36159d8dd79SMed Ismail Bennani
362738621d0SMed Ismail Bennani    if (!process_arg.IsAllocated())
36359d8dd79SMed Ismail Bennani        Py_RETURN_NONE;
36459d8dd79SMed Ismail Bennani
36559d8dd79SMed Ismail Bennani    llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
36659d8dd79SMed Ismail Bennani    if (!arg_info) {
36759d8dd79SMed Ismail Bennani        llvm::handleAllErrors(
36859d8dd79SMed Ismail Bennani            arg_info.takeError(),
36959d8dd79SMed Ismail Bennani            [&](PythonException &E) {
37059d8dd79SMed Ismail Bennani                error_string.append(E.ReadBacktrace());
37159d8dd79SMed Ismail Bennani            },
37259d8dd79SMed Ismail Bennani            [&](const llvm::ErrorInfoBase &E) {
37359d8dd79SMed Ismail Bennani                error_string.append(E.message());
37459d8dd79SMed Ismail Bennani            });
37559d8dd79SMed Ismail Bennani        Py_RETURN_NONE;
37659d8dd79SMed Ismail Bennani    }
37759d8dd79SMed Ismail Bennani
37859d8dd79SMed Ismail Bennani    PythonObject result = {};
379738621d0SMed Ismail Bennani    if (arg_info.get().max_positional_args == 2) {
380*f1127914SPavel Labath        // FIXME: SBStructuredData leaked here
381*f1127914SPavel Labath        PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*new lldb::SBStructuredData(args_impl)));
382738621d0SMed Ismail Bennani        result = pfunc(process_arg, args_arg);
38359d8dd79SMed Ismail Bennani    } else {
384738621d0SMed Ismail Bennani        error_string.assign("wrong number of arguments in __init__, should be 2 (not including self)");
38559d8dd79SMed Ismail Bennani        Py_RETURN_NONE;
38659d8dd79SMed Ismail Bennani    }
38759d8dd79SMed Ismail Bennani
38859d8dd79SMed Ismail Bennani    if (result.IsAllocated())
38959d8dd79SMed Ismail Bennani        return result.release();
39059d8dd79SMed Ismail Bennani    Py_RETURN_NONE;
39159d8dd79SMed Ismail Bennani}
39259d8dd79SMed Ismail Bennani
39359d8dd79SMed Ismail BennaniSWIGEXPORT void*
3946498aff2SJonas DevlieghereLLDBSwigPythonCreateScriptedThreadPlan
3956498aff2SJonas Devlieghere(
3966498aff2SJonas Devlieghere    const char *python_class_name,
3976498aff2SJonas Devlieghere    const char *session_dictionary_name,
3986498aff2SJonas Devlieghere    lldb_private::StructuredDataImpl *args_impl,
3996498aff2SJonas Devlieghere    std::string &error_string,
4006498aff2SJonas Devlieghere    const lldb::ThreadPlanSP& thread_plan_sp
4016498aff2SJonas Devlieghere)
4026498aff2SJonas Devlieghere{
4036498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
4046498aff2SJonas Devlieghere        Py_RETURN_NONE;
4056498aff2SJonas Devlieghere
4066498aff2SJonas Devlieghere
4076498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
4086498aff2SJonas Devlieghere
4096498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
4106498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
4116498aff2SJonas Devlieghere
4126498aff2SJonas Devlieghere    if (!pfunc.IsAllocated()) {
4136498aff2SJonas Devlieghere        error_string.append("could not find script class: ");
4146498aff2SJonas Devlieghere        error_string.append(python_class_name);
4156498aff2SJonas Devlieghere        return nullptr;
4166498aff2SJonas Devlieghere    }
4176498aff2SJonas Devlieghere
418*f1127914SPavel Labath    // FIXME: SBThreadPlan leaked here
419*f1127914SPavel Labath    PythonObject tp_arg(
420*f1127914SPavel Labath        PyRefType::Owned,
421*f1127914SPavel Labath        SBTypeToSWIGWrapper(*new lldb::SBThreadPlan(thread_plan_sp)));
4226498aff2SJonas Devlieghere
4236498aff2SJonas Devlieghere    if (!tp_arg.IsAllocated())
4246498aff2SJonas Devlieghere        Py_RETURN_NONE;
4256498aff2SJonas Devlieghere
4266498aff2SJonas Devlieghere    llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
4276498aff2SJonas Devlieghere    if (!arg_info) {
4286498aff2SJonas Devlieghere        llvm::handleAllErrors(
4296498aff2SJonas Devlieghere            arg_info.takeError(),
4306498aff2SJonas Devlieghere            [&](PythonException &E) {
4316498aff2SJonas Devlieghere                error_string.append(E.ReadBacktrace());
4326498aff2SJonas Devlieghere            },
4336498aff2SJonas Devlieghere            [&](const llvm::ErrorInfoBase &E) {
4346498aff2SJonas Devlieghere                error_string.append(E.message());
4356498aff2SJonas Devlieghere            });
4366498aff2SJonas Devlieghere        Py_RETURN_NONE;
4376498aff2SJonas Devlieghere    }
4386498aff2SJonas Devlieghere
4396498aff2SJonas Devlieghere    PythonObject result = {};
4406498aff2SJonas Devlieghere    if (arg_info.get().max_positional_args == 2) {
4416498aff2SJonas Devlieghere        if (args_impl != nullptr) {
4426498aff2SJonas Devlieghere           error_string.assign("args passed, but __init__ does not take an args dictionary");
4436498aff2SJonas Devlieghere           Py_RETURN_NONE;
4446498aff2SJonas Devlieghere        }
4456498aff2SJonas Devlieghere        result = pfunc(tp_arg, dict);
4466498aff2SJonas Devlieghere    } else if (arg_info.get().max_positional_args >= 3) {
447*f1127914SPavel Labath        // FIXME: SBStructuredData leaked here
448*f1127914SPavel Labath        PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*new lldb::SBStructuredData(args_impl)));
4496498aff2SJonas Devlieghere        result = pfunc(tp_arg, args_arg, dict);
4506498aff2SJonas Devlieghere    } else {
4516498aff2SJonas Devlieghere        error_string.assign("wrong number of arguments in __init__, should be 2 or 3 (not including self)");
4526498aff2SJonas Devlieghere        Py_RETURN_NONE;
4536498aff2SJonas Devlieghere    }
4546498aff2SJonas Devlieghere
4556498aff2SJonas Devlieghere    // FIXME: At this point we should check that the class we found supports all the methods
4566498aff2SJonas Devlieghere    // that we need.
4576498aff2SJonas Devlieghere
4586498aff2SJonas Devlieghere    if (result.IsAllocated())
4596498aff2SJonas Devlieghere        return result.release();
4606498aff2SJonas Devlieghere    Py_RETURN_NONE;
4616498aff2SJonas Devlieghere}
4626498aff2SJonas Devlieghere
4636498aff2SJonas DevlieghereSWIGEXPORT bool
4646498aff2SJonas DevlieghereLLDBSWIGPythonCallThreadPlan
4656498aff2SJonas Devlieghere(
4666498aff2SJonas Devlieghere    void *implementor,
4676498aff2SJonas Devlieghere    const char *method_name,
4686498aff2SJonas Devlieghere    lldb_private::Event *event,
4696498aff2SJonas Devlieghere    bool &got_error
4706498aff2SJonas Devlieghere)
4716498aff2SJonas Devlieghere{
4726498aff2SJonas Devlieghere    got_error = false;
4736498aff2SJonas Devlieghere
4746498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(false);
4756498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor));
4766498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>(method_name);
4776498aff2SJonas Devlieghere
4786498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
4796498aff2SJonas Devlieghere        return false;
4806498aff2SJonas Devlieghere
4816498aff2SJonas Devlieghere    PythonObject result;
4826498aff2SJonas Devlieghere    if (event != nullptr)
4836498aff2SJonas Devlieghere    {
4846498aff2SJonas Devlieghere        lldb::SBEvent sb_event(event);
4856498aff2SJonas Devlieghere        PythonObject event_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_event));
4866498aff2SJonas Devlieghere        result = pfunc(event_arg);
4876498aff2SJonas Devlieghere    }
4886498aff2SJonas Devlieghere    else
4896498aff2SJonas Devlieghere        result = pfunc();
4906498aff2SJonas Devlieghere
4916498aff2SJonas Devlieghere    if (PyErr_Occurred())
4926498aff2SJonas Devlieghere    {
4936498aff2SJonas Devlieghere        got_error = true;
4946498aff2SJonas Devlieghere        printf ("Return value was neither false nor true for call to %s.\n", method_name);
4956498aff2SJonas Devlieghere        PyErr_Print();
4966498aff2SJonas Devlieghere        return false;
4976498aff2SJonas Devlieghere    }
4986498aff2SJonas Devlieghere
4996498aff2SJonas Devlieghere    if (result.get() == Py_True)
5006498aff2SJonas Devlieghere        return true;
5016498aff2SJonas Devlieghere    else if (result.get() == Py_False)
5026498aff2SJonas Devlieghere        return false;
5036498aff2SJonas Devlieghere
5046498aff2SJonas Devlieghere    // Somebody returned the wrong thing...
5056498aff2SJonas Devlieghere    got_error = true;
5066498aff2SJonas Devlieghere    printf ("Wrong return value type for call to %s.\n", method_name);
5076498aff2SJonas Devlieghere    return false;
5086498aff2SJonas Devlieghere}
5096498aff2SJonas Devlieghere
5106498aff2SJonas DevlieghereSWIGEXPORT void *
5116498aff2SJonas DevlieghereLLDBSwigPythonCreateScriptedBreakpointResolver
5126498aff2SJonas Devlieghere(
5136498aff2SJonas Devlieghere    const char *python_class_name,
5146498aff2SJonas Devlieghere    const char *session_dictionary_name,
5156498aff2SJonas Devlieghere    lldb_private::StructuredDataImpl *args_impl,
5166498aff2SJonas Devlieghere    lldb::BreakpointSP &breakpoint_sp
5176498aff2SJonas Devlieghere)
5186498aff2SJonas Devlieghere{
5196498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
5206498aff2SJonas Devlieghere        Py_RETURN_NONE;
5216498aff2SJonas Devlieghere
5226498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
5236498aff2SJonas Devlieghere
5246498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
5256498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
5266498aff2SJonas Devlieghere
5276498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
5286498aff2SJonas Devlieghere        return nullptr;
5296498aff2SJonas Devlieghere
530*f1127914SPavel Labath    // FIXME: SBBreakpoint leaked here
5316498aff2SJonas Devlieghere    lldb::SBBreakpoint *bkpt_value = new lldb::SBBreakpoint(breakpoint_sp);
5326498aff2SJonas Devlieghere
533*f1127914SPavel Labath    PythonObject bkpt_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*bkpt_value));
5346498aff2SJonas Devlieghere
535*f1127914SPavel Labath    // FIXME: SBStructuredData leaked here
5366498aff2SJonas Devlieghere    lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl);
537*f1127914SPavel Labath    PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*args_value));
5386498aff2SJonas Devlieghere
5396498aff2SJonas Devlieghere    PythonObject result = pfunc(bkpt_arg, args_arg, dict);
5406498aff2SJonas Devlieghere    // FIXME: At this point we should check that the class we found supports all the methods
5416498aff2SJonas Devlieghere    // that we need.
5426498aff2SJonas Devlieghere
5436498aff2SJonas Devlieghere    if (result.IsAllocated())
5446498aff2SJonas Devlieghere    {
5456498aff2SJonas Devlieghere        // Check that __callback__ is defined:
5466498aff2SJonas Devlieghere        auto callback_func = result.ResolveName<PythonCallable>("__callback__");
5476498aff2SJonas Devlieghere        if (callback_func.IsAllocated())
5486498aff2SJonas Devlieghere            return result.release();
5496498aff2SJonas Devlieghere        else
5506498aff2SJonas Devlieghere            result.release();
5516498aff2SJonas Devlieghere    }
5526498aff2SJonas Devlieghere    Py_RETURN_NONE;
5536498aff2SJonas Devlieghere}
5546498aff2SJonas Devlieghere
5556498aff2SJonas DevlieghereSWIGEXPORT unsigned int
5566498aff2SJonas DevlieghereLLDBSwigPythonCallBreakpointResolver
5576498aff2SJonas Devlieghere(
5586498aff2SJonas Devlieghere    void *implementor,
5596498aff2SJonas Devlieghere    const char *method_name,
5606498aff2SJonas Devlieghere    lldb_private::SymbolContext *sym_ctx
5616498aff2SJonas Devlieghere)
5626498aff2SJonas Devlieghere{
5636498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(false);
5646498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor));
5656498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>(method_name);
5666498aff2SJonas Devlieghere
5676498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
5686498aff2SJonas Devlieghere        return 0;
5696498aff2SJonas Devlieghere
5706498aff2SJonas Devlieghere    PythonObject result;
5716498aff2SJonas Devlieghere    if (sym_ctx != nullptr) {
5726498aff2SJonas Devlieghere      lldb::SBSymbolContext sb_sym_ctx(sym_ctx);
5736498aff2SJonas Devlieghere      PythonObject sym_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_sym_ctx));
5746498aff2SJonas Devlieghere      result = pfunc(sym_ctx_arg);
5756498aff2SJonas Devlieghere    } else
5766498aff2SJonas Devlieghere      result = pfunc();
5776498aff2SJonas Devlieghere
5786498aff2SJonas Devlieghere    if (PyErr_Occurred())
5796498aff2SJonas Devlieghere    {
5806498aff2SJonas Devlieghere        PyErr_Print();
58152712d3fSLawrence D'Anna        PyErr_Clear();
5826498aff2SJonas Devlieghere        return 0;
5836498aff2SJonas Devlieghere    }
5846498aff2SJonas Devlieghere
5856498aff2SJonas Devlieghere    // The callback will return a bool, but we're need to also return ints
5866498aff2SJonas Devlieghere    // so we're squirrelling the bool through as an int...  And if you return
5876498aff2SJonas Devlieghere    // nothing, we'll continue.
5886498aff2SJonas Devlieghere    if (strcmp(method_name, "__callback__") == 0) {
5896498aff2SJonas Devlieghere        if (result.get() == Py_False)
5906498aff2SJonas Devlieghere          return 0;
5916498aff2SJonas Devlieghere        else
5926498aff2SJonas Devlieghere          return 1;
5936498aff2SJonas Devlieghere    }
5946498aff2SJonas Devlieghere
59552712d3fSLawrence D'Anna    long long ret_val = unwrapOrSetPythonException(As<long long>(result));
596478619cfSMuhammad Omair Javaid
59752712d3fSLawrence D'Anna    if (PyErr_Occurred()) {
59852712d3fSLawrence D'Anna        PyErr_Print();
59952712d3fSLawrence D'Anna        PyErr_Clear();
60052712d3fSLawrence D'Anna        return 0;
60152712d3fSLawrence D'Anna    }
6026498aff2SJonas Devlieghere
6036498aff2SJonas Devlieghere    return ret_val;
6046498aff2SJonas Devlieghere}
6056498aff2SJonas Devlieghere
6061b1d9815SJim InghamSWIGEXPORT void *
6071b1d9815SJim InghamLLDBSwigPythonCreateScriptedStopHook
6081b1d9815SJim Ingham(
6091b1d9815SJim Ingham    lldb::TargetSP target_sp,
6101b1d9815SJim Ingham    const char *python_class_name,
6111b1d9815SJim Ingham    const char *session_dictionary_name,
6121b1d9815SJim Ingham    lldb_private::StructuredDataImpl *args_impl,
6131b1d9815SJim Ingham    Status &error
6141b1d9815SJim Ingham)
6151b1d9815SJim Ingham{
6161b1d9815SJim Ingham    if (python_class_name == NULL || python_class_name[0] == '\0') {
6171b1d9815SJim Ingham        error.SetErrorString("Empty class name.");
6181b1d9815SJim Ingham        Py_RETURN_NONE;
6191b1d9815SJim Ingham    }
6201b1d9815SJim Ingham    if (!session_dictionary_name) {
6211b1d9815SJim Ingham      error.SetErrorString("No session dictionary");
6221b1d9815SJim Ingham      Py_RETURN_NONE;
6231b1d9815SJim Ingham    }
6241b1d9815SJim Ingham
6251b1d9815SJim Ingham    PyErr_Cleaner py_err_cleaner(true);
6261b1d9815SJim Ingham
6271b1d9815SJim Ingham    auto dict =
6281b1d9815SJim Ingham        PythonModule::MainModule().ResolveName<PythonDictionary>(
6291b1d9815SJim Ingham            session_dictionary_name);
6301b1d9815SJim Ingham    auto pfunc =
6311b1d9815SJim Ingham        PythonObject::ResolveNameWithDictionary<PythonCallable>(
6321b1d9815SJim Ingham            python_class_name, dict);
6331b1d9815SJim Ingham
6341b1d9815SJim Ingham    if (!pfunc.IsAllocated()) {
6351b1d9815SJim Ingham        error.SetErrorStringWithFormat("Could not find class: %s.",
6361b1d9815SJim Ingham                                       python_class_name);
6371b1d9815SJim Ingham        return nullptr;
6381b1d9815SJim Ingham    }
6391b1d9815SJim Ingham
640*f1127914SPavel Labath    // FIXME: SBTarget leaked here
6411b1d9815SJim Ingham    lldb::SBTarget *target_val
6421b1d9815SJim Ingham        = new lldb::SBTarget(target_sp);
643*f1127914SPavel Labath    PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*target_val));
6441b1d9815SJim Ingham
645*f1127914SPavel Labath    // FIXME: SBStructuredData leaked here
6461b1d9815SJim Ingham    lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl);
647*f1127914SPavel Labath    PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*args_value));
6481b1d9815SJim Ingham
6491b1d9815SJim Ingham    PythonObject result = pfunc(target_arg, args_arg, dict);
6501b1d9815SJim Ingham
6511b1d9815SJim Ingham    if (result.IsAllocated())
6521b1d9815SJim Ingham    {
6531b1d9815SJim Ingham        // Check that the handle_stop callback is defined:
6541b1d9815SJim Ingham        auto callback_func = result.ResolveName<PythonCallable>("handle_stop");
6551b1d9815SJim Ingham        if (callback_func.IsAllocated()) {
6561b1d9815SJim Ingham          if (auto args_info = callback_func.GetArgInfo()) {
6571b1d9815SJim Ingham            size_t num_args = (*args_info).max_positional_args;
6581b1d9815SJim Ingham            if (num_args != 2) {
6591b1d9815SJim Ingham              error.SetErrorStringWithFormat("Wrong number of args for "
6602f95c50aSRichard Smith              "handle_stop callback, should be 2 (excluding self), got: %zu",
6611b1d9815SJim Ingham              num_args);
6621b1d9815SJim Ingham              Py_RETURN_NONE;
6631b1d9815SJim Ingham            } else
6641b1d9815SJim Ingham              return result.release();
6651b1d9815SJim Ingham          } else {
6661b1d9815SJim Ingham            error.SetErrorString("Couldn't get num arguments for handle_stop "
6671b1d9815SJim Ingham                                 "callback.");
6681b1d9815SJim Ingham            Py_RETURN_NONE;
6691b1d9815SJim Ingham          }
6701b1d9815SJim Ingham          return result.release();
6711b1d9815SJim Ingham        }
6721b1d9815SJim Ingham        else {
6731b1d9815SJim Ingham          error.SetErrorStringWithFormat("Class \"%s\" is missing the required "
6741b1d9815SJim Ingham                                         "handle_stop callback.",
6751b1d9815SJim Ingham                                         python_class_name);
6761b1d9815SJim Ingham          result.release();
6771b1d9815SJim Ingham        }
6781b1d9815SJim Ingham    }
6791b1d9815SJim Ingham    Py_RETURN_NONE;
6801b1d9815SJim Ingham}
6811b1d9815SJim Ingham
6821b1d9815SJim InghamSWIGEXPORT bool
6831b1d9815SJim InghamLLDBSwigPythonStopHookCallHandleStop
6841b1d9815SJim Ingham(
6851b1d9815SJim Ingham    void *implementor,
6861b1d9815SJim Ingham    lldb::ExecutionContextRefSP exc_ctx_sp,
6871b1d9815SJim Ingham    lldb::StreamSP stream
6881b1d9815SJim Ingham)
6891b1d9815SJim Ingham{
6901b1d9815SJim Ingham    // handle_stop will return a bool with the meaning "should_stop"...
6911b1d9815SJim Ingham    // If you return nothing we'll assume we are going to stop.
6921b1d9815SJim Ingham    // Also any errors should return true, since we should stop on error.
6931b1d9815SJim Ingham
6941b1d9815SJim Ingham    PyErr_Cleaner py_err_cleaner(false);
6951b1d9815SJim Ingham    PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor));
6961b1d9815SJim Ingham    auto pfunc = self.ResolveName<PythonCallable>("handle_stop");
6971b1d9815SJim Ingham
6981b1d9815SJim Ingham    if (!pfunc.IsAllocated())
6991b1d9815SJim Ingham        return true;
7001b1d9815SJim Ingham
7011b1d9815SJim Ingham    PythonObject result;
7021b1d9815SJim Ingham    lldb::SBExecutionContext sb_exc_ctx(exc_ctx_sp);
7031b1d9815SJim Ingham    PythonObject exc_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_exc_ctx));
7041b1d9815SJim Ingham    lldb::SBStream sb_stream;
7051b1d9815SJim Ingham    PythonObject sb_stream_arg(PyRefType::Owned,
7061b1d9815SJim Ingham                               SBTypeToSWIGWrapper(sb_stream));
7071b1d9815SJim Ingham    result = pfunc(exc_ctx_arg, sb_stream_arg);
7081b1d9815SJim Ingham
7091b1d9815SJim Ingham    if (PyErr_Occurred())
7101b1d9815SJim Ingham    {
7111b1d9815SJim Ingham        stream->PutCString("Python error occurred handling stop-hook.");
7121b1d9815SJim Ingham        PyErr_Print();
7131b1d9815SJim Ingham        PyErr_Clear();
7141b1d9815SJim Ingham        return true;
7151b1d9815SJim Ingham    }
7161b1d9815SJim Ingham
7171b1d9815SJim Ingham    // Now add the result to the output stream.  SBStream only
7181b1d9815SJim Ingham    // makes an internally help StreamString which I can't interpose, so I
7191b1d9815SJim Ingham    // have to copy it over here.
7201b1d9815SJim Ingham    stream->PutCString(sb_stream.GetData());
7211b1d9815SJim Ingham
7221b1d9815SJim Ingham    if (result.get() == Py_False)
7231b1d9815SJim Ingham      return false;
7241b1d9815SJim Ingham    else
7251b1d9815SJim Ingham      return true;
7261b1d9815SJim Ingham}
7271b1d9815SJim Ingham
7286498aff2SJonas Devlieghere// wrapper that calls an optional instance member of an object taking no arguments
7296498aff2SJonas Devliegherestatic PyObject*
7306498aff2SJonas DevlieghereLLDBSwigPython_CallOptionalMember
7316498aff2SJonas Devlieghere(
7326498aff2SJonas Devlieghere    PyObject* implementor,
7336498aff2SJonas Devlieghere    char* callee_name,
7346498aff2SJonas Devlieghere    PyObject* ret_if_not_found = Py_None,
7356498aff2SJonas Devlieghere    bool* was_found = NULL
7366498aff2SJonas Devlieghere)
7376498aff2SJonas Devlieghere{
7386498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(false);
7396498aff2SJonas Devlieghere
7406498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor));
7416498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>(callee_name);
7426498aff2SJonas Devlieghere
7436498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
7446498aff2SJonas Devlieghere    {
7456498aff2SJonas Devlieghere        if (was_found)
7466498aff2SJonas Devlieghere            *was_found = false;
7476498aff2SJonas Devlieghere        Py_XINCREF(ret_if_not_found);
7486498aff2SJonas Devlieghere        return ret_if_not_found;
7496498aff2SJonas Devlieghere    }
7506498aff2SJonas Devlieghere
7516498aff2SJonas Devlieghere    if (was_found)
7526498aff2SJonas Devlieghere        *was_found = true;
7536498aff2SJonas Devlieghere
7546498aff2SJonas Devlieghere    PythonObject result = pfunc();
7556498aff2SJonas Devlieghere    return result.release();
7566498aff2SJonas Devlieghere}
7576498aff2SJonas Devlieghere
7586498aff2SJonas DevlieghereSWIGEXPORT size_t
7596498aff2SJonas DevlieghereLLDBSwigPython_CalculateNumChildren
7606498aff2SJonas Devlieghere(
7616498aff2SJonas Devlieghere    PyObject *implementor,
7626498aff2SJonas Devlieghere    uint32_t max
7636498aff2SJonas Devlieghere)
7646498aff2SJonas Devlieghere{
7656498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, implementor);
7666498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>("num_children");
7676498aff2SJonas Devlieghere
7686498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
7696498aff2SJonas Devlieghere        return 0;
7706498aff2SJonas Devlieghere
7716498aff2SJonas Devlieghere    auto arg_info = pfunc.GetArgInfo();
7726498aff2SJonas Devlieghere    if (!arg_info) {
7736498aff2SJonas Devlieghere        llvm::consumeError(arg_info.takeError());
7746498aff2SJonas Devlieghere        return 0;
7756498aff2SJonas Devlieghere    }
7766498aff2SJonas Devlieghere
77752712d3fSLawrence D'Anna    size_t ret_val;
778478619cfSMuhammad Omair Javaid    if (arg_info.get().max_positional_args < 1)
77952712d3fSLawrence D'Anna        ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call()));
780478619cfSMuhammad Omair Javaid    else
78152712d3fSLawrence D'Anna        ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call(PythonInteger(max))));
782478619cfSMuhammad Omair Javaid
78352712d3fSLawrence D'Anna    if (PyErr_Occurred())
7846498aff2SJonas Devlieghere    {
7856498aff2SJonas Devlieghere        PyErr_Print();
7866498aff2SJonas Devlieghere        PyErr_Clear();
78752712d3fSLawrence D'Anna        return 0;
7886498aff2SJonas Devlieghere    }
7896498aff2SJonas Devlieghere
7906498aff2SJonas Devlieghere    if (arg_info.get().max_positional_args < 1)
7916498aff2SJonas Devlieghere        ret_val = std::min(ret_val, static_cast<size_t>(max));
7926498aff2SJonas Devlieghere
7936498aff2SJonas Devlieghere    return ret_val;
7946498aff2SJonas Devlieghere}
7956498aff2SJonas Devlieghere
7966498aff2SJonas DevlieghereSWIGEXPORT PyObject*
7976498aff2SJonas DevlieghereLLDBSwigPython_GetChildAtIndex
7986498aff2SJonas Devlieghere(
7996498aff2SJonas Devlieghere    PyObject *implementor,
8006498aff2SJonas Devlieghere    uint32_t idx
8016498aff2SJonas Devlieghere)
8026498aff2SJonas Devlieghere{
8036498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
8046498aff2SJonas Devlieghere
8056498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, implementor);
8066498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index");
8076498aff2SJonas Devlieghere
8086498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
8096498aff2SJonas Devlieghere        return nullptr;
8106498aff2SJonas Devlieghere
8116498aff2SJonas Devlieghere    PythonObject result = pfunc(PythonInteger(idx));
8126498aff2SJonas Devlieghere
8136498aff2SJonas Devlieghere    if (!result.IsAllocated())
8146498aff2SJonas Devlieghere        return nullptr;
8156498aff2SJonas Devlieghere
8166498aff2SJonas Devlieghere    lldb::SBValue* sbvalue_ptr = nullptr;
8176498aff2SJonas Devlieghere    if (SWIG_ConvertPtr(result.get(), (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
8186498aff2SJonas Devlieghere        return nullptr;
8196498aff2SJonas Devlieghere
8206498aff2SJonas Devlieghere    if (sbvalue_ptr == nullptr)
8216498aff2SJonas Devlieghere        return nullptr;
8226498aff2SJonas Devlieghere
8236498aff2SJonas Devlieghere    return result.release();
8246498aff2SJonas Devlieghere}
8256498aff2SJonas Devlieghere
8266498aff2SJonas DevlieghereSWIGEXPORT int
8276498aff2SJonas DevlieghereLLDBSwigPython_GetIndexOfChildWithName
8286498aff2SJonas Devlieghere(
8296498aff2SJonas Devlieghere    PyObject *implementor,
8306498aff2SJonas Devlieghere    const char* child_name
8316498aff2SJonas Devlieghere)
8326498aff2SJonas Devlieghere{
8336498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
8346498aff2SJonas Devlieghere
8356498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, implementor);
8366498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>("get_child_index");
8376498aff2SJonas Devlieghere
8386498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
8396498aff2SJonas Devlieghere        return UINT32_MAX;
8406498aff2SJonas Devlieghere
84152712d3fSLawrence D'Anna    llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name));
8426498aff2SJonas Devlieghere
84352712d3fSLawrence D'Anna    long long retval = unwrapOrSetPythonException(As<long long>(std::move(result)));
84452712d3fSLawrence D'Anna
84552712d3fSLawrence D'Anna    if (PyErr_Occurred()) {
84652712d3fSLawrence D'Anna        PyErr_Clear(); // FIXME print this? do something else
8476498aff2SJonas Devlieghere        return UINT32_MAX;
84852712d3fSLawrence D'Anna    }
8496498aff2SJonas Devlieghere
8506498aff2SJonas Devlieghere    if (retval >= 0)
8516498aff2SJonas Devlieghere        return (uint32_t)retval;
8526498aff2SJonas Devlieghere
8536498aff2SJonas Devlieghere    return UINT32_MAX;
8546498aff2SJonas Devlieghere}
8556498aff2SJonas Devlieghere
8566498aff2SJonas DevlieghereSWIGEXPORT bool
8576498aff2SJonas DevlieghereLLDBSwigPython_UpdateSynthProviderInstance
8586498aff2SJonas Devlieghere(
8596498aff2SJonas Devlieghere    PyObject *implementor
8606498aff2SJonas Devlieghere)
8616498aff2SJonas Devlieghere{
8626498aff2SJonas Devlieghere    bool ret_val = false;
8636498aff2SJonas Devlieghere
8646498aff2SJonas Devlieghere    static char callee_name[] = "update";
8656498aff2SJonas Devlieghere
8666498aff2SJonas Devlieghere    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name);
8676498aff2SJonas Devlieghere
8686498aff2SJonas Devlieghere    if (py_return == Py_True)
8696498aff2SJonas Devlieghere        ret_val = true;
8706498aff2SJonas Devlieghere
8716498aff2SJonas Devlieghere    Py_XDECREF(py_return);
8726498aff2SJonas Devlieghere
8736498aff2SJonas Devlieghere    return ret_val;
8746498aff2SJonas Devlieghere}
8756498aff2SJonas Devlieghere
8766498aff2SJonas DevlieghereSWIGEXPORT bool
8776498aff2SJonas DevlieghereLLDBSwigPython_MightHaveChildrenSynthProviderInstance
8786498aff2SJonas Devlieghere(
8796498aff2SJonas Devlieghere    PyObject *implementor
8806498aff2SJonas Devlieghere)
8816498aff2SJonas Devlieghere{
8826498aff2SJonas Devlieghere    bool ret_val = false;
8836498aff2SJonas Devlieghere
8846498aff2SJonas Devlieghere    static char callee_name[] = "has_children";
8856498aff2SJonas Devlieghere
8866498aff2SJonas Devlieghere    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True);
8876498aff2SJonas Devlieghere
8886498aff2SJonas Devlieghere    if (py_return == Py_True)
8896498aff2SJonas Devlieghere        ret_val = true;
8906498aff2SJonas Devlieghere
8916498aff2SJonas Devlieghere    Py_XDECREF(py_return);
8926498aff2SJonas Devlieghere
8936498aff2SJonas Devlieghere    return ret_val;
8946498aff2SJonas Devlieghere}
8956498aff2SJonas Devlieghere
8966498aff2SJonas DevlieghereSWIGEXPORT PyObject*
8976498aff2SJonas DevlieghereLLDBSwigPython_GetValueSynthProviderInstance
8986498aff2SJonas Devlieghere(
8996498aff2SJonas Devlieghere    PyObject *implementor
9006498aff2SJonas Devlieghere)
9016498aff2SJonas Devlieghere{
9026498aff2SJonas Devlieghere    PyObject* ret_val = nullptr;
9036498aff2SJonas Devlieghere
9046498aff2SJonas Devlieghere    static char callee_name[] = "get_value";
9056498aff2SJonas Devlieghere
9066498aff2SJonas Devlieghere    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_None);
9076498aff2SJonas Devlieghere
9086498aff2SJonas Devlieghere    if (py_return == Py_None || py_return == nullptr)
9096498aff2SJonas Devlieghere        ret_val = nullptr;
9106498aff2SJonas Devlieghere
9116498aff2SJonas Devlieghere    lldb::SBValue* sbvalue_ptr = NULL;
9126498aff2SJonas Devlieghere
9136498aff2SJonas Devlieghere    if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
9146498aff2SJonas Devlieghere        ret_val = nullptr;
9156498aff2SJonas Devlieghere    else if (sbvalue_ptr == NULL)
9166498aff2SJonas Devlieghere        ret_val = nullptr;
9176498aff2SJonas Devlieghere    else
9186498aff2SJonas Devlieghere        ret_val = py_return;
9196498aff2SJonas Devlieghere
9206498aff2SJonas Devlieghere    Py_XDECREF(py_return);
9216498aff2SJonas Devlieghere    return ret_val;
9226498aff2SJonas Devlieghere}
9236498aff2SJonas Devlieghere
9246498aff2SJonas DevlieghereSWIGEXPORT void*
9251f6a57c1SMed Ismail BennaniLLDBSWIGPython_CastPyObjectToSBData
9261f6a57c1SMed Ismail Bennani(
9271f6a57c1SMed Ismail Bennani    PyObject* data
9281f6a57c1SMed Ismail Bennani)
9291f6a57c1SMed Ismail Bennani{
9301f6a57c1SMed Ismail Bennani    lldb::SBData* sb_ptr = nullptr;
9311f6a57c1SMed Ismail Bennani
9321f6a57c1SMed Ismail Bennani    int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0);
9331f6a57c1SMed Ismail Bennani
9341f6a57c1SMed Ismail Bennani    if (valid_cast == -1)
9351f6a57c1SMed Ismail Bennani        return NULL;
9361f6a57c1SMed Ismail Bennani
9371f6a57c1SMed Ismail Bennani    return sb_ptr;
9381f6a57c1SMed Ismail Bennani}
9391f6a57c1SMed Ismail Bennani
9401f6a57c1SMed Ismail Bennani
9411f6a57c1SMed Ismail BennaniSWIGEXPORT void*
9421f6a57c1SMed Ismail BennaniLLDBSWIGPython_CastPyObjectToSBError
9431f6a57c1SMed Ismail Bennani(
9441f6a57c1SMed Ismail Bennani    PyObject* data
9451f6a57c1SMed Ismail Bennani)
9461f6a57c1SMed Ismail Bennani{
9471f6a57c1SMed Ismail Bennani    lldb::SBError* sb_ptr = nullptr;
9481f6a57c1SMed Ismail Bennani
9491f6a57c1SMed Ismail Bennani    int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0);
9501f6a57c1SMed Ismail Bennani
9511f6a57c1SMed Ismail Bennani    if (valid_cast == -1)
9521f6a57c1SMed Ismail Bennani        return NULL;
9531f6a57c1SMed Ismail Bennani
9541f6a57c1SMed Ismail Bennani    return sb_ptr;
9551f6a57c1SMed Ismail Bennani}
9561f6a57c1SMed Ismail Bennani
9571f6a57c1SMed Ismail Bennani
9581f6a57c1SMed Ismail BennaniSWIGEXPORT void*
9596498aff2SJonas DevlieghereLLDBSWIGPython_CastPyObjectToSBValue
9606498aff2SJonas Devlieghere(
9616498aff2SJonas Devlieghere    PyObject* data
9626498aff2SJonas Devlieghere)
9636498aff2SJonas Devlieghere{
9646498aff2SJonas Devlieghere    lldb::SBValue* sb_ptr = NULL;
9656498aff2SJonas Devlieghere
9666498aff2SJonas Devlieghere    int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
9676498aff2SJonas Devlieghere
9686498aff2SJonas Devlieghere    if (valid_cast == -1)
9696498aff2SJonas Devlieghere        return NULL;
9706498aff2SJonas Devlieghere
9716498aff2SJonas Devlieghere    return sb_ptr;
9726498aff2SJonas Devlieghere}
9736498aff2SJonas Devlieghere
974a758c9f7SMed Ismail BennaniSWIGEXPORT void*
975a758c9f7SMed Ismail BennaniLLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo
976a758c9f7SMed Ismail Bennani(
977a758c9f7SMed Ismail Bennani    PyObject* data
978a758c9f7SMed Ismail Bennani)
979a758c9f7SMed Ismail Bennani{
980a758c9f7SMed Ismail Bennani    lldb::SBMemoryRegionInfo* sb_ptr = NULL;
981a758c9f7SMed Ismail Bennani
982a758c9f7SMed Ismail Bennani    int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0);
983a758c9f7SMed Ismail Bennani
984a758c9f7SMed Ismail Bennani    if (valid_cast == -1)
985a758c9f7SMed Ismail Bennani        return NULL;
986a758c9f7SMed Ismail Bennani
987a758c9f7SMed Ismail Bennani    return sb_ptr;
988a758c9f7SMed Ismail Bennani}
989a758c9f7SMed Ismail Bennani
9906498aff2SJonas DevlieghereSWIGEXPORT bool
9916498aff2SJonas DevlieghereLLDBSwigPythonCallCommand
9926498aff2SJonas Devlieghere(
9936498aff2SJonas Devlieghere    const char *python_function_name,
9946498aff2SJonas Devlieghere    const char *session_dictionary_name,
9956498aff2SJonas Devlieghere    lldb::DebuggerSP& debugger,
9966498aff2SJonas Devlieghere    const char* args,
9976498aff2SJonas Devlieghere    lldb_private::CommandReturnObject& cmd_retobj,
9986498aff2SJonas Devlieghere    lldb::ExecutionContextRefSP exe_ctx_ref_sp
9996498aff2SJonas Devlieghere)
10006498aff2SJonas Devlieghere{
10016498aff2SJonas Devlieghere    lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
10026498aff2SJonas Devlieghere    lldb::SBDebugger debugger_sb(debugger);
10036498aff2SJonas Devlieghere    lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
10046498aff2SJonas Devlieghere
10056498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
10066498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
10076498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
10086498aff2SJonas Devlieghere
10096498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
10106498aff2SJonas Devlieghere        return false;
10116498aff2SJonas Devlieghere
10126498aff2SJonas Devlieghere    auto argc = pfunc.GetArgInfo();
10136498aff2SJonas Devlieghere    if (!argc) {
10146498aff2SJonas Devlieghere        llvm::consumeError(argc.takeError());
10156498aff2SJonas Devlieghere        return false;
10166498aff2SJonas Devlieghere    }
10176498aff2SJonas Devlieghere    PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
10186498aff2SJonas Devlieghere    PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb));
1019*f1127914SPavel Labath    PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(cmd_retobj_sb));
10206498aff2SJonas Devlieghere
10216498aff2SJonas Devlieghere    if (argc.get().max_positional_args < 5u)
10226498aff2SJonas Devlieghere        pfunc(debugger_arg, PythonString(args), cmd_retobj_arg, dict);
10236498aff2SJonas Devlieghere    else
10246498aff2SJonas Devlieghere        pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg, dict);
10256498aff2SJonas Devlieghere
10266498aff2SJonas Devlieghere    return true;
10276498aff2SJonas Devlieghere}
10286498aff2SJonas Devlieghere
10296498aff2SJonas DevlieghereSWIGEXPORT bool
10306498aff2SJonas DevlieghereLLDBSwigPythonCallCommandObject
10316498aff2SJonas Devlieghere(
10326498aff2SJonas Devlieghere    PyObject *implementor,
10336498aff2SJonas Devlieghere    lldb::DebuggerSP& debugger,
10346498aff2SJonas Devlieghere    const char* args,
10356498aff2SJonas Devlieghere    lldb_private::CommandReturnObject& cmd_retobj,
10366498aff2SJonas Devlieghere    lldb::ExecutionContextRefSP exe_ctx_ref_sp
10376498aff2SJonas Devlieghere)
10386498aff2SJonas Devlieghere{
10396498aff2SJonas Devlieghere    lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
10406498aff2SJonas Devlieghere    lldb::SBDebugger debugger_sb(debugger);
10416498aff2SJonas Devlieghere    lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
10426498aff2SJonas Devlieghere
10436498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
10446498aff2SJonas Devlieghere
10456498aff2SJonas Devlieghere    PythonObject self(PyRefType::Borrowed, implementor);
10466498aff2SJonas Devlieghere    auto pfunc = self.ResolveName<PythonCallable>("__call__");
10476498aff2SJonas Devlieghere
10486498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
10496498aff2SJonas Devlieghere        return false;
10506498aff2SJonas Devlieghere
10516498aff2SJonas Devlieghere    PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
10526498aff2SJonas Devlieghere    PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb));
1053*f1127914SPavel Labath    PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(cmd_retobj_sb));
10546498aff2SJonas Devlieghere
10556498aff2SJonas Devlieghere    pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg);
10566498aff2SJonas Devlieghere
10576498aff2SJonas Devlieghere    return true;
10586498aff2SJonas Devlieghere}
10596498aff2SJonas Devlieghere
10606498aff2SJonas DevlieghereSWIGEXPORT void*
10616498aff2SJonas DevlieghereLLDBSWIGPythonCreateOSPlugin
10626498aff2SJonas Devlieghere(
10636498aff2SJonas Devlieghere    const char *python_class_name,
10646498aff2SJonas Devlieghere    const char *session_dictionary_name,
10656498aff2SJonas Devlieghere    const lldb::ProcessSP& process_sp
10666498aff2SJonas Devlieghere)
10676498aff2SJonas Devlieghere{
10686498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
10696498aff2SJonas Devlieghere        Py_RETURN_NONE;
10706498aff2SJonas Devlieghere
10716498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
10726498aff2SJonas Devlieghere
10736498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
10746498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
10756498aff2SJonas Devlieghere
10766498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
10776498aff2SJonas Devlieghere        Py_RETURN_NONE;
10786498aff2SJonas Devlieghere
1079*f1127914SPavel Labath    // FIXME: This leaks the SBProcess object
10806498aff2SJonas Devlieghere    lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp);
1081*f1127914SPavel Labath    PythonObject process_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*process_sb));
10826498aff2SJonas Devlieghere    if (!process_arg.IsAllocated())
10836498aff2SJonas Devlieghere        Py_RETURN_NONE;
10846498aff2SJonas Devlieghere
10856498aff2SJonas Devlieghere    auto result = pfunc(process_arg);
10866498aff2SJonas Devlieghere
10876498aff2SJonas Devlieghere    if (result.IsAllocated())
10886498aff2SJonas Devlieghere        return result.release();
10896498aff2SJonas Devlieghere
10906498aff2SJonas Devlieghere    Py_RETURN_NONE;
10916498aff2SJonas Devlieghere}
10926498aff2SJonas Devlieghere
10936498aff2SJonas DevlieghereSWIGEXPORT void*
10946498aff2SJonas DevlieghereLLDBSWIGPython_CreateFrameRecognizer
10956498aff2SJonas Devlieghere(
10966498aff2SJonas Devlieghere    const char *python_class_name,
10976498aff2SJonas Devlieghere    const char *session_dictionary_name
10986498aff2SJonas Devlieghere)
10996498aff2SJonas Devlieghere{
11006498aff2SJonas Devlieghere    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
11016498aff2SJonas Devlieghere        Py_RETURN_NONE;
11026498aff2SJonas Devlieghere
11036498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
11046498aff2SJonas Devlieghere
11056498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
11066498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
11076498aff2SJonas Devlieghere
11086498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
11096498aff2SJonas Devlieghere        Py_RETURN_NONE;
11106498aff2SJonas Devlieghere
11116498aff2SJonas Devlieghere    auto result = pfunc();
11126498aff2SJonas Devlieghere
11136498aff2SJonas Devlieghere    if (result.IsAllocated())
11146498aff2SJonas Devlieghere        return result.release();
11156498aff2SJonas Devlieghere
11166498aff2SJonas Devlieghere    Py_RETURN_NONE;
11176498aff2SJonas Devlieghere}
11186498aff2SJonas Devlieghere
11196498aff2SJonas DevlieghereSWIGEXPORT PyObject*
11206498aff2SJonas DevlieghereLLDBSwigPython_GetRecognizedArguments
11216498aff2SJonas Devlieghere(
11226498aff2SJonas Devlieghere    PyObject *implementor,
11236498aff2SJonas Devlieghere    const lldb::StackFrameSP& frame_sp
11246498aff2SJonas Devlieghere)
11256498aff2SJonas Devlieghere{
11266498aff2SJonas Devlieghere    static char callee_name[] = "get_recognized_arguments";
11276498aff2SJonas Devlieghere
11286498aff2SJonas Devlieghere    lldb::SBFrame frame_sb(frame_sp);
11296498aff2SJonas Devlieghere    PyObject *arg = SBTypeToSWIGWrapper(frame_sb);
11306498aff2SJonas Devlieghere
11316498aff2SJonas Devlieghere    PythonString str(callee_name);
11326498aff2SJonas Devlieghere    PyObject* result = PyObject_CallMethodObjArgs(implementor, str.get(), arg,
11336498aff2SJonas Devlieghere                                                  NULL);
11346498aff2SJonas Devlieghere    return result;
11356498aff2SJonas Devlieghere}
11366498aff2SJonas Devlieghere
11376498aff2SJonas DevlieghereSWIGEXPORT void*
11386498aff2SJonas DevlieghereLLDBSWIGPython_GetDynamicSetting (void* module, const char* setting, const lldb::TargetSP& target_sp)
11396498aff2SJonas Devlieghere{
11406498aff2SJonas Devlieghere    if (!module || !setting)
11416498aff2SJonas Devlieghere        Py_RETURN_NONE;
11426498aff2SJonas Devlieghere
11436498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
11446498aff2SJonas Devlieghere    PythonObject py_module(PyRefType::Borrowed, (PyObject *)module);
11456498aff2SJonas Devlieghere    auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting");
11466498aff2SJonas Devlieghere
11476498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
11486498aff2SJonas Devlieghere        Py_RETURN_NONE;
11496498aff2SJonas Devlieghere
11506498aff2SJonas Devlieghere    lldb::SBTarget target_sb(target_sp);
11516498aff2SJonas Devlieghere    PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(target_sb));
11526498aff2SJonas Devlieghere    auto result = pfunc(target_arg, PythonString(setting));
11536498aff2SJonas Devlieghere
11546498aff2SJonas Devlieghere    return result.release();
11556498aff2SJonas Devlieghere}
11566498aff2SJonas Devlieghere
11576498aff2SJonas DevlieghereSWIGEXPORT bool
11586498aff2SJonas DevlieghereLLDBSWIGPythonRunScriptKeywordProcess
11596498aff2SJonas Devlieghere(const char* python_function_name,
11606498aff2SJonas Devlieghereconst char* session_dictionary_name,
11616498aff2SJonas Devliegherelldb::ProcessSP& process,
11626498aff2SJonas Devliegherestd::string& output)
11636498aff2SJonas Devlieghere
11646498aff2SJonas Devlieghere{
11656498aff2SJonas Devlieghere    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
11666498aff2SJonas Devlieghere        return false;
11676498aff2SJonas Devlieghere
11686498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
11696498aff2SJonas Devlieghere
11706498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
11716498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
11726498aff2SJonas Devlieghere
11736498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
11746498aff2SJonas Devlieghere        return false;
11756498aff2SJonas Devlieghere
11766498aff2SJonas Devlieghere    lldb::SBProcess process_sb(process);
11776498aff2SJonas Devlieghere    PythonObject process_arg(PyRefType::Owned, SBTypeToSWIGWrapper(process_sb));
11786498aff2SJonas Devlieghere    auto result = pfunc(process_arg, dict);
11796498aff2SJonas Devlieghere
11806498aff2SJonas Devlieghere    output = result.Str().GetString().str();
11816498aff2SJonas Devlieghere
11826498aff2SJonas Devlieghere    return true;
11836498aff2SJonas Devlieghere}
11846498aff2SJonas Devlieghere
11856498aff2SJonas DevlieghereSWIGEXPORT bool
11866498aff2SJonas DevlieghereLLDBSWIGPythonRunScriptKeywordThread
11876498aff2SJonas Devlieghere(const char* python_function_name,
11886498aff2SJonas Devlieghereconst char* session_dictionary_name,
11896498aff2SJonas Devliegherelldb::ThreadSP& thread,
11906498aff2SJonas Devliegherestd::string& output)
11916498aff2SJonas Devlieghere
11926498aff2SJonas Devlieghere{
11936498aff2SJonas Devlieghere    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
11946498aff2SJonas Devlieghere        return false;
11956498aff2SJonas Devlieghere
11966498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
11976498aff2SJonas Devlieghere
11986498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
11996498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
12006498aff2SJonas Devlieghere
12016498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
12026498aff2SJonas Devlieghere        return false;
12036498aff2SJonas Devlieghere
12046498aff2SJonas Devlieghere    lldb::SBThread thread_sb(thread);
12056498aff2SJonas Devlieghere    PythonObject thread_arg(PyRefType::Owned, SBTypeToSWIGWrapper(thread_sb));
12066498aff2SJonas Devlieghere    auto result = pfunc(thread_arg, dict);
12076498aff2SJonas Devlieghere
12086498aff2SJonas Devlieghere    output = result.Str().GetString().str();
12096498aff2SJonas Devlieghere
12106498aff2SJonas Devlieghere    return true;
12116498aff2SJonas Devlieghere}
12126498aff2SJonas Devlieghere
12136498aff2SJonas DevlieghereSWIGEXPORT bool
12146498aff2SJonas DevlieghereLLDBSWIGPythonRunScriptKeywordTarget
12156498aff2SJonas Devlieghere(const char* python_function_name,
12166498aff2SJonas Devlieghereconst char* session_dictionary_name,
12176498aff2SJonas Devliegherelldb::TargetSP& target,
12186498aff2SJonas Devliegherestd::string& output)
12196498aff2SJonas Devlieghere
12206498aff2SJonas Devlieghere{
12216498aff2SJonas Devlieghere    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
12226498aff2SJonas Devlieghere        return false;
12236498aff2SJonas Devlieghere
12246498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
12256498aff2SJonas Devlieghere
12266498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
12276498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name,dict);
12286498aff2SJonas Devlieghere
12296498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
12306498aff2SJonas Devlieghere        return false;
12316498aff2SJonas Devlieghere
12326498aff2SJonas Devlieghere    lldb::SBTarget target_sb(target);
12336498aff2SJonas Devlieghere    PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(target_sb));
12346498aff2SJonas Devlieghere    auto result = pfunc(target_arg, dict);
12356498aff2SJonas Devlieghere
12366498aff2SJonas Devlieghere    output = result.Str().GetString().str();
12376498aff2SJonas Devlieghere
12386498aff2SJonas Devlieghere    return true;
12396498aff2SJonas Devlieghere}
12406498aff2SJonas Devlieghere
12416498aff2SJonas DevlieghereSWIGEXPORT bool
12426498aff2SJonas DevlieghereLLDBSWIGPythonRunScriptKeywordFrame
12436498aff2SJonas Devlieghere(const char* python_function_name,
12446498aff2SJonas Devlieghereconst char* session_dictionary_name,
12456498aff2SJonas Devliegherelldb::StackFrameSP& frame,
12466498aff2SJonas Devliegherestd::string& output)
12476498aff2SJonas Devlieghere
12486498aff2SJonas Devlieghere{
12496498aff2SJonas Devlieghere    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
12506498aff2SJonas Devlieghere        return false;
12516498aff2SJonas Devlieghere
12526498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
12536498aff2SJonas Devlieghere
12546498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
12556498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name,dict);
12566498aff2SJonas Devlieghere
12576498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
12586498aff2SJonas Devlieghere        return false;
12596498aff2SJonas Devlieghere
12606498aff2SJonas Devlieghere    lldb::SBFrame frame_sb(frame);
12616498aff2SJonas Devlieghere    PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(frame_sb));
12626498aff2SJonas Devlieghere    auto result = pfunc(frame_arg, dict);
12636498aff2SJonas Devlieghere
12646498aff2SJonas Devlieghere    output = result.Str().GetString().str();
12656498aff2SJonas Devlieghere
12666498aff2SJonas Devlieghere    return true;
12676498aff2SJonas Devlieghere}
12686498aff2SJonas Devlieghere
12696498aff2SJonas DevlieghereSWIGEXPORT bool
12706498aff2SJonas DevlieghereLLDBSWIGPythonRunScriptKeywordValue
12716498aff2SJonas Devlieghere(const char* python_function_name,
12726498aff2SJonas Devlieghereconst char* session_dictionary_name,
12736498aff2SJonas Devliegherelldb::ValueObjectSP& value,
12746498aff2SJonas Devliegherestd::string& output)
12756498aff2SJonas Devlieghere
12766498aff2SJonas Devlieghere{
12776498aff2SJonas Devlieghere    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
12786498aff2SJonas Devlieghere        return false;
12796498aff2SJonas Devlieghere
12806498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
12816498aff2SJonas Devlieghere
12826498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
12836498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
12846498aff2SJonas Devlieghere
12856498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
12866498aff2SJonas Devlieghere        return false;
12876498aff2SJonas Devlieghere
12886498aff2SJonas Devlieghere    lldb::SBValue value_sb(value);
12896498aff2SJonas Devlieghere    PythonObject value_arg(PyRefType::Owned, SBTypeToSWIGWrapper(value_sb));
12906498aff2SJonas Devlieghere    auto result = pfunc(value_arg, dict);
12916498aff2SJonas Devlieghere
12926498aff2SJonas Devlieghere    output = result.Str().GetString().str();
12936498aff2SJonas Devlieghere
12946498aff2SJonas Devlieghere    return true;
12956498aff2SJonas Devlieghere}
12966498aff2SJonas Devlieghere
12976498aff2SJonas DevlieghereSWIGEXPORT bool
12986498aff2SJonas DevlieghereLLDBSwigPythonCallModuleInit
12996498aff2SJonas Devlieghere(
13006498aff2SJonas Devlieghere    const char *python_module_name,
13016498aff2SJonas Devlieghere    const char *session_dictionary_name,
13026498aff2SJonas Devlieghere    lldb::DebuggerSP& debugger
13036498aff2SJonas Devlieghere)
13046498aff2SJonas Devlieghere{
13056498aff2SJonas Devlieghere    std::string python_function_name_string = python_module_name;
13066498aff2SJonas Devlieghere    python_function_name_string += ".__lldb_init_module";
13076498aff2SJonas Devlieghere    const char* python_function_name = python_function_name_string.c_str();
13086498aff2SJonas Devlieghere
13096498aff2SJonas Devlieghere    PyErr_Cleaner py_err_cleaner(true);
13106498aff2SJonas Devlieghere
13116498aff2SJonas Devlieghere    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
13126498aff2SJonas Devlieghere    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
13136498aff2SJonas Devlieghere
13146498aff2SJonas Devlieghere    // This method is optional and need not exist.  So if we don't find it,
13156498aff2SJonas Devlieghere    // it's actually a success, not a failure.
13166498aff2SJonas Devlieghere    if (!pfunc.IsAllocated())
13176498aff2SJonas Devlieghere        return true;
13186498aff2SJonas Devlieghere
13196498aff2SJonas Devlieghere    lldb::SBDebugger debugger_sb(debugger);
13206498aff2SJonas Devlieghere    PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
13216498aff2SJonas Devlieghere    pfunc(debugger_arg, dict);
13226498aff2SJonas Devlieghere
13236498aff2SJonas Devlieghere    return true;
13246498aff2SJonas Devlieghere}
13256498aff2SJonas Devlieghere%}
13266498aff2SJonas Devlieghere
13276498aff2SJonas Devlieghere
13286498aff2SJonas Devlieghere%runtime %{
13296498aff2SJonas Devlieghere// Forward declaration to be inserted at the start of LLDBWrapPython.h
13306498aff2SJonas Devlieghere#include "lldb/API/SBDebugger.h"
13316498aff2SJonas Devlieghere#include "lldb/API/SBValue.h"
13326498aff2SJonas Devlieghere
13336498aff2SJonas DevlieghereSWIGEXPORT lldb::ValueObjectSP
13346498aff2SJonas DevlieghereLLDBSWIGPython_GetValueObjectSPFromSBValue (void* data)
13356498aff2SJonas Devlieghere{
13366498aff2SJonas Devlieghere    lldb::ValueObjectSP valobj_sp;
13376498aff2SJonas Devlieghere    if (data)
13386498aff2SJonas Devlieghere    {
13396498aff2SJonas Devlieghere        lldb::SBValue* sb_ptr = (lldb::SBValue *)data;
13406498aff2SJonas Devlieghere        valobj_sp = sb_ptr->GetSP();
13416498aff2SJonas Devlieghere    }
13426498aff2SJonas Devlieghere    return valobj_sp;
13436498aff2SJonas Devlieghere}
13446498aff2SJonas Devlieghere
13456498aff2SJonas Devlieghere#ifdef __cplusplus
13466498aff2SJonas Devlieghereextern "C" {
13476498aff2SJonas Devlieghere#endif
13486498aff2SJonas Devlieghere
13496498aff2SJonas Devliegherevoid LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton);
13506498aff2SJonas Devlieghere
13516498aff2SJonas Devlieghere#ifdef __cplusplus
13526498aff2SJonas Devlieghere}
13536498aff2SJonas Devlieghere#endif
13546498aff2SJonas Devlieghere%}
13556498aff2SJonas Devlieghere
13566498aff2SJonas Devlieghere%wrapper %{
13576498aff2SJonas Devlieghere
13586498aff2SJonas Devlieghere
13596498aff2SJonas Devlieghere// For the LogOutputCallback functions
13606498aff2SJonas Devliegherevoid LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) {
13616498aff2SJonas Devlieghere    if (baton != Py_None) {
13626498aff2SJonas Devlieghere      SWIG_PYTHON_THREAD_BEGIN_BLOCK;
13636498aff2SJonas Devlieghere      PyObject *result = PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str);
13646498aff2SJonas Devlieghere	  Py_XDECREF(result);
13656498aff2SJonas Devlieghere      SWIG_PYTHON_THREAD_END_BLOCK;
13666498aff2SJonas Devlieghere    }
13676498aff2SJonas Devlieghere}
13686498aff2SJonas Devlieghere%}
1369