blob: 128c4c16c36d4dd34c2b33d99d46d0d28140527a [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>
Adrian McCarthyd9fa2b52015-11-12 21:16:15 +000016#include <memory>
Adrian McCarthyc96516f2015-08-03 23:01:51 +000017#include <mutex>
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include <stdlib.h>
Adrian McCarthyc96516f2015-08-03 23:01:51 +000019
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.
Kate Stoneb9c1b512016-09-06 20:57:50 +000050class ProcessWinMiniDump::Impl {
Adrian McCarthyc96516f2015-08-03 23:01:51 +000051public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 Impl(const FileSpec &core_file, ProcessWinMiniDump *self);
53 ~Impl();
Adrian McCarthyc96516f2015-08-03 23:01:51 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 Error DoLoadCore();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 bool UpdateThreadList(ThreadList &old_thread_list,
58 ThreadList &new_thread_list);
Adrian McCarthya7ad58b2016-02-29 21:15:23 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 void RefreshStateAfterStop();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error);
Adrian McCarthya7ad58b2016-02-29 21:15:23 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 Error GetMemoryRegionInfo(lldb::addr_t load_addr,
65 lldb_private::MemoryRegionInfo &info);
Adrian McCarthya7ad58b2016-02-29 21:15:23 +000066
67private:
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 // Describes a range of memory captured in the mini dump.
69 struct Range {
70 lldb::addr_t start; // virtual address of the beginning of the range
71 size_t size; // size of the range in bytes
72 const uint8_t *ptr; // absolute pointer to the first byte of the range
73 };
Adrian McCarthya7ad58b2016-02-29 21:15:23 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 // If the mini dump has a memory range that contains the desired address, it
76 // returns true with the details of the range in *range_out. Otherwise, it
77 // returns false.
78 bool FindMemoryRange(lldb::addr_t addr, Range *range_out) const;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +000079
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 lldb_private::Error MapMiniDumpIntoMemory();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 lldb_private::ArchSpec DetermineArchitecture();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 void ReadExceptionRecord();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 void ReadMiscInfo();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 void ReadModuleList();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 // A thin wrapper around WinAPI's MiniDumpReadDumpStream to avoid redundant
91 // checks. If there's a failure (e.g., if the requested stream doesn't
92 // exist),
93 // the function returns nullptr and sets *size_out to 0.
94 void *FindDumpStream(unsigned stream_number, size_t *size_out) const;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 // Getting a string out of a mini dump is a chore. You're usually given a
97 // relative virtual address (RVA), which points to a counted string that's in
98 // Windows Unicode (UTF-16). This wrapper handles all the redirection and
99 // returns a UTF-8 copy of the string.
100 std::string GetMiniDumpString(RVA rva) const;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 ProcessWinMiniDump *m_self; // non-owning back pointer
103 FileSpec m_core_file;
104 HANDLE m_dump_file; // handle to the open minidump file
105 HANDLE m_mapping; // handle to the file mapping for the minidump file
106 void *m_base_addr; // base memory address of the minidump
107 std::shared_ptr<ExceptionRecord> m_exception_sp;
108 bool m_is_wow64; // minidump is of a 32-bit process captured with a 64-bit
109 // debugger
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000110};
111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112ProcessWinMiniDump::Impl::Impl(const FileSpec &core_file,
113 ProcessWinMiniDump *self)
114 : m_self(self), m_core_file(core_file), m_dump_file(INVALID_HANDLE_VALUE),
115 m_mapping(NULL), m_base_addr(nullptr), m_exception_sp(),
116 m_is_wow64(false) {}
117
118ProcessWinMiniDump::Impl::~Impl() {
119 if (m_base_addr) {
120 ::UnmapViewOfFile(m_base_addr);
121 m_base_addr = nullptr;
122 }
123 if (m_mapping) {
124 ::CloseHandle(m_mapping);
125 m_mapping = NULL;
126 }
127 if (m_dump_file != INVALID_HANDLE_VALUE) {
128 ::CloseHandle(m_dump_file);
129 m_dump_file = INVALID_HANDLE_VALUE;
130 }
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000131}
132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133Error ProcessWinMiniDump::Impl::DoLoadCore() {
134 Error error = MapMiniDumpIntoMemory();
135 if (error.Fail()) {
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000136 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 }
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 m_self->GetTarget().SetArchitecture(DetermineArchitecture());
140 ReadMiscInfo(); // notably for process ID
141 ReadModuleList();
142 ReadExceptionRecord();
143
144 return error;
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000145}
146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147bool ProcessWinMiniDump::Impl::UpdateThreadList(ThreadList &old_thread_list,
148 ThreadList &new_thread_list) {
149 size_t size = 0;
150 auto thread_list_ptr = static_cast<const MINIDUMP_THREAD_LIST *>(
151 FindDumpStream(ThreadListStream, &size));
152 if (thread_list_ptr) {
153 const ULONG32 thread_count = thread_list_ptr->NumberOfThreads;
154 for (ULONG32 i = 0; i < thread_count; ++i) {
155 const auto &mini_dump_thread = thread_list_ptr->Threads[i];
156 auto thread_sp = std::make_shared<ThreadWinMiniDump>(
157 *m_self, mini_dump_thread.ThreadId);
158 if (mini_dump_thread.ThreadContext.DataSize >= sizeof(CONTEXT)) {
159 const CONTEXT *context = reinterpret_cast<const CONTEXT *>(
160 static_cast<const char *>(m_base_addr) +
161 mini_dump_thread.ThreadContext.Rva);
Adrian McCarthy0a750822016-02-25 00:23:27 +0000162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 if (m_is_wow64) {
164 // On Windows, a 32-bit process can run on a 64-bit machine under
Zachary Turner5a8ad4592016-10-05 17:07:34 +0000165 // WOW64. If the minidump was captured with a 64-bit debugger, then
166 // the CONTEXT we just grabbed from the mini_dump_thread is the one
167 // for the 64-bit "native" process rather than the 32-bit "guest"
168 // process we care about. In this case, we can get the 32-bit CONTEXT
169 // from the TEB (Thread Environment Block) of the 64-bit process.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 Error error;
Zachary Turner5a8ad4592016-10-05 17:07:34 +0000171 TEB64 wow64teb = {};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172 m_self->ReadMemory(mini_dump_thread.Teb, &wow64teb, sizeof(wow64teb),
173 error);
174 if (error.Success()) {
175 // Slot 1 of the thread-local storage in the 64-bit TEB points to a
Zachary Turner5a8ad4592016-10-05 17:07:34 +0000176 // structure that includes the 32-bit CONTEXT (after a ULONG).
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 // See: https://msdn.microsoft.com/en-us/library/ms681670.aspx
Zachary Turner5a8ad4592016-10-05 17:07:34 +0000178 const lldb::addr_t addr = wow64teb.TlsSlots[1];
179 Range range = {};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 if (FindMemoryRange(addr, &range)) {
181 lldbassert(range.start <= addr);
182 const size_t offset = addr - range.start + sizeof(ULONG);
183 if (offset < range.size) {
184 const size_t overlap = range.size - offset;
185 if (overlap >= sizeof(CONTEXT)) {
186 context =
187 reinterpret_cast<const CONTEXT *>(range.ptr + offset);
Adrian McCarthy0a750822016-02-25 00:23:27 +0000188 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 }
Adrian McCarthyd9fa2b52015-11-12 21:16:15 +0000190 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 }
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 // NOTE: We don't currently use the TEB for anything else. If we
194 // need it in
195 // the future, the 32-bit TEB is located according to the address
196 // stored in the
197 // first slot of the 64-bit TEB (wow64teb.Reserved1[0]).
198 }
199
200 thread_sp->SetContext(context);
201 }
202 new_thread_list.AddThread(thread_sp);
203 }
204 }
205
206 return new_thread_list.GetSize(false) > 0;
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000207}
208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209void ProcessWinMiniDump::Impl::RefreshStateAfterStop() {
210 if (!m_exception_sp)
211 return;
Adrian McCarthy61ede152015-08-19 20:43:22 +0000212
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 auto active_exception = m_exception_sp;
214 std::string desc;
215 llvm::raw_string_ostream desc_stream(desc);
216 desc_stream << "Exception "
217 << llvm::format_hex(active_exception->GetExceptionCode(), 8)
218 << " encountered at address "
219 << llvm::format_hex(active_exception->GetExceptionAddress(), 8);
220 m_self->m_thread_list.SetSelectedThreadByID(active_exception->GetThreadID());
221 auto stop_thread = m_self->m_thread_list.GetSelectedThread();
222 auto stop_info = StopInfo::CreateStopReasonWithException(
223 *stop_thread, desc_stream.str().c_str());
224 stop_thread->SetStopInfo(stop_info);
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000225}
226
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227size_t ProcessWinMiniDump::Impl::DoReadMemory(lldb::addr_t addr, void *buf,
228 size_t size, Error &error) {
229 // I don't have a sense of how frequently this is called or how many memory
230 // ranges a mini dump typically has, so I'm not sure if searching for the
231 // appropriate range linearly each time is stupid. Perhaps we should build
232 // an index for faster lookups.
Zachary Turner5a8ad4592016-10-05 17:07:34 +0000233 Range range = {};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234 if (!FindMemoryRange(addr, &range)) {
235 return 0;
236 }
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000237
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 // There's at least some overlap between the beginning of the desired range
239 // (addr) and the current range. Figure out where the overlap begins and
240 // how much overlap there is, then copy it to the destination buffer.
241 lldbassert(range.start <= addr);
242 const size_t offset = addr - range.start;
243 lldbassert(offset < range.size);
244 const size_t overlap = std::min(size, range.size - offset);
245 std::memcpy(buf, range.ptr + offset, overlap);
246 return overlap;
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000247}
248
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249Error ProcessWinMiniDump::Impl::GetMemoryRegionInfo(
250 lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &info) {
251 Error error;
252 size_t size;
253 info.Clear();
254 const auto list = reinterpret_cast<const MINIDUMP_MEMORY_INFO_LIST *>(
255 FindDumpStream(MemoryInfoListStream, &size));
256 if (list == nullptr || size < sizeof(MINIDUMP_MEMORY_INFO_LIST)) {
257 error.SetErrorString("the mini dump contains no memory range information");
Adrian McCarthy0c35cde2015-12-04 22:22:15 +0000258 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 }
Adrian McCarthy0c35cde2015-12-04 22:22:15 +0000260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 if (list->SizeOfEntry < sizeof(MINIDUMP_MEMORY_INFO)) {
262 error.SetErrorString("the entries in the mini dump memory info list are "
263 "smaller than expected");
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000264 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 }
266
267 if (size < list->SizeOfHeader + list->SizeOfEntry * list->NumberOfEntries) {
268 error.SetErrorString("the mini dump memory info list is incomplete");
269 return error;
270 }
271
272 const MINIDUMP_MEMORY_INFO *next_entry = nullptr;
273
Zachary Turner5a8ad4592016-10-05 17:07:34 +0000274 for (uint64_t i = 0; i < list->NumberOfEntries; ++i) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 const auto entry = reinterpret_cast<const MINIDUMP_MEMORY_INFO *>(
276 reinterpret_cast<const char *>(list) + list->SizeOfHeader +
277 i * list->SizeOfEntry);
278 const auto head = entry->BaseAddress;
279 const auto tail = head + entry->RegionSize;
280 if (head <= load_addr && load_addr < tail) {
281 info.GetRange().SetRangeBase((entry->State != MEM_FREE) ? head
282 : load_addr);
283 info.GetRange().SetRangeEnd(tail);
284 info.SetReadable(IsPageReadable(entry->Protect) ? MemoryRegionInfo::eYes
285 : MemoryRegionInfo::eNo);
286 info.SetWritable(IsPageWritable(entry->Protect) ? MemoryRegionInfo::eYes
287 : MemoryRegionInfo::eNo);
288 info.SetExecutable(IsPageExecutable(entry->Protect)
289 ? MemoryRegionInfo::eYes
290 : MemoryRegionInfo::eNo);
291 info.SetMapped((entry->State != MEM_FREE) ? MemoryRegionInfo::eYes
292 : MemoryRegionInfo::eNo);
293 return error;
294 } else if (head > load_addr &&
295 (next_entry == nullptr || head < next_entry->BaseAddress)) {
296 // In case there is no region containing load_addr keep track of the
297 // nearest region
298 // after load_addr so we can return the distance to it.
299 next_entry = entry;
300 }
301 }
302
303 // No containing region found. Create an unmapped region that extends to the
304 // next region
305 // or LLDB_INVALID_ADDRESS
306 info.GetRange().SetRangeBase(load_addr);
307 info.GetRange().SetRangeEnd((next_entry != nullptr) ? next_entry->BaseAddress
308 : LLDB_INVALID_ADDRESS);
309 info.SetReadable(MemoryRegionInfo::eNo);
310 info.SetWritable(MemoryRegionInfo::eNo);
311 info.SetExecutable(MemoryRegionInfo::eNo);
312 info.SetMapped(MemoryRegionInfo::eNo);
313
314 // Note that the memory info list doesn't seem to contain ranges in kernel
315 // space,
316 // so if you're walking a stack that has kernel frames, the stack may appear
317 // truncated.
318 return error;
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000319}
320
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321bool ProcessWinMiniDump::Impl::FindMemoryRange(lldb::addr_t addr,
322 Range *range_out) const {
323 size_t stream_size = 0;
324 auto mem_list_stream = static_cast<const MINIDUMP_MEMORY_LIST *>(
325 FindDumpStream(MemoryListStream, &stream_size));
326 if (mem_list_stream) {
327 for (ULONG32 i = 0; i < mem_list_stream->NumberOfMemoryRanges; ++i) {
328 const MINIDUMP_MEMORY_DESCRIPTOR &mem_desc =
329 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 range_out->start = range_start;
335 range_out->size = range_size;
336 range_out->ptr =
337 reinterpret_cast<const uint8_t *>(m_base_addr) + loc_desc.Rva;
338 return true;
339 }
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000340 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 }
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000342
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343 // 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 *>(
347 FindDumpStream(Memory64ListStream, &stream_size));
348 if (mem_list64_stream) {
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 =
352 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 range_out->start = range_start;
357 range_out->size = range_size;
358 range_out->ptr =
359 reinterpret_cast<const uint8_t *>(m_base_addr) + base_rva;
360 return true;
361 }
362 base_rva += range_size;
363 }
364 }
365
366 return false;
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000367}
Adrian McCarthy61ede152015-08-19 20:43:22 +0000368
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369Error ProcessWinMiniDump::Impl::MapMiniDumpIntoMemory() {
370 Error error;
371 const char *file = m_core_file.GetCString();
372 std::wstring wfile;
373 if (!llvm::ConvertUTF8toWide(file, wfile)) {
374 error.SetErrorString("Error converting path to UTF-16");
375 return error;
376 }
377 m_dump_file = ::CreateFileW(wfile.c_str(), GENERIC_READ, FILE_SHARE_READ,
378 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
379 if (m_dump_file == INVALID_HANDLE_VALUE) {
380 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
381 return error;
382 }
383
384 m_mapping =
385 ::CreateFileMappingW(m_dump_file, NULL, PAGE_READONLY, 0, 0, NULL);
386 if (m_mapping == NULL) {
387 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
388 return error;
389 }
390
391 m_base_addr = ::MapViewOfFile(m_mapping, FILE_MAP_READ, 0, 0, 0);
392 if (m_base_addr == nullptr) {
393 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
394 return error;
395 }
396
397 return error;
Adrian McCarthy61ede152015-08-19 20:43:22 +0000398}
399
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400ArchSpec ProcessWinMiniDump::Impl::DetermineArchitecture() {
401 size_t size = 0;
402 auto system_info_ptr = static_cast<const MINIDUMP_SYSTEM_INFO *>(
403 FindDumpStream(SystemInfoStream, &size));
404 if (system_info_ptr) {
405 switch (system_info_ptr->ProcessorArchitecture) {
406 case PROCESSOR_ARCHITECTURE_INTEL:
407 if (system_info_ptr->ProcessorLevel == 6) {
408 return ArchSpec("i686-pc-windows");
409 } else {
410 return ArchSpec("i386-pc-windows");
411 }
412 break;
413 case PROCESSOR_ARCHITECTURE_AMD64:
414 return ArchSpec("x86_64-pc-windows");
415 default:
416 break;
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000417 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418 }
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 return ArchSpec(); // invalid or unknown
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000421}
422
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423void ProcessWinMiniDump::Impl::ReadExceptionRecord() {
424 size_t size = 0;
425 auto exception_stream_ptr = static_cast<MINIDUMP_EXCEPTION_STREAM *>(
426 FindDumpStream(ExceptionStream, &size));
427 if (exception_stream_ptr) {
428 m_exception_sp.reset(new ExceptionRecord(
429 exception_stream_ptr->ExceptionRecord, exception_stream_ptr->ThreadId));
430 } else {
431 WINLOG_IFALL(WINDOWS_LOG_PROCESS, "Minidump has no exception record.");
432 // TODO: See if we can recover the exception from the TEB.
433 }
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000434}
435
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436void ProcessWinMiniDump::Impl::ReadMiscInfo() {
437 size_t size = 0;
438 const auto misc_info_ptr =
439 static_cast<MINIDUMP_MISC_INFO *>(FindDumpStream(MiscInfoStream, &size));
440 if (!misc_info_ptr || size < sizeof(MINIDUMP_MISC_INFO)) {
441 return;
442 }
Adrian McCarthy61ede152015-08-19 20:43:22 +0000443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 if ((misc_info_ptr->Flags1 & MINIDUMP_MISC1_PROCESS_ID) != 0) {
445 // This misc info record has the process ID.
446 m_self->SetID(misc_info_ptr->ProcessId);
447 }
Adrian McCarthy61ede152015-08-19 20:43:22 +0000448}
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000449
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450void ProcessWinMiniDump::Impl::ReadModuleList() {
451 size_t size = 0;
452 auto module_list_ptr = static_cast<MINIDUMP_MODULE_LIST *>(
453 FindDumpStream(ModuleListStream, &size));
454 if (!module_list_ptr || module_list_ptr->NumberOfModules == 0) {
455 return;
456 }
457
458 for (ULONG32 i = 0; i < module_list_ptr->NumberOfModules; ++i) {
459 const auto &module = module_list_ptr->Modules[i];
460 const auto file_name = GetMiniDumpString(module.ModuleNameRva);
461 const auto file_spec = FileSpec(file_name, true);
462 if (FileSpec::Compare(file_spec, FileSpec("wow64.dll", false), false) ==
463 0) {
464 WINLOG_IFALL(WINDOWS_LOG_PROCESS, "Minidump is for a WOW64 process.");
465 m_is_wow64 = true;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000466 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 ModuleSpec module_spec = file_spec;
468
469 lldb::ModuleSP module_sp = m_self->GetTarget().GetSharedModule(module_spec);
470 if (!module_sp) {
471 continue;
472 }
473 bool load_addr_changed = false;
474 module_sp->SetLoadAddress(m_self->GetTarget(), module.BaseOfImage, false,
475 load_addr_changed);
476 }
477}
478
479void *ProcessWinMiniDump::Impl::FindDumpStream(unsigned stream_number,
480 size_t *size_out) const {
481 void *stream = nullptr;
482 *size_out = 0;
483
484 MINIDUMP_DIRECTORY *dir = nullptr;
485 if (::MiniDumpReadDumpStream(m_base_addr, stream_number, &dir, nullptr,
486 nullptr) &&
487 dir != nullptr && dir->Location.DataSize > 0) {
488 assert(dir->StreamType == stream_number);
489 *size_out = dir->Location.DataSize;
490 stream = static_cast<void *>(static_cast<char *>(m_base_addr) +
491 dir->Location.Rva);
492 }
493
494 return stream;
495}
496
497std::string ProcessWinMiniDump::Impl::GetMiniDumpString(RVA rva) const {
498 std::string result;
499 if (!m_base_addr) {
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000500 return result;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501 }
502 auto md_string = reinterpret_cast<const MINIDUMP_STRING *>(
503 static_cast<const char *>(m_base_addr) + rva);
Adrian McCarthy59082492016-09-30 16:11:42 +0000504 auto source_start = reinterpret_cast<const llvm::UTF16 *>(md_string->Buffer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505 const auto source_length = ::wcslen(md_string->Buffer);
506 const auto source_end = source_start + source_length;
507 result.resize(UNI_MAX_UTF8_BYTES_PER_CODE_POINT *
508 source_length); // worst case length
Adrian McCarthy59082492016-09-30 16:11:42 +0000509 auto result_start = reinterpret_cast<llvm::UTF8 *>(&result[0]);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 const auto result_end = result_start + result.size();
Adrian McCarthy59082492016-09-30 16:11:42 +0000511 llvm::ConvertUTF16toUTF8(&source_start, source_end, &result_start, result_end,
512 llvm::ConversionFlags::strictConversion);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 const auto result_size =
Adrian McCarthy59082492016-09-30 16:11:42 +0000514 std::distance(reinterpret_cast<llvm::UTF8 *>(&result[0]), result_start);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515 result.resize(result_size); // shrink to actual length
516 return result;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000517}
518
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519ConstString ProcessWinMiniDump::GetPluginNameStatic() {
520 static ConstString g_name("win-minidump");
521 return g_name;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000522}
523
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524const char *ProcessWinMiniDump::GetPluginDescriptionStatic() {
525 return "Windows minidump plug-in.";
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000526}
527
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528void ProcessWinMiniDump::Terminate() {
529 PluginManager::UnregisterPlugin(ProcessWinMiniDump::CreateInstance);
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000530}
531
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532lldb::ProcessSP ProcessWinMiniDump::CreateInstance(lldb::TargetSP target_sp,
533 lldb::ListenerSP listener_sp,
534 const FileSpec *crash_file) {
535 lldb::ProcessSP process_sp;
536 if (crash_file) {
537 process_sp.reset(
538 new ProcessWinMiniDump(target_sp, listener_sp, *crash_file));
539 }
540 return process_sp;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000541}
542
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543bool ProcessWinMiniDump::CanDebug(lldb::TargetSP target_sp,
544 bool plugin_specified_by_name) {
545 // TODO(amccarth): Eventually, this needs some actual logic.
546 return true;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000547}
548
Kate Stoneb9c1b512016-09-06 20:57:50 +0000549ProcessWinMiniDump::ProcessWinMiniDump(lldb::TargetSP target_sp,
550 lldb::ListenerSP listener_sp,
551 const FileSpec &core_file)
552 : ProcessWindows(target_sp, listener_sp),
553 m_impl_up(new Impl(core_file, this)) {}
554
555ProcessWinMiniDump::~ProcessWinMiniDump() {
556 Clear();
557 // We need to call finalize on the process before destroying ourselves
558 // to make sure all of the broadcaster cleanup goes as planned. If we
559 // destruct this class, then Process::~Process() might have problems
560 // trying to fully destroy the broadcaster.
561 Finalize();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000562}
563
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564ConstString ProcessWinMiniDump::GetPluginName() {
565 return GetPluginNameStatic();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000566}
567
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568uint32_t ProcessWinMiniDump::GetPluginVersion() { return 1; }
569
570Error ProcessWinMiniDump::DoLoadCore() { return m_impl_up->DoLoadCore(); }
571
572DynamicLoader *ProcessWinMiniDump::GetDynamicLoader() {
573 if (m_dyld_ap.get() == NULL)
574 m_dyld_ap.reset(DynamicLoader::FindPlugin(
575 this, DynamicLoaderWindowsDYLD::GetPluginNameStatic().GetCString()));
576 return m_dyld_ap.get();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000577}
578
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579bool ProcessWinMiniDump::UpdateThreadList(ThreadList &old_thread_list,
580 ThreadList &new_thread_list) {
581 return m_impl_up->UpdateThreadList(old_thread_list, new_thread_list);
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000582}
583
Kate Stoneb9c1b512016-09-06 20:57:50 +0000584void ProcessWinMiniDump::RefreshStateAfterStop() {
585 if (!m_impl_up)
586 return;
587 return m_impl_up->RefreshStateAfterStop();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000588}
589
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590Error ProcessWinMiniDump::DoDestroy() { return Error(); }
591
592bool ProcessWinMiniDump::IsAlive() { return true; }
593
594bool ProcessWinMiniDump::WarnBeforeDetach() const {
595 // Since this is post-mortem debugging, there's no need to warn the user
596 // that quitting the debugger will terminate the process.
597 return false;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000598}
599
Kate Stoneb9c1b512016-09-06 20:57:50 +0000600size_t ProcessWinMiniDump::ReadMemory(lldb::addr_t addr, void *buf, size_t size,
601 Error &error) {
602 // Don't allow the caching that lldb_private::Process::ReadMemory does
603 // since we have it all cached our our dump file anyway.
604 return DoReadMemory(addr, buf, size, error);
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000605}
606
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607size_t ProcessWinMiniDump::DoReadMemory(lldb::addr_t addr, void *buf,
608 size_t size, Error &error) {
609 return m_impl_up->DoReadMemory(addr, buf, size, error);
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000610}
611
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612Error ProcessWinMiniDump::GetMemoryRegionInfo(
613 lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &info) {
614 return m_impl_up->GetMemoryRegionInfo(load_addr, info);
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000615}
616
Kate Stoneb9c1b512016-09-06 20:57:50 +0000617void ProcessWinMiniDump::Clear() { m_thread_list.Clear(); }
618
619void ProcessWinMiniDump::Initialize() {
620 static std::once_flag g_once_flag;
621
622 std::call_once(g_once_flag, []() {
623 PluginManager::RegisterPlugin(GetPluginNameStatic(),
624 GetPluginDescriptionStatic(), CreateInstance);
625 });
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000626}
627
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628ArchSpec ProcessWinMiniDump::GetArchitecture() {
629 // TODO
630 return ArchSpec();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000631}