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