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