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