LLDB mainline
NativeProcessLinux.h
Go to the documentation of this file.
1//===-- NativeProcessLinux.h ---------------------------------- -*- C++ -*-===//
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
9#ifndef liblldb_NativeProcessLinux_H_
10#define liblldb_NativeProcessLinux_H_
11
12#include <csignal>
13#include <unordered_set>
14
15#include "lldb/Host/Debug.h"
22#include "lldb/lldb-types.h"
23#include "llvm/ADT/SmallPtrSet.h"
24
25#include "IntelPTCollector.h"
26#include "NativeThreadLinux.h"
29
30namespace lldb_private {
31class Status;
32class Scalar;
33
34namespace process_linux {
35/// \class NativeProcessLinux
36/// Manages communication with the inferior (debugee) process.
37///
38/// Upon construction, this class prepares and launches an inferior process
39/// for debugging.
40///
41/// Changes in the inferior process state are broadcasted.
44public:
46 public:
47 Manager(MainLoop &mainloop);
48
49 llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
50 Launch(ProcessLaunchInfo &launch_info,
51 NativeDelegate &native_delegate) override;
52
53 llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
54 Attach(lldb::pid_t pid, NativeDelegate &native_delegate) override;
55
56 Extension GetSupportedExtensions() const override;
57
59 m_processes.insert(&process);
60 }
61
63 m_processes.erase(&process);
64 }
65
66 // Collect an event for the given tid, waiting for it if necessary.
67 void CollectThread(::pid_t tid);
68
69 private:
71
72 llvm::SmallPtrSet<NativeProcessLinux *, 2> m_processes;
73
74 // Threads (events) which haven't been claimed by any process.
75 llvm::DenseSet<::pid_t> m_unowned_threads;
76
77 void SigchldHandler();
78 };
79
80 // NativeProcessProtocol Interface
81
82 ~NativeProcessLinux() override { m_manager.RemoveProcess(*this); }
83
84 Status Resume(const ResumeActionList &resume_actions) override;
85
86 Status Halt() override;
87
88 Status Detach() override;
89
90 Status Signal(int signo) override;
91
92 Status Interrupt() override;
93
94 Status Kill() override;
95
97 MemoryRegionInfo &range_info) override;
98
99 Status ReadMemory(lldb::addr_t addr, void *buf, size_t size,
100 size_t &bytes_read) override;
101
102 Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
103 size_t &bytes_written) override;
104
105 llvm::Expected<lldb::addr_t> AllocateMemory(size_t size,
106 uint32_t permissions) override;
107
108 llvm::Error DeallocateMemory(lldb::addr_t addr) override;
109
110 Status ReadMemoryTags(int32_t type, lldb::addr_t addr, size_t len,
111 std::vector<uint8_t> &tags) override;
112
113 Status WriteMemoryTags(int32_t type, lldb::addr_t addr, size_t len,
114 const std::vector<uint8_t> &tags) override;
115
116 size_t UpdateThreads() override;
117
118 const ArchSpec &GetArchitecture() const override { return m_arch; }
119
120 Status SetBreakpoint(lldb::addr_t addr, uint32_t size,
121 bool hardware) override;
122
123 Status RemoveBreakpoint(lldb::addr_t addr, bool hardware = false) override;
124
125 Status GetLoadedModuleFileSpec(const char *module_path,
126 FileSpec &file_spec) override;
127
128 Status GetFileLoadAddress(const llvm::StringRef &file_name,
129 lldb::addr_t &load_addr) override;
130
133
134 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
135 GetAuxvData() const override {
136 return getProcFile(GetID(), "auxv");
137 }
138
139 /// Tracing
140 /// These methods implement the jLLDBTrace packets
141 /// \{
142 llvm::Error TraceStart(llvm::StringRef json_request,
143 llvm::StringRef type) override;
144
145 llvm::Error TraceStop(const TraceStopRequest &request) override;
146
147 llvm::Expected<llvm::json::Value>
148 TraceGetState(llvm::StringRef type) override;
149
150 llvm::Expected<std::vector<uint8_t>>
151 TraceGetBinaryData(const TraceGetBinaryDataRequest &request) override;
152
153 llvm::Expected<TraceSupportedResponse> TraceSupported() override;
154 /// }
155
156 // Interface used by NativeRegisterContext-derived classes.
157 static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr,
158 void *data = nullptr, size_t data_size = 0,
159 long *result = nullptr);
160
162
163 /// Writes a siginfo_t structure corresponding to the given thread ID to the
164 /// memory region pointed to by \p siginfo.
165 Status GetSignalInfo(lldb::tid_t tid, void *siginfo) const;
166
167protected:
168 llvm::Expected<llvm::ArrayRef<uint8_t>>
169 GetSoftwareBreakpointTrapOpcode(size_t size_hint) override;
170
171 llvm::Expected<uint64_t> Syscall(llvm::ArrayRef<uint64_t> args);
172
173private:
176
178
180
181 /// Inferior memory (allocated by us) and its size.
182 llvm::DenseMap<lldb::addr_t, lldb::addr_t> m_allocated_memory;
183
184 // Private Instance Methods
185 NativeProcessLinux(::pid_t pid, int terminal_fd, NativeDelegate &delegate,
186 const ArchSpec &arch, Manager &manager,
187 llvm::ArrayRef<::pid_t> tids);
188
189 // Returns a list of process threads that we have attached to.
190 static llvm::Expected<std::vector<::pid_t>> Attach(::pid_t pid);
191
193
195
196 void MonitorCallback(NativeThreadLinux &thread, WaitStatus status);
197
198 void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread);
199
200 void MonitorTrace(NativeThreadLinux &thread);
201
203
204 void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index);
205
206 void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread);
207
208 bool HasThreadNoLock(lldb::tid_t thread_id);
209
211
212 /// Create a new thread.
213 ///
214 /// If process tracing is enabled and the thread can't be traced, then the
215 /// thread is left stopped with a \a eStopReasonProcessorTrace status, and
216 /// then the process is stopped.
217 ///
218 /// \param[in] resume
219 /// If a tracing error didn't happen, then resume the thread after
220 /// creation if \b true, or leave it stopped with SIGSTOP if \b false.
221 NativeThreadLinux &AddThread(lldb::tid_t thread_id, bool resume);
222
223 /// Start tracing a new thread if process tracing is enabled.
224 ///
225 /// Trace mechanisms should modify this method to provide automatic tracing
226 /// for new threads.
228
229 /// Stop tracing threads upon a destroy event.
230 ///
231 /// Trace mechanisms should modify this method to provide automatic trace
232 /// stopping for threads being destroyed.
234
235 void NotifyTracersProcessWillResume() override;
236
237 void NotifyTracersProcessDidStop() override;
238
239 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
240 /// corresponding to the given thread ID to the memory pointed to by @p
241 /// message.
242 Status GetEventMessage(lldb::tid_t tid, unsigned long *message);
243
245
247
248 // This method is requests a stop on all threads which are still running. It
249 // sets up a
250 // deferred delegate notification, which will fire once threads report as
251 // stopped. The
252 // triggerring_tid will be set as the current thread (main stop reason).
253 void StopRunningThreads(lldb::tid_t triggering_tid);
254
255 // Notify the delegate if all threads have stopped.
257
258 // Resume the given thread, optionally passing it the given signal. The type
259 // of resume
260 // operation (continue, single-step) depends on the state parameter.
262 int signo);
263
265
267
269
270 /// Manages Intel PT process and thread traces.
272
273 // Handle a clone()-like event.
274 bool MonitorClone(NativeThreadLinux &parent, lldb::pid_t child_pid,
275 int event);
276};
277
278} // namespace process_linux
279} // namespace lldb_private
280
281#endif // #ifndef liblldb_NativeProcessLinux_H_
An architecture specification class.
Definition ArchSpec.h:32
A file utility class.
Definition FileSpec.h:57
std::unique_ptr< SignalHandle > SignalHandleUP
Abstract class that extends NativeProcessProtocol with ELF specific logic.
Extension
Extension flag constants, returned by Manager::GetSupportedExtensions() and passed to SetEnabledExten...
An error handling class.
Definition Status.h:118
Main class that manages intel-pt process and thread tracing.
llvm::Expected< std::unique_ptr< NativeProcessProtocol > > Attach(lldb::pid_t pid, NativeDelegate &native_delegate) override
Attach to an existing process.
llvm::SmallPtrSet< NativeProcessLinux *, 2 > m_processes
Extension GetSupportedExtensions() const override
Get the bitmask of extensions supported by this process plugin.
llvm::Expected< std::unique_ptr< NativeProcessProtocol > > Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate) override
Launch a process for debugging.
llvm::Expected< lldb::addr_t > AllocateMemory(size_t size, uint32_t permissions) override
llvm::Expected< std::vector< uint8_t > > TraceGetBinaryData(const TraceGetBinaryDataRequest &request) override
Get binary data given a trace technology and a data identifier.
NativeThreadLinux * GetThreadByID(lldb::tid_t id)
llvm::Error DeallocateMemory(lldb::addr_t addr) override
Status GetFileLoadAddress(const llvm::StringRef &file_name, lldb::addr_t &load_addr) override
Status NotifyTracersOfNewThread(lldb::tid_t tid)
Start tracing a new thread if process tracing is enabled.
const ArchSpec & GetArchitecture() const override
Status GetEventMessage(lldb::tid_t tid, unsigned long *message)
Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) corresponding to the given thread ID...
IntelPTCollector m_intel_pt_collector
Manages Intel PT process and thread traces.
llvm::Error TraceStart(llvm::StringRef json_request, llvm::StringRef type) override
Tracing These methods implement the jLLDBTrace packets.
bool MonitorClone(NativeThreadLinux &parent, lldb::pid_t child_pid, int event)
llvm::DenseMap< lldb::addr_t, lldb::addr_t > m_allocated_memory
Inferior memory (allocated by us) and its size.
Status Interrupt() override
Tells a process to interrupt all operations as if by a Ctrl-C.
llvm::Expected< llvm::ArrayRef< uint8_t > > GetSoftwareBreakpointTrapOpcode(size_t size_hint) override
void NotifyTracersProcessWillResume() override
Notify tracers that the target process will resume.
void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread)
Status SetBreakpoint(lldb::addr_t addr, uint32_t size, bool hardware) override
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > GetAuxvData() const override
llvm::Expected< TraceSupportedResponse > TraceSupported() override
Get the processor tracing type supported for this process.
void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index)
bool TryHandleWaitStatus(lldb::pid_t pid, WaitStatus status)
llvm::Error TraceStop(const TraceStopRequest &request) override
Stop tracing a live process or its threads.
Status ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override
Status Resume(const ResumeActionList &resume_actions) override
llvm::Expected< llvm::json::Value > TraceGetState(llvm::StringRef type) override
Get the current tracing state of the process and its threads.
Status RemoveBreakpoint(lldb::addr_t addr, bool hardware=false) override
NativeProcessLinux(::pid_t pid, int terminal_fd, NativeDelegate &delegate, const ArchSpec &arch, Manager &manager, llvm::ArrayRef<::pid_t > tids)
Status NotifyTracersOfThreadDestroyed(lldb::tid_t tid)
Stop tracing threads upon a destroy event.
void NotifyTracersProcessDidStop() override
Notify tracers that the target process just stopped.
Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override
static Status SetDefaultPtraceOpts(const lldb::pid_t)
void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread)
Status GetSignalInfo(lldb::tid_t tid, void *siginfo) const
Writes a siginfo_t structure corresponding to the given thread ID to the memory region pointed to by ...
Status WriteMemoryTags(int32_t type, lldb::addr_t addr, size_t len, const std::vector< uint8_t > &tags) override
Status ReadMemoryTags(int32_t type, lldb::addr_t addr, size_t len, std::vector< uint8_t > &tags) override
Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state, int signo)
Status GetMemoryRegionInfo(lldb::addr_t load_addr, MemoryRegionInfo &range_info) override
NativeThreadLinux & AddThread(lldb::tid_t thread_id, bool resume)
Create a new thread.
static llvm::Expected< std::vector<::pid_t > > Attach(::pid_t pid)
llvm::Expected< uint64_t > Syscall(llvm::ArrayRef< uint64_t > args)
Status Signal(int signo) override
Sends a process a UNIX signal signal.
Status GetLoadedModuleFileSpec(const char *module_path, FileSpec &file_spec) override
void MonitorCallback(NativeThreadLinux &thread, WaitStatus status)
static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr=nullptr, void *data=nullptr, size_t data_size=0, long *result=nullptr)
}
#define LLDB_INVALID_THREAD_ID
A class that represents a running process on the host machine.
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getProcFile(::pid_t pid, ::pid_t tid, const llvm::Twine &file)
MainLoopPosix MainLoop
Definition MainLoop.h:20
StateType
Process and Thread States.
uint64_t pid_t
Definition lldb-types.h:83
uint64_t addr_t
Definition lldb-types.h:80
uint64_t tid_t
Definition lldb-types.h:84
jLLDBTraceGetBinaryData gdb-remote packet
jLLDBTraceStop gdb-remote packet