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