ProcessMonitor.h revision 4e1ba0f55504538d823775c36a0cf2b944fcc2bf
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 16// C++ Includes 17// Other libraries and framework includes 18#include "lldb/lldb-types.h" 19#include "lldb/Host/Mutex.h" 20 21namespace lldb_private 22{ 23class Error; 24class Module; 25class Scalar; 26} // End lldb_private namespace. 27 28class ProcessLinux; 29class Operation; 30 31/// @class ProcessMonitor 32/// @brief Manages communication with the inferior (debugee) process. 33/// 34/// Upon construction, this class prepares and launches an inferior process for 35/// debugging. 36/// 37/// Changes in the inferior process state are propagated to the associated 38/// ProcessLinux instance by calling ProcessLinux::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. 43class ProcessMonitor 44{ 45public: 46 47 /// Launches an inferior process ready for debugging. Forms the 48 /// implementation of Process::DoLaunch. 49 ProcessMonitor(ProcessLinux *process, 50 lldb_private::Module *module, 51 char const *argv[], 52 char const *envp[], 53 const char *stdin_path, 54 const char *stdout_path, 55 const char *stderr_path, 56 lldb_private::Error &error); 57 58 ~ProcessMonitor(); 59 60 /// Provides the process number of debugee. 61 lldb::pid_t 62 GetPID() const { return m_pid; } 63 64 /// Returns the process associated with this ProcessMonitor. 65 ProcessLinux & 66 GetProcess() { return *m_process; } 67 68 /// Returns a file descriptor to the controlling terminal of the inferior 69 /// process. 70 /// 71 /// Reads from this file descriptor yield both the standard output and 72 /// standard error of this debugee. Even if stderr and stdout were 73 /// redirected on launch it may still happen that data is available on this 74 /// descriptor (if the inferior process opens /dev/tty, for example). 75 /// 76 /// If this monitor was attached to an existing process this method returns 77 /// -1. 78 int 79 GetTerminalFD() const { return m_terminal_fd; } 80 81 /// Reads @p size bytes from address @vm_adder in the inferior process 82 /// address space. 83 /// 84 /// This method is provided to implement Process::DoReadMemory. 85 size_t 86 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, 87 lldb_private::Error &error); 88 89 /// Writes @p size bytes from address @p vm_adder in the inferior process 90 /// address space. 91 /// 92 /// This method is provided to implement Process::DoWriteMemory. 93 size_t 94 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, 95 lldb_private::Error &error); 96 97 /// Reads the contents from the register identified by the given (architecture 98 /// dependent) offset. 99 /// 100 /// This method is provided for use by RegisterContextLinux derivatives. 101 bool 102 ReadRegisterValue(unsigned offset, lldb_private::Scalar &value); 103 104 /// Writes the given value to the register identified by the given 105 /// (architecture dependent) offset. 106 /// 107 /// This method is provided for use by RegisterContextLinux derivatives. 108 bool 109 WriteRegisterValue(unsigned offset, const lldb_private::Scalar &value); 110 111 /// Reads all general purpose registers into the specified buffer. 112 bool 113 ReadGPR(void *buf); 114 115 /// Reads all floating point registers into the specified buffer. 116 bool 117 ReadFPR(void *buf); 118 119 /// Writes a siginfo_t structure corresponding to the given thread ID to the 120 /// memory region pointed to by @p siginfo. 121 bool 122 GetSignalInfo(lldb::tid_t tid, void *siginfo); 123 124 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 125 /// corresponding to the given thread IDto the memory pointed to by @p 126 /// message. 127 bool 128 GetEventMessage(lldb::tid_t tid, unsigned long *message); 129 130 /// Resumes the given thread. 131 bool 132 Resume(lldb::tid_t tid); 133 134 /// Single steps the given thread. 135 bool 136 SingleStep(lldb::tid_t tid); 137 138 /// Sends the inferior process a PTRACE_KILL signal. The inferior will 139 /// still exists and can be interrogated. Once resumed it will exit as 140 /// though it received a SIGKILL. 141 bool 142 BringProcessIntoLimbo(); 143 144private: 145 ProcessLinux *m_process; 146 147 lldb::thread_t m_operation_thread; 148 lldb::pid_t m_pid; 149 int m_terminal_fd; 150 151 lldb::thread_t m_monitor_thread; 152 153 lldb_private::Mutex m_server_mutex; 154 int m_client_fd; 155 int m_server_fd; 156 157 /// @class LauchArgs 158 /// 159 /// @brief Simple structure to pass data to the thread responsible for 160 /// launching a child process. 161 struct LaunchArgs 162 { 163 LaunchArgs(ProcessMonitor *monitor, 164 lldb_private::Module *module, 165 char const **argv, 166 char const **envp, 167 const char *stdin_path, 168 const char *stdout_path, 169 const char *stderr_path); 170 171 ~LaunchArgs(); 172 173 ProcessMonitor *m_monitor; // The monitor performing the launch. 174 lldb_private::Module *m_module; // The executable image to launch. 175 char const **m_argv; // Process arguments. 176 char const **m_envp; // Process environment. 177 const char *m_stdin_path; // Redirect stdin or NULL. 178 const char *m_stdout_path; // Redirect stdout or NULL. 179 const char *m_stderr_path; // Redirect stderr or NULL. 180 sem_t m_semaphore; // Posted to once launch complete. 181 lldb_private::Error m_error; // Set if process launch failed. 182 }; 183 184 void 185 StartOperationThread(LaunchArgs *args, lldb_private::Error &error); 186 187 void 188 StopOperationThread(); 189 190 static void * 191 OperationThread(void *arg); 192 193 static bool 194 Launch(LaunchArgs *args); 195 196 bool 197 EnableIPC(); 198 199 static void 200 ServeOperation(LaunchArgs *args); 201 202 static bool 203 DupDescriptor(const char *path, int fd, int flags); 204 205 static bool 206 MonitorCallback(void *callback_baton, 207 lldb::pid_t pid, int signal, int status); 208 209 static ProcessMessage 210 MonitorSIGTRAP(ProcessMonitor *monitor, lldb::pid_t pid); 211 212 void 213 DoOperation(Operation *op); 214 215 /// Stops the child monitor thread. 216 void StopMonitoringChildProcess(); 217}; 218 219#endif // #ifndef liblldb_ProcessMonitor_H_ 220