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