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