diff --git a/lldb/tools/lldb-vscode/lldb-vscode.cpp b/lldb/tools/lldb-vscode/lldb-vscode.cpp --- a/lldb/tools/lldb-vscode/lldb-vscode.cpp +++ b/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;