LLDB  mainline
HostProcessPosix.cpp
Go to the documentation of this file.
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"
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 
25 
27  : HostNativeProcessBase(process) {}
28 
30 
31 Status HostProcessPosix::Signal(int signo) const {
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 
42  Status error;
43 
44  if (-1 == ::kill(process, signo))
45  error.SetErrorToErrno();
46 
47  return error;
48 }
49 
51 
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 
78 
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) {
91  monitor_signals);
92 }
kInvalidPosixProcess
static const int kInvalidPosixProcess
Definition: HostProcessPosix.cpp:21
FileSystem.h
lldb_private::Host::MonitorChildProcessCallback
std::function< bool(lldb::pid_t pid, bool exited, int signal, int status)> MonitorChildProcessCallback
Definition: Host.h:68
lldb_private::HostProcessPosix::HostProcessPosix
HostProcessPosix()
Definition: HostProcessPosix.cpp:23
Host.h
lldb_private::HostProcessPosix::GetMainModule
Status GetMainModule(FileSpec &file_spec) const override
Definition: HostProcessPosix.cpp:52
lldb_private::HostProcessPosix::IsRunning
bool IsRunning() const override
Definition: HostProcessPosix.cpp:79
lldb_private::FileSpec
Definition: FileSpec.h:56
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
HostProcessPosix.h
lldb_private::Host::StartMonitoringChildProcess
static llvm::Expected< HostThread > StartMonitoringChildProcess(const MonitorChildProcessCallback &callback, lldb::pid_t pid, bool monitor_signals)
Start monitoring a child process.
Definition: common/Host.cpp:103
SIGKILL
#define SIGKILL
Definition: windows/PosixApi.h:31
lldb_private::ConstString::GetStringRef
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:202
lldb_private::HostProcessPosix::GetProcessId
lldb::pid_t GetProcessId() const override
Definition: HostProcessPosix.cpp:77
lldb_private::ConstString::SetCStringWithLength
void SetCStringWithLength(const char *cstr, size_t cstr_len)
Set the C string value with length.
Definition: ConstString.cpp:321
lldb_private::ConstString::GetLength
size_t GetLength() const
Get the length in bytes of string value.
Definition: ConstString.cpp:238
lldb_private::FileSystem::Readlink
Status Readlink(const FileSpec &src, FileSpec &dst)
Definition: FileSystemPosix.cpp:43
lldb_private::Status
Definition: Status.h:44
lldb_private::HostProcessPosix::~HostProcessPosix
~HostProcessPosix() override
lldb::pid_t
uint64_t pid_t
Definition: lldb-types.h:85
lldb::process_t
uint64_t process_t
Definition: lldb-types.h:57
lldb_private::ConstString::GetCString
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
lldb_private::FileSystem::Instance
static FileSystem & Instance()
Definition: common/FileSystem.cpp:45
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::HostProcessPosix::Signal
virtual Status Signal(int signo) const
Definition: HostProcessPosix.cpp:31
PATH_MAX
#define PATH_MAX
Definition: windows/PosixApi.h:25
lldb_private::HostProcessPosix::Terminate
Status Terminate() override
Definition: HostProcessPosix.cpp:50
lldb_private::HostProcessPosix::StartMonitoring
llvm::Expected< HostThread > StartMonitoring(const Host::MonitorChildProcessCallback &callback, bool monitor_signals) override
Definition: HostProcessPosix.cpp:88
lldb_private::FileSpec::GetFilename
ConstString & GetFilename()
Filename string get accessor.
Definition: FileSpec.cpp:341
lldb_private::HostNativeProcessBase
Definition: HostNativeProcessBase.h:21
lldb_private::HostNativeProcessBase::m_process
lldb::process_t m_process
Definition: HostNativeProcessBase.h:45