xref: /llvm-project-15.0.7/llvm/lib/XRay/Trace.cpp (revision 10c873f1)
1d6c18657SDean Michael Berris //===- Trace.cpp - XRay Trace Loading implementation. ---------------------===//
2d6c18657SDean Michael Berris //
3d6c18657SDean Michael Berris //                     The LLVM Compiler Infrastructure
4d6c18657SDean Michael Berris //
5d6c18657SDean Michael Berris // This file is distributed under the University of Illinois Open Source
6d6c18657SDean Michael Berris // License. See LICENSE.TXT for details.
7d6c18657SDean Michael Berris //
8d6c18657SDean Michael Berris //===----------------------------------------------------------------------===//
9d6c18657SDean Michael Berris //
10d6c18657SDean Michael Berris // XRay log reader implementation.
11d6c18657SDean Michael Berris //
12d6c18657SDean Michael Berris //===----------------------------------------------------------------------===//
13d6c18657SDean Michael Berris #include "llvm/XRay/Trace.h"
14d6c18657SDean Michael Berris #include "llvm/ADT/STLExtras.h"
15d6c18657SDean Michael Berris #include "llvm/Support/DataExtractor.h"
16d6c18657SDean Michael Berris #include "llvm/Support/Error.h"
17d6c18657SDean Michael Berris #include "llvm/Support/FileSystem.h"
18d6c18657SDean Michael Berris #include "llvm/XRay/YAMLXRayRecord.h"
19d6c18657SDean Michael Berris 
20d6c18657SDean Michael Berris using namespace llvm;
21d6c18657SDean Michael Berris using namespace llvm::xray;
22d6c18657SDean Michael Berris using llvm::yaml::Input;
23d6c18657SDean Michael Berris 
2449a49fe8SBenjamin Kramer namespace {
25d6c18657SDean Michael Berris using XRayRecordStorage =
26d6c18657SDean Michael Berris     std::aligned_storage<sizeof(XRayRecord), alignof(XRayRecord)>::type;
27d6c18657SDean Michael Berris 
284f83c4d1SDean Michael Berris // Populates the FileHeader reference by reading the first 32 bytes of the file.
294f83c4d1SDean Michael Berris Error readBinaryFormatHeader(StringRef Data, XRayFileHeader &FileHeader) {
30d6c18657SDean Michael Berris   // FIXME: Maybe deduce whether the data is little or big-endian using some
31d6c18657SDean Michael Berris   // magic bytes in the beginning of the file?
32d6c18657SDean Michael Berris 
33d6c18657SDean Michael Berris   // First 32 bytes of the file will always be the header. We assume a certain
34d6c18657SDean Michael Berris   // format here:
35d6c18657SDean Michael Berris   //
36d6c18657SDean Michael Berris   //   (2)   uint16 : version
37d6c18657SDean Michael Berris   //   (2)   uint16 : type
38d6c18657SDean Michael Berris   //   (4)   uint32 : bitfield
39d6c18657SDean Michael Berris   //   (8)   uint64 : cycle frequency
40d6c18657SDean Michael Berris   //   (16)  -      : padding
41d6c18657SDean Michael Berris 
42d6c18657SDean Michael Berris   DataExtractor HeaderExtractor(Data, true, 8);
43d6c18657SDean Michael Berris   uint32_t OffsetPtr = 0;
44d6c18657SDean Michael Berris   FileHeader.Version = HeaderExtractor.getU16(&OffsetPtr);
45d6c18657SDean Michael Berris   FileHeader.Type = HeaderExtractor.getU16(&OffsetPtr);
46d6c18657SDean Michael Berris   uint32_t Bitfield = HeaderExtractor.getU32(&OffsetPtr);
47d6c18657SDean Michael Berris   FileHeader.ConstantTSC = Bitfield & 1uL;
48d6c18657SDean Michael Berris   FileHeader.NonstopTSC = Bitfield & 1uL << 1;
49d6c18657SDean Michael Berris   FileHeader.CycleFrequency = HeaderExtractor.getU64(&OffsetPtr);
5060c24878SDean Michael Berris   std::memcpy(&FileHeader.FreeFormData, Data.bytes_begin() + OffsetPtr, 16);
510f84a7d3SDean Michael Berris   if (FileHeader.Version != 1 && FileHeader.Version != 2)
52d6c18657SDean Michael Berris     return make_error<StringError>(
53d6c18657SDean Michael Berris         Twine("Unsupported XRay file version: ") + Twine(FileHeader.Version),
54d6c18657SDean Michael Berris         std::make_error_code(std::errc::invalid_argument));
554f83c4d1SDean Michael Berris   return Error::success();
564f83c4d1SDean Michael Berris }
574f83c4d1SDean Michael Berris 
584f83c4d1SDean Michael Berris Error loadNaiveFormatLog(StringRef Data, XRayFileHeader &FileHeader,
594f83c4d1SDean Michael Berris                          std::vector<XRayRecord> &Records) {
604f83c4d1SDean Michael Berris   if (Data.size() < 32)
614f83c4d1SDean Michael Berris     return make_error<StringError>(
624f83c4d1SDean Michael Berris         "Not enough bytes for an XRay log.",
634f83c4d1SDean Michael Berris         std::make_error_code(std::errc::invalid_argument));
644f83c4d1SDean Michael Berris 
654f83c4d1SDean Michael Berris   if (Data.size() - 32 == 0 || Data.size() % 32 != 0)
664f83c4d1SDean Michael Berris     return make_error<StringError>(
674f83c4d1SDean Michael Berris         "Invalid-sized XRay data.",
684f83c4d1SDean Michael Berris         std::make_error_code(std::errc::invalid_argument));
694f83c4d1SDean Michael Berris 
704f83c4d1SDean Michael Berris   if (auto E = readBinaryFormatHeader(Data, FileHeader))
714f83c4d1SDean Michael Berris     return E;
72d6c18657SDean Michael Berris 
73d6c18657SDean Michael Berris   // Each record after the header will be 32 bytes, in the following format:
74d6c18657SDean Michael Berris   //
75d6c18657SDean Michael Berris   //   (2)   uint16 : record type
76d6c18657SDean Michael Berris   //   (1)   uint8  : cpu id
77d6c18657SDean Michael Berris   //   (1)   uint8  : type
78d6c18657SDean Michael Berris   //   (4)   sint32 : function id
79d6c18657SDean Michael Berris   //   (8)   uint64 : tsc
80d6c18657SDean Michael Berris   //   (4)   uint32 : thread id
81d6c18657SDean Michael Berris   //   (12)  -      : padding
82d6c18657SDean Michael Berris   for (auto S = Data.drop_front(32); !S.empty(); S = S.drop_front(32)) {
83d6c18657SDean Michael Berris     DataExtractor RecordExtractor(S, true, 8);
84d6c18657SDean Michael Berris     uint32_t OffsetPtr = 0;
85d6c18657SDean Michael Berris     Records.emplace_back();
86d6c18657SDean Michael Berris     auto &Record = Records.back();
87d6c18657SDean Michael Berris     Record.RecordType = RecordExtractor.getU16(&OffsetPtr);
88d6c18657SDean Michael Berris     Record.CPU = RecordExtractor.getU8(&OffsetPtr);
89d6c18657SDean Michael Berris     auto Type = RecordExtractor.getU8(&OffsetPtr);
90d6c18657SDean Michael Berris     switch (Type) {
91d6c18657SDean Michael Berris     case 0:
92d6c18657SDean Michael Berris       Record.Type = RecordTypes::ENTER;
93d6c18657SDean Michael Berris       break;
94d6c18657SDean Michael Berris     case 1:
95d6c18657SDean Michael Berris       Record.Type = RecordTypes::EXIT;
96d6c18657SDean Michael Berris       break;
970f84a7d3SDean Michael Berris     case 2:
980f84a7d3SDean Michael Berris       Record.Type = RecordTypes::TAIL_EXIT;
990f84a7d3SDean Michael Berris       break;
100d6c18657SDean Michael Berris     default:
101d6c18657SDean Michael Berris       return make_error<StringError>(
102d6c18657SDean Michael Berris           Twine("Unknown record type '") + Twine(int{Type}) + "'",
103d6c18657SDean Michael Berris           std::make_error_code(std::errc::executable_format_error));
104d6c18657SDean Michael Berris     }
105d6c18657SDean Michael Berris     Record.FuncId = RecordExtractor.getSigned(&OffsetPtr, sizeof(int32_t));
106d6c18657SDean Michael Berris     Record.TSC = RecordExtractor.getU64(&OffsetPtr);
107d6c18657SDean Michael Berris     Record.TId = RecordExtractor.getU32(&OffsetPtr);
108d6c18657SDean Michael Berris   }
109d6c18657SDean Michael Berris   return Error::success();
110d6c18657SDean Michael Berris }
111d6c18657SDean Michael Berris 
1124f83c4d1SDean Michael Berris /// When reading from a Flight Data Recorder mode log, metadata records are
1134f83c4d1SDean Michael Berris /// sparse compared to packed function records, so we must maintain state as we
1144f83c4d1SDean Michael Berris /// read through the sequence of entries. This allows the reader to denormalize
1154f83c4d1SDean Michael Berris /// the CPUId and Thread Id onto each Function Record and transform delta
1164f83c4d1SDean Michael Berris /// encoded TSC values into absolute encodings on each record.
1174f83c4d1SDean Michael Berris struct FDRState {
1184f83c4d1SDean Michael Berris   uint16_t CPUId;
1194f83c4d1SDean Michael Berris   uint16_t ThreadId;
1204f83c4d1SDean Michael Berris   uint64_t BaseTSC;
121a7bbe448SDean Michael Berris 
1224f83c4d1SDean Michael Berris   /// Encode some of the state transitions for the FDR log reader as explicit
1234f83c4d1SDean Michael Berris   /// checks. These are expectations for the next Record in the stream.
1244f83c4d1SDean Michael Berris   enum class Token {
1254f83c4d1SDean Michael Berris     NEW_BUFFER_RECORD_OR_EOF,
1264f83c4d1SDean Michael Berris     WALLCLOCK_RECORD,
1274f83c4d1SDean Michael Berris     NEW_CPU_ID_RECORD,
12860c24878SDean Michael Berris     FUNCTION_SEQUENCE,
12960c24878SDean Michael Berris     SCAN_TO_END_OF_THREAD_BUF,
130a7bbe448SDean Michael Berris     CUSTOM_EVENT_DATA,
131*10c873f1SMartin Pelikan     CALL_ARGUMENT,
1324f83c4d1SDean Michael Berris   };
1334f83c4d1SDean Michael Berris   Token Expects;
134a7bbe448SDean Michael Berris 
13560c24878SDean Michael Berris   // Each threads buffer may have trailing garbage to scan over, so we track our
13660c24878SDean Michael Berris   // progress.
13760c24878SDean Michael Berris   uint64_t CurrentBufferSize;
13860c24878SDean Michael Berris   uint64_t CurrentBufferConsumed;
1394f83c4d1SDean Michael Berris };
140d6c18657SDean Michael Berris 
14149a49fe8SBenjamin Kramer const char *fdrStateToTwine(const FDRState::Token &state) {
14260c24878SDean Michael Berris   switch (state) {
14360c24878SDean Michael Berris   case FDRState::Token::NEW_BUFFER_RECORD_OR_EOF:
14460c24878SDean Michael Berris     return "NEW_BUFFER_RECORD_OR_EOF";
14560c24878SDean Michael Berris   case FDRState::Token::WALLCLOCK_RECORD:
14660c24878SDean Michael Berris     return "WALLCLOCK_RECORD";
14760c24878SDean Michael Berris   case FDRState::Token::NEW_CPU_ID_RECORD:
14860c24878SDean Michael Berris     return "NEW_CPU_ID_RECORD";
14960c24878SDean Michael Berris   case FDRState::Token::FUNCTION_SEQUENCE:
15060c24878SDean Michael Berris     return "FUNCTION_SEQUENCE";
15160c24878SDean Michael Berris   case FDRState::Token::SCAN_TO_END_OF_THREAD_BUF:
15260c24878SDean Michael Berris     return "SCAN_TO_END_OF_THREAD_BUF";
153a7bbe448SDean Michael Berris   case FDRState::Token::CUSTOM_EVENT_DATA:
154a7bbe448SDean Michael Berris     return "CUSTOM_EVENT_DATA";
155*10c873f1SMartin Pelikan   case FDRState::Token::CALL_ARGUMENT:
156*10c873f1SMartin Pelikan     return "CALL_ARGUMENT";
15760c24878SDean Michael Berris   }
15860c24878SDean Michael Berris   return "UNKNOWN";
15960c24878SDean Michael Berris }
16060c24878SDean Michael Berris 
1614f83c4d1SDean Michael Berris /// State transition when a NewBufferRecord is encountered.
1624f83c4d1SDean Michael Berris Error processFDRNewBufferRecord(FDRState &State, uint8_t RecordFirstByte,
1634f83c4d1SDean Michael Berris                                 DataExtractor &RecordExtractor) {
1644f83c4d1SDean Michael Berris 
16560c24878SDean Michael Berris   if (State.Expects != FDRState::Token::NEW_BUFFER_RECORD_OR_EOF)
1664f83c4d1SDean Michael Berris     return make_error<StringError>(
1674f83c4d1SDean Michael Berris         "Malformed log. Read New Buffer record kind out of sequence",
1684f83c4d1SDean Michael Berris         std::make_error_code(std::errc::executable_format_error));
1694f83c4d1SDean Michael Berris   uint32_t OffsetPtr = 1; // 1 byte into record.
1704f83c4d1SDean Michael Berris   State.ThreadId = RecordExtractor.getU16(&OffsetPtr);
1714f83c4d1SDean Michael Berris   State.Expects = FDRState::Token::WALLCLOCK_RECORD;
1724f83c4d1SDean Michael Berris   return Error::success();
1734f83c4d1SDean Michael Berris }
1744f83c4d1SDean Michael Berris 
1754f83c4d1SDean Michael Berris /// State transition when an EndOfBufferRecord is encountered.
1764f83c4d1SDean Michael Berris Error processFDREndOfBufferRecord(FDRState &State, uint8_t RecordFirstByte,
1774f83c4d1SDean Michael Berris                                   DataExtractor &RecordExtractor) {
17860c24878SDean Michael Berris   if (State.Expects == FDRState::Token::NEW_BUFFER_RECORD_OR_EOF)
1794f83c4d1SDean Michael Berris     return make_error<StringError>(
1804f83c4d1SDean Michael Berris         "Malformed log. Received EOB message without current buffer.",
1814f83c4d1SDean Michael Berris         std::make_error_code(std::errc::executable_format_error));
18260c24878SDean Michael Berris   State.Expects = FDRState::Token::SCAN_TO_END_OF_THREAD_BUF;
1834f83c4d1SDean Michael Berris   return Error::success();
1844f83c4d1SDean Michael Berris }
1854f83c4d1SDean Michael Berris 
1864f83c4d1SDean Michael Berris /// State transition when a NewCPUIdRecord is encountered.
1874f83c4d1SDean Michael Berris Error processFDRNewCPUIdRecord(FDRState &State, uint8_t RecordFirstByte,
1884f83c4d1SDean Michael Berris                                DataExtractor &RecordExtractor) {
1894f83c4d1SDean Michael Berris   if (State.Expects != FDRState::Token::FUNCTION_SEQUENCE &&
19060c24878SDean Michael Berris       State.Expects != FDRState::Token::NEW_CPU_ID_RECORD)
1914f83c4d1SDean Michael Berris     return make_error<StringError>(
1924f83c4d1SDean Michael Berris         "Malformed log. Read NewCPUId record kind out of sequence",
1934f83c4d1SDean Michael Berris         std::make_error_code(std::errc::executable_format_error));
1944f83c4d1SDean Michael Berris   uint32_t OffsetPtr = 1; // Read starting after the first byte.
1954f83c4d1SDean Michael Berris   State.CPUId = RecordExtractor.getU16(&OffsetPtr);
1964f83c4d1SDean Michael Berris   State.BaseTSC = RecordExtractor.getU64(&OffsetPtr);
1974f83c4d1SDean Michael Berris   State.Expects = FDRState::Token::FUNCTION_SEQUENCE;
1984f83c4d1SDean Michael Berris   return Error::success();
1994f83c4d1SDean Michael Berris }
2004f83c4d1SDean Michael Berris 
2014f83c4d1SDean Michael Berris /// State transition when a TSCWrapRecord (overflow detection) is encountered.
2024f83c4d1SDean Michael Berris Error processFDRTSCWrapRecord(FDRState &State, uint8_t RecordFirstByte,
2034f83c4d1SDean Michael Berris                               DataExtractor &RecordExtractor) {
20460c24878SDean Michael Berris   if (State.Expects != FDRState::Token::FUNCTION_SEQUENCE)
2054f83c4d1SDean Michael Berris     return make_error<StringError>(
2064f83c4d1SDean Michael Berris         "Malformed log. Read TSCWrap record kind out of sequence",
2074f83c4d1SDean Michael Berris         std::make_error_code(std::errc::executable_format_error));
2084f83c4d1SDean Michael Berris   uint32_t OffsetPtr = 1; // Read starting after the first byte.
2094f83c4d1SDean Michael Berris   State.BaseTSC = RecordExtractor.getU64(&OffsetPtr);
2104f83c4d1SDean Michael Berris   return Error::success();
2114f83c4d1SDean Michael Berris }
2124f83c4d1SDean Michael Berris 
2134f83c4d1SDean Michael Berris /// State transition when a WallTimeMarkerRecord is encountered.
2144f83c4d1SDean Michael Berris Error processFDRWallTimeRecord(FDRState &State, uint8_t RecordFirstByte,
2154f83c4d1SDean Michael Berris                                DataExtractor &RecordExtractor) {
21660c24878SDean Michael Berris   if (State.Expects != FDRState::Token::WALLCLOCK_RECORD)
2174f83c4d1SDean Michael Berris     return make_error<StringError>(
2184f83c4d1SDean Michael Berris         "Malformed log. Read Wallclock record kind out of sequence",
2194f83c4d1SDean Michael Berris         std::make_error_code(std::errc::executable_format_error));
2204f83c4d1SDean Michael Berris   // We don't encode the wall time into any of the records.
2214f83c4d1SDean Michael Berris   // XRayRecords are concerned with the TSC instead.
2224f83c4d1SDean Michael Berris   State.Expects = FDRState::Token::NEW_CPU_ID_RECORD;
2234f83c4d1SDean Michael Berris   return Error::success();
2244f83c4d1SDean Michael Berris }
2254f83c4d1SDean Michael Berris 
226a7bbe448SDean Michael Berris /// State transition when a CustomEventMarker is encountered.
227a7bbe448SDean Michael Berris Error processCustomEventMarker(FDRState &State, uint8_t RecordFirstByte,
228a7bbe448SDean Michael Berris                                DataExtractor &RecordExtractor,
229a7bbe448SDean Michael Berris                                size_t &RecordSize) {
230a7bbe448SDean Michael Berris   // We can encounter a CustomEventMarker anywhere in the log, so we can handle
2313d0bc9efSKeith Wyss   // it regardless of the expectation. However, we do set the expectation to
2323d0bc9efSKeith Wyss   // read a set number of fixed bytes, as described in the metadata.
233a7bbe448SDean Michael Berris   uint32_t OffsetPtr = 1; // Read after the first byte.
234a7bbe448SDean Michael Berris   uint32_t DataSize = RecordExtractor.getU32(&OffsetPtr);
235a7bbe448SDean Michael Berris   uint64_t TSC = RecordExtractor.getU64(&OffsetPtr);
236a7bbe448SDean Michael Berris 
237a7bbe448SDean Michael Berris   // FIXME: Actually represent the record through the API. For now we only skip
238a7bbe448SDean Michael Berris   // through the data.
239a7bbe448SDean Michael Berris   (void)TSC;
240a7bbe448SDean Michael Berris   RecordSize = 16 + DataSize;
241a7bbe448SDean Michael Berris   return Error::success();
242a7bbe448SDean Michael Berris }
243a7bbe448SDean Michael Berris 
244*10c873f1SMartin Pelikan /// State transition when a CallArgumentRecord is encountered.
245*10c873f1SMartin Pelikan Error processFDRCallArgumentRecord(FDRState &State, uint8_t RecordFirstByte,
246*10c873f1SMartin Pelikan                                    DataExtractor &RecordExtractor,
247*10c873f1SMartin Pelikan                                    std::vector<XRayRecord> &Records) {
248*10c873f1SMartin Pelikan   uint32_t OffsetPtr = 1; // Read starting after the first byte.
249*10c873f1SMartin Pelikan   auto &Enter = Records.back();
250*10c873f1SMartin Pelikan 
251*10c873f1SMartin Pelikan   if (Enter.Type != RecordTypes::ENTER)
252*10c873f1SMartin Pelikan     return make_error<StringError>(
253*10c873f1SMartin Pelikan         "CallArgument needs to be right after a function entry",
254*10c873f1SMartin Pelikan         std::make_error_code(std::errc::executable_format_error));
255*10c873f1SMartin Pelikan   Enter.Type = RecordTypes::ENTER_ARG;
256*10c873f1SMartin Pelikan   Enter.CallArgs.emplace_back(RecordExtractor.getU64(&OffsetPtr));
257*10c873f1SMartin Pelikan   return Error::success();
258*10c873f1SMartin Pelikan }
259*10c873f1SMartin Pelikan 
2604f83c4d1SDean Michael Berris /// Advances the state machine for reading the FDR record type by reading one
261e96152a9SKeith Wyss /// Metadata Record and updating the State appropriately based on the kind of
2624f83c4d1SDean Michael Berris /// record encountered. The RecordKind is encoded in the first byte of the
2634f83c4d1SDean Michael Berris /// Record, which the caller should pass in because they have already read it
2644f83c4d1SDean Michael Berris /// to determine that this is a metadata record as opposed to a function record.
2654f83c4d1SDean Michael Berris Error processFDRMetadataRecord(FDRState &State, uint8_t RecordFirstByte,
266a7bbe448SDean Michael Berris                                DataExtractor &RecordExtractor,
267*10c873f1SMartin Pelikan                                size_t &RecordSize,
268*10c873f1SMartin Pelikan                                std::vector<XRayRecord> &Records) {
2694f83c4d1SDean Michael Berris   // The remaining 7 bits are the RecordKind enum.
2704f83c4d1SDean Michael Berris   uint8_t RecordKind = RecordFirstByte >> 1;
2714f83c4d1SDean Michael Berris   switch (RecordKind) {
2724f83c4d1SDean Michael Berris   case 0: // NewBuffer
2734f83c4d1SDean Michael Berris     if (auto E =
2744f83c4d1SDean Michael Berris             processFDRNewBufferRecord(State, RecordFirstByte, RecordExtractor))
2754f83c4d1SDean Michael Berris       return E;
2764f83c4d1SDean Michael Berris     break;
2774f83c4d1SDean Michael Berris   case 1: // EndOfBuffer
2784f83c4d1SDean Michael Berris     if (auto E = processFDREndOfBufferRecord(State, RecordFirstByte,
2794f83c4d1SDean Michael Berris                                              RecordExtractor))
2804f83c4d1SDean Michael Berris       return E;
2814f83c4d1SDean Michael Berris     break;
2824f83c4d1SDean Michael Berris   case 2: // NewCPUId
2834f83c4d1SDean Michael Berris     if (auto E =
2844f83c4d1SDean Michael Berris             processFDRNewCPUIdRecord(State, RecordFirstByte, RecordExtractor))
2854f83c4d1SDean Michael Berris       return E;
2864f83c4d1SDean Michael Berris     break;
2874f83c4d1SDean Michael Berris   case 3: // TSCWrap
2884f83c4d1SDean Michael Berris     if (auto E =
2894f83c4d1SDean Michael Berris             processFDRTSCWrapRecord(State, RecordFirstByte, RecordExtractor))
2904f83c4d1SDean Michael Berris       return E;
2914f83c4d1SDean Michael Berris     break;
2924f83c4d1SDean Michael Berris   case 4: // WallTimeMarker
2934f83c4d1SDean Michael Berris     if (auto E =
2944f83c4d1SDean Michael Berris             processFDRWallTimeRecord(State, RecordFirstByte, RecordExtractor))
2954f83c4d1SDean Michael Berris       return E;
2964f83c4d1SDean Michael Berris     break;
297a7bbe448SDean Michael Berris   case 5: // CustomEventMarker
298a7bbe448SDean Michael Berris     if (auto E = processCustomEventMarker(State, RecordFirstByte,
299a7bbe448SDean Michael Berris                                           RecordExtractor, RecordSize))
300a7bbe448SDean Michael Berris       return E;
301a7bbe448SDean Michael Berris     break;
302*10c873f1SMartin Pelikan   case 6: // CallArgument
303*10c873f1SMartin Pelikan     if (auto E = processFDRCallArgumentRecord(State, RecordFirstByte,
304*10c873f1SMartin Pelikan                                               RecordExtractor, Records))
305*10c873f1SMartin Pelikan       return E;
306*10c873f1SMartin Pelikan     break;
3074f83c4d1SDean Michael Berris   default:
3084f83c4d1SDean Michael Berris     // Widen the record type to uint16_t to prevent conversion to char.
3094f83c4d1SDean Michael Berris     return make_error<StringError>(
3104f83c4d1SDean Michael Berris         Twine("Illegal metadata record type: ")
3114f83c4d1SDean Michael Berris             .concat(Twine(static_cast<unsigned>(RecordKind))),
3124f83c4d1SDean Michael Berris         std::make_error_code(std::errc::executable_format_error));
3134f83c4d1SDean Michael Berris   }
3144f83c4d1SDean Michael Berris   return Error::success();
3154f83c4d1SDean Michael Berris }
3164f83c4d1SDean Michael Berris 
3174f83c4d1SDean Michael Berris /// Reads a function record from an FDR format log, appending a new XRayRecord
3184f83c4d1SDean Michael Berris /// to the vector being populated and updating the State with a new value
3194f83c4d1SDean Michael Berris /// reference value to interpret TSC deltas.
3204f83c4d1SDean Michael Berris ///
3214f83c4d1SDean Michael Berris /// The XRayRecord constructed includes information from the function record
3224f83c4d1SDean Michael Berris /// processed here as well as Thread ID and CPU ID formerly extracted into
3234f83c4d1SDean Michael Berris /// State.
3244f83c4d1SDean Michael Berris Error processFDRFunctionRecord(FDRState &State, uint8_t RecordFirstByte,
3254f83c4d1SDean Michael Berris                                DataExtractor &RecordExtractor,
3264f83c4d1SDean Michael Berris                                std::vector<XRayRecord> &Records) {
3274f83c4d1SDean Michael Berris   switch (State.Expects) {
3284f83c4d1SDean Michael Berris   case FDRState::Token::NEW_BUFFER_RECORD_OR_EOF:
3294f83c4d1SDean Michael Berris     return make_error<StringError>(
3304f83c4d1SDean Michael Berris         "Malformed log. Received Function Record before new buffer setup.",
3314f83c4d1SDean Michael Berris         std::make_error_code(std::errc::executable_format_error));
3324f83c4d1SDean Michael Berris   case FDRState::Token::WALLCLOCK_RECORD:
3334f83c4d1SDean Michael Berris     return make_error<StringError>(
3344f83c4d1SDean Michael Berris         "Malformed log. Received Function Record when expecting wallclock.",
3354f83c4d1SDean Michael Berris         std::make_error_code(std::errc::executable_format_error));
3364f83c4d1SDean Michael Berris   case FDRState::Token::NEW_CPU_ID_RECORD:
3374f83c4d1SDean Michael Berris     return make_error<StringError>(
3384f83c4d1SDean Michael Berris         "Malformed log. Received Function Record before first CPU record.",
3394f83c4d1SDean Michael Berris         std::make_error_code(std::errc::executable_format_error));
3404f83c4d1SDean Michael Berris   default:
3414f83c4d1SDean Michael Berris     Records.emplace_back();
3424f83c4d1SDean Michael Berris     auto &Record = Records.back();
3434f83c4d1SDean Michael Berris     Record.RecordType = 0; // Record is type NORMAL.
3444f83c4d1SDean Michael Berris     // Strip off record type bit and use the next three bits.
3454f83c4d1SDean Michael Berris     uint8_t RecordType = (RecordFirstByte >> 1) & 0x07;
3464f83c4d1SDean Michael Berris     switch (RecordType) {
3474f83c4d1SDean Michael Berris     case static_cast<uint8_t>(RecordTypes::ENTER):
3484f83c4d1SDean Michael Berris       Record.Type = RecordTypes::ENTER;
3494f83c4d1SDean Michael Berris       break;
3504f83c4d1SDean Michael Berris     case static_cast<uint8_t>(RecordTypes::EXIT):
3514f83c4d1SDean Michael Berris       Record.Type = RecordTypes::EXIT;
3524f83c4d1SDean Michael Berris       break;
3530f84a7d3SDean Michael Berris     case static_cast<uint8_t>(RecordTypes::TAIL_EXIT):
3540f84a7d3SDean Michael Berris       Record.Type = RecordTypes::TAIL_EXIT;
3550f84a7d3SDean Michael Berris       break;
3564f83c4d1SDean Michael Berris     default:
357d78db156SMartin Pelikan       // Cast to an unsigned integer to not interpret the record type as a char.
3584f83c4d1SDean Michael Berris       return make_error<StringError>(
3594f83c4d1SDean Michael Berris           Twine("Illegal function record type: ")
3604f83c4d1SDean Michael Berris               .concat(Twine(static_cast<unsigned>(RecordType))),
3614f83c4d1SDean Michael Berris           std::make_error_code(std::errc::executable_format_error));
3624f83c4d1SDean Michael Berris     }
3634f83c4d1SDean Michael Berris     Record.CPU = State.CPUId;
3644f83c4d1SDean Michael Berris     Record.TId = State.ThreadId;
3653d0bc9efSKeith Wyss     // Back up to read first 32 bits, including the 4 we pulled RecordType
3664f83c4d1SDean Michael Berris     // and RecordKind out of. The remaining 28 are FunctionId.
3674f83c4d1SDean Michael Berris     uint32_t OffsetPtr = 0;
3684f83c4d1SDean Michael Berris     // Despite function Id being a signed int on XRayRecord,
3694f83c4d1SDean Michael Berris     // when it is written to an FDR format, the top bits are truncated,
3704f83c4d1SDean Michael Berris     // so it is effectively an unsigned value. When we shift off the
3714f83c4d1SDean Michael Berris     // top four bits, we want the shift to be logical, so we read as
3724f83c4d1SDean Michael Berris     // uint32_t.
3734f83c4d1SDean Michael Berris     uint32_t FuncIdBitField = RecordExtractor.getU32(&OffsetPtr);
3744f83c4d1SDean Michael Berris     Record.FuncId = FuncIdBitField >> 4;
3754f83c4d1SDean Michael Berris     // FunctionRecords have a 32 bit delta from the previous absolute TSC
3764f83c4d1SDean Michael Berris     // or TSC delta. If this would overflow, we should read a TSCWrap record
3774f83c4d1SDean Michael Berris     // with an absolute TSC reading.
378d78db156SMartin Pelikan     uint64_t NewTSC = State.BaseTSC + RecordExtractor.getU32(&OffsetPtr);
379d78db156SMartin Pelikan     State.BaseTSC = NewTSC;
380d78db156SMartin Pelikan     Record.TSC = NewTSC;
3814f83c4d1SDean Michael Berris   }
3824f83c4d1SDean Michael Berris   return Error::success();
3834f83c4d1SDean Michael Berris }
3844f83c4d1SDean Michael Berris 
3854f83c4d1SDean Michael Berris /// Reads a log in FDR mode for version 1 of this binary format. FDR mode is
3864f83c4d1SDean Michael Berris /// defined as part of the compiler-rt project in xray_fdr_logging.h, and such
3874f83c4d1SDean Michael Berris /// a log consists of the familiar 32 bit XRayHeader, followed by sequences of
3884f83c4d1SDean Michael Berris /// of interspersed 16 byte Metadata Records and 8 byte Function Records.
3894f83c4d1SDean Michael Berris ///
3904f83c4d1SDean Michael Berris /// The following is an attempt to document the grammar of the format, which is
3914f83c4d1SDean Michael Berris /// parsed by this function for little-endian machines. Since the format makes
392d78db156SMartin Pelikan /// use of BitFields, when we support big-endian architectures, we will need to
39368168d17SSimon Pilgrim /// adjust not only the endianness parameter to llvm's RecordExtractor, but also
3944f83c4d1SDean Michael Berris /// the bit twiddling logic, which is consistent with the little-endian
3954f83c4d1SDean Michael Berris /// convention that BitFields within a struct will first be packed into the
3964f83c4d1SDean Michael Berris /// least significant bits the address they belong to.
3974f83c4d1SDean Michael Berris ///
3984f83c4d1SDean Michael Berris /// We expect a format complying with the grammar in the following pseudo-EBNF.
3994f83c4d1SDean Michael Berris ///
4004f83c4d1SDean Michael Berris /// FDRLog: XRayFileHeader ThreadBuffer*
4013d0bc9efSKeith Wyss /// XRayFileHeader: 32 bytes to identify the log as FDR with machine metadata.
4023d0bc9efSKeith Wyss ///     Includes BufferSize
4033d0bc9efSKeith Wyss /// ThreadBuffer: NewBuffer WallClockTime NewCPUId FunctionSequence EOB
40460c24878SDean Michael Berris /// BufSize: 8 byte unsigned integer indicating how large the buffer is.
4054f83c4d1SDean Michael Berris /// NewBuffer: 16 byte metadata record with Thread Id.
4064f83c4d1SDean Michael Berris /// WallClockTime: 16 byte metadata record with human readable time.
4074f83c4d1SDean Michael Berris /// NewCPUId: 16 byte metadata record with CPUId and a 64 bit TSC reading.
40860c24878SDean Michael Berris /// EOB: 16 byte record in a thread buffer plus mem garbage to fill BufSize.
4094f83c4d1SDean Michael Berris /// FunctionSequence: NewCPUId | TSCWrap | FunctionRecord
4104f83c4d1SDean Michael Berris /// TSCWrap: 16 byte metadata record with a full 64 bit TSC reading.
4114f83c4d1SDean Michael Berris /// FunctionRecord: 8 byte record with FunctionId, entry/exit, and TSC delta.
4124f83c4d1SDean Michael Berris Error loadFDRLog(StringRef Data, XRayFileHeader &FileHeader,
4134f83c4d1SDean Michael Berris                  std::vector<XRayRecord> &Records) {
4144f83c4d1SDean Michael Berris   if (Data.size() < 32)
4154f83c4d1SDean Michael Berris     return make_error<StringError>(
4164f83c4d1SDean Michael Berris         "Not enough bytes for an XRay log.",
4174f83c4d1SDean Michael Berris         std::make_error_code(std::errc::invalid_argument));
4184f83c4d1SDean Michael Berris 
4194f83c4d1SDean Michael Berris   // For an FDR log, there are records sized 16 and 8 bytes.
42060c24878SDean Michael Berris   // There actually may be no records if no non-trivial functions are
42160c24878SDean Michael Berris   // instrumented.
42260c24878SDean Michael Berris   if (Data.size() % 8 != 0)
4234f83c4d1SDean Michael Berris     return make_error<StringError>(
4244f83c4d1SDean Michael Berris         "Invalid-sized XRay data.",
4254f83c4d1SDean Michael Berris         std::make_error_code(std::errc::invalid_argument));
4264f83c4d1SDean Michael Berris 
4274f83c4d1SDean Michael Berris   if (auto E = readBinaryFormatHeader(Data, FileHeader))
4284f83c4d1SDean Michael Berris     return E;
4294f83c4d1SDean Michael Berris 
43060c24878SDean Michael Berris   uint64_t BufferSize = 0;
43160c24878SDean Michael Berris   {
43260c24878SDean Michael Berris     StringRef ExtraDataRef(FileHeader.FreeFormData, 16);
43360c24878SDean Michael Berris     DataExtractor ExtraDataExtractor(ExtraDataRef, true, 8);
43460c24878SDean Michael Berris     uint32_t ExtraDataOffset = 0;
43560c24878SDean Michael Berris     BufferSize = ExtraDataExtractor.getU64(&ExtraDataOffset);
43660c24878SDean Michael Berris   }
43760c24878SDean Michael Berris   FDRState State{0,          0, 0, FDRState::Token::NEW_BUFFER_RECORD_OR_EOF,
43860c24878SDean Michael Berris                  BufferSize, 0};
4394f83c4d1SDean Michael Berris   // RecordSize will tell the loop how far to seek ahead based on the record
4404f83c4d1SDean Michael Berris   // type that we have just read.
4414f83c4d1SDean Michael Berris   size_t RecordSize = 0;
4424f83c4d1SDean Michael Berris   for (auto S = Data.drop_front(32); !S.empty(); S = S.drop_front(RecordSize)) {
4434f83c4d1SDean Michael Berris     DataExtractor RecordExtractor(S, true, 8);
4444f83c4d1SDean Michael Berris     uint32_t OffsetPtr = 0;
44560c24878SDean Michael Berris     if (State.Expects == FDRState::Token::SCAN_TO_END_OF_THREAD_BUF) {
44660c24878SDean Michael Berris       RecordSize = State.CurrentBufferSize - State.CurrentBufferConsumed;
447d78db156SMartin Pelikan       if (S.size() < RecordSize) {
44860c24878SDean Michael Berris         return make_error<StringError>(
449d78db156SMartin Pelikan             Twine("Incomplete thread buffer. Expected at least ") +
450d78db156SMartin Pelikan                 Twine(RecordSize) + " bytes but found " + Twine(S.size()),
45160c24878SDean Michael Berris             make_error_code(std::errc::invalid_argument));
45260c24878SDean Michael Berris       }
45360c24878SDean Michael Berris       State.CurrentBufferConsumed = 0;
45460c24878SDean Michael Berris       State.Expects = FDRState::Token::NEW_BUFFER_RECORD_OR_EOF;
45560c24878SDean Michael Berris       continue;
45660c24878SDean Michael Berris     }
4574f83c4d1SDean Michael Berris     uint8_t BitField = RecordExtractor.getU8(&OffsetPtr);
4584f83c4d1SDean Michael Berris     bool isMetadataRecord = BitField & 0x01uL;
4594f83c4d1SDean Michael Berris     if (isMetadataRecord) {
4604f83c4d1SDean Michael Berris       RecordSize = 16;
461a7bbe448SDean Michael Berris       if (auto E = processFDRMetadataRecord(State, BitField, RecordExtractor,
462*10c873f1SMartin Pelikan                                             RecordSize, Records))
4634f83c4d1SDean Michael Berris         return E;
4644f83c4d1SDean Michael Berris     } else { // Process Function Record
4654f83c4d1SDean Michael Berris       RecordSize = 8;
4664f83c4d1SDean Michael Berris       if (auto E = processFDRFunctionRecord(State, BitField, RecordExtractor,
4674f83c4d1SDean Michael Berris                                             Records))
4684f83c4d1SDean Michael Berris         return E;
469d78db156SMartin Pelikan     }
47060c24878SDean Michael Berris     State.CurrentBufferConsumed += RecordSize;
4714f83c4d1SDean Michael Berris   }
472d78db156SMartin Pelikan 
473d78db156SMartin Pelikan   // Having iterated over everything we've been given, we've either consumed
474d78db156SMartin Pelikan   // everything and ended up in the end state, or were told to skip the rest.
475d78db156SMartin Pelikan   bool Finished = State.Expects == FDRState::Token::SCAN_TO_END_OF_THREAD_BUF &&
476d78db156SMartin Pelikan                   State.CurrentBufferSize == State.CurrentBufferConsumed;
477d78db156SMartin Pelikan   if (State.Expects != FDRState::Token::NEW_BUFFER_RECORD_OR_EOF && !Finished)
4784f83c4d1SDean Michael Berris     return make_error<StringError>(
47960c24878SDean Michael Berris         Twine("Encountered EOF with unexpected state expectation ") +
48060c24878SDean Michael Berris             fdrStateToTwine(State.Expects) +
48160c24878SDean Michael Berris             ". Remaining expected bytes in thread buffer total " +
48260c24878SDean Michael Berris             Twine(State.CurrentBufferSize - State.CurrentBufferConsumed),
4834f83c4d1SDean Michael Berris         std::make_error_code(std::errc::executable_format_error));
4844f83c4d1SDean Michael Berris 
4854f83c4d1SDean Michael Berris   return Error::success();
4864f83c4d1SDean Michael Berris }
4874f83c4d1SDean Michael Berris 
4884f83c4d1SDean Michael Berris Error loadYAMLLog(StringRef Data, XRayFileHeader &FileHeader,
4894f83c4d1SDean Michael Berris                   std::vector<XRayRecord> &Records) {
490d6c18657SDean Michael Berris   YAMLXRayTrace Trace;
491d6c18657SDean Michael Berris   Input In(Data);
492d6c18657SDean Michael Berris   In >> Trace;
493d6c18657SDean Michael Berris   if (In.error())
494d6c18657SDean Michael Berris     return make_error<StringError>("Failed loading YAML Data.", In.error());
495d6c18657SDean Michael Berris 
496d6c18657SDean Michael Berris   FileHeader.Version = Trace.Header.Version;
497d6c18657SDean Michael Berris   FileHeader.Type = Trace.Header.Type;
498d6c18657SDean Michael Berris   FileHeader.ConstantTSC = Trace.Header.ConstantTSC;
499d6c18657SDean Michael Berris   FileHeader.NonstopTSC = Trace.Header.NonstopTSC;
500d6c18657SDean Michael Berris   FileHeader.CycleFrequency = Trace.Header.CycleFrequency;
501d6c18657SDean Michael Berris 
502d6c18657SDean Michael Berris   if (FileHeader.Version != 1)
503d6c18657SDean Michael Berris     return make_error<StringError>(
504d6c18657SDean Michael Berris         Twine("Unsupported XRay file version: ") + Twine(FileHeader.Version),
505d6c18657SDean Michael Berris         std::make_error_code(std::errc::invalid_argument));
506d6c18657SDean Michael Berris 
507d6c18657SDean Michael Berris   Records.clear();
508d6c18657SDean Michael Berris   std::transform(Trace.Records.begin(), Trace.Records.end(),
509d6c18657SDean Michael Berris                  std::back_inserter(Records), [&](const YAMLXRayRecord &R) {
510d6c18657SDean Michael Berris                    return XRayRecord{R.RecordType, R.CPU, R.Type,
511d6c18657SDean Michael Berris                                      R.FuncId,     R.TSC, R.TId};
512d6c18657SDean Michael Berris                  });
513d6c18657SDean Michael Berris   return Error::success();
514d6c18657SDean Michael Berris }
51549a49fe8SBenjamin Kramer } // namespace
516d6c18657SDean Michael Berris 
517d6c18657SDean Michael Berris Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
518d6c18657SDean Michael Berris   int Fd;
519d6c18657SDean Michael Berris   if (auto EC = sys::fs::openFileForRead(Filename, Fd)) {
520d6c18657SDean Michael Berris     return make_error<StringError>(
521d6c18657SDean Michael Berris         Twine("Cannot read log from '") + Filename + "'", EC);
522d6c18657SDean Michael Berris   }
523d6c18657SDean Michael Berris 
524d6c18657SDean Michael Berris   uint64_t FileSize;
525d6c18657SDean Michael Berris   if (auto EC = sys::fs::file_size(Filename, FileSize)) {
526d6c18657SDean Michael Berris     return make_error<StringError>(
527d6c18657SDean Michael Berris         Twine("Cannot read log from '") + Filename + "'", EC);
528d6c18657SDean Michael Berris   }
529d6c18657SDean Michael Berris   if (FileSize < 4) {
530d6c18657SDean Michael Berris     return make_error<StringError>(
531d6c18657SDean Michael Berris         Twine("File '") + Filename + "' too small for XRay.",
53284da6615SHans Wennborg         std::make_error_code(std::errc::executable_format_error));
533d6c18657SDean Michael Berris   }
534d6c18657SDean Michael Berris 
535d78db156SMartin Pelikan   // Map the opened file into memory and use a StringRef to access it later.
536d6c18657SDean Michael Berris   std::error_code EC;
537d6c18657SDean Michael Berris   sys::fs::mapped_file_region MappedFile(
538d6c18657SDean Michael Berris       Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
539d6c18657SDean Michael Berris   if (EC) {
540d6c18657SDean Michael Berris     return make_error<StringError>(
541d6c18657SDean Michael Berris         Twine("Cannot read log from '") + Filename + "'", EC);
542d6c18657SDean Michael Berris   }
543d78db156SMartin Pelikan   auto Data = StringRef(MappedFile.data(), MappedFile.size());
544d6c18657SDean Michael Berris 
545d6c18657SDean Michael Berris   // Attempt to detect the file type using file magic. We have a slight bias
546d6c18657SDean Michael Berris   // towards the binary format, and we do this by making sure that the first 4
547d6c18657SDean Michael Berris   // bytes of the binary file is some combination of the following byte
548d78db156SMartin Pelikan   // patterns: (observe the code loading them assumes they're little endian)
549d6c18657SDean Michael Berris   //
550d78db156SMartin Pelikan   //   0x01 0x00 0x00 0x00 - version 1, "naive" format
551d78db156SMartin Pelikan   //   0x01 0x00 0x01 0x00 - version 1, "flight data recorder" format
552d6c18657SDean Michael Berris   //
553d78db156SMartin Pelikan   // YAML files don't typically have those first four bytes as valid text so we
554d6c18657SDean Michael Berris   // try loading assuming YAML if we don't find these bytes.
555d6c18657SDean Michael Berris   //
556d6c18657SDean Michael Berris   // Only if we can't load either the binary or the YAML format will we yield an
557d6c18657SDean Michael Berris   // error.
558d6c18657SDean Michael Berris   StringRef Magic(MappedFile.data(), 4);
559d6c18657SDean Michael Berris   DataExtractor HeaderExtractor(Magic, true, 8);
560d6c18657SDean Michael Berris   uint32_t OffsetPtr = 0;
561d6c18657SDean Michael Berris   uint16_t Version = HeaderExtractor.getU16(&OffsetPtr);
562d6c18657SDean Michael Berris   uint16_t Type = HeaderExtractor.getU16(&OffsetPtr);
563d6c18657SDean Michael Berris 
5644f83c4d1SDean Michael Berris   enum BinaryFormatType { NAIVE_FORMAT = 0, FLIGHT_DATA_RECORDER_FORMAT = 1 };
5654f83c4d1SDean Michael Berris 
566d6c18657SDean Michael Berris   Trace T;
5670f84a7d3SDean Michael Berris   if (Type == NAIVE_FORMAT && (Version == 1 || Version == 2)) {
5680f84a7d3SDean Michael Berris     if (auto E = loadNaiveFormatLog(Data, T.FileHeader, T.Records))
5694f83c4d1SDean Michael Berris       return std::move(E);
5704f83c4d1SDean Michael Berris   } else if (Version == 1 && Type == FLIGHT_DATA_RECORDER_FORMAT) {
571d78db156SMartin Pelikan     if (auto E = loadFDRLog(Data, T.FileHeader, T.Records))
572d6c18657SDean Michael Berris       return std::move(E);
573d6c18657SDean Michael Berris   } else {
574d78db156SMartin Pelikan     if (auto E = loadYAMLLog(Data, T.FileHeader, T.Records))
575d6c18657SDean Michael Berris       return std::move(E);
576d6c18657SDean Michael Berris   }
577d6c18657SDean Michael Berris 
578d6c18657SDean Michael Berris   if (Sort)
579d6c18657SDean Michael Berris     std::sort(T.Records.begin(), T.Records.end(),
580d6c18657SDean Michael Berris               [&](const XRayRecord &L, const XRayRecord &R) {
581d6c18657SDean Michael Berris                 return L.TSC < R.TSC;
582d6c18657SDean Michael Berris               });
583d6c18657SDean Michael Berris 
584d6c18657SDean Michael Berris   return std::move(T);
585d6c18657SDean Michael Berris }
586