blob: 14ef9f02f2a401f58b9dfae2d9761eb38f1cb4ed [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
165 // WOW64.
166 // If the minidump was captured with a 64-bit debugger, then the
167 // CONTEXT
168 // we just grabbed from the mini_dump_thread is the one for the 64-bit
169 // "native" process rather than the 32-bit "guest" process we care
170 // about.
171 // In this case, we can get the 32-bit CONTEXT from the TEB (Thread
172 // Environment Block) of the 64-bit process.
173 Error error;
174 TEB64 wow64teb = {0};
175 m_self->ReadMemory(mini_dump_thread.Teb, &wow64teb, sizeof(wow64teb),
176 error);
177 if (error.Success()) {
178 // Slot 1 of the thread-local storage in the 64-bit TEB points to a
179 // structure
180 // that includes the 32-bit CONTEXT (after a ULONG).
181 // See: https://msdn.microsoft.com/en-us/library/ms681670.aspx
182 const size_t addr = wow64teb.TlsSlots[1];
183 Range range = {0};
184 if (FindMemoryRange(addr, &range)) {
185 lldbassert(range.start <= addr);
186 const size_t offset = addr - range.start + sizeof(ULONG);
187 if (offset < range.size) {
188 const size_t overlap = range.size - offset;
189 if (overlap >= sizeof(CONTEXT)) {
190 context =
191 reinterpret_cast<const CONTEXT *>(range.ptr + offset);
Adrian McCarthy0a750822016-02-25 00:23:27 +0000192 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 }
Adrian McCarthyd9fa2b52015-11-12 21:16:15 +0000194 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 }
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000196
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 // NOTE: We don't currently use the TEB for anything else. If we
198 // need it in
199 // the future, the 32-bit TEB is located according to the address
200 // stored in the
201 // first slot of the 64-bit TEB (wow64teb.Reserved1[0]).
202 }
203
204 thread_sp->SetContext(context);
205 }
206 new_thread_list.AddThread(thread_sp);
207 }
208 }
209
210 return new_thread_list.GetSize(false) > 0;
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000211}
212
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213void ProcessWinMiniDump::Impl::RefreshStateAfterStop() {
214 if (!m_exception_sp)
215 return;
Adrian McCarthy61ede152015-08-19 20:43:22 +0000216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 auto active_exception = m_exception_sp;
218 std::string desc;
219 llvm::raw_string_ostream desc_stream(desc);
220 desc_stream << "Exception "
221 << llvm::format_hex(active_exception->GetExceptionCode(), 8)
222 << " encountered at address "
223 << llvm::format_hex(active_exception->GetExceptionAddress(), 8);
224 m_self->m_thread_list.SetSelectedThreadByID(active_exception->GetThreadID());
225 auto stop_thread = m_self->m_thread_list.GetSelectedThread();
226 auto stop_info = StopInfo::CreateStopReasonWithException(
227 *stop_thread, desc_stream.str().c_str());
228 stop_thread->SetStopInfo(stop_info);
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000229}
230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231size_t ProcessWinMiniDump::Impl::DoReadMemory(lldb::addr_t addr, void *buf,
232 size_t size, Error &error) {
233 // I don't have a sense of how frequently this is called or how many memory
234 // ranges a mini dump typically has, so I'm not sure if searching for the
235 // appropriate range linearly each time is stupid. Perhaps we should build
236 // an index for faster lookups.
237 Range range = {0};
238 if (!FindMemoryRange(addr, &range)) {
239 return 0;
240 }
Adrian McCarthy6c3d03c2015-09-01 16:59:31 +0000241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 // There's at least some overlap between the beginning of the desired range
243 // (addr) and the current range. Figure out where the overlap begins and
244 // how much overlap there is, then copy it to the destination buffer.
245 lldbassert(range.start <= addr);
246 const size_t offset = addr - range.start;
247 lldbassert(offset < range.size);
248 const size_t overlap = std::min(size, range.size - offset);
249 std::memcpy(buf, range.ptr + offset, overlap);
250 return overlap;
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000251}
252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253Error ProcessWinMiniDump::Impl::GetMemoryRegionInfo(
254 lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &info) {
255 Error error;
256 size_t size;
257 info.Clear();
258 const auto list = reinterpret_cast<const MINIDUMP_MEMORY_INFO_LIST *>(
259 FindDumpStream(MemoryInfoListStream, &size));
260 if (list == nullptr || size < sizeof(MINIDUMP_MEMORY_INFO_LIST)) {
261 error.SetErrorString("the mini dump contains no memory range information");
Adrian McCarthy0c35cde2015-12-04 22:22:15 +0000262 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 }
Adrian McCarthy0c35cde2015-12-04 22:22:15 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 if (list->SizeOfEntry < sizeof(MINIDUMP_MEMORY_INFO)) {
266 error.SetErrorString("the entries in the mini dump memory info list are "
267 "smaller than expected");
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000268 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 }
270
271 if (size < list->SizeOfHeader + list->SizeOfEntry * list->NumberOfEntries) {
272 error.SetErrorString("the mini dump memory info list is incomplete");
273 return error;
274 }
275
276 const MINIDUMP_MEMORY_INFO *next_entry = nullptr;
277
278 for (int i = 0; i < list->NumberOfEntries; ++i) {
279 const auto entry = reinterpret_cast<const MINIDUMP_MEMORY_INFO *>(
280 reinterpret_cast<const char *>(list) + list->SizeOfHeader +
281 i * list->SizeOfEntry);
282 const auto head = entry->BaseAddress;
283 const auto tail = head + entry->RegionSize;
284 if (head <= load_addr && load_addr < tail) {
285 info.GetRange().SetRangeBase((entry->State != MEM_FREE) ? head
286 : load_addr);
287 info.GetRange().SetRangeEnd(tail);
288 info.SetReadable(IsPageReadable(entry->Protect) ? MemoryRegionInfo::eYes
289 : MemoryRegionInfo::eNo);
290 info.SetWritable(IsPageWritable(entry->Protect) ? MemoryRegionInfo::eYes
291 : MemoryRegionInfo::eNo);
292 info.SetExecutable(IsPageExecutable(entry->Protect)
293 ? MemoryRegionInfo::eYes
294 : MemoryRegionInfo::eNo);
295 info.SetMapped((entry->State != MEM_FREE) ? MemoryRegionInfo::eYes
296 : MemoryRegionInfo::eNo);
297 return error;
298 } else if (head > load_addr &&
299 (next_entry == nullptr || head < next_entry->BaseAddress)) {
300 // In case there is no region containing load_addr keep track of the
301 // nearest region
302 // after load_addr so we can return the distance to it.
303 next_entry = entry;
304 }
305 }
306
307 // No containing region found. Create an unmapped region that extends to the
308 // next region
309 // or LLDB_INVALID_ADDRESS
310 info.GetRange().SetRangeBase(load_addr);
311 info.GetRange().SetRangeEnd((next_entry != nullptr) ? next_entry->BaseAddress
312 : LLDB_INVALID_ADDRESS);
313 info.SetReadable(MemoryRegionInfo::eNo);
314 info.SetWritable(MemoryRegionInfo::eNo);
315 info.SetExecutable(MemoryRegionInfo::eNo);
316 info.SetMapped(MemoryRegionInfo::eNo);
317
318 // Note that the memory info list doesn't seem to contain ranges in kernel
319 // space,
320 // so if you're walking a stack that has kernel frames, the stack may appear
321 // truncated.
322 return error;
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000323}
324
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325bool ProcessWinMiniDump::Impl::FindMemoryRange(lldb::addr_t addr,
326 Range *range_out) const {
327 size_t stream_size = 0;
328 auto mem_list_stream = static_cast<const MINIDUMP_MEMORY_LIST *>(
329 FindDumpStream(MemoryListStream, &stream_size));
330 if (mem_list_stream) {
331 for (ULONG32 i = 0; i < mem_list_stream->NumberOfMemoryRanges; ++i) {
332 const MINIDUMP_MEMORY_DESCRIPTOR &mem_desc =
333 mem_list_stream->MemoryRanges[i];
334 const MINIDUMP_LOCATION_DESCRIPTOR &loc_desc = mem_desc.Memory;
335 const lldb::addr_t range_start = mem_desc.StartOfMemoryRange;
336 const size_t range_size = loc_desc.DataSize;
337 if (range_start <= addr && addr < range_start + range_size) {
338 range_out->start = range_start;
339 range_out->size = range_size;
340 range_out->ptr =
341 reinterpret_cast<const uint8_t *>(m_base_addr) + loc_desc.Rva;
342 return true;
343 }
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000344 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345 }
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000346
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 // Some mini dumps have a Memory64ListStream that captures all the heap
348 // memory. We can't exactly use the same loop as above, because the mini
349 // dump uses slightly different data structures to describe those.
350 auto mem_list64_stream = static_cast<const MINIDUMP_MEMORY64_LIST *>(
351 FindDumpStream(Memory64ListStream, &stream_size));
352 if (mem_list64_stream) {
353 size_t base_rva = mem_list64_stream->BaseRva;
354 for (ULONG32 i = 0; i < mem_list64_stream->NumberOfMemoryRanges; ++i) {
355 const MINIDUMP_MEMORY_DESCRIPTOR64 &mem_desc =
356 mem_list64_stream->MemoryRanges[i];
357 const lldb::addr_t range_start = mem_desc.StartOfMemoryRange;
358 const size_t range_size = mem_desc.DataSize;
359 if (range_start <= addr && addr < range_start + range_size) {
360 range_out->start = range_start;
361 range_out->size = range_size;
362 range_out->ptr =
363 reinterpret_cast<const uint8_t *>(m_base_addr) + base_rva;
364 return true;
365 }
366 base_rva += range_size;
367 }
368 }
369
370 return false;
Adrian McCarthyc96516f2015-08-03 23:01:51 +0000371}
Adrian McCarthy61ede152015-08-19 20:43:22 +0000372
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373Error ProcessWinMiniDump::Impl::MapMiniDumpIntoMemory() {
374 Error error;
375 const char *file = m_core_file.GetCString();
376 std::wstring wfile;
377 if (!llvm::ConvertUTF8toWide(file, wfile)) {
378 error.SetErrorString("Error converting path to UTF-16");
379 return error;
380 }
381 m_dump_file = ::CreateFileW(wfile.c_str(), GENERIC_READ, FILE_SHARE_READ,
382 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
383 if (m_dump_file == INVALID_HANDLE_VALUE) {
384 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
385 return error;
386 }
387
388 m_mapping =
389 ::CreateFileMappingW(m_dump_file, NULL, PAGE_READONLY, 0, 0, NULL);
390 if (m_mapping == NULL) {
391 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
392 return error;
393 }
394
395 m_base_addr = ::MapViewOfFile(m_mapping, FILE_MAP_READ, 0, 0, 0);
396 if (m_base_addr == nullptr) {
397 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
398 return error;
399 }
400
401 return error;
Adrian McCarthy61ede152015-08-19 20:43:22 +0000402}
403
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404ArchSpec ProcessWinMiniDump::Impl::DetermineArchitecture() {
405 size_t size = 0;
406 auto system_info_ptr = static_cast<const MINIDUMP_SYSTEM_INFO *>(
407 FindDumpStream(SystemInfoStream, &size));
408 if (system_info_ptr) {
409 switch (system_info_ptr->ProcessorArchitecture) {
410 case PROCESSOR_ARCHITECTURE_INTEL:
411 if (system_info_ptr->ProcessorLevel == 6) {
412 return ArchSpec("i686-pc-windows");
413 } else {
414 return ArchSpec("i386-pc-windows");
415 }
416 break;
417 case PROCESSOR_ARCHITECTURE_AMD64:
418 return ArchSpec("x86_64-pc-windows");
419 default:
420 break;
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000421 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422 }
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000423
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424 return ArchSpec(); // invalid or unknown
Adrian McCarthyab59a0f2015-09-17 20:52:29 +0000425}
426
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427void ProcessWinMiniDump::Impl::ReadExceptionRecord() {
428 size_t size = 0;
429 auto exception_stream_ptr = static_cast<MINIDUMP_EXCEPTION_STREAM *>(
430 FindDumpStream(ExceptionStream, &size));
431 if (exception_stream_ptr) {
432 m_exception_sp.reset(new ExceptionRecord(
433 exception_stream_ptr->ExceptionRecord, exception_stream_ptr->ThreadId));
434 } else {
435 WINLOG_IFALL(WINDOWS_LOG_PROCESS, "Minidump has no exception record.");
436 // TODO: See if we can recover the exception from the TEB.
437 }
Adrian McCarthy23d14b62015-08-28 14:42:03 +0000438}
439
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440void ProcessWinMiniDump::Impl::ReadMiscInfo() {
441 size_t size = 0;
442 const auto misc_info_ptr =
443 static_cast<MINIDUMP_MISC_INFO *>(FindDumpStream(MiscInfoStream, &size));
444 if (!misc_info_ptr || size < sizeof(MINIDUMP_MISC_INFO)) {
445 return;
446 }
Adrian McCarthy61ede152015-08-19 20:43:22 +0000447
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 if ((misc_info_ptr->Flags1 & MINIDUMP_MISC1_PROCESS_ID) != 0) {
449 // This misc info record has the process ID.
450 m_self->SetID(misc_info_ptr->ProcessId);
451 }
Adrian McCarthy61ede152015-08-19 20:43:22 +0000452}
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000453
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454void ProcessWinMiniDump::Impl::ReadModuleList() {
455 size_t size = 0;
456 auto module_list_ptr = static_cast<MINIDUMP_MODULE_LIST *>(
457 FindDumpStream(ModuleListStream, &size));
458 if (!module_list_ptr || module_list_ptr->NumberOfModules == 0) {
459 return;
460 }
461
462 for (ULONG32 i = 0; i < module_list_ptr->NumberOfModules; ++i) {
463 const auto &module = module_list_ptr->Modules[i];
464 const auto file_name = GetMiniDumpString(module.ModuleNameRva);
465 const auto file_spec = FileSpec(file_name, true);
466 if (FileSpec::Compare(file_spec, FileSpec("wow64.dll", false), false) ==
467 0) {
468 WINLOG_IFALL(WINDOWS_LOG_PROCESS, "Minidump is for a WOW64 process.");
469 m_is_wow64 = true;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000470 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471 ModuleSpec module_spec = file_spec;
472
473 lldb::ModuleSP module_sp = m_self->GetTarget().GetSharedModule(module_spec);
474 if (!module_sp) {
475 continue;
476 }
477 bool load_addr_changed = false;
478 module_sp->SetLoadAddress(m_self->GetTarget(), module.BaseOfImage, false,
479 load_addr_changed);
480 }
481}
482
483void *ProcessWinMiniDump::Impl::FindDumpStream(unsigned stream_number,
484 size_t *size_out) const {
485 void *stream = nullptr;
486 *size_out = 0;
487
488 MINIDUMP_DIRECTORY *dir = nullptr;
489 if (::MiniDumpReadDumpStream(m_base_addr, stream_number, &dir, nullptr,
490 nullptr) &&
491 dir != nullptr && dir->Location.DataSize > 0) {
492 assert(dir->StreamType == stream_number);
493 *size_out = dir->Location.DataSize;
494 stream = static_cast<void *>(static_cast<char *>(m_base_addr) +
495 dir->Location.Rva);
496 }
497
498 return stream;
499}
500
501std::string ProcessWinMiniDump::Impl::GetMiniDumpString(RVA rva) const {
502 std::string result;
503 if (!m_base_addr) {
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000504 return result;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505 }
506 auto md_string = reinterpret_cast<const MINIDUMP_STRING *>(
507 static_cast<const char *>(m_base_addr) + rva);
Adrian McCarthy59082492016-09-30 16:11:42 +0000508 auto source_start = reinterpret_cast<const llvm::UTF16 *>(md_string->Buffer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509 const auto source_length = ::wcslen(md_string->Buffer);
510 const auto source_end = source_start + source_length;
511 result.resize(UNI_MAX_UTF8_BYTES_PER_CODE_POINT *
512 source_length); // worst case length
Adrian McCarthy59082492016-09-30 16:11:42 +0000513 auto result_start = reinterpret_cast<llvm::UTF8 *>(&result[0]);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 const auto result_end = result_start + result.size();
Adrian McCarthy59082492016-09-30 16:11:42 +0000515 llvm::ConvertUTF16toUTF8(&source_start, source_end, &result_start, result_end,
516 llvm::ConversionFlags::strictConversion);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 const auto result_size =
Adrian McCarthy59082492016-09-30 16:11:42 +0000518 std::distance(reinterpret_cast<llvm::UTF8 *>(&result[0]), result_start);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519 result.resize(result_size); // shrink to actual length
520 return result;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000521}
522
Kate Stoneb9c1b512016-09-06 20:57:50 +0000523ConstString ProcessWinMiniDump::GetPluginNameStatic() {
524 static ConstString g_name("win-minidump");
525 return g_name;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000526}
527
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528const char *ProcessWinMiniDump::GetPluginDescriptionStatic() {
529 return "Windows minidump plug-in.";
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000530}
531
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532void ProcessWinMiniDump::Terminate() {
533 PluginManager::UnregisterPlugin(ProcessWinMiniDump::CreateInstance);
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000534}
535
Kate Stoneb9c1b512016-09-06 20:57:50 +0000536lldb::ProcessSP ProcessWinMiniDump::CreateInstance(lldb::TargetSP target_sp,
537 lldb::ListenerSP listener_sp,
538 const FileSpec *crash_file) {
539 lldb::ProcessSP process_sp;
540 if (crash_file) {
541 process_sp.reset(
542 new ProcessWinMiniDump(target_sp, listener_sp, *crash_file));
543 }
544 return process_sp;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000545}
546
Kate Stoneb9c1b512016-09-06 20:57:50 +0000547bool ProcessWinMiniDump::CanDebug(lldb::TargetSP target_sp,
548 bool plugin_specified_by_name) {
549 // TODO(amccarth): Eventually, this needs some actual logic.
550 return true;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000551}
552
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553ProcessWinMiniDump::ProcessWinMiniDump(lldb::TargetSP target_sp,
554 lldb::ListenerSP listener_sp,
555 const FileSpec &core_file)
556 : ProcessWindows(target_sp, listener_sp),
557 m_impl_up(new Impl(core_file, this)) {}
558
559ProcessWinMiniDump::~ProcessWinMiniDump() {
560 Clear();
561 // We need to call finalize on the process before destroying ourselves
562 // to make sure all of the broadcaster cleanup goes as planned. If we
563 // destruct this class, then Process::~Process() might have problems
564 // trying to fully destroy the broadcaster.
565 Finalize();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000566}
567
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568ConstString ProcessWinMiniDump::GetPluginName() {
569 return GetPluginNameStatic();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000570}
571
Kate Stoneb9c1b512016-09-06 20:57:50 +0000572uint32_t ProcessWinMiniDump::GetPluginVersion() { return 1; }
573
574Error ProcessWinMiniDump::DoLoadCore() { return m_impl_up->DoLoadCore(); }
575
576DynamicLoader *ProcessWinMiniDump::GetDynamicLoader() {
577 if (m_dyld_ap.get() == NULL)
578 m_dyld_ap.reset(DynamicLoader::FindPlugin(
579 this, DynamicLoaderWindowsDYLD::GetPluginNameStatic().GetCString()));
580 return m_dyld_ap.get();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000581}
582
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583bool ProcessWinMiniDump::UpdateThreadList(ThreadList &old_thread_list,
584 ThreadList &new_thread_list) {
585 return m_impl_up->UpdateThreadList(old_thread_list, new_thread_list);
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000586}
587
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588void ProcessWinMiniDump::RefreshStateAfterStop() {
589 if (!m_impl_up)
590 return;
591 return m_impl_up->RefreshStateAfterStop();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000592}
593
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594Error ProcessWinMiniDump::DoDestroy() { return Error(); }
595
596bool ProcessWinMiniDump::IsAlive() { return true; }
597
598bool ProcessWinMiniDump::WarnBeforeDetach() const {
599 // Since this is post-mortem debugging, there's no need to warn the user
600 // that quitting the debugger will terminate the process.
601 return false;
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000602}
603
Kate Stoneb9c1b512016-09-06 20:57:50 +0000604size_t ProcessWinMiniDump::ReadMemory(lldb::addr_t addr, void *buf, size_t size,
605 Error &error) {
606 // Don't allow the caching that lldb_private::Process::ReadMemory does
607 // since we have it all cached our our dump file anyway.
608 return DoReadMemory(addr, buf, size, error);
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000609}
610
Kate Stoneb9c1b512016-09-06 20:57:50 +0000611size_t ProcessWinMiniDump::DoReadMemory(lldb::addr_t addr, void *buf,
612 size_t size, Error &error) {
613 return m_impl_up->DoReadMemory(addr, buf, size, error);
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000614}
615
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616Error ProcessWinMiniDump::GetMemoryRegionInfo(
617 lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &info) {
618 return m_impl_up->GetMemoryRegionInfo(load_addr, info);
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000619}
620
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621void ProcessWinMiniDump::Clear() { m_thread_list.Clear(); }
622
623void ProcessWinMiniDump::Initialize() {
624 static std::once_flag g_once_flag;
625
626 std::call_once(g_once_flag, []() {
627 PluginManager::RegisterPlugin(GetPluginNameStatic(),
628 GetPluginDescriptionStatic(), CreateInstance);
629 });
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000630}
631
Kate Stoneb9c1b512016-09-06 20:57:50 +0000632ArchSpec ProcessWinMiniDump::GetArchitecture() {
633 // TODO
634 return ArchSpec();
Adrian McCarthya7ad58b2016-02-29 21:15:23 +0000635}