================ @@ -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:
I would work (since the code is still single-threaded at this point), but I don't think it's better than F_DUPFD_CLOEXEC. The reason the code you quote is written this way is because non-linux systems don't have the equivalent of the `pipe2`. I've checked that macos and all supported BSD variants do have this option. The only one I couldn't check is AIX, but it's still too early to call that "supported". https://github.com/llvm/llvm-project/pull/120457 _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits