-
Notifications
You must be signed in to change notification settings - Fork 17.9k
[lldb][windows] use Windows APIs to print to the console #149493
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
Changes from 7 commits
77bf2b7
fcec07f
3e58744
01bf62f
3291838
ae790f8
7f88778
76a8a4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ | |
| #include "llvm/Support/Errno.h" | ||
| #include "llvm/Support/FileSystem.h" | ||
| #include "llvm/Support/Process.h" | ||
| #include "llvm/Support/raw_ostream.h" | ||
|
|
||
| using namespace lldb; | ||
| using namespace lldb_private; | ||
|
|
@@ -247,6 +248,26 @@ uint32_t File::GetPermissions(Status &error) const { | |
| return file_stats.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); | ||
| } | ||
|
|
||
| NativeFile::NativeFile() = default; | ||
|
|
||
| NativeFile::NativeFile(FILE *fh, bool transfer_ownership) | ||
| : m_stream(fh), m_own_stream(transfer_ownership) { | ||
| #ifdef _WIN32 | ||
| int fd = _fileno(fh); | ||
| is_windows_console = | ||
| ::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR; | ||
| #endif | ||
| } | ||
|
|
||
| NativeFile::NativeFile(int fd, OpenOptions options, bool transfer_ownership) | ||
| : m_descriptor(fd), m_own_descriptor(transfer_ownership), | ||
| m_options(options) { | ||
| #ifdef _WIN32 | ||
| is_windows_console = | ||
| ::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR; | ||
| #endif | ||
| } | ||
|
Comment on lines
+254
to
+275
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you not just delegate to the fd constructor? Simply inline the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That isn't quite equivalent as the implementation (for better or worse), remembers whether it was constructed with a FD or a FILE* and then uses the appropriate method to implement read/write operations. |
||
|
|
||
| bool NativeFile::IsValid() const { | ||
| std::scoped_lock<std::mutex, std::mutex> lock(m_descriptor_mutex, m_stream_mutex); | ||
| return DescriptorIsValidUnlocked() || StreamIsValidUnlocked(); | ||
|
|
@@ -618,6 +639,12 @@ Status NativeFile::Write(const void *buf, size_t &num_bytes) { | |
|
|
||
| ssize_t bytes_written = -1; | ||
| if (ValueGuard descriptor_guard = DescriptorIsValid()) { | ||
| #ifdef _WIN32 | ||
| if (is_windows_console) { | ||
| llvm::raw_fd_ostream(m_descriptor, false).write((char *)buf, num_bytes); | ||
| return error; | ||
| } | ||
| #endif | ||
| bytes_written = | ||
| llvm::sys::RetryAfterSignal(-1, ::write, m_descriptor, buf, num_bytes); | ||
| if (bytes_written == -1) { | ||
|
|
@@ -629,6 +656,13 @@ Status NativeFile::Write(const void *buf, size_t &num_bytes) { | |
| } | ||
|
|
||
| if (ValueGuard stream_guard = StreamIsValid()) { | ||
| #ifdef _WIN32 | ||
| if (is_windows_console) { | ||
| llvm::raw_fd_ostream(_fileno(m_stream), false) | ||
| .write((char *)buf, num_bytes); | ||
| return error; | ||
| } | ||
| #endif | ||
| bytes_written = ::fwrite(buf, 1, num_bytes, m_stream); | ||
|
|
||
| if (bytes_written == 0) { | ||
|
|
||
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.
Could you add a comment here explains why we do something special on Windows and (just once sentence) on why/how this works?