-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[lldb] Fix write only file action to truncate the file #112657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-lldb Author: Wanyi (kusmour) ChangesWhen 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 Full diff: https://github.com/llvm/llvm-project/pull/112657.diff 2 Files Affected:
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);
+}
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
What you describe makes some sense, but could you give an example of where this is used? Can this be triggered with |
It would be nice to have a test which verifies actual behavior rather than just some flag being set somewhere. For example, you could add to/extend |
For example, the settings Will add both later! Thanks! |
eac4e0b
to
48831c6
Compare
@DavidSpickett If you don't mind me asking, how do we add to release note? Thanks in advance! |
48831c6
to
4acbf06
Compare
4acbf06
to
6ee1560
Compare
@@ -6,6 +6,8 @@ | |||
// | |||
//===----------------------------------------------------------------------===// | |||
|
|||
#include <fcntl.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this work on windows?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, from the code search it seems a lot of base classes have that #include. I don't have a windows machine to test with. I can change the #include to a file within llvm-project that has the flag definition
eg. https://github.com/llvm/llvm-project/blob/main/lldb/source/Host/common/File.cpp#L15-L19
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's also passing the Windows CI. @bulbazord I can help @kusmour validate if you want, but I personally think CI is good enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI is good enough for me here. I've approved the PR.
LLDB windows build failure: https://lab.llvm.org/buildbot/#/builders/141/builds/3462
API test failed for remote platform in [#112657](#112657) Previously when putting files onto remote platform, I used `platform file write -d <data>` which actually required a `platform file open <path>` first in order to obtain a file descriptor. eg. in file [TestGDBRemotePlatformFile.py](https://github.com/llvm/llvm-project/blob/94e7d9c0bfe517507ea08b00fb00c32fb2837a82/lldb/test/API/functionalities/gdb_remote_client/TestGDBRemotePlatformFile.py#L24-L32) To fix this, use the `platform put-file` method, which is used in the `redirect_stdin` from this test already.
@kusmour , The
see more details here: https://lab.llvm.org/buildbot/#/builders/195/builds/321 |
Hi thank you for your msg. I have the fix up. Merged and waiting on the test now: #114119 |
Disable part of the test failing on windows. as O_NOCTTY and O_RDONLY dont have same behavior on windows vs linux.
API test failed for remote platform in [llvm#112657](llvm#112657) Previously when putting files onto remote platform, I used `platform file write -d <data>` which actually required a `platform file open <path>` first in order to obtain a file descriptor. eg. in file [TestGDBRemotePlatformFile.py](https://github.com/llvm/llvm-project/blob/94e7d9c0bfe517507ea08b00fb00c32fb2837a82/lldb/test/API/functionalities/gdb_remote_client/TestGDBRemotePlatformFile.py#L24-L32) To fix this, use the `platform put-file` method, which is used in the `redirect_stdin` from this test already.
Disable part of the test failing on windows. as O_NOCTTY and O_RDONLY dont have same behavior on windows vs linux.
When `FileAction` opens file with write access, 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. For example, by using the settings `target.output-path` and `target.error-path`, lldb will redirect process stdout/stderr to files. It then calls this function to write to the files which the above symptoms appear. ## Test - Added unit test checking the file flags - Added 2 api tests checking - File content overwritten if the file path already exists - Stdout and stderr redirection to the same file doesn't change its behavior
LLDB windows build failure: https://lab.llvm.org/buildbot/#/builders/141/builds/3462
API test failed for remote platform in [llvm#112657](llvm#112657) Previously when putting files onto remote platform, I used `platform file write -d <data>` which actually required a `platform file open <path>` first in order to obtain a file descriptor. eg. in file [TestGDBRemotePlatformFile.py](https://github.com/llvm/llvm-project/blob/94e7d9c0bfe517507ea08b00fb00c32fb2837a82/lldb/test/API/functionalities/gdb_remote_client/TestGDBRemotePlatformFile.py#L24-L32) To fix this, use the `platform put-file` method, which is used in the `redirect_stdin` from this test already.
Disable part of the test failing on windows. as O_NOCTTY and O_RDONLY dont have same behavior on windows vs linux.
When
FileAction
opens file with write access, 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.For example, by using the settings
target.output-path
andtarget.error-path
, lldb will redirect process stdout/stderr to files. It then calls this function to write to the files which the above symptoms appear.Test