blob: 1f28f7dcec5ded3a9cd0d26518591222a5043519 [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.
Andrew Kaylor6578cb62013-07-09 22:36:48 +000050 ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +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,
Johnny Chen9ed5b492012-01-05 21:48:15 +000058 lldb_private::Error &error);
59
Andrew Kaylor6578cb62013-07-09 22:36:48 +000060 ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +000061 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
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000110 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000111 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
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000120 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000121 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
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000135 /// Reads the specified register set into the specified buffer.
136 ///
137 /// This method is provided for use by RegisterContextFreeBSD derivatives.
138 /// FIXME: The FreeBSD implementation of this function should use tid in order
139 /// to enable support for debugging threaded programs.
140 bool
141 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
142
Johnny Chen9ed5b492012-01-05 21:48:15 +0000143 /// Writes all general purpose registers into the specified buffer.
Daniel Maleaf0da3712012-12-18 19:50:15 +0000144 /// FIXME: The FreeBSD implementation of this function should use tid in order
145 /// to enable support for debugging threaded programs.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000146 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000147 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000148
Matt Kopecc6672c82013-03-15 20:00:39 +0000149 /// Writes all floating point registers into the specified buffer.
Daniel Maleaf0da3712012-12-18 19:50:15 +0000150 /// FIXME: The FreeBSD implementation of this function should use tid in order
151 /// to enable support for debugging threaded programs.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000152 bool
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000153 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
154
155 /// Writes the specified register set into the specified buffer.
156 ///
157 /// This method is provided for use by RegisterContextFreeBSD derivatives.
158 /// FIXME: The FreeBSD implementation of this function should use tid in order
159 /// to enable support for debugging threaded programs.
160 bool
161 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000162
Ed Maste819e3992013-07-17 14:02:20 +0000163 /// Writes a ptrace_lwpinfo structure corresponding to the given thread ID
164 /// to the memory region pointed to by @p lwpinfo.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000165 bool
Ed Maste819e3992013-07-17 14:02:20 +0000166 GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &error_no);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000167
168 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
169 /// corresponding to the given thread IDto the memory pointed to by @p
170 /// message.
171 bool
172 GetEventMessage(lldb::tid_t tid, unsigned long *message);
173
174 /// Resumes the given thread. If @p signo is anything but
175 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
176 bool
177 Resume(lldb::tid_t tid, uint32_t signo);
178
179 /// Single steps the given thread. If @p signo is anything but
180 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
181 bool
182 SingleStep(lldb::tid_t tid, uint32_t signo);
183
184 /// Sends the inferior process a PTRACE_KILL signal. The inferior will
185 /// still exists and can be interrogated. Once resumed it will exit as
186 /// though it received a SIGKILL.
187 bool
188 BringProcessIntoLimbo();
189
190 lldb_private::Error
Matt Kopecedee1822013-06-03 19:48:53 +0000191 Detach(lldb::tid_t tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000192
Andrew Kaylorbc68b432013-07-10 21:57:27 +0000193 void
194 StopMonitor();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000195
Andrew Kaylord4d54992013-09-17 00:30:24 +0000196 // Waits for the initial stop message from a new thread.
197 bool
198 WaitForInitialTIDStop(lldb::tid_t tid);
199
Johnny Chen9ed5b492012-01-05 21:48:15 +0000200private:
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000201 ProcessFreeBSD *m_process;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000202
203 lldb::thread_t m_operation_thread;
204 lldb::thread_t m_monitor_thread;
205 lldb::pid_t m_pid;
206
Johnny Chen9ed5b492012-01-05 21:48:15 +0000207 int m_terminal_fd;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000208
Ed Maste756e1ff2013-09-18 19:34:08 +0000209 // current operation which must be executed on the privileged thread
210 Operation *m_operation;
211 lldb_private::Mutex m_operation_mutex;
212
213 // semaphores notified when Operation is ready to be processed and when
214 // the operation is complete.
215 sem_t m_operation_pending;
216 sem_t m_operation_done;
217
Johnny Chen9ed5b492012-01-05 21:48:15 +0000218 struct OperationArgs
219 {
220 OperationArgs(ProcessMonitor *monitor);
221
222 ~OperationArgs();
223
224 ProcessMonitor *m_monitor; // The monitor performing the attach.
225 sem_t m_semaphore; // Posted to once operation complete.
226 lldb_private::Error m_error; // Set if process operation failed.
227 };
228
229 /// @class LauchArgs
230 ///
231 /// @brief Simple structure to pass data to the thread responsible for
232 /// launching a child process.
233 struct LaunchArgs : OperationArgs
234 {
235 LaunchArgs(ProcessMonitor *monitor,
236 lldb_private::Module *module,
237 char const **argv,
238 char const **envp,
239 const char *stdin_path,
240 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000241 const char *stderr_path,
242 const char *working_dir);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000243
244 ~LaunchArgs();
245
246 lldb_private::Module *m_module; // The executable image to launch.
247 char const **m_argv; // Process arguments.
248 char const **m_envp; // Process environment.
249 const char *m_stdin_path; // Redirect stdin or NULL.
250 const char *m_stdout_path; // Redirect stdout or NULL.
251 const char *m_stderr_path; // Redirect stderr or NULL.
Daniel Malea6217d2a2013-01-08 14:49:22 +0000252 const char *m_working_dir; // Working directory or NULL.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000253 };
254
255 void
256 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
257
Johnny Chen9ed5b492012-01-05 21:48:15 +0000258 static void *
259 LaunchOpThread(void *arg);
260
261 static bool
262 Launch(LaunchArgs *args);
263
Johnny Chen9ed5b492012-01-05 21:48:15 +0000264 struct AttachArgs : OperationArgs
265 {
266 AttachArgs(ProcessMonitor *monitor,
267 lldb::pid_t pid);
268
269 ~AttachArgs();
270
271 lldb::pid_t m_pid; // pid of the process to be attached.
272 };
273
274 void
275 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
276
Johnny Chen9ed5b492012-01-05 21:48:15 +0000277 static void *
278 AttachOpThread(void *args);
279
280 static bool
281 Attach(AttachArgs *args);
282
283 static void
284 ServeOperation(OperationArgs *args);
285
286 static bool
287 DupDescriptor(const char *path, int fd, int flags);
288
289 static bool
290 MonitorCallback(void *callback_baton,
291 lldb::pid_t pid, bool exited, int signal, int status);
292
293 static ProcessMessage
294 MonitorSIGTRAP(ProcessMonitor *monitor,
295 const siginfo_t *info, lldb::pid_t pid);
296
297 static ProcessMessage
298 MonitorSignal(ProcessMonitor *monitor,
299 const siginfo_t *info, lldb::pid_t pid);
300
301 static ProcessMessage::CrashReason
302 GetCrashReasonForSIGSEGV(const siginfo_t *info);
303
304 static ProcessMessage::CrashReason
305 GetCrashReasonForSIGILL(const siginfo_t *info);
306
307 static ProcessMessage::CrashReason
308 GetCrashReasonForSIGFPE(const siginfo_t *info);
309
310 static ProcessMessage::CrashReason
311 GetCrashReasonForSIGBUS(const siginfo_t *info);
312
313 void
314 DoOperation(Operation *op);
315
316 /// Stops the child monitor thread.
317 void
318 StopMonitoringChildProcess();
319
Ed Mastea02f5532013-07-02 16:45:16 +0000320 /// Stops the operation thread used to attach/launch a process.
321 void
322 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000323};
324
325#endif // #ifndef liblldb_ProcessMonitor_H_