xref: /llvm-project/lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp (revision c4fb7180cbbe977f1ab1ce945a691550f8fdd1fb)
1 //===-- ProcessMinidump.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 "ProcessMinidump.h"
10 
11 #include "ThreadMinidump.h"
12 
13 #include "lldb/Core/DumpDataExtractor.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/ModuleSpec.h"
16 #include "lldb/Core/PluginManager.h"
17 #include "lldb/Core/Section.h"
18 #include "lldb/Interpreter/CommandInterpreter.h"
19 #include "lldb/Interpreter/CommandObject.h"
20 #include "lldb/Interpreter/CommandObjectMultiword.h"
21 #include "lldb/Interpreter/CommandReturnObject.h"
22 #include "lldb/Interpreter/OptionArgParser.h"
23 #include "lldb/Interpreter/OptionGroupBoolean.h"
24 #include "lldb/Target/DynamicLoader.h"
25 #include "lldb/Target/JITLoaderList.h"
26 #include "lldb/Target/MemoryRegionInfo.h"
27 #include "lldb/Target/SectionLoadList.h"
28 #include "lldb/Target/Target.h"
29 #include "lldb/Target/UnixSignals.h"
30 #include "lldb/Utility/DataBufferHeap.h"
31 #include "lldb/Utility/LLDBAssert.h"
32 #include "lldb/Utility/LLDBLog.h"
33 #include "lldb/Utility/Log.h"
34 #include "lldb/Utility/State.h"
35 #include "llvm/BinaryFormat/Magic.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/Threading.h"
38 
39 #include "Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h"
40 #include "Plugins/ObjectFile/Placeholder/ObjectFilePlaceholder.h"
41 #include "Plugins/Process/Utility/StopInfoMachException.h"
42 
43 #include <memory>
44 #include <optional>
45 
46 using namespace lldb;
47 using namespace lldb_private;
48 using namespace minidump;
49 
50 LLDB_PLUGIN_DEFINE(ProcessMinidump)
51 
52 namespace {
53 
54 /// Duplicate the HashElfTextSection() from the breakpad sources.
55 ///
56 /// Breakpad, a Google crash log reporting tool suite, creates minidump files
57 /// for many different architectures. When using Breakpad to create ELF
58 /// minidumps, it will check for a GNU build ID when creating a minidump file
59 /// and if one doesn't exist in the file, it will say the UUID of the file is a
60 /// checksum of up to the first 4096 bytes of the .text section. Facebook also
61 /// uses breakpad and modified this hash to avoid collisions so we can
62 /// calculate and check for this as well.
63 ///
64 /// The breakpad code might end up hashing up to 15 bytes that immediately
65 /// follow the .text section in the file, so this code must do exactly what it
66 /// does so we can get an exact match for the UUID.
67 ///
68 /// \param[in] module_sp The module to grab the .text section from.
69 ///
70 /// \param[in,out] breakpad_uuid A vector that will receive the calculated
71 ///                breakpad .text hash.
72 ///
73 /// \param[in,out] facebook_uuid A vector that will receive the calculated
74 ///                facebook .text hash.
75 ///
76 void HashElfTextSection(ModuleSP module_sp, std::vector<uint8_t> &breakpad_uuid,
77                         std::vector<uint8_t> &facebook_uuid) {
78   SectionList *sect_list = module_sp->GetSectionList();
79   if (sect_list == nullptr)
80     return;
81   SectionSP sect_sp = sect_list->FindSectionByName(ConstString(".text"));
82   if (!sect_sp)
83     return;
84   constexpr size_t kMDGUIDSize = 16;
85   constexpr size_t kBreakpadPageSize = 4096;
86   // The breakpad code has a bug where it might access beyond the end of a
87   // .text section by up to 15 bytes, so we must ensure we round up to the
88   // next kMDGUIDSize byte boundary.
89   DataExtractor data;
90   const size_t text_size = sect_sp->GetFileSize();
91   const size_t read_size = std::min<size_t>(
92       llvm::alignTo(text_size, kMDGUIDSize), kBreakpadPageSize);
93   sect_sp->GetObjectFile()->GetData(sect_sp->GetFileOffset(), read_size, data);
94 
95   breakpad_uuid.assign(kMDGUIDSize, 0);
96   facebook_uuid.assign(kMDGUIDSize, 0);
97 
98   // The only difference between the breakpad hash and the facebook hash is the
99   // hashing of the text section size into the hash prior to hashing the .text
100   // contents.
101   for (size_t i = 0; i < kMDGUIDSize; i++)
102     facebook_uuid[i] ^= text_size % 255;
103 
104   // This code carefully duplicates how the hash was created in Breakpad
105   // sources, including the error where it might has an extra 15 bytes past the
106   // end of the .text section if the .text section is less than a page size in
107   // length.
108   const uint8_t *ptr = data.GetDataStart();
109   const uint8_t *ptr_end = data.GetDataEnd();
110   while (ptr < ptr_end) {
111     for (unsigned i = 0; i < kMDGUIDSize; i++) {
112       breakpad_uuid[i] ^= ptr[i];
113       facebook_uuid[i] ^= ptr[i];
114     }
115     ptr += kMDGUIDSize;
116   }
117 }
118 
119 } // namespace
120 
121 llvm::StringRef ProcessMinidump::GetPluginDescriptionStatic() {
122   return "Minidump plug-in.";
123 }
124 
125 lldb::ProcessSP ProcessMinidump::CreateInstance(lldb::TargetSP target_sp,
126                                                 lldb::ListenerSP listener_sp,
127                                                 const FileSpec *crash_file,
128                                                 bool can_connect) {
129   if (!crash_file || can_connect)
130     return nullptr;
131 
132   lldb::ProcessSP process_sp;
133   // Read enough data for the Minidump header
134   constexpr size_t header_size = sizeof(Header);
135   auto DataPtr = FileSystem::Instance().CreateDataBuffer(crash_file->GetPath(),
136                                                          header_size, 0);
137   if (!DataPtr)
138     return nullptr;
139 
140   lldbassert(DataPtr->GetByteSize() == header_size);
141   if (identify_magic(toStringRef(DataPtr->GetData())) != llvm::file_magic::minidump)
142     return nullptr;
143 
144   auto AllData =
145       FileSystem::Instance().CreateDataBuffer(crash_file->GetPath(), -1, 0);
146   if (!AllData)
147     return nullptr;
148 
149   return std::make_shared<ProcessMinidump>(target_sp, listener_sp, *crash_file,
150                                            std::move(AllData));
151 }
152 
153 bool ProcessMinidump::CanDebug(lldb::TargetSP target_sp,
154                                bool plugin_specified_by_name) {
155   return true;
156 }
157 
158 ProcessMinidump::ProcessMinidump(lldb::TargetSP target_sp,
159                                  lldb::ListenerSP listener_sp,
160                                  const FileSpec &core_file,
161                                  DataBufferSP core_data)
162     : PostMortemProcess(target_sp, listener_sp, core_file),
163       m_core_data(std::move(core_data)), m_is_wow64(false) {}
164 
165 ProcessMinidump::~ProcessMinidump() {
166   Clear();
167   // We need to call finalize on the process before destroying ourselves to
168   // make sure all of the broadcaster cleanup goes as planned. If we destruct
169   // this class, then Process::~Process() might have problems trying to fully
170   // destroy the broadcaster.
171   Finalize(true /* destructing */);
172 }
173 
174 void ProcessMinidump::Initialize() {
175   static llvm::once_flag g_once_flag;
176 
177   llvm::call_once(g_once_flag, []() {
178     PluginManager::RegisterPlugin(GetPluginNameStatic(),
179                                   GetPluginDescriptionStatic(),
180                                   ProcessMinidump::CreateInstance);
181   });
182 }
183 
184 void ProcessMinidump::Terminate() {
185   PluginManager::UnregisterPlugin(ProcessMinidump::CreateInstance);
186 }
187 
188 Status ProcessMinidump::DoLoadCore() {
189   auto expected_parser = MinidumpParser::Create(m_core_data);
190   if (!expected_parser)
191     return Status::FromError(expected_parser.takeError());
192   m_minidump_parser = std::move(*expected_parser);
193 
194   Status error;
195 
196   // Do we support the minidump's architecture?
197   ArchSpec arch = GetArchitecture();
198   switch (arch.GetMachine()) {
199   case llvm::Triple::x86:
200   case llvm::Triple::x86_64:
201   case llvm::Triple::arm:
202   case llvm::Triple::aarch64:
203     // Any supported architectures must be listed here and also supported in
204     // ThreadMinidump::CreateRegisterContextForFrame().
205     break;
206   default:
207     error = Status::FromErrorStringWithFormat(
208         "unsupported minidump architecture: %s", arch.GetArchitectureName());
209     return error;
210   }
211   GetTarget().SetArchitecture(arch, true /*set_platform*/);
212 
213   m_thread_list = m_minidump_parser->GetThreads();
214   auto exception_stream_it = m_minidump_parser->GetExceptionStreams();
215   for (auto exception_stream : exception_stream_it) {
216     // If we can't read an exception stream skip it
217     // We should probably serve a warning
218     if (!exception_stream)
219       continue;
220 
221     if (!m_exceptions_by_tid
222              .try_emplace(exception_stream->ThreadId, exception_stream.get())
223              .second) {
224       return Status::FromErrorStringWithFormatv(
225           "Duplicate exception stream for tid {0}", exception_stream->ThreadId);
226     }
227   }
228 
229   SetUnixSignals(UnixSignals::Create(GetArchitecture()));
230 
231   ReadModuleList();
232   if (ModuleSP module = GetTarget().GetExecutableModule())
233     GetTarget().MergeArchitecture(module->GetArchitecture());
234   std::optional<lldb::pid_t> pid = m_minidump_parser->GetPid();
235   if (!pid) {
236     Debugger::ReportWarning("unable to retrieve process ID from minidump file, "
237                             "setting process ID to 1",
238                             GetTarget().GetDebugger().GetID());
239     pid = 1;
240   }
241   SetID(*pid);
242 
243   return error;
244 }
245 
246 Status ProcessMinidump::DoDestroy() { return Status(); }
247 
248 void ProcessMinidump::RefreshStateAfterStop() {
249 
250   for (const auto &[_, exception_stream] : m_exceptions_by_tid) {
251     constexpr uint32_t BreakpadDumpRequested = 0xFFFFFFFF;
252     if (exception_stream.ExceptionRecord.ExceptionCode ==
253         BreakpadDumpRequested) {
254       // This "ExceptionCode" value is a sentinel that is sometimes used
255       // when generating a dump for a process that hasn't crashed.
256 
257       // TODO: The definition and use of this "dump requested" constant
258       // in Breakpad are actually Linux-specific, and for similar use
259       // cases on Mac/Windows it defines different constants, referring
260       // to them as "simulated" exceptions; consider moving this check
261       // down to the OS-specific paths and checking each OS for its own
262       // constant.
263       return;
264     }
265 
266     lldb::StopInfoSP stop_info;
267     lldb::ThreadSP stop_thread;
268 
269     Process::m_thread_list.SetSelectedThreadByID(exception_stream.ThreadId);
270     stop_thread = Process::m_thread_list.GetSelectedThread();
271     ArchSpec arch = GetArchitecture();
272 
273     if (arch.GetTriple().getOS() == llvm::Triple::Linux) {
274       uint32_t signo = exception_stream.ExceptionRecord.ExceptionCode;
275       if (signo == 0) {
276         // No stop.
277         return;
278       }
279       const char *description = nullptr;
280       if (exception_stream.ExceptionRecord.ExceptionFlags ==
281           llvm::minidump::Exception::LLDB_FLAG)
282         description = reinterpret_cast<const char *>(
283             exception_stream.ExceptionRecord.ExceptionInformation);
284 
285       llvm::StringRef description_str(description,
286                                       Exception::MaxParameterBytes);
287       stop_info = StopInfo::CreateStopReasonWithSignal(
288           *stop_thread, signo, description_str.str().c_str());
289     } else if (arch.GetTriple().getVendor() == llvm::Triple::Apple) {
290       stop_info = StopInfoMachException::CreateStopReasonWithMachException(
291           *stop_thread, exception_stream.ExceptionRecord.ExceptionCode, 2,
292           exception_stream.ExceptionRecord.ExceptionFlags,
293           exception_stream.ExceptionRecord.ExceptionAddress, 0);
294     } else {
295       std::string desc;
296       llvm::raw_string_ostream desc_stream(desc);
297       desc_stream << "Exception "
298                   << llvm::format_hex(
299                          exception_stream.ExceptionRecord.ExceptionCode, 8)
300                   << " encountered at address "
301                   << llvm::format_hex(
302                          exception_stream.ExceptionRecord.ExceptionAddress, 8);
303       stop_info =
304           StopInfo::CreateStopReasonWithException(*stop_thread, desc.c_str());
305     }
306 
307     stop_thread->SetStopInfo(stop_info);
308   }
309 }
310 
311 bool ProcessMinidump::IsAlive() { return true; }
312 
313 bool ProcessMinidump::WarnBeforeDetach() const { return false; }
314 
315 size_t ProcessMinidump::ReadMemory(lldb::addr_t addr, void *buf, size_t size,
316                                    Status &error) {
317   // Don't allow the caching that lldb_private::Process::ReadMemory does since
318   // we have it all cached in our dump file anyway.
319   return DoReadMemory(addr, buf, size, error);
320 }
321 
322 size_t ProcessMinidump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
323                                      Status &error) {
324 
325   llvm::ArrayRef<uint8_t> mem = m_minidump_parser->GetMemory(addr, size);
326   if (mem.empty()) {
327     error = Status::FromErrorString("could not parse memory info");
328     return 0;
329   }
330 
331   std::memcpy(buf, mem.data(), mem.size());
332   return mem.size();
333 }
334 
335 ArchSpec ProcessMinidump::GetArchitecture() {
336   if (!m_is_wow64) {
337     return m_minidump_parser->GetArchitecture();
338   }
339 
340   llvm::Triple triple;
341   triple.setVendor(llvm::Triple::VendorType::UnknownVendor);
342   triple.setArch(llvm::Triple::ArchType::x86);
343   triple.setOS(llvm::Triple::OSType::Win32);
344   return ArchSpec(triple);
345 }
346 
347 DataExtractor ProcessMinidump::GetAuxvData() {
348   std::optional<llvm::ArrayRef<uint8_t>> auxv =
349       m_minidump_parser->GetStream(StreamType::LinuxAuxv);
350   if (!auxv)
351     return DataExtractor();
352 
353   return DataExtractor(auxv->data(), auxv->size(), GetByteOrder(),
354                        GetAddressByteSize(), GetAddressByteSize());
355 }
356 
357 bool ProcessMinidump::IsLLDBMinidump() {
358   std::optional<llvm::ArrayRef<uint8_t>> lldb_generated_section =
359       m_minidump_parser->GetRawStream(StreamType::LLDBGenerated);
360   return lldb_generated_section.has_value();
361 }
362 
363 DynamicLoader *ProcessMinidump::GetDynamicLoader() {
364   // This is a workaround for the dynamic loader not playing nice in issue
365   // #119598. The specific reason we use the dynamic loader is to get the TLS
366   // info sections, which we can assume are not being written to the minidump
367   // unless it's an LLDB generate minidump.
368   if (IsLLDBMinidump())
369     return PostMortemProcess::GetDynamicLoader();
370   return nullptr;
371 }
372 
373 void ProcessMinidump::BuildMemoryRegions() {
374   if (m_memory_regions)
375     return;
376   m_memory_regions.emplace();
377   bool is_complete;
378   std::tie(*m_memory_regions, is_complete) =
379       m_minidump_parser->BuildMemoryRegions();
380 
381   if (is_complete)
382     return;
383 
384   MemoryRegionInfos to_add;
385   ModuleList &modules = GetTarget().GetImages();
386   Target &target = GetTarget();
387   modules.ForEach([&](const ModuleSP &module_sp) {
388     SectionList *sections = module_sp->GetSectionList();
389     for (size_t i = 0; i < sections->GetSize(); ++i) {
390       SectionSP section_sp = sections->GetSectionAtIndex(i);
391       addr_t load_addr = target.GetSectionLoadAddress(section_sp);
392       if (load_addr == LLDB_INVALID_ADDRESS)
393         continue;
394       MemoryRegionInfo::RangeType section_range(load_addr,
395                                                 section_sp->GetByteSize());
396       MemoryRegionInfo region =
397           MinidumpParser::GetMemoryRegionInfo(*m_memory_regions, load_addr);
398       if (region.GetMapped() != MemoryRegionInfo::eYes &&
399           region.GetRange().GetRangeBase() <= section_range.GetRangeBase() &&
400           section_range.GetRangeEnd() <= region.GetRange().GetRangeEnd()) {
401         to_add.emplace_back();
402         to_add.back().GetRange() = section_range;
403         to_add.back().SetLLDBPermissions(section_sp->GetPermissions());
404         to_add.back().SetMapped(MemoryRegionInfo::eYes);
405         to_add.back().SetName(module_sp->GetFileSpec().GetPath().c_str());
406       }
407     }
408     return true;
409   });
410   m_memory_regions->insert(m_memory_regions->end(), to_add.begin(),
411                            to_add.end());
412   llvm::sort(*m_memory_regions);
413 }
414 
415 Status ProcessMinidump::DoGetMemoryRegionInfo(lldb::addr_t load_addr,
416                                               MemoryRegionInfo &region) {
417   BuildMemoryRegions();
418   region = MinidumpParser::GetMemoryRegionInfo(*m_memory_regions, load_addr);
419   return Status();
420 }
421 
422 Status ProcessMinidump::GetMemoryRegions(MemoryRegionInfos &region_list) {
423   BuildMemoryRegions();
424   region_list = *m_memory_regions;
425   return Status();
426 }
427 
428 void ProcessMinidump::Clear() { Process::m_thread_list.Clear(); }
429 
430 bool ProcessMinidump::DoUpdateThreadList(ThreadList &old_thread_list,
431                                          ThreadList &new_thread_list) {
432   for (const minidump::Thread &thread : m_thread_list) {
433     LocationDescriptor context_location = thread.Context;
434 
435     // If the minidump contains an exception context, use it
436     if (auto it = m_exceptions_by_tid.find(thread.ThreadId);
437         it != m_exceptions_by_tid.end())
438       context_location = it->second.ThreadContext;
439 
440     llvm::ArrayRef<uint8_t> context;
441     if (!m_is_wow64)
442       context = m_minidump_parser->GetThreadContext(context_location);
443     else
444       context = m_minidump_parser->GetThreadContextWow64(thread);
445 
446     lldb::ThreadSP thread_sp(new ThreadMinidump(*this, thread, context));
447     new_thread_list.AddThread(thread_sp);
448   }
449   return new_thread_list.GetSize(false) > 0;
450 }
451 
452 ModuleSP ProcessMinidump::GetOrCreateModule(UUID minidump_uuid,
453                                             llvm::StringRef name,
454                                             ModuleSpec module_spec) {
455   Log *log = GetLog(LLDBLog::DynamicLoader);
456   Status error;
457 
458   ModuleSP module_sp =
459       GetTarget().GetOrCreateModule(module_spec, true /* notify */, &error);
460   if (!module_sp)
461     return module_sp;
462   // We consider the module to be a match if the minidump UUID is a
463   // prefix of the actual UUID, or if either of the UUIDs are empty.
464   const auto dmp_bytes = minidump_uuid.GetBytes();
465   const auto mod_bytes = module_sp->GetUUID().GetBytes();
466   const bool match = dmp_bytes.empty() || mod_bytes.empty() ||
467                      mod_bytes.take_front(dmp_bytes.size()) == dmp_bytes;
468   if (match) {
469     LLDB_LOG(log, "Partial uuid match for {0}.", name);
470     return module_sp;
471   }
472 
473   // Breakpad generates minindump files, and if there is no GNU build
474   // ID in the binary, it will calculate a UUID by hashing first 4096
475   // bytes of the .text section and using that as the UUID for a module
476   // in the minidump. Facebook uses a modified breakpad client that
477   // uses a slightly modified this hash to avoid collisions. Check for
478   // UUIDs from the minindump that match these cases and accept the
479   // module we find if they do match.
480   std::vector<uint8_t> breakpad_uuid;
481   std::vector<uint8_t> facebook_uuid;
482   HashElfTextSection(module_sp, breakpad_uuid, facebook_uuid);
483   if (dmp_bytes == llvm::ArrayRef<uint8_t>(breakpad_uuid)) {
484     LLDB_LOG(log, "Breakpad .text hash match for {0}.", name);
485     return module_sp;
486   }
487   if (dmp_bytes == llvm::ArrayRef<uint8_t>(facebook_uuid)) {
488     LLDB_LOG(log, "Facebook .text hash match for {0}.", name);
489     return module_sp;
490   }
491   // The UUID wasn't a partial match and didn't match the .text hash
492   // so remove the module from the target, we will need to create a
493   // placeholder object file.
494   GetTarget().GetImages().Remove(module_sp);
495   module_sp.reset();
496   return module_sp;
497 }
498 
499 void ProcessMinidump::ReadModuleList() {
500   std::vector<const minidump::Module *> filtered_modules =
501       m_minidump_parser->GetFilteredModuleList();
502 
503   Log *log = GetLog(LLDBLog::DynamicLoader);
504 
505   for (auto module : filtered_modules) {
506     std::string name = cantFail(m_minidump_parser->GetMinidumpFile().getString(
507         module->ModuleNameRVA));
508     const uint64_t load_addr = module->BaseOfImage;
509     const uint64_t load_size = module->SizeOfImage;
510     LLDB_LOG(log, "found module: name: {0} {1:x10}-{2:x10} size: {3}", name,
511              load_addr, load_addr + load_size, load_size);
512 
513     // check if the process is wow64 - a 32 bit windows process running on a
514     // 64 bit windows
515     if (llvm::StringRef(name).ends_with_insensitive("wow64.dll")) {
516       m_is_wow64 = true;
517     }
518 
519     const auto uuid = m_minidump_parser->GetModuleUUID(module);
520     auto file_spec = FileSpec(name, GetArchitecture().GetTriple());
521     ModuleSpec module_spec(file_spec, uuid);
522     module_spec.GetArchitecture() = GetArchitecture();
523     Status error;
524     // Try and find a module with a full UUID that matches. This function will
525     // add the module to the target if it finds one.
526     lldb::ModuleSP module_sp = GetTarget().GetOrCreateModule(module_spec,
527                                                      true /* notify */, &error);
528     if (module_sp) {
529       LLDB_LOG(log, "Full uuid match for {0}.", name);
530     } else {
531       // We couldn't find a module with an exactly-matching UUID.  Sometimes
532       // a minidump UUID is only a partial match or is a hash.  So try again
533       // without specifying the UUID, then again without specifying the
534       // directory if that fails.  This will allow us to find modules with
535       // partial matches or hash UUIDs in user-provided sysroots or search
536       // directories (target.exec-search-paths).
537       ModuleSpec partial_module_spec = module_spec;
538       partial_module_spec.GetUUID().Clear();
539       module_sp = GetOrCreateModule(uuid, name, partial_module_spec);
540       if (!module_sp) {
541         partial_module_spec.GetFileSpec().ClearDirectory();
542         module_sp = GetOrCreateModule(uuid, name, partial_module_spec);
543       }
544     }
545     if (module_sp) {
546       // Watch out for place holder modules that have different paths, but the
547       // same UUID. If the base address is different, create a new module. If
548       // we don't then we will end up setting the load address of a different
549       // ObjectFilePlaceholder and an assertion will fire.
550       auto *objfile = module_sp->GetObjectFile();
551       if (objfile &&
552           objfile->GetPluginName() ==
553               ObjectFilePlaceholder::GetPluginNameStatic()) {
554         if (((ObjectFilePlaceholder *)objfile)->GetBaseImageAddress() !=
555             load_addr)
556           module_sp.reset();
557       }
558     }
559     if (!module_sp) {
560       // We failed to locate a matching local object file. Fortunately, the
561       // minidump format encodes enough information about each module's memory
562       // range to allow us to create placeholder modules.
563       //
564       // This enables most LLDB functionality involving address-to-module
565       // translations (ex. identifing the module for a stack frame PC) and
566       // modules/sections commands (ex. target modules list, ...)
567       LLDB_LOG(log,
568                "Unable to locate the matching object file, creating a "
569                "placeholder module for: {0}",
570                name);
571 
572       module_sp = Module::CreateModuleFromObjectFile<ObjectFilePlaceholder>(
573           module_spec, load_addr, load_size);
574       // If we haven't loaded a main executable yet, set the first module to be
575       // main executable
576       if (!GetTarget().GetExecutableModule())
577         GetTarget().SetExecutableModule(module_sp);
578       else
579         GetTarget().GetImages().Append(module_sp, true /* notify */);
580     }
581 
582     bool load_addr_changed = false;
583     module_sp->SetLoadAddress(GetTarget(), load_addr, false,
584                               load_addr_changed);
585   }
586 }
587 
588 bool ProcessMinidump::GetProcessInfo(ProcessInstanceInfo &info) {
589   info.Clear();
590   info.SetProcessID(GetID());
591   info.SetArchitecture(GetArchitecture());
592   lldb::ModuleSP module_sp = GetTarget().GetExecutableModule();
593   if (module_sp) {
594     const bool add_exe_file_as_first_arg = false;
595     info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
596                            add_exe_file_as_first_arg);
597   }
598   return true;
599 }
600 
601 // For minidumps there's no runtime generated code so we don't need JITLoader(s)
602 // Avoiding them will also speed up minidump loading since JITLoaders normally
603 // try to set up symbolic breakpoints, which in turn may force loading more
604 // debug information than needed.
605 JITLoaderList &ProcessMinidump::GetJITLoaders() {
606   if (!m_jit_loaders_up) {
607     m_jit_loaders_up = std::make_unique<JITLoaderList>();
608   }
609   return *m_jit_loaders_up;
610 }
611 
612 #define INIT_BOOL(VAR, LONG, SHORT, DESC) \
613     VAR(LLDB_OPT_SET_1, false, LONG, SHORT, DESC, false, true)
614 #define APPEND_OPT(VAR) \
615     m_option_group.Append(&VAR, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1)
616 
617 class CommandObjectProcessMinidumpDump : public CommandObjectParsed {
618 private:
619   OptionGroupOptions m_option_group;
620   OptionGroupBoolean m_dump_all;
621   OptionGroupBoolean m_dump_directory;
622   OptionGroupBoolean m_dump_linux_cpuinfo;
623   OptionGroupBoolean m_dump_linux_proc_status;
624   OptionGroupBoolean m_dump_linux_lsb_release;
625   OptionGroupBoolean m_dump_linux_cmdline;
626   OptionGroupBoolean m_dump_linux_environ;
627   OptionGroupBoolean m_dump_linux_auxv;
628   OptionGroupBoolean m_dump_linux_maps;
629   OptionGroupBoolean m_dump_linux_proc_stat;
630   OptionGroupBoolean m_dump_linux_proc_uptime;
631   OptionGroupBoolean m_dump_linux_proc_fd;
632   OptionGroupBoolean m_dump_linux_all;
633   OptionGroupBoolean m_fb_app_data;
634   OptionGroupBoolean m_fb_build_id;
635   OptionGroupBoolean m_fb_version;
636   OptionGroupBoolean m_fb_java_stack;
637   OptionGroupBoolean m_fb_dalvik;
638   OptionGroupBoolean m_fb_unwind;
639   OptionGroupBoolean m_fb_error_log;
640   OptionGroupBoolean m_fb_app_state;
641   OptionGroupBoolean m_fb_abort;
642   OptionGroupBoolean m_fb_thread;
643   OptionGroupBoolean m_fb_logcat;
644   OptionGroupBoolean m_fb_all;
645 
646   void SetDefaultOptionsIfNoneAreSet() {
647     if (m_dump_all.GetOptionValue().GetCurrentValue() ||
648         m_dump_linux_all.GetOptionValue().GetCurrentValue() ||
649         m_fb_all.GetOptionValue().GetCurrentValue() ||
650         m_dump_directory.GetOptionValue().GetCurrentValue() ||
651         m_dump_linux_cpuinfo.GetOptionValue().GetCurrentValue() ||
652         m_dump_linux_proc_status.GetOptionValue().GetCurrentValue() ||
653         m_dump_linux_lsb_release.GetOptionValue().GetCurrentValue() ||
654         m_dump_linux_cmdline.GetOptionValue().GetCurrentValue() ||
655         m_dump_linux_environ.GetOptionValue().GetCurrentValue() ||
656         m_dump_linux_auxv.GetOptionValue().GetCurrentValue() ||
657         m_dump_linux_maps.GetOptionValue().GetCurrentValue() ||
658         m_dump_linux_proc_stat.GetOptionValue().GetCurrentValue() ||
659         m_dump_linux_proc_uptime.GetOptionValue().GetCurrentValue() ||
660         m_dump_linux_proc_fd.GetOptionValue().GetCurrentValue() ||
661         m_fb_app_data.GetOptionValue().GetCurrentValue() ||
662         m_fb_build_id.GetOptionValue().GetCurrentValue() ||
663         m_fb_version.GetOptionValue().GetCurrentValue() ||
664         m_fb_java_stack.GetOptionValue().GetCurrentValue() ||
665         m_fb_dalvik.GetOptionValue().GetCurrentValue() ||
666         m_fb_unwind.GetOptionValue().GetCurrentValue() ||
667         m_fb_error_log.GetOptionValue().GetCurrentValue() ||
668         m_fb_app_state.GetOptionValue().GetCurrentValue() ||
669         m_fb_abort.GetOptionValue().GetCurrentValue() ||
670         m_fb_thread.GetOptionValue().GetCurrentValue() ||
671         m_fb_logcat.GetOptionValue().GetCurrentValue())
672       return;
673     // If no options were set, then dump everything
674     m_dump_all.GetOptionValue().SetCurrentValue(true);
675   }
676   bool DumpAll() const {
677     return m_dump_all.GetOptionValue().GetCurrentValue();
678   }
679   bool DumpDirectory() const {
680     return DumpAll() ||
681         m_dump_directory.GetOptionValue().GetCurrentValue();
682   }
683   bool DumpLinux() const {
684     return DumpAll() || m_dump_linux_all.GetOptionValue().GetCurrentValue();
685   }
686   bool DumpLinuxCPUInfo() const {
687     return DumpLinux() ||
688         m_dump_linux_cpuinfo.GetOptionValue().GetCurrentValue();
689   }
690   bool DumpLinuxProcStatus() const {
691     return DumpLinux() ||
692         m_dump_linux_proc_status.GetOptionValue().GetCurrentValue();
693   }
694   bool DumpLinuxProcStat() const {
695     return DumpLinux() ||
696         m_dump_linux_proc_stat.GetOptionValue().GetCurrentValue();
697   }
698   bool DumpLinuxLSBRelease() const {
699     return DumpLinux() ||
700         m_dump_linux_lsb_release.GetOptionValue().GetCurrentValue();
701   }
702   bool DumpLinuxCMDLine() const {
703     return DumpLinux() ||
704         m_dump_linux_cmdline.GetOptionValue().GetCurrentValue();
705   }
706   bool DumpLinuxEnviron() const {
707     return DumpLinux() ||
708         m_dump_linux_environ.GetOptionValue().GetCurrentValue();
709   }
710   bool DumpLinuxAuxv() const {
711     return DumpLinux() ||
712         m_dump_linux_auxv.GetOptionValue().GetCurrentValue();
713   }
714   bool DumpLinuxMaps() const {
715     return DumpLinux() ||
716         m_dump_linux_maps.GetOptionValue().GetCurrentValue();
717   }
718   bool DumpLinuxProcUptime() const {
719     return DumpLinux() ||
720         m_dump_linux_proc_uptime.GetOptionValue().GetCurrentValue();
721   }
722   bool DumpLinuxProcFD() const {
723     return DumpLinux() ||
724         m_dump_linux_proc_fd.GetOptionValue().GetCurrentValue();
725   }
726   bool DumpFacebook() const {
727     return DumpAll() || m_fb_all.GetOptionValue().GetCurrentValue();
728   }
729   bool DumpFacebookAppData() const {
730     return DumpFacebook() || m_fb_app_data.GetOptionValue().GetCurrentValue();
731   }
732   bool DumpFacebookBuildID() const {
733     return DumpFacebook() || m_fb_build_id.GetOptionValue().GetCurrentValue();
734   }
735   bool DumpFacebookVersionName() const {
736     return DumpFacebook() || m_fb_version.GetOptionValue().GetCurrentValue();
737   }
738   bool DumpFacebookJavaStack() const {
739     return DumpFacebook() || m_fb_java_stack.GetOptionValue().GetCurrentValue();
740   }
741   bool DumpFacebookDalvikInfo() const {
742     return DumpFacebook() || m_fb_dalvik.GetOptionValue().GetCurrentValue();
743   }
744   bool DumpFacebookUnwindSymbols() const {
745     return DumpFacebook() || m_fb_unwind.GetOptionValue().GetCurrentValue();
746   }
747   bool DumpFacebookErrorLog() const {
748     return DumpFacebook() || m_fb_error_log.GetOptionValue().GetCurrentValue();
749   }
750   bool DumpFacebookAppStateLog() const {
751     return DumpFacebook() || m_fb_app_state.GetOptionValue().GetCurrentValue();
752   }
753   bool DumpFacebookAbortReason() const {
754     return DumpFacebook() || m_fb_abort.GetOptionValue().GetCurrentValue();
755   }
756   bool DumpFacebookThreadName() const {
757     return DumpFacebook() || m_fb_thread.GetOptionValue().GetCurrentValue();
758   }
759   bool DumpFacebookLogcat() const {
760     return DumpFacebook() || m_fb_logcat.GetOptionValue().GetCurrentValue();
761   }
762 public:
763   CommandObjectProcessMinidumpDump(CommandInterpreter &interpreter)
764   : CommandObjectParsed(interpreter, "process plugin dump",
765       "Dump information from the minidump file.", nullptr),
766     m_option_group(),
767     INIT_BOOL(m_dump_all, "all", 'a',
768               "Dump the everything in the minidump."),
769     INIT_BOOL(m_dump_directory, "directory", 'd',
770               "Dump the minidump directory map."),
771     INIT_BOOL(m_dump_linux_cpuinfo, "cpuinfo", 'C',
772               "Dump linux /proc/cpuinfo."),
773     INIT_BOOL(m_dump_linux_proc_status, "status", 's',
774               "Dump linux /proc/<pid>/status."),
775     INIT_BOOL(m_dump_linux_lsb_release, "lsb-release", 'r',
776               "Dump linux /etc/lsb-release."),
777     INIT_BOOL(m_dump_linux_cmdline, "cmdline", 'c',
778               "Dump linux /proc/<pid>/cmdline."),
779     INIT_BOOL(m_dump_linux_environ, "environ", 'e',
780               "Dump linux /proc/<pid>/environ."),
781     INIT_BOOL(m_dump_linux_auxv, "auxv", 'x',
782               "Dump linux /proc/<pid>/auxv."),
783     INIT_BOOL(m_dump_linux_maps, "maps", 'm',
784               "Dump linux /proc/<pid>/maps."),
785     INIT_BOOL(m_dump_linux_proc_stat, "stat", 'S',
786               "Dump linux /proc/<pid>/stat."),
787     INIT_BOOL(m_dump_linux_proc_uptime, "uptime", 'u',
788               "Dump linux process uptime."),
789     INIT_BOOL(m_dump_linux_proc_fd, "fd", 'f',
790               "Dump linux /proc/<pid>/fd."),
791     INIT_BOOL(m_dump_linux_all, "linux", 'l',
792               "Dump all linux streams."),
793     INIT_BOOL(m_fb_app_data, "fb-app-data", 1,
794               "Dump Facebook application custom data."),
795     INIT_BOOL(m_fb_build_id, "fb-build-id", 2,
796               "Dump the Facebook build ID."),
797     INIT_BOOL(m_fb_version, "fb-version", 3,
798               "Dump Facebook application version string."),
799     INIT_BOOL(m_fb_java_stack, "fb-java-stack", 4,
800               "Dump Facebook java stack."),
801     INIT_BOOL(m_fb_dalvik, "fb-dalvik-info", 5,
802               "Dump Facebook Dalvik info."),
803     INIT_BOOL(m_fb_unwind, "fb-unwind-symbols", 6,
804               "Dump Facebook unwind symbols."),
805     INIT_BOOL(m_fb_error_log, "fb-error-log", 7,
806               "Dump Facebook error log."),
807     INIT_BOOL(m_fb_app_state, "fb-app-state-log", 8,
808               "Dump Facebook java stack."),
809     INIT_BOOL(m_fb_abort, "fb-abort-reason", 9,
810               "Dump Facebook abort reason."),
811     INIT_BOOL(m_fb_thread, "fb-thread-name", 10,
812               "Dump Facebook thread name."),
813     INIT_BOOL(m_fb_logcat, "fb-logcat", 11,
814               "Dump Facebook logcat."),
815     INIT_BOOL(m_fb_all, "facebook", 12, "Dump all Facebook streams.") {
816     APPEND_OPT(m_dump_all);
817     APPEND_OPT(m_dump_directory);
818     APPEND_OPT(m_dump_linux_cpuinfo);
819     APPEND_OPT(m_dump_linux_proc_status);
820     APPEND_OPT(m_dump_linux_lsb_release);
821     APPEND_OPT(m_dump_linux_cmdline);
822     APPEND_OPT(m_dump_linux_environ);
823     APPEND_OPT(m_dump_linux_auxv);
824     APPEND_OPT(m_dump_linux_maps);
825     APPEND_OPT(m_dump_linux_proc_stat);
826     APPEND_OPT(m_dump_linux_proc_uptime);
827     APPEND_OPT(m_dump_linux_proc_fd);
828     APPEND_OPT(m_dump_linux_all);
829     APPEND_OPT(m_fb_app_data);
830     APPEND_OPT(m_fb_build_id);
831     APPEND_OPT(m_fb_version);
832     APPEND_OPT(m_fb_java_stack);
833     APPEND_OPT(m_fb_dalvik);
834     APPEND_OPT(m_fb_unwind);
835     APPEND_OPT(m_fb_error_log);
836     APPEND_OPT(m_fb_app_state);
837     APPEND_OPT(m_fb_abort);
838     APPEND_OPT(m_fb_thread);
839     APPEND_OPT(m_fb_logcat);
840     APPEND_OPT(m_fb_all);
841     m_option_group.Finalize();
842   }
843 
844   ~CommandObjectProcessMinidumpDump() override = default;
845 
846   Options *GetOptions() override { return &m_option_group; }
847 
848   void DoExecute(Args &command, CommandReturnObject &result) override {
849     const size_t argc = command.GetArgumentCount();
850     if (argc > 0) {
851       result.AppendErrorWithFormat("'%s' take no arguments, only options",
852                                    m_cmd_name.c_str());
853       return;
854     }
855     SetDefaultOptionsIfNoneAreSet();
856 
857     ProcessMinidump *process = static_cast<ProcessMinidump *>(
858         m_interpreter.GetExecutionContext().GetProcessPtr());
859     result.SetStatus(eReturnStatusSuccessFinishResult);
860     Stream &s = result.GetOutputStream();
861     MinidumpParser &minidump = *process->m_minidump_parser;
862     if (DumpDirectory()) {
863       s.Printf("RVA        SIZE       TYPE       StreamType\n");
864       s.Printf("---------- ---------- ---------- --------------------------\n");
865       for (const auto &stream_desc : minidump.GetMinidumpFile().streams())
866         s.Printf(
867             "0x%8.8x 0x%8.8x 0x%8.8x %s\n", (uint32_t)stream_desc.Location.RVA,
868             (uint32_t)stream_desc.Location.DataSize,
869             (unsigned)(StreamType)stream_desc.Type,
870             MinidumpParser::GetStreamTypeAsString(stream_desc.Type).data());
871       s.Printf("\n");
872     }
873     auto DumpTextStream = [&](StreamType stream_type,
874                               llvm::StringRef label) -> void {
875       auto bytes = minidump.GetStream(stream_type);
876       if (!bytes.empty()) {
877         if (label.empty())
878           label = MinidumpParser::GetStreamTypeAsString(stream_type);
879         s.Printf("%s:\n%s\n\n", label.data(), bytes.data());
880       }
881     };
882     auto DumpBinaryStream = [&](StreamType stream_type,
883                                 llvm::StringRef label) -> void {
884       auto bytes = minidump.GetStream(stream_type);
885       if (!bytes.empty()) {
886         if (label.empty())
887           label = MinidumpParser::GetStreamTypeAsString(stream_type);
888         s.Printf("%s:\n", label.data());
889         DataExtractor data(bytes.data(), bytes.size(), eByteOrderLittle,
890                            process->GetAddressByteSize());
891         DumpDataExtractor(data, &s, 0, lldb::eFormatBytesWithASCII, 1,
892                           bytes.size(), 16, 0, 0, 0);
893         s.Printf("\n\n");
894       }
895     };
896 
897     if (DumpLinuxCPUInfo())
898       DumpTextStream(StreamType::LinuxCPUInfo, "/proc/cpuinfo");
899     if (DumpLinuxProcStatus())
900       DumpTextStream(StreamType::LinuxProcStatus, "/proc/PID/status");
901     if (DumpLinuxLSBRelease())
902       DumpTextStream(StreamType::LinuxLSBRelease, "/etc/lsb-release");
903     if (DumpLinuxCMDLine())
904       DumpTextStream(StreamType::LinuxCMDLine, "/proc/PID/cmdline");
905     if (DumpLinuxEnviron())
906       DumpTextStream(StreamType::LinuxEnviron, "/proc/PID/environ");
907     if (DumpLinuxAuxv())
908       DumpBinaryStream(StreamType::LinuxAuxv, "/proc/PID/auxv");
909     if (DumpLinuxMaps())
910       DumpTextStream(StreamType::LinuxMaps, "/proc/PID/maps");
911     if (DumpLinuxProcStat())
912       DumpTextStream(StreamType::LinuxProcStat, "/proc/PID/stat");
913     if (DumpLinuxProcUptime())
914       DumpTextStream(StreamType::LinuxProcUptime, "uptime");
915     if (DumpLinuxProcFD())
916       DumpTextStream(StreamType::LinuxProcFD, "/proc/PID/fd");
917     if (DumpFacebookAppData())
918       DumpTextStream(StreamType::FacebookAppCustomData,
919                      "Facebook App Data");
920     if (DumpFacebookBuildID()) {
921       auto bytes = minidump.GetStream(StreamType::FacebookBuildID);
922       if (bytes.size() >= 4) {
923         DataExtractor data(bytes.data(), bytes.size(), eByteOrderLittle,
924                            process->GetAddressByteSize());
925         lldb::offset_t offset = 0;
926         uint32_t build_id = data.GetU32(&offset);
927         s.Printf("Facebook Build ID:\n");
928         s.Printf("%u\n", build_id);
929         s.Printf("\n");
930       }
931     }
932     if (DumpFacebookVersionName())
933       DumpTextStream(StreamType::FacebookAppVersionName,
934                      "Facebook Version String");
935     if (DumpFacebookJavaStack())
936       DumpTextStream(StreamType::FacebookJavaStack,
937                      "Facebook Java Stack");
938     if (DumpFacebookDalvikInfo())
939       DumpTextStream(StreamType::FacebookDalvikInfo,
940                      "Facebook Dalvik Info");
941     if (DumpFacebookUnwindSymbols())
942       DumpBinaryStream(StreamType::FacebookUnwindSymbols,
943                        "Facebook Unwind Symbols Bytes");
944     if (DumpFacebookErrorLog())
945       DumpTextStream(StreamType::FacebookDumpErrorLog,
946                      "Facebook Error Log");
947     if (DumpFacebookAppStateLog())
948       DumpTextStream(StreamType::FacebookAppStateLog,
949                      "Faceook Application State Log");
950     if (DumpFacebookAbortReason())
951       DumpTextStream(StreamType::FacebookAbortReason,
952                      "Facebook Abort Reason");
953     if (DumpFacebookThreadName())
954       DumpTextStream(StreamType::FacebookThreadName,
955                      "Facebook Thread Name");
956     if (DumpFacebookLogcat())
957       DumpTextStream(StreamType::FacebookLogcat, "Facebook Logcat");
958   }
959 };
960 
961 class CommandObjectMultiwordProcessMinidump : public CommandObjectMultiword {
962 public:
963   CommandObjectMultiwordProcessMinidump(CommandInterpreter &interpreter)
964     : CommandObjectMultiword(interpreter, "process plugin",
965           "Commands for operating on a ProcessMinidump process.",
966           "process plugin <subcommand> [<subcommand-options>]") {
967     LoadSubCommand("dump",
968         CommandObjectSP(new CommandObjectProcessMinidumpDump(interpreter)));
969   }
970 
971   ~CommandObjectMultiwordProcessMinidump() override = default;
972 };
973 
974 CommandObject *ProcessMinidump::GetPluginCommandObject() {
975   if (!m_command_sp)
976     m_command_sp = std::make_shared<CommandObjectMultiwordProcessMinidump>(
977         GetTarget().GetDebugger().GetCommandInterpreter());
978   return m_command_sp.get();
979 }
980