aadsm created this revision. aadsm added reviewers: clayborg, labath, rmaz. Herald added a project: LLDB. Herald added a subscriber: lldb-commits.
Doing this for 2 main reasons: - Avoid stdout from corrupting DAP messages - Make sure we can see in VSCode (or any other DAP client) the same output we see when debugging from the command line Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D80659 Files: lldb/tools/lldb-vscode/lldb-vscode.cpp Index: lldb/tools/lldb-vscode/lldb-vscode.cpp =================================================================== --- lldb/tools/lldb-vscode/lldb-vscode.cpp +++ lldb/tools/lldb-vscode/lldb-vscode.cpp @@ -485,6 +485,31 @@ } } +int RedirectFileDescriptorToConsoleOutput(int fd) { + int new_fd = dup(fd); + int captured_fd[2]; + + pipe(captured_fd); + dup2(captured_fd[1], fd); + int read_fd = captured_fd[0]; + + std::thread([read_fd] { + char buffer[4096]; + fd_set readfds; + while (true) { + FD_ZERO(&readfds); + FD_SET(read_fd, &readfds); + select(read_fd + 1, &readfds, nullptr, nullptr, nullptr); + if (FD_ISSET(read_fd, &readfds)) { + read(read_fd, &buffer, sizeof(buffer)); + g_vsc.SendOutput(OutputType::Console, buffer); + } + } + }).detach(); + + return new_fd; +} + // "AttachRequest": { // "allOf": [ { "$ref": "#/definitions/Request" }, { // "type": "object", @@ -2818,9 +2843,10 @@ exit(1); } } else { + int new_stdout_fd = RedirectFileDescriptorToConsoleOutput(fileno(stdout)); + RedirectFileDescriptorToConsoleOutput(fileno(stderr)); g_vsc.input.descriptor = StreamDescriptor::from_file(fileno(stdin), false); - g_vsc.output.descriptor = - StreamDescriptor::from_file(fileno(stdout), false); + g_vsc.output.descriptor = StreamDescriptor::from_file(new_stdout_fd, false); } auto request_handlers = GetRequestHandlers(); uint32_t packet_idx = 0;
Index: lldb/tools/lldb-vscode/lldb-vscode.cpp =================================================================== --- lldb/tools/lldb-vscode/lldb-vscode.cpp +++ lldb/tools/lldb-vscode/lldb-vscode.cpp @@ -485,6 +485,31 @@ } } +int RedirectFileDescriptorToConsoleOutput(int fd) { + int new_fd = dup(fd); + int captured_fd[2]; + + pipe(captured_fd); + dup2(captured_fd[1], fd); + int read_fd = captured_fd[0]; + + std::thread([read_fd] { + char buffer[4096]; + fd_set readfds; + while (true) { + FD_ZERO(&readfds); + FD_SET(read_fd, &readfds); + select(read_fd + 1, &readfds, nullptr, nullptr, nullptr); + if (FD_ISSET(read_fd, &readfds)) { + read(read_fd, &buffer, sizeof(buffer)); + g_vsc.SendOutput(OutputType::Console, buffer); + } + } + }).detach(); + + return new_fd; +} + // "AttachRequest": { // "allOf": [ { "$ref": "#/definitions/Request" }, { // "type": "object", @@ -2818,9 +2843,10 @@ exit(1); } } else { + int new_stdout_fd = RedirectFileDescriptorToConsoleOutput(fileno(stdout)); + RedirectFileDescriptorToConsoleOutput(fileno(stderr)); g_vsc.input.descriptor = StreamDescriptor::from_file(fileno(stdin), false); - g_vsc.output.descriptor = - StreamDescriptor::from_file(fileno(stdout), false); + g_vsc.output.descriptor = StreamDescriptor::from_file(new_stdout_fd, false); } auto request_handlers = GetRequestHandlers(); uint32_t packet_idx = 0;
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits