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 liblldb_GDBRemoteCommunicationServer_h_
10 #define liblldb_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 PortMap = std::map<uint16_t, lldb::pid_t>;
31   using PacketHandler =
32       std::function<PacketResult(StringExtractorGDBRemote &packet,
33                                  Status &error, bool &interrupt, bool &quit)>;
34 
35   GDBRemoteCommunicationServer(const char *comm_name,
36                                const char *listener_name);
37 
38   ~GDBRemoteCommunicationServer() override;
39 
40   void
41   RegisterPacketHandler(StringExtractorGDBRemote::ServerPacketType packet_type,
42                         PacketHandler handler);
43 
44   PacketResult GetPacketAndSendResponse(Timeout<std::micro> timeout,
45                                         Status &error, bool &interrupt,
46                                         bool &quit);
47 
48   // After connecting, do a little handshake with the client to make sure
49   // we are at least communicating
50   bool HandshakeWithClient();
51 
52 protected:
53   std::map<StringExtractorGDBRemote::ServerPacketType, PacketHandler>
54       m_packet_handlers;
55   bool m_exit_now; // use in asynchronous handling to indicate process should
56                    // exit.
57 
58   bool m_send_error_strings = false; // If the client enables this then
59                                      // we will send error strings as well.
60 
61   PacketResult Handle_QErrorStringEnable(StringExtractorGDBRemote &packet);
62 
63   PacketResult SendErrorResponse(const Status &error);
64 
65   PacketResult SendErrorResponse(llvm::Error error);
66 
67   PacketResult SendUnimplementedResponse(const char *packet);
68 
69   PacketResult SendErrorResponse(uint8_t error);
70 
71   PacketResult SendIllFormedResponse(const StringExtractorGDBRemote &packet,
72                                      const char *error_message);
73 
74   PacketResult SendOKResponse();
75 
76 private:
77   DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunicationServer);
78 };
79 
80 class PacketUnimplementedError
81     : public llvm::ErrorInfo<PacketUnimplementedError, llvm::StringError> {
82 public:
83   static char ID;
84   using llvm::ErrorInfo<PacketUnimplementedError,
85                         llvm::StringError>::ErrorInfo; // inherit constructors
86   PacketUnimplementedError(const llvm::Twine &S)
87       : ErrorInfo(S, llvm::errc::not_supported) {}
88 
89   PacketUnimplementedError() : ErrorInfo(llvm::errc::not_supported) {}
90 };
91 
92 } // namespace process_gdb_remote
93 } // namespace lldb_private
94 
95 #endif // liblldb_GDBRemoteCommunicationServer_h_
96