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/unittests/dump_analysis.h" // NOLINT michael@0: #include "testing/gtest/include/gtest/gtest.h" michael@0: michael@0: DumpAnalysis::~DumpAnalysis() { michael@0: if (dump_file_view_ != NULL) { michael@0: EXPECT_TRUE(::UnmapViewOfFile(dump_file_view_)); michael@0: ::CloseHandle(dump_file_mapping_); michael@0: dump_file_mapping_ = NULL; michael@0: } michael@0: michael@0: if (dump_file_handle_ != NULL) { michael@0: ::CloseHandle(dump_file_handle_); michael@0: dump_file_handle_ = NULL; michael@0: } michael@0: } michael@0: michael@0: void DumpAnalysis::EnsureDumpMapped() { michael@0: if (dump_file_view_ == NULL) { michael@0: dump_file_handle_ = ::CreateFile(dump_file_.c_str(), michael@0: GENERIC_READ, michael@0: 0, michael@0: NULL, michael@0: OPEN_EXISTING, michael@0: 0, michael@0: NULL); michael@0: ASSERT_TRUE(dump_file_handle_ != NULL); michael@0: ASSERT_TRUE(dump_file_mapping_ == NULL); michael@0: michael@0: dump_file_mapping_ = ::CreateFileMapping(dump_file_handle_, michael@0: NULL, michael@0: PAGE_READONLY, michael@0: 0, michael@0: 0, michael@0: NULL); michael@0: ASSERT_TRUE(dump_file_mapping_ != NULL); michael@0: michael@0: dump_file_view_ = ::MapViewOfFile(dump_file_mapping_, michael@0: FILE_MAP_READ, michael@0: 0, michael@0: 0, michael@0: 0); michael@0: ASSERT_TRUE(dump_file_view_ != NULL); michael@0: } michael@0: } michael@0: michael@0: bool DumpAnalysis::HasTebs() const { michael@0: MINIDUMP_THREAD_LIST* thread_list = NULL; michael@0: size_t thread_list_size = GetStream(ThreadListStream, &thread_list); michael@0: michael@0: if (thread_list_size > 0 && thread_list != NULL) { michael@0: for (ULONG i = 0; i < thread_list->NumberOfThreads; ++i) { michael@0: if (!HasMemory(thread_list->Threads[i].Teb)) michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // No thread list, no TEB info. michael@0: return false; michael@0: } michael@0: michael@0: bool DumpAnalysis::HasPeb() const { michael@0: MINIDUMP_THREAD_LIST* thread_list = NULL; michael@0: size_t thread_list_size = GetStream(ThreadListStream, &thread_list); michael@0: michael@0: if (thread_list_size > 0 && thread_list != NULL && michael@0: thread_list->NumberOfThreads > 0) { michael@0: FakeTEB* teb = NULL; michael@0: if (!HasMemory(thread_list->Threads[0].Teb, &teb)) michael@0: return false; michael@0: michael@0: return HasMemory(teb->peb); michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool DumpAnalysis::HasStream(ULONG stream_number) const { michael@0: void* stream = NULL; michael@0: size_t stream_size = GetStreamImpl(stream_number, &stream); michael@0: return stream_size > 0 && stream != NULL; michael@0: } michael@0: michael@0: size_t DumpAnalysis::GetStreamImpl(ULONG stream_number, void** stream) const { michael@0: MINIDUMP_DIRECTORY* directory = NULL; michael@0: ULONG memory_list_size = 0; michael@0: BOOL ret = ::MiniDumpReadDumpStream(dump_file_view_, michael@0: stream_number, michael@0: &directory, michael@0: stream, michael@0: &memory_list_size); michael@0: michael@0: return ret ? memory_list_size : 0; michael@0: } michael@0: michael@0: bool DumpAnalysis::HasMemoryImpl(const void *addr_in, size_t structuresize, michael@0: void **structure) const { michael@0: uintptr_t address = reinterpret_cast(addr_in); michael@0: MINIDUMP_MEMORY_LIST* memory_list = NULL; michael@0: size_t memory_list_size = GetStream(MemoryListStream, &memory_list); michael@0: if (memory_list_size > 0 && memory_list != NULL) { michael@0: for (ULONG i = 0; i < memory_list->NumberOfMemoryRanges; ++i) { michael@0: MINIDUMP_MEMORY_DESCRIPTOR& descr = memory_list->MemoryRanges[i]; michael@0: const uintptr_t range_start = michael@0: static_cast(descr.StartOfMemoryRange); michael@0: uintptr_t range_end = range_start + descr.Memory.DataSize; michael@0: michael@0: if (address >= range_start && michael@0: address + structuresize < range_end) { michael@0: // The start address falls in the range, and the end address is michael@0: // in bounds, return a pointer to the structure if requested. michael@0: if (structure != NULL) michael@0: *structure = RVA_TO_ADDR(dump_file_view_, descr.Memory.Rva); michael@0: michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // We didn't find the range in a MINIDUMP_MEMORY_LIST, so maybe this michael@0: // is a full dump using MINIDUMP_MEMORY64_LIST with all the memory at the michael@0: // end of the dump file. michael@0: MINIDUMP_MEMORY64_LIST* memory64_list = NULL; michael@0: memory_list_size = GetStream(Memory64ListStream, &memory64_list); michael@0: if (memory_list_size > 0 && memory64_list != NULL) { michael@0: // Keep track of where the current descriptor maps to. michael@0: RVA64 curr_rva = memory64_list->BaseRva; michael@0: for (ULONG i = 0; i < memory64_list->NumberOfMemoryRanges; ++i) { michael@0: MINIDUMP_MEMORY_DESCRIPTOR64& descr = memory64_list->MemoryRanges[i]; michael@0: uintptr_t range_start = michael@0: static_cast(descr.StartOfMemoryRange); michael@0: uintptr_t range_end = range_start + static_cast(descr.DataSize); michael@0: michael@0: if (address >= range_start && michael@0: address + structuresize < range_end) { michael@0: // The start address falls in the range, and the end address is michael@0: // in bounds, return a pointer to the structure if requested. michael@0: if (structure != NULL) michael@0: *structure = RVA_TO_ADDR(dump_file_view_, curr_rva); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // Advance the current RVA. michael@0: curr_rva += descr.DataSize; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: }