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)
90 .Cases(
"w+",
"wb+",
"w+b",
93 .Cases(
"a+",
"ab+",
"a+b",
99 return llvm::createStringError(
100 llvm::inconvertibleErrorCode(),
101 "invalid mode, cannot convert to File::OpenOptions");
108 return std::error_code(ENOTSUP, std::system_category());
111 return std::error_code(ENOTSUP, std::system_category());
124 return std::error_code(ENOTSUP, std::system_category());
133 *error_ptr = std::error_code(ENOTSUP, std::system_category());
139 *error_ptr = std::error_code(ENOTSUP, std::system_category());
145 *error_ptr = std::error_code(ENOTSUP, std::system_category());
150 return std::error_code(ENOTSUP, std::system_category());
154 return std::error_code(ENOTSUP, std::system_category());
175#if defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
182 struct winsize window_size;
183 if (::ioctl(fd, TIOCGWINSZ, &window_size) == 0) {
184 if (window_size.ws_col > 0) {
186 if (llvm::sys::Process::FileDescriptorHasColors(fd))
214 va_start(args, format);
221 llvm::SmallString<0> s;
223 size_t written = s.size();
224 Write(s.data(), written);
231 return llvm::createStringError(
232 llvm::inconvertibleErrorCode(),
233 "GetOptions() not implemented for this File class");
239 error = std::error_code(ENOTSUP, std::system_category());
242 struct stat file_stats;
243 if (::fstat(fd, &file_stats) == -1) {
259 int fd = _fileno(fh);
261 ::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR;
273 ::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR;
317 llvm::consumeError(mode.takeError());
390 file_spec.
SetFile(path, FileSpec::Style::native);
394#elif defined(__linux__)
397 if (::snprintf(proc,
sizeof(proc),
"/proc/self/fd/%d",
GetDescriptor()) < 0)
401 if ((len = ::readlink(proc, path,
sizeof(path) - 1)) == -1)
405 file_spec.
SetFile(path, FileSpec::Style::native);
410 "NativeFile::GetFileSpec is not supported on this platform");
433 result = ::fseek(
m_stream, offset, SEEK_SET);
464 result = ::fseek(
m_stream, offset, SEEK_CUR);
495 result = ::fseek(
m_stream, offset, SEEK_END);
513 if (llvm::sys::RetryAfterSignal(EOF, ::fflush,
m_stream) == EOF)
520 if (!descriptor_guard)
530 int err = FlushFileBuffers((HANDLE)_get_osfhandle(
m_descriptor));
534 if (llvm::sys::RetryAfterSignal(-1, ::fsync,
m_descriptor) == -1)
543#if defined(__APPLE__)
545#define MAX_READ_SIZE INT_MAX
546#define MAX_WRITE_SIZE INT_MAX
552#if defined(MAX_READ_SIZE)
553 if (num_bytes > MAX_READ_SIZE) {
554 uint8_t *p = (uint8_t *)buf;
555 size_t bytes_left = num_bytes;
559 while (bytes_left > 0) {
560 size_t curr_num_bytes;
561 if (bytes_left > MAX_READ_SIZE)
562 curr_num_bytes = MAX_READ_SIZE;
564 curr_num_bytes = bytes_left;
566 error =
Read(p + num_bytes, curr_num_bytes);
569 num_bytes += curr_num_bytes;
570 if (bytes_left < curr_num_bytes)
573 bytes_left -= curr_num_bytes;
582 ssize_t bytes_read = -1;
585 llvm::sys::RetryAfterSignal(-1, ::read,
m_descriptor, buf, num_bytes);
586 if (bytes_read == -1) {
590 num_bytes = bytes_read;
595 bytes_read = ::fread(buf, 1, num_bytes,
m_stream);
597 if (bytes_read == 0) {
604 num_bytes = bytes_read;
616#if defined(MAX_WRITE_SIZE)
617 if (num_bytes > MAX_WRITE_SIZE) {
618 const uint8_t *p = (
const uint8_t *)buf;
619 size_t bytes_left = num_bytes;
623 while (bytes_left > 0) {
624 size_t curr_num_bytes;
625 if (bytes_left > MAX_WRITE_SIZE)
626 curr_num_bytes = MAX_WRITE_SIZE;
628 curr_num_bytes = bytes_left;
633 num_bytes += curr_num_bytes;
634 if (bytes_left < curr_num_bytes)
637 bytes_left -= curr_num_bytes;
646 ssize_t bytes_written = -1;
649 llvm::sys::RetryAfterSignal(-1, ::write,
m_descriptor, buf, num_bytes);
650 if (bytes_written == -1) {
654 num_bytes = bytes_written;
661 llvm::raw_fd_ostream(_fileno(
m_stream),
false)
662 .write((
const char *)buf, num_bytes);
666 bytes_written = ::fwrite(buf, 1, num_bytes,
m_stream);
668 if (bytes_written == 0) {
675 num_bytes = bytes_written;
687#if defined(MAX_READ_SIZE)
688 if (num_bytes > MAX_READ_SIZE) {
689 uint8_t *p = (uint8_t *)buf;
690 size_t bytes_left = num_bytes;
694 while (bytes_left > 0) {
695 size_t curr_num_bytes;
696 if (bytes_left > MAX_READ_SIZE)
697 curr_num_bytes = MAX_READ_SIZE;
699 curr_num_bytes = bytes_left;
701 error =
Read(p + num_bytes, curr_num_bytes, offset);
704 num_bytes += curr_num_bytes;
705 if (bytes_left < curr_num_bytes)
708 bytes_left -= curr_num_bytes;
721 llvm::sys::RetryAfterSignal(-1, ::pread, fd, buf, num_bytes, offset);
722 if (bytes_read < 0) {
726 offset += bytes_read;
727 num_bytes = bytes_read;
747#if defined(MAX_WRITE_SIZE)
748 if (num_bytes > MAX_WRITE_SIZE) {
749 const uint8_t *p = (
const uint8_t *)buf;
750 size_t bytes_left = num_bytes;
754 while (bytes_left > 0) {
755 size_t curr_num_bytes;
756 if (bytes_left > MAX_WRITE_SIZE)
757 curr_num_bytes = MAX_WRITE_SIZE;
759 curr_num_bytes = bytes_left;
761 error =
Write(p + num_bytes, curr_num_bytes, offset);
764 num_bytes += curr_num_bytes;
765 if (bytes_left < curr_num_bytes)
768 bytes_left -= curr_num_bytes;
780 ssize_t bytes_written =
781 llvm::sys::RetryAfterSignal(-1, ::pwrite,
m_descriptor, buf, num_bytes, offset);
782 if (bytes_written < 0) {
786 offset += bytes_written;
787 num_bytes = bytes_written;
810 return ::vfprintf(
m_stream, format, args);
838 mode |= O_CREAT | O_EXCL;
845llvm::Expected<SerialPort::Options>
848 for (llvm::StringRef x : llvm::split(urlqs,
'&')) {
849 if (x.consume_front(
"baud=")) {
850 unsigned int baud_rate;
851 if (!llvm::to_integer(x, baud_rate, 10))
852 return llvm::createStringError(llvm::inconvertibleErrorCode(),
853 "Invalid baud rate: %s",
855 serial_options.
BaudRate = baud_rate;
856 }
else if (x.consume_front(
"parity=")) {
858 llvm::StringSwitch<std::optional<Terminal::Parity>>(x)
864 .Default(std::nullopt);
865 if (!serial_options.
Parity)
866 return llvm::createStringError(
867 llvm::inconvertibleErrorCode(),
868 "Invalid parity (must be no, even, odd, mark or space): %s",
870 }
else if (x.consume_front(
"parity-check=")) {
872 llvm::StringSwitch<std::optional<Terminal::ParityCheck>>(x)
879 .Default(std::nullopt);
881 return llvm::createStringError(
882 llvm::inconvertibleErrorCode(),
883 "Invalid parity-check (must be no, replace, ignore or mark): %s",
885 }
else if (x.consume_front(
"stop-bits=")) {
886 unsigned int stop_bits;
887 if (!llvm::to_integer(x, stop_bits, 10) ||
888 (stop_bits != 1 && stop_bits != 2))
889 return llvm::createStringError(
890 llvm::inconvertibleErrorCode(),
891 "Invalid stop bit number (must be 1 or 2): %s", x.str().c_str());
892 serial_options.
StopBits = stop_bits;
894 return llvm::createStringError(llvm::inconvertibleErrorCode(),
895 "Unknown parameter: %s", x.str().c_str());
897 return serial_options;
900llvm::Expected<std::unique_ptr<SerialPort>>
902 bool transfer_ownership) {
903 std::unique_ptr<SerialPort> out{
904 new SerialPort(fd, options, serial_options, transfer_ownership)};
906 if (!out->GetIsInteractive())
907 return llvm::createStringError(llvm::inconvertibleErrorCode(),
908 "the specified file is not a teletype");
912 return std::move(
error);
915 return std::move(
error);
917 if (serial_options.
Parity) {
919 return std::move(
error);
923 return std::move(
error);
927 return std::move(
error);
930 return std::move(out);
935 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