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