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 <cerrno> 10 #include <cstdlib> 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/SectionLoadList.h" 25 #include "lldb/Target/Target.h" 26 #include "lldb/Target/Thread.h" 27 #include "lldb/Utility/DataBuffer.h" 28 #include "lldb/Utility/Log.h" 29 #include "lldb/Utility/State.h" 30 31 #include "ProcessMachCore.h" 32 #include "Plugins/Process/Utility/StopInfoMachException.h" 33 #include "ThreadMachCore.h" 34 35 // Needed for the plug-in names for the dynamic loaders. 36 #include "lldb/Host/SafeMachO.h" 37 38 #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h" 39 #include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h" 40 #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h" 41 #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h" 42 43 #include <memory> 44 #include <mutex> 45 46 using namespace lldb; 47 using namespace lldb_private; 48 49 LLDB_PLUGIN_DEFINE(ProcessMachCore) 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 bool can_connect) { 68 lldb::ProcessSP process_sp; 69 if (crash_file && !can_connect) { 70 const size_t header_size = sizeof(llvm::MachO::mach_header); 71 auto data_sp = FileSystem::Instance().CreateDataBuffer( 72 crash_file->GetPath(), header_size, 0); 73 if (data_sp && data_sp->GetByteSize() == header_size) { 74 DataExtractor data(data_sp, lldb::eByteOrderLittle, 4); 75 76 lldb::offset_t data_offset = 0; 77 llvm::MachO::mach_header mach_header; 78 if (ObjectFileMachO::ParseHeader(data, &data_offset, mach_header)) { 79 if (mach_header.filetype == llvm::MachO::MH_CORE) 80 process_sp = std::make_shared<ProcessMachCore>(target_sp, listener_sp, 81 *crash_file); 82 } 83 } 84 } 85 return process_sp; 86 } 87 88 bool ProcessMachCore::CanDebug(lldb::TargetSP target_sp, 89 bool plugin_specified_by_name) { 90 if (plugin_specified_by_name) 91 return true; 92 93 // For now we are just making sure the file exists for a given module 94 if (!m_core_module_sp && FileSystem::Instance().Exists(m_core_file)) { 95 // Don't add the Target's architecture to the ModuleSpec - we may be 96 // working with a core file that doesn't have the correct cpusubtype in the 97 // header but we should still try to use it - 98 // ModuleSpecList::FindMatchingModuleSpec 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 nullptr, nullptr, nullptr)); 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 // ProcessMachCore constructor 113 ProcessMachCore::ProcessMachCore(lldb::TargetSP target_sp, 114 ListenerSP listener_sp, 115 const FileSpec &core_file) 116 : PostMortemProcess(target_sp, listener_sp), m_core_aranges(), 117 m_core_range_infos(), 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 // Destructor 122 ProcessMachCore::~ProcessMachCore() { 123 Clear(); 124 // We need to call finalize on the process before destroying ourselves to 125 // make sure all of the broadcaster cleanup goes as planned. If we destruct 126 // this class, then Process::~Process() might have problems trying to fully 127 // destroy the broadcaster. 128 Finalize(); 129 } 130 131 // PluginInterface 132 ConstString ProcessMachCore::GetPluginName() { return GetPluginNameStatic(); } 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 // We have a hint about a binary -- a UUID, possibly a load address. 192 // Try to load a file with that UUID into lldb, and if we have a load 193 // address, set it correctly. Else assume that the binary was loaded 194 // with no slide. 195 static bool load_standalone_binary(UUID uuid, addr_t addr, Target &target) { 196 if (uuid.IsValid()) { 197 ModuleSpec module_spec; 198 module_spec.GetUUID() = uuid; 199 200 // Look up UUID in global module cache before attempting 201 // dsymForUUID-like action. 202 ModuleSP module_sp; 203 Status error = ModuleList::GetSharedModule(module_spec, module_sp, nullptr, 204 nullptr, nullptr); 205 206 if (!module_sp.get()) { 207 // Force a a dsymForUUID lookup, if that tool is available. 208 if (!module_spec.GetSymbolFileSpec()) 209 Symbols::DownloadObjectAndSymbolFile(module_spec, true); 210 211 if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) { 212 module_sp = std::make_shared<Module>(module_spec); 213 } 214 } 215 216 if (module_sp.get() && module_sp->GetObjectFile()) { 217 target.SetArchitecture(module_sp->GetObjectFile()->GetArchitecture()); 218 target.GetImages().AppendIfNeeded(module_sp, false); 219 220 Address base_addr = module_sp->GetObjectFile()->GetBaseAddress(); 221 addr_t slide = 0; 222 if (addr != LLDB_INVALID_ADDRESS && base_addr.IsValid()) { 223 addr_t file_load_addr = base_addr.GetFileAddress(); 224 slide = addr - file_load_addr; 225 } 226 bool changed = false; 227 module_sp->SetLoadAddress(target, slide, true, changed); 228 229 ModuleList added_module; 230 added_module.Append(module_sp, false); 231 target.ModulesDidLoad(added_module); 232 233 // Flush info in the process (stack frames, etc). 234 ProcessSP process_sp(target.GetProcessSP()); 235 if (process_sp) 236 process_sp->Flush(); 237 238 return true; 239 } 240 } 241 return false; 242 } 243 244 // Process Control 245 Status ProcessMachCore::DoLoadCore() { 246 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER | 247 LIBLLDB_LOG_PROCESS)); 248 Status error; 249 if (!m_core_module_sp) { 250 error.SetErrorString("invalid core module"); 251 return error; 252 } 253 254 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); 255 if (core_objfile == nullptr) { 256 error.SetErrorString("invalid core object file"); 257 return error; 258 } 259 260 if (core_objfile->GetNumThreadContexts() == 0) { 261 error.SetErrorString("core file doesn't contain any LC_THREAD load " 262 "commands, or the LC_THREAD architecture is not " 263 "supported in this lldb"); 264 return error; 265 } 266 267 SectionList *section_list = core_objfile->GetSectionList(); 268 if (section_list == nullptr) { 269 error.SetErrorString("core file has no sections"); 270 return error; 271 } 272 273 const uint32_t num_sections = section_list->GetNumSections(0); 274 if (num_sections == 0) { 275 error.SetErrorString("core file has no sections"); 276 return error; 277 } 278 279 SetCanJIT(false); 280 281 llvm::MachO::mach_header header; 282 DataExtractor data(&header, sizeof(header), 283 m_core_module_sp->GetArchitecture().GetByteOrder(), 284 m_core_module_sp->GetArchitecture().GetAddressByteSize()); 285 286 bool ranges_are_sorted = true; 287 addr_t vm_addr = 0; 288 for (uint32_t i = 0; i < num_sections; ++i) { 289 Section *section = section_list->GetSectionAtIndex(i).get(); 290 if (section) { 291 lldb::addr_t section_vm_addr = section->GetFileAddress(); 292 FileRange file_range(section->GetFileOffset(), section->GetFileSize()); 293 VMRangeToFileOffset::Entry range_entry( 294 section_vm_addr, section->GetByteSize(), file_range); 295 296 if (vm_addr > section_vm_addr) 297 ranges_are_sorted = false; 298 vm_addr = section->GetFileAddress(); 299 VMRangeToFileOffset::Entry *last_entry = m_core_aranges.Back(); 300 // printf ("LC_SEGMENT[%u] arange=[0x%16.16" PRIx64 " - 301 // 0x%16.16" PRIx64 "), frange=[0x%8.8x - 0x%8.8x)\n", 302 // i, 303 // range_entry.GetRangeBase(), 304 // range_entry.GetRangeEnd(), 305 // range_entry.data.GetRangeBase(), 306 // range_entry.data.GetRangeEnd()); 307 308 if (last_entry && 309 last_entry->GetRangeEnd() == range_entry.GetRangeBase() && 310 last_entry->data.GetRangeEnd() == range_entry.data.GetRangeBase()) { 311 last_entry->SetRangeEnd(range_entry.GetRangeEnd()); 312 last_entry->data.SetRangeEnd(range_entry.data.GetRangeEnd()); 313 // puts("combine"); 314 } else { 315 m_core_aranges.Append(range_entry); 316 } 317 // Some core files don't fill in the permissions correctly. If that is 318 // the case assume read + execute so clients don't think the memory is 319 // not readable, or executable. The memory isn't writable since this 320 // plug-in doesn't implement DoWriteMemory. 321 uint32_t permissions = section->GetPermissions(); 322 if (permissions == 0) 323 permissions = lldb::ePermissionsReadable | lldb::ePermissionsExecutable; 324 m_core_range_infos.Append(VMRangeToPermissions::Entry( 325 section_vm_addr, section->GetByteSize(), permissions)); 326 } 327 } 328 if (!ranges_are_sorted) { 329 m_core_aranges.Sort(); 330 m_core_range_infos.Sort(); 331 } 332 333 bool found_main_binary_definitively = false; 334 335 addr_t objfile_binary_addr; 336 UUID objfile_binary_uuid; 337 ObjectFile::BinaryType type; 338 if (core_objfile->GetCorefileMainBinaryInfo(objfile_binary_addr, 339 objfile_binary_uuid, type)) { 340 if (log) { 341 log->Printf( 342 "ProcessMachCore::DoLoadCore: using binary hint from 'main bin spec' " 343 "LC_NOTE with UUID %s address 0x%" PRIx64 " and type %d", 344 objfile_binary_uuid.GetAsString().c_str(), objfile_binary_addr, type); 345 } 346 if (objfile_binary_addr != LLDB_INVALID_ADDRESS) { 347 if (type == ObjectFile::eBinaryTypeUser) { 348 m_dyld_addr = objfile_binary_addr; 349 m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic(); 350 found_main_binary_definitively = true; 351 } 352 if (type == ObjectFile::eBinaryTypeKernel) { 353 m_mach_kernel_addr = objfile_binary_addr; 354 m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); 355 found_main_binary_definitively = true; 356 } 357 } 358 if (!found_main_binary_definitively) { 359 // ObjectFile::eBinaryTypeStandalone, undeclared types 360 if (load_standalone_binary(objfile_binary_uuid, objfile_binary_addr, 361 GetTarget())) { 362 found_main_binary_definitively = true; 363 m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic(); 364 } 365 } 366 } 367 368 // This checks for the presence of an LC_IDENT string in a core file; 369 // LC_IDENT is very obsolete and should not be used in new code, but if the 370 // load command is present, let's use the contents. 371 UUID ident_uuid; 372 addr_t ident_binary_addr = LLDB_INVALID_ADDRESS; 373 if (!found_main_binary_definitively) { 374 std::string corefile_identifier = core_objfile->GetIdentifierString(); 375 376 // Search for UUID= and stext= strings in the identifier str. 377 if (corefile_identifier.find("UUID=") != std::string::npos) { 378 size_t p = corefile_identifier.find("UUID=") + strlen("UUID="); 379 std::string uuid_str = corefile_identifier.substr(p, 36); 380 ident_uuid.SetFromStringRef(uuid_str); 381 if (log) 382 log->Printf("Got a UUID from LC_IDENT/kern ver str LC_NOTE: %s", 383 ident_uuid.GetAsString().c_str()); 384 } 385 if (corefile_identifier.find("stext=") != std::string::npos) { 386 size_t p = corefile_identifier.find("stext=") + strlen("stext="); 387 if (corefile_identifier[p] == '0' && corefile_identifier[p + 1] == 'x') { 388 ident_binary_addr = 389 ::strtoul(corefile_identifier.c_str() + p, nullptr, 16); 390 if (log) 391 log->Printf("Got a load address from LC_IDENT/kern ver str " 392 "LC_NOTE: 0x%" PRIx64, 393 ident_binary_addr); 394 } 395 } 396 397 // Search for a "Darwin Kernel" str indicating kernel; else treat as 398 // standalone 399 if (corefile_identifier.find("Darwin Kernel") != std::string::npos && 400 ident_uuid.IsValid() && ident_binary_addr != LLDB_INVALID_ADDRESS) { 401 if (log) 402 log->Printf("ProcessMachCore::DoLoadCore: Found kernel binary via " 403 "LC_IDENT/kern ver str LC_NOTE"); 404 m_mach_kernel_addr = ident_binary_addr; 405 found_main_binary_definitively = true; 406 } else if (ident_uuid.IsValid()) { 407 if (load_standalone_binary(ident_uuid, ident_binary_addr, GetTarget())) { 408 found_main_binary_definitively = true; 409 m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic(); 410 } 411 } 412 } 413 414 // If we have a "all image infos" LC_NOTE, try to load all of the 415 // binaries listed, and set their Section load addresses in the Target. 416 if (found_main_binary_definitively == false && 417 core_objfile->LoadCoreFileImages(*this)) { 418 m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); 419 found_main_binary_definitively = true; 420 } 421 422 if (!found_main_binary_definitively && 423 (m_dyld_addr == LLDB_INVALID_ADDRESS || 424 m_mach_kernel_addr == LLDB_INVALID_ADDRESS)) { 425 // We need to locate the main executable in the memory ranges we have in 426 // the core file. We need to search for both a user-process dyld binary 427 // and a kernel binary in memory; we must look at all the pages in the 428 // binary so we don't miss one or the other. Step through all memory 429 // segments searching for a kernel binary and for a user process dyld -- 430 // we'll decide which to prefer later if both are present. 431 432 const size_t num_core_aranges = m_core_aranges.GetSize(); 433 for (size_t i = 0; i < num_core_aranges; ++i) { 434 const VMRangeToFileOffset::Entry *entry = 435 m_core_aranges.GetEntryAtIndex(i); 436 lldb::addr_t section_vm_addr_start = entry->GetRangeBase(); 437 lldb::addr_t section_vm_addr_end = entry->GetRangeEnd(); 438 for (lldb::addr_t section_vm_addr = section_vm_addr_start; 439 section_vm_addr < section_vm_addr_end; section_vm_addr += 0x1000) { 440 GetDynamicLoaderAddress(section_vm_addr); 441 } 442 } 443 } 444 445 if (!found_main_binary_definitively && 446 m_mach_kernel_addr != LLDB_INVALID_ADDRESS) { 447 // In the case of multiple kernel images found in the core file via 448 // exhaustive search, we may not pick the correct one. See if the 449 // DynamicLoaderDarwinKernel's search heuristics might identify the correct 450 // one. Most of the time, I expect the address from SearchForDarwinKernel() 451 // will be the same as the address we found via exhaustive search. 452 453 if (!GetTarget().GetArchitecture().IsValid() && m_core_module_sp.get()) { 454 GetTarget().SetArchitecture(m_core_module_sp->GetArchitecture()); 455 } 456 457 // SearchForDarwinKernel will end up calling back into this this class in 458 // the GetImageInfoAddress method which will give it the 459 // m_mach_kernel_addr/m_dyld_addr it already has. Save that aside and set 460 // m_mach_kernel_addr/m_dyld_addr to an invalid address temporarily so 461 // DynamicLoaderDarwinKernel does a real search for the kernel using its 462 // own heuristics. 463 464 addr_t saved_mach_kernel_addr = m_mach_kernel_addr; 465 addr_t saved_user_dyld_addr = m_dyld_addr; 466 m_mach_kernel_addr = LLDB_INVALID_ADDRESS; 467 m_dyld_addr = LLDB_INVALID_ADDRESS; 468 469 addr_t better_kernel_address = 470 DynamicLoaderDarwinKernel::SearchForDarwinKernel(this); 471 472 m_mach_kernel_addr = saved_mach_kernel_addr; 473 m_dyld_addr = saved_user_dyld_addr; 474 475 if (better_kernel_address != LLDB_INVALID_ADDRESS) { 476 LLDB_LOGF(log, "ProcessMachCore::DoLoadCore: Using the kernel address " 477 "from DynamicLoaderDarwinKernel"); 478 m_mach_kernel_addr = better_kernel_address; 479 } 480 } 481 482 if (m_dyld_plugin_name.IsEmpty()) { 483 // If we found both a user-process dyld and a kernel binary, we need to 484 // decide which to prefer. 485 if (GetCorefilePreference() == eKernelCorefile) { 486 if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) { 487 LLDB_LOGF(log, 488 "ProcessMachCore::DoLoadCore: Using kernel corefile image " 489 "at 0x%" PRIx64, 490 m_mach_kernel_addr); 491 m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); 492 } else if (m_dyld_addr != LLDB_INVALID_ADDRESS) { 493 LLDB_LOGF(log, 494 "ProcessMachCore::DoLoadCore: Using user process dyld " 495 "image at 0x%" PRIx64, 496 m_dyld_addr); 497 m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic(); 498 } 499 } else { 500 if (m_dyld_addr != LLDB_INVALID_ADDRESS) { 501 LLDB_LOGF(log, 502 "ProcessMachCore::DoLoadCore: Using user process dyld " 503 "image at 0x%" PRIx64, 504 m_dyld_addr); 505 m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic(); 506 } else if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) { 507 LLDB_LOGF(log, 508 "ProcessMachCore::DoLoadCore: Using kernel corefile image " 509 "at 0x%" PRIx64, 510 m_mach_kernel_addr); 511 m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); 512 } 513 } 514 } 515 516 if (m_dyld_plugin_name != DynamicLoaderMacOSXDYLD::GetPluginNameStatic()) { 517 // For non-user process core files, the permissions on the core file 518 // segments are usually meaningless, they may be just "read", because we're 519 // dealing with kernel coredumps or early startup coredumps and the dumper 520 // is grabbing pages of memory without knowing what they are. If they 521 // aren't marked as "executable", that can break the unwinder which will 522 // check a pc value to see if it is in an executable segment and stop the 523 // backtrace early if it is not ("executable" and "unknown" would both be 524 // fine, but "not executable" will break the unwinder). 525 size_t core_range_infos_size = m_core_range_infos.GetSize(); 526 for (size_t i = 0; i < core_range_infos_size; i++) { 527 VMRangeToPermissions::Entry *ent = 528 m_core_range_infos.GetMutableEntryAtIndex(i); 529 ent->data = lldb::ePermissionsReadable | lldb::ePermissionsExecutable; 530 } 531 } 532 533 // Even if the architecture is set in the target, we need to override it to 534 // match the core file which is always single arch. 535 ArchSpec arch(m_core_module_sp->GetArchitecture()); 536 if (arch.GetCore() == ArchSpec::eCore_x86_32_i486) { 537 arch = Platform::GetAugmentedArchSpec(GetTarget().GetPlatform().get(), "i386"); 538 } 539 if (arch.IsValid()) 540 GetTarget().SetArchitecture(arch); 541 542 addr_t address_mask = core_objfile->GetAddressMask(); 543 if (address_mask != 0) { 544 SetCodeAddressMask(address_mask); 545 SetDataAddressMask(address_mask); 546 } 547 return error; 548 } 549 550 lldb_private::DynamicLoader *ProcessMachCore::GetDynamicLoader() { 551 if (m_dyld_up.get() == nullptr) 552 m_dyld_up.reset(DynamicLoader::FindPlugin( 553 this, m_dyld_plugin_name.IsEmpty() ? nullptr 554 : m_dyld_plugin_name.GetCString())); 555 return m_dyld_up.get(); 556 } 557 558 bool ProcessMachCore::DoUpdateThreadList(ThreadList &old_thread_list, 559 ThreadList &new_thread_list) { 560 if (old_thread_list.GetSize(false) == 0) { 561 // Make up the thread the first time this is called so we can setup our one 562 // and only core thread state. 563 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); 564 565 if (core_objfile) { 566 const uint32_t num_threads = core_objfile->GetNumThreadContexts(); 567 for (lldb::tid_t tid = 0; tid < num_threads; ++tid) { 568 ThreadSP thread_sp(new ThreadMachCore(*this, tid)); 569 new_thread_list.AddThread(thread_sp); 570 } 571 } 572 } else { 573 const uint32_t num_threads = old_thread_list.GetSize(false); 574 for (uint32_t i = 0; i < num_threads; ++i) 575 new_thread_list.AddThread(old_thread_list.GetThreadAtIndex(i, false)); 576 } 577 return new_thread_list.GetSize(false) > 0; 578 } 579 580 void ProcessMachCore::RefreshStateAfterStop() { 581 // Let all threads recover from stopping and do any clean up based on the 582 // previous thread state (if any). 583 m_thread_list.RefreshStateAfterStop(); 584 // SetThreadStopInfo (m_last_stop_packet); 585 } 586 587 Status ProcessMachCore::DoDestroy() { return Status(); } 588 589 // Process Queries 590 591 bool ProcessMachCore::IsAlive() { return true; } 592 593 bool ProcessMachCore::WarnBeforeDetach() const { return false; } 594 595 // Process Memory 596 size_t ProcessMachCore::ReadMemory(addr_t addr, void *buf, size_t size, 597 Status &error) { 598 // Don't allow the caching that lldb_private::Process::ReadMemory does since 599 // in core files we have it all cached our our core file anyway. 600 return DoReadMemory(addr, buf, size, error); 601 } 602 603 size_t ProcessMachCore::DoReadMemory(addr_t addr, void *buf, size_t size, 604 Status &error) { 605 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); 606 size_t bytes_read = 0; 607 608 if (core_objfile) { 609 // Segments are not always contiguous in mach-o core files. We have core 610 // files that have segments like: 611 // Address Size File off File size 612 // ---------- ---------- ---------- ---------- 613 // LC_SEGMENT 0x000f6000 0x00001000 0x1d509ee8 0x00001000 --- --- 0 614 // 0x00000000 __TEXT LC_SEGMENT 0x0f600000 0x00100000 0x1d50aee8 0x00100000 615 // --- --- 0 0x00000000 __TEXT LC_SEGMENT 0x000f7000 0x00001000 616 // 0x1d60aee8 0x00001000 --- --- 0 0x00000000 __TEXT 617 // 618 // Any if the user executes the following command: 619 // 620 // (lldb) mem read 0xf6ff0 621 // 622 // We would attempt to read 32 bytes from 0xf6ff0 but would only get 16 623 // unless we loop through consecutive memory ranges that are contiguous in 624 // the address space, but not in the file data. 625 while (bytes_read < size) { 626 const addr_t curr_addr = addr + bytes_read; 627 const VMRangeToFileOffset::Entry *core_memory_entry = 628 m_core_aranges.FindEntryThatContains(curr_addr); 629 630 if (core_memory_entry) { 631 const addr_t offset = curr_addr - core_memory_entry->GetRangeBase(); 632 const addr_t bytes_left = core_memory_entry->GetRangeEnd() - curr_addr; 633 const size_t bytes_to_read = 634 std::min(size - bytes_read, (size_t)bytes_left); 635 const size_t curr_bytes_read = core_objfile->CopyData( 636 core_memory_entry->data.GetRangeBase() + offset, bytes_to_read, 637 (char *)buf + bytes_read); 638 if (curr_bytes_read == 0) 639 break; 640 bytes_read += curr_bytes_read; 641 } else { 642 // Only set the error if we didn't read any bytes 643 if (bytes_read == 0) 644 error.SetErrorStringWithFormat( 645 "core file does not contain 0x%" PRIx64, curr_addr); 646 break; 647 } 648 } 649 } 650 651 return bytes_read; 652 } 653 654 Status ProcessMachCore::GetMemoryRegionInfo(addr_t load_addr, 655 MemoryRegionInfo ®ion_info) { 656 region_info.Clear(); 657 const VMRangeToPermissions::Entry *permission_entry = 658 m_core_range_infos.FindEntryThatContainsOrFollows(load_addr); 659 if (permission_entry) { 660 if (permission_entry->Contains(load_addr)) { 661 region_info.GetRange().SetRangeBase(permission_entry->GetRangeBase()); 662 region_info.GetRange().SetRangeEnd(permission_entry->GetRangeEnd()); 663 const Flags permissions(permission_entry->data); 664 region_info.SetReadable(permissions.Test(ePermissionsReadable) 665 ? MemoryRegionInfo::eYes 666 : MemoryRegionInfo::eNo); 667 region_info.SetWritable(permissions.Test(ePermissionsWritable) 668 ? MemoryRegionInfo::eYes 669 : MemoryRegionInfo::eNo); 670 region_info.SetExecutable(permissions.Test(ePermissionsExecutable) 671 ? MemoryRegionInfo::eYes 672 : MemoryRegionInfo::eNo); 673 region_info.SetMapped(MemoryRegionInfo::eYes); 674 } else if (load_addr < permission_entry->GetRangeBase()) { 675 region_info.GetRange().SetRangeBase(load_addr); 676 region_info.GetRange().SetRangeEnd(permission_entry->GetRangeBase()); 677 region_info.SetReadable(MemoryRegionInfo::eNo); 678 region_info.SetWritable(MemoryRegionInfo::eNo); 679 region_info.SetExecutable(MemoryRegionInfo::eNo); 680 region_info.SetMapped(MemoryRegionInfo::eNo); 681 } 682 return Status(); 683 } 684 685 region_info.GetRange().SetRangeBase(load_addr); 686 region_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 687 region_info.SetReadable(MemoryRegionInfo::eNo); 688 region_info.SetWritable(MemoryRegionInfo::eNo); 689 region_info.SetExecutable(MemoryRegionInfo::eNo); 690 region_info.SetMapped(MemoryRegionInfo::eNo); 691 return Status(); 692 } 693 694 void ProcessMachCore::Clear() { m_thread_list.Clear(); } 695 696 void ProcessMachCore::Initialize() { 697 static llvm::once_flag g_once_flag; 698 699 llvm::call_once(g_once_flag, []() { 700 PluginManager::RegisterPlugin(GetPluginNameStatic(), 701 GetPluginDescriptionStatic(), CreateInstance); 702 }); 703 } 704 705 addr_t ProcessMachCore::GetImageInfoAddress() { 706 // If we found both a user-process dyld and a kernel binary, we need to 707 // decide which to prefer. 708 if (GetCorefilePreference() == eKernelCorefile) { 709 if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) { 710 return m_mach_kernel_addr; 711 } 712 return m_dyld_addr; 713 } else { 714 if (m_dyld_addr != LLDB_INVALID_ADDRESS) { 715 return m_dyld_addr; 716 } 717 return m_mach_kernel_addr; 718 } 719 } 720 721 lldb_private::ObjectFile *ProcessMachCore::GetCoreObjectFile() { 722 return m_core_module_sp->GetObjectFile(); 723 } 724