ProcessMonitor.h revision 9bab8d4a18312d743c62083393fe5c39458c300b
1//===-- 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" 20#include "lldb/Host/Mutex.h" 21 22namespace lldb_private 23{ 24class Error; 25class Module; 26class Scalar; 27} // End lldb_private namespace. 28 29class ProcessLinux; 30class Operation; 31 32/// @class ProcessMonitor 33/// @brief Manages communication with the inferior (debugee) process. 34/// 35/// Upon construction, this class prepares and launches an inferior process for 36/// debugging. 37/// 38/// Changes in the inferior process state are propagated to the associated 39/// ProcessLinux instance by calling ProcessLinux::SendMessage with the 40/// appropriate ProcessMessage events. 41/// 42/// A purposely minimal set of operations are provided to interrogate and change 43/// the inferior process state. 44class ProcessMonitor 45{ 46public: 47 48 /// Launches an inferior process ready for debugging. Forms the 49 /// implementation of Process::DoLaunch. 50 ProcessMonitor(ProcessLinux *process, 51 lldb_private::Module *module, 52 char const *argv[], 53 char const *envp[], 54 const char *stdin_path, 55 const char *stdout_path, 56 const char *stderr_path, 57 lldb_private::Error &error); 58 59 ProcessMonitor(ProcessLinux *process, 60 lldb::pid_t pid, 61 lldb_private::Error &error); 62 63 ~ProcessMonitor(); 64 65 /// Provides the process number of debugee. 66 lldb::pid_t 67 GetPID() const { return m_pid; } 68 69 /// Returns the process associated with this ProcessMonitor. 70 ProcessLinux & 71 GetProcess() { return *m_process; } 72 73 /// Returns a file descriptor to the controlling terminal of the inferior 74 /// process. 75 /// 76 /// Reads from this file descriptor yield both the standard output and 77 /// standard error of this debugee. Even if stderr and stdout were 78 /// redirected on launch it may still happen that data is available on this 79 /// descriptor (if the inferior process opens /dev/tty, for example). 80 /// 81 /// If this monitor was attached to an existing process this method returns 82 /// -1. 83 int 84 GetTerminalFD() const { return m_terminal_fd; } 85 86 /// Reads @p size bytes from address @vm_adder in the inferior process 87 /// address space. 88 /// 89 /// This method is provided to implement Process::DoReadMemory. 90 size_t 91 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, 92 lldb_private::Error &error); 93 94 /// Writes @p size bytes from address @p vm_adder in the inferior process 95 /// address space. 96 /// 97 /// This method is provided to implement Process::DoWriteMemory. 98 size_t 99 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, 100 lldb_private::Error &error); 101 102 /// Reads the contents from the register identified by the given (architecture 103 /// dependent) offset. 104 /// 105 /// This method is provided for use by RegisterContextLinux derivatives. 106 bool 107 ReadRegisterValue(unsigned offset, lldb_private::RegisterValue &value); 108 109 /// Writes the given value to the register identified by the given 110 /// (architecture dependent) offset. 111 /// 112 /// This method is provided for use by RegisterContextLinux derivatives. 113 bool 114 WriteRegisterValue(unsigned offset, const lldb_private::RegisterValue &value); 115 116 /// Reads all general purpose registers into the specified buffer. 117 bool 118 ReadGPR(void *buf); 119 120 /// Reads all floating point registers into the specified buffer. 121 bool 122 ReadFPR(void *buf); 123 124 /// Writes all general purpose registers into the specified buffer. 125 bool 126 WriteGPR(void *buf); 127 128 /// Writes all floating point registers into the specified buffer. 129 bool 130 WriteFPR(void *buf); 131 132 /// Writes a siginfo_t structure corresponding to the given thread ID to the 133 /// memory region pointed to by @p siginfo. 134 bool 135 GetSignalInfo(lldb::tid_t tid, void *siginfo); 136 137 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 138 /// corresponding to the given thread IDto the memory pointed to by @p 139 /// message. 140 bool 141 GetEventMessage(lldb::tid_t tid, unsigned long *message); 142 143 /// Resumes the given thread. If @p signo is anything but 144 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. 145 bool 146 Resume(lldb::tid_t tid, uint32_t signo); 147 148 /// Single steps the given thread. If @p signo is anything but 149 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. 150 bool 151 SingleStep(lldb::tid_t tid, uint32_t signo); 152 153 /// Sends the inferior process a PTRACE_KILL signal. The inferior will 154 /// still exists and can be interrogated. Once resumed it will exit as 155 /// though it received a SIGKILL. 156 bool 157 BringProcessIntoLimbo(); 158 159 bool 160 Detach(); 161 162 163private: 164 ProcessLinux *m_process; 165 166 lldb::thread_t m_operation_thread; 167 lldb::pid_t m_pid; 168 int m_terminal_fd; 169 170 lldb::thread_t m_monitor_thread; 171 172 lldb_private::Mutex m_server_mutex; 173 int m_client_fd; 174 int m_server_fd; 175 176 struct OperationArgs 177 { 178 OperationArgs(ProcessMonitor *monitor); 179 180 ~OperationArgs(); 181 182 ProcessMonitor *m_monitor; // The monitor performing the attach. 183 sem_t m_semaphore; // Posted to once operation complete. 184 lldb_private::Error m_error; // Set if process operation failed. 185 }; 186 187 /// @class LauchArgs 188 /// 189 /// @brief Simple structure to pass data to the thread responsible for 190 /// launching a child process. 191 struct LaunchArgs : OperationArgs 192 { 193 LaunchArgs(ProcessMonitor *monitor, 194 lldb_private::Module *module, 195 char const **argv, 196 char const **envp, 197 const char *stdin_path, 198 const char *stdout_path, 199 const char *stderr_path); 200 201 ~LaunchArgs(); 202 203 ProcessMonitor *m_monitor; // The monitor performing the launch. 204 lldb_private::Module *m_module; // The executable image to launch. 205 char const **m_argv; // Process arguments. 206 char const **m_envp; // Process environment. 207 const char *m_stdin_path; // Redirect stdin or NULL. 208 const char *m_stdout_path; // Redirect stdout or NULL. 209 const char *m_stderr_path; // Redirect stderr or NULL. 210 }; 211 212 void 213 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error); 214 215 void 216 StopLaunchOpThread(); 217 218 static void * 219 LaunchOpThread(void *arg); 220 221 static bool 222 Launch(LaunchArgs *args); 223 224 bool 225 EnableIPC(); 226 227 struct AttachArgs : OperationArgs 228 { 229 AttachArgs(ProcessMonitor *monitor, 230 lldb::pid_t pid); 231 232 ~AttachArgs(); 233 234 lldb::pid_t m_pid; // pid of the process to be attached. 235 }; 236 237 void 238 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error); 239 240 void 241 StopAttachOpThread(); 242 243 static void * 244 AttachOpThread(void *args); 245 246 static bool 247 Attach(AttachArgs *args); 248 249 static void 250 ServeOperation(OperationArgs *args); 251 252 static bool 253 DupDescriptor(const char *path, int fd, int flags); 254 255 static bool 256 MonitorCallback(void *callback_baton, 257 lldb::pid_t pid, int signal, int status); 258 259 static ProcessMessage 260 MonitorSIGTRAP(ProcessMonitor *monitor, 261 const struct siginfo *info, lldb::pid_t pid); 262 263 static ProcessMessage 264 MonitorSignal(ProcessMonitor *monitor, 265 const struct siginfo *info, lldb::pid_t pid); 266 267 static ProcessMessage::CrashReason 268 GetCrashReasonForSIGSEGV(const struct siginfo *info); 269 270 static ProcessMessage::CrashReason 271 GetCrashReasonForSIGILL(const struct siginfo *info); 272 273 static ProcessMessage::CrashReason 274 GetCrashReasonForSIGFPE(const struct siginfo *info); 275 276 static ProcessMessage::CrashReason 277 GetCrashReasonForSIGBUS(const struct siginfo *info); 278 279 void 280 DoOperation(Operation *op); 281 282 /// Stops the child monitor thread. 283 void 284 StopMonitoringChildProcess(); 285 286 void 287 StopMonitor(); 288 289 void 290 CloseFD(int &fd); 291}; 292 293#endif // #ifndef liblldb_ProcessMonitor_H_ 294