blob: 2ffdd1bcefcf9bbdb2fa9b6a61b05dc2bbbfd44f [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 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 McCarthyc96516f2015-08-03 23:01:51 +0000192 std::shared_ptr<ThreadWinMiniDump> thread_sp(new ThreadWinMiniDump(*this, thread_list_ptr->Threads[i].ThreadId));
193 new_thread_list.AddThread(thread_sp);
194 }
195 }
196
197 return new_thread_list.GetSize(false) > 0;
198}
199
200void
201ProcessWinMiniDump::RefreshStateAfterStop()
202{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000203 if (!m_data_up) return;
204 if (!m_data_up->m_exception_sp) return;
205
206 auto active_exception = m_data_up->m_exception_sp;
207 std::string desc;
208 llvm::raw_string_ostream desc_stream(desc);
209 desc_stream << "Exception "
210 << llvm::format_hex(active_exception->GetExceptionCode(), 8)
211 << " encountered at address "
212 << llvm::format_hex(active_exception->GetExceptionAddress(), 8);
213 m_thread_list.SetSelectedThreadByID(active_exception->GetThreadID());
214 auto stop_thread = m_thread_list.GetSelectedThread();
215 auto stop_info = StopInfo::CreateStopReasonWithException(*stop_thread, desc_stream.str().c_str());
216 stop_thread->SetStopInfo(stop_info);
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000217}
218
219Error
220ProcessWinMiniDump::DoDestroy()
221{
222 return Error();
223}
224
225bool
226ProcessWinMiniDump::IsAlive()
227{
228 return true;
229}
230
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000231bool
232ProcessWinMiniDump::WarnBeforeDetach () const
233{
234 // Since this is post-mortem debugging, there's no need to warn the user
235 // that quitting the debugger will terminate the process.
236 return false;
237}
238
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000239size_t
240ProcessWinMiniDump::ReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error)
241{
242 // Don't allow the caching that lldb_private::Process::ReadMemory does
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000243 // since we have it all cached our our dump file anyway.
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000244 return DoReadMemory(addr, buf, size, error);
245}
246
247size_t
248ProcessWinMiniDump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error)
249{
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000250 // I don't have a sense of how frequently this is called or how many memory
251 // ranges a mini dump typically has, so I'm not sure if searching for the
252 // appropriate range linearly each time is stupid. Perhaps we should build
253 // an index for faster lookups.
254 Range range = {0};
255 if (!FindMemoryRange(addr, &range))
256 {
257 return 0;
258 }
259
260 // There's at least some overlap between the beginning of the desired range
261 // (addr) and the current range. Figure out where the overlap begins and
262 // how much overlap there is, then copy it to the destination buffer.
263 const size_t offset = range.start - addr;
264 const size_t overlap = std::min(size, range.size - offset);
265 std::memcpy(buf, range.ptr + offset, overlap);
266 return overlap;
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000267}
268
269void
270ProcessWinMiniDump::Clear()
271{
272 m_thread_list.Clear();
273}
274
275void
276ProcessWinMiniDump::Initialize()
277{
278 static std::once_flag g_once_flag;
279
280 std::call_once(g_once_flag, []()
281 {
282 PluginManager::RegisterPlugin(GetPluginNameStatic(),
283 GetPluginDescriptionStatic(),
284 CreateInstance);
285 });
286}
287
288lldb::addr_t
289ProcessWinMiniDump::GetImageInfoAddress()
290{
291 Target *target = &GetTarget();
292 ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile();
293 Address addr = obj_file->GetImageInfoAddress(target);
294
295 if (addr.IsValid())
296 return addr.GetLoadAddress(target);
297 return LLDB_INVALID_ADDRESS;
298}
299
300ArchSpec
301ProcessWinMiniDump::GetArchitecture()
302{
303 // TODO
304 return ArchSpec();
305}
306
307
308ProcessWinMiniDump::Data::Data() :
309 m_dump_file(INVALID_HANDLE_VALUE),
310 m_mapping(NULL),
311 m_base_addr(nullptr)
312{
313}
314
315ProcessWinMiniDump::Data::~Data()
316{
317 if (m_base_addr)
318 {
319 ::UnmapViewOfFile(m_base_addr);
320 m_base_addr = nullptr;
321 }
322 if (m_mapping)
323 {
324 ::CloseHandle(m_mapping);
325 m_mapping = NULL;
326 }
327 if (m_dump_file != INVALID_HANDLE_VALUE)
328 {
329 ::CloseHandle(m_dump_file);
330 m_dump_file = INVALID_HANDLE_VALUE;
331 }
332}
333
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000334bool
335ProcessWinMiniDump::FindMemoryRange(lldb::addr_t addr, Range *range_out) const
336{
337 size_t stream_size = 0;
338 auto mem_list_stream = static_cast<const MINIDUMP_MEMORY_LIST *>(FindDumpStream(MemoryListStream, &stream_size));
339 if (mem_list_stream)
340 {
341 for (ULONG32 i = 0; i < mem_list_stream->NumberOfMemoryRanges; ++i) {
342 const MINIDUMP_MEMORY_DESCRIPTOR &mem_desc = mem_list_stream->MemoryRanges[i];
343 const MINIDUMP_LOCATION_DESCRIPTOR &loc_desc = mem_desc.Memory;
344 const lldb::addr_t range_start = mem_desc.StartOfMemoryRange;
345 const size_t range_size = loc_desc.DataSize;
346 if (range_start <= addr && addr < range_start + range_size)
347 {
348 range_out->start = range_start;
349 range_out->size = range_size;
350 range_out->ptr = reinterpret_cast<const uint8_t *>(m_data_up->m_base_addr) + loc_desc.Rva;
351 return true;
352 }
353 }
354 }
355
356 // Some mini dumps have a Memory64ListStream that captures all the heap
357 // memory. We can't exactly use the same loop as above, because the mini
358 // dump uses slightly different data structures to describe those.
359 auto mem_list64_stream = static_cast<const MINIDUMP_MEMORY64_LIST *>(FindDumpStream(Memory64ListStream, &stream_size));
360 if (mem_list64_stream)
361 {
362 size_t base_rva = mem_list64_stream->BaseRva;
363 for (ULONG32 i = 0; i < mem_list64_stream->NumberOfMemoryRanges; ++i) {
364 const MINIDUMP_MEMORY_DESCRIPTOR64 &mem_desc = mem_list64_stream->MemoryRanges[i];
365 const lldb::addr_t range_start = mem_desc.StartOfMemoryRange;
366 const size_t range_size = mem_desc.DataSize;
367 if (range_start <= addr && addr < range_start + range_size)
368 {
369 range_out->start = range_start;
370 range_out->size = range_size;
371 range_out->ptr = reinterpret_cast<const uint8_t *>(m_data_up->m_base_addr) + base_rva;
372 return true;
373 }
374 base_rva += range_size;
375 }
376 }
377
378 return false;
379}
380
381
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000382Error
383ProcessWinMiniDump::MapMiniDumpIntoMemory(const char *file)
384{
385 Error error;
386
387 m_data_up->m_dump_file = ::CreateFile(file, GENERIC_READ, FILE_SHARE_READ,
388 NULL, OPEN_EXISTING,
389 FILE_ATTRIBUTE_NORMAL, NULL);
390 if (m_data_up->m_dump_file == INVALID_HANDLE_VALUE)
391 {
392 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
393 return error;
394 }
395
396 m_data_up->m_mapping = ::CreateFileMapping(m_data_up->m_dump_file, NULL,
397 PAGE_READONLY, 0, 0, NULL);
398 if (m_data_up->m_mapping == NULL)
399 {
400 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
401 return error;
402 }
403
404 m_data_up->m_base_addr = ::MapViewOfFile(m_data_up->m_mapping, FILE_MAP_READ, 0, 0, 0);
405 if (m_data_up->m_base_addr == NULL)
406 {
407 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
408 return error;
409 }
410
411 return error;
412}
413
414
415ArchSpec
416ProcessWinMiniDump::DetermineArchitecture()
417{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000418 size_t size = 0;
419 auto system_info_ptr = static_cast<const MINIDUMP_SYSTEM_INFO *>(FindDumpStream(SystemInfoStream, &size));
420 if (system_info_ptr)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000421 {
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000422 switch (system_info_ptr->ProcessorArchitecture)
423 {
424 case PROCESSOR_ARCHITECTURE_INTEL:
425 return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_I386, LLDB_INVALID_CPUTYPE);
426 case PROCESSOR_ARCHITECTURE_AMD64:
427 return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_AMD64, LLDB_INVALID_CPUTYPE);
428 default:
429 break;
430 }
431 }
432
433 return ArchSpec(); // invalid or unknown
434}
Adrian McCarthy61ede152015-08-19 20:43:22 +0000435
436void
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000437ProcessWinMiniDump::ReadExceptionRecord()
438{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000439 size_t size = 0;
440 auto exception_stream_ptr = static_cast<MINIDUMP_EXCEPTION_STREAM*>(FindDumpStream(ExceptionStream, &size));
441 if (exception_stream_ptr)
442 {
443 m_data_up->m_exception_sp.reset(new ExceptionRecord(exception_stream_ptr->ExceptionRecord, exception_stream_ptr->ThreadId));
444 }
445}
446
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000447void
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000448ProcessWinMiniDump::ReadMiscInfo()
449{
450 size_t size = 0;
451 const auto misc_info_ptr = static_cast<MINIDUMP_MISC_INFO*>(FindDumpStream(MiscInfoStream, &size));
452 if (!misc_info_ptr || size < sizeof(MINIDUMP_MISC_INFO)) {
453 return;
454 }
455
456 if ((misc_info_ptr->Flags1 & MINIDUMP_MISC1_PROCESS_ID) != 0) {
457 // This misc info record has the process ID.
458 SetID(misc_info_ptr->ProcessId);
459 }
460}
461
462void
463ProcessWinMiniDump::ReadModuleList()
464{
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000465 size_t size = 0;
466 auto module_list_ptr = static_cast<MINIDUMP_MODULE_LIST*>(FindDumpStream(ModuleListStream, &size));
467 if (!module_list_ptr || module_list_ptr->NumberOfModules == 0)
468 {
469 return;
470 }
471
472 for (ULONG32 i = 0; i < module_list_ptr->NumberOfModules; ++i)
473 {
474 const auto &module = module_list_ptr->Modules[i];
475 const auto file_name = GetMiniDumpString(m_data_up->m_base_addr, module.ModuleNameRva);
476 ModuleSpec module_spec = FileSpec(file_name, true);
477
478 lldb::ModuleSP module_sp = GetTarget().GetSharedModule(module_spec);
479 if (!module_sp)
480 {
481 continue;
482 }
483 bool load_addr_changed = false;
484 module_sp->SetLoadAddress(GetTarget(), module.BaseOfImage, false, load_addr_changed);
485 }
486}
487
Adrian McCarthy61ede152015-08-19 20:43:22 +0000488void *
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000489ProcessWinMiniDump::FindDumpStream(unsigned stream_number, size_t *size_out) const
490{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000491 void *stream = nullptr;
492 *size_out = 0;
493
494 assert(m_data_up != nullptr);
495 assert(m_data_up->m_base_addr != 0);
496
497 MINIDUMP_DIRECTORY *dir = nullptr;
498 if (::MiniDumpReadDumpStream(m_data_up->m_base_addr, stream_number, &dir, nullptr, nullptr) &&
499 dir != nullptr && dir->Location.DataSize > 0)
500 {
501 assert(dir->StreamType == stream_number);
502 *size_out = dir->Location.DataSize;
503 stream = static_cast<void*>(static_cast<char*>(m_data_up->m_base_addr) + dir->Location.Rva);
504 }
505
506 return stream;
507}