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