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