xref: /freebsd-src/contrib/llvm-project/lldb/source/Host/posix/HostProcessPosix.cpp (revision c9ccf3a32da427475985b85d7df023ccfb138c27)
1 //===-- HostProcessPosix.cpp ----------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Host/Host.h"
10 #include "lldb/Host/FileSystem.h"
11 #include "lldb/Host/posix/HostProcessPosix.h"
12 
13 #include "llvm/ADT/STLExtras.h"
14 
15 #include <climits>
16 #include <csignal>
17 #include <unistd.h>
18 
19 using namespace lldb_private;
20 
21 static const int kInvalidPosixProcess = 0;
22 
23 HostProcessPosix::HostProcessPosix()
24     : HostNativeProcessBase(kInvalidPosixProcess) {}
25 
26 HostProcessPosix::HostProcessPosix(lldb::process_t process)
27     : HostNativeProcessBase(process) {}
28 
29 HostProcessPosix::~HostProcessPosix() = default;
30 
31 Status HostProcessPosix::Signal(int signo) const {
32   if (m_process == kInvalidPosixProcess) {
33     Status error;
34     error.SetErrorString("HostProcessPosix refers to an invalid process");
35     return error;
36   }
37 
38   return HostProcessPosix::Signal(m_process, signo);
39 }
40 
41 Status HostProcessPosix::Signal(lldb::process_t process, int signo) {
42   Status error;
43 
44   if (-1 == ::kill(process, signo))
45     error.SetErrorToErrno();
46 
47   return error;
48 }
49 
50 Status HostProcessPosix::Terminate() { return Signal(SIGKILL); }
51 
52 Status HostProcessPosix::GetMainModule(FileSpec &file_spec) const {
53   Status error;
54 
55   // Use special code here because proc/[pid]/exe is a symbolic link.
56   char link_path[PATH_MAX];
57   if (snprintf(link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", m_process) != 1) {
58     error.SetErrorString("Unable to build /proc/<pid>/exe string");
59     return error;
60   }
61 
62   error = FileSystem::Instance().Readlink(FileSpec(link_path), file_spec);
63   if (!error.Success())
64     return error;
65 
66   // If the binary has been deleted, the link name has " (deleted)" appended.
67   // Remove if there.
68   if (file_spec.GetFilename().GetStringRef().endswith(" (deleted)")) {
69     const char *filename = file_spec.GetFilename().GetCString();
70     static const size_t deleted_len = strlen(" (deleted)");
71     const size_t len = file_spec.GetFilename().GetLength();
72     file_spec.GetFilename().SetCStringWithLength(filename, len - deleted_len);
73   }
74   return error;
75 }
76 
77 lldb::pid_t HostProcessPosix::GetProcessId() const { return m_process; }
78 
79 bool HostProcessPosix::IsRunning() const {
80   if (m_process == kInvalidPosixProcess)
81     return false;
82 
83   // Send this process the null signal.  If it succeeds the process is running.
84   Status error = Signal(0);
85   return error.Success();
86 }
87 
88 llvm::Expected<HostThread> HostProcessPosix::StartMonitoring(
89     const Host::MonitorChildProcessCallback &callback, bool monitor_signals) {
90   return Host::StartMonitoringChildProcess(callback, m_process,
91                                            monitor_signals);
92 }
93