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