blob: 97ad5711ff11d416b86be1ca0b33ee9d76b559d7 [file] [log] [blame]
Todd Fialaaf245d12014-06-30 21:05:18 +00001//===-- NativeProcessLinux.h ---------------------------------- -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_NativeProcessLinux_H_
11#define liblldb_NativeProcessLinux_H_
12
Pavel Labathb6dbe9a2017-07-18 13:14:01 +000013#include <csignal>
Todd Fialaaf245d12014-06-30 21:05:18 +000014#include <unordered_set>
15
Todd Fialaaf245d12014-06-30 21:05:18 +000016#include "lldb/Host/Debug.h"
Zachary Turner39de3112014-09-09 20:54:56 +000017#include "lldb/Host/HostThread.h"
Pavel Labathb7f0f452017-03-17 11:08:40 +000018#include "lldb/Host/linux/Support.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000019#include "lldb/Target/MemoryRegionInfo.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000020#include "lldb/Utility/ArchSpec.h"
Zachary Turner5713a052017-03-22 18:40:07 +000021#include "lldb/Utility/FileSpec.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000022#include "lldb/lldb-types.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000023
Pavel Labath8c8ff7a2015-05-11 10:03:10 +000024#include "NativeThreadLinux.h"
Ravitheja Addepally99e37692017-06-28 07:58:31 +000025#include "ProcessorTrace.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000026#include "lldb/Host/common/NativeProcessProtocol.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000027
Tamas Berghammerdb264a62015-03-31 09:52:22 +000028namespace lldb_private {
Zachary Turner97206d52017-05-12 04:51:55 +000029class Status;
Kate Stoneb9c1b512016-09-06 20:57:50 +000030class Scalar;
Todd Fialaaf245d12014-06-30 21:05:18 +000031
Tamas Berghammerdb264a62015-03-31 09:52:22 +000032namespace process_linux {
Kate Stoneb9c1b512016-09-06 20:57:50 +000033/// @class NativeProcessLinux
Adrian Prantld8f460e2018-05-02 16:55:16 +000034/// Manages communication with the inferior (debugee) process.
Kate Stoneb9c1b512016-09-06 20:57:50 +000035///
Adrian Prantld8f460e2018-05-02 16:55:16 +000036/// Upon construction, this class prepares and launches an inferior process
37/// for debugging.
Kate Stoneb9c1b512016-09-06 20:57:50 +000038///
39/// Changes in the inferior process state are broadcasted.
40class NativeProcessLinux : public NativeProcessProtocol {
Kate Stoneb9c1b512016-09-06 20:57:50 +000041public:
Pavel Labath96e600f2017-07-07 11:02:19 +000042 class Factory : public NativeProcessProtocol::Factory {
43 public:
Pavel Labath82abefa2017-07-18 09:24:48 +000044 llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
Pavel Labath96e600f2017-07-07 11:02:19 +000045 Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate,
46 MainLoop &mainloop) const override;
47
Pavel Labath82abefa2017-07-18 09:24:48 +000048 llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
Pavel Labath96e600f2017-07-07 11:02:19 +000049 Attach(lldb::pid_t pid, NativeDelegate &native_delegate,
50 MainLoop &mainloop) const override;
51 };
52
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 // ---------------------------------------------------------------------
54 // NativeProcessProtocol Interface
55 // ---------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +000056 Status Resume(const ResumeActionList &resume_actions) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000057
Zachary Turner97206d52017-05-12 04:51:55 +000058 Status Halt() override;
Todd Fialaaf245d12014-06-30 21:05:18 +000059
Zachary Turner97206d52017-05-12 04:51:55 +000060 Status Detach() override;
Todd Fialaaf245d12014-06-30 21:05:18 +000061
Zachary Turner97206d52017-05-12 04:51:55 +000062 Status Signal(int signo) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000063
Zachary Turner97206d52017-05-12 04:51:55 +000064 Status Interrupt() override;
Chaoren Line9547b82015-02-03 01:51:00 +000065
Zachary Turner97206d52017-05-12 04:51:55 +000066 Status Kill() override;
Todd Fialaaf245d12014-06-30 21:05:18 +000067
Zachary Turner97206d52017-05-12 04:51:55 +000068 Status GetMemoryRegionInfo(lldb::addr_t load_addr,
69 MemoryRegionInfo &range_info) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000070
Zachary Turner97206d52017-05-12 04:51:55 +000071 Status ReadMemory(lldb::addr_t addr, void *buf, size_t size,
72 size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000073
Zachary Turner97206d52017-05-12 04:51:55 +000074 Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size,
75 size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000076
Zachary Turner97206d52017-05-12 04:51:55 +000077 Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
78 size_t &bytes_written) override;
Chaoren Lin3eb4b452015-04-29 17:24:48 +000079
Zachary Turner97206d52017-05-12 04:51:55 +000080 Status AllocateMemory(size_t size, uint32_t permissions,
81 lldb::addr_t &addr) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000082
Zachary Turner97206d52017-05-12 04:51:55 +000083 Status DeallocateMemory(lldb::addr_t addr) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 lldb::addr_t GetSharedLibraryInfoAddress() override;
Todd Fialaaf245d12014-06-30 21:05:18 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 size_t UpdateThreads() override;
Todd Fialaaf245d12014-06-30 21:05:18 +000088
Pavel Labath578a4252017-11-09 10:43:16 +000089 const ArchSpec &GetArchitecture() const override { return m_arch; }
Todd Fialaaf245d12014-06-30 21:05:18 +000090
Zachary Turner97206d52017-05-12 04:51:55 +000091 Status SetBreakpoint(lldb::addr_t addr, uint32_t size,
92 bool hardware) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000093
Zachary Turner97206d52017-05-12 04:51:55 +000094 Status RemoveBreakpoint(lldb::addr_t addr, bool hardware = false) override;
Omair Javaidd5ffbad2017-02-24 13:27:31 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 void DoStopIDBumped(uint32_t newBumpId) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000097
Zachary Turner97206d52017-05-12 04:51:55 +000098 Status GetLoadedModuleFileSpec(const char *module_path,
99 FileSpec &file_spec) override;
Tamas Berghammer068f8a72015-05-26 11:58:52 +0000100
Zachary Turner97206d52017-05-12 04:51:55 +0000101 Status GetFileLoadAddress(const llvm::StringRef &file_name,
102 lldb::addr_t &load_addr) override;
Tamas Berghammer783bfc82015-06-18 20:43:56 +0000103
Pavel Labatha5be48b2017-10-17 15:52:16 +0000104 NativeThreadLinux *GetThreadByID(lldb::tid_t id);
Pavel Labathf9077782015-08-21 09:13:53 +0000105
Pavel Labathb7f0f452017-03-17 11:08:40 +0000106 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
107 GetAuxvData() const override {
108 return getProcFile(GetID(), "auxv");
109 }
110
Ravitheja Addepally99e37692017-06-28 07:58:31 +0000111 lldb::user_id_t StartTrace(const TraceOptions &config,
112 Status &error) override;
113
114 Status StopTrace(lldb::user_id_t traceid,
115 lldb::tid_t thread) override;
116
117 Status GetData(lldb::user_id_t traceid, lldb::tid_t thread,
118 llvm::MutableArrayRef<uint8_t> &buffer,
119 size_t offset = 0) override;
120
121 Status GetMetaData(lldb::user_id_t traceid, lldb::tid_t thread,
122 llvm::MutableArrayRef<uint8_t> &buffer,
123 size_t offset = 0) override;
124
125 Status GetTraceConfig(lldb::user_id_t traceid, TraceOptions &config) override;
126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 // ---------------------------------------------------------------------
128 // Interface used by NativeRegisterContext-derived classes.
129 // ---------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000130 static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr,
131 void *data = nullptr, size_t data_size = 0,
132 long *result = nullptr);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 bool SupportHardwareSingleStepping() const;
Pavel Labath605b51b2016-02-23 13:56:30 +0000135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136protected:
Pavel Labathf8b825f2018-09-09 06:01:12 +0000137 llvm::Expected<llvm::ArrayRef<uint8_t>>
138 GetSoftwareBreakpointTrapOpcode(size_t size_hint) override;
Todd Fialaaf245d12014-06-30 21:05:18 +0000139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140private:
141 MainLoop::SignalHandleUP m_sigchld_handle;
142 ArchSpec m_arch;
Todd Fialaaf245d12014-06-30 21:05:18 +0000143
Pavel Labath96e600f2017-07-07 11:02:19 +0000144 LazyBool m_supports_mem_region = eLazyBoolCalculate;
Tamas Berghammera6f57952017-01-03 16:29:43 +0000145 std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache;
Todd Fialaaf245d12014-06-30 21:05:18 +0000146
Pavel Labath96e600f2017-07-07 11:02:19 +0000147 lldb::tid_t m_pending_notification_tid = LLDB_INVALID_THREAD_ID;
Todd Fialaaf245d12014-06-30 21:05:18 +0000148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 // List of thread ids stepping with a breakpoint with the address of
150 // the relevan breakpoint
151 std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
Pavel Labath0e1d7292015-08-20 09:06:12 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 // ---------------------------------------------------------------------
154 // Private Instance Methods
155 // ---------------------------------------------------------------------
Pavel Labath96e600f2017-07-07 11:02:19 +0000156 NativeProcessLinux(::pid_t pid, int terminal_fd, NativeDelegate &delegate,
Pavel Labath82abefa2017-07-18 09:24:48 +0000157 const ArchSpec &arch, MainLoop &mainloop,
158 llvm::ArrayRef<::pid_t> tids);
Tamas Berghammerd8c338d2015-04-15 09:47:02 +0000159
Pavel Labath96e600f2017-07-07 11:02:19 +0000160 // Returns a list of process threads that we have attached to.
161 static llvm::Expected<std::vector<::pid_t>> Attach(::pid_t pid);
Todd Fialaaf245d12014-06-30 21:05:18 +0000162
Zachary Turner97206d52017-05-12 04:51:55 +0000163 static Status SetDefaultPtraceOpts(const lldb::pid_t);
Pavel Labath1107b5a2015-04-17 14:07:49 +0000164
Pavel Labath3508fc82017-06-19 12:47:50 +0000165 void MonitorCallback(lldb::pid_t pid, bool exited, WaitStatus status);
Todd Fialaaf245d12014-06-30 21:05:18 +0000166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167 void WaitForNewThread(::pid_t tid);
Pavel Labath1107b5a2015-04-17 14:07:49 +0000168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread);
Todd Fialaaf245d12014-06-30 21:05:18 +0000170
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 void MonitorTrace(NativeThreadLinux &thread);
Pavel Labath426bdf82015-04-28 07:51:52 +0000172
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173 void MonitorBreakpoint(NativeThreadLinux &thread);
Todd Fialaaf245d12014-06-30 21:05:18 +0000174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index);
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000176
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread,
178 bool exited);
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000179
Zachary Turner97206d52017-05-12 04:51:55 +0000180 Status SetupSoftwareSingleStepping(NativeThreadLinux &thread);
Tamas Berghammere7708682015-04-22 10:00:23 +0000181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 bool HasThreadNoLock(lldb::tid_t thread_id);
Todd Fialaaf245d12014-06-30 21:05:18 +0000183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 bool StopTrackingThread(lldb::tid_t thread_id);
Todd Fialaaf245d12014-06-30 21:05:18 +0000185
Pavel Labatha5be48b2017-10-17 15:52:16 +0000186 NativeThreadLinux &AddThread(lldb::tid_t thread_id);
Todd Fialaaf245d12014-06-30 21:05:18 +0000187
Zachary Turner97206d52017-05-12 04:51:55 +0000188 Status GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size);
Todd Fialaaf245d12014-06-30 21:05:18 +0000189
Zachary Turner97206d52017-05-12 04:51:55 +0000190 Status FixupBreakpointPCAsNeeded(NativeThreadLinux &thread);
Todd Fialaaf245d12014-06-30 21:05:18 +0000191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192 /// Writes a siginfo_t structure corresponding to the given thread ID to the
193 /// memory region pointed to by @p siginfo.
Zachary Turner97206d52017-05-12 04:51:55 +0000194 Status GetSignalInfo(lldb::tid_t tid, void *siginfo);
Todd Fialaaf245d12014-06-30 21:05:18 +0000195
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
197 /// corresponding to the given thread ID to the memory pointed to by @p
198 /// message.
Zachary Turner97206d52017-05-12 04:51:55 +0000199 Status GetEventMessage(lldb::tid_t tid, unsigned long *message);
Todd Fialaaf245d12014-06-30 21:05:18 +0000200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 void NotifyThreadDeath(lldb::tid_t tid);
Chaoren Linfa03ad22015-02-03 01:50:42 +0000202
Zachary Turner97206d52017-05-12 04:51:55 +0000203 Status Detach(lldb::tid_t tid);
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 // This method is requests a stop on all threads which are still running. It
206 // sets up a
207 // deferred delegate notification, which will fire once threads report as
208 // stopped. The
209 // triggerring_tid will be set as the current thread (main stop reason).
210 void StopRunningThreads(lldb::tid_t triggering_tid);
Pavel Labathc0765592015-05-06 10:46:34 +0000211
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 // Notify the delegate if all threads have stopped.
213 void SignalIfAllThreadsStopped();
Pavel Labathc0765592015-05-06 10:46:34 +0000214
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215 // Resume the given thread, optionally passing it the given signal. The type
216 // of resume
217 // operation (continue, single-step) depends on the state parameter.
Zachary Turner97206d52017-05-12 04:51:55 +0000218 Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state,
219 int signo);
Pavel Labathc0765592015-05-06 10:46:34 +0000220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 void ThreadWasCreated(NativeThreadLinux &thread);
Pavel Labathc0765592015-05-06 10:46:34 +0000222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 void SigchldHandler();
Tamas Berghammera6f57952017-01-03 16:29:43 +0000224
Zachary Turner97206d52017-05-12 04:51:55 +0000225 Status PopulateMemoryRegionCache();
Ravitheja Addepally99e37692017-06-28 07:58:31 +0000226
227 lldb::user_id_t StartTraceGroup(const TraceOptions &config,
228 Status &error);
229
230 // This function is intended to be used to stop tracing
231 // on a thread that exited.
232 Status StopTracingForThread(lldb::tid_t thread);
233
234 // The below function as the name suggests, looks up a ProcessorTrace
235 // instance from the m_processor_trace_monitor map. In the case of
236 // process tracing where the traceid passed would map to the complete
237 // process, it is mandatory to provide a threadid to obtain a trace
238 // instance (since ProcessorTrace is tied to a thread). In the other
239 // scenario that an individual thread is being traced, just the traceid
240 // is sufficient to obtain the actual ProcessorTrace instance.
241 llvm::Expected<ProcessorTraceMonitor &>
242 LookupProcessorTraceInstance(lldb::user_id_t traceid, lldb::tid_t thread);
243
244 // Stops tracing on individual threads being traced. Not intended
245 // to be used to stop tracing on complete process.
246 Status StopProcessorTracingOnThread(lldb::user_id_t traceid,
247 lldb::tid_t thread);
248
249 // Intended to stop tracing on complete process.
250 // Should not be used for stopping trace on
251 // individual threads.
252 void StopProcessorTracingOnProcess();
253
254 llvm::DenseMap<lldb::tid_t, ProcessorTraceMonitorUP>
255 m_processor_trace_monitor;
256
257 // Set for tracking threads being traced under
258 // same process user id.
259 llvm::DenseSet<lldb::tid_t> m_pt_traced_thread_group;
260
Pavel Labath96e600f2017-07-07 11:02:19 +0000261 lldb::user_id_t m_pt_proces_trace_id = LLDB_INVALID_UID;
Ravitheja Addepally99e37692017-06-28 07:58:31 +0000262 TraceOptions m_pt_process_trace_config;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263};
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000264
265} // namespace process_linux
266} // namespace lldb_private
Todd Fialaaf245d12014-06-30 21:05:18 +0000267
268#endif // #ifndef liblldb_NativeProcessLinux_H_