blob: ef9a179729a4c7d596cd96ff2fbca8ab86f2a850 [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.
50 ProcessMonitor(ProcessLinux *process,
51 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,
57 lldb_private::Error &error);
58
Johnny Chen25e68e32011-06-14 19:19:50 +000059 ProcessMonitor(ProcessLinux *process,
60 lldb::pid_t pid,
61 lldb_private::Error &error);
62
Stephen Wilsone6f9f662010-07-24 02:19:04 +000063 ~ProcessMonitor();
64
65 /// Provides the process number of debugee.
66 lldb::pid_t
67 GetPID() const { return m_pid; }
68
69 /// Returns the process associated with this ProcessMonitor.
70 ProcessLinux &
71 GetProcess() { return *m_process; }
72
Greg Clayton710dd5a2011-01-08 20:28:42 +000073 /// Returns a file descriptor to the controlling terminal of the inferior
Stephen Wilsone6f9f662010-07-24 02:19:04 +000074 /// process.
75 ///
Greg Clayton710dd5a2011-01-08 20:28:42 +000076 /// Reads from this file descriptor yield both the standard output and
Stephen Wilsone6f9f662010-07-24 02:19:04 +000077 /// standard error of this debugee. Even if stderr and stdout were
78 /// redirected on launch it may still happen that data is available on this
79 /// descriptor (if the inferior process opens /dev/tty, for example).
80 ///
81 /// If this monitor was attached to an existing process this method returns
82 /// -1.
83 int
84 GetTerminalFD() const { return m_terminal_fd; }
85
86 /// Reads @p size bytes from address @vm_adder in the inferior process
87 /// address space.
88 ///
89 /// This method is provided to implement Process::DoReadMemory.
90 size_t
91 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
92 lldb_private::Error &error);
93
94 /// Writes @p size bytes from address @p vm_adder in the inferior process
95 /// address space.
96 ///
97 /// This method is provided to implement Process::DoWriteMemory.
98 size_t
99 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
100 lldb_private::Error &error);
101
102 /// Reads the contents from the register identified by the given (architecture
103 /// dependent) offset.
104 ///
105 /// This method is provided for use by RegisterContextLinux derivatives.
106 bool
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000107 ReadRegisterValue(unsigned offset, lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000108
109 /// Writes the given value to the register identified by the given
110 /// (architecture dependent) offset.
111 ///
112 /// This method is provided for use by RegisterContextLinux derivatives.
113 bool
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000114 WriteRegisterValue(unsigned offset, const lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000115
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000116 /// Reads all general purpose registers into the specified buffer.
117 bool
118 ReadGPR(void *buf);
119
120 /// Reads all floating point registers into the specified buffer.
121 bool
122 ReadFPR(void *buf);
123
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000124 /// Writes all general purpose registers into the specified buffer.
125 bool
126 WriteGPR(void *buf);
127
128 /// Writes all floating point registers into the specified buffer.
129 bool
130 WriteFPR(void *buf);
131
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000132 /// Writes a siginfo_t structure corresponding to the given thread ID to the
133 /// memory region pointed to by @p siginfo.
134 bool
135 GetSignalInfo(lldb::tid_t tid, void *siginfo);
136
137 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
138 /// corresponding to the given thread IDto the memory pointed to by @p
139 /// message.
140 bool
141 GetEventMessage(lldb::tid_t tid, unsigned long *message);
142
Stephen Wilson84ffe702011-03-30 15:55:52 +0000143 /// Resumes the given thread. If @p signo is anything but
144 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000145 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000146 Resume(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000147
Stephen Wilson84ffe702011-03-30 15:55:52 +0000148 /// Single steps the given thread. If @p signo is anything but
149 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000150 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000151 SingleStep(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000152
153 /// Sends the inferior process a PTRACE_KILL signal. The inferior will
154 /// still exists and can be interrogated. Once resumed it will exit as
155 /// though it received a SIGKILL.
156 bool
157 BringProcessIntoLimbo();
158
Stephen Wilson84ffe702011-03-30 15:55:52 +0000159 bool
160 Detach();
161
162
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000163private:
164 ProcessLinux *m_process;
165
166 lldb::thread_t m_operation_thread;
167 lldb::pid_t m_pid;
168 int m_terminal_fd;
169
Stephen Wilsonf62308c2011-01-15 00:11:28 +0000170 lldb::thread_t m_monitor_thread;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000171
172 lldb_private::Mutex m_server_mutex;
173 int m_client_fd;
174 int m_server_fd;
175
Johnny Chen25e68e32011-06-14 19:19:50 +0000176 struct OperationArgs
177 {
178 OperationArgs(ProcessMonitor *monitor);
179
180 ~OperationArgs();
181
182 ProcessMonitor *m_monitor; // The monitor performing the attach.
183 sem_t m_semaphore; // Posted to once operation complete.
184 lldb_private::Error m_error; // Set if process operation failed.
185 };
186
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000187 /// @class LauchArgs
188 ///
189 /// @brief Simple structure to pass data to the thread responsible for
190 /// launching a child process.
Johnny Chen25e68e32011-06-14 19:19:50 +0000191 struct LaunchArgs : OperationArgs
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000192 {
193 LaunchArgs(ProcessMonitor *monitor,
194 lldb_private::Module *module,
195 char const **argv,
196 char const **envp,
197 const char *stdin_path,
198 const char *stdout_path,
199 const char *stderr_path);
200
201 ~LaunchArgs();
202
203 ProcessMonitor *m_monitor; // The monitor performing the launch.
204 lldb_private::Module *m_module; // The executable image to launch.
205 char const **m_argv; // Process arguments.
206 char const **m_envp; // Process environment.
207 const char *m_stdin_path; // Redirect stdin or NULL.
208 const char *m_stdout_path; // Redirect stdout or NULL.
209 const char *m_stderr_path; // Redirect stderr or NULL.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000210 };
211
212 void
Johnny Chen25e68e32011-06-14 19:19:50 +0000213 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000214
215 void
Johnny Chen25e68e32011-06-14 19:19:50 +0000216 StopLaunchOpThread();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000217
218 static void *
Johnny Chen25e68e32011-06-14 19:19:50 +0000219 LaunchOpThread(void *arg);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000220
221 static bool
222 Launch(LaunchArgs *args);
223
224 bool
225 EnableIPC();
226
Johnny Chen25e68e32011-06-14 19:19:50 +0000227 struct AttachArgs : OperationArgs
228 {
229 AttachArgs(ProcessMonitor *monitor,
230 lldb::pid_t pid);
231
232 ~AttachArgs();
233
234 lldb::pid_t m_pid; // pid of the process to be attached.
235 };
236
237 void
238 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
239
240 void
241 StopAttachOpThread();
242
243 static void *
244 AttachOpThread(void *args);
245
246 static bool
247 Attach(AttachArgs *args);
248
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000249 static void
Johnny Chen25e68e32011-06-14 19:19:50 +0000250 ServeOperation(OperationArgs *args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000251
252 static bool
253 DupDescriptor(const char *path, int fd, int flags);
254
255 static bool
256 MonitorCallback(void *callback_baton,
257 lldb::pid_t pid, int signal, int status);
258
259 static ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +0000260 MonitorSIGTRAP(ProcessMonitor *monitor,
261 const struct siginfo *info, lldb::pid_t pid);
262
263 static ProcessMessage
264 MonitorSignal(ProcessMonitor *monitor,
265 const struct siginfo *info, lldb::pid_t pid);
266
267 static ProcessMessage::CrashReason
268 GetCrashReasonForSIGSEGV(const struct siginfo *info);
269
270 static ProcessMessage::CrashReason
271 GetCrashReasonForSIGILL(const struct siginfo *info);
272
273 static ProcessMessage::CrashReason
274 GetCrashReasonForSIGFPE(const struct siginfo *info);
275
276 static ProcessMessage::CrashReason
277 GetCrashReasonForSIGBUS(const struct siginfo *info);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000278
279 void
280 DoOperation(Operation *op);
Stephen Wilson9212d7f2011-01-04 21:40:25 +0000281
282 /// Stops the child monitor thread.
Stephen Wilson84ffe702011-03-30 15:55:52 +0000283 void
284 StopMonitoringChildProcess();
285
286 void
287 StopMonitor();
288
289 void
290 CloseFD(int &fd);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000291};
292
293#endif // #ifndef liblldb_ProcessMonitor_H_