blob: bc0fbdf2aaef553a9ea915c6c7e3ac928344b7e7 [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.
50 ProcessMonitor(ProcessPOSIX *process,
51 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
60 ProcessMonitor(ProcessPOSIX *process,
61 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.
Daniel Maleaf0da3712012-12-18 19:50:15 +0000107 /// FIXME: The FreeBSD implementation of this function should use tid in order
108 /// to enable support for debugging threaded programs.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000109 bool
Daniel Maleaf0da3712012-12-18 19:50:15 +0000110 ReadRegisterValue(lldb::tid_t tid, unsigned offset,
111 unsigned size, lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +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 RegisterContextFreeBSD derivatives.
Daniel Maleaf0da3712012-12-18 19:50:15 +0000117 /// FIXME: The FreeBSD implementation of this function should use tid in order
118 /// to enable support for debugging threaded programs.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000119 bool
Daniel Maleaf0da3712012-12-18 19:50:15 +0000120 WriteRegisterValue(lldb::tid_t tid, unsigned offset,
121 const lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000122
123 /// Reads all general purpose registers into the specified buffer.
Daniel Maleaf0da3712012-12-18 19:50:15 +0000124 /// FIXME: The FreeBSD implementation of this function should use tid in order
125 /// to enable support for debugging threaded programs.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000126 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000127 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000128
Matt Kopecc6672c82013-03-15 20:00:39 +0000129 /// Reads all floating point registers into the specified buffer.
Daniel Maleaf0da3712012-12-18 19:50:15 +0000130 /// FIXME: The FreeBSD implementation of this function should use tid in order
131 /// to enable support for debugging threaded programs.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000132 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000133 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000134
135 /// Writes all general purpose registers into the specified buffer.
Daniel Maleaf0da3712012-12-18 19:50:15 +0000136 /// FIXME: The FreeBSD implementation of this function should use tid in order
137 /// to enable support for debugging threaded programs.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000138 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000139 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000140
Matt Kopecc6672c82013-03-15 20:00:39 +0000141 /// Writes all floating point registers into the specified buffer.
Daniel Maleaf0da3712012-12-18 19:50:15 +0000142 /// FIXME: The FreeBSD implementation of this function should use tid in order
143 /// to enable support for debugging threaded programs.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000144 bool
Matt Kopecc6672c82013-03-15 20:00:39 +0000145 WriteFPR(lldb::tid_t tid, void *buf);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000146
147 /// 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 &errno);
Johnny Chen9ed5b492012-01-05 21:48:15 +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
158 /// Resumes the given thread. If @p signo is anything but
159 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
160 bool
161 Resume(lldb::tid_t tid, uint32_t signo);
162
163 /// Single steps the given thread. If @p signo is anything but
164 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
165 bool
166 SingleStep(lldb::tid_t tid, uint32_t signo);
167
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
174 lldb_private::Error
175 Detach();
176
177
178private:
179 ProcessFreeBSD *m_process;
180
181 lldb::thread_t m_operation_thread;
182 lldb::thread_t m_monitor_thread;
183 lldb::pid_t m_pid;
184
185
186 lldb_private::Mutex m_server_mutex;
187 int m_terminal_fd;
188 int m_client_fd;
189 int m_server_fd;
190
191 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
202 /// @class LauchArgs
203 ///
204 /// @brief Simple structure to pass data to the thread responsible for
205 /// launching a child process.
206 struct LaunchArgs : OperationArgs
207 {
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);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000216
217 ~LaunchArgs();
218
219 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.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000226 };
227
228 void
229 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
230
231 void
232 StopLaunchOpThread();
233
234 static void *
235 LaunchOpThread(void *arg);
236
237 static bool
238 Launch(LaunchArgs *args);
239
240 bool
241 EnableIPC();
242
243 struct AttachArgs : OperationArgs
244 {
245 AttachArgs(ProcessMonitor *monitor,
246 lldb::pid_t pid);
247
248 ~AttachArgs();
249
250 lldb::pid_t m_pid; // pid of the process to be attached.
251 };
252
253 void
254 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
255
256 void
257 StopAttachOpThread();
258
259 static void *
260 AttachOpThread(void *args);
261
262 static bool
263 Attach(AttachArgs *args);
264
265 static void
266 ServeOperation(OperationArgs *args);
267
268 static bool
269 DupDescriptor(const char *path, int fd, int flags);
270
271 static bool
272 MonitorCallback(void *callback_baton,
273 lldb::pid_t pid, bool exited, int signal, int status);
274
275 static ProcessMessage
276 MonitorSIGTRAP(ProcessMonitor *monitor,
277 const siginfo_t *info, lldb::pid_t pid);
278
279 static ProcessMessage
280 MonitorSignal(ProcessMonitor *monitor,
281 const siginfo_t *info, lldb::pid_t pid);
282
283 static ProcessMessage::CrashReason
284 GetCrashReasonForSIGSEGV(const siginfo_t *info);
285
286 static ProcessMessage::CrashReason
287 GetCrashReasonForSIGILL(const siginfo_t *info);
288
289 static ProcessMessage::CrashReason
290 GetCrashReasonForSIGFPE(const siginfo_t *info);
291
292 static ProcessMessage::CrashReason
293 GetCrashReasonForSIGBUS(const siginfo_t *info);
294
295 void
296 DoOperation(Operation *op);
297
298 /// Stops the child monitor thread.
299 void
300 StopMonitoringChildProcess();
301
302 void
303 StopMonitor();
304
305 void
306 CloseFD(int &fd);
307};
308
309#endif // #ifndef liblldb_ProcessMonitor_H_