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