[Lldb-commits] [lldb] [lldb][windows] add a Windows FifoFile implementation (PR #185894)

Charles Zablit via lldb-commits lldb-commits at lists.llvm.org
Thu Mar 12 04:56:02 PDT 2026


================
@@ -24,17 +28,94 @@ using namespace llvm;
 
 namespace lldb_dap {
 
-FifoFile::FifoFile(StringRef path) : m_path(path) {}
+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);
+  }
+#endif
+  m_pipe = pipe;
+}
 
 FifoFile::~FifoFile() {
-#if !defined(_WIN32)
+#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) {
+    if (!ReadFile(m_pipe, read_buffer, sizeof(read_buffer), &bytes_read,
+                  NULL) ||
+        bytes_read == 0)
+      break;
+
+    buffer.append(read_buffer, bytes_read);
+
+    if (buffer.back() == '\n') {
+      buffer.pop_back();
+      if (!buffer.empty() && buffer.back() == '\r')
+        buffer.pop_back();
+      break;
+    }
----------------
charles-zablit wrote:

Good catch, it was never a problem in my tests at desk but it could happen, probably if we start sending longer JSON in the future.

The latest change use `PIPE_TYPE_MESSAGE` (which I did not know about before) to send data as messages. Messages are sent through Writeline and received through Readline, so they always end with `\n` and are exactly one line.

https://github.com/llvm/llvm-project/pull/185894


More information about the lldb-commits mailing list