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