Minidump.h 8.9 KB

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