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