blob: 314743b00754190740787a30ff24047b13726714 [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.
Andrew Kaylor6578cb62013-07-09 22:36:48 +000050 ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +000051 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,
Daniel Malea6217d2a2013-01-08 14:49:22 +000057 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +000058 const lldb_private::ProcessLaunchInfo &launch_info,
Johnny Chen9ed5b492012-01-05 21:48:15 +000059 lldb_private::Error &error);
60
Andrew Kaylor6578cb62013-07-09 22:36:48 +000061 ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +000062 lldb::pid_t pid,
63 lldb_private::Error &error);
64
65 ~ProcessMonitor();
66
67 /// Provides the process number of debugee.
68 lldb::pid_t
69 GetPID() const { return m_pid; }
70
71 /// Returns the process associated with this ProcessMonitor.
72 ProcessFreeBSD &
73 GetProcess() { return *m_process; }
74
75 /// Returns a file descriptor to the controlling terminal of the inferior
76 /// process.
77 ///
78 /// Reads from this file descriptor yield both the standard output and
79 /// standard error of this debugee. Even if stderr and stdout were
80 /// redirected on launch it may still happen that data is available on this
81 /// descriptor (if the inferior process opens /dev/tty, for example).
82 ///
83 /// If this monitor was attached to an existing process this method returns
84 /// -1.
85 int
86 GetTerminalFD() const { return m_terminal_fd; }
87
88 /// Reads @p size bytes from address @vm_adder in the inferior process
89 /// address space.
90 ///
91 /// This method is provided to implement Process::DoReadMemory.
92 size_t
93 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
94 lldb_private::Error &error);
95
96 /// Writes @p size bytes from address @p vm_adder in the inferior process
97 /// address space.
98 ///
99 /// This method is provided to implement Process::DoWriteMemory.
100 size_t
101 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
102 lldb_private::Error &error);
103
104 /// Reads the contents from the register identified by the given (architecture
105 /// dependent) offset.
106 ///
107 /// This method is provided for use by RegisterContextFreeBSD derivatives.
108 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000109 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000110 unsigned size, lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000111
112 /// Writes the given value to the register identified by the given
113 /// (architecture dependent) offset.
114 ///
115 /// This method is provided for use by RegisterContextFreeBSD derivatives.
116 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000117 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000118 const lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000119
Ed Mastea4be2c52014-02-19 18:34:06 +0000120 /// Reads the contents from the debug register identified by the given
121 /// (architecture dependent) offset.
122 ///
123 /// This method is provided for use by RegisterContextFreeBSD derivatives.
124 bool
125 ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
126 const char *reg_name, unsigned size,
127 lldb_private::RegisterValue &value);
128
129 /// Writes the given value to the debug register identified by the given
130 /// (architecture dependent) offset.
131 ///
132 /// This method is provided for use by RegisterContextFreeBSD derivatives.
133 bool
134 WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
135 const char *reg_name,
136 const lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000137 /// Reads all general purpose registers into the specified buffer.
138 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000139 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000140
Matt Kopecc6672c82013-03-15 20:00:39 +0000141 /// Reads all floating point registers into the specified buffer.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000142 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000143 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000144
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000145 /// Reads the specified register set into the specified buffer.
146 ///
147 /// This method is provided for use by RegisterContextFreeBSD derivatives.
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000148 bool
149 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
150
Johnny Chen9ed5b492012-01-05 21:48:15 +0000151 /// Writes all general purpose registers into the specified buffer.
152 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000153 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000154
Matt Kopecc6672c82013-03-15 20:00:39 +0000155 /// Writes all floating point registers into the specified buffer.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000156 bool
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000157 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
158
159 /// Writes the specified register set into the specified buffer.
160 ///
161 /// This method is provided for use by RegisterContextFreeBSD derivatives.
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000162 bool
163 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000164
Ed Maste68f51792013-10-18 19:16:44 +0000165 /// Reads the value of the thread-specific pointer for a given thread ID.
166 bool
167 ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value);
168
Ed Maste7fd845c2013-12-09 15:51:17 +0000169 /// Returns current thread IDs in process
170 size_t
171 GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids);
172
Ed Maste819e3992013-07-17 14:02:20 +0000173 /// Writes a ptrace_lwpinfo structure corresponding to the given thread ID
174 /// to the memory region pointed to by @p lwpinfo.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000175 bool
Ed Maste819e3992013-07-17 14:02:20 +0000176 GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &error_no);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000177
Ed Maste7fd845c2013-12-09 15:51:17 +0000178 /// Suspends or unsuspends a thread prior to process resume or step.
179 bool
180 ThreadSuspend(lldb::tid_t tid, bool suspend);
181
Johnny Chen9ed5b492012-01-05 21:48:15 +0000182 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
183 /// corresponding to the given thread IDto the memory pointed to by @p
184 /// message.
185 bool
186 GetEventMessage(lldb::tid_t tid, unsigned long *message);
187
Ed Maste502f9022013-11-25 16:31:23 +0000188 /// Resumes the process. If @p signo is anything but
189 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000190 bool
Ed Maste502f9022013-11-25 16:31:23 +0000191 Resume(lldb::tid_t unused, uint32_t signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000192
Ed Maste502f9022013-11-25 16:31:23 +0000193 /// Single steps the process. If @p signo is anything but
194 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000195 bool
Ed Maste502f9022013-11-25 16:31:23 +0000196 SingleStep(lldb::tid_t unused, uint32_t signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000197
Ed Maste70882932014-04-01 14:30:56 +0000198 /// Terminate the traced process.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000199 bool
Ed Maste70882932014-04-01 14:30:56 +0000200 Kill();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000201
202 lldb_private::Error
Matt Kopecedee1822013-06-03 19:48:53 +0000203 Detach(lldb::tid_t tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000204
Andrew Kaylorbc68b432013-07-10 21:57:27 +0000205 void
206 StopMonitor();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000207
Andrew Kaylord4d54992013-09-17 00:30:24 +0000208 // Waits for the initial stop message from a new thread.
209 bool
210 WaitForInitialTIDStop(lldb::tid_t tid);
211
Johnny Chen9ed5b492012-01-05 21:48:15 +0000212private:
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000213 ProcessFreeBSD *m_process;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000214
215 lldb::thread_t m_operation_thread;
216 lldb::thread_t m_monitor_thread;
217 lldb::pid_t m_pid;
218
Johnny Chen9ed5b492012-01-05 21:48:15 +0000219 int m_terminal_fd;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000220
Ed Maste756e1ff2013-09-18 19:34:08 +0000221 // current operation which must be executed on the privileged thread
222 Operation *m_operation;
223 lldb_private::Mutex m_operation_mutex;
224
225 // semaphores notified when Operation is ready to be processed and when
226 // the operation is complete.
227 sem_t m_operation_pending;
228 sem_t m_operation_done;
229
Johnny Chen9ed5b492012-01-05 21:48:15 +0000230 struct OperationArgs
231 {
232 OperationArgs(ProcessMonitor *monitor);
233
234 ~OperationArgs();
235
236 ProcessMonitor *m_monitor; // The monitor performing the attach.
237 sem_t m_semaphore; // Posted to once operation complete.
238 lldb_private::Error m_error; // Set if process operation failed.
239 };
240
241 /// @class LauchArgs
242 ///
243 /// @brief Simple structure to pass data to the thread responsible for
244 /// launching a child process.
245 struct LaunchArgs : OperationArgs
246 {
247 LaunchArgs(ProcessMonitor *monitor,
248 lldb_private::Module *module,
249 char const **argv,
250 char const **envp,
251 const char *stdin_path,
252 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000253 const char *stderr_path,
254 const char *working_dir);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000255
256 ~LaunchArgs();
257
258 lldb_private::Module *m_module; // The executable image to launch.
259 char const **m_argv; // Process arguments.
260 char const **m_envp; // Process environment.
261 const char *m_stdin_path; // Redirect stdin or NULL.
262 const char *m_stdout_path; // Redirect stdout or NULL.
263 const char *m_stderr_path; // Redirect stderr or NULL.
Daniel Malea6217d2a2013-01-08 14:49:22 +0000264 const char *m_working_dir; // Working directory or NULL.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000265 };
266
267 void
268 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
269
Johnny Chen9ed5b492012-01-05 21:48:15 +0000270 static void *
271 LaunchOpThread(void *arg);
272
273 static bool
274 Launch(LaunchArgs *args);
275
Johnny Chen9ed5b492012-01-05 21:48:15 +0000276 struct AttachArgs : OperationArgs
277 {
278 AttachArgs(ProcessMonitor *monitor,
279 lldb::pid_t pid);
280
281 ~AttachArgs();
282
283 lldb::pid_t m_pid; // pid of the process to be attached.
284 };
285
286 void
287 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
288
Johnny Chen9ed5b492012-01-05 21:48:15 +0000289 static void *
290 AttachOpThread(void *args);
291
292 static bool
293 Attach(AttachArgs *args);
294
295 static void
296 ServeOperation(OperationArgs *args);
297
298 static bool
299 DupDescriptor(const char *path, int fd, int flags);
300
301 static bool
302 MonitorCallback(void *callback_baton,
303 lldb::pid_t pid, bool exited, int signal, int status);
304
305 static ProcessMessage
306 MonitorSIGTRAP(ProcessMonitor *monitor,
307 const siginfo_t *info, lldb::pid_t pid);
308
309 static ProcessMessage
310 MonitorSignal(ProcessMonitor *monitor,
311 const siginfo_t *info, lldb::pid_t pid);
312
313 static ProcessMessage::CrashReason
314 GetCrashReasonForSIGSEGV(const siginfo_t *info);
315
316 static ProcessMessage::CrashReason
317 GetCrashReasonForSIGILL(const siginfo_t *info);
318
319 static ProcessMessage::CrashReason
320 GetCrashReasonForSIGFPE(const siginfo_t *info);
321
322 static ProcessMessage::CrashReason
323 GetCrashReasonForSIGBUS(const siginfo_t *info);
324
325 void
326 DoOperation(Operation *op);
327
328 /// Stops the child monitor thread.
329 void
330 StopMonitoringChildProcess();
331
Ed Mastea02f5532013-07-02 16:45:16 +0000332 /// Stops the operation thread used to attach/launch a process.
333 void
334 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000335};
336
337#endif // #ifndef liblldb_ProcessMonitor_H_