xref: /llvm-project/lldb/source/Host/posix/HostProcessPosix.cpp (revision 0642cd768b80665585c8500bed2933a3b99123dc)
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     return Status::FromErrorString(
34         "HostProcessPosix refers to an invalid process");
35   }
36 
37   return HostProcessPosix::Signal(m_process, signo);
38 }
39 
40 Status HostProcessPosix::Signal(lldb::process_t process, int signo) {
41   Status error;
42 
43   if (-1 == ::kill(process, signo))
44     return Status::FromErrno();
45 
46   return error;
47 }
48 
49 Status HostProcessPosix::Terminate() { return Signal(SIGKILL); }
50 
51 lldb::pid_t HostProcessPosix::GetProcessId() const { return m_process; }
52 
53 bool HostProcessPosix::IsRunning() const {
54   if (m_process == kInvalidPosixProcess)
55     return false;
56 
57   // Send this process the null signal.  If it succeeds the process is running.
58   Status error = Signal(0);
59   return error.Success();
60 }
61 
62 llvm::Expected<HostThread> HostProcessPosix::StartMonitoring(
63     const Host::MonitorChildProcessCallback &callback) {
64   return Host::StartMonitoringChildProcess(callback, m_process);
65 }
66