| 1 | //===-- MinidumpParser.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 "MinidumpParser.h" |
| 10 | #include "NtStructures.h" |
| 11 | #include "RegisterContextMinidump_x86_32.h" |
| 12 | |
| 13 | #include "Plugins/Process/Utility/LinuxProcMaps.h" |
| 14 | #include "lldb/Utility/LLDBAssert.h" |
| 15 | #include "lldb/Utility/LLDBLog.h" |
| 16 | #include "lldb/Utility/Log.h" |
| 17 | |
| 18 | // C includes |
| 19 | // C++ includes |
| 20 | #include <algorithm> |
| 21 | #include <map> |
| 22 | #include <optional> |
| 23 | #include <vector> |
| 24 | #include <utility> |
| 25 | |
| 26 | using namespace lldb_private; |
| 27 | using namespace minidump; |
| 28 | |
| 29 | llvm::Expected<MinidumpParser> |
| 30 | MinidumpParser::Create(const lldb::DataBufferSP &data_sp) { |
| 31 | auto ExpectedFile = llvm::object::MinidumpFile::create( |
| 32 | Source: llvm::MemoryBufferRef(toStringRef(Input: data_sp->GetData()), "minidump" )); |
| 33 | if (!ExpectedFile) |
| 34 | return ExpectedFile.takeError(); |
| 35 | |
| 36 | return MinidumpParser(data_sp, std::move(*ExpectedFile)); |
| 37 | } |
| 38 | |
| 39 | MinidumpParser::MinidumpParser(lldb::DataBufferSP data_sp, |
| 40 | std::unique_ptr<llvm::object::MinidumpFile> file) |
| 41 | : m_data_sp(std::move(data_sp)), m_file(std::move(file)) {} |
| 42 | |
| 43 | llvm::ArrayRef<uint8_t> MinidumpParser::GetData() { |
| 44 | return llvm::ArrayRef<uint8_t>(m_data_sp->GetBytes(), |
| 45 | m_data_sp->GetByteSize()); |
| 46 | } |
| 47 | |
| 48 | llvm::ArrayRef<uint8_t> MinidumpParser::GetStream(StreamType stream_type) { |
| 49 | return m_file->getRawStream(Type: stream_type).value_or(u: llvm::ArrayRef<uint8_t>()); |
| 50 | } |
| 51 | |
| 52 | std::optional<llvm::ArrayRef<uint8_t>> |
| 53 | MinidumpParser::GetRawStream(StreamType stream_type) { |
| 54 | return m_file->getRawStream(Type: stream_type); |
| 55 | } |
| 56 | |
| 57 | UUID MinidumpParser::GetModuleUUID(const minidump::Module *module) { |
| 58 | auto cv_record = |
| 59 | GetData().slice(N: module->CvRecord.RVA, M: module->CvRecord.DataSize); |
| 60 | |
| 61 | // Read the CV record signature |
| 62 | const llvm::support::ulittle32_t *signature = nullptr; |
| 63 | Status error = consumeObject(Buffer&: cv_record, Object&: signature); |
| 64 | if (error.Fail()) |
| 65 | return UUID(); |
| 66 | |
| 67 | const CvSignature cv_signature = |
| 68 | static_cast<CvSignature>(static_cast<uint32_t>(*signature)); |
| 69 | |
| 70 | if (cv_signature == CvSignature::Pdb70) { |
| 71 | const UUID::CvRecordPdb70 *pdb70_uuid = nullptr; |
| 72 | Status error = consumeObject(Buffer&: cv_record, Object&: pdb70_uuid); |
| 73 | if (error.Fail()) |
| 74 | return UUID(); |
| 75 | if (GetArchitecture().GetTriple().isOSBinFormatELF()) { |
| 76 | if (pdb70_uuid->Age != 0) |
| 77 | return UUID(pdb70_uuid, sizeof(*pdb70_uuid)); |
| 78 | return UUID(&pdb70_uuid->Uuid, |
| 79 | sizeof(pdb70_uuid->Uuid)); |
| 80 | } |
| 81 | return UUID(*pdb70_uuid); |
| 82 | } else if (cv_signature == CvSignature::ElfBuildId) |
| 83 | return UUID(cv_record); |
| 84 | |
| 85 | return UUID(); |
| 86 | } |
| 87 | |
| 88 | llvm::ArrayRef<minidump::Thread> MinidumpParser::GetThreads() { |
| 89 | auto ExpectedThreads = GetMinidumpFile().getThreadList(); |
| 90 | if (ExpectedThreads) |
| 91 | return *ExpectedThreads; |
| 92 | |
| 93 | LLDB_LOG_ERROR(GetLog(LLDBLog::Thread), ExpectedThreads.takeError(), |
| 94 | "Failed to read thread list: {0}" ); |
| 95 | return {}; |
| 96 | } |
| 97 | |
| 98 | llvm::ArrayRef<uint8_t> |
| 99 | MinidumpParser::GetThreadContext(const LocationDescriptor &location) { |
| 100 | if (location.RVA + location.DataSize > GetData().size()) |
| 101 | return {}; |
| 102 | return GetData().slice(N: location.RVA, M: location.DataSize); |
| 103 | } |
| 104 | |
| 105 | llvm::ArrayRef<uint8_t> |
| 106 | MinidumpParser::GetThreadContext(const minidump::Thread &td) { |
| 107 | return GetThreadContext(location: td.Context); |
| 108 | } |
| 109 | |
| 110 | llvm::ArrayRef<uint8_t> |
| 111 | MinidumpParser::GetThreadContextWow64(const minidump::Thread &td) { |
| 112 | // On Windows, a 32-bit process can run on a 64-bit machine under WOW64. If |
| 113 | // the minidump was captured with a 64-bit debugger, then the CONTEXT we just |
| 114 | // grabbed from the mini_dump_thread is the one for the 64-bit "native" |
| 115 | // process rather than the 32-bit "guest" process we care about. In this |
| 116 | // case, we can get the 32-bit CONTEXT from the TEB (Thread Environment |
| 117 | // Block) of the 64-bit process. |
| 118 | auto teb_mem = GetMemory(addr: td.EnvironmentBlock, size: sizeof(TEB64)); |
| 119 | if (teb_mem.empty()) |
| 120 | return {}; |
| 121 | |
| 122 | const TEB64 *wow64teb; |
| 123 | Status error = consumeObject(Buffer&: teb_mem, Object&: wow64teb); |
| 124 | if (error.Fail()) |
| 125 | return {}; |
| 126 | |
| 127 | // Slot 1 of the thread-local storage in the 64-bit TEB points to a structure |
| 128 | // that includes the 32-bit CONTEXT (after a ULONG). See: |
| 129 | // https://msdn.microsoft.com/en-us/library/ms681670.aspx |
| 130 | auto context = |
| 131 | GetMemory(addr: wow64teb->tls_slots[1] + 4, size: sizeof(MinidumpContext_x86_32)); |
| 132 | if (context.size() < sizeof(MinidumpContext_x86_32)) |
| 133 | return {}; |
| 134 | |
| 135 | return context; |
| 136 | // NOTE: We don't currently use the TEB for anything else. If we |
| 137 | // need it in the future, the 32-bit TEB is located according to the address |
| 138 | // stored in the first slot of the 64-bit TEB (wow64teb.Reserved1[0]). |
| 139 | } |
| 140 | |
| 141 | ArchSpec MinidumpParser::GetArchitecture() { |
| 142 | if (m_arch.IsValid()) |
| 143 | return m_arch; |
| 144 | |
| 145 | // Set the architecture in m_arch |
| 146 | llvm::Expected<const SystemInfo &> system_info = m_file->getSystemInfo(); |
| 147 | |
| 148 | if (!system_info) { |
| 149 | LLDB_LOG_ERROR(GetLog(LLDBLog::Process), system_info.takeError(), |
| 150 | "Failed to read SystemInfo stream: {0}" ); |
| 151 | return m_arch; |
| 152 | } |
| 153 | |
| 154 | // TODO what to do about big endiand flavors of arm ? |
| 155 | // TODO set the arm subarch stuff if the minidump has info about it |
| 156 | |
| 157 | llvm::Triple triple; |
| 158 | triple.setVendor(llvm::Triple::VendorType::UnknownVendor); |
| 159 | |
| 160 | switch (system_info->ProcessorArch) { |
| 161 | case ProcessorArchitecture::X86: |
| 162 | triple.setArch(Kind: llvm::Triple::ArchType::x86); |
| 163 | break; |
| 164 | case ProcessorArchitecture::AMD64: |
| 165 | triple.setArch(Kind: llvm::Triple::ArchType::x86_64); |
| 166 | break; |
| 167 | case ProcessorArchitecture::ARM: |
| 168 | triple.setArch(Kind: llvm::Triple::ArchType::arm); |
| 169 | break; |
| 170 | case ProcessorArchitecture::ARM64: |
| 171 | case ProcessorArchitecture::BP_ARM64: |
| 172 | triple.setArch(Kind: llvm::Triple::ArchType::aarch64); |
| 173 | break; |
| 174 | default: |
| 175 | triple.setArch(Kind: llvm::Triple::ArchType::UnknownArch); |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | // TODO add all of the OSes that Minidump/breakpad distinguishes? |
| 180 | switch (system_info->PlatformId) { |
| 181 | case OSPlatform::Win32S: |
| 182 | case OSPlatform::Win32Windows: |
| 183 | case OSPlatform::Win32NT: |
| 184 | case OSPlatform::Win32CE: |
| 185 | triple.setOS(llvm::Triple::OSType::Win32); |
| 186 | triple.setVendor(llvm::Triple::VendorType::PC); |
| 187 | break; |
| 188 | case OSPlatform::Linux: |
| 189 | triple.setOS(llvm::Triple::OSType::Linux); |
| 190 | break; |
| 191 | case OSPlatform::MacOSX: |
| 192 | triple.setOS(llvm::Triple::OSType::MacOSX); |
| 193 | triple.setVendor(llvm::Triple::Apple); |
| 194 | break; |
| 195 | case OSPlatform::IOS: |
| 196 | triple.setOS(llvm::Triple::OSType::IOS); |
| 197 | triple.setVendor(llvm::Triple::Apple); |
| 198 | break; |
| 199 | case OSPlatform::Android: |
| 200 | triple.setOS(llvm::Triple::OSType::Linux); |
| 201 | triple.setEnvironment(llvm::Triple::EnvironmentType::Android); |
| 202 | break; |
| 203 | default: { |
| 204 | triple.setOS(llvm::Triple::OSType::UnknownOS); |
| 205 | auto ExpectedCSD = m_file->getString(Offset: system_info->CSDVersionRVA); |
| 206 | if (!ExpectedCSD) { |
| 207 | LLDB_LOG_ERROR(GetLog(LLDBLog::Process), ExpectedCSD.takeError(), |
| 208 | "Failed to CSD Version string: {0}" ); |
| 209 | } else { |
| 210 | if (ExpectedCSD->find(s: "Linux" ) != std::string::npos) |
| 211 | triple.setOS(llvm::Triple::OSType::Linux); |
| 212 | } |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | m_arch.SetTriple(triple); |
| 217 | return m_arch; |
| 218 | } |
| 219 | |
| 220 | const MinidumpMiscInfo *MinidumpParser::GetMiscInfo() { |
| 221 | llvm::ArrayRef<uint8_t> data = GetStream(stream_type: StreamType::MiscInfo); |
| 222 | |
| 223 | if (data.size() == 0) |
| 224 | return nullptr; |
| 225 | |
| 226 | return MinidumpMiscInfo::Parse(data); |
| 227 | } |
| 228 | |
| 229 | std::optional<LinuxProcStatus> MinidumpParser::GetLinuxProcStatus() { |
| 230 | llvm::ArrayRef<uint8_t> data = GetStream(stream_type: StreamType::LinuxProcStatus); |
| 231 | |
| 232 | if (data.size() == 0) |
| 233 | return std::nullopt; |
| 234 | |
| 235 | return LinuxProcStatus::Parse(data); |
| 236 | } |
| 237 | |
| 238 | std::optional<lldb::pid_t> MinidumpParser::GetPid() { |
| 239 | const MinidumpMiscInfo *misc_info = GetMiscInfo(); |
| 240 | if (misc_info != nullptr) { |
| 241 | return misc_info->GetPid(); |
| 242 | } |
| 243 | |
| 244 | std::optional<LinuxProcStatus> proc_status = GetLinuxProcStatus(); |
| 245 | if (proc_status) { |
| 246 | return proc_status->GetPid(); |
| 247 | } |
| 248 | |
| 249 | return std::nullopt; |
| 250 | } |
| 251 | |
| 252 | llvm::ArrayRef<minidump::Module> MinidumpParser::GetModuleList() { |
| 253 | auto ExpectedModules = GetMinidumpFile().getModuleList(); |
| 254 | if (ExpectedModules) |
| 255 | return *ExpectedModules; |
| 256 | |
| 257 | LLDB_LOG_ERROR(GetLog(LLDBLog::Modules), ExpectedModules.takeError(), |
| 258 | "Failed to read module list: {0}" ); |
| 259 | return {}; |
| 260 | } |
| 261 | |
| 262 | static bool |
| 263 | CreateRegionsCacheFromLinuxMaps(MinidumpParser &parser, |
| 264 | std::vector<MemoryRegionInfo> ®ions) { |
| 265 | auto data = parser.GetStream(stream_type: StreamType::LinuxMaps); |
| 266 | if (data.empty()) |
| 267 | return false; |
| 268 | |
| 269 | Log *log = GetLog(mask: LLDBLog::Expressions); |
| 270 | ParseLinuxMapRegions( |
| 271 | linux_map: llvm::toStringRef(Input: data), |
| 272 | callback: [®ions, &log](llvm::Expected<MemoryRegionInfo> region) -> bool { |
| 273 | if (region) |
| 274 | regions.push_back(x: *region); |
| 275 | else |
| 276 | LLDB_LOG_ERROR(log, region.takeError(), |
| 277 | "Reading memory region from minidump failed: {0}" ); |
| 278 | return true; |
| 279 | }); |
| 280 | return !regions.empty(); |
| 281 | } |
| 282 | |
| 283 | /// Check for the memory regions starting at \a load_addr for a contiguous |
| 284 | /// section that has execute permissions that matches the module path. |
| 285 | /// |
| 286 | /// When we load a breakpad generated minidump file, we might have the |
| 287 | /// /proc/<pid>/maps text for a process that details the memory map of the |
| 288 | /// process that the minidump is describing. This checks the sorted memory |
| 289 | /// regions for a section that has execute permissions. A sample maps files |
| 290 | /// might look like: |
| 291 | /// |
| 292 | /// 00400000-00401000 r--p 00000000 fd:01 2838574 /tmp/a.out |
| 293 | /// 00401000-00402000 r-xp 00001000 fd:01 2838574 /tmp/a.out |
| 294 | /// 00402000-00403000 r--p 00002000 fd:01 2838574 /tmp/a.out |
| 295 | /// 00403000-00404000 r--p 00002000 fd:01 2838574 /tmp/a.out |
| 296 | /// 00404000-00405000 rw-p 00003000 fd:01 2838574 /tmp/a.out |
| 297 | /// ... |
| 298 | /// |
| 299 | /// This function should return true when given 0x00400000 and "/tmp/a.out" |
| 300 | /// is passed in as the path since it has a consecutive memory region for |
| 301 | /// "/tmp/a.out" that has execute permissions at 0x00401000. This will help us |
| 302 | /// differentiate if a file has been memory mapped into a process for reading |
| 303 | /// and breakpad ends up saving a minidump file that has two module entries for |
| 304 | /// a given file: one that is read only for the entire file, and then one that |
| 305 | /// is the real executable that is loaded into memory for execution. For memory |
| 306 | /// mapped files they will typically show up and r--p permissions and a range |
| 307 | /// matcning the entire range of the file on disk: |
| 308 | /// |
| 309 | /// 00800000-00805000 r--p 00000000 fd:01 2838574 /tmp/a.out |
| 310 | /// 00805000-00806000 r-xp 00001000 fd:01 1234567 /usr/lib/libc.so |
| 311 | /// |
| 312 | /// This function should return false when asked about 0x00800000 with |
| 313 | /// "/tmp/a.out" as the path. |
| 314 | /// |
| 315 | /// \param[in] path |
| 316 | /// The path to the module to check for in the memory regions. Only sequential |
| 317 | /// memory regions whose paths match this path will be considered when looking |
| 318 | /// for execute permissions. |
| 319 | /// |
| 320 | /// \param[in] regions |
| 321 | /// A sorted list of memory regions obtained from a call to |
| 322 | /// CreateRegionsCacheFromLinuxMaps. |
| 323 | /// |
| 324 | /// \param[in] base_of_image |
| 325 | /// The load address of this module from BaseOfImage in the modules list. |
| 326 | /// |
| 327 | /// \return |
| 328 | /// True if a contiguous region of memory belonging to the module with a |
| 329 | /// matching path exists that has executable permissions. Returns false if |
| 330 | /// \a regions is empty or if there are no regions with execute permissions |
| 331 | /// that match \a path. |
| 332 | |
| 333 | static bool CheckForLinuxExecutable(ConstString path, |
| 334 | const MemoryRegionInfos ®ions, |
| 335 | lldb::addr_t base_of_image) { |
| 336 | if (regions.empty()) |
| 337 | return false; |
| 338 | lldb::addr_t addr = base_of_image; |
| 339 | MemoryRegionInfo region = MinidumpParser::GetMemoryRegionInfo(regions, load_addr: addr); |
| 340 | while (region.GetName() == path) { |
| 341 | if (region.GetExecutable() == MemoryRegionInfo::eYes) |
| 342 | return true; |
| 343 | addr += region.GetRange().GetByteSize(); |
| 344 | region = MinidumpParser::GetMemoryRegionInfo(regions, load_addr: addr); |
| 345 | } |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | std::vector<const minidump::Module *> MinidumpParser::GetFilteredModuleList() { |
| 350 | Log *log = GetLog(mask: LLDBLog::Modules); |
| 351 | auto ExpectedModules = GetMinidumpFile().getModuleList(); |
| 352 | if (!ExpectedModules) { |
| 353 | LLDB_LOG_ERROR(log, ExpectedModules.takeError(), |
| 354 | "Failed to read module list: {0}" ); |
| 355 | return {}; |
| 356 | } |
| 357 | |
| 358 | // Create memory regions from the linux maps only. We do this to avoid issues |
| 359 | // with breakpad generated minidumps where if someone has mmap'ed a shared |
| 360 | // library into memory to access its data in the object file, we can get a |
| 361 | // minidump with two mappings for a binary: one whose base image points to a |
| 362 | // memory region that is read + execute and one that is read only. |
| 363 | MemoryRegionInfos linux_regions; |
| 364 | if (CreateRegionsCacheFromLinuxMaps(parser&: *this, regions&: linux_regions)) |
| 365 | llvm::sort(C&: linux_regions); |
| 366 | |
| 367 | // map module_name -> filtered_modules index |
| 368 | typedef llvm::StringMap<size_t> MapType; |
| 369 | MapType module_name_to_filtered_index; |
| 370 | |
| 371 | std::vector<const minidump::Module *> filtered_modules; |
| 372 | |
| 373 | for (const auto &module : *ExpectedModules) { |
| 374 | auto ExpectedName = m_file->getString(Offset: module.ModuleNameRVA); |
| 375 | if (!ExpectedName) { |
| 376 | LLDB_LOG_ERROR(log, ExpectedName.takeError(), |
| 377 | "Failed to get module name: {0}" ); |
| 378 | continue; |
| 379 | } |
| 380 | |
| 381 | MapType::iterator iter; |
| 382 | bool inserted; |
| 383 | // See if we have inserted this module aready into filtered_modules. If we |
| 384 | // haven't insert an entry into module_name_to_filtered_index with the |
| 385 | // index where we will insert it if it isn't in the vector already. |
| 386 | std::tie(args&: iter, args&: inserted) = module_name_to_filtered_index.try_emplace( |
| 387 | Key: *ExpectedName, Args: filtered_modules.size()); |
| 388 | |
| 389 | if (inserted) { |
| 390 | // This module has not been seen yet, insert it into filtered_modules at |
| 391 | // the index that was inserted into module_name_to_filtered_index using |
| 392 | // "filtered_modules.size()" above. |
| 393 | filtered_modules.push_back(x: &module); |
| 394 | } else { |
| 395 | // We have a duplicate module entry. Check the linux regions to see if |
| 396 | // either module is not really a mapped executable. If one but not the |
| 397 | // other is a real mapped executable, prefer the executable one. This |
| 398 | // can happen when a process mmap's in the file for an executable in |
| 399 | // order to read bytes from the executable file. A memory region mapping |
| 400 | // will exist for the mmap'ed version and for the loaded executable, but |
| 401 | // only one will have a consecutive region that is executable in the |
| 402 | // memory regions. |
| 403 | auto dup_module = filtered_modules[iter->second]; |
| 404 | ConstString name(*ExpectedName); |
| 405 | bool is_executable = |
| 406 | CheckForLinuxExecutable(path: name, regions: linux_regions, base_of_image: module.BaseOfImage); |
| 407 | bool dup_is_executable = |
| 408 | CheckForLinuxExecutable(path: name, regions: linux_regions, base_of_image: dup_module->BaseOfImage); |
| 409 | |
| 410 | if (is_executable != dup_is_executable) { |
| 411 | if (is_executable) |
| 412 | filtered_modules[iter->second] = &module; |
| 413 | continue; |
| 414 | } |
| 415 | // This module has been seen. Modules are sometimes mentioned multiple |
| 416 | // times when they are mapped discontiguously, so find the module with |
| 417 | // the lowest "base_of_image" and use that as the filtered module. |
| 418 | if (module.BaseOfImage < dup_module->BaseOfImage) |
| 419 | filtered_modules[iter->second] = &module; |
| 420 | } |
| 421 | } |
| 422 | return filtered_modules; |
| 423 | } |
| 424 | |
| 425 | llvm::iterator_range<ExceptionStreamsIterator> |
| 426 | MinidumpParser::GetExceptionStreams() { |
| 427 | return GetMinidumpFile().getExceptionStreams(); |
| 428 | } |
| 429 | |
| 430 | std::optional<minidump::Range> |
| 431 | MinidumpParser::FindMemoryRange(lldb::addr_t addr) { |
| 432 | Log *log = GetLog(mask: LLDBLog::Modules); |
| 433 | |
| 434 | auto ExpectedMemory = GetMinidumpFile().getMemoryList(); |
| 435 | if (!ExpectedMemory) { |
| 436 | LLDB_LOG_ERROR(log, ExpectedMemory.takeError(), |
| 437 | "Failed to read memory list: {0}" ); |
| 438 | } else { |
| 439 | for (const auto &memory_desc : *ExpectedMemory) { |
| 440 | const LocationDescriptor &loc_desc = memory_desc.Memory; |
| 441 | const lldb::addr_t range_start = memory_desc.StartOfMemoryRange; |
| 442 | const size_t range_size = loc_desc.DataSize; |
| 443 | |
| 444 | if (loc_desc.RVA + loc_desc.DataSize > GetData().size()) |
| 445 | return std::nullopt; |
| 446 | |
| 447 | if (range_start <= addr && addr < range_start + range_size) { |
| 448 | auto ExpectedSlice = GetMinidumpFile().getRawData(Desc: loc_desc); |
| 449 | if (!ExpectedSlice) { |
| 450 | LLDB_LOG_ERROR(log, ExpectedSlice.takeError(), |
| 451 | "Failed to get memory slice: {0}" ); |
| 452 | return std::nullopt; |
| 453 | } |
| 454 | return minidump::Range(range_start, *ExpectedSlice); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | if (!GetStream(stream_type: StreamType::Memory64List).empty()) { |
| 460 | llvm::Error err = llvm::Error::success(); |
| 461 | for (const auto &memory_desc : GetMinidumpFile().getMemory64List(Err&: err)) { |
| 462 | if (memory_desc.first.StartOfMemoryRange <= addr |
| 463 | && addr < memory_desc.first.StartOfMemoryRange + memory_desc.first.DataSize) { |
| 464 | return minidump::Range(memory_desc.first.StartOfMemoryRange, memory_desc.second); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | if (err) |
| 469 | LLDB_LOG_ERROR(log, std::move(err), "Failed to read memory64 list: {0}" ); |
| 470 | } |
| 471 | |
| 472 | return std::nullopt; |
| 473 | } |
| 474 | |
| 475 | llvm::ArrayRef<uint8_t> MinidumpParser::GetMemory(lldb::addr_t addr, |
| 476 | size_t size) { |
| 477 | // I don't have a sense of how frequently this is called or how many memory |
| 478 | // ranges a Minidump typically has, so I'm not sure if searching for the |
| 479 | // appropriate range linearly each time is stupid. Perhaps we should build |
| 480 | // an index for faster lookups. |
| 481 | std::optional<minidump::Range> range = FindMemoryRange(addr); |
| 482 | if (!range) |
| 483 | return {}; |
| 484 | |
| 485 | // There's at least some overlap between the beginning of the desired range |
| 486 | // (addr) and the current range. Figure out where the overlap begins and how |
| 487 | // much overlap there is. |
| 488 | |
| 489 | const size_t offset = addr - range->start; |
| 490 | |
| 491 | if (addr < range->start || offset >= range->range_ref.size()) |
| 492 | return {}; |
| 493 | |
| 494 | const size_t overlap = std::min(a: size, b: range->range_ref.size() - offset); |
| 495 | return range->range_ref.slice(N: offset, M: overlap); |
| 496 | } |
| 497 | |
| 498 | llvm::iterator_range<FallibleMemory64Iterator> MinidumpParser::GetMemory64Iterator(llvm::Error &err) { |
| 499 | llvm::ErrorAsOutParameter ErrAsOutParam(&err); |
| 500 | return m_file->getMemory64List(Err&: err); |
| 501 | } |
| 502 | |
| 503 | static bool |
| 504 | CreateRegionsCacheFromMemoryInfoList(MinidumpParser &parser, |
| 505 | std::vector<MemoryRegionInfo> ®ions) { |
| 506 | Log *log = GetLog(mask: LLDBLog::Modules); |
| 507 | auto ExpectedInfo = parser.GetMinidumpFile().getMemoryInfoList(); |
| 508 | if (!ExpectedInfo) { |
| 509 | LLDB_LOG_ERROR(log, ExpectedInfo.takeError(), |
| 510 | "Failed to read memory info list: {0}" ); |
| 511 | return false; |
| 512 | } |
| 513 | constexpr auto yes = MemoryRegionInfo::eYes; |
| 514 | constexpr auto no = MemoryRegionInfo::eNo; |
| 515 | for (const MemoryInfo &entry : *ExpectedInfo) { |
| 516 | MemoryRegionInfo region; |
| 517 | region.GetRange().SetRangeBase(entry.BaseAddress); |
| 518 | region.GetRange().SetByteSize(entry.RegionSize); |
| 519 | |
| 520 | MemoryProtection prot = entry.Protect; |
| 521 | region.SetReadable(bool(prot & MemoryProtection::NoAccess) ? no : yes); |
| 522 | region.SetWritable( |
| 523 | bool(prot & (MemoryProtection::ReadWrite | MemoryProtection::WriteCopy | |
| 524 | MemoryProtection::ExecuteReadWrite | |
| 525 | MemoryProtection::ExeciteWriteCopy)) |
| 526 | ? yes |
| 527 | : no); |
| 528 | region.SetExecutable( |
| 529 | bool(prot & (MemoryProtection::Execute | MemoryProtection::ExecuteRead | |
| 530 | MemoryProtection::ExecuteReadWrite | |
| 531 | MemoryProtection::ExeciteWriteCopy)) |
| 532 | ? yes |
| 533 | : no); |
| 534 | region.SetMapped(entry.State != MemoryState::Free ? yes : no); |
| 535 | regions.push_back(x: region); |
| 536 | } |
| 537 | return !regions.empty(); |
| 538 | } |
| 539 | |
| 540 | static bool |
| 541 | CreateRegionsCacheFromMemoryList(MinidumpParser &parser, |
| 542 | std::vector<MemoryRegionInfo> ®ions) { |
| 543 | Log *log = GetLog(mask: LLDBLog::Modules); |
| 544 | // Cache the expected memory32 into an optional |
| 545 | // because it is possible to just have a memory64 list |
| 546 | auto ExpectedMemory = parser.GetMinidumpFile().getMemoryList(); |
| 547 | if (!ExpectedMemory) { |
| 548 | LLDB_LOG_ERROR(log, ExpectedMemory.takeError(), |
| 549 | "Failed to read memory list: {0}" ); |
| 550 | } else { |
| 551 | for (const MemoryDescriptor &memory_desc : *ExpectedMemory) { |
| 552 | if (memory_desc.Memory.DataSize == 0) |
| 553 | continue; |
| 554 | MemoryRegionInfo region; |
| 555 | region.GetRange().SetRangeBase(memory_desc.StartOfMemoryRange); |
| 556 | region.GetRange().SetByteSize(memory_desc.Memory.DataSize); |
| 557 | region.SetReadable(MemoryRegionInfo::eYes); |
| 558 | region.SetMapped(MemoryRegionInfo::eYes); |
| 559 | regions.push_back(x: region); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | if (!parser.GetStream(stream_type: StreamType::Memory64List).empty()) { |
| 564 | llvm::Error err = llvm::Error::success(); |
| 565 | for (const auto &memory_desc : parser.GetMemory64Iterator(err)) { |
| 566 | if (memory_desc.first.DataSize == 0) |
| 567 | continue; |
| 568 | MemoryRegionInfo region; |
| 569 | region.GetRange().SetRangeBase(memory_desc.first.StartOfMemoryRange); |
| 570 | region.GetRange().SetByteSize(memory_desc.first.DataSize); |
| 571 | region.SetReadable(MemoryRegionInfo::eYes); |
| 572 | region.SetMapped(MemoryRegionInfo::eYes); |
| 573 | regions.push_back(x: region); |
| 574 | } |
| 575 | |
| 576 | if (err) { |
| 577 | LLDB_LOG_ERROR(log, std::move(err), "Failed to read memory64 list: {0}" ); |
| 578 | return false; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | regions.shrink_to_fit(); |
| 583 | return !regions.empty(); |
| 584 | } |
| 585 | |
| 586 | std::pair<MemoryRegionInfos, bool> MinidumpParser::BuildMemoryRegions() { |
| 587 | // We create the region cache using the best source. We start with |
| 588 | // the linux maps since they are the most complete and have names for the |
| 589 | // regions. Next we try the MemoryInfoList since it has |
| 590 | // read/write/execute/map data, and then fall back to the MemoryList and |
| 591 | // Memory64List to just get a list of the memory that is mapped in this |
| 592 | // core file |
| 593 | MemoryRegionInfos result; |
| 594 | const auto &return_sorted = [&](bool is_complete) { |
| 595 | llvm::sort(C&: result); |
| 596 | return std::make_pair(x: std::move(result), y&: is_complete); |
| 597 | }; |
| 598 | if (CreateRegionsCacheFromLinuxMaps(parser&: *this, regions&: result)) |
| 599 | return return_sorted(true); |
| 600 | if (CreateRegionsCacheFromMemoryInfoList(parser&: *this, regions&: result)) |
| 601 | return return_sorted(true); |
| 602 | CreateRegionsCacheFromMemoryList(parser&: *this, regions&: result); |
| 603 | return return_sorted(false); |
| 604 | } |
| 605 | |
| 606 | #define ENUM_TO_CSTR(ST) \ |
| 607 | case StreamType::ST: \ |
| 608 | return #ST |
| 609 | |
| 610 | llvm::StringRef |
| 611 | MinidumpParser::GetStreamTypeAsString(StreamType stream_type) { |
| 612 | switch (stream_type) { |
| 613 | ENUM_TO_CSTR(Unused); |
| 614 | ENUM_TO_CSTR(ThreadList); |
| 615 | ENUM_TO_CSTR(ModuleList); |
| 616 | ENUM_TO_CSTR(MemoryList); |
| 617 | ENUM_TO_CSTR(Exception); |
| 618 | ENUM_TO_CSTR(SystemInfo); |
| 619 | ENUM_TO_CSTR(ThreadExList); |
| 620 | ENUM_TO_CSTR(Memory64List); |
| 621 | ENUM_TO_CSTR(CommentA); |
| 622 | ENUM_TO_CSTR(CommentW); |
| 623 | ENUM_TO_CSTR(HandleData); |
| 624 | ENUM_TO_CSTR(FunctionTable); |
| 625 | ENUM_TO_CSTR(UnloadedModuleList); |
| 626 | ENUM_TO_CSTR(MiscInfo); |
| 627 | ENUM_TO_CSTR(MemoryInfoList); |
| 628 | ENUM_TO_CSTR(ThreadInfoList); |
| 629 | ENUM_TO_CSTR(HandleOperationList); |
| 630 | ENUM_TO_CSTR(Token); |
| 631 | ENUM_TO_CSTR(JavascriptData); |
| 632 | ENUM_TO_CSTR(SystemMemoryInfo); |
| 633 | ENUM_TO_CSTR(ProcessVMCounters); |
| 634 | ENUM_TO_CSTR(LastReserved); |
| 635 | ENUM_TO_CSTR(BreakpadInfo); |
| 636 | ENUM_TO_CSTR(AssertionInfo); |
| 637 | ENUM_TO_CSTR(LinuxCPUInfo); |
| 638 | ENUM_TO_CSTR(LinuxProcStatus); |
| 639 | ENUM_TO_CSTR(LinuxLSBRelease); |
| 640 | ENUM_TO_CSTR(LinuxCMDLine); |
| 641 | ENUM_TO_CSTR(LinuxEnviron); |
| 642 | ENUM_TO_CSTR(LinuxAuxv); |
| 643 | ENUM_TO_CSTR(LinuxMaps); |
| 644 | ENUM_TO_CSTR(LinuxDSODebug); |
| 645 | ENUM_TO_CSTR(LinuxProcStat); |
| 646 | ENUM_TO_CSTR(LinuxProcUptime); |
| 647 | ENUM_TO_CSTR(LinuxProcFD); |
| 648 | ENUM_TO_CSTR(FacebookAppCustomData); |
| 649 | ENUM_TO_CSTR(FacebookBuildID); |
| 650 | ENUM_TO_CSTR(FacebookAppVersionName); |
| 651 | ENUM_TO_CSTR(FacebookJavaStack); |
| 652 | ENUM_TO_CSTR(FacebookDalvikInfo); |
| 653 | ENUM_TO_CSTR(FacebookUnwindSymbols); |
| 654 | ENUM_TO_CSTR(FacebookDumpErrorLog); |
| 655 | ENUM_TO_CSTR(FacebookAppStateLog); |
| 656 | ENUM_TO_CSTR(FacebookAbortReason); |
| 657 | ENUM_TO_CSTR(FacebookThreadName); |
| 658 | ENUM_TO_CSTR(FacebookLogcat); |
| 659 | ENUM_TO_CSTR(LLDBGenerated); |
| 660 | } |
| 661 | return "unknown stream type" ; |
| 662 | } |
| 663 | |
| 664 | MemoryRegionInfo |
| 665 | MinidumpParser::GetMemoryRegionInfo(const MemoryRegionInfos ®ions, |
| 666 | lldb::addr_t load_addr) { |
| 667 | MemoryRegionInfo region; |
| 668 | auto pos = llvm::upper_bound(Range: regions, Value&: load_addr); |
| 669 | if (pos != regions.begin() && |
| 670 | std::prev(x: pos)->GetRange().Contains(r: load_addr)) { |
| 671 | return *std::prev(x: pos); |
| 672 | } |
| 673 | |
| 674 | if (pos == regions.begin()) |
| 675 | region.GetRange().SetRangeBase(0); |
| 676 | else |
| 677 | region.GetRange().SetRangeBase(std::prev(x: pos)->GetRange().GetRangeEnd()); |
| 678 | |
| 679 | if (pos == regions.end()) |
| 680 | region.GetRange().SetRangeEnd(UINT64_MAX); |
| 681 | else |
| 682 | region.GetRange().SetRangeEnd(pos->GetRange().GetRangeBase()); |
| 683 | |
| 684 | region.SetReadable(MemoryRegionInfo::eNo); |
| 685 | region.SetWritable(MemoryRegionInfo::eNo); |
| 686 | region.SetExecutable(MemoryRegionInfo::eNo); |
| 687 | region.SetMapped(MemoryRegionInfo::eNo); |
| 688 | return region; |
| 689 | } |
| 690 | |