LLDB mainline
ConnectionConPTYWindows.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
10#include "lldb/Utility/Status.h"
11
12using namespace lldb;
13using namespace lldb_private;
14
15/// Strips the ConPTY initialization sequences that Windows unconditionally
16/// emits when a process is first attached to a pseudo console.
17///
18/// These are emitted by ConPTY's host process (conhost.exe) at process attach
19/// time, not by the debuggee. They are always the first bytes on the output
20/// pipe and are always present as a contiguous prefix.
21///
22/// \param dst Buffer containing the data read from the ConPTY output pipe.
23/// Modified in place: if the initialization sequences are present
24/// as a prefix, they are removed by shifting the remaining bytes
25/// to the front of the buffer.
26/// \param dst_len The size of \p dst.
27/// \param len On input, the number of valid bytes in \p dst. On output,
28/// reduced by the number of bytes stripped.
29/// \return
30/// \p true if the sequence was found and stripped.
31static bool StripConPTYInitSequences(void *dst, size_t dst_len, size_t &len) {
32 static const char sequences[] = "\x1b[?9001l\x1b[?1004l";
33 static const size_t sequences_len = sizeof(sequences) - 1;
34 char *buf = static_cast<char *>(dst);
35 if (len >= sequences_len) {
36 assert(dst_len >= len - sequences_len);
37 if (memcmp(buf, sequences, sequences_len) == 0) {
38 memmove(buf, buf + sequences_len, len - sequences_len);
39 len -= sequences_len;
40 return true;
41 }
42 }
43 return false;
44}
45
46ConnectionConPTY::ConnectionConPTY(std::shared_ptr<PseudoConsole> pty)
47 : m_pty(pty), ConnectionGenericFile(pty->GetSTDOUTHandle(), false) {};
48
50
52 Status *error_ptr) {
53 if (m_pty->IsConnected())
56}
57
62
63size_t ConnectionConPTY::Read(void *dst, size_t dst_len,
64 const Timeout<std::micro> &timeout,
66 Status *error_ptr) {
67 std::unique_lock<std::mutex> guard(m_pty->GetMutex());
68 if (m_pty->IsStopping()) {
69 m_pty->GetCV().wait(guard, [this] { return !m_pty->IsStopping(); });
70 }
71
72 size_t bytes_read =
73 ConnectionGenericFile::Read(dst, dst_len, timeout, status, error_ptr);
74
75 if (bytes_read > 0 && !m_pty_vt_sequence_was_stripped) {
76 if (StripConPTYInitSequences(dst, dst_len, bytes_read))
78 }
79
80 return bytes_read;
81}
82
83size_t ConnectionConPTY::Write(const void *src, size_t src_len,
85 Status *error_ptr) {
86 llvm_unreachable("not implemented");
87}
static bool StripConPTYInitSequences(void *dst, size_t dst_len, size_t &len)
Strips the ConPTY initialization sequences that Windows unconditionally emits when a process is first...
lldb::ConnectionStatus Disconnect(Status *error_ptr) override
Disconnect the communications connection if one is currently connected.
size_t Write(const void *src, size_t src_len, lldb::ConnectionStatus &status, Status *error_ptr) override
The actual write function that attempts to write to the communications protocol.
ConnectionConPTY(std::shared_ptr< PseudoConsole > pty)
size_t Read(void *dst, size_t dst_len, const Timeout< std::micro > &timeout, lldb::ConnectionStatus &status, Status *error_ptr) override
Read from the ConPTY's pipe.
std::shared_ptr< PseudoConsole > m_pty
lldb::ConnectionStatus Connect(llvm::StringRef s, Status *error_ptr) override
Connect using the connect string url.
size_t Read(void *dst, size_t dst_len, const Timeout< std::micro > &timeout, lldb::ConnectionStatus &status, Status *error_ptr) override
The read function that attempts to read from the connection.
An error handling class.
Definition Status.h:118
A class that represents a running process on the host machine.
ConnectionStatus
Connection Status Types.
@ eConnectionStatusSuccess
Success.
@ eConnectionStatusNoConnection
No connection.