1 //===-- ProcessTrace.cpp --------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Target/ProcessTrace.h"
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"
21 using namespace lldb_private
;
23 llvm::StringRef
ProcessTrace::GetPluginDescriptionStatic() {
24 return "Trace process plug-in.";
27 void ProcessTrace::Terminate() {
28 PluginManager::UnregisterPlugin(ProcessTrace::CreateInstance
);
31 ProcessSP
ProcessTrace::CreateInstance(TargetSP target_sp
,
32 ListenerSP listener_sp
,
33 const FileSpec
*crash_file
,
37 return std::make_shared
<ProcessTrace
>(target_sp
, listener_sp
);
40 bool ProcessTrace::CanDebug(TargetSP target_sp
, bool plugin_specified_by_name
) {
41 return plugin_specified_by_name
;
44 ProcessTrace::ProcessTrace(TargetSP target_sp
, ListenerSP listener_sp
)
45 : PostMortemProcess(target_sp
, listener_sp
) {}
47 ProcessTrace::~ProcessTrace() {
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.
56 void ProcessTrace::DidAttach(ArchSpec
&process_arch
) {
57 ListenerSP
listener_sp(
58 Listener::MakeListener("lldb.process_trace.did_attach_listener"));
59 HijackProcessEvents(listener_sp
);
62 StartPrivateStateThread();
63 SetPrivateState(eStateStopped
);
66 WaitForProcessToStop(std::nullopt
, &event_sp
, true, listener_sp
);
68 RestoreProcessEvents();
70 Process::DidAttach(process_arch
);
73 bool ProcessTrace::DoUpdateThreadList(ThreadList
&old_thread_list
,
74 ThreadList
&new_thread_list
) {
78 void ProcessTrace::RefreshStateAfterStop() {}
80 Status
ProcessTrace::DoDestroy() { return Status(); }
82 size_t ProcessTrace::ReadMemory(addr_t addr
, void *buf
, size_t size
,
84 if (const ABISP
&abi
= GetABI())
85 addr
= abi
->FixAnyAddress(addr
);
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
);
92 void ProcessTrace::Clear() { m_thread_list
.Clear(); }
94 void ProcessTrace::Initialize() {
95 static llvm::once_flag g_once_flag
;
97 llvm::call_once(g_once_flag
, []() {
98 PluginManager::RegisterPlugin(GetPluginNameStatic(),
99 GetPluginDescriptionStatic(), CreateInstance
);
103 ArchSpec
ProcessTrace::GetArchitecture() {
104 return GetTarget().GetArchitecture();
107 bool ProcessTrace::GetProcessInfo(ProcessInstanceInfo
&info
) {
109 info
.SetProcessID(GetID());
110 info
.SetArchitecture(GetArchitecture());
111 ModuleSP module_sp
= GetTarget().GetExecutableModule();
113 const bool add_exe_file_as_first_arg
= false;
114 info
.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
115 add_exe_file_as_first_arg
);
120 size_t ProcessTrace::DoReadMemory(addr_t addr
, void *buf
, size_t size
,
122 Address resolved_address
;
123 GetTarget().GetSectionLoadList().ResolveLoadAddress(addr
, resolved_address
);
125 return GetTarget().ReadMemoryFromFileCache(resolved_address
, buf
, size
,