1 //===-- ProcessMachCore.cpp ------------------------------------------*- C++ 2 //-*-===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include <errno.h> 11 #include <stdlib.h> 12 13 #include "llvm/Support/MathExtras.h" 14 #include "llvm/Support/Threading.h" 15 16 #include "lldb/Core/Debugger.h" 17 #include "lldb/Core/Module.h" 18 #include "lldb/Core/ModuleSpec.h" 19 #include "lldb/Core/PluginManager.h" 20 #include "lldb/Core/Section.h" 21 #include "lldb/Host/Host.h" 22 #include "lldb/Symbol/ObjectFile.h" 23 #include "lldb/Target/MemoryRegionInfo.h" 24 #include "lldb/Target/Target.h" 25 #include "lldb/Target/Thread.h" 26 #include "lldb/Utility/DataBuffer.h" 27 #include "lldb/Utility/Log.h" 28 #include "lldb/Utility/State.h" 29 30 #include "ProcessMachCore.h" 31 #include "Plugins/Process/Utility/StopInfoMachException.h" 32 #include "ThreadMachCore.h" 33 34 // Needed for the plug-in names for the dynamic loaders. 35 #include "lldb/Host/SafeMachO.h" 36 37 #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h" 38 #include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h" 39 #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h" 40 41 #include <memory> 42 #include <mutex> 43 44 using namespace lldb; 45 using namespace lldb_private; 46 47 ConstString ProcessMachCore::GetPluginNameStatic() { 48 static ConstString g_name("mach-o-core"); 49 return g_name; 50 } 51 52 const char *ProcessMachCore::GetPluginDescriptionStatic() { 53 return "Mach-O core file debugging plug-in."; 54 } 55 56 void ProcessMachCore::Terminate() { 57 PluginManager::UnregisterPlugin(ProcessMachCore::CreateInstance); 58 } 59 60 lldb::ProcessSP ProcessMachCore::CreateInstance(lldb::TargetSP target_sp, 61 ListenerSP listener_sp, 62 const FileSpec *crash_file) { 63 lldb::ProcessSP process_sp; 64 if (crash_file) { 65 const size_t header_size = sizeof(llvm::MachO::mach_header); 66 auto data_sp = FileSystem::Instance().CreateDataBuffer( 67 crash_file->GetPath(), header_size, 0); 68 if (data_sp && data_sp->GetByteSize() == header_size) { 69 DataExtractor data(data_sp, lldb::eByteOrderLittle, 4); 70 71 lldb::offset_t data_offset = 0; 72 llvm::MachO::mach_header mach_header; 73 if (ObjectFileMachO::ParseHeader(data, &data_offset, mach_header)) { 74 if (mach_header.filetype == llvm::MachO::MH_CORE) 75 process_sp = std::make_shared<ProcessMachCore>(target_sp, listener_sp, 76 *crash_file); 77 } 78 } 79 } 80 return process_sp; 81 } 82 83 bool ProcessMachCore::CanDebug(lldb::TargetSP target_sp, 84 bool plugin_specified_by_name) { 85 if (plugin_specified_by_name) 86 return true; 87 88 // For now we are just making sure the file exists for a given module 89 if (!m_core_module_sp && FileSystem::Instance().Exists(m_core_file)) { 90 // Don't add the Target's architecture to the ModuleSpec - we may be 91 // working with a core file that doesn't have the correct cpusubtype in the 92 // header but we should still try to use it - 93 // ModuleSpecList::FindMatchingModuleSpec enforces a strict arch mach. 94 ModuleSpec core_module_spec(m_core_file); 95 Status error(ModuleList::GetSharedModule(core_module_spec, m_core_module_sp, 96 NULL, NULL, NULL)); 97 98 if (m_core_module_sp) { 99 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); 100 if (core_objfile && core_objfile->GetType() == ObjectFile::eTypeCoreFile) 101 return true; 102 } 103 } 104 return false; 105 } 106 107 // ProcessMachCore constructor 108 ProcessMachCore::ProcessMachCore(lldb::TargetSP target_sp, 109 ListenerSP listener_sp, 110 const FileSpec &core_file) 111 : Process(target_sp, listener_sp), m_core_aranges(), m_core_range_infos(), 112 m_core_module_sp(), m_core_file(core_file), 113 m_dyld_addr(LLDB_INVALID_ADDRESS), 114 m_mach_kernel_addr(LLDB_INVALID_ADDRESS), m_dyld_plugin_name() {} 115 116 // Destructor 117 ProcessMachCore::~ProcessMachCore() { 118 Clear(); 119 // We need to call finalize on the process before destroying ourselves to 120 // make sure all of the broadcaster cleanup goes as planned. If we destruct 121 // this class, then Process::~Process() might have problems trying to fully 122 // destroy the broadcaster. 123 Finalize(); 124 } 125 126 // PluginInterface 127 ConstString ProcessMachCore::GetPluginName() { return GetPluginNameStatic(); } 128 129 uint32_t ProcessMachCore::GetPluginVersion() { return 1; } 130 131 bool ProcessMachCore::GetDynamicLoaderAddress(lldb::addr_t addr) { 132 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER | 133 LIBLLDB_LOG_PROCESS)); 134 llvm::MachO::mach_header header; 135 Status error; 136 if (DoReadMemory(addr, &header, sizeof(header), error) != sizeof(header)) 137 return false; 138 if (header.magic == llvm::MachO::MH_CIGAM || 139 header.magic == llvm::MachO::MH_CIGAM_64) { 140 header.magic = llvm::ByteSwap_32(header.magic); 141 header.cputype = llvm::ByteSwap_32(header.cputype); 142 header.cpusubtype = llvm::ByteSwap_32(header.cpusubtype); 143 header.filetype = llvm::ByteSwap_32(header.filetype); 144 header.ncmds = llvm::ByteSwap_32(header.ncmds); 145 header.sizeofcmds = llvm::ByteSwap_32(header.sizeofcmds); 146 header.flags = llvm::ByteSwap_32(header.flags); 147 } 148 149 // TODO: swap header if needed... 150 // printf("0x%16.16" PRIx64 ": magic = 0x%8.8x, file_type= %u\n", vaddr, 151 // header.magic, header.filetype); 152 if (header.magic == llvm::MachO::MH_MAGIC || 153 header.magic == llvm::MachO::MH_MAGIC_64) { 154 // Check MH_EXECUTABLE to see if we can find the mach image that contains 155 // the shared library list. The dynamic loader (dyld) is what contains the 156 // list for user applications, and the mach kernel contains a global that 157 // has the list of kexts to load 158 switch (header.filetype) { 159 case llvm::MachO::MH_DYLINKER: 160 // printf("0x%16.16" PRIx64 ": file_type = MH_DYLINKER\n", vaddr); 161 // Address of dyld "struct mach_header" in the core file 162 if (log) 163 log->Printf("ProcessMachCore::GetDynamicLoaderAddress found a user " 164 "process dyld binary image at 0x%" PRIx64, 165 addr); 166 m_dyld_addr = addr; 167 return true; 168 169 case llvm::MachO::MH_EXECUTE: 170 // printf("0x%16.16" PRIx64 ": file_type = MH_EXECUTE\n", vaddr); 171 // Check MH_EXECUTABLE file types to see if the dynamic link object flag 172 // is NOT set. If it isn't, then we have a mach_kernel. 173 if ((header.flags & llvm::MachO::MH_DYLDLINK) == 0) { 174 if (log) 175 log->Printf("ProcessMachCore::GetDynamicLoaderAddress found a mach " 176 "kernel binary image at 0x%" PRIx64, 177 addr); 178 // Address of the mach kernel "struct mach_header" in the core file. 179 m_mach_kernel_addr = addr; 180 return true; 181 } 182 break; 183 } 184 } 185 return false; 186 } 187 188 // Process Control 189 Status ProcessMachCore::DoLoadCore() { 190 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER | 191 LIBLLDB_LOG_PROCESS)); 192 Status error; 193 if (!m_core_module_sp) { 194 error.SetErrorString("invalid core module"); 195 return error; 196 } 197 198 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); 199 if (core_objfile == NULL) { 200 error.SetErrorString("invalid core object file"); 201 return error; 202 } 203 204 if (core_objfile->GetNumThreadContexts() == 0) { 205 error.SetErrorString("core file doesn't contain any LC_THREAD load " 206 "commands, or the LC_THREAD architecture is not " 207 "supported in this lldb"); 208 return error; 209 } 210 211 SectionList *section_list = core_objfile->GetSectionList(); 212 if (section_list == NULL) { 213 error.SetErrorString("core file has no sections"); 214 return error; 215 } 216 217 const uint32_t num_sections = section_list->GetNumSections(0); 218 if (num_sections == 0) { 219 error.SetErrorString("core file has no sections"); 220 return error; 221 } 222 223 SetCanJIT(false); 224 225 llvm::MachO::mach_header header; 226 DataExtractor data(&header, sizeof(header), 227 m_core_module_sp->GetArchitecture().GetByteOrder(), 228 m_core_module_sp->GetArchitecture().GetAddressByteSize()); 229 230 bool ranges_are_sorted = true; 231 addr_t vm_addr = 0; 232 for (uint32_t i = 0; i < num_sections; ++i) { 233 Section *section = section_list->GetSectionAtIndex(i).get(); 234 if (section) { 235 lldb::addr_t section_vm_addr = section->GetFileAddress(); 236 FileRange file_range(section->GetFileOffset(), section->GetFileSize()); 237 VMRangeToFileOffset::Entry range_entry( 238 section_vm_addr, section->GetByteSize(), file_range); 239 240 if (vm_addr > section_vm_addr) 241 ranges_are_sorted = false; 242 vm_addr = section->GetFileAddress(); 243 VMRangeToFileOffset::Entry *last_entry = m_core_aranges.Back(); 244 // printf ("LC_SEGMENT[%u] arange=[0x%16.16" PRIx64 " - 245 // 0x%16.16" PRIx64 "), frange=[0x%8.8x - 0x%8.8x)\n", 246 // i, 247 // range_entry.GetRangeBase(), 248 // range_entry.GetRangeEnd(), 249 // range_entry.data.GetRangeBase(), 250 // range_entry.data.GetRangeEnd()); 251 252 if (last_entry && 253 last_entry->GetRangeEnd() == range_entry.GetRangeBase() && 254 last_entry->data.GetRangeEnd() == range_entry.data.GetRangeBase()) { 255 last_entry->SetRangeEnd(range_entry.GetRangeEnd()); 256 last_entry->data.SetRangeEnd(range_entry.data.GetRangeEnd()); 257 // puts("combine"); 258 } else { 259 m_core_aranges.Append(range_entry); 260 } 261 // Some core files don't fill in the permissions correctly. If that is 262 // the case assume read + execute so clients don't think the memory is 263 // not readable, or executable. The memory isn't writable since this 264 // plug-in doesn't implement DoWriteMemory. 265 uint32_t permissions = section->GetPermissions(); 266 if (permissions == 0) 267 permissions = lldb::ePermissionsReadable | lldb::ePermissionsExecutable; 268 m_core_range_infos.Append(VMRangeToPermissions::Entry( 269 section_vm_addr, section->GetByteSize(), permissions)); 270 } 271 } 272 if (!ranges_are_sorted) { 273 m_core_aranges.Sort(); 274 m_core_range_infos.Sort(); 275 } 276 277 278 bool found_main_binary_definitively = false; 279 280 addr_t objfile_binary_addr; 281 UUID objfile_binary_uuid; 282 if (core_objfile->GetCorefileMainBinaryInfo (objfile_binary_addr, objfile_binary_uuid)) 283 { 284 if (objfile_binary_addr != LLDB_INVALID_ADDRESS) 285 { 286 m_mach_kernel_addr = objfile_binary_addr; 287 found_main_binary_definitively = true; 288 if (log) 289 log->Printf ("ProcessMachCore::DoLoadCore: using kernel address 0x%" PRIx64 290 " from LC_NOTE 'main bin spec' load command.", m_mach_kernel_addr); 291 } 292 } 293 294 // This checks for the presence of an LC_IDENT string in a core file; 295 // LC_IDENT is very obsolete and should not be used in new code, but if the 296 // load command is present, let's use the contents. 297 std::string corefile_identifier = core_objfile->GetIdentifierString(); 298 if (!found_main_binary_definitively && 299 corefile_identifier.find("Darwin Kernel") != std::string::npos) { 300 UUID uuid; 301 addr_t addr = LLDB_INVALID_ADDRESS; 302 if (corefile_identifier.find("UUID=") != std::string::npos) { 303 size_t p = corefile_identifier.find("UUID=") + strlen("UUID="); 304 std::string uuid_str = corefile_identifier.substr(p, 36); 305 uuid.SetFromStringRef(uuid_str); 306 } 307 if (corefile_identifier.find("stext=") != std::string::npos) { 308 size_t p = corefile_identifier.find("stext=") + strlen("stext="); 309 if (corefile_identifier[p] == '0' && corefile_identifier[p + 1] == 'x') { 310 errno = 0; 311 addr = ::strtoul(corefile_identifier.c_str() + p, NULL, 16); 312 if (errno != 0 || addr == 0) 313 addr = LLDB_INVALID_ADDRESS; 314 } 315 } 316 if (uuid.IsValid() && addr != LLDB_INVALID_ADDRESS) { 317 m_mach_kernel_addr = addr; 318 found_main_binary_definitively = true; 319 if (log) 320 log->Printf( 321 "ProcessMachCore::DoLoadCore: Using the kernel address 0x%" PRIx64 322 " from LC_IDENT/LC_NOTE 'kern ver str' string: '%s'", 323 addr, corefile_identifier.c_str()); 324 } 325 } 326 327 if (!found_main_binary_definitively && 328 (m_dyld_addr == LLDB_INVALID_ADDRESS || 329 m_mach_kernel_addr == LLDB_INVALID_ADDRESS)) { 330 // We need to locate the main executable in the memory ranges we have in 331 // the core file. We need to search for both a user-process dyld binary 332 // and a kernel binary in memory; we must look at all the pages in the 333 // binary so we don't miss one or the other. Step through all memory 334 // segments searching for a kernel binary and for a user process dyld -- 335 // we'll decide which to prefer later if both are present. 336 337 const size_t num_core_aranges = m_core_aranges.GetSize(); 338 for (size_t i = 0; i < num_core_aranges; ++i) { 339 const VMRangeToFileOffset::Entry *entry = 340 m_core_aranges.GetEntryAtIndex(i); 341 lldb::addr_t section_vm_addr_start = entry->GetRangeBase(); 342 lldb::addr_t section_vm_addr_end = entry->GetRangeEnd(); 343 for (lldb::addr_t section_vm_addr = section_vm_addr_start; 344 section_vm_addr < section_vm_addr_end; section_vm_addr += 0x1000) { 345 GetDynamicLoaderAddress(section_vm_addr); 346 } 347 } 348 } 349 350 if (!found_main_binary_definitively && 351 m_mach_kernel_addr != LLDB_INVALID_ADDRESS) { 352 // In the case of multiple kernel images found in the core file via 353 // exhaustive search, we may not pick the correct one. See if the 354 // DynamicLoaderDarwinKernel's search heuristics might identify the correct 355 // one. Most of the time, I expect the address from SearchForDarwinKernel() 356 // will be the same as the address we found via exhaustive search. 357 358 if (!GetTarget().GetArchitecture().IsValid() && m_core_module_sp.get()) { 359 GetTarget().SetArchitecture(m_core_module_sp->GetArchitecture()); 360 } 361 362 // SearchForDarwinKernel will end up calling back into this this class in 363 // the GetImageInfoAddress method which will give it the 364 // m_mach_kernel_addr/m_dyld_addr it already has. Save that aside and set 365 // m_mach_kernel_addr/m_dyld_addr to an invalid address temporarily so 366 // DynamicLoaderDarwinKernel does a real search for the kernel using its 367 // own heuristics. 368 369 addr_t saved_mach_kernel_addr = m_mach_kernel_addr; 370 addr_t saved_user_dyld_addr = m_dyld_addr; 371 m_mach_kernel_addr = LLDB_INVALID_ADDRESS; 372 m_dyld_addr = LLDB_INVALID_ADDRESS; 373 374 addr_t better_kernel_address = 375 DynamicLoaderDarwinKernel::SearchForDarwinKernel(this); 376 377 m_mach_kernel_addr = saved_mach_kernel_addr; 378 m_dyld_addr = saved_user_dyld_addr; 379 380 if (better_kernel_address != LLDB_INVALID_ADDRESS) { 381 if (log) 382 log->Printf("ProcessMachCore::DoLoadCore: Using the kernel address " 383 "from DynamicLoaderDarwinKernel"); 384 m_mach_kernel_addr = better_kernel_address; 385 } 386 } 387 388 // If we found both a user-process dyld and a kernel binary, we need to 389 // decide which to prefer. 390 if (GetCorefilePreference() == eKernelCorefile) { 391 if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) { 392 if (log) 393 log->Printf("ProcessMachCore::DoLoadCore: Using kernel corefile image " 394 "at 0x%" PRIx64, 395 m_mach_kernel_addr); 396 m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); 397 } else if (m_dyld_addr != LLDB_INVALID_ADDRESS) { 398 if (log) 399 log->Printf("ProcessMachCore::DoLoadCore: Using user process dyld " 400 "image at 0x%" PRIx64, 401 m_dyld_addr); 402 m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic(); 403 } 404 } else { 405 if (m_dyld_addr != LLDB_INVALID_ADDRESS) { 406 if (log) 407 log->Printf("ProcessMachCore::DoLoadCore: Using user process dyld " 408 "image at 0x%" PRIx64, 409 m_dyld_addr); 410 m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic(); 411 } else if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) { 412 if (log) 413 log->Printf("ProcessMachCore::DoLoadCore: Using kernel corefile image " 414 "at 0x%" PRIx64, 415 m_mach_kernel_addr); 416 m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); 417 } 418 } 419 420 if (m_dyld_plugin_name != DynamicLoaderMacOSXDYLD::GetPluginNameStatic()) { 421 // For non-user process core files, the permissions on the core file 422 // segments are usually meaningless, they may be just "read", because we're 423 // dealing with kernel coredumps or early startup coredumps and the dumper 424 // is grabbing pages of memory without knowing what they are. If they 425 // aren't marked as "exeuctable", that can break the unwinder which will 426 // check a pc value to see if it is in an executable segment and stop the 427 // backtrace early if it is not ("executable" and "unknown" would both be 428 // fine, but "not executable" will break the unwinder). 429 size_t core_range_infos_size = m_core_range_infos.GetSize(); 430 for (size_t i = 0; i < core_range_infos_size; i++) { 431 VMRangeToPermissions::Entry *ent = 432 m_core_range_infos.GetMutableEntryAtIndex(i); 433 ent->data = lldb::ePermissionsReadable | lldb::ePermissionsExecutable; 434 } 435 } 436 437 // Even if the architecture is set in the target, we need to override it to 438 // match the core file which is always single arch. 439 ArchSpec arch(m_core_module_sp->GetArchitecture()); 440 if (arch.GetCore() == ArchSpec::eCore_x86_32_i486) { 441 arch = Platform::GetAugmentedArchSpec(GetTarget().GetPlatform().get(), "i386"); 442 } 443 if (arch.IsValid()) 444 GetTarget().SetArchitecture(arch); 445 446 return error; 447 } 448 449 lldb_private::DynamicLoader *ProcessMachCore::GetDynamicLoader() { 450 if (m_dyld_up.get() == NULL) 451 m_dyld_up.reset(DynamicLoader::FindPlugin( 452 this, 453 m_dyld_plugin_name.IsEmpty() ? NULL : m_dyld_plugin_name.GetCString())); 454 return m_dyld_up.get(); 455 } 456 457 bool ProcessMachCore::UpdateThreadList(ThreadList &old_thread_list, 458 ThreadList &new_thread_list) { 459 if (old_thread_list.GetSize(false) == 0) { 460 // Make up the thread the first time this is called so we can setup our one 461 // and only core thread state. 462 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); 463 464 if (core_objfile) { 465 const uint32_t num_threads = core_objfile->GetNumThreadContexts(); 466 for (lldb::tid_t tid = 0; tid < num_threads; ++tid) { 467 ThreadSP thread_sp(new ThreadMachCore(*this, tid)); 468 new_thread_list.AddThread(thread_sp); 469 } 470 } 471 } else { 472 const uint32_t num_threads = old_thread_list.GetSize(false); 473 for (uint32_t i = 0; i < num_threads; ++i) 474 new_thread_list.AddThread(old_thread_list.GetThreadAtIndex(i, false)); 475 } 476 return new_thread_list.GetSize(false) > 0; 477 } 478 479 void ProcessMachCore::RefreshStateAfterStop() { 480 // Let all threads recover from stopping and do any clean up based on the 481 // previous thread state (if any). 482 m_thread_list.RefreshStateAfterStop(); 483 // SetThreadStopInfo (m_last_stop_packet); 484 } 485 486 Status ProcessMachCore::DoDestroy() { return Status(); } 487 488 // Process Queries 489 490 bool ProcessMachCore::IsAlive() { return true; } 491 492 bool ProcessMachCore::WarnBeforeDetach() const { return false; } 493 494 // Process Memory 495 size_t ProcessMachCore::ReadMemory(addr_t addr, void *buf, size_t size, 496 Status &error) { 497 // Don't allow the caching that lldb_private::Process::ReadMemory does since 498 // in core files we have it all cached our our core file anyway. 499 return DoReadMemory(addr, buf, size, error); 500 } 501 502 size_t ProcessMachCore::DoReadMemory(addr_t addr, void *buf, size_t size, 503 Status &error) { 504 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); 505 size_t bytes_read = 0; 506 507 if (core_objfile) { 508 // Segments are not always contiguous in mach-o core files. We have core 509 // files that have segments like: 510 // Address Size File off File size 511 // ---------- ---------- ---------- ---------- 512 // LC_SEGMENT 0x000f6000 0x00001000 0x1d509ee8 0x00001000 --- --- 0 513 // 0x00000000 __TEXT LC_SEGMENT 0x0f600000 0x00100000 0x1d50aee8 0x00100000 514 // --- --- 0 0x00000000 __TEXT LC_SEGMENT 0x000f7000 0x00001000 515 // 0x1d60aee8 0x00001000 --- --- 0 0x00000000 __TEXT 516 // 517 // Any if the user executes the following command: 518 // 519 // (lldb) mem read 0xf6ff0 520 // 521 // We would attempt to read 32 bytes from 0xf6ff0 but would only get 16 522 // unless we loop through consecutive memory ranges that are contiguous in 523 // the address space, but not in the file data. 524 while (bytes_read < size) { 525 const addr_t curr_addr = addr + bytes_read; 526 const VMRangeToFileOffset::Entry *core_memory_entry = 527 m_core_aranges.FindEntryThatContains(curr_addr); 528 529 if (core_memory_entry) { 530 const addr_t offset = curr_addr - core_memory_entry->GetRangeBase(); 531 const addr_t bytes_left = core_memory_entry->GetRangeEnd() - curr_addr; 532 const size_t bytes_to_read = 533 std::min(size - bytes_read, (size_t)bytes_left); 534 const size_t curr_bytes_read = core_objfile->CopyData( 535 core_memory_entry->data.GetRangeBase() + offset, bytes_to_read, 536 (char *)buf + bytes_read); 537 if (curr_bytes_read == 0) 538 break; 539 bytes_read += curr_bytes_read; 540 } else { 541 // Only set the error if we didn't read any bytes 542 if (bytes_read == 0) 543 error.SetErrorStringWithFormat( 544 "core file does not contain 0x%" PRIx64, curr_addr); 545 break; 546 } 547 } 548 } 549 550 return bytes_read; 551 } 552 553 Status ProcessMachCore::GetMemoryRegionInfo(addr_t load_addr, 554 MemoryRegionInfo ®ion_info) { 555 region_info.Clear(); 556 const VMRangeToPermissions::Entry *permission_entry = 557 m_core_range_infos.FindEntryThatContainsOrFollows(load_addr); 558 if (permission_entry) { 559 if (permission_entry->Contains(load_addr)) { 560 region_info.GetRange().SetRangeBase(permission_entry->GetRangeBase()); 561 region_info.GetRange().SetRangeEnd(permission_entry->GetRangeEnd()); 562 const Flags permissions(permission_entry->data); 563 region_info.SetReadable(permissions.Test(ePermissionsReadable) 564 ? MemoryRegionInfo::eYes 565 : MemoryRegionInfo::eNo); 566 region_info.SetWritable(permissions.Test(ePermissionsWritable) 567 ? MemoryRegionInfo::eYes 568 : MemoryRegionInfo::eNo); 569 region_info.SetExecutable(permissions.Test(ePermissionsExecutable) 570 ? MemoryRegionInfo::eYes 571 : MemoryRegionInfo::eNo); 572 region_info.SetMapped(MemoryRegionInfo::eYes); 573 } else if (load_addr < permission_entry->GetRangeBase()) { 574 region_info.GetRange().SetRangeBase(load_addr); 575 region_info.GetRange().SetRangeEnd(permission_entry->GetRangeBase()); 576 region_info.SetReadable(MemoryRegionInfo::eNo); 577 region_info.SetWritable(MemoryRegionInfo::eNo); 578 region_info.SetExecutable(MemoryRegionInfo::eNo); 579 region_info.SetMapped(MemoryRegionInfo::eNo); 580 } 581 return Status(); 582 } 583 584 region_info.GetRange().SetRangeBase(load_addr); 585 region_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 586 region_info.SetReadable(MemoryRegionInfo::eNo); 587 region_info.SetWritable(MemoryRegionInfo::eNo); 588 region_info.SetExecutable(MemoryRegionInfo::eNo); 589 region_info.SetMapped(MemoryRegionInfo::eNo); 590 return Status(); 591 } 592 593 void ProcessMachCore::Clear() { m_thread_list.Clear(); } 594 595 void ProcessMachCore::Initialize() { 596 static llvm::once_flag g_once_flag; 597 598 llvm::call_once(g_once_flag, []() { 599 PluginManager::RegisterPlugin(GetPluginNameStatic(), 600 GetPluginDescriptionStatic(), CreateInstance); 601 }); 602 } 603 604 addr_t ProcessMachCore::GetImageInfoAddress() { 605 // If we found both a user-process dyld and a kernel binary, we need to 606 // decide which to prefer. 607 if (GetCorefilePreference() == eKernelCorefile) { 608 if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) { 609 return m_mach_kernel_addr; 610 } 611 return m_dyld_addr; 612 } else { 613 if (m_dyld_addr != LLDB_INVALID_ADDRESS) { 614 return m_dyld_addr; 615 } 616 return m_mach_kernel_addr; 617 } 618 } 619 620 lldb_private::ObjectFile *ProcessMachCore::GetCoreObjectFile() { 621 return m_core_module_sp->GetObjectFile(); 622 } 623