[Lldb-commits] [lldb] r217632 - Update HostProcess to use the same facade pattern as HostThread.

Zachary Turner zturner at google.com
Thu Sep 11 15:22:17 PDT 2014


Author: zturner
Date: Thu Sep 11 17:22:16 2014
New Revision: 217632

URL: http://llvm.org/viewvc/llvm-project?rev=217632&view=rev
Log:
Update HostProcess to use the same facade pattern as HostThread.

Added:
    lldb/trunk/include/lldb/Host/HostNativeProcess.h
    lldb/trunk/include/lldb/Host/HostNativeProcessBase.h
    lldb/trunk/source/Host/common/HostProcess.cpp
Modified:
    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/lldb.xcodeproj/project.pbxproj
    lldb/trunk/source/Host/CMakeLists.txt
    lldb/trunk/source/Host/posix/HostProcessPosix.cpp
    lldb/trunk/source/Host/windows/HostProcessWindows.cpp

Added: lldb/trunk/include/lldb/Host/HostNativeProcess.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/HostNativeProcess.h?rev=217632&view=auto
==============================================================================
--- lldb/trunk/include/lldb/Host/HostNativeProcess.h (added)
+++ lldb/trunk/include/lldb/Host/HostNativeProcess.h Thu Sep 11 17:22:16 2014
@@ -0,0 +1,27 @@
+//===-- HostNativeProcess.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_HostNativeProcess_h_
+#define lldb_Host_HostNativeProcess_h_
+
+#if defined(_WIN32)
+#include "lldb/Host/windows/HostProcessWindows.h"
+namespace lldb_private
+{
+typedef HostProcessWindows HostNativeProcess;
+}
+#else
+#include "lldb/Host/posix/HostProcessPosix.h"
+namespace lldb_private
+{
+typedef HostProcessPosix HostNativeProcess;
+}
+#endif
+
+#endif

Added: lldb/trunk/include/lldb/Host/HostNativeProcessBase.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/HostNativeProcessBase.h?rev=217632&view=auto
==============================================================================
--- lldb/trunk/include/lldb/Host/HostNativeProcessBase.h (added)
+++ lldb/trunk/include/lldb/Host/HostNativeProcessBase.h Thu Sep 11 17:22:16 2014
@@ -0,0 +1,43 @@
+//===-- HostNativeProcessBase.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_HostNativeProcessBase_h_
+#define lldb_Host_HostNativeProcessBase_h_
+
+#include "lldb/Core/Error.h"
+#include "lldb/lldb-defines.h"
+#include "lldb/lldb-types.h"
+
+namespace lldb_private
+{
+
+class HostNativeProcessBase
+{
+    DISALLOW_COPY_AND_ASSIGN(HostNativeProcessBase);
+
+  public:
+    HostNativeProcessBase() {}
+    explicit HostNativeProcessBase(lldb::process_t process)
+        : m_process(process)
+    {}
+    virtual ~HostNativeProcessBase() {}
+
+    virtual Error Terminate() = 0;
+    virtual Error GetMainModule(FileSpec &file_spec) const = 0;
+
+    virtual lldb::pid_t GetProcessId() const = 0;
+    virtual bool IsRunning() const = 0;
+
+  protected:
+    lldb::process_t m_process;
+};
+
+}
+
+#endif

Modified: lldb/trunk/include/lldb/Host/HostProcess.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/HostProcess.h?rev=217632&r1=217631&r2=217632&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/HostProcess.h (original)
+++ lldb/trunk/include/lldb/Host/HostProcess.h Thu Sep 11 17:22:16 2014
@@ -10,6 +10,8 @@
 #ifndef lldb_Host_HostProcess_h_
 #define lldb_Host_HostProcess_h_
 
+#include "lldb/lldb-types.h"
+
 //----------------------------------------------------------------------
 /// @class HostInfo HostInfo.h "lldb/Host/HostProcess.h"
 /// @brief A class that represents a running process on the host machine.
@@ -26,19 +28,31 @@
 ///
 //----------------------------------------------------------------------
 
-#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
+class HostNativeProcessBase;
+
+class HostProcess
+{
+  public:
+    HostProcess();
+    HostProcess(lldb::process_t process);
+    ~HostProcess();
+
+    Error Terminate();
+    Error GetMainModule(FileSpec &file_spec) const;
+
+    lldb::pid_t GetProcessId() const;
+    bool IsRunning() const;
+
+    HostNativeProcessBase &GetNativeProcess();
+    const HostNativeProcessBase &GetNativeProcess() const;
+
+  private:
+    std::shared_ptr<HostNativeProcessBase> m_native_process;
+};
+
+}
 
 #endif

