[Lldb-commits] [lldb] r353446 - [lldb-server] Improve support on Windows

Aaron Smith via lldb-commits lldb-commits at lists.llvm.org
Thu Feb 7 10:46:25 PST 2019


Author: asmith
Date: Thu Feb  7 10:46:25 2019
New Revision: 353446

URL: http://llvm.org/viewvc/llvm-project?rev=353446&view=rev
Log:
[lldb-server] Improve support on Windows

Summary:
This commit contains the following changes:

  - Rewrite vfile close/read/write packet handlers with portable routines from lldb.
    This removes #if(s) and allows the handlers to work on Windows.

  - Fix a bug in File::Write. This is intended to write data at an offset to a file
    but actually writes at the current position of the file.

  - Add a default boolean argument 'should_close_fd' to FileSystem::Open to
    let the user decide whether to close the fd or not.

Reviewers: zturner, llvm-commits, labath

Reviewed By: zturner

Subscribers: Hui, labath, abidh, lldb-commits

Differential Revision: https://reviews.llvm.org/D56231

Modified:
    lldb/trunk/include/lldb/Host/File.h
    lldb/trunk/include/lldb/Host/FileSystem.h
    lldb/trunk/source/Host/common/File.cpp
    lldb/trunk/source/Host/common/FileSystem.cpp
    lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp

Modified: lldb/trunk/include/lldb/Host/File.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/File.h?rev=353446&r1=353445&r2=353446&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/File.h (original)
+++ lldb/trunk/include/lldb/Host/File.h Thu Feb  7 10:46:25 2019
@@ -14,6 +14,7 @@
 #include "lldb/Utility/Status.h"
 #include "lldb/lldb-private.h"
 
+#include <mutex>
 #include <stdarg.h>
 #include <stdio.h>
 #include <sys/types.h>
@@ -420,6 +421,7 @@ protected:
   LazyBool m_is_interactive;
   LazyBool m_is_real_terminal;
   LazyBool m_supports_colors;
+  std::mutex offset_access_mutex;
 
 private:
   DISALLOW_COPY_AND_ASSIGN(File);

Modified: lldb/trunk/include/lldb/Host/FileSystem.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/FileSystem.h?rev=353446&r1=353445&r2=353446&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/FileSystem.h (original)
+++ lldb/trunk/include/lldb/Host/FileSystem.h Thu Feb  7 10:46:25 2019
@@ -64,7 +64,8 @@ public:
   int Open(const char *path, int flags, int mode);
 
   Status Open(File &File, const FileSpec &file_spec, uint32_t options,
-              uint32_t permissions = lldb::eFilePermissionsFileDefault);
+              uint32_t permissions = lldb::eFilePermissionsFileDefault,
+              bool should_close_fd = true);
 
   /// Get a directory iterator.
   /// @{

