Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +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 | |
| 10 | // C Includes |
| 11 | #include <errno.h> |
| 12 | #include <poll.h> |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 13 | #include <signal.h> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | #include <string.h> |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 16 | #include <sys/ptrace.h> |
| 17 | #include <sys/socket.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/wait.h> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 20 | #include <unistd.h> |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 21 | |
| 22 | // C++ Includes |
| 23 | // Other libraries and framework includes |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 24 | #include "lldb/Core/RegisterValue.h" |
| 25 | #include "lldb/Core/Scalar.h" |
| 26 | #include "lldb/Host/Host.h" |
Zachary Turner | 24ae629 | 2017-02-16 19:38:21 +0000 | [diff] [blame] | 27 | #include "lldb/Host/PseudoTerminal.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 28 | #include "lldb/Host/ThreadLauncher.h" |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 29 | #include "lldb/Target/RegisterContext.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 30 | #include "lldb/Target/Thread.h" |
Ed Maste | 8702e92 | 2015-03-03 22:44:18 +0000 | [diff] [blame] | 31 | #include "lldb/Target/UnixSignals.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 32 | #include "lldb/Utility/Error.h" |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 33 | |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 34 | #include "FreeBSDThread.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 35 | #include "Plugins/Process/POSIX/CrashReason.h" |
Pavel Labath | f0a6d8a | 2017-04-11 12:26:25 +0000 | [diff] [blame^] | 36 | #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 37 | #include "ProcessFreeBSD.h" |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 38 | #include "ProcessMonitor.h" |
| 39 | |
| 40 | extern "C" { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | extern char **environ; |
| 42 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 43 | |
| 44 | using namespace lldb; |
| 45 | using namespace lldb_private; |
| 46 | |
| 47 | // We disable the tracing of ptrace calls for integration builds to |
| 48 | // avoid the additional indirection and checks. |
| 49 | #ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION |
| 50 | // Wrapper for ptrace to catch errors and log calls. |
| 51 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | const char *Get_PT_IO_OP(int op) { |
| 53 | switch (op) { |
| 54 | case PIOD_READ_D: |
| 55 | return "READ_D"; |
| 56 | case PIOD_WRITE_D: |
| 57 | return "WRITE_D"; |
| 58 | case PIOD_READ_I: |
| 59 | return "READ_I"; |
| 60 | case PIOD_WRITE_I: |
| 61 | return "WRITE_I"; |
| 62 | default: |
| 63 | return "Unknown op"; |
| 64 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 67 | // Wrapper for ptrace to catch errors and log calls. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | // Note that ptrace sets errno on error because -1 is reserved as a valid |
| 69 | // result. |
| 70 | extern long PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data, |
| 71 | const char *reqName, const char *file, int line) { |
| 72 | long int result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 75 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | if (log) { |
| 77 | log->Printf("ptrace(%s, %" PRIu64 ", %p, %x) called from file %s line %d", |
| 78 | reqName, pid, addr, data, file, line); |
| 79 | if (req == PT_IO) { |
| 80 | struct ptrace_io_desc *pi = (struct ptrace_io_desc *)addr; |
| 81 | |
| 82 | log->Printf("PT_IO: op=%s offs=%zx size=%zu", Get_PT_IO_OP(pi->piod_op), |
| 83 | (size_t)pi->piod_offs, pi->piod_len); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 84 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 86 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | // PtraceDisplayBytes(req, data); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 88 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | errno = 0; |
| 90 | result = ptrace(req, pid, (caddr_t)addr, data); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 91 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 92 | // PtraceDisplayBytes(req, data); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 93 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 94 | if (log && errno != 0) { |
| 95 | const char *str; |
| 96 | switch (errno) { |
| 97 | case ESRCH: |
| 98 | str = "ESRCH"; |
| 99 | break; |
| 100 | case EINVAL: |
| 101 | str = "EINVAL"; |
| 102 | break; |
| 103 | case EBUSY: |
| 104 | str = "EBUSY"; |
| 105 | break; |
| 106 | case EPERM: |
| 107 | str = "EPERM"; |
| 108 | break; |
| 109 | default: |
| 110 | str = "<unknown>"; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 111 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | log->Printf("ptrace() failed; errno=%d (%s)", errno, str); |
| 113 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 114 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 115 | if (log) { |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 116 | #ifdef __amd64__ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 117 | if (req == PT_GETREGS) { |
| 118 | struct reg *r = (struct reg *)addr; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | log->Printf("PT_GETREGS: rip=0x%lx rsp=0x%lx rbp=0x%lx rax=0x%lx", |
| 121 | r->r_rip, r->r_rsp, r->r_rbp, r->r_rax); |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 122 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 123 | if (req == PT_GETDBREGS || req == PT_SETDBREGS) { |
| 124 | struct dbreg *r = (struct dbreg *)addr; |
| 125 | char setget = (req == PT_GETDBREGS) ? 'G' : 'S'; |
| 126 | |
| 127 | for (int i = 0; i <= 7; i++) |
| 128 | log->Printf("PT_%cETDBREGS: dr[%d]=0x%lx", setget, i, r->dr[i]); |
| 129 | } |
| 130 | #endif |
| 131 | } |
| 132 | |
| 133 | return result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 136 | // Wrapper for ptrace when logging is not required. |
| 137 | // Sets errno to 0 prior to calling ptrace. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 138 | extern long PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data) { |
| 139 | long result = 0; |
| 140 | errno = 0; |
| 141 | result = ptrace(req, pid, (caddr_t)addr, data); |
| 142 | return result; |
Matt Kopec | 7de4846 | 2013-03-06 17:20:48 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | #define PTRACE(req, pid, addr, data) \ |
| 146 | PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 147 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 148 | PtraceWrapper((req), (pid), (addr), (data)) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 149 | #endif |
| 150 | |
| 151 | //------------------------------------------------------------------------------ |
| 152 | // Static implementations of ProcessMonitor::ReadMemory and |
| 153 | // ProcessMonitor::WriteMemory. This enables mutual recursion between these |
| 154 | // functions without needed to go thru the thread funnel. |
| 155 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | static size_t DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf, |
| 157 | size_t size, Error &error) { |
| 158 | struct ptrace_io_desc pi_desc; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 159 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | pi_desc.piod_op = PIOD_READ_D; |
| 161 | pi_desc.piod_offs = (void *)vm_addr; |
| 162 | pi_desc.piod_addr = buf; |
| 163 | pi_desc.piod_len = size; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 164 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 165 | if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0) |
| 166 | error.SetErrorToErrno(); |
| 167 | return pi_desc.piod_len; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | static size_t DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr, |
| 171 | const void *buf, size_t size, Error &error) { |
| 172 | struct ptrace_io_desc pi_desc; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 173 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 174 | pi_desc.piod_op = PIOD_WRITE_D; |
| 175 | pi_desc.piod_offs = (void *)vm_addr; |
| 176 | pi_desc.piod_addr = (void *)buf; |
| 177 | pi_desc.piod_len = size; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 178 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 179 | if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0) |
| 180 | error.SetErrorToErrno(); |
| 181 | return pi_desc.piod_len; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // Simple helper function to ensure flags are enabled on the given file |
| 185 | // descriptor. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 186 | static bool EnsureFDFlags(int fd, int flags, Error &error) { |
| 187 | int status; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 188 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 189 | if ((status = fcntl(fd, F_GETFL)) == -1) { |
| 190 | error.SetErrorToErrno(); |
| 191 | return false; |
| 192 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 193 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 194 | if (fcntl(fd, F_SETFL, status | flags) == -1) { |
| 195 | error.SetErrorToErrno(); |
| 196 | return false; |
| 197 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 198 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 199 | return true; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | //------------------------------------------------------------------------------ |
| 203 | /// @class Operation |
| 204 | /// @brief Represents a ProcessMonitor operation. |
| 205 | /// |
| 206 | /// Under FreeBSD, it is not possible to ptrace() from any other thread but the |
| 207 | /// one that spawned or attached to the process from the start. Therefore, when |
| 208 | /// a ProcessMonitor is asked to deliver or change the state of an inferior |
| 209 | /// process the operation must be "funneled" to a specific thread to perform the |
| 210 | /// task. The Operation class provides an abstract base for all services the |
| 211 | /// ProcessMonitor must perform via the single virtual function Execute, thus |
| 212 | /// encapsulating the code that needs to run in the privileged context. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 213 | class Operation { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 214 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 215 | virtual ~Operation() {} |
| 216 | virtual void Execute(ProcessMonitor *monitor) = 0; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | //------------------------------------------------------------------------------ |
| 220 | /// @class ReadOperation |
| 221 | /// @brief Implements ProcessMonitor::ReadMemory. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 222 | class ReadOperation : public Operation { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 223 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 224 | ReadOperation(lldb::addr_t addr, void *buff, size_t size, Error &error, |
| 225 | size_t &result) |
| 226 | : m_addr(addr), m_buff(buff), m_size(size), m_error(error), |
| 227 | m_result(result) {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 228 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | void Execute(ProcessMonitor *monitor); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 230 | |
| 231 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 232 | lldb::addr_t m_addr; |
| 233 | void *m_buff; |
| 234 | size_t m_size; |
| 235 | Error &m_error; |
| 236 | size_t &m_result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 237 | }; |
| 238 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 239 | void ReadOperation::Execute(ProcessMonitor *monitor) { |
| 240 | lldb::pid_t pid = monitor->GetPID(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 241 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 242 | m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | //------------------------------------------------------------------------------ |
Ed Maste | a56115f | 2013-07-17 14:30:26 +0000 | [diff] [blame] | 246 | /// @class WriteOperation |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 247 | /// @brief Implements ProcessMonitor::WriteMemory. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 248 | class WriteOperation : public Operation { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 249 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 250 | WriteOperation(lldb::addr_t addr, const void *buff, size_t size, Error &error, |
| 251 | size_t &result) |
| 252 | : m_addr(addr), m_buff(buff), m_size(size), m_error(error), |
| 253 | m_result(result) {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 254 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 255 | void Execute(ProcessMonitor *monitor); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 256 | |
| 257 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 258 | lldb::addr_t m_addr; |
| 259 | const void *m_buff; |
| 260 | size_t m_size; |
| 261 | Error &m_error; |
| 262 | size_t &m_result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 263 | }; |
| 264 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 265 | void WriteOperation::Execute(ProcessMonitor *monitor) { |
| 266 | lldb::pid_t pid = monitor->GetPID(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 267 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 268 | m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | //------------------------------------------------------------------------------ |
| 272 | /// @class ReadRegOperation |
| 273 | /// @brief Implements ProcessMonitor::ReadRegisterValue. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 274 | class ReadRegOperation : public Operation { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 275 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 276 | ReadRegOperation(lldb::tid_t tid, unsigned offset, unsigned size, |
| 277 | RegisterValue &value, bool &result) |
| 278 | : m_tid(tid), m_offset(offset), m_size(size), m_value(value), |
| 279 | m_result(result) {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 280 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 281 | void Execute(ProcessMonitor *monitor); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 282 | |
| 283 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 284 | lldb::tid_t m_tid; |
| 285 | unsigned m_offset; |
| 286 | unsigned m_size; |
| 287 | RegisterValue &m_value; |
| 288 | bool &m_result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 289 | }; |
| 290 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 291 | void ReadRegOperation::Execute(ProcessMonitor *monitor) { |
| 292 | struct reg regs; |
| 293 | int rc; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 294 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 295 | if ((rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)®s, 0)) < 0) { |
| 296 | m_result = false; |
| 297 | } else { |
| 298 | // 'struct reg' contains only 32- or 64-bit register values. Punt on |
| 299 | // others. Also, not all entries may be uintptr_t sized, such as 32-bit |
| 300 | // processes on powerpc64 (probably the same for i386 on amd64) |
| 301 | if (m_size == sizeof(uint32_t)) |
| 302 | m_value = *(uint32_t *)(((caddr_t)®s) + m_offset); |
| 303 | else if (m_size == sizeof(uint64_t)) |
| 304 | m_value = *(uint64_t *)(((caddr_t)®s) + m_offset); |
| 305 | else |
| 306 | memcpy((void *)&m_value, (((caddr_t)®s) + m_offset), m_size); |
| 307 | m_result = true; |
| 308 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | //------------------------------------------------------------------------------ |
| 312 | /// @class WriteRegOperation |
| 313 | /// @brief Implements ProcessMonitor::WriteRegisterValue. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 314 | class WriteRegOperation : public Operation { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 315 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 316 | WriteRegOperation(lldb::tid_t tid, unsigned offset, |
| 317 | const RegisterValue &value, bool &result) |
| 318 | : m_tid(tid), m_offset(offset), m_value(value), m_result(result) {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 319 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 320 | void Execute(ProcessMonitor *monitor); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 321 | |
| 322 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 323 | lldb::tid_t m_tid; |
| 324 | unsigned m_offset; |
| 325 | const RegisterValue &m_value; |
| 326 | bool &m_result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 329 | void WriteRegOperation::Execute(ProcessMonitor *monitor) { |
| 330 | struct reg regs; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 331 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 332 | if (PTRACE(PT_GETREGS, m_tid, (caddr_t)®s, 0) < 0) { |
| 333 | m_result = false; |
| 334 | return; |
| 335 | } |
| 336 | *(uintptr_t *)(((caddr_t)®s) + m_offset) = |
| 337 | (uintptr_t)m_value.GetAsUInt64(); |
| 338 | if (PTRACE(PT_SETREGS, m_tid, (caddr_t)®s, 0) < 0) |
| 339 | m_result = false; |
| 340 | else |
| 341 | m_result = true; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | //------------------------------------------------------------------------------ |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 345 | /// @class ReadDebugRegOperation |
| 346 | /// @brief Implements ProcessMonitor::ReadDebugRegisterValue. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 347 | class ReadDebugRegOperation : public Operation { |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 348 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 349 | ReadDebugRegOperation(lldb::tid_t tid, unsigned offset, unsigned size, |
| 350 | RegisterValue &value, bool &result) |
| 351 | : m_tid(tid), m_offset(offset), m_size(size), m_value(value), |
| 352 | m_result(result) {} |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 353 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 354 | void Execute(ProcessMonitor *monitor); |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 355 | |
| 356 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 357 | lldb::tid_t m_tid; |
| 358 | unsigned m_offset; |
| 359 | unsigned m_size; |
| 360 | RegisterValue &m_value; |
| 361 | bool &m_result; |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 362 | }; |
| 363 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 364 | void ReadDebugRegOperation::Execute(ProcessMonitor *monitor) { |
| 365 | struct dbreg regs; |
| 366 | int rc; |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 367 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 368 | if ((rc = PTRACE(PT_GETDBREGS, m_tid, (caddr_t)®s, 0)) < 0) { |
| 369 | m_result = false; |
| 370 | } else { |
| 371 | if (m_size == sizeof(uintptr_t)) |
| 372 | m_value = *(uintptr_t *)(((caddr_t)®s) + m_offset); |
| 373 | else |
| 374 | memcpy((void *)&m_value, (((caddr_t)®s) + m_offset), m_size); |
| 375 | m_result = true; |
| 376 | } |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | //------------------------------------------------------------------------------ |
| 380 | /// @class WriteDebugRegOperation |
| 381 | /// @brief Implements ProcessMonitor::WriteDebugRegisterValue. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 382 | class WriteDebugRegOperation : public Operation { |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 383 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 384 | WriteDebugRegOperation(lldb::tid_t tid, unsigned offset, |
| 385 | const RegisterValue &value, bool &result) |
| 386 | : m_tid(tid), m_offset(offset), m_value(value), m_result(result) {} |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 387 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 388 | void Execute(ProcessMonitor *monitor); |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 389 | |
| 390 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 391 | lldb::tid_t m_tid; |
| 392 | unsigned m_offset; |
| 393 | const RegisterValue &m_value; |
| 394 | bool &m_result; |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 395 | }; |
| 396 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 397 | void WriteDebugRegOperation::Execute(ProcessMonitor *monitor) { |
| 398 | struct dbreg regs; |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 399 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 400 | if (PTRACE(PT_GETDBREGS, m_tid, (caddr_t)®s, 0) < 0) { |
| 401 | m_result = false; |
| 402 | return; |
| 403 | } |
| 404 | *(uintptr_t *)(((caddr_t)®s) + m_offset) = |
| 405 | (uintptr_t)m_value.GetAsUInt64(); |
| 406 | if (PTRACE(PT_SETDBREGS, m_tid, (caddr_t)®s, 0) < 0) |
| 407 | m_result = false; |
| 408 | else |
| 409 | m_result = true; |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | //------------------------------------------------------------------------------ |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 413 | /// @class ReadGPROperation |
| 414 | /// @brief Implements ProcessMonitor::ReadGPR. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 415 | class ReadGPROperation : public Operation { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 416 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 417 | ReadGPROperation(lldb::tid_t tid, void *buf, bool &result) |
| 418 | : m_tid(tid), m_buf(buf), m_result(result) {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 419 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 420 | void Execute(ProcessMonitor *monitor); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 421 | |
| 422 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 423 | lldb::tid_t m_tid; |
| 424 | void *m_buf; |
| 425 | bool &m_result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 426 | }; |
| 427 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 428 | void ReadGPROperation::Execute(ProcessMonitor *monitor) { |
| 429 | int rc; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 430 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 431 | errno = 0; |
| 432 | rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)m_buf, 0); |
| 433 | if (errno != 0) |
| 434 | m_result = false; |
| 435 | else |
| 436 | m_result = true; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | //------------------------------------------------------------------------------ |
| 440 | /// @class ReadFPROperation |
| 441 | /// @brief Implements ProcessMonitor::ReadFPR. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 442 | class ReadFPROperation : public Operation { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 443 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 444 | ReadFPROperation(lldb::tid_t tid, void *buf, bool &result) |
| 445 | : m_tid(tid), m_buf(buf), m_result(result) {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 446 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 447 | void Execute(ProcessMonitor *monitor); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 448 | |
| 449 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 450 | lldb::tid_t m_tid; |
| 451 | void *m_buf; |
| 452 | bool &m_result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 453 | }; |
| 454 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 455 | void ReadFPROperation::Execute(ProcessMonitor *monitor) { |
| 456 | if (PTRACE(PT_GETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0) |
| 457 | m_result = false; |
| 458 | else |
| 459 | m_result = true; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | //------------------------------------------------------------------------------ |
| 463 | /// @class WriteGPROperation |
| 464 | /// @brief Implements ProcessMonitor::WriteGPR. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 465 | class WriteGPROperation : public Operation { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 466 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 467 | WriteGPROperation(lldb::tid_t tid, void *buf, bool &result) |
| 468 | : m_tid(tid), m_buf(buf), m_result(result) {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 469 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 470 | void Execute(ProcessMonitor *monitor); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 471 | |
| 472 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 473 | lldb::tid_t m_tid; |
| 474 | void *m_buf; |
| 475 | bool &m_result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 476 | }; |
| 477 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 478 | void WriteGPROperation::Execute(ProcessMonitor *monitor) { |
| 479 | if (PTRACE(PT_SETREGS, m_tid, (caddr_t)m_buf, 0) < 0) |
| 480 | m_result = false; |
| 481 | else |
| 482 | m_result = true; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | //------------------------------------------------------------------------------ |
| 486 | /// @class WriteFPROperation |
| 487 | /// @brief Implements ProcessMonitor::WriteFPR. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 488 | class WriteFPROperation : public Operation { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 489 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 490 | WriteFPROperation(lldb::tid_t tid, void *buf, bool &result) |
| 491 | : m_tid(tid), m_buf(buf), m_result(result) {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 492 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 493 | void Execute(ProcessMonitor *monitor); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 494 | |
| 495 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 496 | lldb::tid_t m_tid; |
| 497 | void *m_buf; |
| 498 | bool &m_result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 499 | }; |
| 500 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 501 | void WriteFPROperation::Execute(ProcessMonitor *monitor) { |
| 502 | if (PTRACE(PT_SETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0) |
| 503 | m_result = false; |
| 504 | else |
| 505 | m_result = true; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | //------------------------------------------------------------------------------ |
| 509 | /// @class ResumeOperation |
| 510 | /// @brief Implements ProcessMonitor::Resume. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 511 | class ResumeOperation : public Operation { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 512 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 513 | ResumeOperation(uint32_t signo, bool &result) |
| 514 | : m_signo(signo), m_result(result) {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 515 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 516 | void Execute(ProcessMonitor *monitor); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 517 | |
| 518 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 519 | uint32_t m_signo; |
| 520 | bool &m_result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 521 | }; |
| 522 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 523 | void ResumeOperation::Execute(ProcessMonitor *monitor) { |
| 524 | lldb::pid_t pid = monitor->GetPID(); |
| 525 | int data = 0; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 526 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 527 | if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 528 | data = m_signo; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 529 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 530 | if (PTRACE(PT_CONTINUE, pid, (caddr_t)1, data)) { |
| 531 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 532 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 533 | if (log) |
| 534 | log->Printf("ResumeOperation (%" PRIu64 ") failed: %s", pid, |
| 535 | strerror(errno)); |
| 536 | m_result = false; |
| 537 | } else |
| 538 | m_result = true; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | //------------------------------------------------------------------------------ |
Ed Maste | 428a678 | 2013-06-24 15:04:47 +0000 | [diff] [blame] | 542 | /// @class SingleStepOperation |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 543 | /// @brief Implements ProcessMonitor::SingleStep. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 544 | class SingleStepOperation : public Operation { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 545 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 546 | SingleStepOperation(uint32_t signo, bool &result) |
| 547 | : m_signo(signo), m_result(result) {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 548 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 549 | void Execute(ProcessMonitor *monitor); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 550 | |
| 551 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 552 | uint32_t m_signo; |
| 553 | bool &m_result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 554 | }; |
| 555 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 556 | void SingleStepOperation::Execute(ProcessMonitor *monitor) { |
| 557 | lldb::pid_t pid = monitor->GetPID(); |
| 558 | int data = 0; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 559 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 560 | if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 561 | data = m_signo; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 562 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 563 | if (PTRACE(PT_STEP, pid, NULL, data)) |
| 564 | m_result = false; |
| 565 | else |
| 566 | m_result = true; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | //------------------------------------------------------------------------------ |
Ed Maste | 819e399 | 2013-07-17 14:02:20 +0000 | [diff] [blame] | 570 | /// @class LwpInfoOperation |
| 571 | /// @brief Implements ProcessMonitor::GetLwpInfo. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 572 | class LwpInfoOperation : public Operation { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 573 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 574 | LwpInfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err) |
| 575 | : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 576 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 577 | void Execute(ProcessMonitor *monitor); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 578 | |
| 579 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 580 | lldb::tid_t m_tid; |
| 581 | void *m_info; |
| 582 | bool &m_result; |
| 583 | int &m_err; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 584 | }; |
| 585 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 586 | void LwpInfoOperation::Execute(ProcessMonitor *monitor) { |
| 587 | struct ptrace_lwpinfo plwp; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 588 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 589 | if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) { |
| 590 | m_result = false; |
| 591 | m_err = errno; |
| 592 | } else { |
| 593 | memcpy(m_info, &plwp, sizeof(plwp)); |
| 594 | m_result = true; |
| 595 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | //------------------------------------------------------------------------------ |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 599 | /// @class ThreadSuspendOperation |
| 600 | /// @brief Implements ProcessMonitor::ThreadSuspend. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 601 | class ThreadSuspendOperation : public Operation { |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 602 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 603 | ThreadSuspendOperation(lldb::tid_t tid, bool suspend, bool &result) |
| 604 | : m_tid(tid), m_suspend(suspend), m_result(result) {} |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 605 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 606 | void Execute(ProcessMonitor *monitor); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 607 | |
| 608 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 609 | lldb::tid_t m_tid; |
| 610 | bool m_suspend; |
| 611 | bool &m_result; |
| 612 | }; |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 613 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 614 | void ThreadSuspendOperation::Execute(ProcessMonitor *monitor) { |
| 615 | m_result = !PTRACE(m_suspend ? PT_SUSPEND : PT_RESUME, m_tid, NULL, 0); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 618 | //------------------------------------------------------------------------------ |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 619 | /// @class EventMessageOperation |
| 620 | /// @brief Implements ProcessMonitor::GetEventMessage. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 621 | class EventMessageOperation : public Operation { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 622 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 623 | EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result) |
| 624 | : m_tid(tid), m_message(message), m_result(result) {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 625 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 626 | void Execute(ProcessMonitor *monitor); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 627 | |
| 628 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 629 | lldb::tid_t m_tid; |
| 630 | unsigned long *m_message; |
| 631 | bool &m_result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 632 | }; |
| 633 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 634 | void EventMessageOperation::Execute(ProcessMonitor *monitor) { |
| 635 | struct ptrace_lwpinfo plwp; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 636 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 637 | if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) |
| 638 | m_result = false; |
| 639 | else { |
| 640 | if (plwp.pl_flags & PL_FLAG_FORKED) { |
| 641 | *m_message = plwp.pl_child_pid; |
| 642 | m_result = true; |
| 643 | } else |
| 644 | m_result = false; |
| 645 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | //------------------------------------------------------------------------------ |
| 649 | /// @class KillOperation |
Ed Maste | 7088293 | 2014-04-01 14:30:56 +0000 | [diff] [blame] | 650 | /// @brief Implements ProcessMonitor::Kill. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 651 | class KillOperation : public Operation { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 652 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 653 | KillOperation(bool &result) : m_result(result) {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 654 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 655 | void Execute(ProcessMonitor *monitor); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 656 | |
| 657 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 658 | bool &m_result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 659 | }; |
| 660 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 661 | void KillOperation::Execute(ProcessMonitor *monitor) { |
| 662 | lldb::pid_t pid = monitor->GetPID(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 663 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 664 | if (PTRACE(PT_KILL, pid, NULL, 0)) |
| 665 | m_result = false; |
| 666 | else |
| 667 | m_result = true; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | //------------------------------------------------------------------------------ |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 671 | /// @class DetachOperation |
Ed Maste | 263c928 | 2014-03-17 17:45:53 +0000 | [diff] [blame] | 672 | /// @brief Implements ProcessMonitor::Detach. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 673 | class DetachOperation : public Operation { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 674 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 675 | DetachOperation(Error &result) : m_error(result) {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 676 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 677 | void Execute(ProcessMonitor *monitor); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 678 | |
| 679 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 680 | Error &m_error; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 681 | }; |
| 682 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 683 | void DetachOperation::Execute(ProcessMonitor *monitor) { |
| 684 | lldb::pid_t pid = monitor->GetPID(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 685 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 686 | if (PTRACE(PT_DETACH, pid, NULL, 0) < 0) |
| 687 | m_error.SetErrorToErrno(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 691 | : m_monitor(monitor) { |
| 692 | sem_init(&m_semaphore, 0, 0); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 695 | ProcessMonitor::OperationArgs::~OperationArgs() { sem_destroy(&m_semaphore); } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 696 | |
| 697 | ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor, |
| 698 | lldb_private::Module *module, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 699 | char const **argv, char const **envp, |
Ed Maste | 41fba2b | 2015-06-01 15:24:37 +0000 | [diff] [blame] | 700 | const FileSpec &stdin_file_spec, |
| 701 | const FileSpec &stdout_file_spec, |
| 702 | const FileSpec &stderr_file_spec, |
| 703 | const FileSpec &working_dir) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 704 | : OperationArgs(monitor), m_module(module), m_argv(argv), m_envp(envp), |
| 705 | m_stdin_file_spec(stdin_file_spec), m_stdout_file_spec(stdout_file_spec), |
| 706 | m_stderr_file_spec(stderr_file_spec), m_working_dir(working_dir) {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 707 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 708 | ProcessMonitor::LaunchArgs::~LaunchArgs() {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 709 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 710 | ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor, lldb::pid_t pid) |
| 711 | : OperationArgs(monitor), m_pid(pid) {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 712 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 713 | ProcessMonitor::AttachArgs::~AttachArgs() {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 714 | |
| 715 | //------------------------------------------------------------------------------ |
| 716 | /// The basic design of the ProcessMonitor is built around two threads. |
| 717 | /// |
| 718 | /// One thread (@see SignalThread) simply blocks on a call to waitpid() looking |
| 719 | /// for changes in the debugee state. When a change is detected a |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 720 | /// ProcessMessage is sent to the associated ProcessFreeBSD instance. This |
| 721 | /// thread |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 722 | /// "drives" state changes in the debugger. |
| 723 | /// |
| 724 | /// The second thread (@see OperationThread) is responsible for two things 1) |
| 725 | /// launching or attaching to the inferior process, and then 2) servicing |
| 726 | /// operations such as register reads/writes, stepping, etc. See the comments |
| 727 | /// on the Operation class for more info as to why this is needed. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 728 | ProcessMonitor::ProcessMonitor( |
| 729 | ProcessFreeBSD *process, Module *module, const char *argv[], |
| 730 | const char *envp[], const FileSpec &stdin_file_spec, |
| 731 | const FileSpec &stdout_file_spec, const FileSpec &stderr_file_spec, |
| 732 | const FileSpec &working_dir, |
| 733 | const lldb_private::ProcessLaunchInfo & /* launch_info */, |
| 734 | lldb_private::Error &error) |
Andrew Kaylor | 6578cb6 | 2013-07-09 22:36:48 +0000 | [diff] [blame] | 735 | : m_process(static_cast<ProcessFreeBSD *>(process)), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 736 | m_pid(LLDB_INVALID_PROCESS_ID), m_terminal_fd(-1), m_operation(0) { |
| 737 | using namespace std::placeholders; |
Pavel Labath | 998bdc5 | 2016-05-11 16:59:04 +0000 | [diff] [blame] | 738 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 739 | std::unique_ptr<LaunchArgs> args( |
| 740 | new LaunchArgs(this, module, argv, envp, stdin_file_spec, |
| 741 | stdout_file_spec, stderr_file_spec, working_dir)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 742 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 743 | sem_init(&m_operation_pending, 0, 0); |
| 744 | sem_init(&m_operation_done, 0, 0); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 745 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 746 | StartLaunchOpThread(args.get(), error); |
| 747 | if (!error.Success()) |
| 748 | return; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 749 | |
| 750 | WAIT_AGAIN: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 751 | // Wait for the operation thread to initialize. |
| 752 | if (sem_wait(&args->m_semaphore)) { |
| 753 | if (errno == EINTR) |
| 754 | goto WAIT_AGAIN; |
| 755 | else { |
| 756 | error.SetErrorToErrno(); |
| 757 | return; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 758 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 759 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 760 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 761 | // Check that the launch was a success. |
| 762 | if (!args->m_error.Success()) { |
| 763 | StopOpThread(); |
| 764 | error = args->m_error; |
| 765 | return; |
| 766 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 767 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 768 | // Finally, start monitoring the child process for change in state. |
| 769 | m_monitor_thread = Host::StartMonitoringChildProcess( |
| 770 | std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4), |
| 771 | GetPID(), true); |
| 772 | if (!m_monitor_thread.IsJoinable()) { |
| 773 | error.SetErrorToGenericError(); |
| 774 | error.SetErrorString("Process launch failed."); |
| 775 | return; |
| 776 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 779 | ProcessMonitor::ProcessMonitor(ProcessFreeBSD *process, lldb::pid_t pid, |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 780 | lldb_private::Error &error) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 781 | : m_process(static_cast<ProcessFreeBSD *>(process)), m_pid(pid), |
| 782 | m_terminal_fd(-1), m_operation(0) { |
| 783 | using namespace std::placeholders; |
Pavel Labath | 998bdc5 | 2016-05-11 16:59:04 +0000 | [diff] [blame] | 784 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 785 | sem_init(&m_operation_pending, 0, 0); |
| 786 | sem_init(&m_operation_done, 0, 0); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 787 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 788 | std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 789 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 790 | StartAttachOpThread(args.get(), error); |
| 791 | if (!error.Success()) |
| 792 | return; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 793 | |
| 794 | WAIT_AGAIN: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 795 | // Wait for the operation thread to initialize. |
| 796 | if (sem_wait(&args->m_semaphore)) { |
| 797 | if (errno == EINTR) |
| 798 | goto WAIT_AGAIN; |
| 799 | else { |
| 800 | error.SetErrorToErrno(); |
| 801 | return; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 802 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 803 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 804 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 805 | // Check that the attach was a success. |
| 806 | if (!args->m_error.Success()) { |
| 807 | StopOpThread(); |
| 808 | error = args->m_error; |
| 809 | return; |
| 810 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 811 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 812 | // Finally, start monitoring the child process for change in state. |
| 813 | m_monitor_thread = Host::StartMonitoringChildProcess( |
| 814 | std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4), |
| 815 | GetPID(), true); |
| 816 | if (!m_monitor_thread.IsJoinable()) { |
| 817 | error.SetErrorToGenericError(); |
| 818 | error.SetErrorString("Process attach failed."); |
| 819 | return; |
| 820 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 823 | ProcessMonitor::~ProcessMonitor() { StopMonitor(); } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 824 | |
| 825 | //------------------------------------------------------------------------------ |
| 826 | // Thread setup and tear down. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 827 | void ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error) { |
| 828 | static const char *g_thread_name = "lldb.process.freebsd.operation"; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 829 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 830 | if (m_operation_thread.IsJoinable()) |
| 831 | return; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 832 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 833 | m_operation_thread = |
| 834 | ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 837 | void *ProcessMonitor::LaunchOpThread(void *arg) { |
| 838 | LaunchArgs *args = static_cast<LaunchArgs *>(arg); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 839 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 840 | if (!Launch(args)) { |
| 841 | sem_post(&args->m_semaphore); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 842 | return NULL; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | ServeOperation(args); |
| 846 | return NULL; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 849 | bool ProcessMonitor::Launch(LaunchArgs *args) { |
| 850 | ProcessMonitor *monitor = args->m_monitor; |
| 851 | ProcessFreeBSD &process = monitor->GetProcess(); |
| 852 | const char **argv = args->m_argv; |
| 853 | const char **envp = args->m_envp; |
| 854 | const FileSpec &stdin_file_spec = args->m_stdin_file_spec; |
| 855 | const FileSpec &stdout_file_spec = args->m_stdout_file_spec; |
| 856 | const FileSpec &stderr_file_spec = args->m_stderr_file_spec; |
| 857 | const FileSpec &working_dir = args->m_working_dir; |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 858 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 859 | lldb_utility::PseudoTerminal terminal; |
| 860 | const size_t err_len = 1024; |
| 861 | char err_str[err_len]; |
| 862 | ::pid_t pid; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 863 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 864 | // Propagate the environment if one is not supplied. |
| 865 | if (envp == NULL || envp[0] == NULL) |
| 866 | envp = const_cast<const char **>(environ); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 867 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 868 | if ((pid = terminal.Fork(err_str, err_len)) == -1) { |
| 869 | args->m_error.SetErrorToGenericError(); |
| 870 | args->m_error.SetErrorString("Process fork failed."); |
| 871 | goto FINISH; |
| 872 | } |
| 873 | |
| 874 | // Recognized child exit status codes. |
| 875 | enum { |
| 876 | ePtraceFailed = 1, |
| 877 | eDupStdinFailed, |
| 878 | eDupStdoutFailed, |
| 879 | eDupStderrFailed, |
| 880 | eChdirFailed, |
| 881 | eExecFailed, |
| 882 | eSetGidFailed |
| 883 | }; |
| 884 | |
| 885 | // Child process. |
| 886 | if (pid == 0) { |
| 887 | // Trace this process. |
| 888 | if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0) |
| 889 | exit(ePtraceFailed); |
| 890 | |
| 891 | // terminal has already dupped the tty descriptors to stdin/out/err. |
| 892 | // This closes original fd from which they were copied (and avoids |
| 893 | // leaking descriptors to the debugged process. |
| 894 | terminal.CloseSlaveFileDescriptor(); |
| 895 | |
| 896 | // Do not inherit setgid powers. |
| 897 | if (setgid(getgid()) != 0) |
| 898 | exit(eSetGidFailed); |
| 899 | |
| 900 | // Let us have our own process group. |
| 901 | setpgid(0, 0); |
| 902 | |
| 903 | // Dup file descriptors if needed. |
| 904 | // |
| 905 | // FIXME: If two or more of the paths are the same we needlessly open |
| 906 | // the same file multiple times. |
| 907 | if (stdin_file_spec) |
| 908 | if (!DupDescriptor(stdin_file_spec, STDIN_FILENO, O_RDONLY)) |
| 909 | exit(eDupStdinFailed); |
| 910 | |
| 911 | if (stdout_file_spec) |
| 912 | if (!DupDescriptor(stdout_file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT)) |
| 913 | exit(eDupStdoutFailed); |
| 914 | |
| 915 | if (stderr_file_spec) |
| 916 | if (!DupDescriptor(stderr_file_spec, STDERR_FILENO, O_WRONLY | O_CREAT)) |
| 917 | exit(eDupStderrFailed); |
| 918 | |
| 919 | // Change working directory |
| 920 | if (working_dir && 0 != ::chdir(working_dir.GetCString())) |
| 921 | exit(eChdirFailed); |
| 922 | |
| 923 | // Execute. We should never return. |
| 924 | execve(argv[0], const_cast<char *const *>(argv), |
| 925 | const_cast<char *const *>(envp)); |
| 926 | exit(eExecFailed); |
| 927 | } |
| 928 | |
| 929 | // Wait for the child process to to trap on its call to execve. |
| 930 | ::pid_t wpid; |
| 931 | int status; |
| 932 | if ((wpid = waitpid(pid, &status, 0)) < 0) { |
| 933 | args->m_error.SetErrorToErrno(); |
| 934 | goto FINISH; |
| 935 | } else if (WIFEXITED(status)) { |
| 936 | // open, dup or execve likely failed for some reason. |
| 937 | args->m_error.SetErrorToGenericError(); |
| 938 | switch (WEXITSTATUS(status)) { |
| 939 | case ePtraceFailed: |
| 940 | args->m_error.SetErrorString("Child ptrace failed."); |
| 941 | break; |
| 942 | case eDupStdinFailed: |
| 943 | args->m_error.SetErrorString("Child open stdin failed."); |
| 944 | break; |
| 945 | case eDupStdoutFailed: |
| 946 | args->m_error.SetErrorString("Child open stdout failed."); |
| 947 | break; |
| 948 | case eDupStderrFailed: |
| 949 | args->m_error.SetErrorString("Child open stderr failed."); |
| 950 | break; |
| 951 | case eChdirFailed: |
| 952 | args->m_error.SetErrorString("Child failed to set working directory."); |
| 953 | break; |
| 954 | case eExecFailed: |
| 955 | args->m_error.SetErrorString("Child exec failed."); |
| 956 | break; |
| 957 | case eSetGidFailed: |
| 958 | args->m_error.SetErrorString("Child setgid failed."); |
| 959 | break; |
| 960 | default: |
| 961 | args->m_error.SetErrorString("Child returned unknown exit status."); |
| 962 | break; |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 963 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 964 | goto FINISH; |
| 965 | } |
| 966 | assert(WIFSTOPPED(status) && wpid == (::pid_t)pid && |
| 967 | "Could not sync with inferior process."); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 968 | |
| 969 | #ifdef notyet |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 970 | // Have the child raise an event on exit. This is used to keep the child in |
| 971 | // limbo until it is destroyed. |
| 972 | if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0) { |
| 973 | args->m_error.SetErrorToErrno(); |
| 974 | goto FINISH; |
| 975 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 976 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 977 | // Release the master terminal descriptor and pass it off to the |
| 978 | // ProcessMonitor instance. Similarly stash the inferior pid. |
| 979 | monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); |
| 980 | monitor->m_pid = pid; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 981 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 982 | // Set the terminal fd to be in non blocking mode (it simplifies the |
| 983 | // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking |
| 984 | // descriptor to read from). |
| 985 | if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error)) |
| 986 | goto FINISH; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 987 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 988 | process.SendMessage(ProcessMessage::Attach(pid)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 989 | |
| 990 | FINISH: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 991 | return args->m_error.Success(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 994 | void ProcessMonitor::StartAttachOpThread(AttachArgs *args, |
| 995 | lldb_private::Error &error) { |
| 996 | static const char *g_thread_name = "lldb.process.freebsd.operation"; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 997 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 998 | if (m_operation_thread.IsJoinable()) |
| 999 | return; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1000 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1001 | m_operation_thread = |
| 1002 | ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1005 | void *ProcessMonitor::AttachOpThread(void *arg) { |
| 1006 | AttachArgs *args = static_cast<AttachArgs *>(arg); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1007 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1008 | Attach(args); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1009 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1010 | ServeOperation(args); |
| 1011 | return NULL; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1014 | void ProcessMonitor::Attach(AttachArgs *args) { |
| 1015 | lldb::pid_t pid = args->m_pid; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1016 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1017 | ProcessMonitor *monitor = args->m_monitor; |
| 1018 | ProcessFreeBSD &process = monitor->GetProcess(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1019 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1020 | if (pid <= 1) { |
| 1021 | args->m_error.SetErrorToGenericError(); |
| 1022 | args->m_error.SetErrorString("Attaching to process 1 is not allowed."); |
| 1023 | return; |
| 1024 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1025 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1026 | // Attach to the requested process. |
| 1027 | if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0) { |
| 1028 | args->m_error.SetErrorToErrno(); |
| 1029 | return; |
| 1030 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1031 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1032 | int status; |
| 1033 | if ((status = waitpid(pid, NULL, 0)) < 0) { |
| 1034 | args->m_error.SetErrorToErrno(); |
| 1035 | return; |
| 1036 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1037 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1038 | process.SendMessage(ProcessMessage::Attach(pid)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1041 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1042 | ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids) { |
| 1043 | lwpid_t *tids; |
| 1044 | int tdcnt; |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1045 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1046 | thread_ids.clear(); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1047 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1048 | tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0); |
| 1049 | if (tdcnt <= 0) |
| 1050 | return 0; |
| 1051 | tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids)); |
| 1052 | if (tids == NULL) |
| 1053 | return 0; |
| 1054 | if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) { |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1055 | free(tids); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1056 | return 0; |
| 1057 | } |
| 1058 | thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt); |
| 1059 | free(tids); |
| 1060 | return thread_ids.size(); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1063 | bool ProcessMonitor::MonitorCallback(ProcessMonitor *monitor, lldb::pid_t pid, |
| 1064 | bool exited, int signal, int status) { |
| 1065 | ProcessMessage message; |
| 1066 | ProcessFreeBSD *process = monitor->m_process; |
| 1067 | assert(process); |
| 1068 | bool stop_monitoring; |
| 1069 | struct ptrace_lwpinfo plwp; |
| 1070 | int ptrace_err; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1071 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1072 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1073 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1074 | if (exited) { |
| 1075 | if (log) |
| 1076 | log->Printf("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, |
| 1077 | __FUNCTION__, pid); |
| 1078 | message = ProcessMessage::Exit(pid, status); |
| 1079 | process->SendMessage(message); |
| 1080 | return pid == process->GetID(); |
| 1081 | } |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1082 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1083 | if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err)) |
| 1084 | stop_monitoring = true; // pid is gone. Bail. |
| 1085 | else { |
| 1086 | switch (plwp.pl_siginfo.si_signo) { |
| 1087 | case SIGTRAP: |
| 1088 | message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid); |
| 1089 | break; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1090 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1091 | default: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1092 | message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid); |
| 1093 | break; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1096 | process->SendMessage(message); |
| 1097 | stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage; |
| 1098 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1099 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1100 | return stop_monitoring; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1103 | ProcessMessage ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor, |
| 1104 | const siginfo_t *info, |
| 1105 | lldb::tid_t tid) { |
| 1106 | ProcessMessage message; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1108 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1109 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1110 | assert(monitor); |
| 1111 | assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!"); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1112 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1113 | switch (info->si_code) { |
| 1114 | default: |
| 1115 | assert(false && "Unexpected SIGTRAP code!"); |
| 1116 | break; |
| 1117 | |
| 1118 | case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */): { |
| 1119 | // The inferior process is about to exit. Maintain the process in a |
| 1120 | // state of "limbo" until we are explicitly commanded to detach, |
| 1121 | // destroy, resume, etc. |
| 1122 | unsigned long data = 0; |
| 1123 | if (!monitor->GetEventMessage(tid, &data)) |
| 1124 | data = -1; |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1125 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1126 | log->Printf("ProcessMonitor::%s() received exit? event, data = %lx, tid " |
| 1127 | "= %" PRIu64, |
| 1128 | __FUNCTION__, data, tid); |
| 1129 | message = ProcessMessage::Limbo(tid, (data >> 8)); |
| 1130 | break; |
| 1131 | } |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1132 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1133 | case 0: |
| 1134 | case TRAP_TRACE: |
| 1135 | if (log) |
| 1136 | log->Printf("ProcessMonitor::%s() received trace event, tid = %" PRIu64 |
| 1137 | " : si_code = %d", |
| 1138 | __FUNCTION__, tid, info->si_code); |
| 1139 | message = ProcessMessage::Trace(tid); |
| 1140 | break; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1142 | case SI_KERNEL: |
| 1143 | case TRAP_BRKPT: |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 1144 | if (monitor->m_process->IsSoftwareStepBreakpoint(tid)) { |
| 1145 | if (log) |
| 1146 | log->Printf("ProcessMonitor::%s() received sw single step breakpoint " |
| 1147 | "event, tid = %" PRIu64, |
| 1148 | __FUNCTION__, tid); |
| 1149 | message = ProcessMessage::Trace(tid); |
| 1150 | } else { |
| 1151 | if (log) |
| 1152 | log->Printf( |
| 1153 | "ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64, |
| 1154 | __FUNCTION__, tid); |
| 1155 | message = ProcessMessage::Break(tid); |
| 1156 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1157 | break; |
| 1158 | } |
| 1159 | |
| 1160 | return message; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1163 | ProcessMessage ProcessMonitor::MonitorSignal(ProcessMonitor *monitor, |
| 1164 | const siginfo_t *info, |
| 1165 | lldb::tid_t tid) { |
| 1166 | ProcessMessage message; |
| 1167 | int signo = info->si_signo; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1168 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1169 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1170 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1171 | // POSIX says that process behaviour is undefined after it ignores a SIGFPE, |
| 1172 | // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a |
| 1173 | // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD. |
| 1174 | // |
| 1175 | // IOW, user generated signals never generate what we consider to be a |
| 1176 | // "crash". |
| 1177 | // |
| 1178 | // Similarly, ACK signals generated by this monitor. |
| 1179 | if (info->si_code == SI_USER) { |
| 1180 | if (log) |
| 1181 | log->Printf( |
| 1182 | "ProcessMonitor::%s() received signal %s with code %s, pid = %d", |
| 1183 | __FUNCTION__, |
| 1184 | monitor->m_process->GetUnixSignals()->GetSignalAsCString(signo), |
| 1185 | "SI_USER", info->si_pid); |
| 1186 | if (info->si_pid == getpid()) |
| 1187 | return ProcessMessage::SignalDelivered(tid, signo); |
| 1188 | else |
| 1189 | return ProcessMessage::Signal(tid, signo); |
| 1190 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1191 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1192 | if (log) |
| 1193 | log->Printf( |
| 1194 | "ProcessMonitor::%s() received signal %s", __FUNCTION__, |
| 1195 | monitor->m_process->GetUnixSignals()->GetSignalAsCString(signo)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1196 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1197 | switch (signo) { |
| 1198 | case SIGSEGV: |
| 1199 | case SIGILL: |
| 1200 | case SIGFPE: |
| 1201 | case SIGBUS: |
| 1202 | lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); |
| 1203 | const auto reason = GetCrashReason(*info); |
| 1204 | return ProcessMessage::Crash(tid, reason, signo, fault_addr); |
| 1205 | } |
| 1206 | |
| 1207 | // Everything else is "normal" and does not require any special action on |
| 1208 | // our part. |
| 1209 | return ProcessMessage::Signal(tid, signo); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1212 | void ProcessMonitor::ServeOperation(OperationArgs *args) { |
| 1213 | ProcessMonitor *monitor = args->m_monitor; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1214 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1215 | // We are finised with the arguments and are ready to go. Sync with the |
| 1216 | // parent thread and start serving operations on the inferior. |
| 1217 | sem_post(&args->m_semaphore); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1218 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1219 | for (;;) { |
| 1220 | // wait for next pending operation |
| 1221 | sem_wait(&monitor->m_operation_pending); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1222 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1223 | monitor->m_operation->Execute(monitor); |
| 1224 | |
| 1225 | // notify calling thread that operation is complete |
| 1226 | sem_post(&monitor->m_operation_done); |
| 1227 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1230 | void ProcessMonitor::DoOperation(Operation *op) { |
| 1231 | std::lock_guard<std::mutex> guard(m_operation_mutex); |
| 1232 | |
| 1233 | m_operation = op; |
| 1234 | |
| 1235 | // notify operation thread that an operation is ready to be processed |
| 1236 | sem_post(&m_operation_pending); |
| 1237 | |
| 1238 | // wait for operation to complete |
| 1239 | sem_wait(&m_operation_done); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1242 | size_t ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, |
| 1243 | Error &error) { |
| 1244 | size_t result; |
| 1245 | ReadOperation op(vm_addr, buf, size, error, result); |
| 1246 | DoOperation(&op); |
| 1247 | return result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1250 | size_t ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, |
| 1251 | size_t size, lldb_private::Error &error) { |
| 1252 | size_t result; |
| 1253 | WriteOperation op(vm_addr, buf, size, error, result); |
| 1254 | DoOperation(&op); |
| 1255 | return result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1258 | bool ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 1259 | const char *reg_name, unsigned size, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1260 | RegisterValue &value) { |
| 1261 | bool result; |
| 1262 | ReadRegOperation op(tid, offset, size, value, result); |
| 1263 | DoOperation(&op); |
| 1264 | return result; |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1267 | bool ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset, |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 1268 | const char *reg_name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1269 | const RegisterValue &value) { |
| 1270 | bool result; |
| 1271 | WriteRegOperation op(tid, offset, value, result); |
| 1272 | DoOperation(&op); |
| 1273 | return result; |
Ed Maste | a4be2c5 | 2014-02-19 18:34:06 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1276 | bool ProcessMonitor::ReadDebugRegisterValue( |
| 1277 | lldb::tid_t tid, unsigned offset, const char *reg_name, unsigned size, |
| 1278 | lldb_private::RegisterValue &value) { |
| 1279 | bool result; |
| 1280 | ReadDebugRegOperation op(tid, offset, size, value, result); |
| 1281 | DoOperation(&op); |
| 1282 | return result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1285 | bool ProcessMonitor::WriteDebugRegisterValue( |
| 1286 | lldb::tid_t tid, unsigned offset, const char *reg_name, |
| 1287 | const lldb_private::RegisterValue &value) { |
| 1288 | bool result; |
| 1289 | WriteDebugRegOperation op(tid, offset, value, result); |
| 1290 | DoOperation(&op); |
| 1291 | return result; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1294 | bool ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) { |
| 1295 | bool result; |
| 1296 | ReadGPROperation op(tid, buf, result); |
| 1297 | DoOperation(&op); |
| 1298 | return result; |
| 1299 | } |
| 1300 | |
| 1301 | bool ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) { |
| 1302 | bool result; |
| 1303 | ReadFPROperation op(tid, buf, result); |
| 1304 | DoOperation(&op); |
| 1305 | return result; |
| 1306 | } |
| 1307 | |
| 1308 | bool ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, |
| 1309 | size_t buf_size, unsigned int regset) { |
| 1310 | return false; |
| 1311 | } |
| 1312 | |
| 1313 | bool ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) { |
| 1314 | bool result; |
| 1315 | WriteGPROperation op(tid, buf, result); |
| 1316 | DoOperation(&op); |
| 1317 | return result; |
| 1318 | } |
| 1319 | |
| 1320 | bool ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) { |
| 1321 | bool result; |
| 1322 | WriteFPROperation op(tid, buf, result); |
| 1323 | DoOperation(&op); |
| 1324 | return result; |
| 1325 | } |
| 1326 | |
| 1327 | bool ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, |
| 1328 | size_t buf_size, unsigned int regset) { |
| 1329 | return false; |
| 1330 | } |
| 1331 | |
| 1332 | bool ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value) { |
| 1333 | return false; |
| 1334 | } |
| 1335 | |
| 1336 | bool ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo) { |
| 1337 | bool result; |
| 1338 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 1339 | |
| 1340 | if (log) { |
| 1341 | const char *signame = |
| 1342 | m_process->GetUnixSignals()->GetSignalAsCString(signo); |
| 1343 | if (signame == nullptr) |
| 1344 | signame = "<none>"; |
| 1345 | log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s", |
| 1346 | __FUNCTION__, GetPID(), signame); |
| 1347 | } |
| 1348 | ResumeOperation op(signo, result); |
| 1349 | DoOperation(&op); |
| 1350 | if (log) |
| 1351 | log->Printf("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, |
| 1352 | result ? "true" : "false"); |
| 1353 | return result; |
| 1354 | } |
| 1355 | |
| 1356 | bool ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo) { |
| 1357 | bool result; |
| 1358 | SingleStepOperation op(signo, result); |
| 1359 | DoOperation(&op); |
| 1360 | return result; |
| 1361 | } |
| 1362 | |
| 1363 | bool ProcessMonitor::Kill() { |
| 1364 | bool result; |
| 1365 | KillOperation op(result); |
| 1366 | DoOperation(&op); |
| 1367 | return result; |
| 1368 | } |
| 1369 | |
| 1370 | bool ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo, |
| 1371 | int &ptrace_err) { |
| 1372 | bool result; |
| 1373 | LwpInfoOperation op(tid, lwpinfo, result, ptrace_err); |
| 1374 | DoOperation(&op); |
| 1375 | return result; |
| 1376 | } |
| 1377 | |
| 1378 | bool ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend) { |
| 1379 | bool result; |
| 1380 | ThreadSuspendOperation op(tid, suspend, result); |
| 1381 | DoOperation(&op); |
| 1382 | return result; |
| 1383 | } |
| 1384 | |
| 1385 | bool ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message) { |
| 1386 | bool result; |
| 1387 | EventMessageOperation op(tid, message, result); |
| 1388 | DoOperation(&op); |
| 1389 | return result; |
| 1390 | } |
| 1391 | |
| 1392 | lldb_private::Error ProcessMonitor::Detach(lldb::tid_t tid) { |
| 1393 | lldb_private::Error error; |
| 1394 | if (tid != LLDB_INVALID_THREAD_ID) { |
| 1395 | DetachOperation op(error); |
| 1396 | DoOperation(&op); |
| 1397 | } |
| 1398 | return error; |
| 1399 | } |
| 1400 | |
| 1401 | bool ProcessMonitor::DupDescriptor(const FileSpec &file_spec, int fd, |
| 1402 | int flags) { |
| 1403 | int target_fd = open(file_spec.GetCString(), flags, 0666); |
| 1404 | |
| 1405 | if (target_fd == -1) |
Ed Maste | 5d34af3 | 2013-06-24 15:09:18 +0000 | [diff] [blame] | 1406 | return false; |
Ed Maste | 5d34af3 | 2013-06-24 15:09:18 +0000 | [diff] [blame] | 1407 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1408 | if (dup2(target_fd, fd) == -1) |
Ed Maste | 5d34af3 | 2013-06-24 15:09:18 +0000 | [diff] [blame] | 1409 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1410 | |
| 1411 | return (close(target_fd) == -1) ? false : true; |
Ed Maste | 5d34af3 | 2013-06-24 15:09:18 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1414 | void ProcessMonitor::StopMonitoringChildProcess() { |
| 1415 | if (m_monitor_thread.IsJoinable()) { |
| 1416 | m_monitor_thread.Cancel(); |
| 1417 | m_monitor_thread.Join(nullptr); |
| 1418 | m_monitor_thread.Reset(); |
| 1419 | } |
Ed Maste | 68f5179 | 2013-10-18 19:16:44 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1422 | void ProcessMonitor::StopMonitor() { |
| 1423 | StopMonitoringChildProcess(); |
| 1424 | StopOpThread(); |
| 1425 | sem_destroy(&m_operation_pending); |
| 1426 | sem_destroy(&m_operation_done); |
| 1427 | if (m_terminal_fd >= 0) { |
| 1428 | close(m_terminal_fd); |
| 1429 | m_terminal_fd = -1; |
| 1430 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Andrew Kaylor | d4d5499 | 2013-09-17 00:30:24 +0000 | [diff] [blame] | 1433 | // FIXME: On Linux, when a new thread is created, we receive to notifications, |
| 1434 | // (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the |
| 1435 | // child thread id as additional information, and (2) a SIGSTOP|SI_USER from |
| 1436 | // the new child thread indicating that it has is stopped because we attached. |
| 1437 | // We have no guarantee of the order in which these arrive, but we need both |
| 1438 | // before we are ready to proceed. We currently keep a list of threads which |
| 1439 | // have sent the initial SIGSTOP|SI_USER event. Then when we receive the |
| 1440 | // SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred |
| 1441 | // we call ProcessMonitor::WaitForInitialTIDStop() to wait for it. |
| 1442 | // |
| 1443 | // Right now, the above logic is in ProcessPOSIX, so we need a definition of |
| 1444 | // this function in the FreeBSD ProcessMonitor implementation even if it isn't |
| 1445 | // logically needed. |
| 1446 | // |
| 1447 | // We really should figure out what actually happens on FreeBSD and move the |
| 1448 | // Linux-specific logic out of ProcessPOSIX as needed. |
| 1449 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1450 | bool ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid) { return true; } |
Andrew Kaylor | d4d5499 | 2013-09-17 00:30:24 +0000 | [diff] [blame] | 1451 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1452 | void ProcessMonitor::StopOpThread() { |
| 1453 | if (!m_operation_thread.IsJoinable()) |
| 1454 | return; |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1455 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1456 | m_operation_thread.Cancel(); |
| 1457 | m_operation_thread.Join(nullptr); |
| 1458 | m_operation_thread.Reset(); |
Ed Maste | a02f553 | 2013-07-02 16:45:16 +0000 | [diff] [blame] | 1459 | } |