Modified: lldb/trunk/include/lldb/Host/posix/HostProcessPosix.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/posix/HostProcessPosix.h?rev=217632&r1=217631&r2=217632&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/posix/HostProcessPosix.h (original)
+++ lldb/trunk/include/lldb/Host/posix/HostProcessPosix.h Thu Sep 11 17:22:16 2014
@@ -12,34 +12,28 @@
 
 #include "lldb/lldb-types.h"
 #include "lldb/Core/Error.h"
-#include "lldb/Target/ProcessLaunchInfo.h"
+#include "lldb/Host/HostNativeProcessBase.h"
 
 namespace lldb_private
 {
 
 class FileSpec;
 
-class HostProcessPosix
+class HostProcessPosix : public HostNativeProcessBase
 {
   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;
+    HostProcessPosix(lldb::process_t process);
+    virtual ~HostProcessPosix();
 
-    lldb::pid_t GetProcessId() const;
-    bool IsRunning() const;
+    virtual Error Signal(int signo) const;
+    static Error Signal(lldb::process_t process, int signo);
 
-  private:
+    virtual Error Terminate();
+    virtual Error GetMainModule(FileSpec &file_spec) const;
 
-    lldb::pid_t m_pid;
+    virtual lldb::pid_t GetProcessId() const;
+    virtual bool IsRunning() const;
 };
 }
 

Modified: lldb/trunk/include/lldb/Host/windows/HostProcessWindows.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/windows/HostProcessWindows.h?rev=217632&r1=217631&r2=217632&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/windows/HostProcessWindows.h (original)
+++ lldb/trunk/include/lldb/Host/windows/HostProcessWindows.h Thu Sep 11 17:22:16 2014
@@ -10,34 +10,28 @@
 #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"
+#include "lldb/Host/HostNativeProcessBase.h"
 
 namespace lldb_private
 {
 
 class FileSpec;
 
-class HostProcessWindows
+class HostProcessWindows : public HostNativeProcessBase
 {
   public:
     HostProcessWindows();
+    explicit HostProcessWindows(lldb::process_t process);
     ~HostProcessWindows();
 
-    Error Create(lldb::pid_t pid);
-    Error Create(lldb::process_t process);
-    Error Terminate();
-    Error GetMainModule(FileSpec &file_spec) const;
+    virtual Error Terminate();
+    virtual Error GetMainModule(FileSpec &file_spec) const;
 
-    lldb::pid_t GetProcessId() const;
-    bool IsRunning() const;
+    virtual lldb::pid_t GetProcessId() const;
+    virtual bool IsRunning() const;
 
   private:
     void Close();
-
-    lldb::pid_t m_pid;
-    lldb::process_t m_process;
 };
 }
 

Modified: lldb/trunk/lldb.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lldb.xcodeproj/project.pbxproj?rev=217632&r1=217631&r2=217632&view=diff
==============================================================================
--- lldb/trunk/lldb.xcodeproj/project.pbxproj (original)
+++ lldb/trunk/lldb.xcodeproj/project.pbxproj Thu Sep 11 17:22:16 2014
@@ -597,6 +597,7 @@
 		3FDFED2719BA6D96009756A7 /* HostNativeThreadBase.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3FDFED2419BA6D96009756A7 /* HostNativeThreadBase.cpp */; };
 		3FDFED2819BA6D96009756A7 /* HostThread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3FDFED2519BA6D96009756A7 /* HostThread.cpp */; };
 		3FDFED2919BA6D96009756A7 /* ThreadLauncher.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3FDFED2619BA6D96009756A7 /* ThreadLauncher.cpp */; };
+		3FDFED2D19C257A0009756A7 /* HostProcess.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3FDFED2C19C257A0009756A7 /* HostProcess.cpp */; };
 		449ACC98197DEA0B008D175E /* FastDemangle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 449ACC96197DE9EC008D175E /* FastDemangle.cpp */; };
 		490A36C0180F0E6F00BA31F8 /* PlatformWindows.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 490A36BD180F0E6F00BA31F8 /* PlatformWindows.cpp */; };
 		490A36C2180F0E9300BA31F8 /* PlatformWindows.h in Headers */ = {isa = PBXBuildFile; fileRef = 490A36BE180F0E6F00BA31F8 /* PlatformWindows.h */; };
