xref: /llvm-project/lldb/source/Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h (revision d76fb6ea1230fd96e69e2eb76c10887fdc2ec7d6)
1 //===-- DynamicLoaderDarwinKernel.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_DynamicLoaderDarwinKernel_h_
11 #define liblldb_DynamicLoaderDarwinKernel_h_
12 
13 // C Includes
14 // C++ Includes
15 #include <map>
16 #include <vector>
17 #include <string>
18 
19 // Other libraries and framework includes
20 #include "llvm/Support/MachO.h"
21 
22 #include "lldb/Target/DynamicLoader.h"
23 #include "lldb/Host/FileSpec.h"
24 #include "lldb/Host/TimeValue.h"
25 #include "lldb/Core/UUID.h"
26 #include "lldb/Host/Mutex.h"
27 #include "lldb/Target/Process.h"
28 
29 class DynamicLoaderDarwinKernel : public lldb_private::DynamicLoader
30 {
31 public:
32     //------------------------------------------------------------------
33     // Static Functions
34     //------------------------------------------------------------------
35     static void
36     Initialize();
37 
38     static void
39     Terminate();
40 
41     static const char *
42     GetPluginNameStatic();
43 
44     static const char *
45     GetPluginDescriptionStatic();
46 
47     static lldb_private::DynamicLoader *
48     CreateInstance (lldb_private::Process *process, bool force);
49 
50     static void
51     DebuggerInitialize (lldb_private::Debugger &debugger);
52 
53     DynamicLoaderDarwinKernel (lldb_private::Process *process, lldb::addr_t kernel_addr);
54 
55     virtual
56     ~DynamicLoaderDarwinKernel ();
57 
58     //------------------------------------------------------------------
59     /// Called after attaching a process.
60     ///
61     /// Allow DynamicLoader plug-ins to execute some code after
62     /// attaching to a process.
63     //------------------------------------------------------------------
64     virtual void
65     DidAttach ();
66 
67     virtual void
68     DidLaunch ();
69 
70     virtual lldb::ThreadPlanSP
71     GetStepThroughTrampolinePlan (lldb_private::Thread &thread,
72                                   bool stop_others);
73 
74     virtual lldb_private::Error
75     CanLoadImage ();
76 
77     //------------------------------------------------------------------
78     // PluginInterface protocol
79     //------------------------------------------------------------------
80     virtual const char *
81     GetPluginName();
82 
83     virtual const char *
84     GetShortPluginName();
85 
86     virtual uint32_t
87     GetPluginVersion();
88 
89 protected:
90     void
91     PrivateInitialize (lldb_private::Process *process);
92 
93     void
94     PrivateProcessStateChanged (lldb_private::Process *process,
95                                 lldb::StateType state);
96 
97     void
98     UpdateIfNeeded();
99 
100     void
101     LoadKernelModuleIfNeeded ();
102 
103     void
104     Clear (bool clear_process);
105 
106     void
107     PutToLog (lldb_private::Log *log) const;
108 
109     static bool
110     BreakpointHitCallback (void *baton,
111                            lldb_private::StoppointCallbackContext *context,
112                            lldb::user_id_t break_id,
113                            lldb::user_id_t break_loc_id);
114 
115     bool
116     BreakpointHit (lldb_private::StoppointCallbackContext *context,
117                    lldb::user_id_t break_id,
118                    lldb::user_id_t break_loc_id);
119     uint32_t
120     GetAddrByteSize()
121     {
122         return m_kernel.GetAddressByteSize();
123     }
124 
125     static lldb::ByteOrder
126     GetByteOrderFromMagic (uint32_t magic);
127 
128     enum
129     {
130         KERNEL_MODULE_MAX_NAME = 64u,
131         // Versions less than 2 didn't have an entry size,
132         // they had a 64 bit name, 16 byte UUID, 8 byte addr,
133         // 8 byte size, 8 byte version, 4 byte load tag, and
134         // 4 byte flags
135         KERNEL_MODULE_ENTRY_SIZE_VERSION_1 = 64u + 16u + 8u + 8u + 8u + 4u + 4u
136     };
137 
138     // class KextImageInfo represents a single kext or kernel binary image.
139     // The class was designed to hold the information from the OSKextLoadedKextSummary
140     // structure (in libkern/libkern/OSKextLibPrivate.h from xnu).  The kernel maintains
141     // a list of loded kexts in memory (the OSKextLoadedKextSummaryHeader structure,
142     // which points to an array of OSKextLoadedKextSummary's).
143     //
144     // A KextImageInfos may have -
145     //
146     // 1. The load address, name, UUID, and size of a kext/kernel binary in memory
147     //    (read straight out of the kernel's list-of-kexts loaded)
148     // 2. A ModuleSP based on a MemoryModule read out of the kernel's memory
149     //    (very unlikely to have any symbolic information)
150     // 3. A ModuleSP for an on-disk copy of the kext binary, possibly with debug info
151     //    or a dSYM
152     //
153     // For performance reasons, the developer may prefer that lldb not load the kexts out
154     // of memory at the start of a kernel session.  But we should build up / maintain a
155     // list of kexts that the kernel has told us about so we can relocate a kext module
156     // later if the user explicitly adds it to the target.
157 
158     class KextImageInfo
159     {
160     public:
161         KextImageInfo () :
162             m_name (),
163             m_module_sp (),
164             m_memory_module_sp (),
165             m_load_process_stop_id (UINT32_MAX),
166             m_uuid (),
167             m_load_address (LLDB_INVALID_ADDRESS),
168             m_size (0),
169             m_kernel_image (false)
170         { }
171 
172         void
173         Clear ()
174         {
175             m_load_address = LLDB_INVALID_ADDRESS;
176             m_size = 0;
177             m_name.clear ();
178             m_uuid.Clear();
179             m_module_sp.reset();
180             m_memory_module_sp.reset();
181             m_load_process_stop_id = UINT32_MAX;
182         }
183 
184         bool
185         LoadImageAtFileAddress (lldb_private::Process *process);
186 
187         bool
188         LoadImageUsingMemoryModule (lldb_private::Process *process);
189 
190         bool
191         IsLoaded ()
192         {
193             return m_load_process_stop_id != UINT32_MAX;
194         }
195 
196         void
197         SetLoadAddress (lldb::addr_t load_addr);     // Address of the Mach-O header for this binary
198 
199         lldb::addr_t
200         GetLoadAddress () const;                     // Address of the Mach-O header for this binary
201 
202         lldb_private::UUID
203         GetUUID () const;
204 
205         void
206         SetUUID (const lldb_private::UUID &uuid);
207 
208         void
209         SetName (const char *);
210 
211         std::string
212         GetName () const;
213 
214         void
215         SetModule (lldb::ModuleSP module);
216 
217         lldb::ModuleSP
218         GetModule ();
219 
220         // try to fill in m_memory_module_sp from memory based on the m_load_address
221         bool
222         ReadMemoryModule (lldb_private::Process *process);
223 
224         bool
225         IsKernel () const;            // true if this is the mach_kernel; false if this is a kext
226 
227         void
228         SetIsKernel (bool is_kernel);
229 
230         uint64_t
231         GetSize () const;
232 
233         void
234         SetSize (uint64_t size);
235 
236         uint32_t
237         GetProcessStopId () const;    // the stop-id when this binary was first noticed
238 
239         void
240         SetProcessStopId (uint32_t stop_id);
241 
242         bool
243         operator== (const KextImageInfo &rhs);
244 
245         uint32_t
246         GetAddressByteSize ();        // as determined by Mach-O header
247 
248         lldb::ByteOrder
249         GetByteOrder();               // as determined by Mach-O header
250 
251         lldb_private::ArchSpec
252         GetArchitecture () const;     // as determined by Mach-O header
253 
254         void
255         PutToLog (lldb_private::Log *log) const;
256 
257         typedef std::vector<KextImageInfo> collection;
258         typedef collection::iterator iterator;
259         typedef collection::const_iterator const_iterator;
260 
261     private:
262         std::string              m_name;
263         lldb::ModuleSP           m_module_sp;
264         lldb::ModuleSP           m_memory_module_sp;
265         uint32_t                 m_load_process_stop_id; // the stop-id when this module was added to the Target
266         lldb_private::UUID       m_uuid;                 // UUID for this dylib if it has one, else all zeros
267         lldb::addr_t             m_load_address;
268         uint64_t                 m_size;
269         bool                     m_kernel_image;         // true if this is the kernel, false if this is a kext
270 
271     };
272 
273     struct OSKextLoadedKextSummaryHeader
274     {
275         uint32_t version;
276         uint32_t entry_size;
277         uint32_t entry_count;
278         lldb::addr_t image_infos_addr;
279 
280         OSKextLoadedKextSummaryHeader() :
281             version (0),
282             entry_size (0),
283             entry_count (0),
284             image_infos_addr (LLDB_INVALID_ADDRESS)
285         {
286         }
287 
288         uint32_t
289         GetSize()
290         {
291             switch (version)
292             {
293                 case 0: return 0;   // Can't know the size without a valid version
294                 case 1: return 8;   // Version 1 only had a version + entry_count
295                 default: break;
296             }
297             // Version 2 and above has version, entry_size, entry_count, and reserved
298             return 16;
299         }
300 
301         void
302         Clear()
303         {
304             version = 0;
305             entry_size = 0;
306             entry_count = 0;
307             image_infos_addr = LLDB_INVALID_ADDRESS;
308         }
309 
310         bool
311         IsValid() const
312         {
313             return version >= 1 || version <= 2;
314         }
315     };
316 
317     void
318     RegisterNotificationCallbacks();
319 
320     void
321     UnregisterNotificationCallbacks();
322 
323     void
324     SetNotificationBreakpointIfNeeded ();
325 
326     bool
327     ReadAllKextSummaries ();
328 
329     bool
330     ReadKextSummaryHeader ();
331 
332     bool
333     ParseKextSummaries (const lldb_private::Address &kext_summary_addr,
334                         uint32_t count);
335 
336     void
337     UpdateImageInfosHeaderAndLoadCommands(KextImageInfo::collection &image_infos,
338                                           uint32_t infos_count,
339                                           bool update_executable);
340 
341     uint32_t
342     ReadKextSummaries (const lldb_private::Address &kext_summary_addr,
343                        uint32_t image_infos_count,
344                        KextImageInfo::collection &image_infos);
345 
346     static lldb::addr_t
347     SearchForKernelAtSameLoadAddr (lldb_private::Process *process);
348 
349     static lldb::addr_t
350     SearchForKernelWithDebugHints (lldb_private::Process *process);
351 
352     static lldb::addr_t
353     SearchForKernelNearPC (lldb_private::Process *process);
354 
355     static lldb::addr_t
356     SearchForKernelViaExhaustiveSearch (lldb_private::Process *process);
357 
358     static lldb_private::UUID
359     CheckForKernelImageAtAddress (lldb::addr_t addr, lldb_private::Process *process);
360 
361     lldb::addr_t  m_kernel_load_address;
362     KextImageInfo m_kernel;                 // Info about the current kernel image being used
363 
364     lldb_private::Address          m_kext_summary_header_ptr_addr;
365     lldb_private::Address          m_kext_summary_header_addr;
366     OSKextLoadedKextSummaryHeader  m_kext_summary_header;
367     KextImageInfo::collection      m_known_kexts;
368     mutable lldb_private::Mutex    m_mutex;
369     lldb::user_id_t                m_break_id;
370 
371 private:
372     DISALLOW_COPY_AND_ASSIGN (DynamicLoaderDarwinKernel);
373 };
374 
375 #endif  // liblldb_DynamicLoaderDarwinKernel_h_
376