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