blob: 4d02e61a10fdb8c5621e15fe61adb9fd54301d40 [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.
Andrew Kaylor6578cb62013-07-09 22:36:48 +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
Andrew Kaylor6578cb62013-07-09 22:36:48 +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
Matt Kopec085d6ce2013-05-31 22:00:07 +0000178 Detach(lldb::tid_t tid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000179
Andrew Kaylorbc68b432013-07-10 21:57:27 +0000180 /// Stops the monitoring the child process thread.
181 void
182 StopMonitor();
183
Andrew Kaylor93132f52013-05-28 23:04:25 +0000184 /// Stops the requested thread and waits for the stop signal.
185 bool
186 StopThread(lldb::tid_t tid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000187
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000188private:
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000189 ProcessLinux *m_process;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000190
191 lldb::thread_t m_operation_thread;
Matt Kopec7de48462013-03-06 17:20:48 +0000192 lldb::thread_t m_monitor_thread;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000193 lldb::pid_t m_pid;
194 int m_terminal_fd;
195
Daniel Malea1efb4182013-09-16 23:12:18 +0000196 // current operation which must be executed on the priviliged thread
197 Operation *m_operation;
198 lldb_private::Mutex m_operation_mutex;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000199
Daniel Malea1efb4182013-09-16 23:12:18 +0000200 // semaphores notified when Operation is ready to be processed and when
201 // the operation is complete.
202 sem_t m_operation_pending;
203 sem_t m_operation_done;
204
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000205
Johnny Chen25e68e32011-06-14 19:19:50 +0000206 struct OperationArgs
207 {
208 OperationArgs(ProcessMonitor *monitor);
209
210 ~OperationArgs();
211
212 ProcessMonitor *m_monitor; // The monitor performing the attach.
213 sem_t m_semaphore; // Posted to once operation complete.
214 lldb_private::Error m_error; // Set if process operation failed.
215 };
216
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000217 /// @class LauchArgs
218 ///
219 /// @brief Simple structure to pass data to the thread responsible for
220 /// launching a child process.
Johnny Chen25e68e32011-06-14 19:19:50 +0000221 struct LaunchArgs : OperationArgs
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000222 {
223 LaunchArgs(ProcessMonitor *monitor,
224 lldb_private::Module *module,
225 char const **argv,
226 char const **envp,
227 const char *stdin_path,
228 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000229 const char *stderr_path,
230 const char *working_dir);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000231
232 ~LaunchArgs();
233
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000234 lldb_private::Module *m_module; // The executable image to launch.
235 char const **m_argv; // Process arguments.
236 char const **m_envp; // Process environment.
237 const char *m_stdin_path; // Redirect stdin or NULL.
238 const char *m_stdout_path; // Redirect stdout or NULL.
239 const char *m_stderr_path; // Redirect stderr or NULL.
Daniel Malea6217d2a2013-01-08 14:49:22 +0000240 const char *m_working_dir; // Working directory or NULL.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000241 };
242
243 void
Johnny Chen25e68e32011-06-14 19:19:50 +0000244 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000245
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000246 static void *
Johnny Chen25e68e32011-06-14 19:19:50 +0000247 LaunchOpThread(void *arg);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000248
249 static bool
250 Launch(LaunchArgs *args);
251
Johnny Chen25e68e32011-06-14 19:19:50 +0000252 struct AttachArgs : OperationArgs
253 {
254 AttachArgs(ProcessMonitor *monitor,
255 lldb::pid_t pid);
256
257 ~AttachArgs();
258
259 lldb::pid_t m_pid; // pid of the process to be attached.
260 };
261
262 void
263 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
264
Johnny Chen25e68e32011-06-14 19:19:50 +0000265 static void *
266 AttachOpThread(void *args);
267
268 static bool
269 Attach(AttachArgs *args);
270
Matt Kopec085d6ce2013-05-31 22:00:07 +0000271 static bool
272 SetDefaultPtraceOpts(const lldb::pid_t);
273
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000274 static void
Johnny Chen25e68e32011-06-14 19:19:50 +0000275 ServeOperation(OperationArgs *args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000276
277 static bool
278 DupDescriptor(const char *path, int fd, int flags);
279
280 static bool
281 MonitorCallback(void *callback_baton,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +0000282 lldb::pid_t pid, bool exited, int signal, int status);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000283
284 static ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +0000285 MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000286 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000287
288 static ProcessMessage
289 MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000290 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000291
292 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000293 GetCrashReasonForSIGSEGV(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000294
295 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000296 GetCrashReasonForSIGILL(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000297
298 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000299 GetCrashReasonForSIGFPE(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000300
301 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000302 GetCrashReasonForSIGBUS(const siginfo_t *info);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000303
304 void
305 DoOperation(Operation *op);
Stephen Wilson9212d7f2011-01-04 21:40:25 +0000306
307 /// Stops the child monitor thread.
Stephen Wilson84ffe702011-03-30 15:55:52 +0000308 void
309 StopMonitoringChildProcess();
310
Greg Clayton743ecf42012-10-16 20:20:18 +0000311 /// Stops the operation thread used to attach/launch a process.
312 void
313 StopOpThread();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000314};
315
316#endif // #ifndef liblldb_ProcessMonitor_H_