1 //===-- DynamicLoaderDarwinKernel.h -----------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_SOURCE_PLUGINS_DYNAMICLOADER_DARWIN_KERNEL_DYNAMICLOADERDARWINKERNEL_H
10 #define LLDB_SOURCE_PLUGINS_DYNAMICLOADER_DARWIN_KERNEL_DYNAMICLOADERDARWINKERNEL_H
17 #include "lldb/Host/SafeMachO.h"
19 #include "lldb/Core/Progress.h"
20 #include "lldb/Target/DynamicLoader.h"
21 #include "lldb/Target/Process.h"
22 #include "lldb/Utility/FileSpec.h"
23 #include "lldb/Utility/UUID.h"
25 class DynamicLoaderDarwinKernel
: public lldb_private::DynamicLoader
{
27 DynamicLoaderDarwinKernel(lldb_private::Process
*process
,
28 lldb::addr_t kernel_addr
);
30 ~DynamicLoaderDarwinKernel() override
;
33 static void Initialize();
35 static void Terminate();
37 static llvm::StringRef
GetPluginNameStatic() { return "darwin-kernel"; }
39 static llvm::StringRef
GetPluginDescriptionStatic();
41 static lldb_private::DynamicLoader
*
42 CreateInstance(lldb_private::Process
*process
, bool force
);
44 static void DebuggerInitialize(lldb_private::Debugger
&debugger
);
46 static lldb::addr_t
SearchForDarwinKernel(lldb_private::Process
*process
);
48 /// Called after attaching a process.
50 /// Allow DynamicLoader plug-ins to execute some code after
51 /// attaching to a process.
52 void DidAttach() override
;
54 void DidLaunch() override
;
56 lldb::ThreadPlanSP
GetStepThroughTrampolinePlan(lldb_private::Thread
&thread
,
57 bool stop_others
) override
;
59 lldb_private::Status
CanLoadImage() override
;
61 // PluginInterface protocol
62 llvm::StringRef
GetPluginName() override
{ return GetPluginNameStatic(); }
65 void PrivateInitialize(lldb_private::Process
*process
);
67 void PrivateProcessStateChanged(lldb_private::Process
*process
,
68 lldb::StateType state
);
70 void UpdateIfNeeded();
72 void LoadKernelModuleIfNeeded();
74 void Clear(bool clear_process
);
76 void PutToLog(lldb_private::Log
*log
) const;
79 BreakpointHitCallback(void *baton
,
80 lldb_private::StoppointCallbackContext
*context
,
81 lldb::user_id_t break_id
, lldb::user_id_t break_loc_id
);
83 bool BreakpointHit(lldb_private::StoppointCallbackContext
*context
,
84 lldb::user_id_t break_id
, lldb::user_id_t break_loc_id
);
85 uint32_t GetAddrByteSize() { return m_kernel
.GetAddressByteSize(); }
87 static lldb::ByteOrder
GetByteOrderFromMagic(uint32_t magic
);
90 KERNEL_MODULE_MAX_NAME
= 64u,
91 // Versions less than 2 didn't have an entry size,
92 // they had a 64 bit name, 16 byte UUID, 8 byte addr,
93 // 8 byte size, 8 byte version, 4 byte load tag, and
95 KERNEL_MODULE_ENTRY_SIZE_VERSION_1
= 64u + 16u + 8u + 8u + 8u + 4u + 4u
98 // class KextImageInfo represents a single kext or kernel binary image.
99 // The class was designed to hold the information from the
100 // OSKextLoadedKextSummary
101 // structure (in libkern/libkern/OSKextLibPrivate.h from xnu). The kernel
103 // a list of loded kexts in memory (the OSKextLoadedKextSummaryHeader
105 // which points to an array of OSKextLoadedKextSummary's).
107 // A KextImageInfos may have -
109 // 1. The load address, name, UUID, and size of a kext/kernel binary in memory
110 // (read straight out of the kernel's list-of-kexts loaded)
111 // 2. A ModuleSP based on a MemoryModule read out of the kernel's memory
112 // (very unlikely to have any symbolic information)
113 // 3. A ModuleSP for an on-disk copy of the kext binary, possibly with debug
117 // For performance reasons, the developer may prefer that lldb not load the
119 // of memory at the start of a kernel session. But we should build up /
121 // list of kexts that the kernel has told us about so we can relocate a kext
123 // later if the user explicitly adds it to the target.
125 class KextImageInfo
{
127 KextImageInfo() : m_name(), m_module_sp(), m_memory_module_sp(), m_uuid() {}
130 m_load_address
= LLDB_INVALID_ADDRESS
;
135 m_memory_module_sp
.reset();
136 m_load_process_stop_id
= UINT32_MAX
;
139 bool LoadImageAtFileAddress(lldb_private::Process
*process
);
141 bool LoadImageUsingMemoryModule(lldb_private::Process
*process
,
142 lldb_private::Progress
*progress
= nullptr);
144 bool IsLoaded() { return m_load_process_stop_id
!= UINT32_MAX
; }
147 lldb::addr_t load_addr
); // Address of the Mach-O header for this binary
150 GetLoadAddress() const; // Address of the Mach-O header for this binary
152 lldb_private::UUID
GetUUID() const;
154 void SetUUID(const lldb_private::UUID
&uuid
);
156 void SetName(const char *);
158 std::string
GetName() const;
160 void SetModule(lldb::ModuleSP module
);
162 lldb::ModuleSP
GetModule();
164 // try to fill in m_memory_module_sp from memory based on the m_load_address
165 bool ReadMemoryModule(lldb_private::Process
*process
);
168 const; // true if this is the mach_kernel; false if this is a kext
170 void SetIsKernel(bool is_kernel
);
172 uint64_t GetSize() const;
174 void SetSize(uint64_t size
);
177 GetProcessStopId() const; // the stop-id when this binary was first noticed
179 void SetProcessStopId(uint32_t stop_id
);
181 bool operator==(const KextImageInfo
&rhs
) const;
183 uint32_t GetAddressByteSize(); // as determined by Mach-O header
185 lldb::ByteOrder
GetByteOrder(); // as determined by Mach-O header
187 lldb_private::ArchSpec
188 GetArchitecture() const; // as determined by Mach-O header
190 void PutToLog(lldb_private::Log
*log
) const;
192 typedef std::vector
<KextImageInfo
> collection
;
193 typedef collection::iterator iterator
;
194 typedef collection::const_iterator const_iterator
;
198 lldb::ModuleSP m_module_sp
;
199 lldb::ModuleSP m_memory_module_sp
;
200 uint32_t m_load_process_stop_id
=
201 UINT32_MAX
; // the stop-id when this module was added
204 m_uuid
; // UUID for this dylib if it has one, else all zeros
205 lldb::addr_t m_load_address
= LLDB_INVALID_ADDRESS
;
207 bool m_kernel_image
=
208 false; // true if this is the kernel, false if this is a kext
211 struct OSKextLoadedKextSummaryHeader
{
212 uint32_t version
= 0;
213 uint32_t entry_size
= 0;
214 uint32_t entry_count
= 0;
215 lldb::addr_t image_infos_addr
= LLDB_INVALID_ADDRESS
;
217 OSKextLoadedKextSummaryHeader() = default;
222 return 0; // Can't know the size without a valid version
224 return 8; // Version 1 only had a version + entry_count
228 // Version 2 and above has version, entry_size, entry_count, and reserved
236 image_infos_addr
= LLDB_INVALID_ADDRESS
;
239 bool IsValid() const { return version
>= 1 && version
<= 2; }
242 void RegisterNotificationCallbacks();
244 void UnregisterNotificationCallbacks();
246 void SetNotificationBreakpointIfNeeded();
248 bool ReadAllKextSummaries();
250 bool ReadKextSummaryHeader();
252 bool ParseKextSummaries(const lldb_private::Address
&kext_summary_addr
,
256 UpdateImageInfosHeaderAndLoadCommands(KextImageInfo::collection
&image_infos
,
257 uint32_t infos_count
,
258 bool update_executable
);
260 uint32_t ReadKextSummaries(const lldb_private::Address
&kext_summary_addr
,
261 uint32_t image_infos_count
,
262 KextImageInfo::collection
&image_infos
);
265 SearchForKernelAtSameLoadAddr(lldb_private::Process
*process
);
268 SearchForKernelWithDebugHints(lldb_private::Process
*process
);
270 static lldb::addr_t
SearchForKernelNearPC(lldb_private::Process
*process
);
273 SearchForKernelViaExhaustiveSearch(lldb_private::Process
*process
);
276 ReadMachHeader(lldb::addr_t addr
, lldb_private::Process
*process
, llvm::MachO::mach_header
&mh
,
277 bool *read_error
= nullptr);
279 static lldb_private::UUID
280 CheckForKernelImageAtAddress(lldb::addr_t addr
,
281 lldb_private::Process
*process
,
282 bool *read_error
= nullptr);
284 lldb::addr_t m_kernel_load_address
;
285 KextImageInfo m_kernel
; // Info about the current kernel image being used
287 lldb_private::Address m_kext_summary_header_ptr_addr
;
288 lldb_private::Address m_kext_summary_header_addr
;
289 OSKextLoadedKextSummaryHeader m_kext_summary_header
;
290 KextImageInfo::collection m_known_kexts
;
291 mutable std::recursive_mutex m_mutex
;
292 lldb::user_id_t m_break_id
;
295 DynamicLoaderDarwinKernel(const DynamicLoaderDarwinKernel
&) = delete;
296 const DynamicLoaderDarwinKernel
&
297 operator=(const DynamicLoaderDarwinKernel
&) = delete;
300 #endif // LLDB_SOURCE_PLUGINS_DYNAMICLOADER_DARWIN_KERNEL_DYNAMICLOADERDARWINKERNEL_H