27#include "lldb/Host/Config.h"
34#include "llvm/ADT/StringExtras.h"
35#include "llvm/Support/ConvertUTF.h"
36#include "llvm/Support/Errno.h"
37#include "llvm/Support/FileSystem.h"
38#include "llvm/Support/Process.h"
39#include "llvm/Support/raw_ostream.h"
76 return llvm::createStringError(
77 llvm::inconvertibleErrorCode(),
78 "invalid options, cannot convert to mode string");
83 llvm::StringSwitch<OpenOptions>(mode)
98 return llvm::createStringError(
99 llvm::inconvertibleErrorCode(),
100 "invalid mode, cannot convert to File::OpenOptions");
107 return std::error_code(ENOTSUP, std::system_category());
110 return std::error_code(ENOTSUP, std::system_category());
123 return std::error_code(ENOTSUP, std::system_category());
132 *error_ptr = std::error_code(ENOTSUP, std::system_category());
138 *error_ptr = std::error_code(ENOTSUP, std::system_category());
144 *error_ptr = std::error_code(ENOTSUP, std::system_category());
149 return std::error_code(ENOTSUP, std::system_category());
153 return std::error_code(ENOTSUP, std::system_category());
174#if defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
181 struct winsize window_size;
182 if (::ioctl(fd, TIOCGWINSZ, &window_size) == 0) {
183 if (window_size.ws_col > 0) {
185 if (llvm::sys::Process::FileDescriptorHasColors(fd))
213 va_start(args, format);
220 llvm::SmallString<0> s;
222 size_t written = s.size();
223 Write(s.data(), written);
230 return llvm::createStringError(
231 llvm::inconvertibleErrorCode(),
232 "GetOptions() not implemented for this File class");
238 error = std::error_code(ENOTSUP, std::system_category());
241 struct stat file_stats;
242 if (::fstat(fd, &file_stats) == -1) {
258 int fd = _fileno(fh);
260 ::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR;
272 ::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR;
316 llvm::consumeError(mode.takeError());
389 file_spec.
SetFile(path, FileSpec::Style::native);
393#elif defined(__linux__)
396 if (::snprintf(proc,
sizeof(proc),
"/proc/self/fd/%d",
GetDescriptor()) < 0)
400 if ((len = ::readlink(proc, path,
sizeof(path) - 1)) == -1)
404 file_spec.
SetFile(path, FileSpec::Style::native);
409 "NativeFile::GetFileSpec is not supported on this platform");
432 result = ::fseek(
m_stream, offset, SEEK_SET);
463 result = ::fseek(
m_stream, offset, SEEK_CUR);
494 result = ::fseek(
m_stream, offset, SEEK_END);
512 if (llvm::sys::RetryAfterSignal(EOF, ::fflush,
m_stream) == EOF)
519 if (!descriptor_guard)
529 int err = FlushFileBuffers((HANDLE)_get_osfhandle(
m_descriptor));
533 if (llvm::sys::RetryAfterSignal(-1, ::fsync,
m_descriptor) == -1)
542#if defined(__APPLE__)
544#define MAX_READ_SIZE INT_MAX
545#define MAX_WRITE_SIZE INT_MAX
551#if defined(MAX_READ_SIZE)
552 if (num_bytes > MAX_READ_SIZE) {
553 uint8_t *p = (uint8_t *)buf;
554 size_t bytes_left = num_bytes;
558 while (bytes_left > 0) {
559 size_t curr_num_bytes;
560 if (bytes_left > MAX_READ_SIZE)
561 curr_num_bytes = MAX_READ_SIZE;
563 curr_num_bytes = bytes_left;
565 error =
Read(p + num_bytes, curr_num_bytes);
568 num_bytes += curr_num_bytes;
569 if (bytes_left < curr_num_bytes)
572 bytes_left -= curr_num_bytes;
581 ssize_t bytes_read = -1;
584 llvm::sys::RetryAfterSignal(-1, ::read,
m_descriptor, buf, num_bytes);
585 if (bytes_read == -1) {
589 num_bytes = bytes_read;
594 bytes_read = ::fread(buf, 1, num_bytes,
m_stream);
596 if (bytes_read == 0) {
603 num_bytes = bytes_read;
615#if defined(MAX_WRITE_SIZE)
616 if (num_bytes > MAX_WRITE_SIZE) {
617 const uint8_t *p = (
const uint8_t *)buf;
618 size_t bytes_left = num_bytes;
622 while (bytes_left > 0) {
623 size_t curr_num_bytes;
624 if (bytes_left > MAX_WRITE_SIZE)
625 curr_num_bytes = MAX_WRITE_SIZE;
627 curr_num_bytes = bytes_left;
632 num_bytes += curr_num_bytes;
633 if (bytes_left < curr_num_bytes)
636 bytes_left -= curr_num_bytes;
645 ssize_t bytes_written = -1;
648 llvm::sys::RetryAfterSignal(-1, ::write,
m_descriptor, buf, num_bytes);
649 if (bytes_written == -1) {
653 num_bytes = bytes_written;
660 llvm::raw_fd_ostream(_fileno(
m_stream),
false)
661 .write((
const char *)buf, num_bytes);
665 bytes_written = ::fwrite(buf, 1, num_bytes,
m_stream);
667 if (bytes_written == 0) {
674 num_bytes = bytes_written;
686#if defined(MAX_READ_SIZE)
687 if (num_bytes > MAX_READ_SIZE) {
688 uint8_t *p = (uint8_t *)buf;
689 size_t bytes_left = num_bytes;
693 while (bytes_left > 0) {
694 size_t curr_num_bytes;
695 if (bytes_left > MAX_READ_SIZE)
696 curr_num_bytes = MAX_READ_SIZE;
698 curr_num_bytes = bytes_left;
700 error =
Read(p + num_bytes, curr_num_bytes, offset);
703 num_bytes += curr_num_bytes;
704 if (bytes_left < curr_num_bytes)
707 bytes_left -= curr_num_bytes;
720 llvm::sys::RetryAfterSignal(-1, ::pread, fd, buf, num_bytes, offset);
721 if (bytes_read < 0) {
725 offset += bytes_read;
726 num_bytes = bytes_read;
746#if defined(MAX_WRITE_SIZE)
747 if (num_bytes > MAX_WRITE_SIZE) {
748 const uint8_t *p = (
const uint8_t *)buf;
749 size_t bytes_left = num_bytes;
753 while (bytes_left > 0) {
754 size_t curr_num_bytes;
755 if (bytes_left > MAX_WRITE_SIZE)
756 curr_num_bytes = MAX_WRITE_SIZE;
758 curr_num_bytes = bytes_left;
760 error =
Write(p + num_bytes, curr_num_bytes, offset);
763 num_bytes += curr_num_bytes;
764 if (bytes_left < curr_num_bytes)
767 bytes_left -= curr_num_bytes;
779 ssize_t bytes_written =
780 llvm::sys::RetryAfterSignal(-1, ::pwrite,
m_descriptor, buf, num_bytes, offset);
781 if (bytes_written < 0) {
785 offset += bytes_written;
786 num_bytes = bytes_written;
809 return ::vfprintf(
m_stream, format, args);
837 mode |= O_CREAT | O_EXCL;
844llvm::Expected<SerialPort::Options>
847 for (llvm::StringRef x : llvm::split(urlqs,
'&')) {
848 if (x.consume_front(
"baud=")) {
849 unsigned int baud_rate;
850 if (!llvm::to_integer(x, baud_rate, 10))
851 return llvm::createStringError(llvm::inconvertibleErrorCode(),
852 "Invalid baud rate: %s",
854 serial_options.
BaudRate = baud_rate;
855 }
else if (x.consume_front(
"parity=")) {
857 llvm::StringSwitch<std::optional<Terminal::Parity>>(x)
863 .Default(std::nullopt);
864 if (!serial_options.
Parity)
865 return llvm::createStringError(
866 llvm::inconvertibleErrorCode(),
867 "Invalid parity (must be no, even, odd, mark or space): %s",
869 }
else if (x.consume_front(
"parity-check=")) {
871 llvm::StringSwitch<std::optional<Terminal::ParityCheck>>(x)
878 .Default(std::nullopt);
880 return llvm::createStringError(
881 llvm::inconvertibleErrorCode(),
882 "Invalid parity-check (must be no, replace, ignore or mark): %s",
884 }
else if (x.consume_front(
"stop-bits=")) {
885 unsigned int stop_bits;
886 if (!llvm::to_integer(x, stop_bits, 10) ||
887 (stop_bits != 1 && stop_bits != 2))
888 return llvm::createStringError(
889 llvm::inconvertibleErrorCode(),
890 "Invalid stop bit number (must be 1 or 2): %s", x.str().c_str());
891 serial_options.
StopBits = stop_bits;
893 return llvm::createStringError(llvm::inconvertibleErrorCode(),
894 "Unknown parameter: %s", x.str().c_str());
896 return serial_options;
899llvm::Expected<std::unique_ptr<SerialPort>>
901 bool transfer_ownership) {
902 std::unique_ptr<SerialPort> out{
903 new SerialPort(fd, options, serial_options, transfer_ownership)};
905 if (!out->GetIsInteractive())
906 return llvm::createStringError(llvm::inconvertibleErrorCode(),
907 "the specified file is not a teletype");
911 return std::move(
error);
914 return std::move(
error);
916 if (serial_options.
Parity) {
918 return std::move(
error);
922 return std::move(
error);
926 return std::move(
error);
929 return std::move(out);
934 bool transfer_ownership)
static llvm::raw_ostream & error(Stream &strm)
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
void Clear()
Clears the object state.
virtual llvm::Expected< OpenOptions > GetOptions() const
Return the OpenOptions for this file.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
WaitableHandle GetWaitableHandle() override
Get a handle that can be used for OS polling interfaces, such as WaitForMultipleObjects,...
bool GetIsRealTerminal()
Return true if this file from a real terminal.
virtual FILE * GetStream()
Get the underlying libc stream for this file, or NULL.
Status Read(void *buf, size_t &num_bytes) override
Read bytes from a file from the current file position into buf.
virtual off_t SeekFromStart(off_t offset, Status *error_ptr=nullptr)
Seek to an offset relative to the beginning of the file.
static int kInvalidDescriptor
static FILE * kInvalidStream
virtual Status GetFileSpec(FileSpec &file_spec) const
Get the file specification for this file, if possible.
LazyBool m_is_real_terminal
virtual off_t SeekFromCurrent(off_t offset, Status *error_ptr=nullptr)
Seek to an offset relative to the current file position.
LazyBool m_is_interactive
static bool DescriptorIsValid(int descriptor)
static mode_t ConvertOpenOptionsForPOSIXOpen(OpenOptions open_options)
virtual int GetDescriptor() const
Get underlying OS file descriptor for this file, or kInvalidDescriptor.
static llvm::Expected< const char * > GetStreamOpenModeFromOptions(OpenOptions options)
bool GetIsTerminalWithColors()
Return true if this file is a terminal which supports colors.
Status Close() override
Flush any buffers and release any resources owned by the file.
Status Write(const void *buf, size_t &num_bytes) override
Write bytes from buf to a file at the current file position.
@ eOpenOptionCanCreateNewOnly
uint32_t GetPermissions(Status &error) const
Get the permissions for a this file.
bool IsValid() const override
IsValid.
virtual Status Flush()
Flush the current stream.
static llvm::Expected< OpenOptions > GetOptionsFromMode(llvm::StringRef mode)
size_t virtual size_t PrintfVarArg(const char *format, va_list args)
Output printf formatted output to the stream.
virtual Status Sync()
Sync to disk.
LazyBool m_supports_colors
bool GetIsInteractive()
Return true if this file is interactive.
void CalculateInteractiveAndTerminal()
virtual off_t SeekFromEnd(off_t offset, Status *error_ptr=nullptr)
Seek to an offset relative to the end of the file.
static const WaitableHandle kInvalidHandleValue
lldb::file_t WaitableHandle
bool DescriptorIsValidUnlocked() const
off_t SeekFromStart(off_t offset, Status *error_ptr=nullptr) override
Seek to an offset relative to the beginning of the file.
Status GetFileSpec(FileSpec &file_spec) const override
Get the file specification for this file, if possible.
FILE * GetStream() override
Get the underlying libc stream for this file, or NULL.
Status Sync() override
Sync to disk.
std::mutex offset_access_mutex
Status Close() override
Flush any buffers and release any resources owned by the file.
WaitableHandle GetWaitableHandle() override
Get a handle that can be used for OS polling interfaces, such as WaitForMultipleObjects,...
Status Flush() override
Flush the current stream.
bool StreamIsValidUnlocked() const
ValueGuard DescriptorIsValid() const
llvm::Expected< OpenOptions > GetOptions() const override
Return the OpenOptions for this file.
off_t SeekFromCurrent(off_t offset, Status *error_ptr=nullptr) override
Seek to an offset relative to the current file position.
size_t PrintfVarArg(const char *format, va_list args) override
Output printf formatted output to the stream.
Status Write(const void *buf, size_t &num_bytes) override
Write bytes from buf to a file at the current file position.
int GetDescriptor() const override
Get underlying OS file descriptor for this file, or kInvalidDescriptor.
Status Read(void *buf, size_t &num_bytes) override
Read bytes from a file from the current file position into buf.
bool IsValid() const override
IsValid.
std::mutex m_descriptor_mutex
std::mutex m_stream_mutex
ValueGuard StreamIsValid() const
off_t SeekFromEnd(off_t offset, Status *error_ptr=nullptr) override
Seek to an offset relative to the end of the file.
static llvm::Expected< std::unique_ptr< SerialPort > > Create(int fd, OpenOptions options, Options serial_options, bool transfer_ownership)
SerialPort(int fd, OpenOptions options, Options serial_options, bool transfer_ownership)
Status Close() override
Flush any buffers and release any resources owned by the file.
static llvm::Expected< Options > OptionsFromURL(llvm::StringRef urlqs)
void Clear()
Clear the object state.
static Status FromErrno()
Set the current error to errno.
static Status FromErrorString(const char *str)
llvm::Error SetParityCheck(ParityCheck parity_check)
llvm::Error SetBaudRate(unsigned int baud_rate)
llvm::Error SetParity(Parity parity)
llvm::Error SetStopBits(unsigned int stop_bits)
A class that represents a running process on the host machine.
bool VASprintf(llvm::SmallVectorImpl< char > &buf, const char *fmt, va_list args)
std::optional< Terminal::ParityCheck > ParityCheck
std::optional< unsigned int > StopBits
std::optional< Terminal::Parity > Parity
std::optional< unsigned int > BaudRate