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"
17 #include "lldb/Target/Target.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 ConstString ProcessTrace::GetPluginNameStatic() {
23  static ConstString g_name("trace");
24  return g_name;
25 }
26 
27 const char *ProcessTrace::GetPluginDescriptionStatic() {
28  return "Trace process plug-in.";
29 }
30 
31 void ProcessTrace::Terminate() {
32  PluginManager::UnregisterPlugin(ProcessTrace::CreateInstance);
33 }
34 
35 ProcessSP ProcessTrace::CreateInstance(TargetSP target_sp,
36  ListenerSP listener_sp,
37  const FileSpec *crash_file,
38  bool can_connect) {
39  if (can_connect)
40  return nullptr;
41  return std::make_shared<ProcessTrace>(target_sp, listener_sp);
42 }
43 
44 bool ProcessTrace::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) {
45  return plugin_specified_by_name;
46 }
47 
48 ProcessTrace::ProcessTrace(TargetSP target_sp, ListenerSP listener_sp)
49  : PostMortemProcess(target_sp, listener_sp) {}
50 
52  Clear();
53  // We need to call finalize on the process before destroying ourselves to
54  // make sure all of the broadcaster cleanup goes as planned. If we destruct
55  // this class, then Process::~Process() might have problems trying to fully
56  // destroy the broadcaster.
57  Finalize();
58 }
59 
60 void ProcessTrace::DidAttach(ArchSpec &process_arch) {
61  ListenerSP listener_sp(
62  Listener::MakeListener("lldb.process_trace.did_attach_listener"));
63  HijackProcessEvents(listener_sp);
64 
65  SetCanJIT(false);
68 
69  EventSP event_sp;
70  WaitForProcessToStop(llvm::None, &event_sp, true, listener_sp);
71 
73 
74  Process::DidAttach(process_arch);
75 }
76 
78  ThreadList &new_thread_list) {
79  return false;
80 }
81 
83 
85 
86 size_t ProcessTrace::ReadMemory(addr_t addr, void *buf, size_t size,
87  Status &error) {
88  // Don't allow the caching that lldb_private::Process::ReadMemory does since
89  // we have it all cached in the trace files.
90  return DoReadMemory(addr, buf, size, error);
91 }
92 
94 
96  static llvm::once_flag g_once_flag;
97 
98  llvm::call_once(g_once_flag, []() {
101  });
102 }
103 
105  return GetTarget().GetArchitecture();
106 }
107 
109  info.Clear();
110  info.SetProcessID(GetID());
112  ModuleSP module_sp = GetTarget().GetExecutableModule();
113  if (module_sp) {
114  const bool add_exe_file_as_first_arg = false;
115  info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
116  add_exe_file_as_first_arg);
117  }
118  return true;
119 }
120 
121 size_t ProcessTrace::DoReadMemory(addr_t addr, void *buf, size_t size,
122  Status &error) {
123  Address resolved_address;
124  GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, resolved_address);
125 
126  return GetTarget().ReadMemoryFromFileCache(resolved_address, buf, size,
127  error);
128 }
lldb_private::ProcessTrace::Initialize
static void Initialize()
Definition: ProcessTrace.cpp:95
lldb_private::ProcessInstanceInfo
Definition: ProcessInfo.h:108
lldb_private::Process::StartPrivateStateThread
bool StartPrivateStateThread(bool is_secondary_thread=false)
Definition: Process.cpp:3507
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:979
lldb_private::Process::m_thread_list
ThreadList m_thread_list
The threads for this process as the user will see them.
Definition: Process.h:2894
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:108
lldb_private::ProcessInstanceInfo::Clear
void Clear()
Definition: ProcessInfo.h:116
lldb_private::ProcessTrace::GetPluginDescriptionStatic
static const char * GetPluginDescriptionStatic()
Definition: ProcessTrace.cpp:27
lldb_private::ProcessTrace::GetArchitecture
ArchSpec GetArchitecture()
Definition: ProcessTrace.cpp:104
lldb_private::Target::GetExecutableModule
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
Definition: Target.cpp:1345
Module.h
lldb_private::Process::GetTarget
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1209
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:1686
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::Process::SetPrivateState
void SetPrivateState(lldb::StateType state)
Definition: Process.cpp:1416
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:86
Section.h
lldb_private::Process::RestoreProcessEvents
void RestoreProcessEvents()
Restores the process event broadcasting to its normal state.
Definition: Process.cpp:953
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:77
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:443
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:121
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::ConstString
Definition: ConstString.h:40
lldb_private::PostMortemProcess
Definition: PostMortemProcess.h:23
lldb_private::ProcessTrace::Clear
void Clear()
Definition: ProcessTrace.cpp:93
lldb_private::SectionLoadList::ResolveLoadAddress
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, bool allow_section_end=false) const
Definition: SectionLoadList.cpp:208
lldb_private::ProcessTrace::~ProcessTrace
~ProcessTrace() override
Definition: ProcessTrace.cpp:51
lldb_private::Target::GetArchitecture
const ArchSpec & GetArchitecture() const
Definition: Target.h:967
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:84
lldb_private::ProcessTrace::DidAttach
void DidAttach(ArchSpec &process_arch) override
Called after attaching a process.
Definition: ProcessTrace.cpp:60
PluginManager.h
lldb_private::ProcessTrace::GetPluginNameStatic
static ConstString GetPluginNameStatic()
Definition: ProcessTrace.cpp:22
lldb_private::Target::GetSectionLoadList
SectionLoadList & GetSectionLoadList()
Definition: Target.h:1040
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:541
lldb_private::PluginManager::RegisterPlugin
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
Definition: PluginManager.cpp:288
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:82
lldb_private::Process::SetCanJIT
void SetCanJIT(bool can_jit)
Sets whether executing JIT-compiled code in this process is possible.
Definition: Process.cpp:2371
lldb_private::Listener::MakeListener
static lldb::ListenerSP MakeListener(const char *name)
Definition: Listener.cpp:464
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:35