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,bool can_connect)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 
CanDebug(TargetSP target_sp,bool plugin_specified_by_name)44 bool ProcessTrace::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) {
45   return plugin_specified_by_name;
46 }
47 
ProcessTrace(TargetSP target_sp,ListenerSP listener_sp)48 ProcessTrace::ProcessTrace(TargetSP target_sp, ListenerSP listener_sp)
49     : PostMortemProcess(target_sp, listener_sp) {}
50 
~ProcessTrace()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 
GetPluginName()60 ConstString ProcessTrace::GetPluginName() { return GetPluginNameStatic(); }
61 
GetPluginVersion()62 uint32_t ProcessTrace::GetPluginVersion() { return 1; }
63 
DidAttach(ArchSpec & process_arch)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 
DoUpdateThreadList(ThreadList & old_thread_list,ThreadList & new_thread_list)81 bool ProcessTrace::DoUpdateThreadList(ThreadList &old_thread_list,
82                                       ThreadList &new_thread_list) {
83   return false;
84 }
85 
RefreshStateAfterStop()86 void ProcessTrace::RefreshStateAfterStop() {}
87 
DoDestroy()88 Status ProcessTrace::DoDestroy() { return Status(); }
89 
ReadMemory(addr_t addr,void * buf,size_t size,Status & error)90 size_t ProcessTrace::ReadMemory(addr_t addr, void *buf, size_t size,
91                                 Status &error) {
92   // Don't allow the caching that lldb_private::Process::ReadMemory does since
93   // we have it all cached in the trace files.
94   return DoReadMemory(addr, buf, size, error);
95 }
96 
Clear()97 void ProcessTrace::Clear() { m_thread_list.Clear(); }
98 
Initialize()99 void ProcessTrace::Initialize() {
100   static llvm::once_flag g_once_flag;
101 
102   llvm::call_once(g_once_flag, []() {
103     PluginManager::RegisterPlugin(GetPluginNameStatic(),
104                                   GetPluginDescriptionStatic(), CreateInstance);
105   });
106 }
107 
GetArchitecture()108 ArchSpec ProcessTrace::GetArchitecture() {
109   return GetTarget().GetArchitecture();
110 }
111 
GetProcessInfo(ProcessInstanceInfo & info)112 bool ProcessTrace::GetProcessInfo(ProcessInstanceInfo &info) {
113   info.Clear();
114   info.SetProcessID(GetID());
115   info.SetArchitecture(GetArchitecture());
116   ModuleSP module_sp = GetTarget().GetExecutableModule();
117   if (module_sp) {
118     const bool add_exe_file_as_first_arg = false;
119     info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
120                            add_exe_file_as_first_arg);
121   }
122   return true;
123 }
124 
DoReadMemory(addr_t addr,void * buf,size_t size,Status & error)125 size_t ProcessTrace::DoReadMemory(addr_t addr, void *buf, size_t size,
126                                   Status &error) {
127   Address resolved_address;
128   GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, resolved_address);
129 
130   return GetTarget().ReadMemoryFromFileCache(resolved_address, buf, size,
131                                              error);
132 }
133