1 //===-- ProcessMinidump.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "ProcessMinidump.h"
12 #include "ThreadMinidump.h"
14 // Other libraries and framework includes
15 #include "lldb/Core/DataBufferHeap.h"
16 #include "lldb/Core/Log.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/ModuleSpec.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Core/Section.h"
21 #include "lldb/Core/State.h"
22 #include "lldb/Target/DynamicLoader.h"
23 #include "lldb/Target/MemoryRegionInfo.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Target/UnixSignals.h"
26 #include "lldb/Utility/LLDBAssert.h"
31 using namespace lldb_private;
32 using namespace minidump;
34 ConstString ProcessMinidump::GetPluginNameStatic() {
35 static ConstString g_name("minidump");
39 const char *ProcessMinidump::GetPluginDescriptionStatic() {
40 return "Minidump plug-in.";
43 lldb::ProcessSP ProcessMinidump::CreateInstance(lldb::TargetSP target_sp,
44 lldb::ListenerSP listener_sp,
45 const FileSpec *crash_file) {
49 lldb::ProcessSP process_sp;
50 // Read enough data for the Minidump header
51 const size_t header_size = sizeof(MinidumpHeader);
52 lldb::DataBufferSP data_sp(crash_file->MemoryMapFileContents(0, header_size));
56 // first, only try to parse the header, beacuse we need to be fast
57 llvm::ArrayRef<uint8_t> header_data(data_sp->GetBytes(), header_size);
58 const MinidumpHeader *header = MinidumpHeader::Parse(header_data);
60 if (data_sp->GetByteSize() != header_size || header == nullptr)
63 lldb::DataBufferSP all_data_sp(crash_file->MemoryMapFileContents());
64 auto minidump_parser = MinidumpParser::Create(all_data_sp);
65 // check if the parser object is valid
69 return std::make_shared<ProcessMinidump>(target_sp, listener_sp, *crash_file,
70 minidump_parser.getValue());
73 bool ProcessMinidump::CanDebug(lldb::TargetSP target_sp,
74 bool plugin_specified_by_name) {
78 ProcessMinidump::ProcessMinidump(lldb::TargetSP target_sp,
79 lldb::ListenerSP listener_sp,
80 const FileSpec &core_file,
81 MinidumpParser minidump_parser)
82 : Process(target_sp, listener_sp), m_minidump_parser(minidump_parser),
83 m_core_file(core_file), m_is_wow64(false) {}
85 ProcessMinidump::~ProcessMinidump() {
87 // We need to call finalize on the process before destroying ourselves
88 // to make sure all of the broadcaster cleanup goes as planned. If we
89 // destruct this class, then Process::~Process() might have problems
90 // trying to fully destroy the broadcaster.
94 void ProcessMinidump::Initialize() {
95 static std::once_flag g_once_flag;
97 std::call_once(g_once_flag, []() {
98 PluginManager::RegisterPlugin(GetPluginNameStatic(),
99 GetPluginDescriptionStatic(),
100 ProcessMinidump::CreateInstance);
104 void ProcessMinidump::Terminate() {
105 PluginManager::UnregisterPlugin(ProcessMinidump::CreateInstance);
108 Error ProcessMinidump::DoLoadCore() {
111 m_thread_list = m_minidump_parser.GetThreads();
112 m_active_exception = m_minidump_parser.GetExceptionStream();
114 GetTarget().SetArchitecture(GetArchitecture());
116 llvm::Optional<lldb::pid_t> pid = m_minidump_parser.GetPid();
118 error.SetErrorString("failed to parse PID");
121 SetID(pid.getValue());
126 DynamicLoader *ProcessMinidump::GetDynamicLoader() {
127 if (m_dyld_ap.get() == nullptr)
128 m_dyld_ap.reset(DynamicLoader::FindPlugin(this, nullptr));
129 return m_dyld_ap.get();
132 ConstString ProcessMinidump::GetPluginName() { return GetPluginNameStatic(); }
134 uint32_t ProcessMinidump::GetPluginVersion() { return 1; }
136 Error ProcessMinidump::DoDestroy() { return Error(); }
138 void ProcessMinidump::RefreshStateAfterStop() {
139 if (!m_active_exception)
142 if (m_active_exception->exception_record.exception_code ==
143 MinidumpException::DumpRequested) {
147 lldb::StopInfoSP stop_info;
148 lldb::ThreadSP stop_thread;
150 Process::m_thread_list.SetSelectedThreadByID(m_active_exception->thread_id);
151 stop_thread = Process::m_thread_list.GetSelectedThread();
152 ArchSpec arch = GetArchitecture();
154 if (arch.GetTriple().getOS() == llvm::Triple::Linux) {
155 stop_info = StopInfo::CreateStopReasonWithSignal(
156 *stop_thread, m_active_exception->exception_record.exception_code);
159 llvm::raw_string_ostream desc_stream(desc);
160 desc_stream << "Exception "
162 m_active_exception->exception_record.exception_code, 8)
163 << " encountered at address "
165 m_active_exception->exception_record.exception_address,
167 stop_info = StopInfo::CreateStopReasonWithException(
168 *stop_thread, desc_stream.str().c_str());
171 stop_thread->SetStopInfo(stop_info);
174 bool ProcessMinidump::IsAlive() { return true; }
176 bool ProcessMinidump::WarnBeforeDetach() const { return false; }
178 size_t ProcessMinidump::ReadMemory(lldb::addr_t addr, void *buf, size_t size,
180 // Don't allow the caching that lldb_private::Process::ReadMemory does
181 // since we have it all cached in our dump file anyway.
182 return DoReadMemory(addr, buf, size, error);
185 size_t ProcessMinidump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
188 llvm::ArrayRef<uint8_t> mem = m_minidump_parser.GetMemory(addr, size);
190 error.SetErrorString("could not parse memory info");
194 std::memcpy(buf, mem.data(), mem.size());
198 ArchSpec ProcessMinidump::GetArchitecture() {
200 return m_minidump_parser.GetArchitecture();
204 triple.setVendor(llvm::Triple::VendorType::UnknownVendor);
205 triple.setArch(llvm::Triple::ArchType::x86);
206 triple.setOS(llvm::Triple::OSType::Win32);
207 return ArchSpec(triple);
210 Error ProcessMinidump::GetMemoryRegionInfo(lldb::addr_t load_addr,
211 MemoryRegionInfo &range_info) {
213 auto info = m_minidump_parser.GetMemoryRegionInfo(load_addr);
215 error.SetErrorString("No valid MemoryRegionInfo found!");
218 range_info = info.getValue();
222 void ProcessMinidump::Clear() { Process::m_thread_list.Clear(); }
224 bool ProcessMinidump::UpdateThreadList(ThreadList &old_thread_list,
225 ThreadList &new_thread_list) {
226 uint32_t num_threads = 0;
227 if (m_thread_list.size() > 0)
228 num_threads = m_thread_list.size();
230 for (lldb::tid_t tid = 0; tid < num_threads; ++tid) {
231 llvm::ArrayRef<uint8_t> context;
233 context = m_minidump_parser.GetThreadContext(m_thread_list[tid]);
235 context = m_minidump_parser.GetThreadContextWow64(m_thread_list[tid]);
237 lldb::ThreadSP thread_sp(
238 new ThreadMinidump(*this, m_thread_list[tid], context));
239 new_thread_list.AddThread(thread_sp);
241 return new_thread_list.GetSize(false) > 0;
244 void ProcessMinidump::ReadModuleList() {
245 std::vector<const MinidumpModule *> filtered_modules =
246 m_minidump_parser.GetFilteredModuleList();
248 for (auto module : filtered_modules) {
249 llvm::Optional<std::string> name =
250 m_minidump_parser.GetMinidumpString(module->module_name_rva);
255 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES));
257 log->Printf("ProcessMinidump::%s found module: name: %s %#010" PRIx64
258 "-%#010" PRIx64 " size: %" PRIu32,
259 __FUNCTION__, name.getValue().c_str(),
260 uint64_t(module->base_of_image),
261 module->base_of_image + module->size_of_image,
262 uint32_t(module->size_of_image));
265 // check if the process is wow64 - a 32 bit windows process running on a
267 if (llvm::StringRef(name.getValue()).endswith_lower("wow64.dll")) {
271 const auto file_spec = FileSpec(name.getValue(), true);
272 ModuleSpec module_spec = file_spec;
274 lldb::ModuleSP module_sp = GetTarget().GetSharedModule(module_spec, &error);
275 if (!module_sp || error.Fail()) {
280 log->Printf("ProcessMinidump::%s load module: name: %s", __FUNCTION__,
281 name.getValue().c_str());
284 bool load_addr_changed = false;
285 module_sp->SetLoadAddress(GetTarget(), module->base_of_image, false,
290 bool ProcessMinidump::GetProcessInfo(ProcessInstanceInfo &info) {
292 info.SetProcessID(GetID());
293 info.SetArchitecture(GetArchitecture());
294 lldb::ModuleSP module_sp = GetTarget().GetExecutableModule();
296 const bool add_exe_file_as_first_arg = false;
297 info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
298 add_exe_file_as_first_arg);