blob: 1343886fa9d4579611d819700d771ba599d40a4e [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 McCarthya7ad58b2016-02-29 21:15:23 +000046// Implementation class for ProcessWinMiniDump encapsulates the Windows-specific
47// code, keeping non-portable types out of the header files.
48// TODO(amccarth): Determine if we need a mutex for access. Given that this is
49// postmortem debugging, I don't think so.
50class ProcessWinMiniDump::Impl
Adrian McCarthyc96516f2015-08-03 23:01:51 +000051{
52public:
Adrian McCarthya7ad58b2016-02-29 21:15:23 +000053 Impl(const FileSpec &core_file, ProcessWinMiniDump *self);
54 ~Impl();
Adrian McCarthyc96516f2015-08-03 23:01:51 +000055
Adrian McCarthya7ad58b2016-02-29 21:15:23 +000056 Error
57 DoLoadCore();
58
59 bool
60 UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list);
61
62 void
63 RefreshStateAfterStop();
64
65 size_t
66 DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error);
67
68 Error
69 GetMemoryRegionInfo(lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &info);
70
71private:
72 // Describes a range of memory captured in the mini dump.
73 struct Range
74 {
75 lldb::addr_t start; // virtual address of the beginning of the range
76 size_t size; // size of the range in bytes
77 const uint8_t *ptr; // absolute pointer to the first byte of the range
78 };
79
80 // If the mini dump has a memory range that contains the desired address, it
81 // returns true with the details of the range in *range_out. Otherwise, it
82 // returns false.
83 bool
84 FindMemoryRange(lldb::addr_t addr, Range *range_out) const;
85
86 lldb_private::Error
87 MapMiniDumpIntoMemory();
88
89 lldb_private::ArchSpec
90 DetermineArchitecture();
91
92 void
93 ReadExceptionRecord();
94
95 void
96 ReadMiscInfo();
97
98 void
99 ReadModuleList();
100
101 // A thin wrapper around WinAPI's MiniDumpReadDumpStream to avoid redundant
102 // checks. If there's a failure (e.g., if the requested stream doesn't exist),
103 // the function returns nullptr and sets *size_out to 0.
104 void *
105 FindDumpStream(unsigned stream_number, size_t *size_out) const;
106
107 // Getting a string out of a mini dump is a chore. You're usually given a
108 // relative virtual address (RVA), which points to a counted string that's in
109 // Windows Unicode (UTF-16). This wrapper handles all the redirection and
110 // returns a UTF-8 copy of the string.
111 std::string
112 GetMiniDumpString(RVA rva) const;
113
114 ProcessWinMiniDump *m_self; // non-owning back pointer
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000115 FileSpec m_core_file;
116 HANDLE m_dump_file; // handle to the open minidump file
117 HANDLE m_mapping; // handle to the file mapping for the minidump file
118 void * m_base_addr; // base memory address of the minidump
Adrian McCarthy61ede152015-08-19 20:43:22 +0000119 std::shared_ptr<ExceptionRecord> m_exception_sp;
Adrian McCarthy0a750822016-02-25 00:23:27 +0000120 bool m_is_wow64; // minidump is of a 32-bit process captured with a 64-bit debugger
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000121};
122
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000123ProcessWinMiniDump::Impl::Impl(const FileSpec &core_file, ProcessWinMiniDump *self)
124 : m_self(self),
125 m_core_file(core_file),
126 m_dump_file(INVALID_HANDLE_VALUE),
127 m_mapping(NULL),
128 m_base_addr(nullptr),
129 m_exception_sp(),
130 m_is_wow64(false)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000131{
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000132}
133
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000134ProcessWinMiniDump::Impl::~Impl()
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000135{
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000136 if (m_base_addr)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000137 {
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000138 ::UnmapViewOfFile(m_base_addr);
139 m_base_addr = nullptr;
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000140 }
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000141 if (m_mapping)
142 {
143 ::CloseHandle(m_mapping);
144 m_mapping = NULL;
145 }
146 if (m_dump_file != INVALID_HANDLE_VALUE)
147 {
148 ::CloseHandle(m_dump_file);
149 m_dump_file = INVALID_HANDLE_VALUE;
150 }
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000151}
152
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000153Error
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000154ProcessWinMiniDump::Impl::DoLoadCore()
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000155{
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000156 Error error = MapMiniDumpIntoMemory();
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000157 if (error.Fail())
158 {
159 return error;
160 }
161
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000162 m_self->GetTarget().SetArchitecture(DetermineArchitecture());
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000163 ReadMiscInfo(); // notably for process ID
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000164 ReadModuleList();
Adrian McCarthy61ede152015-08-19 20:43:22 +0000165 ReadExceptionRecord();
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000166
167 return error;
168
169}
170
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000171bool
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000172ProcessWinMiniDump::Impl::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000173{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000174 size_t size = 0;
175 auto thread_list_ptr = static_cast<const MINIDUMP_THREAD_LIST *>(FindDumpStream(ThreadListStream, &size));
176 if (thread_list_ptr)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000177 {
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000178 const ULONG32 thread_count = thread_list_ptr->NumberOfThreads;
Adrian McCarthy61ede152015-08-19 20:43:22 +0000179 for (ULONG32 i = 0; i < thread_count; ++i) {
Adrian McCarthyd9fa2b52015-11-12 21:16:15 +0000180 const auto &mini_dump_thread = thread_list_ptr->Threads[i];
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000181 auto thread_sp = std::make_shared<ThreadWinMiniDump>(*m_self, mini_dump_thread.ThreadId);
Adrian McCarthyd9fa2b52015-11-12 21:16:15 +0000182 if (mini_dump_thread.ThreadContext.DataSize >= sizeof(CONTEXT))
183 {
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000184 const CONTEXT *context = reinterpret_cast<const CONTEXT *>(static_cast<const char *>(m_base_addr) +
185 mini_dump_thread.ThreadContext.Rva);
Adrian McCarthy0a750822016-02-25 00:23:27 +0000186
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000187 if (m_is_wow64)
Adrian McCarthy0a750822016-02-25 00:23:27 +0000188 {
189 // On Windows, a 32-bit process can run on a 64-bit machine under WOW64.
190 // If the minidump was captured with a 64-bit debugger, then the CONTEXT
191 // we just grabbed from the mini_dump_thread is the one for the 64-bit
192 // "native" process rather than the 32-bit "guest" process we care about.
193 // In this case, we can get the 32-bit CONTEXT from the TEB (Thread
194 // Environment Block) of the 64-bit process.
195 Error error;
196 TEB64 wow64teb = {0};
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000197 m_self->ReadMemory(mini_dump_thread.Teb, &wow64teb, sizeof(wow64teb), error);
Adrian McCarthy0a750822016-02-25 00:23:27 +0000198 if (error.Success())
199 {
200 // Slot 1 of the thread-local storage in the 64-bit TEB points to a structure
201 // that includes the 32-bit CONTEXT (after a ULONG).
202 // See: https://msdn.microsoft.com/en-us/library/ms681670.aspx
203 const size_t addr = wow64teb.TlsSlots[1];
204 Range range = {0};
205 if (FindMemoryRange(addr, &range))
206 {
207 lldbassert(range.start <= addr);
208 const size_t offset = addr - range.start + sizeof(ULONG);
209 if (offset < range.size)
210 {
211 const size_t overlap = range.size - offset;
212 if (overlap >= sizeof(CONTEXT))
213 {
214 context = reinterpret_cast<const CONTEXT *>(range.ptr + offset);
215 }
216 }
217 }
218 }
219
220 // NOTE: We don't currently use the TEB for anything else. If we need it in
221 // the future, the 32-bit TEB is located according to the address stored in the
222 // first slot of the 64-bit TEB (wow64teb.Reserved1[0]).
223 }
224
Adrian McCarthyd9fa2b52015-11-12 21:16:15 +0000225 thread_sp->SetContext(context);
226 }
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000227 new_thread_list.AddThread(thread_sp);
228 }
229 }
230
231 return new_thread_list.GetSize(false) > 0;
232}
233
234void
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000235ProcessWinMiniDump::Impl::RefreshStateAfterStop()
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000236{
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000237 if (!m_exception_sp)
238 return;
Adrian McCarthy61ede152015-08-19 20:43:22 +0000239
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000240 auto active_exception = m_exception_sp;
Adrian McCarthy61ede152015-08-19 20:43:22 +0000241 std::string desc;
242 llvm::raw_string_ostream desc_stream(desc);
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000243 desc_stream << "Exception " << llvm::format_hex(active_exception->GetExceptionCode(), 8)
244 << " encountered at address " << llvm::format_hex(active_exception->GetExceptionAddress(), 8);
245 m_self->m_thread_list.SetSelectedThreadByID(active_exception->GetThreadID());
246 auto stop_thread = m_self->m_thread_list.GetSelectedThread();
Adrian McCarthy61ede152015-08-19 20:43:22 +0000247 auto stop_info = StopInfo::CreateStopReasonWithException(*stop_thread, desc_stream.str().c_str());
248 stop_thread->SetStopInfo(stop_info);
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000249}
250
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000251size_t
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000252ProcessWinMiniDump::Impl::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000253{
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000254 // I don't have a sense of how frequently this is called or how many memory
255 // ranges a mini dump typically has, so I'm not sure if searching for the
256 // appropriate range linearly each time is stupid. Perhaps we should build
257 // an index for faster lookups.
258 Range range = {0};
259 if (!FindMemoryRange(addr, &range))
260 {
261 return 0;
262 }
263
264 // There's at least some overlap between the beginning of the desired range
265 // (addr) and the current range. Figure out where the overlap begins and
266 // how much overlap there is, then copy it to the destination buffer.
Adrian McCarthy278a6c92015-12-09 00:29:38 +0000267 lldbassert(range.start <= addr);
268 const size_t offset = addr - range.start;
269 lldbassert(offset < range.size);
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000270 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
Adrian McCarthy0c35cde2015-12-04 22:22:15 +0000275Error
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000276ProcessWinMiniDump::Impl::GetMemoryRegionInfo(lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &info)
Adrian McCarthy0c35cde2015-12-04 22:22:15 +0000277{
278 Error error;
279 size_t size;
280 const auto list = reinterpret_cast<const MINIDUMP_MEMORY_INFO_LIST *>(FindDumpStream(MemoryInfoListStream, &size));
281 if (list == nullptr || size < sizeof(MINIDUMP_MEMORY_INFO_LIST))
282 {
283 error.SetErrorString("the mini dump contains no memory range information");
284 return error;
285 }
286
287 if (list->SizeOfEntry < sizeof(MINIDUMP_MEMORY_INFO))
288 {
289 error.SetErrorString("the entries in the mini dump memory info list are smaller than expected");
290 return error;
291 }
292
293 if (size < list->SizeOfHeader + list->SizeOfEntry * list->NumberOfEntries)
294 {
295 error.SetErrorString("the mini dump memory info list is incomplete");
296 return error;
297 }
298
299 for (int i = 0; i < list->NumberOfEntries; ++i)
300 {
301 const auto entry = reinterpret_cast<const MINIDUMP_MEMORY_INFO *>(reinterpret_cast<const char *>(list) +
302 list->SizeOfHeader + i * list->SizeOfEntry);
303 const auto head = entry->BaseAddress;
304 const auto tail = head + entry->RegionSize;
305 if (head <= load_addr && load_addr < tail)
306 {
307 info.SetReadable(IsPageReadable(entry->Protect) ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo);
308 info.SetWritable(IsPageWritable(entry->Protect) ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo);
309 info.SetExecutable(IsPageExecutable(entry->Protect) ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo);
310 return error;
311 }
312 }
313 // Note that the memory info list doesn't seem to contain ranges in kernel space,
314 // so if you're walking a stack that has kernel frames, the stack may appear
315 // truncated.
316 error.SetErrorString("address is not in a known range");
317 return error;
318}
319
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000320bool
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000321ProcessWinMiniDump::Impl::FindMemoryRange(lldb::addr_t addr, Range *range_out) const
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000322{
323 size_t stream_size = 0;
324 auto mem_list_stream = static_cast<const MINIDUMP_MEMORY_LIST *>(FindDumpStream(MemoryListStream, &stream_size));
325 if (mem_list_stream)
326 {
Adrian McCarthy0a750822016-02-25 00:23:27 +0000327 for (ULONG32 i = 0; i < mem_list_stream->NumberOfMemoryRanges; ++i)
328 {
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000329 const MINIDUMP_MEMORY_DESCRIPTOR &mem_desc = mem_list_stream->MemoryRanges[i];
330 const MINIDUMP_LOCATION_DESCRIPTOR &loc_desc = mem_desc.Memory;
331 const lldb::addr_t range_start = mem_desc.StartOfMemoryRange;
332 const size_t range_size = loc_desc.DataSize;
333 if (range_start <= addr && addr < range_start + range_size)
334 {
335 range_out->start = range_start;
336 range_out->size = range_size;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000337 range_out->ptr = reinterpret_cast<const uint8_t *>(m_base_addr) + loc_desc.Rva;
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000338 return true;
339 }
340 }
341 }
342
343 // Some mini dumps have a Memory64ListStream that captures all the heap
344 // memory. We can't exactly use the same loop as above, because the mini
345 // dump uses slightly different data structures to describe those.
346 auto mem_list64_stream = static_cast<const MINIDUMP_MEMORY64_LIST *>(FindDumpStream(Memory64ListStream, &stream_size));
347 if (mem_list64_stream)
348 {
349 size_t base_rva = mem_list64_stream->BaseRva;
350 for (ULONG32 i = 0; i < mem_list64_stream->NumberOfMemoryRanges; ++i) {
351 const MINIDUMP_MEMORY_DESCRIPTOR64 &mem_desc = mem_list64_stream->MemoryRanges[i];
352 const lldb::addr_t range_start = mem_desc.StartOfMemoryRange;
353 const size_t range_size = mem_desc.DataSize;
354 if (range_start <= addr && addr < range_start + range_size)
355 {
356 range_out->start = range_start;
357 range_out->size = range_size;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000358 range_out->ptr = reinterpret_cast<const uint8_t *>(m_base_addr) + base_rva;
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000359 return true;
360 }
361 base_rva += range_size;
362 }
363 }
364
365 return false;
366}
367
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000368Error
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000369ProcessWinMiniDump::Impl::MapMiniDumpIntoMemory()
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000370{
371 Error error;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000372 const char *file = m_core_file.GetCString();
373 m_dump_file = ::CreateFile(file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
374 if (m_dump_file == INVALID_HANDLE_VALUE)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000375 {
376 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
377 return error;
378 }
379
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000380 m_mapping = ::CreateFileMapping(m_dump_file, NULL, PAGE_READONLY, 0, 0, NULL);
381 if (m_mapping == NULL)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000382 {
383 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
384 return error;
385 }
386
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000387 m_base_addr = ::MapViewOfFile(m_mapping, FILE_MAP_READ, 0, 0, 0);
388 if (m_base_addr == nullptr)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000389 {
390 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
391 return error;
392 }
393
394 return error;
395}
396
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000397ArchSpec
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000398ProcessWinMiniDump::Impl::DetermineArchitecture()
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000399{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000400 size_t size = 0;
401 auto system_info_ptr = static_cast<const MINIDUMP_SYSTEM_INFO *>(FindDumpStream(SystemInfoStream, &size));
402 if (system_info_ptr)
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000403 {
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000404 switch (system_info_ptr->ProcessorArchitecture)
405 {
406 case PROCESSOR_ARCHITECTURE_INTEL:
407 return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_I386, LLDB_INVALID_CPUTYPE);
408 case PROCESSOR_ARCHITECTURE_AMD64:
409 return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_AMD64, LLDB_INVALID_CPUTYPE);
410 default:
411 break;
412 }
413 }
414
415 return ArchSpec(); // invalid or unknown
416}
Adrian McCarthy61ede152015-08-19 20:43:22 +0000417
418void
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000419ProcessWinMiniDump::Impl::ReadExceptionRecord()
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000420{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000421 size_t size = 0;
422 auto exception_stream_ptr = static_cast<MINIDUMP_EXCEPTION_STREAM*>(FindDumpStream(ExceptionStream, &size));
423 if (exception_stream_ptr)
424 {
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000425 m_exception_sp.reset(
426 new ExceptionRecord(exception_stream_ptr->ExceptionRecord, exception_stream_ptr->ThreadId));
Adrian McCarthy61ede152015-08-19 20:43:22 +0000427 }
Adrian McCarthy0a750822016-02-25 00:23:27 +0000428 else
429 {
430 WINLOG_IFALL(WINDOWS_LOG_PROCESS, "Minidump has no exception record.");
431 // TODO: See if we can recover the exception from the TEB.
432 }
Adrian McCarthy61ede152015-08-19 20:43:22 +0000433}
434
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000435void
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000436ProcessWinMiniDump::Impl::ReadMiscInfo()
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000437{
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.
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000446 m_self->SetID(misc_info_ptr->ProcessId);
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000447 }
448}
449
450void
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000451ProcessWinMiniDump::Impl::ReadModuleList()
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000452{
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];
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000463 const auto file_name = GetMiniDumpString(module.ModuleNameRva);
Adrian McCarthy0a750822016-02-25 00:23:27 +0000464 const auto file_spec = FileSpec(file_name, true);
465 if (FileSpec::Compare(file_spec, FileSpec("wow64.dll", false), false) == 0)
466 {
467 WINLOG_IFALL(WINDOWS_LOG_PROCESS, "Minidump is for a WOW64 process.");
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000468 m_is_wow64 = true;
Adrian McCarthy0a750822016-02-25 00:23:27 +0000469 }
470 ModuleSpec module_spec = file_spec;
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000471
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000472 lldb::ModuleSP module_sp = m_self->GetTarget().GetSharedModule(module_spec);
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000473 if (!module_sp)
474 {
475 continue;
476 }
477 bool load_addr_changed = false;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000478 module_sp->SetLoadAddress(m_self->GetTarget(), module.BaseOfImage, false, load_addr_changed);
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000479 }
480}
481
Adrian McCarthy61ede152015-08-19 20:43:22 +0000482void *
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000483ProcessWinMiniDump::Impl::FindDumpStream(unsigned stream_number, size_t *size_out) const
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000484{
Adrian McCarthy61ede152015-08-19 20:43:22 +0000485 void *stream = nullptr;
486 *size_out = 0;
487
Adrian McCarthy61ede152015-08-19 20:43:22 +0000488 MINIDUMP_DIRECTORY *dir = nullptr;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000489 if (::MiniDumpReadDumpStream(m_base_addr, stream_number, &dir, nullptr, nullptr) && dir != nullptr &&
490 dir->Location.DataSize > 0)
Adrian McCarthy61ede152015-08-19 20:43:22 +0000491 {
492 assert(dir->StreamType == stream_number);
493 *size_out = dir->Location.DataSize;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000494 stream = static_cast<void *>(static_cast<char *>(m_base_addr) + dir->Location.Rva);
Adrian McCarthy61ede152015-08-19 20:43:22 +0000495 }
496
497 return stream;
498}
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000499
500std::string
501ProcessWinMiniDump::Impl::GetMiniDumpString(RVA rva) const
502{
503 std::string result;
504 if (!m_base_addr)
505 {
506 return result;
507 }
508 auto md_string = reinterpret_cast<const MINIDUMP_STRING *>(static_cast<const char *>(m_base_addr) + rva);
509 auto source_start = reinterpret_cast<const UTF16 *>(md_string->Buffer);
510 const auto source_length = ::wcslen(md_string->Buffer);
511 const auto source_end = source_start + source_length;
512 result.resize(4 * source_length); // worst case length
513 auto result_start = reinterpret_cast<UTF8 *>(&result[0]);
514 const auto result_end = result_start + result.size();
515 ConvertUTF16toUTF8(&source_start, source_end, &result_start, result_end, strictConversion);
516 const auto result_size = std::distance(reinterpret_cast<UTF8 *>(&result[0]), result_start);
517 result.resize(result_size); // shrink to actual length
518 return result;
519}
520
521ConstString
522ProcessWinMiniDump::GetPluginNameStatic()
523{
524 static ConstString g_name("win-minidump");
525 return g_name;
526}
527
528const char *
529ProcessWinMiniDump::GetPluginDescriptionStatic()
530{
531 return "Windows minidump plug-in.";
532}
533
534void
535ProcessWinMiniDump::Terminate()
536{
537 PluginManager::UnregisterPlugin(ProcessWinMiniDump::CreateInstance);
538}
539
540lldb::ProcessSP
541ProcessWinMiniDump::CreateInstance(lldb::TargetSP target_sp, Listener &listener, const FileSpec *crash_file)
542{
543 lldb::ProcessSP process_sp;
544 if (crash_file)
545 {
546 process_sp.reset(new ProcessWinMiniDump(target_sp, listener, *crash_file));
547 }
548 return process_sp;
549}
550
551bool
552ProcessWinMiniDump::CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name)
553{
554 // TODO(amccarth): Eventually, this needs some actual logic.
555 return true;
556}
557
558ProcessWinMiniDump::ProcessWinMiniDump(lldb::TargetSP target_sp, Listener &listener, const FileSpec &core_file)
559 : ProcessWindows(target_sp, listener), m_impl_up(new Impl(core_file, this))
560{
561}
562
563ProcessWinMiniDump::~ProcessWinMiniDump()
564{
565 Clear();
566 // We need to call finalize on the process before destroying ourselves
567 // to make sure all of the broadcaster cleanup goes as planned. If we
568 // destruct this class, then Process::~Process() might have problems
569 // trying to fully destroy the broadcaster.
570 Finalize();
571}
572
573ConstString
574ProcessWinMiniDump::GetPluginName()
575{
576 return GetPluginNameStatic();
577}
578
579uint32_t
580ProcessWinMiniDump::GetPluginVersion()
581{
582 return 1;
583}
584
585Error
586ProcessWinMiniDump::DoLoadCore()
587{
588 return m_impl_up->DoLoadCore();
589}
590
591DynamicLoader *
592ProcessWinMiniDump::GetDynamicLoader()
593{
594 if (m_dyld_ap.get() == NULL)
595 m_dyld_ap.reset(DynamicLoader::FindPlugin(this, DynamicLoaderWindowsDYLD::GetPluginNameStatic().GetCString()));
596 return m_dyld_ap.get();
597}
598
599bool
600ProcessWinMiniDump::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list)
601{
602 return m_impl_up->UpdateThreadList(old_thread_list, new_thread_list);
603}
604
605void
606ProcessWinMiniDump::RefreshStateAfterStop()
607{
608 if (!m_impl_up)
609 return;
610 return m_impl_up->RefreshStateAfterStop();
611}
612
613Error
614ProcessWinMiniDump::DoDestroy()
615{
616 return Error();
617}
618
619bool
620ProcessWinMiniDump::IsAlive()
621{
622 return true;
623}
624
625bool
626ProcessWinMiniDump::WarnBeforeDetach() const
627{
628 // Since this is post-mortem debugging, there's no need to warn the user
629 // that quitting the debugger will terminate the process.
630 return false;
631}
632
633size_t
634ProcessWinMiniDump::ReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error)
635{
636 // Don't allow the caching that lldb_private::Process::ReadMemory does
637 // since we have it all cached our our dump file anyway.
638 return DoReadMemory(addr, buf, size, error);
639}
640
641size_t
642ProcessWinMiniDump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error)
643{
644 return m_impl_up->DoReadMemory(addr, buf, size, error);
645}
646
647Error
648ProcessWinMiniDump::GetMemoryRegionInfo(lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &info)
649{
650 return m_impl_up->GetMemoryRegionInfo(load_addr, info);
651}
652
653void
654ProcessWinMiniDump::Clear()
655{
656 m_thread_list.Clear();
657}
658
659void
660ProcessWinMiniDump::Initialize()
661{
662 static std::once_flag g_once_flag;
663
664 std::call_once(g_once_flag, []() {
665 PluginManager::RegisterPlugin(GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance);
666 });
667}
668
669ArchSpec
670ProcessWinMiniDump::GetArchitecture()
671{
672 // TODO
673 return ArchSpec();
674}