|
7 | 7 | //===----------------------------------------------------------------------===//
|
8 | 8 |
|
9 | 9 | #include "DAP.h"
|
| 10 | +#include "Handler/RequestHandler.h" |
10 | 11 | #include "Handler/ResponseHandler.h"
|
11 | 12 | #include "JSONUtils.h"
|
12 | 13 | #include "LLDBUtils.h"
|
13 | 14 | #include "OutputRedirector.h"
|
| 15 | +#include "Protocol.h" |
14 | 16 | #include "Transport.h"
|
15 | 17 | #include "lldb/API/SBBreakpoint.h"
|
16 | 18 | #include "lldb/API/SBCommandInterpreter.h"
|
|
24 | 26 | #include "lldb/lldb-defines.h"
|
25 | 27 | #include "lldb/lldb-enumerations.h"
|
26 | 28 | #include "llvm/ADT/ArrayRef.h"
|
| 29 | +#include "llvm/ADT/STLExtras.h" |
27 | 30 | #include "llvm/ADT/ScopeExit.h"
|
28 | 31 | #include "llvm/ADT/StringExtras.h"
|
29 | 32 | #include "llvm/ADT/Twine.h"
|
|
39 | 42 | #include <fstream>
|
40 | 43 | #include <memory>
|
41 | 44 | #include <mutex>
|
| 45 | +#include <string> |
42 | 46 | #include <utility>
|
43 | 47 |
|
44 | 48 | #if defined(_WIN32)
|
@@ -684,59 +688,66 @@ void DAP::SetTarget(const lldb::SBTarget target) {
|
684 | 688 | }
|
685 | 689 |
|
686 | 690 | bool DAP::HandleObject(const protocol::Message &M) {
|
687 |
| - llvm::json::Value v = toJSON(M); |
688 |
| - llvm::json::Object object = *v.getAsObject(); |
689 |
| - const auto packet_type = GetString(object, "type"); |
690 |
| - if (packet_type == "request") { |
691 |
| - const auto command = GetString(object, "command"); |
692 |
| - |
693 |
| - auto new_handler_pos = request_handlers.find(command); |
| 691 | + if (const auto *req = std::get_if<protocol::Request>(&M)) { |
| 692 | + auto new_handler_pos = request_handlers.find(req->command); |
694 | 693 | if (new_handler_pos != request_handlers.end()) {
|
695 |
| - (*new_handler_pos->second)(object); |
| 694 | + (*new_handler_pos->second)(*req); |
696 | 695 | return true; // Success
|
697 | 696 | }
|
698 | 697 |
|
699 | 698 | if (log)
|
700 |
| - *log << "error: unhandled command \"" << command.data() << "\"" |
| 699 | + *log << "error: unhandled command \"" << req->command << "\"" |
701 | 700 | << std::endl;
|
702 | 701 | return false; // Fail
|
703 | 702 | }
|
704 | 703 |
|
705 |
| - if (packet_type == "response") { |
706 |
| - auto id = GetInteger<int64_t>(object, "request_seq").value_or(0); |
707 |
| - |
| 704 | + if (const auto *resp = std::get_if<protocol::Response>(&M)) { |
708 | 705 | std::unique_ptr<ResponseHandler> response_handler;
|
709 | 706 | {
|
710 | 707 | std::lock_guard<std::mutex> locker(call_mutex);
|
711 |
| - auto inflight = inflight_reverse_requests.find(id); |
| 708 | + auto inflight = inflight_reverse_requests.find(resp->request_seq); |
712 | 709 | if (inflight != inflight_reverse_requests.end()) {
|
713 | 710 | response_handler = std::move(inflight->second);
|
714 | 711 | inflight_reverse_requests.erase(inflight);
|
715 | 712 | }
|
716 | 713 | }
|
717 | 714 |
|
718 | 715 | if (!response_handler)
|
719 |
| - response_handler = std::make_unique<UnknownResponseHandler>("", id); |
| 716 | + response_handler = |
| 717 | + std::make_unique<UnknownResponseHandler>("", resp->request_seq); |
720 | 718 |
|
721 | 719 | // Result should be given, use null if not.
|
722 |
| - if (GetBoolean(object, "success").value_or(false)) { |
723 |
| - llvm::json::Value Result = nullptr; |
724 |
| - if (auto *B = object.get("body")) { |
725 |
| - Result = std::move(*B); |
726 |
| - } |
727 |
| - (*response_handler)(Result); |
| 720 | + if (resp->success) { |
| 721 | + (*response_handler)(resp->rawBody); |
728 | 722 | } else {
|
729 |
| - llvm::StringRef message = GetString(object, "message"); |
730 |
| - if (message.empty()) { |
731 |
| - message = "Unknown error, response failed"; |
| 723 | + std::string message = "Unknown error, response failed"; |
| 724 | + if (resp->message) { |
| 725 | + message = std::visit( |
| 726 | + llvm::makeVisitor( |
| 727 | + [](const std::string &message) -> std::string { |
| 728 | + return message; |
| 729 | + }, |
| 730 | + [](const protocol::Response::Message &message) -> std::string { |
| 731 | + switch (message) { |
| 732 | + case protocol::Response::Message::cancelled: |
| 733 | + return "cancelled"; |
| 734 | + case protocol::Response::Message::notStopped: |
| 735 | + return "notStopped"; |
| 736 | + } |
| 737 | + }), |
| 738 | + *resp->message); |
732 | 739 | }
|
| 740 | + |
733 | 741 | (*response_handler)(llvm::createStringError(
|
734 | 742 | std::error_code(-1, std::generic_category()), message));
|
735 | 743 | }
|
736 | 744 |
|
737 | 745 | return true;
|
738 | 746 | }
|
739 | 747 |
|
| 748 | + if (log) |
| 749 | + *log << "Unsupported protocol message" << std::endl; |
| 750 | + |
740 | 751 | return false;
|
741 | 752 | }
|
742 | 753 |
|
|
0 commit comments