[Lldb-commits] [lldb] [lldb][windows] add unit tests for the StripConPTYSequences method (PR #194654)
Charles Zablit via lldb-commits
lldb-commits at lists.llvm.org
Tue Apr 28 09:01:33 PDT 2026
https://github.com/charles-zablit updated https://github.com/llvm/llvm-project/pull/194654
>From 4c6780845d9565a84f0697ba22320a2f8380fedc Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Tue, 28 Apr 2026 16:07:45 +0100
Subject: [PATCH 1/3] [lldb][windows] add unit tests for the
StripConPTYSequences method
---
lldb/include/lldb/Host/windows/ConPTYUtils.h | 32 +++++
.../Host/windows/ConnectionConPTYWindows.h | 1 +
lldb/source/Host/CMakeLists.txt | 1 +
lldb/source/Host/common/ConPTYUtils.cpp | 78 +++++++++++
.../Host/windows/ConnectionConPTYWindows.cpp | 80 -----------
lldb/unittests/Host/CMakeLists.txt | 1 +
.../Host/StripConPTYSequencesTest.cpp | 132 ++++++++++++++++++
7 files changed, 245 insertions(+), 80 deletions(-)
create mode 100644 lldb/include/lldb/Host/windows/ConPTYUtils.h
create mode 100644 lldb/source/Host/common/ConPTYUtils.cpp
create mode 100644 lldb/unittests/Host/StripConPTYSequencesTest.cpp
diff --git a/lldb/include/lldb/Host/windows/ConPTYUtils.h b/lldb/include/lldb/Host/windows/ConPTYUtils.h
new file mode 100644
index 0000000000000..9629ad0afb66d
--- /dev/null
+++ b/lldb/include/lldb/Host/windows/ConPTYUtils.h
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_HOST_WINDOWS_CONPTYUTILS_H
+#define LLDB_HOST_WINDOWS_CONPTYUTILS_H
+
+#include <cstddef>
+
+namespace lldb_private {
+
+/// Remove ConPTY management sequences from a buffer in-place.
+///
+/// ConPTY injects several VT sequences into its output pipe that are not part
+/// of the inferior's output: a cursor-position query (\x1b[6n), Win32 Input
+/// Mode toggles (\x1b[?9001h/l), focus-event toggles (\x1b[?1004h/l), and a
+/// window-title OSC sequence (\x1b]0;...\x07).
+///
+/// \param[in,out] data Buffer containing raw ConPTY output.
+/// \param[in,out] len On entry, the number of valid bytes in \p data.
+/// Updated to the number of bytes after stripping.
+/// \param[in] strip_init If true, also strip init-only sequences (\x1b[m,
+/// \x1b[?25h) that ConPTY emits at startup.
+void StripConPTYSequences(void *data, size_t &len, bool strip_init);
+
+} // namespace lldb_private
+
+#endif // LLDB_HOST_WINDOWS_CONPTYUTILS_H
diff --git a/lldb/include/lldb/Host/windows/ConnectionConPTYWindows.h b/lldb/include/lldb/Host/windows/ConnectionConPTYWindows.h
index 518dcf2275582..1d708dffa5c87 100644
--- a/lldb/include/lldb/Host/windows/ConnectionConPTYWindows.h
+++ b/lldb/include/lldb/Host/windows/ConnectionConPTYWindows.h
@@ -9,6 +9,7 @@
#ifndef LLDB_HOST_WINDOWS_CONNECTIONCONPTYWINDOWS_H
#define LLDB_HOST_WINDOWS_CONNECTIONCONPTYWINDOWS_H
+#include "lldb/Host/windows/ConPTYUtils.h"
#include "lldb/Host/windows/ConnectionGenericFileWindows.h"
#include "lldb/Host/windows/PseudoConsole.h"
#include "lldb/Host/windows/windows.h"
diff --git a/lldb/source/Host/CMakeLists.txt b/lldb/source/Host/CMakeLists.txt
index d52183f2f968e..e1329af67de32 100644
--- a/lldb/source/Host/CMakeLists.txt
+++ b/lldb/source/Host/CMakeLists.txt
@@ -17,6 +17,7 @@ macro(add_host_subdirectory group)
endmacro()
add_host_subdirectory(common
+ common/ConPTYUtils.cpp
common/DiagnosticsRendering.cpp
common/FileAction.cpp
common/FileCache.cpp
diff --git a/lldb/source/Host/common/ConPTYUtils.cpp b/lldb/source/Host/common/ConPTYUtils.cpp
new file mode 100644
index 0000000000000..1b4b5e9cf71f8
--- /dev/null
+++ b/lldb/source/Host/common/ConPTYUtils.cpp
@@ -0,0 +1,78 @@
+//===-- ConPTYUtils.cpp ---------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Host/windows/ConPTYUtils.h"
+#include <cstring>
+
+using namespace lldb_private;
+
+void lldb_private::StripConPTYSequences(void *data, size_t &len,
+ bool strip_init) {
+ auto *buf = static_cast<char *>(data);
+ char *out = buf;
+ const char *in = buf;
+ const char *end = buf + len;
+
+ while (in < end) {
+ if (*in != '\x1b') {
+ *out++ = *in++;
+ continue;
+ }
+
+ size_t remaining = end - in;
+
+ // \x1b[6n - cursor-position query (PSEUDOCONSOLE_INHERIT_CURSOR init)
+ if (remaining >= 4 && memcmp(in, "\x1b[6n", 4) == 0) {
+ in += 4;
+ continue;
+ }
+
+ if (strip_init) {
+ // \x1b[m - SGR reset (ConPTY init)
+ if (remaining >= 3 && memcmp(in, "\x1b[m", 3) == 0) {
+ in += 3;
+ continue;
+ }
+
+ // \x1b[?25h - show cursor (ConPTY init)
+ if (remaining >= 6 && memcmp(in, "\x1b[?25h", 6) == 0) {
+ in += 6;
+ continue;
+ }
+ }
+
+ // \x1b[?9001h / \x1b[?9001l - Win32 Input Mode enable/disable
+ if (remaining >= 8 && memcmp(in, "\x1b[?9001", 7) == 0 &&
+ (in[7] == 'h' || in[7] == 'l')) {
+ in += 8;
+ continue;
+ }
+
+ // \x1b[?1004h / \x1b[?1004l - focus-event reporting enable/disable
+ if (remaining >= 8 && memcmp(in, "\x1b[?1004", 7) == 0 &&
+ (in[7] == 'h' || in[7] == 'l')) {
+ in += 8;
+ continue;
+ }
+
+ // \x1b]0;...\x07 - ConPTY window-title OSC sequence
+ if (remaining >= 4 && in[1] == ']' && in[2] == '0' && in[3] == ';') {
+ const char *bel =
+ static_cast<const char *>(memchr(in + 4, '\x07', end - in - 4));
+ if (bel)
+ in = bel + 1;
+ else
+ in = end;
+ continue;
+ }
+
+ *out++ = *in++;
+ }
+
+ len = static_cast<size_t>(out - buf);
+}
diff --git a/lldb/source/Host/windows/ConnectionConPTYWindows.cpp b/lldb/source/Host/windows/ConnectionConPTYWindows.cpp
index dd097b16fa190..ddbfbbc594a98 100644
--- a/lldb/source/Host/windows/ConnectionConPTYWindows.cpp
+++ b/lldb/source/Host/windows/ConnectionConPTYWindows.cpp
@@ -15,86 +15,6 @@
using namespace lldb;
using namespace lldb_private;
-/// Remove ConPTY management sequences from a buffer in-place.
-///
-/// ConPTY injects several VT sequences into its output pipe that are not part
-/// of the inferior's output: a cursor-position query (\x1b[6n) emitted during
-/// PSEUDOCONSOLE_INHERIT_CURSOR initialisation, Win32 Input Mode toggles
-/// (\x1b[?9001h/l), focus-event toggles (\x1b[?1004h/l), and a window-title
-/// OSC sequence (\x1b]0;...\x07). These sequences must not reach the outer
-/// terminal.
-///
-/// \param[in,out] data Buffer containing raw ConPTY output.
-/// \param[in,out] len On entry, the number of valid bytes in \p data.
-/// Updated to the number of bytes after stripping.
-/// \param[in] strip_init If true, also strip init-only sequences (\x1b[m,
-/// \x1b[?25h) that ConPTY emits at startup.
-static void StripConPTYSequences(void *data, size_t &len, bool strip_init) {
- auto *buf = static_cast<char *>(data);
- char *out = buf;
- const char *in = buf;
- const char *end = buf + len;
-
- while (in < end) {
- if (*in != '\x1b') {
- *out++ = *in++;
- continue;
- }
-
- size_t remaining = end - in;
-
- // \x1b[6n - cursor-position query (PSEUDOCONSOLE_INHERIT_CURSOR init)
- // This query is always replied to in OpenPseudoConsole.
- if (remaining >= 4 && memcmp(in, "\x1b[6n", 4) == 0) {
- in += 4;
- continue;
- }
-
- if (strip_init) {
- // \x1b[m - SGR reset (ConPTY init)
- if (remaining >= 3 && memcmp(in, "\x1b[m", 3) == 0) {
- in += 3;
- continue;
- }
-
- // \x1b[?25h - show cursor (ConPTY init)
- if (remaining >= 6 && memcmp(in, "\x1b[?25h", 6) == 0) {
- in += 6;
- continue;
- }
- }
-
- // \x1b[?9001h / \x1b[?9001l - Win32 Input Mode enable/disable
- if (remaining >= 8 && memcmp(in, "\x1b[?9001", 7) == 0 &&
- (in[7] == 'h' || in[7] == 'l')) {
- in += 8;
- continue;
- }
-
- // \x1b[?1004h / \x1b[?1004l - focus-event reporting enable/disable
- if (remaining >= 8 && memcmp(in, "\x1b[?1004", 7) == 0 &&
- (in[7] == 'h' || in[7] == 'l')) {
- in += 8;
- continue;
- }
-
- // \x1b]0;...\x07 - ConPTY window-title OSC sequence
- if (remaining >= 4 && in[1] == ']' && in[2] == '0' && in[3] == ';') {
- const char *bel =
- static_cast<const char *>(memchr(in + 4, '\x07', end - in - 4));
- // We assume a sequence is not split accross multiple chunks.
- if (bel)
- in = bel + 1;
- else
- in = end;
- continue;
- }
-
- *out++ = *in++;
- }
-
- len = static_cast<size_t>(out - buf);
-}
ConnectionConPTY::ConnectionConPTY(std::shared_ptr<PseudoConsole> pty)
: ConnectionGenericFile(pty->GetSTDOUTHandle(), false), m_pty(pty) {}
diff --git a/lldb/unittests/Host/CMakeLists.txt b/lldb/unittests/Host/CMakeLists.txt
index 5591edda38aca..82dcd0b480b67 100644
--- a/lldb/unittests/Host/CMakeLists.txt
+++ b/lldb/unittests/Host/CMakeLists.txt
@@ -12,6 +12,7 @@ set (FILES
ProcessLaunchInfoTest.cpp
SocketAddressTest.cpp
SocketTest.cpp
+ StripConPTYSequencesTest.cpp
ThreadLauncherTest.cpp
XMLTest.cpp
)
diff --git a/lldb/unittests/Host/StripConPTYSequencesTest.cpp b/lldb/unittests/Host/StripConPTYSequencesTest.cpp
new file mode 100644
index 0000000000000..ad91b2636f872
--- /dev/null
+++ b/lldb/unittests/Host/StripConPTYSequencesTest.cpp
@@ -0,0 +1,132 @@
+//===-- StripConPTYSequencesTest.cpp ---------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Host/windows/ConPTYUtils.h"
+#include "gtest/gtest.h"
+#include <cstring>
+#include <string>
+
+using namespace lldb_private;
+
+namespace {
+
+std::string Strip(const std::string &input, bool strip_init) {
+ std::string buf(input);
+ size_t len = buf.size();
+ StripConPTYSequences(buf.data(), len, strip_init);
+ return buf.substr(0, len);
+}
+
+} // namespace
+
+TEST(StripConPTYSequencesTest, PassthroughPlainText) {
+ EXPECT_EQ("Hello World!\r\n", Strip("Hello World!\r\n", true));
+}
+
+TEST(StripConPTYSequencesTest, EmptyInput) {
+ EXPECT_EQ("", Strip("", true));
+}
+
+TEST(StripConPTYSequencesTest, StripCursorQuery) {
+ EXPECT_EQ("", Strip("\x1b[6n", true));
+ EXPECT_EQ("abc", Strip("abc\x1b[6n", true));
+ EXPECT_EQ("abc", Strip("\x1b[6nabc", true));
+}
+
+TEST(StripConPTYSequencesTest, StripWin32InputMode) {
+ EXPECT_EQ("", Strip("\x1b[?9001h", true));
+ EXPECT_EQ("", Strip("\x1b[?9001l", true));
+ EXPECT_EQ("abc", Strip("\x1b[?9001habc", true));
+ EXPECT_EQ("abc", Strip("abc\x1b[?9001l", true));
+}
+
+TEST(StripConPTYSequencesTest, StripFocusEvents) {
+ EXPECT_EQ("", Strip("\x1b[?1004h", true));
+ EXPECT_EQ("", Strip("\x1b[?1004l", true));
+ EXPECT_EQ("text", Strip("\x1b[?1004htext\x1b[?1004l", true));
+}
+
+TEST(StripConPTYSequencesTest, StripWindowTitle) {
+ EXPECT_EQ("", Strip("\x1b]0;My Title\x07", true));
+ EXPECT_EQ("abc", Strip("abc\x1b]0;window\x07", true));
+ EXPECT_EQ("abc", Strip("\x1b]0;title\x07" "abc", true));
+}
+
+TEST(StripConPTYSequencesTest, WindowTitleWithoutBEL) {
+ // If BEL is missing, strip to end of buffer.
+ EXPECT_EQ("abc", Strip("abc\x1b]0;unterminated title", true));
+}
+
+TEST(StripConPTYSequencesTest, StripSGRResetOnlyWhenInit) {
+ EXPECT_EQ("", Strip("\x1b[m", true));
+ // With strip_init=false, \x1b[m is preserved.
+ EXPECT_EQ("\x1b[m", Strip("\x1b[m", false));
+}
+
+TEST(StripConPTYSequencesTest, StripShowCursorOnlyWhenInit) {
+ EXPECT_EQ("", Strip("\x1b[?25h", true));
+ // With strip_init=false, \x1b[?25h is preserved.
+ EXPECT_EQ("\x1b[?25h", Strip("\x1b[?25h", false));
+}
+
+TEST(StripConPTYSequencesTest, AlwaysStripNonInitSequences) {
+ // Cursor query, Win32 Input Mode, focus events, and window title are
+ // stripped regardless of strip_init.
+ EXPECT_EQ("", Strip("\x1b[6n", false));
+ EXPECT_EQ("", Strip("\x1b[?9001h", false));
+ EXPECT_EQ("", Strip("\x1b[?1004l", false));
+ EXPECT_EQ("", Strip("\x1b]0;title\x07", false));
+}
+
+TEST(StripConPTYSequencesTest, PreserveUnrecognizedEscape) {
+ // An ESC sequence we don't recognize should be passed through.
+ EXPECT_EQ("\x1b[31m", Strip("\x1b[31m", true));
+ EXPECT_EQ("\x1b[H", Strip("\x1b[H", true));
+}
+
+TEST(StripConPTYSequencesTest, TypicalConPTYInitBurst) {
+ // Simulates the first read from ConPTY with PSEUDOCONSOLE_INHERIT_CURSOR.
+ std::string init = "\x1b[6n" // cursor query
+ "\x1b[?9001h" // Win32 Input Mode on
+ "\x1b[?1004h" // focus events on
+ "\x1b[m" // SGR reset
+ "\x1b]0;C:\\app\x07" // window title
+ "\x1b[?25h"; // show cursor
+ EXPECT_EQ("", Strip(init, true));
+}
+
+TEST(StripConPTYSequencesTest, InitBurstWithInferiorOutput) {
+ std::string input = "\x1b[6n\x1b[?9001h\x1b[?1004h\x1b[m"
+ "\x1b]0;app\x07\x1b[?25h"
+ "Hello World!\r\n";
+ EXPECT_EQ("Hello World!\r\n", Strip(input, true));
+}
+
+TEST(StripConPTYSequencesTest, ShutdownSequences) {
+ // ConPTY emits these when the process exits.
+ std::string shutdown = "\x1b[?9001l\x1b[?1004l\r\n";
+ EXPECT_EQ("\r\n", Strip(shutdown, false));
+}
+
+TEST(StripConPTYSequencesTest, MultipleSequencesInterleaved) {
+ std::string input = "aaa\x1b[6nbbb\x1b[?9001hccc";
+ EXPECT_EQ("aaabbbccc", Strip(input, true));
+}
+
+TEST(StripConPTYSequencesTest, PartialEscapeAtEnd) {
+ // A lone ESC at the end of buffer - not a recognized sequence, pass through.
+ std::string input = "text\x1b";
+ EXPECT_EQ("text\x1b", Strip(input, true));
+}
+
+TEST(StripConPTYSequencesTest, LenUpdatedToZero) {
+ std::string buf = "\x1b[6n";
+ size_t len = buf.size();
+ StripConPTYSequences(buf.data(), len, true);
+ EXPECT_EQ(0u, len);
+}
>From 676865a865b6257e8d37fec2578fbb3810fc7a30 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Tue, 28 Apr 2026 16:12:27 +0100
Subject: [PATCH 2/3] fixup! [lldb][windows] add unit tests for the
StripConPTYSequences method
---
.../Host/windows/ConnectionConPTYWindows.cpp | 1 -
.../Host/StripConPTYSequencesTest.cpp | 21 ++++++++++---------
2 files changed, 11 insertions(+), 11 deletions(-)
diff --git a/lldb/source/Host/windows/ConnectionConPTYWindows.cpp b/lldb/source/Host/windows/ConnectionConPTYWindows.cpp
index ddbfbbc594a98..d370aa7a77e1b 100644
--- a/lldb/source/Host/windows/ConnectionConPTYWindows.cpp
+++ b/lldb/source/Host/windows/ConnectionConPTYWindows.cpp
@@ -15,7 +15,6 @@
using namespace lldb;
using namespace lldb_private;
-
ConnectionConPTY::ConnectionConPTY(std::shared_ptr<PseudoConsole> pty)
: ConnectionGenericFile(pty->GetSTDOUTHandle(), false), m_pty(pty) {}
diff --git a/lldb/unittests/Host/StripConPTYSequencesTest.cpp b/lldb/unittests/Host/StripConPTYSequencesTest.cpp
index ad91b2636f872..1796f43d32799 100644
--- a/lldb/unittests/Host/StripConPTYSequencesTest.cpp
+++ b/lldb/unittests/Host/StripConPTYSequencesTest.cpp
@@ -1,4 +1,5 @@
-//===-- StripConPTYSequencesTest.cpp ---------------------------------------===//
+//===-- StripConPTYSequencesTest.cpp
+//---------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -28,9 +29,7 @@ TEST(StripConPTYSequencesTest, PassthroughPlainText) {
EXPECT_EQ("Hello World!\r\n", Strip("Hello World!\r\n", true));
}
-TEST(StripConPTYSequencesTest, EmptyInput) {
- EXPECT_EQ("", Strip("", true));
-}
+TEST(StripConPTYSequencesTest, EmptyInput) { EXPECT_EQ("", Strip("", true)); }
TEST(StripConPTYSequencesTest, StripCursorQuery) {
EXPECT_EQ("", Strip("\x1b[6n", true));
@@ -54,7 +53,9 @@ TEST(StripConPTYSequencesTest, StripFocusEvents) {
TEST(StripConPTYSequencesTest, StripWindowTitle) {
EXPECT_EQ("", Strip("\x1b]0;My Title\x07", true));
EXPECT_EQ("abc", Strip("abc\x1b]0;window\x07", true));
- EXPECT_EQ("abc", Strip("\x1b]0;title\x07" "abc", true));
+ EXPECT_EQ("abc", Strip("\x1b]0;title\x07"
+ "abc",
+ true));
}
TEST(StripConPTYSequencesTest, WindowTitleWithoutBEL) {
@@ -91,12 +92,12 @@ TEST(StripConPTYSequencesTest, PreserveUnrecognizedEscape) {
TEST(StripConPTYSequencesTest, TypicalConPTYInitBurst) {
// Simulates the first read from ConPTY with PSEUDOCONSOLE_INHERIT_CURSOR.
- std::string init = "\x1b[6n" // cursor query
- "\x1b[?9001h" // Win32 Input Mode on
- "\x1b[?1004h" // focus events on
- "\x1b[m" // SGR reset
+ std::string init = "\x1b[6n" // cursor query
+ "\x1b[?9001h" // Win32 Input Mode on
+ "\x1b[?1004h" // focus events on
+ "\x1b[m" // SGR reset
"\x1b]0;C:\\app\x07" // window title
- "\x1b[?25h"; // show cursor
+ "\x1b[?25h"; // show cursor
EXPECT_EQ("", Strip(init, true));
}
>From 1d8dbc7d0c4651383260b30b7128ae6a3d66d1c0 Mon Sep 17 00:00:00 2001
From: Charles Zablit <zablitcharles at gmail.com>
Date: Tue, 28 Apr 2026 17:01:22 +0100
Subject: [PATCH 3/3] Apply suggestions from code review
Co-authored-by: Nerixyz <nero.9 at hotmail.de>
---
lldb/source/Host/common/ConPTYUtils.cpp | 2 +-
lldb/unittests/Host/StripConPTYSequencesTest.cpp | 3 +--
2 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/lldb/source/Host/common/ConPTYUtils.cpp b/lldb/source/Host/common/ConPTYUtils.cpp
index 1b4b5e9cf71f8..0450f4d5dcc4a 100644
--- a/lldb/source/Host/common/ConPTYUtils.cpp
+++ b/lldb/source/Host/common/ConPTYUtils.cpp
@@ -1,4 +1,4 @@
-//===-- ConPTYUtils.cpp ---------------------------------------------------===//
+//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
diff --git a/lldb/unittests/Host/StripConPTYSequencesTest.cpp b/lldb/unittests/Host/StripConPTYSequencesTest.cpp
index 1796f43d32799..f181eebb75a44 100644
--- a/lldb/unittests/Host/StripConPTYSequencesTest.cpp
+++ b/lldb/unittests/Host/StripConPTYSequencesTest.cpp
@@ -1,5 +1,4 @@
-//===-- StripConPTYSequencesTest.cpp
-//---------------------------------------===//
+//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
More information about the lldb-commits
mailing list