1 //===-- HostProcessPosix.cpp ------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Host/Host.h"
10 #include "lldb/Host/FileSystem.h"
11 #include "lldb/Host/posix/HostProcessPosix.h"
13 #include "llvm/ADT/STLExtras.h"
19 using namespace lldb_private;
22 const int kInvalidPosixProcess = 0;
25 HostProcessPosix::HostProcessPosix()
26 : HostNativeProcessBase(kInvalidPosixProcess) {}
28 HostProcessPosix::HostProcessPosix(lldb::process_t process)
29 : HostNativeProcessBase(process) {}
31 HostProcessPosix::~HostProcessPosix() {}
33 Status HostProcessPosix::Signal(int signo) const {
34 if (m_process == kInvalidPosixProcess) {
36 error.SetErrorString("HostProcessPosix refers to an invalid process");
40 return HostProcessPosix::Signal(m_process, signo);
43 Status HostProcessPosix::Signal(lldb::process_t process, int signo) {
46 if (-1 == ::kill(process, signo))
47 error.SetErrorToErrno();
52 Status HostProcessPosix::Terminate() { return Signal(SIGKILL); }
54 Status HostProcessPosix::GetMainModule(FileSpec &file_spec) const {
57 // Use special code here because proc/[pid]/exe is a symbolic link.
58 char link_path[PATH_MAX];
59 if (snprintf(link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", m_process) != 1) {
60 error.SetErrorString("Unable to build /proc/<pid>/exe string");
64 error = FileSystem::Instance().Readlink(FileSpec(link_path), file_spec);
68 // If the binary has been deleted, the link name has " (deleted)" appended.
70 if (file_spec.GetFilename().GetStringRef().endswith(" (deleted)")) {
71 const char *filename = file_spec.GetFilename().GetCString();
72 static const size_t deleted_len = strlen(" (deleted)");
73 const size_t len = file_spec.GetFilename().GetLength();
74 file_spec.GetFilename().SetCStringWithLength(filename, len - deleted_len);
79 lldb::pid_t HostProcessPosix::GetProcessId() const { return m_process; }
81 bool HostProcessPosix::IsRunning() const {
82 if (m_process == kInvalidPosixProcess)
85 // Send this process the null signal. If it succeeds the process is running.
86 Status error = Signal(0);
87 return error.Success();
90 llvm::Expected<HostThread> HostProcessPosix::StartMonitoring(
91 const Host::MonitorChildProcessCallback &callback, bool monitor_signals) {
92 return Host::StartMonitoringChildProcess(callback, m_process,