1 //===-- HostProcessPosix.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 #include "lldb/Host/Host.h" 11 #include "lldb/Host/FileSystem.h" 12 #include "lldb/Host/posix/HostProcessPosix.h" 13 14 #include "llvm/ADT/STLExtras.h" 15 16 #include <csignal> 17 #include <limits.h> 18 #include <unistd.h> 19 20 using namespace lldb_private; 21 22 namespace { 23 const int kInvalidPosixProcess = 0; 24 } 25 26 HostProcessPosix::HostProcessPosix() 27 : HostNativeProcessBase(kInvalidPosixProcess) {} 28 29 HostProcessPosix::HostProcessPosix(lldb::process_t process) 30 : HostNativeProcessBase(process) {} 31 32 HostProcessPosix::~HostProcessPosix() {} 33 34 Status HostProcessPosix::Signal(int signo) const { 35 if (m_process == kInvalidPosixProcess) { 36 Status error; 37 error.SetErrorString("HostProcessPosix refers to an invalid process"); 38 return error; 39 } 40 41 return HostProcessPosix::Signal(m_process, signo); 42 } 43 44 Status HostProcessPosix::Signal(lldb::process_t process, int signo) { 45 Status error; 46 47 if (-1 == ::kill(process, signo)) 48 error.SetErrorToErrno(); 49 50 return error; 51 } 52 53 Status HostProcessPosix::Terminate() { return Signal(SIGKILL); } 54 55 Status HostProcessPosix::GetMainModule(FileSpec &file_spec) const { 56 Status error; 57 58 // Use special code here because proc/[pid]/exe is a symbolic link. 59 char link_path[PATH_MAX]; 60 if (snprintf(link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", m_process) != 1) { 61 error.SetErrorString("Unable to build /proc/<pid>/exe string"); 62 return error; 63 } 64 65 error = FileSystem::Readlink(FileSpec{link_path, false}, file_spec); 66 if (!error.Success()) 67 return error; 68 69 // If the binary has been deleted, the link name has " (deleted)" appended. 70 // Remove if there. 71 if (file_spec.GetFilename().GetStringRef().endswith(" (deleted)")) { 72 const char *filename = file_spec.GetFilename().GetCString(); 73 static const size_t deleted_len = strlen(" (deleted)"); 74 const size_t len = file_spec.GetFilename().GetLength(); 75 file_spec.GetFilename().SetCStringWithLength(filename, len - deleted_len); 76 } 77 return error; 78 } 79 80 lldb::pid_t HostProcessPosix::GetProcessId() const { return m_process; } 81 82 bool HostProcessPosix::IsRunning() const { 83 if (m_process == kInvalidPosixProcess) 84 return false; 85 86 // Send this process the null signal. If it succeeds the process is running. 87 Status error = Signal(0); 88 return error.Success(); 89 } 90 91 HostThread HostProcessPosix::StartMonitoring( 92 const Host::MonitorChildProcessCallback &callback, bool monitor_signals) { 93 return Host::StartMonitoringChildProcess(callback, m_process, 94 monitor_signals); 95 } 96