101263c6cSJonas Devlieghere //===-- RunInTerminal.h ----------------------------------------*- C++ -*-===// 201263c6cSJonas Devlieghere // 301263c6cSJonas Devlieghere // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 401263c6cSJonas Devlieghere // See https://llvm.org/LICENSE.txt for license information. 501263c6cSJonas Devlieghere // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 601263c6cSJonas Devlieghere // 701263c6cSJonas Devlieghere //===----------------------------------------------------------------------===// 801263c6cSJonas Devlieghere 901263c6cSJonas Devlieghere #ifndef LLDB_TOOLS_LLDB_DAP_RUNINTERMINAL_H 1001263c6cSJonas Devlieghere #define LLDB_TOOLS_LLDB_DAP_RUNINTERMINAL_H 1101263c6cSJonas Devlieghere 1201263c6cSJonas Devlieghere #include "FifoFiles.h" 13*09c258efSAdrian Vogelsgesang #include "lldb/API/SBError.h" 1401263c6cSJonas Devlieghere 1501263c6cSJonas Devlieghere #include <future> 16*09c258efSAdrian Vogelsgesang #include <memory> 17*09c258efSAdrian Vogelsgesang #include <string> 1801263c6cSJonas Devlieghere 1901263c6cSJonas Devlieghere namespace lldb_dap { 2001263c6cSJonas Devlieghere 2101263c6cSJonas Devlieghere enum RunInTerminalMessageKind { 2201263c6cSJonas Devlieghere eRunInTerminalMessageKindPID = 0, 2301263c6cSJonas Devlieghere eRunInTerminalMessageKindError, 2401263c6cSJonas Devlieghere eRunInTerminalMessageKindDidAttach, 2501263c6cSJonas Devlieghere }; 2601263c6cSJonas Devlieghere 2701263c6cSJonas Devlieghere struct RunInTerminalMessage; 2801263c6cSJonas Devlieghere struct RunInTerminalMessagePid; 2901263c6cSJonas Devlieghere struct RunInTerminalMessageError; 3001263c6cSJonas Devlieghere struct RunInTerminalMessageDidAttach; 3101263c6cSJonas Devlieghere 3201263c6cSJonas Devlieghere struct RunInTerminalMessage { 3301263c6cSJonas Devlieghere RunInTerminalMessage(RunInTerminalMessageKind kind); 3401263c6cSJonas Devlieghere 3501263c6cSJonas Devlieghere virtual ~RunInTerminalMessage() = default; 3601263c6cSJonas Devlieghere 3701263c6cSJonas Devlieghere /// Serialize this object to JSON 3801263c6cSJonas Devlieghere virtual llvm::json::Value ToJSON() const = 0; 3901263c6cSJonas Devlieghere 4001263c6cSJonas Devlieghere const RunInTerminalMessagePid *GetAsPidMessage() const; 4101263c6cSJonas Devlieghere 4201263c6cSJonas Devlieghere const RunInTerminalMessageError *GetAsErrorMessage() const; 4301263c6cSJonas Devlieghere 4401263c6cSJonas Devlieghere RunInTerminalMessageKind kind; 4501263c6cSJonas Devlieghere }; 4601263c6cSJonas Devlieghere 4701263c6cSJonas Devlieghere using RunInTerminalMessageUP = std::unique_ptr<RunInTerminalMessage>; 4801263c6cSJonas Devlieghere 4901263c6cSJonas Devlieghere struct RunInTerminalMessagePid : RunInTerminalMessage { 5001263c6cSJonas Devlieghere RunInTerminalMessagePid(lldb::pid_t pid); 5101263c6cSJonas Devlieghere 5201263c6cSJonas Devlieghere llvm::json::Value ToJSON() const override; 5301263c6cSJonas Devlieghere 5401263c6cSJonas Devlieghere lldb::pid_t pid; 5501263c6cSJonas Devlieghere }; 5601263c6cSJonas Devlieghere 5701263c6cSJonas Devlieghere struct RunInTerminalMessageError : RunInTerminalMessage { 5801263c6cSJonas Devlieghere RunInTerminalMessageError(llvm::StringRef error); 5901263c6cSJonas Devlieghere 6001263c6cSJonas Devlieghere llvm::json::Value ToJSON() const override; 6101263c6cSJonas Devlieghere 6201263c6cSJonas Devlieghere std::string error; 6301263c6cSJonas Devlieghere }; 6401263c6cSJonas Devlieghere 6501263c6cSJonas Devlieghere struct RunInTerminalMessageDidAttach : RunInTerminalMessage { 6601263c6cSJonas Devlieghere RunInTerminalMessageDidAttach(); 6701263c6cSJonas Devlieghere 6801263c6cSJonas Devlieghere llvm::json::Value ToJSON() const override; 6901263c6cSJonas Devlieghere }; 7001263c6cSJonas Devlieghere 7101263c6cSJonas Devlieghere class RunInTerminalLauncherCommChannel { 7201263c6cSJonas Devlieghere public: 7301263c6cSJonas Devlieghere RunInTerminalLauncherCommChannel(llvm::StringRef comm_file); 7401263c6cSJonas Devlieghere 7501263c6cSJonas Devlieghere /// Wait until the debug adaptor attaches. 7601263c6cSJonas Devlieghere /// 7701263c6cSJonas Devlieghere /// \param[in] timeout 7801263c6cSJonas Devlieghere /// How long to wait to be attached. 7901263c6cSJonas Devlieghere // 8001263c6cSJonas Devlieghere /// \return 8101263c6cSJonas Devlieghere /// An \a llvm::Error object in case of errors or if this operation times 8201263c6cSJonas Devlieghere /// out. 8301263c6cSJonas Devlieghere llvm::Error WaitUntilDebugAdaptorAttaches(std::chrono::milliseconds timeout); 8401263c6cSJonas Devlieghere 8501263c6cSJonas Devlieghere /// Notify the debug adaptor this process' pid. 8601263c6cSJonas Devlieghere /// 8701263c6cSJonas Devlieghere /// \return 8801263c6cSJonas Devlieghere /// An \a llvm::Error object in case of errors or if this operation times 8901263c6cSJonas Devlieghere /// out. 9001263c6cSJonas Devlieghere llvm::Error NotifyPid(); 9101263c6cSJonas Devlieghere 9201263c6cSJonas Devlieghere /// Notify the debug adaptor that there's been an error. 9301263c6cSJonas Devlieghere void NotifyError(llvm::StringRef error); 9401263c6cSJonas Devlieghere 9501263c6cSJonas Devlieghere private: 9601263c6cSJonas Devlieghere FifoFileIO m_io; 9701263c6cSJonas Devlieghere }; 9801263c6cSJonas Devlieghere 9901263c6cSJonas Devlieghere class RunInTerminalDebugAdapterCommChannel { 10001263c6cSJonas Devlieghere public: 10101263c6cSJonas Devlieghere RunInTerminalDebugAdapterCommChannel(llvm::StringRef comm_file); 10201263c6cSJonas Devlieghere 10301263c6cSJonas Devlieghere /// Notify the runInTerminal launcher that it was attached. 10401263c6cSJonas Devlieghere /// 10501263c6cSJonas Devlieghere /// \return 10601263c6cSJonas Devlieghere /// A future indicated whether the runInTerminal launcher received the 10701263c6cSJonas Devlieghere /// message correctly or not. 10801263c6cSJonas Devlieghere std::future<lldb::SBError> NotifyDidAttach(); 10901263c6cSJonas Devlieghere 11001263c6cSJonas Devlieghere /// Fetch the pid of the runInTerminal launcher. 11101263c6cSJonas Devlieghere /// 11201263c6cSJonas Devlieghere /// \return 11301263c6cSJonas Devlieghere /// An \a llvm::Error object in case of errors or if this operation times 11401263c6cSJonas Devlieghere /// out. 11501263c6cSJonas Devlieghere llvm::Expected<lldb::pid_t> GetLauncherPid(); 11601263c6cSJonas Devlieghere 11701263c6cSJonas Devlieghere /// Fetch any errors emitted by the runInTerminal launcher or return a 11801263c6cSJonas Devlieghere /// default error message if a certain timeout if reached. 11901263c6cSJonas Devlieghere std::string GetLauncherError(); 12001263c6cSJonas Devlieghere 12101263c6cSJonas Devlieghere private: 12201263c6cSJonas Devlieghere FifoFileIO m_io; 12301263c6cSJonas Devlieghere }; 12401263c6cSJonas Devlieghere 12501263c6cSJonas Devlieghere /// Create a fifo file used to communicate the debug adaptor with 12601263c6cSJonas Devlieghere /// the runInTerminal launcher. 12701263c6cSJonas Devlieghere llvm::Expected<std::shared_ptr<FifoFile>> CreateRunInTerminalCommFile(); 12801263c6cSJonas Devlieghere 12901263c6cSJonas Devlieghere } // namespace lldb_dap 13001263c6cSJonas Devlieghere 13101263c6cSJonas Devlieghere #endif // LLDB_TOOLS_LLDB_DAP_RUNINTERMINAL_H 132