blob: c94a78bd2ea1080500f5e85f98b87d75fd0cd75d [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
28#include "Host/common/NativeProcessProtocol.h"
29
30namespace lldb_private
31{
32 class Error;
33 class Module;
34 class Scalar;
35
36 /// @class NativeProcessLinux
37 /// @brief Manages communication with the inferior (debugee) process.
38 ///
39 /// Upon construction, this class prepares and launches an inferior process for
40 /// debugging.
41 ///
42 /// Changes in the inferior process state are broadcasted.
43 class NativeProcessLinux: public NativeProcessProtocol
44 {
45 public:
46
47 // ---------------------------------------------------------------------
48 // Public Static Methods
49 // ---------------------------------------------------------------------
50 static lldb_private::Error
51 LaunchProcess (
52 Module *exe_module,
53 ProcessLaunchInfo &launch_info,
54 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
55 NativeProcessProtocolSP &native_process_sp);
56
57 static lldb_private::Error
58 AttachToProcess (
59 lldb::pid_t pid,
60 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
61 NativeProcessProtocolSP &native_process_sp);
62
63 // ---------------------------------------------------------------------
64 // Public Instance Methods
65 // ---------------------------------------------------------------------
66
67 ~NativeProcessLinux() override;
68
69 // ---------------------------------------------------------------------
70 // 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
85 Kill () override;
86
87 Error
88 GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override;
89
90 Error
91 ReadMemory (lldb::addr_t addr, void *buf, lldb::addr_t size, lldb::addr_t &bytes_read) override;
92
93 Error
94 WriteMemory (lldb::addr_t addr, const void *buf, lldb::addr_t size, lldb::addr_t &bytes_written) override;
95
96 Error
97 AllocateMemory (lldb::addr_t size, uint32_t permissions, lldb::addr_t &addr) override;
98
99 Error
100 DeallocateMemory (lldb::addr_t addr) override;
101
102 lldb::addr_t
103 GetSharedLibraryInfoAddress () override;
104
105 size_t
106 UpdateThreads () override;
107
108 bool
109 GetArchitecture (ArchSpec &arch) const override;
110
111 Error
112 SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override;
113
114 void
115 DoStopIDBumped (uint32_t newBumpId) override;
116
117 // ---------------------------------------------------------------------
118 // Interface used by NativeRegisterContext-derived classes.
119 // ---------------------------------------------------------------------
120
121 /// Reads the contents from the register identified by the given (architecture
122 /// dependent) offset.
123 ///
124 /// This method is provided for use by RegisterContextLinux derivatives.
125 bool
126 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
127 unsigned size, lldb_private::RegisterValue &value);
128
129 /// Writes the given value to the register identified by the given
130 /// (architecture dependent) offset.
131 ///
132 /// This method is provided for use by RegisterContextLinux derivatives.
133 bool
134 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
135 const lldb_private::RegisterValue &value);
136
137 /// Reads all general purpose registers into the specified buffer.
138 bool
139 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
140
141 /// Reads generic floating point registers into the specified buffer.
142 bool
143 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
144
145 /// Reads the specified register set into the specified buffer.
146 /// For instance, the extended floating-point register set.
147 bool
148 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
149
150 /// Writes all general purpose registers into the specified buffer.
151 bool
152 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
153
154 /// Writes generic floating point registers into the specified buffer.
155 bool
156 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
157
158 /// Writes the specified register set into the specified buffer.
159 /// For instance, the extended floating-point register set.
160 bool
161 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
162
163 protected:
164 // ---------------------------------------------------------------------
165 // NativeProcessProtocol protected interface
166 // ---------------------------------------------------------------------
167 Error
168 GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override;
169
170 private:
171
172 lldb_private::ArchSpec m_arch;
173
Zachary Turner39de3112014-09-09 20:54:56 +0000174 HostThread m_operation_thread;
175 HostThread m_monitor_thread;
Todd Fialaaf245d12014-06-30 21:05:18 +0000176
177 // current operation which must be executed on the priviliged thread
178 void *m_operation;
179 lldb_private::Mutex m_operation_mutex;
180
181 // semaphores notified when Operation is ready to be processed and when
182 // the operation is complete.
183 sem_t m_operation_pending;
184 sem_t m_operation_done;
185
186 // Set of tids we're waiting to stop before we notify the delegate of
187 // the stopped state. We only notify the delegate after all threads
188 // ordered to stop have signaled their stop.
189 std::unordered_set<lldb::tid_t> m_wait_for_stop_tids;
190 lldb_private::Mutex m_wait_for_stop_tids_mutex;
191
192 lldb_private::LazyBool m_supports_mem_region;
193 std::vector<MemoryRegionInfo> m_mem_region_cache;
194 lldb_private::Mutex m_mem_region_cache_mutex;
195
196
197 struct OperationArgs
198 {
199 OperationArgs(NativeProcessLinux *monitor);
200
201 ~OperationArgs();
202
203 NativeProcessLinux *m_monitor; // The monitor performing the attach.
204 sem_t m_semaphore; // Posted to once operation complete.
205 lldb_private::Error m_error; // Set if process operation failed.
206 };
207
208 /// @class LauchArgs
209 ///
210 /// @brief Simple structure to pass data to the thread responsible for
211 /// launching a child process.
212 struct LaunchArgs : OperationArgs
213 {
214 LaunchArgs(NativeProcessLinux *monitor,
215 lldb_private::Module *module,
216 char const **argv,
217 char const **envp,
218 const char *stdin_path,
219 const char *stdout_path,
220 const char *stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000221 const char *working_dir,
222 const lldb_private::ProcessLaunchInfo &launch_info);
Todd Fialaaf245d12014-06-30 21:05:18 +0000223
224 ~LaunchArgs();
225
226 lldb_private::Module *m_module; // The executable image to launch.
227 char const **m_argv; // Process arguments.
228 char const **m_envp; // Process environment.
229 const char *m_stdin_path; // Redirect stdin or NULL.
230 const char *m_stdout_path; // Redirect stdout or NULL.
231 const char *m_stderr_path; // Redirect stderr or NULL.
232 const char *m_working_dir; // Working directory or NULL.
Todd Fiala0bce1b62014-08-17 00:10:50 +0000233 const lldb_private::ProcessLaunchInfo &m_launch_info;
Todd Fialaaf245d12014-06-30 21:05:18 +0000234 };
235
236 struct AttachArgs : OperationArgs
237 {
238 AttachArgs(NativeProcessLinux *monitor,
239 lldb::pid_t pid);
240
241 ~AttachArgs();
242
243 lldb::pid_t m_pid; // pid of the process to be attached.
244 };
245
246 // ---------------------------------------------------------------------
247 // Private Instance Methods
248 // ---------------------------------------------------------------------
249 NativeProcessLinux ();
250
251 /// Launches an inferior process ready for debugging. Forms the
252 /// implementation of Process::DoLaunch.
253 void
254 LaunchInferior (
255 Module *module,
256 char const *argv[],
257 char const *envp[],
258 const char *stdin_path,
259 const char *stdout_path,
260 const char *stderr_path,
261 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000262 const lldb_private::ProcessLaunchInfo &launch_info,
Todd Fialaaf245d12014-06-30 21:05:18 +0000263 Error &error);
264
265 /// Attaches to an existing process. Forms the
266 /// implementation of Process::DoLaunch.
267 void
268 AttachToInferior (lldb::pid_t pid, Error &error);
269
270 void
271 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
272
273 static void *
274 LaunchOpThread(void *arg);
275
276 static bool
277 Launch(LaunchArgs *args);
278
279 void
280 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
281
282 static void *
283 AttachOpThread(void *args);
284
285 static bool
286 Attach(AttachArgs *args);
287
288 static bool
289 SetDefaultPtraceOpts(const lldb::pid_t);
290
291 static void
292 ServeOperation(OperationArgs *args);
293
294 static bool
295 DupDescriptor(const char *path, int fd, int flags);
296
297 static bool
298 MonitorCallback(void *callback_baton,
299 lldb::pid_t pid, bool exited, int signal, int status);
300
301 void
302 MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid);
303
304 void
305 MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited);
306
307#if 0
308 static ::ProcessMessage::CrashReason
309 GetCrashReasonForSIGSEGV(const siginfo_t *info);
310
311 static ::ProcessMessage::CrashReason
312 GetCrashReasonForSIGILL(const siginfo_t *info);
313
314 static ::ProcessMessage::CrashReason
315 GetCrashReasonForSIGFPE(const siginfo_t *info);
316
317 static ::ProcessMessage::CrashReason
318 GetCrashReasonForSIGBUS(const siginfo_t *info);
319#endif
320
321 void
322 DoOperation(void *op);
323
324 /// Stops the child monitor thread.
325 void
326 StopMonitoringChildProcess();
327
328 /// Stops the operation thread used to attach/launch a process.
329 void
330 StopOpThread();
331
332 /// Stops monitoring the child process thread.
333 void
334 StopMonitor();
335
336 bool
337 HasThreadNoLock (lldb::tid_t thread_id);
338
339 NativeThreadProtocolSP
340 MaybeGetThreadNoLock (lldb::tid_t thread_id);
341
342 bool
343 StopTrackingThread (lldb::tid_t thread_id);
344
345 NativeThreadProtocolSP
346 AddThread (lldb::tid_t thread_id);
347
348 NativeThreadProtocolSP
349 GetOrCreateThread (lldb::tid_t thread_id, bool &created);
350
351 Error
352 GetSoftwareBreakpointSize (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size);
353
354 Error
355 FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp);
356
357 /// Writes a siginfo_t structure corresponding to the given thread ID to the
358 /// memory region pointed to by @p siginfo.
359 bool
360 GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err);
361
362 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
363 /// corresponding to the given thread ID to the memory pointed to by @p
364 /// message.
365 bool
366 GetEventMessage(lldb::tid_t tid, unsigned long *message);
367
368 /// Resumes the given thread. If @p signo is anything but
369 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
370 bool
371 Resume(lldb::tid_t tid, uint32_t signo);
372
373 /// Single steps the given thread. If @p signo is anything but
374 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
375 bool
376 SingleStep(lldb::tid_t tid, uint32_t signo);
377
378 lldb_private::Error
379 Detach(lldb::tid_t tid);
380 };
381} // End lldb_private namespace.
382
383#endif // #ifndef liblldb_NativeProcessLinux_H_