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