[Lldb-commits] [lldb] [lldb][windows] inherit cursor's position when creating a ConPTY (PR #193818)

Charles Zablit via lldb-commits lldb-commits at lists.llvm.org
Mon Apr 27 11:52:05 PDT 2026


================
@@ -8,39 +8,83 @@
 
 #include "lldb/Host/windows/ConnectionConPTYWindows.h"
 #include "lldb/Utility/Status.h"
+#include "lldb/Utility/Timeout.h"
+
+#include <cstring>
 
 using namespace lldb;
 using namespace lldb_private;
 
-/// Strips the ConPTY initialization sequences that Windows unconditionally
-/// emits when a process is first attached to a pseudo console.
+/// Remove ConPTY management sequences from a buffer in-place.
 ///
-/// These are emitted by ConPTY's host process (conhost.exe) at process attach
-/// time, not by the debuggee. They are always the first bytes on the output
-/// pipe and are always present as a contiguous prefix.
+/// 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 dst  Buffer containing the data read from the ConPTY output pipe.
-///             Modified in place: if the initialization sequences are present
-///             as a prefix, they are removed by shifting the remaining bytes
-///             to the front of the buffer.
-/// \param dst_len The size of \p dst.
-/// \param len  On input, the number of valid bytes in \p dst. On output,
-///             reduced by the number of bytes stripped.
-/// \return
-///     \p true if the sequence was found and stripped.
-static bool StripConPTYInitSequences(void *dst, size_t dst_len, size_t &len) {
-  static const char sequences[] = "\x1b[?9001l\x1b[?1004l";
-  static const size_t sequences_len = sizeof(sequences) - 1;
-  char *buf = static_cast<char *>(dst);
-  if (len >= sequences_len) {
-    assert(dst_len >= len - sequences_len);
-    if (memcmp(buf, sequences, sequences_len) == 0) {
-      memmove(buf, buf + sequences_len, len - sequences_len);
-      len -= sequences_len;
-      return true;
+/// \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.
+/// \return  true if at least one sequence was stripped (caller should stop
+///          calling this function on future reads).
+static bool StripConPTYSequences(void *data, size_t &len) {
+  auto *buf = static_cast<char *>(data);
+  char *out = buf;
+  const char *in = buf;
+  const char *end = buf + len;
+  bool stripped = false;
+
+  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) {
----------------
charles-zablit wrote:

Fixed, thanks 👍 

https://github.com/llvm/llvm-project/pull/193818


More information about the lldb-commits mailing list