@@ -1823,6 +1824,7 @@
 		3FDFED2419BA6D96009756A7 /* HostNativeThreadBase.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HostNativeThreadBase.cpp; sourceTree = "<group>"; };
 		3FDFED2519BA6D96009756A7 /* HostThread.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HostThread.cpp; sourceTree = "<group>"; };
 		3FDFED2619BA6D96009756A7 /* ThreadLauncher.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ThreadLauncher.cpp; sourceTree = "<group>"; };
+		3FDFED2C19C257A0009756A7 /* HostProcess.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HostProcess.cpp; sourceTree = "<group>"; };
 		449ACC96197DE9EC008D175E /* FastDemangle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = FastDemangle.cpp; path = source/Core/FastDemangle.cpp; sourceTree = "<group>"; };
 		4906FD4012F2255300A2A77C /* ASTDumper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ASTDumper.cpp; path = source/Expression/ASTDumper.cpp; sourceTree = "<group>"; };
 		4906FD4412F2257600A2A77C /* ASTDumper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ASTDumper.h; path = include/lldb/Expression/ASTDumper.h; sourceTree = "<group>"; };
@@ -4198,6 +4200,7 @@
 				69A01E1C1236C5D400C660B5 /* Host.cpp */,
 				3FDFE53419A29327009756A7 /* HostInfoBase.cpp */,
 				3FDFED2419BA6D96009756A7 /* HostNativeThreadBase.cpp */,
+				3FDFED2C19C257A0009756A7 /* HostProcess.cpp */,
 				3FDFED2519BA6D96009756A7 /* HostThread.cpp */,
 				236124A21986B4E2004EFC37 /* IOObject.cpp */,
 				69A01E1E1236C5D400C660B5 /* Mutex.cpp */,
@@ -5101,6 +5104,7 @@
 				268900B013353E5000698AC0 /* RegisterContextLLDB.cpp in Sources */,
 				3FDFE56C19AF9C44009756A7 /* HostProcessPosix.cpp in Sources */,
 				268900B413353E5000698AC0 /* RegisterContextMacOSXFrameBackchain.cpp in Sources */,
+				3FDFED2D19C257A0009756A7 /* HostProcess.cpp in Sources */,
 				268900B513353E5000698AC0 /* StopInfoMachException.cpp in Sources */,
 				268900B613353E5000698AC0 /* UnwindMacOSXFrameBackchain.cpp in Sources */,
 				268900B713353E5F00698AC0 /* DWARFAbbreviationDeclaration.cpp in Sources */,

Modified: lldb/trunk/source/Host/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/CMakeLists.txt?rev=217632&r1=217631&r2=217632&view=diff
==============================================================================
--- lldb/trunk/source/Host/CMakeLists.txt (original)
+++ lldb/trunk/source/Host/CMakeLists.txt Thu Sep 11 17:22:16 2014
@@ -12,6 +12,7 @@ add_host_subdirectory(common
   common/Host.cpp
   common/HostInfoBase.cpp
   common/HostNativeThreadBase.cpp
+  common/HostProcess.cpp
   common/HostThread.cpp
   common/IOObject.cpp
   common/Mutex.cpp

Added: lldb/trunk/source/Host/common/HostProcess.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/HostProcess.cpp?rev=217632&view=auto
==============================================================================
--- lldb/trunk/source/Host/common/HostProcess.cpp (added)
+++ lldb/trunk/source/Host/common/HostProcess.cpp Thu Sep 11 17:22:16 2014
@@ -0,0 +1,54 @@
+//===-- HostProcess.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/HostNativeProcess.h"
+#include "lldb/Host/HostProcess.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+HostProcess::HostProcess()
+    : m_native_process(new HostNativeProcess)
+{
+}
+
+HostProcess::HostProcess(lldb::process_t process)
+    : m_native_process(new HostNativeProcess(process))
+{
+}
+
+Error HostProcess::Terminate()
+{
+    return m_native_process->Terminate();
+}
+
+Error HostProcess::GetMainModule(FileSpec &file_spec) const
+{
+    return m_native_process->GetMainModule(file_spec);
+}
+
+lldb::pid_t HostProcess::GetProcessId() const
+{
+    return m_native_process->GetProcessId();
+}
+
+bool HostProcess::IsRunning() const
+{
+    return m_native_process->IsRunning();
+}
+
+HostNativeProcessBase &HostProcess::GetNativeProcess()
+{
+    return *m_native_process;
+}
+
+const HostNativeProcessBase &HostProcess::GetNativeProcess() const
+{
+    return *m_native_process;
+}

Modified: lldb/trunk/source/Host/posix/HostProcessPosix.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/posix/HostProcessPosix.cpp?rev=217632&r1=217631&r2=217632&view=diff
==============================================================================
--- lldb/trunk/source/Host/posix/HostProcessPosix.cpp (original)
+++ lldb/trunk/source/Host/posix/HostProcessPosix.cpp Thu Sep 11 17:22:16 2014
@@ -16,10 +16,13 @@
 
 using namespace lldb_private;
 
-const lldb::pid_t HostProcessPosix::kInvalidProcessId = 0;
+namespace
+{
+    const int kInvalidPosixProcess = 0;
+}
 
 HostProcessPosix::HostProcessPosix()
-: m_pid(kInvalidProcessId)
+    : HostNativeProcessBase(kInvalidPosixProcess)
 {
 }
 
@@ -27,38 +30,33 @@ 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)
+    if (m_process == kInvalidPosixProcess)
     {
         Error error;
         error.SetErrorString("HostProcessPosix refers to an invalid process");
         return error;
     }
 
