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 Error 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 Error 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 Error ProcessMachCore::DoLoadCore() { 204 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER | 205 LIBLLDB_LOG_PROCESS)); 206 Error 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 // This checks for the presence of an LC_IDENT string in a core file; 298 // LC_IDENT is very obsolete and should not be used in new code, but 299 // if the load command is present, let's use the contents. 300 std::string corefile_identifier = core_objfile->GetIdentifierString(); 301 if (corefile_identifier.find("Darwin Kernel") != std::string::npos) { 302 UUID uuid; 303 addr_t addr = LLDB_INVALID_ADDRESS; 304 if (corefile_identifier.find("UUID=") != std::string::npos) { 305 size_t p = corefile_identifier.find("UUID=") + strlen("UUID="); 306 std::string uuid_str = corefile_identifier.substr(p, 36); 307 uuid.SetFromCString(uuid_str.c_str()); 308 } 309 if (corefile_identifier.find("stext=") != std::string::npos) { 310 size_t p = corefile_identifier.find("stext=") + strlen("stext="); 311 if (corefile_identifier[p] == '0' && corefile_identifier[p + 1] == 'x') { 312 errno = 0; 313 addr = ::strtoul(corefile_identifier.c_str() + p, NULL, 16); 314 if (errno != 0 || addr == 0) 315 addr = LLDB_INVALID_ADDRESS; 316 } 317 } 318 if (uuid.IsValid() && addr != LLDB_INVALID_ADDRESS) { 319 m_mach_kernel_addr = addr; 320 found_main_binary_definitively = true; 321 if (log) 322 log->Printf("ProcessMachCore::DoLoadCore: Using the kernel address 0x%" PRIx64 323 "from LC_IDENT string '%s'", addr, corefile_identifier.c_str()); 324 } 325 } 326 327 if (found_main_binary_definitively == false && 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 331 // we have in the core file. We need to search for both a user-process dyld 332 // binary 333 // and a kernel binary in memory; we must look at all the pages in the 334 // binary so 335 // we don't miss one or the other. Step through all memory segments 336 // searching for 337 // a kernel binary and for a user process dyld -- we'll decide which to 338 // prefer 339 // later if both are present. 340 341 const size_t num_core_aranges = m_core_aranges.GetSize(); 342 for (size_t i = 0; i < num_core_aranges; ++i) { 343 const VMRangeToFileOffset::Entry *entry = 344 m_core_aranges.GetEntryAtIndex(i); 345 lldb::addr_t section_vm_addr_start = entry->GetRangeBase(); 346 lldb::addr_t section_vm_addr_end = entry->GetRangeEnd(); 347 for (lldb::addr_t section_vm_addr = section_vm_addr_start; 348 section_vm_addr < section_vm_addr_end; section_vm_addr += 0x1000) { 349 GetDynamicLoaderAddress(section_vm_addr); 350 } 351 } 352 } 353 354 if (found_main_binary_definitively == false 355 && m_mach_kernel_addr != LLDB_INVALID_ADDRESS) { 356 // In the case of multiple kernel images found in the core file via 357 // exhaustive 358 // search, we may not pick the correct one. See if the 359 // DynamicLoaderDarwinKernel's 360 // search heuristics might identify the correct one. 361 // Most of the time, I expect the address from SearchForDarwinKernel() will 362 // be the 363 // same as the address we found via exhaustive search. 364 365 if (GetTarget().GetArchitecture().IsValid() == false && 366 m_core_module_sp.get()) { 367 GetTarget().SetArchitecture(m_core_module_sp->GetArchitecture()); 368 } 369 370 // SearchForDarwinKernel will end up calling back into this this class in 371 // the GetImageInfoAddress 372 // method which will give it the m_mach_kernel_addr/m_dyld_addr it already 373 // has. Save that aside 374 // and set m_mach_kernel_addr/m_dyld_addr to an invalid address temporarily 375 // so 376 // DynamicLoaderDarwinKernel does a real search for the kernel using its own 377 // 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 decide 399 // 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 433 // meaningless, they may be just "read", because we're dealing with kernel 434 // coredumps or 435 // early startup coredumps and the dumper is grabbing pages of memory 436 // without knowing 437 // what they are. If they aren't marked as "exeuctable", that can break the 438 // unwinder 439 // which will check a pc value to see if it is in an executable segment and 440 // stop the 441 // backtrace early if it is not ("executable" and "unknown" would both be 442 // fine, but 443 // "not executable" will break the unwinder). 444 size_t core_range_infos_size = m_core_range_infos.GetSize(); 445 for (size_t i = 0; i < core_range_infos_size; i++) { 446 VMRangeToPermissions::Entry *ent = 447 m_core_range_infos.GetMutableEntryAtIndex(i); 448 ent->data = lldb::ePermissionsReadable | lldb::ePermissionsExecutable; 449 } 450 } 451 452 // Even if the architecture is set in the target, we need to override 453 // it to match the core file which is always single arch. 454 ArchSpec arch(m_core_module_sp->GetArchitecture()); 455 if (arch.GetCore() == ArchSpec::eCore_x86_32_i486) { 456 arch.SetTriple("i386", GetTarget().GetPlatform().get()); 457 } 458 if (arch.IsValid()) 459 GetTarget().SetArchitecture(arch); 460 461 return error; 462 } 463 464 lldb_private::DynamicLoader *ProcessMachCore::GetDynamicLoader() { 465 if (m_dyld_ap.get() == NULL) 466 m_dyld_ap.reset(DynamicLoader::FindPlugin( 467 this, 468 m_dyld_plugin_name.IsEmpty() ? NULL : m_dyld_plugin_name.GetCString())); 469 return m_dyld_ap.get(); 470 } 471 472 bool ProcessMachCore::UpdateThreadList(ThreadList &old_thread_list, 473 ThreadList &new_thread_list) { 474 if (old_thread_list.GetSize(false) == 0) { 475 // Make up the thread the first time this is called so we can setup our one 476 // and only 477 // core thread state. 478 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); 479 480 if (core_objfile) { 481 const uint32_t num_threads = core_objfile->GetNumThreadContexts(); 482 for (lldb::tid_t tid = 0; tid < num_threads; ++tid) { 483 ThreadSP thread_sp(new ThreadMachCore(*this, tid)); 484 new_thread_list.AddThread(thread_sp); 485 } 486 } 487 } else { 488 const uint32_t num_threads = old_thread_list.GetSize(false); 489 for (uint32_t i = 0; i < num_threads; ++i) 490 new_thread_list.AddThread(old_thread_list.GetThreadAtIndex(i, false)); 491 } 492 return new_thread_list.GetSize(false) > 0; 493 } 494 495 void ProcessMachCore::RefreshStateAfterStop() { 496 // Let all threads recover from stopping and do any clean up based 497 // on the previous thread state (if any). 498 m_thread_list.RefreshStateAfterStop(); 499 // SetThreadStopInfo (m_last_stop_packet); 500 } 501 502 Error ProcessMachCore::DoDestroy() { return Error(); } 503 504 //------------------------------------------------------------------ 505 // Process Queries 506 //------------------------------------------------------------------ 507 508 bool ProcessMachCore::IsAlive() { return true; } 509 510 bool ProcessMachCore::WarnBeforeDetach() const { return false; } 511 512 //------------------------------------------------------------------ 513 // Process Memory 514 //------------------------------------------------------------------ 515 size_t ProcessMachCore::ReadMemory(addr_t addr, void *buf, size_t size, 516 Error &error) { 517 // Don't allow the caching that lldb_private::Process::ReadMemory does 518 // since in core files we have it all cached our our core file anyway. 519 return DoReadMemory(addr, buf, size, error); 520 } 521 522 size_t ProcessMachCore::DoReadMemory(addr_t addr, void *buf, size_t size, 523 Error &error) { 524 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); 525 size_t bytes_read = 0; 526 527 if (core_objfile) { 528 //---------------------------------------------------------------------- 529 // Segments are not always contiguous in mach-o core files. We have core 530 // files that have segments like: 531 // Address Size File off File size 532 // ---------- ---------- ---------- ---------- 533 // LC_SEGMENT 0x000f6000 0x00001000 0x1d509ee8 0x00001000 --- --- 0 534 // 0x00000000 __TEXT 535 // LC_SEGMENT 0x0f600000 0x00100000 0x1d50aee8 0x00100000 --- --- 0 536 // 0x00000000 __TEXT 537 // LC_SEGMENT 0x000f7000 0x00001000 0x1d60aee8 0x00001000 --- --- 0 538 // 0x00000000 __TEXT 539 // 540 // Any if the user executes the following command: 541 // 542 // (lldb) mem read 0xf6ff0 543 // 544 // We would attempt to read 32 bytes from 0xf6ff0 but would only 545 // get 16 unless we loop through consecutive memory ranges that are 546 // contiguous in the address space, but not in the file data. 547 //---------------------------------------------------------------------- 548 while (bytes_read < size) { 549 const addr_t curr_addr = addr + bytes_read; 550 const VMRangeToFileOffset::Entry *core_memory_entry = 551 m_core_aranges.FindEntryThatContains(curr_addr); 552 553 if (core_memory_entry) { 554 const addr_t offset = curr_addr - core_memory_entry->GetRangeBase(); 555 const addr_t bytes_left = core_memory_entry->GetRangeEnd() - curr_addr; 556 const size_t bytes_to_read = 557 std::min(size - bytes_read, (size_t)bytes_left); 558 const size_t curr_bytes_read = core_objfile->CopyData( 559 core_memory_entry->data.GetRangeBase() + offset, bytes_to_read, 560 (char *)buf + bytes_read); 561 if (curr_bytes_read == 0) 562 break; 563 bytes_read += curr_bytes_read; 564 } else { 565 // Only set the error if we didn't read any bytes 566 if (bytes_read == 0) 567 error.SetErrorStringWithFormat( 568 "core file does not contain 0x%" PRIx64, curr_addr); 569 break; 570 } 571 } 572 } 573 574 return bytes_read; 575 } 576 577 Error ProcessMachCore::GetMemoryRegionInfo(addr_t load_addr, 578 MemoryRegionInfo ®ion_info) { 579 region_info.Clear(); 580 const VMRangeToPermissions::Entry *permission_entry = 581 m_core_range_infos.FindEntryThatContainsOrFollows(load_addr); 582 if (permission_entry) { 583 if (permission_entry->Contains(load_addr)) { 584 region_info.GetRange().SetRangeBase(permission_entry->GetRangeBase()); 585 region_info.GetRange().SetRangeEnd(permission_entry->GetRangeEnd()); 586 const Flags permissions(permission_entry->data); 587 region_info.SetReadable(permissions.Test(ePermissionsReadable) 588 ? MemoryRegionInfo::eYes 589 : MemoryRegionInfo::eNo); 590 region_info.SetWritable(permissions.Test(ePermissionsWritable) 591 ? MemoryRegionInfo::eYes 592 : MemoryRegionInfo::eNo); 593 region_info.SetExecutable(permissions.Test(ePermissionsExecutable) 594 ? MemoryRegionInfo::eYes 595 : MemoryRegionInfo::eNo); 596 region_info.SetMapped(MemoryRegionInfo::eYes); 597 } else if (load_addr < permission_entry->GetRangeBase()) { 598 region_info.GetRange().SetRangeBase(load_addr); 599 region_info.GetRange().SetRangeEnd(permission_entry->GetRangeBase()); 600 region_info.SetReadable(MemoryRegionInfo::eNo); 601 region_info.SetWritable(MemoryRegionInfo::eNo); 602 region_info.SetExecutable(MemoryRegionInfo::eNo); 603 region_info.SetMapped(MemoryRegionInfo::eNo); 604 } 605 return Error(); 606 } 607 608 region_info.GetRange().SetRangeBase(load_addr); 609 region_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 610 region_info.SetReadable(MemoryRegionInfo::eNo); 611 region_info.SetWritable(MemoryRegionInfo::eNo); 612 region_info.SetExecutable(MemoryRegionInfo::eNo); 613 region_info.SetMapped(MemoryRegionInfo::eNo); 614 return Error(); 615 } 616 617 void ProcessMachCore::Clear() { m_thread_list.Clear(); } 618 619 void ProcessMachCore::Initialize() { 620 static llvm::once_flag g_once_flag; 621 622 llvm::call_once(g_once_flag, []() { 623 PluginManager::RegisterPlugin(GetPluginNameStatic(), 624 GetPluginDescriptionStatic(), CreateInstance); 625 }); 626 } 627 628 addr_t ProcessMachCore::GetImageInfoAddress() { 629 // If we found both a user-process dyld and a kernel binary, we need to decide 630 // which to prefer. 631 if (GetCorefilePreference() == eKernelCorefile) { 632 if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) { 633 return m_mach_kernel_addr; 634 } 635 return m_dyld_addr; 636 } else { 637 if (m_dyld_addr != LLDB_INVALID_ADDRESS) { 638 return m_dyld_addr; 639 } 640 return m_mach_kernel_addr; 641 } 642 } 643 644 lldb_private::ObjectFile *ProcessMachCore::GetCoreObjectFile() { 645 return m_core_module_sp->GetObjectFile(); 646 } 647