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