88
99#include " DAP.h"
1010#include " DAPLog.h"
11+ #include " Handler/RequestHandler.h"
1112#include " Handler/ResponseHandler.h"
1213#include " JSONUtils.h"
1314#include " LLDBUtils.h"
1415#include " OutputRedirector.h"
16+ #include " Protocol/ProtocolBase.h"
1517#include " Transport.h"
1618#include " lldb/API/SBBreakpoint.h"
1719#include " lldb/API/SBCommandInterpreter.h"
2527#include " lldb/lldb-defines.h"
2628#include " lldb/lldb-enumerations.h"
2729#include " llvm/ADT/ArrayRef.h"
30+ #include " llvm/ADT/STLExtras.h"
2831#include " llvm/ADT/ScopeExit.h"
2932#include " llvm/ADT/StringExtras.h"
3033#include " llvm/ADT/StringRef.h"
4144#include < fstream>
4245#include < memory>
4346#include < mutex>
47+ #include < string>
4448#include < utility>
4549
4650#if defined(_WIN32)
@@ -233,6 +237,10 @@ void DAP::SendJSON(const llvm::json::Value &json) {
233237 transport.GetClientName ());
234238 return ;
235239 }
240+ Send (message);
241+ }
242+
243+ void DAP::Send (const protocol::Message &message) {
236244 if (llvm::Error err = transport.Write (message))
237245 DAP_LOG_ERROR (log, std::move (err), " ({1}) write failed: {0}" ,
238246 transport.GetClientName ());
@@ -665,58 +673,65 @@ void DAP::SetTarget(const lldb::SBTarget target) {
665673}
666674
667675bool DAP::HandleObject (const protocol::Message &M) {
668- // FIXME: Directly handle `Message` instead of serializing to JSON.
669- llvm::json::Value v = toJSON (M);
670- llvm::json::Object object = *v.getAsObject ();
671- const auto packet_type = GetString (object, " type" );
672- if (packet_type == " request" ) {
673- const auto command = GetString (object, " command" ).value_or (" " );
674-
675- auto new_handler_pos = request_handlers.find (command);
676- if (new_handler_pos != request_handlers.end ()) {
677- (*new_handler_pos->second )(object);
676+ if (const auto *req = std::get_if<protocol::Request>(&M)) {
677+ auto handler_pos = request_handlers.find (req->command );
678+ if (handler_pos != request_handlers.end ()) {
679+ (*handler_pos->second )(*req);
678680 return true ; // Success
679681 }
680682
681683 DAP_LOG (log, " ({0}) error: unhandled command '{1}'" ,
682- transport.GetClientName (), command);
684+ transport.GetClientName (), req-> command );
683685 return false ; // Fail
684686 }
685687
686- if (packet_type == " response" ) {
687- auto id = GetInteger<int64_t >(object, " request_seq" ).value_or (0 );
688-
688+ if (const auto *resp = std::get_if<protocol::Response>(&M)) {
689689 std::unique_ptr<ResponseHandler> response_handler;
690690 {
691691 std::lock_guard<std::mutex> locker (call_mutex);
692- auto inflight = inflight_reverse_requests.find (id );
692+ auto inflight = inflight_reverse_requests.find (resp-> request_seq );
693693 if (inflight != inflight_reverse_requests.end ()) {
694694 response_handler = std::move (inflight->second );
695695 inflight_reverse_requests.erase (inflight);
696696 }
697697 }
698698
699699 if (!response_handler)
700- response_handler = std::make_unique<UnknownResponseHandler>(" " , id);
700+ response_handler =
701+ std::make_unique<UnknownResponseHandler>(" " , resp->request_seq );
701702
702703 // Result should be given, use null if not.
703- if (GetBoolean (object, " success" ).value_or (false )) {
704- llvm::json::Value Result = nullptr ;
705- if (auto *B = object.get (" body" ))
706- Result = std::move (*B);
707- (*response_handler)(Result);
704+ if (resp->success ) {
705+ (*response_handler)(resp->body );
708706 } else {
709- llvm::StringRef message = GetString (object, " message" ).value_or (" " );
710- if (message.empty ()) {
711- message = " Unknown error, response failed" ;
707+ llvm::StringRef message = " Unknown error, response failed" ;
708+ if (resp->message ) {
709+ message =
710+ std::visit (llvm::makeVisitor (
711+ [](const std::string &message) -> llvm::StringRef {
712+ return message;
713+ },
714+ [](const protocol::Response::Message &message)
715+ -> llvm::StringRef {
716+ switch (message) {
717+ case protocol::Response::Message::cancelled:
718+ return " cancelled" ;
719+ case protocol::Response::Message::notStopped:
720+ return " notStopped" ;
721+ }
722+ }),
723+ *resp->message );
712724 }
725+
713726 (*response_handler)(llvm::createStringError (
714727 std::error_code (-1 , std::generic_category ()), message));
715728 }
716729
717730 return true ;
718731 }
719732
733+ DAP_LOG (log, " Unsupported protocol message" );
734+
720735 return false ;
721736}
722737
@@ -730,9 +745,9 @@ void DAP::SendTerminatedEvent() {
730745 });
731746}
732747
733- lldb::SBError DAP::Disconnect () { return Disconnect (is_attach); }
748+ llvm::Error DAP::Disconnect () { return Disconnect (is_attach); }
734749
735- lldb::SBError DAP::Disconnect (bool terminateDebuggee) {
750+ llvm::Error DAP::Disconnect (bool terminateDebuggee) {
736751 lldb::SBError error;
737752 lldb::SBProcess process = target.GetProcess ();
738753 auto state = process.GetState ();
@@ -760,7 +775,7 @@ lldb::SBError DAP::Disconnect(bool terminateDebuggee) {
760775
761776 disconnecting = true ;
762777
763- return error;
778+ return ToError ( error) ;
764779}
765780
766781llvm::Error DAP::Loop () {
0 commit comments