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