blob: a2ffd6cb68b6a81e4db1aafad9d04195d0c581c8 [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
30namespace lldb_private
31{
32 class Error;
33 class Module;
Chaoren Linfa03ad22015-02-03 01:50:42 +000034 class ThreadStateCoordinator;
Todd Fialaaf245d12014-06-30 21:05:18 +000035 class Scalar;
36
37 /// @class NativeProcessLinux
38 /// @brief Manages communication with the inferior (debugee) process.
39 ///
40 /// Upon construction, this class prepares and launches an inferior process for
41 /// debugging.
42 ///
43 /// Changes in the inferior process state are broadcasted.
44 class NativeProcessLinux: public NativeProcessProtocol
45 {
46 public:
47
Todd Fialaaf245d12014-06-30 21:05:18 +000048 static lldb_private::Error
49 LaunchProcess (
50 Module *exe_module,
51 ProcessLaunchInfo &launch_info,
52 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
53 NativeProcessProtocolSP &native_process_sp);
54
55 static lldb_private::Error
56 AttachToProcess (
57 lldb::pid_t pid,
58 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
59 NativeProcessProtocolSP &native_process_sp);
60
61 // ---------------------------------------------------------------------
Todd Fialaaf245d12014-06-30 21:05:18 +000062 // NativeProcessProtocol Interface
63 // ---------------------------------------------------------------------
64 Error
65 Resume (const ResumeActionList &resume_actions) override;
66
67 Error
68 Halt () override;
69
70 Error
71 Detach () override;
72
73 Error
74 Signal (int signo) override;
75
76 Error
Chaoren Line9547b82015-02-03 01:51:00 +000077 Interrupt () override;
78
79 Error
Todd Fialaaf245d12014-06-30 21:05:18 +000080 Kill () override;
81
82 Error
83 GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override;
84
85 Error
86 ReadMemory (lldb::addr_t addr, void *buf, lldb::addr_t size, lldb::addr_t &bytes_read) override;
87
88 Error
89 WriteMemory (lldb::addr_t addr, const void *buf, lldb::addr_t size, lldb::addr_t &bytes_written) override;
90
91 Error
92 AllocateMemory (lldb::addr_t size, uint32_t permissions, lldb::addr_t &addr) override;
93
94 Error
95 DeallocateMemory (lldb::addr_t addr) override;
96
97 lldb::addr_t
98 GetSharedLibraryInfoAddress () override;
99
100 size_t
101 UpdateThreads () override;
102
103 bool
104 GetArchitecture (ArchSpec &arch) const override;
105
106 Error
107 SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override;
108
109 void
110 DoStopIDBumped (uint32_t newBumpId) override;
111
Oleksiy Vyalov8bc34f42015-02-19 17:58:04 +0000112 void
113 Terminate () override;
114
Todd Fialaaf245d12014-06-30 21:05:18 +0000115 // ---------------------------------------------------------------------
116 // Interface used by NativeRegisterContext-derived classes.
117 // ---------------------------------------------------------------------
118
119 /// Reads the contents from the register identified by the given (architecture
120 /// dependent) offset.
121 ///
122 /// This method is provided for use by RegisterContextLinux derivatives.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000123 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000124 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
125 unsigned size, lldb_private::RegisterValue &value);
126
127 /// Writes the given value to the register identified by the given
128 /// (architecture dependent) offset.
129 ///
130 /// This method is provided for use by RegisterContextLinux derivatives.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000131 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000132 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
133 const lldb_private::RegisterValue &value);
134
135 /// Reads all general purpose registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000136 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000137 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
138
139 /// Reads generic floating point registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000140 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000141 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
142
143 /// Reads the specified register set into the specified buffer.
144 /// For instance, the extended floating-point register set.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000145 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000146 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
147
148 /// Writes all general purpose registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000149 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000150 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
151
152 /// Writes generic floating point registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000153 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000154 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
155
156 /// Writes the specified register set into the specified buffer.
157 /// For instance, the extended floating-point register set.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000158 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000159 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
160
161 protected:
162 // ---------------------------------------------------------------------
163 // NativeProcessProtocol protected interface
164 // ---------------------------------------------------------------------
165 Error
166 GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override;
167
168 private:
169
170 lldb_private::ArchSpec m_arch;
171
Zachary Turner39de3112014-09-09 20:54:56 +0000172 HostThread m_operation_thread;
173 HostThread m_monitor_thread;
Todd Fialaaf245d12014-06-30 21:05:18 +0000174
175 // current operation which must be executed on the priviliged thread
176 void *m_operation;
177 lldb_private::Mutex m_operation_mutex;
178
179 // semaphores notified when Operation is ready to be processed and when
180 // the operation is complete.
181 sem_t m_operation_pending;
182 sem_t m_operation_done;
183
Todd Fialaaf245d12014-06-30 21:05:18 +0000184 lldb_private::LazyBool m_supports_mem_region;
185 std::vector<MemoryRegionInfo> m_mem_region_cache;
186 lldb_private::Mutex m_mem_region_cache_mutex;
187
Chaoren Linfa03ad22015-02-03 01:50:42 +0000188 std::unique_ptr<ThreadStateCoordinator> m_coordinator_up;
189 HostThread m_coordinator_thread;
Todd Fialaaf245d12014-06-30 21:05:18 +0000190
191 struct OperationArgs
192 {
193 OperationArgs(NativeProcessLinux *monitor);
194
195 ~OperationArgs();
196
197 NativeProcessLinux *m_monitor; // The monitor performing the attach.
198 sem_t m_semaphore; // Posted to once operation complete.
199 lldb_private::Error m_error; // Set if process operation failed.
200 };
201
202 /// @class LauchArgs
203 ///
204 /// @brief Simple structure to pass data to the thread responsible for
205 /// launching a child process.
206 struct LaunchArgs : OperationArgs
207 {
208 LaunchArgs(NativeProcessLinux *monitor,
209 lldb_private::Module *module,
210 char const **argv,
211 char const **envp,
Todd Fiala75f47c32014-10-11 21:42:09 +0000212 const std::string &stdin_path,
213 const std::string &stdout_path,
214 const std::string &stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000215 const char *working_dir,
216 const lldb_private::ProcessLaunchInfo &launch_info);
Todd Fialaaf245d12014-06-30 21:05:18 +0000217
218 ~LaunchArgs();
219
220 lldb_private::Module *m_module; // The executable image to launch.
221 char const **m_argv; // Process arguments.
222 char const **m_envp; // Process environment.
Todd Fiala75f47c32014-10-11 21:42:09 +0000223 const std::string &m_stdin_path; // Redirect stdin if not empty.
224 const std::string &m_stdout_path; // Redirect stdout if not empty.
225 const std::string &m_stderr_path; // Redirect stderr if not empty.
Todd Fialaaf245d12014-06-30 21:05:18 +0000226 const char *m_working_dir; // Working directory or NULL.
Todd Fiala0bce1b62014-08-17 00:10:50 +0000227 const lldb_private::ProcessLaunchInfo &m_launch_info;
Todd Fialaaf245d12014-06-30 21:05:18 +0000228 };
229
230 struct AttachArgs : OperationArgs
231 {
232 AttachArgs(NativeProcessLinux *monitor,
233 lldb::pid_t pid);
234
235 ~AttachArgs();
236
237 lldb::pid_t m_pid; // pid of the process to be attached.
238 };
239
240 // ---------------------------------------------------------------------
241 // Private Instance Methods
242 // ---------------------------------------------------------------------
243 NativeProcessLinux ();
244
245 /// Launches an inferior process ready for debugging. Forms the
246 /// implementation of Process::DoLaunch.
247 void
248 LaunchInferior (
249 Module *module,
250 char const *argv[],
251 char const *envp[],
Todd Fiala75f47c32014-10-11 21:42:09 +0000252 const std::string &stdin_path,
253 const std::string &stdout_path,
254 const std::string &stderr_path,
Todd Fialaaf245d12014-06-30 21:05:18 +0000255 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000256 const lldb_private::ProcessLaunchInfo &launch_info,
Todd Fialaaf245d12014-06-30 21:05:18 +0000257 Error &error);
258
259 /// Attaches to an existing process. Forms the
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +0000260 /// implementation of Process::DoAttach
Todd Fialaaf245d12014-06-30 21:05:18 +0000261 void
262 AttachToInferior (lldb::pid_t pid, Error &error);
263
264 void
265 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
266
267 static void *
268 LaunchOpThread(void *arg);
269
270 static bool
271 Launch(LaunchArgs *args);
272
273 void
274 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
275
276 static void *
277 AttachOpThread(void *args);
278
279 static bool
280 Attach(AttachArgs *args);
281
Chaoren Lin97ccc292015-02-03 01:51:12 +0000282 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000283 SetDefaultPtraceOpts(const lldb::pid_t);
284
285 static void
286 ServeOperation(OperationArgs *args);
287
288 static bool
289 DupDescriptor(const char *path, int fd, int flags);
290
291 static bool
292 MonitorCallback(void *callback_baton,
293 lldb::pid_t pid, bool exited, int signal, int status);
294
295 void
296 MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid);
297
298 void
299 MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited);
300
301#if 0
302 static ::ProcessMessage::CrashReason
303 GetCrashReasonForSIGSEGV(const siginfo_t *info);
304
305 static ::ProcessMessage::CrashReason
306 GetCrashReasonForSIGILL(const siginfo_t *info);
307
308 static ::ProcessMessage::CrashReason
309 GetCrashReasonForSIGFPE(const siginfo_t *info);
310
311 static ::ProcessMessage::CrashReason
312 GetCrashReasonForSIGBUS(const siginfo_t *info);
313#endif
314
315 void
316 DoOperation(void *op);
317
318 /// Stops the child monitor thread.
319 void
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +0000320 StopMonitorThread();
Todd Fialaaf245d12014-06-30 21:05:18 +0000321
322 /// Stops the operation thread used to attach/launch a process.
323 void
324 StopOpThread();
325
Chaoren Linfa03ad22015-02-03 01:50:42 +0000326 Error
327 StartCoordinatorThread ();
328
329 static void*
330 CoordinatorThread (void *arg);
331
332 void
333 StopCoordinatorThread ();
334
Todd Fialaaf245d12014-06-30 21:05:18 +0000335 /// Stops monitoring the child process thread.
336 void
337 StopMonitor();
338
339 bool
340 HasThreadNoLock (lldb::tid_t thread_id);
341
342 NativeThreadProtocolSP
343 MaybeGetThreadNoLock (lldb::tid_t thread_id);
344
345 bool
346 StopTrackingThread (lldb::tid_t thread_id);
347
348 NativeThreadProtocolSP
349 AddThread (lldb::tid_t thread_id);
350
351 NativeThreadProtocolSP
352 GetOrCreateThread (lldb::tid_t thread_id, bool &created);
353
354 Error
355 GetSoftwareBreakpointSize (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size);
356
357 Error
358 FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp);
359
360 /// Writes a siginfo_t structure corresponding to the given thread ID to the
361 /// memory region pointed to by @p siginfo.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000362 Error
363 GetSignalInfo(lldb::tid_t tid, void *siginfo);
Todd Fialaaf245d12014-06-30 21:05:18 +0000364
365 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
366 /// corresponding to the given thread ID to the memory pointed to by @p
367 /// message.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000368 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000369 GetEventMessage(lldb::tid_t tid, unsigned long *message);
370
371 /// Resumes the given thread. If @p signo is anything but
372 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000373 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000374 Resume(lldb::tid_t tid, uint32_t signo);
375
376 /// Single steps the given thread. If @p signo is anything but
377 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000378 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000379 SingleStep(lldb::tid_t tid, uint32_t signo);
380
Chaoren Linfa03ad22015-02-03 01:50:42 +0000381 // ThreadStateCoordinator helper methods.
Todd Fiala511e5cd2014-09-11 23:29:14 +0000382 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000383 NotifyThreadCreateStopped (lldb::tid_t tid);
Todd Fiala511e5cd2014-09-11 23:29:14 +0000384
385 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000386 NotifyThreadCreateRunning (lldb::tid_t tid);
387
388 void
389 NotifyThreadDeath (lldb::tid_t tid);
390
391 void
392 NotifyThreadStop (lldb::tid_t tid);
393
394 void
395 CallAfterRunningThreadsStop (lldb::tid_t tid,
396 const std::function<void (lldb::tid_t tid)> &call_after_function);
Todd Fiala511e5cd2014-09-11 23:29:14 +0000397
Chaoren Lin03f12d62015-02-03 01:50:49 +0000398 void
399 CallAfterRunningThreadsStopWithSkipTID (lldb::tid_t deferred_signal_tid,
400 lldb::tid_t skip_stop_request_tid,
401 const std::function<void (lldb::tid_t tid)> &call_after_function);
402
Todd Fialaaf245d12014-06-30 21:05:18 +0000403 lldb_private::Error
404 Detach(lldb::tid_t tid);
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000405
406 lldb_private::Error
407 RequestThreadStop (const lldb::pid_t pid, const lldb::tid_t tid);
Todd Fialaaf245d12014-06-30 21:05:18 +0000408 };
409} // End lldb_private namespace.
410
411#endif // #ifndef liblldb_NativeProcessLinux_H_