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