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