Modified: lldb/trunk/source/Host/common/File.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/File.cpp?rev=353446&r1=353445&r2=353446&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/File.cpp (original)
+++ lldb/trunk/source/Host/common/File.cpp Thu Feb  7 10:46:25 2019
@@ -178,7 +178,7 @@ Status File::Close() {
 
 void File::Clear() {
   m_stream = nullptr;
-  m_descriptor = -1;
+  m_descriptor = kInvalidDescriptor;
   m_options = 0;
   m_own_stream = false;
   m_is_interactive = m_supports_colors = m_is_real_terminal =
@@ -503,6 +503,7 @@ Status File::Read(void *buf, size_t &num
     error.SetErrorString("invalid file handle");
   }
 #else
+  std::lock_guard<std::mutex> guard(offset_access_mutex);
   long cur = ::lseek(m_descriptor, 0, SEEK_CUR);
   SeekFromStart(offset);
   error = Read(buf, num_bytes);
@@ -602,7 +603,9 @@ Status File::Write(const void *buf, size
       num_bytes = bytes_written;
     }
 #else
+    std::lock_guard<std::mutex> guard(offset_access_mutex);
     long cur = ::lseek(m_descriptor, 0, SEEK_CUR);
+    SeekFromStart(offset);
     error = Write(buf, num_bytes);
     long after = ::lseek(m_descriptor, 0, SEEK_CUR);
 

Modified: lldb/trunk/source/Host/common/FileSystem.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/FileSystem.cpp?rev=353446&r1=353445&r2=353446&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/FileSystem.cpp (original)
+++ lldb/trunk/source/Host/common/FileSystem.cpp Thu Feb  7 10:46:25 2019
@@ -408,7 +408,7 @@ static mode_t GetOpenMode(uint32_t permi
 }
 
 Status FileSystem::Open(File &File, const FileSpec &file_spec, uint32_t options,
-                        uint32_t permissions) {
+                        uint32_t permissions, bool should_close_fd) {
   if (m_collector)
     m_collector->AddFile(file_spec);
 
@@ -431,7 +431,7 @@ Status FileSystem::Open(File &File, cons
     File.SetDescriptor(descriptor, false);
     error.SetErrorToErrno();
   } else {
-    File.SetDescriptor(descriptor, true);
+    File.SetDescriptor(descriptor, should_close_fd);
     File.SetOptions(options);
   }
   return error;

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp?rev=353446&r1=353445&r2=353446&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp Thu Feb  7 10:46:25 2019
@@ -10,7 +10,6 @@
 
 #include <errno.h>
 
-
 #ifdef __APPLE__
 #include <TargetConditionals.h>
 #endif
@@ -214,8 +213,7 @@ GDBRemoteCommunicationServerCommon::Hand
   if (sub != LLDB_INVALID_CPUTYPE)
     response.Printf("cpusubtype:%u;", sub);
 
-  if (cpu == llvm::MachO::CPU_TYPE_ARM
-      || cpu == llvm::MachO::CPU_TYPE_ARM64) {
+  if (cpu == llvm::MachO::CPU_TYPE_ARM || cpu == llvm::MachO::CPU_TYPE_ARM64) {
 // Indicate the OS type.
 #if defined(TARGET_OS_TV) && TARGET_OS_TV == 1
     response.PutCString("ostype:tvos;");
@@ -510,18 +508,19 @@ GDBRemoteCommunicationServerCommon::Hand
   packet.GetHexByteStringTerminatedBy(path, ',');
   if (!path.empty()) {
     if (packet.GetChar() == ',') {
-      uint32_t flags =
-          File::ConvertOpenOptionsForPOSIXOpen(packet.GetHexMaxU32(false, 0));
+      uint32_t flags = packet.GetHexMaxU32(false, 0);
       if (packet.GetChar() == ',') {
         mode_t mode = packet.GetHexMaxU32(false, 0600);
-        Status error;
         FileSpec path_spec(path);
         FileSystem::Instance().Resolve(path_spec);
-        int fd = ::open(path_spec.GetCString(), flags, mode);
-        const int save_errno = fd == -1 ? errno : 0;
+        File file;
+        // Do not close fd.
+        Status error =
+            FileSystem::Instance().Open(file, path_spec, flags, mode, false);
+        const int save_errno = error.GetError();
         StreamString response;
         response.PutChar('F');
-        response.Printf("%i", fd);
+        response.Printf("%i", file.GetDescriptor());
         if (save_errno)
           response.Printf(",%i", save_errno);
         return SendPacketNoLock(response.GetString());
@@ -536,12 +535,13 @@ GDBRemoteCommunicationServerCommon::Hand
     StringExtractorGDBRemote &packet) {
   packet.SetFilePos(::strlen("vFile:close:"));
   int fd = packet.GetS32(-1);
-  Status error;
   int err = -1;
   int save_errno = 0;
   if (fd >= 0) {
-    err = close(fd);
-    save_errno = err == -1 ? errno : 0;
+    File file(fd, true);
+    Status error = file.Close();
+    err = 0;
+    save_errno = error.GetError();
   } else {
     save_errno = EINVAL;
   }
@@ -556,26 +556,23 @@ GDBRemoteCommunicationServerCommon::Hand
 GDBRemoteCommunication::PacketResult
 GDBRemoteCommunicationServerCommon::Handle_vFile_pRead(
     StringExtractorGDBRemote &packet) {
-#ifdef _WIN32
-  // Not implemented on Windows
-  return SendUnimplementedResponse(
-      "GDBRemoteCommunicationServerCommon::Handle_vFile_pRead() unimplemented");
-#else
   StreamGDBRemote response;
   packet.SetFilePos(::strlen("vFile:pread:"));
   int fd = packet.GetS32(-1);
   if (packet.GetChar() == ',') {
-    uint64_t count = packet.GetU64(UINT64_MAX);
+    size_t count = packet.GetU64(UINT64_MAX);
     if (packet.GetChar() == ',') {
-      uint64_t offset = packet.GetU64(UINT32_MAX);
+      off_t offset = packet.GetU64(UINT32_MAX);
       if (count == UINT64_MAX) {
         response.Printf("F-1:%i", EINVAL);
         return SendPacketNoLock(response.GetString());
       }
 
       std::string buffer(count, 0);
-      const ssize_t bytes_read = ::pread(fd, &buffer[0], buffer.size(), offset);
-      const int save_errno = bytes_read == -1 ? errno : 0;
+      File file(fd, false);
+      Status error = file.Read(static_cast<void *>(&buffer[0]), count, offset);
+      const ssize_t bytes_read = error.Success() ? count : -1;
+      const int save_errno = error.GetError();
       response.PutChar('F');
       response.Printf("%zi", bytes_read);
       if (save_errno)
@@ -588,17 +585,11 @@ GDBRemoteCommunicationServerCommon::Hand
     }
   }
   return SendErrorResponse(21);
-
-#endif
 }
 
 GDBRemoteCommunication::PacketResult
 GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite(
     StringExtractorGDBRemote &packet) {
-#ifdef _WIN32
-  return SendUnimplementedResponse("GDBRemoteCommunicationServerCommon::Handle_"
-                                   "vFile_pWrite() unimplemented");
-#else
   packet.SetFilePos(::strlen("vFile:pwrite:"));
 
   StreamGDBRemote response;
@@ -610,9 +601,12 @@ GDBRemoteCommunicationServerCommon::Hand
     if (packet.GetChar() == ',') {
       std::string buffer;
       if (packet.GetEscapedBinaryData(buffer)) {
-        const ssize_t bytes_written =
-            ::pwrite(fd, buffer.data(), buffer.size(), offset);
-        const int save_errno = bytes_written == -1 ? errno : 0;
+        File file(fd, false);
+        size_t count = buffer.size();
+        Status error =
+            file.Write(static_cast<const void *>(&buffer[0]), count, offset);
+        const ssize_t bytes_written = error.Success() ? count : -1;
+        const int save_errno = error.GetError();
         response.Printf("%zi", bytes_written);
         if (save_errno)
           response.Printf(",%i", save_errno);
@@ -623,7 +617,6 @@ GDBRemoteCommunicationServerCommon::Hand
     }
   }
   return SendErrorResponse(27);
-#endif
 }
 
 GDBRemoteCommunication::PacketResult
@@ -971,7 +964,8 @@ GDBRemoteCommunicationServerCommon::Hand
   const uint32_t bytes_left = packet.GetBytesLeft();
   if (bytes_left > 0) {
     const char *arch_triple = packet.Peek();
-    m_process_launch_info.SetArchitecture(HostInfo::GetAugmentedArchSpec(arch_triple));
+    m_process_launch_info.SetArchitecture(
+        HostInfo::GetAugmentedArchSpec(arch_triple));
     return SendOKResponse();
   }
   return SendErrorResponse(13);
@@ -1085,7 +1079,8 @@ GDBRemoteCommunicationServerCommon::Hand
   StreamGDBRemote response;
 
   if (uuid_str.empty()) {
-    auto Result = llvm::sys::fs::md5_contents(matched_module_spec.GetFileSpec().GetPath());
+    auto Result = llvm::sys::fs::md5_contents(
+        matched_module_spec.GetFileSpec().GetPath());
     if (!Result)
       return SendErrorResponse(5);
     response.PutCString("md5:");




More information about the lldb-commits mailing list