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 
LLDB_PLUGIN_DEFINE(ProcessTrace)23*297eecfbSDimitry Andric LLDB_PLUGIN_DEFINE(ProcessTrace)
24*297eecfbSDimitry Andric 
25349cc55cSDimitry Andric llvm::StringRef ProcessTrace::GetPluginDescriptionStatic() {
26e8d8bef9SDimitry Andric   return "Trace process plug-in.";
27e8d8bef9SDimitry Andric }
28e8d8bef9SDimitry Andric 
Terminate()29e8d8bef9SDimitry Andric void ProcessTrace::Terminate() {
30e8d8bef9SDimitry Andric   PluginManager::UnregisterPlugin(ProcessTrace::CreateInstance);
31e8d8bef9SDimitry Andric }
32e8d8bef9SDimitry Andric 
CreateInstance(TargetSP target_sp,ListenerSP listener_sp,const FileSpec * crash_file,bool can_connect)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;
39e8d8bef9SDimitry Andric   return std::make_shared<ProcessTrace>(target_sp, listener_sp);
40e8d8bef9SDimitry Andric }
41e8d8bef9SDimitry Andric 
CanDebug(TargetSP target_sp,bool plugin_specified_by_name)42e8d8bef9SDimitry Andric bool ProcessTrace::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) {
43e8d8bef9SDimitry Andric   return plugin_specified_by_name;
44e8d8bef9SDimitry Andric }
45e8d8bef9SDimitry Andric 
ProcessTrace(TargetSP target_sp,ListenerSP listener_sp)46e8d8bef9SDimitry Andric ProcessTrace::ProcessTrace(TargetSP target_sp, ListenerSP listener_sp)
47e8d8bef9SDimitry Andric     : PostMortemProcess(target_sp, listener_sp) {}
48e8d8bef9SDimitry Andric 
~ProcessTrace()49e8d8bef9SDimitry Andric ProcessTrace::~ProcessTrace() {
50e8d8bef9SDimitry Andric   Clear();
51e8d8bef9SDimitry Andric   // We need to call finalize on the process before destroying ourselves to
52e8d8bef9SDimitry Andric   // make sure all of the broadcaster cleanup goes as planned. If we destruct
53e8d8bef9SDimitry Andric   // this class, then Process::~Process() might have problems trying to fully
54e8d8bef9SDimitry Andric   // destroy the broadcaster.
555f757f3fSDimitry Andric   Finalize(true /* destructing */);
56e8d8bef9SDimitry Andric }
57e8d8bef9SDimitry Andric 
DidAttach(ArchSpec & process_arch)58e8d8bef9SDimitry Andric void ProcessTrace::DidAttach(ArchSpec &process_arch) {
59e8d8bef9SDimitry Andric   ListenerSP listener_sp(
60e8d8bef9SDimitry Andric       Listener::MakeListener("lldb.process_trace.did_attach_listener"));
61e8d8bef9SDimitry Andric   HijackProcessEvents(listener_sp);
62e8d8bef9SDimitry Andric 
63e8d8bef9SDimitry Andric   SetCanJIT(false);
64e8d8bef9SDimitry Andric   StartPrivateStateThread();
65e8d8bef9SDimitry Andric   SetPrivateState(eStateStopped);
66e8d8bef9SDimitry Andric 
67e8d8bef9SDimitry Andric   EventSP event_sp;
68bdd1243dSDimitry Andric   WaitForProcessToStop(std::nullopt, &event_sp, true, listener_sp);
69e8d8bef9SDimitry Andric 
70e8d8bef9SDimitry Andric   RestoreProcessEvents();
71e8d8bef9SDimitry Andric 
72e8d8bef9SDimitry Andric   Process::DidAttach(process_arch);
73e8d8bef9SDimitry Andric }
74e8d8bef9SDimitry Andric 
DoUpdateThreadList(ThreadList & old_thread_list,ThreadList & new_thread_list)75e8d8bef9SDimitry Andric bool ProcessTrace::DoUpdateThreadList(ThreadList &old_thread_list,
76e8d8bef9SDimitry Andric                                       ThreadList &new_thread_list) {
77e8d8bef9SDimitry Andric   return false;
78e8d8bef9SDimitry Andric }
79e8d8bef9SDimitry Andric 
RefreshStateAfterStop()80e8d8bef9SDimitry Andric void ProcessTrace::RefreshStateAfterStop() {}
81e8d8bef9SDimitry Andric 
DoDestroy()82e8d8bef9SDimitry Andric Status ProcessTrace::DoDestroy() { return Status(); }
83e8d8bef9SDimitry Andric 
ReadMemory(addr_t addr,void * buf,size_t size,Status & error)84e8d8bef9SDimitry Andric size_t ProcessTrace::ReadMemory(addr_t addr, void *buf, size_t size,
85e8d8bef9SDimitry Andric                                 Status &error) {
8681ad6265SDimitry Andric   if (const ABISP &abi = GetABI())
8781ad6265SDimitry Andric     addr = abi->FixAnyAddress(addr);
8881ad6265SDimitry Andric 
89e8d8bef9SDimitry Andric   // Don't allow the caching that lldb_private::Process::ReadMemory does since
90e8d8bef9SDimitry Andric   // we have it all cached in the trace files.
91e8d8bef9SDimitry Andric   return DoReadMemory(addr, buf, size, error);
92e8d8bef9SDimitry Andric }
93e8d8bef9SDimitry Andric 
Clear()94e8d8bef9SDimitry Andric void ProcessTrace::Clear() { m_thread_list.Clear(); }
95e8d8bef9SDimitry Andric 
Initialize()96e8d8bef9SDimitry Andric void ProcessTrace::Initialize() {
97e8d8bef9SDimitry Andric   static llvm::once_flag g_once_flag;
98e8d8bef9SDimitry Andric 
99e8d8bef9SDimitry Andric   llvm::call_once(g_once_flag, []() {
100e8d8bef9SDimitry Andric     PluginManager::RegisterPlugin(GetPluginNameStatic(),
101e8d8bef9SDimitry Andric                                   GetPluginDescriptionStatic(), CreateInstance);
102e8d8bef9SDimitry Andric   });
103e8d8bef9SDimitry Andric }
104e8d8bef9SDimitry Andric 
GetArchitecture()105e8d8bef9SDimitry Andric ArchSpec ProcessTrace::GetArchitecture() {
106e8d8bef9SDimitry Andric   return GetTarget().GetArchitecture();
107e8d8bef9SDimitry Andric }
108e8d8bef9SDimitry Andric 
GetProcessInfo(ProcessInstanceInfo & info)109e8d8bef9SDimitry Andric bool ProcessTrace::GetProcessInfo(ProcessInstanceInfo &info) {
110e8d8bef9SDimitry Andric   info.Clear();
111e8d8bef9SDimitry Andric   info.SetProcessID(GetID());
112e8d8bef9SDimitry Andric   info.SetArchitecture(GetArchitecture());
113e8d8bef9SDimitry Andric   ModuleSP module_sp = GetTarget().GetExecutableModule();
114e8d8bef9SDimitry Andric   if (module_sp) {
115e8d8bef9SDimitry Andric     const bool add_exe_file_as_first_arg = false;
116e8d8bef9SDimitry Andric     info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
117e8d8bef9SDimitry Andric                            add_exe_file_as_first_arg);
118e8d8bef9SDimitry Andric   }
119e8d8bef9SDimitry Andric   return true;
120e8d8bef9SDimitry Andric }
121e8d8bef9SDimitry Andric 
DoReadMemory(addr_t addr,void * buf,size_t size,Status & error)122e8d8bef9SDimitry Andric size_t ProcessTrace::DoReadMemory(addr_t addr, void *buf, size_t size,
123e8d8bef9SDimitry Andric                                   Status &error) {
124e8d8bef9SDimitry Andric   Address resolved_address;
125e8d8bef9SDimitry Andric   GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, resolved_address);
126e8d8bef9SDimitry Andric 
127e8d8bef9SDimitry Andric   return GetTarget().ReadMemoryFromFileCache(resolved_address, buf, size,
128e8d8bef9SDimitry Andric                                              error);
129e8d8bef9SDimitry Andric }
130