blob: d72217ef0cc663f558e1ea5b0cdc27651c2c3f70 [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"
18#include "lldb/lldb-types.h"
19#include "lldb/Host/Debug.h"
Chaoren Lind3173f32015-05-29 19:52:29 +000020#include "lldb/Host/FileSpec.h"
Zachary Turner39de3112014-09-09 20:54:56 +000021#include "lldb/Host/HostThread.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000022#include "lldb/Host/Mutex.h"
23#include "lldb/Target/MemoryRegionInfo.h"
24
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000025#include "lldb/Host/common/NativeProcessProtocol.h"
Pavel Labath8c8ff7a2015-05-11 10:03:10 +000026#include "NativeThreadLinux.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000027
Tamas Berghammerdb264a62015-03-31 09:52:22 +000028namespace lldb_private {
Todd Fialaaf245d12014-06-30 21:05:18 +000029 class Error;
30 class Module;
31 class Scalar;
32
Tamas Berghammerdb264a62015-03-31 09:52:22 +000033namespace process_linux {
Todd Fialaaf245d12014-06-30 21:05:18 +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.
41 class NativeProcessLinux: public NativeProcessProtocol
42 {
43 public:
44
Tamas Berghammerdb264a62015-03-31 09:52:22 +000045 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +000046 LaunchProcess (
47 Module *exe_module,
48 ProcessLaunchInfo &launch_info,
Tamas Berghammerdb264a62015-03-31 09:52:22 +000049 NativeProcessProtocol::NativeDelegate &native_delegate,
Todd Fialaaf245d12014-06-30 21:05:18 +000050 NativeProcessProtocolSP &native_process_sp);
51
Tamas Berghammerdb264a62015-03-31 09:52:22 +000052 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +000053 AttachToProcess (
54 lldb::pid_t pid,
Tamas Berghammerdb264a62015-03-31 09:52:22 +000055 NativeProcessProtocol::NativeDelegate &native_delegate,
Todd Fialaaf245d12014-06-30 21:05:18 +000056 NativeProcessProtocolSP &native_process_sp);
57
Tamas Berghammer068f8a72015-05-26 11:58:52 +000058 //------------------------------------------------------------------------------
59 /// @class Operation
60 /// @brief Represents a NativeProcessLinux operation.
61 ///
62 /// Under Linux, it is not possible to ptrace() from any other thread but the
63 /// one that spawned or attached to the process from the start. Therefore, when
64 /// a NativeProcessLinux is asked to deliver or change the state of an inferior
65 /// process the operation must be "funneled" to a specific thread to perform the
Pavel Labathc7512fd2015-06-26 10:14:12 +000066 /// task.
67 typedef std::function<Error()> Operation;
Tamas Berghammer068f8a72015-05-26 11:58:52 +000068
Todd Fialaaf245d12014-06-30 21:05:18 +000069 // ---------------------------------------------------------------------
Todd Fialaaf245d12014-06-30 21:05:18 +000070 // NativeProcessProtocol Interface
71 // ---------------------------------------------------------------------
72 Error
73 Resume (const ResumeActionList &resume_actions) override;
74
75 Error
76 Halt () override;
77
78 Error
79 Detach () override;
80
81 Error
82 Signal (int signo) override;
83
84 Error
Chaoren Line9547b82015-02-03 01:51:00 +000085 Interrupt () override;
86
87 Error
Todd Fialaaf245d12014-06-30 21:05:18 +000088 Kill () override;
89
90 Error
91 GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override;
92
93 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000094 ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000095
96 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000097 ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000098
99 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +0000100 WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override;
101
102 Error
103 AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) override;
Todd Fialaaf245d12014-06-30 21:05:18 +0000104
105 Error
106 DeallocateMemory (lldb::addr_t addr) override;
107
108 lldb::addr_t
109 GetSharedLibraryInfoAddress () override;
110
111 size_t
112 UpdateThreads () override;
113
114 bool
115 GetArchitecture (ArchSpec &arch) const override;
116
117 Error
118 SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override;
119
Pavel Labath45f5cb32015-05-05 15:05:50 +0000120 Error
121 SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) override;
122
123 Error
124 RemoveWatchpoint (lldb::addr_t addr) override;
125
Todd Fialaaf245d12014-06-30 21:05:18 +0000126 void
127 DoStopIDBumped (uint32_t newBumpId) override;
128
Oleksiy Vyalov8bc34f42015-02-19 17:58:04 +0000129 void
130 Terminate () override;
131
Tamas Berghammer068f8a72015-05-26 11:58:52 +0000132 Error
133 GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override;
134
Tamas Berghammer783bfc82015-06-18 20:43:56 +0000135 Error
136 GetFileLoadAddress(const llvm::StringRef& file_name, lldb::addr_t& load_addr) override;
137
Todd Fialaaf245d12014-06-30 21:05:18 +0000138 // ---------------------------------------------------------------------
139 // Interface used by NativeRegisterContext-derived classes.
140 // ---------------------------------------------------------------------
Chaoren Lin97ccc292015-02-03 01:51:12 +0000141 Error
Pavel Labathc7512fd2015-06-26 10:14:12 +0000142 DoOperation(const Operation &op);
Tamas Berghammer068f8a72015-05-26 11:58:52 +0000143
144 static long
145 PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, Error& error);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000146
Todd Fialaaf245d12014-06-30 21:05:18 +0000147 protected:
148 // ---------------------------------------------------------------------
149 // NativeProcessProtocol protected interface
150 // ---------------------------------------------------------------------
151 Error
152 GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override;
153
154 private:
155
Pavel Labath1107b5a2015-04-17 14:07:49 +0000156 class Monitor;
157
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000158 ArchSpec m_arch;
Todd Fialaaf245d12014-06-30 21:05:18 +0000159
Pavel Labath1107b5a2015-04-17 14:07:49 +0000160 std::unique_ptr<Monitor> m_monitor_up;
Todd Fialaaf245d12014-06-30 21:05:18 +0000161
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000162 LazyBool m_supports_mem_region;
Todd Fialaaf245d12014-06-30 21:05:18 +0000163 std::vector<MemoryRegionInfo> m_mem_region_cache;
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000164 Mutex m_mem_region_cache_mutex;
Todd Fialaaf245d12014-06-30 21:05:18 +0000165
Tamas Berghammerd8c338d2015-04-15 09:47:02 +0000166 // List of thread ids stepping with a breakpoint with the address of
167 // the relevan breakpoint
168 std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
169
Todd Fialaaf245d12014-06-30 21:05:18 +0000170 /// @class LauchArgs
171 ///
172 /// @brief Simple structure to pass data to the thread responsible for
173 /// launching a child process.
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000174 struct LaunchArgs
Todd Fialaaf245d12014-06-30 21:05:18 +0000175 {
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000176 LaunchArgs(Module *module,
Todd Fialaaf245d12014-06-30 21:05:18 +0000177 char const **argv,
178 char const **envp,
Chaoren Lind3173f32015-05-29 19:52:29 +0000179 const FileSpec &stdin_file_spec,
180 const FileSpec &stdout_file_spec,
181 const FileSpec &stderr_file_spec,
182 const FileSpec &working_dir,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000183 const ProcessLaunchInfo &launch_info);
Todd Fialaaf245d12014-06-30 21:05:18 +0000184
185 ~LaunchArgs();
186
Chaoren Lind3173f32015-05-29 19:52:29 +0000187 Module *m_module; // The executable image to launch.
188 char const **m_argv; // Process arguments.
189 char const **m_envp; // Process environment.
190 const FileSpec m_stdin_file_spec; // Redirect stdin if not empty.
191 const FileSpec m_stdout_file_spec; // Redirect stdout if not empty.
192 const FileSpec m_stderr_file_spec; // Redirect stderr if not empty.
193 const FileSpec m_working_dir; // Working directory or empty.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000194 const ProcessLaunchInfo &m_launch_info;
Todd Fialaaf245d12014-06-30 21:05:18 +0000195 };
196
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000197 typedef std::function<::pid_t(Error &)> InitialOperation;
Todd Fialaaf245d12014-06-30 21:05:18 +0000198
199 // ---------------------------------------------------------------------
200 // Private Instance Methods
201 // ---------------------------------------------------------------------
202 NativeProcessLinux ();
203
204 /// Launches an inferior process ready for debugging. Forms the
205 /// implementation of Process::DoLaunch.
206 void
207 LaunchInferior (
208 Module *module,
209 char const *argv[],
210 char const *envp[],
Chaoren Lind3173f32015-05-29 19:52:29 +0000211 const FileSpec &stdin_file_spec,
212 const FileSpec &stdout_file_spec,
213 const FileSpec &stderr_file_spec,
214 const FileSpec &working_dir,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000215 const ProcessLaunchInfo &launch_info,
Todd Fialaaf245d12014-06-30 21:05:18 +0000216 Error &error);
217
218 /// Attaches to an existing process. Forms the
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +0000219 /// implementation of Process::DoAttach
Todd Fialaaf245d12014-06-30 21:05:18 +0000220 void
221 AttachToInferior (lldb::pid_t pid, Error &error);
222
223 void
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000224 StartMonitorThread(const InitialOperation &operation, Error &error);
Pavel Labath1107b5a2015-04-17 14:07:49 +0000225
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000226 ::pid_t
227 Launch(LaunchArgs *args, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000228
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000229 ::pid_t
230 Attach(lldb::pid_t pid, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000231
Chaoren Lin97ccc292015-02-03 01:51:12 +0000232 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000233 SetDefaultPtraceOpts(const lldb::pid_t);
234
Todd Fialaaf245d12014-06-30 21:05:18 +0000235 static bool
Chaoren Lind3173f32015-05-29 19:52:29 +0000236 DupDescriptor(const FileSpec &file_spec, int fd, int flags);
Todd Fialaaf245d12014-06-30 21:05:18 +0000237
Pavel Labath1107b5a2015-04-17 14:07:49 +0000238 static void *
239 MonitorThread(void *baton);
240
241 void
242 MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status);
Todd Fialaaf245d12014-06-30 21:05:18 +0000243
244 void
Pavel Labath426bdf82015-04-28 07:51:52 +0000245 WaitForNewThread(::pid_t tid);
246
247 void
Todd Fialaaf245d12014-06-30 21:05:18 +0000248 MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid);
249
250 void
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000251 MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp);
252
253 void
254 MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp);
255
256 void
257 MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index);
258
259 void
Todd Fialaaf245d12014-06-30 21:05:18 +0000260 MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited);
261
Tamas Berghammere7708682015-04-22 10:00:23 +0000262 bool
263 SupportHardwareSingleStepping() const;
264
265 Error
266 SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp);
267
Todd Fialaaf245d12014-06-30 21:05:18 +0000268#if 0
269 static ::ProcessMessage::CrashReason
270 GetCrashReasonForSIGSEGV(const siginfo_t *info);
271
272 static ::ProcessMessage::CrashReason
273 GetCrashReasonForSIGILL(const siginfo_t *info);
274
275 static ::ProcessMessage::CrashReason
276 GetCrashReasonForSIGFPE(const siginfo_t *info);
277
278 static ::ProcessMessage::CrashReason
279 GetCrashReasonForSIGBUS(const siginfo_t *info);
280#endif
281
Todd Fialaaf245d12014-06-30 21:05:18 +0000282 bool
283 HasThreadNoLock (lldb::tid_t thread_id);
284
285 NativeThreadProtocolSP
286 MaybeGetThreadNoLock (lldb::tid_t thread_id);
287
288 bool
289 StopTrackingThread (lldb::tid_t thread_id);
290
291 NativeThreadProtocolSP
292 AddThread (lldb::tid_t thread_id);
293
Todd Fialaaf245d12014-06-30 21:05:18 +0000294 Error
Tamas Berghammer63c8be92015-04-15 09:38:48 +0000295 GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size);
Todd Fialaaf245d12014-06-30 21:05:18 +0000296
297 Error
298 FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp);
299
300 /// Writes a siginfo_t structure corresponding to the given thread ID to the
301 /// memory region pointed to by @p siginfo.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000302 Error
303 GetSignalInfo(lldb::tid_t tid, void *siginfo);
Todd Fialaaf245d12014-06-30 21:05:18 +0000304
305 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
306 /// corresponding to the given thread ID to the memory pointed to by @p
307 /// message.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000308 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000309 GetEventMessage(lldb::tid_t tid, unsigned long *message);
310
311 /// Resumes the given thread. If @p signo is anything but
312 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000313 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000314 Resume(lldb::tid_t tid, uint32_t signo);
315
316 /// Single steps the given thread. If @p signo is anything but
317 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000318 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000319 SingleStep(lldb::tid_t tid, uint32_t signo);
320
Todd Fiala511e5cd2014-09-11 23:29:14 +0000321 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000322 NotifyThreadDeath (lldb::tid_t tid);
323
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000324 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000325 Detach(lldb::tid_t tid);
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000326
Pavel Labathc0765592015-05-06 10:46:34 +0000327
Pavel Labathc0765592015-05-06 10:46:34 +0000328 // Typedefs.
329 typedef std::unordered_set<lldb::tid_t> ThreadIDSet;
330
Pavel Labath1dbc6c92015-05-12 08:35:33 +0000331 // This method is requests a stop on all threads which are still running. It sets up a
332 // deferred delegate notification, which will fire once threads report as stopped. The
333 // triggerring_tid will be set as the current thread (main stop reason).
Pavel Labathc0765592015-05-06 10:46:34 +0000334 void
Pavel Labath337f3eb2015-05-08 08:57:45 +0000335 StopRunningThreads(lldb::tid_t triggering_tid);
Pavel Labathc0765592015-05-06 10:46:34 +0000336
Pavel Labathc0765592015-05-06 10:46:34 +0000337 struct PendingNotification
338 {
Pavel Labath337f3eb2015-05-08 08:57:45 +0000339 PendingNotification (lldb::tid_t triggering_tid):
340 triggering_tid (triggering_tid),
Pavel Labath108c3252015-05-12 09:03:18 +0000341 wait_for_stop_tids ()
Pavel Labathc0765592015-05-06 10:46:34 +0000342 {
343 }
344
345 const lldb::tid_t triggering_tid;
346 ThreadIDSet wait_for_stop_tids;
Pavel Labathc0765592015-05-06 10:46:34 +0000347 };
348 typedef std::unique_ptr<PendingNotification> PendingNotificationUP;
349
Pavel Labath9eb1ecb2015-05-15 13:49:01 +0000350 // Notify the delegate if all threads have stopped.
351 void SignalIfAllThreadsStopped();
Pavel Labathc0765592015-05-06 10:46:34 +0000352
Pavel Labathc0765592015-05-06 10:46:34 +0000353 void
354 RequestStopOnAllRunningThreads();
355
Pavel Labath5eb721e2015-05-07 08:30:31 +0000356 Error
357 ThreadDidStop(lldb::tid_t tid, bool initiated_by_llgs);
Pavel Labathc0765592015-05-06 10:46:34 +0000358
Pavel Labath1dbc6c92015-05-12 08:35:33 +0000359 // Resume the thread with the given thread id using the request_thread_resume_function
360 // called. If error_when_already_running is then then an error is raised if we think this
361 // thread is already running.
Pavel Labath5eb721e2015-05-07 08:30:31 +0000362 Error
Pavel Labath1dbc6c92015-05-12 08:35:33 +0000363 ResumeThread(lldb::tid_t tid, NativeThreadLinux::ResumeThreadFunction request_thread_resume_function,
Pavel Labath5eb721e2015-05-07 08:30:31 +0000364 bool error_when_already_running);
Pavel Labathc0765592015-05-06 10:46:34 +0000365
366 void
Pavel Labathed89c7f2015-05-06 12:22:37 +0000367 DoStopThreads(PendingNotificationUP &&notification_up);
Pavel Labathc0765592015-05-06 10:46:34 +0000368
369 void
Pavel Labath8c8ff7a2015-05-11 10:03:10 +0000370 ThreadWasCreated (lldb::tid_t tid);
Pavel Labathc0765592015-05-06 10:46:34 +0000371
Pavel Labathc0765592015-05-06 10:46:34 +0000372 // Member variables.
Pavel Labathc0765592015-05-06 10:46:34 +0000373 PendingNotificationUP m_pending_notification_up;
Todd Fialaaf245d12014-06-30 21:05:18 +0000374 };
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000375
376} // namespace process_linux
377} // namespace lldb_private
Todd Fialaaf245d12014-06-30 21:05:18 +0000378
379#endif // #ifndef liblldb_NativeProcessLinux_H_