blob: ad8880951472461dbbac6c0ddc83550e4399ec7d [file] [log] [blame]
Johnny Chen9ed5b492012-01-05 21:48:15 +00001//===-- ProcessMonitor.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_ProcessMonitor_H_
11#define liblldb_ProcessMonitor_H_
12
13// C Includes
14#include <semaphore.h>
15#include <signal.h>
16
17// C++ Includes
18// Other libraries and framework includes
19#include "lldb/lldb-types.h"
20#include "lldb/Host/Mutex.h"
21
22namespace lldb_private
23{
24class Error;
25class Module;
26class Scalar;
27} // End lldb_private namespace.
28
29class ProcessFreeBSD;
30class Operation;
31
32/// @class ProcessMonitor
33/// @brief Manages communication with the inferior (debugee) process.
34///
35/// Upon construction, this class prepares and launches an inferior process for
36/// debugging.
37///
38/// Changes in the inferior process state are propagated to the associated
39/// ProcessFreeBSD instance by calling ProcessFreeBSD::SendMessage with the
40/// appropriate ProcessMessage events.
41///
42/// A purposely minimal set of operations are provided to interrogate and change
43/// the inferior process state.
44class ProcessMonitor
45{
46public:
47
48 /// Launches an inferior process ready for debugging. Forms the
49 /// implementation of Process::DoLaunch.
Andrew Kaylor6578cb62013-07-09 22:36:48 +000050 ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +000051 lldb_private::Module *module,
52 char const *argv[],
53 char const *envp[],
54 const char *stdin_path,
55 const char *stdout_path,
56 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +000057 const char *working_dir,
Johnny Chen9ed5b492012-01-05 21:48:15 +000058 lldb_private::Error &error);
59
Andrew Kaylor6578cb62013-07-09 22:36:48 +000060 ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +000061 lldb::pid_t pid,
62 lldb_private::Error &error);
63
64 ~ProcessMonitor();
65
66 /// Provides the process number of debugee.
67 lldb::pid_t
68 GetPID() const { return m_pid; }
69
70 /// Returns the process associated with this ProcessMonitor.
71 ProcessFreeBSD &
72 GetProcess() { return *m_process; }
73
74 /// Returns a file descriptor to the controlling terminal of the inferior
75 /// process.
76 ///
77 /// Reads from this file descriptor yield both the standard output and
78 /// standard error of this debugee. Even if stderr and stdout were
79 /// redirected on launch it may still happen that data is available on this
80 /// descriptor (if the inferior process opens /dev/tty, for example).
81 ///
82 /// If this monitor was attached to an existing process this method returns
83 /// -1.
84 int
85 GetTerminalFD() const { return m_terminal_fd; }
86
87 /// Reads @p size bytes from address @vm_adder in the inferior process
88 /// address space.
89 ///
90 /// This method is provided to implement Process::DoReadMemory.
91 size_t
92 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
93 lldb_private::Error &error);
94
95 /// Writes @p size bytes from address @p vm_adder in the inferior process
96 /// address space.
97 ///
98 /// This method is provided to implement Process::DoWriteMemory.
99 size_t
100 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
101 lldb_private::Error &error);
102
103 /// Reads the contents from the register identified by the given (architecture
104 /// dependent) offset.
105 ///
106 /// This method is provided for use by RegisterContextFreeBSD derivatives.
107 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000108 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000109 unsigned size, lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000110
111 /// Writes the given value to the register identified by the given
112 /// (architecture dependent) offset.
113 ///
114 /// This method is provided for use by RegisterContextFreeBSD derivatives.
115 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000116 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000117 const lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000118
Ed Mastea4be2c52014-02-19 18:34:06 +0000119 /// Reads the contents from the debug register identified by the given
120 /// (architecture dependent) offset.
121 ///
122 /// This method is provided for use by RegisterContextFreeBSD derivatives.
123 bool
124 ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
125 const char *reg_name, unsigned size,
126 lldb_private::RegisterValue &value);
127
128 /// Writes the given value to the debug register identified by the given
129 /// (architecture dependent) offset.
130 ///
131 /// This method is provided for use by RegisterContextFreeBSD derivatives.
132 bool
133 WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
134 const char *reg_name,
135 const lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000136 /// Reads all general purpose registers into the specified buffer.
137 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000138 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000139
Matt Kopecc6672c82013-03-15 20:00:39 +0000140 /// Reads all floating point registers into the specified buffer.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000141 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000142 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000143
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000144 /// Reads the specified register set into the specified buffer.
145 ///
146 /// This method is provided for use by RegisterContextFreeBSD derivatives.
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000147 bool
148 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
149
Johnny Chen9ed5b492012-01-05 21:48:15 +0000150 /// Writes all general purpose registers into the specified buffer.
151 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000152 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000153
Matt Kopecc6672c82013-03-15 20:00:39 +0000154 /// Writes all floating point registers into the specified buffer.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000155 bool
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000156 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
157
158 /// Writes the specified register set into the specified buffer.
159 ///
160 /// This method is provided for use by RegisterContextFreeBSD derivatives.
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000161 bool
162 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000163
Ed Maste68f51792013-10-18 19:16:44 +0000164 /// Reads the value of the thread-specific pointer for a given thread ID.
165 bool
166 ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value);
167
Ed Maste7fd845c2013-12-09 15:51:17 +0000168 /// Returns current thread IDs in process
169 size_t
170 GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids);
171
Ed Maste819e3992013-07-17 14:02:20 +0000172 /// Writes a ptrace_lwpinfo structure corresponding to the given thread ID
173 /// to the memory region pointed to by @p lwpinfo.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000174 bool
Ed Maste819e3992013-07-17 14:02:20 +0000175 GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &error_no);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000176
Ed Maste7fd845c2013-12-09 15:51:17 +0000177 /// Suspends or unsuspends a thread prior to process resume or step.
178 bool
179 ThreadSuspend(lldb::tid_t tid, bool suspend);
180
Johnny Chen9ed5b492012-01-05 21:48:15 +0000181 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
182 /// corresponding to the given thread IDto the memory pointed to by @p
183 /// message.
184 bool
185 GetEventMessage(lldb::tid_t tid, unsigned long *message);
186
Ed Maste502f9022013-11-25 16:31:23 +0000187 /// Resumes the process. If @p signo is anything but
188 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000189 bool
Ed Maste502f9022013-11-25 16:31:23 +0000190 Resume(lldb::tid_t unused, uint32_t signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000191
Ed Maste502f9022013-11-25 16:31:23 +0000192 /// Single steps the process. If @p signo is anything but
193 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000194 bool
Ed Maste502f9022013-11-25 16:31:23 +0000195 SingleStep(lldb::tid_t unused, uint32_t signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000196
Ed Maste70882932014-04-01 14:30:56 +0000197 /// Terminate the traced process.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000198 bool
Ed Maste70882932014-04-01 14:30:56 +0000199 Kill();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000200
201 lldb_private::Error
Matt Kopecedee1822013-06-03 19:48:53 +0000202 Detach(lldb::tid_t tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000203
Andrew Kaylorbc68b432013-07-10 21:57:27 +0000204 void
205 StopMonitor();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000206
Andrew Kaylord4d54992013-09-17 00:30:24 +0000207 // Waits for the initial stop message from a new thread.
208 bool
209 WaitForInitialTIDStop(lldb::tid_t tid);
210
Johnny Chen9ed5b492012-01-05 21:48:15 +0000211private:
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000212 ProcessFreeBSD *m_process;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000213
214 lldb::thread_t m_operation_thread;
215 lldb::thread_t m_monitor_thread;
216 lldb::pid_t m_pid;
217
Johnny Chen9ed5b492012-01-05 21:48:15 +0000218 int m_terminal_fd;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000219
Ed Maste756e1ff2013-09-18 19:34:08 +0000220 // current operation which must be executed on the privileged thread
221 Operation *m_operation;
222 lldb_private::Mutex m_operation_mutex;
223
224 // semaphores notified when Operation is ready to be processed and when
225 // the operation is complete.
226 sem_t m_operation_pending;
227 sem_t m_operation_done;
228
Johnny Chen9ed5b492012-01-05 21:48:15 +0000229 struct OperationArgs
230 {
231 OperationArgs(ProcessMonitor *monitor);
232
233 ~OperationArgs();
234
235 ProcessMonitor *m_monitor; // The monitor performing the attach.
236 sem_t m_semaphore; // Posted to once operation complete.
237 lldb_private::Error m_error; // Set if process operation failed.
238 };
239
240 /// @class LauchArgs
241 ///
242 /// @brief Simple structure to pass data to the thread responsible for
243 /// launching a child process.
244 struct LaunchArgs : OperationArgs
245 {
246 LaunchArgs(ProcessMonitor *monitor,
247 lldb_private::Module *module,
248 char const **argv,
249 char const **envp,
250 const char *stdin_path,
251 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000252 const char *stderr_path,
253 const char *working_dir);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000254
255 ~LaunchArgs();
256
257 lldb_private::Module *m_module; // The executable image to launch.
258 char const **m_argv; // Process arguments.
259 char const **m_envp; // Process environment.
260 const char *m_stdin_path; // Redirect stdin or NULL.
261 const char *m_stdout_path; // Redirect stdout or NULL.
262 const char *m_stderr_path; // Redirect stderr or NULL.
Daniel Malea6217d2a2013-01-08 14:49:22 +0000263 const char *m_working_dir; // Working directory or NULL.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000264 };
265
266 void
267 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
268
Johnny Chen9ed5b492012-01-05 21:48:15 +0000269 static void *
270 LaunchOpThread(void *arg);
271
272 static bool
273 Launch(LaunchArgs *args);
274
Johnny Chen9ed5b492012-01-05 21:48:15 +0000275 struct AttachArgs : OperationArgs
276 {
277 AttachArgs(ProcessMonitor *monitor,
278 lldb::pid_t pid);
279
280 ~AttachArgs();
281
282 lldb::pid_t m_pid; // pid of the process to be attached.
283 };
284
285 void
286 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
287
Johnny Chen9ed5b492012-01-05 21:48:15 +0000288 static void *
289 AttachOpThread(void *args);
290
291 static bool
292 Attach(AttachArgs *args);
293
294 static void
295 ServeOperation(OperationArgs *args);
296
297 static bool
298 DupDescriptor(const char *path, int fd, int flags);
299
300 static bool
301 MonitorCallback(void *callback_baton,
302 lldb::pid_t pid, bool exited, int signal, int status);
303
304 static ProcessMessage
305 MonitorSIGTRAP(ProcessMonitor *monitor,
306 const siginfo_t *info, lldb::pid_t pid);
307
308 static ProcessMessage
309 MonitorSignal(ProcessMonitor *monitor,
310 const siginfo_t *info, lldb::pid_t pid);
311
312 static ProcessMessage::CrashReason
313 GetCrashReasonForSIGSEGV(const siginfo_t *info);
314
315 static ProcessMessage::CrashReason
316 GetCrashReasonForSIGILL(const siginfo_t *info);
317
318 static ProcessMessage::CrashReason
319 GetCrashReasonForSIGFPE(const siginfo_t *info);
320
321 static ProcessMessage::CrashReason
322 GetCrashReasonForSIGBUS(const siginfo_t *info);
323
324 void
325 DoOperation(Operation *op);
326
327 /// Stops the child monitor thread.
328 void
329 StopMonitoringChildProcess();
330
Ed Mastea02f5532013-07-02 16:45:16 +0000331 /// Stops the operation thread used to attach/launch a process.
332 void
333 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000334};
335
336#endif // #ifndef liblldb_ProcessMonitor_H_