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/FileSpec.h" 21 #include "lldb/Host/HostThread.h" 22 #include "lldb/Host/Mutex.h" 23 24 namespace lldb_private 25 { 26 class Error; 27 class Module; 28 class Scalar; 29 } // End lldb_private namespace. 30 31 class ProcessFreeBSD; 32 class Operation; 33 34 /// @class ProcessMonitor 35 /// @brief Manages communication with the inferior (debugee) process. 36 /// 37 /// Upon construction, this class prepares and launches an inferior process for 38 /// debugging. 39 /// 40 /// Changes in the inferior process state are propagated to the associated 41 /// ProcessFreeBSD instance by calling ProcessFreeBSD::SendMessage with the 42 /// appropriate ProcessMessage events. 43 /// 44 /// A purposely minimal set of operations are provided to interrogate and change 45 /// the inferior process state. 46 class ProcessMonitor 47 { 48 public: 49 50 /// Launches an inferior process ready for debugging. Forms the 51 /// implementation of Process::DoLaunch. 52 ProcessMonitor(ProcessFreeBSD *process, 53 lldb_private::Module *module, 54 char const *argv[], 55 char const *envp[], 56 const lldb_private::FileSpec &stdin_file_spec, 57 const lldb_private::FileSpec &stdout_file_spec, 58 const lldb_private::FileSpec &stderr_file_spec, 59 const lldb_private::FileSpec &working_dir, 60 const lldb_private::ProcessLaunchInfo &launch_info, 61 lldb_private::Error &error); 62 63 ProcessMonitor(ProcessFreeBSD *process, 64 lldb::pid_t pid, 65 lldb_private::Error &error); 66 67 ~ProcessMonitor(); 68 69 /// Provides the process number of debugee. 70 lldb::pid_t 71 GetPID() const { return m_pid; } 72 73 /// Returns the process associated with this ProcessMonitor. 74 ProcessFreeBSD & 75 GetProcess() { return *m_process; } 76 77 /// Returns a file descriptor to the controlling terminal of the inferior 78 /// process. 79 /// 80 /// Reads from this file descriptor yield both the standard output and 81 /// standard error of this debugee. Even if stderr and stdout were 82 /// redirected on launch it may still happen that data is available on this 83 /// descriptor (if the inferior process opens /dev/tty, for example). This descriptor is 84 /// closed after a call to StopMonitor(). 85 /// 86 /// If this monitor was attached to an existing process this method returns 87 /// -1. 88 int 89 GetTerminalFD() const { return m_terminal_fd; } 90 91 /// Reads @p size bytes from address @vm_adder in the inferior process 92 /// address space. 93 /// 94 /// This method is provided to implement Process::DoReadMemory. 95 size_t 96 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, 97 lldb_private::Error &error); 98 99 /// Writes @p size bytes from address @p vm_adder in the inferior process 100 /// address space. 101 /// 102 /// This method is provided to implement Process::DoWriteMemory. 103 size_t 104 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, 105 lldb_private::Error &error); 106 107 /// Reads the contents from the register identified by the given (architecture 108 /// dependent) offset. 109 /// 110 /// This method is provided for use by RegisterContextFreeBSD derivatives. 111 bool 112 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name, 113 unsigned size, lldb_private::RegisterValue &value); 114 115 /// Writes the given value to the register identified by the given 116 /// (architecture dependent) offset. 117 /// 118 /// This method is provided for use by RegisterContextFreeBSD derivatives. 119 bool 120 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name, 121 const lldb_private::RegisterValue &value); 122 123 /// Reads the contents from the debug register identified by the given 124 /// (architecture dependent) offset. 125 /// 126 /// This method is provided for use by RegisterContextFreeBSD derivatives. 127 bool 128 ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset, 129 const char *reg_name, unsigned size, 130 lldb_private::RegisterValue &value); 131 132 /// Writes the given value to the debug register identified by the given 133 /// (architecture dependent) offset. 134 /// 135 /// This method is provided for use by RegisterContextFreeBSD derivatives. 136 bool 137 WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset, 138 const char *reg_name, 139 const lldb_private::RegisterValue &value); 140 /// Reads all general purpose registers into the specified buffer. 141 bool 142 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size); 143 144 /// Reads all floating point registers into the specified buffer. 145 bool 146 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size); 147 148 /// Reads the specified register set into the specified buffer. 149 /// 150 /// This method is provided for use by RegisterContextFreeBSD derivatives. 151 bool 152 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset); 153 154 /// Writes all general purpose registers into the specified buffer. 155 bool 156 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size); 157 158 /// Writes all floating point registers into the specified buffer. 159 bool 160 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size); 161 162 /// Writes the specified register set into the specified buffer. 163 /// 164 /// This method is provided for use by RegisterContextFreeBSD derivatives. 165 bool 166 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset); 167 168 /// Reads the value of the thread-specific pointer for a given thread ID. 169 bool 170 ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value); 171 172 /// Returns current thread IDs in process 173 size_t 174 GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids); 175 176 /// Writes a ptrace_lwpinfo structure corresponding to the given thread ID 177 /// to the memory region pointed to by @p lwpinfo. 178 bool 179 GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &error_no); 180 181 /// Suspends or unsuspends a thread prior to process resume or step. 182 bool 183 ThreadSuspend(lldb::tid_t tid, bool suspend); 184 185 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 186 /// corresponding to the given thread IDto the memory pointed to by @p 187 /// message. 188 bool 189 GetEventMessage(lldb::tid_t tid, unsigned long *message); 190 191 /// Resumes the process. If @p signo is anything but 192 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process. 193 bool 194 Resume(lldb::tid_t unused, uint32_t signo); 195 196 /// Single steps the process. If @p signo is anything but 197 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process. 198 bool 199 SingleStep(lldb::tid_t unused, uint32_t signo); 200 201 /// Terminate the traced process. 202 bool 203 Kill(); 204 205 lldb_private::Error 206 Detach(lldb::tid_t tid); 207 208 void 209 StopMonitor(); 210 211 // Waits for the initial stop message from a new thread. 212 bool 213 WaitForInitialTIDStop(lldb::tid_t tid); 214 215 private: 216 ProcessFreeBSD *m_process; 217 218 lldb_private::HostThread m_operation_thread; 219 lldb_private::HostThread m_monitor_thread; 220 lldb::pid_t m_pid; 221 222 int m_terminal_fd; 223 224 // current operation which must be executed on the privileged thread 225 Operation *m_operation; 226 lldb_private::Mutex m_operation_mutex; 227 228 // semaphores notified when Operation is ready to be processed and when 229 // the operation is complete. 230 sem_t m_operation_pending; 231 sem_t m_operation_done; 232 233 struct OperationArgs 234 { 235 OperationArgs(ProcessMonitor *monitor); 236 237 ~OperationArgs(); 238 239 ProcessMonitor *m_monitor; // The monitor performing the attach. 240 sem_t m_semaphore; // Posted to once operation complete. 241 lldb_private::Error m_error; // Set if process operation failed. 242 }; 243 244 /// @class LauchArgs 245 /// 246 /// @brief Simple structure to pass data to the thread responsible for 247 /// launching a child process. 248 struct LaunchArgs : OperationArgs 249 { 250 LaunchArgs(ProcessMonitor *monitor, 251 lldb_private::Module *module, 252 char const **argv, 253 char const **envp, 254 const lldb_private::FileSpec &stdin_file_spec, 255 const lldb_private::FileSpec &stdout_file_spec, 256 const lldb_private::FileSpec &stderr_file_spec, 257 const lldb_private::FileSpec &working_dir); 258 259 ~LaunchArgs(); 260 261 lldb_private::Module *m_module; // The executable image to launch. 262 char const **m_argv; // Process arguments. 263 char const **m_envp; // Process environment. 264 const lldb_private::FileSpec m_stdin_file_spec; // Redirect stdin or empty. 265 const lldb_private::FileSpec m_stdout_file_spec; // Redirect stdout or empty. 266 const lldb_private::FileSpec m_stderr_file_spec; // Redirect stderr or empty. 267 const lldb_private::FileSpec m_working_dir; // Working directory or empty. 268 }; 269 270 void 271 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error); 272 273 static void * 274 LaunchOpThread(void *arg); 275 276 static bool 277 Launch(LaunchArgs *args); 278 279 struct AttachArgs : OperationArgs 280 { 281 AttachArgs(ProcessMonitor *monitor, 282 lldb::pid_t pid); 283 284 ~AttachArgs(); 285 286 lldb::pid_t m_pid; // pid of the process to be attached. 287 }; 288 289 void 290 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error); 291 292 static void * 293 AttachOpThread(void *args); 294 295 static void 296 Attach(AttachArgs *args); 297 298 static void 299 ServeOperation(OperationArgs *args); 300 301 static bool 302 DupDescriptor(const lldb_private::FileSpec &file_spec, int fd, int flags); 303 304 static bool 305 MonitorCallback(void *callback_baton, 306 lldb::pid_t pid, bool exited, int signal, int status); 307 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 316 void 317 DoOperation(Operation *op); 318 319 /// Stops the child monitor thread. 320 void 321 StopMonitoringChildProcess(); 322 323 /// Stops the operation thread used to attach/launch a process. 324 void 325 StopOpThread(); 326 }; 327 328 #endif // #ifndef liblldb_ProcessMonitor_H_ 329