lawrence_danna created this revision.
lawrence_danna added reviewers: JDevlieghere, jasonmolenda.
Herald added a project: LLDB.

It's not clear from the header a File created with a descriptor will be
not be usable by LLDB unless SetOptions is also called, but it is.

This is because some parts of  LLDB relies on GetStream() to use the
file, and that in turn relies on calling fdopen on the descriptor.  When
calling fdopen, GetStream relies on m_options to determine the access
mode.   If m_options has never been set, GetStream() will fail.

Instead of giving up if m_options==0, GetStream() will now call fcntl to
get the access flags.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D67792

Files:
  lldb/source/Host/common/File.cpp


Index: lldb/source/Host/common/File.cpp
===================================================================
--- lldb/source/Host/common/File.cpp
+++ lldb/source/Host/common/File.cpp
@@ -103,7 +103,22 @@
 FILE *File::GetStream() {
   if (!StreamIsValid()) {
     if (DescriptorIsValid()) {
-      const char *mode = GetStreamOpenModeFromOptions(m_options);
+      const char *mode = nullptr;
+      if (m_options) {
+        mode = GetStreamOpenModeFromOptions(m_options);
+      } else {
+       int fdflags = ::fcntl(m_descriptor, F_GETFL);
+       if (fdflags >= 0) {
+          if (fdflags & O_RDWR) {
+            mode = "r+";
+          } else if (fdflags & O_WRONLY) {
+            mode = "w";
+          } else {
+            mode = "r";
+          }
+        }
+      }
+
       if (mode) {
         if (!m_should_close_fd) {
 // We must duplicate the file descriptor if we don't own it because when you


Index: lldb/source/Host/common/File.cpp
===================================================================
--- lldb/source/Host/common/File.cpp
+++ lldb/source/Host/common/File.cpp
@@ -103,7 +103,22 @@
 FILE *File::GetStream() {
   if (!StreamIsValid()) {
     if (DescriptorIsValid()) {
-      const char *mode = GetStreamOpenModeFromOptions(m_options);
+      const char *mode = nullptr;
+      if (m_options) {
+        mode = GetStreamOpenModeFromOptions(m_options);
+      } else {
+       int fdflags = ::fcntl(m_descriptor, F_GETFL);
+       if (fdflags >= 0) {
+          if (fdflags & O_RDWR) {
+            mode = "r+";
+          } else if (fdflags & O_WRONLY) {
+            mode = "w";
+          } else {
+            mode = "r";
+          }
+        }
+      }
+
       if (mode) {
         if (!m_should_close_fd) {
 // We must duplicate the file descriptor if we don't own it because when you
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to