1944b828aSGreg Clayton //===-- DynamicLoaderDarwinKernel.cpp -----------------------------*- C++ -*-===//
2d4bfbc9aSGreg Clayton //
3d4bfbc9aSGreg Clayton //                     The LLVM Compiler Infrastructure
4d4bfbc9aSGreg Clayton //
5d4bfbc9aSGreg Clayton // This file is distributed under the University of Illinois Open Source
6d4bfbc9aSGreg Clayton // License. See LICENSE.TXT for details.
7d4bfbc9aSGreg Clayton //
8d4bfbc9aSGreg Clayton //===----------------------------------------------------------------------===//
9d4bfbc9aSGreg Clayton 
1093a64300SDaniel Malea #include "lldb/lldb-python.h"
1193a64300SDaniel Malea 
12d4bfbc9aSGreg Clayton #include "lldb/Breakpoint/StoppointCallbackContext.h"
13d4bfbc9aSGreg Clayton #include "lldb/Core/DataBuffer.h"
14d4bfbc9aSGreg Clayton #include "lldb/Core/DataBufferHeap.h"
15d4bfbc9aSGreg Clayton #include "lldb/Core/Debugger.h"
16d4bfbc9aSGreg Clayton #include "lldb/Core/Log.h"
17d4bfbc9aSGreg Clayton #include "lldb/Core/Module.h"
181f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h"
19d4bfbc9aSGreg Clayton #include "lldb/Core/PluginManager.h"
201f746071SGreg Clayton #include "lldb/Core/Section.h"
21d4bfbc9aSGreg Clayton #include "lldb/Core/State.h"
2268b3607fSJason Molenda #include "lldb/Host/Symbols.h"
23d4bfbc9aSGreg Clayton #include "lldb/Symbol/ObjectFile.h"
24d4bfbc9aSGreg Clayton #include "lldb/Target/RegisterContext.h"
2568b3607fSJason Molenda #include "lldb/Target/StackFrame.h"
26d4bfbc9aSGreg Clayton #include "lldb/Target/Target.h"
27d4bfbc9aSGreg Clayton #include "lldb/Target/Thread.h"
28d4bfbc9aSGreg Clayton #include "lldb/Target/ThreadPlanRunToAddress.h"
2968b3607fSJason Molenda 
30d4bfbc9aSGreg Clayton 
31944b828aSGreg Clayton #include "DynamicLoaderDarwinKernel.h"
32d4bfbc9aSGreg Clayton 
33d4bfbc9aSGreg Clayton //#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
34d4bfbc9aSGreg Clayton #ifdef ENABLE_DEBUG_PRINTF
35d4bfbc9aSGreg Clayton #include <stdio.h>
36d4bfbc9aSGreg Clayton #define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
37d4bfbc9aSGreg Clayton #else
38d4bfbc9aSGreg Clayton #define DEBUG_PRINTF(fmt, ...)
39d4bfbc9aSGreg Clayton #endif
40d4bfbc9aSGreg Clayton 
41d4bfbc9aSGreg Clayton using namespace lldb;
42d4bfbc9aSGreg Clayton using namespace lldb_private;
43d4bfbc9aSGreg Clayton 
446ba6d3d1SJason Molenda // Progressively greater amounts of scanning we will allow
456ba6d3d1SJason Molenda // For some targets very early in startup, we can't do any random reads of memory or we can crash the device
466ba6d3d1SJason Molenda // so a setting is needed that can completely disable the KASLR scans.
476ba6d3d1SJason Molenda 
486ba6d3d1SJason Molenda enum KASLRScanType
496ba6d3d1SJason Molenda {
506ba6d3d1SJason Molenda     eKASLRScanNone = 0,         // No reading into the inferior at all
516ba6d3d1SJason Molenda     eKASLRScanLowgloAddresses,  // Check one word of memory for a possible kernel addr, then see if a kernel is there
5263969221SJason Molenda     eKASLRScanNearPC,           // Scan backwards from the current $pc looking for kernel; checking at 96 locations total
536ba6d3d1SJason Molenda     eKASLRScanExhaustiveScan    // Scan through the entire possible kernel address range looking for a kernel
546ba6d3d1SJason Molenda };
556ba6d3d1SJason Molenda 
566ba6d3d1SJason Molenda OptionEnumValueElement
576ba6d3d1SJason Molenda g_kaslr_kernel_scan_enum_values[] =
586ba6d3d1SJason Molenda {
596ba6d3d1SJason Molenda     { eKASLRScanNone,            "none",            "Do not read memory looking for a Darwin kernel when attaching." },
606ba6d3d1SJason Molenda     { eKASLRScanLowgloAddresses, "basic",           "Check for the Darwin kernel's load addr in the lowglo page (boot-args=debug) only." },
616ba6d3d1SJason Molenda     { eKASLRScanNearPC,          "fast-scan",       "Scan near the pc value on attach to find the Darwin kernel's load address."},
626ba6d3d1SJason Molenda     { eKASLRScanExhaustiveScan,  "exhaustive-scan", "Scan through the entire potential address range of Darwin kernel (only on 32-bit targets)."},
636ba6d3d1SJason Molenda     { 0, NULL, NULL }
646ba6d3d1SJason Molenda };
656ba6d3d1SJason Molenda 
66e8cd0c98SGreg Clayton static PropertyDefinition
67e8cd0c98SGreg Clayton g_properties[] =
68e8cd0c98SGreg Clayton {
6966763eedSGreg Clayton     { "load-kexts" , OptionValue::eTypeBoolean, true, true, NULL, NULL, "Automatically loads kext images when attaching to a kernel." },
706ba6d3d1SJason Molenda     { "scan-type",   OptionValue::eTypeEnum,    true, eKASLRScanNearPC, NULL, g_kaslr_kernel_scan_enum_values, "Control how many reads lldb will make while searching for a Darwin kernel on attach." },
71e8cd0c98SGreg Clayton     {  NULL        , OptionValue::eTypeInvalid, false, 0  , NULL, NULL, NULL  }
72e8cd0c98SGreg Clayton };
73e8cd0c98SGreg Clayton 
74e8cd0c98SGreg Clayton enum {
756ba6d3d1SJason Molenda     ePropertyLoadKexts,
766ba6d3d1SJason Molenda     ePropertyScanType
77e8cd0c98SGreg Clayton };
78e8cd0c98SGreg Clayton 
79e8cd0c98SGreg Clayton class DynamicLoaderDarwinKernelProperties : public Properties
80e8cd0c98SGreg Clayton {
81e8cd0c98SGreg Clayton public:
82e8cd0c98SGreg Clayton 
83e8cd0c98SGreg Clayton     static ConstString &
84e8cd0c98SGreg Clayton     GetSettingName ()
85e8cd0c98SGreg Clayton     {
86468ea4ebSGreg Clayton         static ConstString g_setting_name("darwin-kernel");
87e8cd0c98SGreg Clayton         return g_setting_name;
88e8cd0c98SGreg Clayton     }
89e8cd0c98SGreg Clayton 
90e8cd0c98SGreg Clayton     DynamicLoaderDarwinKernelProperties() :
91e8cd0c98SGreg Clayton         Properties ()
92e8cd0c98SGreg Clayton     {
93e8cd0c98SGreg Clayton         m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
94e8cd0c98SGreg Clayton         m_collection_sp->Initialize(g_properties);
95e8cd0c98SGreg Clayton     }
96e8cd0c98SGreg Clayton 
97e8cd0c98SGreg Clayton     virtual
98e8cd0c98SGreg Clayton     ~DynamicLoaderDarwinKernelProperties()
99e8cd0c98SGreg Clayton     {
100e8cd0c98SGreg Clayton     }
101e8cd0c98SGreg Clayton 
102e8cd0c98SGreg Clayton     bool
10366763eedSGreg Clayton     GetLoadKexts() const
104e8cd0c98SGreg Clayton     {
10566763eedSGreg Clayton         const uint32_t idx = ePropertyLoadKexts;
106e8cd0c98SGreg Clayton         return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
107e8cd0c98SGreg Clayton     }
108e8cd0c98SGreg Clayton 
1096ba6d3d1SJason Molenda     KASLRScanType
1106ba6d3d1SJason Molenda     GetScanType() const
1116ba6d3d1SJason Molenda     {
1126ba6d3d1SJason Molenda         const uint32_t idx = ePropertyScanType;
11363969221SJason Molenda         return (KASLRScanType) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
1146ba6d3d1SJason Molenda     }
1156ba6d3d1SJason Molenda 
1166ba6d3d1SJason Molenda 
117e8cd0c98SGreg Clayton };
118e8cd0c98SGreg Clayton 
1197b0992d9SGreg Clayton typedef std::shared_ptr<DynamicLoaderDarwinKernelProperties> DynamicLoaderDarwinKernelPropertiesSP;
120e8cd0c98SGreg Clayton 
121e8cd0c98SGreg Clayton static const DynamicLoaderDarwinKernelPropertiesSP &
122e8cd0c98SGreg Clayton GetGlobalProperties()
123e8cd0c98SGreg Clayton {
124e8cd0c98SGreg Clayton     static DynamicLoaderDarwinKernelPropertiesSP g_settings_sp;
125e8cd0c98SGreg Clayton     if (!g_settings_sp)
126e8cd0c98SGreg Clayton         g_settings_sp.reset (new DynamicLoaderDarwinKernelProperties ());
127e8cd0c98SGreg Clayton     return g_settings_sp;
128e8cd0c98SGreg Clayton }
129e8cd0c98SGreg Clayton 
130d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
131d4bfbc9aSGreg Clayton // Create an instance of this class. This function is filled into
132d4bfbc9aSGreg Clayton // the plugin info class that gets handed out by the plugin factory and
133d4bfbc9aSGreg Clayton // allows the lldb to instantiate an instance of this class.
134d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
135d4bfbc9aSGreg Clayton DynamicLoader *
136944b828aSGreg Clayton DynamicLoaderDarwinKernel::CreateInstance (Process* process, bool force)
137d4bfbc9aSGreg Clayton {
1386ba6d3d1SJason Molenda     if (!force)
139d4bfbc9aSGreg Clayton     {
1406ba6d3d1SJason Molenda         // If the user provided an executable binary and it is not a kernel,
1416ba6d3d1SJason Molenda         // this plugin should not create an instance.
142d4bfbc9aSGreg Clayton         Module* exe_module = process->GetTarget().GetExecutableModulePointer();
143d4bfbc9aSGreg Clayton         if (exe_module)
144d4bfbc9aSGreg Clayton         {
145d4bfbc9aSGreg Clayton             ObjectFile *object_file = exe_module->GetObjectFile();
146d4bfbc9aSGreg Clayton             if (object_file)
147d4bfbc9aSGreg Clayton             {
1486ba6d3d1SJason Molenda                 if (object_file->GetStrata() != ObjectFile::eStrataKernel)
1496ba6d3d1SJason Molenda                 {
1506ba6d3d1SJason Molenda                     return NULL;
1516ba6d3d1SJason Molenda                 }
152d4bfbc9aSGreg Clayton             }
153d4bfbc9aSGreg Clayton         }
154d4bfbc9aSGreg Clayton 
1556ba6d3d1SJason Molenda         // If the target's architecture does not look like an Apple environment,
1566ba6d3d1SJason Molenda         // this plugin should not create an instance.
157d4bfbc9aSGreg Clayton         const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
15870512317SGreg Clayton         switch (triple_ref.getOS())
15970512317SGreg Clayton         {
16070512317SGreg Clayton             case llvm::Triple::Darwin:
16170512317SGreg Clayton             case llvm::Triple::MacOSX:
16270512317SGreg Clayton             case llvm::Triple::IOS:
1636ba6d3d1SJason Molenda                 if (triple_ref.getVendor() != llvm::Triple::Apple)
1646ba6d3d1SJason Molenda                 {
1656ba6d3d1SJason Molenda                    return NULL;
1666ba6d3d1SJason Molenda                 }
1676ba6d3d1SJason Molenda                 break;
1686ba6d3d1SJason Molenda             // If we have triple like armv7-unknown-unknown, we should try looking for a Darwin kernel.
1696ba6d3d1SJason Molenda             case llvm::Triple::UnknownOS:
17070512317SGreg Clayton                 break;
17170512317SGreg Clayton             default:
1726ba6d3d1SJason Molenda                 return NULL;
17370512317SGreg Clayton                 break;
17470512317SGreg Clayton         }
175d4bfbc9aSGreg Clayton     }
1766ba6d3d1SJason Molenda 
1776ba6d3d1SJason Molenda     // At this point if there is an ExecutableModule, it is a kernel and the Target is some variant of an Apple system.
1786ba6d3d1SJason Molenda     // If the Process hasn't provided the kernel load address, we need to look around in memory to find it.
1796ba6d3d1SJason Molenda 
180503d0181SJason Molenda     addr_t kernel_load_address = SearchForDarwinKernel (process);
181503d0181SJason Molenda     if (kernel_load_address != LLDB_INVALID_ADDRESS)
182503d0181SJason Molenda     {
183503d0181SJason Molenda         process->SetCanJIT(false);
184503d0181SJason Molenda         return new DynamicLoaderDarwinKernel (process, kernel_load_address);
185503d0181SJason Molenda     }
186503d0181SJason Molenda     return NULL;
187503d0181SJason Molenda }
188503d0181SJason Molenda 
189503d0181SJason Molenda lldb::addr_t
190503d0181SJason Molenda DynamicLoaderDarwinKernel::SearchForDarwinKernel (Process *process)
191503d0181SJason Molenda {
1926ba6d3d1SJason Molenda     addr_t kernel_load_address = process->GetImageInfoAddress();
1936ba6d3d1SJason Molenda     if (kernel_load_address == LLDB_INVALID_ADDRESS)
1946ba6d3d1SJason Molenda     {
1956ba6d3d1SJason Molenda         kernel_load_address = SearchForKernelAtSameLoadAddr (process);
1966ba6d3d1SJason Molenda         if (kernel_load_address == LLDB_INVALID_ADDRESS)
1976ba6d3d1SJason Molenda         {
1986ba6d3d1SJason Molenda             kernel_load_address = SearchForKernelWithDebugHints (process);
1996ba6d3d1SJason Molenda             if (kernel_load_address == LLDB_INVALID_ADDRESS)
2006ba6d3d1SJason Molenda             {
2016ba6d3d1SJason Molenda                 kernel_load_address = SearchForKernelNearPC (process);
2026ba6d3d1SJason Molenda                 if (kernel_load_address == LLDB_INVALID_ADDRESS)
2036ba6d3d1SJason Molenda                 {
2046ba6d3d1SJason Molenda                     kernel_load_address = SearchForKernelViaExhaustiveSearch (process);
2056ba6d3d1SJason Molenda                 }
2066ba6d3d1SJason Molenda             }
2076ba6d3d1SJason Molenda         }
208d4bfbc9aSGreg Clayton     }
209503d0181SJason Molenda     return kernel_load_address;
210d4bfbc9aSGreg Clayton }
211d4bfbc9aSGreg Clayton 
212d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
2136ba6d3d1SJason Molenda // Check if the kernel binary is loaded in memory without a slide.
2146ba6d3d1SJason Molenda // First verify that the ExecutableModule is a kernel before we proceed.
2156ba6d3d1SJason Molenda // Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
2166ba6d3d1SJason Molenda //----------------------------------------------------------------------
2176ba6d3d1SJason Molenda lldb::addr_t
2186ba6d3d1SJason Molenda DynamicLoaderDarwinKernel::SearchForKernelAtSameLoadAddr (Process *process)
2196ba6d3d1SJason Molenda {
2206ba6d3d1SJason Molenda     Module *exe_module = process->GetTarget().GetExecutableModulePointer();
2216ba6d3d1SJason Molenda     if (exe_module == NULL)
2226ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
2236ba6d3d1SJason Molenda 
2246ba6d3d1SJason Molenda     ObjectFile *exe_objfile = exe_module->GetObjectFile();
2256ba6d3d1SJason Molenda     if (exe_objfile == NULL)
2266ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
2276ba6d3d1SJason Molenda 
2286ba6d3d1SJason Molenda     if (exe_objfile->GetType() != ObjectFile::eTypeExecutable || exe_objfile->GetStrata() != ObjectFile::eStrataKernel)
2296ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
2306ba6d3d1SJason Molenda 
2316ba6d3d1SJason Molenda     if (!exe_objfile->GetHeaderAddress().IsValid())
2326ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
2336ba6d3d1SJason Molenda 
2346ba6d3d1SJason Molenda     if (CheckForKernelImageAtAddress (exe_objfile->GetHeaderAddress().GetFileAddress(), process) == exe_module->GetUUID())
2356ba6d3d1SJason Molenda         return exe_objfile->GetHeaderAddress().GetFileAddress();
2366ba6d3d1SJason Molenda 
2376ba6d3d1SJason Molenda     return LLDB_INVALID_ADDRESS;
2386ba6d3d1SJason Molenda }
2396ba6d3d1SJason Molenda 
2406ba6d3d1SJason Molenda //----------------------------------------------------------------------
2416ba6d3d1SJason Molenda // If the debug flag is included in the boot-args nvram setting, the kernel's load address
2426ba6d3d1SJason Molenda // will be noted in the lowglo page at a fixed address
2436ba6d3d1SJason Molenda // Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
2446ba6d3d1SJason Molenda //----------------------------------------------------------------------
2456ba6d3d1SJason Molenda lldb::addr_t
2466ba6d3d1SJason Molenda DynamicLoaderDarwinKernel::SearchForKernelWithDebugHints (Process *process)
2476ba6d3d1SJason Molenda {
2486ba6d3d1SJason Molenda     if (GetGlobalProperties()->GetScanType() == eKASLRScanNone)
2496ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
2506ba6d3d1SJason Molenda 
2516ba6d3d1SJason Molenda     Error read_err;
2526ba6d3d1SJason Molenda     addr_t addr = LLDB_INVALID_ADDRESS;
2536ba6d3d1SJason Molenda     if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
2546ba6d3d1SJason Molenda     {
2556ba6d3d1SJason Molenda         addr = process->ReadUnsignedIntegerFromMemory (0xffffff8000002010ULL, 8, LLDB_INVALID_ADDRESS, read_err);
2566ba6d3d1SJason Molenda     }
2576ba6d3d1SJason Molenda     else
2586ba6d3d1SJason Molenda     {
2596ba6d3d1SJason Molenda         addr = process->ReadUnsignedIntegerFromMemory (0xffff0110, 4, LLDB_INVALID_ADDRESS, read_err);
2606ba6d3d1SJason Molenda     }
2616ba6d3d1SJason Molenda 
2626ba6d3d1SJason Molenda     if (addr == 0)
2636ba6d3d1SJason Molenda         addr = LLDB_INVALID_ADDRESS;
2646ba6d3d1SJason Molenda 
2656ba6d3d1SJason Molenda     if (addr != LLDB_INVALID_ADDRESS)
2666ba6d3d1SJason Molenda     {
2676ba6d3d1SJason Molenda         if (CheckForKernelImageAtAddress (addr, process).IsValid())
2686ba6d3d1SJason Molenda             return addr;
2696ba6d3d1SJason Molenda     }
2706ba6d3d1SJason Molenda 
2716ba6d3d1SJason Molenda     return LLDB_INVALID_ADDRESS;
2726ba6d3d1SJason Molenda }
2736ba6d3d1SJason Molenda 
2746ba6d3d1SJason Molenda //----------------------------------------------------------------------
2756ba6d3d1SJason Molenda // If the kernel is currently executing when lldb attaches, and we don't have
2766ba6d3d1SJason Molenda // a better way of finding the kernel's load address, try searching backwards
2776ba6d3d1SJason Molenda // from the current pc value looking for the kernel's Mach header in memory.
2786ba6d3d1SJason Molenda // Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
2796ba6d3d1SJason Molenda //----------------------------------------------------------------------
2806ba6d3d1SJason Molenda lldb::addr_t
2816ba6d3d1SJason Molenda DynamicLoaderDarwinKernel::SearchForKernelNearPC (Process *process)
2826ba6d3d1SJason Molenda {
2836ba6d3d1SJason Molenda     if (GetGlobalProperties()->GetScanType() == eKASLRScanNone
2846ba6d3d1SJason Molenda         || GetGlobalProperties()->GetScanType() == eKASLRScanLowgloAddresses)
2856ba6d3d1SJason Molenda     {
2866ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
2876ba6d3d1SJason Molenda     }
2886ba6d3d1SJason Molenda 
2896ba6d3d1SJason Molenda     ThreadSP thread = process->GetThreadList().GetSelectedThread ();
2906ba6d3d1SJason Molenda     if (thread.get() == NULL)
2916ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
2926ba6d3d1SJason Molenda     addr_t pc = thread->GetRegisterContext ()->GetPC(LLDB_INVALID_ADDRESS);
2936ba6d3d1SJason Molenda 
2946ba6d3d1SJason Molenda     if (pc == LLDB_INVALID_ADDRESS)
2956ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
2966ba6d3d1SJason Molenda 
29744edbf13SJason Molenda     addr_t kernel_range_low;
2986ba6d3d1SJason Molenda     if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
2996ba6d3d1SJason Molenda     {
3006ba6d3d1SJason Molenda         kernel_range_low = 1ULL << 63;
3016ba6d3d1SJason Molenda     }
3026ba6d3d1SJason Molenda     else
3036ba6d3d1SJason Molenda     {
3046ba6d3d1SJason Molenda         kernel_range_low = 1ULL << 31;
3056ba6d3d1SJason Molenda     }
3066ba6d3d1SJason Molenda 
3076ba6d3d1SJason Molenda     // Outside the normal kernel address range, this is probably userland code running right now
3086ba6d3d1SJason Molenda     if (pc < kernel_range_low)
30944edbf13SJason Molenda         return LLDB_INVALID_ADDRESS;
3106ba6d3d1SJason Molenda 
3116ba6d3d1SJason Molenda     // The kernel will load at at one megabyte boundary (0x100000), or at that boundary plus
3126ba6d3d1SJason Molenda     // an offset of one page (0x1000) or two, depending on the device.
3136ba6d3d1SJason Molenda 
3146ba6d3d1SJason Molenda     // Round the current pc down to the nearest one megabyte boundary - the place where we will start searching.
3156ba6d3d1SJason Molenda     addr_t addr = pc & ~0xfffff;
3166ba6d3d1SJason Molenda 
3176ba6d3d1SJason Molenda     int i = 0;
3186ba6d3d1SJason Molenda     while (i < 32 && pc >= kernel_range_low)
3196ba6d3d1SJason Molenda     {
3206ba6d3d1SJason Molenda         if (CheckForKernelImageAtAddress (addr, process).IsValid())
3216ba6d3d1SJason Molenda             return addr;
3226ba6d3d1SJason Molenda         if (CheckForKernelImageAtAddress (addr + 0x1000, process).IsValid())
3236ba6d3d1SJason Molenda             return addr + 0x1000;
3246ba6d3d1SJason Molenda         if (CheckForKernelImageAtAddress (addr + 0x2000, process).IsValid())
3256ba6d3d1SJason Molenda             return addr + 0x2000;
3266ba6d3d1SJason Molenda         i++;
3276ba6d3d1SJason Molenda         addr -= 0x100000;
3286ba6d3d1SJason Molenda     }
3296ba6d3d1SJason Molenda 
3306ba6d3d1SJason Molenda     return LLDB_INVALID_ADDRESS;
3316ba6d3d1SJason Molenda }
3326ba6d3d1SJason Molenda 
3336ba6d3d1SJason Molenda //----------------------------------------------------------------------
3346ba6d3d1SJason Molenda // Scan through the valid address range for a kernel binary.
3356ba6d3d1SJason Molenda // This is uselessly slow in 64-bit environments so we don't even try it.
3366ba6d3d1SJason Molenda // This scan is not enabled by default even for 32-bit targets.
3376ba6d3d1SJason Molenda // Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
3386ba6d3d1SJason Molenda //----------------------------------------------------------------------
3396ba6d3d1SJason Molenda lldb::addr_t
3406ba6d3d1SJason Molenda DynamicLoaderDarwinKernel::SearchForKernelViaExhaustiveSearch (Process *process)
3416ba6d3d1SJason Molenda {
3426ba6d3d1SJason Molenda     if (GetGlobalProperties()->GetScanType() != eKASLRScanExhaustiveScan)
3436ba6d3d1SJason Molenda     {
3446ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
3456ba6d3d1SJason Molenda     }
3466ba6d3d1SJason Molenda 
3476ba6d3d1SJason Molenda     addr_t kernel_range_low, kernel_range_high;
3486ba6d3d1SJason Molenda     if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
3496ba6d3d1SJason Molenda     {
3506ba6d3d1SJason Molenda         kernel_range_low = 1ULL << 63;
3516ba6d3d1SJason Molenda         kernel_range_high = UINT64_MAX;
3526ba6d3d1SJason Molenda     }
3536ba6d3d1SJason Molenda     else
3546ba6d3d1SJason Molenda     {
3556ba6d3d1SJason Molenda         kernel_range_low = 1ULL << 31;
3566ba6d3d1SJason Molenda         kernel_range_high = UINT32_MAX;
3576ba6d3d1SJason Molenda     }
3586ba6d3d1SJason Molenda 
3596ba6d3d1SJason Molenda     // Stepping through memory at one-megabyte resolution looking for a kernel
3606ba6d3d1SJason Molenda     // rarely works (fast enough) with a 64-bit address space -- for now, let's
3616ba6d3d1SJason Molenda     // not even bother.  We may be attaching to something which *isn't* a kernel
3626ba6d3d1SJason Molenda     // and we don't want to spin for minutes on-end looking for a kernel.
3636ba6d3d1SJason Molenda     if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
3646ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
3656ba6d3d1SJason Molenda 
3666ba6d3d1SJason Molenda     addr_t addr = kernel_range_low;
3676ba6d3d1SJason Molenda 
3686ba6d3d1SJason Molenda     while (addr >= kernel_range_low && addr < kernel_range_high)
3696ba6d3d1SJason Molenda     {
3706ba6d3d1SJason Molenda         if (CheckForKernelImageAtAddress (addr, process).IsValid())
3716ba6d3d1SJason Molenda             return addr;
3726ba6d3d1SJason Molenda         if (CheckForKernelImageAtAddress (addr + 0x1000, process).IsValid())
3736ba6d3d1SJason Molenda             return addr + 0x1000;
3746ba6d3d1SJason Molenda         if (CheckForKernelImageAtAddress (addr + 0x2000, process).IsValid())
3756ba6d3d1SJason Molenda             return addr + 0x2000;
3766ba6d3d1SJason Molenda         addr += 0x100000;
3776ba6d3d1SJason Molenda     }
3786ba6d3d1SJason Molenda     return LLDB_INVALID_ADDRESS;
3796ba6d3d1SJason Molenda }
3806ba6d3d1SJason Molenda 
3816ba6d3d1SJason Molenda //----------------------------------------------------------------------
3826ba6d3d1SJason Molenda // Given an address in memory, look to see if there is a kernel image at that
3836ba6d3d1SJason Molenda // address.
3846ba6d3d1SJason Molenda // Returns a UUID; if a kernel was not found at that address, UUID.IsValid() will be false.
3856ba6d3d1SJason Molenda //----------------------------------------------------------------------
3866ba6d3d1SJason Molenda lldb_private::UUID
3876ba6d3d1SJason Molenda DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress (lldb::addr_t addr, Process *process)
3886ba6d3d1SJason Molenda {
3896ba6d3d1SJason Molenda     if (addr == LLDB_INVALID_ADDRESS)
3906ba6d3d1SJason Molenda         return UUID();
3916ba6d3d1SJason Molenda 
3926ba6d3d1SJason Molenda     // First try a quick test -- read the first 4 bytes and see if there is a valid Mach-O magic field there
3936ba6d3d1SJason Molenda     // (the first field of the mach_header/mach_header_64 struct).
3946ba6d3d1SJason Molenda 
3956ba6d3d1SJason Molenda     Error read_error;
3966ba6d3d1SJason Molenda     uint64_t result = process->ReadUnsignedIntegerFromMemory (addr, 4, LLDB_INVALID_ADDRESS, read_error);
3976ba6d3d1SJason Molenda     if (result != llvm::MachO::HeaderMagic64
3986ba6d3d1SJason Molenda         && result != llvm::MachO::HeaderMagic32
3996ba6d3d1SJason Molenda         && result != llvm::MachO::HeaderMagic32Swapped
4006ba6d3d1SJason Molenda         && result != llvm::MachO::HeaderMagic64Swapped)
4016ba6d3d1SJason Molenda     {
4026ba6d3d1SJason Molenda         return UUID();
4036ba6d3d1SJason Molenda     }
4046ba6d3d1SJason Molenda 
4056ba6d3d1SJason Molenda     // Read the mach header and see whether it looks like a kernel
4066ba6d3d1SJason Molenda     llvm::MachO::mach_header header;
4076ba6d3d1SJason Molenda     if (process->DoReadMemory (addr, &header, sizeof(header), read_error) != sizeof(header))
4086ba6d3d1SJason Molenda         return UUID();
4096ba6d3d1SJason Molenda 
4106ba6d3d1SJason Molenda     if (header.magic == llvm::MachO::HeaderMagic32Swapped ||
4116ba6d3d1SJason Molenda         header.magic == llvm::MachO::HeaderMagic64Swapped)
4126ba6d3d1SJason Molenda     {
4136ba6d3d1SJason Molenda         header.magic        = llvm::ByteSwap_32(header.magic);
4146ba6d3d1SJason Molenda         header.cputype      = llvm::ByteSwap_32(header.cputype);
4156ba6d3d1SJason Molenda         header.cpusubtype   = llvm::ByteSwap_32(header.cpusubtype);
4166ba6d3d1SJason Molenda         header.filetype     = llvm::ByteSwap_32(header.filetype);
4176ba6d3d1SJason Molenda         header.ncmds        = llvm::ByteSwap_32(header.ncmds);
4186ba6d3d1SJason Molenda         header.sizeofcmds   = llvm::ByteSwap_32(header.sizeofcmds);
4196ba6d3d1SJason Molenda         header.flags        = llvm::ByteSwap_32(header.flags);
4206ba6d3d1SJason Molenda     }
4216ba6d3d1SJason Molenda 
4226ba6d3d1SJason Molenda     // A kernel is an executable which does not have the dynamic link object flag set.
4236ba6d3d1SJason Molenda     if (header.filetype == llvm::MachO::HeaderFileTypeExecutable
4246ba6d3d1SJason Molenda         && (header.flags & llvm::MachO::HeaderFlagBitIsDynamicLinkObject) == 0)
4256ba6d3d1SJason Molenda     {
4266ba6d3d1SJason Molenda         // Create a full module to get the UUID
42739f7ee86SGreg Clayton         ModuleSP memory_module_sp = process->ReadModuleFromMemory (FileSpec ("temp_mach_kernel", false), addr);
4286ba6d3d1SJason Molenda         if (!memory_module_sp.get())
4296ba6d3d1SJason Molenda             return UUID();
4306ba6d3d1SJason Molenda 
4316ba6d3d1SJason Molenda         ObjectFile *exe_objfile = memory_module_sp->GetObjectFile();
4326ba6d3d1SJason Molenda         if (exe_objfile == NULL)
4336ba6d3d1SJason Molenda             return UUID();
4346ba6d3d1SJason Molenda 
4356ba6d3d1SJason Molenda         if (exe_objfile->GetType() == ObjectFile::eTypeExecutable && exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
4366ba6d3d1SJason Molenda         {
4376ba6d3d1SJason Molenda             return memory_module_sp->GetUUID();
4386ba6d3d1SJason Molenda         }
4396ba6d3d1SJason Molenda     }
4406ba6d3d1SJason Molenda 
4416ba6d3d1SJason Molenda     return UUID();
4426ba6d3d1SJason Molenda }
4436ba6d3d1SJason Molenda 
4446ba6d3d1SJason Molenda //----------------------------------------------------------------------
445d4bfbc9aSGreg Clayton // Constructor
446d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
4476ba6d3d1SJason Molenda DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel (Process* process, lldb::addr_t kernel_addr) :
448d4bfbc9aSGreg Clayton     DynamicLoader(process),
4496ba6d3d1SJason Molenda     m_kernel_load_address (kernel_addr),
450d4bfbc9aSGreg Clayton     m_kernel(),
451d4bfbc9aSGreg Clayton     m_kext_summary_header_ptr_addr (),
452d4bfbc9aSGreg Clayton     m_kext_summary_header_addr (),
453d4bfbc9aSGreg Clayton     m_kext_summary_header (),
454306bd0aaSJason Molenda     m_known_kexts (),
455a08823fdSDaniel Dunbar     m_mutex(Mutex::eMutexTypeRecursive),
456a08823fdSDaniel Dunbar     m_break_id (LLDB_INVALID_BREAK_ID)
457d4bfbc9aSGreg Clayton {
4581c627543SJason Molenda     PlatformSP platform_sp(Platform::FindPlugin (process, "darwin-kernel"));
4591c627543SJason Molenda     // Only select the darwin-kernel Platform if we've been asked to load kexts.
4601c627543SJason Molenda     // It can take some time to scan over all of the kext info.plists and that
4611c627543SJason Molenda     // shouldn't be done if kext loading is explicitly disabled.
4621c627543SJason Molenda     if (platform_sp.get() && GetGlobalProperties()->GetLoadKexts())
4631c627543SJason Molenda     {
4641c627543SJason Molenda         process->GetTarget().SetPlatform (platform_sp);
4651c627543SJason Molenda     }
466d4bfbc9aSGreg Clayton }
467d4bfbc9aSGreg Clayton 
468d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
469d4bfbc9aSGreg Clayton // Destructor
470d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
471944b828aSGreg Clayton DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel()
472d4bfbc9aSGreg Clayton {
473d4bfbc9aSGreg Clayton     Clear(true);
474d4bfbc9aSGreg Clayton }
475d4bfbc9aSGreg Clayton 
476d4bfbc9aSGreg Clayton void
477944b828aSGreg Clayton DynamicLoaderDarwinKernel::UpdateIfNeeded()
478d4bfbc9aSGreg Clayton {
479d4bfbc9aSGreg Clayton     LoadKernelModuleIfNeeded();
480d4bfbc9aSGreg Clayton     SetNotificationBreakpointIfNeeded ();
481d4bfbc9aSGreg Clayton }
482d4bfbc9aSGreg Clayton //------------------------------------------------------------------
483d4bfbc9aSGreg Clayton /// Called after attaching a process.
484d4bfbc9aSGreg Clayton ///
485d4bfbc9aSGreg Clayton /// Allow DynamicLoader plug-ins to execute some code after
486d4bfbc9aSGreg Clayton /// attaching to a process.
487d4bfbc9aSGreg Clayton //------------------------------------------------------------------
488d4bfbc9aSGreg Clayton void
489944b828aSGreg Clayton DynamicLoaderDarwinKernel::DidAttach ()
490d4bfbc9aSGreg Clayton {
491d4bfbc9aSGreg Clayton     PrivateInitialize(m_process);
492d4bfbc9aSGreg Clayton     UpdateIfNeeded();
493d4bfbc9aSGreg Clayton }
494d4bfbc9aSGreg Clayton 
495d4bfbc9aSGreg Clayton //------------------------------------------------------------------
496d4bfbc9aSGreg Clayton /// Called after attaching a process.
497d4bfbc9aSGreg Clayton ///
498d4bfbc9aSGreg Clayton /// Allow DynamicLoader plug-ins to execute some code after
499d4bfbc9aSGreg Clayton /// attaching to a process.
500d4bfbc9aSGreg Clayton //------------------------------------------------------------------
501d4bfbc9aSGreg Clayton void
502944b828aSGreg Clayton DynamicLoaderDarwinKernel::DidLaunch ()
503d4bfbc9aSGreg Clayton {
504d4bfbc9aSGreg Clayton     PrivateInitialize(m_process);
505d4bfbc9aSGreg Clayton     UpdateIfNeeded();
506d4bfbc9aSGreg Clayton }
507d4bfbc9aSGreg Clayton 
508d4bfbc9aSGreg Clayton 
509d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
510d4bfbc9aSGreg Clayton // Clear out the state of this class.
511d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
512d4bfbc9aSGreg Clayton void
513944b828aSGreg Clayton DynamicLoaderDarwinKernel::Clear (bool clear_process)
514d4bfbc9aSGreg Clayton {
515d4bfbc9aSGreg Clayton     Mutex::Locker locker(m_mutex);
516d4bfbc9aSGreg Clayton 
517d4bfbc9aSGreg Clayton     if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
518d4bfbc9aSGreg Clayton         m_process->ClearBreakpointSiteByID(m_break_id);
519d4bfbc9aSGreg Clayton 
520d4bfbc9aSGreg Clayton     if (clear_process)
521d4bfbc9aSGreg Clayton         m_process = NULL;
522306bd0aaSJason Molenda     m_kernel.Clear();
523306bd0aaSJason Molenda     m_known_kexts.clear();
524d4bfbc9aSGreg Clayton     m_kext_summary_header_ptr_addr.Clear();
525d4bfbc9aSGreg Clayton     m_kext_summary_header_addr.Clear();
526d4bfbc9aSGreg Clayton     m_break_id = LLDB_INVALID_BREAK_ID;
527d4bfbc9aSGreg Clayton }
528d4bfbc9aSGreg Clayton 
529d4bfbc9aSGreg Clayton 
530c859e2d5SGreg Clayton bool
531306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::LoadImageAtFileAddress (Process *process)
5322af282a1SGreg Clayton {
5332af282a1SGreg Clayton     if (IsLoaded())
5342af282a1SGreg Clayton         return true;
5352af282a1SGreg Clayton 
536306bd0aaSJason Molenda     if (m_module_sp)
5372af282a1SGreg Clayton     {
5382af282a1SGreg Clayton         bool changed = false;
539306bd0aaSJason Molenda         if (m_module_sp->SetLoadAddress (process->GetTarget(), 0, changed))
540306bd0aaSJason Molenda             m_load_process_stop_id = process->GetStopID();
5412af282a1SGreg Clayton     }
5422af282a1SGreg Clayton     return false;
5432af282a1SGreg Clayton }
5442af282a1SGreg Clayton 
545306bd0aaSJason Molenda void
546306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::SetModule (ModuleSP module_sp)
547306bd0aaSJason Molenda {
548306bd0aaSJason Molenda     m_module_sp = module_sp;
549306bd0aaSJason Molenda     if (module_sp.get() && module_sp->GetObjectFile())
550306bd0aaSJason Molenda     {
551306bd0aaSJason Molenda         if (module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
552306bd0aaSJason Molenda             && module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
553306bd0aaSJason Molenda         {
554306bd0aaSJason Molenda             m_kernel_image = true;
555306bd0aaSJason Molenda         }
556306bd0aaSJason Molenda         else
557306bd0aaSJason Molenda         {
558306bd0aaSJason Molenda             m_kernel_image = false;
559306bd0aaSJason Molenda         }
560306bd0aaSJason Molenda     }
561306bd0aaSJason Molenda }
562306bd0aaSJason Molenda 
563306bd0aaSJason Molenda ModuleSP
564306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetModule ()
565306bd0aaSJason Molenda {
566306bd0aaSJason Molenda     return m_module_sp;
567306bd0aaSJason Molenda }
568306bd0aaSJason Molenda 
569306bd0aaSJason Molenda void
570306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::SetLoadAddress (addr_t load_addr)
571306bd0aaSJason Molenda {
572306bd0aaSJason Molenda     m_load_address = load_addr;
573306bd0aaSJason Molenda }
574306bd0aaSJason Molenda 
575306bd0aaSJason Molenda addr_t
576306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetLoadAddress () const
577306bd0aaSJason Molenda {
578306bd0aaSJason Molenda     return m_load_address;
579306bd0aaSJason Molenda }
580306bd0aaSJason Molenda 
581306bd0aaSJason Molenda uint64_t
582306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetSize () const
583306bd0aaSJason Molenda {
584306bd0aaSJason Molenda     return m_size;
585306bd0aaSJason Molenda }
586306bd0aaSJason Molenda 
587306bd0aaSJason Molenda void
588306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::SetSize (uint64_t size)
589306bd0aaSJason Molenda {
590306bd0aaSJason Molenda     m_size = size;
591306bd0aaSJason Molenda }
592306bd0aaSJason Molenda 
593306bd0aaSJason Molenda uint32_t
594306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetProcessStopId () const
595306bd0aaSJason Molenda {
596306bd0aaSJason Molenda     return m_load_process_stop_id;
597306bd0aaSJason Molenda }
598306bd0aaSJason Molenda 
599306bd0aaSJason Molenda void
600306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::SetProcessStopId (uint32_t stop_id)
601306bd0aaSJason Molenda {
602306bd0aaSJason Molenda     m_load_process_stop_id = stop_id;
603306bd0aaSJason Molenda }
604306bd0aaSJason Molenda 
6052af282a1SGreg Clayton bool
606306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::operator== (const KextImageInfo &rhs)
607306bd0aaSJason Molenda {
608306bd0aaSJason Molenda     if (m_uuid.IsValid() || rhs.GetUUID().IsValid())
609306bd0aaSJason Molenda     {
610306bd0aaSJason Molenda         if (m_uuid == rhs.GetUUID())
611306bd0aaSJason Molenda         {
612306bd0aaSJason Molenda             return true;
613306bd0aaSJason Molenda         }
614306bd0aaSJason Molenda         return false;
615306bd0aaSJason Molenda     }
616306bd0aaSJason Molenda 
617306bd0aaSJason Molenda     if (m_name == rhs.GetName() && m_load_address == rhs.GetLoadAddress())
618306bd0aaSJason Molenda         return true;
619306bd0aaSJason Molenda 
620306bd0aaSJason Molenda     return false;
621306bd0aaSJason Molenda }
622306bd0aaSJason Molenda 
623306bd0aaSJason Molenda void
624306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::SetName (const char *name)
625306bd0aaSJason Molenda {
626306bd0aaSJason Molenda     m_name = name;
627306bd0aaSJason Molenda }
628306bd0aaSJason Molenda 
629306bd0aaSJason Molenda std::string
630306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetName () const
631306bd0aaSJason Molenda {
632306bd0aaSJason Molenda     return m_name;
633306bd0aaSJason Molenda }
634306bd0aaSJason Molenda 
635306bd0aaSJason Molenda void
636306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::SetUUID (const UUID &uuid)
637306bd0aaSJason Molenda {
638306bd0aaSJason Molenda     m_uuid = uuid;
639306bd0aaSJason Molenda }
640306bd0aaSJason Molenda 
641306bd0aaSJason Molenda UUID
642306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetUUID () const
643306bd0aaSJason Molenda {
644306bd0aaSJason Molenda     return m_uuid;
645306bd0aaSJason Molenda }
646306bd0aaSJason Molenda 
647306bd0aaSJason Molenda // Given the m_load_address from the kext summaries, and a UUID, try to create an in-memory
648306bd0aaSJason Molenda // Module at that address.  Require that the MemoryModule have a matching UUID and detect
649306bd0aaSJason Molenda // if this MemoryModule is a kernel or a kext.
650306bd0aaSJason Molenda //
651306bd0aaSJason Molenda // Returns true if m_memory_module_sp is now set to a valid Module.
652306bd0aaSJason Molenda 
653306bd0aaSJason Molenda bool
654306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::ReadMemoryModule (Process *process)
655306bd0aaSJason Molenda {
656306bd0aaSJason Molenda     if (m_memory_module_sp.get() != NULL)
657306bd0aaSJason Molenda         return true;
658306bd0aaSJason Molenda     if (m_load_address == LLDB_INVALID_ADDRESS)
659306bd0aaSJason Molenda         return false;
660306bd0aaSJason Molenda 
661306bd0aaSJason Molenda     FileSpec file_spec;
662306bd0aaSJason Molenda     file_spec.SetFile (m_name.c_str(), false);
663306bd0aaSJason Molenda 
664306bd0aaSJason Molenda     ModuleSP memory_module_sp = process->ReadModuleFromMemory (file_spec, m_load_address);
665306bd0aaSJason Molenda 
666306bd0aaSJason Molenda     if (memory_module_sp.get() == NULL)
667306bd0aaSJason Molenda         return false;
668306bd0aaSJason Molenda 
669306bd0aaSJason Molenda     bool is_kernel = false;
670306bd0aaSJason Molenda     if (memory_module_sp->GetObjectFile())
671306bd0aaSJason Molenda     {
672306bd0aaSJason Molenda         if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
673306bd0aaSJason Molenda             && memory_module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
674306bd0aaSJason Molenda         {
675306bd0aaSJason Molenda             is_kernel = true;
676306bd0aaSJason Molenda         }
677306bd0aaSJason Molenda         else if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeSharedLibrary)
678306bd0aaSJason Molenda         {
679306bd0aaSJason Molenda             is_kernel = false;
680306bd0aaSJason Molenda         }
681306bd0aaSJason Molenda     }
682306bd0aaSJason Molenda 
68338e70d11SJason Molenda     // If this is a kext, and the kernel specified what UUID we should find at this
68438e70d11SJason Molenda     // load address, require that the memory module have a matching UUID or something
68538e70d11SJason Molenda     // has gone wrong and we should discard it.
686306bd0aaSJason Molenda     if (m_uuid.IsValid())
687306bd0aaSJason Molenda     {
688306bd0aaSJason Molenda         if (m_uuid != memory_module_sp->GetUUID())
689306bd0aaSJason Molenda         {
690306bd0aaSJason Molenda             return false;
691306bd0aaSJason Molenda         }
692306bd0aaSJason Molenda     }
693306bd0aaSJason Molenda 
694306bd0aaSJason Molenda     // If the in-memory Module has a UUID, let's use that.
695306bd0aaSJason Molenda     if (!m_uuid.IsValid() && memory_module_sp->GetUUID().IsValid())
696306bd0aaSJason Molenda     {
697306bd0aaSJason Molenda         m_uuid = memory_module_sp->GetUUID();
698306bd0aaSJason Molenda     }
699306bd0aaSJason Molenda 
700306bd0aaSJason Molenda     m_memory_module_sp = memory_module_sp;
701306bd0aaSJason Molenda     m_kernel_image = is_kernel;
702306bd0aaSJason Molenda     if (is_kernel)
703306bd0aaSJason Molenda     {
704306bd0aaSJason Molenda         if (memory_module_sp->GetArchitecture().IsValid())
705306bd0aaSJason Molenda         {
706306bd0aaSJason Molenda             process->GetTarget().SetArchitecture(memory_module_sp->GetArchitecture());
707306bd0aaSJason Molenda         }
70838e70d11SJason Molenda         if (m_uuid.IsValid())
70938e70d11SJason Molenda         {
71038e70d11SJason Molenda             Module* exe_module = process->GetTarget().GetExecutableModulePointer();
71138e70d11SJason Molenda             if (exe_module && exe_module->GetUUID().IsValid())
71238e70d11SJason Molenda             {
71338e70d11SJason Molenda                 if (m_uuid != exe_module->GetUUID())
71438e70d11SJason Molenda                 {
71538e70d11SJason Molenda                     Stream *s = &process->GetTarget().GetDebugger().GetOutputStream();
71638e70d11SJason Molenda                     if (s)
71738e70d11SJason Molenda                     {
71838e70d11SJason Molenda                         char memory_module_uuidbuf[64];
71938e70d11SJason Molenda                         char exe_module_uuidbuf[64];
72038e70d11SJason Molenda                         s->Printf ("warning: Host-side kernel file has Mach-O UUID of %s but remote kernel has a UUID of %s -- a mismatched kernel file will result in a poor debugger experience.\n",
72138e70d11SJason Molenda                                    exe_module->GetUUID().GetAsCString(exe_module_uuidbuf, sizeof (exe_module_uuidbuf)),
72238e70d11SJason Molenda                                    m_uuid.GetAsCString(memory_module_uuidbuf, sizeof (memory_module_uuidbuf)));
72338e70d11SJason Molenda                         s->Flush ();
72438e70d11SJason Molenda                     }
72538e70d11SJason Molenda                 }
72638e70d11SJason Molenda             }
72738e70d11SJason Molenda         }
728306bd0aaSJason Molenda     }
729306bd0aaSJason Molenda 
730306bd0aaSJason Molenda     return true;
731306bd0aaSJason Molenda }
732306bd0aaSJason Molenda 
733306bd0aaSJason Molenda bool
734306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::IsKernel () const
735306bd0aaSJason Molenda {
736306bd0aaSJason Molenda     return m_kernel_image == true;
737306bd0aaSJason Molenda }
738306bd0aaSJason Molenda 
739306bd0aaSJason Molenda void
740306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::SetIsKernel (bool is_kernel)
741306bd0aaSJason Molenda {
742306bd0aaSJason Molenda     m_kernel_image = is_kernel;
743306bd0aaSJason Molenda }
744306bd0aaSJason Molenda 
745306bd0aaSJason Molenda bool
746306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule (Process *process)
747c859e2d5SGreg Clayton {
748c859e2d5SGreg Clayton     if (IsLoaded())
749c859e2d5SGreg Clayton         return true;
750c859e2d5SGreg Clayton 
751c859e2d5SGreg Clayton 
752c859e2d5SGreg Clayton     Target &target = process->GetTarget();
75387a04b24SJason Molenda 
754306bd0aaSJason Molenda     // If we don't have / can't create a memory module for this kext, don't try to load it - we won't
755306bd0aaSJason Molenda     // have the correct segment load addresses.
756306bd0aaSJason Molenda     if (!ReadMemoryModule (process))
75787a04b24SJason Molenda     {
75887a04b24SJason Molenda         return false;
75987a04b24SJason Molenda     }
76087a04b24SJason Molenda 
761306bd0aaSJason Molenda     bool uuid_is_valid = m_uuid.IsValid();
762306bd0aaSJason Molenda 
763e575e7bcSJason Molenda     if (IsKernel() && uuid_is_valid && m_memory_module_sp.get())
764e575e7bcSJason Molenda     {
765e575e7bcSJason Molenda         Stream *s = &target.GetDebugger().GetOutputStream();
766e575e7bcSJason Molenda         if (s)
767e575e7bcSJason Molenda         {
768e575e7bcSJason Molenda             char uuidbuf[64];
769e575e7bcSJason Molenda             s->Printf ("Kernel UUID: %s\n", m_memory_module_sp->GetUUID().GetAsCString(uuidbuf, sizeof (uuidbuf)));
770e575e7bcSJason Molenda             s->Printf ("Load Address: 0x%" PRIx64 "\n", m_load_address);
771e575e7bcSJason Molenda         }
772e575e7bcSJason Molenda     }
773e575e7bcSJason Molenda 
774306bd0aaSJason Molenda     if (!m_module_sp)
775c859e2d5SGreg Clayton     {
776306bd0aaSJason Molenda         // See if the kext has already been loaded into the target, probably by the user doing target modules add.
7771759848bSEnrico Granata         const ModuleList &target_images = target.GetImages();
778306bd0aaSJason Molenda         m_module_sp = target_images.FindModule(m_uuid);
779c859e2d5SGreg Clayton 
780306bd0aaSJason Molenda         // Search for the kext on the local filesystem via the UUID
781306bd0aaSJason Molenda         if (!m_module_sp && uuid_is_valid)
782b9a01b39SGreg Clayton         {
7832af282a1SGreg Clayton             ModuleSpec module_spec;
784306bd0aaSJason Molenda             module_spec.GetUUID() = m_uuid;
78553667f5dSJason Molenda             module_spec.GetArchitecture() = target.GetArchitecture();
786bb860bd2SJason Molenda 
787d76fb6eaSJason Molenda             // For the kernel, we really do need an on-disk file copy of the binary to do anything useful.
788d76fb6eaSJason Molenda             // This will force a clal to
789306bd0aaSJason Molenda             if (IsKernel())
790bb860bd2SJason Molenda             {
791d76fb6eaSJason Molenda                 if (Symbols::DownloadObjectAndSymbolFile (module_spec, true))
792bb860bd2SJason Molenda                 {
793bb860bd2SJason Molenda                     if (module_spec.GetFileSpec().Exists())
794bb860bd2SJason Molenda                     {
795306bd0aaSJason Molenda                         m_module_sp.reset(new Module (module_spec.GetFileSpec(), target.GetArchitecture()));
796306bd0aaSJason Molenda                         if (m_module_sp.get() && m_module_sp->MatchesModuleSpec (module_spec))
797bb860bd2SJason Molenda                         {
798bb860bd2SJason Molenda                             ModuleList loaded_module_list;
799306bd0aaSJason Molenda                             loaded_module_list.Append (m_module_sp);
800bb860bd2SJason Molenda                             target.ModulesDidLoad (loaded_module_list);
801bb860bd2SJason Molenda                         }
802bb860bd2SJason Molenda                     }
803bb860bd2SJason Molenda                 }
804d76fb6eaSJason Molenda             }
805bb860bd2SJason Molenda 
8061c627543SJason Molenda             // If the current platform is PlatformDarwinKernel, create a ModuleSpec with the filename set
8071c627543SJason Molenda             // to be the bundle ID for this kext, e.g. "com.apple.filesystems.msdosfs", and ask the platform
8081c627543SJason Molenda             // to find it.
8091c627543SJason Molenda             PlatformSP platform_sp (target.GetPlatform());
8101c627543SJason Molenda             if (platform_sp)
8111c627543SJason Molenda             {
8121c627543SJason Molenda                 const char *pname = platform_sp->GetShortPluginName();
8131c627543SJason Molenda                 if (pname && strcmp (pname, "darwin-kernel") == 0)
8141c627543SJason Molenda                 {
8151c627543SJason Molenda                     ModuleSpec kext_bundle_module_spec(module_spec);
8161c627543SJason Molenda                     FileSpec kext_filespec(m_name.c_str(), false);
8171c627543SJason Molenda                     kext_bundle_module_spec.GetFileSpec() = kext_filespec;
8181c627543SJason Molenda                     platform_sp->GetSharedModule (kext_bundle_module_spec, m_module_sp, &target.GetExecutableSearchPaths(), NULL, NULL);
8191c627543SJason Molenda                 }
8201c627543SJason Molenda             }
8211c627543SJason Molenda 
822d76fb6eaSJason Molenda             // Ask the Target to find this file on the local system, if possible.
823bb860bd2SJason Molenda             // This will search in the list of currently-loaded files, look in the
824bb860bd2SJason Molenda             // standard search paths on the system, and on a Mac it will try calling
825bb860bd2SJason Molenda             // the DebugSymbols framework with the UUID to find the binary via its
826bb860bd2SJason Molenda             // search methods.
827306bd0aaSJason Molenda             if (!m_module_sp)
828bb860bd2SJason Molenda             {
829306bd0aaSJason Molenda                 m_module_sp = target.GetSharedModule (module_spec);
830306bd0aaSJason Molenda             }
831e575e7bcSJason Molenda 
832d76fb6eaSJason Molenda             if (IsKernel() && !m_module_sp)
833e575e7bcSJason Molenda             {
834e575e7bcSJason Molenda                 Stream *s = &target.GetDebugger().GetOutputStream();
835e575e7bcSJason Molenda                 if (s)
836e575e7bcSJason Molenda                 {
837*cc6dc780SJason Molenda                     s->Printf ("WARNING: Unable to locate kernel binary on the debugger system.\n");
838e575e7bcSJason Molenda                 }
839e575e7bcSJason Molenda             }
840b9a01b39SGreg Clayton         }
84165d57a3dSJason Molenda 
842306bd0aaSJason Molenda         // If we managed to find a module, append it to the target's list of images.
843306bd0aaSJason Molenda         // If we also have a memory module, require that they have matching UUIDs
844306bd0aaSJason Molenda         if (m_module_sp)
84565d57a3dSJason Molenda         {
846306bd0aaSJason Molenda             bool uuid_match_ok = true;
847306bd0aaSJason Molenda             if (m_memory_module_sp)
84865d57a3dSJason Molenda             {
849306bd0aaSJason Molenda                 if (m_module_sp->GetUUID() != m_memory_module_sp->GetUUID())
850306bd0aaSJason Molenda                 {
851306bd0aaSJason Molenda                     uuid_match_ok = false;
85265d57a3dSJason Molenda                 }
85365d57a3dSJason Molenda             }
854306bd0aaSJason Molenda             if (uuid_match_ok)
855306bd0aaSJason Molenda             {
856a4d3e1d2SJason Molenda                 target.GetImages().AppendIfNeeded(m_module_sp);
857306bd0aaSJason Molenda                 if (IsKernel() && target.GetExecutableModulePointer() != m_module_sp.get())
858306bd0aaSJason Molenda                 {
859306bd0aaSJason Molenda                     target.SetExecutableModule (m_module_sp, false);
860306bd0aaSJason Molenda                 }
861c859e2d5SGreg Clayton             }
862c859e2d5SGreg Clayton         }
863bb860bd2SJason Molenda     }
864c859e2d5SGreg Clayton 
86556c23285SJason Molenda     if (!m_module_sp && !IsKernel() && m_uuid.IsValid() && !m_name.empty())
86656c23285SJason Molenda     {
86756c23285SJason Molenda         Stream *s = &target.GetDebugger().GetOutputStream();
86856c23285SJason Molenda         if (s)
86956c23285SJason Molenda         {
87056c23285SJason Molenda             char uuidbuf[64];
87156c23285SJason Molenda             s->Printf ("warning: Can't find binary/dSYM for %s (%s)\n",
87256c23285SJason Molenda                        m_name.c_str(), m_uuid.GetAsCString(uuidbuf, sizeof (uuidbuf)));
87356c23285SJason Molenda         }
87456c23285SJason Molenda     }
875c859e2d5SGreg Clayton 
87667408537SGreg Clayton     static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
87767408537SGreg Clayton 
878306bd0aaSJason Molenda     if (m_memory_module_sp && m_module_sp)
879c859e2d5SGreg Clayton     {
880306bd0aaSJason Molenda         if (m_module_sp->GetUUID() == m_memory_module_sp->GetUUID())
881c859e2d5SGreg Clayton         {
882306bd0aaSJason Molenda             ObjectFile *ondisk_object_file = m_module_sp->GetObjectFile();
883306bd0aaSJason Molenda             ObjectFile *memory_object_file = m_memory_module_sp->GetObjectFile();
88467408537SGreg Clayton 
885c859e2d5SGreg Clayton             if (memory_object_file && ondisk_object_file)
886c859e2d5SGreg Clayton             {
887306bd0aaSJason Molenda                 // The memory_module for kexts may have an invalid __LINKEDIT seg; skip it.
888306bd0aaSJason Molenda                 const bool ignore_linkedit = !IsKernel ();
88967408537SGreg Clayton 
890c859e2d5SGreg Clayton                 SectionList *ondisk_section_list = ondisk_object_file->GetSectionList ();
891c859e2d5SGreg Clayton                 SectionList *memory_section_list = memory_object_file->GetSectionList ();
892c859e2d5SGreg Clayton                 if (memory_section_list && ondisk_section_list)
893c859e2d5SGreg Clayton                 {
894bae2f2f8SGreg Clayton                     const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
895c859e2d5SGreg Clayton                     // There may be CTF sections in the memory image so we can't
896c859e2d5SGreg Clayton                     // always just compare the number of sections (which are actually
897c859e2d5SGreg Clayton                     // segments in mach-o parlance)
898c859e2d5SGreg Clayton                     uint32_t sect_idx = 0;
899bae2f2f8SGreg Clayton 
90053667f5dSJason Molenda                     // Use the memory_module's addresses for each section to set the
90153667f5dSJason Molenda                     // file module's load address as appropriate.  We don't want to use
90253667f5dSJason Molenda                     // a single slide value for the entire kext - different segments may
90353667f5dSJason Molenda                     // be slid different amounts by the kext loader.
904bae2f2f8SGreg Clayton 
905bae2f2f8SGreg Clayton                     uint32_t num_sections_loaded = 0;
906bae2f2f8SGreg Clayton                     for (sect_idx=0; sect_idx<num_ondisk_sections; ++sect_idx)
907c859e2d5SGreg Clayton                     {
9087820bd1eSGreg Clayton                         SectionSP ondisk_section_sp(ondisk_section_list->GetSectionAtIndex(sect_idx));
9097820bd1eSGreg Clayton                         if (ondisk_section_sp)
910c859e2d5SGreg Clayton                         {
91167408537SGreg Clayton                             // Don't ever load __LINKEDIT as it may or may not be actually
91267408537SGreg Clayton                             // mapped into memory and there is no current way to tell.
91367408537SGreg Clayton                             // I filed rdar://problem/12851706 to track being able to tell
91467408537SGreg Clayton                             // if the __LINKEDIT is actually mapped, but until then, we need
91567408537SGreg Clayton                             // to not load the __LINKEDIT
91667408537SGreg Clayton                             if (ignore_linkedit && ondisk_section_sp->GetName() == g_section_name_LINKEDIT)
91767408537SGreg Clayton                                 continue;
91867408537SGreg Clayton 
9197820bd1eSGreg Clayton                             const Section *memory_section = memory_section_list->FindSectionByName(ondisk_section_sp->GetName()).get();
920bae2f2f8SGreg Clayton                             if (memory_section)
921c859e2d5SGreg Clayton                             {
9227820bd1eSGreg Clayton                                 target.GetSectionLoadList().SetSectionLoadAddress (ondisk_section_sp, memory_section->GetFileAddress());
923bae2f2f8SGreg Clayton                                 ++num_sections_loaded;
924c859e2d5SGreg Clayton                             }
925bae2f2f8SGreg Clayton                         }
926bae2f2f8SGreg Clayton                     }
927bae2f2f8SGreg Clayton                     if (num_sections_loaded > 0)
928306bd0aaSJason Molenda                         m_load_process_stop_id = process->GetStopID();
929bae2f2f8SGreg Clayton                     else
930306bd0aaSJason Molenda                         m_module_sp.reset(); // No sections were loaded
931c859e2d5SGreg Clayton                 }
932c859e2d5SGreg Clayton                 else
933306bd0aaSJason Molenda                     m_module_sp.reset(); // One or both section lists
934c859e2d5SGreg Clayton             }
935c859e2d5SGreg Clayton             else
936306bd0aaSJason Molenda                 m_module_sp.reset(); // One or both object files missing
937c859e2d5SGreg Clayton         }
938c859e2d5SGreg Clayton         else
939306bd0aaSJason Molenda             m_module_sp.reset(); // UUID mismatch
940c859e2d5SGreg Clayton     }
941c859e2d5SGreg Clayton 
942c859e2d5SGreg Clayton     bool is_loaded = IsLoaded();
943c859e2d5SGreg Clayton 
944306bd0aaSJason Molenda     if (is_loaded && m_module_sp && IsKernel())
94568b3607fSJason Molenda     {
94668b3607fSJason Molenda         Stream *s = &target.GetDebugger().GetOutputStream();
94768b3607fSJason Molenda         if (s)
94868b3607fSJason Molenda         {
949732238cdSJason Molenda             ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
950732238cdSJason Molenda             if (kernel_object_file)
951732238cdSJason Molenda             {
952732238cdSJason Molenda                 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
953732238cdSJason Molenda                 if (m_load_address != LLDB_INVALID_ADDRESS && file_address != LLDB_INVALID_ADDRESS)
954732238cdSJason Molenda                 {
955787d1623SMatt Kopec                     s->Printf ("Kernel slid 0x%" PRIx64 " in memory.\n", m_load_address - file_address);
956732238cdSJason Molenda                 }
957732238cdSJason Molenda             }
958743e4396SJason Molenda             {
959b5ad4ec7SGreg Clayton                 s->Printf ("Loaded kernel file %s\n",
960b5ad4ec7SGreg Clayton                            m_module_sp->GetFileSpec().GetPath().c_str());
961743e4396SJason Molenda             }
96268b3607fSJason Molenda             s->Flush ();
96368b3607fSJason Molenda         }
96468b3607fSJason Molenda     }
965c859e2d5SGreg Clayton     return is_loaded;
966c859e2d5SGreg Clayton }
967c859e2d5SGreg Clayton 
9681f746071SGreg Clayton uint32_t
969306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetAddressByteSize ()
9701f746071SGreg Clayton {
971306bd0aaSJason Molenda     if (m_memory_module_sp)
972306bd0aaSJason Molenda         return m_memory_module_sp->GetArchitecture().GetAddressByteSize();
973306bd0aaSJason Molenda     if (m_module_sp)
974306bd0aaSJason Molenda         return m_module_sp->GetArchitecture().GetAddressByteSize();
9751f746071SGreg Clayton     return 0;
9761f746071SGreg Clayton }
9771f746071SGreg Clayton 
9781f746071SGreg Clayton lldb::ByteOrder
979306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetByteOrder()
9801f746071SGreg Clayton {
981306bd0aaSJason Molenda     if (m_memory_module_sp)
982306bd0aaSJason Molenda         return m_memory_module_sp->GetArchitecture().GetByteOrder();
983306bd0aaSJason Molenda     if (m_module_sp)
984306bd0aaSJason Molenda         return m_module_sp->GetArchitecture().GetByteOrder();
9851f746071SGreg Clayton     return lldb::endian::InlHostByteOrder();
9861f746071SGreg Clayton }
9871f746071SGreg Clayton 
9881f746071SGreg Clayton lldb_private::ArchSpec
989306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetArchitecture () const
9901f746071SGreg Clayton {
991306bd0aaSJason Molenda     if (m_memory_module_sp)
992306bd0aaSJason Molenda         return m_memory_module_sp->GetArchitecture();
993306bd0aaSJason Molenda     if (m_module_sp)
994306bd0aaSJason Molenda         return m_module_sp->GetArchitecture();
9951f746071SGreg Clayton     return lldb_private::ArchSpec ();
9961f746071SGreg Clayton }
9971f746071SGreg Clayton 
9981f746071SGreg Clayton 
999d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1000d4bfbc9aSGreg Clayton // Load the kernel module and initialize the "m_kernel" member. Return
1001d4bfbc9aSGreg Clayton // true _only_ if the kernel is loaded the first time through (subsequent
1002d4bfbc9aSGreg Clayton // calls to this function should return false after the kernel has been
1003d4bfbc9aSGreg Clayton // already loaded).
1004d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1005d4bfbc9aSGreg Clayton void
1006944b828aSGreg Clayton DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
1007d4bfbc9aSGreg Clayton {
1008d4bfbc9aSGreg Clayton     if (!m_kext_summary_header_ptr_addr.IsValid())
1009d4bfbc9aSGreg Clayton     {
1010306bd0aaSJason Molenda         m_kernel.Clear();
1011306bd0aaSJason Molenda         m_kernel.SetModule (m_process->GetTarget().GetExecutableModule());
1012306bd0aaSJason Molenda         m_kernel.SetIsKernel(true);
10134bd4e7e3SJason Molenda 
10144bd4e7e3SJason Molenda         ConstString kernel_name("mach_kernel");
1015306bd0aaSJason Molenda         if (m_kernel.GetModule().get()
1016306bd0aaSJason Molenda             && m_kernel.GetModule()->GetObjectFile()
1017306bd0aaSJason Molenda             && !m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename().IsEmpty())
10184bd4e7e3SJason Molenda         {
1019306bd0aaSJason Molenda             kernel_name = m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename();
10204bd4e7e3SJason Molenda         }
1021306bd0aaSJason Molenda         m_kernel.SetName (kernel_name.AsCString());
10224bd4e7e3SJason Molenda 
1023306bd0aaSJason Molenda         if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS)
1024d4bfbc9aSGreg Clayton         {
1025306bd0aaSJason Molenda             m_kernel.SetLoadAddress(m_kernel_load_address);
1026306bd0aaSJason Molenda             if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS && m_kernel.GetModule())
1027c859e2d5SGreg Clayton             {
1028c859e2d5SGreg Clayton                 // We didn't get a hint from the process, so we will
1029c859e2d5SGreg Clayton                 // try the kernel at the address that it exists at in
1030c859e2d5SGreg Clayton                 // the file if we have one
1031306bd0aaSJason Molenda                 ObjectFile *kernel_object_file = m_kernel.GetModule()->GetObjectFile();
1032c859e2d5SGreg Clayton                 if (kernel_object_file)
10334bd4e7e3SJason Molenda                 {
10344bd4e7e3SJason Molenda                     addr_t load_address = kernel_object_file->GetHeaderAddress().GetLoadAddress(&m_process->GetTarget());
10354bd4e7e3SJason Molenda                     addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
10364bd4e7e3SJason Molenda                     if (load_address != LLDB_INVALID_ADDRESS && load_address != 0)
10374bd4e7e3SJason Molenda                     {
1038306bd0aaSJason Molenda                         m_kernel.SetLoadAddress (load_address);
10394bd4e7e3SJason Molenda                         if (load_address != file_address)
10404bd4e7e3SJason Molenda                         {
10414bd4e7e3SJason Molenda                             // Don't accidentally relocate the kernel to the File address --
10424bd4e7e3SJason Molenda                             // the Load address has already been set to its actual in-memory address.
10434bd4e7e3SJason Molenda                             // Mark it as IsLoaded.
1044306bd0aaSJason Molenda                             m_kernel.SetProcessStopId (m_process->GetStopID());
10454bd4e7e3SJason Molenda                         }
10464bd4e7e3SJason Molenda                     }
10474bd4e7e3SJason Molenda                     else
10484bd4e7e3SJason Molenda                     {
1049306bd0aaSJason Molenda                         m_kernel.SetLoadAddress(file_address);
10504bd4e7e3SJason Molenda                     }
10514bd4e7e3SJason Molenda                 }
1052c859e2d5SGreg Clayton             }
1053c859e2d5SGreg Clayton         }
1054c859e2d5SGreg Clayton 
1055306bd0aaSJason Molenda         if (m_kernel.GetLoadAddress() != LLDB_INVALID_ADDRESS)
10562af282a1SGreg Clayton         {
10572af282a1SGreg Clayton             if (!m_kernel.LoadImageUsingMemoryModule (m_process))
10582af282a1SGreg Clayton             {
10592af282a1SGreg Clayton                 m_kernel.LoadImageAtFileAddress (m_process);
10602af282a1SGreg Clayton             }
10612af282a1SGreg Clayton         }
1062c859e2d5SGreg Clayton 
1063306bd0aaSJason Molenda         if (m_kernel.IsLoaded() && m_kernel.GetModule())
1064c859e2d5SGreg Clayton         {
1065d4bfbc9aSGreg Clayton             static ConstString kext_summary_symbol ("gLoadedKextSummaries");
1066306bd0aaSJason Molenda             const Symbol *symbol = m_kernel.GetModule()->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
1067d4bfbc9aSGreg Clayton             if (symbol)
1068c859e2d5SGreg Clayton             {
1069e7612134SGreg Clayton                 m_kext_summary_header_ptr_addr = symbol->GetAddress();
1070d4bfbc9aSGreg Clayton                 // Update all image infos
1071d4bfbc9aSGreg Clayton                 ReadAllKextSummaries ();
1072d4bfbc9aSGreg Clayton             }
1073d4bfbc9aSGreg Clayton         }
1074d4bfbc9aSGreg Clayton         else
1075d4bfbc9aSGreg Clayton         {
1076306bd0aaSJason Molenda             m_kernel.Clear();
1077d4bfbc9aSGreg Clayton         }
1078d4bfbc9aSGreg Clayton     }
1079d4bfbc9aSGreg Clayton }
1080d4bfbc9aSGreg Clayton 
1081d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1082d4bfbc9aSGreg Clayton // Static callback function that gets called when our DYLD notification
1083d4bfbc9aSGreg Clayton // breakpoint gets hit. We update all of our image infos and then
1084d4bfbc9aSGreg Clayton // let our super class DynamicLoader class decide if we should stop
1085d4bfbc9aSGreg Clayton // or not (based on global preference).
1086d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1087d4bfbc9aSGreg Clayton bool
1088944b828aSGreg Clayton DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
1089d4bfbc9aSGreg Clayton                                                   StoppointCallbackContext *context,
1090d4bfbc9aSGreg Clayton                                                   user_id_t break_id,
1091d4bfbc9aSGreg Clayton                                                   user_id_t break_loc_id)
1092d4bfbc9aSGreg Clayton {
1093944b828aSGreg Clayton     return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
1094d4bfbc9aSGreg Clayton }
1095d4bfbc9aSGreg Clayton 
1096d4bfbc9aSGreg Clayton bool
1097944b828aSGreg Clayton DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
1098d4bfbc9aSGreg Clayton                                           user_id_t break_id,
1099d4bfbc9aSGreg Clayton                                           user_id_t break_loc_id)
1100d4bfbc9aSGreg Clayton {
11015160ce5cSGreg Clayton     Log *log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
1102d4bfbc9aSGreg Clayton     if (log)
1103944b828aSGreg Clayton         log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
1104d4bfbc9aSGreg Clayton 
1105d4bfbc9aSGreg Clayton     ReadAllKextSummaries ();
1106d4bfbc9aSGreg Clayton 
1107d4bfbc9aSGreg Clayton     if (log)
11085160ce5cSGreg Clayton         PutToLog(log);
1109d4bfbc9aSGreg Clayton 
1110d4bfbc9aSGreg Clayton     return GetStopWhenImagesChange();
1111d4bfbc9aSGreg Clayton }
1112d4bfbc9aSGreg Clayton 
1113d4bfbc9aSGreg Clayton 
1114d4bfbc9aSGreg Clayton bool
1115944b828aSGreg Clayton DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
1116d4bfbc9aSGreg Clayton {
1117d4bfbc9aSGreg Clayton     Mutex::Locker locker(m_mutex);
1118d4bfbc9aSGreg Clayton 
1119d4bfbc9aSGreg Clayton     // the all image infos is already valid for this process stop ID
1120d4bfbc9aSGreg Clayton 
1121d4bfbc9aSGreg Clayton     if (m_kext_summary_header_ptr_addr.IsValid())
1122d4bfbc9aSGreg Clayton     {
1123d4bfbc9aSGreg Clayton         const uint32_t addr_size = m_kernel.GetAddressByteSize ();
1124d4bfbc9aSGreg Clayton         const ByteOrder byte_order = m_kernel.GetByteOrder();
1125d4bfbc9aSGreg Clayton         Error error;
1126d4bfbc9aSGreg Clayton         // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
1127d4bfbc9aSGreg Clayton         // which is currenty 4 uint32_t and a pointer.
1128d4bfbc9aSGreg Clayton         uint8_t buf[24];
1129d4bfbc9aSGreg Clayton         DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
1130d4bfbc9aSGreg Clayton         const size_t count = 4 * sizeof(uint32_t) + addr_size;
1131d4bfbc9aSGreg Clayton         const bool prefer_file_cache = false;
1132d4bfbc9aSGreg Clayton         if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
1133d4bfbc9aSGreg Clayton                                                           prefer_file_cache,
1134d4bfbc9aSGreg Clayton                                                           error,
1135d4bfbc9aSGreg Clayton                                                           m_kext_summary_header_addr))
1136d4bfbc9aSGreg Clayton         {
1137d4bfbc9aSGreg Clayton             // We got a valid address for our kext summary header and make sure it isn't NULL
1138d4bfbc9aSGreg Clayton             if (m_kext_summary_header_addr.IsValid() &&
1139d4bfbc9aSGreg Clayton                 m_kext_summary_header_addr.GetFileAddress() != 0)
1140d4bfbc9aSGreg Clayton             {
1141d4bfbc9aSGreg Clayton                 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
1142d4bfbc9aSGreg Clayton                 if (bytes_read == count)
1143d4bfbc9aSGreg Clayton                 {
1144c7bece56SGreg Clayton                     lldb::offset_t offset = 0;
1145d4bfbc9aSGreg Clayton                     m_kext_summary_header.version = data.GetU32(&offset);
1146d4bfbc9aSGreg Clayton                     if (m_kext_summary_header.version >= 2)
1147d4bfbc9aSGreg Clayton                     {
1148d4bfbc9aSGreg Clayton                         m_kext_summary_header.entry_size = data.GetU32(&offset);
1149d4bfbc9aSGreg Clayton                     }
1150d4bfbc9aSGreg Clayton                     else
1151d4bfbc9aSGreg Clayton                     {
1152d4bfbc9aSGreg Clayton                         // Versions less than 2 didn't have an entry size, it was hard coded
1153d4bfbc9aSGreg Clayton                         m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
1154d4bfbc9aSGreg Clayton                     }
1155d4bfbc9aSGreg Clayton                     m_kext_summary_header.entry_count = data.GetU32(&offset);
1156d4bfbc9aSGreg Clayton                     return true;
1157d4bfbc9aSGreg Clayton                 }
1158d4bfbc9aSGreg Clayton             }
1159d4bfbc9aSGreg Clayton         }
1160d4bfbc9aSGreg Clayton     }
1161d4bfbc9aSGreg Clayton     m_kext_summary_header_addr.Clear();
1162d4bfbc9aSGreg Clayton     return false;
1163d4bfbc9aSGreg Clayton }
1164d4bfbc9aSGreg Clayton 
1165306bd0aaSJason Molenda // We've either (a) just attached to a new kernel, or (b) the kexts-changed breakpoint was hit
1166306bd0aaSJason Molenda // and we need to figure out what kexts have been added or removed.
1167306bd0aaSJason Molenda // Read the kext summaries from the inferior kernel memory, compare them against the
1168306bd0aaSJason Molenda // m_known_kexts vector and update the m_known_kexts vector as needed to keep in sync with the
1169306bd0aaSJason Molenda // inferior.
1170d4bfbc9aSGreg Clayton 
1171d4bfbc9aSGreg Clayton bool
1172306bd0aaSJason Molenda DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr, uint32_t count)
1173d4bfbc9aSGreg Clayton {
1174306bd0aaSJason Molenda     KextImageInfo::collection kext_summaries;
11755160ce5cSGreg Clayton     Log *log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
1176d4bfbc9aSGreg Clayton     if (log)
1177306bd0aaSJason Molenda         log->Printf ("Kexts-changed breakpoint hit, there are %d kexts currently.\n", count);
1178d4bfbc9aSGreg Clayton 
1179d4bfbc9aSGreg Clayton     Mutex::Locker locker(m_mutex);
1180d4bfbc9aSGreg Clayton 
1181d4bfbc9aSGreg Clayton     if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
1182d4bfbc9aSGreg Clayton         return false;
1183d4bfbc9aSGreg Clayton 
11844da2e32fSJason Molenda     // read the plugin.dynamic-loader.darwin-kernel.load-kexts setting -- if the user requested no
11854da2e32fSJason Molenda     // kext loading, don't print any messages about kexts & don't try to read them.
11864da2e32fSJason Molenda     const bool load_kexts = GetGlobalProperties()->GetLoadKexts();
11874da2e32fSJason Molenda 
1188306bd0aaSJason Molenda     // By default, all kexts we've loaded in the past are marked as "remove" and all of the kexts
1189306bd0aaSJason Molenda     // we just found out about from ReadKextSummaries are marked as "add".
1190306bd0aaSJason Molenda     std::vector<bool> to_be_removed(m_known_kexts.size(), true);
1191306bd0aaSJason Molenda     std::vector<bool> to_be_added(count, true);
1192306bd0aaSJason Molenda 
1193306bd0aaSJason Molenda     int number_of_new_kexts_being_added = 0;
1194306bd0aaSJason Molenda     int number_of_old_kexts_being_removed = m_known_kexts.size();
1195306bd0aaSJason Molenda 
1196306bd0aaSJason Molenda     const uint32_t new_kexts_size = kext_summaries.size();
1197306bd0aaSJason Molenda     const uint32_t old_kexts_size = m_known_kexts.size();
1198306bd0aaSJason Molenda 
1199306bd0aaSJason Molenda     // The m_known_kexts vector may have entries that have been Cleared,
1200306bd0aaSJason Molenda     // or are a kernel.
1201306bd0aaSJason Molenda     for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1202306bd0aaSJason Molenda     {
1203306bd0aaSJason Molenda         bool ignore = false;
1204306bd0aaSJason Molenda         KextImageInfo &image_info = m_known_kexts[old_kext];
1205306bd0aaSJason Molenda         if (image_info.IsKernel())
1206306bd0aaSJason Molenda         {
1207306bd0aaSJason Molenda             ignore = true;
1208306bd0aaSJason Molenda         }
1209306bd0aaSJason Molenda         else if (image_info.GetLoadAddress() == LLDB_INVALID_ADDRESS && !image_info.GetModule())
1210306bd0aaSJason Molenda         {
1211306bd0aaSJason Molenda             ignore = true;
1212306bd0aaSJason Molenda         }
1213306bd0aaSJason Molenda 
1214306bd0aaSJason Molenda         if (ignore)
1215306bd0aaSJason Molenda         {
1216306bd0aaSJason Molenda             number_of_old_kexts_being_removed--;
1217306bd0aaSJason Molenda             to_be_removed[old_kext] = false;
1218306bd0aaSJason Molenda         }
1219306bd0aaSJason Molenda     }
1220306bd0aaSJason Molenda 
1221306bd0aaSJason Molenda     // Scan over the list of kexts we just read from the kernel, note those that
1222306bd0aaSJason Molenda     // need to be added and those already loaded.
1223306bd0aaSJason Molenda     for (uint32_t new_kext = 0; new_kext < new_kexts_size; new_kext++)
1224306bd0aaSJason Molenda     {
1225306bd0aaSJason Molenda         bool add_this_one = true;
1226306bd0aaSJason Molenda         for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1227306bd0aaSJason Molenda         {
1228306bd0aaSJason Molenda             if (m_known_kexts[old_kext] == kext_summaries[new_kext])
1229306bd0aaSJason Molenda             {
1230306bd0aaSJason Molenda                 // We already have this kext, don't re-load it.
1231306bd0aaSJason Molenda                 to_be_added[new_kext] = false;
1232306bd0aaSJason Molenda                 // This kext is still present, do not remove it.
1233306bd0aaSJason Molenda                 to_be_removed[old_kext] = false;
1234306bd0aaSJason Molenda 
1235306bd0aaSJason Molenda                 number_of_old_kexts_being_removed--;
1236306bd0aaSJason Molenda                 add_this_one = false;
1237306bd0aaSJason Molenda                 break;
1238306bd0aaSJason Molenda             }
1239306bd0aaSJason Molenda         }
1240306bd0aaSJason Molenda         if (add_this_one)
1241306bd0aaSJason Molenda         {
1242306bd0aaSJason Molenda             number_of_new_kexts_being_added++;
1243306bd0aaSJason Molenda         }
1244306bd0aaSJason Molenda     }
1245306bd0aaSJason Molenda 
1246306bd0aaSJason Molenda     if (number_of_new_kexts_being_added == 0 && number_of_old_kexts_being_removed == 0)
1247306bd0aaSJason Molenda         return true;
1248306bd0aaSJason Molenda 
1249d4bfbc9aSGreg Clayton     Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
12504da2e32fSJason Molenda     if (s && load_kexts)
1251d4bfbc9aSGreg Clayton     {
1252306bd0aaSJason Molenda         if (number_of_new_kexts_being_added > 0 && number_of_old_kexts_being_removed > 0)
1253306bd0aaSJason Molenda         {
1254306bd0aaSJason Molenda             s->Printf ("Loading %d kext modules and unloading %d kext modules ", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1255306bd0aaSJason Molenda         }
1256306bd0aaSJason Molenda         else if (number_of_new_kexts_being_added > 0)
1257306bd0aaSJason Molenda         {
1258306bd0aaSJason Molenda             s->Printf ("Loading %d kext modules ", number_of_new_kexts_being_added);
1259306bd0aaSJason Molenda         }
1260306bd0aaSJason Molenda         else if (number_of_old_kexts_being_removed > 0)
1261306bd0aaSJason Molenda         {
1262306bd0aaSJason Molenda             s->Printf ("Unloading %d kext modules ", number_of_old_kexts_being_removed);
1263306bd0aaSJason Molenda         }
1264306bd0aaSJason Molenda     }
1265306bd0aaSJason Molenda 
1266306bd0aaSJason Molenda     if (log)
12674da2e32fSJason Molenda     {
12684da2e32fSJason Molenda         if (load_kexts)
12694da2e32fSJason Molenda         {
1270306bd0aaSJason Molenda             log->Printf ("DynamicLoaderDarwinKernel::ParseKextSummaries: %d kexts added, %d kexts removed", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
12714da2e32fSJason Molenda         }
12724da2e32fSJason Molenda         else
12734da2e32fSJason Molenda         {
12744da2e32fSJason Molenda             log->Printf ("DynamicLoaderDarwinKernel::ParseKextSummaries kext loading is disabled, else would have %d kexts added, %d kexts removed", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
12754da2e32fSJason Molenda         }
12764da2e32fSJason Molenda     }
12774da2e32fSJason Molenda 
1278306bd0aaSJason Molenda 
1279306bd0aaSJason Molenda     if (number_of_new_kexts_being_added > 0)
1280306bd0aaSJason Molenda     {
1281306bd0aaSJason Molenda         ModuleList loaded_module_list;
1282306bd0aaSJason Molenda 
1283306bd0aaSJason Molenda         const uint32_t num_of_new_kexts = kext_summaries.size();
1284306bd0aaSJason Molenda         for (uint32_t new_kext = 0; new_kext < num_of_new_kexts; new_kext++)
1285306bd0aaSJason Molenda         {
1286306bd0aaSJason Molenda             if (to_be_added[new_kext] == true)
1287306bd0aaSJason Molenda             {
1288306bd0aaSJason Molenda                 KextImageInfo &image_info = kext_summaries[new_kext];
12894da2e32fSJason Molenda                 if (load_kexts)
12904da2e32fSJason Molenda                 {
1291306bd0aaSJason Molenda                     if (!image_info.LoadImageUsingMemoryModule (m_process))
12924da2e32fSJason Molenda                     {
1293306bd0aaSJason Molenda                         image_info.LoadImageAtFileAddress (m_process);
12944da2e32fSJason Molenda                     }
12954da2e32fSJason Molenda                 }
1296306bd0aaSJason Molenda 
1297306bd0aaSJason Molenda                 m_known_kexts.push_back(image_info);
1298306bd0aaSJason Molenda 
1299306bd0aaSJason Molenda                 if (image_info.GetModule() && m_process->GetStopID() == image_info.GetProcessStopId())
1300306bd0aaSJason Molenda                     loaded_module_list.AppendIfNeeded (image_info.GetModule());
1301d4bfbc9aSGreg Clayton 
13024da2e32fSJason Molenda                 if (s && load_kexts)
130368b3607fSJason Molenda                     s->Printf (".");
1304d4bfbc9aSGreg Clayton 
1305d4bfbc9aSGreg Clayton                 if (log)
13065160ce5cSGreg Clayton                     kext_summaries[new_kext].PutToLog (log);
1307d4bfbc9aSGreg Clayton             }
1308306bd0aaSJason Molenda         }
1309306bd0aaSJason Molenda         m_process->GetTarget().ModulesDidLoad (loaded_module_list);
1310306bd0aaSJason Molenda     }
1311306bd0aaSJason Molenda 
1312306bd0aaSJason Molenda     if (number_of_old_kexts_being_removed > 0)
1313306bd0aaSJason Molenda     {
1314306bd0aaSJason Molenda         ModuleList loaded_module_list;
1315306bd0aaSJason Molenda         const uint32_t num_of_old_kexts = m_known_kexts.size();
1316306bd0aaSJason Molenda         for (uint32_t old_kext = 0; old_kext < num_of_old_kexts; old_kext++)
1317306bd0aaSJason Molenda         {
1318306bd0aaSJason Molenda             ModuleList unloaded_module_list;
1319306bd0aaSJason Molenda             if (to_be_removed[old_kext])
1320306bd0aaSJason Molenda             {
1321306bd0aaSJason Molenda                 KextImageInfo &image_info = m_known_kexts[old_kext];
1322306bd0aaSJason Molenda                 // You can't unload the kernel.
1323306bd0aaSJason Molenda                 if (!image_info.IsKernel())
1324306bd0aaSJason Molenda                 {
1325306bd0aaSJason Molenda                     if (image_info.GetModule())
1326306bd0aaSJason Molenda                     {
1327306bd0aaSJason Molenda                         unloaded_module_list.AppendIfNeeded (image_info.GetModule());
1328306bd0aaSJason Molenda                     }
1329306bd0aaSJason Molenda                     if (s)
1330306bd0aaSJason Molenda                         s->Printf (".");
1331306bd0aaSJason Molenda                     image_info.Clear();
1332306bd0aaSJason Molenda                     // should pull it out of the KextImageInfos vector but that would mutate the list and invalidate
1333306bd0aaSJason Molenda                     // the to_be_removed bool vector; leaving it in place once Cleared() is relatively harmless.
1334306bd0aaSJason Molenda                 }
1335306bd0aaSJason Molenda             }
1336306bd0aaSJason Molenda             m_process->GetTarget().ModulesDidUnload (unloaded_module_list);
1337306bd0aaSJason Molenda         }
1338306bd0aaSJason Molenda     }
1339306bd0aaSJason Molenda 
13404da2e32fSJason Molenda     if (s && load_kexts)
134168b3607fSJason Molenda     {
134268b3607fSJason Molenda         s->Printf (" done.\n");
134368b3607fSJason Molenda         s->Flush ();
134468b3607fSJason Molenda     }
134568b3607fSJason Molenda 
1346306bd0aaSJason Molenda     return true;
1347d4bfbc9aSGreg Clayton }
1348d4bfbc9aSGreg Clayton 
1349d4bfbc9aSGreg Clayton uint32_t
1350944b828aSGreg Clayton DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
1351d4bfbc9aSGreg Clayton                                               uint32_t image_infos_count,
1352306bd0aaSJason Molenda                                               KextImageInfo::collection &image_infos)
1353d4bfbc9aSGreg Clayton {
1354d4bfbc9aSGreg Clayton     const ByteOrder endian = m_kernel.GetByteOrder();
1355d4bfbc9aSGreg Clayton     const uint32_t addr_size = m_kernel.GetAddressByteSize();
1356d4bfbc9aSGreg Clayton 
1357d4bfbc9aSGreg Clayton     image_infos.resize(image_infos_count);
1358d4bfbc9aSGreg Clayton     const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
1359d4bfbc9aSGreg Clayton     DataBufferHeap data(count, 0);
1360d4bfbc9aSGreg Clayton     Error error;
1361d4bfbc9aSGreg Clayton 
1362d4bfbc9aSGreg Clayton     const bool prefer_file_cache = false;
1363d4bfbc9aSGreg Clayton     const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
1364d4bfbc9aSGreg Clayton                                                                  prefer_file_cache,
1365d4bfbc9aSGreg Clayton                                                                  data.GetBytes(),
1366d4bfbc9aSGreg Clayton                                                                  data.GetByteSize(),
1367d4bfbc9aSGreg Clayton                                                                  error);
1368d4bfbc9aSGreg Clayton     if (bytes_read == count)
1369d4bfbc9aSGreg Clayton     {
1370d4bfbc9aSGreg Clayton 
1371d4bfbc9aSGreg Clayton         DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
1372d4bfbc9aSGreg Clayton         uint32_t i=0;
1373d4bfbc9aSGreg Clayton         for (uint32_t kext_summary_offset = 0;
1374d4bfbc9aSGreg Clayton              i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
1375d4bfbc9aSGreg Clayton              ++i, kext_summary_offset += m_kext_summary_header.entry_size)
1376d4bfbc9aSGreg Clayton         {
1377c7bece56SGreg Clayton             lldb::offset_t offset = kext_summary_offset;
1378d4bfbc9aSGreg Clayton             const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
1379d4bfbc9aSGreg Clayton             if (name_data == NULL)
1380d4bfbc9aSGreg Clayton                 break;
1381306bd0aaSJason Molenda             image_infos[i].SetName ((const char *) name_data);
1382306bd0aaSJason Molenda             UUID uuid (extractor.GetData (&offset, 16), 16);
1383306bd0aaSJason Molenda             image_infos[i].SetUUID (uuid);
1384306bd0aaSJason Molenda             image_infos[i].SetLoadAddress (extractor.GetU64(&offset));
1385306bd0aaSJason Molenda             image_infos[i].SetSize (extractor.GetU64(&offset));
1386d4bfbc9aSGreg Clayton         }
1387d4bfbc9aSGreg Clayton         if (i < image_infos.size())
1388d4bfbc9aSGreg Clayton             image_infos.resize(i);
1389d4bfbc9aSGreg Clayton     }
1390d4bfbc9aSGreg Clayton     else
1391d4bfbc9aSGreg Clayton     {
1392d4bfbc9aSGreg Clayton         image_infos.clear();
1393d4bfbc9aSGreg Clayton     }
1394d4bfbc9aSGreg Clayton     return image_infos.size();
1395d4bfbc9aSGreg Clayton }
1396d4bfbc9aSGreg Clayton 
1397d4bfbc9aSGreg Clayton bool
1398944b828aSGreg Clayton DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
1399d4bfbc9aSGreg Clayton {
1400d4bfbc9aSGreg Clayton     Mutex::Locker locker(m_mutex);
1401d4bfbc9aSGreg Clayton 
1402d4bfbc9aSGreg Clayton     if (ReadKextSummaryHeader ())
1403d4bfbc9aSGreg Clayton     {
1404d4bfbc9aSGreg Clayton         if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
1405d4bfbc9aSGreg Clayton         {
1406d4bfbc9aSGreg Clayton             Address summary_addr (m_kext_summary_header_addr);
1407d4bfbc9aSGreg Clayton             summary_addr.Slide(m_kext_summary_header.GetSize());
1408d4bfbc9aSGreg Clayton             if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
1409d4bfbc9aSGreg Clayton             {
1410306bd0aaSJason Molenda                 m_known_kexts.clear();
1411d4bfbc9aSGreg Clayton             }
1412d4bfbc9aSGreg Clayton             return true;
1413d4bfbc9aSGreg Clayton         }
1414d4bfbc9aSGreg Clayton     }
1415d4bfbc9aSGreg Clayton     return false;
1416d4bfbc9aSGreg Clayton }
1417d4bfbc9aSGreg Clayton 
1418d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1419d4bfbc9aSGreg Clayton // Dump an image info structure to the file handle provided.
1420d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1421d4bfbc9aSGreg Clayton void
1422306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::PutToLog (Log *log) const
1423d4bfbc9aSGreg Clayton {
1424d4bfbc9aSGreg Clayton     if (log == NULL)
1425d4bfbc9aSGreg Clayton         return;
1426306bd0aaSJason Molenda     const uint8_t *u = (uint8_t *) m_uuid.GetBytes();
1427d4bfbc9aSGreg Clayton 
1428306bd0aaSJason Molenda     if (m_load_address == LLDB_INVALID_ADDRESS)
1429d4bfbc9aSGreg Clayton     {
1430d4bfbc9aSGreg Clayton         if (u)
1431d4bfbc9aSGreg Clayton         {
1432d4bfbc9aSGreg Clayton             log->Printf("\tuuid=%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X name=\"%s\" (UNLOADED)",
1433d4bfbc9aSGreg Clayton                         u[ 0], u[ 1], u[ 2], u[ 3],
1434d4bfbc9aSGreg Clayton                         u[ 4], u[ 5], u[ 6], u[ 7],
1435d4bfbc9aSGreg Clayton                         u[ 8], u[ 9], u[10], u[11],
1436d4bfbc9aSGreg Clayton                         u[12], u[13], u[14], u[15],
1437306bd0aaSJason Molenda                         m_name.c_str());
1438d4bfbc9aSGreg Clayton         }
1439d4bfbc9aSGreg Clayton         else
1440306bd0aaSJason Molenda             log->Printf("\tname=\"%s\" (UNLOADED)", m_name.c_str());
1441d4bfbc9aSGreg Clayton     }
1442d4bfbc9aSGreg Clayton     else
1443d4bfbc9aSGreg Clayton     {
1444d4bfbc9aSGreg Clayton         if (u)
1445d4bfbc9aSGreg Clayton         {
1446306bd0aaSJason Molenda             log->Printf("\taddr=0x%16.16" PRIx64 " size=0x%16.16" PRIx64 " uuid=%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X name=\"%s\"",
1447306bd0aaSJason Molenda                         m_load_address, m_size,
1448d4bfbc9aSGreg Clayton                         u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
1449d4bfbc9aSGreg Clayton                         u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
1450306bd0aaSJason Molenda                         m_name.c_str());
1451d4bfbc9aSGreg Clayton         }
1452d4bfbc9aSGreg Clayton         else
1453d4bfbc9aSGreg Clayton         {
1454306bd0aaSJason Molenda             log->Printf("\t[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") name=\"%s\"",
1455306bd0aaSJason Molenda                         m_load_address, m_load_address+m_size, m_name.c_str());
1456d4bfbc9aSGreg Clayton         }
1457d4bfbc9aSGreg Clayton     }
1458d4bfbc9aSGreg Clayton }
1459d4bfbc9aSGreg Clayton 
1460d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1461d4bfbc9aSGreg Clayton // Dump the _dyld_all_image_infos members and all current image infos
1462d4bfbc9aSGreg Clayton // that we have parsed to the file handle provided.
1463d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1464d4bfbc9aSGreg Clayton void
1465944b828aSGreg Clayton DynamicLoaderDarwinKernel::PutToLog(Log *log) const
1466d4bfbc9aSGreg Clayton {
1467d4bfbc9aSGreg Clayton     if (log == NULL)
1468d4bfbc9aSGreg Clayton         return;
1469d4bfbc9aSGreg Clayton 
1470d4bfbc9aSGreg Clayton     Mutex::Locker locker(m_mutex);
1471d01b2953SDaniel Malea     log->Printf("gLoadedKextSummaries = 0x%16.16" PRIx64 " { version=%u, entry_size=%u, entry_count=%u }",
1472d4bfbc9aSGreg Clayton                 m_kext_summary_header_addr.GetFileAddress(),
1473d4bfbc9aSGreg Clayton                 m_kext_summary_header.version,
1474d4bfbc9aSGreg Clayton                 m_kext_summary_header.entry_size,
1475d4bfbc9aSGreg Clayton                 m_kext_summary_header.entry_count);
1476d4bfbc9aSGreg Clayton 
1477d4bfbc9aSGreg Clayton     size_t i;
1478306bd0aaSJason Molenda     const size_t count = m_known_kexts.size();
1479d4bfbc9aSGreg Clayton     if (count > 0)
1480d4bfbc9aSGreg Clayton     {
1481d4bfbc9aSGreg Clayton         log->PutCString("Loaded:");
1482d4bfbc9aSGreg Clayton         for (i = 0; i<count; i++)
1483306bd0aaSJason Molenda             m_known_kexts[i].PutToLog(log);
1484d4bfbc9aSGreg Clayton     }
1485d4bfbc9aSGreg Clayton }
1486d4bfbc9aSGreg Clayton 
1487d4bfbc9aSGreg Clayton void
1488944b828aSGreg Clayton DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
1489d4bfbc9aSGreg Clayton {
1490944b828aSGreg Clayton     DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
1491d4bfbc9aSGreg Clayton     Clear(true);
1492d4bfbc9aSGreg Clayton     m_process = process;
1493d4bfbc9aSGreg Clayton }
1494d4bfbc9aSGreg Clayton 
1495d4bfbc9aSGreg Clayton void
1496944b828aSGreg Clayton DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
1497d4bfbc9aSGreg Clayton {
1498306bd0aaSJason Molenda     if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.GetModule())
1499d4bfbc9aSGreg Clayton     {
1500944b828aSGreg Clayton         DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
1501d4bfbc9aSGreg Clayton 
1502d4bfbc9aSGreg Clayton 
1503a8558b62SJim Ingham         const bool internal_bp = true;
1504d4bfbc9aSGreg Clayton         const LazyBool skip_prologue = eLazyBoolNo;
1505969795f1SJim Ingham         FileSpecList module_spec_list;
1506306bd0aaSJason Molenda         module_spec_list.Append (m_kernel.GetModule()->GetFileSpec());
1507969795f1SJim Ingham         Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
150887df91b8SJim Ingham                                                                   NULL,
1509d4bfbc9aSGreg Clayton                                                                   "OSKextLoadedKextSummariesUpdated",
1510d4bfbc9aSGreg Clayton                                                                   eFunctionNameTypeFull,
1511a8558b62SJim Ingham                                                                   skip_prologue,
1512a8558b62SJim Ingham                                                                   internal_bp).get();
1513d4bfbc9aSGreg Clayton 
1514944b828aSGreg Clayton         bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
1515d4bfbc9aSGreg Clayton         m_break_id = bp->GetID();
1516d4bfbc9aSGreg Clayton     }
1517d4bfbc9aSGreg Clayton }
1518d4bfbc9aSGreg Clayton 
1519d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1520d4bfbc9aSGreg Clayton // Member function that gets called when the process state changes.
1521d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1522d4bfbc9aSGreg Clayton void
1523944b828aSGreg Clayton DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
1524d4bfbc9aSGreg Clayton {
1525944b828aSGreg Clayton     DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
1526d4bfbc9aSGreg Clayton     switch (state)
1527d4bfbc9aSGreg Clayton     {
1528d4bfbc9aSGreg Clayton     case eStateConnected:
1529d4bfbc9aSGreg Clayton     case eStateAttaching:
1530d4bfbc9aSGreg Clayton     case eStateLaunching:
1531d4bfbc9aSGreg Clayton     case eStateInvalid:
1532d4bfbc9aSGreg Clayton     case eStateUnloaded:
1533d4bfbc9aSGreg Clayton     case eStateExited:
1534d4bfbc9aSGreg Clayton     case eStateDetached:
1535d4bfbc9aSGreg Clayton         Clear(false);
1536d4bfbc9aSGreg Clayton         break;
1537d4bfbc9aSGreg Clayton 
1538d4bfbc9aSGreg Clayton     case eStateStopped:
1539d4bfbc9aSGreg Clayton         UpdateIfNeeded();
1540d4bfbc9aSGreg Clayton         break;
1541d4bfbc9aSGreg Clayton 
1542d4bfbc9aSGreg Clayton     case eStateRunning:
1543d4bfbc9aSGreg Clayton     case eStateStepping:
1544d4bfbc9aSGreg Clayton     case eStateCrashed:
1545d4bfbc9aSGreg Clayton     case eStateSuspended:
1546d4bfbc9aSGreg Clayton         break;
1547d4bfbc9aSGreg Clayton     }
1548d4bfbc9aSGreg Clayton }
1549d4bfbc9aSGreg Clayton 
1550d4bfbc9aSGreg Clayton ThreadPlanSP
1551944b828aSGreg Clayton DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
1552d4bfbc9aSGreg Clayton {
1553d4bfbc9aSGreg Clayton     ThreadPlanSP thread_plan_sp;
15545160ce5cSGreg Clayton     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1555d4bfbc9aSGreg Clayton     if (log)
1556d4bfbc9aSGreg Clayton         log->Printf ("Could not find symbol for step through.");
1557d4bfbc9aSGreg Clayton     return thread_plan_sp;
1558d4bfbc9aSGreg Clayton }
1559d4bfbc9aSGreg Clayton 
1560d4bfbc9aSGreg Clayton Error
1561944b828aSGreg Clayton DynamicLoaderDarwinKernel::CanLoadImage ()
1562d4bfbc9aSGreg Clayton {
1563d4bfbc9aSGreg Clayton     Error error;
1564d4bfbc9aSGreg Clayton     error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
1565d4bfbc9aSGreg Clayton     return error;
1566d4bfbc9aSGreg Clayton }
1567d4bfbc9aSGreg Clayton 
1568d4bfbc9aSGreg Clayton void
1569944b828aSGreg Clayton DynamicLoaderDarwinKernel::Initialize()
1570d4bfbc9aSGreg Clayton {
1571d4bfbc9aSGreg Clayton     PluginManager::RegisterPlugin (GetPluginNameStatic(),
1572d4bfbc9aSGreg Clayton                                    GetPluginDescriptionStatic(),
1573e8cd0c98SGreg Clayton                                    CreateInstance,
1574e8cd0c98SGreg Clayton                                    DebuggerInitialize);
1575d4bfbc9aSGreg Clayton }
1576d4bfbc9aSGreg Clayton 
1577d4bfbc9aSGreg Clayton void
1578944b828aSGreg Clayton DynamicLoaderDarwinKernel::Terminate()
1579d4bfbc9aSGreg Clayton {
1580d4bfbc9aSGreg Clayton     PluginManager::UnregisterPlugin (CreateInstance);
1581d4bfbc9aSGreg Clayton }
1582d4bfbc9aSGreg Clayton 
1583e8cd0c98SGreg Clayton void
1584e8cd0c98SGreg Clayton DynamicLoaderDarwinKernel::DebuggerInitialize (lldb_private::Debugger &debugger)
1585e8cd0c98SGreg Clayton {
1586e8cd0c98SGreg Clayton     if (!PluginManager::GetSettingForDynamicLoaderPlugin (debugger, DynamicLoaderDarwinKernelProperties::GetSettingName()))
1587e8cd0c98SGreg Clayton     {
1588e8cd0c98SGreg Clayton         const bool is_global_setting = true;
1589e8cd0c98SGreg Clayton         PluginManager::CreateSettingForDynamicLoaderPlugin (debugger,
1590e8cd0c98SGreg Clayton                                                             GetGlobalProperties()->GetValueProperties(),
1591e8cd0c98SGreg Clayton                                                             ConstString ("Properties for the DynamicLoaderDarwinKernel plug-in."),
1592e8cd0c98SGreg Clayton                                                             is_global_setting);
1593e8cd0c98SGreg Clayton     }
1594e8cd0c98SGreg Clayton }
1595d4bfbc9aSGreg Clayton 
1596d4bfbc9aSGreg Clayton const char *
1597944b828aSGreg Clayton DynamicLoaderDarwinKernel::GetPluginNameStatic()
1598d4bfbc9aSGreg Clayton {
1599468ea4ebSGreg Clayton     return "dynamic-loader.darwin-kernel";
1600d4bfbc9aSGreg Clayton }
1601d4bfbc9aSGreg Clayton 
1602d4bfbc9aSGreg Clayton const char *
1603944b828aSGreg Clayton DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
1604d4bfbc9aSGreg Clayton {
1605d4bfbc9aSGreg Clayton     return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
1606d4bfbc9aSGreg Clayton }
1607d4bfbc9aSGreg Clayton 
1608d4bfbc9aSGreg Clayton 
1609d4bfbc9aSGreg Clayton //------------------------------------------------------------------
1610d4bfbc9aSGreg Clayton // PluginInterface protocol
1611d4bfbc9aSGreg Clayton //------------------------------------------------------------------
1612d4bfbc9aSGreg Clayton const char *
1613944b828aSGreg Clayton DynamicLoaderDarwinKernel::GetPluginName()
1614d4bfbc9aSGreg Clayton {
1615944b828aSGreg Clayton     return "DynamicLoaderDarwinKernel";
1616d4bfbc9aSGreg Clayton }
1617d4bfbc9aSGreg Clayton 
1618d4bfbc9aSGreg Clayton const char *
1619944b828aSGreg Clayton DynamicLoaderDarwinKernel::GetShortPluginName()
1620d4bfbc9aSGreg Clayton {
1621d4bfbc9aSGreg Clayton     return GetPluginNameStatic();
1622d4bfbc9aSGreg Clayton }
1623d4bfbc9aSGreg Clayton 
1624d4bfbc9aSGreg Clayton uint32_t
1625944b828aSGreg Clayton DynamicLoaderDarwinKernel::GetPluginVersion()
1626d4bfbc9aSGreg Clayton {
1627d4bfbc9aSGreg Clayton     return 1;
1628d4bfbc9aSGreg Clayton }
1629d4bfbc9aSGreg Clayton 
16301f746071SGreg Clayton lldb::ByteOrder
16311f746071SGreg Clayton DynamicLoaderDarwinKernel::GetByteOrderFromMagic (uint32_t magic)
16321f746071SGreg Clayton {
16331f746071SGreg Clayton     switch (magic)
16341f746071SGreg Clayton     {
16351f746071SGreg Clayton         case llvm::MachO::HeaderMagic32:
16361f746071SGreg Clayton         case llvm::MachO::HeaderMagic64:
16371f746071SGreg Clayton             return lldb::endian::InlHostByteOrder();
16381f746071SGreg Clayton 
16391f746071SGreg Clayton         case llvm::MachO::HeaderMagic32Swapped:
16401f746071SGreg Clayton         case llvm::MachO::HeaderMagic64Swapped:
16411f746071SGreg Clayton             if (lldb::endian::InlHostByteOrder() == lldb::eByteOrderBig)
16421f746071SGreg Clayton                 return lldb::eByteOrderLittle;
16431f746071SGreg Clayton             else
16441f746071SGreg Clayton                 return lldb::eByteOrderBig;
16451f746071SGreg Clayton 
16461f746071SGreg Clayton         default:
16471f746071SGreg Clayton             break;
16481f746071SGreg Clayton     }
16491f746071SGreg Clayton     return lldb::eByteOrderInvalid;
16501f746071SGreg Clayton }
16511f746071SGreg Clayton 
1652