LLDB mainline
CommunicationKDP.h
Go to the documentation of this file.
1//===-- CommunicationKDP.h --------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_SOURCE_PLUGINS_PROCESS_MACOSX_KERNEL_COMMUNICATIONKDP_H
10#define LLDB_SOURCE_PLUGINS_PROCESS_MACOSX_KERNEL_COMMUNICATIONKDP_H
11
12#include <list>
13#include <mutex>
14#include <string>
15
20#include "lldb/lldb-private.h"
21
23public:
24 const static uint32_t kMaxPacketSize = 1200;
25 const static uint32_t kMaxDataSize = 1024;
60 };
61
62 enum { KDP_FEATURE_BP = (1u << 0) };
63
64 enum KDPError {
69 };
70
75 eCommandTypeMask = 0x7fu
76 };
77 // Constructors and Destructors
78 CommunicationKDP(const char *comm_name);
79
80 ~CommunicationKDP() override;
81
82 bool SendRequestPacket(const PacketStreamType &request_packet);
83
84 // Wait for a packet within 'nsec' seconds
85 size_t
87 uint32_t usec);
88
89 bool GetSequenceMutex(std::unique_lock<std::recursive_mutex> &lock);
90
91 bool CheckForPacket(const uint8_t *src, size_t src_len,
93 bool IsRunning() const { return m_is_running.GetValue(); }
94
95 // Set the global packet timeout.
96 //
97 // For clients, this is the timeout that gets used when sending
98 // packets and waiting for responses. For servers, this might not
99 // get used, and if it doesn't this should be moved to the
100 // CommunicationKDPClient.
101 std::chrono::seconds SetPacketTimeout(std::chrono::seconds packet_timeout) {
102 const auto old_packet_timeout = m_packet_timeout;
103 m_packet_timeout = packet_timeout;
104 return old_packet_timeout;
105 }
106
107 std::chrono::seconds GetPacketTimeout() const { return m_packet_timeout; }
108
109 // Public Request Packets
110 bool SendRequestConnect(uint16_t reply_port, uint16_t exc_port,
111 const char *greeting);
112
113 bool SendRequestReattach(uint16_t reply_port);
114
116
117 uint32_t SendRequestReadMemory(lldb::addr_t addr, void *dst,
118 uint32_t dst_size,
120
121 uint32_t SendRequestWriteMemory(lldb::addr_t addr, const void *src,
122 uint32_t src_len,
124
125 bool SendRawRequest(uint8_t command_byte, const void *src, uint32_t src_len,
128
129 uint32_t SendRequestReadRegisters(uint32_t cpu, uint32_t flavor, void *dst,
130 uint32_t dst_size,
132
133 uint32_t SendRequestWriteRegisters(uint32_t cpu, uint32_t flavor,
134 const void *src, uint32_t src_size,
136
137 const char *GetKernelVersion();
138
139 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
140 // const char *
141 // GetImagePath ();
142
143 uint32_t GetVersion();
144
145 uint32_t GetFeatureFlags();
146
148 return (GetFeatureFlags() & KDP_FEATURE_BP) != 0;
149 }
150
151 uint32_t GetCPUMask();
152
153 uint32_t GetCPUType();
154
155 uint32_t GetCPUSubtype();
156
158
159 bool RemoteIsEFI();
160
162
164
165 bool SendRequestResume();
166
167 bool SendRequestSuspend();
168
169 bool SendRequestBreakpoint(bool set, lldb::addr_t addr);
170
171protected:
172 bool SendRequestPacketNoLock(const PacketStreamType &request_packet);
173
175 lldb_private::DataExtractor &response, uint32_t timeout_usec);
176
177 bool WaitForNotRunningPrivate(const std::chrono::microseconds &timeout);
178
179 void MakeRequestPacketHeader(CommandType request_type,
180 PacketStreamType &request_packet,
181 uint16_t request_length);
182
183 // Protected Request Packets (use public accessors which will cache
184 // results.
185 bool SendRequestVersion();
186
187 bool SendRequestHostInfo();
188
190
191 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
192 // bool
193 // SendRequestImagePath ();
194
195 void DumpPacket(lldb_private::Stream &s, const void *data, uint32_t data_len);
196
198 const lldb_private::DataExtractor &extractor);
199
200 bool VersionIsValid() const { return m_kdp_version_version != 0; }
201
202 bool HostInfoIsValid() const { return m_kdp_hostinfo_cpu_type != 0; }
203
204 bool ExtractIsReply(uint8_t first_packet_byte) const {
205 // TODO: handle big endian...
206 return (first_packet_byte & ePacketTypeMask) != 0;
207 }
208
209 CommandType ExtractCommand(uint8_t first_packet_byte) const {
210 // TODO: handle big endian...
211 return (CommandType)(first_packet_byte & eCommandTypeMask);
212 }
213
214 static const char *GetCommandAsCString(uint8_t command);
215
216 void ClearKDPSettings();
217
218 bool SendRequestAndGetReply(const CommandType command,
219 const PacketStreamType &request_packet,
220 lldb_private::DataExtractor &reply_packet);
221 // Classes that inherit from CommunicationKDP can see and modify these
224 std::string m_bytes;
225 std::recursive_mutex m_bytes_mutex;
226 std::chrono::seconds m_packet_timeout;
227 std::recursive_mutex m_sequence_mutex; // Restrict access to sending/receiving
228 // packets to a single thread at a time
238 std::string m_kernel_version;
239 // std::string m_image_path; // Disable KDP_IMAGEPATH for now, it seems to
240 // hang the KDP connection...
241 lldb::addr_t m_last_read_memory_addr; // Last memory read address for logging
242private:
243 // For CommunicationKDP only
246};
247
248#endif // LLDB_SOURCE_PLUGINS_PROCESS_MACOSX_KERNEL_COMMUNICATIONKDP_H
static llvm::raw_ostream & error(Stream &strm)
bool SendRequestBreakpoint(bool set, lldb::addr_t addr)
bool SendRequestConnect(uint16_t reply_port, uint16_t exc_port, const char *greeting)
bool SendRequestAndGetReply(const CommandType command, const PacketStreamType &request_packet, lldb_private::DataExtractor &reply_packet)
bool SendRequestPacketNoLock(const PacketStreamType &request_packet)
bool WaitForNotRunningPrivate(const std::chrono::microseconds &timeout)
static const uint32_t kMaxPacketSize
std::chrono::seconds SetPacketTimeout(std::chrono::seconds packet_timeout)
lldb::addr_t m_last_read_memory_addr
bool IsRunning() const
void DumpPacket(lldb_private::Stream &s, const void *data, uint32_t data_len)
uint32_t m_kdp_hostinfo_cpu_mask
uint32_t m_kdp_hostinfo_cpu_type
lldb_private::StreamBuffer< 4096 > PacketStreamType
std::chrono::seconds GetPacketTimeout() const
uint32_t SendRequestWriteMemory(lldb::addr_t addr, const void *src, uint32_t src_len, lldb_private::Status &error)
uint32_t m_kdp_version_version
bool SendRawRequest(uint8_t command_byte, const void *src, uint32_t src_len, lldb_private::DataExtractor &reply, lldb_private::Status &error)
bool GetSequenceMutex(std::unique_lock< std::recursive_mutex > &lock)
uint32_t SendRequestReadMemory(lldb::addr_t addr, void *dst, uint32_t dst_size, lldb_private::Status &error)
bool SendRequestPacket(const PacketStreamType &request_packet)
const CommunicationKDP & operator=(const CommunicationKDP &)=delete
lldb::addr_t GetLoadAddress()
size_t WaitForPacketWithTimeoutMicroSecondsNoLock(lldb_private::DataExtractor &response, uint32_t timeout_usec)
static const uint32_t kMaxDataSize
~CommunicationKDP() override
std::recursive_mutex m_bytes_mutex
std::chrono::seconds m_packet_timeout
uint32_t SendRequestReadRegisters(uint32_t cpu, uint32_t flavor, void *dst, uint32_t dst_size, lldb_private::Status &error)
bool CheckForPacket(const uint8_t *src, size_t src_len, lldb_private::DataExtractor &packet)
uint8_t m_request_sequence_id
void MakeRequestPacketHeader(CommandType request_type, PacketStreamType &request_packet, uint16_t request_length)
uint32_t SendRequestWriteRegisters(uint32_t cpu, uint32_t flavor, const void *src, uint32_t src_size, lldb_private::Status &error)
uint32_t m_kdp_version_feature
const char * GetKernelVersion()
uint32_t GetFeatureFlags()
lldb_private::UUID GetUUID()
bool ExtractIsReply(uint8_t first_packet_byte) const
uint8_t m_exception_sequence_id
bool HostInfoIsValid() const
size_t WaitForPacketWithTimeoutMicroSeconds(lldb_private::DataExtractor &response, uint32_t usec)
bool LocalBreakpointsAreSupported()
static const char * GetCommandAsCString(uint8_t command)
uint32_t m_kdp_hostinfo_cpu_subtype
lldb_private::Predicate< bool > m_is_running
std::string m_kernel_version
CommunicationKDP(const CommunicationKDP &)=delete
bool SendRequestReattach(uint16_t reply_port)
CommandType ExtractCommand(uint8_t first_packet_byte) const
lldb::ByteOrder m_byte_order
std::recursive_mutex m_sequence_mutex
bool VersionIsValid() const
An abstract communications class.
Definition: Communication.h:39
An data extractor class.
Definition: DataExtractor.h:48
A C++ wrapper class for providing threaded access to a value of type T.
Definition: Predicate.h:42
T GetValue() const
Value get accessor.
Definition: Predicate.h:71
An error handling class.
Definition: Status.h:115
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
ByteOrder
Byte ordering definitions.
uint64_t addr_t
Definition: lldb-types.h:80