diff options
Diffstat (limited to 'source/Plugins/Process/mach-core/ProcessMachCore.cpp')
-rw-r--r-- | source/Plugins/Process/mach-core/ProcessMachCore.cpp | 183 |
1 files changed, 158 insertions, 25 deletions
diff --git a/source/Plugins/Process/mach-core/ProcessMachCore.cpp b/source/Plugins/Process/mach-core/ProcessMachCore.cpp index b199ec606367..6bf198ca2f37 100644 --- a/source/Plugins/Process/mach-core/ProcessMachCore.cpp +++ b/source/Plugins/Process/mach-core/ProcessMachCore.cpp @@ -18,13 +18,15 @@ // Other libraries and framework includes #include "lldb/Core/DataBuffer.h" #include "lldb/Core/Debugger.h" -#include "lldb/Core/PluginManager.h" +#include "lldb/Core/Log.h" #include "lldb/Core/Module.h" #include "lldb/Core/ModuleSpec.h" +#include "lldb/Core/PluginManager.h" #include "lldb/Core/Section.h" #include "lldb/Core/State.h" #include "lldb/Host/Host.h" #include "lldb/Symbol/ObjectFile.h" +#include "lldb/Target/MemoryRegionInfo.h" #include "lldb/Target/Target.h" #include "lldb/Target/Thread.h" @@ -64,7 +66,7 @@ ProcessMachCore::Terminate() lldb::ProcessSP -ProcessMachCore::CreateInstance (lldb::TargetSP target_sp, Listener &listener, const FileSpec *crash_file) +ProcessMachCore::CreateInstance (lldb::TargetSP target_sp, ListenerSP listener_sp, const FileSpec *crash_file) { lldb::ProcessSP process_sp; if (crash_file) @@ -80,7 +82,7 @@ ProcessMachCore::CreateInstance (lldb::TargetSP target_sp, Listener &listener, c if (ObjectFileMachO::ParseHeader(data, &data_offset, mach_header)) { if (mach_header.filetype == llvm::MachO::MH_CORE) - process_sp.reset(new ProcessMachCore (target_sp, listener, *crash_file)); + process_sp.reset(new ProcessMachCore (target_sp, listener_sp, *crash_file)); } } @@ -121,14 +123,15 @@ ProcessMachCore::CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_nam //---------------------------------------------------------------------- // ProcessMachCore constructor //---------------------------------------------------------------------- -ProcessMachCore::ProcessMachCore(lldb::TargetSP target_sp, Listener &listener, const FileSpec &core_file) : - Process (target_sp, listener), - m_core_aranges (), - m_core_module_sp (), - m_core_file (core_file), - m_dyld_addr (LLDB_INVALID_ADDRESS), - m_mach_kernel_addr (LLDB_INVALID_ADDRESS), - m_dyld_plugin_name () +ProcessMachCore::ProcessMachCore(lldb::TargetSP target_sp, ListenerSP listener_sp, const FileSpec &core_file) + : Process(target_sp, listener_sp), + m_core_aranges(), + m_core_range_infos(), + m_core_module_sp(), + m_core_file(core_file), + m_dyld_addr(LLDB_INVALID_ADDRESS), + m_mach_kernel_addr(LLDB_INVALID_ADDRESS), + m_dyld_plugin_name() { } @@ -163,6 +166,7 @@ ProcessMachCore::GetPluginVersion() bool ProcessMachCore::GetDynamicLoaderAddress (lldb::addr_t addr) { + Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_PROCESS)); llvm::MachO::mach_header header; Error error; if (DoReadMemory (addr, &header, sizeof(header), error) != sizeof(header)) @@ -194,6 +198,8 @@ ProcessMachCore::GetDynamicLoaderAddress (lldb::addr_t addr) case llvm::MachO::MH_DYLINKER: //printf("0x%16.16" PRIx64 ": file_type = MH_DYLINKER\n", vaddr); // Address of dyld "struct mach_header" in the core file + if (log) + log->Printf ("ProcessMachCore::GetDynamicLoaderAddress found a user process dyld binary image at 0x%" PRIx64, addr); m_dyld_addr = addr; return true; @@ -203,6 +209,8 @@ ProcessMachCore::GetDynamicLoaderAddress (lldb::addr_t addr) // is NOT set. If it isn't, then we have a mach_kernel. if ((header.flags & llvm::MachO::MH_DYLDLINK) == 0) { + if (log) + log->Printf ("ProcessMachCore::GetDynamicLoaderAddress found a mach kernel binary image at 0x%" PRIx64, addr); // Address of the mach kernel "struct mach_header" in the core file. m_mach_kernel_addr = addr; return true; @@ -219,6 +227,7 @@ ProcessMachCore::GetDynamicLoaderAddress (lldb::addr_t addr) Error ProcessMachCore::DoLoadCore () { + Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER | LIBLLDB_LOG_PROCESS)); Error error; if (!m_core_module_sp) { @@ -297,11 +306,21 @@ ProcessMachCore::DoLoadCore () { m_core_aranges.Append(range_entry); } + // Some core files don't fill in the permissions correctly. If that is the case + // assume read + execute so clients don't think the memory is not readable, + // or executable. The memory isn't writable since this plug-in doesn't implement + // DoWriteMemory. + uint32_t permissions = section->GetPermissions(); + if (permissions == 0) + permissions = lldb::ePermissionsReadable | lldb::ePermissionsExecutable; + m_core_range_infos.Append( + VMRangeToPermissions::Entry(section_vm_addr, section->GetByteSize(), permissions)); } } if (!ranges_are_sorted) { m_core_aranges.Sort(); + m_core_range_infos.Sort(); } if (m_dyld_addr == LLDB_INVALID_ADDRESS || m_mach_kernel_addr == LLDB_INVALID_ADDRESS) @@ -314,9 +333,7 @@ ProcessMachCore::DoLoadCore () // later if both are present. const size_t num_core_aranges = m_core_aranges.GetSize(); - for (size_t i = 0; - i < num_core_aranges && (m_dyld_addr == LLDB_INVALID_ADDRESS || m_mach_kernel_addr == LLDB_INVALID_ADDRESS); - ++i) + for (size_t i = 0; i < num_core_aranges; ++i) { const VMRangeToFileOffset::Entry *entry = m_core_aranges.GetEntryAtIndex(i); lldb::addr_t section_vm_addr_start = entry->GetRangeBase(); @@ -330,16 +347,58 @@ ProcessMachCore::DoLoadCore () } } + + if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) + { + // In the case of multiple kernel images found in the core file via exhaustive + // search, we may not pick the correct one. See if the DynamicLoaderDarwinKernel's + // search heuristics might identify the correct one. + // Most of the time, I expect the address from SearchForDarwinKernel() will be the + // same as the address we found via exhaustive search. + + if (GetTarget().GetArchitecture().IsValid() == false && m_core_module_sp.get()) + { + GetTarget().SetArchitecture (m_core_module_sp->GetArchitecture()); + } + + // SearchForDarwinKernel will end up calling back into this this class in the GetImageInfoAddress + // method which will give it the m_mach_kernel_addr/m_dyld_addr it already has. Save that aside + // and set m_mach_kernel_addr/m_dyld_addr to an invalid address temporarily so + // DynamicLoaderDarwinKernel does a real search for the kernel using its own heuristics. + + addr_t saved_mach_kernel_addr = m_mach_kernel_addr; + addr_t saved_user_dyld_addr = m_dyld_addr; + m_mach_kernel_addr = LLDB_INVALID_ADDRESS; + m_dyld_addr = LLDB_INVALID_ADDRESS; + + addr_t better_kernel_address = DynamicLoaderDarwinKernel::SearchForDarwinKernel (this); + + m_mach_kernel_addr = saved_mach_kernel_addr; + m_dyld_addr = saved_user_dyld_addr; + + if (better_kernel_address != LLDB_INVALID_ADDRESS) + { + if (log) + log->Printf ("ProcessMachCore::DoLoadCore: Using the kernel address from DynamicLoaderDarwinKernel"); + m_mach_kernel_addr = better_kernel_address; + } + } + + // If we found both a user-process dyld and a kernel binary, we need to decide // which to prefer. if (GetCorefilePreference() == eKernelCorefile) { if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) { + if (log) + log->Printf ("ProcessMachCore::DoLoadCore: Using kernel corefile image at 0x%" PRIx64, m_mach_kernel_addr); m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); } else if (m_dyld_addr != LLDB_INVALID_ADDRESS) { + if (log) + log->Printf ("ProcessMachCore::DoLoadCore: Using user process dyld image at 0x%" PRIx64, m_dyld_addr); m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic(); } } @@ -347,10 +406,14 @@ ProcessMachCore::DoLoadCore () { if (m_dyld_addr != LLDB_INVALID_ADDRESS) { + if (log) + log->Printf ("ProcessMachCore::DoLoadCore: Using user process dyld image at 0x%" PRIx64, m_dyld_addr); m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic(); } else if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) { + if (log) + log->Printf ("ProcessMachCore::DoLoadCore: Using kernel corefile image at 0x%" PRIx64, m_mach_kernel_addr); m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); } } @@ -450,25 +513,95 @@ size_t ProcessMachCore::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) { ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); + size_t bytes_read = 0; if (core_objfile) { - const VMRangeToFileOffset::Entry *core_memory_entry = m_core_aranges.FindEntryThatContains (addr); - if (core_memory_entry) + //---------------------------------------------------------------------- + // Segments are not always contiguous in mach-o core files. We have core + // files that have segments like: + // Address Size File off File size + // ---------- ---------- ---------- ---------- + // LC_SEGMENT 0x000f6000 0x00001000 0x1d509ee8 0x00001000 --- --- 0 0x00000000 __TEXT + // LC_SEGMENT 0x0f600000 0x00100000 0x1d50aee8 0x00100000 --- --- 0 0x00000000 __TEXT + // LC_SEGMENT 0x000f7000 0x00001000 0x1d60aee8 0x00001000 --- --- 0 0x00000000 __TEXT + // + // Any if the user executes the following command: + // + // (lldb) mem read 0xf6ff0 + // + // We would attempt to read 32 bytes from 0xf6ff0 but would only + // get 16 unless we loop through consecutive memory ranges that are + // contiguous in the address space, but not in the file data. + //---------------------------------------------------------------------- + while (bytes_read < size) { - const addr_t offset = addr - core_memory_entry->GetRangeBase(); - const addr_t bytes_left = core_memory_entry->GetRangeEnd() - addr; - size_t bytes_to_read = size; - if (bytes_to_read > bytes_left) - bytes_to_read = bytes_left; - return core_objfile->CopyData (core_memory_entry->data.GetRangeBase() + offset, bytes_to_read, buf); + const addr_t curr_addr = addr + bytes_read; + const VMRangeToFileOffset::Entry *core_memory_entry = m_core_aranges.FindEntryThatContains(curr_addr); + + if (core_memory_entry) + { + const addr_t offset = curr_addr - core_memory_entry->GetRangeBase(); + const addr_t bytes_left = core_memory_entry->GetRangeEnd() - curr_addr; + const size_t bytes_to_read = std::min(size - bytes_read, (size_t)bytes_left); + const size_t curr_bytes_read = core_objfile->CopyData(core_memory_entry->data.GetRangeBase() + offset, + bytes_to_read, (char *)buf + bytes_read); + if (curr_bytes_read == 0) + break; + bytes_read += curr_bytes_read; + } + else + { + // Only set the error if we didn't read any bytes + if (bytes_read == 0) + error.SetErrorStringWithFormat("core file does not contain 0x%" PRIx64, curr_addr); + break; + } } - else + } + + return bytes_read; +} + +Error +ProcessMachCore::GetMemoryRegionInfo(addr_t load_addr, MemoryRegionInfo ®ion_info) +{ + region_info.Clear(); + const VMRangeToPermissions::Entry *permission_entry = m_core_range_infos.FindEntryThatContainsOrFollows(load_addr); + if (permission_entry) + { + if (permission_entry->Contains(load_addr)) { - error.SetErrorStringWithFormat ("core file does not contain 0x%" PRIx64, addr); + region_info.GetRange().SetRangeBase(permission_entry->GetRangeBase()); + region_info.GetRange().SetRangeEnd(permission_entry->GetRangeEnd()); + const Flags permissions(permission_entry->data); + region_info.SetReadable(permissions.Test(ePermissionsReadable) ? MemoryRegionInfo::eYes + : MemoryRegionInfo::eNo); + region_info.SetWritable(permissions.Test(ePermissionsWritable) ? MemoryRegionInfo::eYes + : MemoryRegionInfo::eNo); + region_info.SetExecutable(permissions.Test(ePermissionsExecutable) ? MemoryRegionInfo::eYes + : MemoryRegionInfo::eNo); + region_info.SetMapped(MemoryRegionInfo::eYes); } + else if (load_addr < permission_entry->GetRangeBase()) + { + region_info.GetRange().SetRangeBase(load_addr); + region_info.GetRange().SetRangeEnd(permission_entry->GetRangeBase()); + region_info.SetReadable(MemoryRegionInfo::eNo); + region_info.SetWritable(MemoryRegionInfo::eNo); + region_info.SetExecutable(MemoryRegionInfo::eNo); + region_info.SetMapped(MemoryRegionInfo::eNo); + } + return Error(); } - return 0; + + region_info.GetRange().SetRangeBase(load_addr); + region_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); + region_info.SetReadable(MemoryRegionInfo::eNo); + region_info.SetWritable(MemoryRegionInfo::eNo); + region_info.SetExecutable(MemoryRegionInfo::eNo); + region_info.SetMapped(MemoryRegionInfo::eNo); + return Error(); } void |