xref: /llvm-project/llvm/include/llvm/BinaryFormat/Minidump.h (revision c742a5dc2e67e1f0020a1fd9f602b369b740eafc)
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/BitmaskEnum.h"
22 #include "llvm/ADT/DenseMapInfo.h"
23 #include "llvm/Support/Endian.h"
24 
25 namespace llvm {
26 namespace minidump {
27 
28 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
29 
30 /// The minidump header is the first part of a minidump file. It identifies the
31 /// file as a minidump file, and gives the location of the stream directory.
32 struct Header {
33   static constexpr uint32_t MagicSignature = 0x504d444d; // PMDM
34   static constexpr uint16_t MagicVersion = 0xa793;
35 
36   support::ulittle32_t Signature;
37   // The high 16 bits of version field are implementation specific. The low 16
38   // bits should be MagicVersion.
39   support::ulittle32_t Version;
40   support::ulittle32_t NumberOfStreams;
41   support::ulittle32_t StreamDirectoryRVA;
42   support::ulittle32_t Checksum;
43   support::ulittle32_t TimeDateStamp;
44   support::ulittle64_t Flags;
45 };
46 static_assert(sizeof(Header) == 32);
47 
48 /// The type of a minidump stream identifies its contents. Streams numbers after
49 /// LastReserved are for application-defined data streams.
50 enum class StreamType : uint32_t {
51 #define HANDLE_MDMP_STREAM_TYPE(CODE, NAME) NAME = CODE,
52 #include "llvm/BinaryFormat/MinidumpConstants.def"
53   Unused = 0,
54   LastReserved = 0x0000ffff,
55 };
56 
57 /// Specifies the location (and size) of various objects in the minidump file.
58 /// The location is relative to the start of the file.
59 struct LocationDescriptor {
60   support::ulittle32_t DataSize;
61   support::ulittle32_t RVA;
62 };
63 static_assert(sizeof(LocationDescriptor) == 8);
64 
65 /// Describes a single memory range (both its VM address and where to find it in
66 /// the file) of the process from which this minidump file was generated.
67 struct MemoryDescriptor {
68   support::ulittle64_t StartOfMemoryRange;
69   LocationDescriptor Memory;
70 };
71 static_assert(sizeof(MemoryDescriptor) == 16);
72 
73 struct MemoryDescriptor_64 {
74   support::ulittle64_t StartOfMemoryRange;
75   support::ulittle64_t DataSize;
76 };
77 static_assert(sizeof(MemoryDescriptor_64) == 16);
78 
79 struct MemoryListHeader {
80   support::ulittle32_t NumberOfMemoryRanges;
81 };
82 static_assert(sizeof(MemoryListHeader) == 4);
83 
84 struct Memory64ListHeader {
85   support::ulittle64_t NumberOfMemoryRanges;
86   support::ulittle64_t BaseRVA;
87 };
88 static_assert(sizeof(Memory64ListHeader) == 16);
89 
90 struct MemoryInfoListHeader {
91   support::ulittle32_t SizeOfHeader;
92   support::ulittle32_t SizeOfEntry;
93   support::ulittle64_t NumberOfEntries;
94 
95   MemoryInfoListHeader() = default;
96   MemoryInfoListHeader(uint32_t SizeOfHeader, uint32_t SizeOfEntry,
97                        uint64_t NumberOfEntries)
98       : SizeOfHeader(SizeOfHeader), SizeOfEntry(SizeOfEntry),
99         NumberOfEntries(NumberOfEntries) {}
100 };
101 static_assert(sizeof(MemoryInfoListHeader) == 16);
102 
103 enum class MemoryProtection : uint32_t {
104 #define HANDLE_MDMP_PROTECT(CODE, NAME, NATIVENAME) NAME = CODE,
105 #include "llvm/BinaryFormat/MinidumpConstants.def"
106   LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/0xffffffffu),
107 };
108 
109 enum class MemoryState : uint32_t {
110 #define HANDLE_MDMP_MEMSTATE(CODE, NAME, NATIVENAME) NAME = CODE,
111 #include "llvm/BinaryFormat/MinidumpConstants.def"
112   LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/0xffffffffu),
113 };
114 
115 enum class MemoryType : uint32_t {
116 #define HANDLE_MDMP_MEMTYPE(CODE, NAME, NATIVENAME) NAME = CODE,
117 #include "llvm/BinaryFormat/MinidumpConstants.def"
118   LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/0xffffffffu),
119 };
120 
121 struct MemoryInfo {
122   support::ulittle64_t BaseAddress;
123   support::ulittle64_t AllocationBase;
124   support::little_t<MemoryProtection> AllocationProtect;
125   support::ulittle32_t Reserved0;
126   support::ulittle64_t RegionSize;
127   support::little_t<MemoryState> State;
128   support::little_t<MemoryProtection> Protect;
129   support::little_t<MemoryType> Type;
130   support::ulittle32_t Reserved1;
131 };
132 static_assert(sizeof(MemoryInfo) == 48);
133 
134 /// Specifies the location and type of a single stream in the minidump file. The
135 /// minidump stream directory is an array of entries of this type, with its size
136 /// given by Header.NumberOfStreams.
137 struct Directory {
138   support::little_t<StreamType> Type;
139   LocationDescriptor Location;
140 };
141 static_assert(sizeof(Directory) == 12);
142 
143 /// The processor architecture of the system that generated this minidump. Used
144 /// in the ProcessorArch field of the SystemInfo stream.
145 enum class ProcessorArchitecture : uint16_t {
146 #define HANDLE_MDMP_ARCH(CODE, NAME) NAME = CODE,
147 #include "llvm/BinaryFormat/MinidumpConstants.def"
148 };
149 
150 /// The OS Platform of the system that generated this minidump. Used in the
151 /// PlatformId field of the SystemInfo stream.
152 enum class OSPlatform : uint32_t {
153 #define HANDLE_MDMP_PLATFORM(CODE, NAME) NAME = CODE,
154 #include "llvm/BinaryFormat/MinidumpConstants.def"
155 };
156 
157 /// Detailed information about the processor of the system that generated this
158 /// minidump. Its interpretation depends on the ProcessorArchitecture enum.
159 union CPUInfo {
160   struct X86Info {
161     char VendorID[12];                        // cpuid 0: ebx, edx, ecx
162     support::ulittle32_t VersionInfo;         // cpuid 1: eax
163     support::ulittle32_t FeatureInfo;         // cpuid 1: edx
164     support::ulittle32_t AMDExtendedFeatures; // cpuid 0x80000001, ebx
165   } X86;
166   struct ArmInfo {
167     support::ulittle32_t CPUID;
168     support::ulittle32_t ElfHWCaps; // linux specific, 0 otherwise
169   } Arm;
170   struct OtherInfo {
171     uint8_t ProcessorFeatures[16];
172   } Other;
173 };
174 static_assert(sizeof(CPUInfo) == 24);
175 
176 /// The SystemInfo stream, containing various information about the system where
177 /// this minidump was generated.
178 struct SystemInfo {
179   support::little_t<ProcessorArchitecture> ProcessorArch;
180   support::ulittle16_t ProcessorLevel;
181   support::ulittle16_t ProcessorRevision;
182 
183   uint8_t NumberOfProcessors;
184   uint8_t ProductType;
185 
186   support::ulittle32_t MajorVersion;
187   support::ulittle32_t MinorVersion;
188   support::ulittle32_t BuildNumber;
189   support::little_t<OSPlatform> PlatformId;
190   support::ulittle32_t CSDVersionRVA;
191 
192   support::ulittle16_t SuiteMask;
193   support::ulittle16_t Reserved;
194 
195   CPUInfo CPU;
196 };
197 static_assert(sizeof(SystemInfo) == 56);
198 
199 struct VSFixedFileInfo {
200   support::ulittle32_t Signature;
201   support::ulittle32_t StructVersion;
202   support::ulittle32_t FileVersionHigh;
203   support::ulittle32_t FileVersionLow;
204   support::ulittle32_t ProductVersionHigh;
205   support::ulittle32_t ProductVersionLow;
206   support::ulittle32_t FileFlagsMask;
207   support::ulittle32_t FileFlags;
208   support::ulittle32_t FileOS;
209   support::ulittle32_t FileType;
210   support::ulittle32_t FileSubtype;
211   support::ulittle32_t FileDateHigh;
212   support::ulittle32_t FileDateLow;
213 };
214 static_assert(sizeof(VSFixedFileInfo) == 52);
215 
216 inline bool operator==(const VSFixedFileInfo &LHS, const VSFixedFileInfo &RHS) {
217   return memcmp(&LHS, &RHS, sizeof(VSFixedFileInfo)) == 0;
218 }
219 
220 struct Module {
221   support::ulittle64_t BaseOfImage;
222   support::ulittle32_t SizeOfImage;
223   support::ulittle32_t Checksum;
224   support::ulittle32_t TimeDateStamp;
225   support::ulittle32_t ModuleNameRVA;
226   VSFixedFileInfo VersionInfo;
227   LocationDescriptor CvRecord;
228   LocationDescriptor MiscRecord;
229   support::ulittle64_t Reserved0;
230   support::ulittle64_t Reserved1;
231 };
232 static_assert(sizeof(Module) == 108);
233 
234 /// Describes a single thread in the minidump file. Part of the ThreadList
235 /// stream.
236 struct Thread {
237   support::ulittle32_t ThreadId;
238   support::ulittle32_t SuspendCount;
239   support::ulittle32_t PriorityClass;
240   support::ulittle32_t Priority;
241   support::ulittle64_t EnvironmentBlock;
242   MemoryDescriptor Stack;
243   LocationDescriptor Context;
244 };
245 static_assert(sizeof(Thread) == 48);
246 
247 struct Exception {
248   static constexpr size_t MaxParameters = 15;
249   static constexpr size_t MaxParameterBytes = MaxParameters * sizeof(uint64_t);
250   static const uint32_t LLDB_FLAG = 0x4C4C4442; // ASCII for 'LLDB'
251 
252   support::ulittle32_t ExceptionCode;
253   support::ulittle32_t ExceptionFlags;
254   support::ulittle64_t ExceptionRecord;
255   support::ulittle64_t ExceptionAddress;
256   support::ulittle32_t NumberParameters;
257   support::ulittle32_t UnusedAlignment;
258   support::ulittle64_t ExceptionInformation[MaxParameters];
259 };
260 static_assert(sizeof(Exception) == 152);
261 
262 struct ExceptionStream {
263   support::ulittle32_t ThreadId;
264   support::ulittle32_t UnusedAlignment;
265   Exception ExceptionRecord;
266   LocationDescriptor ThreadContext;
267 };
268 static_assert(sizeof(ExceptionStream) == 168);
269 
270 } // namespace minidump
271 
272 template <> struct DenseMapInfo<minidump::StreamType> {
273   static minidump::StreamType getEmptyKey() { return minidump::StreamType(-1); }
274 
275   static minidump::StreamType getTombstoneKey() {
276     return minidump::StreamType(-2);
277   }
278 
279   static unsigned getHashValue(minidump::StreamType Val) {
280     return DenseMapInfo<uint32_t>::getHashValue(static_cast<uint32_t>(Val));
281   }
282 
283   static bool isEqual(minidump::StreamType LHS, minidump::StreamType RHS) {
284     return LHS == RHS;
285   }
286 };
287 
288 } // namespace llvm
289 
290 #endif // LLVM_BINARYFORMAT_MINIDUMP_H
291