|
8 | 8 |
|
9 | 9 | #include "DAP.h"
|
10 | 10 | #include "DAPLog.h"
|
| 11 | +#include "Handler/RequestHandler.h" |
11 | 12 | #include "Handler/ResponseHandler.h"
|
12 | 13 | #include "JSONUtils.h"
|
13 | 14 | #include "LLDBUtils.h"
|
14 | 15 | #include "OutputRedirector.h"
|
| 16 | +#include "Protocol.h" |
15 | 17 | #include "Transport.h"
|
16 | 18 | #include "lldb/API/SBBreakpoint.h"
|
17 | 19 | #include "lldb/API/SBCommandInterpreter.h"
|
|
25 | 27 | #include "lldb/lldb-defines.h"
|
26 | 28 | #include "lldb/lldb-enumerations.h"
|
27 | 29 | #include "llvm/ADT/ArrayRef.h"
|
| 30 | +#include "llvm/ADT/STLExtras.h" |
28 | 31 | #include "llvm/ADT/ScopeExit.h"
|
29 | 32 | #include "llvm/ADT/StringExtras.h"
|
30 | 33 | #include "llvm/ADT/StringRef.h"
|
|
41 | 44 | #include <fstream>
|
42 | 45 | #include <memory>
|
43 | 46 | #include <mutex>
|
| 47 | +#include <string> |
44 | 48 | #include <utility>
|
45 | 49 |
|
46 | 50 | #if defined(_WIN32)
|
@@ -663,58 +667,65 @@ void DAP::SetTarget(const lldb::SBTarget target) {
|
663 | 667 | }
|
664 | 668 |
|
665 | 669 | bool DAP::HandleObject(const protocol::Message &M) {
|
666 |
| - // FIXME: Directly handle `Message` instead of serializing to JSON. |
667 |
| - llvm::json::Value v = toJSON(M); |
668 |
| - llvm::json::Object object = *v.getAsObject(); |
669 |
| - const auto packet_type = GetString(object, "type"); |
670 |
| - if (packet_type == "request") { |
671 |
| - const auto command = GetString(object, "command"); |
672 |
| - |
673 |
| - auto new_handler_pos = request_handlers.find(command); |
| 670 | + if (const auto *req = std::get_if<protocol::Request>(&M)) { |
| 671 | + auto new_handler_pos = request_handlers.find(req->command); |
674 | 672 | if (new_handler_pos != request_handlers.end()) {
|
675 |
| - (*new_handler_pos->second)(object); |
| 673 | + (*new_handler_pos->second)(*req); |
676 | 674 | return true; // Success
|
677 | 675 | }
|
678 | 676 |
|
679 | 677 | DAP_LOG(log, "({0}) error: unhandled command '{1}'",
|
680 |
| - transport.GetClientName(), command); |
| 678 | + transport.GetClientName(), req->command); |
681 | 679 | return false; // Fail
|
682 | 680 | }
|
683 | 681 |
|
684 |
| - if (packet_type == "response") { |
685 |
| - auto id = GetInteger<int64_t>(object, "request_seq").value_or(0); |
686 |
| - |
| 682 | + if (const auto *resp = std::get_if<protocol::Response>(&M)) { |
687 | 683 | std::unique_ptr<ResponseHandler> response_handler;
|
688 | 684 | {
|
689 | 685 | std::lock_guard<std::mutex> locker(call_mutex);
|
690 |
| - auto inflight = inflight_reverse_requests.find(id); |
| 686 | + auto inflight = inflight_reverse_requests.find(resp->request_seq); |
691 | 687 | if (inflight != inflight_reverse_requests.end()) {
|
692 | 688 | response_handler = std::move(inflight->second);
|
693 | 689 | inflight_reverse_requests.erase(inflight);
|
694 | 690 | }
|
695 | 691 | }
|
696 | 692 |
|
697 | 693 | if (!response_handler)
|
698 |
| - response_handler = std::make_unique<UnknownResponseHandler>("", id); |
| 694 | + response_handler = |
| 695 | + std::make_unique<UnknownResponseHandler>("", resp->request_seq); |
699 | 696 |
|
700 | 697 | // Result should be given, use null if not.
|
701 |
| - if (GetBoolean(object, "success").value_or(false)) { |
702 |
| - llvm::json::Value Result = nullptr; |
703 |
| - if (auto *B = object.get("body")) |
704 |
| - Result = std::move(*B); |
705 |
| - (*response_handler)(Result); |
| 698 | + if (resp->success) { |
| 699 | + (*response_handler)(resp->rawBody); |
706 | 700 | } else {
|
707 |
| - llvm::StringRef message = GetString(object, "message"); |
708 |
| - if (message.empty()) { |
709 |
| - message = "Unknown error, response failed"; |
| 701 | + std::string message = "Unknown error, response failed"; |
| 702 | + if (resp->message) { |
| 703 | + message = std::visit( |
| 704 | + llvm::makeVisitor( |
| 705 | + [](const std::string &message) -> std::string { |
| 706 | + return message; |
| 707 | + }, |
| 708 | + [](const protocol::Response::Message &message) -> std::string { |
| 709 | + switch (message) { |
| 710 | + case protocol::Response::Message::cancelled: |
| 711 | + return "cancelled"; |
| 712 | + case protocol::Response::Message::notStopped: |
| 713 | + return "notStopped"; |
| 714 | + } |
| 715 | + }), |
| 716 | + *resp->message); |
710 | 717 | }
|
| 718 | + |
711 | 719 | (*response_handler)(llvm::createStringError(
|
712 | 720 | std::error_code(-1, std::generic_category()), message));
|
713 | 721 | }
|
714 | 722 |
|
715 | 723 | return true;
|
716 | 724 | }
|
717 | 725 |
|
| 726 | + if (log) |
| 727 | + *log << "Unsupported protocol message" << std::endl; |
| 728 | + |
718 | 729 | return false;
|
719 | 730 | }
|
720 | 731 |
|
|
0 commit comments