blob: b154a6d12221f8ca3f0bd2fd1d5379cbc3e19569 [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
Daniel Maleaf0da3712012-12-18 19:50:15 +0000109 ReadRegisterValue(lldb::tid_t tid, unsigned offset,
110 unsigned size, lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000111
112 /// Writes the given value to the register identified by the given
113 /// (architecture dependent) offset.
114 ///
115 /// This method is provided for use by RegisterContextLinux derivatives.
116 bool
Daniel Maleaf0da3712012-12-18 19:50:15 +0000117 WriteRegisterValue(lldb::tid_t tid, unsigned offset,
118 const lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000119
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000120 /// Reads all general purpose registers into the specified buffer.
121 bool
Daniel Maleaf0da3712012-12-18 19:50:15 +0000122 ReadGPR(lldb::tid_t tid, void *buf);
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000123
124 /// Reads all floating point registers into the specified buffer.
125 bool
Daniel Maleaf0da3712012-12-18 19:50:15 +0000126 ReadFPR(lldb::tid_t tid, void *buf);
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000127
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000128 /// Writes all general purpose registers into the specified buffer.
129 bool
Daniel Maleaf0da3712012-12-18 19:50:15 +0000130 WriteGPR(lldb::tid_t tid, void *buf);
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000131
132 /// Writes all floating point registers into the specified buffer.
133 bool
Daniel Maleaf0da3712012-12-18 19:50:15 +0000134 WriteFPR(lldb::tid_t tid, void *buf);
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000135
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000136 /// Writes a siginfo_t structure corresponding to the given thread ID to the
137 /// memory region pointed to by @p siginfo.
138 bool
Daniel Maleaa35970a2012-11-23 18:09:58 +0000139 GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000140
141 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
142 /// corresponding to the given thread IDto the memory pointed to by @p
143 /// message.
144 bool
145 GetEventMessage(lldb::tid_t tid, unsigned long *message);
146
Stephen Wilson84ffe702011-03-30 15:55:52 +0000147 /// Resumes the given thread. If @p signo is anything but
148 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000149 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000150 Resume(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000151
Stephen Wilson84ffe702011-03-30 15:55:52 +0000152 /// Single steps the given thread. If @p signo is anything but
153 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000154 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000155 SingleStep(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000156
157 /// Sends the inferior process a PTRACE_KILL signal. The inferior will
158 /// still exists and can be interrogated. Once resumed it will exit as
159 /// though it received a SIGKILL.
160 bool
161 BringProcessIntoLimbo();
162
Greg Clayton743ecf42012-10-16 20:20:18 +0000163 lldb_private::Error
Stephen Wilson84ffe702011-03-30 15:55:52 +0000164 Detach();
165
166
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000167private:
168 ProcessLinux *m_process;
169
170 lldb::thread_t m_operation_thread;
171 lldb::pid_t m_pid;
172 int m_terminal_fd;
173
Stephen Wilsonf62308c2011-01-15 00:11:28 +0000174 lldb::thread_t m_monitor_thread;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000175
176 lldb_private::Mutex m_server_mutex;
177 int m_client_fd;
178 int m_server_fd;
179
Johnny Chen25e68e32011-06-14 19:19:50 +0000180 struct OperationArgs
181 {
182 OperationArgs(ProcessMonitor *monitor);
183
184 ~OperationArgs();
185
186 ProcessMonitor *m_monitor; // The monitor performing the attach.
187 sem_t m_semaphore; // Posted to once operation complete.
188 lldb_private::Error m_error; // Set if process operation failed.
189 };
190
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000191 /// @class LauchArgs
192 ///
193 /// @brief Simple structure to pass data to the thread responsible for
194 /// launching a child process.
Johnny Chen25e68e32011-06-14 19:19:50 +0000195 struct LaunchArgs : OperationArgs
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000196 {
197 LaunchArgs(ProcessMonitor *monitor,
198 lldb_private::Module *module,
199 char const **argv,
200 char const **envp,
201 const char *stdin_path,
202 const char *stdout_path,
203 const char *stderr_path);
204
205 ~LaunchArgs();
206
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000207 lldb_private::Module *m_module; // The executable image to launch.
208 char const **m_argv; // Process arguments.
209 char const **m_envp; // Process environment.
210 const char *m_stdin_path; // Redirect stdin or NULL.
211 const char *m_stdout_path; // Redirect stdout or NULL.
212 const char *m_stderr_path; // Redirect stderr or NULL.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000213 };
214
215 void
Johnny Chen25e68e32011-06-14 19:19:50 +0000216 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000217
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000218 static void *
Johnny Chen25e68e32011-06-14 19:19:50 +0000219 LaunchOpThread(void *arg);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000220
221 static bool
222 Launch(LaunchArgs *args);
223
224 bool
225 EnableIPC();
226
Johnny Chen25e68e32011-06-14 19:19:50 +0000227 struct AttachArgs : OperationArgs
228 {
229 AttachArgs(ProcessMonitor *monitor,
230 lldb::pid_t pid);
231
232 ~AttachArgs();
233
234 lldb::pid_t m_pid; // pid of the process to be attached.
235 };
236
237 void
238 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
239
Johnny Chen25e68e32011-06-14 19:19:50 +0000240 static void *
241 AttachOpThread(void *args);
242
243 static bool
244 Attach(AttachArgs *args);
245
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000246 static void
Johnny Chen25e68e32011-06-14 19:19:50 +0000247 ServeOperation(OperationArgs *args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000248
249 static bool
250 DupDescriptor(const char *path, int fd, int flags);
251
252 static bool
253 MonitorCallback(void *callback_baton,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +0000254 lldb::pid_t pid, bool exited, int signal, int status);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000255
256 static ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +0000257 MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000258 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000259
260 static ProcessMessage
261 MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000262 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000263
264 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000265 GetCrashReasonForSIGSEGV(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000266
267 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000268 GetCrashReasonForSIGILL(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000269
270 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000271 GetCrashReasonForSIGFPE(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000272
273 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000274 GetCrashReasonForSIGBUS(const siginfo_t *info);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000275
276 void
277 DoOperation(Operation *op);
Stephen Wilson9212d7f2011-01-04 21:40:25 +0000278
279 /// Stops the child monitor thread.
Stephen Wilson84ffe702011-03-30 15:55:52 +0000280 void
281 StopMonitoringChildProcess();
282
283 void
284 StopMonitor();
285
Greg Clayton743ecf42012-10-16 20:20:18 +0000286 /// Stops the operation thread used to attach/launch a process.
287 void
288 StopOpThread();
289
Stephen Wilson84ffe702011-03-30 15:55:52 +0000290 void
291 CloseFD(int &fd);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000292};
293
294#endif // #ifndef liblldb_ProcessMonitor_H_