blob: ca7c4e03966c8d145326de4f7902c71eaf0377ca [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
Johnny Chen9ed5b492012-01-05 21:48:15 +000013#include <semaphore.h>
14#include <signal.h>
15
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000016#include <mutex>
17
Zachary Turner39de3112014-09-09 20:54:56 +000018#include "lldb/Host/HostThread.h"
Zachary Turner5713a052017-03-22 18:40:07 +000019#include "lldb/Utility/FileSpec.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000020#include "lldb/lldb-types.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000021
Kate Stoneb9c1b512016-09-06 20:57:50 +000022namespace lldb_private {
Zachary Turner97206d52017-05-12 04:51:55 +000023class Status;
Johnny Chen9ed5b492012-01-05 21:48:15 +000024class Module;
25class Scalar;
26} // End lldb_private namespace.
27
28class ProcessFreeBSD;
29class Operation;
30
31/// @class ProcessMonitor
Adrian Prantld8f460e2018-05-02 16:55:16 +000032/// Manages communication with the inferior (debugee) process.
Johnny Chen9ed5b492012-01-05 21:48:15 +000033///
Adrian Prantld8f460e2018-05-02 16:55:16 +000034/// Upon construction, this class prepares and launches an inferior process
35/// for debugging.
Johnny Chen9ed5b492012-01-05 21:48:15 +000036///
37/// Changes in the inferior process state are propagated to the associated
38/// ProcessFreeBSD instance by calling ProcessFreeBSD::SendMessage with the
39/// appropriate ProcessMessage events.
40///
41/// A purposely minimal set of operations are provided to interrogate and change
42/// the inferior process state.
Kate Stoneb9c1b512016-09-06 20:57:50 +000043class ProcessMonitor {
Johnny Chen9ed5b492012-01-05 21:48:15 +000044public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000045 /// Launches an inferior process ready for debugging. Forms the
46 /// implementation of Process::DoLaunch.
47 ProcessMonitor(ProcessFreeBSD *process, lldb_private::Module *module,
Pavel Labath75c6de02018-01-10 13:53:40 +000048 char const *argv[], lldb_private::Environment env,
Kate Stoneb9c1b512016-09-06 20:57:50 +000049 const lldb_private::FileSpec &stdin_file_spec,
50 const lldb_private::FileSpec &stdout_file_spec,
51 const lldb_private::FileSpec &stderr_file_spec,
52 const lldb_private::FileSpec &working_dir,
53 const lldb_private::ProcessLaunchInfo &launch_info,
Zachary Turner97206d52017-05-12 04:51:55 +000054 lldb_private::Status &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 ProcessMonitor(ProcessFreeBSD *process, lldb::pid_t pid,
Zachary Turner97206d52017-05-12 04:51:55 +000057 lldb_private::Status &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 ~ProcessMonitor();
Johnny Chen9ed5b492012-01-05 21:48:15 +000060
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 /// Provides the process number of debugee.
62 lldb::pid_t GetPID() const { return m_pid; }
Johnny Chen9ed5b492012-01-05 21:48:15 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 /// Returns the process associated with this ProcessMonitor.
65 ProcessFreeBSD &GetProcess() { return *m_process; }
Johnny Chen9ed5b492012-01-05 21:48:15 +000066
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 /// Returns a file descriptor to the controlling terminal of the inferior
68 /// process.
69 ///
70 /// Reads from this file descriptor yield both the standard output and
71 /// standard error of this debugee. Even if stderr and stdout were
72 /// redirected on launch it may still happen that data is available on this
73 /// descriptor (if the inferior process opens /dev/tty, for example). This
Adrian Prantld8f460e2018-05-02 16:55:16 +000074 /// descriptor is closed after a call to StopMonitor().
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 ///
76 /// If this monitor was attached to an existing process this method returns
77 /// -1.
78 int GetTerminalFD() const { return m_terminal_fd; }
Johnny Chen9ed5b492012-01-05 21:48:15 +000079
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 /// Reads @p size bytes from address @vm_adder in the inferior process
81 /// address space.
82 ///
83 /// This method is provided to implement Process::DoReadMemory.
84 size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +000085 lldb_private::Status &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 /// Writes @p size bytes from address @p vm_adder in the inferior process
88 /// address space.
89 ///
90 /// This method is provided to implement Process::DoWriteMemory.
91 size_t WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +000092 lldb_private::Status &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +000093
Adrian Prantld8f460e2018-05-02 16:55:16 +000094 /// Reads the contents from the register identified by the given
95 /// (architecture dependent) offset.
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 ///
97 /// This method is provided for use by RegisterContextFreeBSD derivatives.
98 bool ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
99 unsigned size, lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 /// Writes the given value to the register identified by the given
102 /// (architecture dependent) offset.
103 ///
104 /// This method is provided for use by RegisterContextFreeBSD derivatives.
105 bool WriteRegisterValue(lldb::tid_t tid, unsigned offset,
106 const char *reg_name,
107 const lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 /// Reads the contents from the debug register identified by the given
110 /// (architecture dependent) offset.
111 ///
112 /// This method is provided for use by RegisterContextFreeBSD derivatives.
113 bool ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
114 const char *reg_name, unsigned size,
115 lldb_private::RegisterValue &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 /// Writes the given value to the debug register identified by the given
118 /// (architecture dependent) offset.
119 ///
120 /// This method is provided for use by RegisterContextFreeBSD derivatives.
121 bool WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
122 const char *reg_name,
123 const lldb_private::RegisterValue &value);
124 /// Reads all general purpose registers into the specified buffer.
125 bool ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Ed Mastea4be2c52014-02-19 18:34:06 +0000126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 /// Reads all floating point registers into the specified buffer.
128 bool ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 /// Reads the specified register set into the specified buffer.
131 ///
132 /// This method is provided for use by RegisterContextFreeBSD derivatives.
133 bool ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size,
134 unsigned int regset);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 /// Writes all general purpose registers into the specified buffer.
137 bool WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 /// Writes all floating point registers into the specified buffer.
140 bool WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 /// Writes the specified register set into the specified buffer.
143 ///
144 /// This method is provided for use by RegisterContextFreeBSD derivatives.
145 bool WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size,
146 unsigned int regset);
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 /// Reads the value of the thread-specific pointer for a given thread ID.
149 bool ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151 /// Returns current thread IDs in process
152 size_t GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids);
Ed Maste68f51792013-10-18 19:16:44 +0000153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 /// Writes a ptrace_lwpinfo structure corresponding to the given thread ID
155 /// to the memory region pointed to by @p lwpinfo.
156 bool GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &error_no);
Ed Maste7fd845c2013-12-09 15:51:17 +0000157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 /// Suspends or unsuspends a thread prior to process resume or step.
159 bool ThreadSuspend(lldb::tid_t tid, bool suspend);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 /// 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 GetEventMessage(lldb::tid_t tid, unsigned long *message);
Ed Maste7fd845c2013-12-09 15:51:17 +0000165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 /// Resumes the process. If @p signo is anything but
167 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
168 bool Resume(lldb::tid_t unused, uint32_t signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000169
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 /// Single steps the process. If @p signo is anything but
171 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
172 bool SingleStep(lldb::tid_t unused, uint32_t signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 /// Terminate the traced process.
175 bool Kill();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000176
Zachary Turner97206d52017-05-12 04:51:55 +0000177 lldb_private::Status Detach(lldb::tid_t tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 void StopMonitor();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000180
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 // Waits for the initial stop message from a new thread.
182 bool WaitForInitialTIDStop(lldb::tid_t tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +0000183
Johnny Chen9ed5b492012-01-05 21:48:15 +0000184private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 ProcessFreeBSD *m_process;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 lldb_private::HostThread m_operation_thread;
188 lldb_private::HostThread m_monitor_thread;
189 lldb::pid_t m_pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000190
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 int m_terminal_fd;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 // current operation which must be executed on the privileged thread
194 Operation *m_operation;
195 std::mutex m_operation_mutex;
Ed Maste756e1ff2013-09-18 19:34:08 +0000196
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 // semaphores notified when Operation is ready to be processed and when
198 // the operation is complete.
199 sem_t m_operation_pending;
200 sem_t m_operation_done;
Ed Maste41fba2b2015-06-01 15:24:37 +0000201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 struct OperationArgs {
203 OperationArgs(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 ~OperationArgs();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 ProcessMonitor *m_monitor; // The monitor performing the attach.
208 sem_t m_semaphore; // Posted to once operation complete.
Zachary Turner97206d52017-05-12 04:51:55 +0000209 lldb_private::Status m_error; // Set if process operation failed.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 };
Johnny Chen9ed5b492012-01-05 21:48:15 +0000211
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 /// @class LauchArgs
213 ///
Adrian Prantld8f460e2018-05-02 16:55:16 +0000214 /// Simple structure to pass data to the thread responsible for launching a
215 /// child process.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 struct LaunchArgs : OperationArgs {
217 LaunchArgs(ProcessMonitor *monitor, lldb_private::Module *module,
Pavel Labath75c6de02018-01-10 13:53:40 +0000218 char const **argv, lldb_private::Environment env,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219 const lldb_private::FileSpec &stdin_file_spec,
220 const lldb_private::FileSpec &stdout_file_spec,
221 const lldb_private::FileSpec &stderr_file_spec,
222 const lldb_private::FileSpec &working_dir);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000223
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 ~LaunchArgs();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 lldb_private::Module *m_module; // The executable image to launch.
227 char const **m_argv; // Process arguments.
Pavel Labath75c6de02018-01-10 13:53:40 +0000228 lldb_private::Environment m_env; // Process environment.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 const lldb_private::FileSpec m_stdin_file_spec; // Redirect stdin or empty.
230 const lldb_private::FileSpec
231 m_stdout_file_spec; // Redirect stdout or empty.
232 const lldb_private::FileSpec
233 m_stderr_file_spec; // Redirect stderr or empty.
234 const lldb_private::FileSpec m_working_dir; // Working directory or empty.
235 };
Johnny Chen9ed5b492012-01-05 21:48:15 +0000236
Zachary Turner97206d52017-05-12 04:51:55 +0000237 void StartLaunchOpThread(LaunchArgs *args, lldb_private::Status &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 static void *LaunchOpThread(void *arg);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 static bool Launch(LaunchArgs *args);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 struct AttachArgs : OperationArgs {
244 AttachArgs(ProcessMonitor *monitor, lldb::pid_t pid);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 ~AttachArgs();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 lldb::pid_t m_pid; // pid of the process to be attached.
249 };
Johnny Chen9ed5b492012-01-05 21:48:15 +0000250
Zachary Turner97206d52017-05-12 04:51:55 +0000251 void StartAttachOpThread(AttachArgs *args, lldb_private::Status &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 static void *AttachOpThread(void *args);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000254
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255 static void Attach(AttachArgs *args);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000256
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 static void ServeOperation(OperationArgs *args);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000258
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 static bool DupDescriptor(const lldb_private::FileSpec &file_spec, int fd,
260 int flags);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 static bool MonitorCallback(ProcessMonitor *monitor, lldb::pid_t pid,
263 bool exited, int signal, int status);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 static ProcessMessage MonitorSIGTRAP(ProcessMonitor *monitor,
266 const siginfo_t *info, lldb::pid_t pid);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 static ProcessMessage MonitorSignal(ProcessMonitor *monitor,
269 const siginfo_t *info, lldb::pid_t pid);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000270
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 void DoOperation(Operation *op);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 /// Stops the child monitor thread.
274 void StopMonitoringChildProcess();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000275
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 /// Stops the operation thread used to attach/launch a process.
277 void StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000278};
279
280#endif // #ifndef liblldb_ProcessMonitor_H_