#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { namespace fs = std::filesystem; namespace P = Void::LanguageServer::Protocol; using Json = P::Json; using Void::Testing::expect; using Void::Testing::require; using namespace std::chrono_literals; fs::path sibling(std::string_view name) { const auto directory = fs::canonical("/proc/self/exe").parent_path(); // VBS keeps every executable in one directory; CMake puts the server tools // at the build root and the test executables under tests/. if (fs::exists(directory / name)) return directory / name; return directory.parent_path() / name; } void write_frame(int fd, const Json &message) { auto body = P::serialize_json(message); require(body.has_value()); const std::string header = "Content-Length: " + std::to_string(body->size()) + "\r\n\r\n"; for (const std::string_view part : {std::string_view{header}, std::string_view{*body}}) { std::size_t done{}; while (done != part.size()) { const auto written = ::write(fd, part.data() + done, part.size() - done); require(written > 0); done += static_cast(written); } } } std::optional read_frame(int fd, std::string &buffer) { const auto deadline = std::chrono::steady_clock::now() + 20s; while (true) { const auto header = buffer.find("\r\n\r\n"); if (header != std::string::npos) { const auto marker = buffer.find("Content-Length: "); require(marker != std::string::npos); const auto length = std::stoul(buffer.substr(marker + 16)); if (buffer.size() >= header + 4 + length) { const auto body = buffer.substr(header + 4, length); buffer.erase(0, header + 4 + length); auto parsed = P::parse_json(body, 8); require(parsed.has_value()); return *parsed; } } if (std::chrono::steady_clock::now() >= deadline) return std::nullopt; pollfd pipe{fd, POLLIN, 0}; if (::poll(&pipe, 1, 100) <= 0) continue; char bytes[8192]; const auto read = ::read(fd, bytes, sizeof bytes); if (read <= 0) return std::nullopt; buffer.append(bytes, static_cast(read)); } } Json wait_response(int fd, std::string &buffer, std::int64_t sequence) { while (true) { auto message = read_frame(fd, buffer); require(message.has_value()); const auto *fields = P::as_object(&*message); require(fields); const auto *type = P::as_string(P::find_member(*fields, "type")); const auto *request = P::find_member(*fields, "request_seq"); if (type && *type == "response" && request) if (const auto *value = std::get_if(&request->value); value && *value == sequence) return *message; } } Json wait_event(int fd, std::string &buffer, std::string_view name) { while (true) { auto message = read_frame(fd, buffer); require(message.has_value()); const auto *fields = P::as_object(&*message); require(fields); const auto *type = P::as_string(P::find_member(*fields, "type")); const auto *event = P::as_string(P::find_member(*fields, "event")); if (type && event && *type == "event" && *event == name) return *message; } } } // namespace VOID_TEST(LiveDap, breakpoints_and_typed_inspection_over_the_adapter) { Void::FileSystem::TempDir temp{"vls_live_dap_"}; const auto source = temp.path() / "normal.cpp"; std::ofstream{source} << "int value = 17;\n"; const auto trace = temp.path() / "session.vls"; Void::Process::SpawnOptions spawn; spawn.executable = sibling("vls-dap"); spawn.arguments = {"--live", sibling("vls_live_worker_fixture").native(), "/private/cc1plus", std::string(64, 'a'), source.native(), trace.native()}; spawn.search_path = false; auto child = Void::Process::spawn(spawn); require(child.has_value()); const int input = child->stdin_fd(), output = child->stdout_fd(); std::string buffer; std::int64_t sequence{}; auto request = [&](std::string_view command, Json::Object arguments) { write_frame(input, Json{Json::Object{{"seq", Json{++sequence}}, {"type", Json{std::string{"request"}}}, {"command", Json{std::string{command}}}, {"arguments", Json{std::move(arguments)}}}}); return sequence; }; const auto object_of = [](const Json &value) { const auto *fields = P::as_object(&value); require(fields != nullptr); return fields; }; const auto bool_of = [](const Json &value, std::string_view name) { const auto *fields = P::as_object(&value); require(fields != nullptr); return P::as_bool(P::find_member(*fields, name)).value_or(false); }; request("initialize", {{"adapterID", Json{std::string{"vls"}}}}); expect(bool_of(wait_response(output, buffer, sequence), "success")); wait_event(output, buffer, "initialized"); request("launch", {}); request("configurationDone", {}); wait_event(output, buffer, "stopped"); // Typed value projection from the live inspection snapshot. The reply // objects must outlive the pointers into them, so each is kept in a local. request("stackTrace", {{"threadId", Json{std::int64_t{1}}}}); const Json stack_reply = wait_response(output, buffer, sequence); const auto *stack_body = P::as_object(P::find_member(*object_of(stack_reply), "body")); require(stack_body); const auto *frames = P::as_array(P::find_member(*stack_body, "stackFrames")); require(frames && !frames->empty()); const auto *frame = P::as_object(&frames->front()); require(frame); const auto *handle = P::find_member(*frame, "id"); require(handle); request("scopes", {{"frameId", *handle}}); const Json scopes_reply = wait_response(output, buffer, sequence); const auto *scopes_body = P::as_object(P::find_member(*object_of(scopes_reply), "body")); require(scopes_body); const auto *scope_list = P::as_array(P::find_member(*scopes_body, "scopes")); require(scope_list && !scope_list->empty()); const auto *scope = P::as_object(&scope_list->front()); require(scope); const auto *reference = P::find_member(*scope, "variablesReference"); require(reference); request("variables", {{"variablesReference", *reference}}); const Json values_reply = wait_response(output, buffer, sequence); const auto *values_body = P::as_object(P::find_member(*object_of(values_reply), "body")); require(values_body); const auto *variables = P::as_array(P::find_member(*values_body, "variables")); require(variables); bool projected{}, reflected{}; for (const auto &value : *variables) { const auto *fields = P::as_object(&value); const auto *name = fields ? P::as_string(P::find_member(*fields, "name")) : nullptr; const auto *text = fields ? P::as_string(P::find_member(*fields, "value")) : nullptr; if (name && text && *name == "Result" && *text == "17") projected = true; if (name && text && *name == "Ready" && text->find("input.cpp:1:20") != std::string::npos && text->find("input.cpp:4:31") != std::string::npos) reflected = true; } expect(projected); expect(reflected); // Detailed frame, breakpoint and typed-value projection is covered by the // real DAP transcript in tests/native/LiveDap.py; this suite registers the // live control path in the C++ build. request("continue", {{"threadId", Json{std::int64_t{1}}}}); wait_response(output, buffer, sequence); wait_event(output, buffer, "terminated"); request("disconnect", {}); wait_response(output, buffer, sequence); static_cast(child->terminate(1s)); }