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 |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame^] | 109 | ReadRegisterValue(lldb::tid_t tid, unsigned offset, |
| 110 | unsigned size, lldb_private::RegisterValue &value); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 111 | |
| 112 | /// Writes the given value to the register identified by the given |
| 113 | /// (architecture dependent) offset. |
| 114 | /// |
| 115 | /// This method is provided for use by RegisterContextLinux derivatives. |
| 116 | bool |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame^] | 117 | WriteRegisterValue(lldb::tid_t tid, unsigned offset, |
| 118 | const lldb_private::RegisterValue &value); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 119 | |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 120 | /// Reads all general purpose registers into the specified buffer. |
| 121 | bool |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame^] | 122 | ReadGPR(lldb::tid_t tid, void *buf); |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 123 | |
| 124 | /// Reads all floating point registers into the specified buffer. |
| 125 | bool |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame^] | 126 | ReadFPR(lldb::tid_t tid, void *buf); |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 127 | |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 128 | /// Writes all general purpose registers into the specified buffer. |
| 129 | bool |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame^] | 130 | WriteGPR(lldb::tid_t tid, void *buf); |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 131 | |
| 132 | /// Writes all floating point registers into the specified buffer. |
| 133 | bool |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame^] | 134 | WriteFPR(lldb::tid_t tid, void *buf); |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 135 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 136 | /// Writes a siginfo_t structure corresponding to the given thread ID to the |
| 137 | /// memory region pointed to by @p siginfo. |
| 138 | bool |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 139 | GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 140 | |
| 141 | /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) |
| 142 | /// corresponding to the given thread IDto the memory pointed to by @p |
| 143 | /// message. |
| 144 | bool |
| 145 | GetEventMessage(lldb::tid_t tid, unsigned long *message); |
| 146 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 147 | /// Resumes the given thread. If @p signo is anything but |
| 148 | /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 149 | bool |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 150 | Resume(lldb::tid_t tid, uint32_t signo); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 151 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 152 | /// Single steps the given thread. If @p signo is anything but |
| 153 | /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 154 | bool |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 155 | SingleStep(lldb::tid_t tid, uint32_t signo); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 156 | |
| 157 | /// Sends the inferior process a PTRACE_KILL signal. The inferior will |
| 158 | /// still exists and can be interrogated. Once resumed it will exit as |
| 159 | /// though it received a SIGKILL. |
| 160 | bool |
| 161 | BringProcessIntoLimbo(); |
| 162 | |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 163 | lldb_private::Error |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 164 | Detach(); |
| 165 | |
| 166 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 167 | private: |
| 168 | ProcessLinux *m_process; |
| 169 | |
| 170 | lldb::thread_t m_operation_thread; |
| 171 | lldb::pid_t m_pid; |
| 172 | int m_terminal_fd; |
| 173 | |
Stephen Wilson | f62308c | 2011-01-15 00:11:28 +0000 | [diff] [blame] | 174 | lldb::thread_t m_monitor_thread; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 175 | |
| 176 | lldb_private::Mutex m_server_mutex; |
| 177 | int m_client_fd; |
| 178 | int m_server_fd; |
| 179 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 180 | struct OperationArgs |
| 181 | { |
| 182 | OperationArgs(ProcessMonitor *monitor); |
| 183 | |
| 184 | ~OperationArgs(); |
| 185 | |
| 186 | ProcessMonitor *m_monitor; // The monitor performing the attach. |
| 187 | sem_t m_semaphore; // Posted to once operation complete. |
| 188 | lldb_private::Error m_error; // Set if process operation failed. |
| 189 | }; |
| 190 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 191 | /// @class LauchArgs |
| 192 | /// |
| 193 | /// @brief Simple structure to pass data to the thread responsible for |
| 194 | /// launching a child process. |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 195 | struct LaunchArgs : OperationArgs |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 196 | { |
| 197 | LaunchArgs(ProcessMonitor *monitor, |
| 198 | lldb_private::Module *module, |
| 199 | char const **argv, |
| 200 | char const **envp, |
| 201 | const char *stdin_path, |
| 202 | const char *stdout_path, |
| 203 | const char *stderr_path); |
| 204 | |
| 205 | ~LaunchArgs(); |
| 206 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 207 | lldb_private::Module *m_module; // The executable image to launch. |
| 208 | char const **m_argv; // Process arguments. |
| 209 | char const **m_envp; // Process environment. |
| 210 | const char *m_stdin_path; // Redirect stdin or NULL. |
| 211 | const char *m_stdout_path; // Redirect stdout or NULL. |
| 212 | const char *m_stderr_path; // Redirect stderr or NULL. |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | void |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 216 | StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 217 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 218 | static void * |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 219 | LaunchOpThread(void *arg); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 220 | |
| 221 | static bool |
| 222 | Launch(LaunchArgs *args); |
| 223 | |
| 224 | bool |
| 225 | EnableIPC(); |
| 226 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 227 | struct AttachArgs : OperationArgs |
| 228 | { |
| 229 | AttachArgs(ProcessMonitor *monitor, |
| 230 | lldb::pid_t pid); |
| 231 | |
| 232 | ~AttachArgs(); |
| 233 | |
| 234 | lldb::pid_t m_pid; // pid of the process to be attached. |
| 235 | }; |
| 236 | |
| 237 | void |
| 238 | StartAttachOpThread(AttachArgs *args, lldb_private::Error &error); |
| 239 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 240 | static void * |
| 241 | AttachOpThread(void *args); |
| 242 | |
| 243 | static bool |
| 244 | Attach(AttachArgs *args); |
| 245 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 246 | static void |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 247 | ServeOperation(OperationArgs *args); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 248 | |
| 249 | static bool |
| 250 | DupDescriptor(const char *path, int fd, int flags); |
| 251 | |
| 252 | static bool |
| 253 | MonitorCallback(void *callback_baton, |
Peter Collingbourne | 2c67b9a | 2011-11-21 00:10:19 +0000 | [diff] [blame] | 254 | lldb::pid_t pid, bool exited, int signal, int status); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 255 | |
| 256 | static ProcessMessage |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 257 | MonitorSIGTRAP(ProcessMonitor *monitor, |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 258 | const siginfo_t *info, lldb::pid_t pid); |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 259 | |
| 260 | static ProcessMessage |
| 261 | MonitorSignal(ProcessMonitor *monitor, |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 262 | const siginfo_t *info, lldb::pid_t pid); |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 263 | |
| 264 | static ProcessMessage::CrashReason |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 265 | GetCrashReasonForSIGSEGV(const siginfo_t *info); |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 266 | |
| 267 | static ProcessMessage::CrashReason |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 268 | GetCrashReasonForSIGILL(const siginfo_t *info); |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 269 | |
| 270 | static ProcessMessage::CrashReason |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 271 | GetCrashReasonForSIGFPE(const siginfo_t *info); |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 272 | |
| 273 | static ProcessMessage::CrashReason |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 274 | GetCrashReasonForSIGBUS(const siginfo_t *info); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 275 | |
| 276 | void |
| 277 | DoOperation(Operation *op); |
Stephen Wilson | 9212d7f | 2011-01-04 21:40:25 +0000 | [diff] [blame] | 278 | |
| 279 | /// Stops the child monitor thread. |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 280 | void |
| 281 | StopMonitoringChildProcess(); |
| 282 | |
| 283 | void |
| 284 | StopMonitor(); |
| 285 | |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 286 | /// Stops the operation thread used to attach/launch a process. |
| 287 | void |
| 288 | StopOpThread(); |
| 289 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 290 | void |
| 291 | CloseFD(int &fd); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 292 | }; |
| 293 | |
| 294 | #endif // #ifndef liblldb_ProcessMonitor_H_ |