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