blob: c27ddb2a7051cccb21d0b3b89e12e73b84e46109 [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) :
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 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
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000288ArchSpec
289ProcessWinMiniDump::GetArchitecture()
290{
291 // TODO
292 return ArchSpec();
293}
294
295
296ProcessWinMiniDump::Data::Data() :
297 m_dump_file(INVALID_HANDLE_VALUE),
298 m_mapping(NULL),
299 m_base_addr(nullptr)
300{
301}
302
303ProcessWinMiniDump::Data::~Data()
304{
305 if (m_base_addr)
306 {
307 ::UnmapViewOfFile(m_base_addr);
308 m_base_addr = nullptr;
309 }
310 if (m_mapping)
311 {
312 ::CloseHandle(m_mapping);
313 m_mapping = NULL;
314 }
315 if (m_dump_file != INVALID_HANDLE_VALUE)
316 {
317 ::CloseHandle(m_dump_file);
318 m_dump_file = INVALID_HANDLE_VALUE;
319 }
320}
321
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000322bool
323ProcessWinMiniDump::FindMemoryRange(lldb::addr_t addr, Range *range_out) const
324{
325 size_t stream_size = 0;
326 auto mem_list_stream = static_cast<const MINIDUMP_MEMORY_LIST *>(FindDumpStream(MemoryListStream, &stream_size));
327 if (mem_list_stream)
328 {
329 for (ULONG32 i = 0; i < mem_list_stream->NumberOfMemoryRanges; ++i) {
330 const MINIDUMP_MEMORY_DESCRIPTOR &mem_desc = mem_list_stream->MemoryRanges[i];
331 const MINIDUMP_LOCATION_DESCRIPTOR &loc_desc = mem_desc.Memory;
332 const lldb::addr_t range_start = mem_desc.StartOfMemoryRange;
333 const size_t range_size = loc_desc.DataSize;
334 if (range_start <= addr && addr < range_start + range_size)
335 {
336 range_out->start = range_start;
337 range_out->size = range_size;
338 range_out->ptr = reinterpret_cast<const uint8_t *>(m_data_up->m_base_addr) + loc_desc.Rva;
339 return true;
340 }
341 }
342 }
343
344 // Some mini dumps have a Memory64ListStream that captures all the heap
345 // memory. We can't exactly use the same loop as above, because the mini
346 // dump uses slightly different data structures to describe those.
347 auto mem_list64_stream = static_cast<const MINIDUMP_MEMORY64_LIST *>(FindDumpStream(Memory64ListStream, &stream_size));
348 if (mem_list64_stream)
349 {
350 size_t base_rva = mem_list64_stream->BaseRva;
351 for (ULONG32 i = 0; i < mem_list64_stream->NumberOfMemoryRanges; ++i) {
352 const MINIDUMP_MEMORY_DESCRIPTOR64 &mem_desc = mem_list64_stream->MemoryRanges[i];
353 const lldb::addr_t range_start = mem_desc.StartOfMemoryRange;
354 const size_t range_size = mem_desc.DataSize;
355 if (range_start <= addr && addr < range_start + range_size)
356 {
357 range_out->start = range_start;
358 range_out->size = range_size;
359 range_out->ptr = reinterpret_cast<const uint8_t *>(m_data_up->m_base_addr) + base_rva;
360 return true;
361 }
362 base_rva += range_size;
363 }
364 }
365
366 return false;
367}
368
369
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000370Error
371ProcessWinMiniDump::MapMiniDumpIntoMemory(const char *file)
372{
373 Error error;
374
375 m_data_up->m_dump_file = ::CreateFile(file, GENERIC_READ, FILE_SHARE_READ,
376 NULL, OPEN_EXISTING,
377 FILE_ATTRIBUTE_NORMAL, NULL);
378 if (m_data_up->m_dump_file == INVALID_HANDLE_VALUE)
379 {
380 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
381 return error;
382 }
383
384 m_data_up->m_mapping = ::CreateFileMapping(m_data_up->m_dump_file, NULL,
385 PAGE_READONLY, 0, 0, NULL);
386 if (m_data_up->m_mapping == NULL)
387 {
388 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
389 return error;
390 }
391
392 m_data_up->m_base_addr = ::MapViewOfFile(m_data_up->m_mapping, FILE_MAP_READ, 0, 0, 0);
393 if (m_data_up->m_base_addr == NULL)
394 {
395 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
396 return error;
397 }
398
399 return error;
400}
401
402
403ArchSpec
404ProcessWinMiniDump::DetermineArchitecture()
405{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000406 size_t size = 0;
407 auto system_info_ptr = static_cast<const MINIDUMP_SYSTEM_INFO *>(FindDumpStream(SystemInfoStream, &size));
408 if (system_info_ptr)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000409 {
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000410 switch (system_info_ptr->ProcessorArchitecture)
411 {
412 case PROCESSOR_ARCHITECTURE_INTEL:
413 return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_I386, LLDB_INVALID_CPUTYPE);
414 case PROCESSOR_ARCHITECTURE_AMD64:
415 return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_AMD64, LLDB_INVALID_CPUTYPE);
416 default:
417 break;
418 }
419 }
420
421 return ArchSpec(); // invalid or unknown
422}
Adrian McCarthy61ede152015-08-19 20:43:22 +0000423
424void
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000425ProcessWinMiniDump::ReadExceptionRecord()
426{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000427 size_t size = 0;
428 auto exception_stream_ptr = static_cast<MINIDUMP_EXCEPTION_STREAM*>(FindDumpStream(ExceptionStream, &size));
429 if (exception_stream_ptr)
430 {
431 m_data_up->m_exception_sp.reset(new ExceptionRecord(exception_stream_ptr->ExceptionRecord, exception_stream_ptr->ThreadId));
432 }
433}
434
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000435void
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000436ProcessWinMiniDump::ReadMiscInfo()
437{
438 size_t size = 0;
439 const auto misc_info_ptr = static_cast<MINIDUMP_MISC_INFO*>(FindDumpStream(MiscInfoStream, &size));
440 if (!misc_info_ptr || size < sizeof(MINIDUMP_MISC_INFO)) {
441 return;
442 }
443
444 if ((misc_info_ptr->Flags1 & MINIDUMP_MISC1_PROCESS_ID) != 0) {
445 // This misc info record has the process ID.
446 SetID(misc_info_ptr->ProcessId);
447 }
448}
449
450void
451ProcessWinMiniDump::ReadModuleList()
452{
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000453 size_t size = 0;
454 auto module_list_ptr = static_cast<MINIDUMP_MODULE_LIST*>(FindDumpStream(ModuleListStream, &size));
455 if (!module_list_ptr || module_list_ptr->NumberOfModules == 0)
456 {
457 return;
458 }
459
460 for (ULONG32 i = 0; i < module_list_ptr->NumberOfModules; ++i)
461 {
462 const auto &module = module_list_ptr->Modules[i];
463 const auto file_name = GetMiniDumpString(m_data_up->m_base_addr, module.ModuleNameRva);
464 ModuleSpec module_spec = FileSpec(file_name, true);
465
466 lldb::ModuleSP module_sp = GetTarget().GetSharedModule(module_spec);
467 if (!module_sp)
468 {
469 continue;
470 }
471 bool load_addr_changed = false;
472 module_sp->SetLoadAddress(GetTarget(), module.BaseOfImage, false, load_addr_changed);
473 }
474}
475
Adrian McCarthy61ede152015-08-19 20:43:22 +0000476void *
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000477ProcessWinMiniDump::FindDumpStream(unsigned stream_number, size_t *size_out) const
478{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000479 void *stream = nullptr;
480 *size_out = 0;
481
482 assert(m_data_up != nullptr);
483 assert(m_data_up->m_base_addr != 0);
484
485 MINIDUMP_DIRECTORY *dir = nullptr;
486 if (::MiniDumpReadDumpStream(m_data_up->m_base_addr, stream_number, &dir, nullptr, nullptr) &&
487 dir != nullptr && dir->Location.DataSize > 0)
488 {
489 assert(dir->StreamType == stream_number);
490 *size_out = dir->Location.DataSize;
491 stream = static_cast<void*>(static_cast<char*>(m_data_up->m_base_addr) + dir->Location.Rva);
492 }
493
494 return stream;
495}