1 //===-- GDBRemoteCommunication.h --------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_GDBRemoteCommunication_h_
11 #define liblldb_GDBRemoteCommunication_h_
18 // Other libraries and framework includes
20 #include "lldb/lldb-public.h"
21 #include "lldb/Core/Communication.h"
22 #include "lldb/Core/Listener.h"
23 #include "lldb/Host/Mutex.h"
24 #include "lldb/Host/Predicate.h"
25 #include "lldb/Host/TimeValue.h"
27 #include "Utility/StringExtractorGDBRemote.h"
29 class ProcessGDBRemote;
31 class GDBRemoteCommunication : public lldb_private::Communication
36 eBroadcastBitRunPacketSent = kLoUserBroadcastBit
39 enum class PacketResult
41 Success = 0, // Success
42 ErrorSendFailed, // Error sending the packet
43 ErrorSendAck, // Didn't get an ack back after sending a packet
44 ErrorReplyFailed, // Error getting the reply
45 ErrorReplyTimeout, // Timed out waiting for reply
46 ErrorReplyInvalid, // Got a reply but it wasn't valid for the packet that was sent
47 ErrorReplyAck, // Sending reply ack failed
48 ErrorDisconnected, // We were disconnected
49 ErrorNoSequenceLock // We couldn't get the sequence lock for a multi-packet request
51 //------------------------------------------------------------------
52 // Constructors and Destructors
53 //------------------------------------------------------------------
54 GDBRemoteCommunication(const char *comm_name,
55 const char *listener_name,
59 ~GDBRemoteCommunication();
71 CalculcateChecksum (const char *payload,
72 size_t payload_length);
75 GetSequenceMutex (lldb_private::Mutex::Locker& locker, const char *failure_message = NULL);
78 CheckForPacket (const uint8_t *src,
80 StringExtractorGDBRemote &packet);
84 return m_public_is_running.GetValue();
93 //------------------------------------------------------------------
94 // Client and server must implement these pure virtual functions
95 //------------------------------------------------------------------
97 GetThreadSuffixSupported () = 0;
99 //------------------------------------------------------------------
100 // Set the global packet timeout.
102 // For clients, this is the timeout that gets used when sending
103 // packets and waiting for responses. For servers, this might not
104 // get used, and if it doesn't this should be moved to the
105 // GDBRemoteCommunicationClient.
106 //------------------------------------------------------------------
108 SetPacketTimeout (uint32_t packet_timeout)
110 const uint32_t old_packet_timeout = m_packet_timeout;
111 m_packet_timeout = packet_timeout;
112 return old_packet_timeout;
116 GetPacketTimeoutInMicroSeconds () const
118 return m_packet_timeout * lldb_private::TimeValue::MicroSecPerSec;
120 //------------------------------------------------------------------
121 // Start a debugserver instance on the current host using the
122 // supplied connection URL.
123 //------------------------------------------------------------------
125 StartDebugserverProcess (const char *hostname,
126 uint16_t in_port, // If set to zero, then out_port will contain the bound port on exit
127 lldb_private::ProcessLaunchInfo &launch_info,
131 DumpHistory(lldb_private::Stream &strm);
140 ePacketTypeInvalid = 0,
149 type (ePacketTypeInvalid),
150 bytes_transmitted (0),
152 tid (LLDB_INVALID_THREAD_ID)
160 type = ePacketTypeInvalid;
161 bytes_transmitted = 0;
163 tid = LLDB_INVALID_THREAD_ID;
167 uint32_t bytes_transmitted;
172 History (uint32_t size);
176 // For single char packets for ack, nack and /x03
178 AddPacket (char packet_char,
180 uint32_t bytes_transmitted);
182 AddPacket (const std::string &src,
185 uint32_t bytes_transmitted);
188 Dump (lldb_private::Stream &strm) const;
191 Dump (lldb_private::Log *log) const;
194 DidDumpToLog () const
196 return m_dumped_to_log;
201 GetFirstSavedPacketIndex () const
203 if (m_total_packet_count < m_packets.size())
206 return m_curr_idx + 1;
210 GetNumPacketsInHistory () const
212 if (m_total_packet_count < m_packets.size())
213 return m_total_packet_count;
215 return (uint32_t)m_packets.size();
221 ++m_total_packet_count;
222 const uint32_t idx = m_curr_idx;
223 m_curr_idx = NormalizeIndex(idx + 1);
228 NormalizeIndex (uint32_t i) const
230 return i % m_packets.size();
234 std::vector<Entry> m_packets;
236 uint32_t m_total_packet_count;
237 mutable bool m_dumped_to_log;
241 SendPacket (const char *payload,
242 size_t payload_length);
245 SendPacketNoLock (const char *payload,
246 size_t payload_length);
249 WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &response,
250 uint32_t timeout_usec);
253 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
255 //------------------------------------------------------------------
256 // Classes that inherit from GDBRemoteCommunication can see and modify these
257 //------------------------------------------------------------------
258 uint32_t m_packet_timeout;
259 #ifdef ENABLE_MUTEX_ERROR_CHECKING
260 lldb_private::TrackingMutex m_sequence_mutex;
262 lldb_private::Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
264 lldb_private::Predicate<bool> m_public_is_running;
265 lldb_private::Predicate<bool> m_private_is_running;
268 bool m_is_platform; // Set to true if this class represents a platform,
269 // false if this class represents a debug session for
274 StartListenThread (const char *hostname = "localhost",
280 static lldb::thread_result_t
281 ListenThread (lldb::thread_arg_t arg);
285 lldb::thread_t m_listen_thread;
286 std::string m_listen_url;
289 //------------------------------------------------------------------
290 // For GDBRemoteCommunication only
291 //------------------------------------------------------------------
292 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
295 #endif // liblldb_GDBRemoteCommunication_h_