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