CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
175 lines
5.6 KiB
C++
175 lines
5.6 KiB
C++
//===-- FifoFiles.cpp -------------------------------------------*- C++ -*-===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "FifoFiles.h"
|
|
#include "JSONUtils.h"
|
|
|
|
#ifdef _WIN32
|
|
#include "lldb/Host/windows/PipeWindows.h"
|
|
#include "lldb/Host/windows/windows.h"
|
|
#include "llvm/Support/Path.h"
|
|
#else
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
#include <chrono>
|
|
#include <fstream>
|
|
#include <future>
|
|
#include <optional>
|
|
|
|
using namespace llvm;
|
|
|
|
namespace lldb_dap {
|
|
|
|
FifoFile::FifoFile(StringRef path, lldb::pipe_t pipe) : m_path(path) {
|
|
#ifdef _WIN32
|
|
if (pipe == INVALID_HANDLE_VALUE) {
|
|
assert(path.starts_with("\\\\.\\pipe\\") &&
|
|
"FifoFile path should start with '\\\\.\\pipe\\'");
|
|
pipe = CreateFileA(m_path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL,
|
|
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
|
|
DWORD mode = PIPE_READMODE_MESSAGE;
|
|
SetNamedPipeHandleState(pipe, &mode, NULL, NULL);
|
|
}
|
|
#endif
|
|
m_pipe = pipe;
|
|
}
|
|
|
|
FifoFile::~FifoFile() {
|
|
#ifdef _WIN32
|
|
if (m_pipe != INVALID_HANDLE_VALUE) {
|
|
DisconnectNamedPipe(m_pipe);
|
|
CloseHandle(m_pipe);
|
|
}
|
|
#else
|
|
unlink(m_path.c_str());
|
|
#endif
|
|
}
|
|
|
|
void FifoFile::WriteLine(llvm::StringRef line) {
|
|
#ifdef _WIN32
|
|
DWORD written;
|
|
std::string str = line.str() + "\n";
|
|
WriteFile(m_pipe, str.data(), static_cast<DWORD>(str.size()), &written, NULL);
|
|
FlushFileBuffers(m_pipe);
|
|
#else
|
|
std::ofstream writer(m_path, std::ofstream::out);
|
|
writer << line.data() << std::endl;
|
|
#endif
|
|
}
|
|
|
|
void FifoFile::Connect() {
|
|
#ifdef _WIN32
|
|
ConnectNamedPipe(m_pipe, NULL);
|
|
#endif
|
|
}
|
|
|
|
std::string FifoFile::ReadLine() {
|
|
#ifdef _WIN32
|
|
std::string buffer;
|
|
char read_buffer[4096];
|
|
DWORD bytes_read;
|
|
|
|
while (true) {
|
|
BOOL success =
|
|
ReadFile(m_pipe, read_buffer, sizeof(read_buffer), &bytes_read, NULL);
|
|
buffer.append(read_buffer, bytes_read);
|
|
if (success || GetLastError() != ERROR_MORE_DATA)
|
|
break;
|
|
}
|
|
|
|
return buffer;
|
|
#else
|
|
std::ifstream reader(m_path, std::ifstream::in);
|
|
std::string buffer;
|
|
std::getline(reader, buffer);
|
|
return buffer;
|
|
#endif
|
|
}
|
|
|
|
Expected<std::shared_ptr<FifoFile>> CreateFifoFile(StringRef path) {
|
|
#if defined(_WIN32)
|
|
assert(path.starts_with("\\\\.\\pipe\\") &&
|
|
"FifoFile path should start with '\\\\.\\pipe\\'");
|
|
HANDLE pipe_handle =
|
|
CreateNamedPipeA(path.data(), PIPE_ACCESS_DUPLEX,
|
|
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
|
|
PIPE_UNLIMITED_INSTANCES, 4096, 4096, 0, NULL);
|
|
|
|
if (pipe_handle == INVALID_HANDLE_VALUE) {
|
|
DWORD error = GetLastError();
|
|
return createStringError(std::error_code(error, std::system_category()),
|
|
"Couldn't create named pipe: %s", path.data());
|
|
}
|
|
|
|
return std::make_shared<FifoFile>(path, pipe_handle);
|
|
#else
|
|
if (int err = mkfifo(path.data(), 0600))
|
|
return createStringError(std::error_code(err, std::generic_category()),
|
|
"Couldn't create fifo file: %s", path.data());
|
|
return std::make_shared<FifoFile>(path);
|
|
#endif
|
|
}
|
|
|
|
FifoFileIO::FifoFileIO(std::shared_ptr<FifoFile> fifo_file,
|
|
StringRef other_endpoint_name)
|
|
: m_fifo_file(std::move(fifo_file)),
|
|
m_other_endpoint_name(other_endpoint_name) {}
|
|
|
|
Expected<json::Value> FifoFileIO::ReadJSON(std::chrono::milliseconds timeout) {
|
|
// We use a pointer for this future, because otherwise its normal destructor
|
|
// would wait for the getline to end, rendering the timeout useless.
|
|
std::optional<std::string> line;
|
|
std::future<void> *future =
|
|
new std::future<void>(std::async(std::launch::async, [&]() {
|
|
std::string buffer = m_fifo_file->ReadLine();
|
|
if (!buffer.empty())
|
|
line = buffer;
|
|
}));
|
|
if (future->wait_for(timeout) == std::future_status::timeout || !line)
|
|
// Indeed this is a leak, but it's intentional. "future" obj destructor
|
|
// will block on waiting for the worker thread to join. And the worker
|
|
// thread might be stuck in blocking I/O. Intentionally leaking the obj
|
|
// as a hack to avoid blocking main thread, and adding annotation to
|
|
// supress static code inspection warnings
|
|
|
|
// coverity[leaked_storage]
|
|
return createStringError(inconvertibleErrorCode(),
|
|
"Timed out trying to get messages from the " +
|
|
m_other_endpoint_name);
|
|
delete future;
|
|
return json::parse(*line);
|
|
}
|
|
|
|
Error FifoFileIO::SendJSON(const json::Value &json,
|
|
std::chrono::milliseconds timeout) {
|
|
bool done = false;
|
|
std::future<void> *future =
|
|
new std::future<void>(std::async(std::launch::async, [&]() {
|
|
m_fifo_file->WriteLine(JSONToString(json));
|
|
done = true;
|
|
}));
|
|
if (future->wait_for(timeout) == std::future_status::timeout || !done) {
|
|
// Indeed this is a leak, but it's intentional. "future" obj destructor will
|
|
// block on waiting for the worker thread to join. And the worker thread
|
|
// might be stuck in blocking I/O. Intentionally leaking the obj as a hack
|
|
// to avoid blocking main thread, and adding annotation to supress static
|
|
// code inspection warnings"
|
|
|
|
// coverity[leaked_storage]
|
|
return createStringError(inconvertibleErrorCode(),
|
|
"Timed out trying to send messages to the " +
|
|
m_other_endpoint_name);
|
|
}
|
|
delete future;
|
|
return Error::success();
|
|
}
|
|
|
|
} // namespace lldb_dap
|