blob: d4d65c044eabeec48292b2621885ef157212f9f1 [file] [log] [blame]
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +00001//===-- 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// Project includes
11#include "ProcessMinidump.h"
12#include "ThreadMinidump.h"
13
14// Other libraries and framework includes
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +000015#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"
Zachary Turner666cc0b2017-03-04 01:30:05 +000024#include "lldb/Utility/DataBufferLLVM.h"
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +000025#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000026#include "lldb/Utility/Log.h"
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +000027
Zachary Turner3f4a4b32017-02-24 18:56:49 +000028#include "llvm/Support/MemoryBuffer.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000029#include "llvm/Support/Threading.h"
30
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +000031// C includes
32// C++ includes
33
34using namespace lldb_private;
35using namespace minidump;
36
37ConstString ProcessMinidump::GetPluginNameStatic() {
38 static ConstString g_name("minidump");
39 return g_name;
40}
41
42const char *ProcessMinidump::GetPluginDescriptionStatic() {
43 return "Minidump plug-in.";
44}
45
46lldb::ProcessSP ProcessMinidump::CreateInstance(lldb::TargetSP target_sp,
47 lldb::ListenerSP listener_sp,
48 const FileSpec *crash_file) {
49 if (!crash_file)
50 return nullptr;
51
52 lldb::ProcessSP process_sp;
53 // Read enough data for the Minidump header
Zachary Turner3f4a4b32017-02-24 18:56:49 +000054 constexpr size_t header_size = sizeof(MinidumpHeader);
55 auto DataPtr =
Zachary Turner7f6a7a32017-03-06 23:42:14 +000056 DataBufferLLVM::CreateSliceFromPath(crash_file->GetPath(), header_size, 0);
Zachary Turner3f4a4b32017-02-24 18:56:49 +000057 if (!DataPtr)
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +000058 return nullptr;
59
Zachary Turner3f4a4b32017-02-24 18:56:49 +000060 assert(DataPtr->GetByteSize() == header_size);
61
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +000062 // first, only try to parse the header, beacuse we need to be fast
Zachary Turner3f4a4b32017-02-24 18:56:49 +000063 llvm::ArrayRef<uint8_t> HeaderBytes = DataPtr->GetData();
64 const MinidumpHeader *header = MinidumpHeader::Parse(HeaderBytes);
65 if (header == nullptr)
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +000066 return nullptr;
67
Zachary Turner7f6a7a32017-03-06 23:42:14 +000068 auto AllData = DataBufferLLVM::CreateSliceFromPath(crash_file->GetPath(), -1, 0);
Zachary Turner3f4a4b32017-02-24 18:56:49 +000069 if (!AllData)
70 return nullptr;
71
72 auto minidump_parser = MinidumpParser::Create(AllData);
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +000073 // check if the parser object is valid
Pavel Labath222fd132016-11-09 10:16:11 +000074 if (!minidump_parser)
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +000075 return nullptr;
76
77 return std::make_shared<ProcessMinidump>(target_sp, listener_sp, *crash_file,
78 minidump_parser.getValue());
79}
80
81bool ProcessMinidump::CanDebug(lldb::TargetSP target_sp,
82 bool plugin_specified_by_name) {
83 return true;
84}
85
86ProcessMinidump::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) {}
92
93ProcessMinidump::~ProcessMinidump() {
94 Clear();
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.
99 Finalize();
100}
101
102void ProcessMinidump::Initialize() {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000103 static llvm::once_flag g_once_flag;
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +0000104
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000105 llvm::call_once(g_once_flag, []() {
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +0000106 PluginManager::RegisterPlugin(GetPluginNameStatic(),
107 GetPluginDescriptionStatic(),
108 ProcessMinidump::CreateInstance);
109 });
110}
111
112void ProcessMinidump::Terminate() {
113 PluginManager::UnregisterPlugin(ProcessMinidump::CreateInstance);
114}
115
Zachary Turner97206d52017-05-12 04:51:55 +0000116Status ProcessMinidump::DoLoadCore() {
117 Status error;
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +0000118
119 m_thread_list = m_minidump_parser.GetThreads();
120 m_active_exception = m_minidump_parser.GetExceptionStream();
121 ReadModuleList();
122 GetTarget().SetArchitecture(GetArchitecture());
123
124 llvm::Optional<lldb::pid_t> pid = m_minidump_parser.GetPid();
125 if (!pid) {
126 error.SetErrorString("failed to parse PID");
127 return error;
128 }
129 SetID(pid.getValue());
130
131 return error;
132}
133
134DynamicLoader *ProcessMinidump::GetDynamicLoader() {
135 if (m_dyld_ap.get() == nullptr)
136 m_dyld_ap.reset(DynamicLoader::FindPlugin(this, nullptr));
137 return m_dyld_ap.get();
138}
139
140ConstString ProcessMinidump::GetPluginName() { return GetPluginNameStatic(); }
141
142uint32_t ProcessMinidump::GetPluginVersion() { return 1; }
143
Zachary Turner97206d52017-05-12 04:51:55 +0000144Status ProcessMinidump::DoDestroy() { return Status(); }
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +0000145
146void ProcessMinidump::RefreshStateAfterStop() {
147 if (!m_active_exception)
148 return;
149
150 if (m_active_exception->exception_record.exception_code ==
151 MinidumpException::DumpRequested) {
152 return;
153 }
154
155 lldb::StopInfoSP stop_info;
156 lldb::ThreadSP stop_thread;
157
158 Process::m_thread_list.SetSelectedThreadByID(m_active_exception->thread_id);
159 stop_thread = Process::m_thread_list.GetSelectedThread();
160 ArchSpec arch = GetArchitecture();
161
162 if (arch.GetTriple().getOS() == llvm::Triple::Linux) {
163 stop_info = StopInfo::CreateStopReasonWithSignal(
164 *stop_thread, m_active_exception->exception_record.exception_code);
165 } else {
166 std::string desc;
167 llvm::raw_string_ostream desc_stream(desc);
168 desc_stream << "Exception "
169 << llvm::format_hex(
170 m_active_exception->exception_record.exception_code, 8)
171 << " encountered at address "
172 << llvm::format_hex(
173 m_active_exception->exception_record.exception_address,
174 8);
175 stop_info = StopInfo::CreateStopReasonWithException(
176 *stop_thread, desc_stream.str().c_str());
177 }
178
179 stop_thread->SetStopInfo(stop_info);
180}
181
182bool ProcessMinidump::IsAlive() { return true; }
183
184bool ProcessMinidump::WarnBeforeDetach() const { return false; }
185
186size_t ProcessMinidump::ReadMemory(lldb::addr_t addr, void *buf, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +0000187 Status &error) {
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +0000188 // 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);
191}
192
193size_t ProcessMinidump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +0000194 Status &error) {
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +0000195
196 llvm::ArrayRef<uint8_t> mem = m_minidump_parser.GetMemory(addr, size);
197 if (mem.empty()) {
198 error.SetErrorString("could not parse memory info");
199 return 0;
200 }
201
202 std::memcpy(buf, mem.data(), mem.size());
203 return mem.size();
204}
205
206ArchSpec ProcessMinidump::GetArchitecture() {
207 if (!m_is_wow64) {
208 return m_minidump_parser.GetArchitecture();
209 }
210
211 llvm::Triple triple;
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);
216}
217
Zachary Turner97206d52017-05-12 04:51:55 +0000218Status ProcessMinidump::GetMemoryRegionInfo(lldb::addr_t load_addr,
219 MemoryRegionInfo &range_info) {
220 Status error;
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +0000221 auto info = m_minidump_parser.GetMemoryRegionInfo(load_addr);
222 if (!info) {
223 error.SetErrorString("No valid MemoryRegionInfo found!");
224 return error;
225 }
226 range_info = info.getValue();
227 return error;
228}
229
230void ProcessMinidump::Clear() { Process::m_thread_list.Clear(); }
231
Dimitar Vlahovski5a19c0c2016-11-01 15:48:24 +0000232bool ProcessMinidump::UpdateThreadList(ThreadList &old_thread_list,
233 ThreadList &new_thread_list) {
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +0000234 uint32_t num_threads = 0;
235 if (m_thread_list.size() > 0)
236 num_threads = m_thread_list.size();
237
238 for (lldb::tid_t tid = 0; tid < num_threads; ++tid) {
239 llvm::ArrayRef<uint8_t> context;
240 if (!m_is_wow64)
241 context = m_minidump_parser.GetThreadContext(m_thread_list[tid]);
242 else
243 context = m_minidump_parser.GetThreadContextWow64(m_thread_list[tid]);
244
245 lldb::ThreadSP thread_sp(
246 new ThreadMinidump(*this, m_thread_list[tid], context));
247 new_thread_list.AddThread(thread_sp);
248 }
249 return new_thread_list.GetSize(false) > 0;
250}
251
252void ProcessMinidump::ReadModuleList() {
253 std::vector<const MinidumpModule *> filtered_modules =
254 m_minidump_parser.GetFilteredModuleList();
255
256 for (auto module : filtered_modules) {
257 llvm::Optional<std::string> name =
258 m_minidump_parser.GetMinidumpString(module->module_name_rva);
259
260 if (!name)
261 continue;
262
263 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES));
264 if (log) {
Pavel Labatheaa419c2016-11-02 10:29:47 +0000265 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));
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +0000271 }
272
273 // check if the process is wow64 - a 32 bit windows process running on a
274 // 64 bit windows
275 if (llvm::StringRef(name.getValue()).endswith_lower("wow64.dll")) {
276 m_is_wow64 = true;
277 }
278
279 const auto file_spec = FileSpec(name.getValue(), true);
280 ModuleSpec module_spec = file_spec;
Zachary Turner97206d52017-05-12 04:51:55 +0000281 Status error;
Dimitar Vlahovski7b18dd42016-10-31 15:35:18 +0000282 lldb::ModuleSP module_sp = GetTarget().GetSharedModule(module_spec, &error);
283 if (!module_sp || error.Fail()) {
284 continue;
285 }
286
287 if (log) {
288 log->Printf("ProcessMinidump::%s load module: name: %s", __FUNCTION__,
289 name.getValue().c_str());
290 }
291
292 bool load_addr_changed = false;
293 module_sp->SetLoadAddress(GetTarget(), module->base_of_image, false,
294 load_addr_changed);
295 }
296}
Dimitar Vlahovski5a19c0c2016-11-01 15:48:24 +0000297
298bool ProcessMinidump::GetProcessInfo(ProcessInstanceInfo &info) {
299 info.Clear();
300 info.SetProcessID(GetID());
301 info.SetArchitecture(GetArchitecture());
302 lldb::ModuleSP module_sp = GetTarget().GetExecutableModule();
303 if (module_sp) {
304 const bool add_exe_file_as_first_arg = false;
305 info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
306 add_exe_file_as_first_arg);
307 }
308 return true;
309}