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> |
| 15 | |
| 16 | // C++ Includes |
| 17 | // Other libraries and framework includes |
| 18 | #include "lldb/lldb-types.h" |
| 19 | #include "lldb/Host/Mutex.h" |
| 20 | |
| 21 | namespace lldb_private |
| 22 | { |
| 23 | class Error; |
| 24 | class Module; |
| 25 | class Scalar; |
| 26 | } // End lldb_private namespace. |
| 27 | |
| 28 | class ProcessLinux; |
| 29 | class Operation; |
| 30 | |
| 31 | /// @class ProcessMonitor |
| 32 | /// @brief Manages communication with the inferior (debugee) process. |
| 33 | /// |
| 34 | /// Upon construction, this class prepares and launches an inferior process for |
| 35 | /// debugging. |
| 36 | /// |
| 37 | /// Changes in the inferior process state are propagated to the associated |
| 38 | /// ProcessLinux instance by calling ProcessLinux::SendMessage with the |
| 39 | /// appropriate ProcessMessage events. |
| 40 | /// |
| 41 | /// A purposely minimal set of operations are provided to interrogate and change |
| 42 | /// the inferior process state. |
| 43 | class ProcessMonitor |
| 44 | { |
| 45 | public: |
| 46 | |
| 47 | /// Launches an inferior process ready for debugging. Forms the |
| 48 | /// implementation of Process::DoLaunch. |
| 49 | ProcessMonitor(ProcessLinux *process, |
| 50 | lldb_private::Module *module, |
| 51 | char const *argv[], |
| 52 | char const *envp[], |
| 53 | const char *stdin_path, |
| 54 | const char *stdout_path, |
| 55 | const char *stderr_path, |
| 56 | lldb_private::Error &error); |
| 57 | |
| 58 | ~ProcessMonitor(); |
| 59 | |
| 60 | /// Provides the process number of debugee. |
| 61 | lldb::pid_t |
| 62 | GetPID() const { return m_pid; } |
| 63 | |
| 64 | /// Returns the process associated with this ProcessMonitor. |
| 65 | ProcessLinux & |
| 66 | GetProcess() { return *m_process; } |
| 67 | |
| 68 | /// Returns a file descriptor to the controling terminal of the inferior |
| 69 | /// process. |
| 70 | /// |
| 71 | /// Reads from this file descriptor yeild both the standard output and |
| 72 | /// standard error of this debugee. Even if stderr and stdout were |
| 73 | /// redirected on launch it may still happen that data is available on this |
| 74 | /// descriptor (if the inferior process opens /dev/tty, for example). |
| 75 | /// |
| 76 | /// If this monitor was attached to an existing process this method returns |
| 77 | /// -1. |
| 78 | int |
| 79 | GetTerminalFD() const { return m_terminal_fd; } |
| 80 | |
| 81 | /// Reads @p size bytes from address @vm_adder in the inferior process |
| 82 | /// address space. |
| 83 | /// |
| 84 | /// This method is provided to implement Process::DoReadMemory. |
| 85 | size_t |
| 86 | ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, |
| 87 | lldb_private::Error &error); |
| 88 | |
| 89 | /// Writes @p size bytes from address @p vm_adder in the inferior process |
| 90 | /// address space. |
| 91 | /// |
| 92 | /// This method is provided to implement Process::DoWriteMemory. |
| 93 | size_t |
| 94 | WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, |
| 95 | lldb_private::Error &error); |
| 96 | |
| 97 | /// Reads the contents from the register identified by the given (architecture |
| 98 | /// dependent) offset. |
| 99 | /// |
| 100 | /// This method is provided for use by RegisterContextLinux derivatives. |
| 101 | bool |
| 102 | ReadRegisterValue(unsigned offset, lldb_private::Scalar &value); |
| 103 | |
| 104 | /// Writes the given value to the register identified by the given |
| 105 | /// (architecture dependent) offset. |
| 106 | /// |
| 107 | /// This method is provided for use by RegisterContextLinux derivatives. |
| 108 | bool |
| 109 | WriteRegisterValue(unsigned offset, const lldb_private::Scalar &value); |
| 110 | |
| 111 | /// Writes a siginfo_t structure corresponding to the given thread ID to the |
| 112 | /// memory region pointed to by @p siginfo. |
| 113 | bool |
| 114 | GetSignalInfo(lldb::tid_t tid, void *siginfo); |
| 115 | |
| 116 | /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) |
| 117 | /// corresponding to the given thread IDto the memory pointed to by @p |
| 118 | /// message. |
| 119 | bool |
| 120 | GetEventMessage(lldb::tid_t tid, unsigned long *message); |
| 121 | |
| 122 | /// Resumes the given thread. |
| 123 | bool |
| 124 | Resume(lldb::tid_t tid); |
| 125 | |
| 126 | /// Single steps the given thread. |
| 127 | bool |
| 128 | SingleStep(lldb::tid_t tid); |
| 129 | |
| 130 | /// Sends the inferior process a PTRACE_KILL signal. The inferior will |
| 131 | /// still exists and can be interrogated. Once resumed it will exit as |
| 132 | /// though it received a SIGKILL. |
| 133 | bool |
| 134 | BringProcessIntoLimbo(); |
| 135 | |
| 136 | private: |
| 137 | ProcessLinux *m_process; |
| 138 | |
| 139 | lldb::thread_t m_operation_thread; |
| 140 | lldb::pid_t m_pid; |
| 141 | int m_terminal_fd; |
| 142 | |
| 143 | uint32_t m_monitor_handle; |
| 144 | |
| 145 | lldb_private::Mutex m_server_mutex; |
| 146 | int m_client_fd; |
| 147 | int m_server_fd; |
| 148 | |
| 149 | /// @class LauchArgs |
| 150 | /// |
| 151 | /// @brief Simple structure to pass data to the thread responsible for |
| 152 | /// launching a child process. |
| 153 | struct LaunchArgs |
| 154 | { |
| 155 | LaunchArgs(ProcessMonitor *monitor, |
| 156 | lldb_private::Module *module, |
| 157 | char const **argv, |
| 158 | char const **envp, |
| 159 | const char *stdin_path, |
| 160 | const char *stdout_path, |
| 161 | const char *stderr_path); |
| 162 | |
| 163 | ~LaunchArgs(); |
| 164 | |
| 165 | ProcessMonitor *m_monitor; // The monitor performing the launch. |
| 166 | lldb_private::Module *m_module; // The executable image to launch. |
| 167 | char const **m_argv; // Process arguments. |
| 168 | char const **m_envp; // Process environment. |
| 169 | const char *m_stdin_path; // Redirect stdin or NULL. |
| 170 | const char *m_stdout_path; // Redirect stdout or NULL. |
| 171 | const char *m_stderr_path; // Redirect stderr or NULL. |
| 172 | sem_t m_semaphore; // Posted to once launch complete. |
| 173 | lldb_private::Error m_error; // Set if process launch failed. |
| 174 | }; |
| 175 | |
| 176 | void |
| 177 | StartOperationThread(LaunchArgs *args, lldb_private::Error &error); |
| 178 | |
| 179 | void |
| 180 | StopOperationThread(); |
| 181 | |
| 182 | static void * |
| 183 | OperationThread(void *arg); |
| 184 | |
| 185 | static bool |
| 186 | Launch(LaunchArgs *args); |
| 187 | |
| 188 | bool |
| 189 | EnableIPC(); |
| 190 | |
| 191 | static void |
| 192 | ServeOperation(ProcessMonitor *monitor); |
| 193 | |
| 194 | static bool |
| 195 | DupDescriptor(const char *path, int fd, int flags); |
| 196 | |
| 197 | static bool |
| 198 | MonitorCallback(void *callback_baton, |
| 199 | lldb::pid_t pid, int signal, int status); |
| 200 | |
| 201 | static ProcessMessage |
| 202 | MonitorSIGTRAP(ProcessMonitor *monitor, lldb::pid_t pid); |
| 203 | |
| 204 | void |
| 205 | DoOperation(Operation *op); |
| 206 | }; |
| 207 | |
| 208 | #endif // #ifndef liblldb_ProcessMonitor_H_ |