blob: fdccb67745a7dbb3aa27ffef3be53e002bf2ea15 [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
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000203 lldb_private::Module *m_module; // The executable image to launch.
204 char const **m_argv; // Process arguments.
205 char const **m_envp; // Process environment.
206 const char *m_stdin_path; // Redirect stdin or NULL.
207 const char *m_stdout_path; // Redirect stdout or NULL.
208 const char *m_stderr_path; // Redirect stderr or NULL.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000209 };
210
211 void
Johnny Chen25e68e32011-06-14 19:19:50 +0000212 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000213
214 void
Johnny Chen25e68e32011-06-14 19:19:50 +0000215 StopLaunchOpThread();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000216
217 static void *
Johnny Chen25e68e32011-06-14 19:19:50 +0000218 LaunchOpThread(void *arg);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000219
220 static bool
221 Launch(LaunchArgs *args);
222
223 bool
224 EnableIPC();
225
Johnny Chen25e68e32011-06-14 19:19:50 +0000226 struct AttachArgs : OperationArgs
227 {
228 AttachArgs(ProcessMonitor *monitor,
229 lldb::pid_t pid);
230
231 ~AttachArgs();
232
233 lldb::pid_t m_pid; // pid of the process to be attached.
234 };
235
236 void
237 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
238
239 void
240 StopAttachOpThread();
241
242 static void *
243 AttachOpThread(void *args);
244
245 static bool
246 Attach(AttachArgs *args);
247
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000248 static void
Johnny Chen25e68e32011-06-14 19:19:50 +0000249 ServeOperation(OperationArgs *args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000250
251 static bool
252 DupDescriptor(const char *path, int fd, int flags);
253
254 static bool
255 MonitorCallback(void *callback_baton,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +0000256 lldb::pid_t pid, bool exited, int signal, int status);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000257
258 static ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +0000259 MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000260 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000261
262 static ProcessMessage
263 MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000264 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000265
266 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000267 GetCrashReasonForSIGSEGV(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000268
269 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000270 GetCrashReasonForSIGILL(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000271
272 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000273 GetCrashReasonForSIGFPE(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000274
275 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000276 GetCrashReasonForSIGBUS(const siginfo_t *info);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000277
278 void
279 DoOperation(Operation *op);
Stephen Wilson9212d7f2011-01-04 21:40:25 +0000280
281 /// Stops the child monitor thread.
Stephen Wilson84ffe702011-03-30 15:55:52 +0000282 void
283 StopMonitoringChildProcess();
284
285 void
286 StopMonitor();
287
288 void
289 CloseFD(int &fd);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000290};
291
292#endif // #ifndef liblldb_ProcessMonitor_H_