blob: a399856bde3ead6ebcbd1d0b7586e9b5068390d6 [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
Tamas Berghammerdb264a62015-03-31 09:52:22 +000023namespace lldb_private {
24
Stephen Wilsone6f9f662010-07-24 02:19:04 +000025class Error;
26class Module;
27class Scalar;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000028
29namespace process_linux {
Stephen Wilsone6f9f662010-07-24 02:19:04 +000030
31class ProcessLinux;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000032
33} // namespace process_linux
34
35} // namespace lldb_private
36
Stephen Wilsone6f9f662010-07-24 02:19:04 +000037class Operation;
38
39/// @class ProcessMonitor
40/// @brief Manages communication with the inferior (debugee) process.
41///
42/// Upon construction, this class prepares and launches an inferior process for
43/// debugging.
44///
45/// Changes in the inferior process state are propagated to the associated
46/// ProcessLinux instance by calling ProcessLinux::SendMessage with the
47/// appropriate ProcessMessage events.
48///
49/// A purposely minimal set of operations are provided to interrogate and change
50/// the inferior process state.
51class ProcessMonitor
52{
53public:
54
55 /// Launches an inferior process ready for debugging. Forms the
56 /// implementation of Process::DoLaunch.
Andrew Kaylor6578cb62013-07-09 22:36:48 +000057 ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +000058 lldb_private::Module *module,
59 char const *argv[],
60 char const *envp[],
61 const char *stdin_path,
62 const char *stdout_path,
63 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +000064 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +000065 const lldb_private::ProcessLaunchInfo &launch_info,
Stephen Wilsone6f9f662010-07-24 02:19:04 +000066 lldb_private::Error &error);
67
Andrew Kaylor6578cb62013-07-09 22:36:48 +000068 ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +000069 lldb::pid_t pid,
70 lldb_private::Error &error);
71
Stephen Wilsone6f9f662010-07-24 02:19:04 +000072 ~ProcessMonitor();
73
Andrew Kaylor93132f52013-05-28 23:04:25 +000074 enum ResumeSignals
75 {
76 eResumeSignalNone = 0
77 };
78
Stephen Wilsone6f9f662010-07-24 02:19:04 +000079 /// Provides the process number of debugee.
80 lldb::pid_t
81 GetPID() const { return m_pid; }
82
83 /// Returns the process associated with this ProcessMonitor.
Tamas Berghammerdb264a62015-03-31 09:52:22 +000084 lldb_private::process_linux::ProcessLinux &
Stephen Wilsone6f9f662010-07-24 02:19:04 +000085 GetProcess() { return *m_process; }
86
Greg Clayton710dd5a2011-01-08 20:28:42 +000087 /// Returns a file descriptor to the controlling terminal of the inferior
Stephen Wilsone6f9f662010-07-24 02:19:04 +000088 /// process.
89 ///
Greg Clayton710dd5a2011-01-08 20:28:42 +000090 /// Reads from this file descriptor yield both the standard output and
Stephen Wilsone6f9f662010-07-24 02:19:04 +000091 /// standard error of this debugee. Even if stderr and stdout were
92 /// redirected on launch it may still happen that data is available on this
Pavel Labath3a2da9e2015-02-06 11:32:52 +000093 /// descriptor (if the inferior process opens /dev/tty, for example). This descriptor is
94 /// closed after a call to StopMonitor().
Stephen Wilsone6f9f662010-07-24 02:19:04 +000095 ///
96 /// If this monitor was attached to an existing process this method returns
97 /// -1.
98 int
99 GetTerminalFD() const { return m_terminal_fd; }
100
101 /// Reads @p size bytes from address @vm_adder in the inferior process
102 /// address space.
103 ///
104 /// This method is provided to implement Process::DoReadMemory.
105 size_t
106 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
107 lldb_private::Error &error);
108
109 /// Writes @p size bytes from address @p vm_adder in the inferior process
110 /// address space.
111 ///
112 /// This method is provided to implement Process::DoWriteMemory.
113 size_t
114 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
115 lldb_private::Error &error);
116
117 /// Reads the contents from the register identified by the given (architecture
118 /// dependent) offset.
119 ///
120 /// This method is provided for use by RegisterContextLinux derivatives.
121 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000122 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000123 unsigned size, lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000124
125 /// Writes the given value to the register identified by the given
126 /// (architecture dependent) offset.
127 ///
128 /// This method is provided for use by RegisterContextLinux derivatives.
129 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000130 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000131 const lldb_private::RegisterValue &value);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000132
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000133 /// Reads all general purpose registers into the specified buffer.
134 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000135 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000136
Matt Kopec58c0b962013-03-20 20:34:35 +0000137 /// Reads generic floating point registers into the specified buffer.
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000138 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000139 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000140
Omair Javaidea8c25a802015-05-15 06:29:58 +0000141#if defined (__arm64__) || defined (__aarch64__)
142 /// Reads hardware breakpoints and watchpoints capability information.
143 bool
144 ReadHardwareDebugInfo (lldb::tid_t tid, unsigned int &watch_count ,
145 unsigned int &break_count);
146
147 /// Write hardware breakpoint/watchpoint control and address registers.
148 bool
149 WriteHardwareDebugRegs (lldb::tid_t tid, lldb::addr_t *addr_buf,
150 uint32_t *cntrl_buf, int type, int count);
151#endif
152
Matt Kopec58c0b962013-03-20 20:34:35 +0000153 /// Reads the specified register set into the specified buffer.
154 /// For instance, the extended floating-point register set.
155 bool
156 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
157
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000158 /// Writes all general purpose registers into the specified buffer.
159 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000160 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000161
Matt Kopec58c0b962013-03-20 20:34:35 +0000162 /// Writes generic floating point registers into the specified buffer.
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000163 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000164 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000165
Matt Kopec58c0b962013-03-20 20:34:35 +0000166 /// Writes the specified register set into the specified buffer.
167 /// For instance, the extended floating-point register set.
168 bool
169 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
170
Richard Mitton0a558352013-10-17 21:14:00 +0000171 /// Reads the value of the thread-specific pointer for a given thread ID.
172 bool
173 ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value);
174
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000175 /// Writes a siginfo_t structure corresponding to the given thread ID to the
176 /// memory region pointed to by @p siginfo.
177 bool
Daniel Maleaa35970a2012-11-23 18:09:58 +0000178 GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000179
180 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
181 /// corresponding to the given thread IDto the memory pointed to by @p
182 /// message.
183 bool
184 GetEventMessage(lldb::tid_t tid, unsigned long *message);
185
Stephen Wilson84ffe702011-03-30 15:55:52 +0000186 /// Resumes the given thread. If @p signo is anything but
187 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000188 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000189 Resume(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000190
Stephen Wilson84ffe702011-03-30 15:55:52 +0000191 /// Single steps the given thread. If @p signo is anything but
192 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000193 bool
Stephen Wilson84ffe702011-03-30 15:55:52 +0000194 SingleStep(lldb::tid_t tid, uint32_t signo);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000195
Ed Maste4e0999b2014-04-01 18:14:06 +0000196 /// Terminate the traced process.
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000197 bool
Ed Maste4e0999b2014-04-01 18:14:06 +0000198 Kill();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000199
Greg Clayton743ecf42012-10-16 20:20:18 +0000200 lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +0000201 Detach(lldb::tid_t tid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000202
Andrew Kaylorbc68b432013-07-10 21:57:27 +0000203 /// Stops the monitoring the child process thread.
204 void
205 StopMonitor();
206
Andrew Kaylor93132f52013-05-28 23:04:25 +0000207 /// Stops the requested thread and waits for the stop signal.
208 bool
209 StopThread(lldb::tid_t tid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000210
Andrew Kaylord4d54992013-09-17 00:30:24 +0000211 // Waits for the initial stop message from a new thread.
212 bool
213 WaitForInitialTIDStop(lldb::tid_t tid);
214
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000215private:
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000216 lldb_private::process_linux::ProcessLinux *m_process;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000217
Zachary Turner39de3112014-09-09 20:54:56 +0000218 lldb_private::HostThread m_operation_thread;
219 lldb_private::HostThread m_monitor_thread;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000220 lldb::pid_t m_pid;
221 int m_terminal_fd;
222
Daniel Malea1efb4182013-09-16 23:12:18 +0000223 // current operation which must be executed on the priviliged thread
224 Operation *m_operation;
225 lldb_private::Mutex m_operation_mutex;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000226
Daniel Malea1efb4182013-09-16 23:12:18 +0000227 // semaphores notified when Operation is ready to be processed and when
228 // the operation is complete.
229 sem_t m_operation_pending;
230 sem_t m_operation_done;
231
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000232
Johnny Chen25e68e32011-06-14 19:19:50 +0000233 struct OperationArgs
234 {
235 OperationArgs(ProcessMonitor *monitor);
236
237 ~OperationArgs();
238
239 ProcessMonitor *m_monitor; // The monitor performing the attach.
240 sem_t m_semaphore; // Posted to once operation complete.
241 lldb_private::Error m_error; // Set if process operation failed.
242 };
243
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000244 /// @class LauchArgs
245 ///
246 /// @brief Simple structure to pass data to the thread responsible for
247 /// launching a child process.
Johnny Chen25e68e32011-06-14 19:19:50 +0000248 struct LaunchArgs : OperationArgs
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000249 {
250 LaunchArgs(ProcessMonitor *monitor,
251 lldb_private::Module *module,
252 char const **argv,
253 char const **envp,
254 const char *stdin_path,
255 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000256 const char *stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000257 const char *working_dir,
258 const lldb_private::ProcessLaunchInfo &launch_info);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000259
260 ~LaunchArgs();
261
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000262 lldb_private::Module *m_module; // The executable image to launch.
263 char const **m_argv; // Process arguments.
264 char const **m_envp; // Process environment.
265 const char *m_stdin_path; // Redirect stdin or NULL.
266 const char *m_stdout_path; // Redirect stdout or NULL.
267 const char *m_stderr_path; // Redirect stderr or NULL.
Daniel Malea6217d2a2013-01-08 14:49:22 +0000268 const char *m_working_dir; // Working directory or NULL.
Todd Fiala0bce1b62014-08-17 00:10:50 +0000269 const lldb_private::ProcessLaunchInfo &m_launch_info;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000270 };
271
272 void
Johnny Chen25e68e32011-06-14 19:19:50 +0000273 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000274
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000275 static void *
Johnny Chen25e68e32011-06-14 19:19:50 +0000276 LaunchOpThread(void *arg);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000277
278 static bool
279 Launch(LaunchArgs *args);
280
Johnny Chen25e68e32011-06-14 19:19:50 +0000281 struct AttachArgs : OperationArgs
282 {
283 AttachArgs(ProcessMonitor *monitor,
284 lldb::pid_t pid);
285
286 ~AttachArgs();
287
288 lldb::pid_t m_pid; // pid of the process to be attached.
289 };
290
291 void
292 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
293
Johnny Chen25e68e32011-06-14 19:19:50 +0000294 static void *
295 AttachOpThread(void *args);
296
297 static bool
298 Attach(AttachArgs *args);
299
Matt Kopec085d6ce2013-05-31 22:00:07 +0000300 static bool
301 SetDefaultPtraceOpts(const lldb::pid_t);
302
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000303 static void
Johnny Chen25e68e32011-06-14 19:19:50 +0000304 ServeOperation(OperationArgs *args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000305
306 static bool
307 DupDescriptor(const char *path, int fd, int flags);
308
309 static bool
310 MonitorCallback(void *callback_baton,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +0000311 lldb::pid_t pid, bool exited, int signal, int status);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000312
313 static ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +0000314 MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000315 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000316
317 static ProcessMessage
318 MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +0000319 const siginfo_t *info, lldb::pid_t pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +0000320
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000321 void
322 DoOperation(Operation *op);
Stephen Wilson9212d7f2011-01-04 21:40:25 +0000323
324 /// Stops the child monitor thread.
Stephen Wilson84ffe702011-03-30 15:55:52 +0000325 void
326 StopMonitoringChildProcess();
327
Greg Clayton743ecf42012-10-16 20:20:18 +0000328 /// Stops the operation thread used to attach/launch a process.
329 void
330 StopOpThread();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000331};
332
333#endif // #ifndef liblldb_ProcessMonitor_H_