-    return HostProcessPosix::Signal(m_pid, signo);
+    return HostProcessPosix::Signal(m_process, signo);
 }
 
-Error HostProcessPosix::Signal(lldb::pid_t pid, int signo)
+Error HostProcessPosix::Signal(lldb::process_t process, int signo)
 {
     Error error;
 
-    if (-1 == ::kill(pid, signo))
+    if (-1 == ::kill(process, signo))
         error.SetErrorToErrno();
 
     return error;
 }
 
+Error HostProcessPosix::Terminate()
+{
+    return Signal(SIGKILL);
+}
+
 Error HostProcessPosix::GetMainModule(FileSpec &file_spec) const
 {
     Error error;
@@ -66,7 +64,7 @@ Error HostProcessPosix::GetMainModule(Fi
     // 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)
+    if (snprintf (link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", m_process) <= 0)
     {
         error.SetErrorString("Unable to build /proc/<pid>/exe string");
         return error;
@@ -92,7 +90,7 @@ Error HostProcessPosix::GetMainModule(Fi
 
 lldb::pid_t HostProcessPosix::GetProcessId() const
 {
-    return m_pid;
+    return m_process;
 }
 
 bool HostProcessPosix::IsRunning() const

Modified: lldb/trunk/source/Host/windows/HostProcessWindows.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/windows/HostProcessWindows.cpp?rev=217632&r1=217631&r2=217632&view=diff
==============================================================================
--- lldb/trunk/source/Host/windows/HostProcessWindows.cpp (original)
+++ lldb/trunk/source/Host/windows/HostProcessWindows.cpp Thu Sep 11 17:22:16 2014
@@ -7,19 +7,18 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "lldb/Host/FileSpec.h"
 #include "lldb/Host/windows/windows.h"
-
-#include <Psapi.h>
-
 #include "lldb/Host/windows/HostProcessWindows.h"
 
 #include "llvm/ADT/STLExtras.h"
 
+#include <Psapi.h>
+
 using namespace lldb_private;
 
-HostProcessWindows::HostProcessWindows()
-    : m_process(NULL)
-    , m_pid(0)
+HostProcessWindows::HostProcessWindows(lldb::process_t process)
+    : HostNativeProcessBase(process)
 {
 }
 
@@ -28,41 +27,10 @@ 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)
+    if (m_process == nullptr)
         error.SetError(ERROR_INVALID_HANDLE, lldb::eErrorTypeWin32);
 
     if (!::TerminateProcess(m_process, 0))
@@ -74,7 +42,7 @@ Error HostProcessWindows::Terminate()
 Error HostProcessWindows::GetMainModule(FileSpec &file_spec) const
 {
     Error error;
-    if (m_process == NULL)
+    if (m_process == nullptr)
         error.SetError(ERROR_INVALID_HANDLE, lldb::eErrorTypeWin32);
 
     char path[MAX_PATH] = { 0 };
@@ -88,12 +56,12 @@ Error HostProcessWindows::GetMainModule(
 
 lldb::pid_t HostProcessWindows::GetProcessId() const
 {
-    return m_pid;
+    return ::GetProcessId(m_process);
 }
 
 bool HostProcessWindows::IsRunning() const
 {
-    if (m_process == NULL)
+    if (m_process == nullptr)
         return false;
 
     DWORD code = 0;
@@ -105,8 +73,7 @@ bool HostProcessWindows::IsRunning() con
 
 void HostProcessWindows::Close()
 {
-    if (m_process != NULL)
+    if (m_process != nullptr)
         ::CloseHandle(m_process);
     m_process = nullptr;
-    m_pid = 0;
 }





More information about the lldb-commits mailing list