LLDB mainline
DynamicLoaderDarwinKernel.h
Go to the documentation of this file.
1//===-- DynamicLoaderDarwinKernel.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_DYNAMICLOADER_DARWIN_KERNEL_DYNAMICLOADERDARWINKERNEL_H
10#define LLDB_SOURCE_PLUGINS_DYNAMICLOADER_DARWIN_KERNEL_DYNAMICLOADERDARWINKERNEL_H
11
12#include <mutex>
13#include <optional>
14#include <string>
15#include <vector>
16
17
18#include "lldb/Host/SafeMachO.h"
19
20#include "lldb/Core/Progress.h"
23#include "lldb/Target/Process.h"
25#include "lldb/Utility/UUID.h"
26
28public:
30 lldb::addr_t kernel_addr);
31
33
34 // Static Functions
35 static void Initialize();
36
37 static void Terminate();
38
39 static llvm::StringRef GetPluginNameStatic() { return "darwin-kernel"; }
40
41 static llvm::StringRef GetPluginDescriptionStatic();
42
44 CreateInstance(lldb_private::Process *process, bool force);
45
46 static void DebuggerInitialize(lldb_private::Debugger &debugger);
47
49
50 /// Called after attaching a process.
51 ///
52 /// Allow DynamicLoader plug-ins to execute some code after
53 /// attaching to a process.
54 void DidAttach() override;
55
56 void DidLaunch() override;
57
59 bool stop_others) override;
60
62
63 // PluginInterface protocol
64 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
65
66protected:
68
70 lldb::StateType state);
71
72 void UpdateIfNeeded();
73
75
76 void Clear(bool clear_process);
77
78 void PutToLog(lldb_private::Log *log) const;
79
80 static bool
81 BreakpointHitCallback(void *baton,
83 lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
84
86 lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
87 uint32_t GetAddrByteSize() { return m_kernel.GetAddressByteSize(); }
88
89 static lldb::ByteOrder GetByteOrderFromMagic(uint32_t magic);
90
91 enum {
93 // Versions less than 2 didn't have an entry size,
94 // they had a 64 bit name, 16 byte UUID, 8 byte addr,
95 // 8 byte size, 8 byte version, 4 byte load tag, and
96 // 4 byte flags
97 KERNEL_MODULE_ENTRY_SIZE_VERSION_1 = 64u + 16u + 8u + 8u + 8u + 4u + 4u
98 };
99
100 // class KextImageInfo represents a single kext or kernel binary image.
101 // The class was designed to hold the information from the
102 // OSKextLoadedKextSummary
103 // structure (in libkern/libkern/OSKextLibPrivate.h from xnu). The kernel
104 // maintains
105 // a list of loded kexts in memory (the OSKextLoadedKextSummaryHeader
106 // structure,
107 // which points to an array of OSKextLoadedKextSummary's).
108 //
109 // A KextImageInfos may have -
110 //
111 // 1. The load address, name, UUID, and size of a kext/kernel binary in memory
112 // (read straight out of the kernel's list-of-kexts loaded)
113 // 2. A ModuleSP based on a MemoryModule read out of the kernel's memory
114 // (very unlikely to have any symbolic information)
115 // 3. A ModuleSP for an on-disk copy of the kext binary, possibly with debug
116 // info
117 // or a dSYM
118 //
119 // For performance reasons, the developer may prefer that lldb not load the
120 // kexts out
121 // of memory at the start of a kernel session. But we should build up /
122 // maintain a
123 // list of kexts that the kernel has told us about so we can relocate a kext
124 // module
125 // later if the user explicitly adds it to the target.
126
128 public:
130
131 void Clear() {
133 m_size = 0;
134 m_name.clear();
135 m_uuid.Clear();
136 m_module_sp.reset();
137 m_memory_module_sp.reset();
139 }
140
142
143 /// Determine what the search for this binary needs: its UUID, and whether
144 /// the Target or the shared module list already has it. For the kernel,
145 /// the UUID is read from process memory and any kernel with a different
146 /// UUID is removed from the Target.
147 ///
148 /// \param request Set to the search to run, or left empty if none is
149 /// needed.
150 ///
151 /// \return Whether the binary can be loaded.
152 bool
154 std::optional<lldb_private::SymbolLocator::Request> &request);
155
156 /// Add the module to the Target and set its load addresses.
157 ///
158 /// The order of the calls determines the order of the Target's modules.
159 ///
160 /// \param located The search result, or empty if PrepareSearch requested
161 /// no search.
162 bool LoadLocatedImage(
163 lldb_private::Process *process,
164 std::optional<llvm::Expected<lldb_private::SymbolLocator::Result>>
165 located);
166
167 /// Load this binary on its own: PrepareSearch, then the search, then
168 /// LoadLocatedImage.
170
171 /// Searches for the kernel and sets the module, without SymbolLocator.
172 void FindKernel(lldb_private::Target &target,
173 lldb_private::ModuleSpec module_spec);
174
176
177 void SetLoadAddress(
178 lldb::addr_t load_addr); // Address of the Mach-O header for this binary
179
181 GetLoadAddress() const; // Address of the Mach-O header for this binary
182
184
185 void SetUUID(const lldb_private::UUID &uuid);
186
187 void SetName(const char *);
188
189 std::string GetName() const;
190
191 void SetModule(lldb::ModuleSP module);
192
194
195 // try to fill in m_memory_module_sp from memory based on the m_load_address
197
198 bool IsKernel()
199 const; // true if this is the mach_kernel; false if this is a kext
200
201 void SetIsKernel(bool is_kernel);
202
203 uint64_t GetSize() const;
204
205 void SetSize(uint64_t size);
206
207 uint32_t
208 GetProcessStopId() const; // the stop-id when this binary was first noticed
209
210 void SetProcessStopId(uint32_t stop_id);
211
212 bool operator==(const KextImageInfo &rhs) const;
213
214 uint32_t GetAddressByteSize(); // as determined by Mach-O header
215
216 lldb::ByteOrder GetByteOrder(); // as determined by Mach-O header
217
219 GetArchitecture() const; // as determined by Mach-O header
220
221 void PutToLog(lldb_private::Log *log) const;
222
223 typedef std::vector<KextImageInfo> collection;
224 typedef collection::iterator iterator;
225 typedef collection::const_iterator const_iterator;
226
227 private:
228 std::string m_name;
232 UINT32_MAX; // the stop-id when this module was added
233 // to the Target
235 m_uuid; // UUID for this dylib if it has one, else all zeros
237 uint64_t m_size = 0;
239 false; // true if this is the kernel, false if this is a kext
240 };
241
243 uint32_t version = 0;
244 uint32_t entry_size = 0;
245 uint32_t entry_count = 0;
247
249
250 uint32_t GetSize() {
251 switch (version) {
252 case 0:
253 return 0; // Can't know the size without a valid version
254 case 1:
255 return 8; // Version 1 only had a version + entry_count
256 default:
257 break;
258 }
259 // Version 2 and above has version, entry_size, entry_count, and reserved
260 return 16;
261 }
262
263 void Clear() {
264 version = 0;
265 entry_size = 0;
266 entry_count = 0;
268 }
269
270 bool IsValid() const { return version >= 1 && version <= 2; }
271 };
272
274
276
278
280
282
283 bool ParseKextSummaries(const lldb_private::Address &kext_summary_addr,
284 uint32_t count);
285
286 void
288 uint32_t infos_count,
289 bool update_executable);
290
291 uint32_t ReadKextSummaries(const lldb_private::Address &kext_summary_addr,
292 uint32_t image_infos_count,
293 KextImageInfo::collection &image_infos);
294
295 static lldb::addr_t
297
298 static lldb::addr_t
300
302
303 static lldb::addr_t
305
306 static bool
307 ReadMachHeader(lldb::addr_t addr, lldb_private::Process *process, llvm::MachO::mach_header &mh,
308 bool *read_error = nullptr);
309
310 static lldb_private::UUID
312 lldb_private::Process *process,
313 bool *read_error = nullptr);
314
316 KextImageInfo m_kernel; // Info about the current kernel image being used
317
322 mutable std::recursive_mutex m_mutex;
324
325private:
329};
330
331#endif // LLDB_SOURCE_PLUGINS_DYNAMICLOADER_DARWIN_KERNEL_DYNAMICLOADERDARWINKERNEL_H
static bool is_kernel(Module *module)
void FindKernel(lldb_private::Target &target, lldb_private::ModuleSpec module_spec)
Searches for the kernel and sets the module, without SymbolLocator.
bool operator==(const KextImageInfo &rhs) const
bool ReadMemoryModule(lldb_private::Process *process)
void SetUUID(const lldb_private::UUID &uuid)
bool PrepareSearch(lldb_private::Process *process, std::optional< lldb_private::SymbolLocator::Request > &request)
Determine what the search for this binary needs: its UUID, and whether the Target or the shared modul...
bool LoadImageAtFileAddress(lldb_private::Process *process)
bool LoadImageUsingMemoryModule(lldb_private::Process *process)
Load this binary on its own: PrepareSearch, then the search, then LoadLocatedImage.
bool LoadLocatedImage(lldb_private::Process *process, std::optional< llvm::Expected< lldb_private::SymbolLocator::Result > > located)
Add the module to the Target and set its load addresses.
const DynamicLoaderDarwinKernel & operator=(const DynamicLoaderDarwinKernel &)=delete
void DidLaunch() override
Called after attaching a process.
lldb_private::Address m_kext_summary_header_ptr_addr
void PrivateProcessStateChanged(lldb_private::Process *process, lldb::StateType state)
lldb::ThreadPlanSP GetStepThroughTrampolinePlan(lldb_private::Thread &thread, bool stop_others) override
Provides a plan to step through the dynamic loader trampoline for the current state of thread.
static lldb_private::DynamicLoader * CreateInstance(lldb_private::Process *process, bool force)
lldb_private::Address m_kext_summary_header_addr
static void DebuggerInitialize(lldb_private::Debugger &debugger)
static bool ReadMachHeader(lldb::addr_t addr, lldb_private::Process *process, llvm::MachO::mach_header &mh, bool *read_error=nullptr)
void PutToLog(lldb_private::Log *log) const
static lldb::addr_t SearchForKernelViaExhaustiveSearch(lldb_private::Process *process)
void UpdateImageInfosHeaderAndLoadCommands(KextImageInfo::collection &image_infos, uint32_t infos_count, bool update_executable)
static llvm::StringRef GetPluginDescriptionStatic()
lldb_private::Status CanLoadImage() override
Ask if it is ok to try and load or unload an shared library (image).
void DidAttach() override
Called after attaching a process.
static lldb::addr_t SearchForKernelAtSameLoadAddr(lldb_private::Process *process)
static llvm::StringRef GetPluginNameStatic()
llvm::StringRef GetPluginName() override
OSKextLoadedKextSummaryHeader m_kext_summary_header
void PrivateInitialize(lldb_private::Process *process)
DynamicLoaderDarwinKernel(lldb_private::Process *process, lldb::addr_t kernel_addr)
static lldb_private::UUID CheckForKernelImageAtAddress(lldb::addr_t addr, lldb_private::Process *process, bool *read_error=nullptr)
static bool BreakpointHitCallback(void *baton, lldb_private::StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
static lldb::ByteOrder GetByteOrderFromMagic(uint32_t magic)
bool BreakpointHit(lldb_private::StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
static lldb::addr_t SearchForKernelNearPC(lldb_private::Process *process)
DynamicLoaderDarwinKernel(const DynamicLoaderDarwinKernel &)=delete
uint32_t ReadKextSummaries(const lldb_private::Address &kext_summary_addr, uint32_t image_infos_count, KextImageInfo::collection &image_infos)
static lldb::addr_t SearchForDarwinKernel(lldb_private::Process *process)
KextImageInfo::collection m_known_kexts
static lldb::addr_t SearchForKernelWithDebugHints(lldb_private::Process *process)
bool ParseKextSummaries(const lldb_private::Address &kext_summary_addr, uint32_t count)
A section + offset based address class.
Definition Address.h:62
An architecture specification class.
Definition ArchSpec.h:32
A class to manage flag bits.
Definition Debugger.h:103
A plug-in interface definition class for dynamic loaders.
A plug-in interface definition class for debugging a process.
Definition Process.h:369
An error handling class.
Definition Status.h:118
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
Represents UUID's of various sizes.
Definition UUID.h:27
#define LLDB_INVALID_ADDRESS
#define UINT32_MAX
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
StateType
Process and Thread States.
ByteOrder
Byte ordering definitions.
uint64_t user_id_t
Definition lldb-types.h:83
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Module > ModuleSP