blob: d8c54ddc2b93dd203f60ee119551364a663e7883 [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>(
Martin Pelikan79d729e2018-01-30 13:41:34 +0000123 Twine("Corrupted log, found arg payload following non-matching "
124 "function + thread record. Record for function ") +
125 Twine(Record.FuncId) + " != " + Twine(FuncId) + "; offset: " +
126 Twine(S.data() - Data.data()),
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000127 std::make_error_code(std::errc::executable_format_error));
128 // Advance another four bytes to avoid padding.
129 OffsetPtr += 4;
130 auto Arg = RecordExtractor.getU64(&OffsetPtr);
131 Record.CallArgs.push_back(Arg);
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000132 break;
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000133 }
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000134 default:
135 return make_error<StringError>(
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000136 Twine("Unknown record type == ") + Twine(RecordType),
NAKAMURA Takumib09bec22017-01-11 01:06:57 +0000137 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000138 }
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000139 }
140 return Error::success();
141}
142
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000143/// When reading from a Flight Data Recorder mode log, metadata records are
144/// sparse compared to packed function records, so we must maintain state as we
145/// read through the sequence of entries. This allows the reader to denormalize
146/// the CPUId and Thread Id onto each Function Record and transform delta
147/// encoded TSC values into absolute encodings on each record.
148struct FDRState {
149 uint16_t CPUId;
150 uint16_t ThreadId;
151 uint64_t BaseTSC;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000152
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000153 /// Encode some of the state transitions for the FDR log reader as explicit
154 /// checks. These are expectations for the next Record in the stream.
155 enum class Token {
156 NEW_BUFFER_RECORD_OR_EOF,
157 WALLCLOCK_RECORD,
158 NEW_CPU_ID_RECORD,
Dean Michael Berris60c24872017-03-29 06:10:12 +0000159 FUNCTION_SEQUENCE,
160 SCAN_TO_END_OF_THREAD_BUF,
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000161 CUSTOM_EVENT_DATA,
Martin Pelikan10c873f2017-09-27 04:48:03 +0000162 CALL_ARGUMENT,
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000163 BUFFER_EXTENTS,
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000164 };
165 Token Expects;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000166
Dean Michael Berris60c24872017-03-29 06:10:12 +0000167 // Each threads buffer may have trailing garbage to scan over, so we track our
168 // progress.
169 uint64_t CurrentBufferSize;
170 uint64_t CurrentBufferConsumed;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000171};
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000172
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000173const char *fdrStateToTwine(const FDRState::Token &state) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000174 switch (state) {
175 case FDRState::Token::NEW_BUFFER_RECORD_OR_EOF:
176 return "NEW_BUFFER_RECORD_OR_EOF";
177 case FDRState::Token::WALLCLOCK_RECORD:
178 return "WALLCLOCK_RECORD";
179 case FDRState::Token::NEW_CPU_ID_RECORD:
180 return "NEW_CPU_ID_RECORD";
181 case FDRState::Token::FUNCTION_SEQUENCE:
182 return "FUNCTION_SEQUENCE";
183 case FDRState::Token::SCAN_TO_END_OF_THREAD_BUF:
184 return "SCAN_TO_END_OF_THREAD_BUF";
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000185 case FDRState::Token::CUSTOM_EVENT_DATA:
186 return "CUSTOM_EVENT_DATA";
Martin Pelikan10c873f2017-09-27 04:48:03 +0000187 case FDRState::Token::CALL_ARGUMENT:
188 return "CALL_ARGUMENT";
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000189 case FDRState::Token::BUFFER_EXTENTS:
190 return "BUFFER_EXTENTS";
Dean Michael Berris60c24872017-03-29 06:10:12 +0000191 }
192 return "UNKNOWN";
193}
194
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000195/// State transition when a NewBufferRecord is encountered.
196Error processFDRNewBufferRecord(FDRState &State, uint8_t RecordFirstByte,
197 DataExtractor &RecordExtractor) {
198
Dean Michael Berris60c24872017-03-29 06:10:12 +0000199 if (State.Expects != FDRState::Token::NEW_BUFFER_RECORD_OR_EOF)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000200 return make_error<StringError>(
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000201 Twine("Malformed log. Read New Buffer record kind out of sequence; "
202 "expected: ") +
203 fdrStateToTwine(State.Expects),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000204 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000205 uint32_t OffsetPtr = 1; // 1 byte into record.
206 State.ThreadId = RecordExtractor.getU16(&OffsetPtr);
207 State.Expects = FDRState::Token::WALLCLOCK_RECORD;
208 return Error::success();
209}
210
211/// State transition when an EndOfBufferRecord is encountered.
212Error processFDREndOfBufferRecord(FDRState &State, uint8_t RecordFirstByte,
213 DataExtractor &RecordExtractor) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000214 if (State.Expects == FDRState::Token::NEW_BUFFER_RECORD_OR_EOF)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000215 return make_error<StringError>(
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000216 Twine("Malformed log. Received EOB message without current buffer; "
217 "expected: ") +
218 fdrStateToTwine(State.Expects),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000219 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris60c24872017-03-29 06:10:12 +0000220 State.Expects = FDRState::Token::SCAN_TO_END_OF_THREAD_BUF;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000221 return Error::success();
222}
223
224/// State transition when a NewCPUIdRecord is encountered.
225Error processFDRNewCPUIdRecord(FDRState &State, uint8_t RecordFirstByte,
226 DataExtractor &RecordExtractor) {
227 if (State.Expects != FDRState::Token::FUNCTION_SEQUENCE &&
Dean Michael Berris60c24872017-03-29 06:10:12 +0000228 State.Expects != FDRState::Token::NEW_CPU_ID_RECORD)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000229 return make_error<StringError>(
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000230 Twine("Malformed log. Read NewCPUId record kind out of sequence; "
231 "expected: ") +
232 fdrStateToTwine(State.Expects),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000233 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000234 uint32_t OffsetPtr = 1; // Read starting after the first byte.
235 State.CPUId = RecordExtractor.getU16(&OffsetPtr);
236 State.BaseTSC = RecordExtractor.getU64(&OffsetPtr);
237 State.Expects = FDRState::Token::FUNCTION_SEQUENCE;
238 return Error::success();
239}
240
241/// State transition when a TSCWrapRecord (overflow detection) is encountered.
242Error processFDRTSCWrapRecord(FDRState &State, uint8_t RecordFirstByte,
243 DataExtractor &RecordExtractor) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000244 if (State.Expects != FDRState::Token::FUNCTION_SEQUENCE)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000245 return make_error<StringError>(
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000246 Twine("Malformed log. Read TSCWrap record kind out of sequence; "
247 "expecting: ") +
248 fdrStateToTwine(State.Expects),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000249 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000250 uint32_t OffsetPtr = 1; // Read starting after the first byte.
251 State.BaseTSC = RecordExtractor.getU64(&OffsetPtr);
252 return Error::success();
253}
254
255/// State transition when a WallTimeMarkerRecord is encountered.
256Error processFDRWallTimeRecord(FDRState &State, uint8_t RecordFirstByte,
257 DataExtractor &RecordExtractor) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000258 if (State.Expects != FDRState::Token::WALLCLOCK_RECORD)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000259 return make_error<StringError>(
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000260 Twine("Malformed log. Read Wallclock record kind out of sequence; "
261 "expecting: ") +
262 fdrStateToTwine(State.Expects),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000263 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000264
265 // TODO: Someday, reconcile the TSC ticks to wall clock time for presentation
266 // purposes. For now, we're ignoring these records.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000267 State.Expects = FDRState::Token::NEW_CPU_ID_RECORD;
268 return Error::success();
269}
270
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000271/// State transition when a CustomEventMarker is encountered.
272Error processCustomEventMarker(FDRState &State, uint8_t RecordFirstByte,
273 DataExtractor &RecordExtractor,
274 size_t &RecordSize) {
275 // We can encounter a CustomEventMarker anywhere in the log, so we can handle
Keith Wyss3d0bc9e2017-08-02 21:47:27 +0000276 // it regardless of the expectation. However, we do set the expectation to
277 // read a set number of fixed bytes, as described in the metadata.
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000278 uint32_t OffsetPtr = 1; // Read after the first byte.
279 uint32_t DataSize = RecordExtractor.getU32(&OffsetPtr);
280 uint64_t TSC = RecordExtractor.getU64(&OffsetPtr);
281
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000282 // FIXME: Actually represent the record through the API. For now we only
283 // skip through the data.
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000284 (void)TSC;
285 RecordSize = 16 + DataSize;
286 return Error::success();
287}
288
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000289/// State transition when an BufferExtents record is encountered.
290Error processBufferExtents(FDRState &State, uint8_t RecordFirstByte,
291 DataExtractor &RecordExtractor) {
292 if (State.Expects != FDRState::Token::BUFFER_EXTENTS)
293 return make_error<StringError>(
294 Twine("Malformed log. Buffer Extents unexpected; expected: ") +
295 fdrStateToTwine(State.Expects),
296 std::make_error_code(std::errc::executable_format_error));
297 uint32_t OffsetPtr = 1; // Read after the first byte.
298 State.CurrentBufferSize = RecordExtractor.getU64(&OffsetPtr);
299 State.Expects = FDRState::Token::NEW_BUFFER_RECORD_OR_EOF;
300 return Error::success();
301}
302
Martin Pelikan10c873f2017-09-27 04:48:03 +0000303/// State transition when a CallArgumentRecord is encountered.
304Error processFDRCallArgumentRecord(FDRState &State, uint8_t RecordFirstByte,
305 DataExtractor &RecordExtractor,
306 std::vector<XRayRecord> &Records) {
307 uint32_t OffsetPtr = 1; // Read starting after the first byte.
308 auto &Enter = Records.back();
309
310 if (Enter.Type != RecordTypes::ENTER)
311 return make_error<StringError>(
312 "CallArgument needs to be right after a function entry",
313 std::make_error_code(std::errc::executable_format_error));
314 Enter.Type = RecordTypes::ENTER_ARG;
315 Enter.CallArgs.emplace_back(RecordExtractor.getU64(&OffsetPtr));
316 return Error::success();
317}
318
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000319/// Advances the state machine for reading the FDR record type by reading one
Keith Wysse96152a2017-04-06 03:32:01 +0000320/// Metadata Record and updating the State appropriately based on the kind of
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000321/// record encountered. The RecordKind is encoded in the first byte of the
322/// Record, which the caller should pass in because they have already read it
323/// to determine that this is a metadata record as opposed to a function record.
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000324///
325/// Beginning with Version 2 of the FDR log, we do not depend on the size of the
326/// buffer, but rather use the extents to determine how far to read in the log
327/// for this particular buffer.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000328Error processFDRMetadataRecord(FDRState &State, uint8_t RecordFirstByte,
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000329 DataExtractor &RecordExtractor,
Martin Pelikan10c873f2017-09-27 04:48:03 +0000330 size_t &RecordSize,
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000331 std::vector<XRayRecord> &Records,
332 uint16_t Version) {
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000333 // The remaining 7 bits are the RecordKind enum.
334 uint8_t RecordKind = RecordFirstByte >> 1;
335 switch (RecordKind) {
336 case 0: // NewBuffer
337 if (auto E =
338 processFDRNewBufferRecord(State, RecordFirstByte, RecordExtractor))
339 return E;
340 break;
341 case 1: // EndOfBuffer
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000342 if (Version >= 2)
343 return make_error<StringError>(
344 "Since Version 2 of FDR logging, we no longer support EOB records.",
345 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000346 if (auto E = processFDREndOfBufferRecord(State, RecordFirstByte,
347 RecordExtractor))
348 return E;
349 break;
350 case 2: // NewCPUId
351 if (auto E =
352 processFDRNewCPUIdRecord(State, RecordFirstByte, RecordExtractor))
353 return E;
354 break;
355 case 3: // TSCWrap
356 if (auto E =
357 processFDRTSCWrapRecord(State, RecordFirstByte, RecordExtractor))
358 return E;
359 break;
360 case 4: // WallTimeMarker
361 if (auto E =
362 processFDRWallTimeRecord(State, RecordFirstByte, RecordExtractor))
363 return E;
364 break;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000365 case 5: // CustomEventMarker
366 if (auto E = processCustomEventMarker(State, RecordFirstByte,
367 RecordExtractor, RecordSize))
368 return E;
369 break;
Martin Pelikan10c873f2017-09-27 04:48:03 +0000370 case 6: // CallArgument
371 if (auto E = processFDRCallArgumentRecord(State, RecordFirstByte,
372 RecordExtractor, Records))
373 return E;
374 break;
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000375 case 7: // BufferExtents
376 if (auto E = processBufferExtents(State, RecordFirstByte, RecordExtractor))
377 return E;
378 break;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000379 default:
380 // Widen the record type to uint16_t to prevent conversion to char.
381 return make_error<StringError>(
382 Twine("Illegal metadata record type: ")
383 .concat(Twine(static_cast<unsigned>(RecordKind))),
384 std::make_error_code(std::errc::executable_format_error));
385 }
386 return Error::success();
387}
388
389/// Reads a function record from an FDR format log, appending a new XRayRecord
390/// to the vector being populated and updating the State with a new value
391/// reference value to interpret TSC deltas.
392///
393/// The XRayRecord constructed includes information from the function record
394/// processed here as well as Thread ID and CPU ID formerly extracted into
395/// State.
396Error processFDRFunctionRecord(FDRState &State, uint8_t RecordFirstByte,
397 DataExtractor &RecordExtractor,
398 std::vector<XRayRecord> &Records) {
399 switch (State.Expects) {
400 case FDRState::Token::NEW_BUFFER_RECORD_OR_EOF:
401 return make_error<StringError>(
402 "Malformed log. Received Function Record before new buffer setup.",
403 std::make_error_code(std::errc::executable_format_error));
404 case FDRState::Token::WALLCLOCK_RECORD:
405 return make_error<StringError>(
406 "Malformed log. Received Function Record when expecting wallclock.",
407 std::make_error_code(std::errc::executable_format_error));
408 case FDRState::Token::NEW_CPU_ID_RECORD:
409 return make_error<StringError>(
410 "Malformed log. Received Function Record before first CPU record.",
411 std::make_error_code(std::errc::executable_format_error));
412 default:
413 Records.emplace_back();
414 auto &Record = Records.back();
415 Record.RecordType = 0; // Record is type NORMAL.
416 // Strip off record type bit and use the next three bits.
417 uint8_t RecordType = (RecordFirstByte >> 1) & 0x07;
418 switch (RecordType) {
419 case static_cast<uint8_t>(RecordTypes::ENTER):
420 Record.Type = RecordTypes::ENTER;
421 break;
422 case static_cast<uint8_t>(RecordTypes::EXIT):
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000423 Record.Type = RecordTypes::EXIT;
424 break;
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000425 case static_cast<uint8_t>(RecordTypes::TAIL_EXIT):
426 Record.Type = RecordTypes::TAIL_EXIT;
427 break;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000428 default:
Martin Pelikand78db152017-09-15 04:22:16 +0000429 // Cast to an unsigned integer to not interpret the record type as a char.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000430 return make_error<StringError>(
431 Twine("Illegal function record type: ")
432 .concat(Twine(static_cast<unsigned>(RecordType))),
433 std::make_error_code(std::errc::executable_format_error));
434 }
435 Record.CPU = State.CPUId;
436 Record.TId = State.ThreadId;
Keith Wyss3d0bc9e2017-08-02 21:47:27 +0000437 // Back up to read first 32 bits, including the 4 we pulled RecordType
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000438 // and RecordKind out of. The remaining 28 are FunctionId.
439 uint32_t OffsetPtr = 0;
440 // Despite function Id being a signed int on XRayRecord,
441 // when it is written to an FDR format, the top bits are truncated,
442 // so it is effectively an unsigned value. When we shift off the
443 // top four bits, we want the shift to be logical, so we read as
444 // uint32_t.
445 uint32_t FuncIdBitField = RecordExtractor.getU32(&OffsetPtr);
446 Record.FuncId = FuncIdBitField >> 4;
447 // FunctionRecords have a 32 bit delta from the previous absolute TSC
448 // or TSC delta. If this would overflow, we should read a TSCWrap record
449 // with an absolute TSC reading.
Martin Pelikand78db152017-09-15 04:22:16 +0000450 uint64_t NewTSC = State.BaseTSC + RecordExtractor.getU32(&OffsetPtr);
451 State.BaseTSC = NewTSC;
452 Record.TSC = NewTSC;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000453 }
454 return Error::success();
455}
456
457/// Reads a log in FDR mode for version 1 of this binary format. FDR mode is
458/// defined as part of the compiler-rt project in xray_fdr_logging.h, and such
459/// a log consists of the familiar 32 bit XRayHeader, followed by sequences of
460/// of interspersed 16 byte Metadata Records and 8 byte Function Records.
461///
462/// The following is an attempt to document the grammar of the format, which is
463/// parsed by this function for little-endian machines. Since the format makes
Martin Pelikand78db152017-09-15 04:22:16 +0000464/// use of BitFields, when we support big-endian architectures, we will need to
Simon Pilgrim68168d12017-03-30 12:59:53 +0000465/// adjust not only the endianness parameter to llvm's RecordExtractor, but also
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000466/// the bit twiddling logic, which is consistent with the little-endian
467/// convention that BitFields within a struct will first be packed into the
468/// least significant bits the address they belong to.
469///
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000470/// We expect a format complying with the grammar in the following pseudo-EBNF
471/// in Version 1 of the FDR log.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000472///
473/// FDRLog: XRayFileHeader ThreadBuffer*
Keith Wyss3d0bc9e2017-08-02 21:47:27 +0000474/// XRayFileHeader: 32 bytes to identify the log as FDR with machine metadata.
475/// Includes BufferSize
476/// ThreadBuffer: NewBuffer WallClockTime NewCPUId FunctionSequence EOB
Dean Michael Berris60c24872017-03-29 06:10:12 +0000477/// BufSize: 8 byte unsigned integer indicating how large the buffer is.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000478/// NewBuffer: 16 byte metadata record with Thread Id.
479/// WallClockTime: 16 byte metadata record with human readable time.
480/// NewCPUId: 16 byte metadata record with CPUId and a 64 bit TSC reading.
Dean Michael Berris60c24872017-03-29 06:10:12 +0000481/// EOB: 16 byte record in a thread buffer plus mem garbage to fill BufSize.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000482/// FunctionSequence: NewCPUId | TSCWrap | FunctionRecord
483/// TSCWrap: 16 byte metadata record with a full 64 bit TSC reading.
484/// FunctionRecord: 8 byte record with FunctionId, entry/exit, and TSC delta.
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000485///
486/// In Version 2, we make the following changes:
487///
488/// ThreadBuffer: BufferExtents NewBuffer WallClockTime NewCPUId
489/// FunctionSequence
490/// BufferExtents: 16 byte metdata record describing how many usable bytes are
491/// in the buffer. This is measured from the start of the buffer
492/// and must always be at least 48 (bytes).
493/// EOB: *deprecated*
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000494Error loadFDRLog(StringRef Data, XRayFileHeader &FileHeader,
495 std::vector<XRayRecord> &Records) {
496 if (Data.size() < 32)
497 return make_error<StringError>(
498 "Not enough bytes for an XRay log.",
499 std::make_error_code(std::errc::invalid_argument));
500
501 // For an FDR log, there are records sized 16 and 8 bytes.
Dean Michael Berris60c24872017-03-29 06:10:12 +0000502 // There actually may be no records if no non-trivial functions are
503 // instrumented.
504 if (Data.size() % 8 != 0)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000505 return make_error<StringError>(
506 "Invalid-sized XRay data.",
507 std::make_error_code(std::errc::invalid_argument));
508
509 if (auto E = readBinaryFormatHeader(Data, FileHeader))
510 return E;
511
Dean Michael Berris60c24872017-03-29 06:10:12 +0000512 uint64_t BufferSize = 0;
513 {
514 StringRef ExtraDataRef(FileHeader.FreeFormData, 16);
515 DataExtractor ExtraDataExtractor(ExtraDataRef, true, 8);
516 uint32_t ExtraDataOffset = 0;
517 BufferSize = ExtraDataExtractor.getU64(&ExtraDataOffset);
518 }
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000519
520 FDRState::Token InitialExpectation;
521 switch (FileHeader.Version) {
522 case 1:
523 InitialExpectation = FDRState::Token::NEW_BUFFER_RECORD_OR_EOF;
524 break;
525 case 2:
526 InitialExpectation = FDRState::Token::BUFFER_EXTENTS;
527 break;
528 default:
529 return make_error<StringError>(
530 Twine("Unsupported version '") + Twine(FileHeader.Version) + "'",
531 std::make_error_code(std::errc::executable_format_error));
532 }
533 FDRState State{0, 0, 0, InitialExpectation, BufferSize, 0};
534
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000535 // RecordSize will tell the loop how far to seek ahead based on the record
536 // type that we have just read.
537 size_t RecordSize = 0;
538 for (auto S = Data.drop_front(32); !S.empty(); S = S.drop_front(RecordSize)) {
539 DataExtractor RecordExtractor(S, true, 8);
540 uint32_t OffsetPtr = 0;
Dean Michael Berris60c24872017-03-29 06:10:12 +0000541 if (State.Expects == FDRState::Token::SCAN_TO_END_OF_THREAD_BUF) {
542 RecordSize = State.CurrentBufferSize - State.CurrentBufferConsumed;
Martin Pelikand78db152017-09-15 04:22:16 +0000543 if (S.size() < RecordSize) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000544 return make_error<StringError>(
Martin Pelikand78db152017-09-15 04:22:16 +0000545 Twine("Incomplete thread buffer. Expected at least ") +
546 Twine(RecordSize) + " bytes but found " + Twine(S.size()),
Dean Michael Berris60c24872017-03-29 06:10:12 +0000547 make_error_code(std::errc::invalid_argument));
548 }
549 State.CurrentBufferConsumed = 0;
550 State.Expects = FDRState::Token::NEW_BUFFER_RECORD_OR_EOF;
551 continue;
552 }
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000553 uint8_t BitField = RecordExtractor.getU8(&OffsetPtr);
554 bool isMetadataRecord = BitField & 0x01uL;
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000555 bool isBufferExtents =
556 (BitField >> 1) == 7; // BufferExtents record kind == 7
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000557 if (isMetadataRecord) {
558 RecordSize = 16;
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000559 if (auto E =
560 processFDRMetadataRecord(State, BitField, RecordExtractor,
561 RecordSize, Records, FileHeader.Version))
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000562 return E;
563 } else { // Process Function Record
564 RecordSize = 8;
565 if (auto E = processFDRFunctionRecord(State, BitField, RecordExtractor,
566 Records))
567 return E;
568 }
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000569
570 // The BufferExtents record is technically not part of the buffer, so we
571 // don't count the size of that record against the buffer's actual size.
572 if (!isBufferExtents)
573 State.CurrentBufferConsumed += RecordSize;
574 assert(State.CurrentBufferConsumed <= State.CurrentBufferSize);
575 if (FileHeader.Version == 2 &&
576 State.CurrentBufferSize == State.CurrentBufferConsumed) {
577 // In Version 2 of the log, we don't need to scan to the end of the thread
578 // buffer if we've already consumed all the bytes we need to.
579 State.Expects = FDRState::Token::BUFFER_EXTENTS;
580 State.CurrentBufferSize = BufferSize;
581 State.CurrentBufferConsumed = 0;
582 }
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000583 }
Martin Pelikand78db152017-09-15 04:22:16 +0000584
585 // Having iterated over everything we've been given, we've either consumed
586 // everything and ended up in the end state, or were told to skip the rest.
587 bool Finished = State.Expects == FDRState::Token::SCAN_TO_END_OF_THREAD_BUF &&
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000588 State.CurrentBufferSize == State.CurrentBufferConsumed;
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000589 if ((State.Expects != FDRState::Token::NEW_BUFFER_RECORD_OR_EOF &&
590 State.Expects != FDRState::Token::BUFFER_EXTENTS) &&
591 !Finished)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000592 return make_error<StringError>(
Dean Michael Berris60c24872017-03-29 06:10:12 +0000593 Twine("Encountered EOF with unexpected state expectation ") +
594 fdrStateToTwine(State.Expects) +
595 ". Remaining expected bytes in thread buffer total " +
596 Twine(State.CurrentBufferSize - State.CurrentBufferConsumed),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000597 std::make_error_code(std::errc::executable_format_error));
598
599 return Error::success();
600}
601
602Error loadYAMLLog(StringRef Data, XRayFileHeader &FileHeader,
603 std::vector<XRayRecord> &Records) {
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000604 YAMLXRayTrace Trace;
605 Input In(Data);
606 In >> Trace;
607 if (In.error())
608 return make_error<StringError>("Failed loading YAML Data.", In.error());
609
610 FileHeader.Version = Trace.Header.Version;
611 FileHeader.Type = Trace.Header.Type;
612 FileHeader.ConstantTSC = Trace.Header.ConstantTSC;
613 FileHeader.NonstopTSC = Trace.Header.NonstopTSC;
614 FileHeader.CycleFrequency = Trace.Header.CycleFrequency;
615
616 if (FileHeader.Version != 1)
617 return make_error<StringError>(
618 Twine("Unsupported XRay file version: ") + Twine(FileHeader.Version),
619 std::make_error_code(std::errc::invalid_argument));
620
621 Records.clear();
622 std::transform(Trace.Records.begin(), Trace.Records.end(),
623 std::back_inserter(Records), [&](const YAMLXRayRecord &R) {
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000624 return XRayRecord{R.RecordType, R.CPU, R.Type, R.FuncId,
625 R.TSC, R.TId, R.CallArgs};
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000626 });
627 return Error::success();
628}
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000629} // namespace
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000630
631Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
632 int Fd;
633 if (auto EC = sys::fs::openFileForRead(Filename, Fd)) {
634 return make_error<StringError>(
635 Twine("Cannot read log from '") + Filename + "'", EC);
636 }
637
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000638 uint64_t FileSize;
639 if (auto EC = sys::fs::file_size(Filename, FileSize)) {
640 return make_error<StringError>(
641 Twine("Cannot read log from '") + Filename + "'", EC);
642 }
643 if (FileSize < 4) {
644 return make_error<StringError>(
645 Twine("File '") + Filename + "' too small for XRay.",
Hans Wennborg84da6612017-01-12 18:33:14 +0000646 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000647 }
648
Martin Pelikand78db152017-09-15 04:22:16 +0000649 // Map the opened file into memory and use a StringRef to access it later.
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000650 std::error_code EC;
651 sys::fs::mapped_file_region MappedFile(
652 Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
653 if (EC) {
654 return make_error<StringError>(
655 Twine("Cannot read log from '") + Filename + "'", EC);
656 }
Martin Pelikand78db152017-09-15 04:22:16 +0000657 auto Data = StringRef(MappedFile.data(), MappedFile.size());
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000658
659 // Attempt to detect the file type using file magic. We have a slight bias
660 // towards the binary format, and we do this by making sure that the first 4
661 // bytes of the binary file is some combination of the following byte
Martin Pelikand78db152017-09-15 04:22:16 +0000662 // patterns: (observe the code loading them assumes they're little endian)
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000663 //
Martin Pelikand78db152017-09-15 04:22:16 +0000664 // 0x01 0x00 0x00 0x00 - version 1, "naive" format
665 // 0x01 0x00 0x01 0x00 - version 1, "flight data recorder" format
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000666 // 0x02 0x00 0x01 0x00 - version 2, "flight data recorder" format
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000667 //
Martin Pelikand78db152017-09-15 04:22:16 +0000668 // YAML files don't typically have those first four bytes as valid text so we
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000669 // try loading assuming YAML if we don't find these bytes.
670 //
671 // Only if we can't load either the binary or the YAML format will we yield an
672 // error.
673 StringRef Magic(MappedFile.data(), 4);
674 DataExtractor HeaderExtractor(Magic, true, 8);
675 uint32_t OffsetPtr = 0;
676 uint16_t Version = HeaderExtractor.getU16(&OffsetPtr);
677 uint16_t Type = HeaderExtractor.getU16(&OffsetPtr);
678
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000679 enum BinaryFormatType { NAIVE_FORMAT = 0, FLIGHT_DATA_RECORDER_FORMAT = 1 };
680
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000681 Trace T;
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000682 switch (Type) {
683 case NAIVE_FORMAT:
684 if (Version == 1 || Version == 2) {
685 if (auto E = loadNaiveFormatLog(Data, T.FileHeader, T.Records))
686 return std::move(E);
687 } else {
688 return make_error<StringError>(
689 Twine("Unsupported version for Basic/Naive Mode logging: ") +
690 Twine(Version),
691 std::make_error_code(std::errc::executable_format_error));
692 }
693 break;
694 case FLIGHT_DATA_RECORDER_FORMAT:
695 if (Version == 1 || Version == 2) {
696 if (auto E = loadFDRLog(Data, T.FileHeader, T.Records))
697 return std::move(E);
698 } else {
699 return make_error<StringError>(
700 Twine("Unsupported version for FDR Mode logging: ") + Twine(Version),
701 std::make_error_code(std::errc::executable_format_error));
702 }
703 break;
704 default:
Martin Pelikand78db152017-09-15 04:22:16 +0000705 if (auto E = loadYAMLLog(Data, T.FileHeader, T.Records))
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000706 return std::move(E);
707 }
708
709 if (Sort)
Mandeep Singh Grang28f3d5c2017-11-14 18:11:08 +0000710 std::stable_sort(T.Records.begin(), T.Records.end(),
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000711 [&](const XRayRecord &L, const XRayRecord &R) {
712 return L.TSC < R.TSC;
713 });
714
715 return std::move(T);
716}