132 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLDB_TOOLS_LLDB_DAP_RUNINTERMINAL_H10#define LLDB_TOOLS_LLDB_DAP_RUNINTERMINAL_H11 12#include "FifoFiles.h"13#include "lldb/API/SBError.h"14 15#include <future>16#include <memory>17#include <string>18 19namespace lldb_dap {20 21enum RunInTerminalMessageKind {22 eRunInTerminalMessageKindPID = 0,23 eRunInTerminalMessageKindError,24 eRunInTerminalMessageKindDidAttach,25};26 27struct RunInTerminalMessage;28struct RunInTerminalMessagePid;29struct RunInTerminalMessageError;30struct RunInTerminalMessageDidAttach;31 32struct RunInTerminalMessage {33 RunInTerminalMessage(RunInTerminalMessageKind kind);34 35 virtual ~RunInTerminalMessage() = default;36 37 /// Serialize this object to JSON38 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 47using RunInTerminalMessageUP = std::unique_ptr<RunInTerminalMessage>;48 49struct RunInTerminalMessagePid : RunInTerminalMessage {50 RunInTerminalMessagePid(lldb::pid_t pid);51 52 llvm::json::Value ToJSON() const override;53 54 lldb::pid_t pid;55};56 57struct RunInTerminalMessageError : RunInTerminalMessage {58 RunInTerminalMessageError(llvm::StringRef error);59 60 llvm::json::Value ToJSON() const override;61 62 std::string error;63};64 65struct RunInTerminalMessageDidAttach : RunInTerminalMessage {66 RunInTerminalMessageDidAttach();67 68 llvm::json::Value ToJSON() const override;69};70 71class RunInTerminalLauncherCommChannel {72public:73 RunInTerminalLauncherCommChannel(llvm::StringRef comm_file);74 75 /// Wait until the debug adapter attaches.76 ///77 /// \param[in] timeout78 /// How long to wait to be attached.79 //80 /// \return81 /// An \a llvm::Error object in case of errors or if this operation times82 /// out.83 llvm::Error WaitUntilDebugAdapterAttaches(std::chrono::milliseconds timeout);84 85 /// Notify the debug adapter this process' pid.86 ///87 /// \return88 /// An \a llvm::Error object in case of errors or if this operation times89 /// out.90 llvm::Error NotifyPid();91 92 /// Notify the debug adapter that there's been an error.93 void NotifyError(llvm::StringRef error);94 95private:96 FifoFileIO m_io;97};98 99class RunInTerminalDebugAdapterCommChannel {100public:101 RunInTerminalDebugAdapterCommChannel(llvm::StringRef comm_file);102 103 /// Notify the runInTerminal launcher that it was attached.104 ///105 /// \return106 /// A future indicated whether the runInTerminal launcher received the107 /// message correctly or not.108 std::future<lldb::SBError> NotifyDidAttach();109 110 /// Fetch the pid of the runInTerminal launcher.111 ///112 /// \return113 /// An \a llvm::Error object in case of errors or if this operation times114 /// out.115 llvm::Expected<lldb::pid_t> GetLauncherPid();116 117 /// Fetch any errors emitted by the runInTerminal launcher or return a118 /// default error message if a certain timeout if reached.119 std::string GetLauncherError();120 121private:122 FifoFileIO m_io;123};124 125/// Create a fifo file used to communicate the debug adapter with126/// the runInTerminal launcher.127llvm::Expected<std::shared_ptr<FifoFile>> CreateRunInTerminalCommFile();128 129} // namespace lldb_dap130 131#endif // LLDB_TOOLS_LLDB_DAP_RUNINTERMINAL_H132