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