blob: bd3253412a163013f5335f69a3da6763d7d1e330 [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.
Andrew Kaylor6578cb62013-07-09 22:36:48 +000050 ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +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,
Stephen Wilsone6f9f662010-07-24 02:19:04 +000059 lldb_private::Error &error);
60
Andrew Kaylor6578cb62013-07-09 22:36:48 +000061 ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +000062 lldb::pid_t pid,
63 lldb_private::Error &error);
64
Stephen Wilsone6f9f662010-07-24 02:19:04 +000065 ~ProcessMonitor();
66
Andrew Kaylor93132f52013-05-28 23:04:25 +000067 enum ResumeSignals
68 {
69 eResumeSignalNone = 0
70 };
71
Stephen Wilsone6f9f662010-07-24 02:19:04 +000072 /// Provides the process number of debugee.
73 lldb::pid_t
74 GetPID() const { return m_pid; }
75
76 /// Returns the process associated with this ProcessMonitor.
77 ProcessLinux &
78 GetProcess() { return *m_process; }
79
Greg Clayton710dd5a2011-01-08 20:28:42 +000080 /// Returns a file descriptor to the controlling terminal of the inferior
Stephen Wilsone6f9f662010-07-24 02:19:04 +000081 /// process.
82 ///
Greg Clayton710dd5a2011-01-08 20:28:42 +000083 /// Reads from this file descriptor yield both the standard output and
Stephen Wilsone6f9f662010-07-24 02:19:04 +000084 /// standard error of this debugee. Even if stderr and stdout were
85 /// redirected on launch it may still happen that data is available on this
86 /// descriptor (if the inferior process opens /dev/tty, for example).
87 ///
88 /// If this monitor was attached to an existing process this method returns
89 /// -1.
90 int
91 GetTerminalFD() const { return m_terminal_fd; }
92
93 /// Reads @p size bytes from address @vm_adder in the inferior process
94 /// address space.
95 ///
96 /// This method is provided to implement Process::DoReadMemory.
97 size_t
98 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
99 lldb_private::Error &error);
100
101 /// Writes @p size bytes from address @p vm_adder in the inferior process
102 /// address space.
103 ///
104 /// This method is provided to implement Process::DoWriteMemory.
105 size_t
106 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
107 lldb_private::Error &error);
108
109 /// Reads the contents from the register identified by the given (architecture
110 /// dependent) offset.
111 ///
112 /// This method is provided for use by RegisterContextLinux derivatives.
113 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000114 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000115 unsigned size, lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000116
117 /// Writes the given value to the register identified by the given
118 /// (architecture dependent) offset.
119 ///
120 /// This method is provided for use by RegisterContextLinux derivatives.
121 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000122 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000123 const lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000124
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000125 /// Reads all general purpose registers into the specified buffer.
126 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000127 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000128
Matt Kopec58c0b962013-03-20 20:34:35 +0000129 /// Reads generic floating point registers into the specified buffer.
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000130 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000131 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000132
Matt Kopec58c0b962013-03-20 20:34:35 +0000133 /// Reads the specified register set into the specified buffer.
134 /// For instance, the extended floating-point register set.
135 bool
136 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
137
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000138 /// Writes all general purpose registers into the specified buffer.
139 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000140 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000141
Matt Kopec58c0b962013-03-20 20:34:35 +0000142 /// Writes generic floating point registers into the specified buffer.
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000143 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000144 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000145
Matt Kopec58c0b962013-03-20 20:34:35 +0000146 /// Writes the specified register set into the specified buffer.
147 /// For instance, the extended floating-point register set.
148 bool
149 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
150
Richard Mitton0a558352013-10-17 21:14:00 +0000151 /// Reads the value of the thread-specific pointer for a given thread ID.
152 bool
153 ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value);
154
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000155 /// Writes a siginfo_t structure corresponding to the given thread ID to the
156 /// memory region pointed to by @p siginfo.
157 bool
Daniel Maleaa35970a2012-11-23 18:09:58 +0000158 GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000159
160 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
161 /// corresponding to the given thread IDto the memory pointed to by @p
162 /// message.
163 bool
164 GetEventMessage(lldb::tid_t tid, unsigned long *message);
165
Stephen Wilson84ffe702011-03-30 15:55:52 +0000166 /// Resumes the given thread. If @p signo is anything but
167 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000168 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000169 Resume(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000170
Stephen Wilson84ffe702011-03-30 15:55:52 +0000171 /// Single steps the given thread. If @p signo is anything but
172 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000173 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000174 SingleStep(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000175
Ed Maste4e0999b2014-04-01 18:14:06 +0000176 /// Terminate the traced process.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000177 bool
Ed Maste4e0999b2014-04-01 18:14:06 +0000178 Kill();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000179
Greg Clayton743ecf42012-10-16 20:20:18 +0000180 lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +0000181 Detach(lldb::tid_t tid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000182
Andrew Kaylorbc68b432013-07-10 21:57:27 +0000183 /// Stops the monitoring the child process thread.
184 void
185 StopMonitor();
186
Andrew Kaylor93132f52013-05-28 23:04:25 +0000187 /// Stops the requested thread and waits for the stop signal.
188 bool
189 StopThread(lldb::tid_t tid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000190
Andrew Kaylord4d54992013-09-17 00:30:24 +0000191 // Waits for the initial stop message from a new thread.
192 bool
193 WaitForInitialTIDStop(lldb::tid_t tid);
194
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000195private:
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000196 ProcessLinux *m_process;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000197
198 lldb::thread_t m_operation_thread;
Matt Kopec7de48462013-03-06 17:20:48 +0000199 lldb::thread_t m_monitor_thread;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000200 lldb::pid_t m_pid;
201 int m_terminal_fd;
202
Daniel Malea1efb4182013-09-16 23:12:18 +0000203 // current operation which must be executed on the priviliged thread
204 Operation *m_operation;
205 lldb_private::Mutex m_operation_mutex;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000206
Daniel Malea1efb4182013-09-16 23:12:18 +0000207 // semaphores notified when Operation is ready to be processed and when
208 // the operation is complete.
209 sem_t m_operation_pending;
210 sem_t m_operation_done;
211
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000212
Johnny Chen25e68e32011-06-14 19:19:50 +0000213 struct OperationArgs
214 {
215 OperationArgs(ProcessMonitor *monitor);
216
217 ~OperationArgs();
218
219 ProcessMonitor *m_monitor; // The monitor performing the attach.
220 sem_t m_semaphore; // Posted to once operation complete.
221 lldb_private::Error m_error; // Set if process operation failed.
222 };
223
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000224 /// @class LauchArgs
225 ///
226 /// @brief Simple structure to pass data to the thread responsible for
227 /// launching a child process.
Johnny Chen25e68e32011-06-14 19:19:50 +0000228 struct LaunchArgs : OperationArgs
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000229 {
230 LaunchArgs(ProcessMonitor *monitor,
231 lldb_private::Module *module,
232 char const **argv,
233 char const **envp,
234 const char *stdin_path,
235 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000236 const char *stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000237 const char *working_dir,
238 const lldb_private::ProcessLaunchInfo &launch_info);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000239
240 ~LaunchArgs();
241
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000242 lldb_private::Module *m_module; // The executable image to launch.
243 char const **m_argv; // Process arguments.
244 char const **m_envp; // Process environment.
245 const char *m_stdin_path; // Redirect stdin or NULL.
246 const char *m_stdout_path; // Redirect stdout or NULL.
247 const char *m_stderr_path; // Redirect stderr or NULL.
Daniel Malea6217d2a2013-01-08 14:49:22 +0000248 const char *m_working_dir; // Working directory or NULL.
Todd Fiala0bce1b62014-08-17 00:10:50 +0000249 const lldb_private::ProcessLaunchInfo &m_launch_info;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000250 };
251
252 void
Johnny Chen25e68e32011-06-14 19:19:50 +0000253 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000254
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000255 static void *
Johnny Chen25e68e32011-06-14 19:19:50 +0000256 LaunchOpThread(void *arg);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000257
258 static bool
259 Launch(LaunchArgs *args);
260
Johnny Chen25e68e32011-06-14 19:19:50 +0000261 struct AttachArgs : OperationArgs
262 {
263 AttachArgs(ProcessMonitor *monitor,
264 lldb::pid_t pid);
265
266 ~AttachArgs();
267
268 lldb::pid_t m_pid; // pid of the process to be attached.
269 };
270
271 void
272 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
273
Johnny Chen25e68e32011-06-14 19:19:50 +0000274 static void *
275 AttachOpThread(void *args);
276
277 static bool
278 Attach(AttachArgs *args);
279
Matt Kopec085d6ce2013-05-31 22:00:07 +0000280 static bool
281 SetDefaultPtraceOpts(const lldb::pid_t);
282
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000283 static void
Johnny Chen25e68e32011-06-14 19:19:50 +0000284 ServeOperation(OperationArgs *args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000285
286 static bool
287 DupDescriptor(const char *path, int fd, int flags);
288
289 static bool
290 MonitorCallback(void *callback_baton,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +0000291 lldb::pid_t pid, bool exited, int signal, int status);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000292
293 static ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +0000294 MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000295 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000296
297 static ProcessMessage
298 MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000299 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000300
301 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000302 GetCrashReasonForSIGSEGV(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000303
304 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000305 GetCrashReasonForSIGILL(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000306
307 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000308 GetCrashReasonForSIGFPE(const siginfo_t *info);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000309
310 static ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +0000311 GetCrashReasonForSIGBUS(const siginfo_t *info);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000312
313 void
314 DoOperation(Operation *op);
Stephen Wilson9212d7f2011-01-04 21:40:25 +0000315
316 /// Stops the child monitor thread.
Stephen Wilson84ffe702011-03-30 15:55:52 +0000317 void
318 StopMonitoringChildProcess();
319
Greg Clayton743ecf42012-10-16 20:20:18 +0000320 /// Stops the operation thread used to attach/launch a process.
321 void
322 StopOpThread();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000323};
324
325#endif // #ifndef liblldb_ProcessMonitor_H_