blob: 93f6be1113610641ed437f357846ef27003a63e4 [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
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000018#include <mutex>
19
Johnny Chen9ed5b492012-01-05 21:48:15 +000020// Other libraries and framework includes
21#include "lldb/lldb-types.h"
Ed Maste41fba2b2015-06-01 15:24:37 +000022#include "lldb/Host/FileSpec.h"
Zachary Turner39de3112014-09-09 20:54:56 +000023#include "lldb/Host/HostThread.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000024
25namespace lldb_private
26{
27class Error;
28class Module;
29class Scalar;
30} // End lldb_private namespace.
31
32class ProcessFreeBSD;
33class Operation;
34
35/// @class ProcessMonitor
36/// @brief Manages communication with the inferior (debugee) process.
37///
38/// Upon construction, this class prepares and launches an inferior process for
39/// debugging.
40///
41/// Changes in the inferior process state are propagated to the associated
42/// ProcessFreeBSD instance by calling ProcessFreeBSD::SendMessage with the
43/// appropriate ProcessMessage events.
44///
45/// A purposely minimal set of operations are provided to interrogate and change
46/// the inferior process state.
47class ProcessMonitor
48{
49public:
50
51 /// Launches an inferior process ready for debugging. Forms the
52 /// implementation of Process::DoLaunch.
Ed Mastefe5a6422015-07-28 15:45:57 +000053 ProcessMonitor(ProcessFreeBSD *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +000054 lldb_private::Module *module,
55 char const *argv[],
56 char const *envp[],
Ed Maste41fba2b2015-06-01 15:24:37 +000057 const lldb_private::FileSpec &stdin_file_spec,
58 const lldb_private::FileSpec &stdout_file_spec,
59 const lldb_private::FileSpec &stderr_file_spec,
60 const lldb_private::FileSpec &working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +000061 const lldb_private::ProcessLaunchInfo &launch_info,
Johnny Chen9ed5b492012-01-05 21:48:15 +000062 lldb_private::Error &error);
63
Ed Mastefe5a6422015-07-28 15:45:57 +000064 ProcessMonitor(ProcessFreeBSD *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +000065 lldb::pid_t pid,
66 lldb_private::Error &error);
67
68 ~ProcessMonitor();
69
70 /// Provides the process number of debugee.
71 lldb::pid_t
72 GetPID() const { return m_pid; }
73
74 /// Returns the process associated with this ProcessMonitor.
75 ProcessFreeBSD &
76 GetProcess() { return *m_process; }
77
78 /// Returns a file descriptor to the controlling terminal of the inferior
79 /// process.
80 ///
81 /// Reads from this file descriptor yield both the standard output and
82 /// standard error of this debugee. Even if stderr and stdout were
83 /// redirected on launch it may still happen that data is available on this
Pavel Labath3a2da9e2015-02-06 11:32:52 +000084 /// descriptor (if the inferior process opens /dev/tty, for example). This descriptor is
85 /// closed after a call to StopMonitor().
Johnny Chen9ed5b492012-01-05 21:48:15 +000086 ///
87 /// If this monitor was attached to an existing process this method returns
88 /// -1.
89 int
90 GetTerminalFD() const { return m_terminal_fd; }
91
92 /// Reads @p size bytes from address @vm_adder in the inferior process
93 /// address space.
94 ///
95 /// This method is provided to implement Process::DoReadMemory.
96 size_t
97 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
98 lldb_private::Error &error);
99
100 /// Writes @p size bytes from address @p vm_adder in the inferior process
101 /// address space.
102 ///
103 /// This method is provided to implement Process::DoWriteMemory.
104 size_t
105 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
106 lldb_private::Error &error);
107
108 /// Reads the contents from the register identified by the given (architecture
109 /// dependent) offset.
110 ///
111 /// This method is provided for use by RegisterContextFreeBSD derivatives.
112 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000113 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000114 unsigned size, lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000115
116 /// Writes the given value to the register identified by the given
117 /// (architecture dependent) offset.
118 ///
119 /// This method is provided for use by RegisterContextFreeBSD derivatives.
120 bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000121 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000122 const lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000123
Ed Mastea4be2c52014-02-19 18:34:06 +0000124 /// Reads the contents from the debug register identified by the given
125 /// (architecture dependent) offset.
126 ///
127 /// This method is provided for use by RegisterContextFreeBSD derivatives.
128 bool
129 ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
130 const char *reg_name, unsigned size,
131 lldb_private::RegisterValue &value);
132
133 /// Writes the given value to the debug register identified by the given
134 /// (architecture dependent) offset.
135 ///
136 /// This method is provided for use by RegisterContextFreeBSD derivatives.
137 bool
138 WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
139 const char *reg_name,
140 const lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000141 /// Reads all general purpose registers into the specified buffer.
142 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000143 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000144
Matt Kopecc6672c82013-03-15 20:00:39 +0000145 /// Reads all floating point registers into the specified buffer.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000146 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000147 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000148
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000149 /// Reads the specified register set into the specified buffer.
150 ///
151 /// This method is provided for use by RegisterContextFreeBSD derivatives.
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000152 bool
153 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
154
Johnny Chen9ed5b492012-01-05 21:48:15 +0000155 /// Writes all general purpose registers into the specified buffer.
156 bool
Matt Kopec7de48462013-03-06 17:20:48 +0000157 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000158
Matt Kopecc6672c82013-03-15 20:00:39 +0000159 /// Writes all floating point registers into the specified buffer.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000160 bool
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000161 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
162
163 /// Writes the specified register set into the specified buffer.
164 ///
165 /// This method is provided for use by RegisterContextFreeBSD derivatives.
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000166 bool
167 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000168
Ed Maste68f51792013-10-18 19:16:44 +0000169 /// Reads the value of the thread-specific pointer for a given thread ID.
170 bool
171 ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value);
172
Ed Maste7fd845c2013-12-09 15:51:17 +0000173 /// Returns current thread IDs in process
174 size_t
175 GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids);
176
Ed Maste819e3992013-07-17 14:02:20 +0000177 /// Writes a ptrace_lwpinfo structure corresponding to the given thread ID
178 /// to the memory region pointed to by @p lwpinfo.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000179 bool
Ed Maste819e3992013-07-17 14:02:20 +0000180 GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &error_no);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000181
Ed Maste7fd845c2013-12-09 15:51:17 +0000182 /// Suspends or unsuspends a thread prior to process resume or step.
183 bool
184 ThreadSuspend(lldb::tid_t tid, bool suspend);
185
Johnny Chen9ed5b492012-01-05 21:48:15 +0000186 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
187 /// corresponding to the given thread IDto the memory pointed to by @p
188 /// message.
189 bool
190 GetEventMessage(lldb::tid_t tid, unsigned long *message);
191
Ed Maste502f9022013-11-25 16:31:23 +0000192 /// Resumes the process. If @p signo is anything but
193 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000194 bool
Ed Maste502f9022013-11-25 16:31:23 +0000195 Resume(lldb::tid_t unused, uint32_t signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000196
Ed Maste502f9022013-11-25 16:31:23 +0000197 /// Single steps the process. If @p signo is anything but
198 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000199 bool
Ed Maste502f9022013-11-25 16:31:23 +0000200 SingleStep(lldb::tid_t unused, uint32_t signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000201
Ed Maste70882932014-04-01 14:30:56 +0000202 /// Terminate the traced process.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000203 bool
Ed Maste70882932014-04-01 14:30:56 +0000204 Kill();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000205
206 lldb_private::Error
Matt Kopecedee1822013-06-03 19:48:53 +0000207 Detach(lldb::tid_t tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000208
Andrew Kaylorbc68b432013-07-10 21:57:27 +0000209 void
210 StopMonitor();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000211
Andrew Kaylord4d54992013-09-17 00:30:24 +0000212 // Waits for the initial stop message from a new thread.
213 bool
214 WaitForInitialTIDStop(lldb::tid_t tid);
215
Johnny Chen9ed5b492012-01-05 21:48:15 +0000216private:
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000217 ProcessFreeBSD *m_process;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000218
Ed Maste39677642014-09-10 13:38:47 +0000219 lldb_private::HostThread m_operation_thread;
220 lldb_private::HostThread m_monitor_thread;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000221 lldb::pid_t m_pid;
222
Johnny Chen9ed5b492012-01-05 21:48:15 +0000223 int m_terminal_fd;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000224
Ed Maste756e1ff2013-09-18 19:34:08 +0000225 // current operation which must be executed on the privileged thread
226 Operation *m_operation;
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000227 std::mutex m_operation_mutex;
Ed Maste756e1ff2013-09-18 19:34:08 +0000228
229 // semaphores notified when Operation is ready to be processed and when
230 // the operation is complete.
231 sem_t m_operation_pending;
232 sem_t m_operation_done;
Ed Maste41fba2b2015-06-01 15:24:37 +0000233
Johnny Chen9ed5b492012-01-05 21:48:15 +0000234 struct OperationArgs
235 {
236 OperationArgs(ProcessMonitor *monitor);
237
238 ~OperationArgs();
239
240 ProcessMonitor *m_monitor; // The monitor performing the attach.
241 sem_t m_semaphore; // Posted to once operation complete.
242 lldb_private::Error m_error; // Set if process operation failed.
243 };
244
245 /// @class LauchArgs
246 ///
247 /// @brief Simple structure to pass data to the thread responsible for
248 /// launching a child process.
249 struct LaunchArgs : OperationArgs
250 {
251 LaunchArgs(ProcessMonitor *monitor,
252 lldb_private::Module *module,
253 char const **argv,
254 char const **envp,
Ed Maste41fba2b2015-06-01 15:24:37 +0000255 const lldb_private::FileSpec &stdin_file_spec,
256 const lldb_private::FileSpec &stdout_file_spec,
257 const lldb_private::FileSpec &stderr_file_spec,
258 const lldb_private::FileSpec &working_dir);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000259
260 ~LaunchArgs();
261
Ed Maste41fba2b2015-06-01 15:24:37 +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 lldb_private::FileSpec m_stdin_file_spec; // Redirect stdin or empty.
266 const lldb_private::FileSpec m_stdout_file_spec; // Redirect stdout or empty.
267 const lldb_private::FileSpec m_stderr_file_spec; // Redirect stderr or empty.
268 const lldb_private::FileSpec m_working_dir; // Working directory or empty.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000269 };
270
271 void
272 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
273
Johnny Chen9ed5b492012-01-05 21:48:15 +0000274 static void *
275 LaunchOpThread(void *arg);
276
277 static bool
278 Launch(LaunchArgs *args);
279
Johnny Chen9ed5b492012-01-05 21:48:15 +0000280 struct AttachArgs : OperationArgs
281 {
282 AttachArgs(ProcessMonitor *monitor,
283 lldb::pid_t pid);
284
285 ~AttachArgs();
286
287 lldb::pid_t m_pid; // pid of the process to be attached.
288 };
289
290 void
291 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
292
Johnny Chen9ed5b492012-01-05 21:48:15 +0000293 static void *
294 AttachOpThread(void *args);
295
Oleksiy Vyalov5d064742014-11-19 18:27:45 +0000296 static void
Johnny Chen9ed5b492012-01-05 21:48:15 +0000297 Attach(AttachArgs *args);
298
299 static void
300 ServeOperation(OperationArgs *args);
301
302 static bool
Ed Maste41fba2b2015-06-01 15:24:37 +0000303 DupDescriptor(const lldb_private::FileSpec &file_spec, int fd, int flags);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000304
305 static bool
Pavel Labath998bdc52016-05-11 16:59:04 +0000306 MonitorCallback(ProcessMonitor *monitor, lldb::pid_t pid, bool exited, int signal, int status);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000307
308 static ProcessMessage
309 MonitorSIGTRAP(ProcessMonitor *monitor,
310 const siginfo_t *info, lldb::pid_t pid);
311
312 static ProcessMessage
313 MonitorSignal(ProcessMonitor *monitor,
314 const siginfo_t *info, lldb::pid_t pid);
315
Johnny Chen9ed5b492012-01-05 21:48:15 +0000316 void
317 DoOperation(Operation *op);
318
319 /// Stops the child monitor thread.
320 void
321 StopMonitoringChildProcess();
322
Ed Mastea02f5532013-07-02 16:45:16 +0000323 /// Stops the operation thread used to attach/launch a process.
324 void
325 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000326};
327
328#endif // #ifndef liblldb_ProcessMonitor_H_