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