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