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