]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/source/Host/posix/HostProcessPosix.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / source / Host / posix / HostProcessPosix.cpp
1 //===-- HostProcessPosix.cpp ------------------------------------*- C++ -*-===//
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 <csignal>
16 #include <limits.h>
17 #include <unistd.h>
18
19 using namespace lldb_private;
20
21 namespace {
22 const int kInvalidPosixProcess = 0;
23 }
24
25 HostProcessPosix::HostProcessPosix()
26     : HostNativeProcessBase(kInvalidPosixProcess) {}
27
28 HostProcessPosix::HostProcessPosix(lldb::process_t process)
29     : HostNativeProcessBase(process) {}
30
31 HostProcessPosix::~HostProcessPosix() {}
32
33 Status HostProcessPosix::Signal(int signo) const {
34   if (m_process == kInvalidPosixProcess) {
35     Status error;
36     error.SetErrorString("HostProcessPosix refers to an invalid process");
37     return error;
38   }
39
40   return HostProcessPosix::Signal(m_process, signo);
41 }
42
43 Status HostProcessPosix::Signal(lldb::process_t process, int signo) {
44   Status error;
45
46   if (-1 == ::kill(process, signo))
47     error.SetErrorToErrno();
48
49   return error;
50 }
51
52 Status HostProcessPosix::Terminate() { return Signal(SIGKILL); }
53
54 Status HostProcessPosix::GetMainModule(FileSpec &file_spec) const {
55   Status error;
56
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");
61     return error;
62   }
63
64   error = FileSystem::Instance().Readlink(FileSpec(link_path), file_spec);
65   if (!error.Success())
66     return error;
67
68   // If the binary has been deleted, the link name has " (deleted)" appended.
69   // Remove if there.
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);
75   }
76   return error;
77 }
78
79 lldb::pid_t HostProcessPosix::GetProcessId() const { return m_process; }
80
81 bool HostProcessPosix::IsRunning() const {
82   if (m_process == kInvalidPosixProcess)
83     return false;
84
85   // Send this process the null signal.  If it succeeds the process is running.
86   Status error = Signal(0);
87   return error.Success();
88 }
89
90 llvm::Expected<HostThread> HostProcessPosix::StartMonitoring(
91     const Host::MonitorChildProcessCallback &callback, bool monitor_signals) {
92   return Host::StartMonitoringChildProcess(callback, m_process,
93                                            monitor_signals);
94 }