blob: f2e9aa4be7d0d9b476f74cbf5b901ac2342f9b0a [file] [log] [blame]
Adrian McCarthyc96516f2015-08-03 23:01:51 +00001//===-- ProcessWinMiniDump.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 "ProcessWinMiniDump.h"
11
12#include "lldb/Host/windows/windows.h"
13#include <DbgHelp.h>
14
15#include <assert.h>
16#include <stdlib.h>
17
18#include <mutex>
19
20#include "lldb/Core/PluginManager.h"
21#include "lldb/Core/Module.h"
22#include "lldb/Core/ModuleSpec.h"
23#include "lldb/Core/Section.h"
24#include "lldb/Core/State.h"
25#include "lldb/Core/DataBufferHeap.h"
26#include "lldb/Core/Log.h"
Adrian McCarthy61ede152015-08-19 20:43:22 +000027#include "lldb/Target/StopInfo.h"
Adrian McCarthyc96516f2015-08-03 23:01:51 +000028#include "lldb/Target/Target.h"
29#include "lldb/Target/DynamicLoader.h"
30#include "lldb/Target/UnixSignals.h"
Adrian McCarthy61ede152015-08-19 20:43:22 +000031#include "llvm/Support/Format.h"
32#include "llvm/Support/raw_ostream.h"
Adrian McCarthy23d14b62015-08-28 14:42:03 +000033#include "llvm/Support/ConvertUTF.h"
Adrian McCarthyc96516f2015-08-03 23:01:51 +000034#include "Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.h"
35
Adrian McCarthy27785dd2015-08-24 16:00:51 +000036#include "ExceptionRecord.h"
Adrian McCarthyc96516f2015-08-03 23:01:51 +000037#include "ThreadWinMiniDump.h"
38
39using namespace lldb_private;
40
Adrian McCarthy23d14b62015-08-28 14:42:03 +000041namespace
42{
43
44// Getting a string out of a mini dump is a chore. You're usually given a
45// relative virtual address (RVA), which points to a counted string that's in
46// Windows Unicode (UTF-16). This wrapper handles all the redirection and
47// returns a UTF-8 copy of the string.
48std::string
49GetMiniDumpString(const void *base_addr, const RVA rva)
50{
51 std::string result;
52 if (!base_addr)
53 {
54 return result;
55 }
56 auto md_string = reinterpret_cast<const MINIDUMP_STRING *>(static_cast<const char *>(base_addr) + rva);
57 auto source_start = reinterpret_cast<const UTF16 *>(md_string->Buffer);
58 const auto source_length = ::wcslen(md_string->Buffer);
59 const auto source_end = source_start + source_length;
60 result.resize(4*source_length); // worst case length
61 auto result_start = reinterpret_cast<UTF8 *>(&result[0]);
62 const auto result_end = result_start + result.size();
63 ConvertUTF16toUTF8(&source_start, source_end, &result_start, result_end, strictConversion);
64 const auto result_size = std::distance(reinterpret_cast<UTF8 *>(&result[0]), result_start);
65 result.resize(result_size); // shrink to actual length
66 return result;
67}
68
69} // anonymous namespace
70
Adrian McCarthyc96516f2015-08-03 23:01:51 +000071// Encapsulates the private data for ProcessWinMiniDump.
72// TODO(amccarth): Determine if we need a mutex for access.
73class ProcessWinMiniDump::Data
74{
75public:
76 Data();
77 ~Data();
78
79 FileSpec m_core_file;
80 HANDLE m_dump_file; // handle to the open minidump file
81 HANDLE m_mapping; // handle to the file mapping for the minidump file
82 void * m_base_addr; // base memory address of the minidump
Adrian McCarthy61ede152015-08-19 20:43:22 +000083 std::shared_ptr<ExceptionRecord> m_exception_sp;
Adrian McCarthyc96516f2015-08-03 23:01:51 +000084};
85
86ConstString
87ProcessWinMiniDump::GetPluginNameStatic()
88{
89 static ConstString g_name("win-minidump");
90 return g_name;
91}
92
93const char *
94ProcessWinMiniDump::GetPluginDescriptionStatic()
95{
96 return "Windows minidump plug-in.";
97}
98
99void
100ProcessWinMiniDump::Terminate()
101{
102 PluginManager::UnregisterPlugin(ProcessWinMiniDump::CreateInstance);
103}
104
105
106lldb::ProcessSP
Zachary Turner7529df92015-09-01 20:02:29 +0000107ProcessWinMiniDump::CreateInstance(lldb::TargetSP target_sp, Listener &listener, const FileSpec *crash_file)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000108{
109 lldb::ProcessSP process_sp;
110 if (crash_file)
111 {
Zachary Turner7529df92015-09-01 20:02:29 +0000112 process_sp.reset(new ProcessWinMiniDump(target_sp, listener, *crash_file));
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000113 }
114 return process_sp;
115}
116
117bool
Zachary Turner7529df92015-09-01 20:02:29 +0000118ProcessWinMiniDump::CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000119{
120 // TODO(amccarth): Eventually, this needs some actual logic.
121 return true;
122}
123
Zachary Turner7529df92015-09-01 20:02:29 +0000124ProcessWinMiniDump::ProcessWinMiniDump(lldb::TargetSP target_sp, Listener &listener,
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000125 const FileSpec &core_file) :
Zachary Turner7529df92015-09-01 20:02:29 +0000126 Process(target_sp, listener),
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000127 m_data_up(new Data)
128{
129 m_data_up->m_core_file = core_file;
130}
131
132ProcessWinMiniDump::~ProcessWinMiniDump()
133{
134 Clear();
135 // We need to call finalize on the process before destroying ourselves
136 // to make sure all of the broadcaster cleanup goes as planned. If we
137 // destruct this class, then Process::~Process() might have problems
138 // trying to fully destroy the broadcaster.
139 Finalize();
140}
141
142ConstString
143ProcessWinMiniDump::GetPluginName()
144{
145 return GetPluginNameStatic();
146}
147
148uint32_t
149ProcessWinMiniDump::GetPluginVersion()
150{
151 return 1;
152}
153
154
155Error
156ProcessWinMiniDump::DoLoadCore()
157{
158 Error error;
159
160 error = MapMiniDumpIntoMemory(m_data_up->m_core_file.GetCString());
161 if (error.Fail())
162 {
163 return error;
164 }
165
Zachary Turner7529df92015-09-01 20:02:29 +0000166 GetTarget().SetArchitecture(DetermineArchitecture());
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000167 ReadModuleList();
Adrian McCarthy61ede152015-08-19 20:43:22 +0000168 ReadExceptionRecord();
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000169
170 return error;
171
172}
173
174DynamicLoader *
175ProcessWinMiniDump::GetDynamicLoader()
176{
177 if (m_dyld_ap.get() == NULL)
178 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, DynamicLoaderWindowsDYLD::GetPluginNameStatic().GetCString()));
179 return m_dyld_ap.get();
180}
181
182bool
183ProcessWinMiniDump::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list)
184{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000185 size_t size = 0;
186 auto thread_list_ptr = static_cast<const MINIDUMP_THREAD_LIST *>(FindDumpStream(ThreadListStream, &size));
187 if (thread_list_ptr)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000188 {
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000189 const ULONG32 thread_count = thread_list_ptr->NumberOfThreads;
Adrian McCarthy61ede152015-08-19 20:43:22 +0000190 for (ULONG32 i = 0; i < thread_count; ++i) {
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000191 std::shared_ptr<ThreadWinMiniDump> thread_sp(new ThreadWinMiniDump(*this, thread_list_ptr->Threads[i].ThreadId));
192 new_thread_list.AddThread(thread_sp);
193 }
194 }
195
196 return new_thread_list.GetSize(false) > 0;
197}
198
199void
200ProcessWinMiniDump::RefreshStateAfterStop()
201{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000202 if (!m_data_up) return;
203 if (!m_data_up->m_exception_sp) return;
204
205 auto active_exception = m_data_up->m_exception_sp;
206 std::string desc;
207 llvm::raw_string_ostream desc_stream(desc);
208 desc_stream << "Exception "
209 << llvm::format_hex(active_exception->GetExceptionCode(), 8)
210 << " encountered at address "
211 << llvm::format_hex(active_exception->GetExceptionAddress(), 8);
212 m_thread_list.SetSelectedThreadByID(active_exception->GetThreadID());
213 auto stop_thread = m_thread_list.GetSelectedThread();
214 auto stop_info = StopInfo::CreateStopReasonWithException(*stop_thread, desc_stream.str().c_str());
215 stop_thread->SetStopInfo(stop_info);
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000216}
217
218Error
219ProcessWinMiniDump::DoDestroy()
220{
221 return Error();
222}
223
224bool
225ProcessWinMiniDump::IsAlive()
226{
227 return true;
228}
229
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000230bool
231ProcessWinMiniDump::WarnBeforeDetach () const
232{
233 // Since this is post-mortem debugging, there's no need to warn the user
234 // that quitting the debugger will terminate the process.
235 return false;
236}
237
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000238size_t
239ProcessWinMiniDump::ReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error)
240{
241 // Don't allow the caching that lldb_private::Process::ReadMemory does
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000242 // since we have it all cached our our dump file anyway.
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000243 return DoReadMemory(addr, buf, size, error);
244}
245
246size_t
247ProcessWinMiniDump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error)
248{
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000249 // I don't have a sense of how frequently this is called or how many memory
250 // ranges a mini dump typically has, so I'm not sure if searching for the
251 // appropriate range linearly each time is stupid. Perhaps we should build
252 // an index for faster lookups.
253 Range range = {0};
254 if (!FindMemoryRange(addr, &range))
255 {
256 return 0;
257 }
258
259 // There's at least some overlap between the beginning of the desired range
260 // (addr) and the current range. Figure out where the overlap begins and
261 // how much overlap there is, then copy it to the destination buffer.
262 const size_t offset = range.start - addr;
263 const size_t overlap = std::min(size, range.size - offset);
264 std::memcpy(buf, range.ptr + offset, overlap);
265 return overlap;
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000266}
267
268void
269ProcessWinMiniDump::Clear()
270{
271 m_thread_list.Clear();
272}
273
274void
275ProcessWinMiniDump::Initialize()
276{
277 static std::once_flag g_once_flag;
278
279 std::call_once(g_once_flag, []()
280 {
281 PluginManager::RegisterPlugin(GetPluginNameStatic(),
282 GetPluginDescriptionStatic(),
283 CreateInstance);
284 });
285}
286
287lldb::addr_t
288ProcessWinMiniDump::GetImageInfoAddress()
289{
290 Target *target = &GetTarget();
291 ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile();
292 Address addr = obj_file->GetImageInfoAddress(target);
293
294 if (addr.IsValid())
295 return addr.GetLoadAddress(target);
296 return LLDB_INVALID_ADDRESS;
297}
298
299ArchSpec
300ProcessWinMiniDump::GetArchitecture()
301{
302 // TODO
303 return ArchSpec();
304}
305
306
307ProcessWinMiniDump::Data::Data() :
308 m_dump_file(INVALID_HANDLE_VALUE),
309 m_mapping(NULL),
310 m_base_addr(nullptr)
311{
312}
313
314ProcessWinMiniDump::Data::~Data()
315{
316 if (m_base_addr)
317 {
318 ::UnmapViewOfFile(m_base_addr);
319 m_base_addr = nullptr;
320 }
321 if (m_mapping)
322 {
323 ::CloseHandle(m_mapping);
324 m_mapping = NULL;
325 }
326 if (m_dump_file != INVALID_HANDLE_VALUE)
327 {
328 ::CloseHandle(m_dump_file);
329 m_dump_file = INVALID_HANDLE_VALUE;
330 }
331}
332
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000333bool
334ProcessWinMiniDump::FindMemoryRange(lldb::addr_t addr, Range *range_out) const
335{
336 size_t stream_size = 0;
337 auto mem_list_stream = static_cast<const MINIDUMP_MEMORY_LIST *>(FindDumpStream(MemoryListStream, &stream_size));
338 if (mem_list_stream)
339 {
340 for (ULONG32 i = 0; i < mem_list_stream->NumberOfMemoryRanges; ++i) {
341 const MINIDUMP_MEMORY_DESCRIPTOR &mem_desc = mem_list_stream->MemoryRanges[i];
342 const MINIDUMP_LOCATION_DESCRIPTOR &loc_desc = mem_desc.Memory;
343 const lldb::addr_t range_start = mem_desc.StartOfMemoryRange;
344 const size_t range_size = loc_desc.DataSize;
345 if (range_start <= addr && addr < range_start + range_size)
346 {
347 range_out->start = range_start;
348 range_out->size = range_size;
349 range_out->ptr = reinterpret_cast<const uint8_t *>(m_data_up->m_base_addr) + loc_desc.Rva;
350 return true;
351 }
352 }
353 }
354
355 // Some mini dumps have a Memory64ListStream that captures all the heap
356 // memory. We can't exactly use the same loop as above, because the mini
357 // dump uses slightly different data structures to describe those.
358 auto mem_list64_stream = static_cast<const MINIDUMP_MEMORY64_LIST *>(FindDumpStream(Memory64ListStream, &stream_size));
359 if (mem_list64_stream)
360 {
361 size_t base_rva = mem_list64_stream->BaseRva;
362 for (ULONG32 i = 0; i < mem_list64_stream->NumberOfMemoryRanges; ++i) {
363 const MINIDUMP_MEMORY_DESCRIPTOR64 &mem_desc = mem_list64_stream->MemoryRanges[i];
364 const lldb::addr_t range_start = mem_desc.StartOfMemoryRange;
365 const size_t range_size = mem_desc.DataSize;
366 if (range_start <= addr && addr < range_start + range_size)
367 {
368 range_out->start = range_start;
369 range_out->size = range_size;
370 range_out->ptr = reinterpret_cast<const uint8_t *>(m_data_up->m_base_addr) + base_rva;
371 return true;
372 }
373 base_rva += range_size;
374 }
375 }
376
377 return false;
378}
379
380
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000381Error
382ProcessWinMiniDump::MapMiniDumpIntoMemory(const char *file)
383{
384 Error error;
385
386 m_data_up->m_dump_file = ::CreateFile(file, GENERIC_READ, FILE_SHARE_READ,
387 NULL, OPEN_EXISTING,
388 FILE_ATTRIBUTE_NORMAL, NULL);
389 if (m_data_up->m_dump_file == INVALID_HANDLE_VALUE)
390 {
391 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
392 return error;
393 }
394
395 m_data_up->m_mapping = ::CreateFileMapping(m_data_up->m_dump_file, NULL,
396 PAGE_READONLY, 0, 0, NULL);
397 if (m_data_up->m_mapping == NULL)
398 {
399 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
400 return error;
401 }
402
403 m_data_up->m_base_addr = ::MapViewOfFile(m_data_up->m_mapping, FILE_MAP_READ, 0, 0, 0);
404 if (m_data_up->m_base_addr == NULL)
405 {
406 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
407 return error;
408 }
409
410 return error;
411}
412
413
414ArchSpec
415ProcessWinMiniDump::DetermineArchitecture()
416{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000417 size_t size = 0;
418 auto system_info_ptr = static_cast<const MINIDUMP_SYSTEM_INFO *>(FindDumpStream(SystemInfoStream, &size));
419 if (system_info_ptr)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000420 {
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000421 switch (system_info_ptr->ProcessorArchitecture)
422 {
423 case PROCESSOR_ARCHITECTURE_INTEL:
424 return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_I386, LLDB_INVALID_CPUTYPE);
425 case PROCESSOR_ARCHITECTURE_AMD64:
426 return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_AMD64, LLDB_INVALID_CPUTYPE);
427 default:
428 break;
429 }
430 }
431
432 return ArchSpec(); // invalid or unknown
433}
Adrian McCarthy61ede152015-08-19 20:43:22 +0000434
435void
436ProcessWinMiniDump::ReadExceptionRecord() {
437 size_t size = 0;
438 auto exception_stream_ptr = static_cast<MINIDUMP_EXCEPTION_STREAM*>(FindDumpStream(ExceptionStream, &size));
439 if (exception_stream_ptr)
440 {
441 m_data_up->m_exception_sp.reset(new ExceptionRecord(exception_stream_ptr->ExceptionRecord, exception_stream_ptr->ThreadId));
442 }
443}
444
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000445void
446ProcessWinMiniDump::ReadModuleList() {
447 size_t size = 0;
448 auto module_list_ptr = static_cast<MINIDUMP_MODULE_LIST*>(FindDumpStream(ModuleListStream, &size));
449 if (!module_list_ptr || module_list_ptr->NumberOfModules == 0)
450 {
451 return;
452 }
453
454 for (ULONG32 i = 0; i < module_list_ptr->NumberOfModules; ++i)
455 {
456 const auto &module = module_list_ptr->Modules[i];
457 const auto file_name = GetMiniDumpString(m_data_up->m_base_addr, module.ModuleNameRva);
458 ModuleSpec module_spec = FileSpec(file_name, true);
459
460 lldb::ModuleSP module_sp = GetTarget().GetSharedModule(module_spec);
461 if (!module_sp)
462 {
463 continue;
464 }
465 bool load_addr_changed = false;
466 module_sp->SetLoadAddress(GetTarget(), module.BaseOfImage, false, load_addr_changed);
467 }
468}
469
Adrian McCarthy61ede152015-08-19 20:43:22 +0000470void *
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000471ProcessWinMiniDump::FindDumpStream(unsigned stream_number, size_t *size_out) const
472{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000473 void *stream = nullptr;
474 *size_out = 0;
475
476 assert(m_data_up != nullptr);
477 assert(m_data_up->m_base_addr != 0);
478
479 MINIDUMP_DIRECTORY *dir = nullptr;
480 if (::MiniDumpReadDumpStream(m_data_up->m_base_addr, stream_number, &dir, nullptr, nullptr) &&
481 dir != nullptr && dir->Location.DataSize > 0)
482 {
483 assert(dir->StreamType == stream_number);
484 *size_out = dir->Location.DataSize;
485 stream = static_cast<void*>(static_cast<char*>(m_data_up->m_base_addr) + dir->Location.Rva);
486 }
487
488 return stream;
489}