Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +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> |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 15 | #include <signal.h> |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 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; |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 27 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 28 | } // End lldb_private namespace. |
| 29 | |
| 30 | class ProcessLinux; |
| 31 | class Operation; |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 32 | class ProcessPOSIX; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 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 | /// ProcessLinux instance by calling ProcessLinux::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. |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 52 | ProcessMonitor(ProcessPOSIX *process, |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 53 | lldb_private::Module *module, |
| 54 | char const *argv[], |
| 55 | char const *envp[], |
| 56 | const char *stdin_path, |
| 57 | const char *stdout_path, |
| 58 | const char *stderr_path, |
| 59 | lldb_private::Error &error); |
| 60 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 61 | ProcessMonitor(ProcessPOSIX *process, |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 62 | lldb::pid_t pid, |
| 63 | lldb_private::Error &error); |
| 64 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 65 | ~ProcessMonitor(); |
| 66 | |
| 67 | /// Provides the process number of debugee. |
| 68 | lldb::pid_t |
| 69 | GetPID() const { return m_pid; } |
| 70 | |
| 71 | /// Returns the process associated with this ProcessMonitor. |
| 72 | ProcessLinux & |
| 73 | GetProcess() { return *m_process; } |
| 74 | |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 75 | /// Returns a file descriptor to the controlling terminal of the inferior |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 76 | /// process. |
| 77 | /// |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 78 | /// Reads from this file descriptor yield both the standard output and |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 79 | /// standard error of this debugee. Even if stderr and stdout were |
| 80 | /// redirected on launch it may still happen that data is available on this |
| 81 | /// descriptor (if the inferior process opens /dev/tty, for example). |
| 82 | /// |
| 83 | /// If this monitor was attached to an existing process this method returns |
| 84 | /// -1. |
| 85 | int |
| 86 | GetTerminalFD() const { return m_terminal_fd; } |
| 87 | |
| 88 | /// Reads @p size bytes from address @vm_adder in the inferior process |
| 89 | /// address space. |
| 90 | /// |
| 91 | /// This method is provided to implement Process::DoReadMemory. |
| 92 | size_t |
| 93 | ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, |
| 94 | lldb_private::Error &error); |
| 95 | |
| 96 | /// Writes @p size bytes from address @p vm_adder in the inferior process |
| 97 | /// address space. |
| 98 | /// |
| 99 | /// This method is provided to implement Process::DoWriteMemory. |
| 100 | size_t |
| 101 | WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, |
| 102 | lldb_private::Error &error); |
| 103 | |
| 104 | /// Reads the contents from the register identified by the given (architecture |
| 105 | /// dependent) offset. |
| 106 | /// |
| 107 | /// This method is provided for use by RegisterContextLinux derivatives. |
| 108 | bool |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 109 | ReadRegisterValue(unsigned offset, unsigned size, lldb_private::RegisterValue &value); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 110 | |
| 111 | /// Writes the given value to the register identified by the given |
| 112 | /// (architecture dependent) offset. |
| 113 | /// |
| 114 | /// This method is provided for use by RegisterContextLinux derivatives. |
| 115 | bool |
Johnny Chen | 13e8e1c | 2011-05-13 21:29:50 +0000 | [diff] [blame] | 116 | WriteRegisterValue(unsigned offset, const lldb_private::RegisterValue &value); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 117 | |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 118 | /// Reads all general purpose registers into the specified buffer. |
| 119 | bool |
| 120 | ReadGPR(void *buf); |
| 121 | |
| 122 | /// Reads all floating point registers into the specified buffer. |
| 123 | bool |
| 124 | ReadFPR(void *buf); |
| 125 | |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 126 | /// Writes all general purpose registers into the specified buffer. |
| 127 | bool |
| 128 | WriteGPR(void *buf); |
| 129 | |
| 130 | /// Writes all floating point registers into the specified buffer. |
| 131 | bool |
| 132 | WriteFPR(void *buf); |
| 133 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 134 | /// Writes a siginfo_t structure corresponding to the given thread ID to the |
| 135 | /// memory region pointed to by @p siginfo. |
| 136 | bool |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 137 | GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 138 | |
| 139 | /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) |
| 140 | /// corresponding to the given thread IDto the memory pointed to by @p |
| 141 | /// message. |
| 142 | bool |
| 143 | GetEventMessage(lldb::tid_t tid, unsigned long *message); |
| 144 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 145 | /// Resumes the given thread. If @p signo is anything but |
| 146 | /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 147 | bool |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 148 | Resume(lldb::tid_t tid, uint32_t signo); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 149 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 150 | /// Single steps the given thread. If @p signo is anything but |
| 151 | /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 152 | bool |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 153 | SingleStep(lldb::tid_t tid, uint32_t signo); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 154 | |
| 155 | /// Sends the inferior process a PTRACE_KILL signal. The inferior will |
| 156 | /// still exists and can be interrogated. Once resumed it will exit as |
| 157 | /// though it received a SIGKILL. |
| 158 | bool |
| 159 | BringProcessIntoLimbo(); |
| 160 | |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 161 | lldb_private::Error |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 162 | Detach(); |
| 163 | |
| 164 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 165 | private: |
| 166 | ProcessLinux *m_process; |
| 167 | |
| 168 | lldb::thread_t m_operation_thread; |
| 169 | lldb::pid_t m_pid; |
| 170 | int m_terminal_fd; |
| 171 | |
Stephen Wilson | f62308c | 2011-01-15 00:11:28 +0000 | [diff] [blame] | 172 | lldb::thread_t m_monitor_thread; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 173 | |
| 174 | lldb_private::Mutex m_server_mutex; |
| 175 | int m_client_fd; |
| 176 | int m_server_fd; |
| 177 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 178 | struct OperationArgs |
| 179 | { |
| 180 | OperationArgs(ProcessMonitor *monitor); |
| 181 | |
| 182 | ~OperationArgs(); |
| 183 | |
| 184 | ProcessMonitor *m_monitor; // The monitor performing the attach. |
| 185 | sem_t m_semaphore; // Posted to once operation complete. |
| 186 | lldb_private::Error m_error; // Set if process operation failed. |
| 187 | }; |
| 188 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 189 | /// @class LauchArgs |
| 190 | /// |
| 191 | /// @brief Simple structure to pass data to the thread responsible for |
| 192 | /// launching a child process. |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 193 | struct LaunchArgs : OperationArgs |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 194 | { |
| 195 | LaunchArgs(ProcessMonitor *monitor, |
| 196 | lldb_private::Module *module, |
| 197 | char const **argv, |
| 198 | char const **envp, |
| 199 | const char *stdin_path, |
| 200 | const char *stdout_path, |
| 201 | const char *stderr_path); |
| 202 | |
| 203 | ~LaunchArgs(); |
| 204 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 205 | lldb_private::Module *m_module; // The executable image to launch. |
| 206 | char const **m_argv; // Process arguments. |
| 207 | char const **m_envp; // Process environment. |
| 208 | const char *m_stdin_path; // Redirect stdin or NULL. |
| 209 | const char *m_stdout_path; // Redirect stdout or NULL. |
| 210 | const char *m_stderr_path; // Redirect stderr or NULL. |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | void |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 214 | StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 215 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 216 | static void * |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 217 | LaunchOpThread(void *arg); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 218 | |
| 219 | static bool |
| 220 | Launch(LaunchArgs *args); |
| 221 | |
| 222 | bool |
| 223 | EnableIPC(); |
| 224 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 225 | struct AttachArgs : OperationArgs |
| 226 | { |
| 227 | AttachArgs(ProcessMonitor *monitor, |
| 228 | lldb::pid_t pid); |
| 229 | |
| 230 | ~AttachArgs(); |
| 231 | |
| 232 | lldb::pid_t m_pid; // pid of the process to be attached. |
| 233 | }; |
| 234 | |
| 235 | void |
| 236 | StartAttachOpThread(AttachArgs *args, lldb_private::Error &error); |
| 237 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 238 | static void * |
| 239 | AttachOpThread(void *args); |
| 240 | |
| 241 | static bool |
| 242 | Attach(AttachArgs *args); |
| 243 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 244 | static void |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 245 | ServeOperation(OperationArgs *args); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 246 | |
| 247 | static bool |
| 248 | DupDescriptor(const char *path, int fd, int flags); |
| 249 | |
| 250 | static bool |
| 251 | MonitorCallback(void *callback_baton, |
Peter Collingbourne | 2c67b9a | 2011-11-21 00:10:19 +0000 | [diff] [blame] | 252 | lldb::pid_t pid, bool exited, int signal, int status); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 253 | |
| 254 | static ProcessMessage |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 255 | MonitorSIGTRAP(ProcessMonitor *monitor, |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 256 | const siginfo_t *info, lldb::pid_t pid); |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 257 | |
| 258 | static ProcessMessage |
| 259 | MonitorSignal(ProcessMonitor *monitor, |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 260 | const siginfo_t *info, lldb::pid_t pid); |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 261 | |
| 262 | static ProcessMessage::CrashReason |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 263 | GetCrashReasonForSIGSEGV(const siginfo_t *info); |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 264 | |
| 265 | static ProcessMessage::CrashReason |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 266 | GetCrashReasonForSIGILL(const siginfo_t *info); |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 267 | |
| 268 | static ProcessMessage::CrashReason |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 269 | GetCrashReasonForSIGFPE(const siginfo_t *info); |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 270 | |
| 271 | static ProcessMessage::CrashReason |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 272 | GetCrashReasonForSIGBUS(const siginfo_t *info); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 273 | |
| 274 | void |
| 275 | DoOperation(Operation *op); |
Stephen Wilson | 9212d7f | 2011-01-04 21:40:25 +0000 | [diff] [blame] | 276 | |
| 277 | /// Stops the child monitor thread. |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 278 | void |
| 279 | StopMonitoringChildProcess(); |
| 280 | |
| 281 | void |
| 282 | StopMonitor(); |
| 283 | |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 284 | /// Stops the operation thread used to attach/launch a process. |
| 285 | void |
| 286 | StopOpThread(); |
| 287 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 288 | void |
| 289 | CloseFD(int &fd); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 290 | }; |
| 291 | |
| 292 | #endif // #ifndef liblldb_ProcessMonitor_H_ |