blob: 0543e930d408c8c186f64eaac9001406d8dc4920 [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
Pavel Labath3a2da9e2015-02-06 11:32:52 +000087 /// descriptor (if the inferior process opens /dev/tty, for example). This descriptor is
88 /// closed after a call to StopMonitor().
Stephen Wilsone6f9f662010-07-24 02:19:04 +000089 ///
90 /// If this monitor was attached to an existing process this method returns
91 /// -1.
92 int
93 GetTerminalFD() const { return m_terminal_fd; }
94
95 /// Reads @p size bytes from address @vm_adder in the inferior process
96 /// address space.
97 ///
98 /// This method is provided to implement Process::DoReadMemory.
99 size_t
100 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
101 lldb_private::Error &error);
102
103 /// Writes @p size bytes from address @p vm_adder in the inferior process
104 /// address space.
105 ///
106 /// This method is provided to implement Process::DoWriteMemory.
107 size_t
108 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
109 lldb_private::Error &error);
110
111 /// Reads the contents from the register identified by the given (architecture
112 /// dependent) offset.
113 ///
114 /// This method is provided for use by RegisterContextLinux derivatives.
115 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000116 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000117 unsigned size, lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000118
119 /// Writes the given value to the register identified by the given
120 /// (architecture dependent) offset.
121 ///
122 /// This method is provided for use by RegisterContextLinux derivatives.
123 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000124 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000125 const lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000126
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000127 /// Reads all general purpose registers into the specified buffer.
128 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000129 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000130
Matt Kopec58c0b962013-03-20 20:34:35 +0000131 /// Reads generic floating point registers into the specified buffer.
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000132 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000133 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000134
Matt Kopec58c0b962013-03-20 20:34:35 +0000135 /// Reads the specified register set into the specified buffer.
136 /// For instance, the extended floating-point register set.
137 bool
138 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
139
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000140 /// Writes all general purpose registers into the specified buffer.
141 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000142 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000143
Matt Kopec58c0b962013-03-20 20:34:35 +0000144 /// Writes generic floating point registers into the specified buffer.
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000145 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000146 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000147
Matt Kopec58c0b962013-03-20 20:34:35 +0000148 /// Writes the specified register set into the specified buffer.
149 /// For instance, the extended floating-point register set.
150 bool
151 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
152
Richard Mitton0a558352013-10-17 21:14:00 +0000153 /// Reads the value of the thread-specific pointer for a given thread ID.
154 bool
155 ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value);
156
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000157 /// Writes a siginfo_t structure corresponding to the given thread ID to the
158 /// memory region pointed to by @p siginfo.
159 bool
Daniel Maleaa35970a2012-11-23 18:09:58 +0000160 GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000161
162 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
163 /// corresponding to the given thread IDto the memory pointed to by @p
164 /// message.
165 bool
166 GetEventMessage(lldb::tid_t tid, unsigned long *message);
167
Stephen Wilson84ffe702011-03-30 15:55:52 +0000168 /// Resumes the given thread. If @p signo is anything but
169 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000170 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000171 Resume(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000172
Stephen Wilson84ffe702011-03-30 15:55:52 +0000173 /// Single steps the given thread. If @p signo is anything but
174 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000175 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000176 SingleStep(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000177
Ed Maste4e0999b2014-04-01 18:14:06 +0000178 /// Terminate the traced process.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000179 bool
Ed Maste4e0999b2014-04-01 18:14:06 +0000180 Kill();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000181
Greg Clayton743ecf42012-10-16 20:20:18 +0000182 lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +0000183 Detach(lldb::tid_t tid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000184
Andrew Kaylorbc68b432013-07-10 21:57:27 +0000185 /// Stops the monitoring the child process thread.
186 void
187 StopMonitor();
188
Andrew Kaylor93132f52013-05-28 23:04:25 +0000189 /// Stops the requested thread and waits for the stop signal.
190 bool
191 StopThread(lldb::tid_t tid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000192
Andrew Kaylord4d54992013-09-17 00:30:24 +0000193 // Waits for the initial stop message from a new thread.
194 bool
195 WaitForInitialTIDStop(lldb::tid_t tid);
196
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000197private:
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000198 ProcessLinux *m_process;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000199
Zachary Turner39de3112014-09-09 20:54:56 +0000200 lldb_private::HostThread m_operation_thread;
201 lldb_private::HostThread m_monitor_thread;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000202 lldb::pid_t m_pid;
203 int m_terminal_fd;
204
Daniel Malea1efb4182013-09-16 23:12:18 +0000205 // current operation which must be executed on the priviliged thread
206 Operation *m_operation;
207 lldb_private::Mutex m_operation_mutex;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000208
Daniel Malea1efb4182013-09-16 23:12:18 +0000209 // semaphores notified when Operation is ready to be processed and when
210 // the operation is complete.
211 sem_t m_operation_pending;
212 sem_t m_operation_done;
213
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000214
Johnny Chen25e68e32011-06-14 19:19:50 +0000215 struct OperationArgs
216 {
217 OperationArgs(ProcessMonitor *monitor);
218
219 ~OperationArgs();
220
221 ProcessMonitor *m_monitor; // The monitor performing the attach.
222 sem_t m_semaphore; // Posted to once operation complete.
223 lldb_private::Error m_error; // Set if process operation failed.
224 };
225
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000226 /// @class LauchArgs
227 ///
228 /// @brief Simple structure to pass data to the thread responsible for
229 /// launching a child process.
Johnny Chen25e68e32011-06-14 19:19:50 +0000230 struct LaunchArgs : OperationArgs
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000231 {
232 LaunchArgs(ProcessMonitor *monitor,
233 lldb_private::Module *module,
234 char const **argv,
235 char const **envp,
236 const char *stdin_path,
237 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000238 const char *stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000239 const char *working_dir,
240 const lldb_private::ProcessLaunchInfo &launch_info);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000241
242 ~LaunchArgs();
243
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000244 lldb_private::Module *m_module; // The executable image to launch.
245 char const **m_argv; // Process arguments.
246 char const **m_envp; // Process environment.
247 const char *m_stdin_path; // Redirect stdin or NULL.
248 const char *m_stdout_path; // Redirect stdout or NULL.
249 const char *m_stderr_path; // Redirect stderr or NULL.
Daniel Malea6217d2a2013-01-08 14:49:22 +0000250 const char *m_working_dir; // Working directory or NULL.
Todd Fiala0bce1b62014-08-17 00:10:50 +0000251 const lldb_private::ProcessLaunchInfo &m_launch_info;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000252 };
253
254 void
Johnny Chen25e68e32011-06-14 19:19:50 +0000255 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000256
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000257 static void *
Johnny Chen25e68e32011-06-14 19:19:50 +0000258 LaunchOpThread(void *arg);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000259
260 static bool
261 Launch(LaunchArgs *args);
262
Johnny Chen25e68e32011-06-14 19:19:50 +0000263 struct AttachArgs : OperationArgs
264 {
265 AttachArgs(ProcessMonitor *monitor,
266 lldb::pid_t pid);
267
268 ~AttachArgs();
269
270 lldb::pid_t m_pid; // pid of the process to be attached.
271 };
272
273 void
274 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
275
Johnny Chen25e68e32011-06-14 19:19:50 +0000276 static void *
277 AttachOpThread(void *args);
278
279 static bool
280 Attach(AttachArgs *args);
281
Matt Kopec085d6ce2013-05-31 22:00:07 +0000282 static bool
283 SetDefaultPtraceOpts(const lldb::pid_t);
284
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000285 static void
Johnny Chen25e68e32011-06-14 19:19:50 +0000286 ServeOperation(OperationArgs *args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000287
288 static bool
289 DupDescriptor(const char *path, int fd, int flags);
290
291 static bool
292 MonitorCallback(void *callback_baton,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +0000293 lldb::pid_t pid, bool exited, int signal, int status);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000294
295 static ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +0000296 MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000297 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000298
299 static ProcessMessage
300 MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000301 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000302
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000303 void
304 DoOperation(Operation *op);
Stephen Wilson9212d7f2011-01-04 21:40:25 +0000305
306 /// Stops the child monitor thread.
Stephen Wilson84ffe702011-03-30 15:55:52 +0000307 void
308 StopMonitoringChildProcess();
309
Greg Clayton743ecf42012-10-16 20:20:18 +0000310 /// Stops the operation thread used to attach/launch a process.
311 void
312 StopOpThread();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000313};
314
315#endif // #ifndef liblldb_ProcessMonitor_H_