blob: 44456080e39915b662a44092ea54929653d56f50 [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 Claytond52d00f2011-07-16 03:19:08 +0000143 bool
Greg Clayton1e5b0212011-07-15 16:31:38 +0000144 Connect (uint16_t reply_port,
145 uint16_t exc_port,
146 const char *greeting);
147
Greg Claytond52d00f2011-07-16 03:19:08 +0000148 bool
149 Reattach (uint16_t reply_port);
150
151 bool
Greg Clayton1e5b0212011-07-15 16:31:38 +0000152 Disconnect ();
Greg Claytond52d00f2011-07-16 03:19:08 +0000153
154 uint32_t
155 GetVersion ();
156
157 uint32_t
158 GetFeatureFlags ();
159
160 uint32_t
161 GetCPUMask ();
162
163 uint32_t
164 GetCPUType ();
165
166 uint32_t
167 GetCPUSubtype ();
Greg Clayton1e5b0212011-07-15 16:31:38 +0000168
Greg Clayton363be3f2011-07-15 03:27:12 +0000169protected:
170 typedef std::list<std::string> packet_collection;
171
Greg Clayton1e5b0212011-07-15 16:31:38 +0000172 bool
Greg Claytond52d00f2011-07-16 03:19:08 +0000173 SendRequestPacketNoLock (const PacketStreamType &request_packet);
Greg Clayton363be3f2011-07-15 03:27:12 +0000174
175 size_t
Greg Claytond52d00f2011-07-16 03:19:08 +0000176 WaitForPacketWithTimeoutMicroSecondsNoLock (lldb_private::DataExtractor &response,
Greg Clayton363be3f2011-07-15 03:27:12 +0000177 uint32_t timeout_usec);
178
179 bool
180 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
181
Greg Clayton1e5b0212011-07-15 16:31:38 +0000182 void
Greg Claytond52d00f2011-07-16 03:19:08 +0000183 MakeRequestPacketHeader (CommandType request_type,
184 PacketStreamType &request_packet,
185 uint16_t request_length);
Greg Clayton1e5b0212011-07-15 16:31:38 +0000186
Greg Claytond52d00f2011-07-16 03:19:08 +0000187 bool
188 SendRequestVersion ();
189
190
191 bool
192 VersionIsValid() const
193 {
194 return m_kdp_version_version != 0;
195 }
196
197 bool
198 HostInfoIsValid() const
199 {
200 return m_kdp_hostinfo_cpu_type != 0;
201 }
202
203 bool
204 SendRequestHostInfo ();
205
206 void
207 ClearKDPSettings ();
208
209 bool
210 SendRequestAndGetReply (const CommandType command,
211 const uint8_t request_sequence_id,
212 const PacketStreamType &request_packet,
213 lldb_private::DataExtractor &reply_packet);
Greg Clayton363be3f2011-07-15 03:27:12 +0000214 //------------------------------------------------------------------
215 // Classes that inherit from CommunicationKDP can see and modify these
216 //------------------------------------------------------------------
Greg Claytond52d00f2011-07-16 03:19:08 +0000217 lldb::ByteOrder m_byte_order;
Greg Clayton363be3f2011-07-15 03:27:12 +0000218 uint32_t m_packet_timeout;
219 lldb_private::Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
220 lldb_private::Predicate<bool> m_public_is_running;
221 lldb_private::Predicate<bool> m_private_is_running;
Greg Clayton1e5b0212011-07-15 16:31:38 +0000222 uint32_t m_session_key;
223 uint8_t m_request_sequence_id;
224 uint8_t m_exception_sequence_id;
Greg Claytond52d00f2011-07-16 03:19:08 +0000225 uint32_t m_kdp_version_version;
226 uint32_t m_kdp_version_feature;
227 uint32_t m_kdp_hostinfo_cpu_mask;
228 uint32_t m_kdp_hostinfo_cpu_type;
229 uint32_t m_kdp_hostinfo_cpu_subtype;
Greg Clayton363be3f2011-07-15 03:27:12 +0000230private:
231 //------------------------------------------------------------------
232 // For CommunicationKDP only
233 //------------------------------------------------------------------
234 DISALLOW_COPY_AND_ASSIGN (CommunicationKDP);
235};
236
237#endif // liblldb_CommunicationKDP_h_