blob: 9c630049caf72c1353a83aa69ac80ae5fd06b78e [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.
50 ProcessMonitor(ProcessPOSIX *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
59 ProcessMonitor(ProcessPOSIX *process,
60 lldb::pid_t pid,
61 lldb_private::Error &error);
62
63 ~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 ProcessFreeBSD &
71 GetProcess() { return *m_process; }
72
73 /// Returns a file descriptor to the controlling terminal of the inferior
74 /// process.
75 ///
76 /// Reads from this file descriptor yield both the standard output and
77 /// 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 RegisterContextFreeBSD derivatives.
106 bool
107 ReadRegisterValue(unsigned offset, unsigned size, lldb_private::RegisterValue &value);
108
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 RegisterContextFreeBSD derivatives.
113 bool
114 WriteRegisterValue(unsigned offset, const lldb_private::RegisterValue &value);
115
116 /// 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
124 /// 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
132 /// 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
143 /// Resumes the given thread. If @p signo is anything but
144 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
145 bool
146 Resume(lldb::tid_t tid, uint32_t signo);
147
148 /// Single steps the given thread. If @p signo is anything but
149 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
150 bool
151 SingleStep(lldb::tid_t tid, uint32_t signo);
152
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
159 lldb_private::Error
160 Detach();
161
162
163private:
164 ProcessFreeBSD *m_process;
165
166 lldb::thread_t m_operation_thread;
167 lldb::thread_t m_monitor_thread;
168 lldb::pid_t m_pid;
169
170
171 lldb_private::Mutex m_server_mutex;
172 int m_terminal_fd;
173 int m_client_fd;
174 int m_server_fd;
175
176 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
187 /// @class LauchArgs
188 ///
189 /// @brief Simple structure to pass data to the thread responsible for
190 /// launching a child process.
191 struct LaunchArgs : OperationArgs
192 {
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 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.
209 };
210
211 void
212 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
213
214 void
215 StopLaunchOpThread();
216
217 static void *
218 LaunchOpThread(void *arg);
219
220 static bool
221 Launch(LaunchArgs *args);
222
223 bool
224 EnableIPC();
225
226 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
248 static void
249 ServeOperation(OperationArgs *args);
250
251 static bool
252 DupDescriptor(const char *path, int fd, int flags);
253
254 static bool
255 MonitorCallback(void *callback_baton,
256 lldb::pid_t pid, bool exited, int signal, int status);
257
258 static ProcessMessage
259 MonitorSIGTRAP(ProcessMonitor *monitor,
260 const siginfo_t *info, lldb::pid_t pid);
261
262 static ProcessMessage
263 MonitorSignal(ProcessMonitor *monitor,
264 const siginfo_t *info, lldb::pid_t pid);
265
266 static ProcessMessage::CrashReason
267 GetCrashReasonForSIGSEGV(const siginfo_t *info);
268
269 static ProcessMessage::CrashReason
270 GetCrashReasonForSIGILL(const siginfo_t *info);
271
272 static ProcessMessage::CrashReason
273 GetCrashReasonForSIGFPE(const siginfo_t *info);
274
275 static ProcessMessage::CrashReason
276 GetCrashReasonForSIGBUS(const siginfo_t *info);
277
278 void
279 DoOperation(Operation *op);
280
281 /// Stops the child monitor thread.
282 void
283 StopMonitoringChildProcess();
284
285 void
286 StopMonitor();
287
288 void
289 CloseFD(int &fd);
290};
291
292#endif // #ifndef liblldb_ProcessMonitor_H_