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