10b57cec5SDimitry Andric //===-- GDBRemoteCommunicationServer.h --------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
95ffd83dbSDimitry Andric #ifndef LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVER_H
105ffd83dbSDimitry Andric #define LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVER_H
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include <functional>
130b57cec5SDimitry Andric #include <map>
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "GDBRemoteCommunication.h"
160b57cec5SDimitry Andric #include "lldb/lldb-private-forward.h"
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric #include "llvm/Support/Errc.h"
190b57cec5SDimitry Andric #include "llvm/Support/Error.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric class StringExtractorGDBRemote;
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric namespace lldb_private {
240b57cec5SDimitry Andric namespace process_gdb_remote {
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric class ProcessGDBRemote;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric class GDBRemoteCommunicationServer : public GDBRemoteCommunication {
290b57cec5SDimitry Andric public:
300b57cec5SDimitry Andric   using PacketHandler =
310b57cec5SDimitry Andric       std::function<PacketResult(StringExtractorGDBRemote &packet,
320b57cec5SDimitry Andric                                  Status &error, bool &interrupt, bool &quit)>;
330b57cec5SDimitry Andric 
34*bdd1243dSDimitry Andric   GDBRemoteCommunicationServer();
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric   ~GDBRemoteCommunicationServer() override;
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric   void
390b57cec5SDimitry Andric   RegisterPacketHandler(StringExtractorGDBRemote::ServerPacketType packet_type,
400b57cec5SDimitry Andric                         PacketHandler handler);
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric   PacketResult GetPacketAndSendResponse(Timeout<std::micro> timeout,
430b57cec5SDimitry Andric                                         Status &error, bool &interrupt,
440b57cec5SDimitry Andric                                         bool &quit);
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric protected:
470b57cec5SDimitry Andric   std::map<StringExtractorGDBRemote::ServerPacketType, PacketHandler>
480b57cec5SDimitry Andric       m_packet_handlers;
490b57cec5SDimitry Andric   bool m_exit_now; // use in asynchronous handling to indicate process should
500b57cec5SDimitry Andric                    // exit.
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   bool m_send_error_strings = false; // If the client enables this then
530b57cec5SDimitry Andric                                      // we will send error strings as well.
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   PacketResult Handle_QErrorStringEnable(StringExtractorGDBRemote &packet);
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   PacketResult SendErrorResponse(const Status &error);
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   PacketResult SendErrorResponse(llvm::Error error);
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   PacketResult SendUnimplementedResponse(const char *packet);
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric   PacketResult SendErrorResponse(uint8_t error);
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   PacketResult SendIllFormedResponse(const StringExtractorGDBRemote &packet,
660b57cec5SDimitry Andric                                      const char *error_message);
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   PacketResult SendOKResponse();
690b57cec5SDimitry Andric 
70fe6060f1SDimitry Andric   /// Serialize and send a JSON object response.
71fe6060f1SDimitry Andric   PacketResult SendJSONResponse(const llvm::json::Value &value);
72fe6060f1SDimitry Andric 
73fe6060f1SDimitry Andric   /// Serialize and send a JSON object response, or respond with an error if the
74fe6060f1SDimitry Andric   /// input object is an \a llvm::Error.
75fe6060f1SDimitry Andric   PacketResult SendJSONResponse(llvm::Expected<llvm::json::Value> value);
76fe6060f1SDimitry Andric 
770b57cec5SDimitry Andric private:
785ffd83dbSDimitry Andric   GDBRemoteCommunicationServer(const GDBRemoteCommunicationServer &) = delete;
795ffd83dbSDimitry Andric   const GDBRemoteCommunicationServer &
805ffd83dbSDimitry Andric   operator=(const GDBRemoteCommunicationServer &) = delete;
810b57cec5SDimitry Andric };
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric } // namespace process_gdb_remote
840b57cec5SDimitry Andric } // namespace lldb_private
850b57cec5SDimitry Andric 
865ffd83dbSDimitry Andric #endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVER_H
87