blob: 63b487e1808709133e08afaaeab00a9c00878eba [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,
59 lldb_private::Error &error);
60
Johnny Chen30213ff2012-01-05 19:17:38 +000061 ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +000062 lldb::pid_t pid,
63 lldb_private::Error &error);
64
Stephen Wilsone6f9f662010-07-24 02:19:04 +000065 ~ProcessMonitor();
66
67 /// Provides the process number of debugee.
68 lldb::pid_t
69 GetPID() const { return m_pid; }
70
71 /// Returns the process associated with this ProcessMonitor.
72 ProcessLinux &
73 GetProcess() { return *m_process; }
74
Greg Clayton710dd5a2011-01-08 20:28:42 +000075 /// Returns a file descriptor to the controlling terminal of the inferior
Stephen Wilsone6f9f662010-07-24 02:19:04 +000076 /// process.
77 ///
Greg Clayton710dd5a2011-01-08 20:28:42 +000078 /// Reads from this file descriptor yield both the standard output and
Stephen Wilsone6f9f662010-07-24 02:19:04 +000079 /// standard error of this debugee. Even if stderr and stdout were
80 /// redirected on launch it may still happen that data is available on this
81 /// descriptor (if the inferior process opens /dev/tty, for example).
82 ///
83 /// If this monitor was attached to an existing process this method returns
84 /// -1.
85 int
86 GetTerminalFD() const { return m_terminal_fd; }
87
88 /// Reads @p size bytes from address @vm_adder in the inferior process
89 /// address space.
90 ///
91 /// This method is provided to implement Process::DoReadMemory.
92 size_t
93 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
94 lldb_private::Error &error);
95
96 /// Writes @p size bytes from address @p vm_adder in the inferior process
97 /// address space.
98 ///
99 /// This method is provided to implement Process::DoWriteMemory.
100 size_t
101 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
102 lldb_private::Error &error);
103
104 /// Reads the contents from the register identified by the given (architecture
105 /// dependent) offset.
106 ///
107 /// This method is provided for use by RegisterContextLinux derivatives.
108 bool
Johnny Chen30213ff2012-01-05 19:17:38 +0000109 ReadRegisterValue(unsigned offset, unsigned size, lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +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 RegisterContextLinux derivatives.
115 bool
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000116 WriteRegisterValue(unsigned offset, const lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000117
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000118 /// Reads all general purpose registers into the specified buffer.
119 bool
120 ReadGPR(void *buf);
121
122 /// Reads all floating point registers into the specified buffer.
123 bool
124 ReadFPR(void *buf);
125
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000126 /// Writes all general purpose registers into the specified buffer.
127 bool
128 WriteGPR(void *buf);
129
130 /// Writes all floating point registers into the specified buffer.
131 bool
132 WriteFPR(void *buf);
133
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000134 /// Writes a siginfo_t structure corresponding to the given thread ID to the
135 /// memory region pointed to by @p siginfo.
136 bool
Daniel Maleaa35970a2012-11-23 18:09:58 +0000137 GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000138
139 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
140 /// corresponding to the given thread IDto the memory pointed to by @p
141 /// message.
142 bool
143 GetEventMessage(lldb::tid_t tid, unsigned long *message);
144
Stephen Wilson84ffe702011-03-30 15:55:52 +0000145 /// Resumes the given thread. If @p signo is anything but
146 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000147 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000148 Resume(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000149
Stephen Wilson84ffe702011-03-30 15:55:52 +0000150 /// Single steps the given thread. If @p signo is anything but
151 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000152 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000153 SingleStep(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000154
155 /// Sends the inferior process a PTRACE_KILL signal. The inferior will
156 /// still exists and can be interrogated. Once resumed it will exit as
157 /// though it received a SIGKILL.
158 bool
159 BringProcessIntoLimbo();
160
Greg Clayton743ecf42012-10-16 20:20:18 +0000161 lldb_private::Error
Stephen Wilson84ffe702011-03-30 15:55:52 +0000162 Detach();
163
164
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000165private:
166 ProcessLinux *m_process;
167
168 lldb::thread_t m_operation_thread;
169 lldb::pid_t m_pid;
170 int m_terminal_fd;
171
Stephen Wilsonf62308c2011-01-15 00:11:28 +0000172 lldb::thread_t m_monitor_thread;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000173
174 lldb_private::Mutex m_server_mutex;
175 int m_client_fd;
176 int m_server_fd;
177
Johnny Chen25e68e32011-06-14 19:19:50 +0000178 struct OperationArgs
179 {
180 OperationArgs(ProcessMonitor *monitor);
181
182 ~OperationArgs();
183
184 ProcessMonitor *m_monitor; // The monitor performing the attach.
185 sem_t m_semaphore; // Posted to once operation complete.
186 lldb_private::Error m_error; // Set if process operation failed.
187 };
188
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000189 /// @class LauchArgs
190 ///
191 /// @brief Simple structure to pass data to the thread responsible for
192 /// launching a child process.
Johnny Chen25e68e32011-06-14 19:19:50 +0000193 struct LaunchArgs : OperationArgs
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000194 {
195 LaunchArgs(ProcessMonitor *monitor,
196 lldb_private::Module *module,
197 char const **argv,
198 char const **envp,
199 const char *stdin_path,
200 const char *stdout_path,
201 const char *stderr_path);
202
203 ~LaunchArgs();
204
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000205 lldb_private::Module *m_module; // The executable image to launch.
206 char const **m_argv; // Process arguments.
207 char const **m_envp; // Process environment.
208 const char *m_stdin_path; // Redirect stdin or NULL.
209 const char *m_stdout_path; // Redirect stdout or NULL.
210 const char *m_stderr_path; // Redirect stderr or NULL.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000211 };
212
213 void
Johnny Chen25e68e32011-06-14 19:19:50 +0000214 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000215
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000216 static void *
Johnny Chen25e68e32011-06-14 19:19:50 +0000217 LaunchOpThread(void *arg);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000218
219 static bool
220 Launch(LaunchArgs *args);
221
222 bool
223 EnableIPC();
224
Johnny Chen25e68e32011-06-14 19:19:50 +0000225 struct AttachArgs : OperationArgs
226 {
227 AttachArgs(ProcessMonitor *monitor,
228 lldb::pid_t pid);
229
230 ~AttachArgs();
231
232 lldb::pid_t m_pid; // pid of the process to be attached.
233 };
234
235 void
236 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
237
Johnny Chen25e68e32011-06-14 19:19:50 +0000238 static void *
239 AttachOpThread(void *args);
240
241 static bool
242 Attach(AttachArgs *args);
243
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000244 static void
Johnny Chen25e68e32011-06-14 19:19:50 +0000245 ServeOperation(OperationArgs *args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000246
247 static bool
248 DupDescriptor(const char *path, int fd, int flags);
249
250 static bool
251 MonitorCallback(void *callback_baton,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +0000252 lldb::pid_t pid, bool exited, int signal, int status);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000253
254 static ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +0000255 MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000256 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000257
258 static ProcessMessage
259 MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000260 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000261
262 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000263 GetCrashReasonForSIGSEGV(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000264
265 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000266 GetCrashReasonForSIGILL(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000267
268 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000269 GetCrashReasonForSIGFPE(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000270
271 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000272 GetCrashReasonForSIGBUS(const siginfo_t *info);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000273
274 void
275 DoOperation(Operation *op);
Stephen Wilson9212d7f2011-01-04 21:40:25 +0000276
277 /// Stops the child monitor thread.
Stephen Wilson84ffe702011-03-30 15:55:52 +0000278 void
279 StopMonitoringChildProcess();
280
281 void
282 StopMonitor();
283
Greg Clayton743ecf42012-10-16 20:20:18 +0000284 /// Stops the operation thread used to attach/launch a process.
285 void
286 StopOpThread();
287
Stephen Wilson84ffe702011-03-30 15:55:52 +0000288 void
289 CloseFD(int &fd);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000290};
291
292#endif // #ifndef liblldb_ProcessMonitor_H_