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