[Lldb-commits] [lldb] 6617aac - [lldb][windows] add unit tests for the StripConPTYSequences method (#194654)
via lldb-commits
lldb-commits at lists.llvm.org
Wed Apr 29 04:05:30 PDT 2026
Author: Charles Zablit
Date: 2026-04-29T12:05:25+01:00
New Revision: 6617aac292a1718c9adb7dcd4f5aaab0cd5f39d7
URL: https://github.com/llvm/llvm-project/commit/6617aac292a1718c9adb7dcd4f5aaab0cd5f39d7
DIFF: https://github.com/llvm/llvm-project/commit/6617aac292a1718c9adb7dcd4f5aaab0cd5f39d7.diff
LOG: [lldb][windows] add unit tests for the StripConPTYSequences method (#194654)
Co-authored-by: Nerixyz <nero.9 at hotmail.de>
Added:
lldb/include/lldb/Host/windows/ConPTYUtils.h
lldb/source/Host/common/ConPTYUtils.cpp
lldb/unittests/Host/StripConPTYSequencesTest.cpp
Modified:
lldb/include/lldb/Host/windows/ConnectionConPTYWindows.h
lldb/source/Host/CMakeLists.txt
lldb/source/Host/windows/ConnectionConPTYWindows.cpp
lldb/unittests/Host/CMakeLists.txt
Removed:
################################################################################
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..0450f4d5dcc4a
--- /dev/null
+++ b/lldb/source/Host/common/ConPTYUtils.cpp
@@ -0,0 +1,78 @@
+//===----------------------------------------------------------------------===//
+//
+// 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..d370aa7a77e1b 100644
--- a/lldb/source/Host/windows/ConnectionConPTYWindows.cpp
+++ b/lldb/source/Host/windows/ConnectionConPTYWindows.cpp
@@ -15,87 +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..f181eebb75a44
--- /dev/null
+++ b/lldb/unittests/Host/StripConPTYSequencesTest.cpp
@@ -0,0 +1,132 @@
+//===----------------------------------------------------------------------===//
+//
+// 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);
+}
More information about the lldb-commits
mailing list