blob: 0f319348b462e0f9b3e3e2ff474adce60d883763 [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"
Zachary Turner39de3112014-09-09 20:54:56 +000020#include "lldb/Host/HostThread.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000021#include "lldb/Host/Mutex.h"
22
23namespace lldb_private
24{
25class Error;
26class Module;
27class Scalar;
28} // End lldb_private namespace.
29
30class ProcessFreeBSD;
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/// ProcessFreeBSD instance by calling ProcessFreeBSD::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,
Johnny Chen9ed5b492012-01-05 21:48:15 +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,
Johnny Chen9ed5b492012-01-05 21:48:15 +000060 lldb_private::Error &error);
61
Andrew Kaylor6578cb62013-07-09 22:36:48 +000062 ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +000063 lldb::pid_t pid,
64 lldb_private::Error &error);
65
66 ~ProcessMonitor();
67
68 /// Provides the process number of debugee.
69 lldb::pid_t
70 GetPID() const { return m_pid; }
71
72 /// Returns the process associated with this ProcessMonitor.
73 ProcessFreeBSD &
74 GetProcess() { return *m_process; }
75
76 /// Returns a file descriptor to the controlling terminal of the inferior
77 /// process.
78 ///
79 /// Reads from this file descriptor yield both the standard output and
80 /// standard error of this debugee. Even if stderr and stdout were
81 /// redirected on launch it may still happen that data is available on this
82 /// descriptor (if the inferior process opens /dev/tty, for example).
83 ///
84 /// If this monitor was attached to an existing process this method returns
85 /// -1.
86 int
87 GetTerminalFD() const { return m_terminal_fd; }
88
89 /// Reads @p size bytes from address @vm_adder in the inferior process
90 /// address space.
91 ///
92 /// This method is provided to implement Process::DoReadMemory.
93 size_t
94 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
95 lldb_private::Error &error);
96
97 /// Writes @p size bytes from address @p vm_adder in the inferior process
98 /// address space.
99 ///
100 /// This method is provided to implement Process::DoWriteMemory.
101 size_t
102 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
103 lldb_private::Error &error);
104
105 /// Reads the contents from the register identified by the given (architecture
106 /// dependent) offset.
107 ///
108 /// This method is provided for use by RegisterContextFreeBSD derivatives.
109 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000110 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000111 unsigned size, lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000112
113 /// Writes the given value to the register identified by the given
114 /// (architecture dependent) offset.
115 ///
116 /// This method is provided for use by RegisterContextFreeBSD derivatives.
117 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000118 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000119 const lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000120
Ed Mastea4be2c52014-02-19 18:34:06 +0000121 /// Reads the contents from the debug register identified by the given
122 /// (architecture dependent) offset.
123 ///
124 /// This method is provided for use by RegisterContextFreeBSD derivatives.
125 bool
126 ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
127 const char *reg_name, unsigned size,
128 lldb_private::RegisterValue &value);
129
130 /// Writes the given value to the debug register identified by the given
131 /// (architecture dependent) offset.
132 ///
133 /// This method is provided for use by RegisterContextFreeBSD derivatives.
134 bool
135 WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
136 const char *reg_name,
137 const lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000138 /// Reads all general purpose registers into the specified buffer.
139 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000140 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000141
Matt Kopecc6672c82013-03-15 20:00:39 +0000142 /// Reads all floating point registers into the specified buffer.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000143 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000144 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000145
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000146 /// Reads the specified register set into the specified buffer.
147 ///
148 /// This method is provided for use by RegisterContextFreeBSD derivatives.
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000149 bool
150 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
151
Johnny Chen9ed5b492012-01-05 21:48:15 +0000152 /// Writes all general purpose registers into the specified buffer.
153 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000154 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000155
Matt Kopecc6672c82013-03-15 20:00:39 +0000156 /// Writes all floating point registers into the specified buffer.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000157 bool
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000158 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
159
160 /// Writes the specified register set into the specified buffer.
161 ///
162 /// This method is provided for use by RegisterContextFreeBSD derivatives.
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000163 bool
164 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000165
Ed Maste68f51792013-10-18 19:16:44 +0000166 /// Reads the value of the thread-specific pointer for a given thread ID.
167 bool
168 ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value);
169
Ed Maste7fd845c2013-12-09 15:51:17 +0000170 /// Returns current thread IDs in process
171 size_t
172 GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids);
173
Ed Maste819e3992013-07-17 14:02:20 +0000174 /// Writes a ptrace_lwpinfo structure corresponding to the given thread ID
175 /// to the memory region pointed to by @p lwpinfo.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000176 bool
Ed Maste819e3992013-07-17 14:02:20 +0000177 GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &error_no);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000178
Ed Maste7fd845c2013-12-09 15:51:17 +0000179 /// Suspends or unsuspends a thread prior to process resume or step.
180 bool
181 ThreadSuspend(lldb::tid_t tid, bool suspend);
182
Johnny Chen9ed5b492012-01-05 21:48:15 +0000183 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
184 /// corresponding to the given thread IDto the memory pointed to by @p
185 /// message.
186 bool
187 GetEventMessage(lldb::tid_t tid, unsigned long *message);
188
Ed Maste502f9022013-11-25 16:31:23 +0000189 /// Resumes the process. If @p signo is anything but
190 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000191 bool
Ed Maste502f9022013-11-25 16:31:23 +0000192 Resume(lldb::tid_t unused, uint32_t signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000193
Ed Maste502f9022013-11-25 16:31:23 +0000194 /// Single steps the process. If @p signo is anything but
195 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000196 bool
Ed Maste502f9022013-11-25 16:31:23 +0000197 SingleStep(lldb::tid_t unused, uint32_t signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000198
Ed Maste70882932014-04-01 14:30:56 +0000199 /// Terminate the traced process.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000200 bool
Ed Maste70882932014-04-01 14:30:56 +0000201 Kill();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000202
203 lldb_private::Error
Matt Kopecedee1822013-06-03 19:48:53 +0000204 Detach(lldb::tid_t tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000205
Andrew Kaylorbc68b432013-07-10 21:57:27 +0000206 void
207 StopMonitor();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000208
Andrew Kaylord4d54992013-09-17 00:30:24 +0000209 // Waits for the initial stop message from a new thread.
210 bool
211 WaitForInitialTIDStop(lldb::tid_t tid);
212
Johnny Chen9ed5b492012-01-05 21:48:15 +0000213private:
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000214 ProcessFreeBSD *m_process;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000215
Zachary Turner39de3112014-09-09 20:54:56 +0000216 HostThread m_operation_thread;
217 HostThread m_monitor_thread;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000218 lldb::pid_t m_pid;
219
Johnny Chen9ed5b492012-01-05 21:48:15 +0000220 int m_terminal_fd;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000221
Ed Maste756e1ff2013-09-18 19:34:08 +0000222 // current operation which must be executed on the privileged thread
223 Operation *m_operation;
224 lldb_private::Mutex m_operation_mutex;
225
226 // semaphores notified when Operation is ready to be processed and when
227 // the operation is complete.
228 sem_t m_operation_pending;
229 sem_t m_operation_done;
230
Johnny Chen9ed5b492012-01-05 21:48:15 +0000231 struct OperationArgs
232 {
233 OperationArgs(ProcessMonitor *monitor);
234
235 ~OperationArgs();
236
237 ProcessMonitor *m_monitor; // The monitor performing the attach.
238 sem_t m_semaphore; // Posted to once operation complete.
239 lldb_private::Error m_error; // Set if process operation failed.
240 };
241
242 /// @class LauchArgs
243 ///
244 /// @brief Simple structure to pass data to the thread responsible for
245 /// launching a child process.
246 struct LaunchArgs : OperationArgs
247 {
248 LaunchArgs(ProcessMonitor *monitor,
249 lldb_private::Module *module,
250 char const **argv,
251 char const **envp,
252 const char *stdin_path,
253 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000254 const char *stderr_path,
255 const char *working_dir);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000256
257 ~LaunchArgs();
258
259 lldb_private::Module *m_module; // The executable image to launch.
260 char const **m_argv; // Process arguments.
261 char const **m_envp; // Process environment.
262 const char *m_stdin_path; // Redirect stdin or NULL.
263 const char *m_stdout_path; // Redirect stdout or NULL.
264 const char *m_stderr_path; // Redirect stderr or NULL.
Daniel Malea6217d2a2013-01-08 14:49:22 +0000265 const char *m_working_dir; // Working directory or NULL.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000266 };
267
268 void
269 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
270
Johnny Chen9ed5b492012-01-05 21:48:15 +0000271 static void *
272 LaunchOpThread(void *arg);
273
274 static bool
275 Launch(LaunchArgs *args);
276
Johnny Chen9ed5b492012-01-05 21:48:15 +0000277 struct AttachArgs : OperationArgs
278 {
279 AttachArgs(ProcessMonitor *monitor,
280 lldb::pid_t pid);
281
282 ~AttachArgs();
283
284 lldb::pid_t m_pid; // pid of the process to be attached.
285 };
286
287 void
288 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
289
Johnny Chen9ed5b492012-01-05 21:48:15 +0000290 static void *
291 AttachOpThread(void *args);
292
293 static bool
294 Attach(AttachArgs *args);
295
296 static void
297 ServeOperation(OperationArgs *args);
298
299 static bool
300 DupDescriptor(const char *path, int fd, int flags);
301
302 static bool
303 MonitorCallback(void *callback_baton,
304 lldb::pid_t pid, bool exited, int signal, int status);
305
306 static ProcessMessage
307 MonitorSIGTRAP(ProcessMonitor *monitor,
308 const siginfo_t *info, lldb::pid_t pid);
309
310 static ProcessMessage
311 MonitorSignal(ProcessMonitor *monitor,
312 const siginfo_t *info, lldb::pid_t pid);
313
314 static ProcessMessage::CrashReason
315 GetCrashReasonForSIGSEGV(const siginfo_t *info);
316
317 static ProcessMessage::CrashReason
318 GetCrashReasonForSIGILL(const siginfo_t *info);
319
320 static ProcessMessage::CrashReason
321 GetCrashReasonForSIGFPE(const siginfo_t *info);
322
323 static ProcessMessage::CrashReason
324 GetCrashReasonForSIGBUS(const siginfo_t *info);
325
326 void
327 DoOperation(Operation *op);
328
329 /// Stops the child monitor thread.
330 void
331 StopMonitoringChildProcess();
332
Ed Mastea02f5532013-07-02 16:45:16 +0000333 /// Stops the operation thread used to attach/launch a process.
334 void
335 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000336};
337
338#endif // #ifndef liblldb_ProcessMonitor_H_