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