MinidumpYAML.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MinidumpYAML.h - Minidump YAMLIO implementation ----------*- 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. #ifndef LLVM_OBJECTYAML_MINIDUMPYAML_H
  14. #define LLVM_OBJECTYAML_MINIDUMPYAML_H
  15. #include "llvm/BinaryFormat/Minidump.h"
  16. #include "llvm/Object/Minidump.h"
  17. #include "llvm/ObjectYAML/YAML.h"
  18. #include "llvm/Support/YAMLTraits.h"
  19. namespace llvm {
  20. namespace MinidumpYAML {
  21. /// The base class for all minidump streams. The "Type" of the stream
  22. /// corresponds to the Stream Type field in the minidump file. The "Kind" field
  23. /// specifies how are we going to treat it. For highly specialized streams (e.g.
  24. /// SystemInfo), there is a 1:1 mapping between Types and Kinds, but in general
  25. /// one stream Kind can be used to represent multiple stream Types (e.g. any
  26. /// unrecognised stream Type will be handled via RawContentStream). The mapping
  27. /// from Types to Kinds is fixed and given by the static getKind function.
  28. struct Stream {
  29. enum class StreamKind {
  30. Exception,
  31. MemoryInfoList,
  32. MemoryList,
  33. ModuleList,
  34. RawContent,
  35. SystemInfo,
  36. TextContent,
  37. ThreadList,
  38. };
  39. Stream(StreamKind Kind, minidump::StreamType Type) : Kind(Kind), Type(Type) {}
  40. virtual ~Stream(); // anchor
  41. const StreamKind Kind;
  42. const minidump::StreamType Type;
  43. /// Get the stream Kind used for representing streams of a given Type.
  44. static StreamKind getKind(minidump::StreamType Type);
  45. /// Create an empty stream of the given Type.
  46. static std::unique_ptr<Stream> create(minidump::StreamType Type);
  47. /// Create a stream from the given stream directory entry.
  48. static Expected<std::unique_ptr<Stream>>
  49. create(const minidump::Directory &StreamDesc,
  50. const object::MinidumpFile &File);
  51. };
  52. namespace detail {
  53. /// A stream representing a list of abstract entries in a minidump stream. Its
  54. /// instantiations can be used to represent the ModuleList stream and other
  55. /// streams with a similar structure.
  56. template <typename EntryT> struct ListStream : public Stream {
  57. using entry_type = EntryT;
  58. std::vector<entry_type> Entries;
  59. explicit ListStream(std::vector<entry_type> Entries = {})
  60. : Stream(EntryT::Kind, EntryT::Type), Entries(std::move(Entries)) {}
  61. static bool classof(const Stream *S) { return S->Kind == EntryT::Kind; }
  62. };
  63. /// A structure containing all data belonging to a single minidump module.
  64. struct ParsedModule {
  65. static constexpr Stream::StreamKind Kind = Stream::StreamKind::ModuleList;
  66. static constexpr minidump::StreamType Type = minidump::StreamType::ModuleList;
  67. minidump::Module Entry;
  68. std::string Name;
  69. yaml::BinaryRef CvRecord;
  70. yaml::BinaryRef MiscRecord;
  71. };
  72. /// A structure containing all data belonging to a single minidump thread.
  73. struct ParsedThread {
  74. static constexpr Stream::StreamKind Kind = Stream::StreamKind::ThreadList;
  75. static constexpr minidump::StreamType Type = minidump::StreamType::ThreadList;
  76. minidump::Thread Entry;
  77. yaml::BinaryRef Stack;
  78. yaml::BinaryRef Context;
  79. };
  80. /// A structure containing all data describing a single memory region.
  81. struct ParsedMemoryDescriptor {
  82. static constexpr Stream::StreamKind Kind = Stream::StreamKind::MemoryList;
  83. static constexpr minidump::StreamType Type = minidump::StreamType::MemoryList;
  84. minidump::MemoryDescriptor Entry;
  85. yaml::BinaryRef Content;
  86. };
  87. } // namespace detail
  88. using ModuleListStream = detail::ListStream<detail::ParsedModule>;
  89. using ThreadListStream = detail::ListStream<detail::ParsedThread>;
  90. using MemoryListStream = detail::ListStream<detail::ParsedMemoryDescriptor>;
  91. /// ExceptionStream minidump stream.
  92. struct ExceptionStream : public Stream {
  93. minidump::ExceptionStream MDExceptionStream;
  94. yaml::BinaryRef ThreadContext;
  95. ExceptionStream()
  96. : Stream(StreamKind::Exception, minidump::StreamType::Exception),
  97. MDExceptionStream({}) {}
  98. explicit ExceptionStream(const minidump::ExceptionStream &MDExceptionStream,
  99. ArrayRef<uint8_t> ThreadContext)
  100. : Stream(StreamKind::Exception, minidump::StreamType::Exception),
  101. MDExceptionStream(MDExceptionStream), ThreadContext(ThreadContext) {}
  102. static bool classof(const Stream *S) {
  103. return S->Kind == StreamKind::Exception;
  104. }
  105. };
  106. /// A structure containing the list of MemoryInfo entries comprising a
  107. /// MemoryInfoList stream.
  108. struct MemoryInfoListStream : public Stream {
  109. std::vector<minidump::MemoryInfo> Infos;
  110. MemoryInfoListStream()
  111. : Stream(StreamKind::MemoryInfoList,
  112. minidump::StreamType::MemoryInfoList) {}
  113. explicit MemoryInfoListStream(
  114. iterator_range<object::MinidumpFile::MemoryInfoIterator> Range)
  115. : Stream(StreamKind::MemoryInfoList,
  116. minidump::StreamType::MemoryInfoList),
  117. Infos(Range.begin(), Range.end()) {}
  118. static bool classof(const Stream *S) {
  119. return S->Kind == StreamKind::MemoryInfoList;
  120. }
  121. };
  122. /// A minidump stream represented as a sequence of hex bytes. This is used as a
  123. /// fallback when no other stream kind is suitable.
  124. struct RawContentStream : public Stream {
  125. yaml::BinaryRef Content;
  126. yaml::Hex32 Size;
  127. RawContentStream(minidump::StreamType Type, ArrayRef<uint8_t> Content = {})
  128. : Stream(StreamKind::RawContent, Type), Content(Content),
  129. Size(Content.size()) {}
  130. static bool classof(const Stream *S) {
  131. return S->Kind == StreamKind::RawContent;
  132. }
  133. };
  134. /// SystemInfo minidump stream.
  135. struct SystemInfoStream : public Stream {
  136. minidump::SystemInfo Info;
  137. std::string CSDVersion;
  138. SystemInfoStream()
  139. : Stream(StreamKind::SystemInfo, minidump::StreamType::SystemInfo) {
  140. memset(&Info, 0, sizeof(Info));
  141. }
  142. explicit SystemInfoStream(const minidump::SystemInfo &Info,
  143. std::string CSDVersion)
  144. : Stream(StreamKind::SystemInfo, minidump::StreamType::SystemInfo),
  145. Info(Info), CSDVersion(std::move(CSDVersion)) {}
  146. static bool classof(const Stream *S) {
  147. return S->Kind == StreamKind::SystemInfo;
  148. }
  149. };
  150. /// A StringRef, which is printed using YAML block notation.
  151. LLVM_YAML_STRONG_TYPEDEF(StringRef, BlockStringRef)
  152. /// A minidump stream containing textual data (typically, the contents of a
  153. /// /proc/<pid> file on linux).
  154. struct TextContentStream : public Stream {
  155. BlockStringRef Text;
  156. TextContentStream(minidump::StreamType Type, StringRef Text = {})
  157. : Stream(StreamKind::TextContent, Type), Text(Text) {}
  158. static bool classof(const Stream *S) {
  159. return S->Kind == StreamKind::TextContent;
  160. }
  161. };
  162. /// The top level structure representing a minidump object, consisting of a
  163. /// minidump header, and zero or more streams. To construct an Object from a
  164. /// minidump file, use the static create function. To serialize to/from yaml,
  165. /// use the appropriate streaming operator on a yaml stream.
  166. struct Object {
  167. Object() = default;
  168. Object(const Object &) = delete;
  169. Object &operator=(const Object &) = delete;
  170. Object(Object &&) = default;
  171. Object &operator=(Object &&) = default;
  172. Object(const minidump::Header &Header,
  173. std::vector<std::unique_ptr<Stream>> Streams)
  174. : Header(Header), Streams(std::move(Streams)) {}
  175. /// The minidump header.
  176. minidump::Header Header;
  177. /// The list of streams in this minidump object.
  178. std::vector<std::unique_ptr<Stream>> Streams;
  179. static Expected<Object> create(const object::MinidumpFile &File);
  180. };
  181. } // namespace MinidumpYAML
  182. namespace yaml {
  183. template <> struct BlockScalarTraits<MinidumpYAML::BlockStringRef> {
  184. static void output(const MinidumpYAML::BlockStringRef &Text, void *,
  185. raw_ostream &OS) {
  186. OS << Text;
  187. }
  188. static StringRef input(StringRef Scalar, void *,
  189. MinidumpYAML::BlockStringRef &Text) {
  190. Text = Scalar;
  191. return "";
  192. }
  193. };
  194. template <> struct MappingTraits<std::unique_ptr<MinidumpYAML::Stream>> {
  195. static void mapping(IO &IO, std::unique_ptr<MinidumpYAML::Stream> &S);
  196. static std::string validate(IO &IO, std::unique_ptr<MinidumpYAML::Stream> &S);
  197. };
  198. template <> struct MappingContextTraits<minidump::MemoryDescriptor, BinaryRef> {
  199. static void mapping(IO &IO, minidump::MemoryDescriptor &Memory,
  200. BinaryRef &Content);
  201. };
  202. } // namespace yaml
  203. } // namespace llvm
  204. LLVM_YAML_DECLARE_BITSET_TRAITS(llvm::minidump::MemoryProtection)
  205. LLVM_YAML_DECLARE_BITSET_TRAITS(llvm::minidump::MemoryState)
  206. LLVM_YAML_DECLARE_BITSET_TRAITS(llvm::minidump::MemoryType)
  207. LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::minidump::ProcessorArchitecture)
  208. LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::minidump::OSPlatform)
  209. LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::minidump::StreamType)
  210. LLVM_YAML_DECLARE_MAPPING_TRAITS(llvm::minidump::CPUInfo::ArmInfo)
  211. LLVM_YAML_DECLARE_MAPPING_TRAITS(llvm::minidump::CPUInfo::OtherInfo)
  212. LLVM_YAML_DECLARE_MAPPING_TRAITS(llvm::minidump::CPUInfo::X86Info)
  213. LLVM_YAML_DECLARE_MAPPING_TRAITS(llvm::minidump::Exception)
  214. LLVM_YAML_DECLARE_MAPPING_TRAITS(llvm::minidump::MemoryInfo)
  215. LLVM_YAML_DECLARE_MAPPING_TRAITS(llvm::minidump::VSFixedFileInfo)
  216. LLVM_YAML_DECLARE_MAPPING_TRAITS(
  217. llvm::MinidumpYAML::MemoryListStream::entry_type)
  218. LLVM_YAML_DECLARE_MAPPING_TRAITS(
  219. llvm::MinidumpYAML::ModuleListStream::entry_type)
  220. LLVM_YAML_DECLARE_MAPPING_TRAITS(
  221. llvm::MinidumpYAML::ThreadListStream::entry_type)
  222. LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::MinidumpYAML::Stream>)
  223. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MinidumpYAML::MemoryListStream::entry_type)
  224. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MinidumpYAML::ModuleListStream::entry_type)
  225. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MinidumpYAML::ThreadListStream::entry_type)
  226. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::minidump::MemoryInfo)
  227. LLVM_YAML_DECLARE_MAPPING_TRAITS(llvm::MinidumpYAML::Object)
  228. #endif // LLVM_OBJECTYAML_MINIDUMPYAML_H
  229. #ifdef __GNUC__
  230. #pragma GCC diagnostic pop
  231. #endif