blob: 3729e257feb1143f1f928f1ee33776dc9bda0243 [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
Andrew Kaylord4d54992013-09-17 00:30:24 +0000188 // Waits for the initial stop message from a new thread.
189 bool
190 WaitForInitialTIDStop(lldb::tid_t tid);
191
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000192private:
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000193 ProcessLinux *m_process;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000194
195 lldb::thread_t m_operation_thread;
Matt Kopec7de48462013-03-06 17:20:48 +0000196 lldb::thread_t m_monitor_thread;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000197 lldb::pid_t m_pid;
198 int m_terminal_fd;
199
Daniel Malea1efb4182013-09-16 23:12:18 +0000200 // current operation which must be executed on the priviliged thread
201 Operation *m_operation;
202 lldb_private::Mutex m_operation_mutex;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000203
Daniel Malea1efb4182013-09-16 23:12:18 +0000204 // semaphores notified when Operation is ready to be processed and when
205 // the operation is complete.
206 sem_t m_operation_pending;
207 sem_t m_operation_done;
208
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000209
Johnny Chen25e68e32011-06-14 19:19:50 +0000210 struct OperationArgs
211 {
212 OperationArgs(ProcessMonitor *monitor);
213
214 ~OperationArgs();
215
216 ProcessMonitor *m_monitor; // The monitor performing the attach.
217 sem_t m_semaphore; // Posted to once operation complete.
218 lldb_private::Error m_error; // Set if process operation failed.
219 };
220
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000221 /// @class LauchArgs
222 ///
223 /// @brief Simple structure to pass data to the thread responsible for
224 /// launching a child process.
Johnny Chen25e68e32011-06-14 19:19:50 +0000225 struct LaunchArgs : OperationArgs
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000226 {
227 LaunchArgs(ProcessMonitor *monitor,
228 lldb_private::Module *module,
229 char const **argv,
230 char const **envp,
231 const char *stdin_path,
232 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000233 const char *stderr_path,
234 const char *working_dir);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000235
236 ~LaunchArgs();
237
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000238 lldb_private::Module *m_module; // The executable image to launch.
239 char const **m_argv; // Process arguments.
240 char const **m_envp; // Process environment.
241 const char *m_stdin_path; // Redirect stdin or NULL.
242 const char *m_stdout_path; // Redirect stdout or NULL.
243 const char *m_stderr_path; // Redirect stderr or NULL.
Daniel Malea6217d2a2013-01-08 14:49:22 +0000244 const char *m_working_dir; // Working directory or NULL.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000245 };
246
247 void
Johnny Chen25e68e32011-06-14 19:19:50 +0000248 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000249
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000250 static void *
Johnny Chen25e68e32011-06-14 19:19:50 +0000251 LaunchOpThread(void *arg);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000252
253 static bool
254 Launch(LaunchArgs *args);
255
Johnny Chen25e68e32011-06-14 19:19:50 +0000256 struct AttachArgs : OperationArgs
257 {
258 AttachArgs(ProcessMonitor *monitor,
259 lldb::pid_t pid);
260
261 ~AttachArgs();
262
263 lldb::pid_t m_pid; // pid of the process to be attached.
264 };
265
266 void
267 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
268
Johnny Chen25e68e32011-06-14 19:19:50 +0000269 static void *
270 AttachOpThread(void *args);
271
272 static bool
273 Attach(AttachArgs *args);
274
Matt Kopec085d6ce2013-05-31 22:00:07 +0000275 static bool
276 SetDefaultPtraceOpts(const lldb::pid_t);
277
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000278 static void
Johnny Chen25e68e32011-06-14 19:19:50 +0000279 ServeOperation(OperationArgs *args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000280
281 static bool
282 DupDescriptor(const char *path, int fd, int flags);
283
284 static bool
285 MonitorCallback(void *callback_baton,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +0000286 lldb::pid_t pid, bool exited, int signal, int status);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000287
288 static ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +0000289 MonitorSIGTRAP(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
293 MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000294 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000295
296 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000297 GetCrashReasonForSIGSEGV(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000298
299 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000300 GetCrashReasonForSIGILL(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000301
302 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000303 GetCrashReasonForSIGFPE(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000304
305 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000306 GetCrashReasonForSIGBUS(const siginfo_t *info);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000307
308 void
309 DoOperation(Operation *op);
Stephen Wilson9212d7f2011-01-04 21:40:25 +0000310
311 /// Stops the child monitor thread.
Stephen Wilson84ffe702011-03-30 15:55:52 +0000312 void
313 StopMonitoringChildProcess();
314
Greg Clayton743ecf42012-10-16 20:20:18 +0000315 /// Stops the operation thread used to attach/launch a process.
316 void
317 StopOpThread();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000318};
319
320#endif // #ifndef liblldb_ProcessMonitor_H_