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/Module.h"
16 #include "lldb/Core/ModuleSpec.h"
17 #include "lldb/Core/PluginManager.h"
18 #include "lldb/Core/Section.h"
19 #include "lldb/Core/State.h"
20 #include "lldb/Target/DynamicLoader.h"
21 #include "lldb/Target/MemoryRegionInfo.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Target/UnixSignals.h"
24 #include "lldb/Utility/DataBufferLLVM.h"
25 #include "lldb/Utility/LLDBAssert.h"
26 #include "lldb/Utility/Log.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/Threading.h"
34 using namespace lldb_private;
35 using namespace minidump;
37 ConstString ProcessMinidump::GetPluginNameStatic() {
38 static ConstString g_name("minidump");
42 const char *ProcessMinidump::GetPluginDescriptionStatic() {
43 return "Minidump plug-in.";
46 lldb::ProcessSP ProcessMinidump::CreateInstance(lldb::TargetSP target_sp,
47 lldb::ListenerSP listener_sp,
48 const FileSpec *crash_file) {
52 lldb::ProcessSP process_sp;
53 // Read enough data for the Minidump header
54 constexpr size_t header_size = sizeof(MinidumpHeader);
56 DataBufferLLVM::CreateSliceFromPath(crash_file->GetPath(), header_size, 0);
60 assert(DataPtr->GetByteSize() == header_size);
62 // first, only try to parse the header, beacuse we need to be fast
63 llvm::ArrayRef<uint8_t> HeaderBytes = DataPtr->GetData();
64 const MinidumpHeader *header = MinidumpHeader::Parse(HeaderBytes);
65 if (header == nullptr)
68 auto AllData = DataBufferLLVM::CreateSliceFromPath(crash_file->GetPath(), -1, 0);
72 auto minidump_parser = MinidumpParser::Create(AllData);
73 // check if the parser object is valid
77 return std::make_shared<ProcessMinidump>(target_sp, listener_sp, *crash_file,
78 minidump_parser.getValue());
81 bool ProcessMinidump::CanDebug(lldb::TargetSP target_sp,
82 bool plugin_specified_by_name) {
86 ProcessMinidump::ProcessMinidump(lldb::TargetSP target_sp,
87 lldb::ListenerSP listener_sp,
88 const FileSpec &core_file,
89 MinidumpParser minidump_parser)
90 : Process(target_sp, listener_sp), m_minidump_parser(minidump_parser),
91 m_core_file(core_file), m_is_wow64(false) {}
93 ProcessMinidump::~ProcessMinidump() {
95 // We need to call finalize on the process before destroying ourselves
96 // to make sure all of the broadcaster cleanup goes as planned. If we
97 // destruct this class, then Process::~Process() might have problems
98 // trying to fully destroy the broadcaster.
102 void ProcessMinidump::Initialize() {
103 static llvm::once_flag g_once_flag;
105 llvm::call_once(g_once_flag, []() {
106 PluginManager::RegisterPlugin(GetPluginNameStatic(),
107 GetPluginDescriptionStatic(),
108 ProcessMinidump::CreateInstance);
112 void ProcessMinidump::Terminate() {
113 PluginManager::UnregisterPlugin(ProcessMinidump::CreateInstance);
116 Status ProcessMinidump::DoLoadCore() {
119 m_thread_list = m_minidump_parser.GetThreads();
120 m_active_exception = m_minidump_parser.GetExceptionStream();
122 GetTarget().SetArchitecture(GetArchitecture());
124 llvm::Optional<lldb::pid_t> pid = m_minidump_parser.GetPid();
126 error.SetErrorString("failed to parse PID");
129 SetID(pid.getValue());
134 DynamicLoader *ProcessMinidump::GetDynamicLoader() {
135 if (m_dyld_ap.get() == nullptr)
136 m_dyld_ap.reset(DynamicLoader::FindPlugin(this, nullptr));
137 return m_dyld_ap.get();
140 ConstString ProcessMinidump::GetPluginName() { return GetPluginNameStatic(); }
142 uint32_t ProcessMinidump::GetPluginVersion() { return 1; }
144 Status ProcessMinidump::DoDestroy() { return Status(); }
146 void ProcessMinidump::RefreshStateAfterStop() {
147 if (!m_active_exception)
150 if (m_active_exception->exception_record.exception_code ==
151 MinidumpException::DumpRequested) {
155 lldb::StopInfoSP stop_info;
156 lldb::ThreadSP stop_thread;
158 Process::m_thread_list.SetSelectedThreadByID(m_active_exception->thread_id);
159 stop_thread = Process::m_thread_list.GetSelectedThread();
160 ArchSpec arch = GetArchitecture();
162 if (arch.GetTriple().getOS() == llvm::Triple::Linux) {
163 stop_info = StopInfo::CreateStopReasonWithSignal(
164 *stop_thread, m_active_exception->exception_record.exception_code);
167 llvm::raw_string_ostream desc_stream(desc);
168 desc_stream << "Exception "
170 m_active_exception->exception_record.exception_code, 8)
171 << " encountered at address "
173 m_active_exception->exception_record.exception_address,
175 stop_info = StopInfo::CreateStopReasonWithException(
176 *stop_thread, desc_stream.str().c_str());
179 stop_thread->SetStopInfo(stop_info);
182 bool ProcessMinidump::IsAlive() { return true; }
184 bool ProcessMinidump::WarnBeforeDetach() const { return false; }
186 size_t ProcessMinidump::ReadMemory(lldb::addr_t addr, void *buf, size_t size,
188 // Don't allow the caching that lldb_private::Process::ReadMemory does
189 // since we have it all cached in our dump file anyway.
190 return DoReadMemory(addr, buf, size, error);
193 size_t ProcessMinidump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
196 llvm::ArrayRef<uint8_t> mem = m_minidump_parser.GetMemory(addr, size);
198 error.SetErrorString("could not parse memory info");
202 std::memcpy(buf, mem.data(), mem.size());
206 ArchSpec ProcessMinidump::GetArchitecture() {
208 return m_minidump_parser.GetArchitecture();
212 triple.setVendor(llvm::Triple::VendorType::UnknownVendor);
213 triple.setArch(llvm::Triple::ArchType::x86);
214 triple.setOS(llvm::Triple::OSType::Win32);
215 return ArchSpec(triple);
218 Status ProcessMinidump::GetMemoryRegionInfo(lldb::addr_t load_addr,
219 MemoryRegionInfo &range_info) {
221 auto info = m_minidump_parser.GetMemoryRegionInfo(load_addr);
223 error.SetErrorString("No valid MemoryRegionInfo found!");
226 range_info = info.getValue();
230 void ProcessMinidump::Clear() { Process::m_thread_list.Clear(); }
232 bool ProcessMinidump::UpdateThreadList(ThreadList &old_thread_list,
233 ThreadList &new_thread_list) {
234 uint32_t num_threads = 0;
235 if (m_thread_list.size() > 0)
236 num_threads = m_thread_list.size();
238 for (lldb::tid_t tid = 0; tid < num_threads; ++tid) {
239 llvm::ArrayRef<uint8_t> context;
241 context = m_minidump_parser.GetThreadContext(m_thread_list[tid]);
243 context = m_minidump_parser.GetThreadContextWow64(m_thread_list[tid]);
245 lldb::ThreadSP thread_sp(
246 new ThreadMinidump(*this, m_thread_list[tid], context));
247 new_thread_list.AddThread(thread_sp);
249 return new_thread_list.GetSize(false) > 0;
252 void ProcessMinidump::ReadModuleList() {
253 std::vector<const MinidumpModule *> filtered_modules =
254 m_minidump_parser.GetFilteredModuleList();
256 for (auto module : filtered_modules) {
257 llvm::Optional<std::string> name =
258 m_minidump_parser.GetMinidumpString(module->module_name_rva);
263 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES));
265 log->Printf("ProcessMinidump::%s found module: name: %s %#010" PRIx64
266 "-%#010" PRIx64 " size: %" PRIu32,
267 __FUNCTION__, name.getValue().c_str(),
268 uint64_t(module->base_of_image),
269 module->base_of_image + module->size_of_image,
270 uint32_t(module->size_of_image));
273 // check if the process is wow64 - a 32 bit windows process running on a
275 if (llvm::StringRef(name.getValue()).endswith_lower("wow64.dll")) {
279 const auto file_spec = FileSpec(name.getValue(), true);
280 ModuleSpec module_spec = file_spec;
282 lldb::ModuleSP module_sp = GetTarget().GetSharedModule(module_spec, &error);
283 if (!module_sp || error.Fail()) {
288 log->Printf("ProcessMinidump::%s load module: name: %s", __FUNCTION__,
289 name.getValue().c_str());
292 bool load_addr_changed = false;
293 module_sp->SetLoadAddress(GetTarget(), module->base_of_image, false,
298 bool ProcessMinidump::GetProcessInfo(ProcessInstanceInfo &info) {
300 info.SetProcessID(GetID());
301 info.SetArchitecture(GetArchitecture());
302 lldb::ModuleSP module_sp = GetTarget().GetExecutableModule();
304 const bool add_exe_file_as_first_arg = false;
305 info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
306 add_exe_file_as_first_arg);