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