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