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