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 78 struct MemoryInfoListHeader { 79 support::ulittle32_t SizeOfHeader; 80 support::ulittle32_t SizeOfEntry; 81 support::ulittle64_t NumberOfEntries; 82 83 MemoryInfoListHeader() = default; MemoryInfoListHeaderMemoryInfoListHeader84 MemoryInfoListHeader(uint32_t SizeOfHeader, uint32_t SizeOfEntry, 85 uint64_t NumberOfEntries) 86 : SizeOfHeader(SizeOfHeader), SizeOfEntry(SizeOfEntry), 87 NumberOfEntries(NumberOfEntries) {} 88 }; 89 static_assert(sizeof(MemoryInfoListHeader) == 16); 90 91 enum class MemoryProtection : uint32_t { 92 #define HANDLE_MDMP_PROTECT(CODE, NAME, NATIVENAME) NAME = CODE, 93 #include "llvm/BinaryFormat/MinidumpConstants.def" 94 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/0xffffffffu), 95 }; 96 97 enum class MemoryState : uint32_t { 98 #define HANDLE_MDMP_MEMSTATE(CODE, NAME, NATIVENAME) NAME = CODE, 99 #include "llvm/BinaryFormat/MinidumpConstants.def" 100 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/0xffffffffu), 101 }; 102 103 enum class MemoryType : uint32_t { 104 #define HANDLE_MDMP_MEMTYPE(CODE, NAME, NATIVENAME) NAME = CODE, 105 #include "llvm/BinaryFormat/MinidumpConstants.def" 106 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/0xffffffffu), 107 }; 108 109 struct MemoryInfo { 110 support::ulittle64_t BaseAddress; 111 support::ulittle64_t AllocationBase; 112 support::little_t<MemoryProtection> AllocationProtect; 113 support::ulittle32_t Reserved0; 114 support::ulittle64_t RegionSize; 115 support::little_t<MemoryState> State; 116 support::little_t<MemoryProtection> Protect; 117 support::little_t<MemoryType> Type; 118 support::ulittle32_t Reserved1; 119 }; 120 static_assert(sizeof(MemoryInfo) == 48); 121 122 /// Specifies the location and type of a single stream in the minidump file. The 123 /// minidump stream directory is an array of entries of this type, with its size 124 /// given by Header.NumberOfStreams. 125 struct Directory { 126 support::little_t<StreamType> Type; 127 LocationDescriptor Location; 128 }; 129 static_assert(sizeof(Directory) == 12); 130 131 /// The processor architecture of the system that generated this minidump. Used 132 /// in the ProcessorArch field of the SystemInfo stream. 133 enum class ProcessorArchitecture : uint16_t { 134 #define HANDLE_MDMP_ARCH(CODE, NAME) NAME = CODE, 135 #include "llvm/BinaryFormat/MinidumpConstants.def" 136 }; 137 138 /// The OS Platform of the system that generated this minidump. Used in the 139 /// PlatformId field of the SystemInfo stream. 140 enum class OSPlatform : uint32_t { 141 #define HANDLE_MDMP_PLATFORM(CODE, NAME) NAME = CODE, 142 #include "llvm/BinaryFormat/MinidumpConstants.def" 143 }; 144 145 /// Detailed information about the processor of the system that generated this 146 /// minidump. Its interpretation depends on the ProcessorArchitecture enum. 147 union CPUInfo { 148 struct X86Info { 149 char VendorID[12]; // cpuid 0: ebx, edx, ecx 150 support::ulittle32_t VersionInfo; // cpuid 1: eax 151 support::ulittle32_t FeatureInfo; // cpuid 1: edx 152 support::ulittle32_t AMDExtendedFeatures; // cpuid 0x80000001, ebx 153 } X86; 154 struct ArmInfo { 155 support::ulittle32_t CPUID; 156 support::ulittle32_t ElfHWCaps; // linux specific, 0 otherwise 157 } Arm; 158 struct OtherInfo { 159 uint8_t ProcessorFeatures[16]; 160 } Other; 161 }; 162 static_assert(sizeof(CPUInfo) == 24); 163 164 /// The SystemInfo stream, containing various information about the system where 165 /// this minidump was generated. 166 struct SystemInfo { 167 support::little_t<ProcessorArchitecture> ProcessorArch; 168 support::ulittle16_t ProcessorLevel; 169 support::ulittle16_t ProcessorRevision; 170 171 uint8_t NumberOfProcessors; 172 uint8_t ProductType; 173 174 support::ulittle32_t MajorVersion; 175 support::ulittle32_t MinorVersion; 176 support::ulittle32_t BuildNumber; 177 support::little_t<OSPlatform> PlatformId; 178 support::ulittle32_t CSDVersionRVA; 179 180 support::ulittle16_t SuiteMask; 181 support::ulittle16_t Reserved; 182 183 CPUInfo CPU; 184 }; 185 static_assert(sizeof(SystemInfo) == 56); 186 187 struct VSFixedFileInfo { 188 support::ulittle32_t Signature; 189 support::ulittle32_t StructVersion; 190 support::ulittle32_t FileVersionHigh; 191 support::ulittle32_t FileVersionLow; 192 support::ulittle32_t ProductVersionHigh; 193 support::ulittle32_t ProductVersionLow; 194 support::ulittle32_t FileFlagsMask; 195 support::ulittle32_t FileFlags; 196 support::ulittle32_t FileOS; 197 support::ulittle32_t FileType; 198 support::ulittle32_t FileSubtype; 199 support::ulittle32_t FileDateHigh; 200 support::ulittle32_t FileDateLow; 201 }; 202 static_assert(sizeof(VSFixedFileInfo) == 52); 203 204 inline bool operator==(const VSFixedFileInfo &LHS, const VSFixedFileInfo &RHS) { 205 return memcmp(&LHS, &RHS, sizeof(VSFixedFileInfo)) == 0; 206 } 207 208 struct Module { 209 support::ulittle64_t BaseOfImage; 210 support::ulittle32_t SizeOfImage; 211 support::ulittle32_t Checksum; 212 support::ulittle32_t TimeDateStamp; 213 support::ulittle32_t ModuleNameRVA; 214 VSFixedFileInfo VersionInfo; 215 LocationDescriptor CvRecord; 216 LocationDescriptor MiscRecord; 217 support::ulittle64_t Reserved0; 218 support::ulittle64_t Reserved1; 219 }; 220 static_assert(sizeof(Module) == 108); 221 222 /// Describes a single thread in the minidump file. Part of the ThreadList 223 /// stream. 224 struct Thread { 225 support::ulittle32_t ThreadId; 226 support::ulittle32_t SuspendCount; 227 support::ulittle32_t PriorityClass; 228 support::ulittle32_t Priority; 229 support::ulittle64_t EnvironmentBlock; 230 MemoryDescriptor Stack; 231 LocationDescriptor Context; 232 }; 233 static_assert(sizeof(Thread) == 48); 234 235 struct Exception { 236 static constexpr size_t MaxParameters = 15; 237 238 support::ulittle32_t ExceptionCode; 239 support::ulittle32_t ExceptionFlags; 240 support::ulittle64_t ExceptionRecord; 241 support::ulittle64_t ExceptionAddress; 242 support::ulittle32_t NumberParameters; 243 support::ulittle32_t UnusedAlignment; 244 support::ulittle64_t ExceptionInformation[MaxParameters]; 245 }; 246 static_assert(sizeof(Exception) == 152); 247 248 struct ExceptionStream { 249 support::ulittle32_t ThreadId; 250 support::ulittle32_t UnusedAlignment; 251 Exception ExceptionRecord; 252 LocationDescriptor ThreadContext; 253 }; 254 static_assert(sizeof(ExceptionStream) == 168); 255 256 } // namespace minidump 257 258 template <> struct DenseMapInfo<minidump::StreamType> { 259 static minidump::StreamType getEmptyKey() { return minidump::StreamType(-1); } 260 261 static minidump::StreamType getTombstoneKey() { 262 return minidump::StreamType(-2); 263 } 264 265 static unsigned getHashValue(minidump::StreamType Val) { 266 return DenseMapInfo<uint32_t>::getHashValue(static_cast<uint32_t>(Val)); 267 } 268 269 static bool isEqual(minidump::StreamType LHS, minidump::StreamType RHS) { 270 return LHS == RHS; 271 } 272 }; 273 274 } // namespace llvm 275 276 #endif // LLVM_BINARYFORMAT_MINIDUMP_H 277