xref: /llvm-project-15.0.7/llvm/lib/XRay/Trace.cpp (revision 967d4384)
1 //===- Trace.cpp - XRay Trace Loading implementation. ---------------------===//
2 //
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 //===----------------------------------------------------------------------===//
13 #include "llvm/XRay/Trace.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/Support/DataExtractor.h"
16 #include "llvm/Support/Error.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/XRay/YAMLXRayRecord.h"
19 
20 using namespace llvm;
21 using namespace llvm::xray;
22 using llvm::yaml::Input;
23 
24 namespace {
25 using XRayRecordStorage =
26     std::aligned_storage<sizeof(XRayRecord), alignof(XRayRecord)>::type;
27 
28 // Populates the FileHeader reference by reading the first 32 bytes of the file.
29 Error readBinaryFormatHeader(StringRef Data, XRayFileHeader &FileHeader) {
30   // FIXME: Maybe deduce whether the data is little or big-endian using some
31   // magic bytes in the beginning of the file?
32 
33   // First 32 bytes of the file will always be the header. We assume a certain
34   // format here:
35   //
36   //   (2)   uint16 : version
37   //   (2)   uint16 : type
38   //   (4)   uint32 : bitfield
39   //   (8)   uint64 : cycle frequency
40   //   (16)  -      : padding
41 
42   DataExtractor HeaderExtractor(Data, true, 8);
43   uint32_t OffsetPtr = 0;
44   FileHeader.Version = HeaderExtractor.getU16(&OffsetPtr);
45   FileHeader.Type = HeaderExtractor.getU16(&OffsetPtr);
46   uint32_t Bitfield = HeaderExtractor.getU32(&OffsetPtr);
47   FileHeader.ConstantTSC = Bitfield & 1uL;
48   FileHeader.NonstopTSC = Bitfield & 1uL << 1;
49   FileHeader.CycleFrequency = HeaderExtractor.getU64(&OffsetPtr);
50   std::memcpy(&FileHeader.FreeFormData, Data.bytes_begin() + OffsetPtr, 16);
51   if (FileHeader.Version != 1 && FileHeader.Version != 2)
52     return make_error<StringError>(
53         Twine("Unsupported XRay file version: ") + Twine(FileHeader.Version),
54         std::make_error_code(std::errc::invalid_argument));
55   return Error::success();
56 }
57 
58 Error loadNaiveFormatLog(StringRef Data, XRayFileHeader &FileHeader,
59                          std::vector<XRayRecord> &Records) {
60   if (Data.size() < 32)
61     return make_error<StringError>(
62         "Not enough bytes for an XRay log.",
63         std::make_error_code(std::errc::invalid_argument));
64 
65   if (Data.size() - 32 == 0 || Data.size() % 32 != 0)
66     return make_error<StringError>(
67         "Invalid-sized XRay data.",
68         std::make_error_code(std::errc::invalid_argument));
69 
70   if (auto E = readBinaryFormatHeader(Data, FileHeader))
71     return E;
72 
73   // Each record after the header will be 32 bytes, in the following format:
74   //
75   //   (2)   uint16 : record type
76   //   (1)   uint8  : cpu id
77   //   (1)   uint8  : type
78   //   (4)   sint32 : function id
79   //   (8)   uint64 : tsc
80   //   (4)   uint32 : thread id
81   //   (12)  -      : padding
82   for (auto S = Data.drop_front(32); !S.empty(); S = S.drop_front(32)) {
83     DataExtractor RecordExtractor(S, true, 8);
84     uint32_t OffsetPtr = 0;
85     Records.emplace_back();
86     auto &Record = Records.back();
87     Record.RecordType = RecordExtractor.getU16(&OffsetPtr);
88     Record.CPU = RecordExtractor.getU8(&OffsetPtr);
89     auto Type = RecordExtractor.getU8(&OffsetPtr);
90     switch (Type) {
91     case 0:
92       Record.Type = RecordTypes::ENTER;
93       break;
94     case 1:
95       Record.Type = RecordTypes::EXIT;
96       break;
97     case 2:
98       Record.Type = RecordTypes::TAIL_EXIT;
99       break;
100     default:
101       return make_error<StringError>(
102           Twine("Unknown record type '") + Twine(int{Type}) + "'",
103           std::make_error_code(std::errc::executable_format_error));
104     }
105     Record.FuncId = RecordExtractor.getSigned(&OffsetPtr, sizeof(int32_t));
106     Record.TSC = RecordExtractor.getU64(&OffsetPtr);
107     Record.TId = RecordExtractor.getU32(&OffsetPtr);
108   }
109   return Error::success();
110 }
111 
112 /// When reading from a Flight Data Recorder mode log, metadata records are
113 /// sparse compared to packed function records, so we must maintain state as we
114 /// read through the sequence of entries. This allows the reader to denormalize
115 /// the CPUId and Thread Id onto each Function Record and transform delta
116 /// encoded TSC values into absolute encodings on each record.
117 struct FDRState {
118   uint16_t CPUId;
119   uint16_t ThreadId;
120   uint64_t BaseTSC;
121 
122   /// Encode some of the state transitions for the FDR log reader as explicit
123   /// checks. These are expectations for the next Record in the stream.
124   enum class Token {
125     NEW_BUFFER_RECORD_OR_EOF,
126     WALLCLOCK_RECORD,
127     NEW_CPU_ID_RECORD,
128     FUNCTION_SEQUENCE,
129     SCAN_TO_END_OF_THREAD_BUF,
130     CUSTOM_EVENT_DATA,
131     CALL_ARGUMENT,
132   };
133   Token Expects;
134 
135   // Each threads buffer may have trailing garbage to scan over, so we track our
136   // progress.
137   uint64_t CurrentBufferSize;
138   uint64_t CurrentBufferConsumed;
139 };
140 
141 const char *fdrStateToTwine(const FDRState::Token &state) {
142   switch (state) {
143   case FDRState::Token::NEW_BUFFER_RECORD_OR_EOF:
144     return "NEW_BUFFER_RECORD_OR_EOF";
145   case FDRState::Token::WALLCLOCK_RECORD:
146     return "WALLCLOCK_RECORD";
147   case FDRState::Token::NEW_CPU_ID_RECORD:
148     return "NEW_CPU_ID_RECORD";
149   case FDRState::Token::FUNCTION_SEQUENCE:
150     return "FUNCTION_SEQUENCE";
151   case FDRState::Token::SCAN_TO_END_OF_THREAD_BUF:
152     return "SCAN_TO_END_OF_THREAD_BUF";
153   case FDRState::Token::CUSTOM_EVENT_DATA:
154     return "CUSTOM_EVENT_DATA";
155   case FDRState::Token::CALL_ARGUMENT:
156     return "CALL_ARGUMENT";
157   }
158   return "UNKNOWN";
159 }
160 
161 /// State transition when a NewBufferRecord is encountered.
162 Error processFDRNewBufferRecord(FDRState &State, uint8_t RecordFirstByte,
163                                 DataExtractor &RecordExtractor) {
164 
165   if (State.Expects != FDRState::Token::NEW_BUFFER_RECORD_OR_EOF)
166     return make_error<StringError>(
167         "Malformed log. Read New Buffer record kind out of sequence",
168         std::make_error_code(std::errc::executable_format_error));
169   uint32_t OffsetPtr = 1; // 1 byte into record.
170   State.ThreadId = RecordExtractor.getU16(&OffsetPtr);
171   State.Expects = FDRState::Token::WALLCLOCK_RECORD;
172   return Error::success();
173 }
174 
175 /// State transition when an EndOfBufferRecord is encountered.
176 Error processFDREndOfBufferRecord(FDRState &State, uint8_t RecordFirstByte,
177                                   DataExtractor &RecordExtractor) {
178   if (State.Expects == FDRState::Token::NEW_BUFFER_RECORD_OR_EOF)
179     return make_error<StringError>(
180         "Malformed log. Received EOB message without current buffer.",
181         std::make_error_code(std::errc::executable_format_error));
182   State.Expects = FDRState::Token::SCAN_TO_END_OF_THREAD_BUF;
183   return Error::success();
184 }
185 
186 /// State transition when a NewCPUIdRecord is encountered.
187 Error processFDRNewCPUIdRecord(FDRState &State, uint8_t RecordFirstByte,
188                                DataExtractor &RecordExtractor) {
189   if (State.Expects != FDRState::Token::FUNCTION_SEQUENCE &&
190       State.Expects != FDRState::Token::NEW_CPU_ID_RECORD)
191     return make_error<StringError>(
192         "Malformed log. Read NewCPUId record kind out of sequence",
193         std::make_error_code(std::errc::executable_format_error));
194   uint32_t OffsetPtr = 1; // Read starting after the first byte.
195   State.CPUId = RecordExtractor.getU16(&OffsetPtr);
196   State.BaseTSC = RecordExtractor.getU64(&OffsetPtr);
197   State.Expects = FDRState::Token::FUNCTION_SEQUENCE;
198   return Error::success();
199 }
200 
201 /// State transition when a TSCWrapRecord (overflow detection) is encountered.
202 Error processFDRTSCWrapRecord(FDRState &State, uint8_t RecordFirstByte,
203                               DataExtractor &RecordExtractor) {
204   if (State.Expects != FDRState::Token::FUNCTION_SEQUENCE)
205     return make_error<StringError>(
206         "Malformed log. Read TSCWrap record kind out of sequence",
207         std::make_error_code(std::errc::executable_format_error));
208   uint32_t OffsetPtr = 1; // Read starting after the first byte.
209   State.BaseTSC = RecordExtractor.getU64(&OffsetPtr);
210   return Error::success();
211 }
212 
213 /// State transition when a WallTimeMarkerRecord is encountered.
214 Error processFDRWallTimeRecord(FDRState &State, uint8_t RecordFirstByte,
215                                DataExtractor &RecordExtractor) {
216   if (State.Expects != FDRState::Token::WALLCLOCK_RECORD)
217     return make_error<StringError>(
218         "Malformed log. Read Wallclock record kind out of sequence",
219         std::make_error_code(std::errc::executable_format_error));
220   // We don't encode the wall time into any of the records.
221   // XRayRecords are concerned with the TSC instead.
222   State.Expects = FDRState::Token::NEW_CPU_ID_RECORD;
223   return Error::success();
224 }
225 
226 /// State transition when a CustomEventMarker is encountered.
227 Error processCustomEventMarker(FDRState &State, uint8_t RecordFirstByte,
228                                DataExtractor &RecordExtractor,
229                                size_t &RecordSize) {
230   // We can encounter a CustomEventMarker anywhere in the log, so we can handle
231   // it regardless of the expectation. However, we do set the expectation to
232   // read a set number of fixed bytes, as described in the metadata.
233   uint32_t OffsetPtr = 1; // Read after the first byte.
234   uint32_t DataSize = RecordExtractor.getU32(&OffsetPtr);
235   uint64_t TSC = RecordExtractor.getU64(&OffsetPtr);
236 
237   // FIXME: Actually represent the record through the API. For now we only skip
238   // through the data.
239   (void)TSC;
240   RecordSize = 16 + DataSize;
241   return Error::success();
242 }
243 
244 /// State transition when a CallArgumentRecord is encountered.
245 Error processFDRCallArgumentRecord(FDRState &State, uint8_t RecordFirstByte,
246                                    DataExtractor &RecordExtractor,
247                                    std::vector<XRayRecord> &Records) {
248   uint32_t OffsetPtr = 1; // Read starting after the first byte.
249   auto &Enter = Records.back();
250 
251   if (Enter.Type != RecordTypes::ENTER)
252     return make_error<StringError>(
253         "CallArgument needs to be right after a function entry",
254         std::make_error_code(std::errc::executable_format_error));
255   Enter.Type = RecordTypes::ENTER_ARG;
256   Enter.CallArgs.emplace_back(RecordExtractor.getU64(&OffsetPtr));
257   return Error::success();
258 }
259 
260 /// Advances the state machine for reading the FDR record type by reading one
261 /// Metadata Record and updating the State appropriately based on the kind of
262 /// record encountered. The RecordKind is encoded in the first byte of the
263 /// Record, which the caller should pass in because they have already read it
264 /// to determine that this is a metadata record as opposed to a function record.
265 Error processFDRMetadataRecord(FDRState &State, uint8_t RecordFirstByte,
266                                DataExtractor &RecordExtractor,
267                                size_t &RecordSize,
268                                std::vector<XRayRecord> &Records) {
269   // The remaining 7 bits are the RecordKind enum.
270   uint8_t RecordKind = RecordFirstByte >> 1;
271   switch (RecordKind) {
272   case 0: // NewBuffer
273     if (auto E =
274             processFDRNewBufferRecord(State, RecordFirstByte, RecordExtractor))
275       return E;
276     break;
277   case 1: // EndOfBuffer
278     if (auto E = processFDREndOfBufferRecord(State, RecordFirstByte,
279                                              RecordExtractor))
280       return E;
281     break;
282   case 2: // NewCPUId
283     if (auto E =
284             processFDRNewCPUIdRecord(State, RecordFirstByte, RecordExtractor))
285       return E;
286     break;
287   case 3: // TSCWrap
288     if (auto E =
289             processFDRTSCWrapRecord(State, RecordFirstByte, RecordExtractor))
290       return E;
291     break;
292   case 4: // WallTimeMarker
293     if (auto E =
294             processFDRWallTimeRecord(State, RecordFirstByte, RecordExtractor))
295       return E;
296     break;
297   case 5: // CustomEventMarker
298     if (auto E = processCustomEventMarker(State, RecordFirstByte,
299                                           RecordExtractor, RecordSize))
300       return E;
301     break;
302   case 6: // CallArgument
303     if (auto E = processFDRCallArgumentRecord(State, RecordFirstByte,
304                                               RecordExtractor, Records))
305       return E;
306     break;
307   default:
308     // Widen the record type to uint16_t to prevent conversion to char.
309     return make_error<StringError>(
310         Twine("Illegal metadata record type: ")
311             .concat(Twine(static_cast<unsigned>(RecordKind))),
312         std::make_error_code(std::errc::executable_format_error));
313   }
314   return Error::success();
315 }
316 
317 /// Reads a function record from an FDR format log, appending a new XRayRecord
318 /// to the vector being populated and updating the State with a new value
319 /// reference value to interpret TSC deltas.
320 ///
321 /// The XRayRecord constructed includes information from the function record
322 /// processed here as well as Thread ID and CPU ID formerly extracted into
323 /// State.
324 Error processFDRFunctionRecord(FDRState &State, uint8_t RecordFirstByte,
325                                DataExtractor &RecordExtractor,
326                                std::vector<XRayRecord> &Records) {
327   switch (State.Expects) {
328   case FDRState::Token::NEW_BUFFER_RECORD_OR_EOF:
329     return make_error<StringError>(
330         "Malformed log. Received Function Record before new buffer setup.",
331         std::make_error_code(std::errc::executable_format_error));
332   case FDRState::Token::WALLCLOCK_RECORD:
333     return make_error<StringError>(
334         "Malformed log. Received Function Record when expecting wallclock.",
335         std::make_error_code(std::errc::executable_format_error));
336   case FDRState::Token::NEW_CPU_ID_RECORD:
337     return make_error<StringError>(
338         "Malformed log. Received Function Record before first CPU record.",
339         std::make_error_code(std::errc::executable_format_error));
340   default:
341     Records.emplace_back();
342     auto &Record = Records.back();
343     Record.RecordType = 0; // Record is type NORMAL.
344     // Strip off record type bit and use the next three bits.
345     uint8_t RecordType = (RecordFirstByte >> 1) & 0x07;
346     switch (RecordType) {
347     case static_cast<uint8_t>(RecordTypes::ENTER):
348       Record.Type = RecordTypes::ENTER;
349       break;
350     case static_cast<uint8_t>(RecordTypes::EXIT):
351       Record.Type = RecordTypes::EXIT;
352       break;
353     case static_cast<uint8_t>(RecordTypes::TAIL_EXIT):
354       Record.Type = RecordTypes::TAIL_EXIT;
355       break;
356     default:
357       // Cast to an unsigned integer to not interpret the record type as a char.
358       return make_error<StringError>(
359           Twine("Illegal function record type: ")
360               .concat(Twine(static_cast<unsigned>(RecordType))),
361           std::make_error_code(std::errc::executable_format_error));
362     }
363     Record.CPU = State.CPUId;
364     Record.TId = State.ThreadId;
365     // Back up to read first 32 bits, including the 4 we pulled RecordType
366     // and RecordKind out of. The remaining 28 are FunctionId.
367     uint32_t OffsetPtr = 0;
368     // Despite function Id being a signed int on XRayRecord,
369     // when it is written to an FDR format, the top bits are truncated,
370     // so it is effectively an unsigned value. When we shift off the
371     // top four bits, we want the shift to be logical, so we read as
372     // uint32_t.
373     uint32_t FuncIdBitField = RecordExtractor.getU32(&OffsetPtr);
374     Record.FuncId = FuncIdBitField >> 4;
375     // FunctionRecords have a 32 bit delta from the previous absolute TSC
376     // or TSC delta. If this would overflow, we should read a TSCWrap record
377     // with an absolute TSC reading.
378     uint64_t NewTSC = State.BaseTSC + RecordExtractor.getU32(&OffsetPtr);
379     State.BaseTSC = NewTSC;
380     Record.TSC = NewTSC;
381   }
382   return Error::success();
383 }
384 
385 /// Reads a log in FDR mode for version 1 of this binary format. FDR mode is
386 /// defined as part of the compiler-rt project in xray_fdr_logging.h, and such
387 /// a log consists of the familiar 32 bit XRayHeader, followed by sequences of
388 /// of interspersed 16 byte Metadata Records and 8 byte Function Records.
389 ///
390 /// The following is an attempt to document the grammar of the format, which is
391 /// parsed by this function for little-endian machines. Since the format makes
392 /// use of BitFields, when we support big-endian architectures, we will need to
393 /// adjust not only the endianness parameter to llvm's RecordExtractor, but also
394 /// the bit twiddling logic, which is consistent with the little-endian
395 /// convention that BitFields within a struct will first be packed into the
396 /// least significant bits the address they belong to.
397 ///
398 /// We expect a format complying with the grammar in the following pseudo-EBNF.
399 ///
400 /// FDRLog: XRayFileHeader ThreadBuffer*
401 /// XRayFileHeader: 32 bytes to identify the log as FDR with machine metadata.
402 ///     Includes BufferSize
403 /// ThreadBuffer: NewBuffer WallClockTime NewCPUId FunctionSequence EOB
404 /// BufSize: 8 byte unsigned integer indicating how large the buffer is.
405 /// NewBuffer: 16 byte metadata record with Thread Id.
406 /// WallClockTime: 16 byte metadata record with human readable time.
407 /// NewCPUId: 16 byte metadata record with CPUId and a 64 bit TSC reading.
408 /// EOB: 16 byte record in a thread buffer plus mem garbage to fill BufSize.
409 /// FunctionSequence: NewCPUId | TSCWrap | FunctionRecord
410 /// TSCWrap: 16 byte metadata record with a full 64 bit TSC reading.
411 /// FunctionRecord: 8 byte record with FunctionId, entry/exit, and TSC delta.
412 Error loadFDRLog(StringRef Data, XRayFileHeader &FileHeader,
413                  std::vector<XRayRecord> &Records) {
414   if (Data.size() < 32)
415     return make_error<StringError>(
416         "Not enough bytes for an XRay log.",
417         std::make_error_code(std::errc::invalid_argument));
418 
419   // For an FDR log, there are records sized 16 and 8 bytes.
420   // There actually may be no records if no non-trivial functions are
421   // instrumented.
422   if (Data.size() % 8 != 0)
423     return make_error<StringError>(
424         "Invalid-sized XRay data.",
425         std::make_error_code(std::errc::invalid_argument));
426 
427   if (auto E = readBinaryFormatHeader(Data, FileHeader))
428     return E;
429 
430   uint64_t BufferSize = 0;
431   {
432     StringRef ExtraDataRef(FileHeader.FreeFormData, 16);
433     DataExtractor ExtraDataExtractor(ExtraDataRef, true, 8);
434     uint32_t ExtraDataOffset = 0;
435     BufferSize = ExtraDataExtractor.getU64(&ExtraDataOffset);
436   }
437   FDRState State{0,          0, 0, FDRState::Token::NEW_BUFFER_RECORD_OR_EOF,
438                  BufferSize, 0};
439   // RecordSize will tell the loop how far to seek ahead based on the record
440   // type that we have just read.
441   size_t RecordSize = 0;
442   for (auto S = Data.drop_front(32); !S.empty(); S = S.drop_front(RecordSize)) {
443     DataExtractor RecordExtractor(S, true, 8);
444     uint32_t OffsetPtr = 0;
445     if (State.Expects == FDRState::Token::SCAN_TO_END_OF_THREAD_BUF) {
446       RecordSize = State.CurrentBufferSize - State.CurrentBufferConsumed;
447       if (S.size() < RecordSize) {
448         return make_error<StringError>(
449             Twine("Incomplete thread buffer. Expected at least ") +
450                 Twine(RecordSize) + " bytes but found " + Twine(S.size()),
451             make_error_code(std::errc::invalid_argument));
452       }
453       State.CurrentBufferConsumed = 0;
454       State.Expects = FDRState::Token::NEW_BUFFER_RECORD_OR_EOF;
455       continue;
456     }
457     uint8_t BitField = RecordExtractor.getU8(&OffsetPtr);
458     bool isMetadataRecord = BitField & 0x01uL;
459     if (isMetadataRecord) {
460       RecordSize = 16;
461       if (auto E = processFDRMetadataRecord(State, BitField, RecordExtractor,
462                                             RecordSize, Records))
463         return E;
464     } else { // Process Function Record
465       RecordSize = 8;
466       if (auto E = processFDRFunctionRecord(State, BitField, RecordExtractor,
467                                             Records))
468         return E;
469     }
470     State.CurrentBufferConsumed += RecordSize;
471   }
472 
473   // Having iterated over everything we've been given, we've either consumed
474   // everything and ended up in the end state, or were told to skip the rest.
475   bool Finished = State.Expects == FDRState::Token::SCAN_TO_END_OF_THREAD_BUF &&
476                   State.CurrentBufferSize == State.CurrentBufferConsumed;
477   if (State.Expects != FDRState::Token::NEW_BUFFER_RECORD_OR_EOF && !Finished)
478     return make_error<StringError>(
479         Twine("Encountered EOF with unexpected state expectation ") +
480             fdrStateToTwine(State.Expects) +
481             ". Remaining expected bytes in thread buffer total " +
482             Twine(State.CurrentBufferSize - State.CurrentBufferConsumed),
483         std::make_error_code(std::errc::executable_format_error));
484 
485   return Error::success();
486 }
487 
488 Error loadYAMLLog(StringRef Data, XRayFileHeader &FileHeader,
489                   std::vector<XRayRecord> &Records) {
490   YAMLXRayTrace Trace;
491   Input In(Data);
492   In >> Trace;
493   if (In.error())
494     return make_error<StringError>("Failed loading YAML Data.", In.error());
495 
496   FileHeader.Version = Trace.Header.Version;
497   FileHeader.Type = Trace.Header.Type;
498   FileHeader.ConstantTSC = Trace.Header.ConstantTSC;
499   FileHeader.NonstopTSC = Trace.Header.NonstopTSC;
500   FileHeader.CycleFrequency = Trace.Header.CycleFrequency;
501 
502   if (FileHeader.Version != 1)
503     return make_error<StringError>(
504         Twine("Unsupported XRay file version: ") + Twine(FileHeader.Version),
505         std::make_error_code(std::errc::invalid_argument));
506 
507   Records.clear();
508   std::transform(Trace.Records.begin(), Trace.Records.end(),
509                  std::back_inserter(Records), [&](const YAMLXRayRecord &R) {
510                    return XRayRecord{R.RecordType, R.CPU, R.Type,
511                                      R.FuncId,     R.TSC, R.TId, R.CallArgs};
512                  });
513   return Error::success();
514 }
515 } // namespace
516 
517 Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
518   int Fd;
519   if (auto EC = sys::fs::openFileForRead(Filename, Fd)) {
520     return make_error<StringError>(
521         Twine("Cannot read log from '") + Filename + "'", EC);
522   }
523 
524   uint64_t FileSize;
525   if (auto EC = sys::fs::file_size(Filename, FileSize)) {
526     return make_error<StringError>(
527         Twine("Cannot read log from '") + Filename + "'", EC);
528   }
529   if (FileSize < 4) {
530     return make_error<StringError>(
531         Twine("File '") + Filename + "' too small for XRay.",
532         std::make_error_code(std::errc::executable_format_error));
533   }
534 
535   // Map the opened file into memory and use a StringRef to access it later.
536   std::error_code EC;
537   sys::fs::mapped_file_region MappedFile(
538       Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
539   if (EC) {
540     return make_error<StringError>(
541         Twine("Cannot read log from '") + Filename + "'", EC);
542   }
543   auto Data = StringRef(MappedFile.data(), MappedFile.size());
544 
545   // Attempt to detect the file type using file magic. We have a slight bias
546   // towards the binary format, and we do this by making sure that the first 4
547   // bytes of the binary file is some combination of the following byte
548   // patterns: (observe the code loading them assumes they're little endian)
549   //
550   //   0x01 0x00 0x00 0x00 - version 1, "naive" format
551   //   0x01 0x00 0x01 0x00 - version 1, "flight data recorder" format
552   //
553   // YAML files don't typically have those first four bytes as valid text so we
554   // try loading assuming YAML if we don't find these bytes.
555   //
556   // Only if we can't load either the binary or the YAML format will we yield an
557   // error.
558   StringRef Magic(MappedFile.data(), 4);
559   DataExtractor HeaderExtractor(Magic, true, 8);
560   uint32_t OffsetPtr = 0;
561   uint16_t Version = HeaderExtractor.getU16(&OffsetPtr);
562   uint16_t Type = HeaderExtractor.getU16(&OffsetPtr);
563 
564   enum BinaryFormatType { NAIVE_FORMAT = 0, FLIGHT_DATA_RECORDER_FORMAT = 1 };
565 
566   Trace T;
567   if (Type == NAIVE_FORMAT && (Version == 1 || Version == 2)) {
568     if (auto E = loadNaiveFormatLog(Data, T.FileHeader, T.Records))
569       return std::move(E);
570   } else if (Version == 1 && Type == FLIGHT_DATA_RECORDER_FORMAT) {
571     if (auto E = loadFDRLog(Data, T.FileHeader, T.Records))
572       return std::move(E);
573   } else {
574     if (auto E = loadYAMLLog(Data, T.FileHeader, T.Records))
575       return std::move(E);
576   }
577 
578   if (Sort)
579     std::sort(T.Records.begin(), T.Records.end(),
580               [&](const XRayRecord &L, const XRayRecord &R) {
581                 return L.TSC < R.TSC;
582               });
583 
584   return std::move(T);
585 }
586