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