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 if (snprintf(link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", m_process) != 1)
74 error.SetErrorString("Unable to build /proc/<pid>/exe string");
78 error = FileSystem::Readlink(FileSpec{link_path, false}, file_spec);
82 // If the binary has been deleted, the link name has " (deleted)" appended.
84 if (file_spec.GetFilename().GetStringRef().endswith(" (deleted)"))
86 const char *filename = file_spec.GetFilename().GetCString();
87 static const size_t deleted_len = strlen(" (deleted)");
88 const size_t len = file_spec.GetFilename().GetLength();
89 file_spec.GetFilename().SetCStringWithLength(filename, len - deleted_len);
94 lldb::pid_t HostProcessPosix::GetProcessId() const
99 bool HostProcessPosix::IsRunning() const
101 if (m_process == kInvalidPosixProcess)
104 // Send this process the null signal. If it succeeds the process is running.
105 Error error = Signal(0);
106 return error.Success();
110 HostProcessPosix::StartMonitoring(const Host::MonitorChildProcessCallback &callback, bool monitor_signals)
112 return Host::StartMonitoringChildProcess(callback, m_process, monitor_signals);