blob: 7279ae986d7460a4991e765376589def5253bcca [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>
Adrian McCarthyd9fa2b52015-11-12 21:16:15 +000017#include <memory>
Adrian McCarthyc96516f2015-08-03 23:01:51 +000018#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) :
Adrian McCarthy18a9135d2015-10-28 18:21:45 +0000126 ProcessWindows(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 McCarthyab59a0f2015-09-17 20:52:29 +0000167 ReadMiscInfo(); // notably for process ID
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000168 ReadModuleList();
Adrian McCarthy61ede152015-08-19 20:43:22 +0000169 ReadExceptionRecord();
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000170
171 return error;
172
173}
174
175DynamicLoader *
176ProcessWinMiniDump::GetDynamicLoader()
177{
178 if (m_dyld_ap.get() == NULL)
179 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, DynamicLoaderWindowsDYLD::GetPluginNameStatic().GetCString()));
180 return m_dyld_ap.get();
181}
182
183bool
184ProcessWinMiniDump::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list)
185{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000186 size_t size = 0;
187 auto thread_list_ptr = static_cast<const MINIDUMP_THREAD_LIST *>(FindDumpStream(ThreadListStream, &size));
188 if (thread_list_ptr)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000189 {
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000190 const ULONG32 thread_count = thread_list_ptr->NumberOfThreads;
Adrian McCarthy61ede152015-08-19 20:43:22 +0000191 for (ULONG32 i = 0; i < thread_count; ++i) {
Adrian McCarthyd9fa2b52015-11-12 21:16:15 +0000192 const auto &mini_dump_thread = thread_list_ptr->Threads[i];
193 auto thread_sp = std::make_shared<ThreadWinMiniDump>(*this, mini_dump_thread.ThreadId);
194 if (mini_dump_thread.ThreadContext.DataSize >= sizeof(CONTEXT))
195 {
196 const CONTEXT *context = reinterpret_cast<const CONTEXT *>(static_cast<const char *>(m_data_up->m_base_addr) + mini_dump_thread.ThreadContext.Rva);
197 thread_sp->SetContext(context);
198 }
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000199 new_thread_list.AddThread(thread_sp);
200 }
201 }
202
203 return new_thread_list.GetSize(false) > 0;
204}
205
206void
207ProcessWinMiniDump::RefreshStateAfterStop()
208{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000209 if (!m_data_up) return;
210 if (!m_data_up->m_exception_sp) return;
211
212 auto active_exception = m_data_up->m_exception_sp;
213 std::string desc;
214 llvm::raw_string_ostream desc_stream(desc);
215 desc_stream << "Exception "
216 << llvm::format_hex(active_exception->GetExceptionCode(), 8)
217 << " encountered at address "
218 << llvm::format_hex(active_exception->GetExceptionAddress(), 8);
219 m_thread_list.SetSelectedThreadByID(active_exception->GetThreadID());
220 auto stop_thread = m_thread_list.GetSelectedThread();
221 auto stop_info = StopInfo::CreateStopReasonWithException(*stop_thread, desc_stream.str().c_str());
222 stop_thread->SetStopInfo(stop_info);
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000223}
224
225Error
226ProcessWinMiniDump::DoDestroy()
227{
228 return Error();
229}
230
231bool
232ProcessWinMiniDump::IsAlive()
233{
234 return true;
235}
236
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000237bool
238ProcessWinMiniDump::WarnBeforeDetach () const
239{
240 // Since this is post-mortem debugging, there's no need to warn the user
241 // that quitting the debugger will terminate the process.
242 return false;
243}
244
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000245size_t
246ProcessWinMiniDump::ReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error)
247{
248 // Don't allow the caching that lldb_private::Process::ReadMemory does
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000249 // since we have it all cached our our dump file anyway.
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000250 return DoReadMemory(addr, buf, size, error);
251}
252
253size_t
254ProcessWinMiniDump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error)
255{
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000256 // I don't have a sense of how frequently this is called or how many memory
257 // ranges a mini dump typically has, so I'm not sure if searching for the
258 // appropriate range linearly each time is stupid. Perhaps we should build
259 // an index for faster lookups.
260 Range range = {0};
261 if (!FindMemoryRange(addr, &range))
262 {
263 return 0;
264 }
265
266 // There's at least some overlap between the beginning of the desired range
267 // (addr) and the current range. Figure out where the overlap begins and
268 // how much overlap there is, then copy it to the destination buffer.
269 const size_t offset = range.start - addr;
270 const size_t overlap = std::min(size, range.size - offset);
271 std::memcpy(buf, range.ptr + offset, overlap);
272 return overlap;
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000273}
274
275void
276ProcessWinMiniDump::Clear()
277{
278 m_thread_list.Clear();
279}
280
281void
282ProcessWinMiniDump::Initialize()
283{
284 static std::once_flag g_once_flag;
285
286 std::call_once(g_once_flag, []()
287 {
288 PluginManager::RegisterPlugin(GetPluginNameStatic(),
289 GetPluginDescriptionStatic(),
290 CreateInstance);
291 });
292}
293
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000294ArchSpec
295ProcessWinMiniDump::GetArchitecture()
296{
297 // TODO
298 return ArchSpec();
299}
300
301
302ProcessWinMiniDump::Data::Data() :
303 m_dump_file(INVALID_HANDLE_VALUE),
304 m_mapping(NULL),
305 m_base_addr(nullptr)
306{
307}
308
309ProcessWinMiniDump::Data::~Data()
310{
311 if (m_base_addr)
312 {
313 ::UnmapViewOfFile(m_base_addr);
314 m_base_addr = nullptr;
315 }
316 if (m_mapping)
317 {
318 ::CloseHandle(m_mapping);
319 m_mapping = NULL;
320 }
321 if (m_dump_file != INVALID_HANDLE_VALUE)
322 {
323 ::CloseHandle(m_dump_file);
324 m_dump_file = INVALID_HANDLE_VALUE;
325 }
326}
327
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000328bool
329ProcessWinMiniDump::FindMemoryRange(lldb::addr_t addr, Range *range_out) const
330{
331 size_t stream_size = 0;
332 auto mem_list_stream = static_cast<const MINIDUMP_MEMORY_LIST *>(FindDumpStream(MemoryListStream, &stream_size));
333 if (mem_list_stream)
334 {
335 for (ULONG32 i = 0; i < mem_list_stream->NumberOfMemoryRanges; ++i) {
336 const MINIDUMP_MEMORY_DESCRIPTOR &mem_desc = mem_list_stream->MemoryRanges[i];
337 const MINIDUMP_LOCATION_DESCRIPTOR &loc_desc = mem_desc.Memory;
338 const lldb::addr_t range_start = mem_desc.StartOfMemoryRange;
339 const size_t range_size = loc_desc.DataSize;
340 if (range_start <= addr && addr < range_start + range_size)
341 {
342 range_out->start = range_start;
343 range_out->size = range_size;
344 range_out->ptr = reinterpret_cast<const uint8_t *>(m_data_up->m_base_addr) + loc_desc.Rva;
345 return true;
346 }
347 }
348 }
349
350 // Some mini dumps have a Memory64ListStream that captures all the heap
351 // memory. We can't exactly use the same loop as above, because the mini
352 // dump uses slightly different data structures to describe those.
353 auto mem_list64_stream = static_cast<const MINIDUMP_MEMORY64_LIST *>(FindDumpStream(Memory64ListStream, &stream_size));
354 if (mem_list64_stream)
355 {
356 size_t base_rva = mem_list64_stream->BaseRva;
357 for (ULONG32 i = 0; i < mem_list64_stream->NumberOfMemoryRanges; ++i) {
358 const MINIDUMP_MEMORY_DESCRIPTOR64 &mem_desc = mem_list64_stream->MemoryRanges[i];
359 const lldb::addr_t range_start = mem_desc.StartOfMemoryRange;
360 const size_t range_size = mem_desc.DataSize;
361 if (range_start <= addr && addr < range_start + range_size)
362 {
363 range_out->start = range_start;
364 range_out->size = range_size;
365 range_out->ptr = reinterpret_cast<const uint8_t *>(m_data_up->m_base_addr) + base_rva;
366 return true;
367 }
368 base_rva += range_size;
369 }
370 }
371
372 return false;
373}
374
375
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000376Error
377ProcessWinMiniDump::MapMiniDumpIntoMemory(const char *file)
378{
379 Error error;
380
381 m_data_up->m_dump_file = ::CreateFile(file, GENERIC_READ, FILE_SHARE_READ,
382 NULL, OPEN_EXISTING,
383 FILE_ATTRIBUTE_NORMAL, NULL);
384 if (m_data_up->m_dump_file == INVALID_HANDLE_VALUE)
385 {
386 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
387 return error;
388 }
389
390 m_data_up->m_mapping = ::CreateFileMapping(m_data_up->m_dump_file, NULL,
391 PAGE_READONLY, 0, 0, NULL);
392 if (m_data_up->m_mapping == NULL)
393 {
394 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
395 return error;
396 }
397
398 m_data_up->m_base_addr = ::MapViewOfFile(m_data_up->m_mapping, FILE_MAP_READ, 0, 0, 0);
399 if (m_data_up->m_base_addr == NULL)
400 {
401 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
402 return error;
403 }
404
405 return error;
406}
407
408
409ArchSpec
410ProcessWinMiniDump::DetermineArchitecture()
411{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000412 size_t size = 0;
413 auto system_info_ptr = static_cast<const MINIDUMP_SYSTEM_INFO *>(FindDumpStream(SystemInfoStream, &size));
414 if (system_info_ptr)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000415 {
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000416 switch (system_info_ptr->ProcessorArchitecture)
417 {
418 case PROCESSOR_ARCHITECTURE_INTEL:
419 return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_I386, LLDB_INVALID_CPUTYPE);
420 case PROCESSOR_ARCHITECTURE_AMD64:
421 return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_AMD64, LLDB_INVALID_CPUTYPE);
422 default:
423 break;
424 }
425 }
426
427 return ArchSpec(); // invalid or unknown
428}
Adrian McCarthy61ede152015-08-19 20:43:22 +0000429
430void
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000431ProcessWinMiniDump::ReadExceptionRecord()
432{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000433 size_t size = 0;
434 auto exception_stream_ptr = static_cast<MINIDUMP_EXCEPTION_STREAM*>(FindDumpStream(ExceptionStream, &size));
435 if (exception_stream_ptr)
436 {
437 m_data_up->m_exception_sp.reset(new ExceptionRecord(exception_stream_ptr->ExceptionRecord, exception_stream_ptr->ThreadId));
438 }
439}
440
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000441void
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000442ProcessWinMiniDump::ReadMiscInfo()
443{
444 size_t size = 0;
445 const auto misc_info_ptr = static_cast<MINIDUMP_MISC_INFO*>(FindDumpStream(MiscInfoStream, &size));
446 if (!misc_info_ptr || size < sizeof(MINIDUMP_MISC_INFO)) {
447 return;
448 }
449
450 if ((misc_info_ptr->Flags1 & MINIDUMP_MISC1_PROCESS_ID) != 0) {
451 // This misc info record has the process ID.
452 SetID(misc_info_ptr->ProcessId);
453 }
454}
455
456void
457ProcessWinMiniDump::ReadModuleList()
458{
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000459 size_t size = 0;
460 auto module_list_ptr = static_cast<MINIDUMP_MODULE_LIST*>(FindDumpStream(ModuleListStream, &size));
461 if (!module_list_ptr || module_list_ptr->NumberOfModules == 0)
462 {
463 return;
464 }
465
466 for (ULONG32 i = 0; i < module_list_ptr->NumberOfModules; ++i)
467 {
468 const auto &module = module_list_ptr->Modules[i];
469 const auto file_name = GetMiniDumpString(m_data_up->m_base_addr, module.ModuleNameRva);
470 ModuleSpec module_spec = FileSpec(file_name, true);
471
472 lldb::ModuleSP module_sp = GetTarget().GetSharedModule(module_spec);
473 if (!module_sp)
474 {
475 continue;
476 }
477 bool load_addr_changed = false;
478 module_sp->SetLoadAddress(GetTarget(), module.BaseOfImage, false, load_addr_changed);
479 }
480}
481
Adrian McCarthy61ede152015-08-19 20:43:22 +0000482void *
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000483ProcessWinMiniDump::FindDumpStream(unsigned stream_number, size_t *size_out) const
484{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000485 void *stream = nullptr;
486 *size_out = 0;
487
488 assert(m_data_up != nullptr);
489 assert(m_data_up->m_base_addr != 0);
490
491 MINIDUMP_DIRECTORY *dir = nullptr;
492 if (::MiniDumpReadDumpStream(m_data_up->m_base_addr, stream_number, &dir, nullptr, nullptr) &&
493 dir != nullptr && dir->Location.DataSize > 0)
494 {
495 assert(dir->StreamType == stream_number);
496 *size_out = dir->Location.DataSize;
497 stream = static_cast<void*>(static_cast<char*>(m_data_up->m_base_addr) + dir->Location.Rva);
498 }
499
500 return stream;
501}