LLDB  mainline
ProcessTrace.cpp
Go to the documentation of this file.
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 
10 
11 #include <memory>
12 
13 #include "lldb/Core/Module.h"
15 #include "lldb/Core/Section.h"
16 #include "lldb/Target/ABI.h"
18 #include "lldb/Target/Target.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 llvm::StringRef ProcessTrace::GetPluginDescriptionStatic() {
24  return "Trace process plug-in.";
25 }
26 
27 void ProcessTrace::Terminate() {
28  PluginManager::UnregisterPlugin(ProcessTrace::CreateInstance);
29 }
30 
31 ProcessSP ProcessTrace::CreateInstance(TargetSP target_sp,
32  ListenerSP listener_sp,
33  const FileSpec *crash_file,
34  bool can_connect) {
35  if (can_connect)
36  return nullptr;
37  return std::make_shared<ProcessTrace>(target_sp, listener_sp);
38 }
39 
40 bool ProcessTrace::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) {
41  return plugin_specified_by_name;
42 }
43 
44 ProcessTrace::ProcessTrace(TargetSP target_sp, ListenerSP listener_sp)
45  : PostMortemProcess(target_sp, listener_sp) {}
46 
48  Clear();
49  // We need to call finalize on the process before destroying ourselves to
50  // make sure all of the broadcaster cleanup goes as planned. If we destruct
51  // this class, then Process::~Process() might have problems trying to fully
52  // destroy the broadcaster.
53  Finalize();
54 }
55 
56 void ProcessTrace::DidAttach(ArchSpec &process_arch) {
57  ListenerSP listener_sp(
58  Listener::MakeListener("lldb.process_trace.did_attach_listener"));
59  HijackProcessEvents(listener_sp);
60 
61  SetCanJIT(false);
64 
65  EventSP event_sp;
66  WaitForProcessToStop(llvm::None, &event_sp, true, listener_sp);
67 
69 
70  Process::DidAttach(process_arch);
71 }
72 
74  ThreadList &new_thread_list) {
75  return false;
76 }
77 
79 
81 
82 size_t ProcessTrace::ReadMemory(addr_t addr, void *buf, size_t size,
83  Status &error) {
84  if (const ABISP &abi = GetABI())
85  addr = abi->FixAnyAddress(addr);
86 
87  // Don't allow the caching that lldb_private::Process::ReadMemory does since
88  // we have it all cached in the trace files.
89  return DoReadMemory(addr, buf, size, error);
90 }
91 
93 
95  static llvm::once_flag g_once_flag;
96 
97  llvm::call_once(g_once_flag, []() {
100  });
101 }
102 
104  return GetTarget().GetArchitecture();
105 }
106 
108  info.Clear();
109  info.SetProcessID(GetID());
111  ModuleSP module_sp = GetTarget().GetExecutableModule();
112  if (module_sp) {
113  const bool add_exe_file_as_first_arg = false;
114  info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
115  add_exe_file_as_first_arg);
116  }
117  return true;
118 }
119 
120 size_t ProcessTrace::DoReadMemory(addr_t addr, void *buf, size_t size,
121  Status &error) {
122  Address resolved_address;
123  GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, resolved_address);
124 
125  return GetTarget().ReadMemoryFromFileCache(resolved_address, buf, size,
126  error);
127 }
lldb_private::ProcessTrace::Initialize
static void Initialize()
Definition: ProcessTrace.cpp:94
lldb_private::ProcessInstanceInfo
Definition: ProcessInfo.h:108
lldb_private::Process::StartPrivateStateThread
bool StartPrivateStateThread(bool is_secondary_thread=false)
Definition: Process.cpp:3503
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::Process::DidAttach
virtual void DidAttach(ArchSpec &process_arch)
Called after attaching a process.
Definition: Process.h:976
lldb_private::Process::m_thread_list
ThreadList m_thread_list
The threads for this process as the user will see them.
Definition: Process.h:2848
lldb_private::ProcessInfo::SetExecutableFile
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
Definition: ProcessInfo.cpp:62
lldb_private::ProcessInfo::SetArchitecture
void SetArchitecture(const ArchSpec &arch)
Definition: ProcessInfo.h:65
lldb_private::ProcessTrace::GetProcessInfo
bool GetProcessInfo(ProcessInstanceInfo &info) override
Definition: ProcessTrace.cpp:107
lldb_private::ProcessInstanceInfo::Clear
void Clear()
Definition: ProcessInfo.h:116
lldb_private::ProcessTrace::GetArchitecture
ArchSpec GetArchitecture()
Definition: ProcessTrace.cpp:103
lldb_private::Target::GetExecutableModule
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
Definition: Target.cpp:1362
Module.h
lldb_private::Process::GetTarget
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1206
SectionLoadList.h
lldb_private::ProcessInfo::SetProcessID
void SetProcessID(lldb::pid_t pid)
Definition: ProcessInfo.h:69
lldb_private::Target::ReadMemoryFromFileCache
size_t ReadMemoryFromFileCache(const Address &addr, void *dst, size_t dst_len, Status &error)
Definition: Target.cpp:1703
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::Process::SetPrivateState
void SetPrivateState(lldb::StateType state)
Definition: Process.cpp:1412
lldb_private::ProcessTrace::ReadMemory
size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size, Status &error) override
Read of memory from a process.
Definition: ProcessTrace.cpp:82
Section.h
lldb_private::Process::RestoreProcessEvents
void RestoreProcessEvents()
Restores the process event broadcasting to its normal state.
Definition: Process.cpp:953
ABI.h
lldb_private::ProcessTrace::DoUpdateThreadList
bool DoUpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) override
Update the thread list following process plug-in's specific logic.
Definition: ProcessTrace.cpp:73
lldb::eStateStopped
@ eStateStopped
Process or thread is stopped and can be examined.
Definition: lldb-enumerations.h:84
Target.h
lldb_private::Process::HijackProcessEvents
bool HijackProcessEvents(lldb::ListenerSP listener_sp)
If you need to ensure that you and only you will hear about some public event, then make a new listen...
Definition: Process.cpp:945
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::ThreadList::Clear
void Clear()
Definition: ThreadList.cpp:444
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::ProcessTrace::DoReadMemory
size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Status &error) override
Actually do the reading of memory from a process.
Definition: ProcessTrace.cpp:120
lldb_private::Process::Finalize
virtual void Finalize()
This object is about to be destroyed, do any necessary cleanup.
Definition: Process.cpp:511
lldb_private::ThreadList
Definition: ThreadList.h:26
lldb_private::PostMortemProcess
Definition: PostMortemProcess.h:23
lldb_private::ProcessTrace::Clear
void Clear()
Definition: ProcessTrace.cpp:92
lldb_private::SectionLoadList::ResolveLoadAddress
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, bool allow_section_end=false) const
Definition: SectionLoadList.cpp:209
lldb_private::ProcessTrace::~ProcessTrace
~ProcessTrace() override
Definition: ProcessTrace.cpp:47
lldb_private::ProcessTrace::GetPluginDescriptionStatic
static llvm::StringRef GetPluginDescriptionStatic()
Definition: ProcessTrace.cpp:23
lldb_private::Target::GetArchitecture
const ArchSpec & GetArchitecture() const
Definition: Target.h:983
lldb_private::Status
Definition: Status.h:44
ProcessTrace.h
lldb_private::Address
Definition: Address.h:59
lldb_private::Process::WaitForProcessToStop
lldb::StateType WaitForProcessToStop(const Timeout< std::micro > &timeout, lldb::EventSP *event_sp_ptr=nullptr, bool wait_always=true, lldb::ListenerSP hijack_listener=lldb::ListenerSP(), Stream *stream=nullptr, bool use_run_lock=true)
Definition: Process.cpp:646
lldb_private::ProcessTrace::DoDestroy
Status DoDestroy() override
Definition: ProcessTrace.cpp:80
lldb_private::ProcessTrace::DidAttach
void DidAttach(ArchSpec &process_arch) override
Called after attaching a process.
Definition: ProcessTrace.cpp:56
PluginManager.h
lldb_private::Target::GetSectionLoadList
SectionLoadList & GetSectionLoadList()
Definition: Target.h:1091
lldb_private::Process::GetID
lldb::pid_t GetID() const
Returns the pid of the process or LLDB_INVALID_PROCESS_ID if there is no known pid.
Definition: Process.h:535
lldb_private::PluginManager::RegisterPlugin
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
Definition: PluginManager.cpp:287
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ProcessTrace::RefreshStateAfterStop
void RefreshStateAfterStop() override
Currently called as part of ShouldStop.
Definition: ProcessTrace.cpp:78
lldb_private::ProcessTrace::GetPluginNameStatic
static llvm::StringRef GetPluginNameStatic()
Definition: ProcessTrace.h:26
lldb_private::Process::GetABI
const lldb::ABISP & GetABI()
Definition: Process.cpp:1482
lldb_private::Process::SetCanJIT
void SetCanJIT(bool can_jit)
Sets whether executing JIT-compiled code in this process is possible.
Definition: Process.cpp:2324
lldb_private::Listener::MakeListener
static lldb::ListenerSP MakeListener(const char *name)
Definition: Listener.cpp:461
lldb
Definition: SBAddress.h:15
lldb_private::ProcessTrace::CreateInstance
static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const FileSpec *crash_file_path, bool can_connect)
Definition: ProcessTrace.cpp:31