1*be691f3bSpatrick //===-- RunInTerminal.h ----------------------------------------*- C++ -*-===// 2*be691f3bSpatrick // 3*be691f3bSpatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*be691f3bSpatrick // See https://llvm.org/LICENSE.txt for license information. 5*be691f3bSpatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*be691f3bSpatrick // 7*be691f3bSpatrick //===----------------------------------------------------------------------===// 8*be691f3bSpatrick 9*be691f3bSpatrick #ifndef LLDB_TOOLS_LLDB_VSCODE_RUNINTERMINAL_H 10*be691f3bSpatrick #define LLDB_TOOLS_LLDB_VSCODE_RUNINTERMINAL_H 11*be691f3bSpatrick 12*be691f3bSpatrick #include "FifoFiles.h" 13*be691f3bSpatrick 14*be691f3bSpatrick #include <future> 15*be691f3bSpatrick #include <thread> 16*be691f3bSpatrick 17*be691f3bSpatrick namespace lldb_vscode { 18*be691f3bSpatrick 19*be691f3bSpatrick enum RunInTerminalMessageKind { 20*be691f3bSpatrick eRunInTerminalMessageKindPID = 0, 21*be691f3bSpatrick eRunInTerminalMessageKindError, 22*be691f3bSpatrick eRunInTerminalMessageKindDidAttach, 23*be691f3bSpatrick }; 24*be691f3bSpatrick 25*be691f3bSpatrick struct RunInTerminalMessage; 26*be691f3bSpatrick struct RunInTerminalMessagePid; 27*be691f3bSpatrick struct RunInTerminalMessageError; 28*be691f3bSpatrick struct RunInTerminalMessageDidAttach; 29*be691f3bSpatrick 30*be691f3bSpatrick struct RunInTerminalMessage { 31*be691f3bSpatrick RunInTerminalMessage(RunInTerminalMessageKind kind); 32*be691f3bSpatrick 33*be691f3bSpatrick virtual ~RunInTerminalMessage() = default; 34*be691f3bSpatrick 35*be691f3bSpatrick /// Serialize this object to JSON 36*be691f3bSpatrick virtual llvm::json::Value ToJSON() const = 0; 37*be691f3bSpatrick 38*be691f3bSpatrick const RunInTerminalMessagePid *GetAsPidMessage() const; 39*be691f3bSpatrick 40*be691f3bSpatrick const RunInTerminalMessageError *GetAsErrorMessage() const; 41*be691f3bSpatrick 42*be691f3bSpatrick RunInTerminalMessageKind kind; 43*be691f3bSpatrick }; 44*be691f3bSpatrick 45*be691f3bSpatrick using RunInTerminalMessageUP = std::unique_ptr<RunInTerminalMessage>; 46*be691f3bSpatrick 47*be691f3bSpatrick struct RunInTerminalMessagePid : RunInTerminalMessage { 48*be691f3bSpatrick RunInTerminalMessagePid(lldb::pid_t pid); 49*be691f3bSpatrick 50*be691f3bSpatrick llvm::json::Value ToJSON() const override; 51*be691f3bSpatrick 52*be691f3bSpatrick lldb::pid_t pid; 53*be691f3bSpatrick }; 54*be691f3bSpatrick 55*be691f3bSpatrick struct RunInTerminalMessageError : RunInTerminalMessage { 56*be691f3bSpatrick RunInTerminalMessageError(llvm::StringRef error); 57*be691f3bSpatrick 58*be691f3bSpatrick llvm::json::Value ToJSON() const override; 59*be691f3bSpatrick 60*be691f3bSpatrick std::string error; 61*be691f3bSpatrick }; 62*be691f3bSpatrick 63*be691f3bSpatrick struct RunInTerminalMessageDidAttach : RunInTerminalMessage { 64*be691f3bSpatrick RunInTerminalMessageDidAttach(); 65*be691f3bSpatrick 66*be691f3bSpatrick llvm::json::Value ToJSON() const override; 67*be691f3bSpatrick }; 68*be691f3bSpatrick 69*be691f3bSpatrick class RunInTerminalLauncherCommChannel { 70*be691f3bSpatrick public: 71*be691f3bSpatrick RunInTerminalLauncherCommChannel(llvm::StringRef comm_file); 72*be691f3bSpatrick 73*be691f3bSpatrick /// Wait until the debug adaptor attaches. 74*be691f3bSpatrick /// 75*be691f3bSpatrick /// \param[in] timeout 76*be691f3bSpatrick /// How long to wait to be attached. 77*be691f3bSpatrick // 78*be691f3bSpatrick /// \return 79*be691f3bSpatrick /// An \a llvm::Error object in case of errors or if this operation times 80*be691f3bSpatrick /// out. 81*be691f3bSpatrick llvm::Error WaitUntilDebugAdaptorAttaches(std::chrono::milliseconds timeout); 82*be691f3bSpatrick 83*be691f3bSpatrick /// Notify the debug adaptor this process' pid. 84*be691f3bSpatrick /// 85*be691f3bSpatrick /// \return 86*be691f3bSpatrick /// An \a llvm::Error object in case of errors or if this operation times 87*be691f3bSpatrick /// out. 88*be691f3bSpatrick llvm::Error NotifyPid(); 89*be691f3bSpatrick 90*be691f3bSpatrick /// Notify the debug adaptor that there's been an error. 91*be691f3bSpatrick void NotifyError(llvm::StringRef error); 92*be691f3bSpatrick 93*be691f3bSpatrick private: 94*be691f3bSpatrick FifoFileIO m_io; 95*be691f3bSpatrick }; 96*be691f3bSpatrick 97*be691f3bSpatrick class RunInTerminalDebugAdapterCommChannel { 98*be691f3bSpatrick public: 99*be691f3bSpatrick RunInTerminalDebugAdapterCommChannel(llvm::StringRef comm_file); 100*be691f3bSpatrick 101*be691f3bSpatrick /// Notify the runInTerminal launcher that it was attached. 102*be691f3bSpatrick /// 103*be691f3bSpatrick /// \return 104*be691f3bSpatrick /// A future indicated whether the runInTerminal launcher received the 105*be691f3bSpatrick /// message correctly or not. 106*be691f3bSpatrick std::future<lldb::SBError> NotifyDidAttach(); 107*be691f3bSpatrick 108*be691f3bSpatrick /// Fetch the pid of the runInTerminal launcher. 109*be691f3bSpatrick /// 110*be691f3bSpatrick /// \return 111*be691f3bSpatrick /// An \a llvm::Error object in case of errors or if this operation times 112*be691f3bSpatrick /// out. 113*be691f3bSpatrick llvm::Expected<lldb::pid_t> GetLauncherPid(); 114*be691f3bSpatrick 115*be691f3bSpatrick /// Fetch any errors emitted by the runInTerminal launcher or return a 116*be691f3bSpatrick /// default error message if a certain timeout if reached. 117*be691f3bSpatrick std::string GetLauncherError(); 118*be691f3bSpatrick 119*be691f3bSpatrick private: 120*be691f3bSpatrick FifoFileIO m_io; 121*be691f3bSpatrick }; 122*be691f3bSpatrick 123*be691f3bSpatrick /// Create a fifo file used to communicate the debug adaptor with 124*be691f3bSpatrick /// the runInTerminal launcher. 125*be691f3bSpatrick llvm::Expected<std::shared_ptr<FifoFile>> CreateRunInTerminalCommFile(); 126*be691f3bSpatrick 127*be691f3bSpatrick } // namespace lldb_vscode 128*be691f3bSpatrick 129*be691f3bSpatrick #endif // LLDB_TOOLS_LLDB_VSCODE_RUNINTERMINAL_H 130