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