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 
17 #include "lldb/Core/StreamBuffer.h"
18 #include "lldb/Utility/Listener.h"
19 #include "lldb/Utility/Predicate.h"
20 #include "lldb/lldb-private.h"
21 
23 public:
24  enum { eBroadcastBitRunPacketSent = kLoUserBroadcastBit };
25 
26  const static uint32_t kMaxPacketSize = 1200;
27  const static uint32_t kMaxDataSize = 1024;
29  enum CommandType {
62  };
63 
64  enum { KDP_FEATURE_BP = (1u << 0) };
65 
66  enum KDPError {
71  };
72 
73  enum PacketType {
76  ePacketTypeMask = 0x80u,
78  };
79  // Constructors and Destructors
80  CommunicationKDP(const char *comm_name);
81 
82  virtual ~CommunicationKDP();
83 
84  bool SendRequestPacket(const PacketStreamType &request_packet);
85 
86  // Wait for a packet within 'nsec' seconds
87  size_t
89  uint32_t usec);
90 
91  bool GetSequenceMutex(std::unique_lock<std::recursive_mutex> &lock);
92 
93  bool CheckForPacket(const uint8_t *src, size_t src_len,
95  bool IsRunning() const { return m_is_running.GetValue(); }
96 
97  // Set the global packet timeout.
98  //
99  // For clients, this is the timeout that gets used when sending
100  // packets and waiting for responses. For servers, this might not
101  // get used, and if it doesn't this should be moved to the
102  // CommunicationKDPClient.
103  std::chrono::seconds SetPacketTimeout(std::chrono::seconds packet_timeout) {
104  const auto old_packet_timeout = m_packet_timeout;
105  m_packet_timeout = packet_timeout;
106  return old_packet_timeout;
107  }
108 
109  std::chrono::seconds GetPacketTimeout() const { return m_packet_timeout; }
110 
111  // Public Request Packets
112  bool SendRequestConnect(uint16_t reply_port, uint16_t exc_port,
113  const char *greeting);
114 
115  bool SendRequestReattach(uint16_t reply_port);
116 
117  bool SendRequestDisconnect();
118 
120  uint32_t dst_size,
122 
123  uint32_t SendRequestWriteMemory(lldb::addr_t addr, const void *src,
124  uint32_t src_len,
126 
127  bool SendRawRequest(uint8_t command_byte, const void *src, uint32_t src_len,
130 
131  uint32_t SendRequestReadRegisters(uint32_t cpu, uint32_t flavor, void *dst,
132  uint32_t dst_size,
134 
136  const void *src, uint32_t src_size,
138 
139  const char *GetKernelVersion();
140 
141  // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
142  // const char *
143  // GetImagePath ();
144 
146 
148 
150  return (GetFeatureFlags() & KDP_FEATURE_BP) != 0;
151  }
152 
154 
156 
158 
160 
161  bool RemoteIsEFI();
162 
163  bool RemoteIsDarwinKernel();
164 
166 
167  bool SendRequestResume();
168 
169  bool SendRequestSuspend();
170 
171  bool SendRequestBreakpoint(bool set, lldb::addr_t addr);
172 
173 protected:
174  bool SendRequestPacketNoLock(const PacketStreamType &request_packet);
175 
177  lldb_private::DataExtractor &response, uint32_t timeout_usec);
178 
179  bool WaitForNotRunningPrivate(const std::chrono::microseconds &timeout);
180 
181  void MakeRequestPacketHeader(CommandType request_type,
182  PacketStreamType &request_packet,
183  uint16_t request_length);
184 
185  // Protected Request Packets (use public accessors which will cache
186  // results.
187  bool SendRequestVersion();
188 
189  bool SendRequestHostInfo();
190 
192 
193  // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
194  // bool
195  // SendRequestImagePath ();
196 
197  void DumpPacket(lldb_private::Stream &s, const void *data, uint32_t data_len);
198 
200  const lldb_private::DataExtractor &extractor);
201 
202  bool VersionIsValid() const { return m_kdp_version_version != 0; }
203 
204  bool HostInfoIsValid() const { return m_kdp_hostinfo_cpu_type != 0; }
205 
206  bool ExtractIsReply(uint8_t first_packet_byte) const {
207  // TODO: handle big endian...
208  return (first_packet_byte & ePacketTypeMask) != 0;
209  }
210 
211  CommandType ExtractCommand(uint8_t first_packet_byte) const {
212  // TODO: handle big endian...
213  return (CommandType)(first_packet_byte & eCommandTypeMask);
214  }
215 
216  static const char *GetCommandAsCString(uint8_t command);
217 
218  void ClearKDPSettings();
219 
220  bool SendRequestAndGetReply(const CommandType command,
221  const PacketStreamType &request_packet,
222  lldb_private::DataExtractor &reply_packet);
223  // Classes that inherit from CommunicationKDP can see and modify these
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
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
242 private:
243  // For CommunicationKDP only
244  CommunicationKDP(const CommunicationKDP &) = delete;
245  const CommunicationKDP &operator=(const CommunicationKDP &) = delete;
246 };
247 
248 #endif // LLDB_SOURCE_PLUGINS_PROCESS_MACOSX_KERNEL_COMMUNICATIONKDP_H
CommunicationKDP::m_request_sequence_id
uint8_t m_request_sequence_id
Definition: CommunicationKDP.h:231
CommunicationKDP::m_exception_sequence_id
uint8_t m_exception_sequence_id
Definition: CommunicationKDP.h:232
CommunicationKDP::SendRequestReadRegisters
uint32_t SendRequestReadRegisters(uint32_t cpu, uint32_t flavor, void *dst, uint32_t dst_size, lldb_private::Status &error)
Definition: CommunicationKDP.cpp:1167
lldb_private::UUID
Definition: UUID.h:23
CommunicationKDP::m_kdp_version_feature
uint32_t m_kdp_version_feature
Definition: CommunicationKDP.h:234
CommunicationKDP::KDP_WRITEMSR64
@ KDP_WRITEMSR64
Definition: CommunicationKDP.h:60
CommunicationKDP::SendRequestVersion
bool SendRequestVersion()
Definition: CommunicationKDP.cpp:400
CommunicationKDP::GetCPUSubtype
uint32_t GetCPUSubtype()
Definition: CommunicationKDP.cpp:428
CommunicationKDP::KDP_WRITEPHYSMEM64
@ KDP_WRITEPHYSMEM64
Definition: CommunicationKDP.h:56
CommunicationKDP::SendRequestPacketNoLock
bool SendRequestPacketNoLock(const PacketStreamType &request_packet)
Definition: CommunicationKDP.cpp:130
CommunicationKDP::m_kdp_hostinfo_cpu_subtype
uint32_t m_kdp_hostinfo_cpu_subtype
Definition: CommunicationKDP.h:237
CommunicationKDP::eBroadcastBitRunPacketSent
@ eBroadcastBitRunPacketSent
Definition: CommunicationKDP.h:24
lldb_private::Predicate< bool >
CommunicationKDP::GetPacketTimeout
std::chrono::seconds GetPacketTimeout() const
Definition: CommunicationKDP.h:109
CommunicationKDP::LocalBreakpointsAreSupported
bool LocalBreakpointsAreSupported()
Definition: CommunicationKDP.h:149
CommunicationKDP::KDP_VERSION
@ KDP_VERSION
Definition: CommunicationKDP.h:33
CommunicationKDP::GetFeatureFlags
uint32_t GetFeatureFlags()
Definition: CommunicationKDP.cpp:394
CommunicationKDP::SendRequestAndGetReply
bool SendRequestAndGetReply(const CommandType command, const PacketStreamType &request_packet, lldb_private::DataExtractor &reply_packet)
Definition: CommunicationKDP.cpp:64
CommunicationKDP::RemoteIsDarwinKernel
bool RemoteIsDarwinKernel()
Definition: CommunicationKDP.cpp:461
CommunicationKDP::KDP_CONNECT
@ KDP_CONNECT
Definition: CommunicationKDP.h:30
CommunicationKDP::ClearKDPSettings
void ClearKDPSettings()
Definition: CommunicationKDP.cpp:357
CommunicationKDP::ePacketTypeReply
@ ePacketTypeReply
Definition: CommunicationKDP.h:75
CommunicationKDP::KDP_READMEM
@ KDP_READMEM
Definition: CommunicationKDP.h:35
CommunicationKDP::KDP_MAXBYTES
@ KDP_MAXBYTES
Definition: CommunicationKDP.h:34
CommunicationKDP::m_session_key
uint32_t m_session_key
Definition: CommunicationKDP.h:230
CommunicationKDP::WaitForPacketWithTimeoutMicroSecondsNoLock
size_t WaitForPacketWithTimeoutMicroSecondsNoLock(lldb_private::DataExtractor &response, uint32_t timeout_usec)
Definition: CommunicationKDP.cpp:176
lldb_private::Stream
Definition: Stream.h:28
CommunicationKDP::KDP_WRITEMEM
@ KDP_WRITEMEM
Definition: CommunicationKDP.h:36
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
CommunicationKDP::RemoteIsEFI
bool RemoteIsEFI()
Definition: CommunicationKDP.cpp:455
Predicate.h
CommunicationKDP::GetUUID
lldb_private::UUID GetUUID()
Definition: CommunicationKDP.cpp:434
CommunicationKDP::CommandType
CommandType
Definition: CommunicationKDP.h:29
CommunicationKDP::KDP_DISCONNECT
@ KDP_DISCONNECT
Definition: CommunicationKDP.h:31
Listener.h
CommunicationKDP::KDP_READPHYSMEM64
@ KDP_READPHYSMEM64
Definition: CommunicationKDP.h:55
CommunicationKDP::SendRequestSuspend
bool SendRequestSuspend()
Definition: CommunicationKDP.cpp:1273
CommunicationKDP::eCommandTypeMask
@ eCommandTypeMask
Definition: CommunicationKDP.h:77
CommunicationKDP::KDP_READMSR64
@ KDP_READMSR64
Definition: CommunicationKDP.h:59
CommunicationKDP::kMaxPacketSize
const static uint32_t kMaxPacketSize
Definition: CommunicationKDP.h:26
CommunicationKDP::SendRequestReadMemory
uint32_t SendRequestReadMemory(lldb::addr_t addr, void *dst, uint32_t dst_size, lldb_private::Status &error)
Definition: CommunicationKDP.cpp:546
lldb_private::Communication
Definition: Communication.h:84
CommunicationKDP::SendRequestResume
bool SendRequestResume()
Definition: CommunicationKDP.cpp:1238
StreamBuffer.h
CommunicationKDP::GetVersion
uint32_t GetVersion()
Definition: CommunicationKDP.cpp:388
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
CommunicationKDP::SendRequestWriteRegisters
uint32_t SendRequestWriteRegisters(uint32_t cpu, uint32_t flavor, const void *src, uint32_t src_size, lldb_private::Status &error)
Definition: CommunicationKDP.cpp:1209
CommunicationKDP::KDP_HOSTREBOOT
@ KDP_HOSTREBOOT
Definition: CommunicationKDP.h:49
CommunicationKDP::KDP_REATTACH
@ KDP_REATTACH
Definition: CommunicationKDP.h:48
CommunicationKDP::kMaxDataSize
const static uint32_t kMaxDataSize
Definition: CommunicationKDP.h:27
CommunicationKDP::DumpPacket
void DumpPacket(lldb_private::Stream &s, const void *data, uint32_t data_len)
Definition: CommunicationKDP.cpp:716
lldb_private::DataExtractor
Definition: DataExtractor.h:48
CommunicationKDP::KDP_READIOPORT
@ KDP_READIOPORT
Definition: CommunicationKDP.h:57
CommunicationKDP::KDPError
KDPError
Definition: CommunicationKDP.h:66
CommunicationKDP::m_byte_order
lldb::ByteOrder m_byte_order
Definition: CommunicationKDP.h:225
CommunicationKDP::m_kernel_version
std::string m_kernel_version
Definition: CommunicationKDP.h:238
CommunicationKDP::KDP_PROTERR_ALREADY_CONNECTED
@ KDP_PROTERR_ALREADY_CONNECTED
Definition: CommunicationKDP.h:68
CommunicationKDP::GetSequenceMutex
bool GetSequenceMutex(std::unique_lock< std::recursive_mutex > &lock)
Definition: CommunicationKDP.cpp:157
CommunicationKDP::SendRequestDisconnect
bool SendRequestDisconnect()
Definition: CommunicationKDP.cpp:532
CommunicationKDP::KDP_WRITEIOPORT
@ KDP_WRITEIOPORT
Definition: CommunicationKDP.h:58
CommunicationKDP::KDP_IMAGEPATH
@ KDP_IMAGEPATH
Definition: CommunicationKDP.h:40
CommunicationKDP::KDP_EXCEPTION
@ KDP_EXCEPTION
Definition: CommunicationKDP.h:43
CommunicationKDP::MakeRequestPacketHeader
void MakeRequestPacketHeader(CommandType request_type, PacketStreamType &request_packet, uint16_t request_length)
Definition: CommunicationKDP.cpp:52
CommunicationKDP::KDP_PROTERR_BAD_NBYTES
@ KDP_PROTERR_BAD_NBYTES
Definition: CommunicationKDP.h:69
CommunicationKDP::CommunicationKDP
CommunicationKDP(const char *comm_name)
Definition: CommunicationKDP.cpp:31
CommunicationKDP::GetCPUMask
uint32_t GetCPUMask()
Definition: CommunicationKDP.cpp:416
CommunicationKDP::KDP_BREAKPOINT_SET
@ KDP_BREAKPOINT_SET
Definition: CommunicationKDP.h:45
CommunicationKDP::KDP_FEATURE_BP
@ KDP_FEATURE_BP
Definition: CommunicationKDP.h:64
CommunicationKDP::GetCommandAsCString
static const char * GetCommandAsCString(uint8_t command)
Definition: CommunicationKDP.cpp:646
CommunicationKDP::ExtractIsReply
bool ExtractIsReply(uint8_t first_packet_byte) const
Definition: CommunicationKDP.h:206
CommunicationKDP::WaitForNotRunningPrivate
bool WaitForNotRunningPrivate(const std::chrono::microseconds &timeout)
Definition: CommunicationKDP.cpp:164
CommunicationKDP::m_is_running
lldb_private::Predicate< bool > m_is_running
Definition: CommunicationKDP.h:229
CommunicationKDP
Definition: CommunicationKDP.h:22
set
set(option_framework FRAMEWORK) endif() if(LLDB_ENABLE_PYTHON) get_target_property(python_bindings_dir swig_wrapper_python BINARY_DIR) set(lldb_python_wrapper $
Definition: API/CMakeLists.txt:9
CommunicationKDP::KDP_LOAD
@ KDP_LOAD
Definition: CommunicationKDP.h:39
CommunicationKDP::m_addr_byte_size
uint32_t m_addr_byte_size
Definition: CommunicationKDP.h:224
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
Communication.h
CommunicationKDP::KDP_RESUMECPUS
@ KDP_RESUMECPUS
Definition: CommunicationKDP.h:42
CommunicationKDP::ePacketTypeMask
@ ePacketTypeMask
Definition: CommunicationKDP.h:76
CommunicationKDP::KDP_READMEM64
@ KDP_READMEM64
Definition: CommunicationKDP.h:50
lldb-private.h
CommunicationKDP::SendRawRequest
bool SendRawRequest(uint8_t command_byte, const void *src, uint32_t src_len, lldb_private::DataExtractor &reply, lldb_private::Status &error)
Definition: CommunicationKDP.cpp:617
CommunicationKDP::KDP_WRITEREGS
@ KDP_WRITEREGS
Definition: CommunicationKDP.h:38
CommunicationKDP::SendRequestBreakpoint
bool SendRequestBreakpoint(bool set, lldb::addr_t addr)
Definition: CommunicationKDP.cpp:1250
CommunicationKDP::KDP_KERNELVERSION
@ KDP_KERNELVERSION
Definition: CommunicationKDP.h:54
CommunicationKDP::GetCPUType
uint32_t GetCPUType()
Definition: CommunicationKDP.cpp:422
CommunicationKDP::KDP_SUSPEND
@ KDP_SUSPEND
Definition: CommunicationKDP.h:41
lldb_private::Status
Definition: Status.h:44
CommunicationKDP::SendRequestConnect
bool SendRequestConnect(uint16_t reply_port, uint16_t exc_port, const char *greeting)
Definition: CommunicationKDP.cpp:334
uint32_t
CommunicationKDP::ExtractCommand
CommandType ExtractCommand(uint8_t first_packet_byte) const
Definition: CommunicationKDP.h:211
CommunicationKDP::KDP_BREAKPOINT_REMOVE
@ KDP_BREAKPOINT_REMOVE
Definition: CommunicationKDP.h:46
CommunicationKDP::operator=
const CommunicationKDP & operator=(const CommunicationKDP &)=delete
CommunicationKDP::KDP_REGIONS
@ KDP_REGIONS
Definition: CommunicationKDP.h:47
CommunicationKDP::KDP_BREAKPOINT_REMOVE64
@ KDP_BREAKPOINT_REMOVE64
Definition: CommunicationKDP.h:53
CommunicationKDP::m_packet_timeout
std::chrono::seconds m_packet_timeout
Definition: CommunicationKDP.h:226
CommunicationKDP::m_last_read_memory_addr
lldb::addr_t m_last_read_memory_addr
Definition: CommunicationKDP.h:241
CommunicationKDP::IsRunning
bool IsRunning() const
Definition: CommunicationKDP.h:95
uint16_t
CommunicationKDP::CheckForPacket
bool CheckForPacket(const uint8_t *src, size_t src_len, lldb_private::DataExtractor &packet)
Definition: CommunicationKDP.cpp:229
CommunicationKDP::~CommunicationKDP
virtual ~CommunicationKDP()
Definition: CommunicationKDP.cpp:40
CommunicationKDP::m_kdp_version_version
uint32_t m_kdp_version_version
Definition: CommunicationKDP.h:233
CommunicationKDP::KDP_WRITEMEM64
@ KDP_WRITEMEM64
Definition: CommunicationKDP.h:51
CommunicationKDP::KDP_PROTERR_BADFLAVOR
@ KDP_PROTERR_BADFLAVOR
Definition: CommunicationKDP.h:70
lldb_private::StreamBuffer
Definition: StreamBuffer.h:19
CommunicationKDP::KDP_TERMINATION
@ KDP_TERMINATION
Definition: CommunicationKDP.h:44
CommunicationKDP::WaitForPacketWithTimeoutMicroSeconds
size_t WaitForPacketWithTimeoutMicroSeconds(lldb_private::DataExtractor &response, uint32_t usec)
Definition: CommunicationKDP.cpp:170
CommunicationKDP::KDP_HOSTINFO
@ KDP_HOSTINFO
Definition: CommunicationKDP.h:32
CommunicationKDP::PacketStreamType
lldb_private::StreamBuffer< 4096 > PacketStreamType
Definition: CommunicationKDP.h:28
CommunicationKDP::KDP_BREAKPOINT_SET64
@ KDP_BREAKPOINT_SET64
Definition: CommunicationKDP.h:52
CommunicationKDP::SetPacketTimeout
std::chrono::seconds SetPacketTimeout(std::chrono::seconds packet_timeout)
Definition: CommunicationKDP.h:103
CommunicationKDP::SendRequestPacket
bool SendRequestPacket(const PacketStreamType &request_packet)
Definition: CommunicationKDP.cpp:46
CommunicationKDP::KDP_READREGS
@ KDP_READREGS
Definition: CommunicationKDP.h:37
CommunicationKDP::SendRequestReattach
bool SendRequestReattach(uint16_t reply_port)
Definition: CommunicationKDP.cpp:366
CommunicationKDP::VersionIsValid
bool VersionIsValid() const
Definition: CommunicationKDP.h:202
lldb_private::Predicate::GetValue
T GetValue() const
Value get accessor.
Definition: Predicate.h:71
CommunicationKDP::KDP_DUMPINFO
@ KDP_DUMPINFO
Definition: CommunicationKDP.h:61
CommunicationKDP::m_kdp_hostinfo_cpu_type
uint32_t m_kdp_hostinfo_cpu_type
Definition: CommunicationKDP.h:236
CommunicationKDP::SendRequestWriteMemory
uint32_t SendRequestWriteMemory(lldb::addr_t addr, const void *src, uint32_t src_len, lldb_private::Status &error)
Definition: CommunicationKDP.cpp:584
CommunicationKDP::ePacketTypeRequest
@ ePacketTypeRequest
Definition: CommunicationKDP.h:74
CommunicationKDP::GetKernelVersion
const char * GetKernelVersion()
Definition: CommunicationKDP.cpp:510
CommunicationKDP::m_sequence_mutex
std::recursive_mutex m_sequence_mutex
Definition: CommunicationKDP.h:227
CommunicationKDP::GetLoadAddress
lldb::addr_t GetLoadAddress()
Definition: CommunicationKDP.cpp:467
CommunicationKDP::KDP_PROTERR_SUCCESS
@ KDP_PROTERR_SUCCESS
Definition: CommunicationKDP.h:67
CommunicationKDP::SendRequestHostInfo
bool SendRequestHostInfo()
Definition: CommunicationKDP.cpp:486
CommunicationKDP::SendRequestKernelVersion
bool SendRequestKernelVersion()
Definition: CommunicationKDP.cpp:516
CommunicationKDP::m_kdp_hostinfo_cpu_mask
uint32_t m_kdp_hostinfo_cpu_mask
Definition: CommunicationKDP.h:235
lldb::ByteOrder
ByteOrder
Byte ordering definitions.
Definition: lldb-enumerations.h:138
CommunicationKDP::PacketType
PacketType
Definition: CommunicationKDP.h:73
CommunicationKDP::HostInfoIsValid
bool HostInfoIsValid() const
Definition: CommunicationKDP.h:204