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/posix/HostProcessPosix.h"
|
|
|
|
#include "lldb/Host/FileSystem.h"
|
|
|
|
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2014-09-12 06:22:16 +08:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
const int kInvalidPosixProcess = 0;
|
|
|
|
}
|
2014-08-28 04:44:26 +08:00
|
|
|
|
2014-08-28 04:15:30 +08:00
|
|
|
HostProcessPosix::HostProcessPosix()
|
2014-09-12 06:22:16 +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)
|
|
|
|
: HostNativeProcessBase(process)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-28 04:15:30 +08:00
|
|
|
HostProcessPosix::~HostProcessPosix()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Error HostProcessPosix::Signal(int signo) const
|
|
|
|
{
|
2014-09-12 06:22:16 +08:00
|
|
|
if (m_process == kInvalidPosixProcess)
|
2014-08-28 04:15:30 +08:00
|
|
|
{
|
|
|
|
Error error;
|
|
|
|
error.SetErrorString("HostProcessPosix refers to an invalid process");
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2014-09-12 06:22:16 +08:00
|
|
|
return HostProcessPosix::Signal(m_process, signo);
|
2014-08-28 04:15:30 +08:00
|
|
|
}
|
|
|
|
|
2014-09-12 06:22:16 +08:00
|
|
|
Error HostProcessPosix::Signal(lldb::process_t process, int signo)
|
2014-08-28 04:15:30 +08:00
|
|
|
{
|
|
|
|
Error error;
|
|
|
|
|
2014-09-12 06:22:16 +08:00
|
|
|
if (-1 == ::kill(process, signo))
|
2014-08-28 04:15:30 +08:00
|
|
|
error.SetErrorToErrno();
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2014-09-12 06:22:16 +08:00
|
|
|
Error HostProcessPosix::Terminate()
|
|
|
|
{
|
|
|
|
return Signal(SIGKILL);
|
|
|
|
}
|
|
|
|
|
2014-08-28 04:15:30 +08:00
|
|
|
Error HostProcessPosix::GetMainModule(FileSpec &file_spec) const
|
|
|
|
{
|
|
|
|
Error error;
|
|
|
|
|
|
|
|
// Use special code here because proc/[pid]/exe is a symbolic link.
|
|
|
|
char link_path[PATH_MAX];
|
2015-05-30 03:52:29 +08:00
|
|
|
if (snprintf(link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", m_process) != 1)
|
2014-08-28 04:15:30 +08:00
|
|
|
{
|
|
|
|
error.SetErrorString("Unable to build /proc/<pid>/exe string");
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2015-05-30 03:52:29 +08:00
|
|
|
error = FileSystem::Readlink(FileSpec{link_path, false}, file_spec);
|
2014-08-28 04:15:30 +08:00
|
|
|
if (!error.Success())
|
|
|
|
return error;
|
|
|
|
|
|
|
|
// If the binary has been deleted, the link name has " (deleted)" appended.
|
|
|
|
// Remove if there.
|
2015-05-30 03:52:29 +08:00
|
|
|
if (file_spec.GetFilename().GetStringRef().endswith(" (deleted)"))
|
2014-08-28 04:15:30 +08:00
|
|
|
{
|
2015-05-30 03:52:29 +08:00
|
|
|
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);
|
2014-08-28 04:15:30 +08:00
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
lldb::pid_t HostProcessPosix::GetProcessId() const
|
|
|
|
{
|
2014-09-12 06:22:16 +08:00
|
|
|
return m_process;
|
2014-08-28 04:15:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HostProcessPosix::IsRunning() const
|
|
|
|
{
|
2014-10-01 00:56:40 +08:00
|
|
|
if (m_process == kInvalidPosixProcess)
|
|
|
|
return false;
|
|
|
|
|
2014-08-28 04:15:30 +08:00
|
|
|
// Send this process the null signal. If it succeeds the process is running.
|
|
|
|
Error error = Signal(0);
|
|
|
|
return error.Success();
|
|
|
|
}
|
2014-10-15 05:55:08 +08:00
|
|
|
|
|
|
|
HostThread
|
|
|
|
HostProcessPosix::StartMonitoring(HostProcess::MonitorCallback callback, void *callback_baton, bool monitor_signals)
|
|
|
|
{
|
|
|
|
return Host::StartMonitoringChildProcess(callback, callback_baton, m_process, monitor_signals);
|
|
|
|
}
|