16498aff2SJonas Devlieghere%header %{ 26498aff2SJonas Devlieghere 36498aff2SJonas Devliegheretemplate <typename T> 46498aff2SJonas DevliegherePyObject * 56498aff2SJonas DevlieghereSBTypeToSWIGWrapper (T* item); 66498aff2SJonas Devlieghere 76498aff2SJonas Devlieghereclass PyErr_Cleaner 86498aff2SJonas Devlieghere{ 96498aff2SJonas Devliegherepublic: 106498aff2SJonas Devlieghere PyErr_Cleaner(bool print=false) : 116498aff2SJonas Devlieghere m_print(print) 126498aff2SJonas Devlieghere { 136498aff2SJonas Devlieghere } 146498aff2SJonas Devlieghere 156498aff2SJonas Devlieghere ~PyErr_Cleaner() 166498aff2SJonas Devlieghere { 176498aff2SJonas Devlieghere if (PyErr_Occurred()) 186498aff2SJonas Devlieghere { 196498aff2SJonas Devlieghere if(m_print && !PyErr_ExceptionMatches(PyExc_SystemExit)) 206498aff2SJonas Devlieghere PyErr_Print(); 216498aff2SJonas Devlieghere PyErr_Clear(); 226498aff2SJonas Devlieghere } 236498aff2SJonas Devlieghere } 246498aff2SJonas Devlieghere 256498aff2SJonas Devlieghereprivate: 266498aff2SJonas Devlieghere bool m_print; 276498aff2SJonas Devlieghere}; 286498aff2SJonas Devlieghere 296498aff2SJonas Devlieghere%} 306498aff2SJonas Devlieghere 316498aff2SJonas Devlieghere%wrapper %{ 326498aff2SJonas Devlieghere 336498aff2SJonas Devlieghere// resolve a dotted Python name in the form 346498aff2SJonas Devlieghere// foo.bar.baz.Foobar to an actual Python object 356498aff2SJonas Devlieghere// if pmodule is NULL, the __main__ module will be used 366498aff2SJonas Devlieghere// as the starting point for the search 376498aff2SJonas Devlieghere 386498aff2SJonas Devlieghere 396498aff2SJonas Devlieghere// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...) 406498aff2SJonas Devlieghere// and is used when a script command is attached to a breakpoint for execution. 416498aff2SJonas Devlieghere 42daf36998SDave Lee#pragma clang diagnostic push 43daf36998SDave Lee#pragma clang diagnostic ignored "-Wreturn-type-c-linkage" 44daf36998SDave Lee 45daf36998SDave Lee// Disable warning C4190: 'LLDBSwigPythonBreakpointCallbackFunction' has 46daf36998SDave Lee// C-linkage specified, but returns UDT 'llvm::Expected<bool>' which is 47daf36998SDave Lee// incompatible with C 48daf36998SDave Lee#if _MSC_VER 49daf36998SDave Lee#pragma warning (push) 50daf36998SDave Lee#pragma warning (disable : 4190) 51daf36998SDave Lee#endif 52daf36998SDave Lee 536498aff2SJonas DevlieghereSWIGEXPORT llvm::Expected<bool> 546498aff2SJonas DevlieghereLLDBSwigPythonBreakpointCallbackFunction 556498aff2SJonas Devlieghere( 566498aff2SJonas Devlieghere const char *python_function_name, 576498aff2SJonas Devlieghere const char *session_dictionary_name, 586498aff2SJonas Devlieghere const lldb::StackFrameSP& frame_sp, 596498aff2SJonas Devlieghere const lldb::BreakpointLocationSP& bp_loc_sp, 606498aff2SJonas Devlieghere lldb_private::StructuredDataImpl *args_impl 616498aff2SJonas Devlieghere) 626498aff2SJonas Devlieghere{ 636498aff2SJonas Devlieghere using namespace llvm; 646498aff2SJonas Devlieghere 656498aff2SJonas Devlieghere lldb::SBFrame sb_frame (frame_sp); 666498aff2SJonas Devlieghere lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp); 676498aff2SJonas Devlieghere 686498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 696498aff2SJonas Devlieghere auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 706498aff2SJonas Devlieghere auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 716498aff2SJonas Devlieghere 726498aff2SJonas Devlieghere unsigned max_positional_args; 736498aff2SJonas Devlieghere if (auto arg_info = pfunc.GetArgInfo()) 746498aff2SJonas Devlieghere max_positional_args = arg_info.get().max_positional_args; 756498aff2SJonas Devlieghere else 766498aff2SJonas Devlieghere return arg_info.takeError(); 776498aff2SJonas Devlieghere 786498aff2SJonas Devlieghere PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame)); 796498aff2SJonas Devlieghere PythonObject bp_loc_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_bp_loc)); 806498aff2SJonas Devlieghere 816498aff2SJonas Devlieghere auto result = [&] () -> Expected<PythonObject> { 826498aff2SJonas Devlieghere // If the called function doesn't take extra_args, drop them here: 836498aff2SJonas Devlieghere if (max_positional_args < 4) { 846498aff2SJonas Devlieghere return pfunc.Call(frame_arg, bp_loc_arg, dict); 856498aff2SJonas Devlieghere } else { 866498aff2SJonas Devlieghere lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl); 876498aff2SJonas Devlieghere PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(args_value)); 886498aff2SJonas Devlieghere return pfunc.Call(frame_arg, bp_loc_arg, args_arg, dict); 896498aff2SJonas Devlieghere } 906498aff2SJonas Devlieghere } (); 916498aff2SJonas Devlieghere 926498aff2SJonas Devlieghere if (!result) 936498aff2SJonas Devlieghere return result.takeError(); 946498aff2SJonas Devlieghere 956498aff2SJonas Devlieghere // Only False counts as false! 966498aff2SJonas Devlieghere return result.get().get() != Py_False; 976498aff2SJonas Devlieghere} 986498aff2SJonas Devlieghere 99daf36998SDave Lee#if _MSC_VER 100daf36998SDave Lee#pragma warning (pop) 101daf36998SDave Lee#endif 102daf36998SDave Lee 103daf36998SDave Lee#pragma clang diagnostic pop 104daf36998SDave Lee 1056498aff2SJonas Devlieghere// This function is called by lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...) 1066498aff2SJonas Devlieghere// and is used when a script command is attached to a watchpoint for execution. 1076498aff2SJonas Devlieghere 1086498aff2SJonas DevlieghereSWIGEXPORT bool 1096498aff2SJonas DevlieghereLLDBSwigPythonWatchpointCallbackFunction 1106498aff2SJonas Devlieghere( 1116498aff2SJonas Devlieghere const char *python_function_name, 1126498aff2SJonas Devlieghere const char *session_dictionary_name, 1136498aff2SJonas Devlieghere const lldb::StackFrameSP& frame_sp, 1146498aff2SJonas Devlieghere const lldb::WatchpointSP& wp_sp 1156498aff2SJonas Devlieghere) 1166498aff2SJonas Devlieghere{ 1176498aff2SJonas Devlieghere lldb::SBFrame sb_frame (frame_sp); 1186498aff2SJonas Devlieghere lldb::SBWatchpoint sb_wp(wp_sp); 1196498aff2SJonas Devlieghere 1206498aff2SJonas Devlieghere bool stop_at_watchpoint = true; 1216498aff2SJonas Devlieghere 1226498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 1236498aff2SJonas Devlieghere 1246498aff2SJonas Devlieghere auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 1256498aff2SJonas Devlieghere auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 1266498aff2SJonas Devlieghere 1276498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 1286498aff2SJonas Devlieghere return stop_at_watchpoint; 1296498aff2SJonas Devlieghere 1306498aff2SJonas Devlieghere PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame)); 1316498aff2SJonas Devlieghere PythonObject wp_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_wp)); 1326498aff2SJonas Devlieghere PythonObject result = pfunc(frame_arg, wp_arg, dict); 1336498aff2SJonas Devlieghere 1346498aff2SJonas Devlieghere if (result.get() == Py_False) 1356498aff2SJonas Devlieghere stop_at_watchpoint = false; 1366498aff2SJonas Devlieghere 1376498aff2SJonas Devlieghere return stop_at_watchpoint; 1386498aff2SJonas Devlieghere} 1396498aff2SJonas Devlieghere 1406498aff2SJonas DevlieghereSWIGEXPORT bool 1416498aff2SJonas DevlieghereLLDBSwigPythonCallTypeScript 1426498aff2SJonas Devlieghere( 1436498aff2SJonas Devlieghere const char *python_function_name, 1446498aff2SJonas Devlieghere const void *session_dictionary, 1456498aff2SJonas Devlieghere const lldb::ValueObjectSP& valobj_sp, 1466498aff2SJonas Devlieghere void** pyfunct_wrapper, 1476498aff2SJonas Devlieghere const lldb::TypeSummaryOptionsSP& options_sp, 1486498aff2SJonas Devlieghere std::string& retval 1496498aff2SJonas Devlieghere) 1506498aff2SJonas Devlieghere{ 1516498aff2SJonas Devlieghere lldb::SBValue sb_value (valobj_sp); 1526498aff2SJonas Devlieghere lldb::SBTypeSummaryOptions sb_options(options_sp.get()); 1536498aff2SJonas Devlieghere 1546498aff2SJonas Devlieghere retval.clear(); 1556498aff2SJonas Devlieghere 1566498aff2SJonas Devlieghere if (!python_function_name || !session_dictionary) 1576498aff2SJonas Devlieghere return false; 1586498aff2SJonas Devlieghere 1596498aff2SJonas Devlieghere PyObject *pfunc_impl = nullptr; 1606498aff2SJonas Devlieghere 1616498aff2SJonas Devlieghere if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper)) 1626498aff2SJonas Devlieghere { 1636498aff2SJonas Devlieghere pfunc_impl = (PyObject*)(*pyfunct_wrapper); 1646498aff2SJonas Devlieghere if (pfunc_impl->ob_refcnt == 1) 1656498aff2SJonas Devlieghere { 1666498aff2SJonas Devlieghere Py_XDECREF(pfunc_impl); 1676498aff2SJonas Devlieghere pfunc_impl = NULL; 1686498aff2SJonas Devlieghere } 1696498aff2SJonas Devlieghere } 1706498aff2SJonas Devlieghere 1716498aff2SJonas Devlieghere PyObject *py_dict = (PyObject*)session_dictionary; 1726498aff2SJonas Devlieghere if (!PythonDictionary::Check(py_dict)) 1736498aff2SJonas Devlieghere return true; 1746498aff2SJonas Devlieghere 1756498aff2SJonas Devlieghere PythonDictionary dict(PyRefType::Borrowed, py_dict); 1766498aff2SJonas Devlieghere 1776498aff2SJonas Devlieghere PyErr_Cleaner pyerr_cleanup(true); // show Python errors 1786498aff2SJonas Devlieghere 1796498aff2SJonas Devlieghere PythonCallable pfunc(PyRefType::Borrowed, pfunc_impl); 1806498aff2SJonas Devlieghere 1816498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 1826498aff2SJonas Devlieghere { 1836498aff2SJonas Devlieghere pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 1846498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 1856498aff2SJonas Devlieghere return false; 1866498aff2SJonas Devlieghere 1876498aff2SJonas Devlieghere if (pyfunct_wrapper) 1886498aff2SJonas Devlieghere { 1896498aff2SJonas Devlieghere *pyfunct_wrapper = pfunc.get(); 1906498aff2SJonas Devlieghere Py_XINCREF(pfunc.get()); 1916498aff2SJonas Devlieghere } 1926498aff2SJonas Devlieghere } 1936498aff2SJonas Devlieghere 1946498aff2SJonas Devlieghere PythonObject result; 1956498aff2SJonas Devlieghere auto argc = pfunc.GetArgInfo(); 1966498aff2SJonas Devlieghere if (!argc) { 1976498aff2SJonas Devlieghere llvm::consumeError(argc.takeError()); 1986498aff2SJonas Devlieghere return false; 1996498aff2SJonas Devlieghere } 2006498aff2SJonas Devlieghere 2016498aff2SJonas Devlieghere PythonObject value_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_value)); 2026498aff2SJonas Devlieghere PythonObject options_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_options)); 2036498aff2SJonas Devlieghere 2046498aff2SJonas Devlieghere if (argc.get().max_positional_args < 3) 2056498aff2SJonas Devlieghere result = pfunc(value_arg,dict); 2066498aff2SJonas Devlieghere else 2076498aff2SJonas Devlieghere result = pfunc(value_arg,dict,options_arg); 2086498aff2SJonas Devlieghere 2096498aff2SJonas Devlieghere retval = result.Str().GetString().str(); 2106498aff2SJonas Devlieghere 2116498aff2SJonas Devlieghere return true; 2126498aff2SJonas Devlieghere} 2136498aff2SJonas Devlieghere 2146498aff2SJonas DevlieghereSWIGEXPORT void* 2156498aff2SJonas DevlieghereLLDBSwigPythonCreateSyntheticProvider 2166498aff2SJonas Devlieghere( 2176498aff2SJonas Devlieghere const char *python_class_name, 2186498aff2SJonas Devlieghere const char *session_dictionary_name, 2196498aff2SJonas Devlieghere const lldb::ValueObjectSP& valobj_sp 2206498aff2SJonas Devlieghere) 2216498aff2SJonas Devlieghere{ 2226498aff2SJonas Devlieghere if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) 2236498aff2SJonas Devlieghere Py_RETURN_NONE; 2246498aff2SJonas Devlieghere 2256498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 2266498aff2SJonas Devlieghere 2276498aff2SJonas Devlieghere auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 2286498aff2SJonas Devlieghere auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name,dict); 2296498aff2SJonas Devlieghere 2306498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 2316498aff2SJonas Devlieghere Py_RETURN_NONE; 2326498aff2SJonas Devlieghere 2336498aff2SJonas Devlieghere // I do not want the SBValue to be deallocated when going out of scope because python 2346498aff2SJonas Devlieghere // has ownership of it and will manage memory for this object by itself 2356498aff2SJonas Devlieghere lldb::SBValue *sb_value = new lldb::SBValue(valobj_sp); 2366498aff2SJonas Devlieghere sb_value->SetPreferSyntheticValue(false); 2376498aff2SJonas Devlieghere 2386498aff2SJonas Devlieghere PythonObject val_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_value)); 2396498aff2SJonas Devlieghere if (!val_arg.IsAllocated()) 2406498aff2SJonas Devlieghere Py_RETURN_NONE; 2416498aff2SJonas Devlieghere 2426498aff2SJonas Devlieghere PythonObject result = pfunc(val_arg, dict); 2436498aff2SJonas Devlieghere 2446498aff2SJonas Devlieghere if (result.IsAllocated()) 2456498aff2SJonas Devlieghere return result.release(); 2466498aff2SJonas Devlieghere 2476498aff2SJonas Devlieghere Py_RETURN_NONE; 2486498aff2SJonas Devlieghere} 2496498aff2SJonas Devlieghere 2506498aff2SJonas DevlieghereSWIGEXPORT void* 2516498aff2SJonas DevlieghereLLDBSwigPythonCreateCommandObject 2526498aff2SJonas Devlieghere( 2536498aff2SJonas Devlieghere const char *python_class_name, 2546498aff2SJonas Devlieghere const char *session_dictionary_name, 2556498aff2SJonas Devlieghere const lldb::DebuggerSP debugger_sp 2566498aff2SJonas Devlieghere) 2576498aff2SJonas Devlieghere{ 2586498aff2SJonas Devlieghere if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) 2596498aff2SJonas Devlieghere Py_RETURN_NONE; 2606498aff2SJonas Devlieghere 2616498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 2626498aff2SJonas Devlieghere auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 2636498aff2SJonas Devlieghere auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict); 2646498aff2SJonas Devlieghere 2656498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 2666498aff2SJonas Devlieghere return nullptr; 2676498aff2SJonas Devlieghere 2686498aff2SJonas Devlieghere lldb::SBDebugger debugger_sb(debugger_sp); 2696498aff2SJonas Devlieghere PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); 2706498aff2SJonas Devlieghere PythonObject result = pfunc(debugger_arg, dict); 2716498aff2SJonas Devlieghere 2726498aff2SJonas Devlieghere if (result.IsAllocated()) 2736498aff2SJonas Devlieghere return result.release(); 2746498aff2SJonas Devlieghere 2756498aff2SJonas Devlieghere Py_RETURN_NONE; 2766498aff2SJonas Devlieghere} 2776498aff2SJonas Devlieghere 2786498aff2SJonas DevlieghereSWIGEXPORT void* 2791f6a57c1SMed Ismail BennaniLLDBSwigPythonCreateScriptedProcess 2801f6a57c1SMed Ismail Bennani( 2811f6a57c1SMed Ismail Bennani const char *python_class_name, 2821f6a57c1SMed Ismail Bennani const char *session_dictionary_name, 2831f6a57c1SMed Ismail Bennani const lldb::TargetSP& target_sp, 2841f6a57c1SMed Ismail Bennani lldb_private::StructuredDataImpl *args_impl, 2851f6a57c1SMed Ismail Bennani std::string &error_string 2861f6a57c1SMed Ismail Bennani) 2871f6a57c1SMed Ismail Bennani{ 2881f6a57c1SMed Ismail Bennani if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) 2891f6a57c1SMed Ismail Bennani Py_RETURN_NONE; 2901f6a57c1SMed Ismail Bennani 2911f6a57c1SMed Ismail Bennani PyErr_Cleaner py_err_cleaner(true); 2921f6a57c1SMed Ismail Bennani 2931f6a57c1SMed Ismail Bennani auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 2941f6a57c1SMed Ismail Bennani auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict); 2951f6a57c1SMed Ismail Bennani 2961f6a57c1SMed Ismail Bennani if (!pfunc.IsAllocated()) { 2971f6a57c1SMed Ismail Bennani error_string.append("could not find script class: "); 2981f6a57c1SMed Ismail Bennani error_string.append(python_class_name); 2991f6a57c1SMed Ismail Bennani return nullptr; 3001f6a57c1SMed Ismail Bennani } 3011f6a57c1SMed Ismail Bennani 3021f6a57c1SMed Ismail Bennani // I do not want the SBTarget to be deallocated when going out of scope 3031f6a57c1SMed Ismail Bennani // because python has ownership of it and will manage memory for this 3041f6a57c1SMed Ismail Bennani // object by itself 3051f6a57c1SMed Ismail Bennani PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBTarget(target_sp))); 3061f6a57c1SMed Ismail Bennani 3071f6a57c1SMed Ismail Bennani if (!target_arg.IsAllocated()) 3081f6a57c1SMed Ismail Bennani Py_RETURN_NONE; 3091f6a57c1SMed Ismail Bennani 3101f6a57c1SMed Ismail Bennani llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo(); 3111f6a57c1SMed Ismail Bennani if (!arg_info) { 3121f6a57c1SMed Ismail Bennani llvm::handleAllErrors( 3131f6a57c1SMed Ismail Bennani arg_info.takeError(), 3141f6a57c1SMed Ismail Bennani [&](PythonException &E) { 3151f6a57c1SMed Ismail Bennani error_string.append(E.ReadBacktrace()); 3161f6a57c1SMed Ismail Bennani }, 3171f6a57c1SMed Ismail Bennani [&](const llvm::ErrorInfoBase &E) { 3181f6a57c1SMed Ismail Bennani error_string.append(E.message()); 3191f6a57c1SMed Ismail Bennani }); 3201f6a57c1SMed Ismail Bennani Py_RETURN_NONE; 3211f6a57c1SMed Ismail Bennani } 3221f6a57c1SMed Ismail Bennani 3231f6a57c1SMed Ismail Bennani PythonObject result = {}; 3241f6a57c1SMed Ismail Bennani if (arg_info.get().max_positional_args == 2) { 3251f6a57c1SMed Ismail Bennani PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBStructuredData(args_impl))); 326*738621d0SMed Ismail Bennani result = pfunc(target_arg, args_arg); 3271f6a57c1SMed Ismail Bennani } else { 328*738621d0SMed Ismail Bennani error_string.assign("wrong number of arguments in __init__, should be 2 (not including self)"); 3291f6a57c1SMed Ismail Bennani Py_RETURN_NONE; 3301f6a57c1SMed Ismail Bennani } 3311f6a57c1SMed Ismail Bennani 3321f6a57c1SMed Ismail Bennani if (result.IsAllocated()) 3331f6a57c1SMed Ismail Bennani return result.release(); 3341f6a57c1SMed Ismail Bennani Py_RETURN_NONE; 3351f6a57c1SMed Ismail Bennani} 3361f6a57c1SMed Ismail Bennani 3371f6a57c1SMed Ismail BennaniSWIGEXPORT void* 33859d8dd79SMed Ismail BennaniLLDBSwigPythonCreateScriptedThread 33959d8dd79SMed Ismail Bennani( 34059d8dd79SMed Ismail Bennani const char *python_class_name, 34159d8dd79SMed Ismail Bennani const char *session_dictionary_name, 342*738621d0SMed Ismail Bennani const lldb::ProcessSP& process_sp, 343*738621d0SMed Ismail Bennani lldb_private::StructuredDataImpl *args_impl, 34459d8dd79SMed Ismail Bennani std::string &error_string 34559d8dd79SMed Ismail Bennani) 34659d8dd79SMed Ismail Bennani{ 34759d8dd79SMed Ismail Bennani if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) 34859d8dd79SMed Ismail Bennani Py_RETURN_NONE; 34959d8dd79SMed Ismail Bennani 35059d8dd79SMed Ismail Bennani PyErr_Cleaner py_err_cleaner(true); 35159d8dd79SMed Ismail Bennani 35259d8dd79SMed Ismail Bennani auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 35359d8dd79SMed Ismail Bennani auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict); 35459d8dd79SMed Ismail Bennani 35559d8dd79SMed Ismail Bennani if (!pfunc.IsAllocated()) { 35659d8dd79SMed Ismail Bennani error_string.append("could not find script class: "); 35759d8dd79SMed Ismail Bennani error_string.append(python_class_name); 35859d8dd79SMed Ismail Bennani return nullptr; 35959d8dd79SMed Ismail Bennani } 36059d8dd79SMed Ismail Bennani 361*738621d0SMed Ismail Bennani // I do not want the SBProcess to be deallocated when going out of scope 36259d8dd79SMed Ismail Bennani // because python has ownership of it and will manage memory for this 36359d8dd79SMed Ismail Bennani // object by itself 364*738621d0SMed Ismail Bennani PythonObject process_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBProcess(process_sp))); 36559d8dd79SMed Ismail Bennani 366*738621d0SMed Ismail Bennani if (!process_arg.IsAllocated()) 36759d8dd79SMed Ismail Bennani Py_RETURN_NONE; 36859d8dd79SMed Ismail Bennani 36959d8dd79SMed Ismail Bennani llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo(); 37059d8dd79SMed Ismail Bennani if (!arg_info) { 37159d8dd79SMed Ismail Bennani llvm::handleAllErrors( 37259d8dd79SMed Ismail Bennani arg_info.takeError(), 37359d8dd79SMed Ismail Bennani [&](PythonException &E) { 37459d8dd79SMed Ismail Bennani error_string.append(E.ReadBacktrace()); 37559d8dd79SMed Ismail Bennani }, 37659d8dd79SMed Ismail Bennani [&](const llvm::ErrorInfoBase &E) { 37759d8dd79SMed Ismail Bennani error_string.append(E.message()); 37859d8dd79SMed Ismail Bennani }); 37959d8dd79SMed Ismail Bennani Py_RETURN_NONE; 38059d8dd79SMed Ismail Bennani } 38159d8dd79SMed Ismail Bennani 38259d8dd79SMed Ismail Bennani PythonObject result = {}; 383*738621d0SMed Ismail Bennani if (arg_info.get().max_positional_args == 2) { 384*738621d0SMed Ismail Bennani PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBStructuredData(args_impl))); 385*738621d0SMed Ismail Bennani result = pfunc(process_arg, args_arg); 38659d8dd79SMed Ismail Bennani } else { 387*738621d0SMed Ismail Bennani error_string.assign("wrong number of arguments in __init__, should be 2 (not including self)"); 38859d8dd79SMed Ismail Bennani Py_RETURN_NONE; 38959d8dd79SMed Ismail Bennani } 39059d8dd79SMed Ismail Bennani 39159d8dd79SMed Ismail Bennani if (result.IsAllocated()) 39259d8dd79SMed Ismail Bennani return result.release(); 39359d8dd79SMed Ismail Bennani Py_RETURN_NONE; 39459d8dd79SMed Ismail Bennani} 39559d8dd79SMed Ismail Bennani 39659d8dd79SMed Ismail BennaniSWIGEXPORT void* 3976498aff2SJonas DevlieghereLLDBSwigPythonCreateScriptedThreadPlan 3986498aff2SJonas Devlieghere( 3996498aff2SJonas Devlieghere const char *python_class_name, 4006498aff2SJonas Devlieghere const char *session_dictionary_name, 4016498aff2SJonas Devlieghere lldb_private::StructuredDataImpl *args_impl, 4026498aff2SJonas Devlieghere std::string &error_string, 4036498aff2SJonas Devlieghere const lldb::ThreadPlanSP& thread_plan_sp 4046498aff2SJonas Devlieghere) 4056498aff2SJonas Devlieghere{ 4066498aff2SJonas Devlieghere if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) 4076498aff2SJonas Devlieghere Py_RETURN_NONE; 4086498aff2SJonas Devlieghere 4096498aff2SJonas Devlieghere 4106498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 4116498aff2SJonas Devlieghere 4126498aff2SJonas Devlieghere auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 4136498aff2SJonas Devlieghere auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict); 4146498aff2SJonas Devlieghere 4156498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) { 4166498aff2SJonas Devlieghere error_string.append("could not find script class: "); 4176498aff2SJonas Devlieghere error_string.append(python_class_name); 4186498aff2SJonas Devlieghere return nullptr; 4196498aff2SJonas Devlieghere } 4206498aff2SJonas Devlieghere 421b79dff02SJonas Devlieghere // I do not want the SBThreadPlan to be deallocated when going out of scope 422b79dff02SJonas Devlieghere // because python has ownership of it and will manage memory for this 423b79dff02SJonas Devlieghere // object by itself 424b79dff02SJonas Devlieghere PythonObject tp_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBThreadPlan(thread_plan_sp))); 4256498aff2SJonas Devlieghere 4266498aff2SJonas Devlieghere if (!tp_arg.IsAllocated()) 4276498aff2SJonas Devlieghere Py_RETURN_NONE; 4286498aff2SJonas Devlieghere 4296498aff2SJonas Devlieghere llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo(); 4306498aff2SJonas Devlieghere if (!arg_info) { 4316498aff2SJonas Devlieghere llvm::handleAllErrors( 4326498aff2SJonas Devlieghere arg_info.takeError(), 4336498aff2SJonas Devlieghere [&](PythonException &E) { 4346498aff2SJonas Devlieghere error_string.append(E.ReadBacktrace()); 4356498aff2SJonas Devlieghere }, 4366498aff2SJonas Devlieghere [&](const llvm::ErrorInfoBase &E) { 4376498aff2SJonas Devlieghere error_string.append(E.message()); 4386498aff2SJonas Devlieghere }); 4396498aff2SJonas Devlieghere Py_RETURN_NONE; 4406498aff2SJonas Devlieghere } 4416498aff2SJonas Devlieghere 4426498aff2SJonas Devlieghere PythonObject result = {}; 4436498aff2SJonas Devlieghere if (arg_info.get().max_positional_args == 2) { 4446498aff2SJonas Devlieghere if (args_impl != nullptr) { 4456498aff2SJonas Devlieghere error_string.assign("args passed, but __init__ does not take an args dictionary"); 4466498aff2SJonas Devlieghere Py_RETURN_NONE; 4476498aff2SJonas Devlieghere } 4486498aff2SJonas Devlieghere result = pfunc(tp_arg, dict); 4496498aff2SJonas Devlieghere } else if (arg_info.get().max_positional_args >= 3) { 450b79dff02SJonas Devlieghere PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBStructuredData(args_impl))); 4516498aff2SJonas Devlieghere result = pfunc(tp_arg, args_arg, dict); 4526498aff2SJonas Devlieghere } else { 4536498aff2SJonas Devlieghere error_string.assign("wrong number of arguments in __init__, should be 2 or 3 (not including self)"); 4546498aff2SJonas Devlieghere Py_RETURN_NONE; 4556498aff2SJonas Devlieghere } 4566498aff2SJonas Devlieghere 4576498aff2SJonas Devlieghere // FIXME: At this point we should check that the class we found supports all the methods 4586498aff2SJonas Devlieghere // that we need. 4596498aff2SJonas Devlieghere 4606498aff2SJonas Devlieghere if (result.IsAllocated()) 4616498aff2SJonas Devlieghere return result.release(); 4626498aff2SJonas Devlieghere Py_RETURN_NONE; 4636498aff2SJonas Devlieghere} 4646498aff2SJonas Devlieghere 4656498aff2SJonas DevlieghereSWIGEXPORT bool 4666498aff2SJonas DevlieghereLLDBSWIGPythonCallThreadPlan 4676498aff2SJonas Devlieghere( 4686498aff2SJonas Devlieghere void *implementor, 4696498aff2SJonas Devlieghere const char *method_name, 4706498aff2SJonas Devlieghere lldb_private::Event *event, 4716498aff2SJonas Devlieghere bool &got_error 4726498aff2SJonas Devlieghere) 4736498aff2SJonas Devlieghere{ 4746498aff2SJonas Devlieghere got_error = false; 4756498aff2SJonas Devlieghere 4766498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(false); 4776498aff2SJonas Devlieghere PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor)); 4786498aff2SJonas Devlieghere auto pfunc = self.ResolveName<PythonCallable>(method_name); 4796498aff2SJonas Devlieghere 4806498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 4816498aff2SJonas Devlieghere return false; 4826498aff2SJonas Devlieghere 4836498aff2SJonas Devlieghere PythonObject result; 4846498aff2SJonas Devlieghere if (event != nullptr) 4856498aff2SJonas Devlieghere { 4866498aff2SJonas Devlieghere lldb::SBEvent sb_event(event); 4876498aff2SJonas Devlieghere PythonObject event_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_event)); 4886498aff2SJonas Devlieghere result = pfunc(event_arg); 4896498aff2SJonas Devlieghere } 4906498aff2SJonas Devlieghere else 4916498aff2SJonas Devlieghere result = pfunc(); 4926498aff2SJonas Devlieghere 4936498aff2SJonas Devlieghere if (PyErr_Occurred()) 4946498aff2SJonas Devlieghere { 4956498aff2SJonas Devlieghere got_error = true; 4966498aff2SJonas Devlieghere printf ("Return value was neither false nor true for call to %s.\n", method_name); 4976498aff2SJonas Devlieghere PyErr_Print(); 4986498aff2SJonas Devlieghere return false; 4996498aff2SJonas Devlieghere } 5006498aff2SJonas Devlieghere 5016498aff2SJonas Devlieghere if (result.get() == Py_True) 5026498aff2SJonas Devlieghere return true; 5036498aff2SJonas Devlieghere else if (result.get() == Py_False) 5046498aff2SJonas Devlieghere return false; 5056498aff2SJonas Devlieghere 5066498aff2SJonas Devlieghere // Somebody returned the wrong thing... 5076498aff2SJonas Devlieghere got_error = true; 5086498aff2SJonas Devlieghere printf ("Wrong return value type for call to %s.\n", method_name); 5096498aff2SJonas Devlieghere return false; 5106498aff2SJonas Devlieghere} 5116498aff2SJonas Devlieghere 5126498aff2SJonas DevlieghereSWIGEXPORT void * 5136498aff2SJonas DevlieghereLLDBSwigPythonCreateScriptedBreakpointResolver 5146498aff2SJonas Devlieghere( 5156498aff2SJonas Devlieghere const char *python_class_name, 5166498aff2SJonas Devlieghere const char *session_dictionary_name, 5176498aff2SJonas Devlieghere lldb_private::StructuredDataImpl *args_impl, 5186498aff2SJonas Devlieghere lldb::BreakpointSP &breakpoint_sp 5196498aff2SJonas Devlieghere) 5206498aff2SJonas Devlieghere{ 5216498aff2SJonas Devlieghere if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) 5226498aff2SJonas Devlieghere Py_RETURN_NONE; 5236498aff2SJonas Devlieghere 5246498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 5256498aff2SJonas Devlieghere 5266498aff2SJonas Devlieghere auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 5276498aff2SJonas Devlieghere auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict); 5286498aff2SJonas Devlieghere 5296498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 5306498aff2SJonas Devlieghere return nullptr; 5316498aff2SJonas Devlieghere 5326498aff2SJonas Devlieghere lldb::SBBreakpoint *bkpt_value = new lldb::SBBreakpoint(breakpoint_sp); 5336498aff2SJonas Devlieghere 5346498aff2SJonas Devlieghere PythonObject bkpt_arg(PyRefType::Owned, SBTypeToSWIGWrapper(bkpt_value)); 5356498aff2SJonas Devlieghere 5366498aff2SJonas Devlieghere lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl); 5376498aff2SJonas Devlieghere 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 6401b1d9815SJim Ingham lldb::SBTarget *target_val 6411b1d9815SJim Ingham = new lldb::SBTarget(target_sp); 6421b1d9815SJim Ingham 6431b1d9815SJim Ingham PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(target_val)); 6441b1d9815SJim Ingham 6451b1d9815SJim Ingham lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl); 6461b1d9815SJim Ingham PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(args_value)); 6471b1d9815SJim Ingham 6481b1d9815SJim Ingham PythonObject result = pfunc(target_arg, args_arg, dict); 6491b1d9815SJim Ingham 6501b1d9815SJim Ingham if (result.IsAllocated()) 6511b1d9815SJim Ingham { 6521b1d9815SJim Ingham // Check that the handle_stop callback is defined: 6531b1d9815SJim Ingham auto callback_func = result.ResolveName<PythonCallable>("handle_stop"); 6541b1d9815SJim Ingham if (callback_func.IsAllocated()) { 6551b1d9815SJim Ingham if (auto args_info = callback_func.GetArgInfo()) { 6561b1d9815SJim Ingham size_t num_args = (*args_info).max_positional_args; 6571b1d9815SJim Ingham if (num_args != 2) { 6581b1d9815SJim Ingham error.SetErrorStringWithFormat("Wrong number of args for " 6592f95c50aSRichard Smith "handle_stop callback, should be 2 (excluding self), got: %zu", 6601b1d9815SJim Ingham num_args); 6611b1d9815SJim Ingham Py_RETURN_NONE; 6621b1d9815SJim Ingham } else 6631b1d9815SJim Ingham return result.release(); 6641b1d9815SJim Ingham } else { 6651b1d9815SJim Ingham error.SetErrorString("Couldn't get num arguments for handle_stop " 6661b1d9815SJim Ingham "callback."); 6671b1d9815SJim Ingham Py_RETURN_NONE; 6681b1d9815SJim Ingham } 6691b1d9815SJim Ingham return result.release(); 6701b1d9815SJim Ingham } 6711b1d9815SJim Ingham else { 6721b1d9815SJim Ingham error.SetErrorStringWithFormat("Class \"%s\" is missing the required " 6731b1d9815SJim Ingham "handle_stop callback.", 6741b1d9815SJim Ingham python_class_name); 6751b1d9815SJim Ingham result.release(); 6761b1d9815SJim Ingham } 6771b1d9815SJim Ingham } 6781b1d9815SJim Ingham Py_RETURN_NONE; 6791b1d9815SJim Ingham} 6801b1d9815SJim Ingham 6811b1d9815SJim InghamSWIGEXPORT bool 6821b1d9815SJim InghamLLDBSwigPythonStopHookCallHandleStop 6831b1d9815SJim Ingham( 6841b1d9815SJim Ingham void *implementor, 6851b1d9815SJim Ingham lldb::ExecutionContextRefSP exc_ctx_sp, 6861b1d9815SJim Ingham lldb::StreamSP stream 6871b1d9815SJim Ingham) 6881b1d9815SJim Ingham{ 6891b1d9815SJim Ingham // handle_stop will return a bool with the meaning "should_stop"... 6901b1d9815SJim Ingham // If you return nothing we'll assume we are going to stop. 6911b1d9815SJim Ingham // Also any errors should return true, since we should stop on error. 6921b1d9815SJim Ingham 6931b1d9815SJim Ingham PyErr_Cleaner py_err_cleaner(false); 6941b1d9815SJim Ingham PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor)); 6951b1d9815SJim Ingham auto pfunc = self.ResolveName<PythonCallable>("handle_stop"); 6961b1d9815SJim Ingham 6971b1d9815SJim Ingham if (!pfunc.IsAllocated()) 6981b1d9815SJim Ingham return true; 6991b1d9815SJim Ingham 7001b1d9815SJim Ingham PythonObject result; 7011b1d9815SJim Ingham lldb::SBExecutionContext sb_exc_ctx(exc_ctx_sp); 7021b1d9815SJim Ingham PythonObject exc_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_exc_ctx)); 7031b1d9815SJim Ingham lldb::SBStream sb_stream; 7041b1d9815SJim Ingham PythonObject sb_stream_arg(PyRefType::Owned, 7051b1d9815SJim Ingham SBTypeToSWIGWrapper(sb_stream)); 7061b1d9815SJim Ingham result = pfunc(exc_ctx_arg, sb_stream_arg); 7071b1d9815SJim Ingham 7081b1d9815SJim Ingham if (PyErr_Occurred()) 7091b1d9815SJim Ingham { 7101b1d9815SJim Ingham stream->PutCString("Python error occurred handling stop-hook."); 7111b1d9815SJim Ingham PyErr_Print(); 7121b1d9815SJim Ingham PyErr_Clear(); 7131b1d9815SJim Ingham return true; 7141b1d9815SJim Ingham } 7151b1d9815SJim Ingham 7161b1d9815SJim Ingham // Now add the result to the output stream. SBStream only 7171b1d9815SJim Ingham // makes an internally help StreamString which I can't interpose, so I 7181b1d9815SJim Ingham // have to copy it over here. 7191b1d9815SJim Ingham stream->PutCString(sb_stream.GetData()); 7201b1d9815SJim Ingham 7211b1d9815SJim Ingham if (result.get() == Py_False) 7221b1d9815SJim Ingham return false; 7231b1d9815SJim Ingham else 7241b1d9815SJim Ingham return true; 7251b1d9815SJim Ingham} 7261b1d9815SJim Ingham 7276498aff2SJonas Devlieghere// wrapper that calls an optional instance member of an object taking no arguments 7286498aff2SJonas Devliegherestatic PyObject* 7296498aff2SJonas DevlieghereLLDBSwigPython_CallOptionalMember 7306498aff2SJonas Devlieghere( 7316498aff2SJonas Devlieghere PyObject* implementor, 7326498aff2SJonas Devlieghere char* callee_name, 7336498aff2SJonas Devlieghere PyObject* ret_if_not_found = Py_None, 7346498aff2SJonas Devlieghere bool* was_found = NULL 7356498aff2SJonas Devlieghere) 7366498aff2SJonas Devlieghere{ 7376498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(false); 7386498aff2SJonas Devlieghere 7396498aff2SJonas Devlieghere PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor)); 7406498aff2SJonas Devlieghere auto pfunc = self.ResolveName<PythonCallable>(callee_name); 7416498aff2SJonas Devlieghere 7426498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 7436498aff2SJonas Devlieghere { 7446498aff2SJonas Devlieghere if (was_found) 7456498aff2SJonas Devlieghere *was_found = false; 7466498aff2SJonas Devlieghere Py_XINCREF(ret_if_not_found); 7476498aff2SJonas Devlieghere return ret_if_not_found; 7486498aff2SJonas Devlieghere } 7496498aff2SJonas Devlieghere 7506498aff2SJonas Devlieghere if (was_found) 7516498aff2SJonas Devlieghere *was_found = true; 7526498aff2SJonas Devlieghere 7536498aff2SJonas Devlieghere PythonObject result = pfunc(); 7546498aff2SJonas Devlieghere return result.release(); 7556498aff2SJonas Devlieghere} 7566498aff2SJonas Devlieghere 7576498aff2SJonas DevlieghereSWIGEXPORT size_t 7586498aff2SJonas DevlieghereLLDBSwigPython_CalculateNumChildren 7596498aff2SJonas Devlieghere( 7606498aff2SJonas Devlieghere PyObject *implementor, 7616498aff2SJonas Devlieghere uint32_t max 7626498aff2SJonas Devlieghere) 7636498aff2SJonas Devlieghere{ 7646498aff2SJonas Devlieghere PythonObject self(PyRefType::Borrowed, implementor); 7656498aff2SJonas Devlieghere auto pfunc = self.ResolveName<PythonCallable>("num_children"); 7666498aff2SJonas Devlieghere 7676498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 7686498aff2SJonas Devlieghere return 0; 7696498aff2SJonas Devlieghere 7706498aff2SJonas Devlieghere auto arg_info = pfunc.GetArgInfo(); 7716498aff2SJonas Devlieghere if (!arg_info) { 7726498aff2SJonas Devlieghere llvm::consumeError(arg_info.takeError()); 7736498aff2SJonas Devlieghere return 0; 7746498aff2SJonas Devlieghere } 7756498aff2SJonas Devlieghere 77652712d3fSLawrence D'Anna size_t ret_val; 777478619cfSMuhammad Omair Javaid if (arg_info.get().max_positional_args < 1) 77852712d3fSLawrence D'Anna ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call())); 779478619cfSMuhammad Omair Javaid else 78052712d3fSLawrence D'Anna ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call(PythonInteger(max)))); 781478619cfSMuhammad Omair Javaid 78252712d3fSLawrence D'Anna if (PyErr_Occurred()) 7836498aff2SJonas Devlieghere { 7846498aff2SJonas Devlieghere PyErr_Print(); 7856498aff2SJonas Devlieghere PyErr_Clear(); 78652712d3fSLawrence D'Anna return 0; 7876498aff2SJonas Devlieghere } 7886498aff2SJonas Devlieghere 7896498aff2SJonas Devlieghere if (arg_info.get().max_positional_args < 1) 7906498aff2SJonas Devlieghere ret_val = std::min(ret_val, static_cast<size_t>(max)); 7916498aff2SJonas Devlieghere 7926498aff2SJonas Devlieghere return ret_val; 7936498aff2SJonas Devlieghere} 7946498aff2SJonas Devlieghere 7956498aff2SJonas DevlieghereSWIGEXPORT PyObject* 7966498aff2SJonas DevlieghereLLDBSwigPython_GetChildAtIndex 7976498aff2SJonas Devlieghere( 7986498aff2SJonas Devlieghere PyObject *implementor, 7996498aff2SJonas Devlieghere uint32_t idx 8006498aff2SJonas Devlieghere) 8016498aff2SJonas Devlieghere{ 8026498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 8036498aff2SJonas Devlieghere 8046498aff2SJonas Devlieghere PythonObject self(PyRefType::Borrowed, implementor); 8056498aff2SJonas Devlieghere auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index"); 8066498aff2SJonas Devlieghere 8076498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 8086498aff2SJonas Devlieghere return nullptr; 8096498aff2SJonas Devlieghere 8106498aff2SJonas Devlieghere PythonObject result = pfunc(PythonInteger(idx)); 8116498aff2SJonas Devlieghere 8126498aff2SJonas Devlieghere if (!result.IsAllocated()) 8136498aff2SJonas Devlieghere return nullptr; 8146498aff2SJonas Devlieghere 8156498aff2SJonas Devlieghere lldb::SBValue* sbvalue_ptr = nullptr; 8166498aff2SJonas Devlieghere if (SWIG_ConvertPtr(result.get(), (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1) 8176498aff2SJonas Devlieghere return nullptr; 8186498aff2SJonas Devlieghere 8196498aff2SJonas Devlieghere if (sbvalue_ptr == nullptr) 8206498aff2SJonas Devlieghere return nullptr; 8216498aff2SJonas Devlieghere 8226498aff2SJonas Devlieghere return result.release(); 8236498aff2SJonas Devlieghere} 8246498aff2SJonas Devlieghere 8256498aff2SJonas DevlieghereSWIGEXPORT int 8266498aff2SJonas DevlieghereLLDBSwigPython_GetIndexOfChildWithName 8276498aff2SJonas Devlieghere( 8286498aff2SJonas Devlieghere PyObject *implementor, 8296498aff2SJonas Devlieghere const char* child_name 8306498aff2SJonas Devlieghere) 8316498aff2SJonas Devlieghere{ 8326498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 8336498aff2SJonas Devlieghere 8346498aff2SJonas Devlieghere PythonObject self(PyRefType::Borrowed, implementor); 8356498aff2SJonas Devlieghere auto pfunc = self.ResolveName<PythonCallable>("get_child_index"); 8366498aff2SJonas Devlieghere 8376498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 8386498aff2SJonas Devlieghere return UINT32_MAX; 8396498aff2SJonas Devlieghere 84052712d3fSLawrence D'Anna llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name)); 8416498aff2SJonas Devlieghere 84252712d3fSLawrence D'Anna long long retval = unwrapOrSetPythonException(As<long long>(std::move(result))); 84352712d3fSLawrence D'Anna 84452712d3fSLawrence D'Anna if (PyErr_Occurred()) { 84552712d3fSLawrence D'Anna PyErr_Clear(); // FIXME print this? do something else 8466498aff2SJonas Devlieghere return UINT32_MAX; 84752712d3fSLawrence D'Anna } 8486498aff2SJonas Devlieghere 8496498aff2SJonas Devlieghere if (retval >= 0) 8506498aff2SJonas Devlieghere return (uint32_t)retval; 8516498aff2SJonas Devlieghere 8526498aff2SJonas Devlieghere return UINT32_MAX; 8536498aff2SJonas Devlieghere} 8546498aff2SJonas Devlieghere 8556498aff2SJonas DevlieghereSWIGEXPORT bool 8566498aff2SJonas DevlieghereLLDBSwigPython_UpdateSynthProviderInstance 8576498aff2SJonas Devlieghere( 8586498aff2SJonas Devlieghere PyObject *implementor 8596498aff2SJonas Devlieghere) 8606498aff2SJonas Devlieghere{ 8616498aff2SJonas Devlieghere bool ret_val = false; 8626498aff2SJonas Devlieghere 8636498aff2SJonas Devlieghere static char callee_name[] = "update"; 8646498aff2SJonas Devlieghere 8656498aff2SJonas Devlieghere PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name); 8666498aff2SJonas Devlieghere 8676498aff2SJonas Devlieghere if (py_return == Py_True) 8686498aff2SJonas Devlieghere ret_val = true; 8696498aff2SJonas Devlieghere 8706498aff2SJonas Devlieghere Py_XDECREF(py_return); 8716498aff2SJonas Devlieghere 8726498aff2SJonas Devlieghere return ret_val; 8736498aff2SJonas Devlieghere} 8746498aff2SJonas Devlieghere 8756498aff2SJonas DevlieghereSWIGEXPORT bool 8766498aff2SJonas DevlieghereLLDBSwigPython_MightHaveChildrenSynthProviderInstance 8776498aff2SJonas Devlieghere( 8786498aff2SJonas Devlieghere PyObject *implementor 8796498aff2SJonas Devlieghere) 8806498aff2SJonas Devlieghere{ 8816498aff2SJonas Devlieghere bool ret_val = false; 8826498aff2SJonas Devlieghere 8836498aff2SJonas Devlieghere static char callee_name[] = "has_children"; 8846498aff2SJonas Devlieghere 8856498aff2SJonas Devlieghere PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True); 8866498aff2SJonas Devlieghere 8876498aff2SJonas Devlieghere if (py_return == Py_True) 8886498aff2SJonas Devlieghere ret_val = true; 8896498aff2SJonas Devlieghere 8906498aff2SJonas Devlieghere Py_XDECREF(py_return); 8916498aff2SJonas Devlieghere 8926498aff2SJonas Devlieghere return ret_val; 8936498aff2SJonas Devlieghere} 8946498aff2SJonas Devlieghere 8956498aff2SJonas DevlieghereSWIGEXPORT PyObject* 8966498aff2SJonas DevlieghereLLDBSwigPython_GetValueSynthProviderInstance 8976498aff2SJonas Devlieghere( 8986498aff2SJonas Devlieghere PyObject *implementor 8996498aff2SJonas Devlieghere) 9006498aff2SJonas Devlieghere{ 9016498aff2SJonas Devlieghere PyObject* ret_val = nullptr; 9026498aff2SJonas Devlieghere 9036498aff2SJonas Devlieghere static char callee_name[] = "get_value"; 9046498aff2SJonas Devlieghere 9056498aff2SJonas Devlieghere PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_None); 9066498aff2SJonas Devlieghere 9076498aff2SJonas Devlieghere if (py_return == Py_None || py_return == nullptr) 9086498aff2SJonas Devlieghere ret_val = nullptr; 9096498aff2SJonas Devlieghere 9106498aff2SJonas Devlieghere lldb::SBValue* sbvalue_ptr = NULL; 9116498aff2SJonas Devlieghere 9126498aff2SJonas Devlieghere if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1) 9136498aff2SJonas Devlieghere ret_val = nullptr; 9146498aff2SJonas Devlieghere else if (sbvalue_ptr == NULL) 9156498aff2SJonas Devlieghere ret_val = nullptr; 9166498aff2SJonas Devlieghere else 9176498aff2SJonas Devlieghere ret_val = py_return; 9186498aff2SJonas Devlieghere 9196498aff2SJonas Devlieghere Py_XDECREF(py_return); 9206498aff2SJonas Devlieghere return ret_val; 9216498aff2SJonas Devlieghere} 9226498aff2SJonas Devlieghere 9236498aff2SJonas DevlieghereSWIGEXPORT void* 9241f6a57c1SMed Ismail BennaniLLDBSWIGPython_CastPyObjectToSBData 9251f6a57c1SMed Ismail Bennani( 9261f6a57c1SMed Ismail Bennani PyObject* data 9271f6a57c1SMed Ismail Bennani) 9281f6a57c1SMed Ismail Bennani{ 9291f6a57c1SMed Ismail Bennani lldb::SBData* sb_ptr = nullptr; 9301f6a57c1SMed Ismail Bennani 9311f6a57c1SMed Ismail Bennani int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0); 9321f6a57c1SMed Ismail Bennani 9331f6a57c1SMed Ismail Bennani if (valid_cast == -1) 9341f6a57c1SMed Ismail Bennani return NULL; 9351f6a57c1SMed Ismail Bennani 9361f6a57c1SMed Ismail Bennani return sb_ptr; 9371f6a57c1SMed Ismail Bennani} 9381f6a57c1SMed Ismail Bennani 9391f6a57c1SMed Ismail Bennani 9401f6a57c1SMed Ismail BennaniSWIGEXPORT void* 9411f6a57c1SMed Ismail BennaniLLDBSWIGPython_CastPyObjectToSBError 9421f6a57c1SMed Ismail Bennani( 9431f6a57c1SMed Ismail Bennani PyObject* data 9441f6a57c1SMed Ismail Bennani) 9451f6a57c1SMed Ismail Bennani{ 9461f6a57c1SMed Ismail Bennani lldb::SBError* sb_ptr = nullptr; 9471f6a57c1SMed Ismail Bennani 9481f6a57c1SMed Ismail Bennani int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0); 9491f6a57c1SMed Ismail Bennani 9501f6a57c1SMed Ismail Bennani if (valid_cast == -1) 9511f6a57c1SMed Ismail Bennani return NULL; 9521f6a57c1SMed Ismail Bennani 9531f6a57c1SMed Ismail Bennani return sb_ptr; 9541f6a57c1SMed Ismail Bennani} 9551f6a57c1SMed Ismail Bennani 9561f6a57c1SMed Ismail Bennani 9571f6a57c1SMed Ismail BennaniSWIGEXPORT void* 9586498aff2SJonas DevlieghereLLDBSWIGPython_CastPyObjectToSBValue 9596498aff2SJonas Devlieghere( 9606498aff2SJonas Devlieghere PyObject* data 9616498aff2SJonas Devlieghere) 9626498aff2SJonas Devlieghere{ 9636498aff2SJonas Devlieghere lldb::SBValue* sb_ptr = NULL; 9646498aff2SJonas Devlieghere 9656498aff2SJonas Devlieghere int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0); 9666498aff2SJonas Devlieghere 9676498aff2SJonas Devlieghere if (valid_cast == -1) 9686498aff2SJonas Devlieghere return NULL; 9696498aff2SJonas Devlieghere 9706498aff2SJonas Devlieghere return sb_ptr; 9716498aff2SJonas Devlieghere} 9726498aff2SJonas Devlieghere 973a758c9f7SMed Ismail BennaniSWIGEXPORT void* 974a758c9f7SMed Ismail BennaniLLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo 975a758c9f7SMed Ismail Bennani( 976a758c9f7SMed Ismail Bennani PyObject* data 977a758c9f7SMed Ismail Bennani) 978a758c9f7SMed Ismail Bennani{ 979a758c9f7SMed Ismail Bennani lldb::SBMemoryRegionInfo* sb_ptr = NULL; 980a758c9f7SMed Ismail Bennani 981a758c9f7SMed Ismail Bennani int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0); 982a758c9f7SMed Ismail Bennani 983a758c9f7SMed Ismail Bennani if (valid_cast == -1) 984a758c9f7SMed Ismail Bennani return NULL; 985a758c9f7SMed Ismail Bennani 986a758c9f7SMed Ismail Bennani return sb_ptr; 987a758c9f7SMed Ismail Bennani} 988a758c9f7SMed Ismail Bennani 9896498aff2SJonas DevlieghereSWIGEXPORT bool 9906498aff2SJonas DevlieghereLLDBSwigPythonCallCommand 9916498aff2SJonas Devlieghere( 9926498aff2SJonas Devlieghere const char *python_function_name, 9936498aff2SJonas Devlieghere const char *session_dictionary_name, 9946498aff2SJonas Devlieghere lldb::DebuggerSP& debugger, 9956498aff2SJonas Devlieghere const char* args, 9966498aff2SJonas Devlieghere lldb_private::CommandReturnObject& cmd_retobj, 9976498aff2SJonas Devlieghere lldb::ExecutionContextRefSP exe_ctx_ref_sp 9986498aff2SJonas Devlieghere) 9996498aff2SJonas Devlieghere{ 10006498aff2SJonas Devlieghere lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj); 10016498aff2SJonas Devlieghere lldb::SBDebugger debugger_sb(debugger); 10026498aff2SJonas Devlieghere lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp); 10036498aff2SJonas Devlieghere 10046498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 10056498aff2SJonas Devlieghere auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 10066498aff2SJonas Devlieghere auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 10076498aff2SJonas Devlieghere 10086498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 10096498aff2SJonas Devlieghere return false; 10106498aff2SJonas Devlieghere 10116498aff2SJonas Devlieghere // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you 10126498aff2SJonas Devlieghere // see comment above for SBCommandReturnObjectReleaser for further details 10136498aff2SJonas Devlieghere auto argc = pfunc.GetArgInfo(); 10146498aff2SJonas Devlieghere if (!argc) { 10156498aff2SJonas Devlieghere llvm::consumeError(argc.takeError()); 10166498aff2SJonas Devlieghere return false; 10176498aff2SJonas Devlieghere } 10186498aff2SJonas Devlieghere PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); 10196498aff2SJonas Devlieghere PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb)); 10206498aff2SJonas Devlieghere PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(&cmd_retobj_sb)); 10216498aff2SJonas Devlieghere 10226498aff2SJonas Devlieghere if (argc.get().max_positional_args < 5u) 10236498aff2SJonas Devlieghere pfunc(debugger_arg, PythonString(args), cmd_retobj_arg, dict); 10246498aff2SJonas Devlieghere else 10256498aff2SJonas Devlieghere pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg, dict); 10266498aff2SJonas Devlieghere 10276498aff2SJonas Devlieghere return true; 10286498aff2SJonas Devlieghere} 10296498aff2SJonas Devlieghere 10306498aff2SJonas DevlieghereSWIGEXPORT bool 10316498aff2SJonas DevlieghereLLDBSwigPythonCallCommandObject 10326498aff2SJonas Devlieghere( 10336498aff2SJonas Devlieghere PyObject *implementor, 10346498aff2SJonas Devlieghere lldb::DebuggerSP& debugger, 10356498aff2SJonas Devlieghere const char* args, 10366498aff2SJonas Devlieghere lldb_private::CommandReturnObject& cmd_retobj, 10376498aff2SJonas Devlieghere lldb::ExecutionContextRefSP exe_ctx_ref_sp 10386498aff2SJonas Devlieghere) 10396498aff2SJonas Devlieghere{ 10406498aff2SJonas Devlieghere lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj); 10416498aff2SJonas Devlieghere lldb::SBDebugger debugger_sb(debugger); 10426498aff2SJonas Devlieghere lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp); 10436498aff2SJonas Devlieghere 10446498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 10456498aff2SJonas Devlieghere 10466498aff2SJonas Devlieghere PythonObject self(PyRefType::Borrowed, implementor); 10476498aff2SJonas Devlieghere auto pfunc = self.ResolveName<PythonCallable>("__call__"); 10486498aff2SJonas Devlieghere 10496498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 10506498aff2SJonas Devlieghere return false; 10516498aff2SJonas Devlieghere 10526498aff2SJonas Devlieghere // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you 10536498aff2SJonas Devlieghere // see comment above for SBCommandReturnObjectReleaser for further details 10546498aff2SJonas Devlieghere PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); 10556498aff2SJonas Devlieghere PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb)); 10566498aff2SJonas Devlieghere PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(&cmd_retobj_sb)); 10576498aff2SJonas Devlieghere 10586498aff2SJonas Devlieghere pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg); 10596498aff2SJonas Devlieghere 10606498aff2SJonas Devlieghere return true; 10616498aff2SJonas Devlieghere} 10626498aff2SJonas Devlieghere 10636498aff2SJonas DevlieghereSWIGEXPORT void* 10646498aff2SJonas DevlieghereLLDBSWIGPythonCreateOSPlugin 10656498aff2SJonas Devlieghere( 10666498aff2SJonas Devlieghere const char *python_class_name, 10676498aff2SJonas Devlieghere const char *session_dictionary_name, 10686498aff2SJonas Devlieghere const lldb::ProcessSP& process_sp 10696498aff2SJonas Devlieghere) 10706498aff2SJonas Devlieghere{ 10716498aff2SJonas Devlieghere if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) 10726498aff2SJonas Devlieghere Py_RETURN_NONE; 10736498aff2SJonas Devlieghere 10746498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 10756498aff2SJonas Devlieghere 10766498aff2SJonas Devlieghere auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 10776498aff2SJonas Devlieghere auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict); 10786498aff2SJonas Devlieghere 10796498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 10806498aff2SJonas Devlieghere Py_RETURN_NONE; 10816498aff2SJonas Devlieghere 10826498aff2SJonas Devlieghere // I do not want the SBProcess to be deallocated when going out of scope because python 10836498aff2SJonas Devlieghere // has ownership of it and will manage memory for this object by itself 10846498aff2SJonas Devlieghere lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp); 10856498aff2SJonas Devlieghere PythonObject process_arg(PyRefType::Owned, SBTypeToSWIGWrapper(process_sb)); 10866498aff2SJonas Devlieghere if (!process_arg.IsAllocated()) 10876498aff2SJonas Devlieghere Py_RETURN_NONE; 10886498aff2SJonas Devlieghere 10896498aff2SJonas Devlieghere auto result = pfunc(process_arg); 10906498aff2SJonas Devlieghere 10916498aff2SJonas Devlieghere if (result.IsAllocated()) 10926498aff2SJonas Devlieghere return result.release(); 10936498aff2SJonas Devlieghere 10946498aff2SJonas Devlieghere Py_RETURN_NONE; 10956498aff2SJonas Devlieghere} 10966498aff2SJonas Devlieghere 10976498aff2SJonas DevlieghereSWIGEXPORT void* 10986498aff2SJonas DevlieghereLLDBSWIGPython_CreateFrameRecognizer 10996498aff2SJonas Devlieghere( 11006498aff2SJonas Devlieghere const char *python_class_name, 11016498aff2SJonas Devlieghere const char *session_dictionary_name 11026498aff2SJonas Devlieghere) 11036498aff2SJonas Devlieghere{ 11046498aff2SJonas Devlieghere if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) 11056498aff2SJonas Devlieghere Py_RETURN_NONE; 11066498aff2SJonas Devlieghere 11076498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 11086498aff2SJonas Devlieghere 11096498aff2SJonas Devlieghere auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 11106498aff2SJonas Devlieghere auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict); 11116498aff2SJonas Devlieghere 11126498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 11136498aff2SJonas Devlieghere Py_RETURN_NONE; 11146498aff2SJonas Devlieghere 11156498aff2SJonas Devlieghere auto result = pfunc(); 11166498aff2SJonas Devlieghere 11176498aff2SJonas Devlieghere if (result.IsAllocated()) 11186498aff2SJonas Devlieghere return result.release(); 11196498aff2SJonas Devlieghere 11206498aff2SJonas Devlieghere Py_RETURN_NONE; 11216498aff2SJonas Devlieghere} 11226498aff2SJonas Devlieghere 11236498aff2SJonas DevlieghereSWIGEXPORT PyObject* 11246498aff2SJonas DevlieghereLLDBSwigPython_GetRecognizedArguments 11256498aff2SJonas Devlieghere( 11266498aff2SJonas Devlieghere PyObject *implementor, 11276498aff2SJonas Devlieghere const lldb::StackFrameSP& frame_sp 11286498aff2SJonas Devlieghere) 11296498aff2SJonas Devlieghere{ 11306498aff2SJonas Devlieghere static char callee_name[] = "get_recognized_arguments"; 11316498aff2SJonas Devlieghere 11326498aff2SJonas Devlieghere lldb::SBFrame frame_sb(frame_sp); 11336498aff2SJonas Devlieghere PyObject *arg = SBTypeToSWIGWrapper(frame_sb); 11346498aff2SJonas Devlieghere 11356498aff2SJonas Devlieghere PythonString str(callee_name); 11366498aff2SJonas Devlieghere PyObject* result = PyObject_CallMethodObjArgs(implementor, str.get(), arg, 11376498aff2SJonas Devlieghere NULL); 11386498aff2SJonas Devlieghere return result; 11396498aff2SJonas Devlieghere} 11406498aff2SJonas Devlieghere 11416498aff2SJonas DevlieghereSWIGEXPORT void* 11426498aff2SJonas DevlieghereLLDBSWIGPython_GetDynamicSetting (void* module, const char* setting, const lldb::TargetSP& target_sp) 11436498aff2SJonas Devlieghere{ 11446498aff2SJonas Devlieghere if (!module || !setting) 11456498aff2SJonas Devlieghere Py_RETURN_NONE; 11466498aff2SJonas Devlieghere 11476498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 11486498aff2SJonas Devlieghere PythonObject py_module(PyRefType::Borrowed, (PyObject *)module); 11496498aff2SJonas Devlieghere auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting"); 11506498aff2SJonas Devlieghere 11516498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 11526498aff2SJonas Devlieghere Py_RETURN_NONE; 11536498aff2SJonas Devlieghere 11546498aff2SJonas Devlieghere lldb::SBTarget target_sb(target_sp); 11556498aff2SJonas Devlieghere PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(target_sb)); 11566498aff2SJonas Devlieghere auto result = pfunc(target_arg, PythonString(setting)); 11576498aff2SJonas Devlieghere 11586498aff2SJonas Devlieghere return result.release(); 11596498aff2SJonas Devlieghere} 11606498aff2SJonas Devlieghere 11616498aff2SJonas DevlieghereSWIGEXPORT bool 11626498aff2SJonas DevlieghereLLDBSWIGPythonRunScriptKeywordProcess 11636498aff2SJonas Devlieghere(const char* python_function_name, 11646498aff2SJonas Devlieghereconst char* session_dictionary_name, 11656498aff2SJonas Devliegherelldb::ProcessSP& process, 11666498aff2SJonas Devliegherestd::string& output) 11676498aff2SJonas Devlieghere 11686498aff2SJonas Devlieghere{ 11696498aff2SJonas Devlieghere if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) 11706498aff2SJonas Devlieghere return false; 11716498aff2SJonas Devlieghere 11726498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 11736498aff2SJonas Devlieghere 11746498aff2SJonas Devlieghere auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 11756498aff2SJonas Devlieghere auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 11766498aff2SJonas Devlieghere 11776498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 11786498aff2SJonas Devlieghere return false; 11796498aff2SJonas Devlieghere 11806498aff2SJonas Devlieghere lldb::SBProcess process_sb(process); 11816498aff2SJonas Devlieghere PythonObject process_arg(PyRefType::Owned, SBTypeToSWIGWrapper(process_sb)); 11826498aff2SJonas Devlieghere auto result = pfunc(process_arg, dict); 11836498aff2SJonas Devlieghere 11846498aff2SJonas Devlieghere output = result.Str().GetString().str(); 11856498aff2SJonas Devlieghere 11866498aff2SJonas Devlieghere return true; 11876498aff2SJonas Devlieghere} 11886498aff2SJonas Devlieghere 11896498aff2SJonas DevlieghereSWIGEXPORT bool 11906498aff2SJonas DevlieghereLLDBSWIGPythonRunScriptKeywordThread 11916498aff2SJonas Devlieghere(const char* python_function_name, 11926498aff2SJonas Devlieghereconst char* session_dictionary_name, 11936498aff2SJonas Devliegherelldb::ThreadSP& thread, 11946498aff2SJonas Devliegherestd::string& output) 11956498aff2SJonas Devlieghere 11966498aff2SJonas Devlieghere{ 11976498aff2SJonas Devlieghere if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) 11986498aff2SJonas Devlieghere return false; 11996498aff2SJonas Devlieghere 12006498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 12016498aff2SJonas Devlieghere 12026498aff2SJonas Devlieghere auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 12036498aff2SJonas Devlieghere auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 12046498aff2SJonas Devlieghere 12056498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 12066498aff2SJonas Devlieghere return false; 12076498aff2SJonas Devlieghere 12086498aff2SJonas Devlieghere lldb::SBThread thread_sb(thread); 12096498aff2SJonas Devlieghere PythonObject thread_arg(PyRefType::Owned, SBTypeToSWIGWrapper(thread_sb)); 12106498aff2SJonas Devlieghere auto result = pfunc(thread_arg, dict); 12116498aff2SJonas Devlieghere 12126498aff2SJonas Devlieghere output = result.Str().GetString().str(); 12136498aff2SJonas Devlieghere 12146498aff2SJonas Devlieghere return true; 12156498aff2SJonas Devlieghere} 12166498aff2SJonas Devlieghere 12176498aff2SJonas DevlieghereSWIGEXPORT bool 12186498aff2SJonas DevlieghereLLDBSWIGPythonRunScriptKeywordTarget 12196498aff2SJonas Devlieghere(const char* python_function_name, 12206498aff2SJonas Devlieghereconst char* session_dictionary_name, 12216498aff2SJonas Devliegherelldb::TargetSP& target, 12226498aff2SJonas Devliegherestd::string& output) 12236498aff2SJonas Devlieghere 12246498aff2SJonas Devlieghere{ 12256498aff2SJonas Devlieghere if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) 12266498aff2SJonas Devlieghere return false; 12276498aff2SJonas Devlieghere 12286498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 12296498aff2SJonas Devlieghere 12306498aff2SJonas Devlieghere auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 12316498aff2SJonas Devlieghere auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name,dict); 12326498aff2SJonas Devlieghere 12336498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 12346498aff2SJonas Devlieghere return false; 12356498aff2SJonas Devlieghere 12366498aff2SJonas Devlieghere lldb::SBTarget target_sb(target); 12376498aff2SJonas Devlieghere PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(target_sb)); 12386498aff2SJonas Devlieghere auto result = pfunc(target_arg, dict); 12396498aff2SJonas Devlieghere 12406498aff2SJonas Devlieghere output = result.Str().GetString().str(); 12416498aff2SJonas Devlieghere 12426498aff2SJonas Devlieghere return true; 12436498aff2SJonas Devlieghere} 12446498aff2SJonas Devlieghere 12456498aff2SJonas DevlieghereSWIGEXPORT bool 12466498aff2SJonas DevlieghereLLDBSWIGPythonRunScriptKeywordFrame 12476498aff2SJonas Devlieghere(const char* python_function_name, 12486498aff2SJonas Devlieghereconst char* session_dictionary_name, 12496498aff2SJonas Devliegherelldb::StackFrameSP& frame, 12506498aff2SJonas Devliegherestd::string& output) 12516498aff2SJonas Devlieghere 12526498aff2SJonas Devlieghere{ 12536498aff2SJonas Devlieghere if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) 12546498aff2SJonas Devlieghere return false; 12556498aff2SJonas Devlieghere 12566498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 12576498aff2SJonas Devlieghere 12586498aff2SJonas Devlieghere auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 12596498aff2SJonas Devlieghere auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name,dict); 12606498aff2SJonas Devlieghere 12616498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 12626498aff2SJonas Devlieghere return false; 12636498aff2SJonas Devlieghere 12646498aff2SJonas Devlieghere lldb::SBFrame frame_sb(frame); 12656498aff2SJonas Devlieghere PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(frame_sb)); 12666498aff2SJonas Devlieghere auto result = pfunc(frame_arg, dict); 12676498aff2SJonas Devlieghere 12686498aff2SJonas Devlieghere output = result.Str().GetString().str(); 12696498aff2SJonas Devlieghere 12706498aff2SJonas Devlieghere return true; 12716498aff2SJonas Devlieghere} 12726498aff2SJonas Devlieghere 12736498aff2SJonas DevlieghereSWIGEXPORT bool 12746498aff2SJonas DevlieghereLLDBSWIGPythonRunScriptKeywordValue 12756498aff2SJonas Devlieghere(const char* python_function_name, 12766498aff2SJonas Devlieghereconst char* session_dictionary_name, 12776498aff2SJonas Devliegherelldb::ValueObjectSP& value, 12786498aff2SJonas Devliegherestd::string& output) 12796498aff2SJonas Devlieghere 12806498aff2SJonas Devlieghere{ 12816498aff2SJonas Devlieghere if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) 12826498aff2SJonas Devlieghere return false; 12836498aff2SJonas Devlieghere 12846498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 12856498aff2SJonas Devlieghere 12866498aff2SJonas Devlieghere auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 12876498aff2SJonas Devlieghere auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 12886498aff2SJonas Devlieghere 12896498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 12906498aff2SJonas Devlieghere return false; 12916498aff2SJonas Devlieghere 12926498aff2SJonas Devlieghere lldb::SBValue value_sb(value); 12936498aff2SJonas Devlieghere PythonObject value_arg(PyRefType::Owned, SBTypeToSWIGWrapper(value_sb)); 12946498aff2SJonas Devlieghere auto result = pfunc(value_arg, dict); 12956498aff2SJonas Devlieghere 12966498aff2SJonas Devlieghere output = result.Str().GetString().str(); 12976498aff2SJonas Devlieghere 12986498aff2SJonas Devlieghere return true; 12996498aff2SJonas Devlieghere} 13006498aff2SJonas Devlieghere 13016498aff2SJonas DevlieghereSWIGEXPORT bool 13026498aff2SJonas DevlieghereLLDBSwigPythonCallModuleInit 13036498aff2SJonas Devlieghere( 13046498aff2SJonas Devlieghere const char *python_module_name, 13056498aff2SJonas Devlieghere const char *session_dictionary_name, 13066498aff2SJonas Devlieghere lldb::DebuggerSP& debugger 13076498aff2SJonas Devlieghere) 13086498aff2SJonas Devlieghere{ 13096498aff2SJonas Devlieghere std::string python_function_name_string = python_module_name; 13106498aff2SJonas Devlieghere python_function_name_string += ".__lldb_init_module"; 13116498aff2SJonas Devlieghere const char* python_function_name = python_function_name_string.c_str(); 13126498aff2SJonas Devlieghere 13136498aff2SJonas Devlieghere PyErr_Cleaner py_err_cleaner(true); 13146498aff2SJonas Devlieghere 13156498aff2SJonas Devlieghere auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); 13166498aff2SJonas Devlieghere auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict); 13176498aff2SJonas Devlieghere 13186498aff2SJonas Devlieghere // This method is optional and need not exist. So if we don't find it, 13196498aff2SJonas Devlieghere // it's actually a success, not a failure. 13206498aff2SJonas Devlieghere if (!pfunc.IsAllocated()) 13216498aff2SJonas Devlieghere return true; 13226498aff2SJonas Devlieghere 13236498aff2SJonas Devlieghere lldb::SBDebugger debugger_sb(debugger); 13246498aff2SJonas Devlieghere PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); 13256498aff2SJonas Devlieghere pfunc(debugger_arg, dict); 13266498aff2SJonas Devlieghere 13276498aff2SJonas Devlieghere return true; 13286498aff2SJonas Devlieghere} 13296498aff2SJonas Devlieghere%} 13306498aff2SJonas Devlieghere 13316498aff2SJonas Devlieghere 13326498aff2SJonas Devlieghere%runtime %{ 13336498aff2SJonas Devlieghere// Forward declaration to be inserted at the start of LLDBWrapPython.h 13346498aff2SJonas Devlieghere#include "lldb/API/SBDebugger.h" 13356498aff2SJonas Devlieghere#include "lldb/API/SBValue.h" 13366498aff2SJonas Devlieghere 13376498aff2SJonas DevlieghereSWIGEXPORT lldb::ValueObjectSP 13386498aff2SJonas DevlieghereLLDBSWIGPython_GetValueObjectSPFromSBValue (void* data) 13396498aff2SJonas Devlieghere{ 13406498aff2SJonas Devlieghere lldb::ValueObjectSP valobj_sp; 13416498aff2SJonas Devlieghere if (data) 13426498aff2SJonas Devlieghere { 13436498aff2SJonas Devlieghere lldb::SBValue* sb_ptr = (lldb::SBValue *)data; 13446498aff2SJonas Devlieghere valobj_sp = sb_ptr->GetSP(); 13456498aff2SJonas Devlieghere } 13466498aff2SJonas Devlieghere return valobj_sp; 13476498aff2SJonas Devlieghere} 13486498aff2SJonas Devlieghere 13496498aff2SJonas Devlieghere#ifdef __cplusplus 13506498aff2SJonas Devlieghereextern "C" { 13516498aff2SJonas Devlieghere#endif 13526498aff2SJonas Devlieghere 13536498aff2SJonas Devliegherevoid LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton); 13546498aff2SJonas Devlieghere 13556498aff2SJonas Devlieghere#ifdef __cplusplus 13566498aff2SJonas Devlieghere} 13576498aff2SJonas Devlieghere#endif 13586498aff2SJonas Devlieghere%} 13596498aff2SJonas Devlieghere 13606498aff2SJonas Devlieghere%wrapper %{ 13616498aff2SJonas Devlieghere 13626498aff2SJonas Devlieghere 13636498aff2SJonas Devlieghere// For the LogOutputCallback functions 13646498aff2SJonas Devliegherevoid LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) { 13656498aff2SJonas Devlieghere if (baton != Py_None) { 13666498aff2SJonas Devlieghere SWIG_PYTHON_THREAD_BEGIN_BLOCK; 13676498aff2SJonas Devlieghere PyObject *result = PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str); 13686498aff2SJonas Devlieghere Py_XDECREF(result); 13696498aff2SJonas Devlieghere SWIG_PYTHON_THREAD_END_BLOCK; 13706498aff2SJonas Devlieghere } 13716498aff2SJonas Devlieghere} 13726498aff2SJonas Devlieghere%} 1373