Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1 | //===-- ProcessMonitor.cpp ------------------------------------ -*- 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 | |
Daniel Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 12 | // C Includes |
| 13 | #include <errno.h> |
| 14 | #include <poll.h> |
| 15 | #include <string.h> |
Daniel Malea | a85e6b6 | 2012-12-07 22:21:08 +0000 | [diff] [blame] | 16 | #include <stdint.h> |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 17 | #include <unistd.h> |
| 18 | #include <sys/ptrace.h> |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <sys/wait.h> |
| 22 | |
| 23 | // C++ Includes |
| 24 | // Other libraries and framework includes |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 25 | #include "lldb/Core/Debugger.h" |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 26 | #include "lldb/Core/Error.h" |
Johnny Chen | 13e8e1c | 2011-05-13 21:29:50 +0000 | [diff] [blame] | 27 | #include "lldb/Core/RegisterValue.h" |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 28 | #include "lldb/Core/Scalar.h" |
| 29 | #include "lldb/Host/Host.h" |
| 30 | #include "lldb/Target/Thread.h" |
| 31 | #include "lldb/Target/RegisterContext.h" |
| 32 | #include "lldb/Utility/PseudoTerminal.h" |
| 33 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 34 | #include "POSIXThread.h" |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 35 | #include "ProcessLinux.h" |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 36 | #include "ProcessPOSIXLog.h" |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 37 | #include "ProcessMonitor.h" |
| 38 | |
| 39 | |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 40 | #define DEBUG_PTRACE_MAXBYTES 20 |
| 41 | |
Matt Kopec | 58c0b96 | 2013-03-20 20:34:35 +0000 | [diff] [blame^] | 42 | // Support ptrace extensions even when compiled without required kernel support |
| 43 | #ifndef PTRACE_GETREGSET |
| 44 | #define PTRACE_GETREGSET 0x4204 |
| 45 | #endif |
| 46 | #ifndef PTRACE_SETREGSET |
| 47 | #define PTRACE_SETREGSET 0x4205 |
| 48 | #endif |
| 49 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 50 | using namespace lldb_private; |
| 51 | |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 52 | // FIXME: this code is host-dependent with respect to types and |
| 53 | // endianness and needs to be fixed. For example, lldb::addr_t is |
| 54 | // hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires |
| 55 | // 32-bit pointer arguments. This code uses casts to work around the |
| 56 | // problem. |
| 57 | |
| 58 | // We disable the tracing of ptrace calls for integration builds to |
| 59 | // avoid the additional indirection and checks. |
| 60 | #ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION |
| 61 | |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 62 | static void |
| 63 | DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count) |
| 64 | { |
| 65 | uint8_t *ptr = (uint8_t *)bytes; |
| 66 | const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); |
| 67 | for(uint32_t i=0; i<loop_count; i++) |
| 68 | { |
| 69 | s.Printf ("[%x]", *ptr); |
| 70 | ptr++; |
| 71 | } |
| 72 | } |
| 73 | |
Matt Kopec | 58c0b96 | 2013-03-20 20:34:35 +0000 | [diff] [blame^] | 74 | static void PtraceDisplayBytes(int &req, void *data, size_t data_size) |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 75 | { |
| 76 | StreamString buf; |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 77 | LogSP verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet ( |
| 78 | POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE)); |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 79 | |
| 80 | if (verbose_log) |
| 81 | { |
| 82 | switch(req) |
| 83 | { |
| 84 | case PTRACE_POKETEXT: |
| 85 | { |
| 86 | DisplayBytes(buf, &data, 8); |
| 87 | verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData()); |
| 88 | break; |
| 89 | } |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 90 | case PTRACE_POKEDATA: |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 91 | { |
| 92 | DisplayBytes(buf, &data, 8); |
| 93 | verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData()); |
| 94 | break; |
| 95 | } |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 96 | case PTRACE_POKEUSER: |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 97 | { |
| 98 | DisplayBytes(buf, &data, 8); |
| 99 | verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData()); |
| 100 | break; |
| 101 | } |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 102 | case PTRACE_SETREGS: |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 103 | { |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 104 | DisplayBytes(buf, data, data_size); |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 105 | verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData()); |
| 106 | break; |
| 107 | } |
| 108 | case PTRACE_SETFPREGS: |
| 109 | { |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 110 | DisplayBytes(buf, data, data_size); |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 111 | verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData()); |
| 112 | break; |
| 113 | } |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 114 | case PTRACE_SETSIGINFO: |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 115 | { |
| 116 | DisplayBytes(buf, data, sizeof(siginfo_t)); |
| 117 | verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData()); |
| 118 | break; |
| 119 | } |
Matt Kopec | 58c0b96 | 2013-03-20 20:34:35 +0000 | [diff] [blame^] | 120 | case PTRACE_SETREGSET: |
| 121 | { |
| 122 | // Extract iov_base from data, which is a pointer to the struct IOVEC |
| 123 | DisplayBytes(buf, *(void **)data, data_size); |
| 124 | verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData()); |
| 125 | break; |
| 126 | } |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 127 | default: |
| 128 | { |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 134 | // Wrapper for ptrace to catch errors and log calls. |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 135 | // Note that ptrace sets errno on error because -1 is a valid result for PTRACE_PEEK* |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 136 | extern long |
Matt Kopec | 58c0b96 | 2013-03-20 20:34:35 +0000 | [diff] [blame^] | 137 | PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 138 | const char* reqName, const char* file, int line) |
| 139 | { |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 140 | long int result; |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 141 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 142 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE)); |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 143 | |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 144 | if (log) |
Matt Kopec | 58c0b96 | 2013-03-20 20:34:35 +0000 | [diff] [blame^] | 145 | log->Printf("ptrace(%s, %lu, %p, %p, %zu) called from file %s line %d", |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 146 | reqName, pid, addr, data, data_size, file, line); |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 147 | |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 148 | PtraceDisplayBytes(req, data, data_size); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 149 | |
| 150 | errno = 0; |
Matt Kopec | 58c0b96 | 2013-03-20 20:34:35 +0000 | [diff] [blame^] | 151 | if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) |
| 152 | result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data); |
| 153 | else |
| 154 | result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 155 | |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 156 | PtraceDisplayBytes(req, data, data_size); |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 157 | |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 158 | if (log && errno != 0) |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 159 | { |
| 160 | const char* str; |
| 161 | switch (errno) |
| 162 | { |
| 163 | case ESRCH: str = "ESRCH"; break; |
| 164 | case EINVAL: str = "EINVAL"; break; |
| 165 | case EBUSY: str = "EBUSY"; break; |
| 166 | case EPERM: str = "EPERM"; break; |
| 167 | default: str = "<unknown>"; |
| 168 | } |
| 169 | log->Printf("ptrace() failed; errno=%d (%s)", errno, str); |
| 170 | } |
| 171 | |
| 172 | return result; |
| 173 | } |
| 174 | |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 175 | // Wrapper for ptrace when logging is not required. |
| 176 | // Sets errno to 0 prior to calling ptrace. |
| 177 | extern long |
Matt Kopec | 58c0b96 | 2013-03-20 20:34:35 +0000 | [diff] [blame^] | 178 | PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size) |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 179 | { |
Matt Kopec | 58c0b96 | 2013-03-20 20:34:35 +0000 | [diff] [blame^] | 180 | long result = 0; |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 181 | errno = 0; |
Matt Kopec | 58c0b96 | 2013-03-20 20:34:35 +0000 | [diff] [blame^] | 182 | if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) |
| 183 | result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data); |
| 184 | else |
| 185 | result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data); |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 186 | return result; |
| 187 | } |
| 188 | |
| 189 | #define PTRACE(req, pid, addr, data, data_size) \ |
| 190 | PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__) |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 191 | #else |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 192 | PtraceWrapper((req), (pid), (addr), (data), (data_size)) |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 193 | #endif |
| 194 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 195 | //------------------------------------------------------------------------------ |
| 196 | // Static implementations of ProcessMonitor::ReadMemory and |
| 197 | // ProcessMonitor::WriteMemory. This enables mutual recursion between these |
| 198 | // functions without needed to go thru the thread funnel. |
| 199 | |
| 200 | static size_t |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 201 | DoReadMemory(lldb::pid_t pid, |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 202 | lldb::addr_t vm_addr, void *buf, size_t size, Error &error) |
| 203 | { |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 204 | // ptrace word size is determined by the host, not the child |
| 205 | static const unsigned word_size = sizeof(void*); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 206 | unsigned char *dst = static_cast<unsigned char*>(buf); |
| 207 | size_t bytes_read; |
| 208 | size_t remainder; |
| 209 | long data; |
| 210 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 211 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 212 | if (log) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 213 | ProcessPOSIXLog::IncNestLevel(); |
| 214 | if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 215 | log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__, |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 216 | pid, word_size, (void*)vm_addr, buf, size); |
| 217 | |
| 218 | assert(sizeof(data) >= word_size); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 219 | for (bytes_read = 0; bytes_read < size; bytes_read += remainder) |
| 220 | { |
| 221 | errno = 0; |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 222 | data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0); |
| 223 | if (errno) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 224 | { |
| 225 | error.SetErrorToErrno(); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 226 | if (log) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 227 | ProcessPOSIXLog::DecNestLevel(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 228 | return bytes_read; |
| 229 | } |
| 230 | |
| 231 | remainder = size - bytes_read; |
| 232 | remainder = remainder > word_size ? word_size : remainder; |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 233 | |
| 234 | // Copy the data into our buffer |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 235 | for (unsigned i = 0; i < remainder; ++i) |
| 236 | dst[i] = ((data >> i*8) & 0xFF); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 237 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 238 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 239 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || |
| 240 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && |
| 241 | size <= POSIX_LOG_MEMORY_SHORT_BYTES))) |
Daniel Malea | c63dddd | 2012-12-14 21:07:07 +0000 | [diff] [blame] | 242 | { |
| 243 | uintptr_t print_dst = 0; |
| 244 | // Format bytes from data by moving into print_dst for log output |
| 245 | for (unsigned i = 0; i < remainder; ++i) |
| 246 | print_dst |= (((data >> i*8) & 0xFF) << i*8); |
| 247 | log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, |
| 248 | (void*)vm_addr, print_dst, (unsigned long)data); |
| 249 | } |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 250 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 251 | vm_addr += word_size; |
| 252 | dst += word_size; |
| 253 | } |
| 254 | |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 255 | if (log) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 256 | ProcessPOSIXLog::DecNestLevel(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 257 | return bytes_read; |
| 258 | } |
| 259 | |
| 260 | static size_t |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 261 | DoWriteMemory(lldb::pid_t pid, |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 262 | lldb::addr_t vm_addr, const void *buf, size_t size, Error &error) |
| 263 | { |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 264 | // ptrace word size is determined by the host, not the child |
| 265 | static const unsigned word_size = sizeof(void*); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 266 | const unsigned char *src = static_cast<const unsigned char*>(buf); |
| 267 | size_t bytes_written = 0; |
| 268 | size_t remainder; |
| 269 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 270 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 271 | if (log) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 272 | ProcessPOSIXLog::IncNestLevel(); |
| 273 | if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 274 | log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__, |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 275 | pid, word_size, (void*)vm_addr, buf, size); |
| 276 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 277 | for (bytes_written = 0; bytes_written < size; bytes_written += remainder) |
| 278 | { |
| 279 | remainder = size - bytes_written; |
| 280 | remainder = remainder > word_size ? word_size : remainder; |
| 281 | |
| 282 | if (remainder == word_size) |
| 283 | { |
| 284 | unsigned long data = 0; |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 285 | assert(sizeof(data) >= word_size); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 286 | for (unsigned i = 0; i < word_size; ++i) |
| 287 | data |= (unsigned long)src[i] << i*8; |
| 288 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 289 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 290 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || |
| 291 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && |
| 292 | size <= POSIX_LOG_MEMORY_SHORT_BYTES))) |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 293 | log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, |
| 294 | (void*)vm_addr, *(unsigned long*)src, data); |
| 295 | |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 296 | if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 297 | { |
| 298 | error.SetErrorToErrno(); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 299 | if (log) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 300 | ProcessPOSIXLog::DecNestLevel(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 301 | return bytes_written; |
| 302 | } |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | unsigned char buff[8]; |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 307 | if (DoReadMemory(pid, vm_addr, |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 308 | buff, word_size, error) != word_size) |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 309 | { |
| 310 | if (log) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 311 | ProcessPOSIXLog::DecNestLevel(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 312 | return bytes_written; |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 313 | } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 314 | |
| 315 | memcpy(buff, src, remainder); |
| 316 | |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 317 | if (DoWriteMemory(pid, vm_addr, |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 318 | buff, word_size, error) != word_size) |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 319 | { |
| 320 | if (log) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 321 | ProcessPOSIXLog::DecNestLevel(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 322 | return bytes_written; |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 325 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 326 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || |
| 327 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && |
| 328 | size <= POSIX_LOG_MEMORY_SHORT_BYTES))) |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 329 | log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, |
| 330 | (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | vm_addr += word_size; |
| 334 | src += word_size; |
| 335 | } |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 336 | if (log) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 337 | ProcessPOSIXLog::DecNestLevel(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 338 | return bytes_written; |
| 339 | } |
| 340 | |
Stephen Wilson | 2697716 | 2011-03-23 02:14:42 +0000 | [diff] [blame] | 341 | // Simple helper function to ensure flags are enabled on the given file |
| 342 | // descriptor. |
| 343 | static bool |
| 344 | EnsureFDFlags(int fd, int flags, Error &error) |
| 345 | { |
| 346 | int status; |
| 347 | |
| 348 | if ((status = fcntl(fd, F_GETFL)) == -1) |
| 349 | { |
| 350 | error.SetErrorToErrno(); |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | if (fcntl(fd, F_SETFL, status | flags) == -1) |
| 355 | { |
| 356 | error.SetErrorToErrno(); |
| 357 | return false; |
| 358 | } |
| 359 | |
| 360 | return true; |
| 361 | } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 362 | |
| 363 | //------------------------------------------------------------------------------ |
| 364 | /// @class Operation |
| 365 | /// @brief Represents a ProcessMonitor operation. |
| 366 | /// |
| 367 | /// Under Linux, it is not possible to ptrace() from any other thread but the |
| 368 | /// one that spawned or attached to the process from the start. Therefore, when |
| 369 | /// a ProcessMonitor is asked to deliver or change the state of an inferior |
| 370 | /// process the operation must be "funneled" to a specific thread to perform the |
| 371 | /// task. The Operation class provides an abstract base for all services the |
| 372 | /// ProcessMonitor must perform via the single virtual function Execute, thus |
| 373 | /// encapsulating the code that needs to run in the privileged context. |
| 374 | class Operation |
| 375 | { |
| 376 | public: |
| 377 | virtual void Execute(ProcessMonitor *monitor) = 0; |
| 378 | }; |
| 379 | |
| 380 | //------------------------------------------------------------------------------ |
| 381 | /// @class ReadOperation |
| 382 | /// @brief Implements ProcessMonitor::ReadMemory. |
| 383 | class ReadOperation : public Operation |
| 384 | { |
| 385 | public: |
| 386 | ReadOperation(lldb::addr_t addr, void *buff, size_t size, |
| 387 | Error &error, size_t &result) |
| 388 | : m_addr(addr), m_buff(buff), m_size(size), |
| 389 | m_error(error), m_result(result) |
| 390 | { } |
| 391 | |
| 392 | void Execute(ProcessMonitor *monitor); |
| 393 | |
| 394 | private: |
| 395 | lldb::addr_t m_addr; |
| 396 | void *m_buff; |
| 397 | size_t m_size; |
| 398 | Error &m_error; |
| 399 | size_t &m_result; |
| 400 | }; |
| 401 | |
| 402 | void |
| 403 | ReadOperation::Execute(ProcessMonitor *monitor) |
| 404 | { |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 405 | lldb::pid_t pid = monitor->GetPID(); |
| 406 | |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 407 | m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | //------------------------------------------------------------------------------ |
| 411 | /// @class ReadOperation |
| 412 | /// @brief Implements ProcessMonitor::WriteMemory. |
| 413 | class WriteOperation : public Operation |
| 414 | { |
| 415 | public: |
| 416 | WriteOperation(lldb::addr_t addr, const void *buff, size_t size, |
| 417 | Error &error, size_t &result) |
| 418 | : m_addr(addr), m_buff(buff), m_size(size), |
| 419 | m_error(error), m_result(result) |
| 420 | { } |
| 421 | |
| 422 | void Execute(ProcessMonitor *monitor); |
| 423 | |
| 424 | private: |
| 425 | lldb::addr_t m_addr; |
| 426 | const void *m_buff; |
| 427 | size_t m_size; |
| 428 | Error &m_error; |
| 429 | size_t &m_result; |
| 430 | }; |
| 431 | |
| 432 | void |
| 433 | WriteOperation::Execute(ProcessMonitor *monitor) |
| 434 | { |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 435 | lldb::pid_t pid = monitor->GetPID(); |
| 436 | |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 437 | m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 440 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 441 | //------------------------------------------------------------------------------ |
| 442 | /// @class ReadRegOperation |
| 443 | /// @brief Implements ProcessMonitor::ReadRegisterValue. |
| 444 | class ReadRegOperation : public Operation |
| 445 | { |
| 446 | public: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 447 | ReadRegOperation(lldb::tid_t tid, unsigned offset, |
| 448 | RegisterValue &value, bool &result) |
| 449 | : m_tid(tid), m_offset(offset), |
| 450 | m_value(value), m_result(result) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 451 | { } |
| 452 | |
| 453 | void Execute(ProcessMonitor *monitor); |
| 454 | |
| 455 | private: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 456 | lldb::tid_t m_tid; |
Daniel Malea | a85e6b6 | 2012-12-07 22:21:08 +0000 | [diff] [blame] | 457 | uintptr_t m_offset; |
Johnny Chen | 13e8e1c | 2011-05-13 21:29:50 +0000 | [diff] [blame] | 458 | RegisterValue &m_value; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 459 | bool &m_result; |
| 460 | }; |
| 461 | |
| 462 | void |
| 463 | ReadRegOperation::Execute(ProcessMonitor *monitor) |
| 464 | { |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 465 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 466 | |
| 467 | // Set errno to zero so that we can detect a failed peek. |
| 468 | errno = 0; |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 469 | lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0); |
| 470 | if (errno) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 471 | m_result = false; |
| 472 | else |
| 473 | { |
| 474 | m_value = data; |
| 475 | m_result = true; |
| 476 | } |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 477 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 478 | log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__, |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 479 | POSIXThread::GetRegisterNameFromOffset(m_offset), data); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | //------------------------------------------------------------------------------ |
| 483 | /// @class WriteRegOperation |
| 484 | /// @brief Implements ProcessMonitor::WriteRegisterValue. |
| 485 | class WriteRegOperation : public Operation |
| 486 | { |
| 487 | public: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 488 | WriteRegOperation(lldb::tid_t tid, unsigned offset, |
| 489 | const RegisterValue &value, bool &result) |
| 490 | : m_tid(tid), m_offset(offset), |
| 491 | m_value(value), m_result(result) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 492 | { } |
| 493 | |
| 494 | void Execute(ProcessMonitor *monitor); |
| 495 | |
| 496 | private: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 497 | lldb::tid_t m_tid; |
Daniel Malea | a85e6b6 | 2012-12-07 22:21:08 +0000 | [diff] [blame] | 498 | uintptr_t m_offset; |
Johnny Chen | 13e8e1c | 2011-05-13 21:29:50 +0000 | [diff] [blame] | 499 | const RegisterValue &m_value; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 500 | bool &m_result; |
| 501 | }; |
| 502 | |
| 503 | void |
| 504 | WriteRegOperation::Execute(ProcessMonitor *monitor) |
| 505 | { |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 506 | void* buf; |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 507 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 508 | |
Daniel Malea | a85e6b6 | 2012-12-07 22:21:08 +0000 | [diff] [blame] | 509 | #if __WORDSIZE == 32 |
| 510 | buf = (void*) m_value.GetAsUInt32(); |
| 511 | #else |
| 512 | buf = (void*) m_value.GetAsUInt64(); |
| 513 | #endif |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 514 | |
| 515 | if (log) |
| 516 | log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 517 | POSIXThread::GetRegisterNameFromOffset(m_offset), buf); |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 518 | if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 519 | m_result = false; |
| 520 | else |
| 521 | m_result = true; |
| 522 | } |
| 523 | |
| 524 | //------------------------------------------------------------------------------ |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 525 | /// @class ReadGPROperation |
| 526 | /// @brief Implements ProcessMonitor::ReadGPR. |
| 527 | class ReadGPROperation : public Operation |
| 528 | { |
| 529 | public: |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 530 | ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result) |
| 531 | : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result) |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 532 | { } |
| 533 | |
| 534 | void Execute(ProcessMonitor *monitor); |
| 535 | |
| 536 | private: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 537 | lldb::tid_t m_tid; |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 538 | void *m_buf; |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 539 | size_t m_buf_size; |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 540 | bool &m_result; |
| 541 | }; |
| 542 | |
| 543 | void |
| 544 | ReadGPROperation::Execute(ProcessMonitor *monitor) |
| 545 | { |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 546 | if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0) |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 547 | m_result = false; |
| 548 | else |
| 549 | m_result = true; |
| 550 | } |
| 551 | |
| 552 | //------------------------------------------------------------------------------ |
| 553 | /// @class ReadFPROperation |
| 554 | /// @brief Implements ProcessMonitor::ReadFPR. |
| 555 | class ReadFPROperation : public Operation |
| 556 | { |
| 557 | public: |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 558 | ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result) |
| 559 | : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result) |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 560 | { } |
| 561 | |
| 562 | void Execute(ProcessMonitor *monitor); |
| 563 | |
| 564 | private: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 565 | lldb::tid_t m_tid; |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 566 | void *m_buf; |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 567 | size_t m_buf_size; |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 568 | bool &m_result; |
| 569 | }; |
| 570 | |
| 571 | void |
| 572 | ReadFPROperation::Execute(ProcessMonitor *monitor) |
| 573 | { |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 574 | if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0) |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 575 | m_result = false; |
| 576 | else |
| 577 | m_result = true; |
| 578 | } |
| 579 | |
| 580 | //------------------------------------------------------------------------------ |
Matt Kopec | 58c0b96 | 2013-03-20 20:34:35 +0000 | [diff] [blame^] | 581 | /// @class ReadRegisterSetOperation |
| 582 | /// @brief Implements ProcessMonitor::ReadRegisterSet. |
| 583 | class ReadRegisterSetOperation : public Operation |
| 584 | { |
| 585 | public: |
| 586 | ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result) |
| 587 | : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result) |
| 588 | { } |
| 589 | |
| 590 | void Execute(ProcessMonitor *monitor); |
| 591 | |
| 592 | private: |
| 593 | lldb::tid_t m_tid; |
| 594 | void *m_buf; |
| 595 | size_t m_buf_size; |
| 596 | const unsigned int m_regset; |
| 597 | bool &m_result; |
| 598 | }; |
| 599 | |
| 600 | void |
| 601 | ReadRegisterSetOperation::Execute(ProcessMonitor *monitor) |
| 602 | { |
| 603 | if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0) |
| 604 | m_result = false; |
| 605 | else |
| 606 | m_result = true; |
| 607 | } |
| 608 | |
| 609 | //------------------------------------------------------------------------------ |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 610 | /// @class WriteGPROperation |
| 611 | /// @brief Implements ProcessMonitor::WriteGPR. |
| 612 | class WriteGPROperation : public Operation |
| 613 | { |
| 614 | public: |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 615 | WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result) |
| 616 | : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result) |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 617 | { } |
| 618 | |
| 619 | void Execute(ProcessMonitor *monitor); |
| 620 | |
| 621 | private: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 622 | lldb::tid_t m_tid; |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 623 | void *m_buf; |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 624 | size_t m_buf_size; |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 625 | bool &m_result; |
| 626 | }; |
| 627 | |
| 628 | void |
| 629 | WriteGPROperation::Execute(ProcessMonitor *monitor) |
| 630 | { |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 631 | if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0) |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 632 | m_result = false; |
| 633 | else |
| 634 | m_result = true; |
| 635 | } |
| 636 | |
| 637 | //------------------------------------------------------------------------------ |
| 638 | /// @class WriteFPROperation |
| 639 | /// @brief Implements ProcessMonitor::WriteFPR. |
| 640 | class WriteFPROperation : public Operation |
| 641 | { |
| 642 | public: |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 643 | WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result) |
| 644 | : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result) |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 645 | { } |
| 646 | |
| 647 | void Execute(ProcessMonitor *monitor); |
| 648 | |
| 649 | private: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 650 | lldb::tid_t m_tid; |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 651 | void *m_buf; |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 652 | size_t m_buf_size; |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 653 | bool &m_result; |
| 654 | }; |
| 655 | |
| 656 | void |
| 657 | WriteFPROperation::Execute(ProcessMonitor *monitor) |
| 658 | { |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 659 | if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0) |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 660 | m_result = false; |
| 661 | else |
| 662 | m_result = true; |
| 663 | } |
| 664 | |
| 665 | //------------------------------------------------------------------------------ |
Matt Kopec | 58c0b96 | 2013-03-20 20:34:35 +0000 | [diff] [blame^] | 666 | /// @class WriteRegisterSetOperation |
| 667 | /// @brief Implements ProcessMonitor::WriteRegisterSet. |
| 668 | class WriteRegisterSetOperation : public Operation |
| 669 | { |
| 670 | public: |
| 671 | WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result) |
| 672 | : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result) |
| 673 | { } |
| 674 | |
| 675 | void Execute(ProcessMonitor *monitor); |
| 676 | |
| 677 | private: |
| 678 | lldb::tid_t m_tid; |
| 679 | void *m_buf; |
| 680 | size_t m_buf_size; |
| 681 | const unsigned int m_regset; |
| 682 | bool &m_result; |
| 683 | }; |
| 684 | |
| 685 | void |
| 686 | WriteRegisterSetOperation::Execute(ProcessMonitor *monitor) |
| 687 | { |
| 688 | if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0) |
| 689 | m_result = false; |
| 690 | else |
| 691 | m_result = true; |
| 692 | } |
| 693 | |
| 694 | //------------------------------------------------------------------------------ |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 695 | /// @class ResumeOperation |
| 696 | /// @brief Implements ProcessMonitor::Resume. |
| 697 | class ResumeOperation : public Operation |
| 698 | { |
| 699 | public: |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 700 | ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) : |
| 701 | m_tid(tid), m_signo(signo), m_result(result) { } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 702 | |
| 703 | void Execute(ProcessMonitor *monitor); |
| 704 | |
| 705 | private: |
| 706 | lldb::tid_t m_tid; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 707 | uint32_t m_signo; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 708 | bool &m_result; |
| 709 | }; |
| 710 | |
| 711 | void |
| 712 | ResumeOperation::Execute(ProcessMonitor *monitor) |
| 713 | { |
Daniel Malea | a85e6b6 | 2012-12-07 22:21:08 +0000 | [diff] [blame] | 714 | intptr_t data = 0; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 715 | |
| 716 | if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 717 | data = m_signo; |
| 718 | |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 719 | if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 720 | m_result = false; |
| 721 | else |
| 722 | m_result = true; |
| 723 | } |
| 724 | |
| 725 | //------------------------------------------------------------------------------ |
| 726 | /// @class ResumeOperation |
| 727 | /// @brief Implements ProcessMonitor::SingleStep. |
| 728 | class SingleStepOperation : public Operation |
| 729 | { |
| 730 | public: |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 731 | SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result) |
| 732 | : m_tid(tid), m_signo(signo), m_result(result) { } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 733 | |
| 734 | void Execute(ProcessMonitor *monitor); |
| 735 | |
| 736 | private: |
| 737 | lldb::tid_t m_tid; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 738 | uint32_t m_signo; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 739 | bool &m_result; |
| 740 | }; |
| 741 | |
| 742 | void |
| 743 | SingleStepOperation::Execute(ProcessMonitor *monitor) |
| 744 | { |
Daniel Malea | a85e6b6 | 2012-12-07 22:21:08 +0000 | [diff] [blame] | 745 | intptr_t data = 0; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 746 | |
| 747 | if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 748 | data = m_signo; |
| 749 | |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 750 | if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 751 | m_result = false; |
| 752 | else |
| 753 | m_result = true; |
| 754 | } |
| 755 | |
| 756 | //------------------------------------------------------------------------------ |
| 757 | /// @class SiginfoOperation |
| 758 | /// @brief Implements ProcessMonitor::GetSignalInfo. |
| 759 | class SiginfoOperation : public Operation |
| 760 | { |
| 761 | public: |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 762 | SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err) |
| 763 | : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 764 | |
| 765 | void Execute(ProcessMonitor *monitor); |
| 766 | |
| 767 | private: |
| 768 | lldb::tid_t m_tid; |
| 769 | void *m_info; |
| 770 | bool &m_result; |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 771 | int &m_err; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 772 | }; |
| 773 | |
| 774 | void |
| 775 | SiginfoOperation::Execute(ProcessMonitor *monitor) |
| 776 | { |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 777 | if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) { |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 778 | m_result = false; |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 779 | m_err = errno; |
| 780 | } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 781 | else |
| 782 | m_result = true; |
| 783 | } |
| 784 | |
| 785 | //------------------------------------------------------------------------------ |
| 786 | /// @class EventMessageOperation |
| 787 | /// @brief Implements ProcessMonitor::GetEventMessage. |
| 788 | class EventMessageOperation : public Operation |
| 789 | { |
| 790 | public: |
| 791 | EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result) |
| 792 | : m_tid(tid), m_message(message), m_result(result) { } |
| 793 | |
| 794 | void Execute(ProcessMonitor *monitor); |
| 795 | |
| 796 | private: |
| 797 | lldb::tid_t m_tid; |
| 798 | unsigned long *m_message; |
| 799 | bool &m_result; |
| 800 | }; |
| 801 | |
| 802 | void |
| 803 | EventMessageOperation::Execute(ProcessMonitor *monitor) |
| 804 | { |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 805 | if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 806 | m_result = false; |
| 807 | else |
| 808 | m_result = true; |
| 809 | } |
| 810 | |
| 811 | //------------------------------------------------------------------------------ |
| 812 | /// @class KillOperation |
| 813 | /// @brief Implements ProcessMonitor::BringProcessIntoLimbo. |
| 814 | class KillOperation : public Operation |
| 815 | { |
| 816 | public: |
| 817 | KillOperation(bool &result) : m_result(result) { } |
| 818 | |
| 819 | void Execute(ProcessMonitor *monitor); |
| 820 | |
| 821 | private: |
| 822 | bool &m_result; |
| 823 | }; |
| 824 | |
| 825 | void |
| 826 | KillOperation::Execute(ProcessMonitor *monitor) |
| 827 | { |
| 828 | lldb::pid_t pid = monitor->GetPID(); |
| 829 | |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 830 | if (PTRACE(PTRACE_KILL, pid, NULL, NULL, 0)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 831 | m_result = false; |
| 832 | else |
| 833 | m_result = true; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 836 | //------------------------------------------------------------------------------ |
| 837 | /// @class KillOperation |
| 838 | /// @brief Implements ProcessMonitor::BringProcessIntoLimbo. |
| 839 | class DetachOperation : public Operation |
| 840 | { |
| 841 | public: |
| 842 | DetachOperation(Error &result) : m_error(result) { } |
| 843 | |
| 844 | void Execute(ProcessMonitor *monitor); |
| 845 | |
| 846 | private: |
| 847 | Error &m_error; |
| 848 | }; |
| 849 | |
| 850 | void |
| 851 | DetachOperation::Execute(ProcessMonitor *monitor) |
| 852 | { |
| 853 | lldb::pid_t pid = monitor->GetPID(); |
| 854 | |
| 855 | if (ptrace(PT_DETACH, pid, NULL, 0) < 0) |
| 856 | m_error.SetErrorToErrno(); |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 857 | |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 860 | ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor) |
| 861 | : m_monitor(monitor) |
| 862 | { |
| 863 | sem_init(&m_semaphore, 0, 0); |
| 864 | } |
| 865 | |
| 866 | ProcessMonitor::OperationArgs::~OperationArgs() |
| 867 | { |
| 868 | sem_destroy(&m_semaphore); |
| 869 | } |
| 870 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 871 | ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor, |
| 872 | lldb_private::Module *module, |
| 873 | char const **argv, |
| 874 | char const **envp, |
| 875 | const char *stdin_path, |
| 876 | const char *stdout_path, |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 877 | const char *stderr_path, |
| 878 | const char *working_dir) |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 879 | : OperationArgs(monitor), |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 880 | m_module(module), |
| 881 | m_argv(argv), |
| 882 | m_envp(envp), |
| 883 | m_stdin_path(stdin_path), |
| 884 | m_stdout_path(stdout_path), |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 885 | m_stderr_path(stderr_path), |
| 886 | m_working_dir(working_dir) { } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 887 | |
| 888 | ProcessMonitor::LaunchArgs::~LaunchArgs() |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 889 | { } |
| 890 | |
| 891 | ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor, |
| 892 | lldb::pid_t pid) |
| 893 | : OperationArgs(monitor), m_pid(pid) { } |
| 894 | |
| 895 | ProcessMonitor::AttachArgs::~AttachArgs() |
| 896 | { } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 897 | |
| 898 | //------------------------------------------------------------------------------ |
| 899 | /// The basic design of the ProcessMonitor is built around two threads. |
| 900 | /// |
| 901 | /// One thread (@see SignalThread) simply blocks on a call to waitpid() looking |
| 902 | /// for changes in the debugee state. When a change is detected a |
| 903 | /// ProcessMessage is sent to the associated ProcessLinux instance. This thread |
| 904 | /// "drives" state changes in the debugger. |
| 905 | /// |
| 906 | /// The second thread (@see OperationThread) is responsible for two things 1) |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 907 | /// launching or attaching to the inferior process, and then 2) servicing |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 908 | /// operations such as register reads/writes, stepping, etc. See the comments |
| 909 | /// on the Operation class for more info as to why this is needed. |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 910 | ProcessMonitor::ProcessMonitor(ProcessPOSIX *process, |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 911 | Module *module, |
| 912 | const char *argv[], |
| 913 | const char *envp[], |
| 914 | const char *stdin_path, |
| 915 | const char *stdout_path, |
| 916 | const char *stderr_path, |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 917 | const char *working_dir, |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 918 | lldb_private::Error &error) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 919 | : m_process(static_cast<ProcessLinux *>(process)), |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 920 | m_operation_thread(LLDB_INVALID_HOST_THREAD), |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 921 | m_monitor_thread(LLDB_INVALID_HOST_THREAD), |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 922 | m_pid(LLDB_INVALID_PROCESS_ID), |
| 923 | m_terminal_fd(-1), |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 924 | m_client_fd(-1), |
| 925 | m_server_fd(-1) |
| 926 | { |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 927 | std::auto_ptr<LaunchArgs> args; |
| 928 | |
| 929 | args.reset(new LaunchArgs(this, module, argv, envp, |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 930 | stdin_path, stdout_path, stderr_path, working_dir)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 931 | |
| 932 | // Server/client descriptors. |
| 933 | if (!EnableIPC()) |
| 934 | { |
| 935 | error.SetErrorToGenericError(); |
| 936 | error.SetErrorString("Monitor failed to initialize."); |
| 937 | } |
| 938 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 939 | StartLaunchOpThread(args.get(), error); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 940 | if (!error.Success()) |
| 941 | return; |
| 942 | |
| 943 | WAIT_AGAIN: |
| 944 | // Wait for the operation thread to initialize. |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 945 | if (sem_wait(&args->m_semaphore)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 946 | { |
| 947 | if (errno == EINTR) |
| 948 | goto WAIT_AGAIN; |
| 949 | else |
| 950 | { |
| 951 | error.SetErrorToErrno(); |
| 952 | return; |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | // Check that the launch was a success. |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 957 | if (!args->m_error.Success()) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 958 | { |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 959 | StopOpThread(); |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 960 | error = args->m_error; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 961 | return; |
| 962 | } |
| 963 | |
| 964 | // Finally, start monitoring the child process for change in state. |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 965 | m_monitor_thread = Host::StartMonitoringChildProcess( |
| 966 | ProcessMonitor::MonitorCallback, this, GetPID(), true); |
Stephen Wilson | d4182f4 | 2011-02-09 20:10:35 +0000 | [diff] [blame] | 967 | if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 968 | { |
| 969 | error.SetErrorToGenericError(); |
| 970 | error.SetErrorString("Process launch failed."); |
| 971 | return; |
| 972 | } |
| 973 | } |
| 974 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 975 | ProcessMonitor::ProcessMonitor(ProcessPOSIX *process, |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 976 | lldb::pid_t pid, |
| 977 | lldb_private::Error &error) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 978 | : m_process(static_cast<ProcessLinux *>(process)), |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 979 | m_operation_thread(LLDB_INVALID_HOST_THREAD), |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 980 | m_monitor_thread(LLDB_INVALID_HOST_THREAD), |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 981 | m_pid(LLDB_INVALID_PROCESS_ID), |
| 982 | m_terminal_fd(-1), |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 983 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 984 | m_client_fd(-1), |
| 985 | m_server_fd(-1) |
| 986 | { |
| 987 | std::auto_ptr<AttachArgs> args; |
| 988 | |
| 989 | args.reset(new AttachArgs(this, pid)); |
| 990 | |
| 991 | // Server/client descriptors. |
| 992 | if (!EnableIPC()) |
| 993 | { |
| 994 | error.SetErrorToGenericError(); |
| 995 | error.SetErrorString("Monitor failed to initialize."); |
| 996 | } |
| 997 | |
| 998 | StartAttachOpThread(args.get(), error); |
| 999 | if (!error.Success()) |
| 1000 | return; |
| 1001 | |
| 1002 | WAIT_AGAIN: |
| 1003 | // Wait for the operation thread to initialize. |
| 1004 | if (sem_wait(&args->m_semaphore)) |
| 1005 | { |
| 1006 | if (errno == EINTR) |
| 1007 | goto WAIT_AGAIN; |
| 1008 | else |
| 1009 | { |
| 1010 | error.SetErrorToErrno(); |
| 1011 | return; |
| 1012 | } |
| 1013 | } |
| 1014 | |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1015 | // Check that the attach was a success. |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1016 | if (!args->m_error.Success()) |
| 1017 | { |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1018 | StopOpThread(); |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1019 | error = args->m_error; |
| 1020 | return; |
| 1021 | } |
| 1022 | |
| 1023 | // Finally, start monitoring the child process for change in state. |
| 1024 | m_monitor_thread = Host::StartMonitoringChildProcess( |
| 1025 | ProcessMonitor::MonitorCallback, this, GetPID(), true); |
| 1026 | if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread)) |
| 1027 | { |
| 1028 | error.SetErrorToGenericError(); |
| 1029 | error.SetErrorString("Process attach failed."); |
| 1030 | return; |
| 1031 | } |
| 1032 | } |
| 1033 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1034 | ProcessMonitor::~ProcessMonitor() |
| 1035 | { |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1036 | StopMonitor(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | //------------------------------------------------------------------------------ |
| 1040 | // Thread setup and tear down. |
| 1041 | void |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1042 | ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1043 | { |
| 1044 | static const char *g_thread_name = "lldb.process.linux.operation"; |
| 1045 | |
Stephen Wilson | d4182f4 | 2011-02-09 20:10:35 +0000 | [diff] [blame] | 1046 | if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1047 | return; |
| 1048 | |
| 1049 | m_operation_thread = |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1050 | Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1053 | void * |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1054 | ProcessMonitor::LaunchOpThread(void *arg) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1055 | { |
| 1056 | LaunchArgs *args = static_cast<LaunchArgs*>(arg); |
| 1057 | |
Peter Collingbourne | 4aeb47e | 2011-06-14 03:55:49 +0000 | [diff] [blame] | 1058 | if (!Launch(args)) { |
| 1059 | sem_post(&args->m_semaphore); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1060 | return NULL; |
Peter Collingbourne | 4aeb47e | 2011-06-14 03:55:49 +0000 | [diff] [blame] | 1061 | } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1062 | |
Stephen Wilson | 570243b | 2011-01-19 01:37:06 +0000 | [diff] [blame] | 1063 | ServeOperation(args); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1064 | return NULL; |
| 1065 | } |
| 1066 | |
| 1067 | bool |
| 1068 | ProcessMonitor::Launch(LaunchArgs *args) |
| 1069 | { |
| 1070 | ProcessMonitor *monitor = args->m_monitor; |
| 1071 | ProcessLinux &process = monitor->GetProcess(); |
| 1072 | const char **argv = args->m_argv; |
| 1073 | const char **envp = args->m_envp; |
| 1074 | const char *stdin_path = args->m_stdin_path; |
| 1075 | const char *stdout_path = args->m_stdout_path; |
| 1076 | const char *stderr_path = args->m_stderr_path; |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 1077 | const char *working_dir = args->m_working_dir; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1078 | |
| 1079 | lldb_utility::PseudoTerminal terminal; |
| 1080 | const size_t err_len = 1024; |
| 1081 | char err_str[err_len]; |
| 1082 | lldb::pid_t pid; |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 1083 | long ptrace_opts = 0; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1084 | |
| 1085 | lldb::ThreadSP inferior; |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 1086 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1087 | |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 1088 | // Propagate the environment if one is not supplied. |
| 1089 | if (envp == NULL || envp[0] == NULL) |
| 1090 | envp = const_cast<const char **>(environ); |
| 1091 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1092 | // Pseudo terminal setup. |
| 1093 | if (!terminal.OpenFirstAvailableMaster(O_RDWR | O_NOCTTY, err_str, err_len)) |
| 1094 | { |
| 1095 | args->m_error.SetErrorToGenericError(); |
| 1096 | args->m_error.SetErrorString("Could not open controlling TTY."); |
| 1097 | goto FINISH; |
| 1098 | } |
| 1099 | |
Daniel Malea | a85e6b6 | 2012-12-07 22:21:08 +0000 | [diff] [blame] | 1100 | if ((pid = terminal.Fork(err_str, err_len)) == -1) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1101 | { |
| 1102 | args->m_error.SetErrorToGenericError(); |
| 1103 | args->m_error.SetErrorString("Process fork failed."); |
| 1104 | goto FINISH; |
| 1105 | } |
| 1106 | |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1107 | // Recognized child exit status codes. |
| 1108 | enum { |
| 1109 | ePtraceFailed = 1, |
| 1110 | eDupStdinFailed, |
| 1111 | eDupStdoutFailed, |
| 1112 | eDupStderrFailed, |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 1113 | eChdirFailed, |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1114 | eExecFailed |
| 1115 | }; |
| 1116 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1117 | // Child process. |
| 1118 | if (pid == 0) |
| 1119 | { |
| 1120 | // Trace this process. |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1121 | if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0) |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1122 | exit(ePtraceFailed); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1123 | |
| 1124 | // Do not inherit setgid powers. |
| 1125 | setgid(getgid()); |
| 1126 | |
| 1127 | // Let us have our own process group. |
| 1128 | setpgid(0, 0); |
| 1129 | |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 1130 | // Dup file descriptors if needed. |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1131 | // |
| 1132 | // FIXME: If two or more of the paths are the same we needlessly open |
| 1133 | // the same file multiple times. |
| 1134 | if (stdin_path != NULL && stdin_path[0]) |
Peter Collingbourne | 6234320 | 2011-06-14 03:55:54 +0000 | [diff] [blame] | 1135 | if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY)) |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1136 | exit(eDupStdinFailed); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1137 | |
| 1138 | if (stdout_path != NULL && stdout_path[0]) |
| 1139 | if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT)) |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1140 | exit(eDupStdoutFailed); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1141 | |
| 1142 | if (stderr_path != NULL && stderr_path[0]) |
Peter Collingbourne | 6234320 | 2011-06-14 03:55:54 +0000 | [diff] [blame] | 1143 | if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT)) |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1144 | exit(eDupStderrFailed); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1145 | |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 1146 | // Change working directory |
| 1147 | if (working_dir != NULL && working_dir[0]) |
| 1148 | if (0 != ::chdir(working_dir)) |
| 1149 | exit(eChdirFailed); |
| 1150 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1151 | // Execute. We should never return. |
| 1152 | execve(argv[0], |
| 1153 | const_cast<char *const *>(argv), |
| 1154 | const_cast<char *const *>(envp)); |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1155 | exit(eExecFailed); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | // Wait for the child process to to trap on its call to execve. |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1159 | pid_t wpid; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1160 | int status; |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1161 | if ((wpid = waitpid(pid, &status, 0)) < 0) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1162 | { |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1163 | args->m_error.SetErrorToErrno(); |
| 1164 | goto FINISH; |
| 1165 | } |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1166 | else if (WIFEXITED(status)) |
| 1167 | { |
| 1168 | // open, dup or execve likely failed for some reason. |
| 1169 | args->m_error.SetErrorToGenericError(); |
| 1170 | switch (WEXITSTATUS(status)) |
| 1171 | { |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1172 | case ePtraceFailed: |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1173 | args->m_error.SetErrorString("Child ptrace failed."); |
| 1174 | break; |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1175 | case eDupStdinFailed: |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1176 | args->m_error.SetErrorString("Child open stdin failed."); |
| 1177 | break; |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1178 | case eDupStdoutFailed: |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1179 | args->m_error.SetErrorString("Child open stdout failed."); |
| 1180 | break; |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1181 | case eDupStderrFailed: |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1182 | args->m_error.SetErrorString("Child open stderr failed."); |
| 1183 | break; |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 1184 | case eChdirFailed: |
| 1185 | args->m_error.SetErrorString("Child failed to set working directory."); |
| 1186 | break; |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1187 | case eExecFailed: |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1188 | args->m_error.SetErrorString("Child exec failed."); |
| 1189 | break; |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1190 | default: |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1191 | args->m_error.SetErrorString("Child returned unknown exit status."); |
| 1192 | break; |
| 1193 | } |
| 1194 | goto FINISH; |
| 1195 | } |
| 1196 | assert(WIFSTOPPED(status) && wpid == pid && |
| 1197 | "Could not sync with inferior process."); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1198 | |
| 1199 | // Have the child raise an event on exit. This is used to keep the child in |
| 1200 | // limbo until it is destroyed. |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 1201 | ptrace_opts |= PTRACE_O_TRACEEXIT; |
| 1202 | |
| 1203 | // Have the tracer trace threads which spawn in the inferior process. |
| 1204 | ptrace_opts |= PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE; |
| 1205 | |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1206 | if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) < 0) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1207 | { |
| 1208 | args->m_error.SetErrorToErrno(); |
| 1209 | goto FINISH; |
| 1210 | } |
| 1211 | |
| 1212 | // Release the master terminal descriptor and pass it off to the |
| 1213 | // ProcessMonitor instance. Similarly stash the inferior pid. |
| 1214 | monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); |
| 1215 | monitor->m_pid = pid; |
| 1216 | |
Stephen Wilson | 2697716 | 2011-03-23 02:14:42 +0000 | [diff] [blame] | 1217 | // Set the terminal fd to be in non blocking mode (it simplifies the |
| 1218 | // implementation of ProcessLinux::GetSTDOUT to have a non-blocking |
| 1219 | // descriptor to read from). |
| 1220 | if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error)) |
| 1221 | goto FINISH; |
| 1222 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 1223 | // Update the process thread list with this new thread. |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 1224 | // FIXME: should we be letting UpdateThreadList handle this? |
| 1225 | // FIXME: by using pids instead of tids, we can only support one thread. |
Greg Clayton | df3df25 | 2012-10-12 16:23:23 +0000 | [diff] [blame] | 1226 | inferior.reset(new POSIXThread(process, pid)); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 1227 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1228 | log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1229 | process.GetThreadList().AddThread(inferior); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1230 | |
| 1231 | // Let our process instance know the thread has stopped. |
| 1232 | process.SendMessage(ProcessMessage::Trace(pid)); |
| 1233 | |
| 1234 | FINISH: |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1235 | return args->m_error.Success(); |
| 1236 | } |
| 1237 | |
| 1238 | bool |
| 1239 | ProcessMonitor::EnableIPC() |
| 1240 | { |
| 1241 | int fd[2]; |
| 1242 | |
| 1243 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) |
| 1244 | return false; |
| 1245 | |
| 1246 | m_client_fd = fd[0]; |
| 1247 | m_server_fd = fd[1]; |
| 1248 | return true; |
| 1249 | } |
| 1250 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1251 | void |
| 1252 | ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error) |
| 1253 | { |
| 1254 | static const char *g_thread_name = "lldb.process.linux.operation"; |
| 1255 | |
| 1256 | if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread)) |
| 1257 | return; |
| 1258 | |
| 1259 | m_operation_thread = |
| 1260 | Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error); |
| 1261 | } |
| 1262 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1263 | void * |
| 1264 | ProcessMonitor::AttachOpThread(void *arg) |
| 1265 | { |
| 1266 | AttachArgs *args = static_cast<AttachArgs*>(arg); |
| 1267 | |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1268 | if (!Attach(args)) { |
| 1269 | sem_post(&args->m_semaphore); |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1270 | return NULL; |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1271 | } |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1272 | |
| 1273 | ServeOperation(args); |
| 1274 | return NULL; |
| 1275 | } |
| 1276 | |
| 1277 | bool |
| 1278 | ProcessMonitor::Attach(AttachArgs *args) |
| 1279 | { |
| 1280 | lldb::pid_t pid = args->m_pid; |
| 1281 | |
| 1282 | ProcessMonitor *monitor = args->m_monitor; |
| 1283 | ProcessLinux &process = monitor->GetProcess(); |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1284 | lldb::ThreadSP inferior; |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 1285 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1286 | |
| 1287 | if (pid <= 1) |
| 1288 | { |
| 1289 | args->m_error.SetErrorToGenericError(); |
| 1290 | args->m_error.SetErrorString("Attaching to process 1 is not allowed."); |
| 1291 | goto FINISH; |
| 1292 | } |
| 1293 | |
| 1294 | // Attach to the requested process. |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1295 | if (PTRACE(PTRACE_ATTACH, pid, NULL, NULL, 0) < 0) |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1296 | { |
| 1297 | args->m_error.SetErrorToErrno(); |
| 1298 | goto FINISH; |
| 1299 | } |
| 1300 | |
| 1301 | int status; |
| 1302 | if ((status = waitpid(pid, NULL, 0)) < 0) |
| 1303 | { |
| 1304 | args->m_error.SetErrorToErrno(); |
| 1305 | goto FINISH; |
| 1306 | } |
| 1307 | |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 1308 | monitor->m_pid = pid; |
| 1309 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 1310 | // Update the process thread list with the attached thread. |
Greg Clayton | df3df25 | 2012-10-12 16:23:23 +0000 | [diff] [blame] | 1311 | inferior.reset(new POSIXThread(process, pid)); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 1312 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1313 | log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, pid); |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1314 | process.GetThreadList().AddThread(inferior); |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1315 | |
| 1316 | // Let our process instance know the thread has stopped. |
| 1317 | process.SendMessage(ProcessMessage::Trace(pid)); |
| 1318 | |
| 1319 | FINISH: |
| 1320 | return args->m_error.Success(); |
| 1321 | } |
| 1322 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1323 | bool |
| 1324 | ProcessMonitor::MonitorCallback(void *callback_baton, |
| 1325 | lldb::pid_t pid, |
Peter Collingbourne | 2c67b9a | 2011-11-21 00:10:19 +0000 | [diff] [blame] | 1326 | bool exited, |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1327 | int signal, |
| 1328 | int status) |
| 1329 | { |
| 1330 | ProcessMessage message; |
| 1331 | ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton); |
| 1332 | ProcessLinux *process = monitor->m_process; |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 1333 | assert(process); |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1334 | bool stop_monitoring; |
| 1335 | siginfo_t info; |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 1336 | int ptrace_err; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1337 | |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 1338 | if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) { |
| 1339 | if (ptrace_err == EINVAL) { |
| 1340 | // inferior process is in 'group-stop', so deliver SIGSTOP signal |
| 1341 | if (!monitor->Resume(pid, SIGSTOP)) { |
| 1342 | assert(0 && "SIGSTOP delivery failed while in 'group-stop' state"); |
| 1343 | } |
| 1344 | stop_monitoring = false; |
| 1345 | } else { |
| 1346 | // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely, |
| 1347 | // this means the child pid is gone (or not being debugged) therefore |
| 1348 | // stop the monitor thread. |
| 1349 | stop_monitoring = true; |
| 1350 | } |
| 1351 | } |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1352 | else { |
| 1353 | switch (info.si_signo) |
| 1354 | { |
| 1355 | case SIGTRAP: |
| 1356 | message = MonitorSIGTRAP(monitor, &info, pid); |
| 1357 | break; |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1358 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1359 | default: |
| 1360 | message = MonitorSignal(monitor, &info, pid); |
| 1361 | break; |
| 1362 | } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1363 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1364 | process->SendMessage(message); |
Daniel Malea | 8b9e71e | 2012-11-22 18:21:05 +0000 | [diff] [blame] | 1365 | stop_monitoring = !process->IsAlive(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1368 | return stop_monitoring; |
| 1369 | } |
| 1370 | |
| 1371 | ProcessMessage |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1372 | ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor, |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 1373 | const siginfo_t *info, lldb::pid_t pid) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1374 | { |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1375 | ProcessMessage message; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1376 | |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 1377 | assert(monitor); |
| 1378 | assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!"); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1379 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1380 | switch (info->si_code) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1381 | { |
| 1382 | default: |
| 1383 | assert(false && "Unexpected SIGTRAP code!"); |
| 1384 | break; |
| 1385 | |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 1386 | case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): |
| 1387 | case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): |
| 1388 | case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): |
| 1389 | { |
| 1390 | unsigned long tid = 0; |
| 1391 | if (!monitor->GetEventMessage(pid, &tid)) |
| 1392 | tid = -1; |
| 1393 | message = ProcessMessage::NewThread(pid, tid); |
| 1394 | break; |
| 1395 | } |
| 1396 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1397 | case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): |
| 1398 | { |
| 1399 | // The inferior process is about to exit. Maintain the process in a |
| 1400 | // state of "limbo" until we are explicitly commanded to detach, |
| 1401 | // destroy, resume, etc. |
| 1402 | unsigned long data = 0; |
| 1403 | if (!monitor->GetEventMessage(pid, &data)) |
| 1404 | data = -1; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1405 | message = ProcessMessage::Limbo(pid, (data >> 8)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1406 | break; |
| 1407 | } |
| 1408 | |
| 1409 | case 0: |
| 1410 | case TRAP_TRACE: |
| 1411 | message = ProcessMessage::Trace(pid); |
| 1412 | break; |
| 1413 | |
| 1414 | case SI_KERNEL: |
| 1415 | case TRAP_BRKPT: |
| 1416 | message = ProcessMessage::Break(pid); |
| 1417 | break; |
| 1418 | } |
| 1419 | |
| 1420 | return message; |
| 1421 | } |
| 1422 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1423 | ProcessMessage |
| 1424 | ProcessMonitor::MonitorSignal(ProcessMonitor *monitor, |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 1425 | const siginfo_t *info, lldb::pid_t pid) |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1426 | { |
| 1427 | ProcessMessage message; |
| 1428 | int signo = info->si_signo; |
| 1429 | |
| 1430 | // POSIX says that process behaviour is undefined after it ignores a SIGFPE, |
| 1431 | // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a |
| 1432 | // kill(2) or raise(3). Similarly for tgkill(2) on Linux. |
| 1433 | // |
| 1434 | // IOW, user generated signals never generate what we consider to be a |
| 1435 | // "crash". |
| 1436 | // |
| 1437 | // Similarly, ACK signals generated by this monitor. |
| 1438 | if (info->si_code == SI_TKILL || info->si_code == SI_USER) |
| 1439 | { |
| 1440 | if (info->si_pid == getpid()) |
| 1441 | return ProcessMessage::SignalDelivered(pid, signo); |
| 1442 | else |
| 1443 | return ProcessMessage::Signal(pid, signo); |
| 1444 | } |
| 1445 | |
| 1446 | if (signo == SIGSEGV) { |
| 1447 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1448 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info); |
| 1449 | return ProcessMessage::Crash(pid, reason, signo, fault_addr); |
| 1450 | } |
| 1451 | |
| 1452 | if (signo == SIGILL) { |
| 1453 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1454 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info); |
| 1455 | return ProcessMessage::Crash(pid, reason, signo, fault_addr); |
| 1456 | } |
| 1457 | |
| 1458 | if (signo == SIGFPE) { |
| 1459 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1460 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info); |
| 1461 | return ProcessMessage::Crash(pid, reason, signo, fault_addr); |
| 1462 | } |
| 1463 | |
| 1464 | if (signo == SIGBUS) { |
| 1465 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1466 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info); |
| 1467 | return ProcessMessage::Crash(pid, reason, signo, fault_addr); |
| 1468 | } |
| 1469 | |
| 1470 | // Everything else is "normal" and does not require any special action on |
| 1471 | // our part. |
| 1472 | return ProcessMessage::Signal(pid, signo); |
| 1473 | } |
| 1474 | |
| 1475 | ProcessMessage::CrashReason |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 1476 | ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info) |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1477 | { |
| 1478 | ProcessMessage::CrashReason reason; |
| 1479 | assert(info->si_signo == SIGSEGV); |
| 1480 | |
| 1481 | reason = ProcessMessage::eInvalidCrashReason; |
| 1482 | |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1483 | switch (info->si_code) |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1484 | { |
| 1485 | default: |
| 1486 | assert(false && "unexpected si_code for SIGSEGV"); |
| 1487 | break; |
| 1488 | case SEGV_MAPERR: |
| 1489 | reason = ProcessMessage::eInvalidAddress; |
| 1490 | break; |
| 1491 | case SEGV_ACCERR: |
| 1492 | reason = ProcessMessage::ePrivilegedAddress; |
| 1493 | break; |
| 1494 | } |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1495 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1496 | return reason; |
| 1497 | } |
| 1498 | |
| 1499 | ProcessMessage::CrashReason |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 1500 | ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info) |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1501 | { |
| 1502 | ProcessMessage::CrashReason reason; |
| 1503 | assert(info->si_signo == SIGILL); |
| 1504 | |
| 1505 | reason = ProcessMessage::eInvalidCrashReason; |
| 1506 | |
| 1507 | switch (info->si_code) |
| 1508 | { |
| 1509 | default: |
| 1510 | assert(false && "unexpected si_code for SIGILL"); |
| 1511 | break; |
| 1512 | case ILL_ILLOPC: |
| 1513 | reason = ProcessMessage::eIllegalOpcode; |
| 1514 | break; |
| 1515 | case ILL_ILLOPN: |
| 1516 | reason = ProcessMessage::eIllegalOperand; |
| 1517 | break; |
| 1518 | case ILL_ILLADR: |
| 1519 | reason = ProcessMessage::eIllegalAddressingMode; |
| 1520 | break; |
| 1521 | case ILL_ILLTRP: |
| 1522 | reason = ProcessMessage::eIllegalTrap; |
| 1523 | break; |
| 1524 | case ILL_PRVOPC: |
| 1525 | reason = ProcessMessage::ePrivilegedOpcode; |
| 1526 | break; |
| 1527 | case ILL_PRVREG: |
| 1528 | reason = ProcessMessage::ePrivilegedRegister; |
| 1529 | break; |
| 1530 | case ILL_COPROC: |
| 1531 | reason = ProcessMessage::eCoprocessorError; |
| 1532 | break; |
| 1533 | case ILL_BADSTK: |
| 1534 | reason = ProcessMessage::eInternalStackError; |
| 1535 | break; |
| 1536 | } |
| 1537 | |
| 1538 | return reason; |
| 1539 | } |
| 1540 | |
| 1541 | ProcessMessage::CrashReason |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 1542 | ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info) |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1543 | { |
| 1544 | ProcessMessage::CrashReason reason; |
| 1545 | assert(info->si_signo == SIGFPE); |
| 1546 | |
| 1547 | reason = ProcessMessage::eInvalidCrashReason; |
| 1548 | |
| 1549 | switch (info->si_code) |
| 1550 | { |
| 1551 | default: |
| 1552 | assert(false && "unexpected si_code for SIGFPE"); |
| 1553 | break; |
| 1554 | case FPE_INTDIV: |
| 1555 | reason = ProcessMessage::eIntegerDivideByZero; |
| 1556 | break; |
| 1557 | case FPE_INTOVF: |
| 1558 | reason = ProcessMessage::eIntegerOverflow; |
| 1559 | break; |
| 1560 | case FPE_FLTDIV: |
| 1561 | reason = ProcessMessage::eFloatDivideByZero; |
| 1562 | break; |
| 1563 | case FPE_FLTOVF: |
| 1564 | reason = ProcessMessage::eFloatOverflow; |
| 1565 | break; |
| 1566 | case FPE_FLTUND: |
| 1567 | reason = ProcessMessage::eFloatUnderflow; |
| 1568 | break; |
| 1569 | case FPE_FLTRES: |
| 1570 | reason = ProcessMessage::eFloatInexactResult; |
| 1571 | break; |
| 1572 | case FPE_FLTINV: |
| 1573 | reason = ProcessMessage::eFloatInvalidOperation; |
| 1574 | break; |
| 1575 | case FPE_FLTSUB: |
| 1576 | reason = ProcessMessage::eFloatSubscriptRange; |
| 1577 | break; |
| 1578 | } |
| 1579 | |
| 1580 | return reason; |
| 1581 | } |
| 1582 | |
| 1583 | ProcessMessage::CrashReason |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 1584 | ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info) |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1585 | { |
| 1586 | ProcessMessage::CrashReason reason; |
| 1587 | assert(info->si_signo == SIGBUS); |
| 1588 | |
| 1589 | reason = ProcessMessage::eInvalidCrashReason; |
| 1590 | |
| 1591 | switch (info->si_code) |
| 1592 | { |
| 1593 | default: |
| 1594 | assert(false && "unexpected si_code for SIGBUS"); |
| 1595 | break; |
| 1596 | case BUS_ADRALN: |
| 1597 | reason = ProcessMessage::eIllegalAlignment; |
| 1598 | break; |
| 1599 | case BUS_ADRERR: |
| 1600 | reason = ProcessMessage::eIllegalAddress; |
| 1601 | break; |
| 1602 | case BUS_OBJERR: |
| 1603 | reason = ProcessMessage::eHardwareError; |
| 1604 | break; |
| 1605 | } |
| 1606 | |
| 1607 | return reason; |
| 1608 | } |
| 1609 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1610 | void |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1611 | ProcessMonitor::ServeOperation(OperationArgs *args) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1612 | { |
| 1613 | int status; |
| 1614 | pollfd fdset; |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1615 | |
Stephen Wilson | 570243b | 2011-01-19 01:37:06 +0000 | [diff] [blame] | 1616 | ProcessMonitor *monitor = args->m_monitor; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1617 | |
| 1618 | fdset.fd = monitor->m_server_fd; |
| 1619 | fdset.events = POLLIN | POLLPRI; |
| 1620 | fdset.revents = 0; |
| 1621 | |
Stephen Wilson | 570243b | 2011-01-19 01:37:06 +0000 | [diff] [blame] | 1622 | // We are finised with the arguments and are ready to go. Sync with the |
| 1623 | // parent thread and start serving operations on the inferior. |
| 1624 | sem_post(&args->m_semaphore); |
| 1625 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1626 | for (;;) |
| 1627 | { |
| 1628 | if ((status = poll(&fdset, 1, -1)) < 0) |
| 1629 | { |
| 1630 | switch (errno) |
| 1631 | { |
| 1632 | default: |
| 1633 | assert(false && "Unexpected poll() failure!"); |
| 1634 | continue; |
| 1635 | |
| 1636 | case EINTR: continue; // Just poll again. |
| 1637 | case EBADF: return; // Connection terminated. |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | assert(status == 1 && "Too many descriptors!"); |
| 1642 | |
| 1643 | if (fdset.revents & POLLIN) |
| 1644 | { |
| 1645 | Operation *op = NULL; |
| 1646 | |
| 1647 | READ_AGAIN: |
| 1648 | if ((status = read(fdset.fd, &op, sizeof(op))) < 0) |
| 1649 | { |
| 1650 | // There is only one acceptable failure. |
| 1651 | assert(errno == EINTR); |
| 1652 | goto READ_AGAIN; |
| 1653 | } |
Matt Kopec | 9eb40a9 | 2013-03-14 21:35:26 +0000 | [diff] [blame] | 1654 | if (status == 0) |
| 1655 | continue; // Poll again. The connection probably terminated. |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1656 | assert(status == sizeof(op)); |
| 1657 | op->Execute(monitor); |
| 1658 | write(fdset.fd, &op, sizeof(op)); |
| 1659 | } |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | void |
| 1664 | ProcessMonitor::DoOperation(Operation *op) |
| 1665 | { |
| 1666 | int status; |
| 1667 | Operation *ack = NULL; |
| 1668 | Mutex::Locker lock(m_server_mutex); |
| 1669 | |
| 1670 | // FIXME: Do proper error checking here. |
| 1671 | write(m_client_fd, &op, sizeof(op)); |
| 1672 | |
| 1673 | READ_AGAIN: |
| 1674 | if ((status = read(m_client_fd, &ack, sizeof(ack))) < 0) |
| 1675 | { |
| 1676 | // If interrupted by a signal handler try again. Otherwise the monitor |
| 1677 | // thread probably died and we have a stale file descriptor -- abort the |
| 1678 | // operation. |
| 1679 | if (errno == EINTR) |
| 1680 | goto READ_AGAIN; |
| 1681 | return; |
| 1682 | } |
| 1683 | |
| 1684 | assert(status == sizeof(ack)); |
| 1685 | assert(ack == op && "Invalid monitor thread response!"); |
| 1686 | } |
| 1687 | |
| 1688 | size_t |
| 1689 | ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, |
| 1690 | Error &error) |
| 1691 | { |
| 1692 | size_t result; |
| 1693 | ReadOperation op(vm_addr, buf, size, error, result); |
| 1694 | DoOperation(&op); |
| 1695 | return result; |
| 1696 | } |
| 1697 | |
| 1698 | size_t |
| 1699 | ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, |
| 1700 | lldb_private::Error &error) |
| 1701 | { |
| 1702 | size_t result; |
| 1703 | WriteOperation op(vm_addr, buf, size, error, result); |
| 1704 | DoOperation(&op); |
| 1705 | return result; |
| 1706 | } |
| 1707 | |
| 1708 | bool |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1709 | ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, |
| 1710 | unsigned size, RegisterValue &value) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1711 | { |
| 1712 | bool result; |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 1713 | ReadRegOperation op(tid, offset, value, result); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1714 | DoOperation(&op); |
| 1715 | return result; |
| 1716 | } |
| 1717 | |
| 1718 | bool |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1719 | ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset, |
| 1720 | const RegisterValue &value) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1721 | { |
| 1722 | bool result; |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 1723 | WriteRegOperation op(tid, offset, value, result); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1724 | DoOperation(&op); |
| 1725 | return result; |
| 1726 | } |
| 1727 | |
| 1728 | bool |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1729 | ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 1730 | { |
| 1731 | bool result; |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1732 | ReadGPROperation op(tid, buf, buf_size, result); |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 1733 | DoOperation(&op); |
| 1734 | return result; |
| 1735 | } |
| 1736 | |
| 1737 | bool |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1738 | ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 1739 | { |
| 1740 | bool result; |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1741 | ReadFPROperation op(tid, buf, buf_size, result); |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 1742 | DoOperation(&op); |
| 1743 | return result; |
| 1744 | } |
| 1745 | |
| 1746 | bool |
Matt Kopec | 58c0b96 | 2013-03-20 20:34:35 +0000 | [diff] [blame^] | 1747 | ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) |
| 1748 | { |
| 1749 | bool result; |
| 1750 | ReadRegisterSetOperation op(tid, buf, buf_size, regset, result); |
| 1751 | DoOperation(&op); |
| 1752 | return result; |
| 1753 | } |
| 1754 | |
| 1755 | bool |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1756 | ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 1757 | { |
| 1758 | bool result; |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1759 | WriteGPROperation op(tid, buf, buf_size, result); |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 1760 | DoOperation(&op); |
| 1761 | return result; |
| 1762 | } |
| 1763 | |
| 1764 | bool |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1765 | ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 1766 | { |
| 1767 | bool result; |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 1768 | WriteFPROperation op(tid, buf, buf_size, result); |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 1769 | DoOperation(&op); |
| 1770 | return result; |
| 1771 | } |
| 1772 | |
| 1773 | bool |
Matt Kopec | 58c0b96 | 2013-03-20 20:34:35 +0000 | [diff] [blame^] | 1774 | ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) |
| 1775 | { |
| 1776 | bool result; |
| 1777 | WriteRegisterSetOperation op(tid, buf, buf_size, regset, result); |
| 1778 | DoOperation(&op); |
| 1779 | return result; |
| 1780 | } |
| 1781 | |
| 1782 | bool |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1783 | ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1784 | { |
| 1785 | bool result; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1786 | ResumeOperation op(tid, signo, result); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1787 | DoOperation(&op); |
| 1788 | return result; |
| 1789 | } |
| 1790 | |
| 1791 | bool |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1792 | ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1793 | { |
| 1794 | bool result; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1795 | SingleStepOperation op(tid, signo, result); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1796 | DoOperation(&op); |
| 1797 | return result; |
| 1798 | } |
| 1799 | |
| 1800 | bool |
| 1801 | ProcessMonitor::BringProcessIntoLimbo() |
| 1802 | { |
| 1803 | bool result; |
| 1804 | KillOperation op(result); |
| 1805 | DoOperation(&op); |
| 1806 | return result; |
| 1807 | } |
| 1808 | |
| 1809 | bool |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 1810 | ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1811 | { |
| 1812 | bool result; |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 1813 | SiginfoOperation op(tid, siginfo, result, ptrace_err); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1814 | DoOperation(&op); |
| 1815 | return result; |
| 1816 | } |
| 1817 | |
| 1818 | bool |
| 1819 | ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message) |
| 1820 | { |
| 1821 | bool result; |
| 1822 | EventMessageOperation op(tid, message, result); |
| 1823 | DoOperation(&op); |
| 1824 | return result; |
| 1825 | } |
| 1826 | |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1827 | lldb_private::Error |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1828 | ProcessMonitor::Detach() |
| 1829 | { |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 1830 | lldb_private::Error error; |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1831 | if (m_pid != LLDB_INVALID_PROCESS_ID) { |
| 1832 | DetachOperation op(error); |
| 1833 | DoOperation(&op); |
| 1834 | } |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1835 | return error; |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1836 | } |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1837 | |
| 1838 | bool |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1839 | ProcessMonitor::DupDescriptor(const char *path, int fd, int flags) |
| 1840 | { |
Peter Collingbourne | 6234320 | 2011-06-14 03:55:54 +0000 | [diff] [blame] | 1841 | int target_fd = open(path, flags, 0666); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1842 | |
| 1843 | if (target_fd == -1) |
| 1844 | return false; |
| 1845 | |
Peter Collingbourne | 6234320 | 2011-06-14 03:55:54 +0000 | [diff] [blame] | 1846 | return (dup2(target_fd, fd) == -1) ? false : true; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1847 | } |
Stephen Wilson | 9212d7f | 2011-01-04 21:40:25 +0000 | [diff] [blame] | 1848 | |
| 1849 | void |
| 1850 | ProcessMonitor::StopMonitoringChildProcess() |
| 1851 | { |
| 1852 | lldb::thread_result_t thread_result; |
| 1853 | |
Stephen Wilson | d4182f4 | 2011-02-09 20:10:35 +0000 | [diff] [blame] | 1854 | if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread)) |
Stephen Wilson | 9212d7f | 2011-01-04 21:40:25 +0000 | [diff] [blame] | 1855 | { |
| 1856 | Host::ThreadCancel(m_monitor_thread, NULL); |
| 1857 | Host::ThreadJoin(m_monitor_thread, &thread_result, NULL); |
| 1858 | m_monitor_thread = LLDB_INVALID_HOST_THREAD; |
| 1859 | } |
| 1860 | } |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1861 | |
| 1862 | void |
| 1863 | ProcessMonitor::StopMonitor() |
| 1864 | { |
| 1865 | StopMonitoringChildProcess(); |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1866 | StopOpThread(); |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1867 | CloseFD(m_terminal_fd); |
| 1868 | CloseFD(m_client_fd); |
| 1869 | CloseFD(m_server_fd); |
| 1870 | } |
| 1871 | |
| 1872 | void |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1873 | ProcessMonitor::StopOpThread() |
| 1874 | { |
| 1875 | lldb::thread_result_t result; |
| 1876 | |
| 1877 | if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread)) |
| 1878 | return; |
| 1879 | |
| 1880 | Host::ThreadCancel(m_operation_thread, NULL); |
| 1881 | Host::ThreadJoin(m_operation_thread, &result, NULL); |
Daniel Malea | 8b9e71e | 2012-11-22 18:21:05 +0000 | [diff] [blame] | 1882 | m_operation_thread = LLDB_INVALID_HOST_THREAD; |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | void |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1886 | ProcessMonitor::CloseFD(int &fd) |
| 1887 | { |
| 1888 | if (fd != -1) |
| 1889 | { |
| 1890 | close(fd); |
| 1891 | fd = -1; |
| 1892 | } |
| 1893 | } |