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