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