blob: 5c1f712d9b4f9003d5fd3cc8d67d7584547334e7 [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
13// C Includes
14#include <semaphore.h>
15#include <signal.h>
16
17// C++ Includes
18#include <unordered_set>
19
20// Other libraries and framework includes
21#include "lldb/Core/ArchSpec.h"
22#include "lldb/lldb-types.h"
23#include "lldb/Host/Debug.h"
Zachary Turner39de3112014-09-09 20:54:56 +000024#include "lldb/Host/HostThread.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000025#include "lldb/Host/Mutex.h"
26#include "lldb/Target/MemoryRegionInfo.h"
27
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000028#include "lldb/Host/common/NativeProcessProtocol.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000029
Tamas Berghammerdb264a62015-03-31 09:52:22 +000030namespace lldb_private {
Todd Fialaaf245d12014-06-30 21:05:18 +000031 class Error;
32 class Module;
33 class Scalar;
34
Tamas Berghammerdb264a62015-03-31 09:52:22 +000035namespace process_linux {
36 class ThreadStateCoordinator;
37
Todd Fialaaf245d12014-06-30 21:05:18 +000038 /// @class NativeProcessLinux
39 /// @brief Manages communication with the inferior (debugee) process.
40 ///
41 /// Upon construction, this class prepares and launches an inferior process for
42 /// debugging.
43 ///
44 /// Changes in the inferior process state are broadcasted.
45 class NativeProcessLinux: public NativeProcessProtocol
46 {
47 public:
48
Tamas Berghammerdb264a62015-03-31 09:52:22 +000049 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +000050 LaunchProcess (
51 Module *exe_module,
52 ProcessLaunchInfo &launch_info,
Tamas Berghammerdb264a62015-03-31 09:52:22 +000053 NativeProcessProtocol::NativeDelegate &native_delegate,
Todd Fialaaf245d12014-06-30 21:05:18 +000054 NativeProcessProtocolSP &native_process_sp);
55
Tamas Berghammerdb264a62015-03-31 09:52:22 +000056 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +000057 AttachToProcess (
58 lldb::pid_t pid,
Tamas Berghammerdb264a62015-03-31 09:52:22 +000059 NativeProcessProtocol::NativeDelegate &native_delegate,
Todd Fialaaf245d12014-06-30 21:05:18 +000060 NativeProcessProtocolSP &native_process_sp);
61
62 // ---------------------------------------------------------------------
Todd Fialaaf245d12014-06-30 21:05:18 +000063 // NativeProcessProtocol Interface
64 // ---------------------------------------------------------------------
65 Error
66 Resume (const ResumeActionList &resume_actions) override;
67
68 Error
69 Halt () override;
70
71 Error
72 Detach () override;
73
74 Error
75 Signal (int signo) override;
76
77 Error
Chaoren Line9547b82015-02-03 01:51:00 +000078 Interrupt () override;
79
80 Error
Todd Fialaaf245d12014-06-30 21:05:18 +000081 Kill () override;
82
83 Error
84 GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override;
85
86 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000087 ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000088
89 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000090 ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000091
92 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000093 WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override;
94
95 Error
96 AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000097
98 Error
99 DeallocateMemory (lldb::addr_t addr) override;
100
101 lldb::addr_t
102 GetSharedLibraryInfoAddress () override;
103
104 size_t
105 UpdateThreads () override;
106
107 bool
108 GetArchitecture (ArchSpec &arch) const override;
109
110 Error
111 SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override;
112
113 void
114 DoStopIDBumped (uint32_t newBumpId) override;
115
Oleksiy Vyalov8bc34f42015-02-19 17:58:04 +0000116 void
117 Terminate () override;
118
Todd Fialaaf245d12014-06-30 21:05:18 +0000119 // ---------------------------------------------------------------------
120 // Interface used by NativeRegisterContext-derived classes.
121 // ---------------------------------------------------------------------
122
123 /// Reads the contents from the register identified by the given (architecture
124 /// dependent) offset.
125 ///
126 /// This method is provided for use by RegisterContextLinux derivatives.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000127 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000128 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000129 unsigned size, RegisterValue &value);
Todd Fialaaf245d12014-06-30 21:05:18 +0000130
131 /// Writes the given value to the register identified by the given
132 /// (architecture dependent) offset.
133 ///
134 /// This method is provided for use by RegisterContextLinux derivatives.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000135 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000136 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000137 const RegisterValue &value);
Todd Fialaaf245d12014-06-30 21:05:18 +0000138
139 /// Reads all general purpose registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000140 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000141 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
142
143 /// Reads generic floating point registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000144 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000145 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
146
147 /// Reads the specified register set into the specified buffer.
148 /// For instance, the extended floating-point register set.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000149 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000150 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
151
152 /// Writes all general purpose registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000153 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000154 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
155
156 /// Writes generic floating point registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000157 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000158 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
159
160 /// Writes the specified register set into the specified buffer.
161 /// For instance, the extended floating-point register set.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000162 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000163 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000164
165 Error
166 GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override;
167
Todd Fialaaf245d12014-06-30 21:05:18 +0000168 protected:
169 // ---------------------------------------------------------------------
170 // NativeProcessProtocol protected interface
171 // ---------------------------------------------------------------------
172 Error
173 GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override;
174
175 private:
176
Pavel Labath1107b5a2015-04-17 14:07:49 +0000177 class Monitor;
178
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000179 ArchSpec m_arch;
Todd Fialaaf245d12014-06-30 21:05:18 +0000180
Pavel Labath1107b5a2015-04-17 14:07:49 +0000181 std::unique_ptr<Monitor> m_monitor_up;
Todd Fialaaf245d12014-06-30 21:05:18 +0000182
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000183 LazyBool m_supports_mem_region;
Todd Fialaaf245d12014-06-30 21:05:18 +0000184 std::vector<MemoryRegionInfo> m_mem_region_cache;
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000185 Mutex m_mem_region_cache_mutex;
Todd Fialaaf245d12014-06-30 21:05:18 +0000186
Chaoren Linfa03ad22015-02-03 01:50:42 +0000187 std::unique_ptr<ThreadStateCoordinator> m_coordinator_up;
188 HostThread m_coordinator_thread;
Todd Fialaaf245d12014-06-30 21:05:18 +0000189
Tamas Berghammerd8c338d2015-04-15 09:47:02 +0000190 // List of thread ids stepping with a breakpoint with the address of
191 // the relevan breakpoint
192 std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
193
Todd Fialaaf245d12014-06-30 21:05:18 +0000194 /// @class LauchArgs
195 ///
196 /// @brief Simple structure to pass data to the thread responsible for
197 /// launching a child process.
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000198 struct LaunchArgs
Todd Fialaaf245d12014-06-30 21:05:18 +0000199 {
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000200 LaunchArgs(Module *module,
Todd Fialaaf245d12014-06-30 21:05:18 +0000201 char const **argv,
202 char const **envp,
Todd Fiala75f47c32014-10-11 21:42:09 +0000203 const std::string &stdin_path,
204 const std::string &stdout_path,
205 const std::string &stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000206 const char *working_dir,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000207 const ProcessLaunchInfo &launch_info);
Todd Fialaaf245d12014-06-30 21:05:18 +0000208
209 ~LaunchArgs();
210
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000211 Module *m_module; // The executable image to launch.
212 char const **m_argv; // Process arguments.
213 char const **m_envp; // Process environment.
Todd Fiala75f47c32014-10-11 21:42:09 +0000214 const std::string &m_stdin_path; // Redirect stdin if not empty.
215 const std::string &m_stdout_path; // Redirect stdout if not empty.
216 const std::string &m_stderr_path; // Redirect stderr if not empty.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000217 const char *m_working_dir; // Working directory or NULL.
218 const ProcessLaunchInfo &m_launch_info;
Todd Fialaaf245d12014-06-30 21:05:18 +0000219 };
220
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000221 typedef std::function<::pid_t(Error &)> InitialOperation;
Todd Fialaaf245d12014-06-30 21:05:18 +0000222
223 // ---------------------------------------------------------------------
224 // Private Instance Methods
225 // ---------------------------------------------------------------------
226 NativeProcessLinux ();
227
228 /// Launches an inferior process ready for debugging. Forms the
229 /// implementation of Process::DoLaunch.
230 void
231 LaunchInferior (
232 Module *module,
233 char const *argv[],
234 char const *envp[],
Todd Fiala75f47c32014-10-11 21:42:09 +0000235 const std::string &stdin_path,
236 const std::string &stdout_path,
237 const std::string &stderr_path,
Todd Fialaaf245d12014-06-30 21:05:18 +0000238 const char *working_dir,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000239 const ProcessLaunchInfo &launch_info,
Todd Fialaaf245d12014-06-30 21:05:18 +0000240 Error &error);
241
242 /// Attaches to an existing process. Forms the
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +0000243 /// implementation of Process::DoAttach
Todd Fialaaf245d12014-06-30 21:05:18 +0000244 void
245 AttachToInferior (lldb::pid_t pid, Error &error);
246
247 void
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000248 StartMonitorThread(const InitialOperation &operation, Error &error);
Pavel Labath1107b5a2015-04-17 14:07:49 +0000249
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000250 ::pid_t
251 Launch(LaunchArgs *args, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000252
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000253 ::pid_t
254 Attach(lldb::pid_t pid, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000255
Chaoren Lin97ccc292015-02-03 01:51:12 +0000256 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000257 SetDefaultPtraceOpts(const lldb::pid_t);
258
Todd Fialaaf245d12014-06-30 21:05:18 +0000259 static bool
260 DupDescriptor(const char *path, int fd, int flags);
261
Pavel Labath1107b5a2015-04-17 14:07:49 +0000262 static void *
263 MonitorThread(void *baton);
264
265 void
266 MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status);
Todd Fialaaf245d12014-06-30 21:05:18 +0000267
268 void
Pavel Labath426bdf82015-04-28 07:51:52 +0000269 WaitForNewThread(::pid_t tid);
270
271 void
Todd Fialaaf245d12014-06-30 21:05:18 +0000272 MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid);
273
274 void
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000275 MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp);
276
277 void
278 MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp);
279
280 void
281 MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index);
282
283 void
Todd Fialaaf245d12014-06-30 21:05:18 +0000284 MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited);
285
Tamas Berghammere7708682015-04-22 10:00:23 +0000286 bool
287 SupportHardwareSingleStepping() const;
288
289 Error
290 SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp);
291
Todd Fialaaf245d12014-06-30 21:05:18 +0000292#if 0
293 static ::ProcessMessage::CrashReason
294 GetCrashReasonForSIGSEGV(const siginfo_t *info);
295
296 static ::ProcessMessage::CrashReason
297 GetCrashReasonForSIGILL(const siginfo_t *info);
298
299 static ::ProcessMessage::CrashReason
300 GetCrashReasonForSIGFPE(const siginfo_t *info);
301
302 static ::ProcessMessage::CrashReason
303 GetCrashReasonForSIGBUS(const siginfo_t *info);
304#endif
305
Chaoren Linfa03ad22015-02-03 01:50:42 +0000306 Error
307 StartCoordinatorThread ();
308
309 static void*
310 CoordinatorThread (void *arg);
311
312 void
313 StopCoordinatorThread ();
314
Todd Fialaaf245d12014-06-30 21:05:18 +0000315 /// Stops monitoring the child process thread.
316 void
317 StopMonitor();
318
319 bool
320 HasThreadNoLock (lldb::tid_t thread_id);
321
322 NativeThreadProtocolSP
323 MaybeGetThreadNoLock (lldb::tid_t thread_id);
324
325 bool
326 StopTrackingThread (lldb::tid_t thread_id);
327
328 NativeThreadProtocolSP
329 AddThread (lldb::tid_t thread_id);
330
Todd Fialaaf245d12014-06-30 21:05:18 +0000331 Error
Tamas Berghammer63c8be92015-04-15 09:38:48 +0000332 GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size);
Todd Fialaaf245d12014-06-30 21:05:18 +0000333
334 Error
335 FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp);
336
337 /// Writes a siginfo_t structure corresponding to the given thread ID to the
338 /// memory region pointed to by @p siginfo.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000339 Error
340 GetSignalInfo(lldb::tid_t tid, void *siginfo);
Todd Fialaaf245d12014-06-30 21:05:18 +0000341
342 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
343 /// corresponding to the given thread ID to the memory pointed to by @p
344 /// message.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000345 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000346 GetEventMessage(lldb::tid_t tid, unsigned long *message);
347
348 /// Resumes the given thread. If @p signo is anything but
349 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000350 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000351 Resume(lldb::tid_t tid, uint32_t signo);
352
353 /// Single steps the given thread. If @p signo is anything but
354 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000355 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000356 SingleStep(lldb::tid_t tid, uint32_t signo);
357
Chaoren Linfa03ad22015-02-03 01:50:42 +0000358 // ThreadStateCoordinator helper methods.
Todd Fiala511e5cd2014-09-11 23:29:14 +0000359 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000360 NotifyThreadCreateStopped (lldb::tid_t tid);
Todd Fiala511e5cd2014-09-11 23:29:14 +0000361
362 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000363 NotifyThreadCreateRunning (lldb::tid_t tid);
364
365 void
366 NotifyThreadDeath (lldb::tid_t tid);
367
368 void
369 NotifyThreadStop (lldb::tid_t tid);
370
371 void
372 CallAfterRunningThreadsStop (lldb::tid_t tid,
373 const std::function<void (lldb::tid_t tid)> &call_after_function);
Todd Fiala511e5cd2014-09-11 23:29:14 +0000374
Chaoren Lin03f12d62015-02-03 01:50:49 +0000375 void
376 CallAfterRunningThreadsStopWithSkipTID (lldb::tid_t deferred_signal_tid,
377 lldb::tid_t skip_stop_request_tid,
378 const std::function<void (lldb::tid_t tid)> &call_after_function);
379
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000380 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000381 Detach(lldb::tid_t tid);
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000382
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000383 Error
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000384 RequestThreadStop (const lldb::pid_t pid, const lldb::tid_t tid);
Todd Fialaaf245d12014-06-30 21:05:18 +0000385 };
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000386
387} // namespace process_linux
388} // namespace lldb_private
Todd Fialaaf245d12014-06-30 21:05:18 +0000389
390#endif // #ifndef liblldb_NativeProcessLinux_H_