[Lldb-commits] [PATCH] D80659: [lldb-vscode] Redirect stderr and stdout to DAPs console message

António Afonso via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Wed May 27 13:36:31 PDT 2020


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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80659.266644.patch
Type: text/x-patch
Size: 1481 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20200527/ac63bde4/attachment-0001.bin>


More information about the lldb-commits mailing list