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