blob: 8e9d8b2235e5b7aac34b351dd5f2f11be22da8e6 [file] [log] [blame]
Greg Clayton269f91e2011-07-15 18:02:58 +00001//===-- CommunicationKDP.h --------------------------------------*- C++ -*-===//
Greg Clayton363be3f2011-07-15 03:27:12 +00002//
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_CommunicationKDP_h_
11#define liblldb_CommunicationKDP_h_
12
13// C Includes
14// C++ Includes
15#include <list>
16#include <string>
17
18// Other libraries and framework includes
19// Project includes
20#include "lldb/lldb-private.h"
21#include "lldb/Core/Communication.h"
22#include "lldb/Core/Listener.h"
Greg Claytond52d00f2011-07-16 03:19:08 +000023#include "lldb/Core/StreamBuffer.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000024#include "lldb/Host/Mutex.h"
25#include "lldb/Host/Predicate.h"
26#include "lldb/Host/TimeValue.h"
27
Greg Clayton363be3f2011-07-15 03:27:12 +000028class CommunicationKDP : public lldb_private::Communication
29{
30public:
31 enum
32 {
33 eBroadcastBitRunPacketSent = kLoUserBroadcastBit
34 };
Greg Clayton1e5b0212011-07-15 16:31:38 +000035
36 const static uint32_t kMaxPacketSize = 1200;
37 const static uint32_t kMaxDataSize = 1024;
Greg Claytond52d00f2011-07-16 03:19:08 +000038 typedef lldb_private::StreamBuffer<1024> PacketStreamType;
Greg Clayton1e5b0212011-07-15 16:31:38 +000039 typedef enum
40 {
Greg Claytond52d00f2011-07-16 03:19:08 +000041 eCommandTypeConnect = 0u,
42 eCommandTypeDisconnect,
43 eCommandTypeHostInfo,
44 eCommandTypeVersion,
45 eCommandTypeMaxBytes,
46 eCommandTypeReadMemory,
47 eCommandTypeWriteMemory,
48 eCommandTypeReadRegisters,
49 eCommandTypeWriteRegisters,
50 eCommandTypeLoad,
51 eCommandTypeImagePath,
52 eCommandTypeSuspend,
53 eCommandTypeResume,
54 eCommandTypeException,
55 eCommandTypeTermination,
56 eCommandTypeBreakpointSet,
57 eCommandTypeBreakpointRemove,
58 eCommandTypeRegions,
59 eCommandTypeReattach,
60 eCommandTypeHostReboot,
61 eCommandTypeReadMemory64,
62 eCommandTypeWriteMemory64,
63 eCommandTypeBreakpointSet64,
64 eCommandTypeBreakpointRemove64,
65 eCommandTypeKernelVersion
66 } CommandType;
Greg Clayton1e5b0212011-07-15 16:31:38 +000067
Greg Claytond52d00f2011-07-16 03:19:08 +000068 typedef enum
Greg Clayton1e5b0212011-07-15 16:31:38 +000069 {
Greg Claytond52d00f2011-07-16 03:19:08 +000070 KDP_PROTERR_SUCCESS = 0,
71 KDP_PROTERR_ALREADY_CONNECTED,
72 KDP_PROTERR_BAD_NBYTES,
73 KDP_PROTERR_BADFLAVOR
74 } KDPError;
Greg Clayton1e5b0212011-07-15 16:31:38 +000075
76 typedef enum
77 {
Greg Claytond52d00f2011-07-16 03:19:08 +000078 ePacketTypeRequest = 0x00u,
79 ePacketTypeReply = 0x80u,
80 ePacketTypeMask = 0x80u,
81 eCommandTypeMask = 0x7fu
Greg Clayton1e5b0212011-07-15 16:31:38 +000082 } PacketType;
Greg Clayton363be3f2011-07-15 03:27:12 +000083 //------------------------------------------------------------------
84 // Constructors and Destructors
85 //------------------------------------------------------------------
86 CommunicationKDP (const char *comm_name);
87
88 virtual
89 ~CommunicationKDP();
90
Greg Clayton1e5b0212011-07-15 16:31:38 +000091 bool
Greg Claytond52d00f2011-07-16 03:19:08 +000092 SendRequestPacket (const PacketStreamType &request_packet);
Greg Clayton363be3f2011-07-15 03:27:12 +000093
94 // Wait for a packet within 'nsec' seconds
95 size_t
Greg Claytond52d00f2011-07-16 03:19:08 +000096 WaitForPacketWithTimeoutMicroSeconds (lldb_private::DataExtractor &response,
Greg Clayton363be3f2011-07-15 03:27:12 +000097 uint32_t usec);
98
Greg Clayton363be3f2011-07-15 03:27:12 +000099 bool
100 GetSequenceMutex(lldb_private::Mutex::Locker& locker);
101
102 bool
103 CheckForPacket (const uint8_t *src,
104 size_t src_len,
Greg Claytond52d00f2011-07-16 03:19:08 +0000105 lldb_private::DataExtractor &packet);
Greg Clayton363be3f2011-07-15 03:27:12 +0000106 bool
107 IsRunning() const
108 {
109 return m_public_is_running.GetValue();
110 }
111
Greg Clayton363be3f2011-07-15 03:27:12 +0000112 //------------------------------------------------------------------
113 // Set the global packet timeout.
114 //
115 // For clients, this is the timeout that gets used when sending
116 // packets and waiting for responses. For servers, this might not
117 // get used, and if it doesn't this should be moved to the
118 // CommunicationKDPClient.
119 //------------------------------------------------------------------
120 uint32_t
121 SetPacketTimeout (uint32_t packet_timeout)
122 {
123 const uint32_t old_packet_timeout = m_packet_timeout;
124 m_packet_timeout = packet_timeout;
125 return old_packet_timeout;
126 }
127
128 uint32_t
129 GetPacketTimeoutInMicroSeconds () const
130 {
131 return m_packet_timeout * lldb_private::TimeValue::MicroSecPerSec;
132 }
133 //------------------------------------------------------------------
134 // Start a debugserver instance on the current host using the
135 // supplied connection URL.
136 //------------------------------------------------------------------
137 lldb_private::Error
138 StartDebugserverProcess (const char *connect_url,
139 const char *unix_socket_name,
140 lldb_private::ProcessLaunchInfo &launch_info);
141
142
Greg Clayton0fa51242011-07-19 03:57:15 +0000143 //------------------------------------------------------------------
144 // Public Request Packets
145 //------------------------------------------------------------------
Greg Claytond52d00f2011-07-16 03:19:08 +0000146 bool
Greg Clayton8d2ea282011-07-17 20:36:25 +0000147 SendRequestConnect (uint16_t reply_port,
148 uint16_t exc_port,
149 const char *greeting);
Greg Clayton1e5b0212011-07-15 16:31:38 +0000150
Greg Claytond52d00f2011-07-16 03:19:08 +0000151 bool
Greg Clayton8d2ea282011-07-17 20:36:25 +0000152 SendRequestReattach (uint16_t reply_port);
Greg Claytond52d00f2011-07-16 03:19:08 +0000153
154 bool
Greg Clayton8d2ea282011-07-17 20:36:25 +0000155 SendRequestDisconnect ();
Greg Claytond52d00f2011-07-16 03:19:08 +0000156
157 uint32_t
Greg Clayton0fa51242011-07-19 03:57:15 +0000158 SendRequestReadMemory (lldb::addr_t addr,
159 void *buf,
160 uint32_t size,
161 lldb_private::Error &error);
162
163 uint32_t
164 SendRequestReadRegisters (uint32_t cpu,
165 uint32_t flavor,
166 void *dst,
167 uint32_t dst_size,
168 lldb_private::Error &error);
169// size_t
170// SendRequestWriteMemory (lldb::addr_t addr,
171// const void *buf,
172// size_t size,
173// lldb_private::Error &error);
174
175 uint32_t
Greg Claytond52d00f2011-07-16 03:19:08 +0000176 GetVersion ();
177
178 uint32_t
179 GetFeatureFlags ();
180
181 uint32_t
182 GetCPUMask ();
183
184 uint32_t
185 GetCPUType ();
186
187 uint32_t
188 GetCPUSubtype ();
Greg Clayton1e5b0212011-07-15 16:31:38 +0000189
Greg Clayton363be3f2011-07-15 03:27:12 +0000190protected:
191 typedef std::list<std::string> packet_collection;
192
Greg Clayton1e5b0212011-07-15 16:31:38 +0000193 bool
Greg Claytond52d00f2011-07-16 03:19:08 +0000194 SendRequestPacketNoLock (const PacketStreamType &request_packet);
Greg Clayton363be3f2011-07-15 03:27:12 +0000195
196 size_t
Greg Claytond52d00f2011-07-16 03:19:08 +0000197 WaitForPacketWithTimeoutMicroSecondsNoLock (lldb_private::DataExtractor &response,
Greg Clayton363be3f2011-07-15 03:27:12 +0000198 uint32_t timeout_usec);
199
200 bool
201 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
202
Greg Clayton1e5b0212011-07-15 16:31:38 +0000203 void
Greg Claytond52d00f2011-07-16 03:19:08 +0000204 MakeRequestPacketHeader (CommandType request_type,
205 PacketStreamType &request_packet,
206 uint16_t request_length);
Greg Clayton1e5b0212011-07-15 16:31:38 +0000207
Greg Clayton0fa51242011-07-19 03:57:15 +0000208 //------------------------------------------------------------------
209 // Protected Request Packets (use public accessors which will cache
210 // results.
211 //------------------------------------------------------------------
Greg Claytond52d00f2011-07-16 03:19:08 +0000212 bool
213 SendRequestVersion ();
214
Greg Clayton0fa51242011-07-19 03:57:15 +0000215 bool
216 SendRequestHostInfo ();
217
218
219 void
220 DumpPacket (lldb_private::Stream &s,
221 const void *data,
222 uint32_t data_len);
223
224 void
225 DumpPacket (lldb_private::Stream &s,
226 const lldb_private::DataExtractor& extractor);
Greg Claytond52d00f2011-07-16 03:19:08 +0000227
228 bool
229 VersionIsValid() const
230 {
231 return m_kdp_version_version != 0;
232 }
233
234 bool
235 HostInfoIsValid() const
236 {
237 return m_kdp_hostinfo_cpu_type != 0;
238 }
239
240 bool
Greg Clayton0fa51242011-07-19 03:57:15 +0000241 ExtractIsReply (uint8_t first_packet_byte) const
242 {
243 // TODO: handle big endian...
244 return (first_packet_byte & ePacketTypeMask) != 0;
245 }
246
247 CommandType
248 ExtractCommand (uint8_t first_packet_byte) const
249 {
250 // TODO: handle big endian...
251 return (CommandType)(first_packet_byte & eCommandTypeMask);
252 }
253
254 static const char *
255 GetCommandAsCString (uint8_t command);
Greg Claytond52d00f2011-07-16 03:19:08 +0000256
257 void
258 ClearKDPSettings ();
259
260 bool
261 SendRequestAndGetReply (const CommandType command,
262 const uint8_t request_sequence_id,
263 const PacketStreamType &request_packet,
264 lldb_private::DataExtractor &reply_packet);
Greg Clayton363be3f2011-07-15 03:27:12 +0000265 //------------------------------------------------------------------
266 // Classes that inherit from CommunicationKDP can see and modify these
267 //------------------------------------------------------------------
Greg Clayton0fa51242011-07-19 03:57:15 +0000268 uint32_t m_addr_byte_size;
Greg Claytond52d00f2011-07-16 03:19:08 +0000269 lldb::ByteOrder m_byte_order;
Greg Clayton363be3f2011-07-15 03:27:12 +0000270 uint32_t m_packet_timeout;
271 lldb_private::Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
272 lldb_private::Predicate<bool> m_public_is_running;
273 lldb_private::Predicate<bool> m_private_is_running;
Greg Clayton1e5b0212011-07-15 16:31:38 +0000274 uint32_t m_session_key;
275 uint8_t m_request_sequence_id;
276 uint8_t m_exception_sequence_id;
Greg Claytond52d00f2011-07-16 03:19:08 +0000277 uint32_t m_kdp_version_version;
278 uint32_t m_kdp_version_feature;
279 uint32_t m_kdp_hostinfo_cpu_mask;
280 uint32_t m_kdp_hostinfo_cpu_type;
281 uint32_t m_kdp_hostinfo_cpu_subtype;
Greg Clayton363be3f2011-07-15 03:27:12 +0000282private:
283 //------------------------------------------------------------------
284 // For CommunicationKDP only
285 //------------------------------------------------------------------
286 DISALLOW_COPY_AND_ASSIGN (CommunicationKDP);
287};
288
289#endif // liblldb_CommunicationKDP_h_