blob: 4f28f3f754c1ffaf27522e73d8759def033793c4 [file] [log] [blame]
Dean Michael Berris14cdbca2017-01-11 06:39:09 +00001//===- Trace.cpp - XRay Trace Loading implementation. ---------------------===//
Dean Michael Berris6e1f0662017-01-10 02:38:11 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// XRay log reader implementation.
11//
12//===----------------------------------------------------------------------===//
Dean Michael Berris14cdbca2017-01-11 06:39:09 +000013#include "llvm/XRay/Trace.h"
14#include "llvm/ADT/STLExtras.h"
Dean Michael Berris6e1f0662017-01-10 02:38:11 +000015#include "llvm/Support/DataExtractor.h"
Dean Michael Berris14cdbca2017-01-11 06:39:09 +000016#include "llvm/Support/Error.h"
Dean Michael Berris6e1f0662017-01-10 02:38:11 +000017#include "llvm/Support/FileSystem.h"
Dean Michael Berris935f1172018-09-11 06:45:59 +000018#include "llvm/XRay/BlockIndexer.h"
19#include "llvm/XRay/BlockVerifier.h"
20#include "llvm/XRay/FDRRecordConsumer.h"
21#include "llvm/XRay/FDRRecordProducer.h"
22#include "llvm/XRay/FDRRecords.h"
23#include "llvm/XRay/FDRTraceExpander.h"
Dean Michael Berrisb1801712018-08-22 07:37:55 +000024#include "llvm/XRay/FileHeaderReader.h"
Dean Michael Berris14cdbca2017-01-11 06:39:09 +000025#include "llvm/XRay/YAMLXRayRecord.h"
Dean Michael Berris935f1172018-09-11 06:45:59 +000026#include <memory>
27#include <vector>
Dean Michael Berris6e1f0662017-01-10 02:38:11 +000028
29using namespace llvm;
30using namespace llvm::xray;
31using llvm::yaml::Input;
32
Benjamin Kramerc7732762017-08-20 13:03:48 +000033namespace {
Dean Michael Berris14cdbca2017-01-11 06:39:09 +000034using XRayRecordStorage =
35 std::aligned_storage<sizeof(XRayRecord), alignof(XRayRecord)>::type;
Dean Michael Berris6e1f0662017-01-10 02:38:11 +000036
Dean Michael Berrisc4a2b8b2018-08-31 17:06:28 +000037Error loadNaiveFormatLog(StringRef Data, bool IsLittleEndian,
38 XRayFileHeader &FileHeader,
Dean Michael Berris73d4ebd2017-02-17 01:47:16 +000039 std::vector<XRayRecord> &Records) {
Dean Michael Berris73d4ebd2017-02-17 01:47:16 +000040 if (Data.size() < 32)
41 return make_error<StringError>(
42 "Not enough bytes for an XRay log.",
43 std::make_error_code(std::errc::invalid_argument));
44
45 if (Data.size() - 32 == 0 || Data.size() % 32 != 0)
46 return make_error<StringError>(
47 "Invalid-sized XRay data.",
48 std::make_error_code(std::errc::invalid_argument));
49
Dean Michael Berrisc4a2b8b2018-08-31 17:06:28 +000050 DataExtractor Reader(Data, IsLittleEndian, 8);
Dean Michael Berris300176c2018-08-07 04:42:39 +000051 uint32_t OffsetPtr = 0;
Dean Michael Berrisb1801712018-08-22 07:37:55 +000052 auto FileHeaderOrError = readBinaryFormatHeader(Reader, OffsetPtr);
53 if (!FileHeaderOrError)
54 return FileHeaderOrError.takeError();
55 FileHeader = std::move(FileHeaderOrError.get());
Dean Michael Berris6e1f0662017-01-10 02:38:11 +000056
57 // Each record after the header will be 32 bytes, in the following format:
58 //
59 // (2) uint16 : record type
60 // (1) uint8 : cpu id
61 // (1) uint8 : type
62 // (4) sint32 : function id
63 // (8) uint64 : tsc
64 // (4) uint32 : thread id
Dean Michael Berris93177462018-07-13 05:38:22 +000065 // (4) uint32 : process id
66 // (8) - : padding
Dean Michael Berris300176c2018-08-07 04:42:39 +000067 while (Reader.isValidOffset(OffsetPtr)) {
68 if (!Reader.isValidOffsetForDataOfSize(OffsetPtr, 32))
69 return createStringError(
70 std::make_error_code(std::errc::executable_format_error),
71 "Not enough bytes to read a full record at offset %d.", OffsetPtr);
72 auto PreReadOffset = OffsetPtr;
73 auto RecordType = Reader.getU16(&OffsetPtr);
74 if (OffsetPtr == PreReadOffset)
75 return createStringError(
76 std::make_error_code(std::errc::executable_format_error),
77 "Failed reading record type at offset %d.", OffsetPtr);
78
79 switch (RecordType) {
Dean Michael Berris728dd9a2017-10-05 05:18:17 +000080 case 0: { // Normal records.
81 Records.emplace_back();
82 auto &Record = Records.back();
83 Record.RecordType = RecordType;
Dean Michael Berris300176c2018-08-07 04:42:39 +000084
85 PreReadOffset = OffsetPtr;
86 Record.CPU = Reader.getU8(&OffsetPtr);
87 if (OffsetPtr == PreReadOffset)
88 return createStringError(
89 std::make_error_code(std::errc::executable_format_error),
90 "Failed reading CPU field at offset %d.", OffsetPtr);
91
92 PreReadOffset = OffsetPtr;
93 auto Type = Reader.getU8(&OffsetPtr);
94 if (OffsetPtr == PreReadOffset)
95 return createStringError(
96 std::make_error_code(std::errc::executable_format_error),
97 "Failed reading record type field at offset %d.", OffsetPtr);
98
Dean Michael Berris728dd9a2017-10-05 05:18:17 +000099 switch (Type) {
100 case 0:
101 Record.Type = RecordTypes::ENTER;
102 break;
103 case 1:
104 Record.Type = RecordTypes::EXIT;
105 break;
106 case 2:
107 Record.Type = RecordTypes::TAIL_EXIT;
108 break;
109 case 3:
110 Record.Type = RecordTypes::ENTER_ARG;
111 break;
112 default:
Dean Michael Berris300176c2018-08-07 04:42:39 +0000113 return createStringError(
114 std::make_error_code(std::errc::executable_format_error),
115 "Unknown record type '%d' at offset %d.", Type, OffsetPtr);
Dean Michael Berris728dd9a2017-10-05 05:18:17 +0000116 }
Dean Michael Berris300176c2018-08-07 04:42:39 +0000117
118 PreReadOffset = OffsetPtr;
119 Record.FuncId = Reader.getSigned(&OffsetPtr, sizeof(int32_t));
120 if (OffsetPtr == PreReadOffset)
121 return createStringError(
122 std::make_error_code(std::errc::executable_format_error),
123 "Failed reading function id field at offset %d.", OffsetPtr);
124
125 PreReadOffset = OffsetPtr;
126 Record.TSC = Reader.getU64(&OffsetPtr);
127 if (OffsetPtr == PreReadOffset)
128 return createStringError(
129 std::make_error_code(std::errc::executable_format_error),
130 "Failed reading TSC field at offset %d.", OffsetPtr);
131
132 PreReadOffset = OffsetPtr;
133 Record.TId = Reader.getU32(&OffsetPtr);
134 if (OffsetPtr == PreReadOffset)
135 return createStringError(
136 std::make_error_code(std::errc::executable_format_error),
137 "Failed reading thread id field at offset %d.", OffsetPtr);
138
139 PreReadOffset = OffsetPtr;
140 Record.PId = Reader.getU32(&OffsetPtr);
141 if (OffsetPtr == PreReadOffset)
142 return createStringError(
143 std::make_error_code(std::errc::executable_format_error),
144 "Failed reading process id at offset %d.", OffsetPtr);
145
Dean Michael Berris6e1f0662017-01-10 02:38:11 +0000146 break;
Dean Michael Berris728dd9a2017-10-05 05:18:17 +0000147 }
148 case 1: { // Arg payload record.
149 auto &Record = Records.back();
Dean Michael Berris300176c2018-08-07 04:42:39 +0000150
151 // We skip the next two bytes of the record, because we don't need the
152 // type and the CPU record for arg payloads.
Dean Michael Berris728dd9a2017-10-05 05:18:17 +0000153 OffsetPtr += 2;
Dean Michael Berris300176c2018-08-07 04:42:39 +0000154 PreReadOffset = OffsetPtr;
155 int32_t FuncId = Reader.getSigned(&OffsetPtr, sizeof(int32_t));
156 if (OffsetPtr == PreReadOffset)
157 return createStringError(
158 std::make_error_code(std::errc::executable_format_error),
159 "Failed reading function id field at offset %d.", OffsetPtr);
160
161 PreReadOffset = OffsetPtr;
162 auto TId = Reader.getU32(&OffsetPtr);
163 if (OffsetPtr == PreReadOffset)
164 return createStringError(
165 std::make_error_code(std::errc::executable_format_error),
166 "Failed reading thread id field at offset %d.", OffsetPtr);
167
168 PreReadOffset = OffsetPtr;
169 auto PId = Reader.getU32(&OffsetPtr);
170 if (OffsetPtr == PreReadOffset)
171 return createStringError(
172 std::make_error_code(std::errc::executable_format_error),
173 "Failed reading process id field at offset %d.", OffsetPtr);
Dean Michael Berris93177462018-07-13 05:38:22 +0000174
175 // Make a check for versions above 3 for the Pid field
176 if (Record.FuncId != FuncId || Record.TId != TId ||
177 (FileHeader.Version >= 3 ? Record.PId != PId : false))
Dean Michael Berris300176c2018-08-07 04:42:39 +0000178 return createStringError(
179 std::make_error_code(std::errc::executable_format_error),
180 "Corrupted log, found arg payload following non-matching "
181 "function+thread record. Record for function %d != %d at offset "
182 "%d",
183 Record.FuncId, FuncId, OffsetPtr);
Dean Michael Berris93177462018-07-13 05:38:22 +0000184
Dean Michael Berris300176c2018-08-07 04:42:39 +0000185 PreReadOffset = OffsetPtr;
186 auto Arg = Reader.getU64(&OffsetPtr);
187 if (OffsetPtr == PreReadOffset)
188 return createStringError(
189 std::make_error_code(std::errc::executable_format_error),
190 "Failed reading argument payload at offset %d.", OffsetPtr);
191
Dean Michael Berris728dd9a2017-10-05 05:18:17 +0000192 Record.CallArgs.push_back(Arg);
Dean Michael Berris6e1f0662017-01-10 02:38:11 +0000193 break;
Dean Michael Berris728dd9a2017-10-05 05:18:17 +0000194 }
Dean Michael Berris6e1f0662017-01-10 02:38:11 +0000195 default:
Dean Michael Berris300176c2018-08-07 04:42:39 +0000196 return createStringError(
197 std::make_error_code(std::errc::executable_format_error),
198 "Unknown record type '%d' at offset %d.", RecordType, OffsetPtr);
Dean Michael Berris6e1f0662017-01-10 02:38:11 +0000199 }
Dean Michael Berris300176c2018-08-07 04:42:39 +0000200 // Advance the offset pointer enough bytes to align to 32-byte records for
201 // basic mode logs.
202 OffsetPtr += 8;
Dean Michael Berris6e1f0662017-01-10 02:38:11 +0000203 }
204 return Error::success();
205}
206
Dean Michael Berris73d4ebd2017-02-17 01:47:16 +0000207/// Reads a log in FDR mode for version 1 of this binary format. FDR mode is
208/// defined as part of the compiler-rt project in xray_fdr_logging.h, and such
209/// a log consists of the familiar 32 bit XRayHeader, followed by sequences of
210/// of interspersed 16 byte Metadata Records and 8 byte Function Records.
211///
212/// The following is an attempt to document the grammar of the format, which is
213/// parsed by this function for little-endian machines. Since the format makes
Martin Pelikan4869e6a52017-09-15 04:22:16 +0000214/// use of BitFields, when we support big-endian architectures, we will need to
Simon Pilgrim428e6932017-03-30 12:59:53 +0000215/// adjust not only the endianness parameter to llvm's RecordExtractor, but also
Dean Michael Berris73d4ebd2017-02-17 01:47:16 +0000216/// the bit twiddling logic, which is consistent with the little-endian
217/// convention that BitFields within a struct will first be packed into the
218/// least significant bits the address they belong to.
219///
Dean Michael Berrisc9e07ab2017-11-21 07:16:57 +0000220/// We expect a format complying with the grammar in the following pseudo-EBNF
221/// in Version 1 of the FDR log.
Dean Michael Berris73d4ebd2017-02-17 01:47:16 +0000222///
223/// FDRLog: XRayFileHeader ThreadBuffer*
Keith Wyss630a4ac2017-08-02 21:47:27 +0000224/// XRayFileHeader: 32 bytes to identify the log as FDR with machine metadata.
225/// Includes BufferSize
226/// ThreadBuffer: NewBuffer WallClockTime NewCPUId FunctionSequence EOB
Dean Michael Berrisf5e37622017-03-29 06:10:12 +0000227/// BufSize: 8 byte unsigned integer indicating how large the buffer is.
Dean Michael Berris73d4ebd2017-02-17 01:47:16 +0000228/// NewBuffer: 16 byte metadata record with Thread Id.
229/// WallClockTime: 16 byte metadata record with human readable time.
Dean Michael Berris93177462018-07-13 05:38:22 +0000230/// Pid: 16 byte metadata record with Pid
Dean Michael Berris73d4ebd2017-02-17 01:47:16 +0000231/// NewCPUId: 16 byte metadata record with CPUId and a 64 bit TSC reading.
Dean Michael Berrisf5e37622017-03-29 06:10:12 +0000232/// EOB: 16 byte record in a thread buffer plus mem garbage to fill BufSize.
Dean Michael Berris73d4ebd2017-02-17 01:47:16 +0000233/// FunctionSequence: NewCPUId | TSCWrap | FunctionRecord
234/// TSCWrap: 16 byte metadata record with a full 64 bit TSC reading.
235/// FunctionRecord: 8 byte record with FunctionId, entry/exit, and TSC delta.
Dean Michael Berrisc9e07ab2017-11-21 07:16:57 +0000236///
237/// In Version 2, we make the following changes:
238///
239/// ThreadBuffer: BufferExtents NewBuffer WallClockTime NewCPUId
240/// FunctionSequence
241/// BufferExtents: 16 byte metdata record describing how many usable bytes are
242/// in the buffer. This is measured from the start of the buffer
243/// and must always be at least 48 (bytes).
Dean Michael Berris93177462018-07-13 05:38:22 +0000244///
245/// In Version 3, we make the following changes:
246///
247/// ThreadBuffer: BufferExtents NewBuffer WallClockTime Pid NewCPUId
248/// FunctionSequence
Dean Michael Berrisc9e07ab2017-11-21 07:16:57 +0000249/// EOB: *deprecated*
Dean Michael Berris8fa10fd2018-11-07 04:37:42 +0000250///
251/// In Version 4, we make the following changes:
252///
253/// CustomEventRecord now includes the CPU data.
254///
255/// In Version 5, we make the following changes:
256///
257/// CustomEventRecord and TypedEventRecord now use TSC delta encoding similar to
258/// what FunctionRecord instances use, and we no longer need to include the CPU
259/// id in the CustomEventRecord.
260///
Dean Michael Berrisc4a2b8b2018-08-31 17:06:28 +0000261Error loadFDRLog(StringRef Data, bool IsLittleEndian,
262 XRayFileHeader &FileHeader, std::vector<XRayRecord> &Records) {
Dean Michael Berrisb1801712018-08-22 07:37:55 +0000263
Dean Michael Berris73d4ebd2017-02-17 01:47:16 +0000264 if (Data.size() < 32)
Dean Michael Berris935f1172018-09-11 06:45:59 +0000265 return createStringError(std::make_error_code(std::errc::invalid_argument),
266 "Not enough bytes for an XRay FDR log.");
267 DataExtractor DE(Data, IsLittleEndian, 8);
Dean Michael Berris73d4ebd2017-02-17 01:47:16 +0000268
Dean Michael Berris300176c2018-08-07 04:42:39 +0000269 uint32_t OffsetPtr = 0;
Dean Michael Berris935f1172018-09-11 06:45:59 +0000270 auto FileHeaderOrError = readBinaryFormatHeader(DE, OffsetPtr);
Dean Michael Berrisb1801712018-08-22 07:37:55 +0000271 if (!FileHeaderOrError)
272 return FileHeaderOrError.takeError();
273 FileHeader = std::move(FileHeaderOrError.get());
Dean Michael Berris73d4ebd2017-02-17 01:47:16 +0000274
Dean Michael Berris935f1172018-09-11 06:45:59 +0000275 // First we load the records into memory.
276 std::vector<std::unique_ptr<Record>> FDRRecords;
277
Dean Michael Berrisf5e37622017-03-29 06:10:12 +0000278 {
Dean Michael Berris935f1172018-09-11 06:45:59 +0000279 FileBasedRecordProducer P(FileHeader, DE, OffsetPtr);
280 LogBuilderConsumer C(FDRRecords);
281 while (DE.isValidOffsetForDataOfSize(OffsetPtr, 1)) {
282 auto R = P.produce();
283 if (!R)
284 return R.takeError();
285 if (auto E = C.consume(std::move(R.get())))
Dean Michael Berris73d4ebd2017-02-17 01:47:16 +0000286 return E;
287 }
Dean Michael Berris935f1172018-09-11 06:45:59 +0000288 }
Dean Michael Berrisc9e07ab2017-11-21 07:16:57 +0000289
Dean Michael Berris935f1172018-09-11 06:45:59 +0000290 // Next we index the records into blocks.
291 BlockIndexer::Index Index;
292 {
293 BlockIndexer Indexer(Index);
294 for (auto &R : FDRRecords)
295 if (auto E = R->apply(Indexer))
296 return E;
297 if (auto E = Indexer.flush())
298 return E;
299 }
Dean Michael Berris300176c2018-08-07 04:42:39 +0000300
Dean Michael Berris935f1172018-09-11 06:45:59 +0000301 // Then we verify the consistency of the blocks.
302 {
Dean Michael Berris935f1172018-09-11 06:45:59 +0000303 for (auto &PTB : Index) {
304 auto &Blocks = PTB.second;
305 for (auto &B : Blocks) {
Dean Michael Berris1468b7d2018-09-13 09:25:42 +0000306 BlockVerifier Verifier;
Dean Michael Berris935f1172018-09-11 06:45:59 +0000307 for (auto *R : B.Records)
308 if (auto E = R->apply(Verifier))
309 return E;
310 if (auto E = Verifier.verify())
311 return E;
Dean Michael Berris935f1172018-09-11 06:45:59 +0000312 }
Dean Michael Berrisc9e07ab2017-11-21 07:16:57 +0000313 }
Dean Michael Berris73d4ebd2017-02-17 01:47:16 +0000314 }
Martin Pelikan4869e6a52017-09-15 04:22:16 +0000315
Dean Michael Berris935f1172018-09-11 06:45:59 +0000316 // This is now the meat of the algorithm. Here we sort the blocks according to
317 // the Walltime record in each of the blocks for the same thread. This allows
318 // us to more consistently recreate the execution trace in temporal order.
319 // After the sort, we then reconstitute `Trace` records using a stateful
320 // visitor associated with a single process+thread pair.
321 {
322 for (auto &PTB : Index) {
323 auto &Blocks = PTB.second;
Dean Michael Berris3fe1b122018-11-01 00:18:52 +0000324 llvm::sort(Blocks, [](const BlockIndexer::Block &L,
325 const BlockIndexer::Block &R) {
326 return (L.WallclockTime->seconds() < R.WallclockTime->seconds() &&
327 L.WallclockTime->nanos() < R.WallclockTime->nanos());
328 });
Dean Michael Berrised1752d2018-09-11 08:03:30 +0000329 auto Adder = [&](const XRayRecord &R) { Records.push_back(R); };
330 TraceExpander Expander(Adder, FileHeader.Version);
Dean Michael Berris935f1172018-09-11 06:45:59 +0000331 for (auto &B : Blocks) {
332 for (auto *R : B.Records)
333 if (auto E = R->apply(Expander))
334 return E;
335 }
336 if (auto E = Expander.flush())
337 return E;
338 }
339 }
Dean Michael Berris73d4ebd2017-02-17 01:47:16 +0000340
341 return Error::success();
342}
343
344Error loadYAMLLog(StringRef Data, XRayFileHeader &FileHeader,
345 std::vector<XRayRecord> &Records) {
Dean Michael Berris6e1f0662017-01-10 02:38:11 +0000346 YAMLXRayTrace Trace;
347 Input In(Data);
348 In >> Trace;
349 if (In.error())
350 return make_error<StringError>("Failed loading YAML Data.", In.error());
351
352 FileHeader.Version = Trace.Header.Version;
353 FileHeader.Type = Trace.Header.Type;
354 FileHeader.ConstantTSC = Trace.Header.ConstantTSC;
355 FileHeader.NonstopTSC = Trace.Header.NonstopTSC;
356 FileHeader.CycleFrequency = Trace.Header.CycleFrequency;
357
358 if (FileHeader.Version != 1)
359 return make_error<StringError>(
360 Twine("Unsupported XRay file version: ") + Twine(FileHeader.Version),
361 std::make_error_code(std::errc::invalid_argument));
362
363 Records.clear();
364 std::transform(Trace.Records.begin(), Trace.Records.end(),
365 std::back_inserter(Records), [&](const YAMLXRayRecord &R) {
Dean Michael Berrisb92b0b82018-11-06 08:51:37 +0000366 return XRayRecord{R.RecordType, R.CPU, R.Type,
367 R.FuncId, R.TSC, R.TId,
368 R.PId, R.CallArgs, R.Data};
Dean Michael Berris6e1f0662017-01-10 02:38:11 +0000369 });
370 return Error::success();
371}
Benjamin Kramerc7732762017-08-20 13:03:48 +0000372} // namespace
Dean Michael Berris14cdbca2017-01-11 06:39:09 +0000373
374Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
375 int Fd;
376 if (auto EC = sys::fs::openFileForRead(Filename, Fd)) {
377 return make_error<StringError>(
378 Twine("Cannot read log from '") + Filename + "'", EC);
379 }
380
Dean Michael Berris14cdbca2017-01-11 06:39:09 +0000381 uint64_t FileSize;
382 if (auto EC = sys::fs::file_size(Filename, FileSize)) {
383 return make_error<StringError>(
384 Twine("Cannot read log from '") + Filename + "'", EC);
385 }
386 if (FileSize < 4) {
387 return make_error<StringError>(
388 Twine("File '") + Filename + "' too small for XRay.",
Hans Wennborgfa3329a2017-01-12 18:33:14 +0000389 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris14cdbca2017-01-11 06:39:09 +0000390 }
391
Martin Pelikan4869e6a52017-09-15 04:22:16 +0000392 // Map the opened file into memory and use a StringRef to access it later.
Dean Michael Berris14cdbca2017-01-11 06:39:09 +0000393 std::error_code EC;
394 sys::fs::mapped_file_region MappedFile(
395 Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
396 if (EC) {
397 return make_error<StringError>(
398 Twine("Cannot read log from '") + Filename + "'", EC);
399 }
Martin Pelikan4869e6a52017-09-15 04:22:16 +0000400 auto Data = StringRef(MappedFile.data(), MappedFile.size());
Dean Michael Berrisc4a2b8b2018-08-31 17:06:28 +0000401
402 // TODO: Lift the endianness and implementation selection here.
403 DataExtractor LittleEndianDE(Data, true, 8);
404 auto TraceOrError = loadTrace(LittleEndianDE, Sort);
405 if (!TraceOrError) {
406 DataExtractor BigEndianDE(Data, false, 8);
407 TraceOrError = loadTrace(BigEndianDE, Sort);
408 }
409 return TraceOrError;
Dean Michael Berris3327f8b2018-08-24 10:30:37 +0000410}
Dean Michael Berris14cdbca2017-01-11 06:39:09 +0000411
Dean Michael Berris3327f8b2018-08-24 10:30:37 +0000412Expected<Trace> llvm::xray::loadTrace(const DataExtractor &DE, bool Sort) {
Dean Michael Berris14cdbca2017-01-11 06:39:09 +0000413 // Attempt to detect the file type using file magic. We have a slight bias
414 // towards the binary format, and we do this by making sure that the first 4
415 // bytes of the binary file is some combination of the following byte
Martin Pelikan4869e6a52017-09-15 04:22:16 +0000416 // patterns: (observe the code loading them assumes they're little endian)
Dean Michael Berris14cdbca2017-01-11 06:39:09 +0000417 //
Martin Pelikan4869e6a52017-09-15 04:22:16 +0000418 // 0x01 0x00 0x00 0x00 - version 1, "naive" format
419 // 0x01 0x00 0x01 0x00 - version 1, "flight data recorder" format
Dean Michael Berrisc9e07ab2017-11-21 07:16:57 +0000420 // 0x02 0x00 0x01 0x00 - version 2, "flight data recorder" format
Dean Michael Berris14cdbca2017-01-11 06:39:09 +0000421 //
Martin Pelikan4869e6a52017-09-15 04:22:16 +0000422 // YAML files don't typically have those first four bytes as valid text so we
Dean Michael Berris14cdbca2017-01-11 06:39:09 +0000423 // try loading assuming YAML if we don't find these bytes.
424 //
425 // Only if we can't load either the binary or the YAML format will we yield an
426 // error.
Dean Michael Berrisc4a2b8b2018-08-31 17:06:28 +0000427 DataExtractor HeaderExtractor(DE.getData(), DE.isLittleEndian(), 8);
Dean Michael Berris14cdbca2017-01-11 06:39:09 +0000428 uint32_t OffsetPtr = 0;
429 uint16_t Version = HeaderExtractor.getU16(&OffsetPtr);
430 uint16_t Type = HeaderExtractor.getU16(&OffsetPtr);
431
Dean Michael Berris73d4ebd2017-02-17 01:47:16 +0000432 enum BinaryFormatType { NAIVE_FORMAT = 0, FLIGHT_DATA_RECORDER_FORMAT = 1 };
433
Dean Michael Berris14cdbca2017-01-11 06:39:09 +0000434 Trace T;
Dean Michael Berrisc9e07ab2017-11-21 07:16:57 +0000435 switch (Type) {
436 case NAIVE_FORMAT:
Dean Michael Berris93177462018-07-13 05:38:22 +0000437 if (Version == 1 || Version == 2 || Version == 3) {
Dean Michael Berrisc4a2b8b2018-08-31 17:06:28 +0000438 if (auto E = loadNaiveFormatLog(DE.getData(), DE.isLittleEndian(),
439 T.FileHeader, T.Records))
Dean Michael Berrisc9e07ab2017-11-21 07:16:57 +0000440 return std::move(E);
441 } else {
442 return make_error<StringError>(
443 Twine("Unsupported version for Basic/Naive Mode logging: ") +
444 Twine(Version),
445 std::make_error_code(std::errc::executable_format_error));
446 }
447 break;
448 case FLIGHT_DATA_RECORDER_FORMAT:
Dean Michael Berris8fa10fd2018-11-07 04:37:42 +0000449 if (Version >= 1 && Version <= 5) {
Dean Michael Berrisc4a2b8b2018-08-31 17:06:28 +0000450 if (auto E = loadFDRLog(DE.getData(), DE.isLittleEndian(), T.FileHeader,
451 T.Records))
Dean Michael Berrisc9e07ab2017-11-21 07:16:57 +0000452 return std::move(E);
453 } else {
454 return make_error<StringError>(
455 Twine("Unsupported version for FDR Mode logging: ") + Twine(Version),
456 std::make_error_code(std::errc::executable_format_error));
457 }
458 break;
459 default:
Dean Michael Berris3327f8b2018-08-24 10:30:37 +0000460 if (auto E = loadYAMLLog(DE.getData(), T.FileHeader, T.Records))
Dean Michael Berris14cdbca2017-01-11 06:39:09 +0000461 return std::move(E);
462 }
463
464 if (Sort)
Mandeep Singh Grang952372c2017-11-14 18:11:08 +0000465 std::stable_sort(T.Records.begin(), T.Records.end(),
Dean Michael Berris300176c2018-08-07 04:42:39 +0000466 [&](const XRayRecord &L, const XRayRecord &R) {
467 return L.TSC < R.TSC;
468 });
Dean Michael Berris14cdbca2017-01-11 06:39:09 +0000469
470 return std::move(T);
471}