1 //===-- HostProcessPosix.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/Host.h"
11 #include "lldb/Host/posix/HostProcessPosix.h"
12 #include "lldb/Host/FileSystem.h"
14 #include "llvm/ADT/STLExtras.h"
18 using namespace lldb_private;
22 const int kInvalidPosixProcess = 0;
25 HostProcessPosix::HostProcessPosix()
26 : HostNativeProcessBase(kInvalidPosixProcess)
30 HostProcessPosix::HostProcessPosix(lldb::process_t process)
31 : HostNativeProcessBase(process)
35 HostProcessPosix::~HostProcessPosix()
39 Error HostProcessPosix::Signal(int signo) const
41 if (m_process == kInvalidPosixProcess)
44 error.SetErrorString("HostProcessPosix refers to an invalid process");
48 return HostProcessPosix::Signal(m_process, signo);
51 Error HostProcessPosix::Signal(lldb::process_t process, int signo)
55 if (-1 == ::kill(process, signo))
56 error.SetErrorToErrno();
61 Error HostProcessPosix::Terminate()
63 return Signal(SIGKILL);
66 Error HostProcessPosix::GetMainModule(FileSpec &file_spec) const
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)
75 error.SetErrorString("Unable to build /proc/<pid>/exe string");
79 error = FileSystem::Readlink(link_path, exe_path, llvm::array_lengthof(exe_path));
83 const ssize_t len = strlen(exe_path);
84 // If the binary has been deleted, the link name has " (deleted)" appended.
86 static const ssize_t deleted_len = strlen(" (deleted)");
87 if (len > deleted_len &&
88 !strcmp(exe_path + len - deleted_len, " (deleted)"))
90 exe_path[len - deleted_len] = 0;
93 file_spec.SetFile(exe_path, false);
97 lldb::pid_t HostProcessPosix::GetProcessId() const
102 bool HostProcessPosix::IsRunning() const
104 if (m_process == kInvalidPosixProcess)
107 // Send this process the null signal. If it succeeds the process is running.
108 Error error = Signal(0);
109 return error.Success();
113 HostProcessPosix::StartMonitoring(HostProcess::MonitorCallback callback, void *callback_baton, bool monitor_signals)
115 return Host::StartMonitoringChildProcess(callback, callback_baton, m_process, monitor_signals);