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);
68 SECURITY_ATTRIBUTES sa{
sizeof(SECURITY_ATTRIBUTES), 0,
69 child_process_inherit ? TRUE : FALSE};
89 std::string pipe_name;
90 llvm::raw_string_ostream pipe_name_stream(pipe_name);
91 pipe_name_stream <<
"lldb.pipe." << ::GetCurrentProcessId() <<
"." << serial;
92 pipe_name_stream.flush();
94 return CreateNew(pipe_name.c_str(), child_process_inherit);
98 bool child_process_inherit) {
106 pipe_path.append(name.str());
110 DWORD read_mode = FILE_FLAG_OVERLAPPED;
111 m_read = ::CreateNamedPipeA(
112 pipe_path.c_str(), PIPE_ACCESS_INBOUND | read_mode,
113 PIPE_TYPE_BYTE | PIPE_WAIT, 1, 1024, 1024, 120 * 1000, NULL);
114 if (INVALID_HANDLE_VALUE ==
m_read)
132 bool child_process_inherit,
134 llvm::SmallString<128> pipe_name;
137 RPC_CSTR unique_string;
138 RPC_STATUS status = ::UuidCreate(&unique_id);
139 if (status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY)
140 status = ::UuidToStringA(&unique_id, &unique_string);
141 if (status == RPC_S_OK) {
144 pipe_name +=
reinterpret_cast<char *
>(unique_string);
145 ::RpcStringFreeA(&unique_string);
156 bool child_process_inherit) {
165 bool child_process_inherit,
166 const std::chrono::microseconds &timeout) {
174 bool child_process_inherit,
bool is_read) {
180 SECURITY_ATTRIBUTES attributes = {};
181 attributes.bInheritHandle = child_process_inherit;
184 pipe_path.append(name.str());
187 m_read = ::CreateFileA(pipe_path.c_str(), GENERIC_READ, 0, &attributes,
188 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
189 if (INVALID_HANDLE_VALUE ==
m_read)
197 m_write = ::CreateFileA(pipe_path.c_str(), GENERIC_WRITE, 0, &attributes,
198 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
199 if (INVALID_HANDLE_VALUE ==
m_write)
221 m_read = INVALID_HANDLE_VALUE;
231 m_write = INVALID_HANDLE_VALUE;
244 m_read = INVALID_HANDLE_VALUE;
254 m_write = INVALID_HANDLE_VALUE;
277 const std::chrono::microseconds &duration,
278 size_t &bytes_read) {
283 DWORD sys_bytes_read = size;
284 BOOL result = ::ReadFile(
m_read, buf, sys_bytes_read, &sys_bytes_read,
286 if (!result && GetLastError() != ERROR_IO_PENDING)
289 DWORD timeout = (duration == std::chrono::microseconds::zero())
291 : duration.count() * 1000;
293 if (wait_result != WAIT_OBJECT_0) {
301 DWORD failure_error = ::GetLastError();
302 if (wait_result == WAIT_TIMEOUT) {
304 if (!cancel_result && GetLastError() == ERROR_NOT_FOUND)
316 bytes_read = sys_bytes_read;
321 size_t &bytes_written) {
325 DWORD sys_bytes_written = 0;
326 BOOL write_result = ::WriteFile(
m_write, buf, num_bytes, &sys_bytes_written,
328 if (!write_result && GetLastError() != ERROR_IO_PENDING)
332 &sys_bytes_written, TRUE);
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 Write(const void *buf, size_t size, size_t &bytes_written) 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
Status CreateNewNamed(bool child_process_inherit)
OVERLAPPED m_read_overlapped
static const int kInvalidDescriptor
Status CreateNew(bool child_process_inherit) 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.