MinidumpEmitter.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //===- yaml2minidump.cpp - Convert a YAML file to a minidump file ---------===//
  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. #include "llvm/ObjectYAML/MinidumpYAML.h"
  9. #include "llvm/ObjectYAML/yaml2obj.h"
  10. #include "llvm/Support/ConvertUTF.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. #include <optional>
  13. using namespace llvm;
  14. using namespace llvm::minidump;
  15. using namespace llvm::MinidumpYAML;
  16. namespace {
  17. /// A helper class to manage the placement of various structures into the final
  18. /// minidump binary. Space for objects can be allocated via various allocate***
  19. /// methods, while the final minidump file is written by calling the writeTo
  20. /// method. The plain versions of allocation functions take a reference to the
  21. /// data which is to be written (and hence the data must be available until
  22. /// writeTo is called), while the "New" versions allocate the data in an
  23. /// allocator-managed buffer, which is available until the allocator object is
  24. /// destroyed. For both kinds of functions, it is possible to modify the
  25. /// data for which the space has been "allocated" until the final writeTo call.
  26. /// This is useful for "linking" the allocated structures via their offsets.
  27. class BlobAllocator {
  28. public:
  29. size_t tell() const { return NextOffset; }
  30. size_t allocateCallback(size_t Size,
  31. std::function<void(raw_ostream &)> Callback) {
  32. size_t Offset = NextOffset;
  33. NextOffset += Size;
  34. Callbacks.push_back(std::move(Callback));
  35. return Offset;
  36. }
  37. size_t allocateBytes(ArrayRef<uint8_t> Data) {
  38. return allocateCallback(
  39. Data.size(), [Data](raw_ostream &OS) { OS << toStringRef(Data); });
  40. }
  41. size_t allocateBytes(yaml::BinaryRef Data) {
  42. return allocateCallback(Data.binary_size(), [Data](raw_ostream &OS) {
  43. Data.writeAsBinary(OS);
  44. });
  45. }
  46. template <typename T> size_t allocateArray(ArrayRef<T> Data) {
  47. return allocateBytes({reinterpret_cast<const uint8_t *>(Data.data()),
  48. sizeof(T) * Data.size()});
  49. }
  50. template <typename T, typename RangeType>
  51. std::pair<size_t, MutableArrayRef<T>>
  52. allocateNewArray(const iterator_range<RangeType> &Range);
  53. template <typename T> size_t allocateObject(const T &Data) {
  54. return allocateArray(ArrayRef(Data));
  55. }
  56. template <typename T, typename... Types>
  57. std::pair<size_t, T *> allocateNewObject(Types &&... Args) {
  58. T *Object = new (Temporaries.Allocate<T>()) T(std::forward<Types>(Args)...);
  59. return {allocateObject(*Object), Object};
  60. }
  61. size_t allocateString(StringRef Str);
  62. void writeTo(raw_ostream &OS) const;
  63. private:
  64. size_t NextOffset = 0;
  65. BumpPtrAllocator Temporaries;
  66. std::vector<std::function<void(raw_ostream &)>> Callbacks;
  67. };
  68. } // namespace
  69. template <typename T, typename RangeType>
  70. std::pair<size_t, MutableArrayRef<T>>
  71. BlobAllocator::allocateNewArray(const iterator_range<RangeType> &Range) {
  72. size_t Num = std::distance(Range.begin(), Range.end());
  73. MutableArrayRef<T> Array(Temporaries.Allocate<T>(Num), Num);
  74. std::uninitialized_copy(Range.begin(), Range.end(), Array.begin());
  75. return {allocateArray(Array), Array};
  76. }
  77. size_t BlobAllocator::allocateString(StringRef Str) {
  78. SmallVector<UTF16, 32> WStr;
  79. bool OK = convertUTF8ToUTF16String(Str, WStr);
  80. assert(OK && "Invalid UTF8 in Str?");
  81. (void)OK;
  82. // The utf16 string is null-terminated, but the terminator is not counted in
  83. // the string size.
  84. WStr.push_back(0);
  85. size_t Result =
  86. allocateNewObject<support::ulittle32_t>(2 * (WStr.size() - 1)).first;
  87. allocateNewArray<support::ulittle16_t>(make_range(WStr.begin(), WStr.end()));
  88. return Result;
  89. }
  90. void BlobAllocator::writeTo(raw_ostream &OS) const {
  91. size_t BeginOffset = OS.tell();
  92. for (const auto &Callback : Callbacks)
  93. Callback(OS);
  94. assert(OS.tell() == BeginOffset + NextOffset &&
  95. "Callbacks wrote an unexpected number of bytes.");
  96. (void)BeginOffset;
  97. }
  98. static LocationDescriptor layout(BlobAllocator &File, yaml::BinaryRef Data) {
  99. return {support::ulittle32_t(Data.binary_size()),
  100. support::ulittle32_t(File.allocateBytes(Data))};
  101. }
  102. static size_t layout(BlobAllocator &File, MinidumpYAML::ExceptionStream &S) {
  103. File.allocateObject(S.MDExceptionStream);
  104. size_t DataEnd = File.tell();
  105. // Lay out the thread context data, (which is not a part of the stream).
  106. // TODO: This usually (always?) matches the thread context of the
  107. // corresponding thread, and may overlap memory regions as well. We could
  108. // add a level of indirection to the MinidumpYAML format (like an array of
  109. // Blobs that the LocationDescriptors index into) to be able to distinguish
  110. // the cases where location descriptions overlap vs happen to reference
  111. // identical data.
  112. S.MDExceptionStream.ThreadContext = layout(File, S.ThreadContext);
  113. return DataEnd;
  114. }
  115. static void layout(BlobAllocator &File, MemoryListStream::entry_type &Range) {
  116. Range.Entry.Memory = layout(File, Range.Content);
  117. }
  118. static void layout(BlobAllocator &File, ModuleListStream::entry_type &M) {
  119. M.Entry.ModuleNameRVA = File.allocateString(M.Name);
  120. M.Entry.CvRecord = layout(File, M.CvRecord);
  121. M.Entry.MiscRecord = layout(File, M.MiscRecord);
  122. }
  123. static void layout(BlobAllocator &File, ThreadListStream::entry_type &T) {
  124. T.Entry.Stack.Memory = layout(File, T.Stack);
  125. T.Entry.Context = layout(File, T.Context);
  126. }
  127. template <typename EntryT>
  128. static size_t layout(BlobAllocator &File,
  129. MinidumpYAML::detail::ListStream<EntryT> &S) {
  130. File.allocateNewObject<support::ulittle32_t>(S.Entries.size());
  131. for (auto &E : S.Entries)
  132. File.allocateObject(E.Entry);
  133. size_t DataEnd = File.tell();
  134. // Lay out the auxiliary data, (which is not a part of the stream).
  135. DataEnd = File.tell();
  136. for (auto &E : S.Entries)
  137. layout(File, E);
  138. return DataEnd;
  139. }
  140. static Directory layout(BlobAllocator &File, Stream &S) {
  141. Directory Result;
  142. Result.Type = S.Type;
  143. Result.Location.RVA = File.tell();
  144. std::optional<size_t> DataEnd;
  145. switch (S.Kind) {
  146. case Stream::StreamKind::Exception:
  147. DataEnd = layout(File, cast<MinidumpYAML::ExceptionStream>(S));
  148. break;
  149. case Stream::StreamKind::MemoryInfoList: {
  150. MemoryInfoListStream &InfoList = cast<MemoryInfoListStream>(S);
  151. File.allocateNewObject<minidump::MemoryInfoListHeader>(
  152. sizeof(minidump::MemoryInfoListHeader), sizeof(minidump::MemoryInfo),
  153. InfoList.Infos.size());
  154. File.allocateArray(ArrayRef(InfoList.Infos));
  155. break;
  156. }
  157. case Stream::StreamKind::MemoryList:
  158. DataEnd = layout(File, cast<MemoryListStream>(S));
  159. break;
  160. case Stream::StreamKind::ModuleList:
  161. DataEnd = layout(File, cast<ModuleListStream>(S));
  162. break;
  163. case Stream::StreamKind::RawContent: {
  164. RawContentStream &Raw = cast<RawContentStream>(S);
  165. File.allocateCallback(Raw.Size, [&Raw](raw_ostream &OS) {
  166. Raw.Content.writeAsBinary(OS);
  167. assert(Raw.Content.binary_size() <= Raw.Size);
  168. OS << std::string(Raw.Size - Raw.Content.binary_size(), '\0');
  169. });
  170. break;
  171. }
  172. case Stream::StreamKind::SystemInfo: {
  173. SystemInfoStream &SystemInfo = cast<SystemInfoStream>(S);
  174. File.allocateObject(SystemInfo.Info);
  175. // The CSD string is not a part of the stream.
  176. DataEnd = File.tell();
  177. SystemInfo.Info.CSDVersionRVA = File.allocateString(SystemInfo.CSDVersion);
  178. break;
  179. }
  180. case Stream::StreamKind::TextContent:
  181. File.allocateArray(arrayRefFromStringRef(cast<TextContentStream>(S).Text));
  182. break;
  183. case Stream::StreamKind::ThreadList:
  184. DataEnd = layout(File, cast<ThreadListStream>(S));
  185. break;
  186. }
  187. // If DataEnd is not set, we assume everything we generated is a part of the
  188. // stream.
  189. Result.Location.DataSize =
  190. DataEnd.value_or(File.tell()) - Result.Location.RVA;
  191. return Result;
  192. }
  193. namespace llvm {
  194. namespace yaml {
  195. bool yaml2minidump(MinidumpYAML::Object &Obj, raw_ostream &Out,
  196. ErrorHandler /*EH*/) {
  197. BlobAllocator File;
  198. File.allocateObject(Obj.Header);
  199. std::vector<Directory> StreamDirectory(Obj.Streams.size());
  200. Obj.Header.StreamDirectoryRVA = File.allocateArray(ArrayRef(StreamDirectory));
  201. Obj.Header.NumberOfStreams = StreamDirectory.size();
  202. for (auto &Stream : enumerate(Obj.Streams))
  203. StreamDirectory[Stream.index()] = layout(File, *Stream.value());
  204. File.writeTo(Out);
  205. return true;
  206. }
  207. } // namespace yaml
  208. } // namespace llvm