1// Copyright (c) 2010, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8//     * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10//     * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14//     * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#ifndef CLIENT_WINDOWS_UNITTESTS_DUMP_ANALYSIS_H_
31#define CLIENT_WINDOWS_UNITTESTS_DUMP_ANALYSIS_H_
32
33#include "client/windows/crash_generation/minidump_generator.h"
34
35// Convenience to get to the PEB pointer in a TEB.
36struct FakeTEB {
37  char dummy[0x30];
38  void* peb;
39};
40
41class DumpAnalysis {
42 public:
43  explicit DumpAnalysis(const std::wstring& file_path)
44      : dump_file_(file_path), dump_file_view_(NULL), dump_file_mapping_(NULL),
45        dump_file_handle_(NULL) {
46    EnsureDumpMapped();
47  }
48  ~DumpAnalysis();
49
50  bool HasStream(ULONG stream_number) const;
51
52  // This is template to keep type safety in the front, but we end up casting
53  // to void** inside the implementation to pass the pointer to Win32. So
54  // casting here is considered safe.
55  template <class StreamType>
56  size_t GetStream(ULONG stream_number, StreamType** stream) const {
57    return GetStreamImpl(stream_number, reinterpret_cast<void**>(stream));
58  }
59
60  bool HasTebs() const;
61  bool HasPeb() const;
62  bool HasMemory(ULONG64 address) const {
63    return HasMemory<BYTE>(address, NULL);
64  }
65
66  bool HasMemory(const void* address) const {
67    return HasMemory<BYTE>(address, NULL);
68  }
69
70  template <class StructureType>
71  bool HasMemory(ULONG64 address, StructureType** structure = NULL) const {
72    // We can't cope with 64 bit addresses for now.
73    if (address > 0xFFFFFFFFUL)
74      return false;
75
76    return HasMemory(reinterpret_cast<void*>(address), structure);
77  }
78
79  template <class StructureType>
80  bool HasMemory(const void* addr_in, StructureType** structure = NULL) const {
81    return HasMemoryImpl(addr_in, sizeof(StructureType),
82                             reinterpret_cast<void**>(structure));
83  }
84
85 protected:
86  void EnsureDumpMapped();
87
88  HANDLE dump_file_mapping_;
89  HANDLE dump_file_handle_;
90  void* dump_file_view_;
91  std::wstring dump_file_;
92
93 private:
94  // This is the implementation of GetStream<>.
95  size_t GetStreamImpl(ULONG stream_number, void** stream) const;
96
97  // This is the implementation of HasMemory<>.
98  bool HasMemoryImpl(const void* addr_in, size_t pointersize,
99                     void** structure) const;
100};
101
102#endif  // CLIENT_WINDOWS_UNITTESTS_DUMP_ANALYSIS_H_
103