michael@0: // Copyright (c) 2010, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "client/windows/crash_generation/minidump_generator.h" michael@0: #include "client/windows/unittests/dump_analysis.h" // NOLINT michael@0: michael@0: #include "gtest/gtest.h" michael@0: michael@0: namespace { michael@0: michael@0: // Minidump with stacks, PEB, TEB, and unloaded module list. michael@0: const MINIDUMP_TYPE kSmallDumpType = static_cast( michael@0: MiniDumpWithProcessThreadData | // Get PEB and TEB. michael@0: MiniDumpWithUnloadedModules); // Get unloaded modules when available. michael@0: michael@0: // Minidump with all of the above, plus memory referenced from stack. michael@0: const MINIDUMP_TYPE kLargerDumpType = static_cast( michael@0: MiniDumpWithProcessThreadData | // Get PEB and TEB. michael@0: MiniDumpWithUnloadedModules | // Get unloaded modules when available. michael@0: MiniDumpWithIndirectlyReferencedMemory); // Get memory referenced by stack. michael@0: michael@0: // Large dump with all process memory. michael@0: const MINIDUMP_TYPE kFullDumpType = static_cast( michael@0: MiniDumpWithFullMemory | // Full memory from process. michael@0: MiniDumpWithProcessThreadData | // Get PEB and TEB. michael@0: MiniDumpWithHandleData | // Get all handle information. michael@0: MiniDumpWithUnloadedModules); // Get unloaded modules when available. michael@0: michael@0: class MinidumpTest: public testing::Test { michael@0: public: michael@0: MinidumpTest() { michael@0: wchar_t temp_dir_path[ MAX_PATH ] = {0}; michael@0: ::GetTempPath(MAX_PATH, temp_dir_path); michael@0: dump_path_ = temp_dir_path; michael@0: } michael@0: michael@0: virtual void SetUp() { michael@0: // Make sure URLMon isn't loaded into our process. michael@0: ASSERT_EQ(NULL, ::GetModuleHandle(L"urlmon.dll")); michael@0: michael@0: // Then load and unload it to ensure we have something to michael@0: // stock the unloaded module list with. michael@0: HMODULE urlmon = ::LoadLibrary(L"urlmon.dll"); michael@0: ASSERT_TRUE(urlmon != NULL); michael@0: ASSERT_TRUE(::FreeLibrary(urlmon)); michael@0: } michael@0: michael@0: virtual void TearDown() { michael@0: if (!dump_file_.empty()) { michael@0: ::DeleteFile(dump_file_.c_str()); michael@0: dump_file_ = L""; michael@0: } michael@0: if (!full_dump_file_.empty()) { michael@0: ::DeleteFile(full_dump_file_.c_str()); michael@0: full_dump_file_ = L""; michael@0: } michael@0: } michael@0: michael@0: bool WriteDump(ULONG flags) { michael@0: using google_breakpad::MinidumpGenerator; michael@0: michael@0: // Fake exception is access violation on write to this. michael@0: EXCEPTION_RECORD ex_record = { michael@0: STATUS_ACCESS_VIOLATION, // ExceptionCode michael@0: 0, // ExceptionFlags michael@0: NULL, // ExceptionRecord; michael@0: reinterpret_cast(0xCAFEBABE), // ExceptionAddress; michael@0: 2, // NumberParameters; michael@0: { EXCEPTION_WRITE_FAULT, reinterpret_cast(this) } michael@0: }; michael@0: CONTEXT ctx_record = {}; michael@0: EXCEPTION_POINTERS ex_ptrs = { michael@0: &ex_record, michael@0: &ctx_record, michael@0: }; michael@0: michael@0: MinidumpGenerator generator(dump_path_); michael@0: michael@0: // And write a dump michael@0: bool result = generator.WriteMinidump(::GetCurrentProcess(), michael@0: ::GetCurrentProcessId(), michael@0: ::GetCurrentThreadId(), michael@0: ::GetCurrentThreadId(), michael@0: &ex_ptrs, michael@0: NULL, michael@0: static_cast(flags), michael@0: TRUE, michael@0: &dump_file_, michael@0: &full_dump_file_); michael@0: return result == TRUE; michael@0: } michael@0: michael@0: protected: michael@0: std::wstring dump_file_; michael@0: std::wstring full_dump_file_; michael@0: michael@0: std::wstring dump_path_; michael@0: }; michael@0: michael@0: // We need to be able to get file information from Windows michael@0: bool HasFileInfo(const std::wstring& file_path) { michael@0: DWORD dummy; michael@0: const wchar_t* path = file_path.c_str(); michael@0: DWORD length = ::GetFileVersionInfoSize(path, &dummy); michael@0: if (length == 0) michael@0: return NULL; michael@0: michael@0: void* data = calloc(length, 1); michael@0: if (!data) michael@0: return false; michael@0: michael@0: if (!::GetFileVersionInfo(path, dummy, length, data)) { michael@0: free(data); michael@0: return false; michael@0: } michael@0: michael@0: void* translate = NULL; michael@0: UINT page_count; michael@0: BOOL query_result = VerQueryValue( michael@0: data, michael@0: L"\\VarFileInfo\\Translation", michael@0: static_cast(&translate), michael@0: &page_count); michael@0: michael@0: free(data); michael@0: if (query_result && translate) { michael@0: return true; michael@0: } else { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: TEST_F(MinidumpTest, Version) { michael@0: API_VERSION* version = ::ImagehlpApiVersion(); michael@0: michael@0: HMODULE dbg_help = ::GetModuleHandle(L"dbghelp.dll"); michael@0: ASSERT_TRUE(dbg_help != NULL); michael@0: michael@0: wchar_t dbg_help_file[1024] = {}; michael@0: ASSERT_TRUE(::GetModuleFileName(dbg_help, michael@0: dbg_help_file, michael@0: sizeof(dbg_help_file) / michael@0: sizeof(*dbg_help_file))); michael@0: ASSERT_TRUE(HasFileInfo(std::wstring(dbg_help_file)) != NULL); michael@0: michael@0: // LOG(INFO) << "DbgHelp.dll version: " << file_info->file_version(); michael@0: } michael@0: michael@0: TEST_F(MinidumpTest, Normal) { michael@0: EXPECT_TRUE(WriteDump(MiniDumpNormal)); michael@0: DumpAnalysis mini(dump_file_); michael@0: michael@0: // We expect threads, modules and some memory. michael@0: EXPECT_TRUE(mini.HasStream(ThreadListStream)); michael@0: EXPECT_TRUE(mini.HasStream(ModuleListStream)); michael@0: EXPECT_TRUE(mini.HasStream(MemoryListStream)); michael@0: EXPECT_TRUE(mini.HasStream(ExceptionStream)); michael@0: EXPECT_TRUE(mini.HasStream(SystemInfoStream)); michael@0: EXPECT_TRUE(mini.HasStream(MiscInfoStream)); michael@0: michael@0: EXPECT_FALSE(mini.HasStream(ThreadExListStream)); michael@0: EXPECT_FALSE(mini.HasStream(Memory64ListStream)); michael@0: EXPECT_FALSE(mini.HasStream(CommentStreamA)); michael@0: EXPECT_FALSE(mini.HasStream(CommentStreamW)); michael@0: EXPECT_FALSE(mini.HasStream(HandleDataStream)); michael@0: EXPECT_FALSE(mini.HasStream(FunctionTableStream)); michael@0: EXPECT_FALSE(mini.HasStream(UnloadedModuleListStream)); michael@0: EXPECT_FALSE(mini.HasStream(MemoryInfoListStream)); michael@0: EXPECT_FALSE(mini.HasStream(ThreadInfoListStream)); michael@0: EXPECT_FALSE(mini.HasStream(HandleOperationListStream)); michael@0: EXPECT_FALSE(mini.HasStream(TokenStream)); michael@0: michael@0: // We expect no PEB nor TEBs in this dump. michael@0: EXPECT_FALSE(mini.HasTebs()); michael@0: EXPECT_FALSE(mini.HasPeb()); michael@0: michael@0: // We expect no off-stack memory in this dump. michael@0: EXPECT_FALSE(mini.HasMemory(this)); michael@0: } michael@0: michael@0: TEST_F(MinidumpTest, SmallDump) { michael@0: ASSERT_TRUE(WriteDump(kSmallDumpType)); michael@0: DumpAnalysis mini(dump_file_); michael@0: michael@0: EXPECT_TRUE(mini.HasStream(ThreadListStream)); michael@0: EXPECT_TRUE(mini.HasStream(ModuleListStream)); michael@0: EXPECT_TRUE(mini.HasStream(MemoryListStream)); michael@0: EXPECT_TRUE(mini.HasStream(ExceptionStream)); michael@0: EXPECT_TRUE(mini.HasStream(SystemInfoStream)); michael@0: EXPECT_TRUE(mini.HasStream(UnloadedModuleListStream)); michael@0: EXPECT_TRUE(mini.HasStream(MiscInfoStream)); michael@0: michael@0: // We expect PEB and TEBs in this dump. michael@0: EXPECT_TRUE(mini.HasTebs()); michael@0: EXPECT_TRUE(mini.HasPeb()); michael@0: michael@0: EXPECT_FALSE(mini.HasStream(ThreadExListStream)); michael@0: EXPECT_FALSE(mini.HasStream(Memory64ListStream)); michael@0: EXPECT_FALSE(mini.HasStream(CommentStreamA)); michael@0: EXPECT_FALSE(mini.HasStream(CommentStreamW)); michael@0: EXPECT_FALSE(mini.HasStream(HandleDataStream)); michael@0: EXPECT_FALSE(mini.HasStream(FunctionTableStream)); michael@0: EXPECT_FALSE(mini.HasStream(MemoryInfoListStream)); michael@0: EXPECT_FALSE(mini.HasStream(ThreadInfoListStream)); michael@0: EXPECT_FALSE(mini.HasStream(HandleOperationListStream)); michael@0: EXPECT_FALSE(mini.HasStream(TokenStream)); michael@0: michael@0: // We expect no off-stack memory in this dump. michael@0: EXPECT_FALSE(mini.HasMemory(this)); michael@0: } michael@0: michael@0: TEST_F(MinidumpTest, LargerDump) { michael@0: ASSERT_TRUE(WriteDump(kLargerDumpType)); michael@0: DumpAnalysis mini(dump_file_); michael@0: michael@0: // The dump should have all of these streams. michael@0: EXPECT_TRUE(mini.HasStream(ThreadListStream)); michael@0: EXPECT_TRUE(mini.HasStream(ModuleListStream)); michael@0: EXPECT_TRUE(mini.HasStream(MemoryListStream)); michael@0: EXPECT_TRUE(mini.HasStream(ExceptionStream)); michael@0: EXPECT_TRUE(mini.HasStream(SystemInfoStream)); michael@0: EXPECT_TRUE(mini.HasStream(UnloadedModuleListStream)); michael@0: EXPECT_TRUE(mini.HasStream(MiscInfoStream)); michael@0: michael@0: // We expect memory referenced by stack in this dump. michael@0: EXPECT_TRUE(mini.HasMemory(this)); michael@0: michael@0: // We expect PEB and TEBs in this dump. michael@0: EXPECT_TRUE(mini.HasTebs()); michael@0: EXPECT_TRUE(mini.HasPeb()); michael@0: michael@0: EXPECT_FALSE(mini.HasStream(ThreadExListStream)); michael@0: EXPECT_FALSE(mini.HasStream(Memory64ListStream)); michael@0: EXPECT_FALSE(mini.HasStream(CommentStreamA)); michael@0: EXPECT_FALSE(mini.HasStream(CommentStreamW)); michael@0: EXPECT_FALSE(mini.HasStream(HandleDataStream)); michael@0: EXPECT_FALSE(mini.HasStream(FunctionTableStream)); michael@0: EXPECT_FALSE(mini.HasStream(MemoryInfoListStream)); michael@0: EXPECT_FALSE(mini.HasStream(ThreadInfoListStream)); michael@0: EXPECT_FALSE(mini.HasStream(HandleOperationListStream)); michael@0: EXPECT_FALSE(mini.HasStream(TokenStream)); michael@0: } michael@0: michael@0: TEST_F(MinidumpTest, FullDump) { michael@0: ASSERT_TRUE(WriteDump(kFullDumpType)); michael@0: ASSERT_TRUE(dump_file_ != L""); michael@0: ASSERT_TRUE(full_dump_file_ != L""); michael@0: DumpAnalysis mini(dump_file_); michael@0: DumpAnalysis full(full_dump_file_); michael@0: michael@0: // Either dumps can contain part of the information. michael@0: michael@0: // The dump should have all of these streams. michael@0: EXPECT_TRUE(mini.HasStream(ThreadListStream)); michael@0: EXPECT_TRUE(full.HasStream(ThreadListStream)); michael@0: EXPECT_TRUE(mini.HasStream(ModuleListStream)); michael@0: EXPECT_TRUE(full.HasStream(ModuleListStream)); michael@0: EXPECT_TRUE(mini.HasStream(ExceptionStream)); michael@0: EXPECT_TRUE(full.HasStream(ExceptionStream)); michael@0: EXPECT_TRUE(mini.HasStream(SystemInfoStream)); michael@0: EXPECT_TRUE(full.HasStream(SystemInfoStream)); michael@0: EXPECT_TRUE(mini.HasStream(UnloadedModuleListStream)); michael@0: EXPECT_TRUE(full.HasStream(UnloadedModuleListStream)); michael@0: EXPECT_TRUE(mini.HasStream(MiscInfoStream)); michael@0: EXPECT_TRUE(full.HasStream(MiscInfoStream)); michael@0: EXPECT_TRUE(mini.HasStream(HandleDataStream)); michael@0: EXPECT_TRUE(full.HasStream(HandleDataStream)); michael@0: michael@0: // We expect memory referenced by stack in this dump. michael@0: EXPECT_FALSE(mini.HasMemory(this)); michael@0: EXPECT_TRUE(full.HasMemory(this)); michael@0: michael@0: // We expect PEB and TEBs in this dump. michael@0: EXPECT_TRUE(mini.HasTebs() || full.HasTebs()); michael@0: EXPECT_TRUE(mini.HasPeb() || full.HasPeb()); michael@0: michael@0: EXPECT_TRUE(mini.HasStream(MemoryListStream)); michael@0: EXPECT_TRUE(full.HasStream(Memory64ListStream)); michael@0: EXPECT_FALSE(mini.HasStream(Memory64ListStream)); michael@0: EXPECT_FALSE(full.HasStream(MemoryListStream)); michael@0: michael@0: // This is the only place we don't use OR because we want both not michael@0: // to have the streams. michael@0: EXPECT_FALSE(mini.HasStream(ThreadExListStream)); michael@0: EXPECT_FALSE(full.HasStream(ThreadExListStream)); michael@0: EXPECT_FALSE(mini.HasStream(CommentStreamA)); michael@0: EXPECT_FALSE(full.HasStream(CommentStreamA)); michael@0: EXPECT_FALSE(mini.HasStream(CommentStreamW)); michael@0: EXPECT_FALSE(full.HasStream(CommentStreamW)); michael@0: EXPECT_FALSE(mini.HasStream(FunctionTableStream)); michael@0: EXPECT_FALSE(full.HasStream(FunctionTableStream)); michael@0: EXPECT_FALSE(mini.HasStream(MemoryInfoListStream)); michael@0: EXPECT_FALSE(full.HasStream(MemoryInfoListStream)); michael@0: EXPECT_FALSE(mini.HasStream(ThreadInfoListStream)); michael@0: EXPECT_FALSE(full.HasStream(ThreadInfoListStream)); michael@0: EXPECT_FALSE(mini.HasStream(HandleOperationListStream)); michael@0: EXPECT_FALSE(full.HasStream(HandleOperationListStream)); michael@0: EXPECT_FALSE(mini.HasStream(TokenStream)); michael@0: EXPECT_FALSE(full.HasStream(TokenStream)); michael@0: } michael@0: michael@0: } // namespace