blob: 87dc18d2e031ba3d26f5dc2e08505d7082aa9824 [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
Adrian McCarthy0c35cde2015-12-04 22:22:15 +000020#include "Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.h"
Adrian McCarthyc96516f2015-08-03 23:01:51 +000021#include "lldb/Core/DataBufferHeap.h"
22#include "lldb/Core/Log.h"
Adrian McCarthy0c35cde2015-12-04 22:22:15 +000023#include "lldb/Core/Module.h"
24#include "lldb/Core/ModuleSpec.h"
25#include "lldb/Core/PluginManager.h"
26#include "lldb/Core/Section.h"
27#include "lldb/Core/State.h"
28#include "lldb/Target/DynamicLoader.h"
29#include "lldb/Target/MemoryRegionInfo.h"
Adrian McCarthy61ede152015-08-19 20:43:22 +000030#include "lldb/Target/StopInfo.h"
Adrian McCarthyc96516f2015-08-03 23:01:51 +000031#include "lldb/Target/Target.h"
Adrian McCarthyc96516f2015-08-03 23:01:51 +000032#include "lldb/Target/UnixSignals.h"
Adrian McCarthy278a6c92015-12-09 00:29:38 +000033#include "lldb/Utility/LLDBAssert.h"
Adrian McCarthy0c35cde2015-12-04 22:22:15 +000034#include "llvm/Support/ConvertUTF.h"
Adrian McCarthy61ede152015-08-19 20:43:22 +000035#include "llvm/Support/Format.h"
36#include "llvm/Support/raw_ostream.h"
Adrian McCarthyc96516f2015-08-03 23:01:51 +000037
Adrian McCarthy0a750822016-02-25 00:23:27 +000038#include "Plugins/Process/Windows/Common/NtStructures.h"
39#include "Plugins/Process/Windows/Common/ProcessWindowsLog.h"
40
Adrian McCarthy27785dd2015-08-24 16:00:51 +000041#include "ExceptionRecord.h"
Adrian McCarthyc96516f2015-08-03 23:01:51 +000042#include "ThreadWinMiniDump.h"
43
44using namespace lldb_private;
45
Adrian McCarthy23d14b62015-08-28 14:42:03 +000046namespace
47{
48
49// Getting a string out of a mini dump is a chore. You're usually given a
50// relative virtual address (RVA), which points to a counted string that's in
51// Windows Unicode (UTF-16). This wrapper handles all the redirection and
52// returns a UTF-8 copy of the string.
53std::string
54GetMiniDumpString(const void *base_addr, const RVA rva)
55{
56 std::string result;
57 if (!base_addr)
58 {
59 return result;
60 }
61 auto md_string = reinterpret_cast<const MINIDUMP_STRING *>(static_cast<const char *>(base_addr) + rva);
62 auto source_start = reinterpret_cast<const UTF16 *>(md_string->Buffer);
63 const auto source_length = ::wcslen(md_string->Buffer);
64 const auto source_end = source_start + source_length;
65 result.resize(4*source_length); // worst case length
66 auto result_start = reinterpret_cast<UTF8 *>(&result[0]);
67 const auto result_end = result_start + result.size();
68 ConvertUTF16toUTF8(&source_start, source_end, &result_start, result_end, strictConversion);
69 const auto result_size = std::distance(reinterpret_cast<UTF8 *>(&result[0]), result_start);
70 result.resize(result_size); // shrink to actual length
71 return result;
72}
73
74} // anonymous namespace
75
Adrian McCarthyc96516f2015-08-03 23:01:51 +000076// Encapsulates the private data for ProcessWinMiniDump.
77// TODO(amccarth): Determine if we need a mutex for access.
78class ProcessWinMiniDump::Data
79{
80public:
81 Data();
82 ~Data();
83
84 FileSpec m_core_file;
85 HANDLE m_dump_file; // handle to the open minidump file
86 HANDLE m_mapping; // handle to the file mapping for the minidump file
87 void * m_base_addr; // base memory address of the minidump
Adrian McCarthy61ede152015-08-19 20:43:22 +000088 std::shared_ptr<ExceptionRecord> m_exception_sp;
Adrian McCarthy0a750822016-02-25 00:23:27 +000089 bool m_is_wow64; // minidump is of a 32-bit process captured with a 64-bit debugger
Adrian McCarthyc96516f2015-08-03 23:01:51 +000090};
91
92ConstString
93ProcessWinMiniDump::GetPluginNameStatic()
94{
95 static ConstString g_name("win-minidump");
96 return g_name;
97}
98
99const char *
100ProcessWinMiniDump::GetPluginDescriptionStatic()
101{
102 return "Windows minidump plug-in.";
103}
104
105void
106ProcessWinMiniDump::Terminate()
107{
108 PluginManager::UnregisterPlugin(ProcessWinMiniDump::CreateInstance);
109}
110
111
112lldb::ProcessSP
Zachary Turner7529df92015-09-01 20:02:29 +0000113ProcessWinMiniDump::CreateInstance(lldb::TargetSP target_sp, Listener &listener, const FileSpec *crash_file)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000114{
115 lldb::ProcessSP process_sp;
116 if (crash_file)
117 {
Zachary Turner7529df92015-09-01 20:02:29 +0000118 process_sp.reset(new ProcessWinMiniDump(target_sp, listener, *crash_file));
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000119 }
120 return process_sp;
121}
122
123bool
Zachary Turner7529df92015-09-01 20:02:29 +0000124ProcessWinMiniDump::CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000125{
126 // TODO(amccarth): Eventually, this needs some actual logic.
127 return true;
128}
129
Zachary Turner7529df92015-09-01 20:02:29 +0000130ProcessWinMiniDump::ProcessWinMiniDump(lldb::TargetSP target_sp, Listener &listener,
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000131 const FileSpec &core_file) :
Adrian McCarthy18a9135d2015-10-28 18:21:45 +0000132 ProcessWindows(target_sp, listener),
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000133 m_data_up(new Data)
134{
135 m_data_up->m_core_file = core_file;
136}
137
138ProcessWinMiniDump::~ProcessWinMiniDump()
139{
140 Clear();
141 // We need to call finalize on the process before destroying ourselves
142 // to make sure all of the broadcaster cleanup goes as planned. If we
143 // destruct this class, then Process::~Process() might have problems
144 // trying to fully destroy the broadcaster.
145 Finalize();
146}
147
148ConstString
149ProcessWinMiniDump::GetPluginName()
150{
151 return GetPluginNameStatic();
152}
153
154uint32_t
155ProcessWinMiniDump::GetPluginVersion()
156{
157 return 1;
158}
159
160
161Error
162ProcessWinMiniDump::DoLoadCore()
163{
164 Error error;
165
166 error = MapMiniDumpIntoMemory(m_data_up->m_core_file.GetCString());
167 if (error.Fail())
168 {
169 return error;
170 }
171
Zachary Turner7529df92015-09-01 20:02:29 +0000172 GetTarget().SetArchitecture(DetermineArchitecture());
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000173 ReadMiscInfo(); // notably for process ID
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000174 ReadModuleList();
Adrian McCarthy61ede152015-08-19 20:43:22 +0000175 ReadExceptionRecord();
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000176
177 return error;
178
179}
180
181DynamicLoader *
182ProcessWinMiniDump::GetDynamicLoader()
183{
184 if (m_dyld_ap.get() == NULL)
185 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, DynamicLoaderWindowsDYLD::GetPluginNameStatic().GetCString()));
186 return m_dyld_ap.get();
187}
188
189bool
190ProcessWinMiniDump::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list)
191{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000192 size_t size = 0;
193 auto thread_list_ptr = static_cast<const MINIDUMP_THREAD_LIST *>(FindDumpStream(ThreadListStream, &size));
194 if (thread_list_ptr)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000195 {
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000196 const ULONG32 thread_count = thread_list_ptr->NumberOfThreads;
Adrian McCarthy61ede152015-08-19 20:43:22 +0000197 for (ULONG32 i = 0; i < thread_count; ++i) {
Adrian McCarthyd9fa2b52015-11-12 21:16:15 +0000198 const auto &mini_dump_thread = thread_list_ptr->Threads[i];
199 auto thread_sp = std::make_shared<ThreadWinMiniDump>(*this, mini_dump_thread.ThreadId);
200 if (mini_dump_thread.ThreadContext.DataSize >= sizeof(CONTEXT))
201 {
Adrian McCarthy0a750822016-02-25 00:23:27 +0000202 const CONTEXT *context = reinterpret_cast<const CONTEXT *>(
203 static_cast<const char *>(m_data_up->m_base_addr) + mini_dump_thread.ThreadContext.Rva);
204
205 if (m_data_up->m_is_wow64)
206 {
207 // On Windows, a 32-bit process can run on a 64-bit machine under WOW64.
208 // If the minidump was captured with a 64-bit debugger, then the CONTEXT
209 // we just grabbed from the mini_dump_thread is the one for the 64-bit
210 // "native" process rather than the 32-bit "guest" process we care about.
211 // In this case, we can get the 32-bit CONTEXT from the TEB (Thread
212 // Environment Block) of the 64-bit process.
213 Error error;
214 TEB64 wow64teb = {0};
215 ReadMemory(mini_dump_thread.Teb, &wow64teb, sizeof(wow64teb), error);
216 if (error.Success())
217 {
218 // Slot 1 of the thread-local storage in the 64-bit TEB points to a structure
219 // that includes the 32-bit CONTEXT (after a ULONG).
220 // See: https://msdn.microsoft.com/en-us/library/ms681670.aspx
221 const size_t addr = wow64teb.TlsSlots[1];
222 Range range = {0};
223 if (FindMemoryRange(addr, &range))
224 {
225 lldbassert(range.start <= addr);
226 const size_t offset = addr - range.start + sizeof(ULONG);
227 if (offset < range.size)
228 {
229 const size_t overlap = range.size - offset;
230 if (overlap >= sizeof(CONTEXT))
231 {
232 context = reinterpret_cast<const CONTEXT *>(range.ptr + offset);
233 }
234 }
235 }
236 }
237
238 // NOTE: We don't currently use the TEB for anything else. If we need it in
239 // the future, the 32-bit TEB is located according to the address stored in the
240 // first slot of the 64-bit TEB (wow64teb.Reserved1[0]).
241 }
242
Adrian McCarthyd9fa2b52015-11-12 21:16:15 +0000243 thread_sp->SetContext(context);
244 }
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000245 new_thread_list.AddThread(thread_sp);
246 }
247 }
248
249 return new_thread_list.GetSize(false) > 0;
250}
251
252void
253ProcessWinMiniDump::RefreshStateAfterStop()
254{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000255 if (!m_data_up) return;
256 if (!m_data_up->m_exception_sp) return;
257
258 auto active_exception = m_data_up->m_exception_sp;
259 std::string desc;
260 llvm::raw_string_ostream desc_stream(desc);
261 desc_stream << "Exception "
262 << llvm::format_hex(active_exception->GetExceptionCode(), 8)
263 << " encountered at address "
264 << llvm::format_hex(active_exception->GetExceptionAddress(), 8);
265 m_thread_list.SetSelectedThreadByID(active_exception->GetThreadID());
266 auto stop_thread = m_thread_list.GetSelectedThread();
267 auto stop_info = StopInfo::CreateStopReasonWithException(*stop_thread, desc_stream.str().c_str());
268 stop_thread->SetStopInfo(stop_info);
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000269}
270
271Error
272ProcessWinMiniDump::DoDestroy()
273{
274 return Error();
275}
276
277bool
278ProcessWinMiniDump::IsAlive()
279{
280 return true;
281}
282
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000283bool
284ProcessWinMiniDump::WarnBeforeDetach () const
285{
286 // Since this is post-mortem debugging, there's no need to warn the user
287 // that quitting the debugger will terminate the process.
288 return false;
289}
290
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000291size_t
292ProcessWinMiniDump::ReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error)
293{
294 // Don't allow the caching that lldb_private::Process::ReadMemory does
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000295 // since we have it all cached our our dump file anyway.
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000296 return DoReadMemory(addr, buf, size, error);
297}
298
299size_t
300ProcessWinMiniDump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error)
301{
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000302 // I don't have a sense of how frequently this is called or how many memory
303 // ranges a mini dump typically has, so I'm not sure if searching for the
304 // appropriate range linearly each time is stupid. Perhaps we should build
305 // an index for faster lookups.
306 Range range = {0};
307 if (!FindMemoryRange(addr, &range))
308 {
309 return 0;
310 }
311
312 // There's at least some overlap between the beginning of the desired range
313 // (addr) and the current range. Figure out where the overlap begins and
314 // how much overlap there is, then copy it to the destination buffer.
Adrian McCarthy278a6c92015-12-09 00:29:38 +0000315 lldbassert(range.start <= addr);
316 const size_t offset = addr - range.start;
317 lldbassert(offset < range.size);
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000318 const size_t overlap = std::min(size, range.size - offset);
319 std::memcpy(buf, range.ptr + offset, overlap);
320 return overlap;
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000321}
322
Adrian McCarthy0c35cde2015-12-04 22:22:15 +0000323Error
324ProcessWinMiniDump::GetMemoryRegionInfo(lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &info)
325{
326 Error error;
327 size_t size;
328 const auto list = reinterpret_cast<const MINIDUMP_MEMORY_INFO_LIST *>(FindDumpStream(MemoryInfoListStream, &size));
329 if (list == nullptr || size < sizeof(MINIDUMP_MEMORY_INFO_LIST))
330 {
331 error.SetErrorString("the mini dump contains no memory range information");
332 return error;
333 }
334
335 if (list->SizeOfEntry < sizeof(MINIDUMP_MEMORY_INFO))
336 {
337 error.SetErrorString("the entries in the mini dump memory info list are smaller than expected");
338 return error;
339 }
340
341 if (size < list->SizeOfHeader + list->SizeOfEntry * list->NumberOfEntries)
342 {
343 error.SetErrorString("the mini dump memory info list is incomplete");
344 return error;
345 }
346
347 for (int i = 0; i < list->NumberOfEntries; ++i)
348 {
349 const auto entry = reinterpret_cast<const MINIDUMP_MEMORY_INFO *>(reinterpret_cast<const char *>(list) +
350 list->SizeOfHeader + i * list->SizeOfEntry);
351 const auto head = entry->BaseAddress;
352 const auto tail = head + entry->RegionSize;
353 if (head <= load_addr && load_addr < tail)
354 {
355 info.SetReadable(IsPageReadable(entry->Protect) ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo);
356 info.SetWritable(IsPageWritable(entry->Protect) ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo);
357 info.SetExecutable(IsPageExecutable(entry->Protect) ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo);
358 return error;
359 }
360 }
361 // Note that the memory info list doesn't seem to contain ranges in kernel space,
362 // so if you're walking a stack that has kernel frames, the stack may appear
363 // truncated.
364 error.SetErrorString("address is not in a known range");
365 return error;
366}
367
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000368void
369ProcessWinMiniDump::Clear()
370{
371 m_thread_list.Clear();
372}
373
374void
375ProcessWinMiniDump::Initialize()
376{
377 static std::once_flag g_once_flag;
378
379 std::call_once(g_once_flag, []()
380 {
381 PluginManager::RegisterPlugin(GetPluginNameStatic(),
382 GetPluginDescriptionStatic(),
383 CreateInstance);
384 });
385}
386
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000387ArchSpec
388ProcessWinMiniDump::GetArchitecture()
389{
390 // TODO
391 return ArchSpec();
392}
393
Adrian McCarthy0a750822016-02-25 00:23:27 +0000394ProcessWinMiniDump::Data::Data()
395 : m_dump_file(INVALID_HANDLE_VALUE), m_mapping(NULL), m_base_addr(nullptr), m_is_wow64(false)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000396{
397}
398
399ProcessWinMiniDump::Data::~Data()
400{
401 if (m_base_addr)
402 {
403 ::UnmapViewOfFile(m_base_addr);
404 m_base_addr = nullptr;
405 }
406 if (m_mapping)
407 {
408 ::CloseHandle(m_mapping);
409 m_mapping = NULL;
410 }
411 if (m_dump_file != INVALID_HANDLE_VALUE)
412 {
413 ::CloseHandle(m_dump_file);
414 m_dump_file = INVALID_HANDLE_VALUE;
415 }
416}
417
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000418bool
419ProcessWinMiniDump::FindMemoryRange(lldb::addr_t addr, Range *range_out) const
420{
421 size_t stream_size = 0;
422 auto mem_list_stream = static_cast<const MINIDUMP_MEMORY_LIST *>(FindDumpStream(MemoryListStream, &stream_size));
423 if (mem_list_stream)
424 {
Adrian McCarthy0a750822016-02-25 00:23:27 +0000425 for (ULONG32 i = 0; i < mem_list_stream->NumberOfMemoryRanges; ++i)
426 {
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000427 const MINIDUMP_MEMORY_DESCRIPTOR &mem_desc = mem_list_stream->MemoryRanges[i];
428 const MINIDUMP_LOCATION_DESCRIPTOR &loc_desc = mem_desc.Memory;
429 const lldb::addr_t range_start = mem_desc.StartOfMemoryRange;
430 const size_t range_size = loc_desc.DataSize;
431 if (range_start <= addr && addr < range_start + range_size)
432 {
433 range_out->start = range_start;
434 range_out->size = range_size;
435 range_out->ptr = reinterpret_cast<const uint8_t *>(m_data_up->m_base_addr) + loc_desc.Rva;
436 return true;
437 }
438 }
439 }
440
441 // Some mini dumps have a Memory64ListStream that captures all the heap
442 // memory. We can't exactly use the same loop as above, because the mini
443 // dump uses slightly different data structures to describe those.
444 auto mem_list64_stream = static_cast<const MINIDUMP_MEMORY64_LIST *>(FindDumpStream(Memory64ListStream, &stream_size));
445 if (mem_list64_stream)
446 {
447 size_t base_rva = mem_list64_stream->BaseRva;
448 for (ULONG32 i = 0; i < mem_list64_stream->NumberOfMemoryRanges; ++i) {
449 const MINIDUMP_MEMORY_DESCRIPTOR64 &mem_desc = mem_list64_stream->MemoryRanges[i];
450 const lldb::addr_t range_start = mem_desc.StartOfMemoryRange;
451 const size_t range_size = mem_desc.DataSize;
452 if (range_start <= addr && addr < range_start + range_size)
453 {
454 range_out->start = range_start;
455 range_out->size = range_size;
456 range_out->ptr = reinterpret_cast<const uint8_t *>(m_data_up->m_base_addr) + base_rva;
457 return true;
458 }
459 base_rva += range_size;
460 }
461 }
462
463 return false;
464}
465
466
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000467Error
468ProcessWinMiniDump::MapMiniDumpIntoMemory(const char *file)
469{
470 Error error;
471
472 m_data_up->m_dump_file = ::CreateFile(file, GENERIC_READ, FILE_SHARE_READ,
473 NULL, OPEN_EXISTING,
474 FILE_ATTRIBUTE_NORMAL, NULL);
475 if (m_data_up->m_dump_file == INVALID_HANDLE_VALUE)
476 {
477 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
478 return error;
479 }
480
481 m_data_up->m_mapping = ::CreateFileMapping(m_data_up->m_dump_file, NULL,
482 PAGE_READONLY, 0, 0, NULL);
483 if (m_data_up->m_mapping == NULL)
484 {
485 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
486 return error;
487 }
488
489 m_data_up->m_base_addr = ::MapViewOfFile(m_data_up->m_mapping, FILE_MAP_READ, 0, 0, 0);
490 if (m_data_up->m_base_addr == NULL)
491 {
492 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
493 return error;
494 }
495
496 return error;
497}
498
499
500ArchSpec
501ProcessWinMiniDump::DetermineArchitecture()
502{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000503 size_t size = 0;
504 auto system_info_ptr = static_cast<const MINIDUMP_SYSTEM_INFO *>(FindDumpStream(SystemInfoStream, &size));
505 if (system_info_ptr)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000506 {
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000507 switch (system_info_ptr->ProcessorArchitecture)
508 {
509 case PROCESSOR_ARCHITECTURE_INTEL:
510 return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_I386, LLDB_INVALID_CPUTYPE);
511 case PROCESSOR_ARCHITECTURE_AMD64:
512 return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_AMD64, LLDB_INVALID_CPUTYPE);
513 default:
514 break;
515 }
516 }
517
518 return ArchSpec(); // invalid or unknown
519}
Adrian McCarthy61ede152015-08-19 20:43:22 +0000520
521void
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000522ProcessWinMiniDump::ReadExceptionRecord()
523{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000524 size_t size = 0;
525 auto exception_stream_ptr = static_cast<MINIDUMP_EXCEPTION_STREAM*>(FindDumpStream(ExceptionStream, &size));
526 if (exception_stream_ptr)
527 {
528 m_data_up->m_exception_sp.reset(new ExceptionRecord(exception_stream_ptr->ExceptionRecord, exception_stream_ptr->ThreadId));
529 }
Adrian McCarthy0a750822016-02-25 00:23:27 +0000530 else
531 {
532 WINLOG_IFALL(WINDOWS_LOG_PROCESS, "Minidump has no exception record.");
533 // TODO: See if we can recover the exception from the TEB.
534 }
Adrian McCarthy61ede152015-08-19 20:43:22 +0000535}
536
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000537void
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000538ProcessWinMiniDump::ReadMiscInfo()
539{
540 size_t size = 0;
541 const auto misc_info_ptr = static_cast<MINIDUMP_MISC_INFO*>(FindDumpStream(MiscInfoStream, &size));
542 if (!misc_info_ptr || size < sizeof(MINIDUMP_MISC_INFO)) {
543 return;
544 }
545
546 if ((misc_info_ptr->Flags1 & MINIDUMP_MISC1_PROCESS_ID) != 0) {
547 // This misc info record has the process ID.
548 SetID(misc_info_ptr->ProcessId);
549 }
550}
551
552void
553ProcessWinMiniDump::ReadModuleList()
554{
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000555 size_t size = 0;
556 auto module_list_ptr = static_cast<MINIDUMP_MODULE_LIST*>(FindDumpStream(ModuleListStream, &size));
557 if (!module_list_ptr || module_list_ptr->NumberOfModules == 0)
558 {
559 return;
560 }
561
562 for (ULONG32 i = 0; i < module_list_ptr->NumberOfModules; ++i)
563 {
564 const auto &module = module_list_ptr->Modules[i];
565 const auto file_name = GetMiniDumpString(m_data_up->m_base_addr, module.ModuleNameRva);
Adrian McCarthy0a750822016-02-25 00:23:27 +0000566 const auto file_spec = FileSpec(file_name, true);
567 if (FileSpec::Compare(file_spec, FileSpec("wow64.dll", false), false) == 0)
568 {
569 WINLOG_IFALL(WINDOWS_LOG_PROCESS, "Minidump is for a WOW64 process.");
570 m_data_up->m_is_wow64 = true;
571 }
572 ModuleSpec module_spec = file_spec;
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000573
574 lldb::ModuleSP module_sp = GetTarget().GetSharedModule(module_spec);
575 if (!module_sp)
576 {
577 continue;
578 }
579 bool load_addr_changed = false;
580 module_sp->SetLoadAddress(GetTarget(), module.BaseOfImage, false, load_addr_changed);
581 }
582}
583
Adrian McCarthy61ede152015-08-19 20:43:22 +0000584void *
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000585ProcessWinMiniDump::FindDumpStream(unsigned stream_number, size_t *size_out) const
586{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000587 void *stream = nullptr;
588 *size_out = 0;
589
590 assert(m_data_up != nullptr);
591 assert(m_data_up->m_base_addr != 0);
592
593 MINIDUMP_DIRECTORY *dir = nullptr;
594 if (::MiniDumpReadDumpStream(m_data_up->m_base_addr, stream_number, &dir, nullptr, nullptr) &&
595 dir != nullptr && dir->Location.DataSize > 0)
596 {
597 assert(dir->StreamType == stream_number);
598 *size_out = dir->Location.DataSize;
599 stream = static_cast<void*>(static_cast<char*>(m_data_up->m_base_addr) + dir->Location.Rva);
600 }
601
602 return stream;
603}