xref: /freebsd-src/contrib/llvm-project/lldb/source/Target/ProcessTrace.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1e8d8bef9SDimitry Andric //===-- ProcessTrace.cpp --------------------------------------------------===//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric 
9e8d8bef9SDimitry Andric #include "lldb/Target/ProcessTrace.h"
10e8d8bef9SDimitry Andric 
11e8d8bef9SDimitry Andric #include <memory>
12e8d8bef9SDimitry Andric 
13e8d8bef9SDimitry Andric #include "lldb/Core/Module.h"
14e8d8bef9SDimitry Andric #include "lldb/Core/PluginManager.h"
15e8d8bef9SDimitry Andric #include "lldb/Core/Section.h"
1681ad6265SDimitry Andric #include "lldb/Target/ABI.h"
17e8d8bef9SDimitry Andric #include "lldb/Target/SectionLoadList.h"
18e8d8bef9SDimitry Andric #include "lldb/Target/Target.h"
19e8d8bef9SDimitry Andric 
20e8d8bef9SDimitry Andric using namespace lldb;
21e8d8bef9SDimitry Andric using namespace lldb_private;
22e8d8bef9SDimitry Andric 
23297eecfbSDimitry Andric LLDB_PLUGIN_DEFINE(ProcessTrace)
24297eecfbSDimitry Andric 
25349cc55cSDimitry Andric llvm::StringRef ProcessTrace::GetPluginDescriptionStatic() {
26e8d8bef9SDimitry Andric   return "Trace process plug-in.";
27e8d8bef9SDimitry Andric }
28e8d8bef9SDimitry Andric 
29e8d8bef9SDimitry Andric void ProcessTrace::Terminate() {
30e8d8bef9SDimitry Andric   PluginManager::UnregisterPlugin(ProcessTrace::CreateInstance);
31e8d8bef9SDimitry Andric }
32e8d8bef9SDimitry Andric 
33e8d8bef9SDimitry Andric ProcessSP ProcessTrace::CreateInstance(TargetSP target_sp,
34e8d8bef9SDimitry Andric                                        ListenerSP listener_sp,
35e8d8bef9SDimitry Andric                                        const FileSpec *crash_file,
36e8d8bef9SDimitry Andric                                        bool can_connect) {
37e8d8bef9SDimitry Andric   if (can_connect)
38e8d8bef9SDimitry Andric     return nullptr;
39*0fca6ea1SDimitry Andric   return std::make_shared<ProcessTrace>(target_sp, listener_sp, *crash_file);
40e8d8bef9SDimitry Andric }
41e8d8bef9SDimitry Andric 
42e8d8bef9SDimitry Andric bool ProcessTrace::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) {
43e8d8bef9SDimitry Andric   return plugin_specified_by_name;
44e8d8bef9SDimitry Andric }
45e8d8bef9SDimitry Andric 
46*0fca6ea1SDimitry Andric ProcessTrace::ProcessTrace(TargetSP target_sp, ListenerSP listener_sp,
47*0fca6ea1SDimitry Andric                            const FileSpec &core_file)
48*0fca6ea1SDimitry Andric     : PostMortemProcess(target_sp, listener_sp, core_file) {}
49e8d8bef9SDimitry Andric 
50e8d8bef9SDimitry Andric ProcessTrace::~ProcessTrace() {
51e8d8bef9SDimitry Andric   Clear();
52e8d8bef9SDimitry Andric   // We need to call finalize on the process before destroying ourselves to
53e8d8bef9SDimitry Andric   // make sure all of the broadcaster cleanup goes as planned. If we destruct
54e8d8bef9SDimitry Andric   // this class, then Process::~Process() might have problems trying to fully
55e8d8bef9SDimitry Andric   // destroy the broadcaster.
565f757f3fSDimitry Andric   Finalize(true /* destructing */);
57e8d8bef9SDimitry Andric }
58e8d8bef9SDimitry Andric 
59e8d8bef9SDimitry Andric void ProcessTrace::DidAttach(ArchSpec &process_arch) {
60e8d8bef9SDimitry Andric   ListenerSP listener_sp(
61e8d8bef9SDimitry Andric       Listener::MakeListener("lldb.process_trace.did_attach_listener"));
62e8d8bef9SDimitry Andric   HijackProcessEvents(listener_sp);
63e8d8bef9SDimitry Andric 
64e8d8bef9SDimitry Andric   SetCanJIT(false);
65e8d8bef9SDimitry Andric   StartPrivateStateThread();
66e8d8bef9SDimitry Andric   SetPrivateState(eStateStopped);
67e8d8bef9SDimitry Andric 
68e8d8bef9SDimitry Andric   EventSP event_sp;
69bdd1243dSDimitry Andric   WaitForProcessToStop(std::nullopt, &event_sp, true, listener_sp);
70e8d8bef9SDimitry Andric 
71e8d8bef9SDimitry Andric   RestoreProcessEvents();
72e8d8bef9SDimitry Andric 
73e8d8bef9SDimitry Andric   Process::DidAttach(process_arch);
74e8d8bef9SDimitry Andric }
75e8d8bef9SDimitry Andric 
76e8d8bef9SDimitry Andric bool ProcessTrace::DoUpdateThreadList(ThreadList &old_thread_list,
77e8d8bef9SDimitry Andric                                       ThreadList &new_thread_list) {
78e8d8bef9SDimitry Andric   return false;
79e8d8bef9SDimitry Andric }
80e8d8bef9SDimitry Andric 
81e8d8bef9SDimitry Andric void ProcessTrace::RefreshStateAfterStop() {}
82e8d8bef9SDimitry Andric 
83e8d8bef9SDimitry Andric Status ProcessTrace::DoDestroy() { return Status(); }
84e8d8bef9SDimitry Andric 
85e8d8bef9SDimitry Andric size_t ProcessTrace::ReadMemory(addr_t addr, void *buf, size_t size,
86e8d8bef9SDimitry Andric                                 Status &error) {
8781ad6265SDimitry Andric   if (const ABISP &abi = GetABI())
8881ad6265SDimitry Andric     addr = abi->FixAnyAddress(addr);
8981ad6265SDimitry Andric 
90e8d8bef9SDimitry Andric   // Don't allow the caching that lldb_private::Process::ReadMemory does since
91e8d8bef9SDimitry Andric   // we have it all cached in the trace files.
92e8d8bef9SDimitry Andric   return DoReadMemory(addr, buf, size, error);
93e8d8bef9SDimitry Andric }
94e8d8bef9SDimitry Andric 
95e8d8bef9SDimitry Andric void ProcessTrace::Clear() { m_thread_list.Clear(); }
96e8d8bef9SDimitry Andric 
97e8d8bef9SDimitry Andric void ProcessTrace::Initialize() {
98e8d8bef9SDimitry Andric   static llvm::once_flag g_once_flag;
99e8d8bef9SDimitry Andric 
100e8d8bef9SDimitry Andric   llvm::call_once(g_once_flag, []() {
101e8d8bef9SDimitry Andric     PluginManager::RegisterPlugin(GetPluginNameStatic(),
102e8d8bef9SDimitry Andric                                   GetPluginDescriptionStatic(), CreateInstance);
103e8d8bef9SDimitry Andric   });
104e8d8bef9SDimitry Andric }
105e8d8bef9SDimitry Andric 
106e8d8bef9SDimitry Andric ArchSpec ProcessTrace::GetArchitecture() {
107e8d8bef9SDimitry Andric   return GetTarget().GetArchitecture();
108e8d8bef9SDimitry Andric }
109e8d8bef9SDimitry Andric 
110e8d8bef9SDimitry Andric bool ProcessTrace::GetProcessInfo(ProcessInstanceInfo &info) {
111e8d8bef9SDimitry Andric   info.Clear();
112e8d8bef9SDimitry Andric   info.SetProcessID(GetID());
113e8d8bef9SDimitry Andric   info.SetArchitecture(GetArchitecture());
114e8d8bef9SDimitry Andric   ModuleSP module_sp = GetTarget().GetExecutableModule();
115e8d8bef9SDimitry Andric   if (module_sp) {
116e8d8bef9SDimitry Andric     const bool add_exe_file_as_first_arg = false;
117e8d8bef9SDimitry Andric     info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
118e8d8bef9SDimitry Andric                            add_exe_file_as_first_arg);
119e8d8bef9SDimitry Andric   }
120e8d8bef9SDimitry Andric   return true;
121e8d8bef9SDimitry Andric }
122e8d8bef9SDimitry Andric 
123e8d8bef9SDimitry Andric size_t ProcessTrace::DoReadMemory(addr_t addr, void *buf, size_t size,
124e8d8bef9SDimitry Andric                                   Status &error) {
125e8d8bef9SDimitry Andric   Address resolved_address;
126e8d8bef9SDimitry Andric   GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, resolved_address);
127e8d8bef9SDimitry Andric 
128e8d8bef9SDimitry Andric   return GetTarget().ReadMemoryFromFileCache(resolved_address, buf, size,
129e8d8bef9SDimitry Andric                                              error);
130e8d8bef9SDimitry Andric }
131