diff options
Diffstat (limited to 'contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python')
21 files changed, 8033 insertions, 0 deletions
diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/OperatingSystemPythonInterface.cpp b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/OperatingSystemPythonInterface.cpp new file mode 100644 index 000000000000..c162c7367c65 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/OperatingSystemPythonInterface.cpp @@ -0,0 +1,82 @@ +//===-- ScriptedThreadPythonInterface.cpp ---------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "lldb/Host/Config.h" +#include "lldb/Target/ExecutionContext.h" +#include "lldb/Utility/Log.h" +#include "lldb/lldb-enumerations.h" + +#if LLDB_ENABLE_PYTHON + +// LLDB Python header must be included first +#include "../lldb-python.h" + +#include "../SWIGPythonBridge.h" +#include "../ScriptInterpreterPythonImpl.h" +#include "OperatingSystemPythonInterface.h" + +using namespace lldb; +using namespace lldb_private; +using namespace lldb_private::python; +using Locker = ScriptInterpreterPythonImpl::Locker; + +OperatingSystemPythonInterface::OperatingSystemPythonInterface( + ScriptInterpreterPythonImpl &interpreter) + : OperatingSystemInterface(), ScriptedThreadPythonInterface(interpreter) {} + +llvm::Expected<StructuredData::GenericSP> +OperatingSystemPythonInterface::CreatePluginObject( + llvm::StringRef class_name, ExecutionContext &exe_ctx, + StructuredData::DictionarySP args_sp, StructuredData::Generic *script_obj) { + return ScriptedPythonInterface::CreatePluginObject(class_name, nullptr, + exe_ctx.GetProcessSP()); +} + +StructuredData::DictionarySP +OperatingSystemPythonInterface::CreateThread(lldb::tid_t tid, + lldb::addr_t context) { + Status error; + StructuredData::DictionarySP dict = Dispatch<StructuredData::DictionarySP>( + "create_thread", error, tid, context); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict, + error)) + return {}; + + return dict; +} + +StructuredData::ArraySP OperatingSystemPythonInterface::GetThreadInfo() { + Status error; + StructuredData::ArraySP arr = + Dispatch<StructuredData::ArraySP>("get_thread_info", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, arr, + error)) + return {}; + + return arr; +} + +StructuredData::DictionarySP OperatingSystemPythonInterface::GetRegisterInfo() { + return ScriptedThreadPythonInterface::GetRegisterInfo(); +} + +std::optional<std::string> +OperatingSystemPythonInterface::GetRegisterContextForTID(lldb::tid_t tid) { + Status error; + StructuredData::ObjectSP obj = Dispatch("get_register_data", error, tid); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, + error)) + return {}; + + return obj->GetAsString()->GetValue().str(); +} + +#endif diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/OperatingSystemPythonInterface.h b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/OperatingSystemPythonInterface.h new file mode 100644 index 000000000000..da7bbf13b1d5 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/OperatingSystemPythonInterface.h @@ -0,0 +1,48 @@ +//===-- OperatingSystemPythonInterface.h ------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_OPERATINGSYSTEMPYTHONINTERFACE_H +#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_OPERATINGSYSTEMPYTHONINTERFACE_H + +#include "lldb/Host/Config.h" + +#if LLDB_ENABLE_PYTHON + +#include "ScriptedThreadPythonInterface.h" +#include "lldb/Interpreter/Interfaces/OperatingSystemInterface.h" +#include <optional> + +namespace lldb_private { +class OperatingSystemPythonInterface + : virtual public OperatingSystemInterface, + virtual public ScriptedThreadPythonInterface { +public: + OperatingSystemPythonInterface(ScriptInterpreterPythonImpl &interpreter); + + llvm::Expected<StructuredData::GenericSP> + CreatePluginObject(llvm::StringRef class_name, ExecutionContext &exe_ctx, + StructuredData::DictionarySP args_sp, + StructuredData::Generic *script_obj = nullptr) override; + + llvm::SmallVector<llvm::StringLiteral> GetAbstractMethods() const override { + return llvm::SmallVector<llvm::StringLiteral>({"get_thread_info"}); + } + + StructuredData::DictionarySP CreateThread(lldb::tid_t tid, + lldb::addr_t context) override; + + StructuredData::ArraySP GetThreadInfo() override; + + StructuredData::DictionarySP GetRegisterInfo() override; + + std::optional<std::string> GetRegisterContextForTID(lldb::tid_t tid) override; +}; +} // namespace lldb_private + +#endif // LLDB_ENABLE_PYTHON +#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_OPERATINGSYSTEMPYTHONINTERFACE_H diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPlatformPythonInterface.cpp b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPlatformPythonInterface.cpp new file mode 100644 index 000000000000..6e93bec80056 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPlatformPythonInterface.cpp @@ -0,0 +1,96 @@ +//===-- ScriptedPlatformPythonInterface.cpp -------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "lldb/Host/Config.h" +#include "lldb/Utility/Log.h" +#include "lldb/Utility/Status.h" +#include "lldb/lldb-enumerations.h" + +#if LLDB_ENABLE_PYTHON + +// LLDB Python header must be included first +#include "../lldb-python.h" + +#include "../SWIGPythonBridge.h" +#include "../ScriptInterpreterPythonImpl.h" +#include "ScriptedPlatformPythonInterface.h" + +#include "lldb/Target/ExecutionContext.h" + +using namespace lldb; +using namespace lldb_private; +using namespace lldb_private::python; +using Locker = ScriptInterpreterPythonImpl::Locker; + +ScriptedPlatformPythonInterface::ScriptedPlatformPythonInterface( + ScriptInterpreterPythonImpl &interpreter) + : ScriptedPlatformInterface(), ScriptedPythonInterface(interpreter) {} + +llvm::Expected<StructuredData::GenericSP> +ScriptedPlatformPythonInterface::CreatePluginObject( + llvm::StringRef class_name, ExecutionContext &exe_ctx, + StructuredData::DictionarySP args_sp, StructuredData::Generic *script_obj) { + ExecutionContextRefSP exe_ctx_ref_sp = + std::make_shared<ExecutionContextRef>(exe_ctx); + StructuredDataImpl sd_impl(args_sp); + return ScriptedPythonInterface::CreatePluginObject(class_name, script_obj, + exe_ctx_ref_sp, sd_impl); +} + +StructuredData::DictionarySP ScriptedPlatformPythonInterface::ListProcesses() { + Status error; + StructuredData::DictionarySP dict_sp = + Dispatch<StructuredData::DictionarySP>("list_processes", error); + + if (!dict_sp || !dict_sp->IsValid() || error.Fail()) { + return ScriptedInterface::ErrorWithMessage<StructuredData::DictionarySP>( + LLVM_PRETTY_FUNCTION, + llvm::Twine("Null or invalid object (" + + llvm::Twine(error.AsCString()) + llvm::Twine(").")) + .str(), + error); + } + + return dict_sp; +} + +StructuredData::DictionarySP +ScriptedPlatformPythonInterface::GetProcessInfo(lldb::pid_t pid) { + Status error; + StructuredData::DictionarySP dict_sp = + Dispatch<StructuredData::DictionarySP>("get_process_info", error, pid); + + if (!dict_sp || !dict_sp->IsValid() || error.Fail()) { + return ScriptedInterface::ErrorWithMessage<StructuredData::DictionarySP>( + LLVM_PRETTY_FUNCTION, + llvm::Twine("Null or invalid object (" + + llvm::Twine(error.AsCString()) + llvm::Twine(").")) + .str(), + error); + } + + return dict_sp; +} + +Status ScriptedPlatformPythonInterface::AttachToProcess( + ProcessAttachInfoSP attach_info) { + // FIXME: Pass `attach_info` to method call + return GetStatusFromMethod("attach_to_process"); +} + +Status ScriptedPlatformPythonInterface::LaunchProcess( + ProcessLaunchInfoSP launch_info) { + // FIXME: Pass `launch_info` to method call + return GetStatusFromMethod("launch_process"); +} + +Status ScriptedPlatformPythonInterface::KillProcess(lldb::pid_t pid) { + return GetStatusFromMethod("kill_process", pid); +} + +#endif // LLDB_ENABLE_PYTHON diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPlatformPythonInterface.h b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPlatformPythonInterface.h new file mode 100644 index 000000000000..0842d3a00342 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPlatformPythonInterface.h @@ -0,0 +1,50 @@ +//===-- ScriptedPlatformPythonInterface.h -----------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDPLATFORMPYTHONINTERFACE_H +#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDPLATFORMPYTHONINTERFACE_H + +#include "lldb/Host/Config.h" + +#if LLDB_ENABLE_PYTHON + +#include "ScriptedPythonInterface.h" +#include "lldb/Interpreter/Interfaces/ScriptedPlatformInterface.h" + +namespace lldb_private { +class ScriptedPlatformPythonInterface : public ScriptedPlatformInterface, + public ScriptedPythonInterface { +public: + ScriptedPlatformPythonInterface(ScriptInterpreterPythonImpl &interpreter); + + llvm::Expected<StructuredData::GenericSP> + CreatePluginObject(const llvm::StringRef class_name, + ExecutionContext &exe_ctx, + StructuredData::DictionarySP args_sp, + StructuredData::Generic *script_obj = nullptr) override; + + llvm::SmallVector<llvm::StringLiteral> GetAbstractMethods() const override { + return llvm::SmallVector<llvm::StringLiteral>( + {"list_processes", "attach_to_process", "launch_process", + "kill_process"}); + } + + StructuredData::DictionarySP ListProcesses() override; + + StructuredData::DictionarySP GetProcessInfo(lldb::pid_t) override; + + Status AttachToProcess(lldb::ProcessAttachInfoSP attach_info) override; + + Status LaunchProcess(lldb::ProcessLaunchInfoSP launch_info) override; + + Status KillProcess(lldb::pid_t pid) override; +}; +} // namespace lldb_private + +#endif // LLDB_ENABLE_PYTHON +#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDPLATFORMPYTHONINTERFACE_H diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedProcessPythonInterface.cpp b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedProcessPythonInterface.cpp new file mode 100644 index 000000000000..313c597ce48f --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedProcessPythonInterface.cpp @@ -0,0 +1,211 @@ +//===-- ScriptedProcessPythonInterface.cpp --------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "lldb/Host/Config.h" +#if LLDB_ENABLE_PYTHON +// LLDB Python header must be included first +#include "../lldb-python.h" +#endif +#include "lldb/Target/Process.h" +#include "lldb/Utility/Log.h" +#include "lldb/Utility/Status.h" +#include "lldb/lldb-enumerations.h" + +#if LLDB_ENABLE_PYTHON + +#include "../SWIGPythonBridge.h" +#include "../ScriptInterpreterPythonImpl.h" +#include "ScriptedProcessPythonInterface.h" +#include "ScriptedThreadPythonInterface.h" +#include <optional> + +using namespace lldb; +using namespace lldb_private; +using namespace lldb_private::python; +using Locker = ScriptInterpreterPythonImpl::Locker; + +ScriptedProcessPythonInterface::ScriptedProcessPythonInterface( + ScriptInterpreterPythonImpl &interpreter) + : ScriptedProcessInterface(), ScriptedPythonInterface(interpreter) {} + +llvm::Expected<StructuredData::GenericSP> +ScriptedProcessPythonInterface::CreatePluginObject( + llvm::StringRef class_name, ExecutionContext &exe_ctx, + StructuredData::DictionarySP args_sp, StructuredData::Generic *script_obj) { + ExecutionContextRefSP exe_ctx_ref_sp = + std::make_shared<ExecutionContextRef>(exe_ctx); + StructuredDataImpl sd_impl(args_sp); + return ScriptedPythonInterface::CreatePluginObject(class_name, script_obj, + exe_ctx_ref_sp, sd_impl); +} + +StructuredData::DictionarySP ScriptedProcessPythonInterface::GetCapabilities() { + Status error; + StructuredData::DictionarySP dict = + Dispatch<StructuredData::DictionarySP>("get_capabilities", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict, + error)) + return {}; + + return dict; +} + +Status +ScriptedProcessPythonInterface::Attach(const ProcessAttachInfo &attach_info) { + lldb::ProcessAttachInfoSP attach_info_sp = + std::make_shared<ProcessAttachInfo>(attach_info); + return GetStatusFromMethod("attach", attach_info_sp); +} + +Status ScriptedProcessPythonInterface::Launch() { + return GetStatusFromMethod("launch"); +} + +Status ScriptedProcessPythonInterface::Resume() { + // When calling ScriptedProcess.Resume from lldb we should always stop. + return GetStatusFromMethod("resume", /*should_stop=*/true); +} + +std::optional<MemoryRegionInfo> +ScriptedProcessPythonInterface::GetMemoryRegionContainingAddress( + lldb::addr_t address, Status &error) { + auto mem_region = Dispatch<std::optional<MemoryRegionInfo>>( + "get_memory_region_containing_address", error, address); + + if (error.Fail()) { + return ErrorWithMessage<MemoryRegionInfo>(LLVM_PRETTY_FUNCTION, + error.AsCString(), error); + } + + return mem_region; +} + +StructuredData::DictionarySP ScriptedProcessPythonInterface::GetThreadsInfo() { + Status error; + StructuredData::DictionarySP dict = + Dispatch<StructuredData::DictionarySP>("get_threads_info", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict, + error)) + return {}; + + return dict; +} + +bool ScriptedProcessPythonInterface::CreateBreakpoint(lldb::addr_t addr, + Status &error) { + Status py_error; + StructuredData::ObjectSP obj = + Dispatch("create_breakpoint", py_error, addr, error); + + // If there was an error on the python call, surface it to the user. + if (py_error.Fail()) + error = py_error; + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, + error)) + return {}; + + return obj->GetBooleanValue(); +} + +lldb::DataExtractorSP ScriptedProcessPythonInterface::ReadMemoryAtAddress( + lldb::addr_t address, size_t size, Status &error) { + Status py_error; + lldb::DataExtractorSP data_sp = Dispatch<lldb::DataExtractorSP>( + "read_memory_at_address", py_error, address, size, error); + + // If there was an error on the python call, surface it to the user. + if (py_error.Fail()) + error = py_error; + + return data_sp; +} + +lldb::offset_t ScriptedProcessPythonInterface::WriteMemoryAtAddress( + lldb::addr_t addr, lldb::DataExtractorSP data_sp, Status &error) { + Status py_error; + StructuredData::ObjectSP obj = + Dispatch("write_memory_at_address", py_error, addr, data_sp, error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, + error)) + return LLDB_INVALID_OFFSET; + + // If there was an error on the python call, surface it to the user. + if (py_error.Fail()) + error = py_error; + + return obj->GetUnsignedIntegerValue(LLDB_INVALID_OFFSET); +} + +StructuredData::ArraySP ScriptedProcessPythonInterface::GetLoadedImages() { + Status error; + StructuredData::ArraySP array = + Dispatch<StructuredData::ArraySP>("get_loaded_images", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, array, + error)) + return {}; + + return array; +} + +lldb::pid_t ScriptedProcessPythonInterface::GetProcessID() { + Status error; + StructuredData::ObjectSP obj = Dispatch("get_process_id", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, + error)) + return LLDB_INVALID_PROCESS_ID; + + return obj->GetUnsignedIntegerValue(LLDB_INVALID_PROCESS_ID); +} + +bool ScriptedProcessPythonInterface::IsAlive() { + Status error; + StructuredData::ObjectSP obj = Dispatch("is_alive", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, + error)) + return {}; + + return obj->GetBooleanValue(); +} + +std::optional<std::string> +ScriptedProcessPythonInterface::GetScriptedThreadPluginName() { + Status error; + StructuredData::ObjectSP obj = Dispatch("get_scripted_thread_plugin", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, + error)) + return {}; + + return obj->GetStringValue().str(); +} + +lldb::ScriptedThreadInterfaceSP +ScriptedProcessPythonInterface::CreateScriptedThreadInterface() { + return m_interpreter.CreateScriptedThreadInterface(); +} + +StructuredData::DictionarySP ScriptedProcessPythonInterface::GetMetadata() { + Status error; + StructuredData::DictionarySP dict = + Dispatch<StructuredData::DictionarySP>("get_process_metadata", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict, + error)) + return {}; + + return dict; +} + +#endif diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedProcessPythonInterface.h b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedProcessPythonInterface.h new file mode 100644 index 000000000000..c75caa9340f2 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedProcessPythonInterface.h @@ -0,0 +1,76 @@ +//===-- ScriptedProcessPythonInterface.h ------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDPROCESSPYTHONINTERFACE_H +#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDPROCESSPYTHONINTERFACE_H + +#include "lldb/Host/Config.h" + +#if LLDB_ENABLE_PYTHON + +#include "ScriptedPythonInterface.h" +#include "lldb/Interpreter/Interfaces/ScriptedProcessInterface.h" +#include <optional> + +namespace lldb_private { +class ScriptedProcessPythonInterface : public ScriptedProcessInterface, + public ScriptedPythonInterface { +public: + ScriptedProcessPythonInterface(ScriptInterpreterPythonImpl &interpreter); + + llvm::Expected<StructuredData::GenericSP> + CreatePluginObject(const llvm::StringRef class_name, + ExecutionContext &exe_ctx, + StructuredData::DictionarySP args_sp, + StructuredData::Generic *script_obj = nullptr) override; + + llvm::SmallVector<llvm::StringLiteral> GetAbstractMethods() const override { + return llvm::SmallVector<llvm::StringLiteral>( + {"read_memory_at_address", "is_alive", "get_scripted_thread_plugin"}); + } + + StructuredData::DictionarySP GetCapabilities() override; + + Status Attach(const ProcessAttachInfo &attach_info) override; + + Status Launch() override; + + Status Resume() override; + + std::optional<MemoryRegionInfo> + GetMemoryRegionContainingAddress(lldb::addr_t address, + Status &error) override; + + StructuredData::DictionarySP GetThreadsInfo() override; + + bool CreateBreakpoint(lldb::addr_t addr, Status &error) override; + + lldb::DataExtractorSP ReadMemoryAtAddress(lldb::addr_t address, size_t size, + Status &error) override; + + lldb::offset_t WriteMemoryAtAddress(lldb::addr_t addr, + lldb::DataExtractorSP data_sp, + Status &error) override; + + StructuredData::ArraySP GetLoadedImages() override; + + lldb::pid_t GetProcessID() override; + + bool IsAlive() override; + + std::optional<std::string> GetScriptedThreadPluginName() override; + + StructuredData::DictionarySP GetMetadata() override; + +private: + lldb::ScriptedThreadInterfaceSP CreateScriptedThreadInterface() override; +}; +} // namespace lldb_private + +#endif // LLDB_ENABLE_PYTHON +#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDPROCESSPYTHONINTERFACE_H diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPythonInterface.cpp b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPythonInterface.cpp new file mode 100644 index 000000000000..699412e437a1 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPythonInterface.cpp @@ -0,0 +1,159 @@ +//===-- ScriptedPythonInterface.cpp ---------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "lldb/Host/Config.h" +#include "lldb/Utility/Log.h" +#include "lldb/lldb-enumerations.h" + +#if LLDB_ENABLE_PYTHON + +// LLDB Python header must be included first +#include "../lldb-python.h" + +#include "../ScriptInterpreterPythonImpl.h" +#include "ScriptedPythonInterface.h" +#include <optional> + +using namespace lldb; +using namespace lldb_private; + +ScriptedPythonInterface::ScriptedPythonInterface( + ScriptInterpreterPythonImpl &interpreter) + : ScriptedInterface(), m_interpreter(interpreter) {} + +template <> +StructuredData::ArraySP +ScriptedPythonInterface::ExtractValueFromPythonObject<StructuredData::ArraySP>( + python::PythonObject &p, Status &error) { + python::PythonList result_list(python::PyRefType::Borrowed, p.get()); + return result_list.CreateStructuredArray(); +} + +template <> +StructuredData::DictionarySP +ScriptedPythonInterface::ExtractValueFromPythonObject< + StructuredData::DictionarySP>(python::PythonObject &p, Status &error) { + python::PythonDictionary result_dict(python::PyRefType::Borrowed, p.get()); + return result_dict.CreateStructuredDictionary(); +} + +template <> +Status ScriptedPythonInterface::ExtractValueFromPythonObject<Status>( + python::PythonObject &p, Status &error) { + if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>( + python::LLDBSWIGPython_CastPyObjectToSBError(p.get()))) + return m_interpreter.GetStatusFromSBError(*sb_error); + error.SetErrorString("Couldn't cast lldb::SBError to lldb::Status."); + + return {}; +} + +template <> +Event *ScriptedPythonInterface::ExtractValueFromPythonObject<Event *>( + python::PythonObject &p, Status &error) { + if (lldb::SBEvent *sb_event = reinterpret_cast<lldb::SBEvent *>( + python::LLDBSWIGPython_CastPyObjectToSBEvent(p.get()))) + return m_interpreter.GetOpaqueTypeFromSBEvent(*sb_event); + error.SetErrorString("Couldn't cast lldb::SBEvent to lldb_private::Event."); + + return nullptr; +} + +template <> +lldb::StreamSP +ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StreamSP>( + python::PythonObject &p, Status &error) { + if (lldb::SBStream *sb_stream = reinterpret_cast<lldb::SBStream *>( + python::LLDBSWIGPython_CastPyObjectToSBStream(p.get()))) + return m_interpreter.GetOpaqueTypeFromSBStream(*sb_stream); + error.SetErrorString("Couldn't cast lldb::SBStream to lldb_private::Stream."); + + return nullptr; +} + +template <> +lldb::DataExtractorSP +ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>( + python::PythonObject &p, Status &error) { + lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>( + python::LLDBSWIGPython_CastPyObjectToSBData(p.get())); + + if (!sb_data) { + error.SetErrorString( + "Couldn't cast lldb::SBData to lldb::DataExtractorSP."); + return nullptr; + } + + return m_interpreter.GetDataExtractorFromSBData(*sb_data); +} + +template <> +lldb::BreakpointSP +ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>( + python::PythonObject &p, Status &error) { + lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>( + python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(p.get())); + + if (!sb_breakpoint) { + error.SetErrorString( + "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP."); + return nullptr; + } + + return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint); +} + +template <> +lldb::ProcessAttachInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject< + lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error) { + lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>( + python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(p.get())); + + if (!sb_attach_info) { + error.SetErrorString( + "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP."); + return nullptr; + } + + return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info); +} + +template <> +lldb::ProcessLaunchInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject< + lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error) { + lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>( + python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(p.get())); + + if (!sb_launch_info) { + error.SetErrorString( + "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP."); + return nullptr; + } + + return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info); +} + +template <> +std::optional<MemoryRegionInfo> +ScriptedPythonInterface::ExtractValueFromPythonObject< + std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) { + + lldb::SBMemoryRegionInfo *sb_mem_reg_info = + reinterpret_cast<lldb::SBMemoryRegionInfo *>( + python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get())); + + if (!sb_mem_reg_info) { + error.SetErrorString( + "Couldn't cast lldb::SBMemoryRegionInfo to lldb::MemoryRegionInfoSP."); + return {}; + } + + return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info); +} + +#endif diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPythonInterface.h b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPythonInterface.h new file mode 100644 index 000000000000..e1a3156d10af --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPythonInterface.h @@ -0,0 +1,480 @@ +//===-- ScriptedPythonInterface.h -------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDPYTHONINTERFACE_H +#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDPYTHONINTERFACE_H + +#if LLDB_ENABLE_PYTHON + +#include <optional> +#include <sstream> +#include <tuple> +#include <type_traits> +#include <utility> + +#include "lldb/Host/Config.h" +#include "lldb/Interpreter/Interfaces/ScriptedInterface.h" +#include "lldb/Utility/DataBufferHeap.h" + +#include "../PythonDataObjects.h" +#include "../SWIGPythonBridge.h" +#include "../ScriptInterpreterPythonImpl.h" + +namespace lldb_private { +class ScriptInterpreterPythonImpl; +class ScriptedPythonInterface : virtual public ScriptedInterface { +public: + ScriptedPythonInterface(ScriptInterpreterPythonImpl &interpreter); + ~ScriptedPythonInterface() override = default; + + enum class AbstractMethodCheckerCases { + eNotImplemented, + eNotAllocated, + eNotCallable, + eValid + }; + + llvm::Expected<std::map<llvm::StringLiteral, AbstractMethodCheckerCases>> + CheckAbstractMethodImplementation( + const python::PythonDictionary &class_dict) const { + + using namespace python; + + std::map<llvm::StringLiteral, AbstractMethodCheckerCases> checker; +#define SET_ERROR_AND_CONTINUE(method_name, error) \ + { \ + checker[method_name] = error; \ + continue; \ + } + + for (const llvm::StringLiteral &method_name : GetAbstractMethods()) { + if (!class_dict.HasKey(method_name)) + SET_ERROR_AND_CONTINUE(method_name, + AbstractMethodCheckerCases::eNotImplemented) + auto callable_or_err = class_dict.GetItem(method_name); + if (!callable_or_err) + SET_ERROR_AND_CONTINUE(method_name, + AbstractMethodCheckerCases::eNotAllocated) + if (!PythonCallable::Check(callable_or_err.get().get())) + SET_ERROR_AND_CONTINUE(method_name, + AbstractMethodCheckerCases::eNotCallable) + checker[method_name] = AbstractMethodCheckerCases::eValid; + } + +#undef HANDLE_ERROR + + return checker; + } + + template <typename... Args> + llvm::Expected<StructuredData::GenericSP> + CreatePluginObject(llvm::StringRef class_name, + StructuredData::Generic *script_obj, Args... args) { + using namespace python; + using Locker = ScriptInterpreterPythonImpl::Locker; + + auto create_error = [](std::string message) { + return llvm::createStringError(llvm::inconvertibleErrorCode(), message); + }; + + bool has_class_name = !class_name.empty(); + bool has_interpreter_dict = + !(llvm::StringRef(m_interpreter.GetDictionaryName()).empty()); + if (!has_class_name && !has_interpreter_dict && !script_obj) { + if (!has_class_name) + return create_error("Missing script class name."); + else if (!has_interpreter_dict) + return create_error("Invalid script interpreter dictionary."); + else + return create_error("Missing scripting object."); + } + + Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN, + Locker::FreeLock); + + PythonObject result = {}; + + if (script_obj) { + result = PythonObject(PyRefType::Borrowed, + static_cast<PyObject *>(script_obj->GetValue())); + } else { + auto dict = + PythonModule::MainModule().ResolveName<python::PythonDictionary>( + m_interpreter.GetDictionaryName()); + if (!dict.IsAllocated()) + return create_error( + llvm::formatv("Could not find interpreter dictionary: %s", + m_interpreter.GetDictionaryName())); + + auto init = + PythonObject::ResolveNameWithDictionary<python::PythonCallable>( + class_name, dict); + if (!init.IsAllocated()) + return create_error(llvm::formatv("Could not find script class: {0}", + class_name.data())); + + std::tuple<Args...> original_args = std::forward_as_tuple(args...); + auto transformed_args = TransformArgs(original_args); + + std::string error_string; + llvm::Expected<PythonCallable::ArgInfo> arg_info = init.GetArgInfo(); + if (!arg_info) { + llvm::handleAllErrors( + arg_info.takeError(), + [&](PythonException &E) { error_string.append(E.ReadBacktrace()); }, + [&](const llvm::ErrorInfoBase &E) { + error_string.append(E.message()); + }); + return llvm::createStringError(llvm::inconvertibleErrorCode(), + error_string); + } + + llvm::Expected<PythonObject> expected_return_object = + create_error("Resulting object is not initialized."); + + std::apply( + [&init, &expected_return_object](auto &&...args) { + llvm::consumeError(expected_return_object.takeError()); + expected_return_object = init(args...); + }, + transformed_args); + + if (!expected_return_object) + return expected_return_object.takeError(); + result = expected_return_object.get(); + } + + if (!result.IsValid()) + return create_error("Resulting object is not a valid Python Object."); + if (!result.HasAttribute("__class__")) + return create_error("Resulting object doesn't have '__class__' member."); + + PythonObject obj_class = result.GetAttributeValue("__class__"); + if (!obj_class.IsValid()) + return create_error("Resulting class object is not a valid."); + if (!obj_class.HasAttribute("__name__")) + return create_error( + "Resulting object class doesn't have '__name__' member."); + PythonString obj_class_name = + obj_class.GetAttributeValue("__name__").AsType<PythonString>(); + + PythonObject object_class_mapping_proxy = + obj_class.GetAttributeValue("__dict__"); + if (!obj_class.HasAttribute("__dict__")) + return create_error( + "Resulting object class doesn't have '__dict__' member."); + + PythonCallable dict_converter = PythonModule::BuiltinsModule() + .ResolveName("dict") + .AsType<PythonCallable>(); + if (!dict_converter.IsAllocated()) + return create_error( + "Python 'builtins' module doesn't have 'dict' class."); + + PythonDictionary object_class_dict = + dict_converter(object_class_mapping_proxy).AsType<PythonDictionary>(); + if (!object_class_dict.IsAllocated()) + return create_error("Coudn't create dictionary from resulting object " + "class mapping proxy object."); + + auto checker_or_err = CheckAbstractMethodImplementation(object_class_dict); + if (!checker_or_err) + return checker_or_err.takeError(); + + for (const auto &method_checker : *checker_or_err) + switch (method_checker.second) { + case AbstractMethodCheckerCases::eNotImplemented: + LLDB_LOG(GetLog(LLDBLog::Script), + "Abstract method {0}.{1} not implemented.", + obj_class_name.GetString(), method_checker.first); + break; + case AbstractMethodCheckerCases::eNotAllocated: + LLDB_LOG(GetLog(LLDBLog::Script), + "Abstract method {0}.{1} not allocated.", + obj_class_name.GetString(), method_checker.first); + break; + case AbstractMethodCheckerCases::eNotCallable: + LLDB_LOG(GetLog(LLDBLog::Script), + "Abstract method {0}.{1} not callable.", + obj_class_name.GetString(), method_checker.first); + break; + case AbstractMethodCheckerCases::eValid: + LLDB_LOG(GetLog(LLDBLog::Script), + "Abstract method {0}.{1} implemented & valid.", + obj_class_name.GetString(), method_checker.first); + break; + } + + for (const auto &method_checker : *checker_or_err) + if (method_checker.second != AbstractMethodCheckerCases::eValid) + return create_error( + llvm::formatv("Abstract method {0}.{1} missing. Enable lldb " + "script log for more details.", + obj_class_name.GetString(), method_checker.first)); + + m_object_instance_sp = StructuredData::GenericSP( + new StructuredPythonObject(std::move(result))); + return m_object_instance_sp; + } + +protected: + template <typename T = StructuredData::ObjectSP> + T ExtractValueFromPythonObject(python::PythonObject &p, Status &error) { + return p.CreateStructuredObject(); + } + + template <typename T = StructuredData::ObjectSP, typename... Args> + T Dispatch(llvm::StringRef method_name, Status &error, Args &&...args) { + using namespace python; + using Locker = ScriptInterpreterPythonImpl::Locker; + + std::string caller_signature = + llvm::Twine(LLVM_PRETTY_FUNCTION + llvm::Twine(" (") + + llvm::Twine(method_name) + llvm::Twine(")")) + .str(); + if (!m_object_instance_sp) + return ErrorWithMessage<T>(caller_signature, "Python object ill-formed", + error); + + Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN, + Locker::FreeLock); + + PythonObject implementor(PyRefType::Borrowed, + (PyObject *)m_object_instance_sp->GetValue()); + + if (!implementor.IsAllocated()) + return llvm::is_contained(GetAbstractMethods(), method_name) + ? ErrorWithMessage<T>(caller_signature, + "Python implementor not allocated.", + error) + : T{}; + + std::tuple<Args...> original_args = std::forward_as_tuple(args...); + auto transformed_args = TransformArgs(original_args); + + llvm::Expected<PythonObject> expected_return_object = + llvm::make_error<llvm::StringError>("Not initialized.", + llvm::inconvertibleErrorCode()); + std::apply( + [&implementor, &method_name, &expected_return_object](auto &&...args) { + llvm::consumeError(expected_return_object.takeError()); + expected_return_object = + implementor.CallMethod(method_name.data(), args...); + }, + transformed_args); + + if (llvm::Error e = expected_return_object.takeError()) { + error.SetErrorString(llvm::toString(std::move(e)).c_str()); + return ErrorWithMessage<T>(caller_signature, + "Python method could not be called.", error); + } + + PythonObject py_return = std::move(expected_return_object.get()); + + // Now that we called the python method with the transformed arguments, + // we need to interate again over both the original and transformed + // parameter pack, and transform back the parameter that were passed in + // the original parameter pack as references or pointers. + if (sizeof...(Args) > 0) + if (!ReassignPtrsOrRefsArgs(original_args, transformed_args)) + return ErrorWithMessage<T>( + caller_signature, + "Couldn't re-assign reference and pointer arguments.", error); + + if (!py_return.IsAllocated()) + return {}; + return ExtractValueFromPythonObject<T>(py_return, error); + } + + template <typename... Args> + Status GetStatusFromMethod(llvm::StringRef method_name, Args &&...args) { + Status error; + Dispatch<Status>(method_name, error, std::forward<Args>(args)...); + + return error; + } + + template <typename T> T Transform(T object) { + // No Transformation for generic usage + return {object}; + } + + python::PythonObject Transform(bool arg) { + // Boolean arguments need to be turned into python objects. + return python::PythonBoolean(arg); + } + + python::PythonObject Transform(Status arg) { + return python::SWIGBridge::ToSWIGWrapper(arg); + } + + python::PythonObject Transform(const StructuredDataImpl &arg) { + return python::SWIGBridge::ToSWIGWrapper(arg); + } + + python::PythonObject Transform(lldb::ExecutionContextRefSP arg) { + return python::SWIGBridge::ToSWIGWrapper(arg); + } + + python::PythonObject Transform(lldb::ProcessSP arg) { + return python::SWIGBridge::ToSWIGWrapper(arg); + } + + python::PythonObject Transform(lldb::ThreadPlanSP arg) { + return python::SWIGBridge::ToSWIGWrapper(arg); + } + + python::PythonObject Transform(lldb::ProcessAttachInfoSP arg) { + return python::SWIGBridge::ToSWIGWrapper(arg); + } + + python::PythonObject Transform(lldb::ProcessLaunchInfoSP arg) { + return python::SWIGBridge::ToSWIGWrapper(arg); + } + + python::PythonObject Transform(Event *arg) { + return python::SWIGBridge::ToSWIGWrapper(arg); + } + + python::PythonObject Transform(lldb::StreamSP arg) { + return python::SWIGBridge::ToSWIGWrapper(arg.get()); + } + + python::PythonObject Transform(lldb::DataExtractorSP arg) { + return python::SWIGBridge::ToSWIGWrapper(arg); + } + + template <typename T, typename U> + void ReverseTransform(T &original_arg, U transformed_arg, Status &error) { + // If U is not a PythonObject, don't touch it! + return; + } + + template <typename T> + void ReverseTransform(T &original_arg, python::PythonObject transformed_arg, + Status &error) { + original_arg = ExtractValueFromPythonObject<T>(transformed_arg, error); + } + + void ReverseTransform(bool &original_arg, + python::PythonObject transformed_arg, Status &error) { + python::PythonBoolean boolean_arg = python::PythonBoolean( + python::PyRefType::Borrowed, transformed_arg.get()); + if (boolean_arg.IsValid()) + original_arg = boolean_arg.GetValue(); + else + error.SetErrorString( + llvm::formatv("{}: Invalid boolean argument.", LLVM_PRETTY_FUNCTION) + .str()); + } + + template <std::size_t... I, typename... Args> + auto TransformTuple(const std::tuple<Args...> &args, + std::index_sequence<I...>) { + return std::make_tuple(Transform(std::get<I>(args))...); + } + + // This will iterate over the Dispatch parameter pack and replace in-place + // every `lldb_private` argument that has a SB counterpart. + template <typename... Args> + auto TransformArgs(const std::tuple<Args...> &args) { + return TransformTuple(args, std::make_index_sequence<sizeof...(Args)>()); + } + + template <typename T, typename U> + void TransformBack(T &original_arg, U transformed_arg, Status &error) { + ReverseTransform(original_arg, transformed_arg, error); + } + + template <std::size_t... I, typename... Ts, typename... Us> + bool ReassignPtrsOrRefsArgs(std::tuple<Ts...> &original_args, + std::tuple<Us...> &transformed_args, + std::index_sequence<I...>) { + Status error; + (TransformBack(std::get<I>(original_args), std::get<I>(transformed_args), + error), + ...); + return error.Success(); + } + + template <typename... Ts, typename... Us> + bool ReassignPtrsOrRefsArgs(std::tuple<Ts...> &original_args, + std::tuple<Us...> &transformed_args) { + if (sizeof...(Ts) != sizeof...(Us)) + return false; + + return ReassignPtrsOrRefsArgs(original_args, transformed_args, + std::make_index_sequence<sizeof...(Ts)>()); + } + + template <typename T, typename... Args> + void FormatArgs(std::string &fmt, T arg, Args... args) const { + FormatArgs(fmt, arg); + FormatArgs(fmt, args...); + } + + template <typename T> void FormatArgs(std::string &fmt, T arg) const { + fmt += python::PythonFormat<T>::format; + } + + void FormatArgs(std::string &fmt) const {} + + // The lifetime is managed by the ScriptInterpreter + ScriptInterpreterPythonImpl &m_interpreter; +}; + +template <> +StructuredData::ArraySP +ScriptedPythonInterface::ExtractValueFromPythonObject<StructuredData::ArraySP>( + python::PythonObject &p, Status &error); + +template <> +StructuredData::DictionarySP +ScriptedPythonInterface::ExtractValueFromPythonObject< + StructuredData::DictionarySP>(python::PythonObject &p, Status &error); + +template <> +Status ScriptedPythonInterface::ExtractValueFromPythonObject<Status>( + python::PythonObject &p, Status &error); + +template <> +Event *ScriptedPythonInterface::ExtractValueFromPythonObject<Event *>( + python::PythonObject &p, Status &error); + +template <> +lldb::StreamSP +ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StreamSP>( + python::PythonObject &p, Status &error); + +template <> +lldb::BreakpointSP +ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>( + python::PythonObject &p, Status &error); + +template <> +lldb::ProcessAttachInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject< + lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error); + +template <> +lldb::ProcessLaunchInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject< + lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error); + +template <> +lldb::DataExtractorSP +ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>( + python::PythonObject &p, Status &error); + +template <> +std::optional<MemoryRegionInfo> +ScriptedPythonInterface::ExtractValueFromPythonObject< + std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error); + +} // namespace lldb_private + +#endif // LLDB_ENABLE_PYTHON +#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDPYTHONINTERFACE_H diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedThreadPlanPythonInterface.cpp b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedThreadPlanPythonInterface.cpp new file mode 100644 index 000000000000..f23858c01277 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedThreadPlanPythonInterface.cpp @@ -0,0 +1,105 @@ +//===-- ScriptedThreadPlanPythonInterface.cpp -----------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "lldb/Host/Config.h" +#include "lldb/Utility/Log.h" +#include "lldb/lldb-enumerations.h" + +#if LLDB_ENABLE_PYTHON + +// LLDB Python header must be included first +#include "../lldb-python.h" + +#include "../SWIGPythonBridge.h" +#include "../ScriptInterpreterPythonImpl.h" +#include "ScriptedThreadPlanPythonInterface.h" + +using namespace lldb; +using namespace lldb_private; +using namespace lldb_private::python; + +ScriptedThreadPlanPythonInterface::ScriptedThreadPlanPythonInterface( + ScriptInterpreterPythonImpl &interpreter) + : ScriptedThreadPlanInterface(), ScriptedPythonInterface(interpreter) {} + +llvm::Expected<StructuredData::GenericSP> +ScriptedThreadPlanPythonInterface::CreatePluginObject( + const llvm::StringRef class_name, lldb::ThreadPlanSP thread_plan_sp, + const StructuredDataImpl &args_sp) { + return ScriptedPythonInterface::CreatePluginObject(class_name, nullptr, + thread_plan_sp, args_sp); +} + +llvm::Expected<bool> +ScriptedThreadPlanPythonInterface::ExplainsStop(Event *event) { + Status error; + StructuredData::ObjectSP obj = Dispatch("explains_stop", error, event); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, + error)) { + if (!obj) + return false; + return error.ToError(); + } + + return obj->GetBooleanValue(); +} + +llvm::Expected<bool> +ScriptedThreadPlanPythonInterface::ShouldStop(Event *event) { + Status error; + StructuredData::ObjectSP obj = Dispatch("should_stop", error, event); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, + error)) { + if (!obj) + return false; + return error.ToError(); + } + + return obj->GetBooleanValue(); +} + +llvm::Expected<bool> ScriptedThreadPlanPythonInterface::IsStale() { + Status error; + StructuredData::ObjectSP obj = Dispatch("is_stale", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, + error)) { + if (!obj) + return false; + return error.ToError(); + } + + return obj->GetBooleanValue(); +} + +lldb::StateType ScriptedThreadPlanPythonInterface::GetRunState() { + Status error; + StructuredData::ObjectSP obj = Dispatch("should_step", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, + error)) + return lldb::eStateStepping; + + return static_cast<lldb::StateType>(obj->GetUnsignedIntegerValue( + static_cast<uint32_t>(lldb::eStateStepping))); +} + +llvm::Error +ScriptedThreadPlanPythonInterface::GetStopDescription(lldb::StreamSP &stream) { + Status error; + Dispatch("stop_description", error, stream); + + if (error.Fail()) + return error.ToError(); + + return llvm::Error::success(); +} + +#endif diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedThreadPlanPythonInterface.h b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedThreadPlanPythonInterface.h new file mode 100644 index 000000000000..6ec89b9f5925 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedThreadPlanPythonInterface.h @@ -0,0 +1,48 @@ +//===-- ScriptedThreadPlanPythonInterface.h ---------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDTHREADPLANPYTHONINTERFACE_H +#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDTHREADPLANPYTHONINTERFACE_H + +#include "lldb/Host/Config.h" + +#if LLDB_ENABLE_PYTHON + +#include "ScriptedPythonInterface.h" +#include "lldb/Interpreter/Interfaces/ScriptedThreadPlanInterface.h" +#include <optional> + +namespace lldb_private { +class ScriptedThreadPlanPythonInterface : public ScriptedThreadPlanInterface, + public ScriptedPythonInterface { +public: + ScriptedThreadPlanPythonInterface(ScriptInterpreterPythonImpl &interpreter); + + llvm::Expected<StructuredData::GenericSP> + CreatePluginObject(const llvm::StringRef class_name, + lldb::ThreadPlanSP thread_plan_sp, + const StructuredDataImpl &args_sp) override; + + llvm::SmallVector<llvm::StringLiteral> GetAbstractMethods() const override { + return {}; + } + + llvm::Expected<bool> ExplainsStop(Event *event) override; + + llvm::Expected<bool> ShouldStop(Event *event) override; + + llvm::Expected<bool> IsStale() override; + + lldb::StateType GetRunState() override; + + llvm::Error GetStopDescription(lldb::StreamSP &stream) override; +}; +} // namespace lldb_private + +#endif // LLDB_ENABLE_PYTHON +#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDTHREADPLANPYTHONINTERFACE_H diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedThreadPythonInterface.cpp b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedThreadPythonInterface.cpp new file mode 100644 index 000000000000..8af89d761764 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedThreadPythonInterface.cpp @@ -0,0 +1,147 @@ +//===-- ScriptedThreadPythonInterface.cpp ---------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "lldb/Host/Config.h" +#include "lldb/Target/ExecutionContext.h" +#include "lldb/Utility/Log.h" +#include "lldb/lldb-enumerations.h" + +#if LLDB_ENABLE_PYTHON + +// LLDB Python header must be included first +#include "../lldb-python.h" + +#include "../SWIGPythonBridge.h" +#include "../ScriptInterpreterPythonImpl.h" +#include "ScriptedThreadPythonInterface.h" +#include <optional> + +using namespace lldb; +using namespace lldb_private; +using namespace lldb_private::python; +using Locker = ScriptInterpreterPythonImpl::Locker; + +ScriptedThreadPythonInterface::ScriptedThreadPythonInterface( + ScriptInterpreterPythonImpl &interpreter) + : ScriptedThreadInterface(), ScriptedPythonInterface(interpreter) {} + +llvm::Expected<StructuredData::GenericSP> +ScriptedThreadPythonInterface::CreatePluginObject( + const llvm::StringRef class_name, ExecutionContext &exe_ctx, + StructuredData::DictionarySP args_sp, StructuredData::Generic *script_obj) { + ExecutionContextRefSP exe_ctx_ref_sp = + std::make_shared<ExecutionContextRef>(exe_ctx); + StructuredDataImpl sd_impl(args_sp); + return ScriptedPythonInterface::CreatePluginObject(class_name, script_obj, + exe_ctx_ref_sp, sd_impl); +} + +lldb::tid_t ScriptedThreadPythonInterface::GetThreadID() { + Status error; + StructuredData::ObjectSP obj = Dispatch("get_thread_id", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, + error)) + return LLDB_INVALID_THREAD_ID; + + return obj->GetUnsignedIntegerValue(LLDB_INVALID_THREAD_ID); +} + +std::optional<std::string> ScriptedThreadPythonInterface::GetName() { + Status error; + StructuredData::ObjectSP obj = Dispatch("get_name", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, + error)) + return {}; + + return obj->GetStringValue().str(); +} + +lldb::StateType ScriptedThreadPythonInterface::GetState() { + Status error; + StructuredData::ObjectSP obj = Dispatch("get_state", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, + error)) + return eStateInvalid; + + return static_cast<StateType>(obj->GetUnsignedIntegerValue(eStateInvalid)); +} + +std::optional<std::string> ScriptedThreadPythonInterface::GetQueue() { + Status error; + StructuredData::ObjectSP obj = Dispatch("get_queue", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, + error)) + return {}; + + return obj->GetStringValue().str(); +} + +StructuredData::DictionarySP ScriptedThreadPythonInterface::GetStopReason() { + Status error; + StructuredData::DictionarySP dict = + Dispatch<StructuredData::DictionarySP>("get_stop_reason", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict, + error)) + return {}; + + return dict; +} + +StructuredData::ArraySP ScriptedThreadPythonInterface::GetStackFrames() { + Status error; + StructuredData::ArraySP arr = + Dispatch<StructuredData::ArraySP>("get_stackframes", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, arr, + error)) + return {}; + + return arr; +} + +StructuredData::DictionarySP ScriptedThreadPythonInterface::GetRegisterInfo() { + Status error; + StructuredData::DictionarySP dict = + Dispatch<StructuredData::DictionarySP>("get_register_info", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict, + error)) + return {}; + + return dict; +} + +std::optional<std::string> ScriptedThreadPythonInterface::GetRegisterContext() { + Status error; + StructuredData::ObjectSP obj = Dispatch("get_register_context", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, + error)) + return {}; + + return obj->GetAsString()->GetValue().str(); +} + +StructuredData::ArraySP ScriptedThreadPythonInterface::GetExtendedInfo() { + Status error; + StructuredData::ArraySP arr = + Dispatch<StructuredData::ArraySP>("get_extended_info", error); + + if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, arr, + error)) + return {}; + + return arr; +} + +#endif diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedThreadPythonInterface.h b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedThreadPythonInterface.h new file mode 100644 index 000000000000..5676f7f1d675 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedThreadPythonInterface.h @@ -0,0 +1,57 @@ +//===-- ScriptedThreadPythonInterface.h ------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDTHREADPYTHONINTERFACE_H +#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDTHREADPYTHONINTERFACE_H + +#include "lldb/Host/Config.h" + +#if LLDB_ENABLE_PYTHON + +#include "ScriptedPythonInterface.h" +#include "lldb/Interpreter/Interfaces/ScriptedThreadInterface.h" +#include <optional> + +namespace lldb_private { +class ScriptedThreadPythonInterface : public ScriptedThreadInterface, + public ScriptedPythonInterface { +public: + ScriptedThreadPythonInterface(ScriptInterpreterPythonImpl &interpreter); + + llvm::Expected<StructuredData::GenericSP> + CreatePluginObject(llvm::StringRef class_name, ExecutionContext &exe_ctx, + StructuredData::DictionarySP args_sp, + StructuredData::Generic *script_obj = nullptr) override; + + llvm::SmallVector<llvm::StringLiteral> GetAbstractMethods() const override { + return llvm::SmallVector<llvm::StringLiteral>( + {"get_stop_reason", "get_register_context"}); + } + + lldb::tid_t GetThreadID() override; + + std::optional<std::string> GetName() override; + + lldb::StateType GetState() override; + + std::optional<std::string> GetQueue() override; + + StructuredData::DictionarySP GetStopReason() override; + + StructuredData::ArraySP GetStackFrames() override; + + StructuredData::DictionarySP GetRegisterInfo() override; + + std::optional<std::string> GetRegisterContext() override; + + StructuredData::ArraySP GetExtendedInfo() override; +}; +} // namespace lldb_private + +#endif // LLDB_ENABLE_PYTHON +#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDTHREADPYTHONINTERFACE_H diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp new file mode 100644 index 000000000000..7c7035e0c86c --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp @@ -0,0 +1,1524 @@ +//===-- PythonDataObjects.cpp ---------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "lldb/Host/Config.h" + +#if LLDB_ENABLE_PYTHON + +#include "PythonDataObjects.h" +#include "ScriptInterpreterPython.h" + +#include "lldb/Host/File.h" +#include "lldb/Host/FileSystem.h" +#include "lldb/Interpreter/ScriptInterpreter.h" +#include "lldb/Utility/LLDBLog.h" +#include "lldb/Utility/Log.h" +#include "lldb/Utility/Stream.h" + +#include "llvm/Support/Casting.h" +#include "llvm/Support/ConvertUTF.h" +#include "llvm/Support/Errno.h" + +#include <cstdio> +#include <variant> + +using namespace lldb_private; +using namespace lldb; +using namespace lldb_private::python; +using llvm::cantFail; +using llvm::Error; +using llvm::Expected; +using llvm::Twine; + +template <> Expected<bool> python::As<bool>(Expected<PythonObject> &&obj) { + if (!obj) + return obj.takeError(); + return obj.get().IsTrue(); +} + +template <> +Expected<long long> python::As<long long>(Expected<PythonObject> &&obj) { + if (!obj) + return obj.takeError(); + return obj->AsLongLong(); +} + +template <> +Expected<unsigned long long> +python::As<unsigned long long>(Expected<PythonObject> &&obj) { + if (!obj) + return obj.takeError(); + return obj->AsUnsignedLongLong(); +} + +template <> +Expected<std::string> python::As<std::string>(Expected<PythonObject> &&obj) { + if (!obj) + return obj.takeError(); + PyObject *str_obj = PyObject_Str(obj.get().get()); + if (!str_obj) + return llvm::make_error<PythonException>(); + auto str = Take<PythonString>(str_obj); + auto utf8 = str.AsUTF8(); + if (!utf8) + return utf8.takeError(); + return std::string(utf8.get()); +} + +static bool python_is_finalizing() { +#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 13) || (PY_MAJOR_VERSION > 3) + return Py_IsFinalizing(); +#elif PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 7 + return _Py_Finalizing != nullptr; +#else + return _Py_IsFinalizing(); +#endif +} + +void PythonObject::Reset() { + if (m_py_obj && Py_IsInitialized()) { + if (python_is_finalizing()) { + // Leak m_py_obj rather than crashing the process. + // https://docs.python.org/3/c-api/init.html#c.PyGILState_Ensure + } else { + PyGILState_STATE state = PyGILState_Ensure(); + Py_DECREF(m_py_obj); + PyGILState_Release(state); + } + } + m_py_obj = nullptr; +} + +Expected<long long> PythonObject::AsLongLong() const { + if (!m_py_obj) + return nullDeref(); + assert(!PyErr_Occurred()); + long long r = PyLong_AsLongLong(m_py_obj); + if (PyErr_Occurred()) + return exception(); + return r; +} + +Expected<unsigned long long> PythonObject::AsUnsignedLongLong() const { + if (!m_py_obj) + return nullDeref(); + assert(!PyErr_Occurred()); + long long r = PyLong_AsUnsignedLongLong(m_py_obj); + if (PyErr_Occurred()) + return exception(); + return r; +} + +// wraps on overflow, instead of raising an error. +Expected<unsigned long long> PythonObject::AsModuloUnsignedLongLong() const { + if (!m_py_obj) + return nullDeref(); + assert(!PyErr_Occurred()); + unsigned long long r = PyLong_AsUnsignedLongLongMask(m_py_obj); + // FIXME: We should fetch the exception message and hoist it. + if (PyErr_Occurred()) + return exception(); + return r; +} + +void StructuredPythonObject::Serialize(llvm::json::OStream &s) const { + s.value(llvm::formatv("Python Obj: {0:X}", GetValue()).str()); +} + +// PythonObject + +void PythonObject::Dump(Stream &strm) const { + if (m_py_obj) { + FILE *file = llvm::sys::RetryAfterSignal(nullptr, ::tmpfile); + if (file) { + ::PyObject_Print(m_py_obj, file, 0); + const long length = ftell(file); + if (length) { + ::rewind(file); + std::vector<char> file_contents(length, '\0'); + const size_t length_read = + ::fread(file_contents.data(), 1, file_contents.size(), file); + if (length_read > 0) + strm.Write(file_contents.data(), length_read); + } + ::fclose(file); + } + } else + strm.PutCString("NULL"); +} + +PyObjectType PythonObject::GetObjectType() const { + if (!IsAllocated()) + return PyObjectType::None; + + if (PythonModule::Check(m_py_obj)) + return PyObjectType::Module; + if (PythonList::Check(m_py_obj)) + return PyObjectType::List; + if (PythonTuple::Check(m_py_obj)) + return PyObjectType::Tuple; + if (PythonDictionary::Check(m_py_obj)) + return PyObjectType::Dictionary; + if (PythonString::Check(m_py_obj)) + return PyObjectType::String; + if (PythonBytes::Check(m_py_obj)) + return PyObjectType::Bytes; + if (PythonByteArray::Check(m_py_obj)) + return PyObjectType::ByteArray; + if (PythonBoolean::Check(m_py_obj)) + return PyObjectType::Boolean; + if (PythonInteger::Check(m_py_obj)) + return PyObjectType::Integer; + if (PythonFile::Check(m_py_obj)) + return PyObjectType::File; + if (PythonCallable::Check(m_py_obj)) + return PyObjectType::Callable; + return PyObjectType::Unknown; +} + +PythonString PythonObject::Repr() const { + if (!m_py_obj) + return PythonString(); + PyObject *repr = PyObject_Repr(m_py_obj); + if (!repr) + return PythonString(); + return PythonString(PyRefType::Owned, repr); +} + +PythonString PythonObject::Str() const { + if (!m_py_obj) + return PythonString(); + PyObject *str = PyObject_Str(m_py_obj); + if (!str) + return PythonString(); + return PythonString(PyRefType::Owned, str); +} + +PythonObject +PythonObject::ResolveNameWithDictionary(llvm::StringRef name, + const PythonDictionary &dict) { + size_t dot_pos = name.find('.'); + llvm::StringRef piece = name.substr(0, dot_pos); + PythonObject result = dict.GetItemForKey(PythonString(piece)); + if (dot_pos == llvm::StringRef::npos) { + // There was no dot, we're done. + return result; + } + + // There was a dot. The remaining portion of the name should be looked up in + // the context of the object that was found in the dictionary. + return result.ResolveName(name.substr(dot_pos + 1)); +} + +PythonObject PythonObject::ResolveName(llvm::StringRef name) const { + // Resolve the name in the context of the specified object. If, for example, + // `this` refers to a PyModule, then this will look for `name` in this + // module. If `this` refers to a PyType, then it will resolve `name` as an + // attribute of that type. If `this` refers to an instance of an object, + // then it will resolve `name` as the value of the specified field. + // + // This function handles dotted names so that, for example, if `m_py_obj` + // refers to the `sys` module, and `name` == "path.append", then it will find + // the function `sys.path.append`. + + size_t dot_pos = name.find('.'); + if (dot_pos == llvm::StringRef::npos) { + // No dots in the name, we should be able to find the value immediately as + // an attribute of `m_py_obj`. + return GetAttributeValue(name); + } + + // Look up the first piece of the name, and resolve the rest as a child of + // that. + PythonObject parent = ResolveName(name.substr(0, dot_pos)); + if (!parent.IsAllocated()) + return PythonObject(); + + // Tail recursion.. should be optimized by the compiler + return parent.ResolveName(name.substr(dot_pos + 1)); +} + +bool PythonObject::HasAttribute(llvm::StringRef attr) const { + if (!IsValid()) + return false; + PythonString py_attr(attr); + return !!PyObject_HasAttr(m_py_obj, py_attr.get()); +} + +PythonObject PythonObject::GetAttributeValue(llvm::StringRef attr) const { + if (!IsValid()) + return PythonObject(); + + PythonString py_attr(attr); + if (!PyObject_HasAttr(m_py_obj, py_attr.get())) + return PythonObject(); + + return PythonObject(PyRefType::Owned, + PyObject_GetAttr(m_py_obj, py_attr.get())); +} + +StructuredData::ObjectSP PythonObject::CreateStructuredObject() const { + assert(PyGILState_Check()); + switch (GetObjectType()) { + case PyObjectType::Dictionary: + return PythonDictionary(PyRefType::Borrowed, m_py_obj) + .CreateStructuredDictionary(); + case PyObjectType::Boolean: + return PythonBoolean(PyRefType::Borrowed, m_py_obj) + .CreateStructuredBoolean(); + case PyObjectType::Integer: { + StructuredData::IntegerSP int_sp = + PythonInteger(PyRefType::Borrowed, m_py_obj).CreateStructuredInteger(); + if (std::holds_alternative<StructuredData::UnsignedIntegerSP>(int_sp)) + return std::get<StructuredData::UnsignedIntegerSP>(int_sp); + if (std::holds_alternative<StructuredData::SignedIntegerSP>(int_sp)) + return std::get<StructuredData::SignedIntegerSP>(int_sp); + return nullptr; + }; + case PyObjectType::List: + return PythonList(PyRefType::Borrowed, m_py_obj).CreateStructuredArray(); + case PyObjectType::String: + return PythonString(PyRefType::Borrowed, m_py_obj).CreateStructuredString(); + case PyObjectType::Bytes: + return PythonBytes(PyRefType::Borrowed, m_py_obj).CreateStructuredString(); + case PyObjectType::ByteArray: + return PythonByteArray(PyRefType::Borrowed, m_py_obj) + .CreateStructuredString(); + case PyObjectType::None: + return StructuredData::ObjectSP(); + default: + return StructuredData::ObjectSP(new StructuredPythonObject( + PythonObject(PyRefType::Borrowed, m_py_obj))); + } +} + +// PythonString + +PythonBytes::PythonBytes(llvm::ArrayRef<uint8_t> bytes) { SetBytes(bytes); } + +PythonBytes::PythonBytes(const uint8_t *bytes, size_t length) { + SetBytes(llvm::ArrayRef<uint8_t>(bytes, length)); +} + +bool PythonBytes::Check(PyObject *py_obj) { + if (!py_obj) + return false; + return PyBytes_Check(py_obj); +} + +llvm::ArrayRef<uint8_t> PythonBytes::GetBytes() const { + if (!IsValid()) + return llvm::ArrayRef<uint8_t>(); + + Py_ssize_t size; + char *c; + + PyBytes_AsStringAndSize(m_py_obj, &c, &size); + return llvm::ArrayRef<uint8_t>(reinterpret_cast<uint8_t *>(c), size); +} + +size_t PythonBytes::GetSize() const { + if (!IsValid()) + return 0; + return PyBytes_Size(m_py_obj); +} + +void PythonBytes::SetBytes(llvm::ArrayRef<uint8_t> bytes) { + const char *data = reinterpret_cast<const char *>(bytes.data()); + *this = Take<PythonBytes>(PyBytes_FromStringAndSize(data, bytes.size())); +} + +StructuredData::StringSP PythonBytes::CreateStructuredString() const { + StructuredData::StringSP result(new StructuredData::String); + Py_ssize_t size; + char *c; + PyBytes_AsStringAndSize(m_py_obj, &c, &size); + result->SetValue(std::string(c, size)); + return result; +} + +PythonByteArray::PythonByteArray(llvm::ArrayRef<uint8_t> bytes) + : PythonByteArray(bytes.data(), bytes.size()) {} + +PythonByteArray::PythonByteArray(const uint8_t *bytes, size_t length) { + const char *str = reinterpret_cast<const char *>(bytes); + *this = Take<PythonByteArray>(PyByteArray_FromStringAndSize(str, length)); +} + +bool PythonByteArray::Check(PyObject *py_obj) { + if (!py_obj) + return false; + return PyByteArray_Check(py_obj); +} + +llvm::ArrayRef<uint8_t> PythonByteArray::GetBytes() const { + if (!IsValid()) + return llvm::ArrayRef<uint8_t>(); + + char *c = PyByteArray_AsString(m_py_obj); + size_t size = GetSize(); + return llvm::ArrayRef<uint8_t>(reinterpret_cast<uint8_t *>(c), size); +} + +size_t PythonByteArray::GetSize() const { + if (!IsValid()) + return 0; + + return PyByteArray_Size(m_py_obj); +} + +StructuredData::StringSP PythonByteArray::CreateStructuredString() const { + StructuredData::StringSP result(new StructuredData::String); + llvm::ArrayRef<uint8_t> bytes = GetBytes(); + const char *str = reinterpret_cast<const char *>(bytes.data()); + result->SetValue(std::string(str, bytes.size())); + return result; +} + +// PythonString + +Expected<PythonString> PythonString::FromUTF8(llvm::StringRef string) { + PyObject *str = PyUnicode_FromStringAndSize(string.data(), string.size()); + if (!str) + return llvm::make_error<PythonException>(); + return Take<PythonString>(str); +} + +PythonString::PythonString(llvm::StringRef string) { SetString(string); } + +bool PythonString::Check(PyObject *py_obj) { + if (!py_obj) + return false; + + if (PyUnicode_Check(py_obj)) + return true; + return false; +} + +llvm::StringRef PythonString::GetString() const { + auto s = AsUTF8(); + if (!s) { + llvm::consumeError(s.takeError()); + return llvm::StringRef(""); + } + return s.get(); +} + +Expected<llvm::StringRef> PythonString::AsUTF8() const { + if (!IsValid()) + return nullDeref(); + + Py_ssize_t size; + const char *data; + + data = PyUnicode_AsUTF8AndSize(m_py_obj, &size); + + if (!data) + return exception(); + + return llvm::StringRef(data, size); +} + +size_t PythonString::GetSize() const { + if (IsValid()) { +#if PY_MINOR_VERSION >= 3 + return PyUnicode_GetLength(m_py_obj); +#else + return PyUnicode_GetSize(m_py_obj); +#endif + } + return 0; +} + +void PythonString::SetString(llvm::StringRef string) { + auto s = FromUTF8(string); + if (!s) { + llvm::consumeError(s.takeError()); + Reset(); + } else { + *this = std::move(s.get()); + } +} + +StructuredData::StringSP PythonString::CreateStructuredString() const { + StructuredData::StringSP result(new StructuredData::String); + result->SetValue(GetString()); + return result; +} + +// PythonInteger + +PythonInteger::PythonInteger(int64_t value) { SetInteger(value); } + +bool PythonInteger::Check(PyObject *py_obj) { + if (!py_obj) + return false; + + // Python 3 does not have PyInt_Check. There is only one type of integral + // value, long. + return PyLong_Check(py_obj); +} + +void PythonInteger::SetInteger(int64_t value) { + *this = Take<PythonInteger>(PyLong_FromLongLong(value)); +} + +StructuredData::IntegerSP PythonInteger::CreateStructuredInteger() const { + StructuredData::UnsignedIntegerSP uint_sp = CreateStructuredUnsignedInteger(); + return uint_sp ? StructuredData::IntegerSP(uint_sp) + : CreateStructuredSignedInteger(); +} + +StructuredData::UnsignedIntegerSP +PythonInteger::CreateStructuredUnsignedInteger() const { + StructuredData::UnsignedIntegerSP result = nullptr; + llvm::Expected<unsigned long long> value = AsUnsignedLongLong(); + if (!value) + llvm::consumeError(value.takeError()); + else + result = std::make_shared<StructuredData::UnsignedInteger>(value.get()); + + return result; +} + +StructuredData::SignedIntegerSP +PythonInteger::CreateStructuredSignedInteger() const { + StructuredData::SignedIntegerSP result = nullptr; + llvm::Expected<long long> value = AsLongLong(); + if (!value) + llvm::consumeError(value.takeError()); + else + result = std::make_shared<StructuredData::SignedInteger>(value.get()); + + return result; +} + +// PythonBoolean + +PythonBoolean::PythonBoolean(bool value) { + SetValue(value); +} + +bool PythonBoolean::Check(PyObject *py_obj) { + return py_obj ? PyBool_Check(py_obj) : false; +} + +bool PythonBoolean::GetValue() const { + return m_py_obj ? PyObject_IsTrue(m_py_obj) : false; +} + +void PythonBoolean::SetValue(bool value) { + *this = Take<PythonBoolean>(PyBool_FromLong(value)); +} + +StructuredData::BooleanSP PythonBoolean::CreateStructuredBoolean() const { + StructuredData::BooleanSP result(new StructuredData::Boolean); + result->SetValue(GetValue()); + return result; +} + +// PythonList + +PythonList::PythonList(PyInitialValue value) { + if (value == PyInitialValue::Empty) + *this = Take<PythonList>(PyList_New(0)); +} + +PythonList::PythonList(int list_size) { + *this = Take<PythonList>(PyList_New(list_size)); +} + +bool PythonList::Check(PyObject *py_obj) { + if (!py_obj) + return false; + return PyList_Check(py_obj); +} + +uint32_t PythonList::GetSize() const { + if (IsValid()) + return PyList_GET_SIZE(m_py_obj); + return 0; +} + +PythonObject PythonList::GetItemAtIndex(uint32_t index) const { + if (IsValid()) + return PythonObject(PyRefType::Borrowed, PyList_GetItem(m_py_obj, index)); + return PythonObject(); +} + +void PythonList::SetItemAtIndex(uint32_t index, const PythonObject &object) { + if (IsAllocated() && object.IsValid()) { + // PyList_SetItem is documented to "steal" a reference, so we need to + // convert it to an owned reference by incrementing it. + Py_INCREF(object.get()); + PyList_SetItem(m_py_obj, index, object.get()); + } +} + +void PythonList::AppendItem(const PythonObject &object) { + if (IsAllocated() && object.IsValid()) { + // `PyList_Append` does *not* steal a reference, so do not call `Py_INCREF` + // here like we do with `PyList_SetItem`. + PyList_Append(m_py_obj, object.get()); + } +} + +StructuredData::ArraySP PythonList::CreateStructuredArray() const { + StructuredData::ArraySP result(new StructuredData::Array); + uint32_t count = GetSize(); + for (uint32_t i = 0; i < count; ++i) { + PythonObject obj = GetItemAtIndex(i); + result->AddItem(obj.CreateStructuredObject()); + } + return result; +} + +// PythonTuple + +PythonTuple::PythonTuple(PyInitialValue value) { + if (value == PyInitialValue::Empty) + *this = Take<PythonTuple>(PyTuple_New(0)); +} + +PythonTuple::PythonTuple(int tuple_size) { + *this = Take<PythonTuple>(PyTuple_New(tuple_size)); +} + +PythonTuple::PythonTuple(std::initializer_list<PythonObject> objects) { + m_py_obj = PyTuple_New(objects.size()); + + uint32_t idx = 0; + for (auto object : objects) { + if (object.IsValid()) + SetItemAtIndex(idx, object); + idx++; + } +} + +PythonTuple::PythonTuple(std::initializer_list<PyObject *> objects) { + m_py_obj = PyTuple_New(objects.size()); + + uint32_t idx = 0; + for (auto py_object : objects) { + PythonObject object(PyRefType::Borrowed, py_object); + if (object.IsValid()) + SetItemAtIndex(idx, object); + idx++; + } +} + +bool PythonTuple::Check(PyObject *py_obj) { + if (!py_obj) + return false; + return PyTuple_Check(py_obj); +} + +uint32_t PythonTuple::GetSize() const { + if (IsValid()) + return PyTuple_GET_SIZE(m_py_obj); + return 0; +} + +PythonObject PythonTuple::GetItemAtIndex(uint32_t index) const { + if (IsValid()) + return PythonObject(PyRefType::Borrowed, PyTuple_GetItem(m_py_obj, index)); + return PythonObject(); +} + +void PythonTuple::SetItemAtIndex(uint32_t index, const PythonObject &object) { + if (IsAllocated() && object.IsValid()) { + // PyTuple_SetItem is documented to "steal" a reference, so we need to + // convert it to an owned reference by incrementing it. + Py_INCREF(object.get()); + PyTuple_SetItem(m_py_obj, index, object.get()); + } +} + +StructuredData::ArraySP PythonTuple::CreateStructuredArray() const { + StructuredData::ArraySP result(new StructuredData::Array); + uint32_t count = GetSize(); + for (uint32_t i = 0; i < count; ++i) { + PythonObject obj = GetItemAtIndex(i); + result->AddItem(obj.CreateStructuredObject()); + } + return result; +} + +// PythonDictionary + +PythonDictionary::PythonDictionary(PyInitialValue value) { + if (value == PyInitialValue::Empty) + *this = Take<PythonDictionary>(PyDict_New()); +} + +bool PythonDictionary::Check(PyObject *py_obj) { + if (!py_obj) + return false; + + return PyDict_Check(py_obj); +} + +bool PythonDictionary::HasKey(const llvm::Twine &key) const { + if (!IsValid()) + return false; + + PythonString key_object(key.isSingleStringRef() ? key.getSingleStringRef() + : key.str()); + + if (int res = PyDict_Contains(m_py_obj, key_object.get()) > 0) + return res; + + PyErr_Print(); + return false; +} + +uint32_t PythonDictionary::GetSize() const { + if (IsValid()) + return PyDict_Size(m_py_obj); + return 0; +} + +PythonList PythonDictionary::GetKeys() const { + if (IsValid()) + return PythonList(PyRefType::Owned, PyDict_Keys(m_py_obj)); + return PythonList(PyInitialValue::Invalid); +} + +PythonObject PythonDictionary::GetItemForKey(const PythonObject &key) const { + auto item = GetItem(key); + if (!item) { + llvm::consumeError(item.takeError()); + return PythonObject(); + } + return std::move(item.get()); +} + +Expected<PythonObject> +PythonDictionary::GetItem(const PythonObject &key) const { + if (!IsValid()) + return nullDeref(); + PyObject *o = PyDict_GetItemWithError(m_py_obj, key.get()); + if (PyErr_Occurred()) + return exception(); + if (!o) + return keyError(); + return Retain<PythonObject>(o); +} + +Expected<PythonObject> PythonDictionary::GetItem(const Twine &key) const { + if (!IsValid()) + return nullDeref(); + PyObject *o = PyDict_GetItemString(m_py_obj, NullTerminated(key)); + if (PyErr_Occurred()) + return exception(); + if (!o) + return keyError(); + return Retain<PythonObject>(o); +} + +Error PythonDictionary::SetItem(const PythonObject &key, + const PythonObject &value) const { + if (!IsValid() || !value.IsValid()) + return nullDeref(); + int r = PyDict_SetItem(m_py_obj, key.get(), value.get()); + if (r < 0) + return exception(); + return Error::success(); +} + +Error PythonDictionary::SetItem(const Twine &key, + const PythonObject &value) const { + if (!IsValid() || !value.IsValid()) + return nullDeref(); + int r = PyDict_SetItemString(m_py_obj, NullTerminated(key), value.get()); + if (r < 0) + return exception(); + return Error::success(); +} + +void PythonDictionary::SetItemForKey(const PythonObject &key, + const PythonObject &value) { + Error error = SetItem(key, value); + if (error) + llvm::consumeError(std::move(error)); +} + +StructuredData::DictionarySP +PythonDictionary::CreateStructuredDictionary() const { + StructuredData::DictionarySP result(new StructuredData::Dictionary); + PythonList keys(GetKeys()); + uint32_t num_keys = keys.GetSize(); + for (uint32_t i = 0; i < num_keys; ++i) { + PythonObject key = keys.GetItemAtIndex(i); + PythonObject value = GetItemForKey(key); + StructuredData::ObjectSP structured_value = value.CreateStructuredObject(); + result->AddItem(key.Str().GetString(), structured_value); + } + return result; +} + +PythonModule PythonModule::BuiltinsModule() { return AddModule("builtins"); } + +PythonModule PythonModule::MainModule() { return AddModule("__main__"); } + +PythonModule PythonModule::AddModule(llvm::StringRef module) { + std::string str = module.str(); + return PythonModule(PyRefType::Borrowed, PyImport_AddModule(str.c_str())); +} + +Expected<PythonModule> PythonModule::Import(const Twine &name) { + PyObject *mod = PyImport_ImportModule(NullTerminated(name)); + if (!mod) + return exception(); + return Take<PythonModule>(mod); +} + +Expected<PythonObject> PythonModule::Get(const Twine &name) { + if (!IsValid()) + return nullDeref(); + PyObject *dict = PyModule_GetDict(m_py_obj); + if (!dict) + return exception(); + PyObject *item = PyDict_GetItemString(dict, NullTerminated(name)); + if (!item) + return exception(); + return Retain<PythonObject>(item); +} + +bool PythonModule::Check(PyObject *py_obj) { + if (!py_obj) + return false; + + return PyModule_Check(py_obj); +} + +PythonDictionary PythonModule::GetDictionary() const { + if (!IsValid()) + return PythonDictionary(); + return Retain<PythonDictionary>(PyModule_GetDict(m_py_obj)); +} + +bool PythonCallable::Check(PyObject *py_obj) { + if (!py_obj) + return false; + + return PyCallable_Check(py_obj); +} + +#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 +static const char get_arg_info_script[] = R"( +from inspect import signature, Parameter, ismethod +from collections import namedtuple +ArgInfo = namedtuple('ArgInfo', ['count', 'has_varargs']) +def main(f): + count = 0 + varargs = False + for parameter in signature(f).parameters.values(): + kind = parameter.kind + if kind in (Parameter.POSITIONAL_ONLY, + Parameter.POSITIONAL_OR_KEYWORD): + count += 1 + elif kind == Parameter.VAR_POSITIONAL: + varargs = True + elif kind in (Parameter.KEYWORD_ONLY, + Parameter.VAR_KEYWORD): + pass + else: + raise Exception(f'unknown parameter kind: {kind}') + return ArgInfo(count, varargs) +)"; +#endif + +Expected<PythonCallable::ArgInfo> PythonCallable::GetArgInfo() const { + ArgInfo result = {}; + if (!IsValid()) + return nullDeref(); + +#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 + + // no need to synchronize access to this global, we already have the GIL + static PythonScript get_arg_info(get_arg_info_script); + Expected<PythonObject> pyarginfo = get_arg_info(*this); + if (!pyarginfo) + return pyarginfo.takeError(); + long long count = + cantFail(As<long long>(pyarginfo.get().GetAttribute("count"))); + bool has_varargs = + cantFail(As<bool>(pyarginfo.get().GetAttribute("has_varargs"))); + result.max_positional_args = has_varargs ? ArgInfo::UNBOUNDED : count; + +#else + PyObject *py_func_obj; + bool is_bound_method = false; + bool is_class = false; + + if (PyType_Check(m_py_obj) || PyClass_Check(m_py_obj)) { + auto init = GetAttribute("__init__"); + if (!init) + return init.takeError(); + py_func_obj = init.get().get(); + is_class = true; + } else { + py_func_obj = m_py_obj; + } + + if (PyMethod_Check(py_func_obj)) { + py_func_obj = PyMethod_GET_FUNCTION(py_func_obj); + PythonObject im_self = GetAttributeValue("im_self"); + if (im_self.IsValid() && !im_self.IsNone()) + is_bound_method = true; + } else { + // see if this is a callable object with an __call__ method + if (!PyFunction_Check(py_func_obj)) { + PythonObject __call__ = GetAttributeValue("__call__"); + if (__call__.IsValid()) { + auto __callable__ = __call__.AsType<PythonCallable>(); + if (__callable__.IsValid()) { + py_func_obj = PyMethod_GET_FUNCTION(__callable__.get()); + PythonObject im_self = __callable__.GetAttributeValue("im_self"); + if (im_self.IsValid() && !im_self.IsNone()) + is_bound_method = true; + } + } + } + } + + if (!py_func_obj) + return result; + + PyCodeObject *code = (PyCodeObject *)PyFunction_GET_CODE(py_func_obj); + if (!code) + return result; + + auto count = code->co_argcount; + bool has_varargs = !!(code->co_flags & CO_VARARGS); + result.max_positional_args = + has_varargs ? ArgInfo::UNBOUNDED + : (count - (int)is_bound_method) - (int)is_class; + +#endif + + return result; +} + +constexpr unsigned + PythonCallable::ArgInfo::UNBOUNDED; // FIXME delete after c++17 + +PythonObject PythonCallable::operator()() { + return PythonObject(PyRefType::Owned, PyObject_CallObject(m_py_obj, nullptr)); +} + +PythonObject PythonCallable:: +operator()(std::initializer_list<PyObject *> args) { + PythonTuple arg_tuple(args); + return PythonObject(PyRefType::Owned, + PyObject_CallObject(m_py_obj, arg_tuple.get())); +} + +PythonObject PythonCallable:: +operator()(std::initializer_list<PythonObject> args) { + PythonTuple arg_tuple(args); + return PythonObject(PyRefType::Owned, + PyObject_CallObject(m_py_obj, arg_tuple.get())); +} + +bool PythonFile::Check(PyObject *py_obj) { + if (!py_obj) + return false; + // In Python 3, there is no `PyFile_Check`, and in fact PyFile is not even a + // first-class object type anymore. `PyFile_FromFd` is just a thin wrapper + // over `io.open()`, which returns some object derived from `io.IOBase`. As a + // result, the only way to detect a file in Python 3 is to check whether it + // inherits from `io.IOBase`. + auto io_module = PythonModule::Import("io"); + if (!io_module) { + llvm::consumeError(io_module.takeError()); + return false; + } + auto iobase = io_module.get().Get("IOBase"); + if (!iobase) { + llvm::consumeError(iobase.takeError()); + return false; + } + int r = PyObject_IsInstance(py_obj, iobase.get().get()); + if (r < 0) { + llvm::consumeError(exception()); // clear the exception and log it. + return false; + } + return !!r; +} + +const char *PythonException::toCString() const { + if (!m_repr_bytes) + return "unknown exception"; + return PyBytes_AS_STRING(m_repr_bytes); +} + +PythonException::PythonException(const char *caller) { + assert(PyErr_Occurred()); + m_exception_type = m_exception = m_traceback = m_repr_bytes = nullptr; + PyErr_Fetch(&m_exception_type, &m_exception, &m_traceback); + PyErr_NormalizeException(&m_exception_type, &m_exception, &m_traceback); + PyErr_Clear(); + if (m_exception) { + PyObject *repr = PyObject_Repr(m_exception); + if (repr) { + m_repr_bytes = PyUnicode_AsEncodedString(repr, "utf-8", nullptr); + if (!m_repr_bytes) { + PyErr_Clear(); + } + Py_XDECREF(repr); + } else { + PyErr_Clear(); + } + } + Log *log = GetLog(LLDBLog::Script); + if (caller) + LLDB_LOGF(log, "%s failed with exception: %s", caller, toCString()); + else + LLDB_LOGF(log, "python exception: %s", toCString()); +} +void PythonException::Restore() { + if (m_exception_type && m_exception) { + PyErr_Restore(m_exception_type, m_exception, m_traceback); + } else { + PyErr_SetString(PyExc_Exception, toCString()); + } + m_exception_type = m_exception = m_traceback = nullptr; +} + +PythonException::~PythonException() { + Py_XDECREF(m_exception_type); + Py_XDECREF(m_exception); + Py_XDECREF(m_traceback); + Py_XDECREF(m_repr_bytes); +} + +void PythonException::log(llvm::raw_ostream &OS) const { OS << toCString(); } + +std::error_code PythonException::convertToErrorCode() const { + return llvm::inconvertibleErrorCode(); +} + +bool PythonException::Matches(PyObject *exc) const { + return PyErr_GivenExceptionMatches(m_exception_type, exc); +} + +const char read_exception_script[] = R"( +import sys +from traceback import print_exception +if sys.version_info.major < 3: + from StringIO import StringIO +else: + from io import StringIO +def main(exc_type, exc_value, tb): + f = StringIO() + print_exception(exc_type, exc_value, tb, file=f) + return f.getvalue() +)"; + +std::string PythonException::ReadBacktrace() const { + + if (!m_traceback) + return toCString(); + + // no need to synchronize access to this global, we already have the GIL + static PythonScript read_exception(read_exception_script); + + Expected<std::string> backtrace = As<std::string>( + read_exception(m_exception_type, m_exception, m_traceback)); + + if (!backtrace) { + std::string message = + std::string(toCString()) + "\n" + + "Traceback unavailable, an error occurred while reading it:\n"; + return (message + llvm::toString(backtrace.takeError())); + } + + return std::move(backtrace.get()); +} + +char PythonException::ID = 0; + +llvm::Expected<File::OpenOptions> +GetOptionsForPyObject(const PythonObject &obj) { + auto options = File::OpenOptions(0); + auto readable = As<bool>(obj.CallMethod("readable")); + if (!readable) + return readable.takeError(); + auto writable = As<bool>(obj.CallMethod("writable")); + if (!writable) + return writable.takeError(); + if (readable.get() && writable.get()) + options |= File::eOpenOptionReadWrite; + else if (writable.get()) + options |= File::eOpenOptionWriteOnly; + else if (readable.get()) + options |= File::eOpenOptionReadOnly; + return options; +} + +// Base class template for python files. All it knows how to do +// is hold a reference to the python object and close or flush it +// when the File is closed. +namespace { +template <typename Base> class OwnedPythonFile : public Base { +public: + template <typename... Args> + OwnedPythonFile(const PythonFile &file, bool borrowed, Args... args) + : Base(args...), m_py_obj(file), m_borrowed(borrowed) { + assert(m_py_obj); + } + + ~OwnedPythonFile() override { + assert(m_py_obj); + GIL takeGIL; + Close(); + // we need to ensure the python object is released while we still + // hold the GIL + m_py_obj.Reset(); + } + + bool IsPythonSideValid() const { + GIL takeGIL; + auto closed = As<bool>(m_py_obj.GetAttribute("closed")); + if (!closed) { + llvm::consumeError(closed.takeError()); + return false; + } + return !closed.get(); + } + + bool IsValid() const override { + return IsPythonSideValid() && Base::IsValid(); + } + + Status Close() override { + assert(m_py_obj); + Status py_error, base_error; + GIL takeGIL; + if (!m_borrowed) { + auto r = m_py_obj.CallMethod("close"); + if (!r) + py_error = Status(r.takeError()); + } + base_error = Base::Close(); + if (py_error.Fail()) + return py_error; + return base_error; + }; + + PyObject *GetPythonObject() const { + assert(m_py_obj.IsValid()); + return m_py_obj.get(); + } + + static bool classof(const File *file) = delete; + +protected: + PythonFile m_py_obj; + bool m_borrowed; +}; +} // namespace + +// A SimplePythonFile is a OwnedPythonFile that just does all I/O as +// a NativeFile +namespace { +class SimplePythonFile : public OwnedPythonFile<NativeFile> { +public: + SimplePythonFile(const PythonFile &file, bool borrowed, int fd, + File::OpenOptions options) + : OwnedPythonFile(file, borrowed, fd, options, false) {} + + static char ID; + bool isA(const void *classID) const override { + return classID == &ID || NativeFile::isA(classID); + } + static bool classof(const File *file) { return file->isA(&ID); } +}; +char SimplePythonFile::ID = 0; +} // namespace + +namespace { +class PythonBuffer { +public: + PythonBuffer &operator=(const PythonBuffer &) = delete; + PythonBuffer(const PythonBuffer &) = delete; + + static Expected<PythonBuffer> Create(PythonObject &obj, + int flags = PyBUF_SIMPLE) { + Py_buffer py_buffer = {}; + PyObject_GetBuffer(obj.get(), &py_buffer, flags); + if (!py_buffer.obj) + return llvm::make_error<PythonException>(); + return PythonBuffer(py_buffer); + } + + PythonBuffer(PythonBuffer &&other) { + m_buffer = other.m_buffer; + other.m_buffer.obj = nullptr; + } + + ~PythonBuffer() { + if (m_buffer.obj) + PyBuffer_Release(&m_buffer); + } + + Py_buffer &get() { return m_buffer; } + +private: + // takes ownership of the buffer. + PythonBuffer(const Py_buffer &py_buffer) : m_buffer(py_buffer) {} + Py_buffer m_buffer; +}; +} // namespace + +// Shared methods between TextPythonFile and BinaryPythonFile +namespace { +class PythonIOFile : public OwnedPythonFile<File> { +public: + PythonIOFile(const PythonFile &file, bool borrowed) + : OwnedPythonFile(file, borrowed) {} + + ~PythonIOFile() override { Close(); } + + bool IsValid() const override { return IsPythonSideValid(); } + + Status Close() override { + assert(m_py_obj); + GIL takeGIL; + if (m_borrowed) + return Flush(); + auto r = m_py_obj.CallMethod("close"); + if (!r) + return Status(r.takeError()); + return Status(); + } + + Status Flush() override { + GIL takeGIL; + auto r = m_py_obj.CallMethod("flush"); + if (!r) + return Status(r.takeError()); + return Status(); + } + + Expected<File::OpenOptions> GetOptions() const override { + GIL takeGIL; + return GetOptionsForPyObject(m_py_obj); + } + + static char ID; + bool isA(const void *classID) const override { + return classID == &ID || File::isA(classID); + } + static bool classof(const File *file) { return file->isA(&ID); } +}; +char PythonIOFile::ID = 0; +} // namespace + +namespace { +class BinaryPythonFile : public PythonIOFile { +protected: + int m_descriptor; + +public: + BinaryPythonFile(int fd, const PythonFile &file, bool borrowed) + : PythonIOFile(file, borrowed), + m_descriptor(File::DescriptorIsValid(fd) ? fd + : File::kInvalidDescriptor) {} + + int GetDescriptor() const override { return m_descriptor; } + + Status Write(const void *buf, size_t &num_bytes) override { + GIL takeGIL; + PyObject *pybuffer_p = PyMemoryView_FromMemory( + const_cast<char *>((const char *)buf), num_bytes, PyBUF_READ); + if (!pybuffer_p) + return Status(llvm::make_error<PythonException>()); + auto pybuffer = Take<PythonObject>(pybuffer_p); + num_bytes = 0; + auto bytes_written = As<long long>(m_py_obj.CallMethod("write", pybuffer)); + if (!bytes_written) + return Status(bytes_written.takeError()); + if (bytes_written.get() < 0) + return Status(".write() method returned a negative number!"); + static_assert(sizeof(long long) >= sizeof(size_t), "overflow"); + num_bytes = bytes_written.get(); + return Status(); + } + + Status Read(void *buf, size_t &num_bytes) override { + GIL takeGIL; + static_assert(sizeof(long long) >= sizeof(size_t), "overflow"); + auto pybuffer_obj = + m_py_obj.CallMethod("read", (unsigned long long)num_bytes); + if (!pybuffer_obj) + return Status(pybuffer_obj.takeError()); + num_bytes = 0; + if (pybuffer_obj.get().IsNone()) { + // EOF + num_bytes = 0; + return Status(); + } + auto pybuffer = PythonBuffer::Create(pybuffer_obj.get()); + if (!pybuffer) + return Status(pybuffer.takeError()); + memcpy(buf, pybuffer.get().get().buf, pybuffer.get().get().len); + num_bytes = pybuffer.get().get().len; + return Status(); + } +}; +} // namespace + +namespace { +class TextPythonFile : public PythonIOFile { +protected: + int m_descriptor; + +public: + TextPythonFile(int fd, const PythonFile &file, bool borrowed) + : PythonIOFile(file, borrowed), + m_descriptor(File::DescriptorIsValid(fd) ? fd + : File::kInvalidDescriptor) {} + + int GetDescriptor() const override { return m_descriptor; } + + Status Write(const void *buf, size_t &num_bytes) override { + GIL takeGIL; + auto pystring = + PythonString::FromUTF8(llvm::StringRef((const char *)buf, num_bytes)); + if (!pystring) + return Status(pystring.takeError()); + num_bytes = 0; + auto bytes_written = + As<long long>(m_py_obj.CallMethod("write", pystring.get())); + if (!bytes_written) + return Status(bytes_written.takeError()); + if (bytes_written.get() < 0) + return Status(".write() method returned a negative number!"); + static_assert(sizeof(long long) >= sizeof(size_t), "overflow"); + num_bytes = bytes_written.get(); + return Status(); + } + + Status Read(void *buf, size_t &num_bytes) override { + GIL takeGIL; + size_t num_chars = num_bytes / 6; + size_t orig_num_bytes = num_bytes; + num_bytes = 0; + if (orig_num_bytes < 6) { + return Status("can't read less than 6 bytes from a utf8 text stream"); + } + auto pystring = As<PythonString>( + m_py_obj.CallMethod("read", (unsigned long long)num_chars)); + if (!pystring) + return Status(pystring.takeError()); + if (pystring.get().IsNone()) { + // EOF + return Status(); + } + auto stringref = pystring.get().AsUTF8(); + if (!stringref) + return Status(stringref.takeError()); + num_bytes = stringref.get().size(); + memcpy(buf, stringref.get().begin(), num_bytes); + return Status(); + } +}; +} // namespace + +llvm::Expected<FileSP> PythonFile::ConvertToFile(bool borrowed) { + if (!IsValid()) + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "invalid PythonFile"); + + int fd = PyObject_AsFileDescriptor(m_py_obj); + if (fd < 0) { + PyErr_Clear(); + return ConvertToFileForcingUseOfScriptingIOMethods(borrowed); + } + auto options = GetOptionsForPyObject(*this); + if (!options) + return options.takeError(); + + File::OpenOptions rw = + options.get() & (File::eOpenOptionReadOnly | File::eOpenOptionWriteOnly | + File::eOpenOptionReadWrite); + if (rw == File::eOpenOptionWriteOnly || rw == File::eOpenOptionReadWrite) { + // LLDB and python will not share I/O buffers. We should probably + // flush the python buffers now. + auto r = CallMethod("flush"); + if (!r) + return r.takeError(); + } + + FileSP file_sp; + if (borrowed) { + // In this case we don't need to retain the python + // object at all. + file_sp = std::make_shared<NativeFile>(fd, options.get(), false); + } else { + file_sp = std::static_pointer_cast<File>( + std::make_shared<SimplePythonFile>(*this, borrowed, fd, options.get())); + } + if (!file_sp->IsValid()) + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "invalid File"); + + return file_sp; +} + +llvm::Expected<FileSP> +PythonFile::ConvertToFileForcingUseOfScriptingIOMethods(bool borrowed) { + + assert(!PyErr_Occurred()); + + if (!IsValid()) + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "invalid PythonFile"); + + int fd = PyObject_AsFileDescriptor(m_py_obj); + if (fd < 0) { + PyErr_Clear(); + fd = File::kInvalidDescriptor; + } + + auto io_module = PythonModule::Import("io"); + if (!io_module) + return io_module.takeError(); + auto textIOBase = io_module.get().Get("TextIOBase"); + if (!textIOBase) + return textIOBase.takeError(); + auto rawIOBase = io_module.get().Get("RawIOBase"); + if (!rawIOBase) + return rawIOBase.takeError(); + auto bufferedIOBase = io_module.get().Get("BufferedIOBase"); + if (!bufferedIOBase) + return bufferedIOBase.takeError(); + + FileSP file_sp; + + auto isTextIO = IsInstance(textIOBase.get()); + if (!isTextIO) + return isTextIO.takeError(); + if (isTextIO.get()) + file_sp = std::static_pointer_cast<File>( + std::make_shared<TextPythonFile>(fd, *this, borrowed)); + + auto isRawIO = IsInstance(rawIOBase.get()); + if (!isRawIO) + return isRawIO.takeError(); + auto isBufferedIO = IsInstance(bufferedIOBase.get()); + if (!isBufferedIO) + return isBufferedIO.takeError(); + + if (isRawIO.get() || isBufferedIO.get()) { + file_sp = std::static_pointer_cast<File>( + std::make_shared<BinaryPythonFile>(fd, *this, borrowed)); + } + + if (!file_sp) + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "python file is neither text nor binary"); + + if (!file_sp->IsValid()) + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "invalid File"); + + return file_sp; +} + +Expected<PythonFile> PythonFile::FromFile(File &file, const char *mode) { + if (!file.IsValid()) + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "invalid file"); + + if (auto *simple = llvm::dyn_cast<SimplePythonFile>(&file)) + return Retain<PythonFile>(simple->GetPythonObject()); + if (auto *pythonio = llvm::dyn_cast<PythonIOFile>(&file)) + return Retain<PythonFile>(pythonio->GetPythonObject()); + + if (!mode) { + auto m = file.GetOpenMode(); + if (!m) + return m.takeError(); + mode = m.get(); + } + + PyObject *file_obj; + file_obj = PyFile_FromFd(file.GetDescriptor(), nullptr, mode, -1, nullptr, + "ignore", nullptr, /*closefd=*/0); + + if (!file_obj) + return exception(); + + return Take<PythonFile>(file_obj); +} + +Error PythonScript::Init() { + if (function.IsValid()) + return Error::success(); + + PythonDictionary globals(PyInitialValue::Empty); + auto builtins = PythonModule::BuiltinsModule(); + if (Error error = globals.SetItem("__builtins__", builtins)) + return error; + PyObject *o = + PyRun_String(script, Py_file_input, globals.get(), globals.get()); + if (!o) + return exception(); + Take<PythonObject>(o); + auto f = As<PythonCallable>(globals.GetItem("main")); + if (!f) + return f.takeError(); + function = std::move(f.get()); + + return Error::success(); +} + +llvm::Expected<PythonObject> +python::runStringOneLine(const llvm::Twine &string, + const PythonDictionary &globals, + const PythonDictionary &locals) { + if (!globals.IsValid() || !locals.IsValid()) + return nullDeref(); + + PyObject *code = + Py_CompileString(NullTerminated(string), "<string>", Py_eval_input); + if (!code) { + PyErr_Clear(); + code = + Py_CompileString(NullTerminated(string), "<string>", Py_single_input); + } + if (!code) + return exception(); + auto code_ref = Take<PythonObject>(code); + + PyObject *result = PyEval_EvalCode(code, globals.get(), locals.get()); + + if (!result) + return exception(); + + return Take<PythonObject>(result); +} + +llvm::Expected<PythonObject> +python::runStringMultiLine(const llvm::Twine &string, + const PythonDictionary &globals, + const PythonDictionary &locals) { + if (!globals.IsValid() || !locals.IsValid()) + return nullDeref(); + PyObject *result = PyRun_String(NullTerminated(string), Py_file_input, + globals.get(), locals.get()); + if (!result) + return exception(); + return Take<PythonObject>(result); +} + +#endif diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h new file mode 100644 index 000000000000..88c1bb7e729e --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h @@ -0,0 +1,788 @@ +//===-- PythonDataObjects.h--------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// +// !! FIXME FIXME FIXME !! +// +// Python APIs nearly all can return an exception. They do this +// by returning NULL, or -1, or some such value and setting +// the exception state with PyErr_Set*(). Exceptions must be +// handled before further python API functions are called. Failure +// to do so will result in asserts on debug builds of python. +// It will also sometimes, but not usually result in crashes of +// release builds. +// +// Nearly all the code in this header does not handle python exceptions +// correctly. It should all be converted to return Expected<> or +// Error types to capture the exception. +// +// Everything in this file except functions that return Error or +// Expected<> is considered deprecated and should not be +// used in new code. If you need to use it, fix it first. +// +// +// TODOs for this file +// +// * Make all methods safe for exceptions. +// +// * Eliminate method signatures that must translate exceptions into +// empty objects or NULLs. Almost everything here should return +// Expected<>. It should be acceptable for certain operations that +// can never fail to assert instead, such as the creation of +// PythonString from a string literal. +// +// * Eliminate Reset(), and make all non-default constructors private. +// Python objects should be created with Retain<> or Take<>, and they +// should be assigned with operator= +// +// * Eliminate default constructors, make python objects always +// nonnull, and use optionals where necessary. +// + + +#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H +#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H + +#include "lldb/Host/Config.h" + +#if LLDB_ENABLE_PYTHON + +// LLDB Python header must be included first +#include "lldb-python.h" + +#include "lldb/Host/File.h" +#include "lldb/Utility/StructuredData.h" + +#include "llvm/ADT/ArrayRef.h" + +namespace lldb_private { +namespace python { + +class PythonObject; +class PythonBytes; +class PythonString; +class PythonList; +class PythonDictionary; +class PythonInteger; +class PythonException; + +class GIL { +public: + GIL() { + m_state = PyGILState_Ensure(); + assert(!PyErr_Occurred()); + } + ~GIL() { PyGILState_Release(m_state); } + +protected: + PyGILState_STATE m_state; +}; + +enum class PyObjectType { + Unknown, + None, + Boolean, + Integer, + Dictionary, + List, + String, + Bytes, + ByteArray, + Module, + Callable, + Tuple, + File +}; + +enum class PyRefType { + Borrowed, // We are not given ownership of the incoming PyObject. + // We cannot safely hold it without calling Py_INCREF. + Owned // We have ownership of the incoming PyObject. We should + // not call Py_INCREF. +}; + + +// Take a reference that you already own, and turn it into +// a PythonObject. +// +// Most python API methods will return a +1 reference +// if they succeed or NULL if and only if +// they set an exception. Use this to collect such return +// values, after checking for NULL. +// +// If T is not just PythonObject, then obj must be already be +// checked to be of the correct type. +template <typename T> T Take(PyObject *obj) { + assert(obj); + assert(!PyErr_Occurred()); + T thing(PyRefType::Owned, obj); + assert(thing.IsValid()); + return thing; +} + +// Retain a reference you have borrowed, and turn it into +// a PythonObject. +// +// A minority of python APIs return a borrowed reference +// instead of a +1. They will also return NULL if and only +// if they set an exception. Use this to collect such return +// values, after checking for NULL. +// +// If T is not just PythonObject, then obj must be already be +// checked to be of the correct type. +template <typename T> T Retain(PyObject *obj) { + assert(obj); + assert(!PyErr_Occurred()); + T thing(PyRefType::Borrowed, obj); + assert(thing.IsValid()); + return thing; +} + +// This class can be used like a utility function to convert from +// a llvm-friendly Twine into a null-terminated const char *, +// which is the form python C APIs want their strings in. +// +// Example: +// const llvm::Twine &some_twine; +// PyFoo_Bar(x, y, z, NullTerminated(some_twine)); +// +// Why a class instead of a function? If the twine isn't already null +// terminated, it will need a temporary buffer to copy the string +// into. We need that buffer to stick around for the lifetime of the +// statement. +class NullTerminated { + const char *str; + llvm::SmallString<32> storage; + +public: + NullTerminated(const llvm::Twine &twine) { + llvm::StringRef ref = twine.toNullTerminatedStringRef(storage); + str = ref.begin(); + } + operator const char *() { return str; } +}; + +inline llvm::Error nullDeref() { + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "A NULL PyObject* was dereferenced"); +} + +inline llvm::Error exception(const char *s = nullptr) { + return llvm::make_error<PythonException>(s); +} + +inline llvm::Error keyError() { + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "key not in dict"); +} + +inline const char *py2_const_cast(const char *s) { return s; } + +enum class PyInitialValue { Invalid, Empty }; + +// DOC: https://docs.python.org/3/c-api/arg.html#building-values +template <typename T, typename Enable = void> struct PythonFormat; + +template <typename T, char F> struct PassthroughFormat { + static constexpr char format = F; + static constexpr T get(T t) { return t; } +}; + +template <> struct PythonFormat<char *> : PassthroughFormat<char *, 's'> {}; +template <> struct PythonFormat<const char *> : + PassthroughFormat<const char *, 's'> {}; +template <> struct PythonFormat<char> : PassthroughFormat<char, 'b'> {}; +template <> +struct PythonFormat<unsigned char> : PassthroughFormat<unsigned char, 'B'> {}; +template <> struct PythonFormat<short> : PassthroughFormat<short, 'h'> {}; +template <> +struct PythonFormat<unsigned short> : PassthroughFormat<unsigned short, 'H'> {}; +template <> struct PythonFormat<int> : PassthroughFormat<int, 'i'> {}; +template <> struct PythonFormat<bool> : PassthroughFormat<bool, 'p'> {}; +template <> +struct PythonFormat<unsigned int> : PassthroughFormat<unsigned int, 'I'> {}; +template <> struct PythonFormat<long> : PassthroughFormat<long, 'l'> {}; +template <> +struct PythonFormat<unsigned long> : PassthroughFormat<unsigned long, 'k'> {}; +template <> +struct PythonFormat<long long> : PassthroughFormat<long long, 'L'> {}; +template <> +struct PythonFormat<unsigned long long> + : PassthroughFormat<unsigned long long, 'K'> {}; +template <> +struct PythonFormat<PyObject *> : PassthroughFormat<PyObject *, 'O'> {}; + +template <typename T> +struct PythonFormat< + T, typename std::enable_if<std::is_base_of<PythonObject, T>::value>::type> { + static constexpr char format = 'O'; + static auto get(const T &value) { return value.get(); } +}; + +class PythonObject { +public: + PythonObject() = default; + + PythonObject(PyRefType type, PyObject *py_obj) { + m_py_obj = py_obj; + // If this is a borrowed reference, we need to convert it to + // an owned reference by incrementing it. If it is an owned + // reference (for example the caller allocated it with PyDict_New() + // then we must *not* increment it. + if (m_py_obj && Py_IsInitialized() && type == PyRefType::Borrowed) + Py_XINCREF(m_py_obj); + } + + PythonObject(const PythonObject &rhs) + : PythonObject(PyRefType::Borrowed, rhs.m_py_obj) {} + + PythonObject(PythonObject &&rhs) { + m_py_obj = rhs.m_py_obj; + rhs.m_py_obj = nullptr; + } + + ~PythonObject() { Reset(); } + + void Reset(); + + void Dump() const { + if (m_py_obj) + _PyObject_Dump(m_py_obj); + else + puts("NULL"); + } + + void Dump(Stream &strm) const; + + PyObject *get() const { return m_py_obj; } + + PyObject *release() { + PyObject *result = m_py_obj; + m_py_obj = nullptr; + return result; + } + + PythonObject &operator=(PythonObject other) { + Reset(); + m_py_obj = std::exchange(other.m_py_obj, nullptr); + return *this; + } + + PyObjectType GetObjectType() const; + + PythonString Repr() const; + + PythonString Str() const; + + static PythonObject ResolveNameWithDictionary(llvm::StringRef name, + const PythonDictionary &dict); + + template <typename T> + static T ResolveNameWithDictionary(llvm::StringRef name, + const PythonDictionary &dict) { + return ResolveNameWithDictionary(name, dict).AsType<T>(); + } + + PythonObject ResolveName(llvm::StringRef name) const; + + template <typename T> T ResolveName(llvm::StringRef name) const { + return ResolveName(name).AsType<T>(); + } + + bool HasAttribute(llvm::StringRef attribute) const; + + PythonObject GetAttributeValue(llvm::StringRef attribute) const; + + bool IsNone() const { return m_py_obj == Py_None; } + + bool IsValid() const { return m_py_obj != nullptr; } + + bool IsAllocated() const { return IsValid() && !IsNone(); } + + explicit operator bool() const { return IsValid() && !IsNone(); } + + template <typename T> T AsType() const { + if (!T::Check(m_py_obj)) + return T(); + return T(PyRefType::Borrowed, m_py_obj); + } + + StructuredData::ObjectSP CreateStructuredObject() const; + + template <typename... T> + llvm::Expected<PythonObject> CallMethod(const char *name, + const T &... t) const { + const char format[] = {'(', PythonFormat<T>::format..., ')', 0}; + PyObject *obj = + PyObject_CallMethod(m_py_obj, py2_const_cast(name), + py2_const_cast(format), PythonFormat<T>::get(t)...); + if (!obj) + return exception(); + return python::Take<PythonObject>(obj); + } + + template <typename... T> + llvm::Expected<PythonObject> Call(const T &... t) const { + const char format[] = {'(', PythonFormat<T>::format..., ')', 0}; + PyObject *obj = PyObject_CallFunction(m_py_obj, py2_const_cast(format), + PythonFormat<T>::get(t)...); + if (!obj) + return exception(); + return python::Take<PythonObject>(obj); + } + + llvm::Expected<PythonObject> GetAttribute(const llvm::Twine &name) const { + if (!m_py_obj) + return nullDeref(); + PyObject *obj = PyObject_GetAttrString(m_py_obj, NullTerminated(name)); + if (!obj) + return exception(); + return python::Take<PythonObject>(obj); + } + + llvm::Expected<PythonObject> GetType() const { + if (!m_py_obj) + return nullDeref(); + PyObject *obj = PyObject_Type(m_py_obj); + if (!obj) + return exception(); + return python::Take<PythonObject>(obj); + } + + llvm::Expected<bool> IsTrue() { + if (!m_py_obj) + return nullDeref(); + int r = PyObject_IsTrue(m_py_obj); + if (r < 0) + return exception(); + return !!r; + } + + llvm::Expected<long long> AsLongLong() const; + + llvm::Expected<unsigned long long> AsUnsignedLongLong() const; + + // wraps on overflow, instead of raising an error. + llvm::Expected<unsigned long long> AsModuloUnsignedLongLong() const; + + llvm::Expected<bool> IsInstance(const PythonObject &cls) { + if (!m_py_obj || !cls.IsValid()) + return nullDeref(); + int r = PyObject_IsInstance(m_py_obj, cls.get()); + if (r < 0) + return exception(); + return !!r; + } + +protected: + PyObject *m_py_obj = nullptr; +}; + + +// This is why C++ needs monads. +template <typename T> llvm::Expected<T> As(llvm::Expected<PythonObject> &&obj) { + if (!obj) + return obj.takeError(); + if (!T::Check(obj.get().get())) + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "type error"); + return T(PyRefType::Borrowed, std::move(obj.get().get())); +} + +template <> llvm::Expected<bool> As<bool>(llvm::Expected<PythonObject> &&obj); + +template <> +llvm::Expected<long long> As<long long>(llvm::Expected<PythonObject> &&obj); + +template <> +llvm::Expected<unsigned long long> +As<unsigned long long>(llvm::Expected<PythonObject> &&obj); + +template <> +llvm::Expected<std::string> As<std::string>(llvm::Expected<PythonObject> &&obj); + + +template <class T> class TypedPythonObject : public PythonObject { +public: + TypedPythonObject(PyRefType type, PyObject *py_obj) { + if (!py_obj) + return; + if (T::Check(py_obj)) + PythonObject::operator=(PythonObject(type, py_obj)); + else if (type == PyRefType::Owned) + Py_DECREF(py_obj); + } + + TypedPythonObject() = default; +}; + +class PythonBytes : public TypedPythonObject<PythonBytes> { +public: + using TypedPythonObject::TypedPythonObject; + explicit PythonBytes(llvm::ArrayRef<uint8_t> bytes); + PythonBytes(const uint8_t *bytes, size_t length); + + static bool Check(PyObject *py_obj); + + llvm::ArrayRef<uint8_t> GetBytes() const; + + size_t GetSize() const; + + void SetBytes(llvm::ArrayRef<uint8_t> stringbytes); + + StructuredData::StringSP CreateStructuredString() const; +}; + +class PythonByteArray : public TypedPythonObject<PythonByteArray> { +public: + using TypedPythonObject::TypedPythonObject; + explicit PythonByteArray(llvm::ArrayRef<uint8_t> bytes); + PythonByteArray(const uint8_t *bytes, size_t length); + PythonByteArray(const PythonBytes &object); + + static bool Check(PyObject *py_obj); + + llvm::ArrayRef<uint8_t> GetBytes() const; + + size_t GetSize() const; + + void SetBytes(llvm::ArrayRef<uint8_t> stringbytes); + + StructuredData::StringSP CreateStructuredString() const; +}; + +class PythonString : public TypedPythonObject<PythonString> { +public: + using TypedPythonObject::TypedPythonObject; + static llvm::Expected<PythonString> FromUTF8(llvm::StringRef string); + + PythonString() : TypedPythonObject() {} // MSVC requires this for some reason + + explicit PythonString(llvm::StringRef string); // safe, null on error + + static bool Check(PyObject *py_obj); + + llvm::StringRef GetString() const; // safe, empty string on error + + llvm::Expected<llvm::StringRef> AsUTF8() const; + + size_t GetSize() const; + + void SetString(llvm::StringRef string); // safe, null on error + + StructuredData::StringSP CreateStructuredString() const; +}; + +class PythonInteger : public TypedPythonObject<PythonInteger> { +public: + using TypedPythonObject::TypedPythonObject; + + PythonInteger() : TypedPythonObject() {} // MSVC requires this for some reason + + explicit PythonInteger(int64_t value); + + static bool Check(PyObject *py_obj); + + void SetInteger(int64_t value); + + StructuredData::IntegerSP CreateStructuredInteger() const; + + StructuredData::UnsignedIntegerSP CreateStructuredUnsignedInteger() const; + + StructuredData::SignedIntegerSP CreateStructuredSignedInteger() const; +}; + +class PythonBoolean : public TypedPythonObject<PythonBoolean> { +public: + using TypedPythonObject::TypedPythonObject; + + explicit PythonBoolean(bool value); + + static bool Check(PyObject *py_obj); + + bool GetValue() const; + + void SetValue(bool value); + + StructuredData::BooleanSP CreateStructuredBoolean() const; +}; + +class PythonList : public TypedPythonObject<PythonList> { +public: + using TypedPythonObject::TypedPythonObject; + + PythonList() : TypedPythonObject() {} // MSVC requires this for some reason + + explicit PythonList(PyInitialValue value); + explicit PythonList(int list_size); + + static bool Check(PyObject *py_obj); + + uint32_t GetSize() const; + + PythonObject GetItemAtIndex(uint32_t index) const; + + void SetItemAtIndex(uint32_t index, const PythonObject &object); + + void AppendItem(const PythonObject &object); + + StructuredData::ArraySP CreateStructuredArray() const; +}; + +class PythonTuple : public TypedPythonObject<PythonTuple> { +public: + using TypedPythonObject::TypedPythonObject; + + explicit PythonTuple(PyInitialValue value); + explicit PythonTuple(int tuple_size); + PythonTuple(std::initializer_list<PythonObject> objects); + PythonTuple(std::initializer_list<PyObject *> objects); + + static bool Check(PyObject *py_obj); + + uint32_t GetSize() const; + + PythonObject GetItemAtIndex(uint32_t index) const; + + void SetItemAtIndex(uint32_t index, const PythonObject &object); + + StructuredData::ArraySP CreateStructuredArray() const; +}; + +class PythonDictionary : public TypedPythonObject<PythonDictionary> { +public: + using TypedPythonObject::TypedPythonObject; + + PythonDictionary() : TypedPythonObject() {} // MSVC requires this for some reason + + explicit PythonDictionary(PyInitialValue value); + + static bool Check(PyObject *py_obj); + + bool HasKey(const llvm::Twine &key) const; + + uint32_t GetSize() const; + + PythonList GetKeys() const; + + PythonObject GetItemForKey(const PythonObject &key) const; // DEPRECATED + void SetItemForKey(const PythonObject &key, + const PythonObject &value); // DEPRECATED + + llvm::Expected<PythonObject> GetItem(const PythonObject &key) const; + llvm::Expected<PythonObject> GetItem(const llvm::Twine &key) const; + llvm::Error SetItem(const PythonObject &key, const PythonObject &value) const; + llvm::Error SetItem(const llvm::Twine &key, const PythonObject &value) const; + + StructuredData::DictionarySP CreateStructuredDictionary() const; +}; + +class PythonModule : public TypedPythonObject<PythonModule> { +public: + using TypedPythonObject::TypedPythonObject; + + static bool Check(PyObject *py_obj); + + static PythonModule BuiltinsModule(); + + static PythonModule MainModule(); + + static PythonModule AddModule(llvm::StringRef module); + + // safe, returns invalid on error; + static PythonModule ImportModule(llvm::StringRef name) { + std::string s = std::string(name); + auto mod = Import(s.c_str()); + if (!mod) { + llvm::consumeError(mod.takeError()); + return PythonModule(); + } + return std::move(mod.get()); + } + + static llvm::Expected<PythonModule> Import(const llvm::Twine &name); + + llvm::Expected<PythonObject> Get(const llvm::Twine &name); + + PythonDictionary GetDictionary() const; +}; + +class PythonCallable : public TypedPythonObject<PythonCallable> { +public: + using TypedPythonObject::TypedPythonObject; + + struct ArgInfo { + /* the largest number of positional arguments this callable + * can accept, or UNBOUNDED, ie UINT_MAX if it's a varargs + * function and can accept an arbitrary number */ + unsigned max_positional_args; + static constexpr unsigned UNBOUNDED = UINT_MAX; // FIXME c++17 inline + }; + + static bool Check(PyObject *py_obj); + + llvm::Expected<ArgInfo> GetArgInfo() const; + + PythonObject operator()(); + + PythonObject operator()(std::initializer_list<PyObject *> args); + + PythonObject operator()(std::initializer_list<PythonObject> args); + + template <typename Arg, typename... Args> + PythonObject operator()(const Arg &arg, Args... args) { + return operator()({arg, args...}); + } +}; + +class PythonFile : public TypedPythonObject<PythonFile> { +public: + using TypedPythonObject::TypedPythonObject; + + PythonFile() : TypedPythonObject() {} // MSVC requires this for some reason + + static bool Check(PyObject *py_obj); + + static llvm::Expected<PythonFile> FromFile(File &file, + const char *mode = nullptr); + + llvm::Expected<lldb::FileSP> ConvertToFile(bool borrowed = false); + llvm::Expected<lldb::FileSP> + ConvertToFileForcingUseOfScriptingIOMethods(bool borrowed = false); +}; + +class PythonException : public llvm::ErrorInfo<PythonException> { +private: + PyObject *m_exception_type, *m_exception, *m_traceback; + PyObject *m_repr_bytes; + +public: + static char ID; + const char *toCString() const; + PythonException(const char *caller = nullptr); + void Restore(); + ~PythonException() override; + void log(llvm::raw_ostream &OS) const override; + std::error_code convertToErrorCode() const override; + bool Matches(PyObject *exc) const; + std::string ReadBacktrace() const; +}; + +// This extracts the underlying T out of an Expected<T> and returns it. +// If the Expected is an Error instead of a T, that error will be converted +// into a python exception, and this will return a default-constructed T. +// +// This is appropriate for use right at the boundary of python calling into +// C++, such as in a SWIG typemap. In such a context you should simply +// check if the returned T is valid, and if it is, return a NULL back +// to python. This will result in the Error being raised as an exception +// from python code's point of view. +// +// For example: +// ``` +// Expected<Foo *> efoop = some_cpp_function(); +// Foo *foop = unwrapOrSetPythonException(efoop); +// if (!foop) +// return NULL; +// do_something(*foop); +// +// If the Error returned was itself created because a python exception was +// raised when C++ code called into python, then the original exception +// will be restored. Otherwise a simple string exception will be raised. +template <typename T> T unwrapOrSetPythonException(llvm::Expected<T> expected) { + if (expected) + return expected.get(); + llvm::handleAllErrors( + expected.takeError(), [](PythonException &E) { E.Restore(); }, + [](const llvm::ErrorInfoBase &E) { + PyErr_SetString(PyExc_Exception, E.message().c_str()); + }); + return T(); +} + +// This is only here to help incrementally migrate old, exception-unsafe +// code. +template <typename T> T unwrapIgnoringErrors(llvm::Expected<T> expected) { + if (expected) + return std::move(expected.get()); + llvm::consumeError(expected.takeError()); + return T(); +} + +llvm::Expected<PythonObject> runStringOneLine(const llvm::Twine &string, + const PythonDictionary &globals, + const PythonDictionary &locals); + +llvm::Expected<PythonObject> runStringMultiLine(const llvm::Twine &string, + const PythonDictionary &globals, + const PythonDictionary &locals); + +// Sometimes the best way to interact with a python interpreter is +// to run some python code. You construct a PythonScript with +// script string. The script assigns some function to `_function_` +// and you get a C++ callable object that calls the python function. +// +// Example: +// +// const char script[] = R"( +// def main(x, y): +// .... +// )"; +// +// Expected<PythonObject> cpp_foo_wrapper(PythonObject x, PythonObject y) { +// // no need to synchronize access to this global, we already have the GIL +// static PythonScript foo(script) +// return foo(x, y); +// } +class PythonScript { + const char *script; + PythonCallable function; + + llvm::Error Init(); + +public: + PythonScript(const char *script) : script(script), function() {} + + template <typename... Args> + llvm::Expected<PythonObject> operator()(Args &&... args) { + if (llvm::Error error = Init()) + return std::move(error); + return function.Call(std::forward<Args>(args)...); + } +}; + +class StructuredPythonObject : public StructuredData::Generic { +public: + StructuredPythonObject() : StructuredData::Generic() {} + + // Take ownership of the object we received. + StructuredPythonObject(PythonObject obj) + : StructuredData::Generic(obj.release()) {} + + ~StructuredPythonObject() override { + // Hand ownership back to a (temporary) PythonObject instance and let it + // take care of releasing it. + PythonObject(PyRefType::Owned, static_cast<PyObject *>(GetValue())); + } + + bool IsValid() const override { return GetValue() && GetValue() != Py_None; } + + void Serialize(llvm::json::OStream &s) const override; + +private: + StructuredPythonObject(const StructuredPythonObject &) = delete; + const StructuredPythonObject & + operator=(const StructuredPythonObject &) = delete; +}; + +} // namespace python +} // namespace lldb_private + +#endif + +#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonReadline.cpp b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonReadline.cpp new file mode 100644 index 000000000000..3cbd3b5efecc --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonReadline.cpp @@ -0,0 +1,65 @@ +#include "PythonReadline.h" + +#ifdef LLDB_USE_LIBEDIT_READLINE_COMPAT_MODULE + +#include <cstdio> + +#include <editline/readline.h> + +// Simple implementation of the Python readline module using libedit. +// In the event that libedit is excluded from the build, this turns +// back into a null implementation that blocks the module from pulling +// in the GNU readline shared lib, which causes linkage confusion when +// both readline and libedit's readline compatibility symbols collide. +// +// Currently it only installs a PyOS_ReadlineFunctionPointer, without +// implementing any of the readline module methods. This is meant to +// work around LLVM pr18841 to avoid seg faults in the stock Python +// readline.so linked against GNU readline. +// +// Bug on the cpython side: https://bugs.python.org/issue38634 + +PyDoc_STRVAR(moduleDocumentation, + "Simple readline module implementation based on libedit."); + +static struct PyModuleDef readline_module = { + PyModuleDef_HEAD_INIT, // m_base + "lldb_editline", // m_name + moduleDocumentation, // m_doc + -1, // m_size + nullptr, // m_methods + nullptr, // m_reload + nullptr, // m_traverse + nullptr, // m_clear + nullptr, // m_free +}; + +static char *simple_readline(FILE *stdin, FILE *stdout, const char *prompt) { + rl_instream = stdin; + rl_outstream = stdout; + char *line = readline(prompt); + if (!line) { + char *ret = (char *)PyMem_RawMalloc(1); + if (ret != nullptr) + *ret = '\0'; + return ret; + } + if (*line) + add_history(line); + int n = strlen(line); + char *ret = (char *)PyMem_RawMalloc(n + 2); + if (ret) { + memcpy(ret, line, n); + free(line); + ret[n] = '\n'; + ret[n + 1] = '\0'; + } + return ret; +} + +PyMODINIT_FUNC initlldb_readline(void) { + PyOS_ReadlineFunctionPointer = simple_readline; + + return PyModule_Create(&readline_module); +} +#endif diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonReadline.h b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonReadline.h new file mode 100644 index 000000000000..c75219eb1a4f --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonReadline.h @@ -0,0 +1,28 @@ +//===-- PythonReadline.h ----------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONREADLINE_H +#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONREADLINE_H + +#include "lldb/Host/Config.h" + +#if LLDB_ENABLE_LIBEDIT && defined(__linux__) +// NOTE: Since Python may define some pre-processor definitions which affect the +// standard headers on some systems, you must include Python.h before any +// standard headers are included. +#include "Python.h" + +// no need to hack into Python's readline module if libedit isn't used. +// +#define LLDB_USE_LIBEDIT_READLINE_COMPAT_MODULE 1 + +PyMODINIT_FUNC initlldb_readline(void); + +#endif + +#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONREADLINE_H diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h new file mode 100644 index 000000000000..3026b6113ae8 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h @@ -0,0 +1,271 @@ +//===-- ScriptInterpreterPython.h -------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H +#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H + +#include <optional> +#include <string> + +#include "lldb/Host/Config.h" + +#if LLDB_ENABLE_PYTHON + +// LLDB Python header must be included first +#include "lldb-python.h" + +#include "Plugins/ScriptInterpreter/Python/PythonDataObjects.h" +#include "lldb/lldb-forward.h" +#include "lldb/lldb-types.h" +#include "llvm/Support/Error.h" + +namespace lldb { +class SBEvent; +class SBCommandReturnObject; +class SBValue; +class SBStream; +class SBStructuredData; +class SBFileSpec; +class SBModuleSpec; +class SBStringList; +} // namespace lldb + +namespace lldb_private { +namespace python { + +typedef struct swig_type_info swig_type_info; + +python::PythonObject ToSWIGHelper(void *obj, swig_type_info *info); + +/// A class that automatically clears an SB object when it goes out of scope. +/// Use for cases where the SB object points to a temporary/unowned entity. +template <typename T> class ScopedPythonObject : PythonObject { +public: + ScopedPythonObject(T *sb, swig_type_info *info) + : PythonObject(ToSWIGHelper(sb, info)), m_sb(sb) {} + ~ScopedPythonObject() { + if (m_sb) + *m_sb = T(); + } + ScopedPythonObject(ScopedPythonObject &&rhs) + : PythonObject(std::move(rhs)), m_sb(std::exchange(rhs.m_sb, nullptr)) {} + ScopedPythonObject(const ScopedPythonObject &) = delete; + ScopedPythonObject &operator=(const ScopedPythonObject &) = delete; + ScopedPythonObject &operator=(ScopedPythonObject &&) = delete; + + const PythonObject &obj() const { return *this; } + +private: + T *m_sb; +}; + +// TODO: We may want to support other languages in the future w/ SWIG (we +// already support Lua right now, for example). We could create a generic +// SWIGBridge class and have this one specialize it, something like this: +// +// <typename T> +// class SWIGBridge { +// static T ToSWIGWrapper(...); +// }; +// +// class SWIGPythonBridge : public SWIGBridge<PythonObject> { +// template<> static PythonObject ToSWIGWrapper(...); +// }; +// +// And we should be able to more easily support things like Lua +class SWIGBridge { +public: + static PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBValue> value_sb); + static PythonObject ToSWIGWrapper(lldb::ValueObjectSP value_sp); + static PythonObject ToSWIGWrapper(lldb::TargetSP target_sp); + static PythonObject ToSWIGWrapper(lldb::ProcessSP process_sp); + static PythonObject ToSWIGWrapper(lldb::ThreadPlanSP thread_plan_sp); + static PythonObject ToSWIGWrapper(lldb::BreakpointSP breakpoint_sp); + static PythonObject ToSWIGWrapper(const Status &status); + static PythonObject ToSWIGWrapper(const StructuredDataImpl &data_impl); + static PythonObject ToSWIGWrapper(lldb::ThreadSP thread_sp); + static PythonObject ToSWIGWrapper(lldb::StackFrameSP frame_sp); + static PythonObject ToSWIGWrapper(lldb::DebuggerSP debugger_sp); + static PythonObject ToSWIGWrapper(lldb::WatchpointSP watchpoint_sp); + static PythonObject ToSWIGWrapper(lldb::BreakpointLocationSP bp_loc_sp); + static PythonObject ToSWIGWrapper(lldb::TypeImplSP type_impl_sp); + static PythonObject ToSWIGWrapper(lldb::ExecutionContextRefSP ctx_sp); + static PythonObject ToSWIGWrapper(const TypeSummaryOptions &summary_options); + static PythonObject ToSWIGWrapper(const SymbolContext &sym_ctx); + static PythonObject ToSWIGWrapper(const Stream *stream); + static PythonObject ToSWIGWrapper(std::shared_ptr<lldb::SBStream> stream_sb); + static PythonObject ToSWIGWrapper(Event *event); + + static PythonObject ToSWIGWrapper(lldb::ProcessAttachInfoSP attach_info_sp); + static PythonObject ToSWIGWrapper(lldb::ProcessLaunchInfoSP launch_info_sp); + static PythonObject ToSWIGWrapper(lldb::DataExtractorSP data_extractor_sp); + + static PythonObject + ToSWIGWrapper(std::unique_ptr<lldb::SBStructuredData> data_sb); + static PythonObject + ToSWIGWrapper(std::unique_ptr<lldb::SBFileSpec> file_spec_sb); + static PythonObject + ToSWIGWrapper(std::unique_ptr<lldb::SBModuleSpec> module_spec_sb); + + static python::ScopedPythonObject<lldb::SBCommandReturnObject> + ToSWIGWrapper(CommandReturnObject &cmd_retobj); + // These prototypes are the Pythonic implementations of the required + // callbacks. Although these are scripting-language specific, their definition + // depends on the public API. + + static llvm::Expected<bool> LLDBSwigPythonBreakpointCallbackFunction( + const char *python_function_name, const char *session_dictionary_name, + const lldb::StackFrameSP &sb_frame, + const lldb::BreakpointLocationSP &sb_bp_loc, + const lldb_private::StructuredDataImpl &args_impl); + + static bool LLDBSwigPythonWatchpointCallbackFunction( + const char *python_function_name, const char *session_dictionary_name, + const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp); + + static bool + LLDBSwigPythonFormatterCallbackFunction(const char *python_function_name, + const char *session_dictionary_name, + lldb::TypeImplSP type_impl_sp); + + static bool LLDBSwigPythonCallTypeScript( + const char *python_function_name, const void *session_dictionary, + const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper, + const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval); + + static python::PythonObject + LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name, + const char *session_dictionary_name, + const lldb::ValueObjectSP &valobj_sp); + + static python::PythonObject + LLDBSwigPythonCreateCommandObject(const char *python_class_name, + const char *session_dictionary_name, + lldb::DebuggerSP debugger_sp); + + static python::PythonObject LLDBSwigPythonCreateScriptedBreakpointResolver( + const char *python_class_name, const char *session_dictionary_name, + const StructuredDataImpl &args, const lldb::BreakpointSP &bkpt_sp); + + static unsigned int + LLDBSwigPythonCallBreakpointResolver(void *implementor, + const char *method_name, + lldb_private::SymbolContext *sym_ctx); + + static python::PythonObject LLDBSwigPythonCreateScriptedStopHook( + lldb::TargetSP target_sp, const char *python_class_name, + const char *session_dictionary_name, const StructuredDataImpl &args, + lldb_private::Status &error); + + static bool + LLDBSwigPythonStopHookCallHandleStop(void *implementor, + lldb::ExecutionContextRefSP exc_ctx, + lldb::StreamSP stream); + + static size_t LLDBSwigPython_CalculateNumChildren(PyObject *implementor, + uint32_t max); + + static PyObject *LLDBSwigPython_GetChildAtIndex(PyObject *implementor, + uint32_t idx); + + static int LLDBSwigPython_GetIndexOfChildWithName(PyObject *implementor, + const char *child_name); + + static lldb::ValueObjectSP + LLDBSWIGPython_GetValueObjectSPFromSBValue(void *data); + + static bool LLDBSwigPython_UpdateSynthProviderInstance(PyObject *implementor); + + static bool + LLDBSwigPython_MightHaveChildrenSynthProviderInstance(PyObject *implementor); + + static PyObject * + LLDBSwigPython_GetValueSynthProviderInstance(PyObject *implementor); + + static bool + LLDBSwigPythonCallCommand(const char *python_function_name, + const char *session_dictionary_name, + lldb::DebuggerSP debugger, const char *args, + lldb_private::CommandReturnObject &cmd_retobj, + lldb::ExecutionContextRefSP exe_ctx_ref_sp); + + static bool + LLDBSwigPythonCallCommandObject(PyObject *implementor, + lldb::DebuggerSP debugger, const char *args, + lldb_private::CommandReturnObject &cmd_retobj, + lldb::ExecutionContextRefSP exe_ctx_ref_sp); + static bool + LLDBSwigPythonCallParsedCommandObject(PyObject *implementor, + lldb::DebuggerSP debugger, + StructuredDataImpl &args_impl, + lldb_private::CommandReturnObject &cmd_retobj, + lldb::ExecutionContextRefSP exe_ctx_ref_sp); + + static std::optional<std::string> + LLDBSwigPythonGetRepeatCommandForScriptedCommand(PyObject *implementor, + std::string &command); + + static bool LLDBSwigPythonCallModuleInit(const char *python_module_name, + const char *session_dictionary_name, + lldb::DebuggerSP debugger); + + static python::PythonObject + LLDBSWIGPythonCreateOSPlugin(const char *python_class_name, + const char *session_dictionary_name, + const lldb::ProcessSP &process_sp); + + static python::PythonObject + LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name, + const char *session_dictionary_name); + + static PyObject * + LLDBSwigPython_GetRecognizedArguments(PyObject *implementor, + const lldb::StackFrameSP &frame_sp); + + static bool LLDBSWIGPythonRunScriptKeywordProcess( + const char *python_function_name, const char *session_dictionary_name, + const lldb::ProcessSP &process, std::string &output); + + static std::optional<std::string> + LLDBSWIGPythonRunScriptKeywordThread(const char *python_function_name, + const char *session_dictionary_name, + lldb::ThreadSP thread); + + static bool LLDBSWIGPythonRunScriptKeywordTarget( + const char *python_function_name, const char *session_dictionary_name, + const lldb::TargetSP &target, std::string &output); + + static std::optional<std::string> + LLDBSWIGPythonRunScriptKeywordFrame(const char *python_function_name, + const char *session_dictionary_name, + lldb::StackFrameSP frame); + + static bool LLDBSWIGPythonRunScriptKeywordValue( + const char *python_function_name, const char *session_dictionary_name, + const lldb::ValueObjectSP &value, std::string &output); + + static void * + LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting, + const lldb::TargetSP &target_sp); +}; + +void *LLDBSWIGPython_CastPyObjectToSBData(PyObject *data); +void *LLDBSWIGPython_CastPyObjectToSBBreakpoint(PyObject *data); +void *LLDBSWIGPython_CastPyObjectToSBAttachInfo(PyObject *data); +void *LLDBSWIGPython_CastPyObjectToSBLaunchInfo(PyObject *data); +void *LLDBSWIGPython_CastPyObjectToSBError(PyObject *data); +void *LLDBSWIGPython_CastPyObjectToSBEvent(PyObject *data); +void *LLDBSWIGPython_CastPyObjectToSBStream(PyObject *data); +void *LLDBSWIGPython_CastPyObjectToSBValue(PyObject *data); +void *LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *data); +} // namespace python + +} // namespace lldb_private + +#endif // LLDB_ENABLE_PYTHON +#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp new file mode 100644 index 000000000000..70fa6d83e306 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp @@ -0,0 +1,3184 @@ +//===-- ScriptInterpreterPython.cpp ---------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "lldb/Host/Config.h" +#include "lldb/lldb-enumerations.h" + +#if LLDB_ENABLE_PYTHON + +// LLDB Python header must be included first +#include "lldb-python.h" + +#include "Interfaces/OperatingSystemPythonInterface.h" +#include "Interfaces/ScriptedPlatformPythonInterface.h" +#include "Interfaces/ScriptedProcessPythonInterface.h" +#include "Interfaces/ScriptedThreadPlanPythonInterface.h" +#include "Interfaces/ScriptedThreadPythonInterface.h" +#include "PythonDataObjects.h" +#include "PythonReadline.h" +#include "SWIGPythonBridge.h" +#include "ScriptInterpreterPythonImpl.h" + +#include "lldb/API/SBError.h" +#include "lldb/API/SBExecutionContext.h" +#include "lldb/API/SBFrame.h" +#include "lldb/API/SBValue.h" +#include "lldb/Breakpoint/StoppointCallbackContext.h" +#include "lldb/Breakpoint/WatchpointOptions.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" +#include "lldb/Host/HostInfo.h" +#include "lldb/Host/Pipe.h" +#include "lldb/Interpreter/CommandInterpreter.h" +#include "lldb/Interpreter/CommandReturnObject.h" +#include "lldb/Target/Thread.h" +#include "lldb/Target/ThreadPlan.h" +#include "lldb/Utility/Instrumentation.h" +#include "lldb/Utility/LLDBLog.h" +#include "lldb/Utility/Timer.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/FormatAdapters.h" + +#include <cstdio> +#include <cstdlib> +#include <memory> +#include <mutex> +#include <optional> +#include <string> + +using namespace lldb; +using namespace lldb_private; +using namespace lldb_private::python; +using llvm::Expected; + +LLDB_PLUGIN_DEFINE(ScriptInterpreterPython) + +// Defined in the SWIG source file +extern "C" PyObject *PyInit__lldb(void); + +#define LLDBSwigPyInit PyInit__lldb + +#if defined(_WIN32) +// Don't mess with the signal handlers on Windows. +#define LLDB_USE_PYTHON_SET_INTERRUPT 0 +#else +// PyErr_SetInterrupt was introduced in 3.2. +#define LLDB_USE_PYTHON_SET_INTERRUPT \ + (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2) || (PY_MAJOR_VERSION > 3) +#endif + +static ScriptInterpreterPythonImpl *GetPythonInterpreter(Debugger &debugger) { + ScriptInterpreter *script_interpreter = + debugger.GetScriptInterpreter(true, lldb::eScriptLanguagePython); + return static_cast<ScriptInterpreterPythonImpl *>(script_interpreter); +} + +namespace { + +// Initializing Python is not a straightforward process. We cannot control +// what external code may have done before getting to this point in LLDB, +// including potentially having already initialized Python, so we need to do a +// lot of work to ensure that the existing state of the system is maintained +// across our initialization. We do this by using an RAII pattern where we +// save off initial state at the beginning, and restore it at the end +struct InitializePythonRAII { +public: + InitializePythonRAII() { + InitializePythonHome(); + + // The table of built-in modules can only be extended before Python is + // initialized. + if (!Py_IsInitialized()) { +#ifdef LLDB_USE_LIBEDIT_READLINE_COMPAT_MODULE + // 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 != nullptr; p++) { + if (strcmp(p->name, "readline") == 0) { + p->initfunc = initlldb_readline; + break; + } + } + if (!ReadlinePatched) { + PyImport_AppendInittab("readline", initlldb_readline); + ReadlinePatched = true; + } +#endif + + // Register _lldb as a built-in module. + PyImport_AppendInittab("_lldb", LLDBSwigPyInit); + } + +// Python < 3.2 and Python >= 3.2 reversed the ordering requirements for +// calling `Py_Initialize` and `PyEval_InitThreads`. < 3.2 requires that you +// call `PyEval_InitThreads` first, and >= 3.2 requires that you call it last. +#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2) || (PY_MAJOR_VERSION > 3) + Py_InitializeEx(0); + InitializeThreadsPrivate(); +#else + InitializeThreadsPrivate(); + Py_InitializeEx(0); +#endif + } + + ~InitializePythonRAII() { + if (m_was_already_initialized) { + Log *log = GetLog(LLDBLog::Script); + LLDB_LOGV(log, "Releasing PyGILState. Returning to state = {0}locked", + m_gil_state == PyGILState_UNLOCKED ? "un" : ""); + PyGILState_Release(m_gil_state); + } else { + // We initialized the threads in this function, just unlock the GIL. + PyEval_SaveThread(); + } + } + +private: + void InitializePythonHome() { +#if LLDB_EMBED_PYTHON_HOME + typedef wchar_t *str_type; + static str_type g_python_home = []() -> str_type { + const char *lldb_python_home = LLDB_PYTHON_HOME; + const char *absolute_python_home = nullptr; + llvm::SmallString<64> path; + if (llvm::sys::path::is_absolute(lldb_python_home)) { + absolute_python_home = lldb_python_home; + } else { + FileSpec spec = HostInfo::GetShlibDir(); + if (!spec) + return nullptr; + spec.GetPath(path); + llvm::sys::path::append(path, lldb_python_home); + absolute_python_home = path.c_str(); + } + size_t size = 0; + return Py_DecodeLocale(absolute_python_home, &size); + }(); + if (g_python_home != nullptr) { + Py_SetPythonHome(g_python_home); + } +#endif + } + + void InitializeThreadsPrivate() { +// Since Python 3.7 `Py_Initialize` calls `PyEval_InitThreads` inside itself, +// so there is no way to determine whether the embedded interpreter +// was already initialized by some external code. `PyEval_ThreadsInitialized` +// would always return `true` and `PyGILState_Ensure/Release` flow would be +// executed instead of unlocking GIL with `PyEval_SaveThread`. When +// an another thread calls `PyGILState_Ensure` it would get stuck in deadlock. +#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 7) || (PY_MAJOR_VERSION > 3) + // The only case we should go further and acquire the GIL: it is unlocked. + if (PyGILState_Check()) + return; +#endif + +// `PyEval_ThreadsInitialized` was deprecated in Python 3.9 and removed in +// Python 3.13. It has been returning `true` always since Python 3.7. +#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9) || (PY_MAJOR_VERSION < 3) + if (PyEval_ThreadsInitialized()) { +#else + if (true) { +#endif + Log *log = GetLog(LLDBLog::Script); + + m_was_already_initialized = true; + m_gil_state = PyGILState_Ensure(); + LLDB_LOGV(log, "Ensured PyGILState. Previous state = {0}locked\n", + m_gil_state == PyGILState_UNLOCKED ? "un" : ""); + +// `PyEval_InitThreads` was deprecated in Python 3.9 and removed in +// Python 3.13. +#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9) || (PY_MAJOR_VERSION < 3) + return; + } + + // InitThreads acquires the GIL if it hasn't been called before. + PyEval_InitThreads(); +#else + } +#endif + } + + PyGILState_STATE m_gil_state = PyGILState_UNLOCKED; + bool m_was_already_initialized = false; +}; + +#if LLDB_USE_PYTHON_SET_INTERRUPT +/// Saves the current signal handler for the specified signal and restores +/// it at the end of the current scope. +struct RestoreSignalHandlerScope { + /// The signal handler. + struct sigaction m_prev_handler; + int m_signal_code; + RestoreSignalHandlerScope(int signal_code) : m_signal_code(signal_code) { + // Initialize sigaction to their default state. + std::memset(&m_prev_handler, 0, sizeof(m_prev_handler)); + // Don't install a new handler, just read back the old one. + struct sigaction *new_handler = nullptr; + int signal_err = ::sigaction(m_signal_code, new_handler, &m_prev_handler); + lldbassert(signal_err == 0 && "sigaction failed to read handler"); + } + ~RestoreSignalHandlerScope() { + int signal_err = ::sigaction(m_signal_code, &m_prev_handler, nullptr); + lldbassert(signal_err == 0 && "sigaction failed to restore old handler"); + } +}; +#endif +} // namespace + +void ScriptInterpreterPython::ComputePythonDirForApple( + llvm::SmallVectorImpl<char> &path) { + auto style = llvm::sys::path::Style::posix; + + llvm::StringRef path_ref(path.begin(), path.size()); + auto rbegin = llvm::sys::path::rbegin(path_ref, style); + auto rend = llvm::sys::path::rend(path_ref); + auto framework = std::find(rbegin, rend, "LLDB.framework"); + if (framework == rend) { + ComputePythonDir(path); + return; + } + path.resize(framework - rend); + llvm::sys::path::append(path, style, "LLDB.framework", "Resources", "Python"); +} + +void ScriptInterpreterPython::ComputePythonDir( + llvm::SmallVectorImpl<char> &path) { + // Build the path by backing out of the lib dir, then building with whatever + // the real python interpreter uses. (e.g. lib for most, lib64 on RHEL + // x86_64, or bin on Windows). + llvm::sys::path::remove_filename(path); + llvm::sys::path::append(path, LLDB_PYTHON_RELATIVE_LIBDIR); + +#if defined(_WIN32) + // This will be injected directly through FileSpec.SetDirectory(), + // so we need to normalize manually. + std::replace(path.begin(), path.end(), '\\', '/'); +#endif +} + +FileSpec ScriptInterpreterPython::GetPythonDir() { + static FileSpec g_spec = []() { + FileSpec spec = HostInfo::GetShlibDir(); + if (!spec) + return FileSpec(); + llvm::SmallString<64> path; + spec.GetPath(path); + +#if defined(__APPLE__) + ComputePythonDirForApple(path); +#else + ComputePythonDir(path); +#endif + spec.SetDirectory(path); + return spec; + }(); + return g_spec; +} + +static const char GetInterpreterInfoScript[] = R"( +import os +import sys + +def main(lldb_python_dir, python_exe_relative_path): + info = { + "lldb-pythonpath": lldb_python_dir, + "language": "python", + "prefix": sys.prefix, + "executable": os.path.join(sys.prefix, python_exe_relative_path) + } + return info +)"; + +static const char python_exe_relative_path[] = LLDB_PYTHON_EXE_RELATIVE_PATH; + +StructuredData::DictionarySP ScriptInterpreterPython::GetInterpreterInfo() { + GIL gil; + FileSpec python_dir_spec = GetPythonDir(); + if (!python_dir_spec) + return nullptr; + PythonScript get_info(GetInterpreterInfoScript); + auto info_json = unwrapIgnoringErrors( + As<PythonDictionary>(get_info(PythonString(python_dir_spec.GetPath()), + PythonString(python_exe_relative_path)))); + if (!info_json) + return nullptr; + return info_json.CreateStructuredDictionary(); +} + +void ScriptInterpreterPython::SharedLibraryDirectoryHelper( + FileSpec &this_file) { + // When we're loaded from python, this_file will point to the file inside the + // python package directory. Replace it with the one in the lib directory. +#ifdef _WIN32 + // On windows, we need to manually back out of the python tree, and go into + // the bin directory. This is pretty much the inverse of what ComputePythonDir + // does. + if (this_file.GetFileNameExtension() == ".pyd") { + this_file.RemoveLastPathComponent(); // _lldb.pyd or _lldb_d.pyd + this_file.RemoveLastPathComponent(); // lldb + llvm::StringRef libdir = LLDB_PYTHON_RELATIVE_LIBDIR; + for (auto it = llvm::sys::path::begin(libdir), + end = llvm::sys::path::end(libdir); + it != end; ++it) + this_file.RemoveLastPathComponent(); + this_file.AppendPathComponent("bin"); + this_file.AppendPathComponent("liblldb.dll"); + } +#else + // The python file is a symlink, so we can find the real library by resolving + // it. We can do this unconditionally. + FileSystem::Instance().ResolveSymbolicLink(this_file, this_file); +#endif +} + +llvm::StringRef ScriptInterpreterPython::GetPluginDescriptionStatic() { + return "Embedded Python interpreter"; +} + +void ScriptInterpreterPython::Initialize() { + static llvm::once_flag g_once_flag; + llvm::call_once(g_once_flag, []() { + PluginManager::RegisterPlugin(GetPluginNameStatic(), + GetPluginDescriptionStatic(), + lldb::eScriptLanguagePython, + ScriptInterpreterPythonImpl::CreateInstance); + ScriptInterpreterPythonImpl::Initialize(); + }); +} + +void ScriptInterpreterPython::Terminate() {} + +ScriptInterpreterPythonImpl::Locker::Locker( + ScriptInterpreterPythonImpl *py_interpreter, uint16_t on_entry, + uint16_t on_leave, FileSP in, FileSP out, FileSP err) + : ScriptInterpreterLocker(), + m_teardown_session((on_leave & TearDownSession) == TearDownSession), + m_python_interpreter(py_interpreter) { + DoAcquireLock(); + if ((on_entry & InitSession) == InitSession) { + if (!DoInitSession(on_entry, in, out, err)) { + // Don't teardown the session if we didn't init it. + m_teardown_session = false; + } + } +} + +bool ScriptInterpreterPythonImpl::Locker::DoAcquireLock() { + Log *log = GetLog(LLDBLog::Script); + m_GILState = PyGILState_Ensure(); + LLDB_LOGV(log, "Ensured PyGILState. Previous state = {0}locked", + m_GILState == PyGILState_UNLOCKED ? "un" : ""); + + // we need to save the thread state when we first start the command because + // we might decide to interrupt it while some action is taking place outside + // of Python (e.g. printing to screen, waiting for the network, ...) in that + // case, _PyThreadState_Current will be NULL - and we would be unable to set + // the asynchronous exception - not a desirable situation + m_python_interpreter->SetThreadState(PyThreadState_Get()); + m_python_interpreter->IncrementLockCount(); + return true; +} + +bool ScriptInterpreterPythonImpl::Locker::DoInitSession(uint16_t on_entry_flags, + FileSP in, FileSP out, + FileSP err) { + if (!m_python_interpreter) + return false; + return m_python_interpreter->EnterSession(on_entry_flags, in, out, err); +} + +bool ScriptInterpreterPythonImpl::Locker::DoFreeLock() { + Log *log = GetLog(LLDBLog::Script); + LLDB_LOGV(log, "Releasing PyGILState. Returning to state = {0}locked", + m_GILState == PyGILState_UNLOCKED ? "un" : ""); + PyGILState_Release(m_GILState); + m_python_interpreter->DecrementLockCount(); + return true; +} + +bool ScriptInterpreterPythonImpl::Locker::DoTearDownSession() { + if (!m_python_interpreter) + return false; + m_python_interpreter->LeaveSession(); + return true; +} + +ScriptInterpreterPythonImpl::Locker::~Locker() { + if (m_teardown_session) + DoTearDownSession(); + DoFreeLock(); +} + +ScriptInterpreterPythonImpl::ScriptInterpreterPythonImpl(Debugger &debugger) + : ScriptInterpreterPython(debugger), m_saved_stdin(), m_saved_stdout(), + m_saved_stderr(), m_main_module(), + m_session_dict(PyInitialValue::Invalid), + m_sys_module_dict(PyInitialValue::Invalid), m_run_one_line_function(), + m_run_one_line_str_global(), + m_dictionary_name(m_debugger.GetInstanceName()), + m_active_io_handler(eIOHandlerNone), m_session_is_active(false), + m_pty_secondary_is_open(false), m_valid_session(true), m_lock_count(0), + m_command_thread_state(nullptr) { + + m_dictionary_name.append("_dict"); + StreamString run_string; + run_string.Printf("%s = dict()", m_dictionary_name.c_str()); + + Locker locker(this, Locker::AcquireLock, Locker::FreeAcquiredLock); + PyRun_SimpleString(run_string.GetData()); + + run_string.Clear(); + run_string.Printf( + "run_one_line (%s, 'import copy, keyword, os, re, sys, uuid, lldb')", + m_dictionary_name.c_str()); + PyRun_SimpleString(run_string.GetData()); + + // 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 importlib import reload as reload_module')", + m_dictionary_name.c_str()); + PyRun_SimpleString(run_string.GetData()); + + // WARNING: temporary code that loads Cocoa formatters - this should be done + // on a per-platform basis rather than loading the whole set and letting the + // individual formatter classes exploit APIs to check whether they can/cannot + // do their task + run_string.Clear(); + run_string.Printf( + "run_one_line (%s, 'import lldb.formatters, lldb.formatters.cpp')", + m_dictionary_name.c_str()); + PyRun_SimpleString(run_string.GetData()); + run_string.Clear(); + + run_string.Printf("run_one_line (%s, 'import lldb.embedded_interpreter; from " + "lldb.embedded_interpreter import run_python_interpreter; " + "from lldb.embedded_interpreter import run_one_line')", + m_dictionary_name.c_str()); + PyRun_SimpleString(run_string.GetData()); + run_string.Clear(); + + run_string.Printf("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64 + "')", + m_dictionary_name.c_str(), m_debugger.GetID()); + PyRun_SimpleString(run_string.GetData()); +} + +ScriptInterpreterPythonImpl::~ScriptInterpreterPythonImpl() { + // the session dictionary may hold objects with complex state which means + // that they may need to be torn down with some level of smarts and that, in + // turn, requires a valid thread state force Python to procure itself such a + // thread state, nuke the session dictionary and then release it for others + // to use and proceed with the rest of the shutdown + auto gil_state = PyGILState_Ensure(); + m_session_dict.Reset(); + PyGILState_Release(gil_state); +} + +void ScriptInterpreterPythonImpl::IOHandlerActivated(IOHandler &io_handler, + bool interactive) { + const char *instructions = nullptr; + + switch (m_active_io_handler) { + case eIOHandlerNone: + break; + case eIOHandlerBreakpoint: + instructions = R"(Enter your Python command(s). Type 'DONE' to end. +def function (frame, bp_loc, internal_dict): + """frame: the lldb.SBFrame for the location at which you stopped + bp_loc: an lldb.SBBreakpointLocation for the breakpoint location information + internal_dict: an LLDB support object not to be used""" +)"; + break; + case eIOHandlerWatchpoint: + instructions = "Enter your Python command(s). Type 'DONE' to end.\n"; + break; + } + + if (instructions) { + StreamFileSP output_sp(io_handler.GetOutputStreamFileSP()); + if (output_sp && interactive) { + output_sp->PutCString(instructions); + output_sp->Flush(); + } + } +} + +void ScriptInterpreterPythonImpl::IOHandlerInputComplete(IOHandler &io_handler, + std::string &data) { + io_handler.SetIsDone(true); + bool batch_mode = m_debugger.GetCommandInterpreter().GetBatchCommandMode(); + + switch (m_active_io_handler) { + case eIOHandlerNone: + break; + case eIOHandlerBreakpoint: { + std::vector<std::reference_wrapper<BreakpointOptions>> *bp_options_vec = + (std::vector<std::reference_wrapper<BreakpointOptions>> *) + io_handler.GetUserData(); + for (BreakpointOptions &bp_options : *bp_options_vec) { + + auto data_up = std::make_unique<CommandDataPython>(); + if (!data_up) + break; + data_up->user_source.SplitIntoLines(data); + + if (GenerateBreakpointCommandCallbackData(data_up->user_source, + data_up->script_source, + /*has_extra_args=*/false, + /*is_callback=*/false) + .Success()) { + auto baton_sp = std::make_shared<BreakpointOptions::CommandBaton>( + std::move(data_up)); + bp_options.SetCallback( + ScriptInterpreterPythonImpl::BreakpointCallbackFunction, baton_sp); + } else if (!batch_mode) { + StreamFileSP error_sp = io_handler.GetErrorStreamFileSP(); + if (error_sp) { + error_sp->Printf("Warning: No command attached to breakpoint.\n"); + error_sp->Flush(); + } + } + } + m_active_io_handler = eIOHandlerNone; + } break; + case eIOHandlerWatchpoint: { + WatchpointOptions *wp_options = + (WatchpointOptions *)io_handler.GetUserData(); + auto data_up = std::make_unique<WatchpointOptions::CommandData>(); + data_up->user_source.SplitIntoLines(data); + + if (GenerateWatchpointCommandCallbackData(data_up->user_source, + data_up->script_source, + /*is_callback=*/false)) { + auto baton_sp = + std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up)); + wp_options->SetCallback( + ScriptInterpreterPythonImpl::WatchpointCallbackFunction, baton_sp); + } else if (!batch_mode) { + StreamFileSP error_sp = io_handler.GetErrorStreamFileSP(); + if (error_sp) { + error_sp->Printf("Warning: No command attached to breakpoint.\n"); + error_sp->Flush(); + } + } + m_active_io_handler = eIOHandlerNone; + } break; + } +} + +lldb::ScriptInterpreterSP +ScriptInterpreterPythonImpl::CreateInstance(Debugger &debugger) { + return std::make_shared<ScriptInterpreterPythonImpl>(debugger); +} + +void ScriptInterpreterPythonImpl::LeaveSession() { + Log *log = GetLog(LLDBLog::Script); + if (log) + log->PutCString("ScriptInterpreterPythonImpl::LeaveSession()"); + + // Unset the LLDB global variables. + PyRun_SimpleString("lldb.debugger = None; lldb.target = None; lldb.process " + "= None; lldb.thread = None; lldb.frame = None"); + + // checking that we have a valid thread state - since we use our own + // threading and locking in some (rare) cases during cleanup Python may end + // up believing we have no thread state and PyImport_AddModule will crash if + // that is the case - since that seems to only happen when destroying the + // SBDebugger, we can make do without clearing up stdout and stderr + if (PyThreadState_GetDict()) { + PythonDictionary &sys_module_dict = GetSysModuleDictionary(); + if (sys_module_dict.IsValid()) { + if (m_saved_stdin.IsValid()) { + sys_module_dict.SetItemForKey(PythonString("stdin"), m_saved_stdin); + m_saved_stdin.Reset(); + } + if (m_saved_stdout.IsValid()) { + sys_module_dict.SetItemForKey(PythonString("stdout"), m_saved_stdout); + m_saved_stdout.Reset(); + } + if (m_saved_stderr.IsValid()) { + sys_module_dict.SetItemForKey(PythonString("stderr"), m_saved_stderr); + m_saved_stderr.Reset(); + } + } + } + + m_session_is_active = false; +} + +bool ScriptInterpreterPythonImpl::SetStdHandle(FileSP file_sp, + const char *py_name, + PythonObject &save_file, + const char *mode) { + if (!file_sp || !*file_sp) { + save_file.Reset(); + return false; + } + File &file = *file_sp; + + // Flush the file before giving it to python to avoid interleaved output. + file.Flush(); + + PythonDictionary &sys_module_dict = GetSysModuleDictionary(); + + auto new_file = PythonFile::FromFile(file, mode); + if (!new_file) { + llvm::consumeError(new_file.takeError()); + return false; + } + + save_file = sys_module_dict.GetItemForKey(PythonString(py_name)); + + sys_module_dict.SetItemForKey(PythonString(py_name), new_file.get()); + return true; +} + +bool ScriptInterpreterPythonImpl::EnterSession(uint16_t on_entry_flags, + FileSP in_sp, FileSP out_sp, + FileSP err_sp) { + // If we have already entered the session, without having officially 'left' + // it, then there is no need to 'enter' it again. + Log *log = GetLog(LLDBLog::Script); + if (m_session_is_active) { + LLDB_LOGF( + log, + "ScriptInterpreterPythonImpl::EnterSession(on_entry_flags=0x%" PRIx16 + ") session is already active, returning without doing anything", + on_entry_flags); + return false; + } + + LLDB_LOGF( + log, + "ScriptInterpreterPythonImpl::EnterSession(on_entry_flags=0x%" PRIx16 ")", + on_entry_flags); + + m_session_is_active = true; + + StreamString run_string; + + if (on_entry_flags & Locker::InitGlobals) { + run_string.Printf("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64, + m_dictionary_name.c_str(), m_debugger.GetID()); + run_string.Printf( + "; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")", + m_debugger.GetID()); + run_string.PutCString("; lldb.target = lldb.debugger.GetSelectedTarget()"); + run_string.PutCString("; lldb.process = lldb.target.GetProcess()"); + run_string.PutCString("; lldb.thread = lldb.process.GetSelectedThread ()"); + run_string.PutCString("; lldb.frame = lldb.thread.GetSelectedFrame ()"); + run_string.PutCString("')"); + } else { + // If we aren't initing the globals, we should still always set the + // debugger (since that is always unique.) + run_string.Printf("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64, + m_dictionary_name.c_str(), m_debugger.GetID()); + run_string.Printf( + "; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")", + m_debugger.GetID()); + run_string.PutCString("')"); + } + + PyRun_SimpleString(run_string.GetData()); + run_string.Clear(); + + PythonDictionary &sys_module_dict = GetSysModuleDictionary(); + if (sys_module_dict.IsValid()) { + lldb::FileSP top_in_sp; + lldb::StreamFileSP top_out_sp, top_err_sp; + if (!in_sp || !out_sp || !err_sp || !*in_sp || !*out_sp || !*err_sp) + m_debugger.AdoptTopIOHandlerFilesIfInvalid(top_in_sp, top_out_sp, + top_err_sp); + + if (on_entry_flags & Locker::NoSTDIN) { + m_saved_stdin.Reset(); + } else { + if (!SetStdHandle(in_sp, "stdin", m_saved_stdin, "r")) { + if (top_in_sp) + SetStdHandle(top_in_sp, "stdin", m_saved_stdin, "r"); + } + } + + if (!SetStdHandle(out_sp, "stdout", m_saved_stdout, "w")) { + if (top_out_sp) + SetStdHandle(top_out_sp->GetFileSP(), "stdout", m_saved_stdout, "w"); + } + + if (!SetStdHandle(err_sp, "stderr", m_saved_stderr, "w")) { + if (top_err_sp) + SetStdHandle(top_err_sp->GetFileSP(), "stderr", m_saved_stderr, "w"); + } + } + + if (PyErr_Occurred()) + PyErr_Clear(); + + return true; +} + +PythonModule &ScriptInterpreterPythonImpl::GetMainModule() { + if (!m_main_module.IsValid()) + m_main_module = unwrapIgnoringErrors(PythonModule::Import("__main__")); + return m_main_module; +} + +PythonDictionary &ScriptInterpreterPythonImpl::GetSessionDictionary() { + if (m_session_dict.IsValid()) + return m_session_dict; + + PythonObject &main_module = GetMainModule(); + if (!main_module.IsValid()) + return m_session_dict; + + PythonDictionary main_dict(PyRefType::Borrowed, + PyModule_GetDict(main_module.get())); + if (!main_dict.IsValid()) + return m_session_dict; + + m_session_dict = unwrapIgnoringErrors( + As<PythonDictionary>(main_dict.GetItem(m_dictionary_name))); + return m_session_dict; +} + +PythonDictionary &ScriptInterpreterPythonImpl::GetSysModuleDictionary() { + if (m_sys_module_dict.IsValid()) + return m_sys_module_dict; + PythonModule sys_module = unwrapIgnoringErrors(PythonModule::Import("sys")); + m_sys_module_dict = sys_module.GetDictionary(); + return m_sys_module_dict; +} + +llvm::Expected<unsigned> +ScriptInterpreterPythonImpl::GetMaxPositionalArgumentsForCallable( + const llvm::StringRef &callable_name) { + if (callable_name.empty()) { + return llvm::createStringError( + llvm::inconvertibleErrorCode(), + "called with empty callable name."); + } + Locker py_lock(this, Locker::AcquireLock | + Locker::InitSession | + Locker::NoSTDIN); + auto dict = PythonModule::MainModule() + .ResolveName<PythonDictionary>(m_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( + callable_name, dict); + if (!pfunc.IsAllocated()) { + return llvm::createStringError( + llvm::inconvertibleErrorCode(), + "can't find callable: %s", callable_name.str().c_str()); + } + llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo(); + if (!arg_info) + return arg_info.takeError(); + return arg_info.get().max_positional_args; +} + +static std::string GenerateUniqueName(const char *base_name_wanted, + uint32_t &functions_counter, + const void *name_token = nullptr) { + StreamString sstr; + + if (!base_name_wanted) + return std::string(); + + if (!name_token) + sstr.Printf("%s_%d", base_name_wanted, functions_counter++); + else + sstr.Printf("%s_%p", base_name_wanted, name_token); + + return std::string(sstr.GetString()); +} + +bool ScriptInterpreterPythonImpl::GetEmbeddedInterpreterModuleObjects() { + if (m_run_one_line_function.IsValid()) + return true; + + PythonObject module(PyRefType::Borrowed, + PyImport_AddModule("lldb.embedded_interpreter")); + if (!module.IsValid()) + return false; + + PythonDictionary module_dict(PyRefType::Borrowed, + PyModule_GetDict(module.get())); + if (!module_dict.IsValid()) + return false; + + m_run_one_line_function = + module_dict.GetItemForKey(PythonString("run_one_line")); + m_run_one_line_str_global = + module_dict.GetItemForKey(PythonString("g_run_one_line_str")); + return m_run_one_line_function.IsValid(); +} + +bool ScriptInterpreterPythonImpl::ExecuteOneLine( + llvm::StringRef command, CommandReturnObject *result, + const ExecuteScriptOptions &options) { + std::string command_str = command.str(); + + if (!m_valid_session) + return false; + + if (!command.empty()) { + // We want to call run_one_line, passing in the dictionary and the command + // string. We cannot do this through PyRun_SimpleString here because the + // command string may contain escaped characters, and putting it inside + // another string to pass to PyRun_SimpleString messes up the escaping. So + // we use the following more complicated method to pass the command string + // directly down to Python. + llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>> + io_redirect_or_error = ScriptInterpreterIORedirect::Create( + options.GetEnableIO(), m_debugger, result); + if (!io_redirect_or_error) { + if (result) + result->AppendErrorWithFormatv( + "failed to redirect I/O: {0}\n", + llvm::fmt_consume(io_redirect_or_error.takeError())); + else + llvm::consumeError(io_redirect_or_error.takeError()); + return false; + } + + ScriptInterpreterIORedirect &io_redirect = **io_redirect_or_error; + + bool success = false; + { + // WARNING! It's imperative that this RAII scope be as tight as + // possible. In particular, the scope must end *before* we try to join + // the read thread. The reason for this is that a pre-requisite for + // joining the read thread is that we close the write handle (to break + // the pipe and cause it to wake up and exit). But acquiring the GIL as + // below will redirect Python's stdio to use this same handle. If we + // close the handle while Python is still using it, bad things will + // happen. + Locker locker( + this, + Locker::AcquireLock | Locker::InitSession | + (options.GetSetLLDBGlobals() ? Locker::InitGlobals : 0) | + ((result && result->GetInteractive()) ? 0 : Locker::NoSTDIN), + Locker::FreeAcquiredLock | Locker::TearDownSession, + io_redirect.GetInputFile(), io_redirect.GetOutputFile(), + io_redirect.GetErrorFile()); + + // Find the correct script interpreter dictionary in the main module. + PythonDictionary &session_dict = GetSessionDictionary(); + if (session_dict.IsValid()) { + if (GetEmbeddedInterpreterModuleObjects()) { + if (PyCallable_Check(m_run_one_line_function.get())) { + PythonObject pargs( + PyRefType::Owned, + Py_BuildValue("(Os)", session_dict.get(), command_str.c_str())); + if (pargs.IsValid()) { + PythonObject return_value( + PyRefType::Owned, + PyObject_CallObject(m_run_one_line_function.get(), + pargs.get())); + if (return_value.IsValid()) + success = true; + else if (options.GetMaskoutErrors() && PyErr_Occurred()) { + PyErr_Print(); + PyErr_Clear(); + } + } + } + } + } + + io_redirect.Flush(); + } + + if (success) + return true; + + // The one-liner failed. Append the error message. + if (result) { + result->AppendErrorWithFormat( + "python failed attempting to evaluate '%s'\n", command_str.c_str()); + } + return false; + } + + if (result) + result->AppendError("empty command passed to python\n"); + return false; +} + +void ScriptInterpreterPythonImpl::ExecuteInterpreterLoop() { + LLDB_SCOPED_TIMER(); + + Debugger &debugger = m_debugger; + + // At the moment, the only time the debugger does not have an input file + // handle is when this is called directly from Python, in which case it is + // both dangerous and unnecessary (not to mention confusing) to try to embed + // a running interpreter loop inside the already running Python interpreter + // loop, so we won't do it. + + if (!debugger.GetInputFile().IsValid()) + return; + + IOHandlerSP io_handler_sp(new IOHandlerPythonInterpreter(debugger, this)); + if (io_handler_sp) { + debugger.RunIOHandlerAsync(io_handler_sp); + } +} + +bool ScriptInterpreterPythonImpl::Interrupt() { +#if LLDB_USE_PYTHON_SET_INTERRUPT + // If the interpreter isn't evaluating any Python at the moment then return + // false to signal that this function didn't handle the interrupt and the + // next component should try handling it. + if (!IsExecutingPython()) + return false; + + // Tell Python that it should pretend to have received a SIGINT. + PyErr_SetInterrupt(); + // PyErr_SetInterrupt has no way to return an error so we can only pretend the + // signal got successfully handled and return true. + // Python 3.10 introduces PyErr_SetInterruptEx that could return an error, but + // the error handling is limited to checking the arguments which would be + // just our (hardcoded) input signal code SIGINT, so that's not useful at all. + return true; +#else + Log *log = GetLog(LLDBLog::Script); + + if (IsExecutingPython()) { + PyThreadState *state = PyThreadState_GET(); + if (!state) + state = GetThreadState(); + if (state) { + long tid = state->thread_id; + PyThreadState_Swap(state); + int num_threads = PyThreadState_SetAsyncExc(tid, PyExc_KeyboardInterrupt); + LLDB_LOGF(log, + "ScriptInterpreterPythonImpl::Interrupt() sending " + "PyExc_KeyboardInterrupt (tid = %li, num_threads = %i)...", + tid, num_threads); + return true; + } + } + LLDB_LOGF(log, + "ScriptInterpreterPythonImpl::Interrupt() python code not running, " + "can't interrupt"); + return false; +#endif +} + +bool ScriptInterpreterPythonImpl::ExecuteOneLineWithReturn( + llvm::StringRef in_string, ScriptInterpreter::ScriptReturnType return_type, + void *ret_value, const ExecuteScriptOptions &options) { + + llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>> + io_redirect_or_error = ScriptInterpreterIORedirect::Create( + options.GetEnableIO(), m_debugger, /*result=*/nullptr); + + if (!io_redirect_or_error) { + llvm::consumeError(io_redirect_or_error.takeError()); + return false; + } + + ScriptInterpreterIORedirect &io_redirect = **io_redirect_or_error; + + Locker locker(this, + Locker::AcquireLock | Locker::InitSession | + (options.GetSetLLDBGlobals() ? Locker::InitGlobals : 0) | + Locker::NoSTDIN, + Locker::FreeAcquiredLock | Locker::TearDownSession, + io_redirect.GetInputFile(), io_redirect.GetOutputFile(), + io_redirect.GetErrorFile()); + + PythonModule &main_module = GetMainModule(); + PythonDictionary globals = main_module.GetDictionary(); + + PythonDictionary locals = GetSessionDictionary(); + if (!locals.IsValid()) + locals = unwrapIgnoringErrors( + As<PythonDictionary>(globals.GetAttribute(m_dictionary_name))); + if (!locals.IsValid()) + locals = globals; + + Expected<PythonObject> maybe_py_return = + runStringOneLine(in_string, globals, locals); + + if (!maybe_py_return) { + llvm::handleAllErrors( + maybe_py_return.takeError(), + [&](PythonException &E) { + E.Restore(); + if (options.GetMaskoutErrors()) { + if (E.Matches(PyExc_SyntaxError)) { + PyErr_Print(); + } + PyErr_Clear(); + } + }, + [](const llvm::ErrorInfoBase &E) {}); + return false; + } + + PythonObject py_return = std::move(maybe_py_return.get()); + assert(py_return.IsValid()); + + switch (return_type) { + case eScriptReturnTypeCharPtr: // "char *" + { + const char format[3] = "s#"; + return PyArg_Parse(py_return.get(), format, (char **)ret_value); + } + case eScriptReturnTypeCharStrOrNone: // char* or NULL if py_return == + // Py_None + { + const char format[3] = "z"; + return PyArg_Parse(py_return.get(), format, (char **)ret_value); + } + case eScriptReturnTypeBool: { + const char format[2] = "b"; + return PyArg_Parse(py_return.get(), format, (bool *)ret_value); + } + case eScriptReturnTypeShortInt: { + const char format[2] = "h"; + return PyArg_Parse(py_return.get(), format, (short *)ret_value); + } + case eScriptReturnTypeShortIntUnsigned: { + const char format[2] = "H"; + return PyArg_Parse(py_return.get(), format, (unsigned short *)ret_value); + } + case eScriptReturnTypeInt: { + const char format[2] = "i"; + return PyArg_Parse(py_return.get(), format, (int *)ret_value); + } + case eScriptReturnTypeIntUnsigned: { + const char format[2] = "I"; + return PyArg_Parse(py_return.get(), format, (unsigned int *)ret_value); + } + case eScriptReturnTypeLongInt: { + const char format[2] = "l"; + return PyArg_Parse(py_return.get(), format, (long *)ret_value); + } + case eScriptReturnTypeLongIntUnsigned: { + const char format[2] = "k"; + return PyArg_Parse(py_return.get(), format, (unsigned long *)ret_value); + } + case eScriptReturnTypeLongLong: { + const char format[2] = "L"; + return PyArg_Parse(py_return.get(), format, (long long *)ret_value); + } + case eScriptReturnTypeLongLongUnsigned: { + const char format[2] = "K"; + return PyArg_Parse(py_return.get(), format, + (unsigned long long *)ret_value); + } + case eScriptReturnTypeFloat: { + const char format[2] = "f"; + return PyArg_Parse(py_return.get(), format, (float *)ret_value); + } + case eScriptReturnTypeDouble: { + const char format[2] = "d"; + return PyArg_Parse(py_return.get(), format, (double *)ret_value); + } + case eScriptReturnTypeChar: { + const char format[2] = "c"; + return PyArg_Parse(py_return.get(), format, (char *)ret_value); + } + case eScriptReturnTypeOpaqueObject: { + *((PyObject **)ret_value) = py_return.release(); + return true; + } + } + llvm_unreachable("Fully covered switch!"); +} + +Status ScriptInterpreterPythonImpl::ExecuteMultipleLines( + const char *in_string, const ExecuteScriptOptions &options) { + + if (in_string == nullptr) + return Status(); + + llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>> + io_redirect_or_error = ScriptInterpreterIORedirect::Create( + options.GetEnableIO(), m_debugger, /*result=*/nullptr); + + if (!io_redirect_or_error) + return Status(io_redirect_or_error.takeError()); + + ScriptInterpreterIORedirect &io_redirect = **io_redirect_or_error; + + Locker locker(this, + Locker::AcquireLock | Locker::InitSession | + (options.GetSetLLDBGlobals() ? Locker::InitGlobals : 0) | + Locker::NoSTDIN, + Locker::FreeAcquiredLock | Locker::TearDownSession, + io_redirect.GetInputFile(), io_redirect.GetOutputFile(), + io_redirect.GetErrorFile()); + + PythonModule &main_module = GetMainModule(); + PythonDictionary globals = main_module.GetDictionary(); + + PythonDictionary locals = GetSessionDictionary(); + if (!locals.IsValid()) + locals = unwrapIgnoringErrors( + As<PythonDictionary>(globals.GetAttribute(m_dictionary_name))); + if (!locals.IsValid()) + locals = globals; + + Expected<PythonObject> return_value = + runStringMultiLine(in_string, globals, locals); + + if (!return_value) { + llvm::Error error = + llvm::handleErrors(return_value.takeError(), [&](PythonException &E) { + llvm::Error error = llvm::createStringError( + llvm::inconvertibleErrorCode(), E.ReadBacktrace()); + if (!options.GetMaskoutErrors()) + E.Restore(); + return error; + }); + return Status(std::move(error)); + } + + return Status(); +} + +void ScriptInterpreterPythonImpl::CollectDataForBreakpointCommandCallback( + std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec, + CommandReturnObject &result) { + m_active_io_handler = eIOHandlerBreakpoint; + m_debugger.GetCommandInterpreter().GetPythonCommandsFromIOHandler( + " ", *this, &bp_options_vec); +} + +void ScriptInterpreterPythonImpl::CollectDataForWatchpointCommandCallback( + WatchpointOptions *wp_options, CommandReturnObject &result) { + m_active_io_handler = eIOHandlerWatchpoint; + m_debugger.GetCommandInterpreter().GetPythonCommandsFromIOHandler( + " ", *this, wp_options); +} + +Status ScriptInterpreterPythonImpl::SetBreakpointCommandCallbackFunction( + BreakpointOptions &bp_options, const char *function_name, + StructuredData::ObjectSP extra_args_sp) { + Status error; + // For now just cons up a oneliner that calls the provided function. + std::string function_signature = function_name; + + llvm::Expected<unsigned> maybe_args = + GetMaxPositionalArgumentsForCallable(function_name); + if (!maybe_args) { + error.SetErrorStringWithFormat( + "could not get num args: %s", + llvm::toString(maybe_args.takeError()).c_str()); + return error; + } + size_t max_args = *maybe_args; + + bool uses_extra_args = false; + if (max_args >= 4) { + uses_extra_args = true; + function_signature += "(frame, bp_loc, extra_args, internal_dict)"; + } else if (max_args >= 3) { + if (extra_args_sp) { + error.SetErrorString("cannot pass extra_args to a three argument callback" + ); + return error; + } + uses_extra_args = false; + function_signature += "(frame, bp_loc, internal_dict)"; + } else { + error.SetErrorStringWithFormat("expected 3 or 4 argument " + "function, %s can only take %zu", + function_name, max_args); + return error; + } + + SetBreakpointCommandCallback(bp_options, function_signature.c_str(), + extra_args_sp, uses_extra_args, + /*is_callback=*/true); + return error; +} + +Status ScriptInterpreterPythonImpl::SetBreakpointCommandCallback( + BreakpointOptions &bp_options, + std::unique_ptr<BreakpointOptions::CommandData> &cmd_data_up) { + Status error; + error = GenerateBreakpointCommandCallbackData(cmd_data_up->user_source, + cmd_data_up->script_source, + /*has_extra_args=*/false, + /*is_callback=*/false); + if (error.Fail()) { + return error; + } + auto baton_sp = + std::make_shared<BreakpointOptions::CommandBaton>(std::move(cmd_data_up)); + bp_options.SetCallback( + ScriptInterpreterPythonImpl::BreakpointCallbackFunction, baton_sp); + return error; +} + +Status ScriptInterpreterPythonImpl::SetBreakpointCommandCallback( + BreakpointOptions &bp_options, const char *command_body_text, + bool is_callback) { + return SetBreakpointCommandCallback(bp_options, command_body_text, {}, + /*uses_extra_args=*/false, is_callback); +} + +// Set a Python one-liner as the callback for the breakpoint. +Status ScriptInterpreterPythonImpl::SetBreakpointCommandCallback( + BreakpointOptions &bp_options, const char *command_body_text, + StructuredData::ObjectSP extra_args_sp, bool uses_extra_args, + bool is_callback) { + auto data_up = std::make_unique<CommandDataPython>(extra_args_sp); + // Split the command_body_text into lines, and pass that to + // GenerateBreakpointCommandCallbackData. That will wrap the body in an + // auto-generated function, and return the function name in script_source. + // That is what the callback will actually invoke. + + data_up->user_source.SplitIntoLines(command_body_text); + Status error = GenerateBreakpointCommandCallbackData( + data_up->user_source, data_up->script_source, uses_extra_args, + is_callback); + if (error.Success()) { + auto baton_sp = + std::make_shared<BreakpointOptions::CommandBaton>(std::move(data_up)); + bp_options.SetCallback( + ScriptInterpreterPythonImpl::BreakpointCallbackFunction, baton_sp); + return error; + } + return error; +} + +// Set a Python one-liner as the callback for the watchpoint. +void ScriptInterpreterPythonImpl::SetWatchpointCommandCallback( + WatchpointOptions *wp_options, const char *user_input, + bool is_callback) { + auto data_up = std::make_unique<WatchpointOptions::CommandData>(); + + // It's necessary to set both user_source and script_source to the oneliner. + // The former is used to generate callback description (as in watchpoint + // command list) while the latter is used for Python to interpret during the + // actual callback. + + data_up->user_source.AppendString(user_input); + data_up->script_source.assign(user_input); + + if (GenerateWatchpointCommandCallbackData( + data_up->user_source, data_up->script_source, is_callback)) { + auto baton_sp = + std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up)); + wp_options->SetCallback( + ScriptInterpreterPythonImpl::WatchpointCallbackFunction, baton_sp); + } +} + +Status ScriptInterpreterPythonImpl::ExportFunctionDefinitionToInterpreter( + StringList &function_def) { + // Convert StringList to one long, newline delimited, const char *. + std::string function_def_string(function_def.CopyList()); + + Status error = ExecuteMultipleLines( + function_def_string.c_str(), + ExecuteScriptOptions().SetEnableIO(false)); + return error; +} + +Status ScriptInterpreterPythonImpl::GenerateFunction(const char *signature, + const StringList &input, + bool is_callback) { + Status error; + int num_lines = input.GetSize(); + if (num_lines == 0) { + error.SetErrorString("No input data."); + return error; + } + + if (!signature || *signature == 0) { + error.SetErrorString("No output function name."); + return error; + } + + StreamString sstr; + StringList auto_generated_function; + auto_generated_function.AppendString(signature); + auto_generated_function.AppendString( + " global_dict = globals()"); // Grab the global dictionary + auto_generated_function.AppendString( + " new_keys = internal_dict.keys()"); // Make a list of keys in the + // session dict + auto_generated_function.AppendString( + " old_keys = global_dict.keys()"); // Save list of keys in global dict + auto_generated_function.AppendString( + " global_dict.update(internal_dict)"); // Add the session dictionary + // to the global dictionary. + + if (is_callback) { + // If the user input is a callback to a python function, make sure the input + // is only 1 line, otherwise appending the user input would break the + // generated wrapped function + if (num_lines == 1) { + sstr.Clear(); + sstr.Printf(" __return_val = %s", input.GetStringAtIndex(0)); + auto_generated_function.AppendString(sstr.GetData()); + } else { + return Status("ScriptInterpreterPythonImpl::GenerateFunction(is_callback=" + "true) = ERROR: python function is multiline."); + } + } else { + auto_generated_function.AppendString( + " __return_val = None"); // Initialize user callback return value. + auto_generated_function.AppendString( + " def __user_code():"); // Create a nested function that will wrap + // the user input. This is necessary to + // capture the return value of the user input + // and prevent early returns. + for (int i = 0; i < num_lines; ++i) { + sstr.Clear(); + sstr.Printf(" %s", input.GetStringAtIndex(i)); + auto_generated_function.AppendString(sstr.GetData()); + } + auto_generated_function.AppendString( + " __return_val = __user_code()"); // Call user code and capture + // return value + } + auto_generated_function.AppendString( + " for key in new_keys:"); // Iterate over all the keys from session + // dict + auto_generated_function.AppendString( + " internal_dict[key] = global_dict[key]"); // Update session dict + // values + auto_generated_function.AppendString( + " if key not in old_keys:"); // If key was not originally in + // global dict + auto_generated_function.AppendString( + " del global_dict[key]"); // ...then remove key/value from + // global dict + auto_generated_function.AppendString( + " return __return_val"); // Return the user callback return value. + + // Verify that the results are valid Python. + error = ExportFunctionDefinitionToInterpreter(auto_generated_function); + + return error; +} + +bool ScriptInterpreterPythonImpl::GenerateTypeScriptFunction( + StringList &user_input, std::string &output, const void *name_token) { + static uint32_t num_created_functions = 0; + user_input.RemoveBlankLines(); + StreamString sstr; + + // Check to see if we have any data; if not, just return. + if (user_input.GetSize() == 0) + return false; + + // Take what the user wrote, wrap it all up inside one big auto-generated + // Python function, passing in the ValueObject as parameter to the function. + + std::string auto_generated_function_name( + GenerateUniqueName("lldb_autogen_python_type_print_func", + num_created_functions, name_token)); + sstr.Printf("def %s (valobj, internal_dict):", + auto_generated_function_name.c_str()); + + if (!GenerateFunction(sstr.GetData(), user_input, /*is_callback=*/false) + .Success()) + return false; + + // Store the name of the auto-generated function to be called. + output.assign(auto_generated_function_name); + return true; +} + +bool ScriptInterpreterPythonImpl::GenerateScriptAliasFunction( + StringList &user_input, std::string &output) { + static uint32_t num_created_functions = 0; + user_input.RemoveBlankLines(); + StreamString sstr; + + // Check to see if we have any data; if not, just return. + if (user_input.GetSize() == 0) + return false; + + std::string auto_generated_function_name(GenerateUniqueName( + "lldb_autogen_python_cmd_alias_func", num_created_functions)); + + sstr.Printf("def %s (debugger, args, exe_ctx, result, internal_dict):", + auto_generated_function_name.c_str()); + + if (!GenerateFunction(sstr.GetData(), user_input, /*is_callback=*/false) + .Success()) + return false; + + // Store the name of the auto-generated function to be called. + output.assign(auto_generated_function_name); + return true; +} + +bool ScriptInterpreterPythonImpl::GenerateTypeSynthClass( + StringList &user_input, std::string &output, const void *name_token) { + static uint32_t num_created_classes = 0; + user_input.RemoveBlankLines(); + int num_lines = user_input.GetSize(); + StreamString sstr; + + // Check to see if we have any data; if not, just return. + if (user_input.GetSize() == 0) + return false; + + // Wrap all user input into a Python class + + std::string auto_generated_class_name(GenerateUniqueName( + "lldb_autogen_python_type_synth_class", num_created_classes, name_token)); + + StringList auto_generated_class; + + // Create the function name & definition string. + + sstr.Printf("class %s:", auto_generated_class_name.c_str()); + auto_generated_class.AppendString(sstr.GetString()); + + // Wrap everything up inside the class, increasing the indentation. we don't + // need to play any fancy indentation tricks here because there is no + // surrounding code whose indentation we need to honor + for (int i = 0; i < num_lines; ++i) { + sstr.Clear(); + sstr.Printf(" %s", user_input.GetStringAtIndex(i)); + auto_generated_class.AppendString(sstr.GetString()); + } + + // Verify that the results are valid Python. (even though the method is + // ExportFunctionDefinitionToInterpreter, a class will actually be exported) + // (TODO: rename that method to ExportDefinitionToInterpreter) + if (!ExportFunctionDefinitionToInterpreter(auto_generated_class).Success()) + return false; + + // Store the name of the auto-generated class + + output.assign(auto_generated_class_name); + return true; +} + +StructuredData::GenericSP +ScriptInterpreterPythonImpl::CreateFrameRecognizer(const char *class_name) { + if (class_name == nullptr || class_name[0] == '\0') + return StructuredData::GenericSP(); + + Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); + PythonObject ret_val = SWIGBridge::LLDBSWIGPython_CreateFrameRecognizer( + class_name, m_dictionary_name.c_str()); + + return StructuredData::GenericSP( + new StructuredPythonObject(std::move(ret_val))); +} + +lldb::ValueObjectListSP ScriptInterpreterPythonImpl::GetRecognizedArguments( + const StructuredData::ObjectSP &os_plugin_object_sp, + lldb::StackFrameSP frame_sp) { + Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); + + if (!os_plugin_object_sp) + return ValueObjectListSP(); + + StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric(); + if (!generic) + return nullptr; + + PythonObject implementor(PyRefType::Borrowed, + (PyObject *)generic->GetValue()); + + if (!implementor.IsAllocated()) + return ValueObjectListSP(); + + PythonObject py_return(PyRefType::Owned, + SWIGBridge::LLDBSwigPython_GetRecognizedArguments( + implementor.get(), frame_sp)); + + // if it fails, print the error but otherwise go on + if (PyErr_Occurred()) { + PyErr_Print(); + PyErr_Clear(); + } + if (py_return.get()) { + PythonList result_list(PyRefType::Borrowed, py_return.get()); + ValueObjectListSP result = ValueObjectListSP(new ValueObjectList()); + for (size_t i = 0; i < result_list.GetSize(); i++) { + PyObject *item = result_list.GetItemAtIndex(i).get(); + lldb::SBValue *sb_value_ptr = + (lldb::SBValue *)LLDBSWIGPython_CastPyObjectToSBValue(item); + auto valobj_sp = + SWIGBridge::LLDBSWIGPython_GetValueObjectSPFromSBValue(sb_value_ptr); + if (valobj_sp) + result->Append(valobj_sp); + } + return result; + } + return ValueObjectListSP(); +} + +ScriptedProcessInterfaceUP +ScriptInterpreterPythonImpl::CreateScriptedProcessInterface() { + return std::make_unique<ScriptedProcessPythonInterface>(*this); +} + +ScriptedThreadInterfaceSP +ScriptInterpreterPythonImpl::CreateScriptedThreadInterface() { + return std::make_shared<ScriptedThreadPythonInterface>(*this); +} + +ScriptedThreadPlanInterfaceSP +ScriptInterpreterPythonImpl::CreateScriptedThreadPlanInterface() { + return std::make_shared<ScriptedThreadPlanPythonInterface>(*this); +} + +OperatingSystemInterfaceSP +ScriptInterpreterPythonImpl::CreateOperatingSystemInterface() { + return std::make_shared<OperatingSystemPythonInterface>(*this); +} + +StructuredData::ObjectSP +ScriptInterpreterPythonImpl::CreateStructuredDataFromScriptObject( + ScriptObject obj) { + void *ptr = const_cast<void *>(obj.GetPointer()); + PythonObject py_obj(PyRefType::Borrowed, static_cast<PyObject *>(ptr)); + if (!py_obj.IsValid() || py_obj.IsNone()) + return {}; + Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); + return py_obj.CreateStructuredObject(); +} + +StructuredData::GenericSP +ScriptInterpreterPythonImpl::CreateScriptedBreakpointResolver( + const char *class_name, const StructuredDataImpl &args_data, + lldb::BreakpointSP &bkpt_sp) { + + if (class_name == nullptr || class_name[0] == '\0') + return StructuredData::GenericSP(); + + if (!bkpt_sp.get()) + return StructuredData::GenericSP(); + + Debugger &debugger = bkpt_sp->GetTarget().GetDebugger(); + ScriptInterpreterPythonImpl *python_interpreter = + GetPythonInterpreter(debugger); + + if (!python_interpreter) + return StructuredData::GenericSP(); + + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + + PythonObject ret_val = + SWIGBridge::LLDBSwigPythonCreateScriptedBreakpointResolver( + class_name, python_interpreter->m_dictionary_name.c_str(), args_data, + bkpt_sp); + + return StructuredData::GenericSP( + new StructuredPythonObject(std::move(ret_val))); +} + +bool ScriptInterpreterPythonImpl::ScriptedBreakpointResolverSearchCallback( + StructuredData::GenericSP implementor_sp, SymbolContext *sym_ctx) { + bool should_continue = false; + + if (implementor_sp) { + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + should_continue = SWIGBridge::LLDBSwigPythonCallBreakpointResolver( + implementor_sp->GetValue(), "__callback__", sym_ctx); + if (PyErr_Occurred()) { + PyErr_Print(); + PyErr_Clear(); + } + } + return should_continue; +} + +lldb::SearchDepth +ScriptInterpreterPythonImpl::ScriptedBreakpointResolverSearchDepth( + StructuredData::GenericSP implementor_sp) { + int depth_as_int = lldb::eSearchDepthModule; + if (implementor_sp) { + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + depth_as_int = SWIGBridge::LLDBSwigPythonCallBreakpointResolver( + implementor_sp->GetValue(), "__get_depth__", nullptr); + if (PyErr_Occurred()) { + PyErr_Print(); + PyErr_Clear(); + } + } + if (depth_as_int == lldb::eSearchDepthInvalid) + return lldb::eSearchDepthModule; + + if (depth_as_int <= lldb::kLastSearchDepthKind) + return (lldb::SearchDepth)depth_as_int; + return lldb::eSearchDepthModule; +} + +StructuredData::GenericSP ScriptInterpreterPythonImpl::CreateScriptedStopHook( + TargetSP target_sp, const char *class_name, + const StructuredDataImpl &args_data, Status &error) { + + if (!target_sp) { + error.SetErrorString("No target for scripted stop-hook."); + return StructuredData::GenericSP(); + } + + if (class_name == nullptr || class_name[0] == '\0') { + error.SetErrorString("No class name for scripted stop-hook."); + return StructuredData::GenericSP(); + } + + ScriptInterpreterPythonImpl *python_interpreter = + GetPythonInterpreter(m_debugger); + + if (!python_interpreter) { + error.SetErrorString("No script interpreter for scripted stop-hook."); + return StructuredData::GenericSP(); + } + + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + + PythonObject ret_val = SWIGBridge::LLDBSwigPythonCreateScriptedStopHook( + target_sp, class_name, python_interpreter->m_dictionary_name.c_str(), + args_data, error); + + return StructuredData::GenericSP( + new StructuredPythonObject(std::move(ret_val))); +} + +bool ScriptInterpreterPythonImpl::ScriptedStopHookHandleStop( + StructuredData::GenericSP implementor_sp, ExecutionContext &exc_ctx, + lldb::StreamSP stream_sp) { + assert(implementor_sp && + "can't call a stop hook with an invalid implementor"); + assert(stream_sp && "can't call a stop hook with an invalid stream"); + + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + + lldb::ExecutionContextRefSP exc_ctx_ref_sp(new ExecutionContextRef(exc_ctx)); + + bool ret_val = SWIGBridge::LLDBSwigPythonStopHookCallHandleStop( + implementor_sp->GetValue(), exc_ctx_ref_sp, stream_sp); + return ret_val; +} + +StructuredData::ObjectSP +ScriptInterpreterPythonImpl::LoadPluginModule(const FileSpec &file_spec, + lldb_private::Status &error) { + if (!FileSystem::Instance().Exists(file_spec)) { + error.SetErrorString("no such file"); + return StructuredData::ObjectSP(); + } + + StructuredData::ObjectSP module_sp; + + LoadScriptOptions load_script_options = + LoadScriptOptions().SetInitSession(true).SetSilent(false); + if (LoadScriptingModule(file_spec.GetPath().c_str(), load_script_options, + error, &module_sp)) + return module_sp; + + return StructuredData::ObjectSP(); +} + +StructuredData::DictionarySP ScriptInterpreterPythonImpl::GetDynamicSettings( + StructuredData::ObjectSP plugin_module_sp, Target *target, + const char *setting_name, lldb_private::Status &error) { + if (!plugin_module_sp || !target || !setting_name || !setting_name[0]) + return StructuredData::DictionarySP(); + StructuredData::Generic *generic = plugin_module_sp->GetAsGeneric(); + if (!generic) + return StructuredData::DictionarySP(); + + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + TargetSP target_sp(target->shared_from_this()); + + auto setting = (PyObject *)SWIGBridge::LLDBSWIGPython_GetDynamicSetting( + generic->GetValue(), setting_name, target_sp); + + if (!setting) + return StructuredData::DictionarySP(); + + PythonDictionary py_dict = + unwrapIgnoringErrors(As<PythonDictionary>(Take<PythonObject>(setting))); + + if (!py_dict) + return StructuredData::DictionarySP(); + + return py_dict.CreateStructuredDictionary(); +} + +StructuredData::ObjectSP +ScriptInterpreterPythonImpl::CreateSyntheticScriptedProvider( + const char *class_name, lldb::ValueObjectSP valobj) { + if (class_name == nullptr || class_name[0] == '\0') + return StructuredData::ObjectSP(); + + if (!valobj.get()) + return StructuredData::ObjectSP(); + + ExecutionContext exe_ctx(valobj->GetExecutionContextRef()); + Target *target = exe_ctx.GetTargetPtr(); + + if (!target) + return StructuredData::ObjectSP(); + + Debugger &debugger = target->GetDebugger(); + ScriptInterpreterPythonImpl *python_interpreter = + GetPythonInterpreter(debugger); + + if (!python_interpreter) + return StructuredData::ObjectSP(); + + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + PythonObject ret_val = SWIGBridge::LLDBSwigPythonCreateSyntheticProvider( + class_name, python_interpreter->m_dictionary_name.c_str(), valobj); + + return StructuredData::ObjectSP( + new StructuredPythonObject(std::move(ret_val))); +} + +StructuredData::GenericSP +ScriptInterpreterPythonImpl::CreateScriptCommandObject(const char *class_name) { + DebuggerSP debugger_sp(m_debugger.shared_from_this()); + + if (class_name == nullptr || class_name[0] == '\0') + return StructuredData::GenericSP(); + + if (!debugger_sp.get()) + return StructuredData::GenericSP(); + + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + PythonObject ret_val = SWIGBridge::LLDBSwigPythonCreateCommandObject( + class_name, m_dictionary_name.c_str(), debugger_sp); + + if (ret_val.IsValid()) + return StructuredData::GenericSP( + new StructuredPythonObject(std::move(ret_val))); + else + return {}; +} + +bool ScriptInterpreterPythonImpl::GenerateTypeScriptFunction( + const char *oneliner, std::string &output, const void *name_token) { + StringList input; + input.SplitIntoLines(oneliner, strlen(oneliner)); + return GenerateTypeScriptFunction(input, output, name_token); +} + +bool ScriptInterpreterPythonImpl::GenerateTypeSynthClass( + const char *oneliner, std::string &output, const void *name_token) { + StringList input; + input.SplitIntoLines(oneliner, strlen(oneliner)); + return GenerateTypeSynthClass(input, output, name_token); +} + +Status ScriptInterpreterPythonImpl::GenerateBreakpointCommandCallbackData( + StringList &user_input, std::string &output, bool has_extra_args, + bool is_callback) { + static uint32_t num_created_functions = 0; + user_input.RemoveBlankLines(); + StreamString sstr; + Status error; + if (user_input.GetSize() == 0) { + error.SetErrorString("No input data."); + return error; + } + + std::string auto_generated_function_name(GenerateUniqueName( + "lldb_autogen_python_bp_callback_func_", num_created_functions)); + if (has_extra_args) + sstr.Printf("def %s (frame, bp_loc, extra_args, internal_dict):", + auto_generated_function_name.c_str()); + else + sstr.Printf("def %s (frame, bp_loc, internal_dict):", + auto_generated_function_name.c_str()); + + error = GenerateFunction(sstr.GetData(), user_input, is_callback); + if (!error.Success()) + return error; + + // Store the name of the auto-generated function to be called. + output.assign(auto_generated_function_name); + return error; +} + +bool ScriptInterpreterPythonImpl::GenerateWatchpointCommandCallbackData( + StringList &user_input, std::string &output, bool is_callback) { + static uint32_t num_created_functions = 0; + user_input.RemoveBlankLines(); + StreamString sstr; + + if (user_input.GetSize() == 0) + return false; + + std::string auto_generated_function_name(GenerateUniqueName( + "lldb_autogen_python_wp_callback_func_", num_created_functions)); + sstr.Printf("def %s (frame, wp, internal_dict):", + auto_generated_function_name.c_str()); + + if (!GenerateFunction(sstr.GetData(), user_input, is_callback).Success()) + return false; + + // Store the name of the auto-generated function to be called. + output.assign(auto_generated_function_name); + return true; +} + +bool ScriptInterpreterPythonImpl::GetScriptedSummary( + const char *python_function_name, lldb::ValueObjectSP valobj, + StructuredData::ObjectSP &callee_wrapper_sp, + const TypeSummaryOptions &options, std::string &retval) { + + LLDB_SCOPED_TIMER(); + + if (!valobj.get()) { + retval.assign("<no object>"); + return false; + } + + void *old_callee = nullptr; + StructuredData::Generic *generic = nullptr; + if (callee_wrapper_sp) { + generic = callee_wrapper_sp->GetAsGeneric(); + if (generic) + old_callee = generic->GetValue(); + } + void *new_callee = old_callee; + + bool ret_val; + if (python_function_name && *python_function_name) { + { + Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | + Locker::NoSTDIN); + { + TypeSummaryOptionsSP options_sp(new TypeSummaryOptions(options)); + + static Timer::Category func_cat("LLDBSwigPythonCallTypeScript"); + Timer scoped_timer(func_cat, "LLDBSwigPythonCallTypeScript"); + ret_val = SWIGBridge::LLDBSwigPythonCallTypeScript( + python_function_name, GetSessionDictionary().get(), valobj, + &new_callee, options_sp, retval); + } + } + } else { + retval.assign("<no function name>"); + return false; + } + + if (new_callee && old_callee != new_callee) { + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + callee_wrapper_sp = std::make_shared<StructuredPythonObject>( + PythonObject(PyRefType::Borrowed, static_cast<PyObject *>(new_callee))); + } + + 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 SWIGBridge::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) { + CommandDataPython *bp_option_data = (CommandDataPython *)baton; + const char *python_function_name = bp_option_data->script_source.c_str(); + + if (!context) + return true; + + ExecutionContext exe_ctx(context->exe_ctx_ref); + Target *target = exe_ctx.GetTargetPtr(); + + if (!target) + return true; + + Debugger &debugger = target->GetDebugger(); + ScriptInterpreterPythonImpl *python_interpreter = + GetPythonInterpreter(debugger); + + if (!python_interpreter) + return true; + + if (python_function_name && python_function_name[0]) { + const StackFrameSP stop_frame_sp(exe_ctx.GetFrameSP()); + BreakpointSP breakpoint_sp = target->GetBreakpointByID(break_id); + if (breakpoint_sp) { + const BreakpointLocationSP bp_loc_sp( + breakpoint_sp->FindLocationByID(break_loc_id)); + + if (stop_frame_sp && bp_loc_sp) { + bool ret_val = true; + { + Locker py_lock(python_interpreter, Locker::AcquireLock | + Locker::InitSession | + Locker::NoSTDIN); + Expected<bool> maybe_ret_val = + SWIGBridge::LLDBSwigPythonBreakpointCallbackFunction( + python_function_name, + python_interpreter->m_dictionary_name.c_str(), stop_frame_sp, + bp_loc_sp, bp_option_data->m_extra_args); + + if (!maybe_ret_val) { + + llvm::handleAllErrors( + maybe_ret_val.takeError(), + [&](PythonException &E) { + debugger.GetErrorStream() << E.ReadBacktrace(); + }, + [&](const llvm::ErrorInfoBase &E) { + debugger.GetErrorStream() << E.message(); + }); + + } else { + ret_val = maybe_ret_val.get(); + } + } + return ret_val; + } + } + } + // We currently always true so we stop in case anything goes wrong when + // trying to call the script function + return true; +} + +bool ScriptInterpreterPythonImpl::WatchpointCallbackFunction( + void *baton, StoppointCallbackContext *context, user_id_t watch_id) { + WatchpointOptions::CommandData *wp_option_data = + (WatchpointOptions::CommandData *)baton; + const char *python_function_name = wp_option_data->script_source.c_str(); + + if (!context) + return true; + + ExecutionContext exe_ctx(context->exe_ctx_ref); + Target *target = exe_ctx.GetTargetPtr(); + + if (!target) + return true; + + Debugger &debugger = target->GetDebugger(); + ScriptInterpreterPythonImpl *python_interpreter = + GetPythonInterpreter(debugger); + + if (!python_interpreter) + return true; + + if (python_function_name && python_function_name[0]) { + const StackFrameSP stop_frame_sp(exe_ctx.GetFrameSP()); + WatchpointSP wp_sp = target->GetWatchpointList().FindByID(watch_id); + if (wp_sp) { + if (stop_frame_sp && wp_sp) { + bool ret_val = true; + { + Locker py_lock(python_interpreter, Locker::AcquireLock | + Locker::InitSession | + Locker::NoSTDIN); + ret_val = SWIGBridge::LLDBSwigPythonWatchpointCallbackFunction( + python_function_name, + python_interpreter->m_dictionary_name.c_str(), stop_frame_sp, + wp_sp); + } + return ret_val; + } + } + } + // We currently always true so we stop in case anything goes wrong when + // trying to call the script function + return true; +} + +size_t ScriptInterpreterPythonImpl::CalculateNumChildren( + const StructuredData::ObjectSP &implementor_sp, uint32_t max) { + if (!implementor_sp) + return 0; + StructuredData::Generic *generic = implementor_sp->GetAsGeneric(); + if (!generic) + return 0; + auto *implementor = static_cast<PyObject *>(generic->GetValue()); + if (!implementor) + return 0; + + size_t ret_val = 0; + + { + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + ret_val = SWIGBridge::LLDBSwigPython_CalculateNumChildren(implementor, max); + } + + return ret_val; +} + +lldb::ValueObjectSP ScriptInterpreterPythonImpl::GetChildAtIndex( + const StructuredData::ObjectSP &implementor_sp, uint32_t idx) { + if (!implementor_sp) + return lldb::ValueObjectSP(); + + StructuredData::Generic *generic = implementor_sp->GetAsGeneric(); + if (!generic) + return lldb::ValueObjectSP(); + auto *implementor = static_cast<PyObject *>(generic->GetValue()); + if (!implementor) + return lldb::ValueObjectSP(); + + lldb::ValueObjectSP ret_val; + { + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + PyObject *child_ptr = + SWIGBridge::LLDBSwigPython_GetChildAtIndex(implementor, idx); + if (child_ptr != nullptr && child_ptr != Py_None) { + lldb::SBValue *sb_value_ptr = + (lldb::SBValue *)LLDBSWIGPython_CastPyObjectToSBValue(child_ptr); + if (sb_value_ptr == nullptr) + Py_XDECREF(child_ptr); + else + ret_val = SWIGBridge::LLDBSWIGPython_GetValueObjectSPFromSBValue( + sb_value_ptr); + } else { + Py_XDECREF(child_ptr); + } + } + + return ret_val; +} + +int ScriptInterpreterPythonImpl::GetIndexOfChildWithName( + const StructuredData::ObjectSP &implementor_sp, const char *child_name) { + if (!implementor_sp) + return UINT32_MAX; + + StructuredData::Generic *generic = implementor_sp->GetAsGeneric(); + if (!generic) + return UINT32_MAX; + auto *implementor = static_cast<PyObject *>(generic->GetValue()); + if (!implementor) + return UINT32_MAX; + + int ret_val = UINT32_MAX; + + { + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + ret_val = SWIGBridge::LLDBSwigPython_GetIndexOfChildWithName(implementor, child_name); + } + + return ret_val; +} + +bool ScriptInterpreterPythonImpl::UpdateSynthProviderInstance( + const StructuredData::ObjectSP &implementor_sp) { + bool ret_val = false; + + if (!implementor_sp) + return ret_val; + + StructuredData::Generic *generic = implementor_sp->GetAsGeneric(); + if (!generic) + return ret_val; + auto *implementor = static_cast<PyObject *>(generic->GetValue()); + if (!implementor) + return ret_val; + + { + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + ret_val = + SWIGBridge::LLDBSwigPython_UpdateSynthProviderInstance(implementor); + } + + return ret_val; +} + +bool ScriptInterpreterPythonImpl::MightHaveChildrenSynthProviderInstance( + const StructuredData::ObjectSP &implementor_sp) { + bool ret_val = false; + + if (!implementor_sp) + return ret_val; + + StructuredData::Generic *generic = implementor_sp->GetAsGeneric(); + if (!generic) + return ret_val; + auto *implementor = static_cast<PyObject *>(generic->GetValue()); + if (!implementor) + return ret_val; + + { + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + ret_val = SWIGBridge::LLDBSwigPython_MightHaveChildrenSynthProviderInstance( + implementor); + } + + return ret_val; +} + +lldb::ValueObjectSP ScriptInterpreterPythonImpl::GetSyntheticValue( + const StructuredData::ObjectSP &implementor_sp) { + lldb::ValueObjectSP ret_val(nullptr); + + if (!implementor_sp) + return ret_val; + + StructuredData::Generic *generic = implementor_sp->GetAsGeneric(); + if (!generic) + return ret_val; + auto *implementor = static_cast<PyObject *>(generic->GetValue()); + if (!implementor) + return ret_val; + + { + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + PyObject *child_ptr = + SWIGBridge::LLDBSwigPython_GetValueSynthProviderInstance(implementor); + if (child_ptr != nullptr && child_ptr != Py_None) { + lldb::SBValue *sb_value_ptr = + (lldb::SBValue *)LLDBSWIGPython_CastPyObjectToSBValue(child_ptr); + if (sb_value_ptr == nullptr) + Py_XDECREF(child_ptr); + else + ret_val = SWIGBridge::LLDBSWIGPython_GetValueObjectSPFromSBValue( + sb_value_ptr); + } else { + Py_XDECREF(child_ptr); + } + } + + return ret_val; +} + +ConstString ScriptInterpreterPythonImpl::GetSyntheticTypeName( + const StructuredData::ObjectSP &implementor_sp) { + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + + if (!implementor_sp) + return {}; + + StructuredData::Generic *generic = implementor_sp->GetAsGeneric(); + if (!generic) + return {}; + + PythonObject implementor(PyRefType::Borrowed, + (PyObject *)generic->GetValue()); + if (!implementor.IsAllocated()) + return {}; + + llvm::Expected<PythonObject> expected_py_return = + implementor.CallMethod("get_type_name"); + + if (!expected_py_return) { + llvm::consumeError(expected_py_return.takeError()); + return {}; + } + + PythonObject py_return = std::move(expected_py_return.get()); + if (!py_return.IsAllocated() || !PythonString::Check(py_return.get())) + return {}; + + PythonString type_name(PyRefType::Borrowed, py_return.get()); + return ConstString(type_name.GetString()); +} + +bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword( + const char *impl_function, Process *process, std::string &output, + Status &error) { + bool ret_val; + if (!process) { + error.SetErrorString("no process"); + return false; + } + if (!impl_function || !impl_function[0]) { + error.SetErrorString("no function to execute"); + return false; + } + + { + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + ret_val = SWIGBridge::LLDBSWIGPythonRunScriptKeywordProcess( + impl_function, m_dictionary_name.c_str(), process->shared_from_this(), + output); + if (!ret_val) + error.SetErrorString("python script evaluation failed"); + } + return ret_val; +} + +bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword( + const char *impl_function, Thread *thread, std::string &output, + Status &error) { + if (!thread) { + error.SetErrorString("no thread"); + return false; + } + if (!impl_function || !impl_function[0]) { + error.SetErrorString("no function to execute"); + return false; + } + + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + if (std::optional<std::string> result = + SWIGBridge::LLDBSWIGPythonRunScriptKeywordThread( + impl_function, m_dictionary_name.c_str(), + thread->shared_from_this())) { + output = std::move(*result); + return true; + } + error.SetErrorString("python script evaluation failed"); + return false; +} + +bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword( + const char *impl_function, Target *target, std::string &output, + Status &error) { + bool ret_val; + if (!target) { + error.SetErrorString("no thread"); + return false; + } + if (!impl_function || !impl_function[0]) { + error.SetErrorString("no function to execute"); + return false; + } + + { + TargetSP target_sp(target->shared_from_this()); + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + ret_val = SWIGBridge::LLDBSWIGPythonRunScriptKeywordTarget( + impl_function, m_dictionary_name.c_str(), target_sp, output); + if (!ret_val) + error.SetErrorString("python script evaluation failed"); + } + return ret_val; +} + +bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword( + const char *impl_function, StackFrame *frame, std::string &output, + Status &error) { + if (!frame) { + error.SetErrorString("no frame"); + return false; + } + if (!impl_function || !impl_function[0]) { + error.SetErrorString("no function to execute"); + return false; + } + + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + if (std::optional<std::string> result = + SWIGBridge::LLDBSWIGPythonRunScriptKeywordFrame( + impl_function, m_dictionary_name.c_str(), + frame->shared_from_this())) { + output = std::move(*result); + return true; + } + error.SetErrorString("python script evaluation failed"); + return false; +} + +bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword( + const char *impl_function, ValueObject *value, std::string &output, + Status &error) { + bool ret_val; + if (!value) { + error.SetErrorString("no value"); + return false; + } + if (!impl_function || !impl_function[0]) { + error.SetErrorString("no function to execute"); + return false; + } + + { + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); + ret_val = SWIGBridge::LLDBSWIGPythonRunScriptKeywordValue( + impl_function, m_dictionary_name.c_str(), value->GetSP(), output); + if (!ret_val) + error.SetErrorString("python script evaluation failed"); + } + return ret_val; +} + +uint64_t replace_all(std::string &str, const std::string &oldStr, + const std::string &newStr) { + size_t pos = 0; + uint64_t matches = 0; + while ((pos = str.find(oldStr, pos)) != std::string::npos) { + matches++; + str.replace(pos, oldStr.length(), newStr); + pos += newStr.length(); + } + return matches; +} + +bool ScriptInterpreterPythonImpl::LoadScriptingModule( + const char *pathname, const LoadScriptOptions &options, + lldb_private::Status &error, StructuredData::ObjectSP *module_sp, + FileSpec extra_search_dir) { + namespace fs = llvm::sys::fs; + namespace path = llvm::sys::path; + + ExecuteScriptOptions exc_options = ExecuteScriptOptions() + .SetEnableIO(!options.GetSilent()) + .SetSetLLDBGlobals(false); + + if (!pathname || !pathname[0]) { + error.SetErrorString("empty path"); + return false; + } + + llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>> + io_redirect_or_error = ScriptInterpreterIORedirect::Create( + exc_options.GetEnableIO(), m_debugger, /*result=*/nullptr); + + if (!io_redirect_or_error) { + error = io_redirect_or_error.takeError(); + return false; + } + + ScriptInterpreterIORedirect &io_redirect = **io_redirect_or_error; + + // Before executing Python code, lock the GIL. + Locker py_lock(this, + Locker::AcquireLock | + (options.GetInitSession() ? Locker::InitSession : 0) | + Locker::NoSTDIN, + Locker::FreeAcquiredLock | + (options.GetInitSession() ? Locker::TearDownSession : 0), + io_redirect.GetInputFile(), io_redirect.GetOutputFile(), + io_redirect.GetErrorFile()); + + auto ExtendSysPath = [&](std::string directory) -> llvm::Error { + if (directory.empty()) { + return llvm::createStringError("invalid directory name"); + } + + replace_all(directory, "\\", "\\\\"); + replace_all(directory, "'", "\\'"); + + // Make sure that Python has "directory" in the search path. + StreamString command_stream; + command_stream.Printf("if not (sys.path.__contains__('%s')):\n " + "sys.path.insert(1,'%s');\n\n", + directory.c_str(), directory.c_str()); + bool syspath_retval = + ExecuteMultipleLines(command_stream.GetData(), exc_options).Success(); + if (!syspath_retval) + return llvm::createStringError("Python sys.path handling failed"); + + return llvm::Error::success(); + }; + + std::string module_name(pathname); + bool possible_package = false; + + if (extra_search_dir) { + if (llvm::Error e = ExtendSysPath(extra_search_dir.GetPath())) { + error = std::move(e); + return false; + } + } else { + FileSpec module_file(pathname); + FileSystem::Instance().Resolve(module_file); + + fs::file_status st; + std::error_code ec = status(module_file.GetPath(), st); + + if (ec || st.type() == fs::file_type::status_error || + st.type() == fs::file_type::type_unknown || + st.type() == fs::file_type::file_not_found) { + // if not a valid file of any sort, check if it might be a filename still + // dot can't be used but / and \ can, and if either is found, reject + if (strchr(pathname, '\\') || strchr(pathname, '/')) { + error.SetErrorStringWithFormatv("invalid pathname '{0}'", pathname); + return false; + } + // Not a filename, probably a package of some sort, let it go through. + possible_package = true; + } else if (is_directory(st) || is_regular_file(st)) { + if (module_file.GetDirectory().IsEmpty()) { + error.SetErrorStringWithFormatv("invalid directory name '{0}'", pathname); + return false; + } + if (llvm::Error e = + ExtendSysPath(module_file.GetDirectory().GetCString())) { + error = std::move(e); + return false; + } + module_name = module_file.GetFilename().GetCString(); + } else { + error.SetErrorString("no known way to import this module specification"); + return false; + } + } + + // Strip .py or .pyc extension + llvm::StringRef extension = llvm::sys::path::extension(module_name); + if (!extension.empty()) { + if (extension == ".py") + module_name.resize(module_name.length() - 3); + else if (extension == ".pyc") + module_name.resize(module_name.length() - 4); + } + + if (!possible_package && module_name.find('.') != llvm::StringRef::npos) { + error.SetErrorStringWithFormat( + "Python does not allow dots in module names: %s", module_name.c_str()); + return false; + } + + if (module_name.find('-') != llvm::StringRef::npos) { + error.SetErrorStringWithFormat( + "Python discourages dashes in module names: %s", module_name.c_str()); + return false; + } + + // Check if the module is already imported. + StreamString command_stream; + command_stream.Clear(); + command_stream.Printf("sys.modules.__contains__('%s')", module_name.c_str()); + bool does_contain = false; + // This call will succeed if the module was ever imported in any Debugger in + // the lifetime of the process in which this LLDB framework is living. + const bool does_contain_executed = ExecuteOneLineWithReturn( + command_stream.GetData(), + ScriptInterpreterPythonImpl::eScriptReturnTypeBool, &does_contain, exc_options); + + const bool was_imported_globally = does_contain_executed && does_contain; + const bool was_imported_locally = + GetSessionDictionary() + .GetItemForKey(PythonString(module_name)) + .IsAllocated(); + + // now actually do the import + command_stream.Clear(); + + if (was_imported_globally || was_imported_locally) { + if (!was_imported_locally) + command_stream.Printf("import %s ; reload_module(%s)", + module_name.c_str(), module_name.c_str()); + else + command_stream.Printf("reload_module(%s)", module_name.c_str()); + } else + command_stream.Printf("import %s", module_name.c_str()); + + error = ExecuteMultipleLines(command_stream.GetData(), exc_options); + if (error.Fail()) + return false; + + // if we are here, everything worked + // call __lldb_init_module(debugger,dict) + if (!SWIGBridge::LLDBSwigPythonCallModuleInit( + module_name.c_str(), m_dictionary_name.c_str(), + m_debugger.shared_from_this())) { + error.SetErrorString("calling __lldb_init_module failed"); + return false; + } + + if (module_sp) { + // everything went just great, now set the module object + command_stream.Clear(); + command_stream.Printf("%s", module_name.c_str()); + void *module_pyobj = nullptr; + if (ExecuteOneLineWithReturn( + command_stream.GetData(), + ScriptInterpreter::eScriptReturnTypeOpaqueObject, &module_pyobj, + exc_options) && + module_pyobj) + *module_sp = std::make_shared<StructuredPythonObject>(PythonObject( + PyRefType::Owned, static_cast<PyObject *>(module_pyobj))); + } + + return true; +} + +bool ScriptInterpreterPythonImpl::IsReservedWord(const char *word) { + if (!word || !word[0]) + return false; + + llvm::StringRef word_sr(word); + + // filter out a few characters that would just confuse us and that are + // clearly not keyword material anyway + if (word_sr.find('"') != llvm::StringRef::npos || + word_sr.find('\'') != llvm::StringRef::npos) + return false; + + StreamString command_stream; + command_stream.Printf("keyword.iskeyword('%s')", word); + bool result; + ExecuteScriptOptions options; + options.SetEnableIO(false); + options.SetMaskoutErrors(true); + options.SetSetLLDBGlobals(false); + if (ExecuteOneLineWithReturn(command_stream.GetData(), + ScriptInterpreter::eScriptReturnTypeBool, + &result, options)) + return result; + return false; +} + +ScriptInterpreterPythonImpl::SynchronicityHandler::SynchronicityHandler( + lldb::DebuggerSP debugger_sp, ScriptedCommandSynchronicity synchro) + : m_debugger_sp(debugger_sp), m_synch_wanted(synchro), + m_old_asynch(debugger_sp->GetAsyncExecution()) { + if (m_synch_wanted == eScriptedCommandSynchronicitySynchronous) + m_debugger_sp->SetAsyncExecution(false); + else if (m_synch_wanted == eScriptedCommandSynchronicityAsynchronous) + m_debugger_sp->SetAsyncExecution(true); +} + +ScriptInterpreterPythonImpl::SynchronicityHandler::~SynchronicityHandler() { + if (m_synch_wanted != eScriptedCommandSynchronicityCurrentValue) + m_debugger_sp->SetAsyncExecution(m_old_asynch); +} + +bool ScriptInterpreterPythonImpl::RunScriptBasedCommand( + const char *impl_function, llvm::StringRef args, + ScriptedCommandSynchronicity synchronicity, + lldb_private::CommandReturnObject &cmd_retobj, Status &error, + const lldb_private::ExecutionContext &exe_ctx) { + if (!impl_function) { + error.SetErrorString("no function to execute"); + return false; + } + + lldb::DebuggerSP debugger_sp = m_debugger.shared_from_this(); + lldb::ExecutionContextRefSP exe_ctx_ref_sp(new ExecutionContextRef(exe_ctx)); + + if (!debugger_sp.get()) { + error.SetErrorString("invalid Debugger pointer"); + return false; + } + + bool ret_val = false; + + std::string err_msg; + + { + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | + (cmd_retobj.GetInteractive() ? 0 : Locker::NoSTDIN), + Locker::FreeLock | Locker::TearDownSession); + + SynchronicityHandler synch_handler(debugger_sp, synchronicity); + + std::string args_str = args.str(); + ret_val = SWIGBridge::LLDBSwigPythonCallCommand( + impl_function, m_dictionary_name.c_str(), debugger_sp, args_str.c_str(), + cmd_retobj, exe_ctx_ref_sp); + } + + if (!ret_val) + error.SetErrorString("unable to execute script function"); + else if (cmd_retobj.GetStatus() == eReturnStatusFailed) + return false; + + error.Clear(); + return ret_val; +} + +bool ScriptInterpreterPythonImpl::RunScriptBasedCommand( + StructuredData::GenericSP impl_obj_sp, llvm::StringRef args, + ScriptedCommandSynchronicity synchronicity, + lldb_private::CommandReturnObject &cmd_retobj, Status &error, + const lldb_private::ExecutionContext &exe_ctx) { + if (!impl_obj_sp || !impl_obj_sp->IsValid()) { + error.SetErrorString("no function to execute"); + return false; + } + + lldb::DebuggerSP debugger_sp = m_debugger.shared_from_this(); + lldb::ExecutionContextRefSP exe_ctx_ref_sp(new ExecutionContextRef(exe_ctx)); + + if (!debugger_sp.get()) { + error.SetErrorString("invalid Debugger pointer"); + return false; + } + + bool ret_val = false; + + std::string err_msg; + + { + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | + (cmd_retobj.GetInteractive() ? 0 : Locker::NoSTDIN), + Locker::FreeLock | Locker::TearDownSession); + + SynchronicityHandler synch_handler(debugger_sp, synchronicity); + + std::string args_str = args.str(); + ret_val = SWIGBridge::LLDBSwigPythonCallCommandObject( + static_cast<PyObject *>(impl_obj_sp->GetValue()), debugger_sp, + args_str.c_str(), cmd_retobj, exe_ctx_ref_sp); + } + + if (!ret_val) + error.SetErrorString("unable to execute script function"); + else if (cmd_retobj.GetStatus() == eReturnStatusFailed) + return false; + + error.Clear(); + return ret_val; +} + +bool ScriptInterpreterPythonImpl::RunScriptBasedParsedCommand( + StructuredData::GenericSP impl_obj_sp, Args &args, + ScriptedCommandSynchronicity synchronicity, + lldb_private::CommandReturnObject &cmd_retobj, Status &error, + const lldb_private::ExecutionContext &exe_ctx) { + if (!impl_obj_sp || !impl_obj_sp->IsValid()) { + error.SetErrorString("no function to execute"); + return false; + } + + lldb::DebuggerSP debugger_sp = m_debugger.shared_from_this(); + lldb::ExecutionContextRefSP exe_ctx_ref_sp(new ExecutionContextRef(exe_ctx)); + + if (!debugger_sp.get()) { + error.SetErrorString("invalid Debugger pointer"); + return false; + } + + bool ret_val = false; + + std::string err_msg; + + { + Locker py_lock(this, + Locker::AcquireLock | Locker::InitSession | + (cmd_retobj.GetInteractive() ? 0 : Locker::NoSTDIN), + Locker::FreeLock | Locker::TearDownSession); + + SynchronicityHandler synch_handler(debugger_sp, synchronicity); + + StructuredData::ArraySP args_arr_sp(new StructuredData::Array()); + + for (const Args::ArgEntry &entry : args) { + args_arr_sp->AddStringItem(entry.ref()); + } + StructuredDataImpl args_impl(args_arr_sp); + + ret_val = SWIGBridge::LLDBSwigPythonCallParsedCommandObject( + static_cast<PyObject *>(impl_obj_sp->GetValue()), debugger_sp, + args_impl, cmd_retobj, exe_ctx_ref_sp); + } + + if (!ret_val) + error.SetErrorString("unable to execute script function"); + else if (cmd_retobj.GetStatus() == eReturnStatusFailed) + return false; + + error.Clear(); + return ret_val; +} + +std::optional<std::string> +ScriptInterpreterPythonImpl::GetRepeatCommandForScriptedCommand( + StructuredData::GenericSP impl_obj_sp, Args &args) { + if (!impl_obj_sp || !impl_obj_sp->IsValid()) + return std::nullopt; + + lldb::DebuggerSP debugger_sp = m_debugger.shared_from_this(); + + if (!debugger_sp.get()) + return std::nullopt; + + std::optional<std::string> ret_val; + + { + Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, + Locker::FreeLock); + + StructuredData::ArraySP args_arr_sp(new StructuredData::Array()); + + // For scripting commands, we send the command string: + std::string command; + args.GetQuotedCommandString(command); + ret_val = SWIGBridge::LLDBSwigPythonGetRepeatCommandForScriptedCommand( + static_cast<PyObject *>(impl_obj_sp->GetValue()), command); + } + return ret_val; +} + +/// In Python, a special attribute __doc__ contains the docstring for an object +/// (function, method, class, ...) if any is defined Otherwise, the attribute's +/// value is None. +bool ScriptInterpreterPythonImpl::GetDocumentationForItem(const char *item, + std::string &dest) { + dest.clear(); + + if (!item || !*item) + return false; + + std::string command(item); + command += ".__doc__"; + + // Python is going to point this to valid data if ExecuteOneLineWithReturn + // returns successfully. + char *result_ptr = nullptr; + + if (ExecuteOneLineWithReturn( + command, ScriptInterpreter::eScriptReturnTypeCharStrOrNone, + &result_ptr, + ExecuteScriptOptions().SetEnableIO(false))) { + if (result_ptr) + dest.assign(result_ptr); + return true; + } + + StreamString str_stream; + str_stream << "Function " << item + << " was not found. Containing module might be missing."; + dest = std::string(str_stream.GetString()); + + return false; +} + +bool ScriptInterpreterPythonImpl::GetShortHelpForCommandObject( + StructuredData::GenericSP cmd_obj_sp, std::string &dest) { + dest.clear(); + + Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); + + if (!cmd_obj_sp) + return false; + + PythonObject implementor(PyRefType::Borrowed, + (PyObject *)cmd_obj_sp->GetValue()); + + if (!implementor.IsAllocated()) + return false; + + llvm::Expected<PythonObject> expected_py_return = + implementor.CallMethod("get_short_help"); + + if (!expected_py_return) { + llvm::consumeError(expected_py_return.takeError()); + return false; + } + + 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()); + llvm::StringRef return_data(py_string.GetString()); + dest.assign(return_data.data(), return_data.size()); + return true; + } + + return false; +} + +uint32_t ScriptInterpreterPythonImpl::GetFlagsForCommandObject( + StructuredData::GenericSP cmd_obj_sp) { + uint32_t result = 0; + + Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); + + static char callee_name[] = "get_flags"; + + if (!cmd_obj_sp) + return result; + + PythonObject implementor(PyRefType::Borrowed, + (PyObject *)cmd_obj_sp->GetValue()); + + if (!implementor.IsAllocated()) + return result; + + PythonObject pmeth(PyRefType::Owned, + PyObject_GetAttrString(implementor.get(), callee_name)); + + if (PyErr_Occurred()) + PyErr_Clear(); + + if (!pmeth.IsAllocated()) + return result; + + if (PyCallable_Check(pmeth.get()) == 0) { + if (PyErr_Occurred()) + PyErr_Clear(); + return result; + } + + if (PyErr_Occurred()) + PyErr_Clear(); + + long long py_return = unwrapOrSetPythonException( + As<long long>(implementor.CallMethod(callee_name))); + + // if it fails, print the error but otherwise go on + if (PyErr_Occurred()) { + PyErr_Print(); + PyErr_Clear(); + } else { + result = py_return; + } + + return result; +} + +StructuredData::ObjectSP +ScriptInterpreterPythonImpl::GetOptionsForCommandObject( + StructuredData::GenericSP cmd_obj_sp) { + StructuredData::ObjectSP result = {}; + + Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); + + static char callee_name[] = "get_options_definition"; + + if (!cmd_obj_sp) + return result; + + PythonObject implementor(PyRefType::Borrowed, + (PyObject *)cmd_obj_sp->GetValue()); + + if (!implementor.IsAllocated()) + return result; + + PythonObject pmeth(PyRefType::Owned, + PyObject_GetAttrString(implementor.get(), callee_name)); + + if (PyErr_Occurred()) + PyErr_Clear(); + + if (!pmeth.IsAllocated()) + return result; + + if (PyCallable_Check(pmeth.get()) == 0) { + if (PyErr_Occurred()) + PyErr_Clear(); + return result; + } + + if (PyErr_Occurred()) + PyErr_Clear(); + + PythonDictionary py_return = unwrapOrSetPythonException( + As<PythonDictionary>(implementor.CallMethod(callee_name))); + + // if it fails, print the error but otherwise go on + if (PyErr_Occurred()) { + PyErr_Print(); + PyErr_Clear(); + return {}; + } + return py_return.CreateStructuredObject(); +} + +StructuredData::ObjectSP +ScriptInterpreterPythonImpl::GetArgumentsForCommandObject( + StructuredData::GenericSP cmd_obj_sp) { + StructuredData::ObjectSP result = {}; + + Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); + + static char callee_name[] = "get_args_definition"; + + if (!cmd_obj_sp) + return result; + + PythonObject implementor(PyRefType::Borrowed, + (PyObject *)cmd_obj_sp->GetValue()); + + if (!implementor.IsAllocated()) + return result; + + PythonObject pmeth(PyRefType::Owned, + PyObject_GetAttrString(implementor.get(), callee_name)); + + if (PyErr_Occurred()) + PyErr_Clear(); + + if (!pmeth.IsAllocated()) + return result; + + if (PyCallable_Check(pmeth.get()) == 0) { + if (PyErr_Occurred()) + PyErr_Clear(); + return result; + } + + if (PyErr_Occurred()) + PyErr_Clear(); + + PythonList py_return = unwrapOrSetPythonException( + As<PythonList>(implementor.CallMethod(callee_name))); + + // if it fails, print the error but otherwise go on + if (PyErr_Occurred()) { + PyErr_Print(); + PyErr_Clear(); + return {}; + } + return py_return.CreateStructuredObject(); +} + +void +ScriptInterpreterPythonImpl::OptionParsingStartedForCommandObject( + StructuredData::GenericSP cmd_obj_sp) { + + Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); + + static char callee_name[] = "option_parsing_started"; + + if (!cmd_obj_sp) + return ; + + PythonObject implementor(PyRefType::Borrowed, + (PyObject *)cmd_obj_sp->GetValue()); + + if (!implementor.IsAllocated()) + return; + + PythonObject pmeth(PyRefType::Owned, + PyObject_GetAttrString(implementor.get(), callee_name)); + + if (PyErr_Occurred()) + PyErr_Clear(); + + if (!pmeth.IsAllocated()) + return; + + if (PyCallable_Check(pmeth.get()) == 0) { + if (PyErr_Occurred()) + PyErr_Clear(); + return; + } + + if (PyErr_Occurred()) + PyErr_Clear(); + + // option_parsing_starting doesn't return anything, ignore anything but + // python errors. + unwrapOrSetPythonException( + As<bool>(implementor.CallMethod(callee_name))); + + // if it fails, print the error but otherwise go on + if (PyErr_Occurred()) { + PyErr_Print(); + PyErr_Clear(); + return; + } +} + +bool +ScriptInterpreterPythonImpl::SetOptionValueForCommandObject( + StructuredData::GenericSP cmd_obj_sp, ExecutionContext *exe_ctx, + llvm::StringRef long_option, llvm::StringRef value) { + StructuredData::ObjectSP result = {}; + + Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); + + static char callee_name[] = "set_option_value"; + + if (!cmd_obj_sp) + return false; + + PythonObject implementor(PyRefType::Borrowed, + (PyObject *)cmd_obj_sp->GetValue()); + + 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(); + return false; + } + + if (PyErr_Occurred()) + PyErr_Clear(); + + lldb::ExecutionContextRefSP exe_ctx_ref_sp; + if (exe_ctx) + exe_ctx_ref_sp.reset(new ExecutionContextRef(exe_ctx)); + PythonObject ctx_ref_obj = SWIGBridge::ToSWIGWrapper(exe_ctx_ref_sp); + + bool py_return = unwrapOrSetPythonException( + As<bool>(implementor.CallMethod(callee_name, ctx_ref_obj, long_option.str().c_str(), + value.str().c_str()))); + + // if it fails, print the error but otherwise go on + if (PyErr_Occurred()) { + PyErr_Print(); + PyErr_Clear(); + return false; + } + return py_return; +} + +bool ScriptInterpreterPythonImpl::GetLongHelpForCommandObject( + StructuredData::GenericSP cmd_obj_sp, std::string &dest) { + dest.clear(); + + Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); + + if (!cmd_obj_sp) + return false; + + PythonObject implementor(PyRefType::Borrowed, + (PyObject *)cmd_obj_sp->GetValue()); + + if (!implementor.IsAllocated()) + return false; + + llvm::Expected<PythonObject> expected_py_return = + implementor.CallMethod("get_long_help"); + + if (!expected_py_return) { + llvm::consumeError(expected_py_return.takeError()); + return false; + } + + 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()); + dest.assign(str_data.data(), str_data.size()); + got_string = true; + } + + return got_string; +} + +std::unique_ptr<ScriptInterpreterLocker> +ScriptInterpreterPythonImpl::AcquireInterpreterLock() { + std::unique_ptr<ScriptInterpreterLocker> py_lock(new Locker( + this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN, + Locker::FreeLock | Locker::TearDownSession)); + return py_lock; +} + +void ScriptInterpreterPythonImpl::Initialize() { + LLDB_SCOPED_TIMER(); + + // RAII-based initialization which correctly handles multiple-initialization, + // version- specific differences among Python 2 and Python 3, and saving and + // restoring various other pieces of state that can get mucked with during + // initialization. + InitializePythonRAII initialize_guard; + + LLDBSwigPyInit(); + + // Update the path python uses to search for modules to include the current + // directory. + + PyRun_SimpleString("import sys"); + AddToSysPath(AddLocation::End, "."); + + // Don't denormalize paths when calling file_spec.GetPath(). On platforms + // that use a backslash as the path separator, this will result in executing + // python code containing paths with unescaped backslashes. But Python also + // accepts forward slashes, so to make life easier we just use that. + if (FileSpec file_spec = GetPythonDir()) + AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false)); + if (FileSpec file_spec = HostInfo::GetShlibDir()) + AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false)); + + PyRun_SimpleString("sys.dont_write_bytecode = 1; import " + "lldb.embedded_interpreter; from " + "lldb.embedded_interpreter import run_python_interpreter; " + "from lldb.embedded_interpreter import run_one_line"); + +#if LLDB_USE_PYTHON_SET_INTERRUPT + // Python will not just overwrite its internal SIGINT handler but also the + // one from the process. Backup the current SIGINT handler to prevent that + // Python deletes it. + RestoreSignalHandlerScope save_sigint(SIGINT); + + // Setup a default SIGINT signal handler that works the same way as the + // normal Python REPL signal handler which raises a KeyboardInterrupt. + // Also make sure to not pollute the user's REPL with the signal module nor + // our utility function. + PyRun_SimpleString("def lldb_setup_sigint_handler():\n" + " import signal;\n" + " def signal_handler(sig, frame):\n" + " raise KeyboardInterrupt()\n" + " signal.signal(signal.SIGINT, signal_handler);\n" + "lldb_setup_sigint_handler();\n" + "del lldb_setup_sigint_handler\n"); +#endif +} + +void ScriptInterpreterPythonImpl::AddToSysPath(AddLocation location, + std::string path) { + std::string path_copy; + + std::string statement; + if (location == AddLocation::Beginning) { + statement.assign("sys.path.insert(0,\""); + statement.append(path); + statement.append("\")"); + } else { + statement.assign("sys.path.append(\""); + statement.append(path); + statement.append("\")"); + } + PyRun_SimpleString(statement.c_str()); +} + +// We are intentionally NOT calling Py_Finalize here (this would be the logical +// place to call it). Calling Py_Finalize here causes test suite runs to seg +// fault: The test suite runs in Python. It registers SBDebugger::Terminate to +// be called 'at_exit'. When the test suite Python harness finishes up, it +// calls Py_Finalize, which calls all the 'at_exit' registered functions. +// SBDebugger::Terminate calls Debugger::Terminate, which calls lldb::Terminate, +// which calls ScriptInterpreter::Terminate, which calls +// ScriptInterpreterPythonImpl::Terminate. So if we call Py_Finalize here, we +// end up with Py_Finalize being called from within Py_Finalize, which results +// in a seg fault. Since this function only gets called when lldb is shutting +// down and going away anyway, the fact that we don't actually call Py_Finalize +// should not cause any problems (everything should shut down/go away anyway +// when the process exits). +// +// void ScriptInterpreterPythonImpl::Terminate() { Py_Finalize (); } + +#endif diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h new file mode 100644 index 000000000000..2e8301a85eb6 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h @@ -0,0 +1,63 @@ +//===-- ScriptInterpreterPython.h -------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHON_H +#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHON_H + +#include "lldb/Host/Config.h" + +#if LLDB_ENABLE_PYTHON + +#include "lldb/Breakpoint/BreakpointOptions.h" +#include "lldb/Core/IOHandler.h" +#include "lldb/Core/StructuredDataImpl.h" +#include "lldb/Interpreter/ScriptInterpreter.h" +#include "lldb/lldb-private.h" + +#include <memory> +#include <string> +#include <vector> + +namespace lldb_private { +/// Abstract interface for the Python script interpreter. +class ScriptInterpreterPython : public ScriptInterpreter, + public IOHandlerDelegateMultiline { +public: + class CommandDataPython : public BreakpointOptions::CommandData { + public: + CommandDataPython() : BreakpointOptions::CommandData() { + interpreter = lldb::eScriptLanguagePython; + } + CommandDataPython(StructuredData::ObjectSP extra_args_sp) + : BreakpointOptions::CommandData(), + m_extra_args(std::move(extra_args_sp)) { + interpreter = lldb::eScriptLanguagePython; + } + StructuredDataImpl m_extra_args; + }; + + ScriptInterpreterPython(Debugger &debugger) + : ScriptInterpreter(debugger, lldb::eScriptLanguagePython), + IOHandlerDelegateMultiline("DONE") {} + + StructuredData::DictionarySP GetInterpreterInfo() override; + static void Initialize(); + static void Terminate(); + static llvm::StringRef GetPluginNameStatic() { return "script-python"; } + static llvm::StringRef GetPluginDescriptionStatic(); + static FileSpec GetPythonDir(); + static void SharedLibraryDirectoryHelper(FileSpec &this_file); + +protected: + static void ComputePythonDirForApple(llvm::SmallVectorImpl<char> &path); + static void ComputePythonDir(llvm::SmallVectorImpl<char> &path); +}; +} // namespace lldb_private + +#endif // LLDB_ENABLE_PYTHON +#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHON_H diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h new file mode 100644 index 000000000000..c2024efb395d --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h @@ -0,0 +1,499 @@ +//===-- ScriptInterpreterPythonImpl.h ---------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHONIMPL_H +#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHONIMPL_H + +#include "lldb/Host/Config.h" + +#if LLDB_ENABLE_PYTHON + +#include "lldb-python.h" + +#include "PythonDataObjects.h" +#include "ScriptInterpreterPython.h" + +#include "lldb/Host/Terminal.h" +#include "lldb/Utility/StreamString.h" + +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringRef.h" + +namespace lldb_private { +class IOHandlerPythonInterpreter; +class ScriptInterpreterPythonImpl : public ScriptInterpreterPython { +public: + friend class IOHandlerPythonInterpreter; + + ScriptInterpreterPythonImpl(Debugger &debugger); + + ~ScriptInterpreterPythonImpl() override; + + bool Interrupt() override; + + bool ExecuteOneLine( + llvm::StringRef command, CommandReturnObject *result, + const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; + + void ExecuteInterpreterLoop() override; + + bool ExecuteOneLineWithReturn( + llvm::StringRef in_string, + ScriptInterpreter::ScriptReturnType return_type, void *ret_value, + const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; + + lldb_private::Status ExecuteMultipleLines( + const char *in_string, + const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; + + Status + ExportFunctionDefinitionToInterpreter(StringList &function_def) override; + + bool GenerateTypeScriptFunction(StringList &input, std::string &output, + const void *name_token = nullptr) override; + + bool GenerateTypeSynthClass(StringList &input, std::string &output, + const void *name_token = nullptr) override; + + bool GenerateTypeSynthClass(const char *oneliner, std::string &output, + const void *name_token = nullptr) override; + + // use this if the function code is just a one-liner script + bool GenerateTypeScriptFunction(const char *oneliner, std::string &output, + const void *name_token = nullptr) override; + + bool GenerateScriptAliasFunction(StringList &input, + std::string &output) override; + + StructuredData::ObjectSP + CreateSyntheticScriptedProvider(const char *class_name, + lldb::ValueObjectSP valobj) override; + + StructuredData::GenericSP + CreateScriptCommandObject(const char *class_name) override; + + StructuredData::ObjectSP + CreateStructuredDataFromScriptObject(ScriptObject obj) override; + + StructuredData::GenericSP + CreateScriptedBreakpointResolver(const char *class_name, + const StructuredDataImpl &args_data, + lldb::BreakpointSP &bkpt_sp) override; + bool ScriptedBreakpointResolverSearchCallback( + StructuredData::GenericSP implementor_sp, + SymbolContext *sym_ctx) override; + + lldb::SearchDepth ScriptedBreakpointResolverSearchDepth( + StructuredData::GenericSP implementor_sp) override; + + StructuredData::GenericSP + CreateScriptedStopHook(lldb::TargetSP target_sp, const char *class_name, + const StructuredDataImpl &args_data, + Status &error) override; + + bool ScriptedStopHookHandleStop(StructuredData::GenericSP implementor_sp, + ExecutionContext &exc_ctx, + lldb::StreamSP stream_sp) override; + + StructuredData::GenericSP + CreateFrameRecognizer(const char *class_name) override; + + lldb::ValueObjectListSP + GetRecognizedArguments(const StructuredData::ObjectSP &implementor, + lldb::StackFrameSP frame_sp) override; + + lldb::ScriptedProcessInterfaceUP CreateScriptedProcessInterface() override; + + lldb::ScriptedThreadInterfaceSP CreateScriptedThreadInterface() override; + + lldb::ScriptedThreadPlanInterfaceSP + CreateScriptedThreadPlanInterface() override; + + lldb::OperatingSystemInterfaceSP CreateOperatingSystemInterface() override; + + StructuredData::ObjectSP + LoadPluginModule(const FileSpec &file_spec, + lldb_private::Status &error) override; + + StructuredData::DictionarySP + GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target, + const char *setting_name, + lldb_private::Status &error) override; + + size_t CalculateNumChildren(const StructuredData::ObjectSP &implementor, + uint32_t max) override; + + lldb::ValueObjectSP + GetChildAtIndex(const StructuredData::ObjectSP &implementor, + uint32_t idx) override; + + int GetIndexOfChildWithName(const StructuredData::ObjectSP &implementor, + const char *child_name) override; + + bool UpdateSynthProviderInstance( + const StructuredData::ObjectSP &implementor) override; + + bool MightHaveChildrenSynthProviderInstance( + const StructuredData::ObjectSP &implementor) override; + + lldb::ValueObjectSP + GetSyntheticValue(const StructuredData::ObjectSP &implementor) override; + + ConstString + GetSyntheticTypeName(const StructuredData::ObjectSP &implementor) override; + + bool + RunScriptBasedCommand(const char *impl_function, llvm::StringRef args, + ScriptedCommandSynchronicity synchronicity, + lldb_private::CommandReturnObject &cmd_retobj, + Status &error, + const lldb_private::ExecutionContext &exe_ctx) override; + + bool RunScriptBasedCommand( + StructuredData::GenericSP impl_obj_sp, llvm::StringRef args, + ScriptedCommandSynchronicity synchronicity, + lldb_private::CommandReturnObject &cmd_retobj, Status &error, + const lldb_private::ExecutionContext &exe_ctx) override; + + bool RunScriptBasedParsedCommand( + StructuredData::GenericSP impl_obj_sp, Args &args, + ScriptedCommandSynchronicity synchronicity, + lldb_private::CommandReturnObject &cmd_retobj, Status &error, + const lldb_private::ExecutionContext &exe_ctx) override; + + std::optional<std::string> + GetRepeatCommandForScriptedCommand(StructuredData::GenericSP impl_obj_sp, + Args &args) override; + + Status GenerateFunction(const char *signature, const StringList &input, + bool is_callback) override; + + Status GenerateBreakpointCommandCallbackData(StringList &input, + std::string &output, + bool has_extra_args, + bool is_callback) override; + + bool GenerateWatchpointCommandCallbackData(StringList &input, + std::string &output, + bool is_callback) override; + + bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj, + StructuredData::ObjectSP &callee_wrapper_sp, + const TypeSummaryOptions &options, + std::string &retval) override; + + bool FormatterCallbackFunction(const char *function_name, + lldb::TypeImplSP type_impl_sp) override; + + bool GetDocumentationForItem(const char *item, std::string &dest) override; + + bool GetShortHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, + std::string &dest) override; + + uint32_t + GetFlagsForCommandObject(StructuredData::GenericSP cmd_obj_sp) override; + + bool GetLongHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, + std::string &dest) override; + + StructuredData::ObjectSP + GetOptionsForCommandObject(StructuredData::GenericSP cmd_obj_sp) override; + + StructuredData::ObjectSP + GetArgumentsForCommandObject(StructuredData::GenericSP cmd_obj_sp) override; + + bool SetOptionValueForCommandObject(StructuredData::GenericSP cmd_obj_sp, + ExecutionContext *exe_ctx, + llvm::StringRef long_option, + llvm::StringRef value) override; + + void OptionParsingStartedForCommandObject( + StructuredData::GenericSP cmd_obj_sp) override; + + bool CheckObjectExists(const char *name) override { + if (!name || !name[0]) + return false; + std::string temp; + return GetDocumentationForItem(name, temp); + } + + bool RunScriptFormatKeyword(const char *impl_function, Process *process, + std::string &output, Status &error) override; + + bool RunScriptFormatKeyword(const char *impl_function, Thread *thread, + std::string &output, Status &error) override; + + bool RunScriptFormatKeyword(const char *impl_function, Target *target, + std::string &output, Status &error) override; + + bool RunScriptFormatKeyword(const char *impl_function, StackFrame *frame, + std::string &output, Status &error) override; + + bool RunScriptFormatKeyword(const char *impl_function, ValueObject *value, + std::string &output, Status &error) override; + + bool LoadScriptingModule(const char *filename, + const LoadScriptOptions &options, + lldb_private::Status &error, + StructuredData::ObjectSP *module_sp = nullptr, + FileSpec extra_search_dir = {}) override; + + bool IsReservedWord(const char *word) override; + + std::unique_ptr<ScriptInterpreterLocker> AcquireInterpreterLock() override; + + void CollectDataForBreakpointCommandCallback( + std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec, + CommandReturnObject &result) override; + + void + CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, + CommandReturnObject &result) override; + + /// Set the callback body text into the callback for the breakpoint. + Status SetBreakpointCommandCallback(BreakpointOptions &bp_options, + const char *callback_body, + bool is_callback) override; + + Status SetBreakpointCommandCallbackFunction( + BreakpointOptions &bp_options, const char *function_name, + StructuredData::ObjectSP extra_args_sp) override; + + /// This one is for deserialization: + Status SetBreakpointCommandCallback( + BreakpointOptions &bp_options, + std::unique_ptr<BreakpointOptions::CommandData> &data_up) override; + + Status SetBreakpointCommandCallback(BreakpointOptions &bp_options, + const char *command_body_text, + StructuredData::ObjectSP extra_args_sp, + bool uses_extra_args, + bool is_callback); + + /// Set a one-liner as the callback for the watchpoint. + void SetWatchpointCommandCallback(WatchpointOptions *wp_options, + const char *user_input, + bool is_callback) override; + + const char *GetDictionaryName() { return m_dictionary_name.c_str(); } + + PyThreadState *GetThreadState() { return m_command_thread_state; } + + void SetThreadState(PyThreadState *s) { + if (s) + m_command_thread_state = s; + } + + // IOHandlerDelegate + void IOHandlerActivated(IOHandler &io_handler, bool interactive) override; + + void IOHandlerInputComplete(IOHandler &io_handler, + std::string &data) override; + + static lldb::ScriptInterpreterSP CreateInstance(Debugger &debugger); + + // PluginInterface protocol + llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); } + + class Locker : public ScriptInterpreterLocker { + public: + enum OnEntry { + AcquireLock = 0x0001, + InitSession = 0x0002, + InitGlobals = 0x0004, + NoSTDIN = 0x0008 + }; + + enum OnLeave { + FreeLock = 0x0001, + FreeAcquiredLock = 0x0002, // do not free the lock if we already held it + // when calling constructor + TearDownSession = 0x0004 + }; + + Locker(ScriptInterpreterPythonImpl *py_interpreter, + uint16_t on_entry = AcquireLock | InitSession, + uint16_t on_leave = FreeLock | TearDownSession, + lldb::FileSP in = nullptr, lldb::FileSP out = nullptr, + lldb::FileSP err = nullptr); + + ~Locker() override; + + private: + bool DoAcquireLock(); + + bool DoInitSession(uint16_t on_entry_flags, lldb::FileSP in, + lldb::FileSP out, lldb::FileSP err); + + bool DoFreeLock(); + + bool DoTearDownSession(); + + bool m_teardown_session; + ScriptInterpreterPythonImpl *m_python_interpreter; + PyGILState_STATE m_GILState; + }; + + static bool BreakpointCallbackFunction(void *baton, + StoppointCallbackContext *context, + lldb::user_id_t break_id, + lldb::user_id_t break_loc_id); + static bool WatchpointCallbackFunction(void *baton, + StoppointCallbackContext *context, + lldb::user_id_t watch_id); + static void Initialize(); + + class SynchronicityHandler { + private: + lldb::DebuggerSP m_debugger_sp; + ScriptedCommandSynchronicity m_synch_wanted; + bool m_old_asynch; + + public: + SynchronicityHandler(lldb::DebuggerSP, ScriptedCommandSynchronicity); + + ~SynchronicityHandler(); + }; + + enum class AddLocation { Beginning, End }; + + static void AddToSysPath(AddLocation location, std::string path); + + bool EnterSession(uint16_t on_entry_flags, lldb::FileSP in, lldb::FileSP out, + lldb::FileSP err); + + void LeaveSession(); + + uint32_t IsExecutingPython() { + std::lock_guard<std::mutex> guard(m_mutex); + return m_lock_count > 0; + } + + uint32_t IncrementLockCount() { + std::lock_guard<std::mutex> guard(m_mutex); + return ++m_lock_count; + } + + uint32_t DecrementLockCount() { + std::lock_guard<std::mutex> guard(m_mutex); + if (m_lock_count > 0) + --m_lock_count; + return m_lock_count; + } + + enum ActiveIOHandler { + eIOHandlerNone, + eIOHandlerBreakpoint, + eIOHandlerWatchpoint + }; + + python::PythonModule &GetMainModule(); + + python::PythonDictionary &GetSessionDictionary(); + + python::PythonDictionary &GetSysModuleDictionary(); + + llvm::Expected<unsigned> GetMaxPositionalArgumentsForCallable( + const llvm::StringRef &callable_name) override; + + bool GetEmbeddedInterpreterModuleObjects(); + + bool SetStdHandle(lldb::FileSP file, const char *py_name, + python::PythonObject &save_file, const char *mode); + + python::PythonObject m_saved_stdin; + python::PythonObject m_saved_stdout; + python::PythonObject m_saved_stderr; + python::PythonModule m_main_module; + python::PythonDictionary m_session_dict; + python::PythonDictionary m_sys_module_dict; + python::PythonObject m_run_one_line_function; + python::PythonObject m_run_one_line_str_global; + std::string m_dictionary_name; + ActiveIOHandler m_active_io_handler; + bool m_session_is_active; + bool m_pty_secondary_is_open; + bool m_valid_session; + uint32_t m_lock_count; + std::mutex m_mutex; + PyThreadState *m_command_thread_state; +}; + +class IOHandlerPythonInterpreter : public IOHandler { +public: + IOHandlerPythonInterpreter(Debugger &debugger, + ScriptInterpreterPythonImpl *python) + : IOHandler(debugger, IOHandler::Type::PythonInterpreter), + m_python(python) {} + + ~IOHandlerPythonInterpreter() override = default; + + llvm::StringRef GetControlSequence(char ch) override { + static constexpr llvm::StringLiteral control_sequence("quit()\n"); + if (ch == 'd') + return control_sequence; + return {}; + } + + void Run() override { + if (m_python) { + int stdin_fd = GetInputFD(); + if (stdin_fd >= 0) { + Terminal terminal(stdin_fd); + TerminalState terminal_state(terminal); + + if (terminal.IsATerminal()) { + // FIXME: error handling? + llvm::consumeError(terminal.SetCanonical(false)); + llvm::consumeError(terminal.SetEcho(true)); + } + + ScriptInterpreterPythonImpl::Locker locker( + m_python, + ScriptInterpreterPythonImpl::Locker::AcquireLock | + ScriptInterpreterPythonImpl::Locker::InitSession | + ScriptInterpreterPythonImpl::Locker::InitGlobals, + ScriptInterpreterPythonImpl::Locker::FreeAcquiredLock | + ScriptInterpreterPythonImpl::Locker::TearDownSession); + + // The following call drops into the embedded interpreter loop and + // stays there until the user chooses to exit from the Python + // interpreter. This embedded interpreter will, as any Python code that + // performs I/O, unlock the GIL before a system call that can hang, and + // lock it when the syscall has returned. + + // We need to surround the call to the embedded interpreter with calls + // to PyGILState_Ensure and PyGILState_Release (using the Locker + // above). This is because Python has a global lock which must be held + // whenever we want to touch any Python objects. Otherwise, if the user + // calls Python code, the interpreter state will be off, and things + // could hang (it's happened before). + + StreamString run_string; + run_string.Printf("run_python_interpreter (%s)", + m_python->GetDictionaryName()); + PyRun_SimpleString(run_string.GetData()); + } + } + SetIsDone(true); + } + + void Cancel() override {} + + bool Interrupt() override { return m_python->Interrupt(); } + + void GotEOF() override {} + +protected: + ScriptInterpreterPythonImpl *m_python; +}; + +} // namespace lldb_private + +#endif // LLDB_ENABLE_PYTHON +#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHONIMPL_H diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/lldb-python.h b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/lldb-python.h new file mode 100644 index 000000000000..c99372fa110c --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/lldb-python.h @@ -0,0 +1,52 @@ +//===-- lldb-python.h -------------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_LLDB_PYTHON_H +#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_LLDB_PYTHON_H + +// BEGIN FIXME +// This declaration works around a clang module build failure. +// It should be deleted ASAP. +#include "llvm/Support/Error.h" +static llvm::Expected<bool> *g_fcxx_modules_workaround; +// END + +#include "lldb/Host/Config.h" + +// Python.h needs to be included before any system headers in order to avoid +// redefinition of macros + +#if LLDB_ENABLE_PYTHON +#include "llvm/Support/Compiler.h" +#if defined(_WIN32) +// If anyone #includes Host/PosixApi.h later, it will try to typedef pid_t. We +// need to ensure this doesn't happen. At the same time, Python.h will also try +// to redefine a bunch of stuff that PosixApi.h defines. So define it all now +// so that PosixApi.h doesn't redefine it. +#define NO_PID_T +#endif +#if defined(__linux__) +// features.h will define _POSIX_C_SOURCE if _GNU_SOURCE is defined. This value +// may be different from the value that Python defines it to be which results +// in a warning. Undefine _POSIX_C_SOURCE before including Python.h The same +// holds for _XOPEN_SOURCE. +#undef _POSIX_C_SOURCE +#undef _XOPEN_SOURCE +#endif + +// Include locale before Python so _PY_PORT_CTYPE_UTF8_ISSUE doesn't cause +// macro redefinitions. +#if defined(__APPLE__) +#include <locale> +#endif + +// Include python for non windows machines +#include <Python.h> +#endif + +#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_LLDB_PYTHON_H |