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

source code of lldb/source/Target/ProcessTrace.cpp