blob: c446dcd2821a81177039c4a5bd864402659897eb [file] [log] [blame]
Stephen Wilsone6f9f662010-07-24 02:19:04 +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>
Stephen Wilson84ffe702011-03-30 15:55:52 +000015#include <signal.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000016
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;
Johnny Chen30213ff2012-01-05 19:17:38 +000027
Stephen Wilsone6f9f662010-07-24 02:19:04 +000028} // End lldb_private namespace.
29
30class ProcessLinux;
31class Operation;
Johnny Chen30213ff2012-01-05 19:17:38 +000032class ProcessPOSIX;
Stephen Wilsone6f9f662010-07-24 02:19:04 +000033
34/// @class ProcessMonitor
35/// @brief Manages communication with the inferior (debugee) process.
36///
37/// Upon construction, this class prepares and launches an inferior process for
38/// debugging.
39///
40/// Changes in the inferior process state are propagated to the associated
41/// ProcessLinux instance by calling ProcessLinux::SendMessage with the
42/// appropriate ProcessMessage events.
43///
44/// A purposely minimal set of operations are provided to interrogate and change
45/// the inferior process state.
46class ProcessMonitor
47{
48public:
49
50 /// Launches an inferior process ready for debugging. Forms the
51 /// implementation of Process::DoLaunch.
Johnny Chen30213ff2012-01-05 19:17:38 +000052 ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +000053 lldb_private::Module *module,
54 char const *argv[],
55 char const *envp[],
56 const char *stdin_path,
57 const char *stdout_path,
58 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +000059 const char *working_dir,
Stephen Wilsone6f9f662010-07-24 02:19:04 +000060 lldb_private::Error &error);
61
Johnny Chen30213ff2012-01-05 19:17:38 +000062 ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +000063 lldb::pid_t pid,
64 lldb_private::Error &error);
65
Stephen Wilsone6f9f662010-07-24 02:19:04 +000066 ~ProcessMonitor();
67
68 /// Provides the process number of debugee.
69 lldb::pid_t
70 GetPID() const { return m_pid; }
71
72 /// Returns the process associated with this ProcessMonitor.
73 ProcessLinux &
74 GetProcess() { return *m_process; }
75
Greg Clayton710dd5a2011-01-08 20:28:42 +000076 /// Returns a file descriptor to the controlling terminal of the inferior
Stephen Wilsone6f9f662010-07-24 02:19:04 +000077 /// process.
78 ///
Greg Clayton710dd5a2011-01-08 20:28:42 +000079 /// Reads from this file descriptor yield both the standard output and
Stephen Wilsone6f9f662010-07-24 02:19:04 +000080 /// standard error of this debugee. Even if stderr and stdout were
81 /// redirected on launch it may still happen that data is available on this
82 /// descriptor (if the inferior process opens /dev/tty, for example).
83 ///
84 /// If this monitor was attached to an existing process this method returns
85 /// -1.
86 int
87 GetTerminalFD() const { return m_terminal_fd; }
88
89 /// Reads @p size bytes from address @vm_adder in the inferior process
90 /// address space.
91 ///
92 /// This method is provided to implement Process::DoReadMemory.
93 size_t
94 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
95 lldb_private::Error &error);
96
97 /// Writes @p size bytes from address @p vm_adder in the inferior process
98 /// address space.
99 ///
100 /// This method is provided to implement Process::DoWriteMemory.
101 size_t
102 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
103 lldb_private::Error &error);
104
105 /// Reads the contents from the register identified by the given (architecture
106 /// dependent) offset.
107 ///
108 /// This method is provided for use by RegisterContextLinux derivatives.
109 bool
Daniel Maleaf0da3712012-12-18 19:50:15 +0000110 ReadRegisterValue(lldb::tid_t tid, unsigned offset,
111 unsigned size, lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000112
113 /// Writes the given value to the register identified by the given
114 /// (architecture dependent) offset.
115 ///
116 /// This method is provided for use by RegisterContextLinux derivatives.
117 bool
Daniel Maleaf0da3712012-12-18 19:50:15 +0000118 WriteRegisterValue(lldb::tid_t tid, unsigned offset,
119 const lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000120
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000121 /// Reads all general purpose registers into the specified buffer.
122 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000123 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000124
Matt Kopec58c0b962013-03-20 20:34:35 +0000125 /// Reads generic floating point registers into the specified buffer.
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000126 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000127 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000128
Matt Kopec58c0b962013-03-20 20:34:35 +0000129 /// Reads the specified register set into the specified buffer.
130 /// For instance, the extended floating-point register set.
131 bool
132 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
133
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000134 /// Writes all general purpose registers into the specified buffer.
135 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000136 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000137
Matt Kopec58c0b962013-03-20 20:34:35 +0000138 /// Writes generic floating point registers into the specified buffer.
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000139 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000140 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000141
Matt Kopec58c0b962013-03-20 20:34:35 +0000142 /// Writes the specified register set into the specified buffer.
143 /// For instance, the extended floating-point register set.
144 bool
145 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
146
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000147 /// Writes a siginfo_t structure corresponding to the given thread ID to the
148 /// memory region pointed to by @p siginfo.
149 bool
Daniel Maleaa35970a2012-11-23 18:09:58 +0000150 GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000151
152 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
153 /// corresponding to the given thread IDto the memory pointed to by @p
154 /// message.
155 bool
156 GetEventMessage(lldb::tid_t tid, unsigned long *message);
157
Stephen Wilson84ffe702011-03-30 15:55:52 +0000158 /// Resumes the given thread. If @p signo is anything but
159 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000160 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000161 Resume(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000162
Stephen Wilson84ffe702011-03-30 15:55:52 +0000163 /// Single steps the given thread. If @p signo is anything but
164 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000165 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000166 SingleStep(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000167
168 /// Sends the inferior process a PTRACE_KILL signal. The inferior will
169 /// still exists and can be interrogated. Once resumed it will exit as
170 /// though it received a SIGKILL.
171 bool
172 BringProcessIntoLimbo();
173
Greg Clayton743ecf42012-10-16 20:20:18 +0000174 lldb_private::Error
Stephen Wilson84ffe702011-03-30 15:55:52 +0000175 Detach();
176
177
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000178private:
179 ProcessLinux *m_process;
180
181 lldb::thread_t m_operation_thread;
Matt Kopec7de48462013-03-06 17:20:48 +0000182 lldb::thread_t m_monitor_thread;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000183 lldb::pid_t m_pid;
184 int m_terminal_fd;
185
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000186
187 lldb_private::Mutex m_server_mutex;
188 int m_client_fd;
189 int m_server_fd;
190
Johnny Chen25e68e32011-06-14 19:19:50 +0000191 struct OperationArgs
192 {
193 OperationArgs(ProcessMonitor *monitor);
194
195 ~OperationArgs();
196
197 ProcessMonitor *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
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000202 /// @class LauchArgs
203 ///
204 /// @brief Simple structure to pass data to the thread responsible for
205 /// launching a child process.
Johnny Chen25e68e32011-06-14 19:19:50 +0000206 struct LaunchArgs : OperationArgs
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000207 {
208 LaunchArgs(ProcessMonitor *monitor,
209 lldb_private::Module *module,
210 char const **argv,
211 char const **envp,
212 const char *stdin_path,
213 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000214 const char *stderr_path,
215 const char *working_dir);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000216
217 ~LaunchArgs();
218
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000219 lldb_private::Module *m_module; // The executable image to launch.
220 char const **m_argv; // Process arguments.
221 char const **m_envp; // Process environment.
222 const char *m_stdin_path; // Redirect stdin or NULL.
223 const char *m_stdout_path; // Redirect stdout or NULL.
224 const char *m_stderr_path; // Redirect stderr or NULL.
Daniel Malea6217d2a2013-01-08 14:49:22 +0000225 const char *m_working_dir; // Working directory or NULL.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000226 };
227
228 void
Johnny Chen25e68e32011-06-14 19:19:50 +0000229 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000230
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000231 static void *
Johnny Chen25e68e32011-06-14 19:19:50 +0000232 LaunchOpThread(void *arg);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000233
234 static bool
235 Launch(LaunchArgs *args);
236
237 bool
238 EnableIPC();
239
Johnny Chen25e68e32011-06-14 19:19:50 +0000240 struct AttachArgs : OperationArgs
241 {
242 AttachArgs(ProcessMonitor *monitor,
243 lldb::pid_t pid);
244
245 ~AttachArgs();
246
247 lldb::pid_t m_pid; // pid of the process to be attached.
248 };
249
250 void
251 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
252
Johnny Chen25e68e32011-06-14 19:19:50 +0000253 static void *
254 AttachOpThread(void *args);
255
256 static bool
257 Attach(AttachArgs *args);
258
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000259 static void
Johnny Chen25e68e32011-06-14 19:19:50 +0000260 ServeOperation(OperationArgs *args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000261
262 static bool
263 DupDescriptor(const char *path, int fd, int flags);
264
265 static bool
266 MonitorCallback(void *callback_baton,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +0000267 lldb::pid_t pid, bool exited, int signal, int status);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000268
269 static ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +0000270 MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000271 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000272
273 static ProcessMessage
274 MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000275 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000276
277 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000278 GetCrashReasonForSIGSEGV(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000279
280 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000281 GetCrashReasonForSIGILL(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000282
283 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000284 GetCrashReasonForSIGFPE(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000285
286 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000287 GetCrashReasonForSIGBUS(const siginfo_t *info);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000288
289 void
290 DoOperation(Operation *op);
Stephen Wilson9212d7f2011-01-04 21:40:25 +0000291
292 /// Stops the child monitor thread.
Stephen Wilson84ffe702011-03-30 15:55:52 +0000293 void
294 StopMonitoringChildProcess();
295
296 void
297 StopMonitor();
298
Greg Clayton743ecf42012-10-16 20:20:18 +0000299 /// Stops the operation thread used to attach/launch a process.
300 void
301 StopOpThread();
302
Stephen Wilson84ffe702011-03-30 15:55:52 +0000303 void
304 CloseFD(int &fd);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000305};
306
307#endif // #ifndef liblldb_ProcessMonitor_H_