blob: 9b8051b7bd99f94f547b679b2193e88b18b430db [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
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000120 /// Writes all general purpose registers into the specified buffer.
121 bool
122 WriteGPR(void *buf);
123
124 /// Writes all floating point registers into the specified buffer.
125 bool
126 WriteFPR(void *buf);
127
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000128 /// Writes a siginfo_t structure corresponding to the given thread ID to the
129 /// memory region pointed to by @p siginfo.
130 bool
131 GetSignalInfo(lldb::tid_t tid, void *siginfo);
132
133 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
134 /// corresponding to the given thread IDto the memory pointed to by @p
135 /// message.
136 bool
137 GetEventMessage(lldb::tid_t tid, unsigned long *message);
138
Stephen Wilson84ffe702011-03-30 15:55:52 +0000139 /// Resumes the given thread. If @p signo is anything but
140 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000141 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000142 Resume(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000143
Stephen Wilson84ffe702011-03-30 15:55:52 +0000144 /// Single steps the given thread. If @p signo is anything but
145 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000146 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000147 SingleStep(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000148
149 /// Sends the inferior process a PTRACE_KILL signal. The inferior will
150 /// still exists and can be interrogated. Once resumed it will exit as
151 /// though it received a SIGKILL.
152 bool
153 BringProcessIntoLimbo();
154
Stephen Wilson84ffe702011-03-30 15:55:52 +0000155 bool
156 Detach();
157
158
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000159private:
160 ProcessLinux *m_process;
161
162 lldb::thread_t m_operation_thread;
163 lldb::pid_t m_pid;
164 int m_terminal_fd;
165
Stephen Wilsonf62308c2011-01-15 00:11:28 +0000166 lldb::thread_t m_monitor_thread;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000167
168 lldb_private::Mutex m_server_mutex;
169 int m_client_fd;
170 int m_server_fd;
171
172 /// @class LauchArgs
173 ///
174 /// @brief Simple structure to pass data to the thread responsible for
175 /// launching a child process.
176 struct LaunchArgs
177 {
178 LaunchArgs(ProcessMonitor *monitor,
179 lldb_private::Module *module,
180 char const **argv,
181 char const **envp,
182 const char *stdin_path,
183 const char *stdout_path,
184 const char *stderr_path);
185
186 ~LaunchArgs();
187
188 ProcessMonitor *m_monitor; // The monitor performing the launch.
189 lldb_private::Module *m_module; // The executable image to launch.
190 char const **m_argv; // Process arguments.
191 char const **m_envp; // Process environment.
192 const char *m_stdin_path; // Redirect stdin or NULL.
193 const char *m_stdout_path; // Redirect stdout or NULL.
194 const char *m_stderr_path; // Redirect stderr or NULL.
195 sem_t m_semaphore; // Posted to once launch complete.
196 lldb_private::Error m_error; // Set if process launch failed.
197 };
198
199 void
200 StartOperationThread(LaunchArgs *args, lldb_private::Error &error);
201
202 void
203 StopOperationThread();
204
205 static void *
206 OperationThread(void *arg);
207
208 static bool
209 Launch(LaunchArgs *args);
210
211 bool
212 EnableIPC();
213
214 static void
Stephen Wilson570243b2011-01-19 01:37:06 +0000215 ServeOperation(LaunchArgs *args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000216
217 static bool
218 DupDescriptor(const char *path, int fd, int flags);
219
220 static bool
221 MonitorCallback(void *callback_baton,
222 lldb::pid_t pid, int signal, int status);
223
224 static ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +0000225 MonitorSIGTRAP(ProcessMonitor *monitor,
226 const struct siginfo *info, lldb::pid_t pid);
227
228 static ProcessMessage
229 MonitorSignal(ProcessMonitor *monitor,
230 const struct siginfo *info, lldb::pid_t pid);
231
232 static ProcessMessage::CrashReason
233 GetCrashReasonForSIGSEGV(const struct siginfo *info);
234
235 static ProcessMessage::CrashReason
236 GetCrashReasonForSIGILL(const struct siginfo *info);
237
238 static ProcessMessage::CrashReason
239 GetCrashReasonForSIGFPE(const struct siginfo *info);
240
241 static ProcessMessage::CrashReason
242 GetCrashReasonForSIGBUS(const struct siginfo *info);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000243
244 void
245 DoOperation(Operation *op);
Stephen Wilson9212d7f2011-01-04 21:40:25 +0000246
247 /// Stops the child monitor thread.
Stephen Wilson84ffe702011-03-30 15:55:52 +0000248 void
249 StopMonitoringChildProcess();
250
251 void
252 StopMonitor();
253
254 void
255 CloseFD(int &fd);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000256};
257
258#endif // #ifndef liblldb_ProcessMonitor_H_