blob: e90396959fb2be7348da3bfd0e66b8b7fa7e96a1 [file] [log] [blame]
Dean Michael Berrisd6c18652017-01-11 06:39:09 +00001//===- Trace.cpp - XRay Trace Loading implementation. ---------------------===//
Dean Michael Berrisf8f909f2017-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 Berrisd6c18652017-01-11 06:39:09 +000013#include "llvm/XRay/Trace.h"
14#include "llvm/ADT/STLExtras.h"
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000015#include "llvm/Support/DataExtractor.h"
Dean Michael Berrisd6c18652017-01-11 06:39:09 +000016#include "llvm/Support/Error.h"
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000017#include "llvm/Support/FileSystem.h"
Dean Michael Berrisd6c18652017-01-11 06:39:09 +000018#include "llvm/XRay/YAMLXRayRecord.h"
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000019
20using namespace llvm;
21using namespace llvm::xray;
22using llvm::yaml::Input;
23
Benjamin Kramer49a49fe2017-08-20 13:03:48 +000024namespace {
Dean Michael Berrisd6c18652017-01-11 06:39:09 +000025using XRayRecordStorage =
26 std::aligned_storage<sizeof(XRayRecord), alignof(XRayRecord)>::type;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000027
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +000028// Populates the FileHeader reference by reading the first 32 bytes of the file.
29Error readBinaryFormatHeader(StringRef Data, XRayFileHeader &FileHeader) {
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000030 // 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
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000041
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);
Dean Michael Berris60c24872017-03-29 06:10:12 +000050 std::memcpy(&FileHeader.FreeFormData, Data.bytes_begin() + OffsetPtr, 16);
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +000051 if (FileHeader.Version != 1 && FileHeader.Version != 2)
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000052 return make_error<StringError>(
53 Twine("Unsupported XRay file version: ") + Twine(FileHeader.Version),
54 std::make_error_code(std::errc::invalid_argument));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +000055 return Error::success();
56}
57
58Error loadNaiveFormatLog(StringRef Data, XRayFileHeader &FileHeader,
59 std::vector<XRayRecord> &Records) {
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +000060 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;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000072
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;
Dean Michael Berris0a465d72017-10-05 05:18:17 +000085 switch (auto RecordType = RecordExtractor.getU16(&OffsetPtr)) {
86 case 0: { // Normal records.
87 Records.emplace_back();
88 auto &Record = Records.back();
89 Record.RecordType = RecordType;
90 Record.CPU = RecordExtractor.getU8(&OffsetPtr);
91 auto Type = RecordExtractor.getU8(&OffsetPtr);
92 switch (Type) {
93 case 0:
94 Record.Type = RecordTypes::ENTER;
95 break;
96 case 1:
97 Record.Type = RecordTypes::EXIT;
98 break;
99 case 2:
100 Record.Type = RecordTypes::TAIL_EXIT;
101 break;
102 case 3:
103 Record.Type = RecordTypes::ENTER_ARG;
104 break;
105 default:
106 return make_error<StringError>(
107 Twine("Unknown record type '") + Twine(int{Type}) + "'",
108 std::make_error_code(std::errc::executable_format_error));
109 }
110 Record.FuncId = RecordExtractor.getSigned(&OffsetPtr, sizeof(int32_t));
111 Record.TSC = RecordExtractor.getU64(&OffsetPtr);
112 Record.TId = RecordExtractor.getU32(&OffsetPtr);
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000113 break;
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000114 }
115 case 1: { // Arg payload record.
116 auto &Record = Records.back();
117 // Advance two bytes to avoid padding.
118 OffsetPtr += 2;
119 int32_t FuncId = RecordExtractor.getSigned(&OffsetPtr, sizeof(int32_t));
120 auto TId = RecordExtractor.getU32(&OffsetPtr);
121 if (Record.FuncId != FuncId || Record.TId != TId)
122 return make_error<StringError>(
123 Twine("Corrupted log, found payload following non-matching "
124 "function + thread record. Record for ") +
125 Twine(Record.FuncId) + " != " + Twine(FuncId),
126 std::make_error_code(std::errc::executable_format_error));
127 // Advance another four bytes to avoid padding.
128 OffsetPtr += 4;
129 auto Arg = RecordExtractor.getU64(&OffsetPtr);
130 Record.CallArgs.push_back(Arg);
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000131 break;
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000132 }
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000133 default:
134 return make_error<StringError>(
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000135 Twine("Unknown record type == ") + Twine(RecordType),
NAKAMURA Takumib09bec22017-01-11 01:06:57 +0000136 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000137 }
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000138 }
139 return Error::success();
140}
141
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000142/// When reading from a Flight Data Recorder mode log, metadata records are
143/// sparse compared to packed function records, so we must maintain state as we
144/// read through the sequence of entries. This allows the reader to denormalize
145/// the CPUId and Thread Id onto each Function Record and transform delta
146/// encoded TSC values into absolute encodings on each record.
147struct FDRState {
148 uint16_t CPUId;
149 uint16_t ThreadId;
150 uint64_t BaseTSC;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000151
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000152 /// Encode some of the state transitions for the FDR log reader as explicit
153 /// checks. These are expectations for the next Record in the stream.
154 enum class Token {
155 NEW_BUFFER_RECORD_OR_EOF,
156 WALLCLOCK_RECORD,
157 NEW_CPU_ID_RECORD,
Dean Michael Berris60c24872017-03-29 06:10:12 +0000158 FUNCTION_SEQUENCE,
159 SCAN_TO_END_OF_THREAD_BUF,
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000160 CUSTOM_EVENT_DATA,
Martin Pelikan10c873f2017-09-27 04:48:03 +0000161 CALL_ARGUMENT,
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000162 };
163 Token Expects;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000164
Dean Michael Berris60c24872017-03-29 06:10:12 +0000165 // Each threads buffer may have trailing garbage to scan over, so we track our
166 // progress.
167 uint64_t CurrentBufferSize;
168 uint64_t CurrentBufferConsumed;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000169};
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000170
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000171const char *fdrStateToTwine(const FDRState::Token &state) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000172 switch (state) {
173 case FDRState::Token::NEW_BUFFER_RECORD_OR_EOF:
174 return "NEW_BUFFER_RECORD_OR_EOF";
175 case FDRState::Token::WALLCLOCK_RECORD:
176 return "WALLCLOCK_RECORD";
177 case FDRState::Token::NEW_CPU_ID_RECORD:
178 return "NEW_CPU_ID_RECORD";
179 case FDRState::Token::FUNCTION_SEQUENCE:
180 return "FUNCTION_SEQUENCE";
181 case FDRState::Token::SCAN_TO_END_OF_THREAD_BUF:
182 return "SCAN_TO_END_OF_THREAD_BUF";
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000183 case FDRState::Token::CUSTOM_EVENT_DATA:
184 return "CUSTOM_EVENT_DATA";
Martin Pelikan10c873f2017-09-27 04:48:03 +0000185 case FDRState::Token::CALL_ARGUMENT:
186 return "CALL_ARGUMENT";
Dean Michael Berris60c24872017-03-29 06:10:12 +0000187 }
188 return "UNKNOWN";
189}
190
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000191/// State transition when a NewBufferRecord is encountered.
192Error processFDRNewBufferRecord(FDRState &State, uint8_t RecordFirstByte,
193 DataExtractor &RecordExtractor) {
194
Dean Michael Berris60c24872017-03-29 06:10:12 +0000195 if (State.Expects != FDRState::Token::NEW_BUFFER_RECORD_OR_EOF)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000196 return make_error<StringError>(
197 "Malformed log. Read New Buffer record kind out of sequence",
198 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000199 uint32_t OffsetPtr = 1; // 1 byte into record.
200 State.ThreadId = RecordExtractor.getU16(&OffsetPtr);
201 State.Expects = FDRState::Token::WALLCLOCK_RECORD;
202 return Error::success();
203}
204
205/// State transition when an EndOfBufferRecord is encountered.
206Error processFDREndOfBufferRecord(FDRState &State, uint8_t RecordFirstByte,
207 DataExtractor &RecordExtractor) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000208 if (State.Expects == FDRState::Token::NEW_BUFFER_RECORD_OR_EOF)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000209 return make_error<StringError>(
210 "Malformed log. Received EOB message without current buffer.",
211 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris60c24872017-03-29 06:10:12 +0000212 State.Expects = FDRState::Token::SCAN_TO_END_OF_THREAD_BUF;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000213 return Error::success();
214}
215
216/// State transition when a NewCPUIdRecord is encountered.
217Error processFDRNewCPUIdRecord(FDRState &State, uint8_t RecordFirstByte,
218 DataExtractor &RecordExtractor) {
219 if (State.Expects != FDRState::Token::FUNCTION_SEQUENCE &&
Dean Michael Berris60c24872017-03-29 06:10:12 +0000220 State.Expects != FDRState::Token::NEW_CPU_ID_RECORD)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000221 return make_error<StringError>(
222 "Malformed log. Read NewCPUId record kind out of sequence",
223 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000224 uint32_t OffsetPtr = 1; // Read starting after the first byte.
225 State.CPUId = RecordExtractor.getU16(&OffsetPtr);
226 State.BaseTSC = RecordExtractor.getU64(&OffsetPtr);
227 State.Expects = FDRState::Token::FUNCTION_SEQUENCE;
228 return Error::success();
229}
230
231/// State transition when a TSCWrapRecord (overflow detection) is encountered.
232Error processFDRTSCWrapRecord(FDRState &State, uint8_t RecordFirstByte,
233 DataExtractor &RecordExtractor) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000234 if (State.Expects != FDRState::Token::FUNCTION_SEQUENCE)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000235 return make_error<StringError>(
236 "Malformed log. Read TSCWrap record kind out of sequence",
237 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000238 uint32_t OffsetPtr = 1; // Read starting after the first byte.
239 State.BaseTSC = RecordExtractor.getU64(&OffsetPtr);
240 return Error::success();
241}
242
243/// State transition when a WallTimeMarkerRecord is encountered.
244Error processFDRWallTimeRecord(FDRState &State, uint8_t RecordFirstByte,
245 DataExtractor &RecordExtractor) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000246 if (State.Expects != FDRState::Token::WALLCLOCK_RECORD)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000247 return make_error<StringError>(
248 "Malformed log. Read Wallclock record kind out of sequence",
249 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000250 // We don't encode the wall time into any of the records.
251 // XRayRecords are concerned with the TSC instead.
252 State.Expects = FDRState::Token::NEW_CPU_ID_RECORD;
253 return Error::success();
254}
255
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000256/// State transition when a CustomEventMarker is encountered.
257Error processCustomEventMarker(FDRState &State, uint8_t RecordFirstByte,
258 DataExtractor &RecordExtractor,
259 size_t &RecordSize) {
260 // We can encounter a CustomEventMarker anywhere in the log, so we can handle
Keith Wyss3d0bc9e2017-08-02 21:47:27 +0000261 // it regardless of the expectation. However, we do set the expectation to
262 // read a set number of fixed bytes, as described in the metadata.
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000263 uint32_t OffsetPtr = 1; // Read after the first byte.
264 uint32_t DataSize = RecordExtractor.getU32(&OffsetPtr);
265 uint64_t TSC = RecordExtractor.getU64(&OffsetPtr);
266
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000267 // FIXME: Actually represent the record through the API. For now we only
268 // skip through the data.
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000269 (void)TSC;
270 RecordSize = 16 + DataSize;
271 return Error::success();
272}
273
Martin Pelikan10c873f2017-09-27 04:48:03 +0000274/// State transition when a CallArgumentRecord is encountered.
275Error processFDRCallArgumentRecord(FDRState &State, uint8_t RecordFirstByte,
276 DataExtractor &RecordExtractor,
277 std::vector<XRayRecord> &Records) {
278 uint32_t OffsetPtr = 1; // Read starting after the first byte.
279 auto &Enter = Records.back();
280
281 if (Enter.Type != RecordTypes::ENTER)
282 return make_error<StringError>(
283 "CallArgument needs to be right after a function entry",
284 std::make_error_code(std::errc::executable_format_error));
285 Enter.Type = RecordTypes::ENTER_ARG;
286 Enter.CallArgs.emplace_back(RecordExtractor.getU64(&OffsetPtr));
287 return Error::success();
288}
289
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000290/// Advances the state machine for reading the FDR record type by reading one
Keith Wysse96152a2017-04-06 03:32:01 +0000291/// Metadata Record and updating the State appropriately based on the kind of
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000292/// record encountered. The RecordKind is encoded in the first byte of the
293/// Record, which the caller should pass in because they have already read it
294/// to determine that this is a metadata record as opposed to a function record.
295Error processFDRMetadataRecord(FDRState &State, uint8_t RecordFirstByte,
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000296 DataExtractor &RecordExtractor,
Martin Pelikan10c873f2017-09-27 04:48:03 +0000297 size_t &RecordSize,
298 std::vector<XRayRecord> &Records) {
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000299 // The remaining 7 bits are the RecordKind enum.
300 uint8_t RecordKind = RecordFirstByte >> 1;
301 switch (RecordKind) {
302 case 0: // NewBuffer
303 if (auto E =
304 processFDRNewBufferRecord(State, RecordFirstByte, RecordExtractor))
305 return E;
306 break;
307 case 1: // EndOfBuffer
308 if (auto E = processFDREndOfBufferRecord(State, RecordFirstByte,
309 RecordExtractor))
310 return E;
311 break;
312 case 2: // NewCPUId
313 if (auto E =
314 processFDRNewCPUIdRecord(State, RecordFirstByte, RecordExtractor))
315 return E;
316 break;
317 case 3: // TSCWrap
318 if (auto E =
319 processFDRTSCWrapRecord(State, RecordFirstByte, RecordExtractor))
320 return E;
321 break;
322 case 4: // WallTimeMarker
323 if (auto E =
324 processFDRWallTimeRecord(State, RecordFirstByte, RecordExtractor))
325 return E;
326 break;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000327 case 5: // CustomEventMarker
328 if (auto E = processCustomEventMarker(State, RecordFirstByte,
329 RecordExtractor, RecordSize))
330 return E;
331 break;
Martin Pelikan10c873f2017-09-27 04:48:03 +0000332 case 6: // CallArgument
333 if (auto E = processFDRCallArgumentRecord(State, RecordFirstByte,
334 RecordExtractor, Records))
335 return E;
336 break;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000337 default:
338 // Widen the record type to uint16_t to prevent conversion to char.
339 return make_error<StringError>(
340 Twine("Illegal metadata record type: ")
341 .concat(Twine(static_cast<unsigned>(RecordKind))),
342 std::make_error_code(std::errc::executable_format_error));
343 }
344 return Error::success();
345}
346
347/// Reads a function record from an FDR format log, appending a new XRayRecord
348/// to the vector being populated and updating the State with a new value
349/// reference value to interpret TSC deltas.
350///
351/// The XRayRecord constructed includes information from the function record
352/// processed here as well as Thread ID and CPU ID formerly extracted into
353/// State.
354Error processFDRFunctionRecord(FDRState &State, uint8_t RecordFirstByte,
355 DataExtractor &RecordExtractor,
356 std::vector<XRayRecord> &Records) {
357 switch (State.Expects) {
358 case FDRState::Token::NEW_BUFFER_RECORD_OR_EOF:
359 return make_error<StringError>(
360 "Malformed log. Received Function Record before new buffer setup.",
361 std::make_error_code(std::errc::executable_format_error));
362 case FDRState::Token::WALLCLOCK_RECORD:
363 return make_error<StringError>(
364 "Malformed log. Received Function Record when expecting wallclock.",
365 std::make_error_code(std::errc::executable_format_error));
366 case FDRState::Token::NEW_CPU_ID_RECORD:
367 return make_error<StringError>(
368 "Malformed log. Received Function Record before first CPU record.",
369 std::make_error_code(std::errc::executable_format_error));
370 default:
371 Records.emplace_back();
372 auto &Record = Records.back();
373 Record.RecordType = 0; // Record is type NORMAL.
374 // Strip off record type bit and use the next three bits.
375 uint8_t RecordType = (RecordFirstByte >> 1) & 0x07;
376 switch (RecordType) {
377 case static_cast<uint8_t>(RecordTypes::ENTER):
378 Record.Type = RecordTypes::ENTER;
379 break;
380 case static_cast<uint8_t>(RecordTypes::EXIT):
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000381 Record.Type = RecordTypes::EXIT;
382 break;
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000383 case static_cast<uint8_t>(RecordTypes::TAIL_EXIT):
384 Record.Type = RecordTypes::TAIL_EXIT;
385 break;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000386 default:
Martin Pelikand78db152017-09-15 04:22:16 +0000387 // Cast to an unsigned integer to not interpret the record type as a char.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000388 return make_error<StringError>(
389 Twine("Illegal function record type: ")
390 .concat(Twine(static_cast<unsigned>(RecordType))),
391 std::make_error_code(std::errc::executable_format_error));
392 }
393 Record.CPU = State.CPUId;
394 Record.TId = State.ThreadId;
Keith Wyss3d0bc9e2017-08-02 21:47:27 +0000395 // Back up to read first 32 bits, including the 4 we pulled RecordType
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000396 // and RecordKind out of. The remaining 28 are FunctionId.
397 uint32_t OffsetPtr = 0;
398 // Despite function Id being a signed int on XRayRecord,
399 // when it is written to an FDR format, the top bits are truncated,
400 // so it is effectively an unsigned value. When we shift off the
401 // top four bits, we want the shift to be logical, so we read as
402 // uint32_t.
403 uint32_t FuncIdBitField = RecordExtractor.getU32(&OffsetPtr);
404 Record.FuncId = FuncIdBitField >> 4;
405 // FunctionRecords have a 32 bit delta from the previous absolute TSC
406 // or TSC delta. If this would overflow, we should read a TSCWrap record
407 // with an absolute TSC reading.
Martin Pelikand78db152017-09-15 04:22:16 +0000408 uint64_t NewTSC = State.BaseTSC + RecordExtractor.getU32(&OffsetPtr);
409 State.BaseTSC = NewTSC;
410 Record.TSC = NewTSC;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000411 }
412 return Error::success();
413}
414
415/// Reads a log in FDR mode for version 1 of this binary format. FDR mode is
416/// defined as part of the compiler-rt project in xray_fdr_logging.h, and such
417/// a log consists of the familiar 32 bit XRayHeader, followed by sequences of
418/// of interspersed 16 byte Metadata Records and 8 byte Function Records.
419///
420/// The following is an attempt to document the grammar of the format, which is
421/// parsed by this function for little-endian machines. Since the format makes
Martin Pelikand78db152017-09-15 04:22:16 +0000422/// use of BitFields, when we support big-endian architectures, we will need to
Simon Pilgrim68168d12017-03-30 12:59:53 +0000423/// adjust not only the endianness parameter to llvm's RecordExtractor, but also
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000424/// the bit twiddling logic, which is consistent with the little-endian
425/// convention that BitFields within a struct will first be packed into the
426/// least significant bits the address they belong to.
427///
428/// We expect a format complying with the grammar in the following pseudo-EBNF.
429///
430/// FDRLog: XRayFileHeader ThreadBuffer*
Keith Wyss3d0bc9e2017-08-02 21:47:27 +0000431/// XRayFileHeader: 32 bytes to identify the log as FDR with machine metadata.
432/// Includes BufferSize
433/// ThreadBuffer: NewBuffer WallClockTime NewCPUId FunctionSequence EOB
Dean Michael Berris60c24872017-03-29 06:10:12 +0000434/// BufSize: 8 byte unsigned integer indicating how large the buffer is.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000435/// NewBuffer: 16 byte metadata record with Thread Id.
436/// WallClockTime: 16 byte metadata record with human readable time.
437/// NewCPUId: 16 byte metadata record with CPUId and a 64 bit TSC reading.
Dean Michael Berris60c24872017-03-29 06:10:12 +0000438/// EOB: 16 byte record in a thread buffer plus mem garbage to fill BufSize.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000439/// FunctionSequence: NewCPUId | TSCWrap | FunctionRecord
440/// TSCWrap: 16 byte metadata record with a full 64 bit TSC reading.
441/// FunctionRecord: 8 byte record with FunctionId, entry/exit, and TSC delta.
442Error loadFDRLog(StringRef Data, XRayFileHeader &FileHeader,
443 std::vector<XRayRecord> &Records) {
444 if (Data.size() < 32)
445 return make_error<StringError>(
446 "Not enough bytes for an XRay log.",
447 std::make_error_code(std::errc::invalid_argument));
448
449 // For an FDR log, there are records sized 16 and 8 bytes.
Dean Michael Berris60c24872017-03-29 06:10:12 +0000450 // There actually may be no records if no non-trivial functions are
451 // instrumented.
452 if (Data.size() % 8 != 0)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000453 return make_error<StringError>(
454 "Invalid-sized XRay data.",
455 std::make_error_code(std::errc::invalid_argument));
456
457 if (auto E = readBinaryFormatHeader(Data, FileHeader))
458 return E;
459
Dean Michael Berris60c24872017-03-29 06:10:12 +0000460 uint64_t BufferSize = 0;
461 {
462 StringRef ExtraDataRef(FileHeader.FreeFormData, 16);
463 DataExtractor ExtraDataExtractor(ExtraDataRef, true, 8);
464 uint32_t ExtraDataOffset = 0;
465 BufferSize = ExtraDataExtractor.getU64(&ExtraDataOffset);
466 }
467 FDRState State{0, 0, 0, FDRState::Token::NEW_BUFFER_RECORD_OR_EOF,
468 BufferSize, 0};
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000469 // RecordSize will tell the loop how far to seek ahead based on the record
470 // type that we have just read.
471 size_t RecordSize = 0;
472 for (auto S = Data.drop_front(32); !S.empty(); S = S.drop_front(RecordSize)) {
473 DataExtractor RecordExtractor(S, true, 8);
474 uint32_t OffsetPtr = 0;
Dean Michael Berris60c24872017-03-29 06:10:12 +0000475 if (State.Expects == FDRState::Token::SCAN_TO_END_OF_THREAD_BUF) {
476 RecordSize = State.CurrentBufferSize - State.CurrentBufferConsumed;
Martin Pelikand78db152017-09-15 04:22:16 +0000477 if (S.size() < RecordSize) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000478 return make_error<StringError>(
Martin Pelikand78db152017-09-15 04:22:16 +0000479 Twine("Incomplete thread buffer. Expected at least ") +
480 Twine(RecordSize) + " bytes but found " + Twine(S.size()),
Dean Michael Berris60c24872017-03-29 06:10:12 +0000481 make_error_code(std::errc::invalid_argument));
482 }
483 State.CurrentBufferConsumed = 0;
484 State.Expects = FDRState::Token::NEW_BUFFER_RECORD_OR_EOF;
485 continue;
486 }
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000487 uint8_t BitField = RecordExtractor.getU8(&OffsetPtr);
488 bool isMetadataRecord = BitField & 0x01uL;
489 if (isMetadataRecord) {
490 RecordSize = 16;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000491 if (auto E = processFDRMetadataRecord(State, BitField, RecordExtractor,
Martin Pelikan10c873f2017-09-27 04:48:03 +0000492 RecordSize, Records))
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000493 return E;
494 } else { // Process Function Record
495 RecordSize = 8;
496 if (auto E = processFDRFunctionRecord(State, BitField, RecordExtractor,
497 Records))
498 return E;
499 }
Martin Pelikand78db152017-09-15 04:22:16 +0000500 State.CurrentBufferConsumed += RecordSize;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000501 }
Martin Pelikand78db152017-09-15 04:22:16 +0000502
503 // Having iterated over everything we've been given, we've either consumed
504 // everything and ended up in the end state, or were told to skip the rest.
505 bool Finished = State.Expects == FDRState::Token::SCAN_TO_END_OF_THREAD_BUF &&
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000506 State.CurrentBufferSize == State.CurrentBufferConsumed;
Martin Pelikand78db152017-09-15 04:22:16 +0000507 if (State.Expects != FDRState::Token::NEW_BUFFER_RECORD_OR_EOF && !Finished)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000508 return make_error<StringError>(
Dean Michael Berris60c24872017-03-29 06:10:12 +0000509 Twine("Encountered EOF with unexpected state expectation ") +
510 fdrStateToTwine(State.Expects) +
511 ". Remaining expected bytes in thread buffer total " +
512 Twine(State.CurrentBufferSize - State.CurrentBufferConsumed),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000513 std::make_error_code(std::errc::executable_format_error));
514
515 return Error::success();
516}
517
518Error loadYAMLLog(StringRef Data, XRayFileHeader &FileHeader,
519 std::vector<XRayRecord> &Records) {
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000520 YAMLXRayTrace Trace;
521 Input In(Data);
522 In >> Trace;
523 if (In.error())
524 return make_error<StringError>("Failed loading YAML Data.", In.error());
525
526 FileHeader.Version = Trace.Header.Version;
527 FileHeader.Type = Trace.Header.Type;
528 FileHeader.ConstantTSC = Trace.Header.ConstantTSC;
529 FileHeader.NonstopTSC = Trace.Header.NonstopTSC;
530 FileHeader.CycleFrequency = Trace.Header.CycleFrequency;
531
532 if (FileHeader.Version != 1)
533 return make_error<StringError>(
534 Twine("Unsupported XRay file version: ") + Twine(FileHeader.Version),
535 std::make_error_code(std::errc::invalid_argument));
536
537 Records.clear();
538 std::transform(Trace.Records.begin(), Trace.Records.end(),
539 std::back_inserter(Records), [&](const YAMLXRayRecord &R) {
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000540 return XRayRecord{R.RecordType, R.CPU, R.Type, R.FuncId,
541 R.TSC, R.TId, R.CallArgs};
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000542 });
543 return Error::success();
544}
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000545} // namespace
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000546
547Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
548 int Fd;
549 if (auto EC = sys::fs::openFileForRead(Filename, Fd)) {
550 return make_error<StringError>(
551 Twine("Cannot read log from '") + Filename + "'", EC);
552 }
553
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000554 uint64_t FileSize;
555 if (auto EC = sys::fs::file_size(Filename, FileSize)) {
556 return make_error<StringError>(
557 Twine("Cannot read log from '") + Filename + "'", EC);
558 }
559 if (FileSize < 4) {
560 return make_error<StringError>(
561 Twine("File '") + Filename + "' too small for XRay.",
Hans Wennborg84da6612017-01-12 18:33:14 +0000562 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000563 }
564
Martin Pelikand78db152017-09-15 04:22:16 +0000565 // Map the opened file into memory and use a StringRef to access it later.
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000566 std::error_code EC;
567 sys::fs::mapped_file_region MappedFile(
568 Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
569 if (EC) {
570 return make_error<StringError>(
571 Twine("Cannot read log from '") + Filename + "'", EC);
572 }
Martin Pelikand78db152017-09-15 04:22:16 +0000573 auto Data = StringRef(MappedFile.data(), MappedFile.size());
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000574
575 // Attempt to detect the file type using file magic. We have a slight bias
576 // towards the binary format, and we do this by making sure that the first 4
577 // bytes of the binary file is some combination of the following byte
Martin Pelikand78db152017-09-15 04:22:16 +0000578 // patterns: (observe the code loading them assumes they're little endian)
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000579 //
Martin Pelikand78db152017-09-15 04:22:16 +0000580 // 0x01 0x00 0x00 0x00 - version 1, "naive" format
581 // 0x01 0x00 0x01 0x00 - version 1, "flight data recorder" format
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000582 //
Martin Pelikand78db152017-09-15 04:22:16 +0000583 // YAML files don't typically have those first four bytes as valid text so we
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000584 // try loading assuming YAML if we don't find these bytes.
585 //
586 // Only if we can't load either the binary or the YAML format will we yield an
587 // error.
588 StringRef Magic(MappedFile.data(), 4);
589 DataExtractor HeaderExtractor(Magic, true, 8);
590 uint32_t OffsetPtr = 0;
591 uint16_t Version = HeaderExtractor.getU16(&OffsetPtr);
592 uint16_t Type = HeaderExtractor.getU16(&OffsetPtr);
593
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000594 enum BinaryFormatType { NAIVE_FORMAT = 0, FLIGHT_DATA_RECORDER_FORMAT = 1 };
595
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000596 Trace T;
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000597 if (Type == NAIVE_FORMAT && (Version == 1 || Version == 2)) {
598 if (auto E = loadNaiveFormatLog(Data, T.FileHeader, T.Records))
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000599 return std::move(E);
600 } else if (Version == 1 && Type == FLIGHT_DATA_RECORDER_FORMAT) {
Martin Pelikand78db152017-09-15 04:22:16 +0000601 if (auto E = loadFDRLog(Data, T.FileHeader, T.Records))
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000602 return std::move(E);
603 } else {
Martin Pelikand78db152017-09-15 04:22:16 +0000604 if (auto E = loadYAMLLog(Data, T.FileHeader, T.Records))
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000605 return std::move(E);
606 }
607
608 if (Sort)
609 std::sort(T.Records.begin(), T.Records.end(),
610 [&](const XRayRecord &L, const XRayRecord &R) {
611 return L.TSC < R.TSC;
612 });
613
614 return std::move(T);
615}