blob: a1077957c6a6283437da44dd374ee30ca87093d5 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- GDBRemoteCommunication.h --------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_GDBRemoteCommunication_h_
11#define liblldb_GDBRemoteCommunication_h_
12
13// C Includes
14// C++ Includes
15#include <list>
16#include <string>
17
18// Other libraries and framework includes
19// Project includes
Greg Clayton451fa822012-04-09 22:46:21 +000020#include "lldb/lldb-public.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Communication.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Core/Listener.h"
23#include "lldb/Host/Mutex.h"
24#include "lldb/Host/Predicate.h"
Peter Collingbourne20fe30c2011-06-18 23:52:14 +000025#include "lldb/Host/TimeValue.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026
Greg Clayton54e7afa2010-07-09 20:39:50 +000027#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028
29class ProcessGDBRemote;
30
Greg Clayton61d043b2011-03-22 04:00:09 +000031class GDBRemoteCommunication : public lldb_private::Communication
Chris Lattner24943d22010-06-08 16:52:24 +000032{
33public:
Greg Claytonb749a262010-12-03 06:02:24 +000034 enum
35 {
36 eBroadcastBitRunPacketSent = kLoUserBroadcastBit
37 };
Chris Lattner24943d22010-06-08 16:52:24 +000038 //------------------------------------------------------------------
39 // Constructors and Destructors
40 //------------------------------------------------------------------
Greg Claytonb72d0f02011-04-12 05:54:46 +000041 GDBRemoteCommunication(const char *comm_name,
42 const char *listener_name,
43 bool is_platform);
Chris Lattner24943d22010-06-08 16:52:24 +000044
45 virtual
46 ~GDBRemoteCommunication();
47
Chris Lattner24943d22010-06-08 16:52:24 +000048 char
Greg Claytonc97bfdb2011-03-10 02:26:48 +000049 GetAck ();
Chris Lattner24943d22010-06-08 16:52:24 +000050
51 size_t
Greg Claytona4881d02011-01-22 07:12:45 +000052 SendAck ();
53
54 size_t
55 SendNack ();
56
Chris Lattner24943d22010-06-08 16:52:24 +000057 char
58 CalculcateChecksum (const char *payload,
59 size_t payload_length);
60
Chris Lattner24943d22010-06-08 16:52:24 +000061 bool
Jim Ingham088684d2012-06-08 22:50:40 +000062 GetSequenceMutex (lldb_private::Mutex::Locker& locker, const char *failure_message = NULL);
Chris Lattner24943d22010-06-08 16:52:24 +000063
Greg Clayton63afdb02011-06-17 01:22:15 +000064 bool
65 CheckForPacket (const uint8_t *src,
66 size_t src_len,
67 StringExtractorGDBRemote &packet);
Chris Lattner24943d22010-06-08 16:52:24 +000068 bool
69 IsRunning() const
70 {
Greg Claytoncecf3482011-01-20 07:53:45 +000071 return m_public_is_running.GetValue();
Chris Lattner24943d22010-06-08 16:52:24 +000072 }
Greg Clayton72e1c782011-01-22 23:43:18 +000073
Greg Clayton58e26e02011-03-24 04:28:38 +000074 bool
75 GetSendAcks ()
76 {
77 return m_send_acks;
78 }
79
Greg Clayton61d043b2011-03-22 04:00:09 +000080 //------------------------------------------------------------------
81 // Client and server must implement these pure virtual functions
82 //------------------------------------------------------------------
83 virtual bool
84 GetThreadSuffixSupported () = 0;
Chris Lattner24943d22010-06-08 16:52:24 +000085
Greg Clayton61d043b2011-03-22 04:00:09 +000086 //------------------------------------------------------------------
87 // Set the global packet timeout.
88 //
89 // For clients, this is the timeout that gets used when sending
90 // packets and waiting for responses. For servers, this might not
91 // get used, and if it doesn't this should be moved to the
92 // GDBRemoteCommunicationClient.
93 //------------------------------------------------------------------
Greg Claytonc97bfdb2011-03-10 02:26:48 +000094 uint32_t
95 SetPacketTimeout (uint32_t packet_timeout)
96 {
97 const uint32_t old_packet_timeout = m_packet_timeout;
98 m_packet_timeout = packet_timeout;
99 return old_packet_timeout;
100 }
Greg Claytonc1f45872011-02-12 06:28:37 +0000101
Greg Clayton63afdb02011-06-17 01:22:15 +0000102 uint32_t
103 GetPacketTimeoutInMicroSeconds () const
104 {
Peter Collingbourne20fe30c2011-06-18 23:52:14 +0000105 return m_packet_timeout * lldb_private::TimeValue::MicroSecPerSec;
Greg Clayton63afdb02011-06-17 01:22:15 +0000106 }
Greg Claytonb72d0f02011-04-12 05:54:46 +0000107 //------------------------------------------------------------------
108 // Start a debugserver instance on the current host using the
109 // supplied connection URL.
110 //------------------------------------------------------------------
111 lldb_private::Error
112 StartDebugserverProcess (const char *connect_url,
113 const char *unix_socket_name,
114 lldb_private::ProcessLaunchInfo &launch_info);
115
Greg Clayton451fa822012-04-09 22:46:21 +0000116 void
Greg Clayton33559462012-04-13 21:24:18 +0000117 DumpHistory(lldb_private::Stream &strm);
Greg Clayton63afdb02011-06-17 01:22:15 +0000118
Chris Lattner24943d22010-06-08 16:52:24 +0000119protected:
Greg Clayton451fa822012-04-09 22:46:21 +0000120
121 class History
122 {
123 public:
124 enum PacketType
125 {
126 ePacketTypeInvalid = 0,
127 ePacketTypeSend,
128 ePacketTypeRecv
129 };
130
131 struct Entry
132 {
133 Entry() :
134 packet(),
135 type (ePacketTypeInvalid),
136 bytes_transmitted (0),
Greg Clayton33559462012-04-13 21:24:18 +0000137 packet_idx (0),
138 tid (LLDB_INVALID_THREAD_ID)
Greg Clayton451fa822012-04-09 22:46:21 +0000139 {
140 }
141
142 void
143 Clear ()
144 {
145 packet.clear();
146 type = ePacketTypeInvalid;
147 bytes_transmitted = 0;
148 packet_idx = 0;
Greg Clayton33559462012-04-13 21:24:18 +0000149 tid = LLDB_INVALID_THREAD_ID;
Greg Clayton451fa822012-04-09 22:46:21 +0000150 }
151 std::string packet;
152 PacketType type;
153 uint32_t bytes_transmitted;
154 uint32_t packet_idx;
Greg Clayton33559462012-04-13 21:24:18 +0000155 lldb::tid_t tid;
Greg Clayton451fa822012-04-09 22:46:21 +0000156 };
157
158 History (uint32_t size);
159
160 ~History ();
161
162 // For single char packets for ack, nack and /x03
163 void
164 AddPacket (char packet_char,
165 PacketType type,
Greg Clayton33559462012-04-13 21:24:18 +0000166 uint32_t bytes_transmitted);
Greg Clayton451fa822012-04-09 22:46:21 +0000167 void
168 AddPacket (const std::string &src,
169 uint32_t src_len,
170 PacketType type,
Greg Clayton33559462012-04-13 21:24:18 +0000171 uint32_t bytes_transmitted);
Greg Clayton451fa822012-04-09 22:46:21 +0000172
173 void
174 Dump (lldb_private::Stream &strm) const;
175
176 void
177 Dump (lldb_private::Log *log) const;
178
179 bool
180 DidDumpToLog () const
181 {
182 return m_dumped_to_log;
183 }
184
185protected:
186 uint32_t
187 GetFirstSavedPacketIndex () const
188 {
189 if (m_total_packet_count < m_packets.size())
190 return 0;
191 else
192 return m_curr_idx + 1;
193 }
194
195 uint32_t
196 GetNumPacketsInHistory () const
197 {
198 if (m_total_packet_count < m_packets.size())
199 return m_total_packet_count;
200 else
201 return (uint32_t)m_packets.size();
202 }
203
204 uint32_t
205 GetNextIndex()
206 {
207 ++m_total_packet_count;
208 const uint32_t idx = m_curr_idx;
209 m_curr_idx = NormalizeIndex(idx + 1);
210 return idx;
211 }
212
213 uint32_t
214 NormalizeIndex (uint32_t i) const
215 {
216 return i % m_packets.size();
217 }
218
219
220 std::vector<Entry> m_packets;
221 uint32_t m_curr_idx;
222 uint32_t m_total_packet_count;
223 mutable bool m_dumped_to_log;
224 };
Chris Lattner24943d22010-06-08 16:52:24 +0000225
226 size_t
Greg Clayton516f0842012-04-11 00:24:49 +0000227 SendPacket (const char *payload,
228 size_t payload_length);
229
230 size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000231 SendPacketNoLock (const char *payload,
232 size_t payload_length);
233
234 size_t
Greg Clayton63afdb02011-06-17 01:22:15 +0000235 WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &response,
236 uint32_t timeout_usec);
Greg Clayton72e1c782011-01-22 23:43:18 +0000237
238 bool
239 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000240
241 //------------------------------------------------------------------
242 // Classes that inherit from GDBRemoteCommunication can see and modify these
243 //------------------------------------------------------------------
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000244 uint32_t m_packet_timeout;
Jim Ingham088684d2012-06-08 22:50:40 +0000245#ifdef LLDB_CONFIGURATION_DEBUG
246 lldb_private::TrackingMutex m_sequence_mutex;
247#else
Chris Lattner24943d22010-06-08 16:52:24 +0000248 lldb_private::Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
Jim Ingham088684d2012-06-08 22:50:40 +0000249#endif
Greg Claytoncecf3482011-01-20 07:53:45 +0000250 lldb_private::Predicate<bool> m_public_is_running;
251 lldb_private::Predicate<bool> m_private_is_running;
Greg Clayton451fa822012-04-09 22:46:21 +0000252 History m_history;
Greg Clayton58e26e02011-03-24 04:28:38 +0000253 bool m_send_acks;
Greg Claytonb72d0f02011-04-12 05:54:46 +0000254 bool m_is_platform; // Set to true if this class represents a platform,
255 // false if this class represents a debug session for
256 // a single process
Greg Clayton58e26e02011-03-24 04:28:38 +0000257
258
Chris Lattner24943d22010-06-08 16:52:24 +0000259
Greg Claytonb72d0f02011-04-12 05:54:46 +0000260
Chris Lattner24943d22010-06-08 16:52:24 +0000261private:
262 //------------------------------------------------------------------
263 // For GDBRemoteCommunication only
264 //------------------------------------------------------------------
265 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
266};
267
268#endif // liblldb_GDBRemoteCommunication_h_