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