Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/tools/lldb-vscode/OutputRedirector.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- OutputRedirector.cpp -----------------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===/
8
9
#if !defined(_WIN32)
10
#include <unistd.h>
11
#endif
12
13
#include "OutputRedirector.h"
14
15
using namespace llvm;
16
17
namespace lldb_vscode {
18
19
2
Error RedirectFd(int fd, std::function<void(llvm::StringRef)> callback) {
20
2
#if !defined(_WIN32)
21
2
  int new_fd[2];
22
2
  if (pipe(new_fd) == -1) {
23
0
    int error = errno;
24
0
    return createStringError(inconvertibleErrorCode(),
25
0
                             "Couldn't create new pipe for fd %d. %s", fd,
26
0
                             strerror(error));
27
0
  }
28
29
2
  if (dup2(new_fd[1], fd) == -1) {
30
0
    int error = errno;
31
0
    return createStringError(inconvertibleErrorCode(),
32
0
                             "Couldn't override the fd %d. %s", fd,
33
0
                             strerror(error));
34
0
  }
35
36
2
  int read_fd = new_fd[0];
37
2
  std::thread t([read_fd, callback]() {
38
2
    char buffer[4096];
39
4
    while (true) {
40
2
      ssize_t bytes_count = read(read_fd, &buffer, sizeof(buffer));
41
2
      if (bytes_count == 0)
42
0
        return;
43
2
      if (bytes_count == -1) {
44
0
        if (errno == EAGAIN || errno == EINTR)
45
0
          continue;
46
0
        break;
47
0
      }
48
2
      callback(StringRef(buffer, bytes_count).str());
49
2
    }
50
2
  });
51
2
  t.detach();
52
2
#endif
53
2
  return Error::success();
54
2
}
55
56
} // namespace lldb_vscode