[lldb][NFC] Fix all formatting errors in .cpp file headers
Summary:
A *.cpp file header in LLDB (and in LLDB) should like this:
```
//===-- TestUtilities.cpp -------------------------------------------------===//
```
However in LLDB most of our source files have arbitrary changes to this format and
these changes are spreading through LLDB as folks usually just use the existing
source files as templates for their new files (most notably the unnecessary
editor language indicator `-*- C++ -*-` is spreading and in every review
someone is pointing out that this is wrong, resulting in people pointing out that this
is done in the same way in other files).
This patch removes most of these inconsistencies including the editor language indicators,
all the different missing/additional '-' characters, files that center the file name, missing
trailing `===//` (mostly caused by clang-format breaking the line).
Reviewers: aprantl, espindola, jfb, shafik, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: dexonsmith, wuzish, emaste, sdardis, nemanjai, kbarton, MaskRay, atanasyan, arphaman, jfb, abidh, jsji, JDevlieghere, usaxena95, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D73258
2020-01-24 15:23:27 +08:00
|
|
|
//===-- PipePosix.cpp -----------------------------------------------------===//
|
2014-07-03 05:10:39 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-07-03 05:10:39 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-10-09 04:38:41 +08:00
|
|
|
#include "lldb/Host/posix/PipePosix.h"
|
2015-02-06 00:29:12 +08:00
|
|
|
#include "lldb/Host/HostInfo.h"
|
2016-08-11 06:43:48 +08:00
|
|
|
#include "lldb/Utility/SelectHelper.h"
|
2015-02-06 00:29:12 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2019-03-22 03:35:55 +08:00
|
|
|
#include "llvm/Support/Errno.h"
|
2015-02-06 00:29:12 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2015-01-14 07:19:40 +08:00
|
|
|
#include <functional>
|
|
|
|
#include <thread>
|
2014-07-03 05:10:39 +08:00
|
|
|
|
2014-12-18 02:02:19 +08:00
|
|
|
#include <errno.h>
|
2014-11-22 00:18:57 +08:00
|
|
|
#include <fcntl.h>
|
2015-02-06 00:29:12 +08:00
|
|
|
#include <limits.h>
|
2015-01-14 07:19:40 +08:00
|
|
|
#include <sys/stat.h>
|
2014-12-18 02:02:19 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2014-07-03 05:10:39 +08:00
|
|
|
|
2014-12-18 02:02:19 +08:00
|
|
|
using namespace lldb;
|
2014-07-03 05:10:39 +08:00
|
|
|
using namespace lldb_private;
|
|
|
|
|
2014-12-18 02:02:19 +08:00
|
|
|
int PipePosix::kInvalidDescriptor = -1;
|
2014-07-03 05:10:39 +08:00
|
|
|
|
|
|
|
enum PIPES { READ, WRITE }; // Constants 0 and 1 for READ and WRITE
|
|
|
|
|
2015-09-14 23:12:49 +08:00
|
|
|
// pipe2 is supported by a limited set of platforms
|
2014-11-22 00:18:57 +08:00
|
|
|
// TODO: Add more platforms that support pipe2.
|
2015-09-14 23:12:49 +08:00
|
|
|
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD__ >= 10) || \
|
|
|
|
defined(__NetBSD__)
|
2014-12-18 02:02:19 +08:00
|
|
|
#define PIPE2_SUPPORTED 1
|
|
|
|
#else
|
|
|
|
#define PIPE2_SUPPORTED 0
|
|
|
|
#endif
|
2014-11-22 00:18:57 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-01-14 07:19:40 +08:00
|
|
|
constexpr auto OPEN_WRITER_SLEEP_TIMEOUT_MSECS = 100;
|
|
|
|
|
2014-11-22 00:18:57 +08:00
|
|
|
#if defined(FD_CLOEXEC) && !PIPE2_SUPPORTED
|
|
|
|
bool SetCloexecFlag(int fd) {
|
|
|
|
int flags = ::fcntl(fd, F_GETFD);
|
|
|
|
if (flags == -1)
|
|
|
|
return false;
|
|
|
|
return (::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-14 07:19:40 +08:00
|
|
|
std::chrono::time_point<std::chrono::steady_clock> Now() {
|
|
|
|
return std::chrono::steady_clock::now();
|
|
|
|
}
|
2019-01-10 08:46:09 +08:00
|
|
|
} // namespace
|
2014-11-22 00:18:57 +08:00
|
|
|
|
2014-12-18 02:02:19 +08:00
|
|
|
PipePosix::PipePosix()
|
2015-05-02 00:49:23 +08:00
|
|
|
: m_fds{PipePosix::kInvalidDescriptor, PipePosix::kInvalidDescriptor} {}
|
2015-04-30 01:36:58 +08:00
|
|
|
|
2019-01-10 08:46:09 +08:00
|
|
|
PipePosix::PipePosix(lldb::pipe_t read, lldb::pipe_t write)
|
|
|
|
: m_fds{read, write} {}
|
2014-07-03 05:10:39 +08:00
|
|
|
|
2015-05-02 00:49:23 +08:00
|
|
|
PipePosix::PipePosix(PipePosix &&pipe_posix)
|
|
|
|
: PipeBase{std::move(pipe_posix)},
|
|
|
|
m_fds{pipe_posix.ReleaseReadFileDescriptor(),
|
|
|
|
pipe_posix.ReleaseWriteFileDescriptor()} {}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-05-02 00:49:23 +08:00
|
|
|
PipePosix &PipePosix::operator=(PipePosix &&pipe_posix) {
|
|
|
|
PipeBase::operator=(std::move(pipe_posix));
|
|
|
|
m_fds[READ] = pipe_posix.ReleaseReadFileDescriptor();
|
|
|
|
m_fds[WRITE] = pipe_posix.ReleaseWriteFileDescriptor();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-12-18 02:02:19 +08:00
|
|
|
PipePosix::~PipePosix() { Close(); }
|
2014-07-03 05:10:39 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status PipePosix::CreateNew(bool child_processes_inherit) {
|
2014-12-18 02:02:19 +08:00
|
|
|
if (CanRead() || CanWrite())
|
2017-05-12 12:51:55 +08:00
|
|
|
return Status(EINVAL, eErrorTypePOSIX);
|
2014-07-03 05:10:39 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2014-11-22 00:18:57 +08:00
|
|
|
#if PIPE2_SUPPORTED
|
|
|
|
if (::pipe2(m_fds, (child_processes_inherit) ? 0 : O_CLOEXEC) == 0)
|
2014-12-18 02:02:19 +08:00
|
|
|
return error;
|
2014-11-22 00:18:57 +08:00
|
|
|
#else
|
2014-07-03 05:10:39 +08:00
|
|
|
if (::pipe(m_fds) == 0) {
|
2014-11-22 00:18:57 +08:00
|
|
|
#ifdef FD_CLOEXEC
|
|
|
|
if (!child_processes_inherit) {
|
|
|
|
if (!SetCloexecFlag(m_fds[0]) || !SetCloexecFlag(m_fds[1])) {
|
2014-12-18 02:02:19 +08:00
|
|
|
error.SetErrorToErrno();
|
2014-11-22 00:18:57 +08:00
|
|
|
Close();
|
2014-12-18 02:02:19 +08:00
|
|
|
return error;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-11-22 00:18:57 +08:00
|
|
|
}
|
|
|
|
#endif
|
2014-12-18 02:02:19 +08:00
|
|
|
return error;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-14 07:19:40 +08:00
|
|
|
error.SetErrorToErrno();
|
2014-12-18 02:02:19 +08:00
|
|
|
m_fds[READ] = PipePosix::kInvalidDescriptor;
|
|
|
|
m_fds[WRITE] = PipePosix::kInvalidDescriptor;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status PipePosix::CreateNew(llvm::StringRef name, bool child_process_inherit) {
|
2014-12-18 02:02:19 +08:00
|
|
|
if (CanRead() || CanWrite())
|
2017-05-12 12:51:55 +08:00
|
|
|
return Status("Pipe is already opened");
|
2015-01-14 07:19:40 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2021-01-09 03:37:09 +08:00
|
|
|
if (::mkfifo(name.str().c_str(), 0660) != 0)
|
2015-01-14 07:19:40 +08:00
|
|
|
error.SetErrorToErrno();
|
|
|
|
|
2014-12-18 02:02:19 +08:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status PipePosix::CreateWithUniqueName(llvm::StringRef prefix,
|
|
|
|
bool child_process_inherit,
|
|
|
|
llvm::SmallVectorImpl<char> &name) {
|
2018-12-11 01:23:28 +08:00
|
|
|
llvm::SmallString<128> named_pipe_path;
|
|
|
|
llvm::SmallString<128> pipe_spec((prefix + ".%%%%%%").str());
|
2018-06-19 23:09:07 +08:00
|
|
|
FileSpec tmpdir_file_spec = HostInfo::GetProcessTempDir();
|
|
|
|
if (!tmpdir_file_spec)
|
2015-02-06 00:29:12 +08:00
|
|
|
tmpdir_file_spec.AppendPathComponent("/tmp");
|
2018-06-19 23:09:07 +08:00
|
|
|
tmpdir_file_spec.AppendPathComponent(pipe_spec);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-02-06 00:29:12 +08:00
|
|
|
// It's possible that another process creates the target path after we've
|
2018-05-01 00:49:04 +08:00
|
|
|
// verified it's available but before we create it, in which case we should
|
|
|
|
// try again.
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2015-02-06 00:29:12 +08:00
|
|
|
do {
|
2021-01-09 03:37:09 +08:00
|
|
|
llvm::sys::fs::createUniquePath(tmpdir_file_spec.GetPath(), named_pipe_path,
|
|
|
|
/*MakeAbsolute=*/false);
|
2015-02-06 00:29:12 +08:00
|
|
|
error = CreateNew(named_pipe_path, child_process_inherit);
|
|
|
|
} while (error.GetError() == EEXIST);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-02-06 00:29:12 +08:00
|
|
|
if (error.Success())
|
|
|
|
name = named_pipe_path;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status PipePosix::OpenAsReader(llvm::StringRef name,
|
|
|
|
bool child_process_inherit) {
|
2014-12-18 02:02:19 +08:00
|
|
|
if (CanRead() || CanWrite())
|
2017-05-12 12:51:55 +08:00
|
|
|
return Status("Pipe is already opened");
|
2015-01-14 07:19:40 +08:00
|
|
|
|
|
|
|
int flags = O_RDONLY | O_NONBLOCK;
|
|
|
|
if (!child_process_inherit)
|
|
|
|
flags |= O_CLOEXEC;
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2021-01-09 03:37:09 +08:00
|
|
|
int fd = llvm::sys::RetryAfterSignal(-1, ::open, name.str().c_str(), flags);
|
2015-01-14 07:19:40 +08:00
|
|
|
if (fd != -1)
|
|
|
|
m_fds[READ] = fd;
|
2014-12-18 02:02:19 +08:00
|
|
|
else
|
2015-01-14 07:19:40 +08:00
|
|
|
error.SetErrorToErrno();
|
|
|
|
|
2014-12-18 02:02:19 +08:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status
|
|
|
|
PipePosix::OpenAsWriterWithTimeout(llvm::StringRef name,
|
|
|
|
bool child_process_inherit,
|
|
|
|
const std::chrono::microseconds &timeout) {
|
2014-12-18 02:02:19 +08:00
|
|
|
if (CanRead() || CanWrite())
|
2017-05-12 12:51:55 +08:00
|
|
|
return Status("Pipe is already opened");
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-01-14 07:19:40 +08:00
|
|
|
int flags = O_WRONLY | O_NONBLOCK;
|
|
|
|
if (!child_process_inherit)
|
|
|
|
flags |= O_CLOEXEC;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-01-14 07:19:40 +08:00
|
|
|
using namespace std::chrono;
|
|
|
|
const auto finish_time = Now() + timeout;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-01-14 07:19:40 +08:00
|
|
|
while (!CanWrite()) {
|
|
|
|
if (timeout != microseconds::zero()) {
|
|
|
|
const auto dur = duration_cast<microseconds>(finish_time - Now()).count();
|
|
|
|
if (dur <= 0)
|
2017-05-12 12:51:55 +08:00
|
|
|
return Status("timeout exceeded - reader hasn't opened so far");
|
2015-01-14 07:19:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
2021-01-09 03:37:09 +08:00
|
|
|
int fd = ::open(name.str().c_str(), flags);
|
2015-01-14 07:19:40 +08:00
|
|
|
if (fd == -1) {
|
2016-08-11 06:43:48 +08:00
|
|
|
const auto errno_copy = errno;
|
2015-01-14 07:19:40 +08:00
|
|
|
// We may get ENXIO if a reader side of the pipe hasn't opened yet.
|
2019-03-22 03:35:55 +08:00
|
|
|
if (errno_copy != ENXIO && errno_copy != EINTR)
|
2017-05-12 12:51:55 +08:00
|
|
|
return Status(errno_copy, eErrorTypePOSIX);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-01-14 07:19:40 +08:00
|
|
|
std::this_thread::sleep_for(
|
|
|
|
milliseconds(OPEN_WRITER_SLEEP_TIMEOUT_MSECS));
|
2014-07-03 05:10:39 +08:00
|
|
|
} else {
|
2015-01-14 07:19:40 +08:00
|
|
|
m_fds[WRITE] = fd;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-03 05:10:39 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
return Status();
|
2014-07-03 05:10:39 +08:00
|
|
|
}
|
|
|
|
|
2014-12-18 02:02:19 +08:00
|
|
|
int PipePosix::GetReadFileDescriptor() const { return m_fds[READ]; }
|
2014-07-03 05:10:39 +08:00
|
|
|
|
|
|
|
int PipePosix::GetWriteFileDescriptor() const { return m_fds[WRITE]; }
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-12-18 02:02:19 +08:00
|
|
|
int PipePosix::ReleaseReadFileDescriptor() {
|
2014-07-03 05:10:39 +08:00
|
|
|
const int fd = m_fds[READ];
|
2014-12-18 02:02:19 +08:00
|
|
|
m_fds[READ] = PipePosix::kInvalidDescriptor;
|
2014-07-03 05:10:39 +08:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2014-12-18 02:02:19 +08:00
|
|
|
int PipePosix::ReleaseWriteFileDescriptor() {
|
2014-07-03 05:10:39 +08:00
|
|
|
const int fd = m_fds[WRITE];
|
2014-12-18 02:02:19 +08:00
|
|
|
m_fds[WRITE] = PipePosix::kInvalidDescriptor;
|
2014-07-03 05:10:39 +08:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2014-12-18 02:02:19 +08:00
|
|
|
void PipePosix::Close() {
|
2014-07-03 05:10:39 +08:00
|
|
|
CloseReadFileDescriptor();
|
|
|
|
CloseWriteFileDescriptor();
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status PipePosix::Delete(llvm::StringRef name) {
|
2017-03-21 13:47:57 +08:00
|
|
|
return llvm::sys::fs::remove(name);
|
2015-01-14 07:19:40 +08:00
|
|
|
}
|
|
|
|
|
2014-12-18 02:02:19 +08:00
|
|
|
bool PipePosix::CanRead() const {
|
|
|
|
return m_fds[READ] != PipePosix::kInvalidDescriptor;
|
2014-07-03 05:10:39 +08:00
|
|
|
}
|
|
|
|
|
2014-12-18 02:02:19 +08:00
|
|
|
bool PipePosix::CanWrite() const {
|
|
|
|
return m_fds[WRITE] != PipePosix::kInvalidDescriptor;
|
2014-07-03 05:10:39 +08:00
|
|
|
}
|
|
|
|
|
2014-12-18 02:02:19 +08:00
|
|
|
void PipePosix::CloseReadFileDescriptor() {
|
|
|
|
if (CanRead()) {
|
2015-01-14 07:19:40 +08:00
|
|
|
close(m_fds[READ]);
|
2014-12-18 02:02:19 +08:00
|
|
|
m_fds[READ] = PipePosix::kInvalidDescriptor;
|
2014-07-03 05:10:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 02:02:19 +08:00
|
|
|
void PipePosix::CloseWriteFileDescriptor() {
|
|
|
|
if (CanWrite()) {
|
2015-01-14 07:19:40 +08:00
|
|
|
close(m_fds[WRITE]);
|
2014-12-18 02:02:19 +08:00
|
|
|
m_fds[WRITE] = PipePosix::kInvalidDescriptor;
|
2014-07-03 05:10:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status PipePosix::ReadWithTimeout(void *buf, size_t size,
|
|
|
|
const std::chrono::microseconds &timeout,
|
|
|
|
size_t &bytes_read) {
|
2014-12-18 02:02:19 +08:00
|
|
|
bytes_read = 0;
|
2015-01-14 07:19:40 +08:00
|
|
|
if (!CanRead())
|
2017-05-12 12:51:55 +08:00
|
|
|
return Status(EINVAL, eErrorTypePOSIX);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-08-11 06:43:48 +08:00
|
|
|
const int fd = GetReadFileDescriptor();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-08-11 06:43:48 +08:00
|
|
|
SelectHelper select_helper;
|
|
|
|
select_helper.SetTimeout(timeout);
|
|
|
|
select_helper.FDSetRead(fd);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2016-08-11 06:43:48 +08:00
|
|
|
while (error.Success()) {
|
|
|
|
error = select_helper.Select();
|
|
|
|
if (error.Success()) {
|
2020-01-07 19:13:03 +08:00
|
|
|
auto result =
|
|
|
|
::read(fd, static_cast<char *>(buf) + bytes_read, size - bytes_read);
|
2016-08-11 06:43:48 +08:00
|
|
|
if (result != -1) {
|
|
|
|
bytes_read += result;
|
|
|
|
if (bytes_read == size || result == 0)
|
|
|
|
break;
|
2019-03-22 03:35:55 +08:00
|
|
|
} else if (errno == EINTR) {
|
|
|
|
continue;
|
2016-08-11 06:43:48 +08:00
|
|
|
} else {
|
|
|
|
error.SetErrorToErrno();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2016-08-11 06:43:48 +08:00
|
|
|
return error;
|
2014-07-03 05:10:39 +08:00
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status PipePosix::Write(const void *buf, size_t size, size_t &bytes_written) {
|
2014-12-18 02:02:19 +08:00
|
|
|
bytes_written = 0;
|
2015-01-14 07:19:40 +08:00
|
|
|
if (!CanWrite())
|
2017-05-12 12:51:55 +08:00
|
|
|
return Status(EINVAL, eErrorTypePOSIX);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-08-11 06:43:48 +08:00
|
|
|
const int fd = GetWriteFileDescriptor();
|
|
|
|
SelectHelper select_helper;
|
|
|
|
select_helper.SetTimeout(std::chrono::seconds(0));
|
|
|
|
select_helper.FDSetWrite(fd);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2016-08-11 06:43:48 +08:00
|
|
|
while (error.Success()) {
|
|
|
|
error = select_helper.Select();
|
|
|
|
if (error.Success()) {
|
2020-01-07 19:13:03 +08:00
|
|
|
auto result = ::write(fd, static_cast<const char *>(buf) + bytes_written,
|
|
|
|
size - bytes_written);
|
2016-08-11 06:43:48 +08:00
|
|
|
if (result != -1) {
|
|
|
|
bytes_written += result;
|
|
|
|
if (bytes_written == size)
|
|
|
|
break;
|
2019-03-22 03:35:55 +08:00
|
|
|
} else if (errno == EINTR) {
|
|
|
|
continue;
|
2016-08-11 06:43:48 +08:00
|
|
|
} else {
|
|
|
|
error.SetErrorToErrno();
|
|
|
|
}
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2016-08-11 06:43:48 +08:00
|
|
|
return error;
|
2014-07-03 05:10:39 +08:00
|
|
|
}
|