diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2023-04-14 21:41:27 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2023-06-22 18:20:56 +0000 |
commit | bdd1243df58e60e85101c09001d9812a789b6bc4 (patch) | |
tree | a1ce621c7301dd47ba2ddc3b8eaa63b441389481 /contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp | |
parent | 781624ca2d054430052c828ba8d2c2eaf2d733e7 (diff) | |
parent | e3b557809604d036af6e00c60f012c2025b59a5e (diff) |
Diffstat (limited to 'contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp')
-rw-r--r-- | contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp | 316 |
1 files changed, 87 insertions, 229 deletions
diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp index a21adcfbdbd6..7026815e120d 100644 --- a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp @@ -18,6 +18,7 @@ #include "PythonReadline.h" #include "SWIGPythonBridge.h" #include "ScriptInterpreterPythonImpl.h" +#include "ScriptedPlatformPythonInterface.h" #include "ScriptedProcessPythonInterface.h" #include "lldb/API/SBError.h" @@ -25,9 +26,9 @@ #include "lldb/API/SBValue.h" #include "lldb/Breakpoint/StoppointCallbackContext.h" #include "lldb/Breakpoint/WatchpointOptions.h" -#include "lldb/Core/Communication.h" #include "lldb/Core/Debugger.h" #include "lldb/Core/PluginManager.h" +#include "lldb/Core/ThreadedCommunication.h" #include "lldb/Core/ValueObject.h" #include "lldb/DataFormatters/TypeSummary.h" #include "lldb/Host/FileSystem.h" @@ -50,6 +51,7 @@ #include <cstdlib> #include <memory> #include <mutex> +#include <optional> #include <string> using namespace lldb; @@ -96,7 +98,7 @@ public: // Python's readline is incompatible with libedit being linked into lldb. // Provide a patched version local to the embedded interpreter. bool ReadlinePatched = false; - for (auto *p = PyImport_Inittab; p->name != NULL; p++) { + for (auto *p = PyImport_Inittab; p->name != nullptr; p++) { if (strcmp(p->name, "readline") == 0) { p->initfunc = initlldb_readline; break; @@ -241,7 +243,7 @@ void ScriptInterpreterPython::ComputePythonDir( llvm::sys::path::append(path, LLDB_PYTHON_RELATIVE_LIBDIR); #if defined(_WIN32) - // This will be injected directly through FileSpec.GetDirectory().SetString(), + // This will be injected directly through FileSpec.SetDirectory(), // so we need to normalize manually. std::replace(path.begin(), path.end(), '\\', '/'); #endif @@ -260,7 +262,7 @@ FileSpec ScriptInterpreterPython::GetPythonDir() { #else ComputePythonDir(path); #endif - spec.GetDirectory().SetString(path); + spec.SetDirectory(path); return spec; }(); return g_spec; @@ -412,6 +414,8 @@ ScriptInterpreterPythonImpl::ScriptInterpreterPythonImpl(Debugger &debugger) m_command_thread_state(nullptr) { m_scripted_process_interface_up = std::make_unique<ScriptedProcessPythonInterface>(*this); + m_scripted_platform_interface_up = + std::make_unique<ScriptedPlatformPythonInterface>(*this); m_dictionary_name.append("_dict"); StreamString run_string; @@ -429,7 +433,7 @@ ScriptInterpreterPythonImpl::ScriptInterpreterPythonImpl(Debugger &debugger) // Reloading modules requires a different syntax in Python 2 and Python 3. // This provides a consistent syntax no matter what version of Python. run_string.Clear(); - run_string.Printf("run_one_line (%s, 'from six.moves import reload_module')", + run_string.Printf("run_one_line (%s, 'from importlib import reload as reload_module')", m_dictionary_name.c_str()); PyRun_SimpleString(run_string.GetData()); @@ -1259,8 +1263,6 @@ void ScriptInterpreterPythonImpl::SetWatchpointCommandCallback( wp_options->SetCallback( ScriptInterpreterPythonImpl::WatchpointCallbackFunction, baton_sp); } - - return; } Status ScriptInterpreterPythonImpl::ExportFunctionDefinitionToInterpreter( @@ -1371,7 +1373,7 @@ bool ScriptInterpreterPythonImpl::GenerateScriptAliasFunction( std::string auto_generated_function_name(GenerateUniqueName( "lldb_autogen_python_cmd_alias_func", num_created_functions)); - sstr.Printf("def %s (debugger, args, result, internal_dict):", + sstr.Printf("def %s (debugger, args, exe_ctx, result, internal_dict):", auto_generated_function_name.c_str()); if (!GenerateFunction(sstr.GetData(), user_input).Success()) @@ -1503,50 +1505,29 @@ StructuredData::DictionarySP ScriptInterpreterPythonImpl::OSPlugin_RegisterInfo( StructuredData::ObjectSP os_plugin_object_sp) { Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); - static char callee_name[] = "get_register_info"; - if (!os_plugin_object_sp) - return StructuredData::DictionarySP(); + return {}; StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric(); if (!generic) - return nullptr; + return {}; PythonObject implementor(PyRefType::Borrowed, (PyObject *)generic->GetValue()); if (!implementor.IsAllocated()) - return StructuredData::DictionarySP(); - - PythonObject pmeth(PyRefType::Owned, - PyObject_GetAttrString(implementor.get(), callee_name)); - - if (PyErr_Occurred()) - PyErr_Clear(); - - if (!pmeth.IsAllocated()) - return StructuredData::DictionarySP(); + return {}; - if (PyCallable_Check(pmeth.get()) == 0) { - if (PyErr_Occurred()) - PyErr_Clear(); + llvm::Expected<PythonObject> expected_py_return = + implementor.CallMethod("get_register_info"); - return StructuredData::DictionarySP(); + if (!expected_py_return) { + llvm::consumeError(expected_py_return.takeError()); + return {}; } - if (PyErr_Occurred()) - PyErr_Clear(); - - // right now we know this function exists and is callable.. - PythonObject py_return( - PyRefType::Owned, - PyObject_CallMethod(implementor.get(), callee_name, nullptr)); + PythonObject py_return = std::move(expected_py_return.get()); - // if it fails, print the error but otherwise go on - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - } if (py_return.get()) { PythonDictionary result_dict(PyRefType::Borrowed, py_return.get()); return result_dict.CreateStructuredDictionary(); @@ -1557,51 +1538,28 @@ StructuredData::DictionarySP ScriptInterpreterPythonImpl::OSPlugin_RegisterInfo( StructuredData::ArraySP ScriptInterpreterPythonImpl::OSPlugin_ThreadsInfo( StructuredData::ObjectSP os_plugin_object_sp) { Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); - - static char callee_name[] = "get_thread_info"; - if (!os_plugin_object_sp) - return StructuredData::ArraySP(); + return {}; StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric(); if (!generic) - return nullptr; + return {}; PythonObject implementor(PyRefType::Borrowed, (PyObject *)generic->GetValue()); if (!implementor.IsAllocated()) - return StructuredData::ArraySP(); - - PythonObject pmeth(PyRefType::Owned, - PyObject_GetAttrString(implementor.get(), callee_name)); + return {}; - if (PyErr_Occurred()) - PyErr_Clear(); + llvm::Expected<PythonObject> expected_py_return = + implementor.CallMethod("get_thread_info"); - if (!pmeth.IsAllocated()) - return StructuredData::ArraySP(); - - if (PyCallable_Check(pmeth.get()) == 0) { - if (PyErr_Occurred()) - PyErr_Clear(); - - return StructuredData::ArraySP(); + if (!expected_py_return) { + llvm::consumeError(expected_py_return.takeError()); + return {}; } - if (PyErr_Occurred()) - PyErr_Clear(); - - // right now we know this function exists and is callable.. - PythonObject py_return( - PyRefType::Owned, - PyObject_CallMethod(implementor.get(), callee_name, nullptr)); - - // if it fails, print the error but otherwise go on - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - } + PythonObject py_return = std::move(expected_py_return.get()); if (py_return.get()) { PythonList result_list(PyRefType::Borrowed, py_return.get()); @@ -1615,56 +1573,33 @@ ScriptInterpreterPythonImpl::OSPlugin_RegisterContextData( StructuredData::ObjectSP os_plugin_object_sp, lldb::tid_t tid) { Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); - static char callee_name[] = "get_register_data"; - static char *param_format = - const_cast<char *>(GetPythonValueFormatString(tid)); - if (!os_plugin_object_sp) - return StructuredData::StringSP(); + return {}; StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric(); if (!generic) - return nullptr; + return {}; PythonObject implementor(PyRefType::Borrowed, (PyObject *)generic->GetValue()); if (!implementor.IsAllocated()) - return StructuredData::StringSP(); - - PythonObject pmeth(PyRefType::Owned, - PyObject_GetAttrString(implementor.get(), callee_name)); - - if (PyErr_Occurred()) - PyErr_Clear(); + return {}; - if (!pmeth.IsAllocated()) - return StructuredData::StringSP(); + llvm::Expected<PythonObject> expected_py_return = + implementor.CallMethod("get_register_data", tid); - if (PyCallable_Check(pmeth.get()) == 0) { - if (PyErr_Occurred()) - PyErr_Clear(); - return StructuredData::StringSP(); + if (!expected_py_return) { + llvm::consumeError(expected_py_return.takeError()); + return {}; } - if (PyErr_Occurred()) - PyErr_Clear(); - - // right now we know this function exists and is callable.. - PythonObject py_return( - PyRefType::Owned, - PyObject_CallMethod(implementor.get(), callee_name, param_format, tid)); - - // if it fails, print the error but otherwise go on - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - } + PythonObject py_return = std::move(expected_py_return.get()); if (py_return.get()) { PythonBytes result(PyRefType::Borrowed, py_return.get()); return result.CreateStructuredString(); } - return StructuredData::StringSP(); + return {}; } StructuredData::DictionarySP ScriptInterpreterPythonImpl::OSPlugin_CreateThread( @@ -1672,52 +1607,28 @@ StructuredData::DictionarySP ScriptInterpreterPythonImpl::OSPlugin_CreateThread( lldb::addr_t context) { Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); - static char callee_name[] = "create_thread"; - std::string param_format; - param_format += GetPythonValueFormatString(tid); - param_format += GetPythonValueFormatString(context); - if (!os_plugin_object_sp) - return StructuredData::DictionarySP(); + return {}; StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric(); if (!generic) - return nullptr; + return {}; PythonObject implementor(PyRefType::Borrowed, (PyObject *)generic->GetValue()); if (!implementor.IsAllocated()) - return StructuredData::DictionarySP(); - - PythonObject pmeth(PyRefType::Owned, - PyObject_GetAttrString(implementor.get(), callee_name)); - - if (PyErr_Occurred()) - PyErr_Clear(); + return {}; - if (!pmeth.IsAllocated()) - return StructuredData::DictionarySP(); + llvm::Expected<PythonObject> expected_py_return = + implementor.CallMethod("create_thread", tid, context); - if (PyCallable_Check(pmeth.get()) == 0) { - if (PyErr_Occurred()) - PyErr_Clear(); - return StructuredData::DictionarySP(); + if (!expected_py_return) { + llvm::consumeError(expected_py_return.takeError()); + return {}; } - if (PyErr_Occurred()) - PyErr_Clear(); - - // right now we know this function exists and is callable.. - PythonObject py_return(PyRefType::Owned, - PyObject_CallMethod(implementor.get(), callee_name, - ¶m_format[0], tid, context)); - - // if it fails, print the error but otherwise go on - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - } + PythonObject py_return = std::move(expected_py_return.get()); if (py_return.get()) { PythonDictionary result_dict(PyRefType::Borrowed, py_return.get()); @@ -2156,6 +2067,14 @@ bool ScriptInterpreterPythonImpl::GetScriptedSummary( return ret_val; } +bool ScriptInterpreterPythonImpl::FormatterCallbackFunction( + const char *python_function_name, TypeImplSP type_impl_sp) { + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + return LLDBSwigPythonFormatterCallbackFunction( + python_function_name, m_dictionary_name.c_str(), type_impl_sp); +} + bool ScriptInterpreterPythonImpl::BreakpointCallbackFunction( void *baton, StoppointCallbackContext *context, user_id_t break_id, user_id_t break_loc_id) { @@ -2430,51 +2349,31 @@ ConstString ScriptInterpreterPythonImpl::GetSyntheticTypeName( Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - static char callee_name[] = "get_type_name"; - - ConstString ret_val; - bool got_string = false; - std::string buffer; - if (!implementor_sp) - return ret_val; + return {}; StructuredData::Generic *generic = implementor_sp->GetAsGeneric(); if (!generic) - return ret_val; + return {}; + PythonObject implementor(PyRefType::Borrowed, (PyObject *)generic->GetValue()); if (!implementor.IsAllocated()) - return ret_val; - - PythonObject pmeth(PyRefType::Owned, - PyObject_GetAttrString(implementor.get(), callee_name)); + return {}; - if (PyErr_Occurred()) - PyErr_Clear(); + llvm::Expected<PythonObject> expected_py_return = + implementor.CallMethod("get_type_name"); - if (!pmeth.IsAllocated()) - return ret_val; - - if (PyCallable_Check(pmeth.get()) == 0) { - if (PyErr_Occurred()) - PyErr_Clear(); - return ret_val; + if (!expected_py_return) { + llvm::consumeError(expected_py_return.takeError()); + return {}; } - if (PyErr_Occurred()) - PyErr_Clear(); + PythonObject py_return = std::move(expected_py_return.get()); - // right now we know this function exists and is callable.. - PythonObject py_return( - PyRefType::Owned, - PyObject_CallMethod(implementor.get(), callee_name, nullptr)); - - // if it fails, print the error but otherwise go on - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - } + ConstString ret_val; + bool got_string = false; + std::string buffer; if (py_return.IsAllocated() && PythonString::Check(py_return.get())) { PythonString py_string(PyRefType::Borrowed, py_return.get()); @@ -2530,7 +2429,7 @@ bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword( Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - if (llvm::Optional<std::string> result = LLDBSWIGPythonRunScriptKeywordThread( + if (std::optional<std::string> result = LLDBSWIGPythonRunScriptKeywordThread( impl_function, m_dictionary_name.c_str(), thread->shared_from_this())) { output = std::move(*result); @@ -2579,7 +2478,7 @@ bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword( Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - if (llvm::Optional<std::string> result = LLDBSWIGPythonRunScriptKeywordFrame( + if (std::optional<std::string> result = LLDBSWIGPythonRunScriptKeywordFrame( impl_function, m_dictionary_name.c_str(), frame->shared_from_this())) { output = std::move(*result); @@ -2888,9 +2787,10 @@ bool ScriptInterpreterPythonImpl::RunScriptBasedCommand( if (!ret_val) error.SetErrorString("unable to execute script function"); - else - error.Clear(); + else if (cmd_retobj.GetStatus() == eReturnStatusFailed) + return false; + error.Clear(); return ret_val; } @@ -2932,9 +2832,10 @@ bool ScriptInterpreterPythonImpl::RunScriptBasedCommand( if (!ret_val) error.SetErrorString("unable to execute script function"); - else - error.Clear(); + else if (cmd_retobj.GetStatus() == eReturnStatusFailed) + return false; + error.Clear(); return ret_val; } @@ -2978,8 +2879,6 @@ bool ScriptInterpreterPythonImpl::GetShortHelpForCommandObject( Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); - static char callee_name[] = "get_short_help"; - if (!cmd_obj_sp) return false; @@ -2989,34 +2888,15 @@ bool ScriptInterpreterPythonImpl::GetShortHelpForCommandObject( if (!implementor.IsAllocated()) return false; - PythonObject pmeth(PyRefType::Owned, - PyObject_GetAttrString(implementor.get(), callee_name)); - - if (PyErr_Occurred()) - PyErr_Clear(); - - if (!pmeth.IsAllocated()) - return false; + llvm::Expected<PythonObject> expected_py_return = + implementor.CallMethod("get_short_help"); - if (PyCallable_Check(pmeth.get()) == 0) { - if (PyErr_Occurred()) - PyErr_Clear(); + if (!expected_py_return) { + llvm::consumeError(expected_py_return.takeError()); return false; } - if (PyErr_Occurred()) - PyErr_Clear(); - - // Right now we know this function exists and is callable. - PythonObject py_return( - PyRefType::Owned, - PyObject_CallMethod(implementor.get(), callee_name, nullptr)); - - // If it fails, print the error but otherwise go on. - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - } + PythonObject py_return = std::move(expected_py_return.get()); if (py_return.IsAllocated() && PythonString::Check(py_return.get())) { PythonString py_string(PyRefType::Borrowed, py_return.get()); @@ -3079,13 +2959,10 @@ uint32_t ScriptInterpreterPythonImpl::GetFlagsForCommandObject( bool ScriptInterpreterPythonImpl::GetLongHelpForCommandObject( StructuredData::GenericSP cmd_obj_sp, std::string &dest) { - bool got_string = false; dest.clear(); Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); - static char callee_name[] = "get_long_help"; - if (!cmd_obj_sp) return false; @@ -3095,36 +2972,17 @@ bool ScriptInterpreterPythonImpl::GetLongHelpForCommandObject( if (!implementor.IsAllocated()) return false; - PythonObject pmeth(PyRefType::Owned, - PyObject_GetAttrString(implementor.get(), callee_name)); - - if (PyErr_Occurred()) - PyErr_Clear(); - - if (!pmeth.IsAllocated()) - return false; - - if (PyCallable_Check(pmeth.get()) == 0) { - if (PyErr_Occurred()) - PyErr_Clear(); + llvm::Expected<PythonObject> expected_py_return = + implementor.CallMethod("get_long_help"); + if (!expected_py_return) { + llvm::consumeError(expected_py_return.takeError()); return false; } - if (PyErr_Occurred()) - PyErr_Clear(); - - // right now we know this function exists and is callable.. - PythonObject py_return( - PyRefType::Owned, - PyObject_CallMethod(implementor.get(), callee_name, nullptr)); - - // if it fails, print the error but otherwise go on - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - } + PythonObject py_return = std::move(expected_py_return.get()); + bool got_string = false; if (py_return.IsAllocated() && PythonString::Check(py_return.get())) { PythonString str(PyRefType::Borrowed, py_return.get()); llvm::StringRef str_data(str.GetString()); |