1 //===-- CommunicationKDP.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_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" 23 #include "lldb/Core/StreamBuffer.h" 24 #include "lldb/Host/Mutex.h" 25 #include "lldb/Host/Predicate.h" 26 #include "lldb/Host/TimeValue.h" 27 28 class CommunicationKDP : public lldb_private::Communication 29 { 30 public: 31 enum 32 { 33 eBroadcastBitRunPacketSent = kLoUserBroadcastBit 34 }; 35 36 const static uint32_t kMaxPacketSize = 1200; 37 const static uint32_t kMaxDataSize = 1024; 38 typedef lldb_private::StreamBuffer<1024> PacketStreamType; 39 typedef enum 40 { 41 KDP_CONNECT = 0u, 42 KDP_DISCONNECT, 43 KDP_HOSTINFO, 44 KDP_VERSION, 45 KDP_MAXBYTES, 46 KDP_READMEM, 47 KDP_WRITEMEM, 48 KDP_READREGS, 49 KDP_WRITEREGS, 50 KDP_LOAD, 51 KDP_IMAGEPATH, 52 KDP_SUSPEND, 53 KDP_RESUMECPUS, 54 KDP_EXCEPTION, 55 KDP_TERMINATION, 56 KDP_BREAKPOINT_SET, 57 KDP_BREAKPOINT_REMOVE, 58 KDP_REGIONS, 59 KDP_REATTACH, 60 KDP_HOSTREBOOT, 61 KDP_READMEM64, 62 KDP_WRITEMEM64, 63 KDP_BREAKPOINT_SET64, 64 KDP_BREAKPOINT_REMOVE64, 65 KDP_KERNELVERSION, 66 KDP_READPHYSMEM64, 67 KDP_WRITEPHYSMEM64, 68 KDP_READIOPORT, 69 KDP_WRITEIOPORT, 70 KDP_READMSR64, 71 KDP_WRITEMSR64, 72 KDP_DUMPINFO 73 } CommandType; 74 75 enum 76 { 77 KDP_FEATURE_BP = (1u << 0) 78 }; 79 80 typedef enum 81 { 82 KDP_PROTERR_SUCCESS = 0, 83 KDP_PROTERR_ALREADY_CONNECTED, 84 KDP_PROTERR_BAD_NBYTES, 85 KDP_PROTERR_BADFLAVOR 86 } KDPError; 87 88 typedef enum 89 { 90 ePacketTypeRequest = 0x00u, 91 ePacketTypeReply = 0x80u, 92 ePacketTypeMask = 0x80u, 93 eCommandTypeMask = 0x7fu 94 } PacketType; 95 //------------------------------------------------------------------ 96 // Constructors and Destructors 97 //------------------------------------------------------------------ 98 CommunicationKDP (const char *comm_name); 99 100 virtual 101 ~CommunicationKDP(); 102 103 bool 104 SendRequestPacket (const PacketStreamType &request_packet); 105 106 // Wait for a packet within 'nsec' seconds 107 size_t 108 WaitForPacketWithTimeoutMicroSeconds (lldb_private::DataExtractor &response, 109 uint32_t usec); 110 111 bool 112 GetSequenceMutex(lldb_private::Mutex::Locker& locker); 113 114 bool 115 CheckForPacket (const uint8_t *src, 116 size_t src_len, 117 lldb_private::DataExtractor &packet); 118 bool 119 IsRunning() const 120 { 121 return m_is_running.GetValue(); 122 } 123 124 //------------------------------------------------------------------ 125 // Set the global packet timeout. 126 // 127 // For clients, this is the timeout that gets used when sending 128 // packets and waiting for responses. For servers, this might not 129 // get used, and if it doesn't this should be moved to the 130 // CommunicationKDPClient. 131 //------------------------------------------------------------------ 132 uint32_t 133 SetPacketTimeout (uint32_t packet_timeout) 134 { 135 const uint32_t old_packet_timeout = m_packet_timeout; 136 m_packet_timeout = packet_timeout; 137 return old_packet_timeout; 138 } 139 140 uint32_t 141 GetPacketTimeoutInMicroSeconds () const 142 { 143 return m_packet_timeout * lldb_private::TimeValue::MicroSecPerSec; 144 } 145 146 //------------------------------------------------------------------ 147 // Public Request Packets 148 //------------------------------------------------------------------ 149 bool 150 SendRequestConnect (uint16_t reply_port, 151 uint16_t exc_port, 152 const char *greeting); 153 154 bool 155 SendRequestReattach (uint16_t reply_port); 156 157 bool 158 SendRequestDisconnect (); 159 160 uint32_t 161 SendRequestReadMemory (lldb::addr_t addr, 162 void *dst, 163 uint32_t dst_size, 164 lldb_private::Error &error); 165 166 uint32_t 167 SendRequestWriteMemory (lldb::addr_t addr, 168 const void *src, 169 uint32_t src_len, 170 lldb_private::Error &error); 171 172 bool 173 SendRawRequest (uint8_t command_byte, 174 const void *src, 175 uint32_t src_len, 176 lldb_private::DataExtractor &reply, 177 lldb_private::Error &error); 178 179 uint32_t 180 SendRequestReadRegisters (uint32_t cpu, 181 uint32_t flavor, 182 void *dst, 183 uint32_t dst_size, 184 lldb_private::Error &error); 185 186 uint32_t 187 SendRequestWriteRegisters (uint32_t cpu, 188 uint32_t flavor, 189 const void *src, 190 uint32_t src_size, 191 lldb_private::Error &error); 192 193 const char * 194 GetKernelVersion (); 195 196 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection... 197 // const char * 198 // GetImagePath (); 199 200 uint32_t 201 GetVersion (); 202 203 uint32_t 204 GetFeatureFlags (); 205 206 bool 207 LocalBreakpointsAreSupported () 208 { 209 return (GetFeatureFlags() & KDP_FEATURE_BP) != 0; 210 } 211 212 uint32_t 213 GetCPUMask (); 214 215 uint32_t 216 GetCPUType (); 217 218 uint32_t 219 GetCPUSubtype (); 220 221 lldb_private::UUID 222 GetUUID (); 223 224 bool 225 RemoteIsEFI (); 226 227 bool 228 RemoteIsDarwinKernel (); 229 230 lldb::addr_t 231 GetLoadAddress (); 232 233 bool 234 SendRequestResume (); 235 236 bool 237 SendRequestSuspend (); 238 239 bool 240 SendRequestBreakpoint (bool set, lldb::addr_t addr); 241 242 protected: 243 244 bool 245 SendRequestPacketNoLock (const PacketStreamType &request_packet); 246 247 size_t 248 WaitForPacketWithTimeoutMicroSecondsNoLock (lldb_private::DataExtractor &response, 249 uint32_t timeout_usec); 250 251 bool 252 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr); 253 254 void 255 MakeRequestPacketHeader (CommandType request_type, 256 PacketStreamType &request_packet, 257 uint16_t request_length); 258 259 //------------------------------------------------------------------ 260 // Protected Request Packets (use public accessors which will cache 261 // results. 262 //------------------------------------------------------------------ 263 bool 264 SendRequestVersion (); 265 266 bool 267 SendRequestHostInfo (); 268 269 bool 270 SendRequestKernelVersion (); 271 272 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection... 273 //bool 274 //SendRequestImagePath (); 275 276 void 277 DumpPacket (lldb_private::Stream &s, 278 const void *data, 279 uint32_t data_len); 280 281 void 282 DumpPacket (lldb_private::Stream &s, 283 const lldb_private::DataExtractor& extractor); 284 285 bool 286 VersionIsValid() const 287 { 288 return m_kdp_version_version != 0; 289 } 290 291 bool 292 HostInfoIsValid() const 293 { 294 return m_kdp_hostinfo_cpu_type != 0; 295 } 296 297 bool 298 ExtractIsReply (uint8_t first_packet_byte) const 299 { 300 // TODO: handle big endian... 301 return (first_packet_byte & ePacketTypeMask) != 0; 302 } 303 304 CommandType 305 ExtractCommand (uint8_t first_packet_byte) const 306 { 307 // TODO: handle big endian... 308 return (CommandType)(first_packet_byte & eCommandTypeMask); 309 } 310 311 static const char * 312 GetCommandAsCString (uint8_t command); 313 314 void 315 ClearKDPSettings (); 316 317 bool 318 SendRequestAndGetReply (const CommandType command, 319 const PacketStreamType &request_packet, 320 lldb_private::DataExtractor &reply_packet); 321 //------------------------------------------------------------------ 322 // Classes that inherit from CommunicationKDP can see and modify these 323 //------------------------------------------------------------------ 324 uint32_t m_addr_byte_size; 325 lldb::ByteOrder m_byte_order; 326 uint32_t m_packet_timeout; 327 lldb_private::Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time 328 lldb_private::Predicate<bool> m_is_running; 329 uint32_t m_session_key; 330 uint8_t m_request_sequence_id; 331 uint8_t m_exception_sequence_id; 332 uint32_t m_kdp_version_version; 333 uint32_t m_kdp_version_feature; 334 uint32_t m_kdp_hostinfo_cpu_mask; 335 uint32_t m_kdp_hostinfo_cpu_type; 336 uint32_t m_kdp_hostinfo_cpu_subtype; 337 std::string m_kernel_version; 338 //std::string m_image_path; // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection... 339 lldb::addr_t m_last_read_memory_addr; // Last memory read address for logging 340 private: 341 //------------------------------------------------------------------ 342 // For CommunicationKDP only 343 //------------------------------------------------------------------ 344 DISALLOW_COPY_AND_ASSIGN (CommunicationKDP); 345 }; 346 347 #endif // liblldb_CommunicationKDP_h_ 348