1 //===-- ProcessMinidump.cpp -------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "ProcessMinidump.h"
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/JITLoaderList.h"
25 #include "lldb/Target/MemoryRegionInfo.h"
26 #include "lldb/Target/SectionLoadList.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Target/UnixSignals.h"
29 #include "lldb/Utility/LLDBAssert.h"
30 #include "lldb/Utility/Log.h"
31 #include "lldb/Utility/State.h"
32 
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/Support/Threading.h"
35 
36 #include "Plugins/Process/Utility/StopInfoMachException.h"
37 // C includes
38 // C++ includes
39 
40 using namespace lldb;
41 using namespace lldb_private;
42 using namespace minidump;
43 
44 //------------------------------------------------------------------
45 /// A placeholder module used for minidumps, where the original
46 /// object files may not be available (so we can't parse the object
47 /// files to extract the set of sections/segments)
48 ///
49 /// This placeholder module has a single synthetic section (.module_image)
50 /// which represents the module memory range covering the whole module.
51 //------------------------------------------------------------------
52 class PlaceholderModule : public Module {
53 public:
54   PlaceholderModule(const ModuleSpec &module_spec) :
55     Module(module_spec.GetFileSpec(), module_spec.GetArchitecture()) {
56     if (module_spec.GetUUID().IsValid())
57       SetUUID(module_spec.GetUUID());
58   }
59 
60   // Creates a synthetic module section covering the whole module image (and
61   // sets the section load address as well)
62   void CreateImageSection(const MinidumpModule *module, Target& target) {
63     const ConstString section_name(".module_image");
64     lldb::SectionSP section_sp(new Section(
65         shared_from_this(),     // Module to which this section belongs.
66         nullptr,                // ObjectFile
67         0,                      // Section ID.
68         section_name,           // Section name.
69         eSectionTypeContainer,  // Section type.
70         module->base_of_image,  // VM address.
71         module->size_of_image,  // VM size in bytes of this section.
72         0,                      // Offset of this section in the file.
73         module->size_of_image,  // Size of the section as found in the file.
74         12,                     // Alignment of the section (log2)
75         0,                      // Flags for this section.
76         1));                    // Number of host bytes per target byte
77     section_sp->SetPermissions(ePermissionsExecutable | ePermissionsReadable);
78     GetSectionList()->AddSection(section_sp);
79     target.GetSectionLoadList().SetSectionLoadAddress(
80         section_sp, module->base_of_image);
81   }
82 
83   ObjectFile *GetObjectFile() override { return nullptr; }
84 
85   SectionList *GetSectionList() override {
86     return Module::GetUnifiedSectionList();
87   }
88 };
89 
90 ConstString ProcessMinidump::GetPluginNameStatic() {
91   static ConstString g_name("minidump");
92   return g_name;
93 }
94 
95 const char *ProcessMinidump::GetPluginDescriptionStatic() {
96   return "Minidump plug-in.";
97 }
98 
99 lldb::ProcessSP ProcessMinidump::CreateInstance(lldb::TargetSP target_sp,
100                                                 lldb::ListenerSP listener_sp,
101                                                 const FileSpec *crash_file) {
102   if (!crash_file)
103     return nullptr;
104 
105   lldb::ProcessSP process_sp;
106   // Read enough data for the Minidump header
107   constexpr size_t header_size = sizeof(MinidumpHeader);
108   auto DataPtr = FileSystem::Instance().CreateDataBuffer(crash_file->GetPath(),
109                                                          header_size, 0);
110   if (!DataPtr)
111     return nullptr;
112 
113   lldbassert(DataPtr->GetByteSize() == header_size);
114 
115   // first, only try to parse the header, beacuse we need to be fast
116   llvm::ArrayRef<uint8_t> HeaderBytes = DataPtr->GetData();
117   const MinidumpHeader *header = MinidumpHeader::Parse(HeaderBytes);
118   if (header == nullptr)
119     return nullptr;
120 
121   auto AllData =
122       FileSystem::Instance().CreateDataBuffer(crash_file->GetPath(), -1, 0);
123   if (!AllData)
124     return nullptr;
125 
126   auto minidump_parser = MinidumpParser::Create(AllData);
127   // check if the parser object is valid
128   if (!minidump_parser)
129     return nullptr;
130 
131   return std::make_shared<ProcessMinidump>(target_sp, listener_sp, *crash_file,
132                                            minidump_parser.getValue());
133 }
134 
135 bool ProcessMinidump::CanDebug(lldb::TargetSP target_sp,
136                                bool plugin_specified_by_name) {
137   return true;
138 }
139 
140 ProcessMinidump::ProcessMinidump(lldb::TargetSP target_sp,
141                                  lldb::ListenerSP listener_sp,
142                                  const FileSpec &core_file,
143                                  MinidumpParser minidump_parser)
144     : Process(target_sp, listener_sp), m_minidump_parser(minidump_parser),
145       m_core_file(core_file), m_is_wow64(false) {}
146 
147 ProcessMinidump::~ProcessMinidump() {
148   Clear();
149   // We need to call finalize on the process before destroying ourselves to
150   // make sure all of the broadcaster cleanup goes as planned. If we destruct
151   // this class, then Process::~Process() might have problems trying to fully
152   // destroy the broadcaster.
153   Finalize();
154 }
155 
156 void ProcessMinidump::Initialize() {
157   static llvm::once_flag g_once_flag;
158 
159   llvm::call_once(g_once_flag, []() {
160     PluginManager::RegisterPlugin(GetPluginNameStatic(),
161                                   GetPluginDescriptionStatic(),
162                                   ProcessMinidump::CreateInstance);
163   });
164 }
165 
166 void ProcessMinidump::Terminate() {
167   PluginManager::UnregisterPlugin(ProcessMinidump::CreateInstance);
168 }
169 
170 Status ProcessMinidump::DoLoadCore() {
171   Status error;
172 
173   // Minidump parser initialization & consistency checks
174   error = m_minidump_parser.Initialize();
175   if (error.Fail())
176     return error;
177 
178   // Do we support the minidump's architecture?
179   ArchSpec arch = GetArchitecture();
180   switch (arch.GetMachine()) {
181   case llvm::Triple::x86:
182   case llvm::Triple::x86_64:
183   case llvm::Triple::arm:
184   case llvm::Triple::aarch64:
185     // Any supported architectures must be listed here and also supported in
186     // ThreadMinidump::CreateRegisterContextForFrame().
187     break;
188   default:
189     error.SetErrorStringWithFormat("unsupported minidump architecture: %s",
190                                    arch.GetArchitectureName());
191     return error;
192   }
193   GetTarget().SetArchitecture(arch, true /*set_platform*/);
194 
195   m_thread_list = m_minidump_parser.GetThreads();
196   m_active_exception = m_minidump_parser.GetExceptionStream();
197   ReadModuleList();
198 
199   llvm::Optional<lldb::pid_t> pid = m_minidump_parser.GetPid();
200   if (!pid) {
201     error.SetErrorString("failed to parse PID");
202     return error;
203   }
204   SetID(pid.getValue());
205 
206   return error;
207 }
208 
209 ConstString ProcessMinidump::GetPluginName() { return GetPluginNameStatic(); }
210 
211 uint32_t ProcessMinidump::GetPluginVersion() { return 1; }
212 
213 Status ProcessMinidump::DoDestroy() { return Status(); }
214 
215 void ProcessMinidump::RefreshStateAfterStop() {
216   if (!m_active_exception)
217     return;
218 
219   if (m_active_exception->exception_record.exception_code ==
220       MinidumpException::DumpRequested) {
221     return;
222   }
223 
224   lldb::StopInfoSP stop_info;
225   lldb::ThreadSP stop_thread;
226 
227   Process::m_thread_list.SetSelectedThreadByID(m_active_exception->thread_id);
228   stop_thread = Process::m_thread_list.GetSelectedThread();
229   ArchSpec arch = GetArchitecture();
230 
231   if (arch.GetTriple().getOS() == llvm::Triple::Linux) {
232     stop_info = StopInfo::CreateStopReasonWithSignal(
233         *stop_thread, m_active_exception->exception_record.exception_code);
234   } else if (arch.GetTriple().getVendor() == llvm::Triple::Apple) {
235     stop_info = StopInfoMachException::CreateStopReasonWithMachException(
236         *stop_thread, m_active_exception->exception_record.exception_code, 2,
237         m_active_exception->exception_record.exception_flags,
238         m_active_exception->exception_record.exception_address, 0);
239   } else {
240     std::string desc;
241     llvm::raw_string_ostream desc_stream(desc);
242     desc_stream << "Exception "
243                 << llvm::format_hex(
244                        m_active_exception->exception_record.exception_code, 8)
245                 << " encountered at address "
246                 << llvm::format_hex(
247                        m_active_exception->exception_record.exception_address,
248                        8);
249     stop_info = StopInfo::CreateStopReasonWithException(
250         *stop_thread, desc_stream.str().c_str());
251   }
252 
253   stop_thread->SetStopInfo(stop_info);
254 }
255 
256 bool ProcessMinidump::IsAlive() { return true; }
257 
258 bool ProcessMinidump::WarnBeforeDetach() const { return false; }
259 
260 size_t ProcessMinidump::ReadMemory(lldb::addr_t addr, void *buf, size_t size,
261                                    Status &error) {
262   // Don't allow the caching that lldb_private::Process::ReadMemory does since
263   // we have it all cached in our dump file anyway.
264   return DoReadMemory(addr, buf, size, error);
265 }
266 
267 size_t ProcessMinidump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
268                                      Status &error) {
269 
270   llvm::ArrayRef<uint8_t> mem = m_minidump_parser.GetMemory(addr, size);
271   if (mem.empty()) {
272     error.SetErrorString("could not parse memory info");
273     return 0;
274   }
275 
276   std::memcpy(buf, mem.data(), mem.size());
277   return mem.size();
278 }
279 
280 ArchSpec ProcessMinidump::GetArchitecture() {
281   if (!m_is_wow64) {
282     return m_minidump_parser.GetArchitecture();
283   }
284 
285   llvm::Triple triple;
286   triple.setVendor(llvm::Triple::VendorType::UnknownVendor);
287   triple.setArch(llvm::Triple::ArchType::x86);
288   triple.setOS(llvm::Triple::OSType::Win32);
289   return ArchSpec(triple);
290 }
291 
292 Status ProcessMinidump::GetMemoryRegionInfo(lldb::addr_t load_addr,
293                                             MemoryRegionInfo &range_info) {
294   range_info = m_minidump_parser.GetMemoryRegionInfo(load_addr);
295   return Status();
296 }
297 
298 void ProcessMinidump::Clear() { Process::m_thread_list.Clear(); }
299 
300 bool ProcessMinidump::UpdateThreadList(ThreadList &old_thread_list,
301                                        ThreadList &new_thread_list) {
302   uint32_t num_threads = 0;
303   if (m_thread_list.size() > 0)
304     num_threads = m_thread_list.size();
305 
306   for (lldb::tid_t tid = 0; tid < num_threads; ++tid) {
307     llvm::ArrayRef<uint8_t> context;
308     if (!m_is_wow64)
309       context = m_minidump_parser.GetThreadContext(m_thread_list[tid]);
310     else
311       context = m_minidump_parser.GetThreadContextWow64(m_thread_list[tid]);
312 
313     lldb::ThreadSP thread_sp(
314         new ThreadMinidump(*this, m_thread_list[tid], context));
315     new_thread_list.AddThread(thread_sp);
316   }
317   return new_thread_list.GetSize(false) > 0;
318 }
319 
320 void ProcessMinidump::ReadModuleList() {
321   std::vector<const MinidumpModule *> filtered_modules =
322       m_minidump_parser.GetFilteredModuleList();
323 
324   for (auto module : filtered_modules) {
325     llvm::Optional<std::string> name =
326         m_minidump_parser.GetMinidumpString(module->module_name_rva);
327 
328     if (!name)
329       continue;
330 
331     Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES));
332     if (log) {
333       log->Printf("ProcessMinidump::%s found module: name: %s %#010" PRIx64
334                   "-%#010" PRIx64 " size: %" PRIu32,
335                   __FUNCTION__, name.getValue().c_str(),
336                   uint64_t(module->base_of_image),
337                   module->base_of_image + module->size_of_image,
338                   uint32_t(module->size_of_image));
339     }
340 
341     // check if the process is wow64 - a 32 bit windows process running on a
342     // 64 bit windows
343     if (llvm::StringRef(name.getValue()).endswith_lower("wow64.dll")) {
344       m_is_wow64 = true;
345     }
346 
347     const auto uuid = m_minidump_parser.GetModuleUUID(module);
348     auto file_spec = FileSpec(name.getValue(), GetArchitecture().GetTriple());
349     FileSystem::Instance().Resolve(file_spec);
350     ModuleSpec module_spec(file_spec, uuid);
351     Status error;
352     lldb::ModuleSP module_sp = GetTarget().GetSharedModule(module_spec, &error);
353     if (!module_sp || error.Fail()) {
354       // We failed to locate a matching local object file. Fortunately, the
355       // minidump format encodes enough information about each module's memory
356       // range to allow us to create placeholder modules.
357       //
358       // This enables most LLDB functionality involving address-to-module
359       // translations (ex. identifing the module for a stack frame PC) and
360       // modules/sections commands (ex. target modules list, ...)
361       if (log) {
362         log->Printf("Unable to locate the matching object file, creating a "
363                     "placeholder module for: %s",
364                     name.getValue().c_str());
365       }
366 
367       auto placeholder_module =
368           std::make_shared<PlaceholderModule>(module_spec);
369       placeholder_module->CreateImageSection(module, GetTarget());
370       module_sp = placeholder_module;
371       GetTarget().GetImages().Append(module_sp);
372     }
373 
374     if (log) {
375       log->Printf("ProcessMinidump::%s load module: name: %s", __FUNCTION__,
376                   name.getValue().c_str());
377     }
378 
379     bool load_addr_changed = false;
380     module_sp->SetLoadAddress(GetTarget(), module->base_of_image, false,
381                               load_addr_changed);
382   }
383 }
384 
385 bool ProcessMinidump::GetProcessInfo(ProcessInstanceInfo &info) {
386   info.Clear();
387   info.SetProcessID(GetID());
388   info.SetArchitecture(GetArchitecture());
389   lldb::ModuleSP module_sp = GetTarget().GetExecutableModule();
390   if (module_sp) {
391     const bool add_exe_file_as_first_arg = false;
392     info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
393                            add_exe_file_as_first_arg);
394   }
395   return true;
396 }
397 
398 // For minidumps there's no runtime generated code so we don't need JITLoader(s)
399 // Avoiding them will also speed up minidump loading since JITLoaders normally
400 // try to set up symbolic breakpoints, which in turn may force loading more
401 // debug information than needed.
402 JITLoaderList &ProcessMinidump::GetJITLoaders() {
403   if (!m_jit_loaders_ap) {
404     m_jit_loaders_ap = llvm::make_unique<JITLoaderList>();
405   }
406   return *m_jit_loaders_ap;
407 }
408 
409 #define INIT_BOOL(VAR, LONG, SHORT, DESC) \
410     VAR(LLDB_OPT_SET_1, false, LONG, SHORT, DESC, false, true)
411 #define APPEND_OPT(VAR) \
412     m_option_group.Append(&VAR, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1)
413 
414 class CommandObjectProcessMinidumpDump : public CommandObjectParsed {
415 private:
416   OptionGroupOptions m_option_group;
417   OptionGroupBoolean m_dump_all;
418   OptionGroupBoolean m_dump_directory;
419   OptionGroupBoolean m_dump_linux_cpuinfo;
420   OptionGroupBoolean m_dump_linux_proc_status;
421   OptionGroupBoolean m_dump_linux_lsb_release;
422   OptionGroupBoolean m_dump_linux_cmdline;
423   OptionGroupBoolean m_dump_linux_environ;
424   OptionGroupBoolean m_dump_linux_auxv;
425   OptionGroupBoolean m_dump_linux_maps;
426   OptionGroupBoolean m_dump_linux_proc_stat;
427   OptionGroupBoolean m_dump_linux_proc_uptime;
428   OptionGroupBoolean m_dump_linux_proc_fd;
429   OptionGroupBoolean m_dump_linux_all;
430 
431   void SetDefaultOptionsIfNoneAreSet() {
432     if (m_dump_all.GetOptionValue().GetCurrentValue() ||
433         m_dump_linux_all.GetOptionValue().GetCurrentValue() ||
434         m_dump_directory.GetOptionValue().GetCurrentValue() ||
435         m_dump_linux_cpuinfo.GetOptionValue().GetCurrentValue() ||
436         m_dump_linux_proc_status.GetOptionValue().GetCurrentValue() ||
437         m_dump_linux_lsb_release.GetOptionValue().GetCurrentValue() ||
438         m_dump_linux_cmdline.GetOptionValue().GetCurrentValue() ||
439         m_dump_linux_environ.GetOptionValue().GetCurrentValue() ||
440         m_dump_linux_auxv.GetOptionValue().GetCurrentValue() ||
441         m_dump_linux_maps.GetOptionValue().GetCurrentValue() ||
442         m_dump_linux_proc_stat.GetOptionValue().GetCurrentValue() ||
443         m_dump_linux_proc_uptime.GetOptionValue().GetCurrentValue() ||
444         m_dump_linux_proc_fd.GetOptionValue().GetCurrentValue())
445       return;
446     // If no options were set, then dump everything
447     m_dump_all.GetOptionValue().SetCurrentValue(true);
448   }
449   bool DumpAll() const {
450     return m_dump_all.GetOptionValue().GetCurrentValue();
451   }
452   bool DumpDirectory() const {
453     return DumpAll() ||
454         m_dump_directory.GetOptionValue().GetCurrentValue();
455   }
456   bool DumpLinux() const {
457     return DumpAll() || m_dump_linux_all.GetOptionValue().GetCurrentValue();
458   }
459   bool DumpLinuxCPUInfo() const {
460     return DumpLinux() ||
461         m_dump_linux_cpuinfo.GetOptionValue().GetCurrentValue();
462   }
463   bool DumpLinuxProcStatus() const {
464     return DumpLinux() ||
465         m_dump_linux_proc_status.GetOptionValue().GetCurrentValue();
466   }
467   bool DumpLinuxProcStat() const {
468     return DumpLinux() ||
469         m_dump_linux_proc_stat.GetOptionValue().GetCurrentValue();
470   }
471   bool DumpLinuxLSBRelease() const {
472     return DumpLinux() ||
473         m_dump_linux_lsb_release.GetOptionValue().GetCurrentValue();
474   }
475   bool DumpLinuxCMDLine() const {
476     return DumpLinux() ||
477         m_dump_linux_cmdline.GetOptionValue().GetCurrentValue();
478   }
479   bool DumpLinuxEnviron() const {
480     return DumpLinux() ||
481         m_dump_linux_environ.GetOptionValue().GetCurrentValue();
482   }
483   bool DumpLinuxAuxv() const {
484     return DumpLinux() ||
485         m_dump_linux_auxv.GetOptionValue().GetCurrentValue();
486   }
487   bool DumpLinuxMaps() const {
488     return DumpLinux() ||
489         m_dump_linux_maps.GetOptionValue().GetCurrentValue();
490   }
491   bool DumpLinuxProcUptime() const {
492     return DumpLinux() ||
493         m_dump_linux_proc_uptime.GetOptionValue().GetCurrentValue();
494   }
495   bool DumpLinuxProcFD() const {
496     return DumpLinux() ||
497         m_dump_linux_proc_fd.GetOptionValue().GetCurrentValue();
498   }
499 public:
500 
501   CommandObjectProcessMinidumpDump(CommandInterpreter &interpreter)
502   : CommandObjectParsed(interpreter, "process plugin dump",
503       "Dump information from the minidump file.", NULL),
504     m_option_group(),
505     INIT_BOOL(m_dump_all, "all", 'a',
506               "Dump the everything in the minidump."),
507     INIT_BOOL(m_dump_directory, "directory", 'd',
508               "Dump the minidump directory map."),
509     INIT_BOOL(m_dump_linux_cpuinfo, "cpuinfo", 'C',
510               "Dump linux /proc/cpuinfo."),
511     INIT_BOOL(m_dump_linux_proc_status, "status", 's',
512               "Dump linux /proc/<pid>/status."),
513     INIT_BOOL(m_dump_linux_lsb_release, "lsb-release", 'r',
514               "Dump linux /etc/lsb-release."),
515     INIT_BOOL(m_dump_linux_cmdline, "cmdline", 'c',
516               "Dump linux /proc/<pid>/cmdline."),
517     INIT_BOOL(m_dump_linux_environ, "environ", 'e',
518               "Dump linux /proc/<pid>/environ."),
519     INIT_BOOL(m_dump_linux_auxv, "auxv", 'x',
520               "Dump linux /proc/<pid>/auxv."),
521     INIT_BOOL(m_dump_linux_maps, "maps", 'm',
522               "Dump linux /proc/<pid>/maps."),
523     INIT_BOOL(m_dump_linux_proc_stat, "stat", 'S',
524               "Dump linux /proc/<pid>/stat."),
525     INIT_BOOL(m_dump_linux_proc_uptime, "uptime", 'u',
526               "Dump linux process uptime."),
527     INIT_BOOL(m_dump_linux_proc_fd, "fd", 'f',
528               "Dump linux /proc/<pid>/fd."),
529     INIT_BOOL(m_dump_linux_all, "linux", 'l',
530               "Dump all linux streams.") {
531     APPEND_OPT(m_dump_all);
532     APPEND_OPT(m_dump_directory);
533     APPEND_OPT(m_dump_linux_cpuinfo);
534     APPEND_OPT(m_dump_linux_proc_status);
535     APPEND_OPT(m_dump_linux_lsb_release);
536     APPEND_OPT(m_dump_linux_cmdline);
537     APPEND_OPT(m_dump_linux_environ);
538     APPEND_OPT(m_dump_linux_auxv);
539     APPEND_OPT(m_dump_linux_maps);
540     APPEND_OPT(m_dump_linux_proc_stat);
541     APPEND_OPT(m_dump_linux_proc_uptime);
542     APPEND_OPT(m_dump_linux_proc_fd);
543     APPEND_OPT(m_dump_linux_all);
544     m_option_group.Finalize();
545   }
546 
547   ~CommandObjectProcessMinidumpDump() {}
548 
549   Options *GetOptions() override { return &m_option_group; }
550 
551   bool DoExecute(Args &command, CommandReturnObject &result) override {
552     const size_t argc = command.GetArgumentCount();
553     if (argc > 0) {
554       result.AppendErrorWithFormat("'%s' take no arguments, only options",
555                                    m_cmd_name.c_str());
556       result.SetStatus(eReturnStatusFailed);
557       return false;
558     }
559     SetDefaultOptionsIfNoneAreSet();
560 
561     ProcessMinidump *process = static_cast<ProcessMinidump *>(
562         m_interpreter.GetExecutionContext().GetProcessPtr());
563     result.SetStatus(eReturnStatusSuccessFinishResult);
564     Stream &s = result.GetOutputStream();
565     MinidumpParser &minidump = process->m_minidump_parser;
566     if (DumpDirectory()) {
567       s.Printf("RVA        SIZE       TYPE       MinidumpStreamType\n");
568       s.Printf("---------- ---------- ---------- --------------------------\n");
569       for (const auto &pair: minidump.GetDirectoryMap())
570         s.Printf("0x%8.8x 0x%8.8x 0x%8.8x %s\n", (uint32_t)pair.second.rva,
571                  (uint32_t)pair.second.data_size, pair.first,
572                  MinidumpParser::GetStreamTypeAsString(pair.first).data());
573       s.Printf("\n");
574     }
575     auto DumpTextStream = [&](MinidumpStreamType stream_type,
576         llvm::StringRef label = llvm::StringRef()) -> void {
577       auto bytes = minidump.GetStream(stream_type);
578       if (!bytes.empty()) {
579         if (label.empty())
580           label = MinidumpParser::GetStreamTypeAsString((uint32_t)stream_type);
581         s.Printf("%s:\n%s\n\n", label.data(), bytes.data());
582       }
583     };
584     auto DumpBinaryStream = [&](MinidumpStreamType stream_type,
585         llvm::StringRef label = llvm::StringRef()) -> void {
586       auto bytes = minidump.GetStream(stream_type);
587       if (!bytes.empty()) {
588         if (label.empty())
589           label = MinidumpParser::GetStreamTypeAsString((uint32_t)stream_type);
590         s.Printf("%s:\n", label.data());
591         DataExtractor data(bytes.data(), bytes.size(), eByteOrderLittle,
592                            process->GetAddressByteSize());
593         DumpDataExtractor(data, &s, 0, lldb::eFormatBytesWithASCII, 1,
594                           bytes.size(), 16, 0, 0, 0);
595         s.Printf("\n\n");
596       }
597     };
598 
599     if (DumpLinuxCPUInfo())
600       DumpTextStream(MinidumpStreamType::LinuxCPUInfo, "/proc/cpuinfo");
601     if (DumpLinuxProcStatus())
602       DumpTextStream(MinidumpStreamType::LinuxProcStatus, "/proc/PID/status");
603     if (DumpLinuxLSBRelease())
604       DumpTextStream(MinidumpStreamType::LinuxLSBRelease, "/etc/lsb-release");
605     if (DumpLinuxCMDLine())
606       DumpTextStream(MinidumpStreamType::LinuxCMDLine, "/proc/PID/cmdline");
607     if (DumpLinuxEnviron())
608       DumpTextStream(MinidumpStreamType::LinuxEnviron, "/proc/PID/environ");
609     if (DumpLinuxAuxv())
610       DumpBinaryStream(MinidumpStreamType::LinuxAuxv, "/proc/PID/auxv");
611     if (DumpLinuxMaps())
612       DumpTextStream(MinidumpStreamType::LinuxMaps, "/proc/PID/maps");
613     if (DumpLinuxProcStat())
614       DumpTextStream(MinidumpStreamType::LinuxProcStat, "/proc/PID/stat");
615     if (DumpLinuxProcUptime())
616       DumpTextStream(MinidumpStreamType::LinuxProcUptime, "uptime");
617     if (DumpLinuxProcFD())
618       DumpTextStream(MinidumpStreamType::LinuxProcFD, "/proc/PID/fd");
619     return true;
620   }
621 };
622 
623 class CommandObjectMultiwordProcessMinidump : public CommandObjectMultiword {
624 public:
625   CommandObjectMultiwordProcessMinidump(CommandInterpreter &interpreter)
626     : CommandObjectMultiword(interpreter, "process plugin",
627           "Commands for operating on a ProcessMinidump process.",
628           "process plugin <subcommand> [<subcommand-options>]") {
629     LoadSubCommand("dump",
630         CommandObjectSP(new CommandObjectProcessMinidumpDump(interpreter)));
631   }
632 
633   ~CommandObjectMultiwordProcessMinidump() {}
634 };
635 
636 CommandObject *ProcessMinidump::GetPluginCommandObject() {
637   if (!m_command_sp)
638     m_command_sp.reset(new CommandObjectMultiwordProcessMinidump(
639         GetTarget().GetDebugger().GetCommandInterpreter()));
640   return m_command_sp.get();
641 }
642