blob: 9584713d365401c83ee75661ebd556b4c9ca51b6 [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
Todd Fialaaf245d12014-06-30 21:05:18 +000013// C++ Includes
14#include <unordered_set>
15
16// Other libraries and framework includes
17#include "lldb/Core/ArchSpec.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000018#include "lldb/Host/Debug.h"
Zachary Turner39de3112014-09-09 20:54:56 +000019#include "lldb/Host/HostThread.h"
Pavel Labathb7f0f452017-03-17 11:08:40 +000020#include "lldb/Host/linux/Support.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000021#include "lldb/Target/MemoryRegionInfo.h"
Zachary Turner5713a052017-03-22 18:40:07 +000022#include "lldb/Utility/FileSpec.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000023#include "lldb/lldb-types.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000024
Pavel Labath8c8ff7a2015-05-11 10:03:10 +000025#include "NativeThreadLinux.h"
Ravitheja Addepally99e37692017-06-28 07:58:31 +000026#include "ProcessorTrace.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000027#include "lldb/Host/common/NativeProcessProtocol.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000028
Tamas Berghammerdb264a62015-03-31 09:52:22 +000029namespace lldb_private {
Zachary Turner97206d52017-05-12 04:51:55 +000030class Status;
Kate Stoneb9c1b512016-09-06 20:57:50 +000031class Scalar;
Todd Fialaaf245d12014-06-30 21:05:18 +000032
Tamas Berghammerdb264a62015-03-31 09:52:22 +000033namespace process_linux {
Kate Stoneb9c1b512016-09-06 20:57:50 +000034/// @class NativeProcessLinux
35/// @brief Manages communication with the inferior (debugee) process.
36///
37/// Upon construction, this class prepares and launches an inferior process for
38/// debugging.
39///
40/// Changes in the inferior process state are broadcasted.
41class NativeProcessLinux : public NativeProcessProtocol {
Kate Stoneb9c1b512016-09-06 20:57:50 +000042public:
Pavel Labath96e600f2017-07-07 11:02:19 +000043 class Factory : public NativeProcessProtocol::Factory {
44 public:
45 llvm::Expected<NativeProcessProtocolSP>
46 Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate,
47 MainLoop &mainloop) const override;
48
49 llvm::Expected<NativeProcessProtocolSP>
50 Attach(lldb::pid_t pid, NativeDelegate &native_delegate,
51 MainLoop &mainloop) const override;
52 };
53
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 // ---------------------------------------------------------------------
55 // NativeProcessProtocol Interface
56 // ---------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +000057 Status Resume(const ResumeActionList &resume_actions) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000058
Zachary Turner97206d52017-05-12 04:51:55 +000059 Status Halt() override;
Todd Fialaaf245d12014-06-30 21:05:18 +000060
Zachary Turner97206d52017-05-12 04:51:55 +000061 Status Detach() override;
Todd Fialaaf245d12014-06-30 21:05:18 +000062
Zachary Turner97206d52017-05-12 04:51:55 +000063 Status Signal(int signo) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000064
Zachary Turner97206d52017-05-12 04:51:55 +000065 Status Interrupt() override;
Chaoren Line9547b82015-02-03 01:51:00 +000066
Zachary Turner97206d52017-05-12 04:51:55 +000067 Status Kill() override;
Todd Fialaaf245d12014-06-30 21:05:18 +000068
Zachary Turner97206d52017-05-12 04:51:55 +000069 Status GetMemoryRegionInfo(lldb::addr_t load_addr,
70 MemoryRegionInfo &range_info) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000071
Zachary Turner97206d52017-05-12 04:51:55 +000072 Status ReadMemory(lldb::addr_t addr, void *buf, size_t size,
73 size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000074
Zachary Turner97206d52017-05-12 04:51:55 +000075 Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size,
76 size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000077
Zachary Turner97206d52017-05-12 04:51:55 +000078 Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
79 size_t &bytes_written) override;
Chaoren Lin3eb4b452015-04-29 17:24:48 +000080
Zachary Turner97206d52017-05-12 04:51:55 +000081 Status AllocateMemory(size_t size, uint32_t permissions,
82 lldb::addr_t &addr) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000083
Zachary Turner97206d52017-05-12 04:51:55 +000084 Status DeallocateMemory(lldb::addr_t addr) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 lldb::addr_t GetSharedLibraryInfoAddress() override;
Todd Fialaaf245d12014-06-30 21:05:18 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 size_t UpdateThreads() override;
Todd Fialaaf245d12014-06-30 21:05:18 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 bool GetArchitecture(ArchSpec &arch) const override;
Todd Fialaaf245d12014-06-30 21:05:18 +000091
Zachary Turner97206d52017-05-12 04:51:55 +000092 Status SetBreakpoint(lldb::addr_t addr, uint32_t size,
93 bool hardware) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000094
Zachary Turner97206d52017-05-12 04:51:55 +000095 Status RemoveBreakpoint(lldb::addr_t addr, bool hardware = false) override;
Omair Javaidd5ffbad2017-02-24 13:27:31 +000096
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 void DoStopIDBumped(uint32_t newBumpId) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000098
Zachary Turner97206d52017-05-12 04:51:55 +000099 Status GetLoadedModuleFileSpec(const char *module_path,
100 FileSpec &file_spec) override;
Tamas Berghammer068f8a72015-05-26 11:58:52 +0000101
Zachary Turner97206d52017-05-12 04:51:55 +0000102 Status GetFileLoadAddress(const llvm::StringRef &file_name,
103 lldb::addr_t &load_addr) override;
Tamas Berghammer783bfc82015-06-18 20:43:56 +0000104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 NativeThreadLinuxSP GetThreadByID(lldb::tid_t id);
Pavel Labathf9077782015-08-21 09:13:53 +0000106
Pavel Labathb7f0f452017-03-17 11:08:40 +0000107 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
108 GetAuxvData() const override {
109 return getProcFile(GetID(), "auxv");
110 }
111
Ravitheja Addepally99e37692017-06-28 07:58:31 +0000112 lldb::user_id_t StartTrace(const TraceOptions &config,
113 Status &error) override;
114
115 Status StopTrace(lldb::user_id_t traceid,
116 lldb::tid_t thread) override;
117
118 Status GetData(lldb::user_id_t traceid, lldb::tid_t thread,
119 llvm::MutableArrayRef<uint8_t> &buffer,
120 size_t offset = 0) override;
121
122 Status GetMetaData(lldb::user_id_t traceid, lldb::tid_t thread,
123 llvm::MutableArrayRef<uint8_t> &buffer,
124 size_t offset = 0) override;
125
126 Status GetTraceConfig(lldb::user_id_t traceid, TraceOptions &config) override;
127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 // ---------------------------------------------------------------------
129 // Interface used by NativeRegisterContext-derived classes.
130 // ---------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000131 static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr,
132 void *data = nullptr, size_t data_size = 0,
133 long *result = nullptr);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 bool SupportHardwareSingleStepping() const;
Pavel Labath605b51b2016-02-23 13:56:30 +0000136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137protected:
138 // ---------------------------------------------------------------------
139 // NativeProcessProtocol protected interface
140 // ---------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000141 Status
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 GetSoftwareBreakpointTrapOpcode(size_t trap_opcode_size_hint,
143 size_t &actual_opcode_size,
144 const uint8_t *&trap_opcode_bytes) override;
Todd Fialaaf245d12014-06-30 21:05:18 +0000145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146private:
147 MainLoop::SignalHandleUP m_sigchld_handle;
148 ArchSpec m_arch;
Todd Fialaaf245d12014-06-30 21:05:18 +0000149
Pavel Labath96e600f2017-07-07 11:02:19 +0000150 LazyBool m_supports_mem_region = eLazyBoolCalculate;
Tamas Berghammera6f57952017-01-03 16:29:43 +0000151 std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache;
Todd Fialaaf245d12014-06-30 21:05:18 +0000152
Pavel Labath96e600f2017-07-07 11:02:19 +0000153 lldb::tid_t m_pending_notification_tid = LLDB_INVALID_THREAD_ID;
Todd Fialaaf245d12014-06-30 21:05:18 +0000154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 // List of thread ids stepping with a breakpoint with the address of
156 // the relevan breakpoint
157 std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
Pavel Labath0e1d7292015-08-20 09:06:12 +0000158
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 // ---------------------------------------------------------------------
160 // Private Instance Methods
161 // ---------------------------------------------------------------------
Pavel Labath96e600f2017-07-07 11:02:19 +0000162 NativeProcessLinux(::pid_t pid, int terminal_fd, NativeDelegate &delegate,
163 const ArchSpec &arch, MainLoop &mainloop);
Tamas Berghammerd8c338d2015-04-15 09:47:02 +0000164
Pavel Labath96e600f2017-07-07 11:02:19 +0000165 // Returns a list of process threads that we have attached to.
166 static llvm::Expected<std::vector<::pid_t>> Attach(::pid_t pid);
Todd Fialaaf245d12014-06-30 21:05:18 +0000167
Zachary Turner97206d52017-05-12 04:51:55 +0000168 static Status SetDefaultPtraceOpts(const lldb::pid_t);
Pavel Labath1107b5a2015-04-17 14:07:49 +0000169
Pavel Labath96e600f2017-07-07 11:02:19 +0000170 void InitializeThreads(llvm::ArrayRef<::pid_t> tids);
Todd Fialaaf245d12014-06-30 21:05:18 +0000171
Pavel Labath3508fc82017-06-19 12:47:50 +0000172 void MonitorCallback(lldb::pid_t pid, bool exited, WaitStatus status);
Todd Fialaaf245d12014-06-30 21:05:18 +0000173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 void WaitForNewThread(::pid_t tid);
Pavel Labath1107b5a2015-04-17 14:07:49 +0000175
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176 void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread);
Todd Fialaaf245d12014-06-30 21:05:18 +0000177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 void MonitorTrace(NativeThreadLinux &thread);
Pavel Labath426bdf82015-04-28 07:51:52 +0000179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 void MonitorBreakpoint(NativeThreadLinux &thread);
Todd Fialaaf245d12014-06-30 21:05:18 +0000181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index);
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread,
185 bool exited);
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000186
Zachary Turner97206d52017-05-12 04:51:55 +0000187 Status SetupSoftwareSingleStepping(NativeThreadLinux &thread);
Tamas Berghammere7708682015-04-22 10:00:23 +0000188
Todd Fialaaf245d12014-06-30 21:05:18 +0000189#if 0
190 static ::ProcessMessage::CrashReason
191 GetCrashReasonForSIGSEGV(const siginfo_t *info);
192
193 static ::ProcessMessage::CrashReason
194 GetCrashReasonForSIGILL(const siginfo_t *info);
195
196 static ::ProcessMessage::CrashReason
197 GetCrashReasonForSIGFPE(const siginfo_t *info);
198
199 static ::ProcessMessage::CrashReason
200 GetCrashReasonForSIGBUS(const siginfo_t *info);
201#endif
202
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 bool HasThreadNoLock(lldb::tid_t thread_id);
Todd Fialaaf245d12014-06-30 21:05:18 +0000204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 bool StopTrackingThread(lldb::tid_t thread_id);
Todd Fialaaf245d12014-06-30 21:05:18 +0000206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 NativeThreadLinuxSP AddThread(lldb::tid_t thread_id);
Todd Fialaaf245d12014-06-30 21:05:18 +0000208
Zachary Turner97206d52017-05-12 04:51:55 +0000209 Status GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size);
Todd Fialaaf245d12014-06-30 21:05:18 +0000210
Zachary Turner97206d52017-05-12 04:51:55 +0000211 Status FixupBreakpointPCAsNeeded(NativeThreadLinux &thread);
Todd Fialaaf245d12014-06-30 21:05:18 +0000212
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 /// Writes a siginfo_t structure corresponding to the given thread ID to the
214 /// memory region pointed to by @p siginfo.
Zachary Turner97206d52017-05-12 04:51:55 +0000215 Status GetSignalInfo(lldb::tid_t tid, void *siginfo);
Todd Fialaaf245d12014-06-30 21:05:18 +0000216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
218 /// corresponding to the given thread ID to the memory pointed to by @p
219 /// message.
Zachary Turner97206d52017-05-12 04:51:55 +0000220 Status GetEventMessage(lldb::tid_t tid, unsigned long *message);
Todd Fialaaf245d12014-06-30 21:05:18 +0000221
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 void NotifyThreadDeath(lldb::tid_t tid);
Chaoren Linfa03ad22015-02-03 01:50:42 +0000223
Zachary Turner97206d52017-05-12 04:51:55 +0000224 Status Detach(lldb::tid_t tid);
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 // This method is requests a stop on all threads which are still running. It
227 // sets up a
228 // deferred delegate notification, which will fire once threads report as
229 // stopped. The
230 // triggerring_tid will be set as the current thread (main stop reason).
231 void StopRunningThreads(lldb::tid_t triggering_tid);
Pavel Labathc0765592015-05-06 10:46:34 +0000232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 // Notify the delegate if all threads have stopped.
234 void SignalIfAllThreadsStopped();
Pavel Labathc0765592015-05-06 10:46:34 +0000235
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236 // Resume the given thread, optionally passing it the given signal. The type
237 // of resume
238 // operation (continue, single-step) depends on the state parameter.
Zachary Turner97206d52017-05-12 04:51:55 +0000239 Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state,
240 int signo);
Pavel Labathc0765592015-05-06 10:46:34 +0000241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 void ThreadWasCreated(NativeThreadLinux &thread);
Pavel Labathc0765592015-05-06 10:46:34 +0000243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 void SigchldHandler();
Tamas Berghammera6f57952017-01-03 16:29:43 +0000245
Zachary Turner97206d52017-05-12 04:51:55 +0000246 Status PopulateMemoryRegionCache();
Ravitheja Addepally99e37692017-06-28 07:58:31 +0000247
248 lldb::user_id_t StartTraceGroup(const TraceOptions &config,
249 Status &error);
250
251 // This function is intended to be used to stop tracing
252 // on a thread that exited.
253 Status StopTracingForThread(lldb::tid_t thread);
254
255 // The below function as the name suggests, looks up a ProcessorTrace
256 // instance from the m_processor_trace_monitor map. In the case of
257 // process tracing where the traceid passed would map to the complete
258 // process, it is mandatory to provide a threadid to obtain a trace
259 // instance (since ProcessorTrace is tied to a thread). In the other
260 // scenario that an individual thread is being traced, just the traceid
261 // is sufficient to obtain the actual ProcessorTrace instance.
262 llvm::Expected<ProcessorTraceMonitor &>
263 LookupProcessorTraceInstance(lldb::user_id_t traceid, lldb::tid_t thread);
264
265 // Stops tracing on individual threads being traced. Not intended
266 // to be used to stop tracing on complete process.
267 Status StopProcessorTracingOnThread(lldb::user_id_t traceid,
268 lldb::tid_t thread);
269
270 // Intended to stop tracing on complete process.
271 // Should not be used for stopping trace on
272 // individual threads.
273 void StopProcessorTracingOnProcess();
274
275 llvm::DenseMap<lldb::tid_t, ProcessorTraceMonitorUP>
276 m_processor_trace_monitor;
277
278 // Set for tracking threads being traced under
279 // same process user id.
280 llvm::DenseSet<lldb::tid_t> m_pt_traced_thread_group;
281
Pavel Labath96e600f2017-07-07 11:02:19 +0000282 lldb::user_id_t m_pt_proces_trace_id = LLDB_INVALID_UID;
Ravitheja Addepally99e37692017-06-28 07:58:31 +0000283 TraceOptions m_pt_process_trace_config;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284};
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000285
286} // namespace process_linux
287} // namespace lldb_private
Todd Fialaaf245d12014-06-30 21:05:18 +0000288
289#endif // #ifndef liblldb_NativeProcessLinux_H_