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