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