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;
78 return CreateNew(pipe_name.c_str(), child_process_inherit);
82 bool child_process_inherit) {
90 pipe_path.append(name.str());
92 SECURITY_ATTRIBUTES sa{
sizeof(SECURITY_ATTRIBUTES), 0,
93 child_process_inherit ? TRUE : FALSE};
97 DWORD read_mode = FILE_FLAG_OVERLAPPED;
99 ::CreateNamedPipeA(pipe_path.c_str(), PIPE_ACCESS_INBOUND | read_mode,
100 PIPE_TYPE_BYTE | PIPE_WAIT, 1,
104 if (INVALID_HANDLE_VALUE ==
m_read)
122 bool child_process_inherit,
124 llvm::SmallString<128> pipe_name;
127 RPC_CSTR unique_string;
128 RPC_STATUS status = ::UuidCreate(&unique_id);
129 if (status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY)
130 status = ::UuidToStringA(&unique_id, &unique_string);
131 if (status == RPC_S_OK) {
134 pipe_name +=
reinterpret_cast<char *
>(unique_string);
135 ::RpcStringFreeA(&unique_string);
146 bool child_process_inherit) {
155 bool child_process_inherit,
156 const std::chrono::microseconds &timeout) {
164 bool child_process_inherit,
bool is_read) {
170 SECURITY_ATTRIBUTES attributes{
sizeof(SECURITY_ATTRIBUTES), 0,
171 child_process_inherit ? TRUE : FALSE};
174 pipe_path.append(name.str());
177 m_read = ::CreateFileA(pipe_path.c_str(), GENERIC_READ, 0, &attributes,
178 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
179 if (INVALID_HANDLE_VALUE ==
m_read)
187 m_write = ::CreateFileA(pipe_path.c_str(), GENERIC_WRITE, 0, &attributes,
188 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
189 if (INVALID_HANDLE_VALUE ==
m_write)
212 m_read = INVALID_HANDLE_VALUE;
224 m_write = INVALID_HANDLE_VALUE;
237 m_read = INVALID_HANDLE_VALUE;
250 m_write = INVALID_HANDLE_VALUE;
273 const std::chrono::microseconds &duration,
274 size_t &bytes_read) {
279 DWORD sys_bytes_read = 0;
283 bytes_read = sys_bytes_read;
287 DWORD failure_error = ::GetLastError();
288 if (failure_error != ERROR_IO_PENDING)
291 DWORD timeout = (duration == std::chrono::microseconds::zero())
293 : duration.count() / 1000;
295 if (wait_result != WAIT_OBJECT_0) {
303 failure_error = ::GetLastError();
304 if (wait_result == WAIT_TIMEOUT) {
306 if (!cancel_result && ::GetLastError() == ERROR_NOT_FOUND)
319 bytes_read = sys_bytes_read;
324 const std::chrono::microseconds &duration,
325 size_t &bytes_written) {
330 DWORD sys_bytes_write = 0;
334 bytes_written = sys_bytes_write;
338 DWORD failure_error = ::GetLastError();
339 if (failure_error != ERROR_IO_PENDING)
342 DWORD timeout = (duration == std::chrono::microseconds::zero())
344 : duration.count() / 1000;
346 if (wait_result != WAIT_OBJECT_0) {
354 failure_error = ::GetLastError();
355 if (wait_result == WAIT_TIMEOUT) {
357 if (!cancel_result && ::GetLastError() == ERROR_NOT_FOUND)
370 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.