ProcessMonitor.cpp revision 5d187e5495ee17f6763337a6ae28c2a7b07e4945
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 <unistd.h> 15#include <sys/ptrace.h> 16#include <sys/socket.h> 17#include <sys/types.h> 18#include <sys/wait.h> 19 20// C++ Includes 21// Other libraries and framework includes 22#include "lldb/Core/Error.h" 23#include "lldb/Core/Scalar.h" 24#include "lldb/Host/Host.h" 25#include "lldb/Target/Thread.h" 26#include "lldb/Target/RegisterContext.h" 27#include "lldb/Utility/PseudoTerminal.h" 28 29#include "LinuxThread.h" 30#include "ProcessLinux.h" 31#include "ProcessMonitor.h" 32 33 34using namespace lldb_private; 35 36//------------------------------------------------------------------------------ 37// Static implementations of ProcessMonitor::ReadMemory and 38// ProcessMonitor::WriteMemory. This enables mutual recursion between these 39// functions without needed to go thru the thread funnel. 40 41static size_t 42DoReadMemory(lldb::pid_t pid, unsigned word_size, 43 lldb::addr_t vm_addr, void *buf, size_t size, Error &error) 44{ 45 unsigned char *dst = static_cast<unsigned char*>(buf); 46 size_t bytes_read; 47 size_t remainder; 48 long data; 49 50 for (bytes_read = 0; bytes_read < size; bytes_read += remainder) 51 { 52 errno = 0; 53 data = ptrace(PTRACE_PEEKDATA, pid, vm_addr, NULL); 54 55 if (data == -1L && errno) 56 { 57 error.SetErrorToErrno(); 58 return bytes_read; 59 } 60 61 remainder = size - bytes_read; 62 remainder = remainder > word_size ? word_size : remainder; 63 for (unsigned i = 0; i < remainder; ++i) 64 dst[i] = ((data >> i*8) & 0xFF); 65 vm_addr += word_size; 66 dst += word_size; 67 } 68 69 return bytes_read; 70} 71 72static size_t 73DoWriteMemory(lldb::pid_t pid, unsigned word_size, 74 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error) 75{ 76 const unsigned char *src = static_cast<const unsigned char*>(buf); 77 size_t bytes_written = 0; 78 size_t remainder; 79 80 for (bytes_written = 0; bytes_written < size; bytes_written += remainder) 81 { 82 remainder = size - bytes_written; 83 remainder = remainder > word_size ? word_size : remainder; 84 85 if (remainder == word_size) 86 { 87 unsigned long data = 0; 88 for (unsigned i = 0; i < word_size; ++i) 89 data |= (unsigned long)src[i] << i*8; 90 91 if (ptrace(PTRACE_POKEDATA, pid, vm_addr, data)) 92 { 93 error.SetErrorToErrno(); 94 return bytes_written; 95 } 96 } 97 else 98 { 99 unsigned char buff[8]; 100 if (DoReadMemory(pid, word_size, vm_addr, 101 buff, word_size, error) != word_size) 102 return bytes_written; 103 104 memcpy(buff, src, remainder); 105 106 if (DoWriteMemory(pid, word_size, vm_addr, 107 buff, word_size, error) != word_size) 108 return bytes_written; 109 } 110 111 vm_addr += word_size; 112 src += word_size; 113 } 114 return bytes_written; 115} 116 117 118//------------------------------------------------------------------------------ 119/// @class Operation 120/// @brief Represents a ProcessMonitor operation. 121/// 122/// Under Linux, it is not possible to ptrace() from any other thread but the 123/// one that spawned or attached to the process from the start. Therefore, when 124/// a ProcessMonitor is asked to deliver or change the state of an inferior 125/// process the operation must be "funneled" to a specific thread to perform the 126/// task. The Operation class provides an abstract base for all services the 127/// ProcessMonitor must perform via the single virtual function Execute, thus 128/// encapsulating the code that needs to run in the privileged context. 129class Operation 130{ 131public: 132 virtual void Execute(ProcessMonitor *monitor) = 0; 133}; 134 135//------------------------------------------------------------------------------ 136/// @class ReadOperation 137/// @brief Implements ProcessMonitor::ReadMemory. 138class ReadOperation : public Operation 139{ 140public: 141 ReadOperation(lldb::addr_t addr, void *buff, size_t size, 142 Error &error, size_t &result) 143 : m_addr(addr), m_buff(buff), m_size(size), 144 m_error(error), m_result(result) 145 { } 146 147 void Execute(ProcessMonitor *monitor); 148 149private: 150 lldb::addr_t m_addr; 151 void *m_buff; 152 size_t m_size; 153 Error &m_error; 154 size_t &m_result; 155}; 156 157void 158ReadOperation::Execute(ProcessMonitor *monitor) 159{ 160 const unsigned word_size = monitor->GetProcess().GetAddressByteSize(); 161 lldb::pid_t pid = monitor->GetPID(); 162 163 m_result = DoReadMemory(pid, word_size, m_addr, m_buff, m_size, m_error); 164} 165 166//------------------------------------------------------------------------------ 167/// @class ReadOperation 168/// @brief Implements ProcessMonitor::WriteMemory. 169class WriteOperation : public Operation 170{ 171public: 172 WriteOperation(lldb::addr_t addr, const void *buff, size_t size, 173 Error &error, size_t &result) 174 : m_addr(addr), m_buff(buff), m_size(size), 175 m_error(error), m_result(result) 176 { } 177 178 void Execute(ProcessMonitor *monitor); 179 180private: 181 lldb::addr_t m_addr; 182 const void *m_buff; 183 size_t m_size; 184 Error &m_error; 185 size_t &m_result; 186}; 187 188void 189WriteOperation::Execute(ProcessMonitor *monitor) 190{ 191 const unsigned word_size = monitor->GetProcess().GetAddressByteSize(); 192 lldb::pid_t pid = monitor->GetPID(); 193 194 m_result = DoWriteMemory(pid, word_size, m_addr, m_buff, m_size, m_error); 195} 196 197//------------------------------------------------------------------------------ 198/// @class ReadRegOperation 199/// @brief Implements ProcessMonitor::ReadRegisterValue. 200class ReadRegOperation : public Operation 201{ 202public: 203 ReadRegOperation(unsigned offset, Scalar &value, bool &result) 204 : m_offset(offset), m_value(value), m_result(result) 205 { } 206 207 void Execute(ProcessMonitor *monitor); 208 209private: 210 unsigned m_offset; 211 Scalar &m_value; 212 bool &m_result; 213}; 214 215void 216ReadRegOperation::Execute(ProcessMonitor *monitor) 217{ 218 lldb::pid_t pid = monitor->GetPID(); 219 220 // Set errno to zero so that we can detect a failed peek. 221 errno = 0; 222 unsigned long data = ptrace(PTRACE_PEEKUSER, pid, m_offset, NULL); 223 224 if (data == -1UL && errno) 225 m_result = false; 226 else 227 { 228 m_value = data; 229 m_result = true; 230 } 231} 232 233//------------------------------------------------------------------------------ 234/// @class WriteRegOperation 235/// @brief Implements ProcessMonitor::WriteRegisterValue. 236class WriteRegOperation : public Operation 237{ 238public: 239 WriteRegOperation(unsigned offset, const Scalar &value, bool &result) 240 : m_offset(offset), m_value(value), m_result(result) 241 { } 242 243 void Execute(ProcessMonitor *monitor); 244 245private: 246 unsigned m_offset; 247 const Scalar &m_value; 248 bool &m_result; 249}; 250 251void 252WriteRegOperation::Execute(ProcessMonitor *monitor) 253{ 254 lldb::pid_t pid = monitor->GetPID(); 255 256 if (ptrace(PTRACE_POKEUSER, pid, m_offset, m_value.ULong())) 257 m_result = false; 258 else 259 m_result = true; 260} 261 262//------------------------------------------------------------------------------ 263/// @class ResumeOperation 264/// @brief Implements ProcessMonitor::Resume. 265class ResumeOperation : public Operation 266{ 267public: 268 ResumeOperation(lldb::tid_t tid, bool &result) : 269 m_tid(tid), m_result(result) { } 270 271 void Execute(ProcessMonitor *monitor); 272 273private: 274 lldb::tid_t m_tid; 275 bool &m_result; 276}; 277 278void 279ResumeOperation::Execute(ProcessMonitor *monitor) 280{ 281 if (ptrace(PTRACE_CONT, m_tid, NULL, NULL)) 282 m_result = false; 283 else 284 m_result = true; 285} 286 287//------------------------------------------------------------------------------ 288/// @class ResumeOperation 289/// @brief Implements ProcessMonitor::SingleStep. 290class SingleStepOperation : public Operation 291{ 292public: 293 SingleStepOperation(lldb::tid_t tid, bool &result) 294 : m_tid(tid), m_result(result) { } 295 296 void Execute(ProcessMonitor *monitor); 297 298private: 299 lldb::tid_t m_tid; 300 bool &m_result; 301}; 302 303void 304SingleStepOperation::Execute(ProcessMonitor *monitor) 305{ 306 if (ptrace(PTRACE_SINGLESTEP, m_tid, NULL, NULL)) 307 m_result = false; 308 else 309 m_result = true; 310} 311 312//------------------------------------------------------------------------------ 313/// @class SiginfoOperation 314/// @brief Implements ProcessMonitor::GetSignalInfo. 315class SiginfoOperation : public Operation 316{ 317public: 318 SiginfoOperation(lldb::tid_t tid, void *info, bool &result) 319 : m_tid(tid), m_info(info), m_result(result) { } 320 321 void Execute(ProcessMonitor *monitor); 322 323private: 324 lldb::tid_t m_tid; 325 void *m_info; 326 bool &m_result; 327}; 328 329void 330SiginfoOperation::Execute(ProcessMonitor *monitor) 331{ 332 if (ptrace(PTRACE_GETSIGINFO, m_tid, NULL, m_info)) 333 m_result = false; 334 else 335 m_result = true; 336} 337 338//------------------------------------------------------------------------------ 339/// @class EventMessageOperation 340/// @brief Implements ProcessMonitor::GetEventMessage. 341class EventMessageOperation : public Operation 342{ 343public: 344 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result) 345 : m_tid(tid), m_message(message), m_result(result) { } 346 347 void Execute(ProcessMonitor *monitor); 348 349private: 350 lldb::tid_t m_tid; 351 unsigned long *m_message; 352 bool &m_result; 353}; 354 355void 356EventMessageOperation::Execute(ProcessMonitor *monitor) 357{ 358 if (ptrace(PTRACE_GETEVENTMSG, m_tid, NULL, m_message)) 359 m_result = false; 360 else 361 m_result = true; 362} 363 364//------------------------------------------------------------------------------ 365/// @class KillOperation 366/// @brief Implements ProcessMonitor::BringProcessIntoLimbo. 367class KillOperation : public Operation 368{ 369public: 370 KillOperation(bool &result) : m_result(result) { } 371 372 void Execute(ProcessMonitor *monitor); 373 374private: 375 bool &m_result; 376}; 377 378void 379KillOperation::Execute(ProcessMonitor *monitor) 380{ 381 lldb::pid_t pid = monitor->GetPID(); 382 383 if (ptrace(PTRACE_KILL, pid, NULL, NULL)) 384 m_result = false; 385 else 386 m_result = true; 387 388#if 0 389 // First, stop the inferior process. 390 if (kill(pid, SIGSTOP)) 391 { 392 m_result = false; 393 return; 394 } 395 396 // Clear any ptrace options. When PTRACE_O_TRACEEXIT is set, a plain 397 // PTRACE_KILL (or any termination signal) will not truely terminate the 398 // inferior process. Instead, the process is left in a state of "limbo" 399 // allowing us to interrogate its state. However in this case we really do 400 // want the process gone. 401 if (ptrace(PTRACE_SETOPTIONS, pid, NULL, 0UL)) 402 { 403 m_result = false; 404 return; 405 } 406 407 // Kill it. 408 if (ptrace(PTRACE_KILL, pid, NULL, NULL)) 409 m_result = false; 410 else 411 m_result = true; 412#endif 413} 414 415ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor, 416 lldb_private::Module *module, 417 char const **argv, 418 char const **envp, 419 const char *stdin_path, 420 const char *stdout_path, 421 const char *stderr_path) 422 : m_monitor(monitor), 423 m_module(module), 424 m_argv(argv), 425 m_envp(envp), 426 m_stdin_path(stdin_path), 427 m_stdout_path(stdout_path), 428 m_stderr_path(stderr_path) 429{ 430 sem_init(&m_semaphore, 0, 0); 431} 432 433ProcessMonitor::LaunchArgs::~LaunchArgs() 434{ 435 sem_destroy(&m_semaphore); 436} 437 438//------------------------------------------------------------------------------ 439/// The basic design of the ProcessMonitor is built around two threads. 440/// 441/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking 442/// for changes in the debugee state. When a change is detected a 443/// ProcessMessage is sent to the associated ProcessLinux instance. This thread 444/// "drives" state changes in the debugger. 445/// 446/// The second thread (@see OperationThread) is responsible for two things 1) 447/// launching or attaching to the inferior process, and then 2) servicing 448/// operations such as register reads/writes, stepping, etc. See the comments 449/// on the Operation class for more info as to why this is needed. 450ProcessMonitor::ProcessMonitor(ProcessLinux *process, 451 Module *module, 452 const char *argv[], 453 const char *envp[], 454 const char *stdin_path, 455 const char *stdout_path, 456 const char *stderr_path, 457 lldb_private::Error &error) 458 : m_process(process), 459 m_operation_thread(LLDB_INVALID_HOST_THREAD), 460 m_pid(LLDB_INVALID_PROCESS_ID), 461 m_terminal_fd(-1), 462 m_monitor_thread(LLDB_INVALID_HOST_THREAD), 463 m_client_fd(-1), 464 m_server_fd(-1) 465{ 466 LaunchArgs args(this, module, argv, envp, 467 stdin_path, stdout_path, stderr_path); 468 469 // Server/client descriptors. 470 if (!EnableIPC()) 471 { 472 error.SetErrorToGenericError(); 473 error.SetErrorString("Monitor failed to initialize."); 474 } 475 476 StartOperationThread(&args, error); 477 if (!error.Success()) 478 return; 479 480WAIT_AGAIN: 481 // Wait for the operation thread to initialize. 482 if (sem_wait(&args.m_semaphore)) 483 { 484 if (errno == EINTR) 485 goto WAIT_AGAIN; 486 else 487 { 488 error.SetErrorToErrno(); 489 return; 490 } 491 } 492 493 // Check that the launch was a success. 494 if (!args.m_error.Success()) 495 { 496 StopOperationThread(); 497 error = args.m_error; 498 return; 499 } 500 501 // Finally, start monitoring the child process for change in state. 502 if (!(m_monitor_thread = Host::StartMonitoringChildProcess( 503 ProcessMonitor::MonitorCallback, this, GetPID(), true))) 504 { 505 error.SetErrorToGenericError(); 506 error.SetErrorString("Process launch failed."); 507 return; 508 } 509} 510 511ProcessMonitor::~ProcessMonitor() 512{ 513 StopMonitoringChildProcess(); 514 StopOperationThread(); 515 516 close(m_terminal_fd); 517 close(m_client_fd); 518 close(m_server_fd); 519} 520 521//------------------------------------------------------------------------------ 522// Thread setup and tear down. 523void 524ProcessMonitor::StartOperationThread(LaunchArgs *args, Error &error) 525{ 526 static const char *g_thread_name = "lldb.process.linux.operation"; 527 528 if (m_operation_thread != LLDB_INVALID_HOST_THREAD) 529 return; 530 531 m_operation_thread = 532 Host::ThreadCreate(g_thread_name, OperationThread, args, &error); 533} 534 535void 536ProcessMonitor::StopOperationThread() 537{ 538 lldb::thread_result_t result; 539 540 if (m_operation_thread == LLDB_INVALID_HOST_THREAD) 541 return; 542 543 Host::ThreadCancel(m_operation_thread, NULL); 544 Host::ThreadJoin(m_operation_thread, &result, NULL); 545} 546 547void * 548ProcessMonitor::OperationThread(void *arg) 549{ 550 LaunchArgs *args = static_cast<LaunchArgs*>(arg); 551 552 if (!Launch(args)) 553 return NULL; 554 555 ServeOperation(args->m_monitor); 556 return NULL; 557} 558 559bool 560ProcessMonitor::Launch(LaunchArgs *args) 561{ 562 ProcessMonitor *monitor = args->m_monitor; 563 ProcessLinux &process = monitor->GetProcess(); 564 const char **argv = args->m_argv; 565 const char **envp = args->m_envp; 566 const char *stdin_path = args->m_stdin_path; 567 const char *stdout_path = args->m_stdout_path; 568 const char *stderr_path = args->m_stderr_path; 569 570 lldb_utility::PseudoTerminal terminal; 571 const size_t err_len = 1024; 572 char err_str[err_len]; 573 lldb::pid_t pid; 574 575 lldb::ThreadSP inferior; 576 577 // Pseudo terminal setup. 578 if (!terminal.OpenFirstAvailableMaster(O_RDWR | O_NOCTTY, err_str, err_len)) 579 { 580 args->m_error.SetErrorToGenericError(); 581 args->m_error.SetErrorString("Could not open controlling TTY."); 582 goto FINISH; 583 } 584 585 if ((pid = terminal.Fork(err_str, err_len)) < 0) 586 { 587 args->m_error.SetErrorToGenericError(); 588 args->m_error.SetErrorString("Process fork failed."); 589 goto FINISH; 590 } 591 592 // Child process. 593 if (pid == 0) 594 { 595 // Trace this process. 596 ptrace(PTRACE_TRACEME, 0, NULL, NULL); 597 598 // Do not inherit setgid powers. 599 setgid(getgid()); 600 601 // Let us have our own process group. 602 setpgid(0, 0); 603 604 // Dup file descriptors if needed. 605 // 606 // FIXME: If two or more of the paths are the same we needlessly open 607 // the same file multiple times. 608 if (stdin_path != NULL && stdin_path[0]) 609 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY | O_CREAT)) 610 exit(1); 611 612 if (stdout_path != NULL && stdout_path[0]) 613 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT)) 614 exit(1); 615 616 if (stderr_path != NULL && stderr_path[0]) 617 if (!DupDescriptor(stderr_path, STDOUT_FILENO, O_WRONLY | O_CREAT)) 618 exit(1); 619 620 // Execute. We should never return. 621 execve(argv[0], 622 const_cast<char *const *>(argv), 623 const_cast<char *const *>(envp)); 624 exit(-1); 625 } 626 627 // Wait for the child process to to trap on its call to execve. 628 int status; 629 if ((status = waitpid(pid, NULL, 0)) < 0) 630 { 631 // execve likely failed for some reason. 632 args->m_error.SetErrorToErrno(); 633 goto FINISH; 634 } 635 assert(status == pid && "Could not sync with inferior process."); 636 637 // Have the child raise an event on exit. This is used to keep the child in 638 // limbo until it is destroyed. 639 if (ptrace(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0) 640 { 641 args->m_error.SetErrorToErrno(); 642 goto FINISH; 643 } 644 645 // Release the master terminal descriptor and pass it off to the 646 // ProcessMonitor instance. Similarly stash the inferior pid. 647 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); 648 monitor->m_pid = pid; 649 650 // Update the process thread list with this new thread and mark it as 651 // current. 652 inferior.reset(new LinuxThread(process, pid)); 653 process.GetThreadList().AddThread(inferior); 654 process.GetThreadList().SetSelectedThreadByID(pid); 655 656 // Let our process instance know the thread has stopped. 657 process.SendMessage(ProcessMessage::Trace(pid)); 658 659FINISH: 660 // Sync with our parent thread now that the launch operation is complete. 661 sem_post(&args->m_semaphore); 662 return args->m_error.Success(); 663} 664 665bool 666ProcessMonitor::EnableIPC() 667{ 668 int fd[2]; 669 670 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) 671 return false; 672 673 m_client_fd = fd[0]; 674 m_server_fd = fd[1]; 675 return true; 676} 677 678bool 679ProcessMonitor::MonitorCallback(void *callback_baton, 680 lldb::pid_t pid, 681 int signal, 682 int status) 683{ 684 ProcessMessage message; 685 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton); 686 ProcessLinux *process = monitor->m_process; 687 688 switch (signal) 689 { 690 case 0: 691 // No signal. The child has exited normally. 692 message = ProcessMessage::Exit(pid, status); 693 break; 694 695 case SIGTRAP: 696 // Specially handle SIGTRAP and form the appropriate message. 697 message = MonitorSIGTRAP(monitor, pid); 698 break; 699 700 default: 701 // For all other signals simply notify the process instance. Note that 702 // the process exit status is set when the signal resulted in 703 // termination. 704 // 705 // FIXME: We need a specialized message to inform the process instance 706 // about "crashes". 707 if (status) 708 message = ProcessMessage::Exit(pid, status); 709 else 710 message = ProcessMessage::Signal(pid, signal); 711 } 712 713 process->SendMessage(message); 714 bool stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage; 715 return stop_monitoring; 716} 717 718ProcessMessage 719ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor, lldb::pid_t pid) 720{ 721 siginfo_t info; 722 ProcessMessage message; 723 bool status; 724 725 status = monitor->GetSignalInfo(pid, &info); 726 assert(status && "GetSignalInfo failed!"); 727 728 assert(info.si_signo == SIGTRAP && "Unexpected child signal!"); 729 730 switch (info.si_code) 731 { 732 default: 733 assert(false && "Unexpected SIGTRAP code!"); 734 break; 735 736 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): 737 { 738 // The inferior process is about to exit. Maintain the process in a 739 // state of "limbo" until we are explicitly commanded to detach, 740 // destroy, resume, etc. 741 unsigned long data = 0; 742 if (!monitor->GetEventMessage(pid, &data)) 743 data = -1; 744 message = ProcessMessage::Exit(pid, (data >> 8)); 745 break; 746 } 747 748 case 0: 749 case TRAP_TRACE: 750 message = ProcessMessage::Trace(pid); 751 break; 752 753 case SI_KERNEL: 754 case TRAP_BRKPT: 755 message = ProcessMessage::Break(pid); 756 break; 757 } 758 759 return message; 760} 761 762void 763ProcessMonitor::ServeOperation(ProcessMonitor *monitor) 764{ 765 int status; 766 pollfd fdset; 767 768 fdset.fd = monitor->m_server_fd; 769 fdset.events = POLLIN | POLLPRI; 770 fdset.revents = 0; 771 772 for (;;) 773 { 774 if ((status = poll(&fdset, 1, -1)) < 0) 775 { 776 switch (errno) 777 { 778 default: 779 assert(false && "Unexpected poll() failure!"); 780 continue; 781 782 case EINTR: continue; // Just poll again. 783 case EBADF: return; // Connection terminated. 784 } 785 } 786 787 assert(status == 1 && "Too many descriptors!"); 788 789 if (fdset.revents & POLLIN) 790 { 791 Operation *op = NULL; 792 793 READ_AGAIN: 794 if ((status = read(fdset.fd, &op, sizeof(op))) < 0) 795 { 796 // There is only one acceptable failure. 797 assert(errno == EINTR); 798 goto READ_AGAIN; 799 } 800 801 assert(status == sizeof(op)); 802 op->Execute(monitor); 803 write(fdset.fd, &op, sizeof(op)); 804 } 805 } 806} 807 808void 809ProcessMonitor::DoOperation(Operation *op) 810{ 811 int status; 812 Operation *ack = NULL; 813 Mutex::Locker lock(m_server_mutex); 814 815 // FIXME: Do proper error checking here. 816 write(m_client_fd, &op, sizeof(op)); 817 818READ_AGAIN: 819 if ((status = read(m_client_fd, &ack, sizeof(ack))) < 0) 820 { 821 // If interrupted by a signal handler try again. Otherwise the monitor 822 // thread probably died and we have a stale file descriptor -- abort the 823 // operation. 824 if (errno == EINTR) 825 goto READ_AGAIN; 826 return; 827 } 828 829 assert(status == sizeof(ack)); 830 assert(ack == op && "Invalid monitor thread response!"); 831} 832 833size_t 834ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, 835 Error &error) 836{ 837 size_t result; 838 ReadOperation op(vm_addr, buf, size, error, result); 839 DoOperation(&op); 840 return result; 841} 842 843size_t 844ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, 845 lldb_private::Error &error) 846{ 847 size_t result; 848 WriteOperation op(vm_addr, buf, size, error, result); 849 DoOperation(&op); 850 return result; 851} 852 853bool 854ProcessMonitor::ReadRegisterValue(unsigned offset, Scalar &value) 855{ 856 bool result; 857 ReadRegOperation op(offset, value, result); 858 DoOperation(&op); 859 return result; 860} 861 862bool 863ProcessMonitor::WriteRegisterValue(unsigned offset, const Scalar &value) 864{ 865 bool result; 866 WriteRegOperation op(offset, value, result); 867 DoOperation(&op); 868 return result; 869} 870 871bool 872ProcessMonitor::Resume(lldb::tid_t tid) 873{ 874 bool result; 875 ResumeOperation op(tid, result); 876 DoOperation(&op); 877 return result; 878} 879 880bool 881ProcessMonitor::SingleStep(lldb::tid_t tid) 882{ 883 bool result; 884 SingleStepOperation op(tid, result); 885 DoOperation(&op); 886 return result; 887} 888 889bool 890ProcessMonitor::BringProcessIntoLimbo() 891{ 892 bool result; 893 KillOperation op(result); 894 DoOperation(&op); 895 return result; 896} 897 898bool 899ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo) 900{ 901 bool result; 902 SiginfoOperation op(tid, siginfo, result); 903 DoOperation(&op); 904 return result; 905} 906 907bool 908ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message) 909{ 910 bool result; 911 EventMessageOperation op(tid, message, result); 912 DoOperation(&op); 913 return result; 914} 915 916bool 917ProcessMonitor::DupDescriptor(const char *path, int fd, int flags) 918{ 919 int target_fd = open(path, flags); 920 921 if (target_fd == -1) 922 return false; 923 924 return (dup2(fd, target_fd) == -1) ? false : true; 925} 926 927void 928ProcessMonitor::StopMonitoringChildProcess() 929{ 930 lldb::thread_result_t thread_result; 931 932 if (m_monitor_thread != LLDB_INVALID_HOST_THREAD) 933 { 934 Host::ThreadCancel(m_monitor_thread, NULL); 935 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL); 936 m_monitor_thread = LLDB_INVALID_HOST_THREAD; 937 } 938} 939