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/SectionLoadList.h"
17 #include "lldb/Target/Target.h"
18
19 using namespace lldb;
20 using namespace lldb_private;
21
GetPluginNameStatic()22 ConstString ProcessTrace::GetPluginNameStatic() {
23 static ConstString g_name("trace");
24 return g_name;
25 }
26
GetPluginDescriptionStatic()27 const char *ProcessTrace::GetPluginDescriptionStatic() {
28 return "Trace process plug-in.";
29 }
30
Terminate()31 void ProcessTrace::Terminate() {
32 PluginManager::UnregisterPlugin(ProcessTrace::CreateInstance);
33 }
34
CreateInstance(TargetSP target_sp,ListenerSP listener_sp,const FileSpec * crash_file)35 ProcessSP ProcessTrace::CreateInstance(TargetSP target_sp,
36 ListenerSP listener_sp,
37 const FileSpec *crash_file) {
38 return std::make_shared<ProcessTrace>(target_sp, listener_sp);
39 }
40
CanDebug(TargetSP target_sp,bool plugin_specified_by_name)41 bool ProcessTrace::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) {
42 return plugin_specified_by_name;
43 }
44
ProcessTrace(TargetSP target_sp,ListenerSP listener_sp)45 ProcessTrace::ProcessTrace(TargetSP target_sp, ListenerSP listener_sp)
46 : Process(target_sp, listener_sp) {}
47
~ProcessTrace()48 ProcessTrace::~ProcessTrace() {
49 Clear();
50 // We need to call finalize on the process before destroying ourselves to
51 // make sure all of the broadcaster cleanup goes as planned. If we destruct
52 // this class, then Process::~Process() might have problems trying to fully
53 // destroy the broadcaster.
54 Finalize();
55 }
56
GetPluginName()57 ConstString ProcessTrace::GetPluginName() { return GetPluginNameStatic(); }
58
GetPluginVersion()59 uint32_t ProcessTrace::GetPluginVersion() { return 1; }
60
DidAttach(ArchSpec & process_arch)61 void ProcessTrace::DidAttach(ArchSpec &process_arch) {
62 ListenerSP listener_sp(
63 Listener::MakeListener("lldb.process_trace.did_attach_listener"));
64 HijackProcessEvents(listener_sp);
65
66 SetCanJIT(false);
67 StartPrivateStateThread();
68 SetPrivateState(eStateStopped);
69
70 EventSP event_sp;
71 WaitForProcessToStop(llvm::None, &event_sp, true, listener_sp);
72
73 RestoreProcessEvents();
74
75 Process::DidAttach(process_arch);
76 }
77
UpdateThreadList(ThreadList & old_thread_list,ThreadList & new_thread_list)78 bool ProcessTrace::UpdateThreadList(ThreadList &old_thread_list,
79 ThreadList &new_thread_list) {
80 return false;
81 }
82
RefreshStateAfterStop()83 void ProcessTrace::RefreshStateAfterStop() {}
84
DoDestroy()85 Status ProcessTrace::DoDestroy() { return Status(); }
86
IsAlive()87 bool ProcessTrace::IsAlive() { return true; }
88
ReadMemory(addr_t addr,void * buf,size_t size,Status & error)89 size_t ProcessTrace::ReadMemory(addr_t addr, void *buf, size_t size,
90 Status &error) {
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
Clear()96 void ProcessTrace::Clear() { m_thread_list.Clear(); }
97
Initialize()98 void ProcessTrace::Initialize() {
99 static llvm::once_flag g_once_flag;
100
101 llvm::call_once(g_once_flag, []() {
102 PluginManager::RegisterPlugin(GetPluginNameStatic(),
103 GetPluginDescriptionStatic(), CreateInstance);
104 });
105 }
106
GetArchitecture()107 ArchSpec ProcessTrace::GetArchitecture() {
108 return GetTarget().GetArchitecture();
109 }
110
GetProcessInfo(ProcessInstanceInfo & info)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(GetTarget().GetExecutableModule()->GetFileSpec(),
119 add_exe_file_as_first_arg);
120 }
121 return true;
122 }
123
DoReadMemory(addr_t addr,void * buf,size_t size,Status & error)124 size_t ProcessTrace::DoReadMemory(addr_t addr, void *buf, size_t size,
125 Status &error) {
126 Address resolved_address;
127 GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, resolved_address);
128
129 return GetTarget().ReadMemoryFromFileCache(resolved_address, buf, size,
130 error);
131 }
132