1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "base/basictypes.h"
10 #include "base/command_line.h"
11 #include "base/file_util.h"
12 #include "base/file_version_info.h"
13 #include "base/files/file_path.h"
14 #include "base/logging.h"
15 #include "base/win/scoped_handle.h"
16 #include "gtest/gtest.h"
20 // Convenience to get to the PEB pointer in a TEB.
26 // Minidump with stacks, PEB, TEB, and unloaded module list.
27 const MINIDUMP_TYPE kSmallDumpType = static_cast<MINIDUMP_TYPE>(
28 MiniDumpWithProcessThreadData | // Get PEB and TEB.
29 MiniDumpWithUnloadedModules); // Get unloaded modules when available.
31 // Minidump with all of the above, plus memory referenced from stack.
32 const MINIDUMP_TYPE kLargerDumpType = static_cast<MINIDUMP_TYPE>(
33 MiniDumpWithProcessThreadData | // Get PEB and TEB.
34 MiniDumpWithUnloadedModules | // Get unloaded modules when available.
35 MiniDumpWithIndirectlyReferencedMemory); // Get memory referenced by stack.
37 // Large dump with all process memory.
38 const MINIDUMP_TYPE kFullDumpType = static_cast<MINIDUMP_TYPE>(
39 MiniDumpWithFullMemory | // Full memory from process.
40 MiniDumpWithProcessThreadData | // Get PEB and TEB.
41 MiniDumpWithHandleData | // Get all handle information.
42 MiniDumpWithUnloadedModules); // Get unloaded modules when available.
45 class MinidumpTest: public testing::Test {
47 MinidumpTest() : dump_file_view_(NULL) {
50 virtual void SetUp() {
51 // Make sure URLMon isn't loaded into our process.
52 ASSERT_EQ(NULL, ::GetModuleHandle(L"urlmon.dll"));
54 // Then load and unload it to ensure we have something to
55 // stock the unloaded module list with.
56 HMODULE urlmon = ::LoadLibrary(L"urlmon.dll");
57 ASSERT_TRUE(urlmon != NULL);
58 ASSERT_TRUE(::FreeLibrary(urlmon));
60 ASSERT_TRUE(file_util::CreateTemporaryFile(&dump_file_));
61 dump_file_handle_.Set(::CreateFile(dump_file_.value().c_str(),
62 GENERIC_WRITE | GENERIC_READ,
68 ASSERT_TRUE(dump_file_handle_.IsValid());
71 virtual void TearDown() {
72 if (dump_file_view_ != NULL) {
73 EXPECT_TRUE(::UnmapViewOfFile(dump_file_view_));
74 dump_file_mapping_.Close();
77 dump_file_handle_.Close();
78 EXPECT_TRUE(base::DeleteFile(dump_file_, false));
81 void EnsureDumpMapped() {
82 ASSERT_TRUE(dump_file_handle_.IsValid());
83 if (dump_file_view_ == NULL) {
84 ASSERT_FALSE(dump_file_mapping_.IsValid());
86 dump_file_mapping_.Set(::CreateFileMapping(dump_file_handle_,
92 ASSERT_TRUE(dump_file_mapping_.IsValid());
94 dump_file_view_ = ::MapViewOfFile(dump_file_mapping_.Get(),
99 ASSERT_TRUE(dump_file_view_ != NULL);
103 bool WriteDump(ULONG flags) {
104 // Fake exception is access violation on write to this.
105 EXCEPTION_RECORD ex_record = {
106 STATUS_ACCESS_VIOLATION, // ExceptionCode
108 NULL, // ExceptionRecord;
109 reinterpret_cast<void*>(0xCAFEBABE), // ExceptionAddress;
110 2, // NumberParameters;
111 { EXCEPTION_WRITE_FAULT, reinterpret_cast<ULONG_PTR>(this) }
113 CONTEXT ctx_record = {};
114 EXCEPTION_POINTERS ex_ptrs = {
118 MINIDUMP_EXCEPTION_INFORMATION ex_info = {
119 ::GetCurrentThreadId(),
124 // Capture our register context.
125 ::RtlCaptureContext(&ctx_record);
128 BOOL result = ::MiniDumpWriteDump(::GetCurrentProcess(),
129 ::GetCurrentProcessId(),
130 dump_file_handle_.Get(),
131 static_cast<MINIDUMP_TYPE>(flags),
135 VLOG(1) << "Flags: " << flags << " mindump size: "
136 << ::GetFileSize(dump_file_handle_.Get(), NULL);
138 return result == TRUE;
141 bool DumpHasStream(ULONG stream_number) {
144 MINIDUMP_DIRECTORY* directory = NULL;
146 ULONG stream_size = 0;
147 BOOL ret = ::MiniDumpReadDumpStream(dump_file_view_,
153 return ret != FALSE && stream != NULL && stream_size > 0;
156 template <class StreamType>
157 size_t GetStream(ULONG stream_number, StreamType** stream) {
159 MINIDUMP_DIRECTORY* directory = NULL;
160 ULONG memory_list_size = 0;
161 BOOL ret = ::MiniDumpReadDumpStream(dump_file_view_,
164 reinterpret_cast<void**>(stream),
167 return ret ? memory_list_size : 0;
171 MINIDUMP_THREAD_LIST* thread_list = NULL;
172 size_t thread_list_size = GetStream(ThreadListStream, &thread_list);
174 if (thread_list_size > 0 && thread_list != NULL) {
175 for (ULONG i = 0; i < thread_list->NumberOfThreads; ++i) {
176 if (!DumpHasMemory(thread_list->Threads[i].Teb))
183 // No thread list, no TEB info.
188 MINIDUMP_THREAD_LIST* thread_list = NULL;
189 size_t thread_list_size = GetStream(ThreadListStream, &thread_list);
191 if (thread_list_size > 0 && thread_list != NULL &&
192 thread_list->NumberOfThreads > 0) {
194 if (!DumpHasMemory(thread_list->Threads[0].Teb, &teb))
197 return DumpHasMemory(teb->peb);
203 bool DumpHasMemory(ULONG64 address) {
204 return DumpHasMemory<uint8>(address, NULL);
207 bool DumpHasMemory(const void* address) {
208 return DumpHasMemory<uint8>(address, NULL);
211 template <class StructureType>
212 bool DumpHasMemory(ULONG64 address, StructureType** structure = NULL) {
213 // We can't cope with 64 bit addresses for now.
214 if (address > 0xFFFFFFFFUL)
217 return DumpHasMemory(reinterpret_cast<void*>(address), structure);
220 template <class StructureType>
221 bool DumpHasMemory(const void* addr_in, StructureType** structure = NULL) {
222 uintptr_t address = reinterpret_cast<uintptr_t>(addr_in);
223 MINIDUMP_MEMORY_LIST* memory_list = NULL;
224 size_t memory_list_size = GetStream(MemoryListStream, &memory_list);
225 if (memory_list_size > 0 && memory_list != NULL) {
226 for (ULONG i = 0; i < memory_list->NumberOfMemoryRanges; ++i) {
227 MINIDUMP_MEMORY_DESCRIPTOR& descr = memory_list->MemoryRanges[i];
228 const uintptr_t range_start =
229 static_cast<uintptr_t>(descr.StartOfMemoryRange);
230 uintptr_t range_end = range_start + descr.Memory.DataSize;
232 if (address >= range_start &&
233 address + sizeof(StructureType) < range_end) {
234 // The start address falls in the range, and the end address is
235 // in bounds, return a pointer to the structure if requested.
236 if (structure != NULL)
237 *structure = reinterpret_cast<StructureType*>(
238 RVA_TO_ADDR(dump_file_view_, descr.Memory.Rva));
245 // We didn't find the range in a MINIDUMP_MEMORY_LIST, so maybe this
246 // is a full dump using MINIDUMP_MEMORY64_LIST with all the memory at the
247 // end of the dump file.
248 MINIDUMP_MEMORY64_LIST* memory64_list = NULL;
249 memory_list_size = GetStream(Memory64ListStream, &memory64_list);
250 if (memory_list_size > 0 && memory64_list != NULL) {
251 // Keep track of where the current descriptor maps to.
252 RVA64 curr_rva = memory64_list->BaseRva;
253 for (ULONG i = 0; i < memory64_list->NumberOfMemoryRanges; ++i) {
254 MINIDUMP_MEMORY_DESCRIPTOR64& descr = memory64_list->MemoryRanges[i];
255 uintptr_t range_start =
256 static_cast<uintptr_t>(descr.StartOfMemoryRange);
257 uintptr_t range_end = range_start + static_cast<size_t>(descr.DataSize);
259 if (address >= range_start &&
260 address + sizeof(StructureType) < range_end) {
261 // The start address falls in the range, and the end address is
262 // in bounds, return a pointer to the structure if requested.
263 if (structure != NULL)
264 *structure = reinterpret_cast<StructureType*>(
265 RVA_TO_ADDR(dump_file_view_, curr_rva));
270 // Advance the current RVA.
271 curr_rva += descr.DataSize;
281 base::win::ScopedHandle dump_file_handle_;
282 base::win::ScopedHandle dump_file_mapping_;
283 void* dump_file_view_;
285 base::FilePath dump_file_;
288 TEST_F(MinidumpTest, Version) {
289 API_VERSION* version = ::ImagehlpApiVersion();
291 VLOG(1) << "Imagehlp Api Version: " << version->MajorVersion << "."
292 << version->MinorVersion << "." << version->Revision;
294 HMODULE dbg_help = ::GetModuleHandle(L"dbghelp.dll");
295 ASSERT_TRUE(dbg_help != NULL);
297 wchar_t dbg_help_file[1024] = {};
298 ASSERT_TRUE(::GetModuleFileName(dbg_help,
300 arraysize(dbg_help_file)));
301 scoped_ptr<FileVersionInfo> file_info(
302 FileVersionInfo::CreateFileVersionInfo(base::FilePath(dbg_help_file)));
303 ASSERT_TRUE(file_info != NULL);
305 VLOG(1) << "DbgHelp.dll version: " << file_info->file_version();
308 TEST_F(MinidumpTest, Normal) {
309 EXPECT_TRUE(WriteDump(MiniDumpNormal));
311 // We expect threads, modules and some memory.
312 EXPECT_TRUE(DumpHasStream(ThreadListStream));
313 EXPECT_TRUE(DumpHasStream(ModuleListStream));
314 EXPECT_TRUE(DumpHasStream(MemoryListStream));
315 EXPECT_TRUE(DumpHasStream(ExceptionStream));
316 EXPECT_TRUE(DumpHasStream(SystemInfoStream));
317 EXPECT_TRUE(DumpHasStream(MiscInfoStream));
319 EXPECT_FALSE(DumpHasStream(ThreadExListStream));
320 EXPECT_FALSE(DumpHasStream(Memory64ListStream));
321 EXPECT_FALSE(DumpHasStream(CommentStreamA));
322 EXPECT_FALSE(DumpHasStream(CommentStreamW));
323 EXPECT_FALSE(DumpHasStream(HandleDataStream));
324 EXPECT_FALSE(DumpHasStream(FunctionTableStream));
325 EXPECT_FALSE(DumpHasStream(UnloadedModuleListStream));
326 EXPECT_FALSE(DumpHasStream(MemoryInfoListStream));
327 EXPECT_FALSE(DumpHasStream(ThreadInfoListStream));
328 EXPECT_FALSE(DumpHasStream(HandleOperationListStream));
329 EXPECT_FALSE(DumpHasStream(TokenStream));
331 // We expect no PEB nor TEBs in this dump.
332 EXPECT_FALSE(DumpHasTebs());
333 EXPECT_FALSE(DumpHasPeb());
335 // We expect no off-stack memory in this dump.
336 EXPECT_FALSE(DumpHasMemory(this));
339 TEST_F(MinidumpTest, SmallDump) {
340 ASSERT_TRUE(WriteDump(kSmallDumpType));
342 EXPECT_TRUE(DumpHasStream(ThreadListStream));
343 EXPECT_TRUE(DumpHasStream(ModuleListStream));
344 EXPECT_TRUE(DumpHasStream(MemoryListStream));
345 EXPECT_TRUE(DumpHasStream(ExceptionStream));
346 EXPECT_TRUE(DumpHasStream(SystemInfoStream));
347 EXPECT_TRUE(DumpHasStream(UnloadedModuleListStream));
348 EXPECT_TRUE(DumpHasStream(MiscInfoStream));
350 // We expect PEB and TEBs in this dump.
351 EXPECT_TRUE(DumpHasTebs());
352 EXPECT_TRUE(DumpHasPeb());
354 EXPECT_FALSE(DumpHasStream(ThreadExListStream));
355 EXPECT_FALSE(DumpHasStream(Memory64ListStream));
356 EXPECT_FALSE(DumpHasStream(CommentStreamA));
357 EXPECT_FALSE(DumpHasStream(CommentStreamW));
358 EXPECT_FALSE(DumpHasStream(HandleDataStream));
359 EXPECT_FALSE(DumpHasStream(FunctionTableStream));
360 EXPECT_FALSE(DumpHasStream(MemoryInfoListStream));
361 EXPECT_FALSE(DumpHasStream(ThreadInfoListStream));
362 EXPECT_FALSE(DumpHasStream(HandleOperationListStream));
363 EXPECT_FALSE(DumpHasStream(TokenStream));
365 // We expect no off-stack memory in this dump.
366 EXPECT_FALSE(DumpHasMemory(this));
369 TEST_F(MinidumpTest, LargerDump) {
370 ASSERT_TRUE(WriteDump(kLargerDumpType));
372 // The dump should have all of these streams.
373 EXPECT_TRUE(DumpHasStream(ThreadListStream));
374 EXPECT_TRUE(DumpHasStream(ModuleListStream));
375 EXPECT_TRUE(DumpHasStream(MemoryListStream));
376 EXPECT_TRUE(DumpHasStream(ExceptionStream));
377 EXPECT_TRUE(DumpHasStream(SystemInfoStream));
378 EXPECT_TRUE(DumpHasStream(UnloadedModuleListStream));
379 EXPECT_TRUE(DumpHasStream(MiscInfoStream));
381 // We expect memory referenced by stack in this dump.
382 EXPECT_TRUE(DumpHasMemory(this));
384 // We expect PEB and TEBs in this dump.
385 EXPECT_TRUE(DumpHasTebs());
386 EXPECT_TRUE(DumpHasPeb());
388 EXPECT_FALSE(DumpHasStream(ThreadExListStream));
389 EXPECT_FALSE(DumpHasStream(Memory64ListStream));
390 EXPECT_FALSE(DumpHasStream(CommentStreamA));
391 EXPECT_FALSE(DumpHasStream(CommentStreamW));
392 EXPECT_FALSE(DumpHasStream(HandleDataStream));
393 EXPECT_FALSE(DumpHasStream(FunctionTableStream));
394 EXPECT_FALSE(DumpHasStream(MemoryInfoListStream));
395 EXPECT_FALSE(DumpHasStream(ThreadInfoListStream));
396 EXPECT_FALSE(DumpHasStream(HandleOperationListStream));
397 EXPECT_FALSE(DumpHasStream(TokenStream));
400 TEST_F(MinidumpTest, FullDump) {
401 ASSERT_TRUE(WriteDump(kFullDumpType));
403 // The dump should have all of these streams.
404 EXPECT_TRUE(DumpHasStream(ThreadListStream));
405 EXPECT_TRUE(DumpHasStream(ModuleListStream));
406 EXPECT_TRUE(DumpHasStream(Memory64ListStream));
407 EXPECT_TRUE(DumpHasStream(ExceptionStream));
408 EXPECT_TRUE(DumpHasStream(SystemInfoStream));
409 EXPECT_TRUE(DumpHasStream(UnloadedModuleListStream));
410 EXPECT_TRUE(DumpHasStream(MiscInfoStream));
411 EXPECT_TRUE(DumpHasStream(HandleDataStream));
413 // We expect memory referenced by stack in this dump.
414 EXPECT_TRUE(DumpHasMemory(this));
416 // We expect PEB and TEBs in this dump.
417 EXPECT_TRUE(DumpHasTebs());
418 EXPECT_TRUE(DumpHasPeb());
420 EXPECT_FALSE(DumpHasStream(ThreadExListStream));
421 EXPECT_FALSE(DumpHasStream(MemoryListStream));
422 EXPECT_FALSE(DumpHasStream(CommentStreamA));
423 EXPECT_FALSE(DumpHasStream(CommentStreamW));
424 EXPECT_FALSE(DumpHasStream(FunctionTableStream));
425 EXPECT_FALSE(DumpHasStream(MemoryInfoListStream));
426 EXPECT_FALSE(DumpHasStream(ThreadInfoListStream));
427 EXPECT_FALSE(DumpHasStream(HandleOperationListStream));
428 EXPECT_FALSE(DumpHasStream(TokenStream));
433 int main(int argc, char** argv) {
434 testing::InitGoogleTest(&argc, argv);
435 CommandLine::Init(argc, argv);
437 logging::LoggingSettings settings;
438 settings.logging_dest = logging::LOG_TO_ALL;
439 settings.log_file = L"CON";
440 settings.lock_log = logging::DONT_LOCK_LOG_FILE;
441 logging::InitLogging(settings);
442 return RUN_ALL_TESTS();