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