11#include "llvm/ADT/SmallString.h"
12#include "llvm/Support/Process.h"
13#include "llvm/Support/raw_ostream.h"
29 : m_read(INVALID_HANDLE_VALUE), m_write(INVALID_HANDLE_VALUE),
37 : m_read((HANDLE)read), m_write((HANDLE)write),
48 m_read_fd = _open_osfhandle((intptr_t)read, _O_RDONLY);
51 m_read = INVALID_HANDLE_VALUE;
55 m_write_fd = _open_osfhandle((intptr_t)write, _O_WRONLY);
74 std::string pipe_name;
75 llvm::raw_string_ostream pipe_name_stream(pipe_name);
76 pipe_name_stream <<
"lldb.pipe." << ::GetCurrentProcessId() <<
"." << serial;
77 pipe_name_stream.flush();
79 return CreateNew(pipe_name.c_str(), child_process_inherit);
83 bool child_process_inherit) {
91 pipe_path.append(name.str());
93 SECURITY_ATTRIBUTES sa{
sizeof(SECURITY_ATTRIBUTES), 0,
94 child_process_inherit ? TRUE : FALSE};
98 DWORD read_mode = FILE_FLAG_OVERLAPPED;
100 ::CreateNamedPipeA(pipe_path.c_str(), PIPE_ACCESS_INBOUND | read_mode,
101 PIPE_TYPE_BYTE | PIPE_WAIT, 1,
105 if (INVALID_HANDLE_VALUE ==
m_read)
123 bool child_process_inherit,
125 llvm::SmallString<128> pipe_name;
128 RPC_CSTR unique_string;
129 RPC_STATUS status = ::UuidCreate(&unique_id);
130 if (status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY)
131 status = ::UuidToStringA(&unique_id, &unique_string);
132 if (status == RPC_S_OK) {
135 pipe_name +=
reinterpret_cast<char *
>(unique_string);
136 ::RpcStringFreeA(&unique_string);
147 bool child_process_inherit) {
156 bool child_process_inherit,
157 const std::chrono::microseconds &timeout) {
165 bool child_process_inherit,
bool is_read) {
171 SECURITY_ATTRIBUTES attributes{
sizeof(SECURITY_ATTRIBUTES), 0,
172 child_process_inherit ? TRUE : FALSE};
175 pipe_path.append(name.str());
178 m_read = ::CreateFileA(pipe_path.c_str(), GENERIC_READ, 0, &attributes,
179 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
180 if (INVALID_HANDLE_VALUE ==
m_read)
188 m_write = ::CreateFileA(pipe_path.c_str(), GENERIC_WRITE, 0, &attributes,
189 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
190 if (INVALID_HANDLE_VALUE ==
m_write)
213 m_read = INVALID_HANDLE_VALUE;
225 m_write = INVALID_HANDLE_VALUE;
238 m_read = INVALID_HANDLE_VALUE;
251 m_write = INVALID_HANDLE_VALUE;
274 const std::chrono::microseconds &duration,
275 size_t &bytes_read) {
280 DWORD sys_bytes_read = 0;
284 bytes_read = sys_bytes_read;
288 DWORD failure_error = ::GetLastError();
289 if (failure_error != ERROR_IO_PENDING)
292 DWORD timeout = (duration == std::chrono::microseconds::zero())
294 : duration.count() / 1000;
296 if (wait_result != WAIT_OBJECT_0) {
304 failure_error = ::GetLastError();
305 if (wait_result == WAIT_TIMEOUT) {
307 if (!cancel_result && ::GetLastError() == ERROR_NOT_FOUND)
320 bytes_read = sys_bytes_read;
325 const std::chrono::microseconds &duration,
326 size_t &bytes_written) {
331 DWORD sys_bytes_write = 0;
335 bytes_written = sys_bytes_write;
339 DWORD failure_error = ::GetLastError();
340 if (failure_error != ERROR_IO_PENDING)
343 DWORD timeout = (duration == std::chrono::microseconds::zero())
345 : duration.count() / 1000;
347 if (wait_result != WAIT_OBJECT_0) {
355 failure_error = ::GetLastError();
356 if (wait_result == WAIT_TIMEOUT) {
358 if (!cancel_result && ::GetLastError() == ERROR_NOT_FOUND)
371 bytes_written = sys_bytes_write;
static llvm::raw_ostream & error(Stream &strm)
static std::atomic< uint32_t > g_pipe_serial(0)
static constexpr llvm::StringLiteral g_pipe_name_prefix
bool CanWrite() const override
Status OpenAsReader(llvm::StringRef name, bool child_process_inherit) override
Status CreateWithUniqueName(llvm::StringRef prefix, bool child_process_inherit, llvm::SmallVectorImpl< char > &name) override
HANDLE GetWriteNativeHandle()
Status Delete(llvm::StringRef name) override
int ReleaseReadFileDescriptor() override
void CloseReadFileDescriptor() override
OVERLAPPED m_write_overlapped
Status ReadWithTimeout(void *buf, size_t size, const std::chrono::microseconds &timeout, size_t &bytes_read) override
void CloseWriteFileDescriptor() override
OVERLAPPED m_read_overlapped
static const int kInvalidDescriptor
Status CreateNew(bool child_process_inherit) override
Status WriteWithTimeout(const void *buf, size_t size, const std::chrono::microseconds &timeout, size_t &bytes_written) override
HANDLE GetReadNativeHandle()
Status OpenNamedPipe(llvm::StringRef name, bool child_process_inherit, bool is_read)
Status OpenAsWriterWithTimeout(llvm::StringRef name, bool child_process_inherit, const std::chrono::microseconds &timeout) override
int ReleaseWriteFileDescriptor() override
int GetWriteFileDescriptor() const override
int GetReadFileDescriptor() const override
bool CanRead() const override
bool Success() const
Test for success condition.
#define LLDB_INVALID_PIPE
A class that represents a running process on the host machine.
@ eErrorTypeWin32
Standard Win32 error codes.