LLDB mainline
PseudoConsole.h
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
9#ifndef LIBLLDB_HOST_WINDOWS_PSEUDOCONSOLE_H_
10#define LIBLLDB_HOST_WINDOWS_PSEUDOCONSOLE_H_
11
12#include "llvm/Support/Error.h"
13#include <mutex>
14#include <string>
15
16#define PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE 0x20016
17typedef void *HANDLE;
18typedef void *HPCON;
19
20namespace lldb_private {
21
23
24public:
25 PseudoConsole() = default;
27
28 PseudoConsole(const PseudoConsole &) = delete;
32
33 /// Creates and opens a new ConPTY instance with a default console size of
34 /// 80x25. Also sets up the associated STDIN/STDOUT pipes and drains any
35 /// initialization sequences emitted by Windows.
36 ///
37 /// \return
38 /// An llvm::Error if the ConPTY could not be created, or if ConPTY is
39 /// not available on this version of Windows, llvm::Error::success()
40 /// otherwise.
41 llvm::Error OpenPseudoConsole();
42
43 /// Closes the ConPTY and invalidates its handle, without closing the STDIN
44 /// and STDOUT pipes. Closing the ConPTY signals EOF to any process currently
45 /// attached to it.
46 void Close();
47
48 /// Closes the STDIN and STDOUT pipe handles and invalidates them
49 void ClosePipes();
50
51 /// Returns whether the ConPTY and its pipes are currently open and valid.
52 bool IsConnected() const;
53
54 /// The ConPTY HPCON handle accessor.
55 ///
56 /// This object retains ownership of the HPCON when this accessor is used.
57 ///
58 /// \return
59 /// The ConPTY HPCON handle, or INVALID_HANDLE_VALUE if it is currently
60 /// invalid.
62
63 /// The STDOUT read HANDLE accessor.
64 ///
65 /// This object retains ownership of the HANDLE when this accessor is used.
66 ///
67 /// \return
68 /// The STDOUT read HANDLE, or INVALID_HANDLE_VALUE if it is currently
69 /// invalid.
71
72 /// The STDIN write HANDLE accessor.
73 ///
74 /// This object retains ownership of the HANDLE when this accessor is used.
75 ///
76 /// \return
77 /// The STDIN write HANDLE, or INVALID_HANDLE_VALUE if it is currently
78 /// invalid.
80
81 /// Drains initialization sequences from the ConPTY output pipe.
82 ///
83 /// When a process first attaches to a ConPTY, Windows emits VT100/ANSI escape
84 /// sequences (ESC[2J for clear screen, ESC[H for cursor home and more) as
85 /// part of the PseudoConsole initialization. To prevent these sequences from
86 /// appearing in the debugger output (and flushing lldb's shell for instance)
87 /// we launch a short-lived dummy process that triggers the initialization,
88 /// then drain all output before launching the actual debuggee.
89 llvm::Error DrainInitSequences();
90
91 /// Returns a reference to the mutex used to synchronize access to the
92 /// ConPTY state.
93 std::mutex &GetMutex() { return m_mutex; };
94
95 /// Returns a reference to the condition variable used to signal state changes
96 /// to threads waiting on the ConPTY (e.g. waiting for output or shutdown).
97 std::condition_variable &GetCV() { return m_cv; };
98
99 /// Returns whether the ConPTY is in the process of shutting down.
100 ///
101 /// \return
102 /// A reference to the atomic bool that is set to true when the ConPTY
103 /// is stopping. Callers should check this in their read/write loops to
104 /// exit gracefully.
105 bool IsStopping() const { return m_stopping.load(); };
106
107 /// Sets the stopping flag to \p value, signalling to threads waiting on the
108 /// ConPTY that they should stop.
109 void SetStopping(bool value) { m_stopping = value; };
110
111protected:
112 HANDLE m_conpty_handle = ((HANDLE)(long long)-1);
113 HANDLE m_conpty_output = ((HANDLE)(long long)-1);
114 HANDLE m_conpty_input = ((HANDLE)(long long)-1);
115 std::mutex m_mutex{};
116 std::condition_variable m_cv{};
117 std::atomic<bool> m_stopping = false;
118};
119} // namespace lldb_private
120
121#endif // LIBLLDB_HOST_WINDOWS_PSEUDOCONSOLE_H_
void * HPCON
void * HANDLE
PseudoConsole(const PseudoConsole &)=delete
PseudoConsole & operator=(const PseudoConsole &)=delete
llvm::Error OpenPseudoConsole()
Creates and opens a new ConPTY instance with a default console size of 80x25.
bool IsStopping() const
Returns whether the ConPTY is in the process of shutting down.
void ClosePipes()
Closes the STDIN and STDOUT pipe handles and invalidates them.
std::condition_variable & GetCV()
Returns a reference to the condition variable used to signal state changes to threads waiting on the ...
std::atomic< bool > m_stopping
std::condition_variable m_cv
std::mutex & GetMutex()
Returns a reference to the mutex used to synchronize access to the ConPTY state.
void SetStopping(bool value)
Sets the stopping flag to value, signalling to threads waiting on the ConPTY that they should stop.
PseudoConsole & operator=(PseudoConsole &&)=delete
HANDLE GetSTDOUTHandle() const
The STDOUT read HANDLE accessor.
void Close()
Closes the ConPTY and invalidates its handle, without closing the STDIN and STDOUT pipes.
llvm::Error DrainInitSequences()
Drains initialization sequences from the ConPTY output pipe.
bool IsConnected() const
Returns whether the ConPTY and its pipes are currently open and valid.
HPCON GetPseudoTerminalHandle()
The ConPTY HPCON handle accessor.
PseudoConsole(PseudoConsole &&)=delete
HANDLE GetSTDINHandle() const
The STDIN write HANDLE accessor.
A class that represents a running process on the host machine.