Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 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 | |
| 22 | namespace lldb_private |
| 23 | { |
| 24 | class Error; |
| 25 | class Module; |
| 26 | class Scalar; |
| 27 | } // End lldb_private namespace. |
| 28 | |
| 29 | class ProcessFreeBSD; |
| 30 | class 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. |
| 44 | class ProcessMonitor |
| 45 | { |
| 46 | public: |
| 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, |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 57 | const char *working_dir, |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 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. |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 107 | /// FIXME: The FreeBSD implementation of this function should use tid in order |
| 108 | /// to enable support for debugging threaded programs. |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 109 | bool |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 110 | ReadRegisterValue(lldb::tid_t tid, unsigned offset, |
| 111 | unsigned size, lldb_private::RegisterValue &value); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 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. |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 117 | /// FIXME: The FreeBSD implementation of this function should use tid in order |
| 118 | /// to enable support for debugging threaded programs. |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 119 | bool |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 120 | WriteRegisterValue(lldb::tid_t tid, unsigned offset, |
| 121 | const lldb_private::RegisterValue &value); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 122 | |
| 123 | /// Reads all general purpose registers into the specified buffer. |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 124 | /// FIXME: The FreeBSD implementation of this function should use tid in order |
| 125 | /// to enable support for debugging threaded programs. |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 126 | bool |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 127 | ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 128 | |
Matt Kopec | c6672c8 | 2013-03-15 20:00:39 +0000 | [diff] [blame] | 129 | /// Reads all floating point registers into the specified buffer. |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 130 | /// FIXME: The FreeBSD implementation of this function should use tid in order |
| 131 | /// to enable support for debugging threaded programs. |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 132 | bool |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 133 | ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 134 | |
Ashok Thirumurthi | 0f3b9b8 | 2013-05-01 20:38:19 +0000 | [diff] [blame^] | 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 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 143 | /// Writes all general purpose registers into the specified buffer. |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 144 | /// FIXME: The FreeBSD implementation of this function should use tid in order |
| 145 | /// to enable support for debugging threaded programs. |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 146 | bool |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 147 | WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 148 | |
Matt Kopec | c6672c8 | 2013-03-15 20:00:39 +0000 | [diff] [blame] | 149 | /// Writes all floating point registers into the specified buffer. |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 150 | /// FIXME: The FreeBSD implementation of this function should use tid in order |
| 151 | /// to enable support for debugging threaded programs. |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 152 | bool |
Ashok Thirumurthi | 0f3b9b8 | 2013-05-01 20:38:19 +0000 | [diff] [blame^] | 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); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 162 | |
| 163 | /// Writes a siginfo_t structure corresponding to the given thread ID to the |
| 164 | /// memory region pointed to by @p siginfo. |
| 165 | bool |
Ashok Thirumurthi | 0f3b9b8 | 2013-05-01 20:38:19 +0000 | [diff] [blame^] | 166 | GetSignalInfo(lldb::tid_t tid, void *siginfo, int &error_no); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 167 | |
| 168 | /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) |
| 169 | /// corresponding to the given thread IDto the memory pointed to by @p |
| 170 | /// message. |
| 171 | bool |
| 172 | GetEventMessage(lldb::tid_t tid, unsigned long *message); |
| 173 | |
| 174 | /// Resumes the given thread. If @p signo is anything but |
| 175 | /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. |
| 176 | bool |
| 177 | Resume(lldb::tid_t tid, uint32_t signo); |
| 178 | |
| 179 | /// Single steps the given thread. If @p signo is anything but |
| 180 | /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. |
| 181 | bool |
| 182 | SingleStep(lldb::tid_t tid, uint32_t signo); |
| 183 | |
| 184 | /// Sends the inferior process a PTRACE_KILL signal. The inferior will |
| 185 | /// still exists and can be interrogated. Once resumed it will exit as |
| 186 | /// though it received a SIGKILL. |
| 187 | bool |
| 188 | BringProcessIntoLimbo(); |
| 189 | |
| 190 | lldb_private::Error |
| 191 | Detach(); |
| 192 | |
| 193 | |
| 194 | private: |
| 195 | ProcessFreeBSD *m_process; |
| 196 | |
| 197 | lldb::thread_t m_operation_thread; |
| 198 | lldb::thread_t m_monitor_thread; |
| 199 | lldb::pid_t m_pid; |
| 200 | |
| 201 | |
| 202 | lldb_private::Mutex m_server_mutex; |
| 203 | int m_terminal_fd; |
| 204 | int m_client_fd; |
| 205 | int m_server_fd; |
| 206 | |
| 207 | struct OperationArgs |
| 208 | { |
| 209 | OperationArgs(ProcessMonitor *monitor); |
| 210 | |
| 211 | ~OperationArgs(); |
| 212 | |
| 213 | ProcessMonitor *m_monitor; // The monitor performing the attach. |
| 214 | sem_t m_semaphore; // Posted to once operation complete. |
| 215 | lldb_private::Error m_error; // Set if process operation failed. |
| 216 | }; |
| 217 | |
| 218 | /// @class LauchArgs |
| 219 | /// |
| 220 | /// @brief Simple structure to pass data to the thread responsible for |
| 221 | /// launching a child process. |
| 222 | struct LaunchArgs : OperationArgs |
| 223 | { |
| 224 | LaunchArgs(ProcessMonitor *monitor, |
| 225 | lldb_private::Module *module, |
| 226 | char const **argv, |
| 227 | char const **envp, |
| 228 | const char *stdin_path, |
| 229 | const char *stdout_path, |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 230 | const char *stderr_path, |
| 231 | const char *working_dir); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 232 | |
| 233 | ~LaunchArgs(); |
| 234 | |
| 235 | lldb_private::Module *m_module; // The executable image to launch. |
| 236 | char const **m_argv; // Process arguments. |
| 237 | char const **m_envp; // Process environment. |
| 238 | const char *m_stdin_path; // Redirect stdin or NULL. |
| 239 | const char *m_stdout_path; // Redirect stdout or NULL. |
| 240 | const char *m_stderr_path; // Redirect stderr or NULL. |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 241 | const char *m_working_dir; // Working directory or NULL. |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | void |
| 245 | StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error); |
| 246 | |
| 247 | void |
| 248 | StopLaunchOpThread(); |
| 249 | |
| 250 | static void * |
| 251 | LaunchOpThread(void *arg); |
| 252 | |
| 253 | static bool |
| 254 | Launch(LaunchArgs *args); |
| 255 | |
| 256 | bool |
| 257 | EnableIPC(); |
| 258 | |
| 259 | struct AttachArgs : OperationArgs |
| 260 | { |
| 261 | AttachArgs(ProcessMonitor *monitor, |
| 262 | lldb::pid_t pid); |
| 263 | |
| 264 | ~AttachArgs(); |
| 265 | |
| 266 | lldb::pid_t m_pid; // pid of the process to be attached. |
| 267 | }; |
| 268 | |
| 269 | void |
| 270 | StartAttachOpThread(AttachArgs *args, lldb_private::Error &error); |
| 271 | |
| 272 | void |
| 273 | StopAttachOpThread(); |
| 274 | |
| 275 | static void * |
| 276 | AttachOpThread(void *args); |
| 277 | |
| 278 | static bool |
| 279 | Attach(AttachArgs *args); |
| 280 | |
| 281 | static void |
| 282 | ServeOperation(OperationArgs *args); |
| 283 | |
| 284 | static bool |
| 285 | DupDescriptor(const char *path, int fd, int flags); |
| 286 | |
| 287 | static bool |
| 288 | MonitorCallback(void *callback_baton, |
| 289 | lldb::pid_t pid, bool exited, int signal, int status); |
| 290 | |
| 291 | static ProcessMessage |
| 292 | MonitorSIGTRAP(ProcessMonitor *monitor, |
| 293 | const siginfo_t *info, lldb::pid_t pid); |
| 294 | |
| 295 | static ProcessMessage |
| 296 | MonitorSignal(ProcessMonitor *monitor, |
| 297 | const siginfo_t *info, lldb::pid_t pid); |
| 298 | |
| 299 | static ProcessMessage::CrashReason |
| 300 | GetCrashReasonForSIGSEGV(const siginfo_t *info); |
| 301 | |
| 302 | static ProcessMessage::CrashReason |
| 303 | GetCrashReasonForSIGILL(const siginfo_t *info); |
| 304 | |
| 305 | static ProcessMessage::CrashReason |
| 306 | GetCrashReasonForSIGFPE(const siginfo_t *info); |
| 307 | |
| 308 | static ProcessMessage::CrashReason |
| 309 | GetCrashReasonForSIGBUS(const siginfo_t *info); |
| 310 | |
| 311 | void |
| 312 | DoOperation(Operation *op); |
| 313 | |
| 314 | /// Stops the child monitor thread. |
| 315 | void |
| 316 | StopMonitoringChildProcess(); |
| 317 | |
| 318 | void |
| 319 | StopMonitor(); |
| 320 | |
| 321 | void |
| 322 | CloseFD(int &fd); |
| 323 | }; |
| 324 | |
| 325 | #endif // #ifndef liblldb_ProcessMonitor_H_ |