Minidump.h 9.0 KB

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