1 // Copyright 2019 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 COMMON_WINDOWS_PE_UTIL_H_
30 #define COMMON_WINDOWS_PE_UTIL_H_
31
32 #include <windows.h>
33
34 #include "common/windows/module_info.h"
35
36 namespace google_breakpad {
37
38 using std::wstring;
39
40 // Reads |pe_file| and populates |info|. Returns true on success.
41 // Only supports PE32+ format, ie. a 64bit PE file.
42 // Will fail if |pe_file| does not contain a valid CodeView record.
43 bool ReadModuleInfo(const wstring& pe_file, PDBModuleInfo* info);
44
45 // Reads |pe_file| and populates |info|. Returns true on success.
46 bool ReadPEInfo(const wstring& pe_file, PEModuleInfo* info);
47
48 // Reads |pe_file| and prints frame data (aka. unwind info) to |out_file|.
49 // Only supports PE32+ format, ie. a 64bit PE file.
50 bool PrintPEFrameData(const wstring& pe_file, FILE* out_file);
51
52 // Combines a GUID |signature| and DWORD |age| to create a Breakpad debug
53 // identifier.
54 wstring GenerateDebugIdentifier(DWORD age, GUID signature);
55
56 // Combines a DWORD |signature| and DWORD |age| to create a Breakpad debug
57 // identifier.
58 wstring GenerateDebugIdentifier(DWORD age, DWORD signature);
59
60 // Converts |machine| enum value to the corresponding string used by Breakpad.
61 // The enum is IMAGE_FILE_MACHINE_*, contained in winnt.h.
FileHeaderMachineToCpuString(WORD machine)62 constexpr const wchar_t* FileHeaderMachineToCpuString(WORD machine) {
63 switch (machine) {
64 case IMAGE_FILE_MACHINE_I386: {
65 return L"x86";
66 }
67 case IMAGE_FILE_MACHINE_IA64:
68 case IMAGE_FILE_MACHINE_AMD64: {
69 return L"x86_64";
70 }
71 default: { return L"unknown"; }
72 }
73 }
74
75 } // namespace google_breakpad
76
77 #endif // COMMON_WINDOWS_PE_UTIL_H_
78