blob: 3c597b64e735de9937827306ea5435e0b111ec3c [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
87 ReadMemory (lldb::addr_t addr, void *buf, lldb::addr_t size, lldb::addr_t &bytes_read) override;
88
89 Error
90 WriteMemory (lldb::addr_t addr, const void *buf, lldb::addr_t size, lldb::addr_t &bytes_written) override;
91
92 Error
93 AllocateMemory (lldb::addr_t size, uint32_t permissions, lldb::addr_t &addr) override;
94
95 Error
96 DeallocateMemory (lldb::addr_t addr) override;
97
98 lldb::addr_t
99 GetSharedLibraryInfoAddress () override;
100
101 size_t
102 UpdateThreads () override;
103
104 bool
105 GetArchitecture (ArchSpec &arch) const override;
106
107 Error
108 SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override;
109
110 void
111 DoStopIDBumped (uint32_t newBumpId) override;
112
Oleksiy Vyalov8bc34f42015-02-19 17:58:04 +0000113 void
114 Terminate () override;
115
Todd Fialaaf245d12014-06-30 21:05:18 +0000116 // ---------------------------------------------------------------------
117 // Interface used by NativeRegisterContext-derived classes.
118 // ---------------------------------------------------------------------
119
120 /// Reads the contents from the register identified by the given (architecture
121 /// dependent) offset.
122 ///
123 /// This method is provided for use by RegisterContextLinux derivatives.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000124 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000125 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000126 unsigned size, RegisterValue &value);
Todd Fialaaf245d12014-06-30 21:05:18 +0000127
128 /// Writes the given value to the register identified by the given
129 /// (architecture dependent) offset.
130 ///
131 /// This method is provided for use by RegisterContextLinux derivatives.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000132 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000133 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000134 const RegisterValue &value);
Todd Fialaaf245d12014-06-30 21:05:18 +0000135
136 /// Reads all general purpose registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000137 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000138 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
139
140 /// Reads generic floating point registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000141 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000142 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
143
144 /// Reads the specified register set into the specified buffer.
145 /// For instance, the extended floating-point register set.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000146 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000147 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
148
149 /// Writes all general purpose registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000150 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000151 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
152
153 /// Writes generic floating point registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000154 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000155 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
156
157 /// Writes the specified register set into the specified buffer.
158 /// For instance, the extended floating-point register set.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000159 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000160 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000161
162 Error
163 GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override;
164
Todd Fialaaf245d12014-06-30 21:05:18 +0000165 protected:
166 // ---------------------------------------------------------------------
167 // NativeProcessProtocol protected interface
168 // ---------------------------------------------------------------------
169 Error
170 GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override;
171
172 private:
173
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000174 ArchSpec m_arch;
Todd Fialaaf245d12014-06-30 21:05:18 +0000175
Zachary Turner39de3112014-09-09 20:54:56 +0000176 HostThread m_operation_thread;
177 HostThread m_monitor_thread;
Todd Fialaaf245d12014-06-30 21:05:18 +0000178
179 // current operation which must be executed on the priviliged thread
180 void *m_operation;
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000181 Mutex m_operation_mutex;
Todd Fialaaf245d12014-06-30 21:05:18 +0000182
183 // semaphores notified when Operation is ready to be processed and when
184 // the operation is complete.
185 sem_t m_operation_pending;
186 sem_t m_operation_done;
187
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000188 LazyBool m_supports_mem_region;
Todd Fialaaf245d12014-06-30 21:05:18 +0000189 std::vector<MemoryRegionInfo> m_mem_region_cache;
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000190 Mutex m_mem_region_cache_mutex;
Todd Fialaaf245d12014-06-30 21:05:18 +0000191
Chaoren Linfa03ad22015-02-03 01:50:42 +0000192 std::unique_ptr<ThreadStateCoordinator> m_coordinator_up;
193 HostThread m_coordinator_thread;
Todd Fialaaf245d12014-06-30 21:05:18 +0000194
195 struct OperationArgs
196 {
197 OperationArgs(NativeProcessLinux *monitor);
198
199 ~OperationArgs();
200
201 NativeProcessLinux *m_monitor; // The monitor performing the attach.
202 sem_t m_semaphore; // Posted to once operation complete.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000203 Error m_error; // Set if process operation failed.
Todd Fialaaf245d12014-06-30 21:05:18 +0000204 };
205
206 /// @class LauchArgs
207 ///
208 /// @brief Simple structure to pass data to the thread responsible for
209 /// launching a child process.
210 struct LaunchArgs : OperationArgs
211 {
212 LaunchArgs(NativeProcessLinux *monitor,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000213 Module *module,
Todd Fialaaf245d12014-06-30 21:05:18 +0000214 char const **argv,
215 char const **envp,
Todd Fiala75f47c32014-10-11 21:42:09 +0000216 const std::string &stdin_path,
217 const std::string &stdout_path,
218 const std::string &stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000219 const char *working_dir,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000220 const ProcessLaunchInfo &launch_info);
Todd Fialaaf245d12014-06-30 21:05:18 +0000221
222 ~LaunchArgs();
223
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000224 Module *m_module; // The executable image to launch.
225 char const **m_argv; // Process arguments.
226 char const **m_envp; // Process environment.
Todd Fiala75f47c32014-10-11 21:42:09 +0000227 const std::string &m_stdin_path; // Redirect stdin if not empty.
228 const std::string &m_stdout_path; // Redirect stdout if not empty.
229 const std::string &m_stderr_path; // Redirect stderr if not empty.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000230 const char *m_working_dir; // Working directory or NULL.
231 const ProcessLaunchInfo &m_launch_info;
Todd Fialaaf245d12014-06-30 21:05:18 +0000232 };
233
234 struct AttachArgs : OperationArgs
235 {
236 AttachArgs(NativeProcessLinux *monitor,
237 lldb::pid_t pid);
238
239 ~AttachArgs();
240
241 lldb::pid_t m_pid; // pid of the process to be attached.
242 };
243
244 // ---------------------------------------------------------------------
245 // Private Instance Methods
246 // ---------------------------------------------------------------------
247 NativeProcessLinux ();
248
249 /// Launches an inferior process ready for debugging. Forms the
250 /// implementation of Process::DoLaunch.
251 void
252 LaunchInferior (
253 Module *module,
254 char const *argv[],
255 char const *envp[],
Todd Fiala75f47c32014-10-11 21:42:09 +0000256 const std::string &stdin_path,
257 const std::string &stdout_path,
258 const std::string &stderr_path,
Todd Fialaaf245d12014-06-30 21:05:18 +0000259 const char *working_dir,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000260 const ProcessLaunchInfo &launch_info,
Todd Fialaaf245d12014-06-30 21:05:18 +0000261 Error &error);
262
263 /// Attaches to an existing process. Forms the
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +0000264 /// implementation of Process::DoAttach
Todd Fialaaf245d12014-06-30 21:05:18 +0000265 void
266 AttachToInferior (lldb::pid_t pid, Error &error);
267
268 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000269 StartLaunchOpThread(LaunchArgs *args, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000270
271 static void *
272 LaunchOpThread(void *arg);
273
274 static bool
275 Launch(LaunchArgs *args);
276
277 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000278 StartAttachOpThread(AttachArgs *args, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000279
280 static void *
281 AttachOpThread(void *args);
282
283 static bool
284 Attach(AttachArgs *args);
285
Chaoren Lin97ccc292015-02-03 01:51:12 +0000286 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000287 SetDefaultPtraceOpts(const lldb::pid_t);
288
289 static void
290 ServeOperation(OperationArgs *args);
291
292 static bool
293 DupDescriptor(const char *path, int fd, int flags);
294
295 static bool
296 MonitorCallback(void *callback_baton,
297 lldb::pid_t pid, bool exited, int signal, int status);
298
299 void
300 MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid);
301
302 void
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000303 MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp);
304
305 void
306 MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp);
307
308 void
309 MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index);
310
311 void
Todd Fialaaf245d12014-06-30 21:05:18 +0000312 MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited);
313
314#if 0
315 static ::ProcessMessage::CrashReason
316 GetCrashReasonForSIGSEGV(const siginfo_t *info);
317
318 static ::ProcessMessage::CrashReason
319 GetCrashReasonForSIGILL(const siginfo_t *info);
320
321 static ::ProcessMessage::CrashReason
322 GetCrashReasonForSIGFPE(const siginfo_t *info);
323
324 static ::ProcessMessage::CrashReason
325 GetCrashReasonForSIGBUS(const siginfo_t *info);
326#endif
327
328 void
329 DoOperation(void *op);
330
331 /// Stops the child monitor thread.
332 void
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +0000333 StopMonitorThread();
Todd Fialaaf245d12014-06-30 21:05:18 +0000334
335 /// Stops the operation thread used to attach/launch a process.
336 void
337 StopOpThread();
338
Chaoren Linfa03ad22015-02-03 01:50:42 +0000339 Error
340 StartCoordinatorThread ();
341
342 static void*
343 CoordinatorThread (void *arg);
344
345 void
346 StopCoordinatorThread ();
347
Todd Fialaaf245d12014-06-30 21:05:18 +0000348 /// Stops monitoring the child process thread.
349 void
350 StopMonitor();
351
352 bool
353 HasThreadNoLock (lldb::tid_t thread_id);
354
355 NativeThreadProtocolSP
356 MaybeGetThreadNoLock (lldb::tid_t thread_id);
357
358 bool
359 StopTrackingThread (lldb::tid_t thread_id);
360
361 NativeThreadProtocolSP
362 AddThread (lldb::tid_t thread_id);
363
364 NativeThreadProtocolSP
365 GetOrCreateThread (lldb::tid_t thread_id, bool &created);
366
367 Error
368 GetSoftwareBreakpointSize (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size);
369
370 Error
371 FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp);
372
373 /// Writes a siginfo_t structure corresponding to the given thread ID to the
374 /// memory region pointed to by @p siginfo.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000375 Error
376 GetSignalInfo(lldb::tid_t tid, void *siginfo);
Todd Fialaaf245d12014-06-30 21:05:18 +0000377
378 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
379 /// corresponding to the given thread ID to the memory pointed to by @p
380 /// message.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000381 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000382 GetEventMessage(lldb::tid_t tid, unsigned long *message);
383
384 /// Resumes the given thread. If @p signo is anything but
385 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000386 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000387 Resume(lldb::tid_t tid, uint32_t signo);
388
389 /// Single steps the given thread. If @p signo is anything but
390 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000391 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000392 SingleStep(lldb::tid_t tid, uint32_t signo);
393
Chaoren Linfa03ad22015-02-03 01:50:42 +0000394 // ThreadStateCoordinator helper methods.
Todd Fiala511e5cd2014-09-11 23:29:14 +0000395 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000396 NotifyThreadCreateStopped (lldb::tid_t tid);
Todd Fiala511e5cd2014-09-11 23:29:14 +0000397
398 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000399 NotifyThreadCreateRunning (lldb::tid_t tid);
400
401 void
402 NotifyThreadDeath (lldb::tid_t tid);
403
404 void
405 NotifyThreadStop (lldb::tid_t tid);
406
407 void
408 CallAfterRunningThreadsStop (lldb::tid_t tid,
409 const std::function<void (lldb::tid_t tid)> &call_after_function);
Todd Fiala511e5cd2014-09-11 23:29:14 +0000410
Chaoren Lin03f12d62015-02-03 01:50:49 +0000411 void
412 CallAfterRunningThreadsStopWithSkipTID (lldb::tid_t deferred_signal_tid,
413 lldb::tid_t skip_stop_request_tid,
414 const std::function<void (lldb::tid_t tid)> &call_after_function);
415
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000416 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000417 Detach(lldb::tid_t tid);
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000418
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000419 Error
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000420 RequestThreadStop (const lldb::pid_t pid, const lldb::tid_t tid);
Todd Fialaaf245d12014-06-30 21:05:18 +0000421 };
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000422
423} // namespace process_linux
424} // namespace lldb_private
Todd Fialaaf245d12014-06-30 21:05:18 +0000425
426#endif // #ifndef liblldb_NativeProcessLinux_H_