1e8d8bef9SDimitry Andric //===-- ProcessTrace.cpp --------------------------------------------------===// 2e8d8bef9SDimitry Andric // 3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e8d8bef9SDimitry Andric // 7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 8e8d8bef9SDimitry Andric 9e8d8bef9SDimitry Andric #include "lldb/Target/ProcessTrace.h" 10e8d8bef9SDimitry Andric 11e8d8bef9SDimitry Andric #include <memory> 12e8d8bef9SDimitry Andric 13e8d8bef9SDimitry Andric #include "lldb/Core/Module.h" 14e8d8bef9SDimitry Andric #include "lldb/Core/PluginManager.h" 15e8d8bef9SDimitry Andric #include "lldb/Core/Section.h" 1681ad6265SDimitry Andric #include "lldb/Target/ABI.h" 17e8d8bef9SDimitry Andric #include "lldb/Target/SectionLoadList.h" 18e8d8bef9SDimitry Andric #include "lldb/Target/Target.h" 19e8d8bef9SDimitry Andric 20e8d8bef9SDimitry Andric using namespace lldb; 21e8d8bef9SDimitry Andric using namespace lldb_private; 22e8d8bef9SDimitry Andric 23349cc55cSDimitry Andric llvm::StringRef ProcessTrace::GetPluginDescriptionStatic() { 24e8d8bef9SDimitry Andric return "Trace process plug-in."; 25e8d8bef9SDimitry Andric } 26e8d8bef9SDimitry Andric 27e8d8bef9SDimitry Andric void ProcessTrace::Terminate() { 28e8d8bef9SDimitry Andric PluginManager::UnregisterPlugin(ProcessTrace::CreateInstance); 29e8d8bef9SDimitry Andric } 30e8d8bef9SDimitry Andric 31e8d8bef9SDimitry Andric ProcessSP ProcessTrace::CreateInstance(TargetSP target_sp, 32e8d8bef9SDimitry Andric ListenerSP listener_sp, 33e8d8bef9SDimitry Andric const FileSpec *crash_file, 34e8d8bef9SDimitry Andric bool can_connect) { 35e8d8bef9SDimitry Andric if (can_connect) 36e8d8bef9SDimitry Andric return nullptr; 37e8d8bef9SDimitry Andric return std::make_shared<ProcessTrace>(target_sp, listener_sp); 38e8d8bef9SDimitry Andric } 39e8d8bef9SDimitry Andric 40e8d8bef9SDimitry Andric bool ProcessTrace::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) { 41e8d8bef9SDimitry Andric return plugin_specified_by_name; 42e8d8bef9SDimitry Andric } 43e8d8bef9SDimitry Andric 44e8d8bef9SDimitry Andric ProcessTrace::ProcessTrace(TargetSP target_sp, ListenerSP listener_sp) 45e8d8bef9SDimitry Andric : PostMortemProcess(target_sp, listener_sp) {} 46e8d8bef9SDimitry Andric 47e8d8bef9SDimitry Andric ProcessTrace::~ProcessTrace() { 48e8d8bef9SDimitry Andric Clear(); 49e8d8bef9SDimitry Andric // We need to call finalize on the process before destroying ourselves to 50e8d8bef9SDimitry Andric // make sure all of the broadcaster cleanup goes as planned. If we destruct 51e8d8bef9SDimitry Andric // this class, then Process::~Process() might have problems trying to fully 52e8d8bef9SDimitry Andric // destroy the broadcaster. 53*5f757f3fSDimitry Andric Finalize(true /* destructing */); 54e8d8bef9SDimitry Andric } 55e8d8bef9SDimitry Andric 56e8d8bef9SDimitry Andric void ProcessTrace::DidAttach(ArchSpec &process_arch) { 57e8d8bef9SDimitry Andric ListenerSP listener_sp( 58e8d8bef9SDimitry Andric Listener::MakeListener("lldb.process_trace.did_attach_listener")); 59e8d8bef9SDimitry Andric HijackProcessEvents(listener_sp); 60e8d8bef9SDimitry Andric 61e8d8bef9SDimitry Andric SetCanJIT(false); 62e8d8bef9SDimitry Andric StartPrivateStateThread(); 63e8d8bef9SDimitry Andric SetPrivateState(eStateStopped); 64e8d8bef9SDimitry Andric 65e8d8bef9SDimitry Andric EventSP event_sp; 66bdd1243dSDimitry Andric WaitForProcessToStop(std::nullopt, &event_sp, true, listener_sp); 67e8d8bef9SDimitry Andric 68e8d8bef9SDimitry Andric RestoreProcessEvents(); 69e8d8bef9SDimitry Andric 70e8d8bef9SDimitry Andric Process::DidAttach(process_arch); 71e8d8bef9SDimitry Andric } 72e8d8bef9SDimitry Andric 73e8d8bef9SDimitry Andric bool ProcessTrace::DoUpdateThreadList(ThreadList &old_thread_list, 74e8d8bef9SDimitry Andric ThreadList &new_thread_list) { 75e8d8bef9SDimitry Andric return false; 76e8d8bef9SDimitry Andric } 77e8d8bef9SDimitry Andric 78e8d8bef9SDimitry Andric void ProcessTrace::RefreshStateAfterStop() {} 79e8d8bef9SDimitry Andric 80e8d8bef9SDimitry Andric Status ProcessTrace::DoDestroy() { return Status(); } 81e8d8bef9SDimitry Andric 82e8d8bef9SDimitry Andric size_t ProcessTrace::ReadMemory(addr_t addr, void *buf, size_t size, 83e8d8bef9SDimitry Andric Status &error) { 8481ad6265SDimitry Andric if (const ABISP &abi = GetABI()) 8581ad6265SDimitry Andric addr = abi->FixAnyAddress(addr); 8681ad6265SDimitry Andric 87e8d8bef9SDimitry Andric // Don't allow the caching that lldb_private::Process::ReadMemory does since 88e8d8bef9SDimitry Andric // we have it all cached in the trace files. 89e8d8bef9SDimitry Andric return DoReadMemory(addr, buf, size, error); 90e8d8bef9SDimitry Andric } 91e8d8bef9SDimitry Andric 92e8d8bef9SDimitry Andric void ProcessTrace::Clear() { m_thread_list.Clear(); } 93e8d8bef9SDimitry Andric 94e8d8bef9SDimitry Andric void ProcessTrace::Initialize() { 95e8d8bef9SDimitry Andric static llvm::once_flag g_once_flag; 96e8d8bef9SDimitry Andric 97e8d8bef9SDimitry Andric llvm::call_once(g_once_flag, []() { 98e8d8bef9SDimitry Andric PluginManager::RegisterPlugin(GetPluginNameStatic(), 99e8d8bef9SDimitry Andric GetPluginDescriptionStatic(), CreateInstance); 100e8d8bef9SDimitry Andric }); 101e8d8bef9SDimitry Andric } 102e8d8bef9SDimitry Andric 103e8d8bef9SDimitry Andric ArchSpec ProcessTrace::GetArchitecture() { 104e8d8bef9SDimitry Andric return GetTarget().GetArchitecture(); 105e8d8bef9SDimitry Andric } 106e8d8bef9SDimitry Andric 107e8d8bef9SDimitry Andric bool ProcessTrace::GetProcessInfo(ProcessInstanceInfo &info) { 108e8d8bef9SDimitry Andric info.Clear(); 109e8d8bef9SDimitry Andric info.SetProcessID(GetID()); 110e8d8bef9SDimitry Andric info.SetArchitecture(GetArchitecture()); 111e8d8bef9SDimitry Andric ModuleSP module_sp = GetTarget().GetExecutableModule(); 112e8d8bef9SDimitry Andric if (module_sp) { 113e8d8bef9SDimitry Andric const bool add_exe_file_as_first_arg = false; 114e8d8bef9SDimitry Andric info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(), 115e8d8bef9SDimitry Andric add_exe_file_as_first_arg); 116e8d8bef9SDimitry Andric } 117e8d8bef9SDimitry Andric return true; 118e8d8bef9SDimitry Andric } 119e8d8bef9SDimitry Andric 120e8d8bef9SDimitry Andric size_t ProcessTrace::DoReadMemory(addr_t addr, void *buf, size_t size, 121e8d8bef9SDimitry Andric Status &error) { 122e8d8bef9SDimitry Andric Address resolved_address; 123e8d8bef9SDimitry Andric GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, resolved_address); 124e8d8bef9SDimitry Andric 125e8d8bef9SDimitry Andric return GetTarget().ReadMemoryFromFileCache(resolved_address, buf, size, 126e8d8bef9SDimitry Andric error); 127e8d8bef9SDimitry Andric } 128