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 | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame^] | 143 | bool |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 144 | Connect (uint16_t reply_port, |
| 145 | uint16_t exc_port, |
| 146 | const char *greeting); |
| 147 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame^] | 148 | bool |
| 149 | Reattach (uint16_t reply_port); |
| 150 | |
| 151 | bool |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 152 | Disconnect (); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame^] | 153 | |
| 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 Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 168 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 169 | protected: |
| 170 | typedef std::list<std::string> packet_collection; |
| 171 | |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 172 | bool |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame^] | 173 | SendRequestPacketNoLock (const PacketStreamType &request_packet); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 174 | |
| 175 | size_t |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame^] | 176 | WaitForPacketWithTimeoutMicroSecondsNoLock (lldb_private::DataExtractor &response, |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 177 | uint32_t timeout_usec); |
| 178 | |
| 179 | bool |
| 180 | WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr); |
| 181 | |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 182 | void |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame^] | 183 | MakeRequestPacketHeader (CommandType request_type, |
| 184 | PacketStreamType &request_packet, |
| 185 | uint16_t request_length); |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 186 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame^] | 187 | 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 Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 214 | //------------------------------------------------------------------ |
| 215 | // Classes that inherit from CommunicationKDP can see and modify these |
| 216 | //------------------------------------------------------------------ |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame^] | 217 | lldb::ByteOrder m_byte_order; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 218 | 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 Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 222 | uint32_t m_session_key; |
| 223 | uint8_t m_request_sequence_id; |
| 224 | uint8_t m_exception_sequence_id; |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame^] | 225 | 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 Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 230 | private: |
| 231 | //------------------------------------------------------------------ |
| 232 | // For CommunicationKDP only |
| 233 | //------------------------------------------------------------------ |
| 234 | DISALLOW_COPY_AND_ASSIGN (CommunicationKDP); |
| 235 | }; |
| 236 | |
| 237 | #endif // liblldb_CommunicationKDP_h_ |