LLDB mainline
FileWindows.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
12
13#include <climits>
14#include <io.h>
15#include <mutex>
16#include <stdio.h>
17
18#include "lldb/Utility/Status.h"
19#include "llvm/Support/raw_ostream.h"
20
21using namespace lldb_private;
22
24 bool transfer_ownership)
25 : NativeFileBase(fh, options, transfer_ownership) {
26 HANDLE h = INVALID_HANDLE_VALUE;
27 if (fh == stdin)
28 h = ::GetStdHandle(STD_INPUT_HANDLE);
29 else if (fh == stdout)
30 h = ::GetStdHandle(STD_OUTPUT_HANDLE);
31 else if (fh == stderr)
32 h = ::GetStdHandle(STD_ERROR_HANDLE);
34 h != INVALID_HANDLE_VALUE && ::GetFileType(h) == FILE_TYPE_CHAR;
35}
36
38 bool transfer_ownership)
39 : NativeFileBase(fd, options, transfer_ownership) {
40 HANDLE h = INVALID_HANDLE_VALUE;
41 if (fd == STDIN_FILENO)
42 h = ::GetStdHandle(STD_INPUT_HANDLE);
43 else if (fd == STDOUT_FILENO)
44 h = ::GetStdHandle(STD_OUTPUT_HANDLE);
45 else if (fd == STDERR_FILENO)
46 h = ::GetStdHandle(STD_ERROR_HANDLE);
48 h != INVALID_HANDLE_VALUE && ::GetFileType(h) == FILE_TYPE_CHAR;
49}
50
52 const int fd = GetDescriptor();
53 if (!File::DescriptorIsValid(fd)) {
57 return;
58 }
61 if (_isatty(fd)) {
64#if defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
66#endif
67 }
68}
69
70int NativeFileWindows::Fileno(FILE *fh) const { return ::_fileno(fh); }
71
72int NativeFileWindows::Dup(int fd) const { return ::_dup(fd); }
73
77
80 if (ValueGuard descriptor_guard = DescriptorIsValid()) {
81 if (FlushFileBuffers((HANDLE)_get_osfhandle(m_descriptor)) == 0)
82 error = Status::FromErrorString("unknown error");
83 } else {
84 error = Status::FromErrorString("invalid file handle");
85 }
86 return error;
87}
88
90 size_t &num_bytes,
91 Status &error) {
93 return false;
94 // Bypass fwrite for console output: use raw_fd_ostream so that the Windows
95 // console renders non-ASCII characters via its UTF-16 path.
96 llvm::raw_fd_ostream(_fileno(m_stream), false)
97 .write((const char *)buf, num_bytes);
98 return true;
99}
100
101Status NativeFileWindows::Read(void *buf, size_t &num_bytes, off_t &offset) {
103
104 int fd = GetDescriptor();
105 if (fd != kInvalidDescriptor) {
106 // Win32 has no pread(); emulate it by saving the current offset, seeking,
107 // reading, and restoring.
108 std::lock_guard<std::mutex> guard(offset_access_mutex);
109 long cur = ::lseek(m_descriptor, 0, SEEK_CUR);
110 SeekFromStart(offset);
111 error = NativeFileBase::Read(buf, num_bytes);
112 if (!error.Fail())
113 SeekFromStart(cur);
114 } else {
115 num_bytes = 0;
116 error = Status::FromErrorString("invalid file handle");
117 }
118 return error;
119}
120
121Status NativeFileWindows::Write(const void *buf, size_t &num_bytes,
122 off_t &offset) {
124
125 int fd = GetDescriptor();
126 if (fd != kInvalidDescriptor) {
127 // Win32 has no pwrite(); same trick as Read above, but the post-write
128 // file position is what the caller wants reported back via `offset`.
129 std::lock_guard<std::mutex> guard(offset_access_mutex);
130 long cur = ::lseek(m_descriptor, 0, SEEK_CUR);
131 SeekFromStart(offset);
132 error = NativeFileBase::Write(buf, num_bytes);
133 long after = ::lseek(m_descriptor, 0, SEEK_CUR);
134
135 if (!error.Fail())
136 SeekFromStart(cur);
137
138 offset = after;
139 } else {
140 num_bytes = 0;
141 error = Status::FromErrorString("invalid file handle");
142 }
143 return error;
144}
145
146char NativeFileWindows::ID = 0;
static llvm::raw_ostream & error(Stream &strm)
void * HANDLE
static int kInvalidDescriptor
Definition FileBase.h:36
LazyBool m_is_real_terminal
Definition FileBase.h:368
LazyBool m_is_interactive
Definition FileBase.h:367
static bool DescriptorIsValid(int descriptor)
Definition FileBase.h:72
LazyBool m_supports_colors
Definition FileBase.h:369
lldb::file_t WaitableHandle
Definition IOObject.h:29
Status Write(const void *buf, size_t &num_bytes) override
Write bytes from buf to a file at the current file position.
Definition File.cpp:525
int GetDescriptor() const override
Get underlying OS file descriptor for this file, or kInvalidDescriptor.
Definition File.cpp:243
ValueGuard DescriptorIsValid() const
Definition FileBase.h:442
off_t SeekFromStart(off_t offset, Status *error_ptr=nullptr) override
Seek to an offset relative to the beginning of the file.
Definition File.cpp:342
Status Read(void *buf, size_t &num_bytes) override
Read bytes from a file from the current file position into buf.
Definition File.cpp:457
int Fileno(FILE *fh) const override
Map a stream to its underlying file descriptor.
Status Sync() override
Sync to disk.
void CalculateInteractiveAndTerminal() override
Refresh the cached interactive / terminal / color flags by inspecting the underlying descriptor.
bool TryWriteStreamUnlocked(const void *buf, size_t &num_bytes, Status &error) override
Hook for stream writes that bypass the default fwrite path.
Status Write(const void *src, size_t &num_bytes, off_t &offset) override
Write bytes to a file at the specified file offset.
WaitableHandle GetWaitableHandle() override
Get a handle that can be used for OS polling interfaces, such as WaitForMultipleObjects,...
Status Read(void *dst, size_t &num_bytes, off_t &offset) override
Read bytes from a file from the specified file offset.
bool m_is_windows_console
Set when this file wraps stdin/stdout/stderr connected to a console; triggers the raw_fd_ostream path...
Definition FileWindows.h:54
int Dup(int fd) const override
Duplicate a file descriptor.
An error handling class.
Definition Status.h:118
static Status FromErrorString(const char *str)
Definition Status.h:141
A class that represents a running process on the host machine.