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