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