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> |
| 20 | #include <sys/types.h> |
Benjamin Kramer | c2b5c67 | 2012-04-07 09:13:49 +0000 | [diff] [blame] | 21 | #include <sys/user.h> |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 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 | |
| 40 | |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 41 | #define DEBUG_PTRACE_MAXBYTES 20 |
| 42 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 43 | using namespace lldb_private; |
| 44 | |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 45 | // FIXME: this code is host-dependent with respect to types and |
| 46 | // endianness and needs to be fixed. For example, lldb::addr_t is |
| 47 | // hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires |
| 48 | // 32-bit pointer arguments. This code uses casts to work around the |
| 49 | // problem. |
| 50 | |
| 51 | // We disable the tracing of ptrace calls for integration builds to |
| 52 | // avoid the additional indirection and checks. |
| 53 | #ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION |
| 54 | |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 55 | static void |
| 56 | DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count) |
| 57 | { |
| 58 | uint8_t *ptr = (uint8_t *)bytes; |
| 59 | const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); |
| 60 | for(uint32_t i=0; i<loop_count; i++) |
| 61 | { |
| 62 | s.Printf ("[%x]", *ptr); |
| 63 | ptr++; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static void PtraceDisplayBytes(__ptrace_request &req, void *data) |
| 68 | { |
| 69 | StreamString buf; |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 70 | LogSP verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet ( |
| 71 | POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE)); |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 72 | |
| 73 | if (verbose_log) |
| 74 | { |
| 75 | switch(req) |
| 76 | { |
| 77 | case PTRACE_POKETEXT: |
| 78 | { |
| 79 | DisplayBytes(buf, &data, 8); |
| 80 | verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData()); |
| 81 | break; |
| 82 | } |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 83 | case PTRACE_POKEDATA: |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 84 | { |
| 85 | DisplayBytes(buf, &data, 8); |
| 86 | verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData()); |
| 87 | break; |
| 88 | } |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 89 | case PTRACE_POKEUSER: |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 90 | { |
| 91 | DisplayBytes(buf, &data, 8); |
| 92 | verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData()); |
| 93 | break; |
| 94 | } |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 95 | case PTRACE_SETREGS: |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 96 | { |
| 97 | DisplayBytes(buf, data, sizeof(user_regs_struct)); |
| 98 | verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData()); |
| 99 | break; |
| 100 | } |
| 101 | case PTRACE_SETFPREGS: |
| 102 | { |
| 103 | DisplayBytes(buf, data, sizeof(user_fpregs_struct)); |
| 104 | verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData()); |
| 105 | break; |
| 106 | } |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 107 | case PTRACE_SETSIGINFO: |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 108 | { |
| 109 | DisplayBytes(buf, data, sizeof(siginfo_t)); |
| 110 | verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData()); |
| 111 | break; |
| 112 | } |
| 113 | default: |
| 114 | { |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 120 | // Wrapper for ptrace to catch errors and log calls. |
| 121 | extern long |
| 122 | PtraceWrapper(__ptrace_request req, pid_t pid, void *addr, void *data, |
| 123 | const char* reqName, const char* file, int line) |
| 124 | { |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 125 | long int result; |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 126 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 127 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE)); |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 128 | |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 129 | if (log) |
| 130 | log->Printf("ptrace(%s, %u, %p, %p) called from file %s line %d", |
| 131 | reqName, pid, addr, data, file, line); |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 132 | |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 133 | PtraceDisplayBytes(req, data); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 134 | |
| 135 | errno = 0; |
| 136 | result = ptrace(req, pid, addr, data); |
| 137 | |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 138 | PtraceDisplayBytes(req, data); |
| 139 | |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 140 | if (log && (result == -1 || errno != 0)) |
| 141 | { |
| 142 | const char* str; |
| 143 | switch (errno) |
| 144 | { |
| 145 | case ESRCH: str = "ESRCH"; break; |
| 146 | case EINVAL: str = "EINVAL"; break; |
| 147 | case EBUSY: str = "EBUSY"; break; |
| 148 | case EPERM: str = "EPERM"; break; |
| 149 | default: str = "<unknown>"; |
| 150 | } |
| 151 | log->Printf("ptrace() failed; errno=%d (%s)", errno, str); |
| 152 | } |
| 153 | |
| 154 | return result; |
| 155 | } |
| 156 | |
| 157 | #define PTRACE(req, pid, addr, data) \ |
| 158 | PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__) |
| 159 | #else |
| 160 | #define PTRACE ptrace |
| 161 | #endif |
| 162 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 163 | //------------------------------------------------------------------------------ |
| 164 | // Static implementations of ProcessMonitor::ReadMemory and |
| 165 | // ProcessMonitor::WriteMemory. This enables mutual recursion between these |
| 166 | // functions without needed to go thru the thread funnel. |
| 167 | |
| 168 | static size_t |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 169 | DoReadMemory(lldb::pid_t pid, |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 170 | lldb::addr_t vm_addr, void *buf, size_t size, Error &error) |
| 171 | { |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 172 | // ptrace word size is determined by the host, not the child |
| 173 | static const unsigned word_size = sizeof(void*); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 174 | unsigned char *dst = static_cast<unsigned char*>(buf); |
| 175 | size_t bytes_read; |
| 176 | size_t remainder; |
| 177 | long data; |
| 178 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 179 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 180 | if (log) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 181 | ProcessPOSIXLog::IncNestLevel(); |
| 182 | if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 183 | log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__, |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 184 | pid, word_size, (void*)vm_addr, buf, size); |
| 185 | |
| 186 | assert(sizeof(data) >= word_size); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 187 | for (bytes_read = 0; bytes_read < size; bytes_read += remainder) |
| 188 | { |
| 189 | errno = 0; |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 190 | data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 191 | if (data == -1L && errno) |
| 192 | { |
| 193 | error.SetErrorToErrno(); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 194 | if (log) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 195 | ProcessPOSIXLog::DecNestLevel(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 196 | return bytes_read; |
| 197 | } |
| 198 | |
| 199 | remainder = size - bytes_read; |
| 200 | remainder = remainder > word_size ? word_size : remainder; |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 201 | |
| 202 | // Copy the data into our buffer |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 203 | for (unsigned i = 0; i < remainder; ++i) |
| 204 | dst[i] = ((data >> i*8) & 0xFF); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 205 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 206 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 207 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || |
| 208 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && |
| 209 | size <= POSIX_LOG_MEMORY_SHORT_BYTES))) |
Daniel Malea | c63dddd | 2012-12-14 21:07:07 +0000 | [diff] [blame] | 210 | { |
| 211 | uintptr_t print_dst = 0; |
| 212 | // Format bytes from data by moving into print_dst for log output |
| 213 | for (unsigned i = 0; i < remainder; ++i) |
| 214 | print_dst |= (((data >> i*8) & 0xFF) << i*8); |
| 215 | log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, |
| 216 | (void*)vm_addr, print_dst, (unsigned long)data); |
| 217 | } |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 218 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 219 | vm_addr += word_size; |
| 220 | dst += word_size; |
| 221 | } |
| 222 | |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 223 | if (log) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 224 | ProcessPOSIXLog::DecNestLevel(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 225 | return bytes_read; |
| 226 | } |
| 227 | |
| 228 | static size_t |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 229 | DoWriteMemory(lldb::pid_t pid, |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 230 | lldb::addr_t vm_addr, const void *buf, size_t size, Error &error) |
| 231 | { |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 232 | // ptrace word size is determined by the host, not the child |
| 233 | static const unsigned word_size = sizeof(void*); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 234 | const unsigned char *src = static_cast<const unsigned char*>(buf); |
| 235 | size_t bytes_written = 0; |
| 236 | size_t remainder; |
| 237 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 238 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 239 | if (log) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 240 | ProcessPOSIXLog::IncNestLevel(); |
| 241 | if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 242 | log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__, |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 243 | pid, word_size, (void*)vm_addr, buf, size); |
| 244 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 245 | for (bytes_written = 0; bytes_written < size; bytes_written += remainder) |
| 246 | { |
| 247 | remainder = size - bytes_written; |
| 248 | remainder = remainder > word_size ? word_size : remainder; |
| 249 | |
| 250 | if (remainder == word_size) |
| 251 | { |
| 252 | unsigned long data = 0; |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 253 | assert(sizeof(data) >= word_size); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 254 | for (unsigned i = 0; i < word_size; ++i) |
| 255 | data |= (unsigned long)src[i] << i*8; |
| 256 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 257 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 258 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || |
| 259 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && |
| 260 | size <= POSIX_LOG_MEMORY_SHORT_BYTES))) |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 261 | log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, |
| 262 | (void*)vm_addr, *(unsigned long*)src, data); |
| 263 | |
| 264 | if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 265 | { |
| 266 | error.SetErrorToErrno(); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 267 | if (log) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 268 | ProcessPOSIXLog::DecNestLevel(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 269 | return bytes_written; |
| 270 | } |
| 271 | } |
| 272 | else |
| 273 | { |
| 274 | unsigned char buff[8]; |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 275 | if (DoReadMemory(pid, vm_addr, |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 276 | buff, word_size, error) != word_size) |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 277 | { |
| 278 | if (log) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 279 | ProcessPOSIXLog::DecNestLevel(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 280 | return bytes_written; |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 281 | } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 282 | |
| 283 | memcpy(buff, src, remainder); |
| 284 | |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 285 | if (DoWriteMemory(pid, vm_addr, |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 286 | buff, word_size, error) != word_size) |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 287 | { |
| 288 | if (log) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 289 | ProcessPOSIXLog::DecNestLevel(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 290 | return bytes_written; |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 293 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 294 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || |
| 295 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && |
| 296 | size <= POSIX_LOG_MEMORY_SHORT_BYTES))) |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 297 | log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, |
| 298 | (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | vm_addr += word_size; |
| 302 | src += word_size; |
| 303 | } |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 304 | if (log) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 305 | ProcessPOSIXLog::DecNestLevel(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 306 | return bytes_written; |
| 307 | } |
| 308 | |
Stephen Wilson | 2697716 | 2011-03-23 02:14:42 +0000 | [diff] [blame] | 309 | // Simple helper function to ensure flags are enabled on the given file |
| 310 | // descriptor. |
| 311 | static bool |
| 312 | EnsureFDFlags(int fd, int flags, Error &error) |
| 313 | { |
| 314 | int status; |
| 315 | |
| 316 | if ((status = fcntl(fd, F_GETFL)) == -1) |
| 317 | { |
| 318 | error.SetErrorToErrno(); |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | if (fcntl(fd, F_SETFL, status | flags) == -1) |
| 323 | { |
| 324 | error.SetErrorToErrno(); |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | return true; |
| 329 | } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 330 | |
| 331 | //------------------------------------------------------------------------------ |
| 332 | /// @class Operation |
| 333 | /// @brief Represents a ProcessMonitor operation. |
| 334 | /// |
| 335 | /// Under Linux, it is not possible to ptrace() from any other thread but the |
| 336 | /// one that spawned or attached to the process from the start. Therefore, when |
| 337 | /// a ProcessMonitor is asked to deliver or change the state of an inferior |
| 338 | /// process the operation must be "funneled" to a specific thread to perform the |
| 339 | /// task. The Operation class provides an abstract base for all services the |
| 340 | /// ProcessMonitor must perform via the single virtual function Execute, thus |
| 341 | /// encapsulating the code that needs to run in the privileged context. |
| 342 | class Operation |
| 343 | { |
| 344 | public: |
| 345 | virtual void Execute(ProcessMonitor *monitor) = 0; |
| 346 | }; |
| 347 | |
| 348 | //------------------------------------------------------------------------------ |
| 349 | /// @class ReadOperation |
| 350 | /// @brief Implements ProcessMonitor::ReadMemory. |
| 351 | class ReadOperation : public Operation |
| 352 | { |
| 353 | public: |
| 354 | ReadOperation(lldb::addr_t addr, void *buff, size_t size, |
| 355 | Error &error, size_t &result) |
| 356 | : m_addr(addr), m_buff(buff), m_size(size), |
| 357 | m_error(error), m_result(result) |
| 358 | { } |
| 359 | |
| 360 | void Execute(ProcessMonitor *monitor); |
| 361 | |
| 362 | private: |
| 363 | lldb::addr_t m_addr; |
| 364 | void *m_buff; |
| 365 | size_t m_size; |
| 366 | Error &m_error; |
| 367 | size_t &m_result; |
| 368 | }; |
| 369 | |
| 370 | void |
| 371 | ReadOperation::Execute(ProcessMonitor *monitor) |
| 372 | { |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 373 | lldb::pid_t pid = monitor->GetPID(); |
| 374 | |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 375 | m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | //------------------------------------------------------------------------------ |
| 379 | /// @class ReadOperation |
| 380 | /// @brief Implements ProcessMonitor::WriteMemory. |
| 381 | class WriteOperation : public Operation |
| 382 | { |
| 383 | public: |
| 384 | WriteOperation(lldb::addr_t addr, const void *buff, size_t size, |
| 385 | Error &error, size_t &result) |
| 386 | : m_addr(addr), m_buff(buff), m_size(size), |
| 387 | m_error(error), m_result(result) |
| 388 | { } |
| 389 | |
| 390 | void Execute(ProcessMonitor *monitor); |
| 391 | |
| 392 | private: |
| 393 | lldb::addr_t m_addr; |
| 394 | const void *m_buff; |
| 395 | size_t m_size; |
| 396 | Error &m_error; |
| 397 | size_t &m_result; |
| 398 | }; |
| 399 | |
| 400 | void |
| 401 | WriteOperation::Execute(ProcessMonitor *monitor) |
| 402 | { |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 403 | lldb::pid_t pid = monitor->GetPID(); |
| 404 | |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 405 | m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 408 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 409 | //------------------------------------------------------------------------------ |
| 410 | /// @class ReadRegOperation |
| 411 | /// @brief Implements ProcessMonitor::ReadRegisterValue. |
| 412 | class ReadRegOperation : public Operation |
| 413 | { |
| 414 | public: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 415 | ReadRegOperation(lldb::tid_t tid, unsigned offset, |
| 416 | RegisterValue &value, bool &result) |
| 417 | : m_tid(tid), m_offset(offset), |
| 418 | m_value(value), m_result(result) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 419 | { } |
| 420 | |
| 421 | void Execute(ProcessMonitor *monitor); |
| 422 | |
| 423 | private: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 424 | lldb::tid_t m_tid; |
Daniel Malea | a85e6b6 | 2012-12-07 22:21:08 +0000 | [diff] [blame] | 425 | uintptr_t m_offset; |
Johnny Chen | 13e8e1c | 2011-05-13 21:29:50 +0000 | [diff] [blame] | 426 | RegisterValue &m_value; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 427 | bool &m_result; |
| 428 | }; |
| 429 | |
| 430 | void |
| 431 | ReadRegOperation::Execute(ProcessMonitor *monitor) |
| 432 | { |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 433 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 434 | |
| 435 | // Set errno to zero so that we can detect a failed peek. |
| 436 | errno = 0; |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 437 | lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 438 | if (data == -1UL && errno) |
| 439 | m_result = false; |
| 440 | else |
| 441 | { |
| 442 | m_value = data; |
| 443 | m_result = true; |
| 444 | } |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 445 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 446 | log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__, |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 447 | POSIXThread::GetRegisterNameFromOffset(m_offset), data); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | //------------------------------------------------------------------------------ |
| 451 | /// @class WriteRegOperation |
| 452 | /// @brief Implements ProcessMonitor::WriteRegisterValue. |
| 453 | class WriteRegOperation : public Operation |
| 454 | { |
| 455 | public: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 456 | WriteRegOperation(lldb::tid_t tid, unsigned offset, |
| 457 | const RegisterValue &value, bool &result) |
| 458 | : m_tid(tid), m_offset(offset), |
| 459 | m_value(value), m_result(result) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 460 | { } |
| 461 | |
| 462 | void Execute(ProcessMonitor *monitor); |
| 463 | |
| 464 | private: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 465 | lldb::tid_t m_tid; |
Daniel Malea | a85e6b6 | 2012-12-07 22:21:08 +0000 | [diff] [blame] | 466 | uintptr_t m_offset; |
Johnny Chen | 13e8e1c | 2011-05-13 21:29:50 +0000 | [diff] [blame] | 467 | const RegisterValue &m_value; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 468 | bool &m_result; |
| 469 | }; |
| 470 | |
| 471 | void |
| 472 | WriteRegOperation::Execute(ProcessMonitor *monitor) |
| 473 | { |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 474 | void* buf; |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 475 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 476 | |
Daniel Malea | a85e6b6 | 2012-12-07 22:21:08 +0000 | [diff] [blame] | 477 | #if __WORDSIZE == 32 |
| 478 | buf = (void*) m_value.GetAsUInt32(); |
| 479 | #else |
| 480 | buf = (void*) m_value.GetAsUInt64(); |
| 481 | #endif |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 482 | |
| 483 | if (log) |
| 484 | log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 485 | POSIXThread::GetRegisterNameFromOffset(m_offset), buf); |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 486 | if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 487 | m_result = false; |
| 488 | else |
| 489 | m_result = true; |
| 490 | } |
| 491 | |
| 492 | //------------------------------------------------------------------------------ |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 493 | /// @class ReadGPROperation |
| 494 | /// @brief Implements ProcessMonitor::ReadGPR. |
| 495 | class ReadGPROperation : public Operation |
| 496 | { |
| 497 | public: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 498 | ReadGPROperation(lldb::tid_t tid, void *buf, bool &result) |
| 499 | : m_tid(tid), m_buf(buf), m_result(result) |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 500 | { } |
| 501 | |
| 502 | void Execute(ProcessMonitor *monitor); |
| 503 | |
| 504 | private: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 505 | lldb::tid_t m_tid; |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 506 | void *m_buf; |
| 507 | bool &m_result; |
| 508 | }; |
| 509 | |
| 510 | void |
| 511 | ReadGPROperation::Execute(ProcessMonitor *monitor) |
| 512 | { |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 513 | if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf) < 0) |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 514 | m_result = false; |
| 515 | else |
| 516 | m_result = true; |
| 517 | } |
| 518 | |
| 519 | //------------------------------------------------------------------------------ |
| 520 | /// @class ReadFPROperation |
| 521 | /// @brief Implements ProcessMonitor::ReadFPR. |
| 522 | class ReadFPROperation : public Operation |
| 523 | { |
| 524 | public: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 525 | ReadFPROperation(lldb::tid_t tid, void *buf, bool &result) |
| 526 | : m_tid(tid), m_buf(buf), m_result(result) |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 527 | { } |
| 528 | |
| 529 | void Execute(ProcessMonitor *monitor); |
| 530 | |
| 531 | private: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 532 | lldb::tid_t m_tid; |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 533 | void *m_buf; |
| 534 | bool &m_result; |
| 535 | }; |
| 536 | |
| 537 | void |
| 538 | ReadFPROperation::Execute(ProcessMonitor *monitor) |
| 539 | { |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 540 | if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf) < 0) |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 541 | m_result = false; |
| 542 | else |
| 543 | m_result = true; |
| 544 | } |
| 545 | |
| 546 | //------------------------------------------------------------------------------ |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 547 | /// @class WriteGPROperation |
| 548 | /// @brief Implements ProcessMonitor::WriteGPR. |
| 549 | class WriteGPROperation : public Operation |
| 550 | { |
| 551 | public: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 552 | WriteGPROperation(lldb::tid_t tid, void *buf, bool &result) |
| 553 | : m_tid(tid), m_buf(buf), m_result(result) |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 554 | { } |
| 555 | |
| 556 | void Execute(ProcessMonitor *monitor); |
| 557 | |
| 558 | private: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 559 | lldb::tid_t m_tid; |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 560 | void *m_buf; |
| 561 | bool &m_result; |
| 562 | }; |
| 563 | |
| 564 | void |
| 565 | WriteGPROperation::Execute(ProcessMonitor *monitor) |
| 566 | { |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 567 | if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf) < 0) |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 568 | m_result = false; |
| 569 | else |
| 570 | m_result = true; |
| 571 | } |
| 572 | |
| 573 | //------------------------------------------------------------------------------ |
| 574 | /// @class WriteFPROperation |
| 575 | /// @brief Implements ProcessMonitor::WriteFPR. |
| 576 | class WriteFPROperation : public Operation |
| 577 | { |
| 578 | public: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 579 | WriteFPROperation(lldb::tid_t tid, void *buf, bool &result) |
| 580 | : m_tid(tid), m_buf(buf), m_result(result) |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 581 | { } |
| 582 | |
| 583 | void Execute(ProcessMonitor *monitor); |
| 584 | |
| 585 | private: |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 586 | lldb::tid_t m_tid; |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 587 | void *m_buf; |
| 588 | bool &m_result; |
| 589 | }; |
| 590 | |
| 591 | void |
| 592 | WriteFPROperation::Execute(ProcessMonitor *monitor) |
| 593 | { |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 594 | if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf) < 0) |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 595 | m_result = false; |
| 596 | else |
| 597 | m_result = true; |
| 598 | } |
| 599 | |
| 600 | //------------------------------------------------------------------------------ |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 601 | /// @class ResumeOperation |
| 602 | /// @brief Implements ProcessMonitor::Resume. |
| 603 | class ResumeOperation : public Operation |
| 604 | { |
| 605 | public: |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 606 | ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) : |
| 607 | m_tid(tid), m_signo(signo), m_result(result) { } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 608 | |
| 609 | void Execute(ProcessMonitor *monitor); |
| 610 | |
| 611 | private: |
| 612 | lldb::tid_t m_tid; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 613 | uint32_t m_signo; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 614 | bool &m_result; |
| 615 | }; |
| 616 | |
| 617 | void |
| 618 | ResumeOperation::Execute(ProcessMonitor *monitor) |
| 619 | { |
Daniel Malea | a85e6b6 | 2012-12-07 22:21:08 +0000 | [diff] [blame] | 620 | intptr_t data = 0; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 621 | |
| 622 | if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 623 | data = m_signo; |
| 624 | |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 625 | if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 626 | m_result = false; |
| 627 | else |
| 628 | m_result = true; |
| 629 | } |
| 630 | |
| 631 | //------------------------------------------------------------------------------ |
| 632 | /// @class ResumeOperation |
| 633 | /// @brief Implements ProcessMonitor::SingleStep. |
| 634 | class SingleStepOperation : public Operation |
| 635 | { |
| 636 | public: |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 637 | SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result) |
| 638 | : m_tid(tid), m_signo(signo), m_result(result) { } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 639 | |
| 640 | void Execute(ProcessMonitor *monitor); |
| 641 | |
| 642 | private: |
| 643 | lldb::tid_t m_tid; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 644 | uint32_t m_signo; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 645 | bool &m_result; |
| 646 | }; |
| 647 | |
| 648 | void |
| 649 | SingleStepOperation::Execute(ProcessMonitor *monitor) |
| 650 | { |
Daniel Malea | a85e6b6 | 2012-12-07 22:21:08 +0000 | [diff] [blame] | 651 | intptr_t data = 0; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 652 | |
| 653 | if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 654 | data = m_signo; |
| 655 | |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 656 | if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 657 | m_result = false; |
| 658 | else |
| 659 | m_result = true; |
| 660 | } |
| 661 | |
| 662 | //------------------------------------------------------------------------------ |
| 663 | /// @class SiginfoOperation |
| 664 | /// @brief Implements ProcessMonitor::GetSignalInfo. |
| 665 | class SiginfoOperation : public Operation |
| 666 | { |
| 667 | public: |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 668 | SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err) |
| 669 | : 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] | 670 | |
| 671 | void Execute(ProcessMonitor *monitor); |
| 672 | |
| 673 | private: |
| 674 | lldb::tid_t m_tid; |
| 675 | void *m_info; |
| 676 | bool &m_result; |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 677 | int &m_err; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 678 | }; |
| 679 | |
| 680 | void |
| 681 | SiginfoOperation::Execute(ProcessMonitor *monitor) |
| 682 | { |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 683 | if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info)) { |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 684 | m_result = false; |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 685 | m_err = errno; |
| 686 | } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 687 | else |
| 688 | m_result = true; |
| 689 | } |
| 690 | |
| 691 | //------------------------------------------------------------------------------ |
| 692 | /// @class EventMessageOperation |
| 693 | /// @brief Implements ProcessMonitor::GetEventMessage. |
| 694 | class EventMessageOperation : public Operation |
| 695 | { |
| 696 | public: |
| 697 | EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result) |
| 698 | : m_tid(tid), m_message(message), m_result(result) { } |
| 699 | |
| 700 | void Execute(ProcessMonitor *monitor); |
| 701 | |
| 702 | private: |
| 703 | lldb::tid_t m_tid; |
| 704 | unsigned long *m_message; |
| 705 | bool &m_result; |
| 706 | }; |
| 707 | |
| 708 | void |
| 709 | EventMessageOperation::Execute(ProcessMonitor *monitor) |
| 710 | { |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 711 | if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 712 | m_result = false; |
| 713 | else |
| 714 | m_result = true; |
| 715 | } |
| 716 | |
| 717 | //------------------------------------------------------------------------------ |
| 718 | /// @class KillOperation |
| 719 | /// @brief Implements ProcessMonitor::BringProcessIntoLimbo. |
| 720 | class KillOperation : public Operation |
| 721 | { |
| 722 | public: |
| 723 | KillOperation(bool &result) : m_result(result) { } |
| 724 | |
| 725 | void Execute(ProcessMonitor *monitor); |
| 726 | |
| 727 | private: |
| 728 | bool &m_result; |
| 729 | }; |
| 730 | |
| 731 | void |
| 732 | KillOperation::Execute(ProcessMonitor *monitor) |
| 733 | { |
| 734 | lldb::pid_t pid = monitor->GetPID(); |
| 735 | |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 736 | if (PTRACE(PTRACE_KILL, pid, NULL, NULL)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 737 | m_result = false; |
| 738 | else |
| 739 | m_result = true; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 742 | //------------------------------------------------------------------------------ |
| 743 | /// @class KillOperation |
| 744 | /// @brief Implements ProcessMonitor::BringProcessIntoLimbo. |
| 745 | class DetachOperation : public Operation |
| 746 | { |
| 747 | public: |
| 748 | DetachOperation(Error &result) : m_error(result) { } |
| 749 | |
| 750 | void Execute(ProcessMonitor *monitor); |
| 751 | |
| 752 | private: |
| 753 | Error &m_error; |
| 754 | }; |
| 755 | |
| 756 | void |
| 757 | DetachOperation::Execute(ProcessMonitor *monitor) |
| 758 | { |
| 759 | lldb::pid_t pid = monitor->GetPID(); |
| 760 | |
| 761 | if (ptrace(PT_DETACH, pid, NULL, 0) < 0) |
| 762 | m_error.SetErrorToErrno(); |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 763 | |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 766 | ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor) |
| 767 | : m_monitor(monitor) |
| 768 | { |
| 769 | sem_init(&m_semaphore, 0, 0); |
| 770 | } |
| 771 | |
| 772 | ProcessMonitor::OperationArgs::~OperationArgs() |
| 773 | { |
| 774 | sem_destroy(&m_semaphore); |
| 775 | } |
| 776 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 777 | ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor, |
| 778 | lldb_private::Module *module, |
| 779 | char const **argv, |
| 780 | char const **envp, |
| 781 | const char *stdin_path, |
| 782 | const char *stdout_path, |
| 783 | const char *stderr_path) |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 784 | : OperationArgs(monitor), |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 785 | m_module(module), |
| 786 | m_argv(argv), |
| 787 | m_envp(envp), |
| 788 | m_stdin_path(stdin_path), |
| 789 | m_stdout_path(stdout_path), |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 790 | m_stderr_path(stderr_path) { } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 791 | |
| 792 | ProcessMonitor::LaunchArgs::~LaunchArgs() |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 793 | { } |
| 794 | |
| 795 | ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor, |
| 796 | lldb::pid_t pid) |
| 797 | : OperationArgs(monitor), m_pid(pid) { } |
| 798 | |
| 799 | ProcessMonitor::AttachArgs::~AttachArgs() |
| 800 | { } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 801 | |
| 802 | //------------------------------------------------------------------------------ |
| 803 | /// The basic design of the ProcessMonitor is built around two threads. |
| 804 | /// |
| 805 | /// One thread (@see SignalThread) simply blocks on a call to waitpid() looking |
| 806 | /// for changes in the debugee state. When a change is detected a |
| 807 | /// ProcessMessage is sent to the associated ProcessLinux instance. This thread |
| 808 | /// "drives" state changes in the debugger. |
| 809 | /// |
| 810 | /// The second thread (@see OperationThread) is responsible for two things 1) |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 811 | /// launching or attaching to the inferior process, and then 2) servicing |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 812 | /// operations such as register reads/writes, stepping, etc. See the comments |
| 813 | /// on the Operation class for more info as to why this is needed. |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 814 | ProcessMonitor::ProcessMonitor(ProcessPOSIX *process, |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 815 | Module *module, |
| 816 | const char *argv[], |
| 817 | const char *envp[], |
| 818 | const char *stdin_path, |
| 819 | const char *stdout_path, |
| 820 | const char *stderr_path, |
| 821 | lldb_private::Error &error) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 822 | : m_process(static_cast<ProcessLinux *>(process)), |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 823 | m_operation_thread(LLDB_INVALID_HOST_THREAD), |
| 824 | m_pid(LLDB_INVALID_PROCESS_ID), |
| 825 | m_terminal_fd(-1), |
Stephen Wilson | 9212d7f | 2011-01-04 21:40:25 +0000 | [diff] [blame] | 826 | m_monitor_thread(LLDB_INVALID_HOST_THREAD), |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 827 | m_client_fd(-1), |
| 828 | m_server_fd(-1) |
| 829 | { |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 830 | std::auto_ptr<LaunchArgs> args; |
| 831 | |
| 832 | args.reset(new LaunchArgs(this, module, argv, envp, |
| 833 | stdin_path, stdout_path, stderr_path)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 834 | |
| 835 | // Server/client descriptors. |
| 836 | if (!EnableIPC()) |
| 837 | { |
| 838 | error.SetErrorToGenericError(); |
| 839 | error.SetErrorString("Monitor failed to initialize."); |
| 840 | } |
| 841 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 842 | StartLaunchOpThread(args.get(), error); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 843 | if (!error.Success()) |
| 844 | return; |
| 845 | |
| 846 | WAIT_AGAIN: |
| 847 | // Wait for the operation thread to initialize. |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 848 | if (sem_wait(&args->m_semaphore)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 849 | { |
| 850 | if (errno == EINTR) |
| 851 | goto WAIT_AGAIN; |
| 852 | else |
| 853 | { |
| 854 | error.SetErrorToErrno(); |
| 855 | return; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | // Check that the launch was a success. |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 860 | if (!args->m_error.Success()) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 861 | { |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 862 | StopOpThread(); |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 863 | error = args->m_error; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 864 | return; |
| 865 | } |
| 866 | |
| 867 | // Finally, start monitoring the child process for change in state. |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 868 | m_monitor_thread = Host::StartMonitoringChildProcess( |
| 869 | ProcessMonitor::MonitorCallback, this, GetPID(), true); |
Stephen Wilson | d4182f4 | 2011-02-09 20:10:35 +0000 | [diff] [blame] | 870 | if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 871 | { |
| 872 | error.SetErrorToGenericError(); |
| 873 | error.SetErrorString("Process launch failed."); |
| 874 | return; |
| 875 | } |
| 876 | } |
| 877 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 878 | ProcessMonitor::ProcessMonitor(ProcessPOSIX *process, |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 879 | lldb::pid_t pid, |
| 880 | lldb_private::Error &error) |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 881 | : m_process(static_cast<ProcessLinux *>(process)), |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 882 | m_operation_thread(LLDB_INVALID_HOST_THREAD), |
| 883 | m_pid(LLDB_INVALID_PROCESS_ID), |
| 884 | m_terminal_fd(-1), |
| 885 | m_monitor_thread(LLDB_INVALID_HOST_THREAD), |
| 886 | m_client_fd(-1), |
| 887 | m_server_fd(-1) |
| 888 | { |
| 889 | std::auto_ptr<AttachArgs> args; |
| 890 | |
| 891 | args.reset(new AttachArgs(this, pid)); |
| 892 | |
| 893 | // Server/client descriptors. |
| 894 | if (!EnableIPC()) |
| 895 | { |
| 896 | error.SetErrorToGenericError(); |
| 897 | error.SetErrorString("Monitor failed to initialize."); |
| 898 | } |
| 899 | |
| 900 | StartAttachOpThread(args.get(), error); |
| 901 | if (!error.Success()) |
| 902 | return; |
| 903 | |
| 904 | WAIT_AGAIN: |
| 905 | // Wait for the operation thread to initialize. |
| 906 | if (sem_wait(&args->m_semaphore)) |
| 907 | { |
| 908 | if (errno == EINTR) |
| 909 | goto WAIT_AGAIN; |
| 910 | else |
| 911 | { |
| 912 | error.SetErrorToErrno(); |
| 913 | return; |
| 914 | } |
| 915 | } |
| 916 | |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 917 | // Check that the attach was a success. |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 918 | if (!args->m_error.Success()) |
| 919 | { |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 920 | StopOpThread(); |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 921 | error = args->m_error; |
| 922 | return; |
| 923 | } |
| 924 | |
| 925 | // Finally, start monitoring the child process for change in state. |
| 926 | m_monitor_thread = Host::StartMonitoringChildProcess( |
| 927 | ProcessMonitor::MonitorCallback, this, GetPID(), true); |
| 928 | if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread)) |
| 929 | { |
| 930 | error.SetErrorToGenericError(); |
| 931 | error.SetErrorString("Process attach failed."); |
| 932 | return; |
| 933 | } |
| 934 | } |
| 935 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 936 | ProcessMonitor::~ProcessMonitor() |
| 937 | { |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 938 | StopMonitor(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | //------------------------------------------------------------------------------ |
| 942 | // Thread setup and tear down. |
| 943 | void |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 944 | ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 945 | { |
| 946 | static const char *g_thread_name = "lldb.process.linux.operation"; |
| 947 | |
Stephen Wilson | d4182f4 | 2011-02-09 20:10:35 +0000 | [diff] [blame] | 948 | if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread)) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 949 | return; |
| 950 | |
| 951 | m_operation_thread = |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 952 | Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 955 | void * |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 956 | ProcessMonitor::LaunchOpThread(void *arg) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 957 | { |
| 958 | LaunchArgs *args = static_cast<LaunchArgs*>(arg); |
| 959 | |
Peter Collingbourne | 4aeb47e | 2011-06-14 03:55:49 +0000 | [diff] [blame] | 960 | if (!Launch(args)) { |
| 961 | sem_post(&args->m_semaphore); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 962 | return NULL; |
Peter Collingbourne | 4aeb47e | 2011-06-14 03:55:49 +0000 | [diff] [blame] | 963 | } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 964 | |
Stephen Wilson | 570243b | 2011-01-19 01:37:06 +0000 | [diff] [blame] | 965 | ServeOperation(args); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 966 | return NULL; |
| 967 | } |
| 968 | |
| 969 | bool |
| 970 | ProcessMonitor::Launch(LaunchArgs *args) |
| 971 | { |
| 972 | ProcessMonitor *monitor = args->m_monitor; |
| 973 | ProcessLinux &process = monitor->GetProcess(); |
| 974 | const char **argv = args->m_argv; |
| 975 | const char **envp = args->m_envp; |
| 976 | const char *stdin_path = args->m_stdin_path; |
| 977 | const char *stdout_path = args->m_stdout_path; |
| 978 | const char *stderr_path = args->m_stderr_path; |
| 979 | |
| 980 | lldb_utility::PseudoTerminal terminal; |
| 981 | const size_t err_len = 1024; |
| 982 | char err_str[err_len]; |
| 983 | lldb::pid_t pid; |
| 984 | |
| 985 | lldb::ThreadSP inferior; |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 986 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 987 | |
Stephen Wilson | 57740ec | 2011-01-15 00:12:41 +0000 | [diff] [blame] | 988 | // Propagate the environment if one is not supplied. |
| 989 | if (envp == NULL || envp[0] == NULL) |
| 990 | envp = const_cast<const char **>(environ); |
| 991 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 992 | // Pseudo terminal setup. |
| 993 | if (!terminal.OpenFirstAvailableMaster(O_RDWR | O_NOCTTY, err_str, err_len)) |
| 994 | { |
| 995 | args->m_error.SetErrorToGenericError(); |
| 996 | args->m_error.SetErrorString("Could not open controlling TTY."); |
| 997 | goto FINISH; |
| 998 | } |
| 999 | |
Daniel Malea | a85e6b6 | 2012-12-07 22:21:08 +0000 | [diff] [blame] | 1000 | if ((pid = terminal.Fork(err_str, err_len)) == -1) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1001 | { |
| 1002 | args->m_error.SetErrorToGenericError(); |
| 1003 | args->m_error.SetErrorString("Process fork failed."); |
| 1004 | goto FINISH; |
| 1005 | } |
| 1006 | |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1007 | // Recognized child exit status codes. |
| 1008 | enum { |
| 1009 | ePtraceFailed = 1, |
| 1010 | eDupStdinFailed, |
| 1011 | eDupStdoutFailed, |
| 1012 | eDupStderrFailed, |
| 1013 | eExecFailed |
| 1014 | }; |
| 1015 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1016 | // Child process. |
| 1017 | if (pid == 0) |
| 1018 | { |
| 1019 | // Trace this process. |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 1020 | if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL) < 0) |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1021 | exit(ePtraceFailed); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1022 | |
| 1023 | // Do not inherit setgid powers. |
| 1024 | setgid(getgid()); |
| 1025 | |
| 1026 | // Let us have our own process group. |
| 1027 | setpgid(0, 0); |
| 1028 | |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 1029 | // Dup file descriptors if needed. |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1030 | // |
| 1031 | // FIXME: If two or more of the paths are the same we needlessly open |
| 1032 | // the same file multiple times. |
| 1033 | if (stdin_path != NULL && stdin_path[0]) |
Peter Collingbourne | 6234320 | 2011-06-14 03:55:54 +0000 | [diff] [blame] | 1034 | if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY)) |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1035 | exit(eDupStdinFailed); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1036 | |
| 1037 | if (stdout_path != NULL && stdout_path[0]) |
| 1038 | if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT)) |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1039 | exit(eDupStdoutFailed); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1040 | |
| 1041 | if (stderr_path != NULL && stderr_path[0]) |
Peter Collingbourne | 6234320 | 2011-06-14 03:55:54 +0000 | [diff] [blame] | 1042 | if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT)) |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1043 | exit(eDupStderrFailed); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1044 | |
| 1045 | // Execute. We should never return. |
| 1046 | execve(argv[0], |
| 1047 | const_cast<char *const *>(argv), |
| 1048 | const_cast<char *const *>(envp)); |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1049 | exit(eExecFailed); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | // 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] | 1053 | pid_t wpid; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1054 | int status; |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1055 | if ((wpid = waitpid(pid, &status, 0)) < 0) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1056 | { |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1057 | args->m_error.SetErrorToErrno(); |
| 1058 | goto FINISH; |
| 1059 | } |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1060 | else if (WIFEXITED(status)) |
| 1061 | { |
| 1062 | // open, dup or execve likely failed for some reason. |
| 1063 | args->m_error.SetErrorToGenericError(); |
| 1064 | switch (WEXITSTATUS(status)) |
| 1065 | { |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1066 | case ePtraceFailed: |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1067 | args->m_error.SetErrorString("Child ptrace failed."); |
| 1068 | break; |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1069 | case eDupStdinFailed: |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1070 | args->m_error.SetErrorString("Child open stdin failed."); |
| 1071 | break; |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1072 | case eDupStdoutFailed: |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1073 | args->m_error.SetErrorString("Child open stdout failed."); |
| 1074 | break; |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1075 | case eDupStderrFailed: |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1076 | args->m_error.SetErrorString("Child open stderr failed."); |
| 1077 | break; |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1078 | case eExecFailed: |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1079 | args->m_error.SetErrorString("Child exec failed."); |
| 1080 | break; |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1081 | default: |
Peter Collingbourne | 6a52022 | 2011-06-14 03:55:58 +0000 | [diff] [blame] | 1082 | args->m_error.SetErrorString("Child returned unknown exit status."); |
| 1083 | break; |
| 1084 | } |
| 1085 | goto FINISH; |
| 1086 | } |
| 1087 | assert(WIFSTOPPED(status) && wpid == pid && |
| 1088 | "Could not sync with inferior process."); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1089 | |
| 1090 | // Have the child raise an event on exit. This is used to keep the child in |
| 1091 | // limbo until it is destroyed. |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 1092 | if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)PTRACE_O_TRACEEXIT) < 0) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1093 | { |
| 1094 | args->m_error.SetErrorToErrno(); |
| 1095 | goto FINISH; |
| 1096 | } |
| 1097 | |
| 1098 | // Release the master terminal descriptor and pass it off to the |
| 1099 | // ProcessMonitor instance. Similarly stash the inferior pid. |
| 1100 | monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); |
| 1101 | monitor->m_pid = pid; |
| 1102 | |
Stephen Wilson | 2697716 | 2011-03-23 02:14:42 +0000 | [diff] [blame] | 1103 | // Set the terminal fd to be in non blocking mode (it simplifies the |
| 1104 | // implementation of ProcessLinux::GetSTDOUT to have a non-blocking |
| 1105 | // descriptor to read from). |
| 1106 | if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error)) |
| 1107 | goto FINISH; |
| 1108 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 1109 | // Update the process thread list with this new thread. |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 1110 | // FIXME: should we be letting UpdateThreadList handle this? |
| 1111 | // FIXME: by using pids instead of tids, we can only support one thread. |
Greg Clayton | df3df25 | 2012-10-12 16:23:23 +0000 | [diff] [blame] | 1112 | inferior.reset(new POSIXThread(process, pid)); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 1113 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1114 | log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1115 | process.GetThreadList().AddThread(inferior); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1116 | |
| 1117 | // Let our process instance know the thread has stopped. |
| 1118 | process.SendMessage(ProcessMessage::Trace(pid)); |
| 1119 | |
| 1120 | FINISH: |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1121 | return args->m_error.Success(); |
| 1122 | } |
| 1123 | |
| 1124 | bool |
| 1125 | ProcessMonitor::EnableIPC() |
| 1126 | { |
| 1127 | int fd[2]; |
| 1128 | |
| 1129 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) |
| 1130 | return false; |
| 1131 | |
| 1132 | m_client_fd = fd[0]; |
| 1133 | m_server_fd = fd[1]; |
| 1134 | return true; |
| 1135 | } |
| 1136 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1137 | void |
| 1138 | ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error) |
| 1139 | { |
| 1140 | static const char *g_thread_name = "lldb.process.linux.operation"; |
| 1141 | |
| 1142 | if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread)) |
| 1143 | return; |
| 1144 | |
| 1145 | m_operation_thread = |
| 1146 | Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error); |
| 1147 | } |
| 1148 | |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1149 | void * |
| 1150 | ProcessMonitor::AttachOpThread(void *arg) |
| 1151 | { |
| 1152 | AttachArgs *args = static_cast<AttachArgs*>(arg); |
| 1153 | |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1154 | if (!Attach(args)) { |
| 1155 | sem_post(&args->m_semaphore); |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1156 | return NULL; |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1157 | } |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1158 | |
| 1159 | ServeOperation(args); |
| 1160 | return NULL; |
| 1161 | } |
| 1162 | |
| 1163 | bool |
| 1164 | ProcessMonitor::Attach(AttachArgs *args) |
| 1165 | { |
| 1166 | lldb::pid_t pid = args->m_pid; |
| 1167 | |
| 1168 | ProcessMonitor *monitor = args->m_monitor; |
| 1169 | ProcessLinux &process = monitor->GetProcess(); |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1170 | lldb::ThreadSP inferior; |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 1171 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1172 | |
| 1173 | if (pid <= 1) |
| 1174 | { |
| 1175 | args->m_error.SetErrorToGenericError(); |
| 1176 | args->m_error.SetErrorString("Attaching to process 1 is not allowed."); |
| 1177 | goto FINISH; |
| 1178 | } |
| 1179 | |
| 1180 | // Attach to the requested process. |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 1181 | if (PTRACE(PTRACE_ATTACH, pid, NULL, NULL) < 0) |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1182 | { |
| 1183 | args->m_error.SetErrorToErrno(); |
| 1184 | goto FINISH; |
| 1185 | } |
| 1186 | |
| 1187 | int status; |
| 1188 | if ((status = waitpid(pid, NULL, 0)) < 0) |
| 1189 | { |
| 1190 | args->m_error.SetErrorToErrno(); |
| 1191 | goto FINISH; |
| 1192 | } |
| 1193 | |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 1194 | monitor->m_pid = pid; |
| 1195 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 1196 | // Update the process thread list with the attached thread. |
Greg Clayton | df3df25 | 2012-10-12 16:23:23 +0000 | [diff] [blame] | 1197 | inferior.reset(new POSIXThread(process, pid)); |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 1198 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1199 | log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, pid); |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1200 | process.GetThreadList().AddThread(inferior); |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1201 | |
| 1202 | // Let our process instance know the thread has stopped. |
| 1203 | process.SendMessage(ProcessMessage::Trace(pid)); |
| 1204 | |
| 1205 | FINISH: |
| 1206 | return args->m_error.Success(); |
| 1207 | } |
| 1208 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1209 | bool |
| 1210 | ProcessMonitor::MonitorCallback(void *callback_baton, |
| 1211 | lldb::pid_t pid, |
Peter Collingbourne | 2c67b9a | 2011-11-21 00:10:19 +0000 | [diff] [blame] | 1212 | bool exited, |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1213 | int signal, |
| 1214 | int status) |
| 1215 | { |
| 1216 | ProcessMessage message; |
| 1217 | ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton); |
| 1218 | ProcessLinux *process = monitor->m_process; |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 1219 | assert(process); |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1220 | bool stop_monitoring; |
| 1221 | siginfo_t info; |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 1222 | int ptrace_err; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1223 | |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 1224 | if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) { |
| 1225 | if (ptrace_err == EINVAL) { |
| 1226 | // inferior process is in 'group-stop', so deliver SIGSTOP signal |
| 1227 | if (!monitor->Resume(pid, SIGSTOP)) { |
| 1228 | assert(0 && "SIGSTOP delivery failed while in 'group-stop' state"); |
| 1229 | } |
| 1230 | stop_monitoring = false; |
| 1231 | } else { |
| 1232 | // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely, |
| 1233 | // this means the child pid is gone (or not being debugged) therefore |
| 1234 | // stop the monitor thread. |
| 1235 | stop_monitoring = true; |
| 1236 | } |
| 1237 | } |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1238 | else { |
| 1239 | switch (info.si_signo) |
| 1240 | { |
| 1241 | case SIGTRAP: |
| 1242 | message = MonitorSIGTRAP(monitor, &info, pid); |
| 1243 | break; |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1244 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1245 | default: |
| 1246 | message = MonitorSignal(monitor, &info, pid); |
| 1247 | break; |
| 1248 | } |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1249 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1250 | process->SendMessage(message); |
Daniel Malea | 8b9e71e | 2012-11-22 18:21:05 +0000 | [diff] [blame] | 1251 | stop_monitoring = !process->IsAlive(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1254 | return stop_monitoring; |
| 1255 | } |
| 1256 | |
| 1257 | ProcessMessage |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1258 | ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor, |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 1259 | const siginfo_t *info, lldb::pid_t pid) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1260 | { |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1261 | ProcessMessage message; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1262 | |
Johnny Chen | 0d5f2d4 | 2011-10-18 18:09:30 +0000 | [diff] [blame] | 1263 | assert(monitor); |
| 1264 | assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!"); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1265 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1266 | switch (info->si_code) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1267 | { |
| 1268 | default: |
| 1269 | assert(false && "Unexpected SIGTRAP code!"); |
| 1270 | break; |
| 1271 | |
| 1272 | case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): |
| 1273 | { |
| 1274 | // The inferior process is about to exit. Maintain the process in a |
| 1275 | // state of "limbo" until we are explicitly commanded to detach, |
| 1276 | // destroy, resume, etc. |
| 1277 | unsigned long data = 0; |
| 1278 | if (!monitor->GetEventMessage(pid, &data)) |
| 1279 | data = -1; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1280 | message = ProcessMessage::Limbo(pid, (data >> 8)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1281 | break; |
| 1282 | } |
| 1283 | |
| 1284 | case 0: |
| 1285 | case TRAP_TRACE: |
| 1286 | message = ProcessMessage::Trace(pid); |
| 1287 | break; |
| 1288 | |
| 1289 | case SI_KERNEL: |
| 1290 | case TRAP_BRKPT: |
| 1291 | message = ProcessMessage::Break(pid); |
| 1292 | break; |
| 1293 | } |
| 1294 | |
| 1295 | return message; |
| 1296 | } |
| 1297 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1298 | ProcessMessage |
| 1299 | ProcessMonitor::MonitorSignal(ProcessMonitor *monitor, |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 1300 | const siginfo_t *info, lldb::pid_t pid) |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1301 | { |
| 1302 | ProcessMessage message; |
| 1303 | int signo = info->si_signo; |
| 1304 | |
| 1305 | // POSIX says that process behaviour is undefined after it ignores a SIGFPE, |
| 1306 | // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a |
| 1307 | // kill(2) or raise(3). Similarly for tgkill(2) on Linux. |
| 1308 | // |
| 1309 | // IOW, user generated signals never generate what we consider to be a |
| 1310 | // "crash". |
| 1311 | // |
| 1312 | // Similarly, ACK signals generated by this monitor. |
| 1313 | if (info->si_code == SI_TKILL || info->si_code == SI_USER) |
| 1314 | { |
| 1315 | if (info->si_pid == getpid()) |
| 1316 | return ProcessMessage::SignalDelivered(pid, signo); |
| 1317 | else |
| 1318 | return ProcessMessage::Signal(pid, signo); |
| 1319 | } |
| 1320 | |
| 1321 | if (signo == SIGSEGV) { |
| 1322 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1323 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info); |
| 1324 | return ProcessMessage::Crash(pid, reason, signo, fault_addr); |
| 1325 | } |
| 1326 | |
| 1327 | if (signo == SIGILL) { |
| 1328 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1329 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info); |
| 1330 | return ProcessMessage::Crash(pid, reason, signo, fault_addr); |
| 1331 | } |
| 1332 | |
| 1333 | if (signo == SIGFPE) { |
| 1334 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1335 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info); |
| 1336 | return ProcessMessage::Crash(pid, reason, signo, fault_addr); |
| 1337 | } |
| 1338 | |
| 1339 | if (signo == SIGBUS) { |
| 1340 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1341 | ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info); |
| 1342 | return ProcessMessage::Crash(pid, reason, signo, fault_addr); |
| 1343 | } |
| 1344 | |
| 1345 | // Everything else is "normal" and does not require any special action on |
| 1346 | // our part. |
| 1347 | return ProcessMessage::Signal(pid, signo); |
| 1348 | } |
| 1349 | |
| 1350 | ProcessMessage::CrashReason |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 1351 | ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info) |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1352 | { |
| 1353 | ProcessMessage::CrashReason reason; |
| 1354 | assert(info->si_signo == SIGSEGV); |
| 1355 | |
| 1356 | reason = ProcessMessage::eInvalidCrashReason; |
| 1357 | |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1358 | switch (info->si_code) |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1359 | { |
| 1360 | default: |
| 1361 | assert(false && "unexpected si_code for SIGSEGV"); |
| 1362 | break; |
| 1363 | case SEGV_MAPERR: |
| 1364 | reason = ProcessMessage::eInvalidAddress; |
| 1365 | break; |
| 1366 | case SEGV_ACCERR: |
| 1367 | reason = ProcessMessage::ePrivilegedAddress; |
| 1368 | break; |
| 1369 | } |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1370 | |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1371 | return reason; |
| 1372 | } |
| 1373 | |
| 1374 | ProcessMessage::CrashReason |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 1375 | ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info) |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1376 | { |
| 1377 | ProcessMessage::CrashReason reason; |
| 1378 | assert(info->si_signo == SIGILL); |
| 1379 | |
| 1380 | reason = ProcessMessage::eInvalidCrashReason; |
| 1381 | |
| 1382 | switch (info->si_code) |
| 1383 | { |
| 1384 | default: |
| 1385 | assert(false && "unexpected si_code for SIGILL"); |
| 1386 | break; |
| 1387 | case ILL_ILLOPC: |
| 1388 | reason = ProcessMessage::eIllegalOpcode; |
| 1389 | break; |
| 1390 | case ILL_ILLOPN: |
| 1391 | reason = ProcessMessage::eIllegalOperand; |
| 1392 | break; |
| 1393 | case ILL_ILLADR: |
| 1394 | reason = ProcessMessage::eIllegalAddressingMode; |
| 1395 | break; |
| 1396 | case ILL_ILLTRP: |
| 1397 | reason = ProcessMessage::eIllegalTrap; |
| 1398 | break; |
| 1399 | case ILL_PRVOPC: |
| 1400 | reason = ProcessMessage::ePrivilegedOpcode; |
| 1401 | break; |
| 1402 | case ILL_PRVREG: |
| 1403 | reason = ProcessMessage::ePrivilegedRegister; |
| 1404 | break; |
| 1405 | case ILL_COPROC: |
| 1406 | reason = ProcessMessage::eCoprocessorError; |
| 1407 | break; |
| 1408 | case ILL_BADSTK: |
| 1409 | reason = ProcessMessage::eInternalStackError; |
| 1410 | break; |
| 1411 | } |
| 1412 | |
| 1413 | return reason; |
| 1414 | } |
| 1415 | |
| 1416 | ProcessMessage::CrashReason |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 1417 | ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info) |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1418 | { |
| 1419 | ProcessMessage::CrashReason reason; |
| 1420 | assert(info->si_signo == SIGFPE); |
| 1421 | |
| 1422 | reason = ProcessMessage::eInvalidCrashReason; |
| 1423 | |
| 1424 | switch (info->si_code) |
| 1425 | { |
| 1426 | default: |
| 1427 | assert(false && "unexpected si_code for SIGFPE"); |
| 1428 | break; |
| 1429 | case FPE_INTDIV: |
| 1430 | reason = ProcessMessage::eIntegerDivideByZero; |
| 1431 | break; |
| 1432 | case FPE_INTOVF: |
| 1433 | reason = ProcessMessage::eIntegerOverflow; |
| 1434 | break; |
| 1435 | case FPE_FLTDIV: |
| 1436 | reason = ProcessMessage::eFloatDivideByZero; |
| 1437 | break; |
| 1438 | case FPE_FLTOVF: |
| 1439 | reason = ProcessMessage::eFloatOverflow; |
| 1440 | break; |
| 1441 | case FPE_FLTUND: |
| 1442 | reason = ProcessMessage::eFloatUnderflow; |
| 1443 | break; |
| 1444 | case FPE_FLTRES: |
| 1445 | reason = ProcessMessage::eFloatInexactResult; |
| 1446 | break; |
| 1447 | case FPE_FLTINV: |
| 1448 | reason = ProcessMessage::eFloatInvalidOperation; |
| 1449 | break; |
| 1450 | case FPE_FLTSUB: |
| 1451 | reason = ProcessMessage::eFloatSubscriptRange; |
| 1452 | break; |
| 1453 | } |
| 1454 | |
| 1455 | return reason; |
| 1456 | } |
| 1457 | |
| 1458 | ProcessMessage::CrashReason |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 1459 | ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info) |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1460 | { |
| 1461 | ProcessMessage::CrashReason reason; |
| 1462 | assert(info->si_signo == SIGBUS); |
| 1463 | |
| 1464 | reason = ProcessMessage::eInvalidCrashReason; |
| 1465 | |
| 1466 | switch (info->si_code) |
| 1467 | { |
| 1468 | default: |
| 1469 | assert(false && "unexpected si_code for SIGBUS"); |
| 1470 | break; |
| 1471 | case BUS_ADRALN: |
| 1472 | reason = ProcessMessage::eIllegalAlignment; |
| 1473 | break; |
| 1474 | case BUS_ADRERR: |
| 1475 | reason = ProcessMessage::eIllegalAddress; |
| 1476 | break; |
| 1477 | case BUS_OBJERR: |
| 1478 | reason = ProcessMessage::eHardwareError; |
| 1479 | break; |
| 1480 | } |
| 1481 | |
| 1482 | return reason; |
| 1483 | } |
| 1484 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1485 | void |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1486 | ProcessMonitor::ServeOperation(OperationArgs *args) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1487 | { |
| 1488 | int status; |
| 1489 | pollfd fdset; |
Johnny Chen | 25e68e3 | 2011-06-14 19:19:50 +0000 | [diff] [blame] | 1490 | |
Stephen Wilson | 570243b | 2011-01-19 01:37:06 +0000 | [diff] [blame] | 1491 | ProcessMonitor *monitor = args->m_monitor; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1492 | |
| 1493 | fdset.fd = monitor->m_server_fd; |
| 1494 | fdset.events = POLLIN | POLLPRI; |
| 1495 | fdset.revents = 0; |
| 1496 | |
Stephen Wilson | 570243b | 2011-01-19 01:37:06 +0000 | [diff] [blame] | 1497 | // We are finised with the arguments and are ready to go. Sync with the |
| 1498 | // parent thread and start serving operations on the inferior. |
| 1499 | sem_post(&args->m_semaphore); |
| 1500 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1501 | for (;;) |
| 1502 | { |
| 1503 | if ((status = poll(&fdset, 1, -1)) < 0) |
| 1504 | { |
| 1505 | switch (errno) |
| 1506 | { |
| 1507 | default: |
| 1508 | assert(false && "Unexpected poll() failure!"); |
| 1509 | continue; |
| 1510 | |
| 1511 | case EINTR: continue; // Just poll again. |
| 1512 | case EBADF: return; // Connection terminated. |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | assert(status == 1 && "Too many descriptors!"); |
| 1517 | |
| 1518 | if (fdset.revents & POLLIN) |
| 1519 | { |
| 1520 | Operation *op = NULL; |
| 1521 | |
| 1522 | READ_AGAIN: |
| 1523 | if ((status = read(fdset.fd, &op, sizeof(op))) < 0) |
| 1524 | { |
| 1525 | // There is only one acceptable failure. |
| 1526 | assert(errno == EINTR); |
| 1527 | goto READ_AGAIN; |
| 1528 | } |
| 1529 | |
| 1530 | assert(status == sizeof(op)); |
| 1531 | op->Execute(monitor); |
| 1532 | write(fdset.fd, &op, sizeof(op)); |
| 1533 | } |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | void |
| 1538 | ProcessMonitor::DoOperation(Operation *op) |
| 1539 | { |
| 1540 | int status; |
| 1541 | Operation *ack = NULL; |
| 1542 | Mutex::Locker lock(m_server_mutex); |
| 1543 | |
| 1544 | // FIXME: Do proper error checking here. |
| 1545 | write(m_client_fd, &op, sizeof(op)); |
| 1546 | |
| 1547 | READ_AGAIN: |
| 1548 | if ((status = read(m_client_fd, &ack, sizeof(ack))) < 0) |
| 1549 | { |
| 1550 | // If interrupted by a signal handler try again. Otherwise the monitor |
| 1551 | // thread probably died and we have a stale file descriptor -- abort the |
| 1552 | // operation. |
| 1553 | if (errno == EINTR) |
| 1554 | goto READ_AGAIN; |
| 1555 | return; |
| 1556 | } |
| 1557 | |
| 1558 | assert(status == sizeof(ack)); |
| 1559 | assert(ack == op && "Invalid monitor thread response!"); |
| 1560 | } |
| 1561 | |
| 1562 | size_t |
| 1563 | ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, |
| 1564 | Error &error) |
| 1565 | { |
| 1566 | size_t result; |
| 1567 | ReadOperation op(vm_addr, buf, size, error, result); |
| 1568 | DoOperation(&op); |
| 1569 | return result; |
| 1570 | } |
| 1571 | |
| 1572 | size_t |
| 1573 | ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, |
| 1574 | lldb_private::Error &error) |
| 1575 | { |
| 1576 | size_t result; |
| 1577 | WriteOperation op(vm_addr, buf, size, error, result); |
| 1578 | DoOperation(&op); |
| 1579 | return result; |
| 1580 | } |
| 1581 | |
| 1582 | bool |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 1583 | ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, unsigned size, RegisterValue &value) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1584 | { |
| 1585 | bool result; |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 1586 | ReadRegOperation op(tid, offset, value, result); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1587 | DoOperation(&op); |
| 1588 | return result; |
| 1589 | } |
| 1590 | |
| 1591 | bool |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 1592 | ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset, const RegisterValue &value) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1593 | { |
| 1594 | bool result; |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 1595 | WriteRegOperation op(tid, offset, value, result); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1596 | DoOperation(&op); |
| 1597 | return result; |
| 1598 | } |
| 1599 | |
| 1600 | bool |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 1601 | ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf) |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 1602 | { |
| 1603 | bool result; |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 1604 | ReadGPROperation op(tid, buf, result); |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 1605 | DoOperation(&op); |
| 1606 | return result; |
| 1607 | } |
| 1608 | |
| 1609 | bool |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 1610 | ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf) |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 1611 | { |
| 1612 | bool result; |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 1613 | ReadFPROperation op(tid, buf, result); |
Stephen Wilson | ade1aea | 2011-01-19 01:31:38 +0000 | [diff] [blame] | 1614 | DoOperation(&op); |
| 1615 | return result; |
| 1616 | } |
| 1617 | |
| 1618 | bool |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 1619 | ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf) |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 1620 | { |
| 1621 | bool result; |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 1622 | WriteGPROperation op(tid, buf, result); |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 1623 | DoOperation(&op); |
| 1624 | return result; |
| 1625 | } |
| 1626 | |
| 1627 | bool |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 1628 | ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf) |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 1629 | { |
| 1630 | bool result; |
Daniel Malea | f0da371 | 2012-12-18 19:50:15 +0000 | [diff] [blame] | 1631 | WriteFPROperation op(tid, buf, result); |
Peter Collingbourne | 10bc010 | 2011-06-03 20:41:02 +0000 | [diff] [blame] | 1632 | DoOperation(&op); |
| 1633 | return result; |
| 1634 | } |
| 1635 | |
| 1636 | bool |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1637 | ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1638 | { |
| 1639 | bool result; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1640 | ResumeOperation op(tid, signo, result); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1641 | DoOperation(&op); |
| 1642 | return result; |
| 1643 | } |
| 1644 | |
| 1645 | bool |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1646 | ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1647 | { |
| 1648 | bool result; |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1649 | SingleStepOperation op(tid, signo, result); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1650 | DoOperation(&op); |
| 1651 | return result; |
| 1652 | } |
| 1653 | |
| 1654 | bool |
| 1655 | ProcessMonitor::BringProcessIntoLimbo() |
| 1656 | { |
| 1657 | bool result; |
| 1658 | KillOperation op(result); |
| 1659 | DoOperation(&op); |
| 1660 | return result; |
| 1661 | } |
| 1662 | |
| 1663 | bool |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 1664 | ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1665 | { |
| 1666 | bool result; |
Daniel Malea | a35970a | 2012-11-23 18:09:58 +0000 | [diff] [blame] | 1667 | SiginfoOperation op(tid, siginfo, result, ptrace_err); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1668 | DoOperation(&op); |
| 1669 | return result; |
| 1670 | } |
| 1671 | |
| 1672 | bool |
| 1673 | ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message) |
| 1674 | { |
| 1675 | bool result; |
| 1676 | EventMessageOperation op(tid, message, result); |
| 1677 | DoOperation(&op); |
| 1678 | return result; |
| 1679 | } |
| 1680 | |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1681 | lldb_private::Error |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1682 | ProcessMonitor::Detach() |
| 1683 | { |
Greg Clayton | 2804135 | 2011-11-29 20:50:10 +0000 | [diff] [blame] | 1684 | lldb_private::Error error; |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1685 | if (m_pid != LLDB_INVALID_PROCESS_ID) { |
| 1686 | DetachOperation op(error); |
| 1687 | DoOperation(&op); |
| 1688 | } |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1689 | return error; |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 1690 | } |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1691 | |
| 1692 | bool |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1693 | ProcessMonitor::DupDescriptor(const char *path, int fd, int flags) |
| 1694 | { |
Peter Collingbourne | 6234320 | 2011-06-14 03:55:54 +0000 | [diff] [blame] | 1695 | int target_fd = open(path, flags, 0666); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1696 | |
| 1697 | if (target_fd == -1) |
| 1698 | return false; |
| 1699 | |
Peter Collingbourne | 6234320 | 2011-06-14 03:55:54 +0000 | [diff] [blame] | 1700 | return (dup2(target_fd, fd) == -1) ? false : true; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1701 | } |
Stephen Wilson | 9212d7f | 2011-01-04 21:40:25 +0000 | [diff] [blame] | 1702 | |
| 1703 | void |
| 1704 | ProcessMonitor::StopMonitoringChildProcess() |
| 1705 | { |
| 1706 | lldb::thread_result_t thread_result; |
| 1707 | |
Stephen Wilson | d4182f4 | 2011-02-09 20:10:35 +0000 | [diff] [blame] | 1708 | if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread)) |
Stephen Wilson | 9212d7f | 2011-01-04 21:40:25 +0000 | [diff] [blame] | 1709 | { |
| 1710 | Host::ThreadCancel(m_monitor_thread, NULL); |
| 1711 | Host::ThreadJoin(m_monitor_thread, &thread_result, NULL); |
| 1712 | m_monitor_thread = LLDB_INVALID_HOST_THREAD; |
| 1713 | } |
| 1714 | } |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1715 | |
| 1716 | void |
| 1717 | ProcessMonitor::StopMonitor() |
| 1718 | { |
| 1719 | StopMonitoringChildProcess(); |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1720 | StopOpThread(); |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1721 | CloseFD(m_terminal_fd); |
| 1722 | CloseFD(m_client_fd); |
| 1723 | CloseFD(m_server_fd); |
| 1724 | } |
| 1725 | |
| 1726 | void |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1727 | ProcessMonitor::StopOpThread() |
| 1728 | { |
| 1729 | lldb::thread_result_t result; |
| 1730 | |
| 1731 | if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread)) |
| 1732 | return; |
| 1733 | |
| 1734 | Host::ThreadCancel(m_operation_thread, NULL); |
| 1735 | Host::ThreadJoin(m_operation_thread, &result, NULL); |
Daniel Malea | 8b9e71e | 2012-11-22 18:21:05 +0000 | [diff] [blame] | 1736 | m_operation_thread = LLDB_INVALID_HOST_THREAD; |
Greg Clayton | 743ecf4 | 2012-10-16 20:20:18 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | void |
Stephen Wilson | 84ffe70 | 2011-03-30 15:55:52 +0000 | [diff] [blame] | 1740 | ProcessMonitor::CloseFD(int &fd) |
| 1741 | { |
| 1742 | if (fd != -1) |
| 1743 | { |
| 1744 | close(fd); |
| 1745 | fd = -1; |
| 1746 | } |
| 1747 | } |