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