blob: 27beaef960671e2673bd2c5880288c9d67e3216b [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"
Zachary Turner39de3112014-09-09 20:54:56 +000020#include "lldb/Host/HostThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000021#include "lldb/Host/Mutex.h"
22
23namespace lldb_private
24{
25class Error;
26class Module;
27class Scalar;
28} // End lldb_private namespace.
29
30class ProcessLinux;
31class Operation;
32
33/// @class ProcessMonitor
34/// @brief Manages communication with the inferior (debugee) process.
35///
36/// Upon construction, this class prepares and launches an inferior process for
37/// debugging.
38///
39/// Changes in the inferior process state are propagated to the associated
40/// ProcessLinux instance by calling ProcessLinux::SendMessage with the
41/// appropriate ProcessMessage events.
42///
43/// A purposely minimal set of operations are provided to interrogate and change
44/// the inferior process state.
45class ProcessMonitor
46{
47public:
48
49 /// Launches an inferior process ready for debugging. Forms the
50 /// implementation of Process::DoLaunch.
Andrew Kaylor6578cb62013-07-09 22:36:48 +000051 ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +000052 lldb_private::Module *module,
53 char const *argv[],
54 char const *envp[],
55 const char *stdin_path,
56 const char *stdout_path,
57 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +000058 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +000059 const lldb_private::ProcessLaunchInfo &launch_info,
Stephen Wilsone6f9f662010-07-24 02:19:04 +000060 lldb_private::Error &error);
61
Andrew Kaylor6578cb62013-07-09 22:36:48 +000062 ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +000063 lldb::pid_t pid,
64 lldb_private::Error &error);
65
Stephen Wilsone6f9f662010-07-24 02:19:04 +000066 ~ProcessMonitor();
67
Andrew Kaylor93132f52013-05-28 23:04:25 +000068 enum ResumeSignals
69 {
70 eResumeSignalNone = 0
71 };
72
Stephen Wilsone6f9f662010-07-24 02:19:04 +000073 /// Provides the process number of debugee.
74 lldb::pid_t
75 GetPID() const { return m_pid; }
76
77 /// Returns the process associated with this ProcessMonitor.
78 ProcessLinux &
79 GetProcess() { return *m_process; }
80
Greg Clayton710dd5a2011-01-08 20:28:42 +000081 /// Returns a file descriptor to the controlling terminal of the inferior
Stephen Wilsone6f9f662010-07-24 02:19:04 +000082 /// process.
83 ///
Greg Clayton710dd5a2011-01-08 20:28:42 +000084 /// Reads from this file descriptor yield both the standard output and
Stephen Wilsone6f9f662010-07-24 02:19:04 +000085 /// standard error of this debugee. Even if stderr and stdout were
86 /// redirected on launch it may still happen that data is available on this
87 /// descriptor (if the inferior process opens /dev/tty, for example).
88 ///
89 /// If this monitor was attached to an existing process this method returns
90 /// -1.
91 int
92 GetTerminalFD() const { return m_terminal_fd; }
93
94 /// Reads @p size bytes from address @vm_adder in the inferior process
95 /// address space.
96 ///
97 /// This method is provided to implement Process::DoReadMemory.
98 size_t
99 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
100 lldb_private::Error &error);
101
102 /// Writes @p size bytes from address @p vm_adder in the inferior process
103 /// address space.
104 ///
105 /// This method is provided to implement Process::DoWriteMemory.
106 size_t
107 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
108 lldb_private::Error &error);
109
110 /// Reads the contents from the register identified by the given (architecture
111 /// dependent) offset.
112 ///
113 /// This method is provided for use by RegisterContextLinux derivatives.
114 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000115 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000116 unsigned size, lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000117
118 /// Writes the given value to the register identified by the given
119 /// (architecture dependent) offset.
120 ///
121 /// This method is provided for use by RegisterContextLinux derivatives.
122 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000123 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000124 const lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000125
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000126 /// Reads all general purpose registers into the specified buffer.
127 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000128 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000129
Matt Kopec58c0b962013-03-20 20:34:35 +0000130 /// Reads generic floating point registers into the specified buffer.
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000131 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000132 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000133
Matt Kopec58c0b962013-03-20 20:34:35 +0000134 /// Reads the specified register set into the specified buffer.
135 /// For instance, the extended floating-point register set.
136 bool
137 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
138
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000139 /// Writes all general purpose registers into the specified buffer.
140 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000141 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000142
Matt Kopec58c0b962013-03-20 20:34:35 +0000143 /// Writes generic floating point registers into the specified buffer.
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000144 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000145 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000146
Matt Kopec58c0b962013-03-20 20:34:35 +0000147 /// Writes the specified register set into the specified buffer.
148 /// For instance, the extended floating-point register set.
149 bool
150 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
151
Richard Mitton0a558352013-10-17 21:14:00 +0000152 /// Reads the value of the thread-specific pointer for a given thread ID.
153 bool
154 ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value);
155
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000156 /// Writes a siginfo_t structure corresponding to the given thread ID to the
157 /// memory region pointed to by @p siginfo.
158 bool
Daniel Maleaa35970a2012-11-23 18:09:58 +0000159 GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000160
161 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
162 /// corresponding to the given thread IDto the memory pointed to by @p
163 /// message.
164 bool
165 GetEventMessage(lldb::tid_t tid, unsigned long *message);
166
Stephen Wilson84ffe702011-03-30 15:55:52 +0000167 /// Resumes the given thread. If @p signo is anything but
168 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000169 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000170 Resume(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000171
Stephen Wilson84ffe702011-03-30 15:55:52 +0000172 /// Single steps the given thread. If @p signo is anything but
173 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000174 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000175 SingleStep(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000176
Ed Maste4e0999b2014-04-01 18:14:06 +0000177 /// Terminate the traced process.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000178 bool
Ed Maste4e0999b2014-04-01 18:14:06 +0000179 Kill();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000180
Greg Clayton743ecf42012-10-16 20:20:18 +0000181 lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +0000182 Detach(lldb::tid_t tid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000183
Andrew Kaylorbc68b432013-07-10 21:57:27 +0000184 /// Stops the monitoring the child process thread.
185 void
186 StopMonitor();
187
Andrew Kaylor93132f52013-05-28 23:04:25 +0000188 /// Stops the requested thread and waits for the stop signal.
189 bool
190 StopThread(lldb::tid_t tid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000191
Andrew Kaylord4d54992013-09-17 00:30:24 +0000192 // Waits for the initial stop message from a new thread.
193 bool
194 WaitForInitialTIDStop(lldb::tid_t tid);
195
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000196private:
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000197 ProcessLinux *m_process;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000198
Zachary Turner39de3112014-09-09 20:54:56 +0000199 lldb_private::HostThread m_operation_thread;
200 lldb_private::HostThread m_monitor_thread;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000201 lldb::pid_t m_pid;
202 int m_terminal_fd;
203
Daniel Malea1efb4182013-09-16 23:12:18 +0000204 // current operation which must be executed on the priviliged thread
205 Operation *m_operation;
206 lldb_private::Mutex m_operation_mutex;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000207
Daniel Malea1efb4182013-09-16 23:12:18 +0000208 // semaphores notified when Operation is ready to be processed and when
209 // the operation is complete.
210 sem_t m_operation_pending;
211 sem_t m_operation_done;
212
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000213
Johnny Chen25e68e32011-06-14 19:19:50 +0000214 struct OperationArgs
215 {
216 OperationArgs(ProcessMonitor *monitor);
217
218 ~OperationArgs();
219
220 ProcessMonitor *m_monitor; // The monitor performing the attach.
221 sem_t m_semaphore; // Posted to once operation complete.
222 lldb_private::Error m_error; // Set if process operation failed.
223 };
224
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000225 /// @class LauchArgs
226 ///
227 /// @brief Simple structure to pass data to the thread responsible for
228 /// launching a child process.
Johnny Chen25e68e32011-06-14 19:19:50 +0000229 struct LaunchArgs : OperationArgs
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000230 {
231 LaunchArgs(ProcessMonitor *monitor,
232 lldb_private::Module *module,
233 char const **argv,
234 char const **envp,
235 const char *stdin_path,
236 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000237 const char *stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000238 const char *working_dir,
239 const lldb_private::ProcessLaunchInfo &launch_info);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000240
241 ~LaunchArgs();
242
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000243 lldb_private::Module *m_module; // The executable image to launch.
244 char const **m_argv; // Process arguments.
245 char const **m_envp; // Process environment.
246 const char *m_stdin_path; // Redirect stdin or NULL.
247 const char *m_stdout_path; // Redirect stdout or NULL.
248 const char *m_stderr_path; // Redirect stderr or NULL.
Daniel Malea6217d2a2013-01-08 14:49:22 +0000249 const char *m_working_dir; // Working directory or NULL.
Todd Fiala0bce1b62014-08-17 00:10:50 +0000250 const lldb_private::ProcessLaunchInfo &m_launch_info;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000251 };
252
253 void
Johnny Chen25e68e32011-06-14 19:19:50 +0000254 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000255
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000256 static void *
Johnny Chen25e68e32011-06-14 19:19:50 +0000257 LaunchOpThread(void *arg);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000258
259 static bool
260 Launch(LaunchArgs *args);
261
Johnny Chen25e68e32011-06-14 19:19:50 +0000262 struct AttachArgs : OperationArgs
263 {
264 AttachArgs(ProcessMonitor *monitor,
265 lldb::pid_t pid);
266
267 ~AttachArgs();
268
269 lldb::pid_t m_pid; // pid of the process to be attached.
270 };
271
272 void
273 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
274
Johnny Chen25e68e32011-06-14 19:19:50 +0000275 static void *
276 AttachOpThread(void *args);
277
278 static bool
279 Attach(AttachArgs *args);
280
Matt Kopec085d6ce2013-05-31 22:00:07 +0000281 static bool
282 SetDefaultPtraceOpts(const lldb::pid_t);
283
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000284 static void
Johnny Chen25e68e32011-06-14 19:19:50 +0000285 ServeOperation(OperationArgs *args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000286
287 static bool
288 DupDescriptor(const char *path, int fd, int flags);
289
290 static bool
291 MonitorCallback(void *callback_baton,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +0000292 lldb::pid_t pid, bool exited, int signal, int status);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000293
294 static ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +0000295 MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000296 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000297
298 static ProcessMessage
299 MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000300 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000301
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000302 void
303 DoOperation(Operation *op);
Stephen Wilson9212d7f2011-01-04 21:40:25 +0000304
305 /// Stops the child monitor thread.
Stephen Wilson84ffe702011-03-30 15:55:52 +0000306 void
307 StopMonitoringChildProcess();
308
Greg Clayton743ecf42012-10-16 20:20:18 +0000309 /// Stops the operation thread used to attach/launch a process.
310 void
311 StopOpThread();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000312};
313
314#endif // #ifndef liblldb_ProcessMonitor_H_