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