Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 1 | //===--- JSONRPCDispatcher.cpp - Main JSON parser entry point -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "JSONRPCDispatcher.h" |
| 11 | #include "ProtocolHandlers.h" |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 12 | #include "Trace.h" |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallString.h" |
Sam McCall | 94362c6 | 2017-11-07 14:45:31 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringExtras.h" |
Sam McCall | a90f257 | 2018-02-16 16:41:42 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Chrono.h" |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Errno.h" |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 17 | #include "llvm/Support/FormatVariadic.h" |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 18 | #include "llvm/Support/JSON.h" |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 19 | #include "llvm/Support/SourceMgr.h" |
Ilya Biryukov | 687b92a | 2017-05-16 15:23:55 +0000 | [diff] [blame] | 20 | #include <istream> |
| 21 | |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace clangd; |
| 25 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 26 | namespace { |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 27 | static Key<json::Value> RequestID; |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 28 | static Key<JSONOutput *> RequestOut; |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 29 | |
| 30 | // When tracing, we trace a request and attach the repsonse in reply(). |
| 31 | // Because the Span isn't available, we find the current request using Context. |
| 32 | class RequestSpan { |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 33 | RequestSpan(llvm::json::Object *Args) : Args(Args) {} |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 34 | std::mutex Mu; |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 35 | llvm::json::Object *Args; |
Sam McCall | 24f0fa3 | 2018-01-26 11:23:33 +0000 | [diff] [blame] | 36 | static Key<std::unique_ptr<RequestSpan>> RSKey; |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 37 | |
| 38 | public: |
| 39 | // Return a context that's aware of the enclosing request, identified by Span. |
| 40 | static Context stash(const trace::Span &Span) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 41 | return Context::current().derive( |
| 42 | RSKey, std::unique_ptr<RequestSpan>(new RequestSpan(Span.Args))); |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // If there's an enclosing request and the tracer is interested, calls \p F |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 46 | // with a json::Object where request info can be added. |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 47 | template <typename Func> static void attach(Func &&F) { |
| 48 | auto *RequestArgs = Context::current().get(RSKey); |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 49 | if (!RequestArgs || !*RequestArgs || !(*RequestArgs)->Args) |
| 50 | return; |
| 51 | std::lock_guard<std::mutex> Lock((*RequestArgs)->Mu); |
| 52 | F(*(*RequestArgs)->Args); |
| 53 | } |
| 54 | }; |
Sam McCall | 24f0fa3 | 2018-01-26 11:23:33 +0000 | [diff] [blame] | 55 | Key<std::unique_ptr<RequestSpan>> RequestSpan::RSKey; |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 56 | } // namespace |
| 57 | |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 58 | void JSONOutput::writeMessage(const json::Value &Message) { |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 59 | std::string S; |
| 60 | llvm::raw_string_ostream OS(S); |
| 61 | if (Pretty) |
| 62 | OS << llvm::formatv("{0:2}", Message); |
| 63 | else |
| 64 | OS << Message; |
| 65 | OS.flush(); |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 66 | |
Sam McCall | a90f257 | 2018-02-16 16:41:42 +0000 | [diff] [blame] | 67 | { |
| 68 | std::lock_guard<std::mutex> Guard(StreamMutex); |
| 69 | Outs << "Content-Length: " << S.size() << "\r\n\r\n" << S; |
| 70 | Outs.flush(); |
| 71 | } |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 72 | vlog("--> {0}\n", S); |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 75 | void JSONOutput::log(Logger::Level Level, |
| 76 | const llvm::formatv_object_base &Message) { |
| 77 | if (Level < MinLevel) |
| 78 | return; |
Sam McCall | a90f257 | 2018-02-16 16:41:42 +0000 | [diff] [blame] | 79 | llvm::sys::TimePoint<> Timestamp = std::chrono::system_clock::now(); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 80 | trace::log(Message); |
Benjamin Kramer | e14bd42 | 2017-02-15 16:44:11 +0000 | [diff] [blame] | 81 | std::lock_guard<std::mutex> Guard(StreamMutex); |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 82 | Logs << llvm::formatv("{0}[{1:%H:%M:%S.%L}] {2}\n", indicator(Level), |
| 83 | Timestamp, Message); |
Benjamin Kramer | e14bd42 | 2017-02-15 16:44:11 +0000 | [diff] [blame] | 84 | Logs.flush(); |
| 85 | } |
| 86 | |
Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame] | 87 | void JSONOutput::mirrorInput(const Twine &Message) { |
| 88 | if (!InputMirror) |
| 89 | return; |
| 90 | |
| 91 | *InputMirror << Message; |
| 92 | InputMirror->flush(); |
| 93 | } |
| 94 | |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 95 | void clangd::reply(json::Value &&Result) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 96 | auto ID = Context::current().get(RequestID); |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 97 | if (!ID) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 98 | elog("Attempted to reply to a notification!"); |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 99 | return; |
| 100 | } |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 101 | RequestSpan::attach([&](json::Object &Args) { Args["Reply"] = Result; }); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 102 | Context::current() |
| 103 | .getExisting(RequestOut) |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 104 | ->writeMessage(json::Object{ |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 105 | {"jsonrpc", "2.0"}, |
| 106 | {"id", *ID}, |
| 107 | {"result", std::move(Result)}, |
| 108 | }); |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 111 | void clangd::replyError(ErrorCode code, const llvm::StringRef &Message) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 112 | elog("Error {0}: {1}", static_cast<int>(code), Message); |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 113 | RequestSpan::attach([&](json::Object &Args) { |
| 114 | Args["Error"] = json::Object{{"code", static_cast<int>(code)}, |
| 115 | {"message", Message.str()}}; |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 116 | }); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 117 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 118 | if (auto ID = Context::current().get(RequestID)) { |
| 119 | Context::current() |
| 120 | .getExisting(RequestOut) |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 121 | ->writeMessage(json::Object{ |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 122 | {"jsonrpc", "2.0"}, |
| 123 | {"id", *ID}, |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 124 | {"error", json::Object{{"code", static_cast<int>(code)}, |
| 125 | {"message", Message}}}, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 126 | }); |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 127 | } |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 130 | void clangd::call(StringRef Method, json::Value &&Params) { |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 131 | // FIXME: Generate/Increment IDs for every request so that we can get proper |
| 132 | // replies once we need to. |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 133 | RequestSpan::attach([&](json::Object &Args) { |
| 134 | Args["Call"] = json::Object{{"method", Method.str()}, {"params", Params}}; |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 135 | }); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 136 | Context::current() |
| 137 | .getExisting(RequestOut) |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 138 | ->writeMessage(json::Object{ |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 139 | {"jsonrpc", "2.0"}, |
| 140 | {"id", 1}, |
| 141 | {"method", Method}, |
| 142 | {"params", std::move(Params)}, |
| 143 | }); |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 146 | void JSONRPCDispatcher::registerHandler(StringRef Method, Handler H) { |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 147 | assert(!Handlers.count(Method) && "Handler already registered!"); |
| 148 | Handlers[Method] = std::move(H); |
| 149 | } |
| 150 | |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 151 | bool JSONRPCDispatcher::call(const json::Value &Message, |
| 152 | JSONOutput &Out) const { |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 153 | // Message must be an object with "jsonrpc":"2.0". |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 154 | auto *Object = Message.getAsObject(); |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 155 | if (!Object || Object->getString("jsonrpc") != Optional<StringRef>("2.0")) |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 156 | return false; |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 157 | // ID may be any JSON value. If absent, this is a notification. |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 158 | llvm::Optional<json::Value> ID; |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 159 | if (auto *I = Object->get("id")) |
| 160 | ID = std::move(*I); |
| 161 | // Method must be given. |
| 162 | auto Method = Object->getString("method"); |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 163 | if (!Method) |
| 164 | return false; |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 165 | // Params should be given, use null if not. |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 166 | json::Value Params = nullptr; |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 167 | if (auto *P = Object->get("params")) |
| 168 | Params = std::move(*P); |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 169 | |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 170 | auto I = Handlers.find(*Method); |
| 171 | auto &Handler = I != Handlers.end() ? I->second : UnknownHandler; |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 172 | |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 173 | // Create a Context that contains request information. |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 174 | WithContextValue WithRequestOut(RequestOut, &Out); |
| 175 | llvm::Optional<WithContextValue> WithID; |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 176 | if (ID) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 177 | WithID.emplace(RequestID, *ID); |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 178 | |
| 179 | // Create a tracing Span covering the whole request lifetime. |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 180 | trace::Span Tracer(*Method); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 181 | if (ID) |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 182 | SPAN_ATTACH(Tracer, "ID", *ID); |
| 183 | SPAN_ATTACH(Tracer, "Params", Params); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 184 | |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 185 | // Stash a reference to the span args, so later calls can add metadata. |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 186 | WithContext WithRequestSpan(RequestSpan::stash(Tracer)); |
| 187 | Handler(std::move(Params)); |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 188 | return true; |
| 189 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 190 | |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 191 | // Tries to read a line up to and including \n. |
| 192 | // If failing, feof() or ferror() will be set. |
| 193 | static bool readLine(std::FILE *In, std::string &Out) { |
| 194 | static constexpr int BufSize = 1024; |
| 195 | size_t Size = 0; |
| 196 | Out.clear(); |
| 197 | for (;;) { |
| 198 | Out.resize(Size + BufSize); |
| 199 | // Handle EINTR which is sent when a debugger attaches on some platforms. |
| 200 | if (!llvm::sys::RetryAfterSignal(nullptr, ::fgets, &Out[Size], BufSize, In)) |
| 201 | return false; |
| 202 | clearerr(In); |
| 203 | // If the line contained null bytes, anything after it (including \n) will |
| 204 | // be ignored. Fortunately this is not a legal header or JSON. |
| 205 | size_t Read = std::strlen(&Out[Size]); |
| 206 | if (Read > 0 && Out[Size + Read - 1] == '\n') { |
| 207 | Out.resize(Size + Read); |
| 208 | return true; |
| 209 | } |
| 210 | Size += Read; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // Returns None when: |
| 215 | // - ferror() or feof() are set. |
| 216 | // - Content-Length is missing or empty (protocol error) |
| 217 | static llvm::Optional<std::string> readStandardMessage(std::FILE *In, |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 218 | JSONOutput &Out) { |
| 219 | // A Language Server Protocol message starts with a set of HTTP headers, |
| 220 | // delimited by \r\n, and terminated by an empty line (\r\n). |
| 221 | unsigned long long ContentLength = 0; |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 222 | std::string Line; |
| 223 | while (true) { |
| 224 | if (feof(In) || ferror(In) || !readLine(In, Line)) |
| 225 | return llvm::None; |
Benjamin Kramer | 1d05379 | 2017-10-27 17:06:41 +0000 | [diff] [blame] | 226 | |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 227 | Out.mirrorInput(Line); |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 228 | llvm::StringRef LineRef(Line); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 229 | |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 230 | // We allow comments in headers. Technically this isn't part |
| 231 | // of the LSP specification, but makes writing tests easier. |
| 232 | if (LineRef.startswith("#")) |
| 233 | continue; |
| 234 | |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 235 | // Content-Length is a mandatory header, and the only one we handle. |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 236 | if (LineRef.consume_front("Content-Length: ")) { |
| 237 | if (ContentLength != 0) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 238 | elog("Warning: Duplicate Content-Length header received. " |
| 239 | "The previous value for this message ({0}) was ignored.", |
| 240 | ContentLength); |
Ilya Biryukov | 1fab4f8 | 2017-09-04 12:28:15 +0000 | [diff] [blame] | 241 | } |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 242 | llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength); |
| 243 | continue; |
| 244 | } else if (!LineRef.trim().empty()) { |
| 245 | // It's another header, ignore it. |
| 246 | continue; |
| 247 | } else { |
| 248 | // An empty line indicates the end of headers. |
| 249 | // Go ahead and read the JSON. |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 254 | // The fuzzer likes crashing us by sending "Content-Length: 9999999999999999" |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 255 | if (ContentLength > 1 << 30) { // 1024M |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 256 | elog("Refusing to read message with long Content-Length: {0}. " |
| 257 | "Expect protocol errors", |
| 258 | ContentLength); |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 259 | return llvm::None; |
| 260 | } |
| 261 | if (ContentLength == 0) { |
| 262 | log("Warning: Missing Content-Length header, or zero-length message."); |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 263 | return llvm::None; |
| 264 | } |
| 265 | |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 266 | std::string JSON(ContentLength, '\0'); |
| 267 | for (size_t Pos = 0, Read; Pos < ContentLength; Pos += Read) { |
| 268 | // Handle EINTR which is sent when a debugger attaches on some platforms. |
| 269 | Read = llvm::sys::RetryAfterSignal(0u, ::fread, &JSON[Pos], 1, |
| 270 | ContentLength - Pos, In); |
| 271 | Out.mirrorInput(StringRef(&JSON[Pos], Read)); |
| 272 | if (Read == 0) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 273 | elog("Input was aborted. Read only {0} bytes of expected {1}.", Pos, |
| 274 | ContentLength); |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 275 | return llvm::None; |
| 276 | } |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 277 | clearerr(In); // If we're done, the error was transient. If we're not done, |
| 278 | // either it was transient or we'll see it again on retry. |
| 279 | Pos += Read; |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 280 | } |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 281 | return std::move(JSON); |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | // For lit tests we support a simplified syntax: |
| 285 | // - messages are delimited by '---' on a line by itself |
| 286 | // - lines starting with # are ignored. |
| 287 | // This is a testing path, so favor simplicity over performance here. |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 288 | // When returning None, feof() or ferror() will be set. |
| 289 | static llvm::Optional<std::string> readDelimitedMessage(std::FILE *In, |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 290 | JSONOutput &Out) { |
| 291 | std::string JSON; |
| 292 | std::string Line; |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 293 | while (readLine(In, Line)) { |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 294 | auto LineRef = llvm::StringRef(Line).trim(); |
| 295 | if (LineRef.startswith("#")) // comment |
| 296 | continue; |
| 297 | |
Jan Korous | 6243515 | 2018-04-23 15:55:07 +0000 | [diff] [blame] | 298 | // found a delimiter |
Jan Korous | 1bc528c | 2018-04-23 15:58:42 +0000 | [diff] [blame] | 299 | if (LineRef.rtrim() == "---") |
Jan Korous | 6243515 | 2018-04-23 15:55:07 +0000 | [diff] [blame] | 300 | break; |
| 301 | |
| 302 | JSON += Line; |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 305 | if (ferror(In)) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 306 | elog("Input error while reading message!"); |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 307 | return llvm::None; |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 308 | } else { // Including EOF |
Jan Korous | 6243515 | 2018-04-23 15:55:07 +0000 | [diff] [blame] | 309 | Out.mirrorInput( |
| 310 | llvm::formatv("Content-Length: {0}\r\n\r\n{1}", JSON.size(), JSON)); |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 311 | return std::move(JSON); |
| 312 | } |
| 313 | } |
| 314 | |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 315 | // The use of C-style std::FILE* IO deserves some explanation. |
| 316 | // Previously, std::istream was used. When a debugger attached on MacOS, the |
| 317 | // process received EINTR, the stream went bad, and clangd exited. |
| 318 | // A retry-on-EINTR loop around reads solved this problem, but caused clangd to |
| 319 | // sometimes hang rather than exit on other OSes. The interaction between |
| 320 | // istreams and signals isn't well-specified, so it's hard to get this right. |
| 321 | // The C APIs seem to be clearer in this respect. |
| 322 | void clangd::runLanguageServerLoop(std::FILE *In, JSONOutput &Out, |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 323 | JSONStreamStyle InputStyle, |
| 324 | JSONRPCDispatcher &Dispatcher, |
| 325 | bool &IsDone) { |
| 326 | auto &ReadMessage = |
| 327 | (InputStyle == Delimited) ? readDelimitedMessage : readStandardMessage; |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 328 | while (!IsDone && !feof(In)) { |
| 329 | if (ferror(In)) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 330 | elog("IO error: {0}", llvm::sys::StrError()); |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 331 | return; |
| 332 | } |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 333 | if (auto JSON = ReadMessage(In, Out)) { |
| 334 | if (auto Doc = json::parse(*JSON)) { |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 335 | // Log the formatted message. |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 336 | vlog(Out.Pretty ? "<-- {0:2}\n" : "<-- {0}\n", *Doc); |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 337 | // Finally, execute the action for this JSON message. |
| 338 | if (!Dispatcher.call(*Doc, Out)) |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 339 | elog("JSON dispatch failed!"); |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 340 | } else { |
| 341 | // Parse error. Log the raw message. |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 342 | vlog("<-- {0}\n", *JSON); |
| 343 | elog("JSON parse error: {0}", llvm::toString(Doc.takeError())); |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 344 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | } |