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