2014-10-15 05:55:08 +08:00
|
|
|
//===-- HostProcessPosix.cpp ------------------------------------*- C++ -*-===//
|
2014-08-28 04:15:30 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-10-15 05:55:08 +08:00
|
|
|
#include "lldb/Host/Host.h"
|
2014-08-28 04:15:30 +08:00
|
|
|
#include "lldb/Host/FileSystem.h"
|
2016-09-07 04:57:50 +08:00
|
|
|
#include "lldb/Host/posix/HostProcessPosix.h"
|
2014-08-28 04:15:30 +08:00
|
|
|
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
|
2017-07-18 21:14:01 +08:00
|
|
|
#include <csignal>
|
2014-08-28 04:15:30 +08:00
|
|
|
#include <limits.h>
|
2017-07-18 21:14:01 +08:00
|
|
|
#include <unistd.h>
|
2014-08-28 04:15:30 +08:00
|
|
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
namespace {
|
|
|
|
const int kInvalidPosixProcess = 0;
|
2014-09-12 06:22:16 +08:00
|
|
|
}
|
2014-08-28 04:44:26 +08:00
|
|
|
|
2014-08-28 04:15:30 +08:00
|
|
|
HostProcessPosix::HostProcessPosix()
|
2016-09-07 04:57:50 +08:00
|
|
|
: HostNativeProcessBase(kInvalidPosixProcess) {}
|
2014-08-28 04:15:30 +08:00
|
|
|
|
2014-09-12 06:42:58 +08:00
|
|
|
HostProcessPosix::HostProcessPosix(lldb::process_t process)
|
2016-09-07 04:57:50 +08:00
|
|
|
: HostNativeProcessBase(process) {}
|
2014-09-12 06:42:58 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
HostProcessPosix::~HostProcessPosix() {}
|
2014-08-28 04:15:30 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status HostProcessPosix::Signal(int signo) const {
|
2016-09-07 04:57:50 +08:00
|
|
|
if (m_process == kInvalidPosixProcess) {
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2016-09-07 04:57:50 +08:00
|
|
|
error.SetErrorString("HostProcessPosix refers to an invalid process");
|
|
|
|
return error;
|
|
|
|
}
|
2014-08-28 04:15:30 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return HostProcessPosix::Signal(m_process, signo);
|
2014-08-28 04:15:30 +08:00
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status HostProcessPosix::Signal(lldb::process_t process, int signo) {
|
|
|
|
Status error;
|
2014-08-28 04:15:30 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (-1 == ::kill(process, signo))
|
|
|
|
error.SetErrorToErrno();
|
2014-08-28 04:15:30 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return error;
|
2014-08-28 04:15:30 +08:00
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status HostProcessPosix::Terminate() { return Signal(SIGKILL); }
|
2014-09-12 06:22:16 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status HostProcessPosix::GetMainModule(FileSpec &file_spec) const {
|
|
|
|
Status error;
|
2014-08-28 04:15:30 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Use special code here because proc/[pid]/exe is a symbolic link.
|
|
|
|
char link_path[PATH_MAX];
|
|
|
|
if (snprintf(link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", m_process) != 1) {
|
|
|
|
error.SetErrorString("Unable to build /proc/<pid>/exe string");
|
2014-08-28 04:15:30 +08:00
|
|
|
return error;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-08-28 04:15:30 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
error = FileSystem::Readlink(FileSpec{link_path, false}, file_spec);
|
|
|
|
if (!error.Success())
|
|
|
|
return error;
|
|
|
|
|
|
|
|
// If the binary has been deleted, the link name has " (deleted)" appended.
|
|
|
|
// Remove if there.
|
|
|
|
if (file_spec.GetFilename().GetStringRef().endswith(" (deleted)")) {
|
|
|
|
const char *filename = file_spec.GetFilename().GetCString();
|
|
|
|
static const size_t deleted_len = strlen(" (deleted)");
|
|
|
|
const size_t len = file_spec.GetFilename().GetLength();
|
|
|
|
file_spec.GetFilename().SetCStringWithLength(filename, len - deleted_len);
|
|
|
|
}
|
|
|
|
return error;
|
2014-08-28 04:15:30 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
lldb::pid_t HostProcessPosix::GetProcessId() const { return m_process; }
|
|
|
|
|
|
|
|
bool HostProcessPosix::IsRunning() const {
|
|
|
|
if (m_process == kInvalidPosixProcess)
|
|
|
|
return false;
|
2014-10-01 00:56:40 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Send this process the null signal. If it succeeds the process is running.
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error = Signal(0);
|
2016-09-07 04:57:50 +08:00
|
|
|
return error.Success();
|
2014-08-28 04:15:30 +08:00
|
|
|
}
|
2014-10-15 05:55:08 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
HostThread HostProcessPosix::StartMonitoring(
|
|
|
|
const Host::MonitorChildProcessCallback &callback, bool monitor_signals) {
|
|
|
|
return Host::StartMonitoringChildProcess(callback, m_process,
|
|
|
|
monitor_signals);
|
2014-10-15 05:55:08 +08:00
|
|
|
}
|