blob: f5f2acd0cc4bd2c84948a20d8f7b66e54d203fea [file] [log] [blame]
Pavel Labath69de7a92019-03-22 14:47:26 +00001//===- MinidumpYAML.cpp - Minidump YAMLIO implementation ------------------===//
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#include "llvm/ObjectYAML/MinidumpYAML.h"
Pavel Labath7429d862019-04-18 14:57:31 +000010#include "llvm/Support/Allocator.h"
Pavel Labath51d9fa02019-04-05 08:06:26 +000011#include "llvm/Support/ConvertUTF.h"
Pavel Labath69de7a92019-03-22 14:47:26 +000012
13using namespace llvm;
14using namespace llvm::MinidumpYAML;
15using namespace llvm::minidump;
16
17namespace {
Pavel Labath7429d862019-04-18 14:57:31 +000018/// A helper class to manage the placement of various structures into the final
19/// minidump binary. Space for objects can be allocated via various allocate***
20/// methods, while the final minidump file is written by calling the writeTo
21/// method. The plain versions of allocation functions take a reference to the
22/// data which is to be written (and hence the data must be available until
23/// writeTo is called), while the "New" versions allocate the data in an
24/// allocator-managed buffer, which is available until the allocator object is
25/// destroyed. For both kinds of functions, it is possible to modify the
26/// data for which the space has been "allocated" until the final writeTo call.
27/// This is useful for "linking" the allocated structures via their offsets.
Pavel Labath69de7a92019-03-22 14:47:26 +000028class BlobAllocator {
29public:
30 size_t tell() const { return NextOffset; }
31
Pavel Labathc5f79052019-03-25 14:45:31 +000032 size_t allocateCallback(size_t Size,
Pavel Labath69de7a92019-03-22 14:47:26 +000033 std::function<void(raw_ostream &)> Callback) {
34 size_t Offset = NextOffset;
35 NextOffset += Size;
36 Callbacks.push_back(std::move(Callback));
37 return Offset;
38 }
39
Pavel Labathc5f79052019-03-25 14:45:31 +000040 size_t allocateBytes(ArrayRef<uint8_t> Data) {
41 return allocateCallback(
Pavel Labath69de7a92019-03-22 14:47:26 +000042 Data.size(), [Data](raw_ostream &OS) { OS << toStringRef(Data); });
43 }
44
Pavel Labath7429d862019-04-18 14:57:31 +000045 size_t allocateBytes(yaml::BinaryRef Data) {
46 return allocateCallback(Data.binary_size(), [Data](raw_ostream &OS) {
47 Data.writeAsBinary(OS);
48 });
49 }
50
Pavel Labathc5f79052019-03-25 14:45:31 +000051 template <typename T> size_t allocateArray(ArrayRef<T> Data) {
52 return allocateBytes({reinterpret_cast<const uint8_t *>(Data.data()),
Pavel Labath69de7a92019-03-22 14:47:26 +000053 sizeof(T) * Data.size()});
54 }
55
Pavel Labath7429d862019-04-18 14:57:31 +000056 template <typename T, typename RangeType>
57 std::pair<size_t, MutableArrayRef<T>>
58 allocateNewArray(const iterator_range<RangeType> &Range);
59
Pavel Labathc5f79052019-03-25 14:45:31 +000060 template <typename T> size_t allocateObject(const T &Data) {
61 return allocateArray(makeArrayRef(Data));
Pavel Labath69de7a92019-03-22 14:47:26 +000062 }
63
Pavel Labath7429d862019-04-18 14:57:31 +000064 template <typename T, typename... Types>
65 std::pair<size_t, T *> allocateNewObject(Types &&... Args) {
66 T *Object = new (Temporaries.Allocate<T>()) T(std::forward<Types>(Args)...);
67 return {allocateObject(*Object), Object};
68 }
69
Pavel Labath51d9fa02019-04-05 08:06:26 +000070 size_t allocateString(StringRef Str);
71
Pavel Labath69de7a92019-03-22 14:47:26 +000072 void writeTo(raw_ostream &OS) const;
73
74private:
75 size_t NextOffset = 0;
76
Pavel Labath7429d862019-04-18 14:57:31 +000077 BumpPtrAllocator Temporaries;
Pavel Labath69de7a92019-03-22 14:47:26 +000078 std::vector<std::function<void(raw_ostream &)>> Callbacks;
79};
80} // namespace
81
Pavel Labath7429d862019-04-18 14:57:31 +000082template <typename T, typename RangeType>
83std::pair<size_t, MutableArrayRef<T>>
84BlobAllocator::allocateNewArray(const iterator_range<RangeType> &Range) {
85 size_t Num = std::distance(Range.begin(), Range.end());
86 MutableArrayRef<T> Array(Temporaries.Allocate<T>(Num), Num);
87 std::uninitialized_copy(Range.begin(), Range.end(), Array.begin());
88 return {allocateArray(Array), Array};
89}
90
Pavel Labath51d9fa02019-04-05 08:06:26 +000091size_t BlobAllocator::allocateString(StringRef Str) {
92 SmallVector<UTF16, 32> WStr;
93 bool OK = convertUTF8ToUTF16String(Str, WStr);
94 assert(OK && "Invalid UTF8 in Str?");
95 (void)OK;
96
Pavel Labath7429d862019-04-18 14:57:31 +000097 // The utf16 string is null-terminated, but the terminator is not counted in
98 // the string size.
99 WStr.push_back(0);
100 size_t Result =
101 allocateNewObject<support::ulittle32_t>(2 * (WStr.size() - 1)).first;
102 allocateNewArray<support::ulittle16_t>(make_range(WStr.begin(), WStr.end()));
103 return Result;
Pavel Labath51d9fa02019-04-05 08:06:26 +0000104}
105
Pavel Labath69de7a92019-03-22 14:47:26 +0000106void BlobAllocator::writeTo(raw_ostream &OS) const {
107 size_t BeginOffset = OS.tell();
108 for (const auto &Callback : Callbacks)
109 Callback(OS);
110 assert(OS.tell() == BeginOffset + NextOffset &&
111 "Callbacks wrote an unexpected number of bytes.");
112 (void)BeginOffset;
113}
114
115/// Perform an optional yaml-mapping of an endian-aware type EndianType. The
116/// only purpose of this function is to avoid casting the Default value to the
117/// endian type;
118template <typename EndianType>
119static inline void mapOptional(yaml::IO &IO, const char *Key, EndianType &Val,
120 typename EndianType::value_type Default) {
121 IO.mapOptional(Key, Val, EndianType(Default));
122}
123
124/// Yaml-map an endian-aware type EndianType as some other type MapType.
125template <typename MapType, typename EndianType>
126static inline void mapRequiredAs(yaml::IO &IO, const char *Key,
127 EndianType &Val) {
128 MapType Mapped = static_cast<typename EndianType::value_type>(Val);
129 IO.mapRequired(Key, Mapped);
130 Val = static_cast<typename EndianType::value_type>(Mapped);
131}
132
133/// Perform an optional yaml-mapping of an endian-aware type EndianType as some
134/// other type MapType.
135template <typename MapType, typename EndianType>
136static inline void mapOptionalAs(yaml::IO &IO, const char *Key, EndianType &Val,
137 MapType Default) {
138 MapType Mapped = static_cast<typename EndianType::value_type>(Val);
139 IO.mapOptional(Key, Mapped, Default);
140 Val = static_cast<typename EndianType::value_type>(Mapped);
141}
142
143namespace {
144/// Return the appropriate yaml Hex type for a given endian-aware type.
145template <typename EndianType> struct HexType;
146template <> struct HexType<support::ulittle16_t> { using type = yaml::Hex16; };
147template <> struct HexType<support::ulittle32_t> { using type = yaml::Hex32; };
148template <> struct HexType<support::ulittle64_t> { using type = yaml::Hex64; };
149} // namespace
150
151/// Yaml-map an endian-aware type as an appropriately-sized hex value.
152template <typename EndianType>
153static inline void mapRequiredHex(yaml::IO &IO, const char *Key,
154 EndianType &Val) {
155 mapRequiredAs<typename HexType<EndianType>::type>(IO, Key, Val);
156}
157
158/// Perform an optional yaml-mapping of an endian-aware type as an
159/// appropriately-sized hex value.
160template <typename EndianType>
161static inline void mapOptionalHex(yaml::IO &IO, const char *Key,
162 EndianType &Val,
163 typename EndianType::value_type Default) {
164 mapOptionalAs<typename HexType<EndianType>::type>(IO, Key, Val, Default);
165}
166
167Stream::~Stream() = default;
168
169Stream::StreamKind Stream::getKind(StreamType Type) {
170 switch (Type) {
Pavel Labath2d29e162019-05-16 15:17:30 +0000171 case StreamType::MemoryList:
172 return StreamKind::MemoryList;
Pavel Labath7429d862019-04-18 14:57:31 +0000173 case StreamType::ModuleList:
174 return StreamKind::ModuleList;
Pavel Labath69de7a92019-03-22 14:47:26 +0000175 case StreamType::SystemInfo:
176 return StreamKind::SystemInfo;
177 case StreamType::LinuxCPUInfo:
178 case StreamType::LinuxProcStatus:
179 case StreamType::LinuxLSBRelease:
180 case StreamType::LinuxCMDLine:
181 case StreamType::LinuxMaps:
182 case StreamType::LinuxProcStat:
183 case StreamType::LinuxProcUptime:
184 return StreamKind::TextContent;
Pavel Labathdcdb3c62019-05-09 15:13:53 +0000185 case StreamType::ThreadList:
186 return StreamKind::ThreadList;
Pavel Labath69de7a92019-03-22 14:47:26 +0000187 default:
188 return StreamKind::RawContent;
189 }
190}
191
192std::unique_ptr<Stream> Stream::create(StreamType Type) {
193 StreamKind Kind = getKind(Type);
194 switch (Kind) {
Pavel Labath2d29e162019-05-16 15:17:30 +0000195 case StreamKind::MemoryList:
196 return llvm::make_unique<MemoryListStream>();
Pavel Labath7429d862019-04-18 14:57:31 +0000197 case StreamKind::ModuleList:
198 return llvm::make_unique<ModuleListStream>();
Pavel Labath69de7a92019-03-22 14:47:26 +0000199 case StreamKind::RawContent:
200 return llvm::make_unique<RawContentStream>(Type);
201 case StreamKind::SystemInfo:
202 return llvm::make_unique<SystemInfoStream>();
203 case StreamKind::TextContent:
204 return llvm::make_unique<TextContentStream>(Type);
Pavel Labathdcdb3c62019-05-09 15:13:53 +0000205 case StreamKind::ThreadList:
206 return llvm::make_unique<ThreadListStream>();
Pavel Labath69de7a92019-03-22 14:47:26 +0000207 }
208 llvm_unreachable("Unhandled stream kind!");
209}
210
211void yaml::ScalarEnumerationTraits<ProcessorArchitecture>::enumeration(
212 IO &IO, ProcessorArchitecture &Arch) {
213#define HANDLE_MDMP_ARCH(CODE, NAME) \
214 IO.enumCase(Arch, #NAME, ProcessorArchitecture::NAME);
215#include "llvm/BinaryFormat/MinidumpConstants.def"
216 IO.enumFallback<Hex16>(Arch);
217}
218
219void yaml::ScalarEnumerationTraits<OSPlatform>::enumeration(IO &IO,
220 OSPlatform &Plat) {
221#define HANDLE_MDMP_PLATFORM(CODE, NAME) \
222 IO.enumCase(Plat, #NAME, OSPlatform::NAME);
223#include "llvm/BinaryFormat/MinidumpConstants.def"
224 IO.enumFallback<Hex32>(Plat);
225}
226
227void yaml::ScalarEnumerationTraits<StreamType>::enumeration(IO &IO,
228 StreamType &Type) {
229#define HANDLE_MDMP_STREAM_TYPE(CODE, NAME) \
230 IO.enumCase(Type, #NAME, StreamType::NAME);
231#include "llvm/BinaryFormat/MinidumpConstants.def"
232 IO.enumFallback<Hex32>(Type);
233}
234
235void yaml::MappingTraits<CPUInfo::ArmInfo>::mapping(IO &IO,
236 CPUInfo::ArmInfo &Info) {
237 mapRequiredHex(IO, "CPUID", Info.CPUID);
238 mapOptionalHex(IO, "ELF hwcaps", Info.ElfHWCaps, 0);
239}
240
241namespace {
242template <std::size_t N> struct FixedSizeHex {
243 FixedSizeHex(uint8_t (&Storage)[N]) : Storage(Storage) {}
244
245 uint8_t (&Storage)[N];
246};
247} // namespace
248
249namespace llvm {
250namespace yaml {
251template <std::size_t N> struct ScalarTraits<FixedSizeHex<N>> {
252 static void output(const FixedSizeHex<N> &Fixed, void *, raw_ostream &OS) {
253 OS << toHex(makeArrayRef(Fixed.Storage));
254 }
255
256 static StringRef input(StringRef Scalar, void *, FixedSizeHex<N> &Fixed) {
257 if (!all_of(Scalar, isHexDigit))
258 return "Invalid hex digit in input";
259 if (Scalar.size() < 2 * N)
260 return "String too short";
261 if (Scalar.size() > 2 * N)
262 return "String too long";
263 copy(fromHex(Scalar), Fixed.Storage);
264 return "";
265 }
266
267 static QuotingType mustQuote(StringRef S) { return QuotingType::None; }
268};
269} // namespace yaml
270} // namespace llvm
271void yaml::MappingTraits<CPUInfo::OtherInfo>::mapping(
272 IO &IO, CPUInfo::OtherInfo &Info) {
273 FixedSizeHex<sizeof(Info.ProcessorFeatures)> Features(Info.ProcessorFeatures);
274 IO.mapRequired("Features", Features);
275}
276
277namespace {
278/// A type which only accepts strings of a fixed size for yaml conversion.
279template <std::size_t N> struct FixedSizeString {
280 FixedSizeString(char (&Storage)[N]) : Storage(Storage) {}
281
282 char (&Storage)[N];
283};
284} // namespace
285
286namespace llvm {
287namespace yaml {
288template <std::size_t N> struct ScalarTraits<FixedSizeString<N>> {
289 static void output(const FixedSizeString<N> &Fixed, void *, raw_ostream &OS) {
290 OS << StringRef(Fixed.Storage, N);
291 }
292
293 static StringRef input(StringRef Scalar, void *, FixedSizeString<N> &Fixed) {
294 if (Scalar.size() < N)
295 return "String too short";
296 if (Scalar.size() > N)
297 return "String too long";
298 copy(Scalar, Fixed.Storage);
299 return "";
300 }
301
302 static QuotingType mustQuote(StringRef S) { return needsQuotes(S); }
303};
304} // namespace yaml
305} // namespace llvm
306
307void yaml::MappingTraits<CPUInfo::X86Info>::mapping(IO &IO,
308 CPUInfo::X86Info &Info) {
309 FixedSizeString<sizeof(Info.VendorID)> VendorID(Info.VendorID);
310 IO.mapRequired("Vendor ID", VendorID);
311
312 mapRequiredHex(IO, "Version Info", Info.VersionInfo);
313 mapRequiredHex(IO, "Feature Info", Info.FeatureInfo);
314 mapOptionalHex(IO, "AMD Extended Features", Info.AMDExtendedFeatures, 0);
315}
316
Pavel Labath7429d862019-04-18 14:57:31 +0000317void yaml::MappingTraits<VSFixedFileInfo>::mapping(IO &IO,
318 VSFixedFileInfo &Info) {
319 mapOptionalHex(IO, "Signature", Info.Signature, 0);
320 mapOptionalHex(IO, "Struct Version", Info.StructVersion, 0);
321 mapOptionalHex(IO, "File Version High", Info.FileVersionHigh, 0);
322 mapOptionalHex(IO, "File Version Low", Info.FileVersionLow, 0);
323 mapOptionalHex(IO, "Product Version High", Info.ProductVersionHigh, 0);
324 mapOptionalHex(IO, "Product Version Low", Info.ProductVersionLow, 0);
325 mapOptionalHex(IO, "File Flags Mask", Info.FileFlagsMask, 0);
326 mapOptionalHex(IO, "File Flags", Info.FileFlags, 0);
327 mapOptionalHex(IO, "File OS", Info.FileOS, 0);
328 mapOptionalHex(IO, "File Type", Info.FileType, 0);
329 mapOptionalHex(IO, "File Subtype", Info.FileSubtype, 0);
330 mapOptionalHex(IO, "File Date High", Info.FileDateHigh, 0);
331 mapOptionalHex(IO, "File Date Low", Info.FileDateLow, 0);
332}
333
Pavel Labathdcdb3c62019-05-09 15:13:53 +0000334void yaml::MappingTraits<ModuleListStream::entry_type>::mapping(
335 IO &IO, ModuleListStream::entry_type &M) {
336 mapRequiredHex(IO, "Base of Image", M.Entry.BaseOfImage);
337 mapRequiredHex(IO, "Size of Image", M.Entry.SizeOfImage);
338 mapOptionalHex(IO, "Checksum", M.Entry.Checksum, 0);
339 IO.mapOptional("Time Date Stamp", M.Entry.TimeDateStamp,
Pavel Labath7429d862019-04-18 14:57:31 +0000340 support::ulittle32_t(0));
341 IO.mapRequired("Module Name", M.Name);
Pavel Labathdcdb3c62019-05-09 15:13:53 +0000342 IO.mapOptional("Version Info", M.Entry.VersionInfo, VSFixedFileInfo());
Pavel Labath7429d862019-04-18 14:57:31 +0000343 IO.mapRequired("CodeView Record", M.CvRecord);
344 IO.mapOptional("Misc Record", M.MiscRecord, yaml::BinaryRef());
Pavel Labathdcdb3c62019-05-09 15:13:53 +0000345 mapOptionalHex(IO, "Reserved0", M.Entry.Reserved0, 0);
346 mapOptionalHex(IO, "Reserved1", M.Entry.Reserved1, 0);
Pavel Labath7429d862019-04-18 14:57:31 +0000347}
348
Pavel Labath69de7a92019-03-22 14:47:26 +0000349static void streamMapping(yaml::IO &IO, RawContentStream &Stream) {
350 IO.mapOptional("Content", Stream.Content);
351 IO.mapOptional("Size", Stream.Size, Stream.Content.binary_size());
352}
353
354static StringRef streamValidate(RawContentStream &Stream) {
355 if (Stream.Size.value < Stream.Content.binary_size())
356 return "Stream size must be greater or equal to the content size";
357 return "";
358}
359
Pavel Labath2d29e162019-05-16 15:17:30 +0000360void yaml::MappingTraits<MemoryListStream::entry_type>::mapping(
361 IO &IO, MemoryListStream::entry_type &Range) {
362 MappingContextTraits<MemoryDescriptor, yaml::BinaryRef>::mapping(
363 IO, Range.Entry, Range.Content);
364}
365
366static void streamMapping(yaml::IO &IO, MemoryListStream &Stream) {
367 IO.mapRequired("Memory Ranges", Stream.Entries);
368}
369
Pavel Labath7429d862019-04-18 14:57:31 +0000370static void streamMapping(yaml::IO &IO, ModuleListStream &Stream) {
Pavel Labathdcdb3c62019-05-09 15:13:53 +0000371 IO.mapRequired("Modules", Stream.Entries);
Pavel Labath7429d862019-04-18 14:57:31 +0000372}
373
Pavel Labath69de7a92019-03-22 14:47:26 +0000374static void streamMapping(yaml::IO &IO, SystemInfoStream &Stream) {
375 SystemInfo &Info = Stream.Info;
376 IO.mapRequired("Processor Arch", Info.ProcessorArch);
377 mapOptional(IO, "Processor Level", Info.ProcessorLevel, 0);
378 mapOptional(IO, "Processor Revision", Info.ProcessorRevision, 0);
379 IO.mapOptional("Number of Processors", Info.NumberOfProcessors, 0);
380 IO.mapOptional("Product type", Info.ProductType, 0);
381 mapOptional(IO, "Major Version", Info.MajorVersion, 0);
382 mapOptional(IO, "Minor Version", Info.MinorVersion, 0);
383 mapOptional(IO, "Build Number", Info.BuildNumber, 0);
384 IO.mapRequired("Platform ID", Info.PlatformId);
Pavel Labath51d9fa02019-04-05 08:06:26 +0000385 IO.mapOptional("CSD Version", Stream.CSDVersion, "");
Pavel Labath69de7a92019-03-22 14:47:26 +0000386 mapOptionalHex(IO, "Suite Mask", Info.SuiteMask, 0);
387 mapOptionalHex(IO, "Reserved", Info.Reserved, 0);
388 switch (static_cast<ProcessorArchitecture>(Info.ProcessorArch)) {
389 case ProcessorArchitecture::X86:
390 case ProcessorArchitecture::AMD64:
391 IO.mapOptional("CPU", Info.CPU.X86);
392 break;
393 case ProcessorArchitecture::ARM:
394 case ProcessorArchitecture::ARM64:
395 IO.mapOptional("CPU", Info.CPU.Arm);
396 break;
397 default:
398 IO.mapOptional("CPU", Info.CPU.Other);
399 break;
400 }
401}
402
403static void streamMapping(yaml::IO &IO, TextContentStream &Stream) {
404 IO.mapOptional("Text", Stream.Text);
405}
406
Pavel Labathdcdb3c62019-05-09 15:13:53 +0000407void yaml::MappingContextTraits<MemoryDescriptor, yaml::BinaryRef>::mapping(
408 IO &IO, MemoryDescriptor &Memory, BinaryRef &Content) {
409 mapRequiredHex(IO, "Start of Memory Range", Memory.StartOfMemoryRange);
410 IO.mapRequired("Content", Content);
411}
412
413void yaml::MappingTraits<ThreadListStream::entry_type>::mapping(
414 IO &IO, ThreadListStream::entry_type &T) {
415 mapRequiredHex(IO, "Thread Id", T.Entry.ThreadId);
416 mapOptionalHex(IO, "Suspend Count", T.Entry.SuspendCount, 0);
417 mapOptionalHex(IO, "Priority Class", T.Entry.PriorityClass, 0);
418 mapOptionalHex(IO, "Priority", T.Entry.Priority, 0);
419 mapOptionalHex(IO, "Environment Block", T.Entry.EnvironmentBlock, 0);
420 IO.mapRequired("Context", T.Context);
421 IO.mapRequired("Stack", T.Entry.Stack, T.Stack);
422}
423
424static void streamMapping(yaml::IO &IO, ThreadListStream &Stream) {
425 IO.mapRequired("Threads", Stream.Entries);
426}
427
Pavel Labath69de7a92019-03-22 14:47:26 +0000428void yaml::MappingTraits<std::unique_ptr<Stream>>::mapping(
429 yaml::IO &IO, std::unique_ptr<MinidumpYAML::Stream> &S) {
430 StreamType Type;
431 if (IO.outputting())
432 Type = S->Type;
433 IO.mapRequired("Type", Type);
434
435 if (!IO.outputting())
436 S = MinidumpYAML::Stream::create(Type);
437 switch (S->Kind) {
Pavel Labath2d29e162019-05-16 15:17:30 +0000438 case MinidumpYAML::Stream::StreamKind::MemoryList:
439 streamMapping(IO, llvm::cast<MemoryListStream>(*S));
440 break;
Pavel Labath7429d862019-04-18 14:57:31 +0000441 case MinidumpYAML::Stream::StreamKind::ModuleList:
442 streamMapping(IO, llvm::cast<ModuleListStream>(*S));
443 break;
Pavel Labath69de7a92019-03-22 14:47:26 +0000444 case MinidumpYAML::Stream::StreamKind::RawContent:
445 streamMapping(IO, llvm::cast<RawContentStream>(*S));
446 break;
447 case MinidumpYAML::Stream::StreamKind::SystemInfo:
448 streamMapping(IO, llvm::cast<SystemInfoStream>(*S));
449 break;
450 case MinidumpYAML::Stream::StreamKind::TextContent:
451 streamMapping(IO, llvm::cast<TextContentStream>(*S));
452 break;
Pavel Labathdcdb3c62019-05-09 15:13:53 +0000453 case MinidumpYAML::Stream::StreamKind::ThreadList:
454 streamMapping(IO, llvm::cast<ThreadListStream>(*S));
455 break;
Pavel Labath69de7a92019-03-22 14:47:26 +0000456 }
457}
458
459StringRef yaml::MappingTraits<std::unique_ptr<Stream>>::validate(
460 yaml::IO &IO, std::unique_ptr<MinidumpYAML::Stream> &S) {
461 switch (S->Kind) {
462 case MinidumpYAML::Stream::StreamKind::RawContent:
463 return streamValidate(cast<RawContentStream>(*S));
Pavel Labath2d29e162019-05-16 15:17:30 +0000464 case MinidumpYAML::Stream::StreamKind::MemoryList:
Pavel Labath7429d862019-04-18 14:57:31 +0000465 case MinidumpYAML::Stream::StreamKind::ModuleList:
Pavel Labath69de7a92019-03-22 14:47:26 +0000466 case MinidumpYAML::Stream::StreamKind::SystemInfo:
467 case MinidumpYAML::Stream::StreamKind::TextContent:
Pavel Labathdcdb3c62019-05-09 15:13:53 +0000468 case MinidumpYAML::Stream::StreamKind::ThreadList:
Pavel Labath69de7a92019-03-22 14:47:26 +0000469 return "";
470 }
471 llvm_unreachable("Fully covered switch above!");
472}
473
474void yaml::MappingTraits<Object>::mapping(IO &IO, Object &O) {
475 IO.mapTag("!minidump", true);
476 mapOptionalHex(IO, "Signature", O.Header.Signature, Header::MagicSignature);
477 mapOptionalHex(IO, "Version", O.Header.Version, Header::MagicVersion);
478 mapOptionalHex(IO, "Flags", O.Header.Flags, 0);
479 IO.mapRequired("Streams", O.Streams);
480}
481
Pavel Labathdcdb3c62019-05-09 15:13:53 +0000482static LocationDescriptor layout(BlobAllocator &File, yaml::BinaryRef Data) {
483 return {support::ulittle32_t(Data.binary_size()),
484 support::ulittle32_t(File.allocateBytes(Data))};
485}
486
Pavel Labath2d29e162019-05-16 15:17:30 +0000487static void layout(BlobAllocator &File, MemoryListStream::entry_type &Range) {
488 Range.Entry.Memory = layout(File, Range.Content);
489}
490
Pavel Labathdcdb3c62019-05-09 15:13:53 +0000491static void layout(BlobAllocator &File, ModuleListStream::entry_type &M) {
492 M.Entry.ModuleNameRVA = File.allocateString(M.Name);
493
494 M.Entry.CvRecord = layout(File, M.CvRecord);
495 M.Entry.MiscRecord = layout(File, M.MiscRecord);
496}
497
498static void layout(BlobAllocator &File, ThreadListStream::entry_type &T) {
499 T.Entry.Stack.Memory = layout(File, T.Stack);
500 T.Entry.Context = layout(File, T.Context);
501}
502
503template <typename EntryT>
504static size_t layout(BlobAllocator &File,
505 MinidumpYAML::detail::ListStream<EntryT> &S) {
506
507 File.allocateNewObject<support::ulittle32_t>(S.Entries.size());
508 for (auto &E : S.Entries)
509 File.allocateObject(E.Entry);
510
511 size_t DataEnd = File.tell();
512
513 // Lay out the auxiliary data, (which is not a part of the stream).
514 DataEnd = File.tell();
515 for (auto &E : S.Entries)
516 layout(File, E);
517
518 return DataEnd;
519}
520
Pavel Labath69de7a92019-03-22 14:47:26 +0000521static Directory layout(BlobAllocator &File, Stream &S) {
522 Directory Result;
523 Result.Type = S.Type;
524 Result.Location.RVA = File.tell();
Pavel Labath51d9fa02019-04-05 08:06:26 +0000525 Optional<size_t> DataEnd;
Pavel Labath69de7a92019-03-22 14:47:26 +0000526 switch (S.Kind) {
Pavel Labath2d29e162019-05-16 15:17:30 +0000527 case Stream::StreamKind::MemoryList:
528 DataEnd = layout(File, cast<MemoryListStream>(S));
529 break;
Pavel Labathdcdb3c62019-05-09 15:13:53 +0000530 case Stream::StreamKind::ModuleList:
531 DataEnd = layout(File, cast<ModuleListStream>(S));
Pavel Labath7429d862019-04-18 14:57:31 +0000532 break;
Pavel Labath69de7a92019-03-22 14:47:26 +0000533 case Stream::StreamKind::RawContent: {
534 RawContentStream &Raw = cast<RawContentStream>(S);
Pavel Labathc5f79052019-03-25 14:45:31 +0000535 File.allocateCallback(Raw.Size, [&Raw](raw_ostream &OS) {
Pavel Labath69de7a92019-03-22 14:47:26 +0000536 Raw.Content.writeAsBinary(OS);
537 assert(Raw.Content.binary_size() <= Raw.Size);
538 OS << std::string(Raw.Size - Raw.Content.binary_size(), '\0');
539 });
540 break;
541 }
Pavel Labath51d9fa02019-04-05 08:06:26 +0000542 case Stream::StreamKind::SystemInfo: {
543 SystemInfoStream &SystemInfo = cast<SystemInfoStream>(S);
544 File.allocateObject(SystemInfo.Info);
545 // The CSD string is not a part of the stream.
546 DataEnd = File.tell();
547 SystemInfo.Info.CSDVersionRVA = File.allocateString(SystemInfo.CSDVersion);
Pavel Labath69de7a92019-03-22 14:47:26 +0000548 break;
Pavel Labath51d9fa02019-04-05 08:06:26 +0000549 }
Pavel Labath69de7a92019-03-22 14:47:26 +0000550 case Stream::StreamKind::TextContent:
Pavel Labathc5f79052019-03-25 14:45:31 +0000551 File.allocateArray(arrayRefFromStringRef(cast<TextContentStream>(S).Text));
Pavel Labath69de7a92019-03-22 14:47:26 +0000552 break;
Pavel Labathdcdb3c62019-05-09 15:13:53 +0000553 case Stream::StreamKind::ThreadList:
554 DataEnd = layout(File, cast<ThreadListStream>(S));
555 break;
Pavel Labath69de7a92019-03-22 14:47:26 +0000556 }
Pavel Labath51d9fa02019-04-05 08:06:26 +0000557 // If DataEnd is not set, we assume everything we generated is a part of the
558 // stream.
559 Result.Location.DataSize =
560 DataEnd.getValueOr(File.tell()) - Result.Location.RVA;
Pavel Labath69de7a92019-03-22 14:47:26 +0000561 return Result;
562}
563
564void MinidumpYAML::writeAsBinary(Object &Obj, raw_ostream &OS) {
565 BlobAllocator File;
Pavel Labathc5f79052019-03-25 14:45:31 +0000566 File.allocateObject(Obj.Header);
Pavel Labath69de7a92019-03-22 14:47:26 +0000567
568 std::vector<Directory> StreamDirectory(Obj.Streams.size());
569 Obj.Header.StreamDirectoryRVA =
Pavel Labathc5f79052019-03-25 14:45:31 +0000570 File.allocateArray(makeArrayRef(StreamDirectory));
Pavel Labath69de7a92019-03-22 14:47:26 +0000571 Obj.Header.NumberOfStreams = StreamDirectory.size();
572
573 for (auto &Stream : enumerate(Obj.Streams))
574 StreamDirectory[Stream.index()] = layout(File, *Stream.value());
575
576 File.writeTo(OS);
577}
578
579Error MinidumpYAML::writeAsBinary(StringRef Yaml, raw_ostream &OS) {
580 yaml::Input Input(Yaml);
581 Object Obj;
582 Input >> Obj;
583 if (std::error_code EC = Input.error())
584 return errorCodeToError(EC);
585
586 writeAsBinary(Obj, OS);
587 return Error::success();
588}
Pavel Labath3cee6632019-04-02 11:58:37 +0000589
590Expected<std::unique_ptr<Stream>>
591Stream::create(const Directory &StreamDesc, const object::MinidumpFile &File) {
592 StreamKind Kind = getKind(StreamDesc.Type);
593 switch (Kind) {
Pavel Labath2d29e162019-05-16 15:17:30 +0000594 case StreamKind::MemoryList: {
595 auto ExpectedList = File.getMemoryList();
596 if (!ExpectedList)
597 return ExpectedList.takeError();
598 std::vector<MemoryListStream::entry_type> Ranges;
599 for (const MemoryDescriptor &MD : *ExpectedList) {
600 auto ExpectedContent = File.getRawData(MD.Memory);
601 if (!ExpectedContent)
602 return ExpectedContent.takeError();
603 Ranges.push_back({MD, *ExpectedContent});
604 }
605 return llvm::make_unique<MemoryListStream>(std::move(Ranges));
606 }
Pavel Labath7429d862019-04-18 14:57:31 +0000607 case StreamKind::ModuleList: {
608 auto ExpectedList = File.getModuleList();
609 if (!ExpectedList)
610 return ExpectedList.takeError();
Pavel Labathdcdb3c62019-05-09 15:13:53 +0000611 std::vector<ModuleListStream::entry_type> Modules;
Pavel Labath7429d862019-04-18 14:57:31 +0000612 for (const Module &M : *ExpectedList) {
613 auto ExpectedName = File.getString(M.ModuleNameRVA);
614 if (!ExpectedName)
615 return ExpectedName.takeError();
616 auto ExpectedCv = File.getRawData(M.CvRecord);
617 if (!ExpectedCv)
618 return ExpectedCv.takeError();
619 auto ExpectedMisc = File.getRawData(M.MiscRecord);
620 if (!ExpectedMisc)
621 return ExpectedMisc.takeError();
622 Modules.push_back(
623 {M, std::move(*ExpectedName), *ExpectedCv, *ExpectedMisc});
624 }
Benjamin Kramer70857952019-04-18 15:06:03 +0000625 return llvm::make_unique<ModuleListStream>(std::move(Modules));
Pavel Labath7429d862019-04-18 14:57:31 +0000626 }
Pavel Labath3cee6632019-04-02 11:58:37 +0000627 case StreamKind::RawContent:
Pavel Labath4a6dd1e2019-04-05 08:26:58 +0000628 return llvm::make_unique<RawContentStream>(StreamDesc.Type,
629 File.getRawStream(StreamDesc));
Pavel Labath3cee6632019-04-02 11:58:37 +0000630 case StreamKind::SystemInfo: {
631 auto ExpectedInfo = File.getSystemInfo();
632 if (!ExpectedInfo)
633 return ExpectedInfo.takeError();
Pavel Labath51d9fa02019-04-05 08:06:26 +0000634 auto ExpectedCSDVersion = File.getString(ExpectedInfo->CSDVersionRVA);
635 if (!ExpectedCSDVersion)
636 return ExpectedInfo.takeError();
Pavel Labath4a6dd1e2019-04-05 08:26:58 +0000637 return llvm::make_unique<SystemInfoStream>(*ExpectedInfo,
638 std::move(*ExpectedCSDVersion));
Pavel Labath3cee6632019-04-02 11:58:37 +0000639 }
640 case StreamKind::TextContent:
Pavel Labath4a6dd1e2019-04-05 08:26:58 +0000641 return llvm::make_unique<TextContentStream>(
Pavel Labath3cee6632019-04-02 11:58:37 +0000642 StreamDesc.Type, toStringRef(File.getRawStream(StreamDesc)));
Pavel Labathdcdb3c62019-05-09 15:13:53 +0000643 case StreamKind::ThreadList: {
644 auto ExpectedList = File.getThreadList();
645 if (!ExpectedList)
646 return ExpectedList.takeError();
647 std::vector<ThreadListStream::entry_type> Threads;
648 for (const Thread &T : *ExpectedList) {
649 auto ExpectedStack = File.getRawData(T.Stack.Memory);
650 if (!ExpectedStack)
651 return ExpectedStack.takeError();
652 auto ExpectedContext = File.getRawData(T.Context);
653 if (!ExpectedContext)
654 return ExpectedContext.takeError();
655 Threads.push_back({T, *ExpectedStack, *ExpectedContext});
656 }
657 return llvm::make_unique<ThreadListStream>(std::move(Threads));
658 }
Pavel Labath3cee6632019-04-02 11:58:37 +0000659 }
660 llvm_unreachable("Unhandled stream kind!");
661}
662
663Expected<Object> Object::create(const object::MinidumpFile &File) {
664 std::vector<std::unique_ptr<Stream>> Streams;
665 Streams.reserve(File.streams().size());
666 for (const Directory &StreamDesc : File.streams()) {
667 auto ExpectedStream = Stream::create(StreamDesc, File);
668 if (!ExpectedStream)
669 return ExpectedStream.takeError();
670 Streams.push_back(std::move(*ExpectedStream));
671 }
672 return Object(File.header(), std::move(Streams));
673}