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