diff --git a/lldb/include/lldb/Interpreter/ScriptInterpreter.h b/lldb/include/lldb/Interpreter/ScriptInterpreter.h --- a/lldb/include/lldb/Interpreter/ScriptInterpreter.h +++ b/lldb/include/lldb/Interpreter/ScriptInterpreter.h @@ -88,7 +88,8 @@ ScriptInterpreter( Debugger &debugger, lldb::ScriptLanguage script_lang, - lldb::ScriptedProcessInterfaceUP scripted_process_interface_up = {}); + lldb::ScriptedProcessInterfaceUP scripted_process_interface_up = + std::make_unique()); ~ScriptInterpreter() override = default; diff --git a/lldb/include/lldb/Target/Process.h b/lldb/include/lldb/Target/Process.h --- a/lldb/include/lldb/Target/Process.h +++ b/lldb/include/lldb/Target/Process.h @@ -2612,8 +2612,6 @@ virtual size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, Status &error) = 0; - void SetState(lldb::EventSP &event_sp); - lldb::StateType GetPrivateState(); /// The "private" side of resuming a process. This doesn't alter the state diff --git a/lldb/source/Plugins/Process/CMakeLists.txt b/lldb/source/Plugins/Process/CMakeLists.txt --- a/lldb/source/Plugins/Process/CMakeLists.txt +++ b/lldb/source/Plugins/Process/CMakeLists.txt @@ -12,6 +12,9 @@ elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin") add_subdirectory(MacOSX-Kernel) endif() +if (LLDB_ENABLE_PYTHON) + add_subdirectory(scripted) +endif() add_subdirectory(gdb-remote) add_subdirectory(Utility) add_subdirectory(elf-core) diff --git a/lldb/source/Plugins/Process/scripted/CMakeLists.txt b/lldb/source/Plugins/Process/scripted/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/lldb/source/Plugins/Process/scripted/CMakeLists.txt @@ -0,0 +1,13 @@ +add_lldb_library(lldbPluginScriptedProcess PLUGIN + ScriptedProcess.cpp + + LINK_LIBS + lldbCore + lldbTarget + lldbUtility + lldbPluginProcessUtility + LINK_COMPONENTS + BinaryFormat + Object + Support + ) diff --git a/lldb/source/Plugins/Process/scripted/ScriptedProcess.h b/lldb/source/Plugins/Process/scripted/ScriptedProcess.h new file mode 100644 --- /dev/null +++ b/lldb/source/Plugins/Process/scripted/ScriptedProcess.h @@ -0,0 +1,136 @@ +//===-- ScriptedProcess.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_SOURCE_PLUGINS_SCRIPTED_PROCESS_H +#define LLDB_SOURCE_PLUGINS_SCRIPTED_PROCESS_H + +#include "lldb/Target/Process.h" +#include "lldb/Utility/Broadcaster.h" +#include "lldb/Utility/ConstString.h" +#include "lldb/Utility/Status.h" + +#include + +namespace lldb_private { + +class ScriptedProcess : public Process { +protected: + class LaunchInfo { + public: + LaunchInfo(const ProcessLaunchInfo &launch_info) { + m_class_name = launch_info.GetScriptedProcessClassName(); + m_dictionary_sp = launch_info.GetScriptedProcessDictionarySP(); + } + + std::string GetClassName() const { return m_class_name; } + StructuredData::DictionarySP GetDictionarySP() const { + return m_dictionary_sp; + } + + private: + std::string m_class_name; + StructuredData::DictionarySP m_dictionary_sp; + }; + +public: + static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp, + lldb::ListenerSP listener_sp, + const FileSpec *crash_file_path, + bool can_connect); + + static void Initialize(); + + static void Terminate(); + + static ConstString GetPluginNameStatic(); + + static const char *GetPluginDescriptionStatic(); + + ScriptedProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, + const ScriptedProcess::LaunchInfo &launch_info); + + ~ScriptedProcess() override; + + bool CanDebug(lldb::TargetSP target_sp, + bool plugin_specified_by_name) override; + + DynamicLoader *GetDynamicLoader() override { return nullptr; } + + ConstString GetPluginName() override; + + uint32_t GetPluginVersion() override; + + SystemRuntime *GetSystemRuntime() override { return nullptr; } + + bool StartAsyncThread(); + + void StopAsyncThread(); + + static lldb::thread_result_t AsyncThread(void *arg); + + Status DoLoadCore() override; + + Status WillLaunch(Module *module) override; + + Status DoLaunch(Module *exe_module, ProcessLaunchInfo &launch_info) override; + + void DidLaunch() override; + + Status DoResume() override; + + Status DoDestroy() override; + + void RefreshStateAfterStop() override{}; + + bool IsAlive() override; + + size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size, + Status &error) override; + + size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size, + Status &error) override; + + ArchSpec GetArchitecture(); + + Status GetMemoryRegionInfo(lldb::addr_t load_addr, + MemoryRegionInfo &range_info) override; + + Status + GetMemoryRegions(lldb_private::MemoryRegionInfos ®ion_list) override; + + bool GetProcessInfo(ProcessInstanceInfo &info) override; + +protected: + void Clear(); + + bool DoUpdateThreadList(ThreadList &old_thread_list, + ThreadList &new_thread_list) override; + + lldb::StateType SendContinuePacketAndWaitForResponse(); + + /// Broadcaster event bits definitions. + enum { + eBroadcastBitAsyncContinue = (1 << 0), + eBroadcastBitAsyncThreadShouldExit = (1 << 1), + }; + +private: + ScriptedProcessInterface &GetInterface() const; + + const LaunchInfo m_launch_info; + lldb_private::ScriptInterpreter *m_interpreter = nullptr; + lldb_private::StructuredData::ObjectSP m_script_object_sp = nullptr; + Broadcaster m_async_broadcaster; + lldb::ListenerSP m_async_listener_sp; + HostThread m_async_thread; + std::recursive_mutex m_async_thread_state_mutex; +}; + +} // namespace lldb_private + +#endif // LLDB_SOURCE_PLUGINS_SCRIPTED_PROCESS_H diff --git a/lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp b/lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp new file mode 100644 --- /dev/null +++ b/lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp @@ -0,0 +1,443 @@ +//===-- ScriptedProcess.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 "ScriptedProcess.h" + +#include "lldb/Core/Debugger.h" +#include "lldb/Core/Module.h" +#include "lldb/Core/PluginManager.h" + +#include "lldb/Host/OptionParser.h" +#include "lldb/Host/ThreadLauncher.h" +#include "lldb/Interpreter/OptionArgParser.h" +#include "lldb/Interpreter/OptionGroupBoolean.h" +#include "lldb/Interpreter/ScriptInterpreter.h" +#include "lldb/Target/MemoryRegionInfo.h" +#include "lldb/Target/RegisterContext.h" + +#include "lldb/Utility/Log.h" +#include "lldb/Utility/Logging.h" +#include "lldb/Utility/State.h" + +#include + +LLDB_PLUGIN_DEFINE(ScriptedProcess) + +using namespace lldb; +using namespace lldb_private; + +ConstString ScriptedProcess::GetPluginNameStatic() { + static ConstString g_name("ScriptedProcess"); + return g_name; +} + +const char *ScriptedProcess::GetPluginDescriptionStatic() { + return "Scripted Process plug-in."; +} + +lldb::ProcessSP ScriptedProcess::CreateInstance(lldb::TargetSP target_sp, + lldb::ListenerSP listener_sp, + const FileSpec *file, + bool can_connect) { + ScriptedProcess::LaunchInfo launch_info(target_sp->GetProcessLaunchInfo()); + + auto process_sp = + std::make_shared(target_sp, listener_sp, launch_info); + + if (!process_sp || !process_sp->m_script_object_sp || + !process_sp->m_script_object_sp->IsValid()) + return nullptr; + + return process_sp; +} + +bool ScriptedProcess::CanDebug(lldb::TargetSP target_sp, + bool plugin_specified_by_name) { + return true; +} + +ScriptedProcess::ScriptedProcess(lldb::TargetSP target_sp, + lldb::ListenerSP listener_sp, + const ScriptedProcess::LaunchInfo &launch_info) + : Process(target_sp, listener_sp), m_launch_info(launch_info), + m_async_broadcaster(nullptr, + "lldb.process.scripted-process.async-broadcaster"), + m_async_listener_sp(Listener::MakeListener( + "lldb.process.scripted-process.async-listener")), + m_async_thread_state_mutex() { + if (!target_sp) + return; + + m_interpreter = target_sp->GetDebugger().GetScriptInterpreter(); + + if (!m_interpreter) + return; + + StructuredData::ObjectSP object_sp = GetInterface().CreatePluginObject( + m_launch_info.GetClassName().c_str(), target_sp, + m_launch_info.GetDictionarySP()); + + if (object_sp && object_sp->IsValid()) + m_script_object_sp = object_sp; + + m_async_broadcaster.SetEventName(eBroadcastBitAsyncThreadShouldExit, + "async thread should exit"); + m_async_broadcaster.SetEventName(eBroadcastBitAsyncContinue, + "async thread continue"); + + Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + + const uint32_t async_event_mask = + eBroadcastBitAsyncContinue | eBroadcastBitAsyncThreadShouldExit; + + if (m_async_listener_sp->StartListeningForEvents( + &m_async_broadcaster, async_event_mask) != async_event_mask) { + LLDB_LOGF( + log, + "ScriptedProcess::%s failed to listen for m_async_broadcaster events", + __FUNCTION__); + + m_script_object_sp = nullptr; + } +} + +ScriptedProcess::~ScriptedProcess() { + StopAsyncThread(); + Clear(); + // We need to call finalize on the process before destroying ourselves to + // make sure all of the broadcaster cleanup goes as planned. If we destruct + // this class, then Process::~Process() might have problems trying to fully + // destroy the broadcaster. + Finalize(); +} + +void ScriptedProcess::Initialize() { + static llvm::once_flag g_once_flag; + + llvm::call_once(g_once_flag, []() { + PluginManager::RegisterPlugin(GetPluginNameStatic(), + GetPluginDescriptionStatic(), CreateInstance); + }); +} + +void ScriptedProcess::Terminate() { + PluginManager::UnregisterPlugin(ScriptedProcess::CreateInstance); +} + +ConstString ScriptedProcess::GetPluginName() { return GetPluginNameStatic(); } + +uint32_t ScriptedProcess::GetPluginVersion() { return 1; } + +bool ScriptedProcess::StartAsyncThread() { + Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + + LLDB_LOGF(log, "ScriptedProcess::%s ()", __FUNCTION__); + + std::lock_guard guard(m_async_thread_state_mutex); + if (m_async_thread.IsJoinable()) { + LLDB_LOGF(log, + "ScriptedProcess::%s () - Called when Async thread was " + "already running.", + __FUNCTION__); + return true; + } + + llvm::Expected async_thread = + ThreadLauncher::LaunchThread("", + ScriptedProcess::AsyncThread, this); + + if (!async_thread) { + LLDB_LOG_ERROR(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST), + async_thread.takeError(), + "failed to launch host thread: {}"); + return false; + } + + m_async_thread = *async_thread; + return m_async_thread.IsJoinable(); +} + +void ScriptedProcess::StopAsyncThread() { + Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + + LLDB_LOGF(log, "ScriptedProcess::%s ()", __FUNCTION__); + + std::lock_guard guard(m_async_thread_state_mutex); + + m_async_broadcaster.BroadcastEvent(eBroadcastBitAsyncThreadShouldExit); + + if (m_async_thread.IsJoinable()) { + m_async_thread.Join(nullptr); + m_async_thread.Reset(); + } else + LLDB_LOGF( + log, + "ScriptedProcess::%s () - Called when Async thread was not running.", + __FUNCTION__); +} + +thread_result_t ScriptedProcess::AsyncThread(void *arg) { + ScriptedProcess *process = static_cast(arg); + + const lldb::pid_t pid = process->GetID(); + + Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + LLDB_LOGF(log, + "ScriptedProcess::%s (arg = %p, pid = %" PRIu64 + ") thread starting...", + __FUNCTION__, arg, pid); + + EventSP event_sp; + bool done = false; + while (!done) { + LLDB_LOGF(log, + "ScriptedProcess::%s (arg = %p, pid = %" PRIu64 + ") listener.WaitForEvent (NULL, event_sp)...", + __FUNCTION__, arg, pid); + if (process->m_async_listener_sp->GetEvent(event_sp, llvm::None)) { + const uint32_t event_type = event_sp->GetType(); + bool is_running = false; + do { + switch (event_type) { + case eBroadcastBitAsyncContinue: { + LLDB_LOGF(log, + "ScriptedProcess::%s (arg = %p, pid = %" PRIu64 + ") got eBroadcastBitAsyncContinue", + __FUNCTION__, arg, pid); + + is_running = false; + process->SetPrivateState(eStateStopped); + + } break; + + case eBroadcastBitAsyncThreadShouldExit: { + LLDB_LOGF(log, + "ScriptedProcess::%s (arg = %p, pid = %" PRIu64 + ") got eBroadcastBitAsyncThreadShouldExit...", + __FUNCTION__, arg, pid); + done = true; + is_running = false; + } break; + + default: { + LLDB_LOGF(log, + "ScriptedProcess::%s (arg = %p, pid = %" PRIu64 + ") got unknown event 0x%8.8x", + __FUNCTION__, arg, pid, event_type); + done = true; + is_running = false; + } break; + } + } while (is_running); // do while + } else { + LLDB_LOGF(log, + "ScriptedProcess::AsyncThread (pid = %" PRIu64 + ") listener.WaitForEvent (NULL, event_sp) => false", + pid); + done = true; + } + } + + LLDB_LOGF(log, + "ScriptedProcess::%s (arg = %p, pid = %" PRIu64 + ") thread exiting...", + __FUNCTION__, arg, pid); + + return {}; +} + +StateType ScriptedProcess::SendContinuePacketAndWaitForResponse() { + Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + + LLDB_LOGF(log, "ScriptedProcess::%s ()", __FUNCTION__); + + m_async_broadcaster.BroadcastEvent(eBroadcastBitAsyncThreadShouldExit); + + return eStateStopped; +} + +Status ScriptedProcess::DoLoadCore() { + ProcessLaunchInfo launch_info = GetTarget().GetProcessLaunchInfo(); + + return DoLaunch(nullptr, launch_info); +} + +Status ScriptedProcess::WillLaunch(Module *module) { + if (m_interpreter) + m_pid = GetInterface().GetProcessID(); + return Status(); +} + +Status ScriptedProcess::DoLaunch(Module *exe_module, + ProcessLaunchInfo &launch_info) { + if (!m_interpreter) + return Status("No interpreter."); + + if (!m_script_object_sp) + return Status("No python object."); + + if (!m_async_thread.IsJoinable()) + StartAsyncThread(); + + SetPrivateState(eStateLaunching); + + Status status = GetInterface().Launch(); + + if (status.Fail()) + return status; + + m_async_broadcaster.BroadcastEvent(eBroadcastBitAsyncContinue); + + UpdateThreadListIfNeeded(); + GetThreadList(); + + return {}; +} + +void ScriptedProcess::DidLaunch() { + if (m_interpreter) + m_pid = GetInterface().GetProcessID(); +} + +Status ScriptedProcess::DoResume() { + Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + + if (!m_interpreter) + return Status("No interpreter."); + + if (!m_script_object_sp) + return Status("No python object."); + + bool resume = false; + Status status; + + // FIXME: Fetch data from thread. + const StateType thread_resume_state = eStateRunning; + + LLDB_LOGF(log, "ScriptedProcess::%s thread_resume_state = %s", __FUNCTION__, + StateAsCString(thread_resume_state)); + + if (thread_resume_state == eStateRunning) + resume = true; + else + // The only valid thread resume states are listed above + llvm_unreachable("invalid thread resume state"); + + if (resume) { + LLDB_LOGF(log, "ScriptedProcess::%s sending resume", __FUNCTION__); + + status = GetInterface().Resume(); + + if (status.Success()) { + SetPrivateState(eStateRunning); + m_async_broadcaster.BroadcastEvent(eBroadcastBitAsyncContinue); + } else + status.SetErrorString("ScriptedProcess resume failed"); + } else { + status.SetErrorString("thread is suspended"); + } + + return status; +} + +Status ScriptedProcess::DoDestroy() { return Status(); } + +bool ScriptedProcess::IsAlive() { + if (!m_interpreter) + return false; + + return GetInterface().IsAlive(); +} + +size_t ScriptedProcess::ReadMemory(lldb::addr_t addr, void *buf, size_t size, + Status &error) { + return DoReadMemory(addr, buf, size, error); +} + +size_t ScriptedProcess::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, + Status &error) { + + auto error_with_message = [&error](llvm::StringRef message) { + error.SetErrorString(message); + return LLDB_INVALID_ADDRESS; + }; + + if (!m_interpreter) + return error_with_message("No interpreter."); + + lldb::DataExtractorSP data_extractor_sp = + GetInterface().ReadMemoryAtAddress(addr, size, error); + + if (!data_extractor_sp || error.Fail()) + return LLDB_INVALID_ADDRESS; + + if (data_extractor_sp->GetByteSize() != size) + return error_with_message("Failed to read requested memory size."); + + offset_t bytes_copied = data_extractor_sp->CopyByteOrderedData( + 0, size, buf, size, GetByteOrder()); + + if (!bytes_copied || bytes_copied == LLDB_INVALID_OFFSET) + return error_with_message("Failed to copy read memory to buffer."); + + return size; +} + +ArchSpec ScriptedProcess::GetArchitecture() { + return GetTarget().GetArchitecture(); +} + +Status ScriptedProcess::GetMemoryRegionInfo(lldb::addr_t load_addr, + MemoryRegionInfo ®ion) { + return Status(); +} + +Status ScriptedProcess::GetMemoryRegions(MemoryRegionInfos ®ion_list) { + Status error; + + if (!m_interpreter) { + error.SetErrorString("No interpreter."); + return error; + } + + lldb::addr_t address = 0; + lldb::MemoryRegionInfoSP mem_region_sp = nullptr; + + while ((mem_region_sp = + GetInterface().GetMemoryRegionContainingAddress(address))) { + auto range = mem_region_sp->GetRange(); + address += range.GetRangeBase() + range.GetByteSize(); + region_list.push_back(*mem_region_sp.get()); + } + + return error; +} + +void ScriptedProcess::Clear() { Process::m_thread_list.Clear(); } + +bool ScriptedProcess::DoUpdateThreadList(ThreadList &old_thread_list, + ThreadList &new_thread_list) { + return new_thread_list.GetSize(false) > 0; +} + +bool ScriptedProcess::GetProcessInfo(ProcessInstanceInfo &info) { + info.Clear(); + info.SetProcessID(GetID()); + info.SetArchitecture(GetArchitecture()); + lldb::ModuleSP module_sp = GetTarget().GetExecutableModule(); + if (module_sp) { + const bool add_exe_file_as_first_arg = false; + info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(), + add_exe_file_as_first_arg); + } + return true; +} + +ScriptedProcessInterface &ScriptedProcess::GetInterface() const { + return m_interpreter->GetScriptedProcessInterface(); +} diff --git a/lldb/source/Target/Target.cpp b/lldb/source/Target/Target.cpp --- a/lldb/source/Target/Target.cpp +++ b/lldb/source/Target/Target.cpp @@ -2972,7 +2972,7 @@ // If we're not already connected to the process, and if we have a platform // that can launch a process for debugging, go ahead and do that here. if (state != eStateConnected && platform_sp && - platform_sp->CanDebugProcess()) { + platform_sp->CanDebugProcess() && !launch_info.IsScriptedProcess()) { LLDB_LOGF(log, "Target::%s asking the platform to debug the process", __FUNCTION__); diff --git a/lldb/test/API/functionalities/scripted_process/TestScriptedProcess.py b/lldb/test/API/functionalities/scripted_process/TestScriptedProcess.py --- a/lldb/test/API/functionalities/scripted_process/TestScriptedProcess.py +++ b/lldb/test/API/functionalities/scripted_process/TestScriptedProcess.py @@ -11,7 +11,7 @@ from lldbsuite.test import lldbtest -class PlatformProcessCrashInfoTestCase(TestBase): +class ScriptedProcesTestCase(TestBase): mydir = TestBase.compute_mydir(__file__) @@ -43,3 +43,55 @@ self.expect('script dir(ScriptedProcess)', substrs=["launch"]) + def test_launch_scripted_process_sbapi(self): + """Test that we can launch an lldb scripted process using the SBAPI, + check its process ID and read string from memory.""" + self.build() + target = self.dbg.CreateTarget(self.getBuildArtifact("a.out")) + self.assertTrue(target, VALID_TARGET) + + scripted_process_example_relpath = ['..','..','..','..','examples','python','scripted_process','my_scripted_process.py'] + os.environ['SKIP_SCRIPTED_PROCESS_LAUNCH'] = '1' + self.runCmd("command script import " + os.path.join(self.getSourceDir(), + *scripted_process_example_relpath)) + + launch_info = lldb.SBLaunchInfo(None) + launch_info.SetProcessPluginName("ScriptedProcess") + launch_info.SetScriptedProcessClassName("my_scripted_process.MyScriptedProcess") + + error = lldb.SBError() + process = target.Launch(launch_info, error) + self.assertTrue(process and process.IsValid(), PROCESS_IS_VALID) + self.assertEqual(process.GetProcessID(), 42) + + hello_world = "Hello, world!" + memory_read = process.ReadCStringFromMemory(0x50000000000, + len(hello_world) + 1, # NULL byte + error) + + self.assertTrue(error.Success(), "Failed to read memory from scripted process.") + self.assertEqual(hello_world, memory_read) + + def test_launch_scripted_process_cli(self): + """Test that we can launch an lldb scripted process from the command + line, check its process ID and read string from memory.""" + self.build() + target = self.dbg.CreateTarget(self.getBuildArtifact("a.out")) + self.assertTrue(target, VALID_TARGET) + + scripted_process_example_relpath = ['..','..','..','..','examples','python','scripted_process','my_scripted_process.py'] + self.runCmd("command script import " + os.path.join(self.getSourceDir(), + *scripted_process_example_relpath)) + + process = target.GetProcess() + self.assertTrue(process, PROCESS_IS_VALID) + self.assertEqual(process.GetProcessID(), 42) + + error = lldb.SBError() + hello_world = "Hello, world!" + memory_read = process.ReadCStringFromMemory(0x50000000000, + len(hello_world) + 1, # NULL byte + error) + + self.assertTrue(error.Success(), "Failed to read memory from scripted process.") + self.assertEqual(hello_world, memory_read)