Greg Clayton | 269f91e | 2011-07-15 18:02:58 +0000 | [diff] [blame] | 1 | //===-- CommunicationKDP.h --------------------------------------*- C++ -*-===// |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 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_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 Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 23 | #include "lldb/Core/StreamBuffer.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 24 | #include "lldb/Host/Mutex.h" |
| 25 | #include "lldb/Host/Predicate.h" |
| 26 | #include "lldb/Host/TimeValue.h" |
| 27 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 28 | class CommunicationKDP : public lldb_private::Communication |
| 29 | { |
| 30 | public: |
| 31 | enum |
| 32 | { |
| 33 | eBroadcastBitRunPacketSent = kLoUserBroadcastBit |
| 34 | }; |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 35 | |
| 36 | const static uint32_t kMaxPacketSize = 1200; |
| 37 | const static uint32_t kMaxDataSize = 1024; |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 38 | typedef lldb_private::StreamBuffer<1024> PacketStreamType; |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 39 | typedef enum |
| 40 | { |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 41 | 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 Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 67 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 68 | typedef enum |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 69 | { |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 70 | KDP_PROTERR_SUCCESS = 0, |
| 71 | KDP_PROTERR_ALREADY_CONNECTED, |
| 72 | KDP_PROTERR_BAD_NBYTES, |
| 73 | KDP_PROTERR_BADFLAVOR |
| 74 | } KDPError; |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 75 | |
| 76 | typedef enum |
| 77 | { |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 78 | ePacketTypeRequest = 0x00u, |
| 79 | ePacketTypeReply = 0x80u, |
| 80 | ePacketTypeMask = 0x80u, |
| 81 | eCommandTypeMask = 0x7fu |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 82 | } PacketType; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 83 | //------------------------------------------------------------------ |
| 84 | // Constructors and Destructors |
| 85 | //------------------------------------------------------------------ |
| 86 | CommunicationKDP (const char *comm_name); |
| 87 | |
| 88 | virtual |
| 89 | ~CommunicationKDP(); |
| 90 | |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 91 | bool |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 92 | SendRequestPacket (const PacketStreamType &request_packet); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 93 | |
| 94 | // Wait for a packet within 'nsec' seconds |
| 95 | size_t |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 96 | WaitForPacketWithTimeoutMicroSeconds (lldb_private::DataExtractor &response, |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 97 | uint32_t usec); |
| 98 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 99 | bool |
| 100 | GetSequenceMutex(lldb_private::Mutex::Locker& locker); |
| 101 | |
| 102 | bool |
| 103 | CheckForPacket (const uint8_t *src, |
| 104 | size_t src_len, |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 105 | lldb_private::DataExtractor &packet); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 106 | bool |
| 107 | IsRunning() const |
| 108 | { |
| 109 | return m_public_is_running.GetValue(); |
| 110 | } |
| 111 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 112 | //------------------------------------------------------------------ |
| 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 Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame^] | 143 | //------------------------------------------------------------------ |
| 144 | // Public Request Packets |
| 145 | //------------------------------------------------------------------ |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 146 | bool |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 147 | SendRequestConnect (uint16_t reply_port, |
| 148 | uint16_t exc_port, |
| 149 | const char *greeting); |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 150 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 151 | bool |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 152 | SendRequestReattach (uint16_t reply_port); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 153 | |
| 154 | bool |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 155 | SendRequestDisconnect (); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 156 | |
| 157 | uint32_t |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame^] | 158 | 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 Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 176 | 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 Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 189 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 190 | protected: |
| 191 | typedef std::list<std::string> packet_collection; |
| 192 | |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 193 | bool |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 194 | SendRequestPacketNoLock (const PacketStreamType &request_packet); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 195 | |
| 196 | size_t |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 197 | WaitForPacketWithTimeoutMicroSecondsNoLock (lldb_private::DataExtractor &response, |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 198 | uint32_t timeout_usec); |
| 199 | |
| 200 | bool |
| 201 | WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr); |
| 202 | |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 203 | void |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 204 | MakeRequestPacketHeader (CommandType request_type, |
| 205 | PacketStreamType &request_packet, |
| 206 | uint16_t request_length); |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 207 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame^] | 208 | //------------------------------------------------------------------ |
| 209 | // Protected Request Packets (use public accessors which will cache |
| 210 | // results. |
| 211 | //------------------------------------------------------------------ |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 212 | bool |
| 213 | SendRequestVersion (); |
| 214 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame^] | 215 | 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 Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 227 | |
| 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 Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame^] | 241 | 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 Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 256 | |
| 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 Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 265 | //------------------------------------------------------------------ |
| 266 | // Classes that inherit from CommunicationKDP can see and modify these |
| 267 | //------------------------------------------------------------------ |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame^] | 268 | uint32_t m_addr_byte_size; |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 269 | lldb::ByteOrder m_byte_order; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 270 | 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 Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 274 | uint32_t m_session_key; |
| 275 | uint8_t m_request_sequence_id; |
| 276 | uint8_t m_exception_sequence_id; |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 277 | 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 Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 282 | private: |
| 283 | //------------------------------------------------------------------ |
| 284 | // For CommunicationKDP only |
| 285 | //------------------------------------------------------------------ |
| 286 | DISALLOW_COPY_AND_ASSIGN (CommunicationKDP); |
| 287 | }; |
| 288 | |
| 289 | #endif // liblldb_CommunicationKDP_h_ |