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"
19#include "lldb/Utility/Log.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
25
27 return "Trace process plug-in.";
28}
29
33
35 ListenerSP listener_sp,
36 const FileSpec *crash_file,
37 bool can_connect) {
38 if (can_connect)
39 return nullptr;
40 return std::make_shared<ProcessTrace>(target_sp, listener_sp,
41 crash_file ? *crash_file : FileSpec());
42}
43
44bool ProcessTrace::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) {
45 return plugin_specified_by_name;
46}
47
49 const FileSpec &core_file)
50 : PostMortemProcess(target_sp, listener_sp, core_file) {}
51
53 Clear();
54 // We need to call finalize on the process before destroying ourselves to
55 // make sure all of the broadcaster cleanup goes as planned. If we destruct
56 // this class, then Process::~Process() might have problems trying to fully
57 // destroy the broadcaster.
58 Finalize(true /* destructing */);
59}
60
61void ProcessTrace::DidAttach(ArchSpec &process_arch) {
62 ListenerSP listener_sp(
63 Listener::MakeListener("lldb.process_trace.did_attach_listener"));
64 HijackProcessEvents(listener_sp);
65
66 SetCanJIT(false);
69 LLDB_LOG(GetLog(LLDBLog::Process), "ProcessTrace: failed to start private "
70 "state thread.");
71 return;
72 }
73
74 // Pretend we stopped so we can show all of the threads
75 // in the trace and explore the final state.
77
78 EventSP event_sp;
79 WaitForProcessToStop(std::nullopt, &event_sp, true, listener_sp);
80
82
83 Process::DidAttach(process_arch);
84}
85
87 ThreadList &new_thread_list) {
88 return false;
89}
90
92
94
96 // The trace files are already a local copy of this memory.
97 return false;
98}
99
101
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(const ProcessAddress &process_addr, void *buf,
125 size_t size, Status &error) {
126 lldb::addr_t addr = process_addr.GetValue();
127 Address resolved_address;
128 GetTarget().ResolveLoadAddress(addr, resolved_address);
129
130 return GetTarget().ReadMemoryFromFileCache(resolved_address, buf, size,
131 error);
132}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:375
#define LLDB_PLUGIN_DEFINE(PluginName)
A section + offset based address class.
Definition Address.h:62
An architecture specification class.
Definition ArchSpec.h:32
A file utility class.
Definition FileSpec.h:56
static lldb::ListenerSP MakeListener(llvm::StringRef name)
Definition Listener.cpp:373
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)
An address in a process, qualified by an address space.
lldb::addr_t GetValue() const
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
void SetArchitecture(const ArchSpec &arch)
Definition ProcessInfo.h:64
void SetProcessID(lldb::pid_t pid)
Definition ProcessInfo.h:68
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
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.
size_t DoReadMemory(const ProcessAddress &addr, void *buf, size_t size, Status &error) override
Actually do the reading of memory from a process.
Status DoDestroy() override
bool ShouldUseMemoryCache(const ProcessAddress &process_addr) override
Whether ReadMemory should serve reads of process_addr out of the memory cache.
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:554
std::shared_ptr< PrivateStateThread > m_current_private_state_thread_sp
This is filled on construction with the "main" private state which will be exposed to clients of this...
Definition Process.h:3497
void SetCanJIT(bool can_jit)
Sets whether executing JIT-compiled code in this process is possible.
Definition Process.cpp:2774
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:728
virtual void DidAttach(ArchSpec &process_arch)
Called after attaching a process.
Definition Process.h:1033
bool StartPrivateStateThread(lldb::StateType state, bool run_lock_is_running, std::shared_ptr< PrivateStateThread > *backup_ptr=nullptr)
Definition Process.cpp:4160
void RestoreProcessEvents()
Restores the process event broadcasting to its normal state.
Definition Process.cpp:990
void SetPrivateState(lldb::StateType state)
Definition Process.cpp:1448
virtual void Finalize(bool destructing)
This object is about to be destroyed, do any necessary cleanup.
Definition Process.cpp:600
ThreadList m_thread_list
The threads for this process as the user will see them.
Definition Process.h:3514
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:982
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:2101
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, uint32_t stop_id=SectionLoadHistory::eStopIDNow, bool allow_section_end=false)
Definition Target.cpp:3530
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
Definition Target.cpp:1648
const ArchSpec & GetArchitecture() const
Definition Target.h:1325
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
@ 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