| 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 | LLDB_PLUGIN_DEFINE(ProcessTrace) |
| 24 | |
| 25 | llvm::StringRef ProcessTrace::GetPluginDescriptionStatic() { |
| 26 | return "Trace process plug-in." ; |
| 27 | } |
| 28 | |
| 29 | void ProcessTrace::Terminate() { |
| 30 | PluginManager::UnregisterPlugin(create_callback: ProcessTrace::CreateInstance); |
| 31 | } |
| 32 | |
| 33 | ProcessSP ProcessTrace::CreateInstance(TargetSP target_sp, |
| 34 | ListenerSP listener_sp, |
| 35 | const FileSpec *crash_file, |
| 36 | bool can_connect) { |
| 37 | if (can_connect) |
| 38 | return nullptr; |
| 39 | return std::make_shared<ProcessTrace>(args&: target_sp, args&: listener_sp, |
| 40 | args: crash_file ? *crash_file : FileSpec()); |
| 41 | } |
| 42 | |
| 43 | bool ProcessTrace::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) { |
| 44 | return plugin_specified_by_name; |
| 45 | } |
| 46 | |
| 47 | ProcessTrace::ProcessTrace(TargetSP target_sp, ListenerSP listener_sp, |
| 48 | const FileSpec &core_file) |
| 49 | : PostMortemProcess(target_sp, listener_sp, core_file) {} |
| 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(destructing: true /* destructing */); |
| 58 | } |
| 59 | |
| 60 | void ProcessTrace::DidAttach(ArchSpec &process_arch) { |
| 61 | ListenerSP listener_sp( |
| 62 | Listener::MakeListener(name: "lldb.process_trace.did_attach_listener" )); |
| 63 | HijackProcessEvents(listener_sp); |
| 64 | |
| 65 | SetCanJIT(false); |
| 66 | StartPrivateStateThread(); |
| 67 | SetPrivateState(eStateStopped); |
| 68 | |
| 69 | EventSP event_sp; |
| 70 | WaitForProcessToStop(timeout: std::nullopt, event_sp_ptr: &event_sp, wait_always: true, hijack_listener: listener_sp); |
| 71 | |
| 72 | RestoreProcessEvents(); |
| 73 | |
| 74 | Process::DidAttach(process_arch); |
| 75 | } |
| 76 | |
| 77 | bool ProcessTrace::DoUpdateThreadList(ThreadList &old_thread_list, |
| 78 | ThreadList &new_thread_list) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | void ProcessTrace::RefreshStateAfterStop() {} |
| 83 | |
| 84 | Status ProcessTrace::DoDestroy() { return Status(); } |
| 85 | |
| 86 | size_t ProcessTrace::ReadMemory(addr_t addr, void *buf, size_t size, |
| 87 | Status &error) { |
| 88 | if (const ABISP &abi = GetABI()) |
| 89 | addr = abi->FixAnyAddress(pc: addr); |
| 90 | |
| 91 | // Don't allow the caching that lldb_private::Process::ReadMemory does since |
| 92 | // we have it all cached in the trace files. |
| 93 | return DoReadMemory(addr, buf, size, error); |
| 94 | } |
| 95 | |
| 96 | void ProcessTrace::Clear() { m_thread_list.Clear(); } |
| 97 | |
| 98 | void ProcessTrace::Initialize() { |
| 99 | static llvm::once_flag g_once_flag; |
| 100 | |
| 101 | llvm::call_once(flag&: g_once_flag, F: []() { |
| 102 | PluginManager::RegisterPlugin(name: GetPluginNameStatic(), |
| 103 | description: GetPluginDescriptionStatic(), create_callback: CreateInstance); |
| 104 | }); |
| 105 | } |
| 106 | |
| 107 | ArchSpec ProcessTrace::GetArchitecture() { |
| 108 | return GetTarget().GetArchitecture(); |
| 109 | } |
| 110 | |
| 111 | bool ProcessTrace::GetProcessInfo(ProcessInstanceInfo &info) { |
| 112 | info.Clear(); |
| 113 | info.SetProcessID(GetID()); |
| 114 | info.SetArchitecture(GetArchitecture()); |
| 115 | ModuleSP module_sp = GetTarget().GetExecutableModule(); |
| 116 | if (module_sp) { |
| 117 | const bool add_exe_file_as_first_arg = false; |
| 118 | info.SetExecutableFile(exe_file: GetTarget().GetExecutableModule()->GetFileSpec(), |
| 119 | add_exe_file_as_first_arg); |
| 120 | } |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | size_t ProcessTrace::DoReadMemory(addr_t addr, void *buf, size_t size, |
| 125 | Status &error) { |
| 126 | Address resolved_address; |
| 127 | GetTarget().ResolveLoadAddress(load_addr: addr, so_addr&: resolved_address); |
| 128 | |
| 129 | return GetTarget().ReadMemoryFromFileCache(addr: resolved_address, dst: buf, dst_len: size, |
| 130 | error); |
| 131 | } |
| 132 | |