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 void ProcessTrace::DidAttach(ArchSpec &process_arch) {
61   ListenerSP listener_sp(
62       Listener::MakeListener("lldb.process_trace.did_attach_listener"));
63   HijackProcessEvents(listener_sp);
64 
65   SetCanJIT(false);
66   StartPrivateStateThread();
67   SetPrivateState(eStateStopped);
68 
69   EventSP event_sp;
70   WaitForProcessToStop(llvm::None, &event_sp, true, listener_sp);
71 
72   RestoreProcessEvents();
73 
74   Process::DidAttach(process_arch);
75 }
76 
77 bool ProcessTrace::DoUpdateThreadList(ThreadList &old_thread_list,
78                                       ThreadList &new_thread_list) {
79   return false;
80 }
81 
82 void ProcessTrace::RefreshStateAfterStop() {}
83 
84 Status ProcessTrace::DoDestroy() { return Status(); }
85 
86 size_t ProcessTrace::ReadMemory(addr_t addr, void *buf, size_t size,
87                                 Status &error) {
88   // Don't allow the caching that lldb_private::Process::ReadMemory does since
89   // we have it all cached in the trace files.
90   return DoReadMemory(addr, buf, size, error);
91 }
92 
93 void ProcessTrace::Clear() { m_thread_list.Clear(); }
94 
95 void ProcessTrace::Initialize() {
96   static llvm::once_flag g_once_flag;
97 
98   llvm::call_once(g_once_flag, []() {
99     PluginManager::RegisterPlugin(GetPluginNameStatic(),
100                                   GetPluginDescriptionStatic(), CreateInstance);
101   });
102 }
103 
104 ArchSpec ProcessTrace::GetArchitecture() {
105   return GetTarget().GetArchitecture();
106 }
107 
108 bool ProcessTrace::GetProcessInfo(ProcessInstanceInfo &info) {
109   info.Clear();
110   info.SetProcessID(GetID());
111   info.SetArchitecture(GetArchitecture());
112   ModuleSP module_sp = GetTarget().GetExecutableModule();
113   if (module_sp) {
114     const bool add_exe_file_as_first_arg = false;
115     info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
116                            add_exe_file_as_first_arg);
117   }
118   return true;
119 }
120 
121 size_t ProcessTrace::DoReadMemory(addr_t addr, void *buf, size_t size,
122                                   Status &error) {
123   Address resolved_address;
124   GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, resolved_address);
125 
126   return GetTarget().ReadMemoryFromFileCache(resolved_address, buf, size,
127                                              error);
128 }
129