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 "FreeBSDThread.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((void *)&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((void *)&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(ProcessFreeBSD *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 using namespace std::placeholders; 823 824 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp, 825 stdin_file_spec, 826 stdout_file_spec, 827 stderr_file_spec, 828 working_dir)); 829 830 831 sem_init(&m_operation_pending, 0, 0); 832 sem_init(&m_operation_done, 0, 0); 833 834 StartLaunchOpThread(args.get(), error); 835 if (!error.Success()) 836 return; 837 838 WAIT_AGAIN: 839 // Wait for the operation thread to initialize. 840 if (sem_wait(&args->m_semaphore)) 841 { 842 if (errno == EINTR) 843 goto WAIT_AGAIN; 844 else 845 { 846 error.SetErrorToErrno(); 847 return; 848 } 849 } 850 851 // Check that the launch was a success. 852 if (!args->m_error.Success()) 853 { 854 StopOpThread(); 855 error = args->m_error; 856 return; 857 } 858 859 // Finally, start monitoring the child process for change in state. 860 m_monitor_thread = Host::StartMonitoringChildProcess( 861 std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4), GetPID(), true); 862 if (!m_monitor_thread.IsJoinable()) 863 { 864 error.SetErrorToGenericError(); 865 error.SetErrorString("Process launch failed."); 866 return; 867 } 868 } 869 870 ProcessMonitor::ProcessMonitor(ProcessFreeBSD *process, 871 lldb::pid_t pid, 872 lldb_private::Error &error) 873 : m_process(static_cast<ProcessFreeBSD *>(process)), 874 m_pid(pid), 875 m_terminal_fd(-1), 876 m_operation(0) 877 { 878 using namespace std::placeholders; 879 880 sem_init(&m_operation_pending, 0, 0); 881 sem_init(&m_operation_done, 0, 0); 882 883 884 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid)); 885 886 StartAttachOpThread(args.get(), error); 887 if (!error.Success()) 888 return; 889 890 WAIT_AGAIN: 891 // Wait for the operation thread to initialize. 892 if (sem_wait(&args->m_semaphore)) 893 { 894 if (errno == EINTR) 895 goto WAIT_AGAIN; 896 else 897 { 898 error.SetErrorToErrno(); 899 return; 900 } 901 } 902 903 // Check that the attach was a success. 904 if (!args->m_error.Success()) 905 { 906 StopOpThread(); 907 error = args->m_error; 908 return; 909 } 910 911 // Finally, start monitoring the child process for change in state. 912 m_monitor_thread = Host::StartMonitoringChildProcess( 913 std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4), GetPID(), true); 914 if (!m_monitor_thread.IsJoinable()) 915 { 916 error.SetErrorToGenericError(); 917 error.SetErrorString("Process attach failed."); 918 return; 919 } 920 } 921 922 ProcessMonitor::~ProcessMonitor() 923 { 924 StopMonitor(); 925 } 926 927 //------------------------------------------------------------------------------ 928 // Thread setup and tear down. 929 void 930 ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error) 931 { 932 static const char *g_thread_name = "lldb.process.freebsd.operation"; 933 934 if (m_operation_thread.IsJoinable()) 935 return; 936 937 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error); 938 } 939 940 void * 941 ProcessMonitor::LaunchOpThread(void *arg) 942 { 943 LaunchArgs *args = static_cast<LaunchArgs*>(arg); 944 945 if (!Launch(args)) { 946 sem_post(&args->m_semaphore); 947 return NULL; 948 } 949 950 ServeOperation(args); 951 return NULL; 952 } 953 954 bool 955 ProcessMonitor::Launch(LaunchArgs *args) 956 { 957 ProcessMonitor *monitor = args->m_monitor; 958 ProcessFreeBSD &process = monitor->GetProcess(); 959 const char **argv = args->m_argv; 960 const char **envp = args->m_envp; 961 const FileSpec &stdin_file_spec = args->m_stdin_file_spec; 962 const FileSpec &stdout_file_spec = args->m_stdout_file_spec; 963 const FileSpec &stderr_file_spec = args->m_stderr_file_spec; 964 const FileSpec &working_dir = args->m_working_dir; 965 966 lldb_utility::PseudoTerminal terminal; 967 const size_t err_len = 1024; 968 char err_str[err_len]; 969 ::pid_t pid; 970 971 // Propagate the environment if one is not supplied. 972 if (envp == NULL || envp[0] == NULL) 973 envp = const_cast<const char **>(environ); 974 975 if ((pid = terminal.Fork(err_str, err_len)) == -1) 976 { 977 args->m_error.SetErrorToGenericError(); 978 args->m_error.SetErrorString("Process fork failed."); 979 goto FINISH; 980 } 981 982 // Recognized child exit status codes. 983 enum { 984 ePtraceFailed = 1, 985 eDupStdinFailed, 986 eDupStdoutFailed, 987 eDupStderrFailed, 988 eChdirFailed, 989 eExecFailed, 990 eSetGidFailed 991 }; 992 993 // Child process. 994 if (pid == 0) 995 { 996 // Trace this process. 997 if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0) 998 exit(ePtraceFailed); 999 1000 // terminal has already dupped the tty descriptors to stdin/out/err. 1001 // This closes original fd from which they were copied (and avoids 1002 // leaking descriptors to the debugged process. 1003 terminal.CloseSlaveFileDescriptor(); 1004 1005 // Do not inherit setgid powers. 1006 if (setgid(getgid()) != 0) 1007 exit(eSetGidFailed); 1008 1009 // Let us have our own process group. 1010 setpgid(0, 0); 1011 1012 // Dup file descriptors if needed. 1013 // 1014 // FIXME: If two or more of the paths are the same we needlessly open 1015 // the same file multiple times. 1016 if (stdin_file_spec) 1017 if (!DupDescriptor(stdin_file_spec, STDIN_FILENO, O_RDONLY)) 1018 exit(eDupStdinFailed); 1019 1020 if (stdout_file_spec) 1021 if (!DupDescriptor(stdout_file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT)) 1022 exit(eDupStdoutFailed); 1023 1024 if (stderr_file_spec) 1025 if (!DupDescriptor(stderr_file_spec, STDERR_FILENO, O_WRONLY | O_CREAT)) 1026 exit(eDupStderrFailed); 1027 1028 // Change working directory 1029 if (working_dir && 0 != ::chdir(working_dir.GetCString())) 1030 exit(eChdirFailed); 1031 1032 // Execute. We should never return. 1033 execve(argv[0], 1034 const_cast<char *const *>(argv), 1035 const_cast<char *const *>(envp)); 1036 exit(eExecFailed); 1037 } 1038 1039 // Wait for the child process to to trap on its call to execve. 1040 ::pid_t wpid; 1041 int status; 1042 if ((wpid = waitpid(pid, &status, 0)) < 0) 1043 { 1044 args->m_error.SetErrorToErrno(); 1045 goto FINISH; 1046 } 1047 else if (WIFEXITED(status)) 1048 { 1049 // open, dup or execve likely failed for some reason. 1050 args->m_error.SetErrorToGenericError(); 1051 switch (WEXITSTATUS(status)) 1052 { 1053 case ePtraceFailed: 1054 args->m_error.SetErrorString("Child ptrace failed."); 1055 break; 1056 case eDupStdinFailed: 1057 args->m_error.SetErrorString("Child open stdin failed."); 1058 break; 1059 case eDupStdoutFailed: 1060 args->m_error.SetErrorString("Child open stdout failed."); 1061 break; 1062 case eDupStderrFailed: 1063 args->m_error.SetErrorString("Child open stderr failed."); 1064 break; 1065 case eChdirFailed: 1066 args->m_error.SetErrorString("Child failed to set working directory."); 1067 break; 1068 case eExecFailed: 1069 args->m_error.SetErrorString("Child exec failed."); 1070 break; 1071 case eSetGidFailed: 1072 args->m_error.SetErrorString("Child setgid failed."); 1073 break; 1074 default: 1075 args->m_error.SetErrorString("Child returned unknown exit status."); 1076 break; 1077 } 1078 goto FINISH; 1079 } 1080 assert(WIFSTOPPED(status) && wpid == (::pid_t)pid && 1081 "Could not sync with inferior process."); 1082 1083 #ifdef notyet 1084 // Have the child raise an event on exit. This is used to keep the child in 1085 // limbo until it is destroyed. 1086 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0) 1087 { 1088 args->m_error.SetErrorToErrno(); 1089 goto FINISH; 1090 } 1091 #endif 1092 // Release the master terminal descriptor and pass it off to the 1093 // ProcessMonitor instance. Similarly stash the inferior pid. 1094 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); 1095 monitor->m_pid = pid; 1096 1097 // Set the terminal fd to be in non blocking mode (it simplifies the 1098 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking 1099 // descriptor to read from). 1100 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error)) 1101 goto FINISH; 1102 1103 process.SendMessage(ProcessMessage::Attach(pid)); 1104 1105 FINISH: 1106 return args->m_error.Success(); 1107 } 1108 1109 void 1110 ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error) 1111 { 1112 static const char *g_thread_name = "lldb.process.freebsd.operation"; 1113 1114 if (m_operation_thread.IsJoinable()) 1115 return; 1116 1117 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error); 1118 } 1119 1120 void * 1121 ProcessMonitor::AttachOpThread(void *arg) 1122 { 1123 AttachArgs *args = static_cast<AttachArgs*>(arg); 1124 1125 Attach(args); 1126 1127 ServeOperation(args); 1128 return NULL; 1129 } 1130 1131 void 1132 ProcessMonitor::Attach(AttachArgs *args) 1133 { 1134 lldb::pid_t pid = args->m_pid; 1135 1136 ProcessMonitor *monitor = args->m_monitor; 1137 ProcessFreeBSD &process = monitor->GetProcess(); 1138 1139 if (pid <= 1) 1140 { 1141 args->m_error.SetErrorToGenericError(); 1142 args->m_error.SetErrorString("Attaching to process 1 is not allowed."); 1143 return; 1144 } 1145 1146 // Attach to the requested process. 1147 if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0) 1148 { 1149 args->m_error.SetErrorToErrno(); 1150 return; 1151 } 1152 1153 int status; 1154 if ((status = waitpid(pid, NULL, 0)) < 0) 1155 { 1156 args->m_error.SetErrorToErrno(); 1157 return; 1158 } 1159 1160 process.SendMessage(ProcessMessage::Attach(pid)); 1161 } 1162 1163 size_t 1164 ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t>&thread_ids) 1165 { 1166 lwpid_t *tids; 1167 int tdcnt; 1168 1169 thread_ids.clear(); 1170 1171 tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0); 1172 if (tdcnt <= 0) 1173 return 0; 1174 tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids)); 1175 if (tids == NULL) 1176 return 0; 1177 if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) { 1178 free(tids); 1179 return 0; 1180 } 1181 thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt); 1182 free(tids); 1183 return thread_ids.size(); 1184 } 1185 1186 bool 1187 ProcessMonitor::MonitorCallback(ProcessMonitor *monitor, lldb::pid_t pid, bool exited, int signal, int status) 1188 { 1189 ProcessMessage message; 1190 ProcessFreeBSD *process = monitor->m_process; 1191 assert(process); 1192 bool stop_monitoring; 1193 struct ptrace_lwpinfo plwp; 1194 int ptrace_err; 1195 1196 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); 1197 1198 if (exited) 1199 { 1200 if (log) 1201 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid); 1202 message = ProcessMessage::Exit(pid, status); 1203 process->SendMessage(message); 1204 return pid == process->GetID(); 1205 } 1206 1207 if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err)) 1208 stop_monitoring = true; // pid is gone. Bail. 1209 else { 1210 switch (plwp.pl_siginfo.si_signo) 1211 { 1212 case SIGTRAP: 1213 message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid); 1214 break; 1215 1216 default: 1217 message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid); 1218 break; 1219 } 1220 1221 process->SendMessage(message); 1222 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage; 1223 } 1224 1225 return stop_monitoring; 1226 } 1227 1228 ProcessMessage 1229 ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor, 1230 const siginfo_t *info, lldb::tid_t tid) 1231 { 1232 ProcessMessage message; 1233 1234 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); 1235 1236 assert(monitor); 1237 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!"); 1238 1239 switch (info->si_code) 1240 { 1241 default: 1242 assert(false && "Unexpected SIGTRAP code!"); 1243 break; 1244 1245 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */): 1246 { 1247 // The inferior process is about to exit. Maintain the process in a 1248 // state of "limbo" until we are explicitly commanded to detach, 1249 // destroy, resume, etc. 1250 unsigned long data = 0; 1251 if (!monitor->GetEventMessage(tid, &data)) 1252 data = -1; 1253 if (log) 1254 log->Printf ("ProcessMonitor::%s() received exit? event, data = %lx, tid = %" PRIu64, __FUNCTION__, data, tid); 1255 message = ProcessMessage::Limbo(tid, (data >> 8)); 1256 break; 1257 } 1258 1259 case 0: 1260 case TRAP_TRACE: 1261 if (log) 1262 log->Printf ("ProcessMonitor::%s() received trace event, tid = %" PRIu64 " : si_code = %d", __FUNCTION__, tid, info->si_code); 1263 message = ProcessMessage::Trace(tid); 1264 break; 1265 1266 case SI_KERNEL: 1267 case TRAP_BRKPT: 1268 if (log) 1269 log->Printf ("ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64, __FUNCTION__, tid); 1270 message = ProcessMessage::Break(tid); 1271 break; 1272 } 1273 1274 return message; 1275 } 1276 1277 ProcessMessage 1278 ProcessMonitor::MonitorSignal(ProcessMonitor *monitor, 1279 const siginfo_t *info, lldb::tid_t tid) 1280 { 1281 ProcessMessage message; 1282 int signo = info->si_signo; 1283 1284 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); 1285 1286 // POSIX says that process behaviour is undefined after it ignores a SIGFPE, 1287 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a 1288 // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD. 1289 // 1290 // IOW, user generated signals never generate what we consider to be a 1291 // "crash". 1292 // 1293 // Similarly, ACK signals generated by this monitor. 1294 if (info->si_code == SI_USER) 1295 { 1296 if (log) 1297 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d", 1298 __FUNCTION__, 1299 monitor->m_process->GetUnixSignals()->GetSignalAsCString (signo), 1300 "SI_USER", 1301 info->si_pid); 1302 if (info->si_pid == getpid()) 1303 return ProcessMessage::SignalDelivered(tid, signo); 1304 else 1305 return ProcessMessage::Signal(tid, signo); 1306 } 1307 1308 if (log) 1309 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals()->GetSignalAsCString (signo)); 1310 1311 switch (signo) 1312 { 1313 case SIGSEGV: 1314 case SIGILL: 1315 case SIGFPE: 1316 case SIGBUS: 1317 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); 1318 const auto reason = GetCrashReason(*info); 1319 return ProcessMessage::Crash(tid, reason, signo, fault_addr); 1320 } 1321 1322 // Everything else is "normal" and does not require any special action on 1323 // our part. 1324 return ProcessMessage::Signal(tid, signo); 1325 } 1326 1327 void 1328 ProcessMonitor::ServeOperation(OperationArgs *args) 1329 { 1330 ProcessMonitor *monitor = args->m_monitor; 1331 1332 // We are finised with the arguments and are ready to go. Sync with the 1333 // parent thread and start serving operations on the inferior. 1334 sem_post(&args->m_semaphore); 1335 1336 for (;;) 1337 { 1338 // wait for next pending operation 1339 sem_wait(&monitor->m_operation_pending); 1340 1341 monitor->m_operation->Execute(monitor); 1342 1343 // notify calling thread that operation is complete 1344 sem_post(&monitor->m_operation_done); 1345 } 1346 } 1347 1348 void 1349 ProcessMonitor::DoOperation(Operation *op) 1350 { 1351 std::lock_guard<std::mutex> guard(m_operation_mutex); 1352 1353 m_operation = op; 1354 1355 // notify operation thread that an operation is ready to be processed 1356 sem_post(&m_operation_pending); 1357 1358 // wait for operation to complete 1359 sem_wait(&m_operation_done); 1360 } 1361 1362 size_t 1363 ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, 1364 Error &error) 1365 { 1366 size_t result; 1367 ReadOperation op(vm_addr, buf, size, error, result); 1368 DoOperation(&op); 1369 return result; 1370 } 1371 1372 size_t 1373 ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, 1374 lldb_private::Error &error) 1375 { 1376 size_t result; 1377 WriteOperation op(vm_addr, buf, size, error, result); 1378 DoOperation(&op); 1379 return result; 1380 } 1381 1382 bool 1383 ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name, 1384 unsigned size, RegisterValue &value) 1385 { 1386 bool result; 1387 ReadRegOperation op(tid, offset, size, value, result); 1388 DoOperation(&op); 1389 return result; 1390 } 1391 1392 bool 1393 ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset, 1394 const char* reg_name, const RegisterValue &value) 1395 { 1396 bool result; 1397 WriteRegOperation op(tid, offset, value, result); 1398 DoOperation(&op); 1399 return result; 1400 } 1401 1402 bool 1403 ProcessMonitor::ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset, 1404 const char *reg_name, unsigned size, 1405 lldb_private::RegisterValue &value) 1406 { 1407 bool result; 1408 ReadDebugRegOperation op(tid, offset, size, value, result); 1409 DoOperation(&op); 1410 return result; 1411 } 1412 1413 bool 1414 ProcessMonitor::WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset, 1415 const char *reg_name, 1416 const lldb_private::RegisterValue &value) 1417 { 1418 bool result; 1419 WriteDebugRegOperation op(tid, offset, value, result); 1420 DoOperation(&op); 1421 return result; 1422 } 1423 1424 bool 1425 ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) 1426 { 1427 bool result; 1428 ReadGPROperation op(tid, buf, result); 1429 DoOperation(&op); 1430 return result; 1431 } 1432 1433 bool 1434 ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) 1435 { 1436 bool result; 1437 ReadFPROperation op(tid, buf, result); 1438 DoOperation(&op); 1439 return result; 1440 } 1441 1442 bool 1443 ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 1444 { 1445 return false; 1446 } 1447 1448 bool 1449 ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) 1450 { 1451 bool result; 1452 WriteGPROperation op(tid, buf, result); 1453 DoOperation(&op); 1454 return result; 1455 } 1456 1457 bool 1458 ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) 1459 { 1460 bool result; 1461 WriteFPROperation op(tid, buf, result); 1462 DoOperation(&op); 1463 return result; 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 bool 1473 ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value) 1474 { 1475 return false; 1476 } 1477 1478 bool 1479 ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo) 1480 { 1481 bool result; 1482 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); 1483 1484 if (log) { 1485 const char *signame = m_process->GetUnixSignals()->GetSignalAsCString (signo); 1486 if (signame == nullptr) 1487 signame = "<none>"; 1488 log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s", 1489 __FUNCTION__, GetPID(), signame); 1490 } 1491 ResumeOperation op(signo, result); 1492 DoOperation(&op); 1493 if (log) 1494 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false"); 1495 return result; 1496 } 1497 1498 bool 1499 ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo) 1500 { 1501 bool result; 1502 SingleStepOperation op(signo, result); 1503 DoOperation(&op); 1504 return result; 1505 } 1506 1507 bool 1508 ProcessMonitor::Kill() 1509 { 1510 bool result; 1511 KillOperation op(result); 1512 DoOperation(&op); 1513 return result; 1514 } 1515 1516 bool 1517 ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &ptrace_err) 1518 { 1519 bool result; 1520 LwpInfoOperation op(tid, lwpinfo, result, ptrace_err); 1521 DoOperation(&op); 1522 return result; 1523 } 1524 1525 bool 1526 ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend) 1527 { 1528 bool result; 1529 ThreadSuspendOperation op(tid, suspend, result); 1530 DoOperation(&op); 1531 return result; 1532 } 1533 1534 bool 1535 ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message) 1536 { 1537 bool result; 1538 EventMessageOperation op(tid, message, result); 1539 DoOperation(&op); 1540 return result; 1541 } 1542 1543 lldb_private::Error 1544 ProcessMonitor::Detach(lldb::tid_t tid) 1545 { 1546 lldb_private::Error error; 1547 if (tid != LLDB_INVALID_THREAD_ID) 1548 { 1549 DetachOperation op(error); 1550 DoOperation(&op); 1551 } 1552 return error; 1553 } 1554 1555 bool 1556 ProcessMonitor::DupDescriptor(const FileSpec &file_spec, int fd, int flags) 1557 { 1558 int target_fd = open(file_spec.GetCString(), flags, 0666); 1559 1560 if (target_fd == -1) 1561 return false; 1562 1563 if (dup2(target_fd, fd) == -1) 1564 return false; 1565 1566 return (close(target_fd) == -1) ? false : true; 1567 } 1568 1569 void 1570 ProcessMonitor::StopMonitoringChildProcess() 1571 { 1572 if (m_monitor_thread.IsJoinable()) 1573 { 1574 m_monitor_thread.Cancel(); 1575 m_monitor_thread.Join(nullptr); 1576 m_monitor_thread.Reset(); 1577 } 1578 } 1579 1580 void 1581 ProcessMonitor::StopMonitor() 1582 { 1583 StopMonitoringChildProcess(); 1584 StopOpThread(); 1585 sem_destroy(&m_operation_pending); 1586 sem_destroy(&m_operation_done); 1587 if (m_terminal_fd >= 0) { 1588 close(m_terminal_fd); 1589 m_terminal_fd = -1; 1590 } 1591 } 1592 1593 // FIXME: On Linux, when a new thread is created, we receive to notifications, 1594 // (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the 1595 // child thread id as additional information, and (2) a SIGSTOP|SI_USER from 1596 // the new child thread indicating that it has is stopped because we attached. 1597 // We have no guarantee of the order in which these arrive, but we need both 1598 // before we are ready to proceed. We currently keep a list of threads which 1599 // have sent the initial SIGSTOP|SI_USER event. Then when we receive the 1600 // SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred 1601 // we call ProcessMonitor::WaitForInitialTIDStop() to wait for it. 1602 // 1603 // Right now, the above logic is in ProcessPOSIX, so we need a definition of 1604 // this function in the FreeBSD ProcessMonitor implementation even if it isn't 1605 // logically needed. 1606 // 1607 // We really should figure out what actually happens on FreeBSD and move the 1608 // Linux-specific logic out of ProcessPOSIX as needed. 1609 1610 bool 1611 ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid) 1612 { 1613 return true; 1614 } 1615 1616 void 1617 ProcessMonitor::StopOpThread() 1618 { 1619 if (!m_operation_thread.IsJoinable()) 1620 return; 1621 1622 m_operation_thread.Cancel(); 1623 m_operation_thread.Join(nullptr); 1624 m_operation_thread.Reset(); 1625 } 1626