[Lldb-commits] [lldb] r216607 - Create a HostProcess class.
Zachary Turner
zturner at google.com
Wed Aug 27 13:15:30 PDT 2014
Author: zturner
Date: Wed Aug 27 15:15:30 2014
New Revision: 216607
URL: http://llvm.org/viewvc/llvm-project?rev=216607&view=rev
Log:
Create a HostProcess class.
This is a lightweight wrapper around a pid. It is intended to be
lightweight enough to serve as a replacement anywhere we currently
store a pid. It provides convenience methods and common process
operations.
This patch does not yet make use of HostProcess anywhere.
Added:
lldb/trunk/include/lldb/Host/HostProcess.h
lldb/trunk/include/lldb/Host/posix/HostProcessPosix.h
lldb/trunk/include/lldb/Host/windows/HostProcessWindows.h
lldb/trunk/source/Host/posix/HostProcessPosix.cpp
lldb/trunk/source/Host/windows/HostProcessWindows.cpp
Modified:
lldb/trunk/include/lldb/lldb-types.h
lldb/trunk/source/Host/CMakeLists.txt
Added: lldb/trunk/include/lldb/Host/HostProcess.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/HostProcess.h?rev=216607&view=auto
==============================================================================
--- lldb/trunk/include/lldb/Host/HostProcess.h (added)
+++ lldb/trunk/include/lldb/Host/HostProcess.h Wed Aug 27 15:15:30 2014
@@ -0,0 +1,44 @@
+//===-- HostProcess.h ------------------------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef lldb_Host_HostProcess_h_
+#define lldb_Host_HostProcess_h_
+
+//----------------------------------------------------------------------
+/// @class HostInfo HostInfo.h "lldb/Host/HostProcess.h"
+/// @brief A class that represents a running process on the host machine.
+///
+/// HostProcess allows querying and manipulation of processes running on the
+/// host machine. It is not intended to be represent a process which is
+/// being debugged, although the native debug engine of a platform may likely
+/// back inferior processes by a HostProcess.
+///
+/// HostProcess is implemented using static polymorphism so that on any given
+/// platform, an instance of HostProcess will always be able to bind statically
+/// to the concrete Process implementation for that platform. See HostInfo
+/// for more details.
+///
+//----------------------------------------------------------------------
+
+#if defined(_WIN32)
+#include "lldb/Host/windows/HostProcessWindows.h"
+#define HOST_PROCESS_TYPE HostProcessWindows
+#else
+#include "lldb/Host/posix/HostProcessPosix.h"
+#define HOST_PROCESS_TYPE HostProcessPosix
+#endif
+
+namespace lldb_private
+{
+ typedef HOST_PROCESS_TYPE HostProcess;
+}
+
+#undef HOST_PROCESS_TYPE
+
+#endif
Added: lldb/trunk/include/lldb/Host/posix/HostProcessPosix.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/posix/HostProcessPosix.h?rev=216607&view=auto
==============================================================================
--- lldb/trunk/include/lldb/Host/posix/HostProcessPosix.h (added)
+++ lldb/trunk/include/lldb/Host/posix/HostProcessPosix.h Wed Aug 27 15:15:30 2014
@@ -0,0 +1,46 @@
+//===-- HostProcessPosix.h --------------------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef lldb_Host_HostProcesPosix_h_
+#define lldb_Host_HostProcesPosix_h_
+
+#include "lldb/lldb-types.h"
+#include "lldb/Core/Error.h"
+#include "lldb/Target/ProcessLaunchInfo.h"
+
+namespace lldb_private
+{
+
+class FileSpec;
+
+class HostProcessPosix
+{
+ public:
+ static const lldb::pid_t kInvalidProcessId;
+
+ HostProcessPosix();
+ ~HostProcessPosix();
+
+ Error Signal(int signo) const;
+ static Error Signal(lldb::pid_t pid, int signo);
+
+ Error Create(lldb::pid_t pid);
+ Error Terminate(int signo);
+ Error GetMainModule(FileSpec &file_spec) const;
+
+ lldb::pid_t GetProcessId() const;
+ bool IsRunning() const;
+
+ private:
+
+ lldb::pid_t m_pid;
+};
+}
+
+#endif
Added: lldb/trunk/include/lldb/Host/windows/HostProcessWindows.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/windows/HostProcessWindows.h?rev=216607&view=auto
==============================================================================
--- lldb/trunk/include/lldb/Host/windows/HostProcessWindows.h (added)
+++ lldb/trunk/include/lldb/Host/windows/HostProcessWindows.h Wed Aug 27 15:15:30 2014
@@ -0,0 +1,44 @@
+//===-- HostProcessWindows.h ------------------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef lldb_Host_HostProcessWindows_h_
+#define lldb_Host_HostProcessWindows_h_
+
+#include "lldb/lldb-types.h"
+#include "lldb/Core/Error.h"
+#include "lldb/Target/ProcessLaunchInfo.h"
+
+namespace lldb_private
+{
+
+class FileSpec;
+
+class HostProcessWindows
+{
+ public:
+ HostProcessWindows();
+ ~HostProcessWindows();
+
+ Error Create(lldb::pid_t pid);
+ Error Create(lldb::process_t process);
+ Error Terminate();
+ Error GetMainModule(FileSpec &file_spec) const;
+
+ lldb::pid_t GetProcessId() const;
+ bool IsRunning() const;
+
+ private:
+ void Close();
+
+ lldb::pid_t m_pid;
+ lldb::process_t m_process;
+};
+}
+
+#endif
Modified: lldb/trunk/include/lldb/lldb-types.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/lldb-types.h?rev=216607&r1=216606&r2=216607&view=diff
==============================================================================
--- lldb/trunk/include/lldb/lldb-types.h (original)
+++ lldb/trunk/include/lldb/lldb-types.h Wed Aug 27 15:15:30 2014
@@ -49,6 +49,7 @@ namespace lldb
typedef void* mutex_t;
typedef void* condition_t;
typedef void* rwlock_t;
+ typedef void* process_t; // Process type is HANDLE
typedef uintptr_t thread_t; // Host thread type
typedef uint32_t thread_key_t;
typedef void * thread_arg_t; // Host thread argument type
@@ -68,6 +69,7 @@ namespace lldb
typedef ::pthread_mutex_t mutex_t;
typedef pthread_cond_t condition_t;
typedef pthread_rwlock_t rwlock_t;
+ typedef uint64_t process_t; // Process type is just a pid.
typedef pthread_t thread_t; // Host thread type
typedef pthread_key_t thread_key_t;
typedef void * thread_arg_t; // Host thread argument type
Modified: lldb/trunk/source/Host/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/CMakeLists.txt?rev=216607&r1=216606&r2=216607&view=diff
==============================================================================
--- lldb/trunk/source/Host/CMakeLists.txt (original)
+++ lldb/trunk/source/Host/CMakeLists.txt Wed Aug 27 15:15:30 2014
@@ -33,6 +33,7 @@ if (CMAKE_SYSTEM_NAME MATCHES "Windows")
windows/FileSystem.cpp
windows/Host.cpp
windows/HostInfoWindows.cpp
+ windows/HostProcessWindows.cpp
windows/ProcessRunLock.cpp
windows/Mutex.cpp
windows/Condition.cpp
@@ -43,6 +44,7 @@ else()
add_host_subdirectory(posix
posix/FileSystem.cpp
posix/HostInfoPosix.cpp
+ posix/HostProcessPosix.cpp
)
if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
Added: lldb/trunk/source/Host/posix/HostProcessPosix.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/posix/HostProcessPosix.cpp?rev=216607&view=auto
==============================================================================
--- lldb/trunk/source/Host/posix/HostProcessPosix.cpp (added)
+++ lldb/trunk/source/Host/posix/HostProcessPosix.cpp Wed Aug 27 15:15:30 2014
@@ -0,0 +1,101 @@
+//===-- HostProcessWindows.cpp ----------------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Host/posix/HostProcessPosix.h"
+#include "lldb/Host/FileSystem.h"
+
+#include "llvm/ADT/STLExtras.h"
+
+#include <limits.h>
+
+using namespace lldb_private;
+
+HostProcessPosix::HostProcessPosix()
+: m_pid(kInvalidProcessId)
+{
+}
+
+HostProcessPosix::~HostProcessPosix()
+{
+}
+
+Error HostProcessPosix::Create(lldb::pid_t pid)
+{
+ Error error;
+ if (pid == kInvalidProcessId)
+ error.SetErrorString("Attempt to create an invalid process");
+
+ m_pid = pid;
+ return error;
+}
+
+Error HostProcessPosix::Signal(int signo) const
+{
+ if (m_pid <= 0)
+ {
+ Error error;
+ error.SetErrorString("HostProcessPosix refers to an invalid process");
+ return error;
+ }
+
+ return HostProcessPosix::Signal(m_pid, signo);
+}
+
+Error HostProcessPosix::Signal(lldb::pid_t pid, int signo)
+{
+ Error error;
+
+ if (-1 == ::kill(pid, signo))
+ error.SetErrorToErrno();
+
+ return error;
+}
+
+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];
+ char exe_path[PATH_MAX] = "";
+ if (snprintf (link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", m_pid) <= 0)
+ {
+ error.SetErrorString("Unable to build /proc/<pid>/exe string");
+ return error;
+ }
+
+ error = FileSystem::Readlink(link_path, exe_path, llvm::array_lengthof(exe_path));
+ if (!error.Success())
+ return error;
+
+ const ssize_t len = strlen(exe_path);
+ // If the binary has been deleted, the link name has " (deleted)" appended.
+ // Remove if there.
+ static const ssize_t deleted_len = strlen(" (deleted)");
+ if (len > deleted_len &&
+ !strcmp(exe_path + len - deleted_len, " (deleted)"))
+ {
+ exe_path[len - deleted_len] = 0;
+ }
+
+ file_spec.SetFile(exe_path, false);
+ return error;
+}
+
+lldb::pid_t HostProcessPosix::GetProcessId() const
+{
+ return m_pid;
+}
+
+bool HostProcessPosix::IsRunning() const
+{
+ // Send this process the null signal. If it succeeds the process is running.
+ Error error = Signal(0);
+ return error.Success();
+}
Added: lldb/trunk/source/Host/windows/HostProcessWindows.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/windows/HostProcessWindows.cpp?rev=216607&view=auto
==============================================================================
--- lldb/trunk/source/Host/windows/HostProcessWindows.cpp (added)
+++ lldb/trunk/source/Host/windows/HostProcessWindows.cpp Wed Aug 27 15:15:30 2014
@@ -0,0 +1,112 @@
+//===-- HostProcessWindows.cpp ----------------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Host/windows/windows.h"
+
+#include <Psapi.h>
+
+#include "lldb/Host/windows/HostProcessWindows.h"
+
+#include "llvm/ADT/STLExtras.h"
+
+using namespace lldb_private;
+
+HostProcessWindows::HostProcessWindows()
+ : m_process(NULL)
+ , m_pid(0)
+{
+}
+
+HostProcessWindows::~HostProcessWindows()
+{
+ Close();
+}
+
+Error HostProcessWindows::Create(lldb::pid_t pid)
+{
+ Error error;
+ if (pid == m_pid)
+ return error;
+ Close();
+
+ m_process = ::OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
+ if (m_process == NULL)
+ {
+ error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
+ return error;
+ }
+ m_pid = pid;
+ return error;
+}
+
+Error HostProcessWindows::Create(lldb::process_t process)
+{
+ Error error;
+ if (process == m_process)
+ return error;
+ Close();
+
+ m_pid = ::GetProcessId(process);
+ if (m_pid == 0)
+ error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
+ m_process = process;
+ return error;
+}
+
+Error HostProcessWindows::Terminate()
+{
+ Error error;
+ if (m_process == NULL)
+ error.SetError(ERROR_INVALID_HANDLE, lldb::eErrorTypeWin32);
+
+ if (!::TerminateProcess(m_process, 0))
+ error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
+
+ return error;
+}
+
+Error HostProcessWindows::GetMainModule(FileSpec &file_spec) const
+{
+ Error error;
+ if (m_process == NULL)
+ error.SetError(ERROR_INVALID_HANDLE, lldb::eErrorTypeWin32);
+
+ char path[MAX_PATH] = { 0 };
+ if (::GetProcessImageFileName(m_process, path, llvm::array_lengthof(path)))
+ file_spec.SetFile(path, false);
+ else
+ error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
+
+ return error;
+}
+
+lldb::pid_t HostProcessWindows::GetProcessId() const
+{
+ return m_pid;
+}
+
+bool HostProcessWindows::IsRunning() const
+{
+ if (m_process == NULL)
+ return false;
+
+ DWORD code = 0;
+ if (!::GetExitCodeProcess(m_process, &code))
+ return false;
+
+ return (code == STILL_ACTIVE);
+}
+
+void HostProcessWindows::Close()
+{
+ if (m_process != NULL)
+ ::CloseHandle(m_process);
+ m_process = nullptr;
+ m_pid = 0;
+}
More information about the lldb-commits
mailing list