blob: 0f2f61c8d58cac433bcba901761d436c7467a4e8 [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
59 ~ProcessMonitor();
60
61 /// Provides the process number of debugee.
62 lldb::pid_t
63 GetPID() const { return m_pid; }
64
65 /// Returns the process associated with this ProcessMonitor.
66 ProcessLinux &
67 GetProcess() { return *m_process; }
68
Greg Clayton710dd5a2011-01-08 20:28:42 +000069 /// Returns a file descriptor to the controlling terminal of the inferior
Stephen Wilsone6f9f662010-07-24 02:19:04 +000070 /// process.
71 ///
Greg Clayton710dd5a2011-01-08 20:28:42 +000072 /// Reads from this file descriptor yield both the standard output and
Stephen Wilsone6f9f662010-07-24 02:19:04 +000073 /// standard error of this debugee. Even if stderr and stdout were
74 /// redirected on launch it may still happen that data is available on this
75 /// descriptor (if the inferior process opens /dev/tty, for example).
76 ///
77 /// If this monitor was attached to an existing process this method returns
78 /// -1.
79 int
80 GetTerminalFD() const { return m_terminal_fd; }
81
82 /// Reads @p size bytes from address @vm_adder in the inferior process
83 /// address space.
84 ///
85 /// This method is provided to implement Process::DoReadMemory.
86 size_t
87 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
88 lldb_private::Error &error);
89
90 /// Writes @p size bytes from address @p vm_adder in the inferior process
91 /// address space.
92 ///
93 /// This method is provided to implement Process::DoWriteMemory.
94 size_t
95 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
96 lldb_private::Error &error);
97
98 /// Reads the contents from the register identified by the given (architecture
99 /// dependent) offset.
100 ///
101 /// This method is provided for use by RegisterContextLinux derivatives.
102 bool
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000103 ReadRegisterValue(unsigned offset, lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000104
105 /// Writes the given value to the register identified by the given
106 /// (architecture dependent) offset.
107 ///
108 /// This method is provided for use by RegisterContextLinux derivatives.
109 bool
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000110 WriteRegisterValue(unsigned offset, const lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000111
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000112 /// Reads all general purpose registers into the specified buffer.
113 bool
114 ReadGPR(void *buf);
115
116 /// Reads all floating point registers into the specified buffer.
117 bool
118 ReadFPR(void *buf);
119
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000120 /// Writes a siginfo_t structure corresponding to the given thread ID to the
121 /// memory region pointed to by @p siginfo.
122 bool
123 GetSignalInfo(lldb::tid_t tid, void *siginfo);
124
125 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
126 /// corresponding to the given thread IDto the memory pointed to by @p
127 /// message.
128 bool
129 GetEventMessage(lldb::tid_t tid, unsigned long *message);
130
Stephen Wilson84ffe702011-03-30 15:55:52 +0000131 /// Resumes the given thread. If @p signo is anything but
132 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000133 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000134 Resume(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000135
Stephen Wilson84ffe702011-03-30 15:55:52 +0000136 /// Single steps the given thread. If @p signo is anything but
137 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000138 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000139 SingleStep(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000140
141 /// Sends the inferior process a PTRACE_KILL signal. The inferior will
142 /// still exists and can be interrogated. Once resumed it will exit as
143 /// though it received a SIGKILL.
144 bool
145 BringProcessIntoLimbo();
146
Stephen Wilson84ffe702011-03-30 15:55:52 +0000147 bool
148 Detach();
149
150
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000151private:
152 ProcessLinux *m_process;
153
154 lldb::thread_t m_operation_thread;
155 lldb::pid_t m_pid;
156 int m_terminal_fd;
157
Stephen Wilsonf62308c2011-01-15 00:11:28 +0000158 lldb::thread_t m_monitor_thread;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000159
160 lldb_private::Mutex m_server_mutex;
161 int m_client_fd;
162 int m_server_fd;
163
164 /// @class LauchArgs
165 ///
166 /// @brief Simple structure to pass data to the thread responsible for
167 /// launching a child process.
168 struct LaunchArgs
169 {
170 LaunchArgs(ProcessMonitor *monitor,
171 lldb_private::Module *module,
172 char const **argv,
173 char const **envp,
174 const char *stdin_path,
175 const char *stdout_path,
176 const char *stderr_path);
177
178 ~LaunchArgs();
179
180 ProcessMonitor *m_monitor; // The monitor performing the launch.
181 lldb_private::Module *m_module; // The executable image to launch.
182 char const **m_argv; // Process arguments.
183 char const **m_envp; // Process environment.
184 const char *m_stdin_path; // Redirect stdin or NULL.
185 const char *m_stdout_path; // Redirect stdout or NULL.
186 const char *m_stderr_path; // Redirect stderr or NULL.
187 sem_t m_semaphore; // Posted to once launch complete.
188 lldb_private::Error m_error; // Set if process launch failed.
189 };
190
191 void
192 StartOperationThread(LaunchArgs *args, lldb_private::Error &error);
193
194 void
195 StopOperationThread();
196
197 static void *
198 OperationThread(void *arg);
199
200 static bool
201 Launch(LaunchArgs *args);
202
203 bool
204 EnableIPC();
205
206 static void
Stephen Wilson570243b2011-01-19 01:37:06 +0000207 ServeOperation(LaunchArgs *args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000208
209 static bool
210 DupDescriptor(const char *path, int fd, int flags);
211
212 static bool
213 MonitorCallback(void *callback_baton,
214 lldb::pid_t pid, int signal, int status);
215
216 static ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +0000217 MonitorSIGTRAP(ProcessMonitor *monitor,
218 const struct siginfo *info, lldb::pid_t pid);
219
220 static ProcessMessage
221 MonitorSignal(ProcessMonitor *monitor,
222 const struct siginfo *info, lldb::pid_t pid);
223
224 static ProcessMessage::CrashReason
225 GetCrashReasonForSIGSEGV(const struct siginfo *info);
226
227 static ProcessMessage::CrashReason
228 GetCrashReasonForSIGILL(const struct siginfo *info);
229
230 static ProcessMessage::CrashReason
231 GetCrashReasonForSIGFPE(const struct siginfo *info);
232
233 static ProcessMessage::CrashReason
234 GetCrashReasonForSIGBUS(const struct siginfo *info);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000235
236 void
237 DoOperation(Operation *op);
Stephen Wilson9212d7f2011-01-04 21:40:25 +0000238
239 /// Stops the child monitor thread.
Stephen Wilson84ffe702011-03-30 15:55:52 +0000240 void
241 StopMonitoringChildProcess();
242
243 void
244 StopMonitor();
245
246 void
247 CloseFD(int &fd);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000248};
249
250#endif // #ifndef liblldb_ProcessMonitor_H_