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