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 LLDB_PLUGIN_DEFINE(ProcessTrace
)
25 llvm::StringRef
ProcessTrace::GetPluginDescriptionStatic() {
26 return "Trace process plug-in.";
29 void ProcessTrace::Terminate() {
30 PluginManager::UnregisterPlugin(ProcessTrace::CreateInstance
);
33 ProcessSP
ProcessTrace::CreateInstance(TargetSP target_sp
,
34 ListenerSP listener_sp
,
35 const FileSpec
*crash_file
,
39 return std::make_shared
<ProcessTrace
>(target_sp
, listener_sp
, *crash_file
);
42 bool ProcessTrace::CanDebug(TargetSP target_sp
, bool plugin_specified_by_name
) {
43 return plugin_specified_by_name
;
46 ProcessTrace::ProcessTrace(TargetSP target_sp
, ListenerSP listener_sp
,
47 const FileSpec
&core_file
)
48 : PostMortemProcess(target_sp
, listener_sp
, core_file
) {}
50 ProcessTrace::~ProcessTrace() {
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(true /* destructing */);
59 void ProcessTrace::DidAttach(ArchSpec
&process_arch
) {
60 ListenerSP
listener_sp(
61 Listener::MakeListener("lldb.process_trace.did_attach_listener"));
62 HijackProcessEvents(listener_sp
);
65 StartPrivateStateThread();
66 SetPrivateState(eStateStopped
);
69 WaitForProcessToStop(std::nullopt
, &event_sp
, true, listener_sp
);
71 RestoreProcessEvents();
73 Process::DidAttach(process_arch
);
76 bool ProcessTrace::DoUpdateThreadList(ThreadList
&old_thread_list
,
77 ThreadList
&new_thread_list
) {
81 void ProcessTrace::RefreshStateAfterStop() {}
83 Status
ProcessTrace::DoDestroy() { return Status(); }
85 size_t ProcessTrace::ReadMemory(addr_t addr
, void *buf
, size_t size
,
87 if (const ABISP
&abi
= GetABI())
88 addr
= abi
->FixAnyAddress(addr
);
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
);
95 void ProcessTrace::Clear() { m_thread_list
.Clear(); }
97 void ProcessTrace::Initialize() {
98 static llvm::once_flag g_once_flag
;
100 llvm::call_once(g_once_flag
, []() {
101 PluginManager::RegisterPlugin(GetPluginNameStatic(),
102 GetPluginDescriptionStatic(), CreateInstance
);
106 ArchSpec
ProcessTrace::GetArchitecture() {
107 return GetTarget().GetArchitecture();
110 bool ProcessTrace::GetProcessInfo(ProcessInstanceInfo
&info
) {
112 info
.SetProcessID(GetID());
113 info
.SetArchitecture(GetArchitecture());
114 ModuleSP module_sp
= GetTarget().GetExecutableModule();
116 const bool add_exe_file_as_first_arg
= false;
117 info
.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
118 add_exe_file_as_first_arg
);
123 size_t ProcessTrace::DoReadMemory(addr_t addr
, void *buf
, size_t size
,
125 Address resolved_address
;
126 GetTarget().GetSectionLoadList().ResolveLoadAddress(addr
, resolved_address
);
128 return GetTarget().ReadMemoryFromFileCache(resolved_address
, buf
, size
,