blob: 5563991d26c2db888fada4b14b47408f365fd415 [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;
27} // End lldb_private namespace.
28
29class ProcessLinux;
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/// ProcessLinux instance by calling ProcessLinux::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.
Johnny Chen30213ff2012-01-05 19:17:38 +000050 ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +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,
Stephen Wilsone6f9f662010-07-24 02:19:04 +000058 lldb_private::Error &error);
59
Johnny Chen30213ff2012-01-05 19:17:38 +000060 ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +000061 lldb::pid_t pid,
62 lldb_private::Error &error);
63
Stephen Wilsone6f9f662010-07-24 02:19:04 +000064 ~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 ProcessLinux &
72 GetProcess() { return *m_process; }
73
Greg Clayton710dd5a2011-01-08 20:28:42 +000074 /// Returns a file descriptor to the controlling terminal of the inferior
Stephen Wilsone6f9f662010-07-24 02:19:04 +000075 /// process.
76 ///
Greg Clayton710dd5a2011-01-08 20:28:42 +000077 /// Reads from this file descriptor yield both the standard output and
Stephen Wilsone6f9f662010-07-24 02:19:04 +000078 /// 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 RegisterContextLinux 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);
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
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);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000118
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000119 /// Reads all general purpose registers into the specified buffer.
120 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000121 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000122
Matt Kopec58c0b962013-03-20 20:34:35 +0000123 /// Reads generic floating point registers into the specified buffer.
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000124 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000125 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000126
Matt Kopec58c0b962013-03-20 20:34:35 +0000127 /// Reads the specified register set into the specified buffer.
128 /// For instance, the extended floating-point register set.
129 bool
130 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
131
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000132 /// Writes all general purpose registers into the specified buffer.
133 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000134 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000135
Matt Kopec58c0b962013-03-20 20:34:35 +0000136 /// Writes generic floating point registers into the specified buffer.
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000137 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000138 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000139
Matt Kopec58c0b962013-03-20 20:34:35 +0000140 /// Writes the specified register set into the specified buffer.
141 /// For instance, the extended floating-point register set.
142 bool
143 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
144
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000145 /// Writes a siginfo_t structure corresponding to the given thread ID to the
146 /// memory region pointed to by @p siginfo.
147 bool
Daniel Maleaa35970a2012-11-23 18:09:58 +0000148 GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000149
150 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
151 /// corresponding to the given thread IDto the memory pointed to by @p
152 /// message.
153 bool
154 GetEventMessage(lldb::tid_t tid, unsigned long *message);
155
Stephen Wilson84ffe702011-03-30 15:55:52 +0000156 /// Resumes the given thread. If @p signo is anything but
157 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000158 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000159 Resume(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000160
Stephen Wilson84ffe702011-03-30 15:55:52 +0000161 /// Single steps the given thread. If @p signo is anything but
162 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000163 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000164 SingleStep(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000165
166 /// Sends the inferior process a PTRACE_KILL signal. The inferior will
167 /// still exists and can be interrogated. Once resumed it will exit as
168 /// though it received a SIGKILL.
169 bool
170 BringProcessIntoLimbo();
171
Greg Clayton743ecf42012-10-16 20:20:18 +0000172 lldb_private::Error
Stephen Wilson84ffe702011-03-30 15:55:52 +0000173 Detach();
174
175
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000176private:
177 ProcessLinux *m_process;
178
179 lldb::thread_t m_operation_thread;
Matt Kopec7de48462013-03-06 17:20:48 +0000180 lldb::thread_t m_monitor_thread;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000181 lldb::pid_t m_pid;
182 int m_terminal_fd;
183
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000184
185 lldb_private::Mutex m_server_mutex;
186 int m_client_fd;
187 int m_server_fd;
188
Johnny Chen25e68e32011-06-14 19:19:50 +0000189 struct OperationArgs
190 {
191 OperationArgs(ProcessMonitor *monitor);
192
193 ~OperationArgs();
194
195 ProcessMonitor *m_monitor; // The monitor performing the attach.
196 sem_t m_semaphore; // Posted to once operation complete.
197 lldb_private::Error m_error; // Set if process operation failed.
198 };
199
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000200 /// @class LauchArgs
201 ///
202 /// @brief Simple structure to pass data to the thread responsible for
203 /// launching a child process.
Johnny Chen25e68e32011-06-14 19:19:50 +0000204 struct LaunchArgs : OperationArgs
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000205 {
206 LaunchArgs(ProcessMonitor *monitor,
207 lldb_private::Module *module,
208 char const **argv,
209 char const **envp,
210 const char *stdin_path,
211 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000212 const char *stderr_path,
213 const char *working_dir);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000214
215 ~LaunchArgs();
216
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000217 lldb_private::Module *m_module; // The executable image to launch.
218 char const **m_argv; // Process arguments.
219 char const **m_envp; // Process environment.
220 const char *m_stdin_path; // Redirect stdin or NULL.
221 const char *m_stdout_path; // Redirect stdout or NULL.
222 const char *m_stderr_path; // Redirect stderr or NULL.
Daniel Malea6217d2a2013-01-08 14:49:22 +0000223 const char *m_working_dir; // Working directory or NULL.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000224 };
225
226 void
Johnny Chen25e68e32011-06-14 19:19:50 +0000227 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000228
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000229 static void *
Johnny Chen25e68e32011-06-14 19:19:50 +0000230 LaunchOpThread(void *arg);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000231
232 static bool
233 Launch(LaunchArgs *args);
234
235 bool
236 EnableIPC();
237
Johnny Chen25e68e32011-06-14 19:19:50 +0000238 struct AttachArgs : OperationArgs
239 {
240 AttachArgs(ProcessMonitor *monitor,
241 lldb::pid_t pid);
242
243 ~AttachArgs();
244
245 lldb::pid_t m_pid; // pid of the process to be attached.
246 };
247
248 void
249 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
250
Johnny Chen25e68e32011-06-14 19:19:50 +0000251 static void *
252 AttachOpThread(void *args);
253
254 static bool
255 Attach(AttachArgs *args);
256
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000257 static void
Johnny Chen25e68e32011-06-14 19:19:50 +0000258 ServeOperation(OperationArgs *args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000259
260 static bool
261 DupDescriptor(const char *path, int fd, int flags);
262
263 static bool
264 MonitorCallback(void *callback_baton,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +0000265 lldb::pid_t pid, bool exited, int signal, int status);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000266
267 static ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +0000268 MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000269 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000270
271 static ProcessMessage
272 MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000273 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000274
275 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000276 GetCrashReasonForSIGSEGV(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000277
278 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000279 GetCrashReasonForSIGILL(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000280
281 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000282 GetCrashReasonForSIGFPE(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000283
284 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000285 GetCrashReasonForSIGBUS(const siginfo_t *info);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000286
287 void
288 DoOperation(Operation *op);
Stephen Wilson9212d7f2011-01-04 21:40:25 +0000289
290 /// Stops the child monitor thread.
Stephen Wilson84ffe702011-03-30 15:55:52 +0000291 void
292 StopMonitoringChildProcess();
293
294 void
295 StopMonitor();
296
Greg Clayton743ecf42012-10-16 20:20:18 +0000297 /// Stops the operation thread used to attach/launch a process.
298 void
299 StopOpThread();
300
Stephen Wilson84ffe702011-03-30 15:55:52 +0000301 void
302 CloseFD(int &fd);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000303};
304
305#endif // #ifndef liblldb_ProcessMonitor_H_