1//===-- RunInTerminal.cpp ---------------------------------------*- 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#include "RunInTerminal.h"
10
11#if !defined(_WIN32)
12#include <sys/stat.h>
13#include <sys/types.h>
14#include <unistd.h>
15#endif
16
17#include <chrono>
18#include <fstream>
19#include <future>
20#include <thread>
21
22#include "llvm/Support/FileSystem.h"
23
24#include "lldb/lldb-defines.h"
25
26using namespace llvm;
27
28namespace lldb_dap {
29
30const RunInTerminalMessagePid *RunInTerminalMessage::GetAsPidMessage() const {
31 return static_cast<const RunInTerminalMessagePid *>(this);
32}
33
34const RunInTerminalMessageError *
35RunInTerminalMessage::GetAsErrorMessage() const {
36 return static_cast<const RunInTerminalMessageError *>(this);
37}
38
39RunInTerminalMessage::RunInTerminalMessage(RunInTerminalMessageKind kind)
40 : kind(kind) {}
41
42RunInTerminalMessagePid::RunInTerminalMessagePid(lldb::pid_t pid)
43 : RunInTerminalMessage(eRunInTerminalMessageKindPID), pid(pid) {}
44
45json::Value RunInTerminalMessagePid::ToJSON() const {
46 return json::Object{{.K: "kind", .V: "pid"}, {.K: "pid", .V: static_cast<int64_t>(pid)}};
47}
48
49RunInTerminalMessageError::RunInTerminalMessageError(StringRef error)
50 : RunInTerminalMessage(eRunInTerminalMessageKindError), error(error) {}
51
52json::Value RunInTerminalMessageError::ToJSON() const {
53 return json::Object{{.K: "kind", .V: "error"}, {.K: "value", .V: error}};
54}
55
56RunInTerminalMessageDidAttach::RunInTerminalMessageDidAttach()
57 : RunInTerminalMessage(eRunInTerminalMessageKindDidAttach) {}
58
59json::Value RunInTerminalMessageDidAttach::ToJSON() const {
60 return json::Object{{.K: "kind", .V: "didAttach"}};
61}
62
63static Expected<RunInTerminalMessageUP>
64ParseJSONMessage(const json::Value &json) {
65 if (const json::Object *obj = json.getAsObject()) {
66 if (std::optional<StringRef> kind = obj->getString(K: "kind")) {
67 if (*kind == "pid") {
68 if (std::optional<int64_t> pid = obj->getInteger(K: "pid"))
69 return std::make_unique<RunInTerminalMessagePid>(
70 args: static_cast<lldb::pid_t>(*pid));
71 } else if (*kind == "error") {
72 if (std::optional<StringRef> error = obj->getString(K: "error"))
73 return std::make_unique<RunInTerminalMessageError>(args&: *error);
74 } else if (*kind == "didAttach") {
75 return std::make_unique<RunInTerminalMessageDidAttach>();
76 }
77 }
78 }
79
80 return createStringError(EC: inconvertibleErrorCode(),
81 S: "Incorrect JSON message: " + JSONToString(json));
82}
83
84static Expected<RunInTerminalMessageUP>
85GetNextMessage(FifoFileIO &io, std::chrono::milliseconds timeout) {
86 if (Expected<json::Value> json = io.ReadJSON(timeout))
87 return ParseJSONMessage(json: *json);
88 else
89 return json.takeError();
90}
91
92static Error ToError(const RunInTerminalMessage &message) {
93 if (message.kind == eRunInTerminalMessageKindError)
94 return createStringError(EC: inconvertibleErrorCode(),
95 S: message.GetAsErrorMessage()->error);
96 return createStringError(EC: inconvertibleErrorCode(),
97 S: "Unexpected JSON message: " +
98 JSONToString(json: message.ToJSON()));
99}
100
101RunInTerminalLauncherCommChannel::RunInTerminalLauncherCommChannel(
102 StringRef comm_file)
103 : m_io(comm_file, "debug adaptor") {}
104
105Error RunInTerminalLauncherCommChannel::WaitUntilDebugAdaptorAttaches(
106 std::chrono::milliseconds timeout) {
107 if (Expected<RunInTerminalMessageUP> message =
108 GetNextMessage(io&: m_io, timeout)) {
109 if (message.get()->kind == eRunInTerminalMessageKindDidAttach)
110 return Error::success();
111 else
112 return ToError(message: *message.get());
113 } else
114 return message.takeError();
115}
116
117Error RunInTerminalLauncherCommChannel::NotifyPid() {
118 return m_io.SendJSON(json: RunInTerminalMessagePid(getpid()).ToJSON());
119}
120
121void RunInTerminalLauncherCommChannel::NotifyError(StringRef error) {
122 if (Error err = m_io.SendJSON(json: RunInTerminalMessageError(error).ToJSON(),
123 timeout: std::chrono::seconds(2)))
124 llvm::errs() << llvm::toString(E: std::move(err)) << "\n";
125}
126
127RunInTerminalDebugAdapterCommChannel::RunInTerminalDebugAdapterCommChannel(
128 StringRef comm_file)
129 : m_io(comm_file, "runInTerminal launcher") {}
130
131// Can't use \a std::future<llvm::Error> because it doesn't compile on Windows
132std::future<lldb::SBError>
133RunInTerminalDebugAdapterCommChannel::NotifyDidAttach() {
134 return std::async(policy: std::launch::async, fn: [&]() {
135 lldb::SBError error;
136 if (llvm::Error err =
137 m_io.SendJSON(json: RunInTerminalMessageDidAttach().ToJSON()))
138 error.SetErrorString(llvm::toString(E: std::move(err)).c_str());
139 return error;
140 });
141}
142
143Expected<lldb::pid_t> RunInTerminalDebugAdapterCommChannel::GetLauncherPid() {
144 if (Expected<RunInTerminalMessageUP> message =
145 GetNextMessage(io&: m_io, timeout: std::chrono::seconds(20))) {
146 if (message.get()->kind == eRunInTerminalMessageKindPID)
147 return message.get()->GetAsPidMessage()->pid;
148 return ToError(message: *message.get());
149 } else {
150 return message.takeError();
151 }
152}
153
154std::string RunInTerminalDebugAdapterCommChannel::GetLauncherError() {
155 // We know there's been an error, so a small timeout is enough.
156 if (Expected<RunInTerminalMessageUP> message =
157 GetNextMessage(io&: m_io, timeout: std::chrono::seconds(1)))
158 return toString(E: ToError(message: *message.get()));
159 else
160 return toString(E: message.takeError());
161}
162
163Expected<std::shared_ptr<FifoFile>> CreateRunInTerminalCommFile() {
164 SmallString<256> comm_file;
165 if (std::error_code EC = sys::fs::getPotentiallyUniqueTempFileName(
166 Prefix: "lldb-dap-run-in-terminal-comm", Suffix: "", ResultPath&: comm_file))
167 return createStringError(EC, Msg: "Error making unique file name for "
168 "runInTerminal communication files");
169
170 return CreateFifoFile(path: comm_file.str());
171}
172
173} // namespace lldb_dap
174

source code of lldb/tools/lldb-dap/RunInTerminal.cpp