1 //===-- GDBRemoteCommunicationServer.h --------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVER_H
10 #define LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVER_H
11 
12 #include <functional>
13 #include <map>
14 
15 #include "GDBRemoteCommunication.h"
16 #include "lldb/lldb-private-forward.h"
17 
18 #include "llvm/Support/Errc.h"
19 #include "llvm/Support/Error.h"
20 
21 class StringExtractorGDBRemote;
22 
23 namespace lldb_private {
24 namespace process_gdb_remote {
25 
26 class ProcessGDBRemote;
27 
28 class GDBRemoteCommunicationServer : public GDBRemoteCommunication {
29 public:
30   using PacketHandler =
31       std::function<PacketResult(StringExtractorGDBRemote &packet,
32                                  Status &error, bool &interrupt, bool &quit)>;
33 
34   GDBRemoteCommunicationServer(const char *comm_name,
35                                const char *listener_name);
36 
37   ~GDBRemoteCommunicationServer() override;
38 
39   void
40   RegisterPacketHandler(StringExtractorGDBRemote::ServerPacketType packet_type,
41                         PacketHandler handler);
42 
43   PacketResult GetPacketAndSendResponse(Timeout<std::micro> timeout,
44                                         Status &error, bool &interrupt,
45                                         bool &quit);
46 
47 protected:
48   std::map<StringExtractorGDBRemote::ServerPacketType, PacketHandler>
49       m_packet_handlers;
50   bool m_exit_now; // use in asynchronous handling to indicate process should
51                    // exit.
52 
53   bool m_send_error_strings = false; // If the client enables this then
54                                      // we will send error strings as well.
55 
56   PacketResult Handle_QErrorStringEnable(StringExtractorGDBRemote &packet);
57 
58   PacketResult SendErrorResponse(const Status &error);
59 
60   PacketResult SendErrorResponse(llvm::Error error);
61 
62   PacketResult SendUnimplementedResponse(const char *packet);
63 
64   PacketResult SendErrorResponse(uint8_t error);
65 
66   PacketResult SendIllFormedResponse(const StringExtractorGDBRemote &packet,
67                                      const char *error_message);
68 
69   PacketResult SendOKResponse();
70 
71   /// Serialize and send a JSON object response.
72   PacketResult SendJSONResponse(const llvm::json::Value &value);
73 
74   /// Serialize and send a JSON object response, or respond with an error if the
75   /// input object is an \a llvm::Error.
76   PacketResult SendJSONResponse(llvm::Expected<llvm::json::Value> value);
77 
78 private:
79   GDBRemoteCommunicationServer(const GDBRemoteCommunicationServer &) = delete;
80   const GDBRemoteCommunicationServer &
81   operator=(const GDBRemoteCommunicationServer &) = delete;
82 };
83 
84 } // namespace process_gdb_remote
85 } // namespace lldb_private
86 
87 #endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVER_H
88