blob: 529503e9df0347c1c79c4ede5d4203876a72f960 [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
Andrew Kaylor93132f52013-05-28 23:04:25 +000066 enum ResumeSignals
67 {
68 eResumeSignalNone = 0
69 };
70
Stephen Wilsone6f9f662010-07-24 02:19:04 +000071 /// Provides the process number of debugee.
72 lldb::pid_t
73 GetPID() const { return m_pid; }
74
75 /// Returns the process associated with this ProcessMonitor.
76 ProcessLinux &
77 GetProcess() { return *m_process; }
78
Greg Clayton710dd5a2011-01-08 20:28:42 +000079 /// Returns a file descriptor to the controlling terminal of the inferior
Stephen Wilsone6f9f662010-07-24 02:19:04 +000080 /// process.
81 ///
Greg Clayton710dd5a2011-01-08 20:28:42 +000082 /// Reads from this file descriptor yield both the standard output and
Stephen Wilsone6f9f662010-07-24 02:19:04 +000083 /// standard error of this debugee. Even if stderr and stdout were
84 /// redirected on launch it may still happen that data is available on this
85 /// descriptor (if the inferior process opens /dev/tty, for example).
86 ///
87 /// If this monitor was attached to an existing process this method returns
88 /// -1.
89 int
90 GetTerminalFD() const { return m_terminal_fd; }
91
92 /// Reads @p size bytes from address @vm_adder in the inferior process
93 /// address space.
94 ///
95 /// This method is provided to implement Process::DoReadMemory.
96 size_t
97 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
98 lldb_private::Error &error);
99
100 /// Writes @p size bytes from address @p vm_adder in the inferior process
101 /// address space.
102 ///
103 /// This method is provided to implement Process::DoWriteMemory.
104 size_t
105 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
106 lldb_private::Error &error);
107
108 /// Reads the contents from the register identified by the given (architecture
109 /// dependent) offset.
110 ///
111 /// This method is provided for use by RegisterContextLinux derivatives.
112 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000113 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000114 unsigned size, lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000115
116 /// Writes the given value to the register identified by the given
117 /// (architecture dependent) offset.
118 ///
119 /// This method is provided for use by RegisterContextLinux derivatives.
120 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000121 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000122 const lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000123
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000124 /// Reads all general purpose registers into the specified buffer.
125 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000126 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000127
Matt Kopec58c0b962013-03-20 20:34:35 +0000128 /// Reads generic floating point registers into the specified buffer.
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000129 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000130 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000131
Matt Kopec58c0b962013-03-20 20:34:35 +0000132 /// Reads the specified register set into the specified buffer.
133 /// For instance, the extended floating-point register set.
134 bool
135 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
136
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000137 /// Writes all general purpose registers into the specified buffer.
138 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000139 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000140
Matt Kopec58c0b962013-03-20 20:34:35 +0000141 /// Writes generic floating point registers into the specified buffer.
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000142 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000143 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000144
Matt Kopec58c0b962013-03-20 20:34:35 +0000145 /// Writes the specified register set into the specified buffer.
146 /// For instance, the extended floating-point register set.
147 bool
148 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
149
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000150 /// Writes a siginfo_t structure corresponding to the given thread ID to the
151 /// memory region pointed to by @p siginfo.
152 bool
Daniel Maleaa35970a2012-11-23 18:09:58 +0000153 GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000154
155 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
156 /// corresponding to the given thread IDto the memory pointed to by @p
157 /// message.
158 bool
159 GetEventMessage(lldb::tid_t tid, unsigned long *message);
160
Stephen Wilson84ffe702011-03-30 15:55:52 +0000161 /// Resumes 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 Resume(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000165
Stephen Wilson84ffe702011-03-30 15:55:52 +0000166 /// Single steps the given thread. If @p signo is anything but
167 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000168 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000169 SingleStep(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000170
171 /// Sends the inferior process a PTRACE_KILL signal. The inferior will
172 /// still exists and can be interrogated. Once resumed it will exit as
173 /// though it received a SIGKILL.
174 bool
175 BringProcessIntoLimbo();
176
Greg Clayton743ecf42012-10-16 20:20:18 +0000177 lldb_private::Error
Stephen Wilson84ffe702011-03-30 15:55:52 +0000178 Detach();
179
Andrew Kaylor93132f52013-05-28 23:04:25 +0000180 /// Stops the requested thread and waits for the stop signal.
181 bool
182 StopThread(lldb::tid_t tid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000183
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000184private:
185 ProcessLinux *m_process;
186
187 lldb::thread_t m_operation_thread;
Matt Kopec7de48462013-03-06 17:20:48 +0000188 lldb::thread_t m_monitor_thread;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000189 lldb::pid_t m_pid;
190 int m_terminal_fd;
191
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000192
193 lldb_private::Mutex m_server_mutex;
194 int m_client_fd;
195 int m_server_fd;
196
Johnny Chen25e68e32011-06-14 19:19:50 +0000197 struct OperationArgs
198 {
199 OperationArgs(ProcessMonitor *monitor);
200
201 ~OperationArgs();
202
203 ProcessMonitor *m_monitor; // The monitor performing the attach.
204 sem_t m_semaphore; // Posted to once operation complete.
205 lldb_private::Error m_error; // Set if process operation failed.
206 };
207
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000208 /// @class LauchArgs
209 ///
210 /// @brief Simple structure to pass data to the thread responsible for
211 /// launching a child process.
Johnny Chen25e68e32011-06-14 19:19:50 +0000212 struct LaunchArgs : OperationArgs
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000213 {
214 LaunchArgs(ProcessMonitor *monitor,
215 lldb_private::Module *module,
216 char const **argv,
217 char const **envp,
218 const char *stdin_path,
219 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000220 const char *stderr_path,
221 const char *working_dir);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000222
223 ~LaunchArgs();
224
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000225 lldb_private::Module *m_module; // The executable image to launch.
226 char const **m_argv; // Process arguments.
227 char const **m_envp; // Process environment.
228 const char *m_stdin_path; // Redirect stdin or NULL.
229 const char *m_stdout_path; // Redirect stdout or NULL.
230 const char *m_stderr_path; // Redirect stderr or NULL.
Daniel Malea6217d2a2013-01-08 14:49:22 +0000231 const char *m_working_dir; // Working directory or NULL.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000232 };
233
234 void
Johnny Chen25e68e32011-06-14 19:19:50 +0000235 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000236
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000237 static void *
Johnny Chen25e68e32011-06-14 19:19:50 +0000238 LaunchOpThread(void *arg);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000239
240 static bool
241 Launch(LaunchArgs *args);
242
243 bool
244 EnableIPC();
245
Johnny Chen25e68e32011-06-14 19:19:50 +0000246 struct AttachArgs : OperationArgs
247 {
248 AttachArgs(ProcessMonitor *monitor,
249 lldb::pid_t pid);
250
251 ~AttachArgs();
252
253 lldb::pid_t m_pid; // pid of the process to be attached.
254 };
255
256 void
257 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
258
Johnny Chen25e68e32011-06-14 19:19:50 +0000259 static void *
260 AttachOpThread(void *args);
261
262 static bool
263 Attach(AttachArgs *args);
264
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000265 static void
Johnny Chen25e68e32011-06-14 19:19:50 +0000266 ServeOperation(OperationArgs *args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000267
268 static bool
269 DupDescriptor(const char *path, int fd, int flags);
270
271 static bool
272 MonitorCallback(void *callback_baton,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +0000273 lldb::pid_t pid, bool exited, int signal, int status);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000274
275 static ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +0000276 MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000277 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000278
279 static ProcessMessage
280 MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000281 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000282
283 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000284 GetCrashReasonForSIGSEGV(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000285
286 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000287 GetCrashReasonForSIGILL(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000288
289 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000290 GetCrashReasonForSIGFPE(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000291
292 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000293 GetCrashReasonForSIGBUS(const siginfo_t *info);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000294
295 void
296 DoOperation(Operation *op);
Stephen Wilson9212d7f2011-01-04 21:40:25 +0000297
298 /// Stops the child monitor thread.
Stephen Wilson84ffe702011-03-30 15:55:52 +0000299 void
300 StopMonitoringChildProcess();
301
302 void
303 StopMonitor();
304
Greg Clayton743ecf42012-10-16 20:20:18 +0000305 /// Stops the operation thread used to attach/launch a process.
306 void
307 StopOpThread();
308
Stephen Wilson84ffe702011-03-30 15:55:52 +0000309 void
310 CloseFD(int &fd);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000311};
312
313#endif // #ifndef liblldb_ProcessMonitor_H_