[Lldb-commits] [lldb] [lldb-dap] Ensure the IO forwarding threads are managed by the DAP object lifecycle. (PR #120457)
John Harrison via lldb-commits
lldb-commits at lists.llvm.org
Fri Dec 20 13:49:49 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));
+ if (stdout_fd == -1) {
+ llvm::errs() << "Failed to configure stdout redirect: "
+ << lldb_private::Status::FromErrno().takeError() << "\n";
+ return EXIT_FAILURE;
+ }
+
+ redirectOut = stdout;
+ redirectErr = stderr;
- /// used only by TestVSCode_redirection_to_console.py
- if (getenv("LLDB_DAP_TEST_STDOUT_STDERR_REDIRECTION") != nullptr)
- redirection_test();
+ input = StreamDescriptor::from_file(fileno(stdin), false);
+ output = StreamDescriptor::from_file(stdout_fd, false);
}
+ DAP dap = DAP(program_path.str(), log, default_repl_mode, std::move(input),
----------------
ashgti wrote:
Done.
https://github.com/llvm/llvm-project/pull/120457
More information about the lldb-commits
mailing list