Skip to content

Commit 941df40

Browse files
committed
Applying reviewer suggestions and updating the existing tests to ensure output produced by the debuggee is not affected by the output redirection.
1 parent af07cba commit 941df40

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

lldb/test/API/tools/lldb-dap/output/TestDAP_output.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,33 @@
1010
class TestDAP_output(lldbdap_testcase.DAPTestCaseBase):
1111
@skipIfWindows
1212
def test_output(self):
13+
"""
14+
Test output handling for the running process.
15+
"""
1316
program = self.getBuildArtifact("a.out")
14-
self.build_and_launch(program)
17+
self.build_and_launch(
18+
program,
19+
exitCommands=[
20+
# Ensure that output produced by lldb itself is not consumed by the OutputRedirector.
21+
"?script print('out\\0\\0', end='\\r\\n', file=sys.stdout)",
22+
"?script print('err\\0\\0', end='\\r\\n', file=sys.stderr)",
23+
],
24+
)
1525
source = "main.c"
1626
lines = [line_number(source, "// breakpoint 1")]
1727
breakpoint_ids = self.set_source_breakpoints(source, lines)
1828
self.continue_to_breakpoints(breakpoint_ids)
19-
29+
2030
# Ensure partial messages are still sent.
2131
output = self.collect_stdout(timeout_secs=1.0, pattern="abcdef")
22-
self.assertTrue(output and len(output) > 0, "expect no program output")
32+
self.assertTrue(output and len(output) > 0, "expect program stdout")
2333

2434
self.continue_to_exit()
25-
35+
2636
output += self.get_stdout(timeout=lldbdap_testcase.DAPTestCaseBase.timeoutval)
27-
self.assertTrue(output and len(output) > 0, "expect no program output")
37+
self.assertTrue(output and len(output) > 0, "expect program stdout")
2838
self.assertIn(
29-
"abcdefghi\r\nhello world\r\n",
39+
"abcdefghi\r\nhello world\r\nfinally\0\0out\0\0\r\nerr\0\0",
3040
output,
31-
'full output not found in: ' + output,
41+
"full stdout not found in: " + repr(output),
3242
)

lldb/test/API/tools/lldb-dap/output/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@ int main() {
88
printf("def");
99
printf("ghi\n");
1010
printf("hello world\n"); // breakpoint 1
11+
// Ensure the OutputRedirector does not consume the programs \0\0 output.
12+
char buf[] = "finally\0";
13+
write(STDOUT_FILENO, buf, sizeof(buf));
1114
return 0;
1215
}

lldb/tools/lldb-dap/OutputRedirector.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,9 @@
1919
#include <unistd.h>
2020
#endif
2121

22-
using lldb_private::Pipe;
23-
using llvm::createStringError;
24-
using llvm::Error;
25-
using llvm::Expected;
26-
using llvm::inconvertibleErrorCode;
27-
using llvm::StringRef;
22+
using namespace llvm;
2823

29-
namespace {
30-
char kCloseSentinel[] = "\0";
31-
} // namespace
24+
static constexpr auto kCloseSentinel = StringLiteral::withInnerNUL("\0");
3225

3326
namespace lldb_dap {
3427

@@ -63,19 +56,19 @@ Error OutputRedirector::RedirectTo(std::function<void(StringRef)> callback) {
6356
char buffer[OutputBufferSize];
6457
while (!m_stopped) {
6558
ssize_t bytes_count = ::read(read_fd, &buffer, sizeof(buffer));
66-
// EOF detected.
67-
if (bytes_count == 0 ||
68-
(bytes_count == sizeof(kCloseSentinel) &&
69-
std::memcmp(buffer, kCloseSentinel, sizeof(kCloseSentinel)) == 0 &&
70-
m_fd == kInvalidDescriptor))
71-
break;
7259
if (bytes_count == -1) {
7360
// Skip non-fatal errors.
7461
if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)
7562
continue;
7663
break;
7764
}
7865

66+
StringRef data(buffer, bytes_count);
67+
if (m_stopped)
68+
data.consume_back(kCloseSentinel);
69+
if (data.empty())
70+
break;
71+
7972
callback(StringRef(buffer, bytes_count));
8073
}
8174
::close(read_fd);
@@ -93,7 +86,7 @@ void OutputRedirector::Stop() {
9386
// Closing the pipe may not be sufficient to wake up the thread in case the
9487
// write descriptor is duplicated (to stdout/err or to another process).
9588
// Write a null byte to ensure the read call returns.
96-
(void)::write(fd, kCloseSentinel, sizeof(kCloseSentinel));
89+
(void)::write(fd, kCloseSentinel.data(), kCloseSentinel.size());
9790
::close(fd);
9891
m_forwarder.join();
9992
}

lldb/tools/lldb-dap/OutputRedirector.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#ifndef LLDB_TOOLS_LLDB_DAP_OUTPUT_REDIRECTOR_H
1010
#define LLDB_TOOLS_LLDB_DAP_OUTPUT_REDIRECTOR_H
1111

12-
#include "lldb/Host/Pipe.h"
1312
#include "llvm/ADT/StringRef.h"
1413
#include "llvm/Support/Error.h"
1514
#include <atomic>

0 commit comments

Comments
 (0)