LLDB mainline
ProcessIOHandler.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 LLDB_TARGET_PROCESSIOHANDLER_H
10#define LLDB_TARGET_PROCESSIOHANDLER_H
11
12#include "lldb/Core/IOHandler.h"
13#include "lldb/Host/File.h"
14#include "lldb/Host/Pipe.h"
15#include "lldb/Target/Process.h"
16
17namespace lldb_private {
18
19/// Forwards lldb's STDIN to the inferior's pty (or anything writable) and
20/// supports asynchronous interrupt via an internal pipe. Used on POSIX hosts
21/// and as a no-op stub on Windows.
23public:
24 IOHandlerProcessSTDIO(Process *process, int write_fd);
25
26 ~IOHandlerProcessSTDIO() override = default;
27
28 void SetIsRunning(bool running);
29
30 void Run() override;
31
32 void Cancel() override;
33
34 bool Interrupt() override;
35
36 void GotEOF() override {}
37
38protected:
40 /// Read from this file (usually actual STDIN for LLDB)
42 /// Write to this file (usually the primary pty for getting io to debuggee)
45 std::mutex m_mutex;
46 bool m_is_running = false;
47};
48
49#ifdef _WIN32
50
51using HANDLE = void *;
52
53/// Forwards lldb's STDIN to the inferior on Windows hosts. Reads from the
54/// console (handling the line-buffering quirks of the Windows console) and
55/// writes the bytes into the process via Process::PutSTDIN.
56class IOHandlerProcessSTDIOWindows : public IOHandler {
57public:
58 IOHandlerProcessSTDIOWindows(Process *process);
59
60 ~IOHandlerProcessSTDIOWindows() override;
61
62 void SetIsRunning(bool running);
63
64 /// Peek the console for input. If it has any, drain the pipe until text
65 /// input is found or the pipe is empty.
66 ///
67 /// \param hStdin
68 /// The handle to the standard input's pipe.
69 ///
70 /// \return
71 /// true if the pipe has text input.
72 llvm::Expected<bool> ConsoleHasTextInput(const HANDLE hStdin);
73
74 void Run() override;
75
76 void Cancel() override;
77
78 bool Interrupt() override;
79
80 void GotEOF() override {}
81
82private:
83 enum ControlOp : char {
84 eControlOpQuit = 'q',
85 eControlOpInterrupt = 'i',
86 eControlOpNone = 0,
87 };
88
89 Process *m_process;
90 /// Read from this file (usually actual STDIN for LLDB)
91 NativeFile m_read_file;
92 HANDLE m_interrupt_event =
93 reinterpret_cast<HANDLE>(static_cast<intptr_t>(-1));
94 std::atomic<ControlOp> m_pending_op{eControlOpNone};
95 std::mutex m_mutex;
96 bool m_is_running = false;
97};
98
99#endif // _WIN32
100
101} // namespace lldb_private
102
103#endif // LLDB_TARGET_PROCESSIOHANDLER_H
void * HANDLE
~IOHandlerProcessSTDIO() override=default
NativeFile m_write_file
Write to this file (usually the primary pty for getting io to debuggee)
NativeFile m_read_file
Read from this file (usually actual STDIN for LLDB)
IOHandlerProcessSTDIO(Process *process, int write_fd)
IOHandler(Debugger &debugger, IOHandler::Type type)
Definition IOHandler.cpp:55
A plug-in interface definition class for debugging a process.
Definition Process.h:357
A class that represents a running process on the host machine.
NativeFilePosix NativeFile
Definition File.h:29
PipePosix Pipe
Definition Pipe.h:20