https://github.com/kusmour created 
https://github.com/llvm/llvm-project/pull/112657

When FileAction opens file with write access for lldb stdout/stderr redirect, 
it doesn't clear the file nor append to the end of the file if it already 
exists. Instead, it writes from cursor index 0.

This function is only called when launch debugging with stdin/stdout/stderr 
redirection to files.

We can also implement open options like the `FileSystem::Open` method so that 
we can handle truncate vs. append

>From eac4e0b05f6c55dc9621edb3c2ba6b07876b3efb Mon Sep 17 00:00:00 2001
From: Wanyi Ye <wa...@meta.com>
Date: Mon, 14 Oct 2024 22:37:50 -0700
Subject: [PATCH] [lldb] Fix write only file action to truncate the file

---
 lldb/source/Host/common/FileAction.cpp |  2 +-
 lldb/unittests/Host/FileActionTest.cpp | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/lldb/source/Host/common/FileAction.cpp 
b/lldb/source/Host/common/FileAction.cpp
index f980d3224640e0..e1c3e14a165ea9 100644
--- a/lldb/source/Host/common/FileAction.cpp
+++ b/lldb/source/Host/common/FileAction.cpp
@@ -41,7 +41,7 @@ bool FileAction::Open(int fd, const FileSpec &file_spec, bool 
read,
     else if (read)
       m_arg = O_NOCTTY | O_RDONLY;
     else
-      m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
+      m_arg = O_NOCTTY | O_CREAT | O_WRONLY | O_TRUNC;
     m_file_spec = file_spec;
     return true;
   } else {
diff --git a/lldb/unittests/Host/FileActionTest.cpp 
b/lldb/unittests/Host/FileActionTest.cpp
index b208169aac20e6..8c8b81a9250310 100644
--- a/lldb/unittests/Host/FileActionTest.cpp
+++ b/lldb/unittests/Host/FileActionTest.cpp
@@ -6,6 +6,8 @@
 //
 
//===----------------------------------------------------------------------===//
 
+#include <fcntl.h>
+
 #include "lldb/Host/FileAction.h"
 #include "gtest/gtest.h"
 
@@ -17,3 +19,25 @@ TEST(FileActionTest, Open) {
   EXPECT_EQ(Action.GetAction(), FileAction::eFileActionOpen);
   EXPECT_EQ(Action.GetFileSpec(), FileSpec("/tmp"));
 }
+
+TEST(FileActionTest, OpenReadWrite) {
+  FileAction Action;
+  Action.Open(48, FileSpec("/tmp_0"), /*read*/ true, /*write*/ true);
+  EXPECT_TRUE(Action.GetActionArgument() & (O_NOCTTY | O_CREAT | O_RDWR));
+  EXPECT_FALSE(Action.GetActionArgument() & O_RDONLY);
+  EXPECT_FALSE(Action.GetActionArgument() & O_WRONLY);
+}
+
+TEST(FileActionTest, OpenReadOnly) {
+  FileAction Action;
+  Action.Open(49, FileSpec("/tmp_1"), /*read*/ true, /*write*/ false);
+  EXPECT_TRUE(Action.GetActionArgument() & (O_NOCTTY | O_RDONLY));
+  EXPECT_FALSE(Action.GetActionArgument() & O_WRONLY);
+}
+
+TEST(FileActionTest, OpenWriteOnly) {
+  FileAction Action;
+  Action.Open(50, FileSpec("/tmp_2"), /*read*/ false, /*write*/ true);
+  EXPECT_TRUE(Action.GetActionArgument() & (O_NOCTTY | O_CREAT | O_WRONLY | 
O_TRUNC));
+  EXPECT_FALSE(Action.GetActionArgument() & O_RDONLY);
+} 

_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to