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
20using namespace lldb;
21using namespace lldb_private;
22
24
26 return "Trace process plug-in.";
27}
28
32
34 ListenerSP listener_sp,
35 const FileSpec *crash_file,
36 bool can_connect) {
37 if (can_connect)
38 return nullptr;
39 return std::make_shared<ProcessTrace>(target_sp, listener_sp,
40 crash_file ? *crash_file : FileSpec());
41}
42
43bool ProcessTrace::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) {
44 return plugin_specified_by_name;
45}
46
48 const FileSpec &core_file)
49 : PostMortemProcess(target_sp, listener_sp, core_file) {}
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(true /* destructing */);
58}
59
60void 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(std::nullopt, &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
86size_t ProcessTrace::ReadMemory(addr_t addr, void *buf, size_t size,
87 Status &error) {
88 if (const ABISP &abi = GetABI())
89 addr = abi->FixAnyAddress(addr);
90
91 // Don't allow the caching that lldb_private::Process::ReadMemory does since
92 // we have it all cached in the trace files.
93 return DoReadMemory(addr, buf, size, error);
94}
95
97
99 static llvm::once_flag g_once_flag;
100
101 llvm::call_once(g_once_flag, []() {
104 });
105}
106
110
112 info.Clear();
113 info.SetProcessID(GetID());
115 ModuleSP module_sp = GetTarget().GetExecutableModule();
116 if (module_sp) {
117 const bool add_exe_file_as_first_arg = false;
118 info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
119 add_exe_file_as_first_arg);
120 }
121 return true;
122}
123
124size_t ProcessTrace::DoReadMemory(addr_t addr, void *buf, size_t size,
125 Status &error) {
126 Address resolved_address;
127 GetTarget().ResolveLoadAddress(addr, resolved_address);
128
129 return GetTarget().ReadMemoryFromFileCache(resolved_address, buf, size,
130 error);
131}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_PLUGIN_DEFINE(PluginName)
A section + offset based address class.
Definition Address.h:62
An architecture specification class.
Definition ArchSpec.h:31
A file utility class.
Definition FileSpec.h:57
static lldb::ListenerSP MakeListener(const char *name)
Definition Listener.cpp:375
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
PostMortemProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const FileSpec &core_file)
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
void SetArchitecture(const ArchSpec &arch)
Definition ProcessInfo.h:66
void SetProcessID(lldb::pid_t pid)
Definition ProcessInfo.h:70
Class that represents a defunct process loaded on memory via the "traceload" command.
static llvm::StringRef GetPluginNameStatic()
void RefreshStateAfterStop() override
Currently called as part of ShouldStop.
bool GetProcessInfo(ProcessInstanceInfo &info) override
size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Status &error) override
Actually do the reading of memory from a process.
bool CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) override
Check if a plug-in instance can debug the file in module.
static llvm::StringRef GetPluginDescriptionStatic()
static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const FileSpec *crash_file_path, bool can_connect)
bool DoUpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) override
Update the thread list following process plug-in's specific logic.
Status DoDestroy() override
size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size, Status &error) override
Read of memory from a process.
void DidAttach(ArchSpec &process_arch) override
Called after attaching a process.
ProcessTrace(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const FileSpec &core_file)
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:556
void SetCanJIT(bool can_jit)
Sets whether executing JIT-compiled code in this process is possible.
Definition Process.cpp:2486
bool StartPrivateStateThread(bool is_secondary_thread=false)
Definition Process.cpp:3778
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, SelectMostRelevant select_most_relevant=DoNoSelectMostRelevantFrame)
Definition Process.cpp:665
virtual void DidAttach(ArchSpec &process_arch)
Called after attaching a process.
Definition Process.h:1035
void RestoreProcessEvents()
Restores the process event broadcasting to its normal state.
Definition Process.cpp:927
void SetPrivateState(lldb::StateType state)
Definition Process.cpp:1404
virtual void Finalize(bool destructing)
This object is about to be destroyed, do any necessary cleanup.
Definition Process.cpp:538
ThreadList m_thread_list
The threads for this process as the user will see them.
Definition Process.h:3152
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:919
const lldb::ABISP & GetABI()
Definition Process.cpp:1481
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1270
An error handling class.
Definition Status.h:118
size_t ReadMemoryFromFileCache(const Address &addr, void *dst, size_t dst_len, Status &error)
Definition Target.cpp:1959
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, uint32_t stop_id=SectionLoadHistory::eStopIDNow, bool allow_section_end=false)
Definition Target.cpp:3285
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
Definition Target.cpp:1517
const ArchSpec & GetArchitecture() const
Definition Target.h:1056
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::ABI > ABISP
@ eStateStopped
Process or thread is stopped and can be examined.
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::Event > EventSP
std::shared_ptr< lldb_private::Listener > ListenerSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::Module > ModuleSP