[Lldb-commits] [lldb] [lldb-dap] Ensure the IO forwarding threads are managed by the DAP object lifecycle. (PR #120457)

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Fri Dec 20 02:37:51 PST 2024


================
@@ -5027,43 +5019,77 @@ int main(int argc, char *argv[]) {
   }
 #endif
 
+  std::optional<std::ofstream> log = std::nullopt;
+  const char *log_file_path = getenv("LLDBDAP_LOG");
+  if (log_file_path)
+    log.emplace(log_file_path);
+
   // Initialize LLDB first before we do anything.
   lldb::SBDebugger::Initialize();
 
   // Terminate the debugger before the C++ destructor chain kicks in.
   auto terminate_debugger =
       llvm::make_scope_exit([] { lldb::SBDebugger::Terminate(); });
 
-  DAP dap = DAP(program_path.str(), default_repl_mode);
-
-  RegisterRequestCallbacks(dap);
-
-  // stdout/stderr redirection to the IDE's console
-  int new_stdout_fd = SetupStdoutStderrRedirection(dap);
-
+  StreamDescriptor input;
+  StreamDescriptor output;
+  std::optional<std::FILE *> redirectOut = std::nullopt;
+  std::optional<std::FILE *> redirectErr = std::nullopt;
   if (portno != -1) {
     printf("Listening on port %i...\n", portno);
-    SOCKET socket_fd = AcceptConnection(dap, portno);
-    if (socket_fd >= 0) {
-      dap.input.descriptor = StreamDescriptor::from_socket(socket_fd, true);
-      dap.output.descriptor = StreamDescriptor::from_socket(socket_fd, false);
-    } else {
+    SOCKET socket_fd = AcceptConnection(log, portno);
+    if (socket_fd < 0)
       return EXIT_FAILURE;
-    }
+
+    input = StreamDescriptor::from_socket(socket_fd, true);
+    output = StreamDescriptor::from_socket(socket_fd, false);
   } else {
-    dap.input.descriptor = StreamDescriptor::from_file(fileno(stdin), false);
-    dap.output.descriptor = StreamDescriptor::from_file(new_stdout_fd, false);
+#if defined(_WIN32)
+    // Windows opens stdout and stdin in text mode which converts \n to 13,10
+    // while the value is just 10 on Darwin/Linux. Setting the file mode to
+    // binary fixes this.
+    int result = _setmode(fileno(stdout), _O_BINARY);
+    assert(result);
+    result = _setmode(fileno(stdin), _O_BINARY);
+    UNUSED_IF_ASSERT_DISABLED(result);
+    assert(result);
+#endif
+
+    int stdout_fd = dup(fileno(stdout));
----------------
labath wrote:

```suggestion
    int stdout_fd = dup(fileno(stdout));
#else
    int stdout_fd = fcntl(fileno(stdout), F_DUPFD_CLOEXEC, 3);
#endif

```

I know this isn't your code, but I think it's a good idea to set this flag, particularly as we're going to be doing (a lot) more things within the same process.

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


More information about the lldb-commits mailing list