1 //===- Minidump.h - Minidump constants and structures -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This header constants and data structures pertaining to the Windows Minidump
10 // core file format.
11 //
12 // Reference:
13 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms679293(v=vs.85).aspx
14 // https://chromium.googlesource.com/breakpad/breakpad/
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef LLVM_BINARYFORMAT_MINIDUMP_H
19 #define LLVM_BINARYFORMAT_MINIDUMP_H
20 
21 #include "llvm/ADT/DenseMapInfo.h"
22 #include "llvm/Support/Endian.h"
23 
24 namespace llvm {
25 namespace minidump {
26 
27 /// The minidump header is the first part of a minidump file. It identifies the
28 /// file as a minidump file, and gives the location of the stream directory.
29 struct Header {
30   static constexpr uint32_t MagicSignature = 0x504d444d; // PMDM
31   static constexpr uint16_t MagicVersion = 0xa793;
32 
33   support::ulittle32_t Signature;
34   // The high 16 bits of version field are implementation specific. The low 16
35   // bits should be MagicVersion.
36   support::ulittle32_t Version;
37   support::ulittle32_t NumberOfStreams;
38   support::ulittle32_t StreamDirectoryRVA;
39   support::ulittle32_t Checksum;
40   support::ulittle32_t TimeDateStamp;
41   support::ulittle64_t Flags;
42 };
43 static_assert(sizeof(Header) == 32, "");
44 
45 /// The type of a minidump stream identifies its contents. Streams numbers after
46 /// LastReserved are for application-defined data streams.
47 enum class StreamType : uint32_t {
48 #define HANDLE_MDMP_STREAM_TYPE(CODE, NAME) NAME = CODE,
49 #include "llvm/BinaryFormat/MinidumpConstants.def"
50   Unused = 0,
51   LastReserved = 0x0000ffff,
52 };
53 
54 /// Specifies the location (and size) of various objects in the minidump file.
55 /// The location is relative to the start of the file.
56 struct LocationDescriptor {
57   support::ulittle32_t DataSize;
58   support::ulittle32_t RVA;
59 };
60 static_assert(sizeof(LocationDescriptor) == 8, "");
61 
62 /// Describes a single memory range (both its VM address and where to find it in
63 /// the file) of the process from which this minidump file was generated.
64 struct MemoryDescriptor {
65   support::ulittle64_t StartOfMemoryRange;
66   LocationDescriptor Memory;
67 };
68 static_assert(sizeof(MemoryDescriptor) == 16, "");
69 
70 /// Specifies the location and type of a single stream in the minidump file. The
71 /// minidump stream directory is an array of entries of this type, with its size
72 /// given by Header.NumberOfStreams.
73 struct Directory {
74   support::little_t<StreamType> Type;
75   LocationDescriptor Location;
76 };
77 static_assert(sizeof(Directory) == 12, "");
78 
79 /// The processor architecture of the system that generated this minidump. Used
80 /// in the ProcessorArch field of the SystemInfo stream.
81 enum class ProcessorArchitecture : uint16_t {
82 #define HANDLE_MDMP_ARCH(CODE, NAME) NAME = CODE,
83 #include "llvm/BinaryFormat/MinidumpConstants.def"
84 };
85 
86 /// The OS Platform of the system that generated this minidump. Used in the
87 /// PlatformId field of the SystemInfo stream.
88 enum class OSPlatform : uint32_t {
89 #define HANDLE_MDMP_PLATFORM(CODE, NAME) NAME = CODE,
90 #include "llvm/BinaryFormat/MinidumpConstants.def"
91 };
92 
93 /// Detailed information about the processor of the system that generated this
94 /// minidump. Its interpretation depends on the ProcessorArchitecture enum.
95 union CPUInfo {
96   struct X86Info {
97     char VendorID[12];                        // cpuid 0: ebx, edx, ecx
98     support::ulittle32_t VersionInfo;         // cpuid 1: eax
99     support::ulittle32_t FeatureInfo;         // cpuid 1: edx
100     support::ulittle32_t AMDExtendedFeatures; // cpuid 0x80000001, ebx
101   } X86;
102   struct ArmInfo {
103     support::ulittle32_t CPUID;
104     support::ulittle32_t ElfHWCaps; // linux specific, 0 otherwise
105   } Arm;
106   struct OtherInfo {
107     uint8_t ProcessorFeatures[16];
108   } Other;
109 };
110 static_assert(sizeof(CPUInfo) == 24, "");
111 
112 /// The SystemInfo stream, containing various information about the system where
113 /// this minidump was generated.
114 struct SystemInfo {
115   support::little_t<ProcessorArchitecture> ProcessorArch;
116   support::ulittle16_t ProcessorLevel;
117   support::ulittle16_t ProcessorRevision;
118 
119   uint8_t NumberOfProcessors;
120   uint8_t ProductType;
121 
122   support::ulittle32_t MajorVersion;
123   support::ulittle32_t MinorVersion;
124   support::ulittle32_t BuildNumber;
125   support::little_t<OSPlatform> PlatformId;
126   support::ulittle32_t CSDVersionRVA;
127 
128   support::ulittle16_t SuiteMask;
129   support::ulittle16_t Reserved;
130 
131   CPUInfo CPU;
132 };
133 static_assert(sizeof(SystemInfo) == 56, "");
134 
135 struct VSFixedFileInfo {
136   support::ulittle32_t Signature;
137   support::ulittle32_t StructVersion;
138   support::ulittle32_t FileVersionHigh;
139   support::ulittle32_t FileVersionLow;
140   support::ulittle32_t ProductVersionHigh;
141   support::ulittle32_t ProductVersionLow;
142   support::ulittle32_t FileFlagsMask;
143   support::ulittle32_t FileFlags;
144   support::ulittle32_t FileOS;
145   support::ulittle32_t FileType;
146   support::ulittle32_t FileSubtype;
147   support::ulittle32_t FileDateHigh;
148   support::ulittle32_t FileDateLow;
149 };
150 static_assert(sizeof(VSFixedFileInfo) == 52, "");
151 
152 inline bool operator==(const VSFixedFileInfo &LHS, const VSFixedFileInfo &RHS) {
153   return memcmp(&LHS, &RHS, sizeof(VSFixedFileInfo)) == 0;
154 }
155 
156 struct Module {
157   support::ulittle64_t BaseOfImage;
158   support::ulittle32_t SizeOfImage;
159   support::ulittle32_t Checksum;
160   support::ulittle32_t TimeDateStamp;
161   support::ulittle32_t ModuleNameRVA;
162   VSFixedFileInfo VersionInfo;
163   LocationDescriptor CvRecord;
164   LocationDescriptor MiscRecord;
165   support::ulittle64_t Reserved0;
166   support::ulittle64_t Reserved1;
167 };
168 static_assert(sizeof(Module) == 108, "");
169 
170 /// Describes a single thread in the minidump file. Part of the ThreadList
171 /// stream.
172 struct Thread {
173   support::ulittle32_t ThreadId;
174   support::ulittle32_t SuspendCount;
175   support::ulittle32_t PriorityClass;
176   support::ulittle32_t Priority;
177   support::ulittle64_t EnvironmentBlock;
178   MemoryDescriptor Stack;
179   LocationDescriptor Context;
180 };
181 static_assert(sizeof(Thread) == 48, "");
182 
183 } // namespace minidump
184 
185 template <> struct DenseMapInfo<minidump::StreamType> {
186   static minidump::StreamType getEmptyKey() { return minidump::StreamType(-1); }
187 
188   static minidump::StreamType getTombstoneKey() {
189     return minidump::StreamType(-2);
190   }
191 
192   static unsigned getHashValue(minidump::StreamType Val) {
193     return DenseMapInfo<uint32_t>::getHashValue(static_cast<uint32_t>(Val));
194   }
195 
196   static bool isEqual(minidump::StreamType LHS, minidump::StreamType RHS) {
197     return LHS == RHS;
198   }
199 };
200 
201 } // namespace llvm
202 
203 #endif // LLVM_BINARYFORMAT_MINIDUMP_H
204