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