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