ProcessMonitor.h revision 263363
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 ProcessFreeBSD; 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/// ProcessFreeBSD instance by calling ProcessFreeBSD::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(ProcessPOSIX *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 const char *working_dir, 58 lldb_private::Error &error); 59 60 ProcessMonitor(ProcessPOSIX *process, 61 lldb::pid_t pid, 62 lldb_private::Error &error); 63 64 ~ProcessMonitor(); 65 66 /// Provides the process number of debugee. 67 lldb::pid_t 68 GetPID() const { return m_pid; } 69 70 /// Returns the process associated with this ProcessMonitor. 71 ProcessFreeBSD & 72 GetProcess() { return *m_process; } 73 74 /// Returns a file descriptor to the controlling terminal of the inferior 75 /// process. 76 /// 77 /// Reads from this file descriptor yield both the standard output and 78 /// standard error of this debugee. Even if stderr and stdout were 79 /// redirected on launch it may still happen that data is available on this 80 /// descriptor (if the inferior process opens /dev/tty, for example). 81 /// 82 /// If this monitor was attached to an existing process this method returns 83 /// -1. 84 int 85 GetTerminalFD() const { return m_terminal_fd; } 86 87 /// Reads @p size bytes from address @vm_adder in the inferior process 88 /// address space. 89 /// 90 /// This method is provided to implement Process::DoReadMemory. 91 size_t 92 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, 93 lldb_private::Error &error); 94 95 /// Writes @p size bytes from address @p vm_adder in the inferior process 96 /// address space. 97 /// 98 /// This method is provided to implement Process::DoWriteMemory. 99 size_t 100 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, 101 lldb_private::Error &error); 102 103 /// Reads the contents from the register identified by the given (architecture 104 /// dependent) offset. 105 /// 106 /// This method is provided for use by RegisterContextFreeBSD derivatives. 107 /// FIXME: The FreeBSD implementation of this function should use tid in order 108 /// to enable support for debugging threaded programs. 109 bool 110 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name, 111 unsigned size, lldb_private::RegisterValue &value); 112 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 /// FIXME: The FreeBSD implementation of this function should use tid in order 118 /// to enable support for debugging threaded programs. 119 bool 120 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name, 121 const lldb_private::RegisterValue &value); 122 123 /// Reads all general purpose registers into the specified buffer. 124 /// FIXME: The FreeBSD implementation of this function should use tid in order 125 /// to enable support for debugging threaded programs. 126 bool 127 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size); 128 129 /// Reads all floating point registers into the specified buffer. 130 /// FIXME: The FreeBSD implementation of this function should use tid in order 131 /// to enable support for debugging threaded programs. 132 bool 133 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size); 134 135 /// Reads the specified register set into the specified buffer. 136 /// 137 /// This method is provided for use by RegisterContextFreeBSD derivatives. 138 /// FIXME: The FreeBSD implementation of this function should use tid in order 139 /// to enable support for debugging threaded programs. 140 bool 141 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset); 142 143 /// Writes all general purpose registers into the specified buffer. 144 /// FIXME: The FreeBSD implementation of this function should use tid in order 145 /// to enable support for debugging threaded programs. 146 bool 147 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size); 148 149 /// Writes all floating point registers into the specified buffer. 150 /// FIXME: The FreeBSD implementation of this function should use tid in order 151 /// to enable support for debugging threaded programs. 152 bool 153 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size); 154 155 /// Writes the specified register set into the specified buffer. 156 /// 157 /// This method is provided for use by RegisterContextFreeBSD derivatives. 158 /// FIXME: The FreeBSD implementation of this function should use tid in order 159 /// to enable support for debugging threaded programs. 160 bool 161 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset); 162 163 /// Reads the value of the thread-specific pointer for a given thread ID. 164 bool 165 ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value); 166 167 /// Writes a ptrace_lwpinfo structure corresponding to the given thread ID 168 /// to the memory region pointed to by @p lwpinfo. 169 bool 170 GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &error_no); 171 172 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 173 /// corresponding to the given thread IDto the memory pointed to by @p 174 /// message. 175 bool 176 GetEventMessage(lldb::tid_t tid, unsigned long *message); 177 178 /// Resumes the given thread. If @p signo is anything but 179 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. 180 bool 181 Resume(lldb::tid_t tid, uint32_t signo); 182 183 /// Single steps the given thread. If @p signo is anything but 184 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. 185 bool 186 SingleStep(lldb::tid_t tid, uint32_t signo); 187 188 /// Sends the inferior process a PTRACE_KILL signal. The inferior will 189 /// still exists and can be interrogated. Once resumed it will exit as 190 /// though it received a SIGKILL. 191 bool 192 BringProcessIntoLimbo(); 193 194 lldb_private::Error 195 Detach(lldb::tid_t tid); 196 197 void 198 StopMonitor(); 199 200 // Waits for the initial stop message from a new thread. 201 bool 202 WaitForInitialTIDStop(lldb::tid_t tid); 203 204private: 205 ProcessFreeBSD *m_process; 206 207 lldb::thread_t m_operation_thread; 208 lldb::thread_t m_monitor_thread; 209 lldb::pid_t m_pid; 210 211 int m_terminal_fd; 212 213 // current operation which must be executed on the privileged thread 214 Operation *m_operation; 215 lldb_private::Mutex m_operation_mutex; 216 217 // semaphores notified when Operation is ready to be processed and when 218 // the operation is complete. 219 sem_t m_operation_pending; 220 sem_t m_operation_done; 221 222 struct OperationArgs 223 { 224 OperationArgs(ProcessMonitor *monitor); 225 226 ~OperationArgs(); 227 228 ProcessMonitor *m_monitor; // The monitor performing the attach. 229 sem_t m_semaphore; // Posted to once operation complete. 230 lldb_private::Error m_error; // Set if process operation failed. 231 }; 232 233 /// @class LauchArgs 234 /// 235 /// @brief Simple structure to pass data to the thread responsible for 236 /// launching a child process. 237 struct LaunchArgs : OperationArgs 238 { 239 LaunchArgs(ProcessMonitor *monitor, 240 lldb_private::Module *module, 241 char const **argv, 242 char const **envp, 243 const char *stdin_path, 244 const char *stdout_path, 245 const char *stderr_path, 246 const char *working_dir); 247 248 ~LaunchArgs(); 249 250 lldb_private::Module *m_module; // The executable image to launch. 251 char const **m_argv; // Process arguments. 252 char const **m_envp; // Process environment. 253 const char *m_stdin_path; // Redirect stdin or NULL. 254 const char *m_stdout_path; // Redirect stdout or NULL. 255 const char *m_stderr_path; // Redirect stderr or NULL. 256 const char *m_working_dir; // Working directory or NULL. 257 }; 258 259 void 260 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error); 261 262 static void * 263 LaunchOpThread(void *arg); 264 265 static bool 266 Launch(LaunchArgs *args); 267 268 struct AttachArgs : OperationArgs 269 { 270 AttachArgs(ProcessMonitor *monitor, 271 lldb::pid_t pid); 272 273 ~AttachArgs(); 274 275 lldb::pid_t m_pid; // pid of the process to be attached. 276 }; 277 278 void 279 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error); 280 281 static void * 282 AttachOpThread(void *args); 283 284 static bool 285 Attach(AttachArgs *args); 286 287 static void 288 ServeOperation(OperationArgs *args); 289 290 static bool 291 DupDescriptor(const char *path, int fd, int flags); 292 293 static bool 294 MonitorCallback(void *callback_baton, 295 lldb::pid_t pid, bool exited, int signal, int status); 296 297 static ProcessMessage 298 MonitorSIGTRAP(ProcessMonitor *monitor, 299 const siginfo_t *info, lldb::pid_t pid); 300 301 static ProcessMessage 302 MonitorSignal(ProcessMonitor *monitor, 303 const siginfo_t *info, lldb::pid_t pid); 304 305 static ProcessMessage::CrashReason 306 GetCrashReasonForSIGSEGV(const siginfo_t *info); 307 308 static ProcessMessage::CrashReason 309 GetCrashReasonForSIGILL(const siginfo_t *info); 310 311 static ProcessMessage::CrashReason 312 GetCrashReasonForSIGFPE(const siginfo_t *info); 313 314 static ProcessMessage::CrashReason 315 GetCrashReasonForSIGBUS(const siginfo_t *info); 316 317 void 318 DoOperation(Operation *op); 319 320 /// Stops the child monitor thread. 321 void 322 StopMonitoringChildProcess(); 323 324 /// Stops the operation thread used to attach/launch a process. 325 void 326 StopOpThread(); 327}; 328 329#endif // #ifndef liblldb_ProcessMonitor_H_ 330