blob: d1fcf1c35b36b35fbdb66d2a933289fc24a5ca16 [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 Berris6ec72622017-11-21 07:16:57 +0000162 BUFFER_EXTENTS,
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000163 };
164 Token Expects;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000165
Dean Michael Berris60c24872017-03-29 06:10:12 +0000166 // Each threads buffer may have trailing garbage to scan over, so we track our
167 // progress.
168 uint64_t CurrentBufferSize;
169 uint64_t CurrentBufferConsumed;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000170};
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000171
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000172const char *fdrStateToTwine(const FDRState::Token &state) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000173 switch (state) {
174 case FDRState::Token::NEW_BUFFER_RECORD_OR_EOF:
175 return "NEW_BUFFER_RECORD_OR_EOF";
176 case FDRState::Token::WALLCLOCK_RECORD:
177 return "WALLCLOCK_RECORD";
178 case FDRState::Token::NEW_CPU_ID_RECORD:
179 return "NEW_CPU_ID_RECORD";
180 case FDRState::Token::FUNCTION_SEQUENCE:
181 return "FUNCTION_SEQUENCE";
182 case FDRState::Token::SCAN_TO_END_OF_THREAD_BUF:
183 return "SCAN_TO_END_OF_THREAD_BUF";
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000184 case FDRState::Token::CUSTOM_EVENT_DATA:
185 return "CUSTOM_EVENT_DATA";
Martin Pelikan10c873f2017-09-27 04:48:03 +0000186 case FDRState::Token::CALL_ARGUMENT:
187 return "CALL_ARGUMENT";
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000188 case FDRState::Token::BUFFER_EXTENTS:
189 return "BUFFER_EXTENTS";
Dean Michael Berris60c24872017-03-29 06:10:12 +0000190 }
191 return "UNKNOWN";
192}
193
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000194/// State transition when a NewBufferRecord is encountered.
195Error processFDRNewBufferRecord(FDRState &State, uint8_t RecordFirstByte,
196 DataExtractor &RecordExtractor) {
197
Dean Michael Berris60c24872017-03-29 06:10:12 +0000198 if (State.Expects != FDRState::Token::NEW_BUFFER_RECORD_OR_EOF)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000199 return make_error<StringError>(
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000200 Twine("Malformed log. Read New Buffer record kind out of sequence; "
201 "expected: ") +
202 fdrStateToTwine(State.Expects),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000203 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000204 uint32_t OffsetPtr = 1; // 1 byte into record.
205 State.ThreadId = RecordExtractor.getU16(&OffsetPtr);
206 State.Expects = FDRState::Token::WALLCLOCK_RECORD;
207 return Error::success();
208}
209
210/// State transition when an EndOfBufferRecord is encountered.
211Error processFDREndOfBufferRecord(FDRState &State, uint8_t RecordFirstByte,
212 DataExtractor &RecordExtractor) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000213 if (State.Expects == FDRState::Token::NEW_BUFFER_RECORD_OR_EOF)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000214 return make_error<StringError>(
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000215 Twine("Malformed log. Received EOB message without current buffer; "
216 "expected: ") +
217 fdrStateToTwine(State.Expects),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000218 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris60c24872017-03-29 06:10:12 +0000219 State.Expects = FDRState::Token::SCAN_TO_END_OF_THREAD_BUF;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000220 return Error::success();
221}
222
223/// State transition when a NewCPUIdRecord is encountered.
224Error processFDRNewCPUIdRecord(FDRState &State, uint8_t RecordFirstByte,
225 DataExtractor &RecordExtractor) {
226 if (State.Expects != FDRState::Token::FUNCTION_SEQUENCE &&
Dean Michael Berris60c24872017-03-29 06:10:12 +0000227 State.Expects != FDRState::Token::NEW_CPU_ID_RECORD)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000228 return make_error<StringError>(
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000229 Twine("Malformed log. Read NewCPUId record kind out of sequence; "
230 "expected: ") +
231 fdrStateToTwine(State.Expects),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000232 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000233 uint32_t OffsetPtr = 1; // Read starting after the first byte.
234 State.CPUId = RecordExtractor.getU16(&OffsetPtr);
235 State.BaseTSC = RecordExtractor.getU64(&OffsetPtr);
236 State.Expects = FDRState::Token::FUNCTION_SEQUENCE;
237 return Error::success();
238}
239
240/// State transition when a TSCWrapRecord (overflow detection) is encountered.
241Error processFDRTSCWrapRecord(FDRState &State, uint8_t RecordFirstByte,
242 DataExtractor &RecordExtractor) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000243 if (State.Expects != FDRState::Token::FUNCTION_SEQUENCE)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000244 return make_error<StringError>(
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000245 Twine("Malformed log. Read TSCWrap record kind out of sequence; "
246 "expecting: ") +
247 fdrStateToTwine(State.Expects),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000248 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000249 uint32_t OffsetPtr = 1; // Read starting after the first byte.
250 State.BaseTSC = RecordExtractor.getU64(&OffsetPtr);
251 return Error::success();
252}
253
254/// State transition when a WallTimeMarkerRecord is encountered.
255Error processFDRWallTimeRecord(FDRState &State, uint8_t RecordFirstByte,
256 DataExtractor &RecordExtractor) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000257 if (State.Expects != FDRState::Token::WALLCLOCK_RECORD)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000258 return make_error<StringError>(
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000259 Twine("Malformed log. Read Wallclock record kind out of sequence; "
260 "expecting: ") +
261 fdrStateToTwine(State.Expects),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000262 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000263
264 // TODO: Someday, reconcile the TSC ticks to wall clock time for presentation
265 // purposes. For now, we're ignoring these records.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000266 State.Expects = FDRState::Token::NEW_CPU_ID_RECORD;
267 return Error::success();
268}
269
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000270/// State transition when a CustomEventMarker is encountered.
271Error processCustomEventMarker(FDRState &State, uint8_t RecordFirstByte,
272 DataExtractor &RecordExtractor,
273 size_t &RecordSize) {
274 // We can encounter a CustomEventMarker anywhere in the log, so we can handle
Keith Wyss3d0bc9e2017-08-02 21:47:27 +0000275 // it regardless of the expectation. However, we do set the expectation to
276 // read a set number of fixed bytes, as described in the metadata.
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000277 uint32_t OffsetPtr = 1; // Read after the first byte.
278 uint32_t DataSize = RecordExtractor.getU32(&OffsetPtr);
279 uint64_t TSC = RecordExtractor.getU64(&OffsetPtr);
280
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000281 // FIXME: Actually represent the record through the API. For now we only
282 // skip through the data.
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000283 (void)TSC;
284 RecordSize = 16 + DataSize;
285 return Error::success();
286}
287
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000288/// State transition when an BufferExtents record is encountered.
289Error processBufferExtents(FDRState &State, uint8_t RecordFirstByte,
290 DataExtractor &RecordExtractor) {
291 if (State.Expects != FDRState::Token::BUFFER_EXTENTS)
292 return make_error<StringError>(
293 Twine("Malformed log. Buffer Extents unexpected; expected: ") +
294 fdrStateToTwine(State.Expects),
295 std::make_error_code(std::errc::executable_format_error));
296 uint32_t OffsetPtr = 1; // Read after the first byte.
297 State.CurrentBufferSize = RecordExtractor.getU64(&OffsetPtr);
298 State.Expects = FDRState::Token::NEW_BUFFER_RECORD_OR_EOF;
299 return Error::success();
300}
301
Martin Pelikan10c873f2017-09-27 04:48:03 +0000302/// State transition when a CallArgumentRecord is encountered.
303Error processFDRCallArgumentRecord(FDRState &State, uint8_t RecordFirstByte,
304 DataExtractor &RecordExtractor,
305 std::vector<XRayRecord> &Records) {
306 uint32_t OffsetPtr = 1; // Read starting after the first byte.
307 auto &Enter = Records.back();
308
309 if (Enter.Type != RecordTypes::ENTER)
310 return make_error<StringError>(
311 "CallArgument needs to be right after a function entry",
312 std::make_error_code(std::errc::executable_format_error));
313 Enter.Type = RecordTypes::ENTER_ARG;
314 Enter.CallArgs.emplace_back(RecordExtractor.getU64(&OffsetPtr));
315 return Error::success();
316}
317
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000318/// Advances the state machine for reading the FDR record type by reading one
Keith Wysse96152a2017-04-06 03:32:01 +0000319/// Metadata Record and updating the State appropriately based on the kind of
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000320/// record encountered. The RecordKind is encoded in the first byte of the
321/// Record, which the caller should pass in because they have already read it
322/// to determine that this is a metadata record as opposed to a function record.
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000323///
324/// Beginning with Version 2 of the FDR log, we do not depend on the size of the
325/// buffer, but rather use the extents to determine how far to read in the log
326/// for this particular buffer.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000327Error processFDRMetadataRecord(FDRState &State, uint8_t RecordFirstByte,
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000328 DataExtractor &RecordExtractor,
Martin Pelikan10c873f2017-09-27 04:48:03 +0000329 size_t &RecordSize,
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000330 std::vector<XRayRecord> &Records,
331 uint16_t Version) {
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000332 // The remaining 7 bits are the RecordKind enum.
333 uint8_t RecordKind = RecordFirstByte >> 1;
334 switch (RecordKind) {
335 case 0: // NewBuffer
336 if (auto E =
337 processFDRNewBufferRecord(State, RecordFirstByte, RecordExtractor))
338 return E;
339 break;
340 case 1: // EndOfBuffer
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000341 if (Version >= 2)
342 return make_error<StringError>(
343 "Since Version 2 of FDR logging, we no longer support EOB records.",
344 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000345 if (auto E = processFDREndOfBufferRecord(State, RecordFirstByte,
346 RecordExtractor))
347 return E;
348 break;
349 case 2: // NewCPUId
350 if (auto E =
351 processFDRNewCPUIdRecord(State, RecordFirstByte, RecordExtractor))
352 return E;
353 break;
354 case 3: // TSCWrap
355 if (auto E =
356 processFDRTSCWrapRecord(State, RecordFirstByte, RecordExtractor))
357 return E;
358 break;
359 case 4: // WallTimeMarker
360 if (auto E =
361 processFDRWallTimeRecord(State, RecordFirstByte, RecordExtractor))
362 return E;
363 break;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000364 case 5: // CustomEventMarker
365 if (auto E = processCustomEventMarker(State, RecordFirstByte,
366 RecordExtractor, RecordSize))
367 return E;
368 break;
Martin Pelikan10c873f2017-09-27 04:48:03 +0000369 case 6: // CallArgument
370 if (auto E = processFDRCallArgumentRecord(State, RecordFirstByte,
371 RecordExtractor, Records))
372 return E;
373 break;
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000374 case 7: // BufferExtents
375 if (auto E = processBufferExtents(State, RecordFirstByte, RecordExtractor))
376 return E;
377 break;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000378 default:
379 // Widen the record type to uint16_t to prevent conversion to char.
380 return make_error<StringError>(
381 Twine("Illegal metadata record type: ")
382 .concat(Twine(static_cast<unsigned>(RecordKind))),
383 std::make_error_code(std::errc::executable_format_error));
384 }
385 return Error::success();
386}
387
388/// Reads a function record from an FDR format log, appending a new XRayRecord
389/// to the vector being populated and updating the State with a new value
390/// reference value to interpret TSC deltas.
391///
392/// The XRayRecord constructed includes information from the function record
393/// processed here as well as Thread ID and CPU ID formerly extracted into
394/// State.
395Error processFDRFunctionRecord(FDRState &State, uint8_t RecordFirstByte,
396 DataExtractor &RecordExtractor,
397 std::vector<XRayRecord> &Records) {
398 switch (State.Expects) {
399 case FDRState::Token::NEW_BUFFER_RECORD_OR_EOF:
400 return make_error<StringError>(
401 "Malformed log. Received Function Record before new buffer setup.",
402 std::make_error_code(std::errc::executable_format_error));
403 case FDRState::Token::WALLCLOCK_RECORD:
404 return make_error<StringError>(
405 "Malformed log. Received Function Record when expecting wallclock.",
406 std::make_error_code(std::errc::executable_format_error));
407 case FDRState::Token::NEW_CPU_ID_RECORD:
408 return make_error<StringError>(
409 "Malformed log. Received Function Record before first CPU record.",
410 std::make_error_code(std::errc::executable_format_error));
411 default:
412 Records.emplace_back();
413 auto &Record = Records.back();
414 Record.RecordType = 0; // Record is type NORMAL.
415 // Strip off record type bit and use the next three bits.
416 uint8_t RecordType = (RecordFirstByte >> 1) & 0x07;
417 switch (RecordType) {
418 case static_cast<uint8_t>(RecordTypes::ENTER):
419 Record.Type = RecordTypes::ENTER;
420 break;
421 case static_cast<uint8_t>(RecordTypes::EXIT):
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000422 Record.Type = RecordTypes::EXIT;
423 break;
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000424 case static_cast<uint8_t>(RecordTypes::TAIL_EXIT):
425 Record.Type = RecordTypes::TAIL_EXIT;
426 break;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000427 default:
Martin Pelikand78db152017-09-15 04:22:16 +0000428 // Cast to an unsigned integer to not interpret the record type as a char.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000429 return make_error<StringError>(
430 Twine("Illegal function record type: ")
431 .concat(Twine(static_cast<unsigned>(RecordType))),
432 std::make_error_code(std::errc::executable_format_error));
433 }
434 Record.CPU = State.CPUId;
435 Record.TId = State.ThreadId;
Keith Wyss3d0bc9e2017-08-02 21:47:27 +0000436 // Back up to read first 32 bits, including the 4 we pulled RecordType
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000437 // and RecordKind out of. The remaining 28 are FunctionId.
438 uint32_t OffsetPtr = 0;
439 // Despite function Id being a signed int on XRayRecord,
440 // when it is written to an FDR format, the top bits are truncated,
441 // so it is effectively an unsigned value. When we shift off the
442 // top four bits, we want the shift to be logical, so we read as
443 // uint32_t.
444 uint32_t FuncIdBitField = RecordExtractor.getU32(&OffsetPtr);
445 Record.FuncId = FuncIdBitField >> 4;
446 // FunctionRecords have a 32 bit delta from the previous absolute TSC
447 // or TSC delta. If this would overflow, we should read a TSCWrap record
448 // with an absolute TSC reading.
Martin Pelikand78db152017-09-15 04:22:16 +0000449 uint64_t NewTSC = State.BaseTSC + RecordExtractor.getU32(&OffsetPtr);
450 State.BaseTSC = NewTSC;
451 Record.TSC = NewTSC;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000452 }
453 return Error::success();
454}
455
456/// Reads a log in FDR mode for version 1 of this binary format. FDR mode is
457/// defined as part of the compiler-rt project in xray_fdr_logging.h, and such
458/// a log consists of the familiar 32 bit XRayHeader, followed by sequences of
459/// of interspersed 16 byte Metadata Records and 8 byte Function Records.
460///
461/// The following is an attempt to document the grammar of the format, which is
462/// parsed by this function for little-endian machines. Since the format makes
Martin Pelikand78db152017-09-15 04:22:16 +0000463/// use of BitFields, when we support big-endian architectures, we will need to
Simon Pilgrim68168d12017-03-30 12:59:53 +0000464/// adjust not only the endianness parameter to llvm's RecordExtractor, but also
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000465/// the bit twiddling logic, which is consistent with the little-endian
466/// convention that BitFields within a struct will first be packed into the
467/// least significant bits the address they belong to.
468///
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000469/// We expect a format complying with the grammar in the following pseudo-EBNF
470/// in Version 1 of the FDR log.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000471///
472/// FDRLog: XRayFileHeader ThreadBuffer*
Keith Wyss3d0bc9e2017-08-02 21:47:27 +0000473/// XRayFileHeader: 32 bytes to identify the log as FDR with machine metadata.
474/// Includes BufferSize
475/// ThreadBuffer: NewBuffer WallClockTime NewCPUId FunctionSequence EOB
Dean Michael Berris60c24872017-03-29 06:10:12 +0000476/// BufSize: 8 byte unsigned integer indicating how large the buffer is.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000477/// NewBuffer: 16 byte metadata record with Thread Id.
478/// WallClockTime: 16 byte metadata record with human readable time.
479/// NewCPUId: 16 byte metadata record with CPUId and a 64 bit TSC reading.
Dean Michael Berris60c24872017-03-29 06:10:12 +0000480/// EOB: 16 byte record in a thread buffer plus mem garbage to fill BufSize.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000481/// FunctionSequence: NewCPUId | TSCWrap | FunctionRecord
482/// TSCWrap: 16 byte metadata record with a full 64 bit TSC reading.
483/// FunctionRecord: 8 byte record with FunctionId, entry/exit, and TSC delta.
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000484///
485/// In Version 2, we make the following changes:
486///
487/// ThreadBuffer: BufferExtents NewBuffer WallClockTime NewCPUId
488/// FunctionSequence
489/// BufferExtents: 16 byte metdata record describing how many usable bytes are
490/// in the buffer. This is measured from the start of the buffer
491/// and must always be at least 48 (bytes).
492/// EOB: *deprecated*
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000493Error loadFDRLog(StringRef Data, XRayFileHeader &FileHeader,
494 std::vector<XRayRecord> &Records) {
495 if (Data.size() < 32)
496 return make_error<StringError>(
497 "Not enough bytes for an XRay log.",
498 std::make_error_code(std::errc::invalid_argument));
499
500 // For an FDR log, there are records sized 16 and 8 bytes.
Dean Michael Berris60c24872017-03-29 06:10:12 +0000501 // There actually may be no records if no non-trivial functions are
502 // instrumented.
503 if (Data.size() % 8 != 0)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000504 return make_error<StringError>(
505 "Invalid-sized XRay data.",
506 std::make_error_code(std::errc::invalid_argument));
507
508 if (auto E = readBinaryFormatHeader(Data, FileHeader))
509 return E;
510
Dean Michael Berris60c24872017-03-29 06:10:12 +0000511 uint64_t BufferSize = 0;
512 {
513 StringRef ExtraDataRef(FileHeader.FreeFormData, 16);
514 DataExtractor ExtraDataExtractor(ExtraDataRef, true, 8);
515 uint32_t ExtraDataOffset = 0;
516 BufferSize = ExtraDataExtractor.getU64(&ExtraDataOffset);
517 }
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000518
519 FDRState::Token InitialExpectation;
520 switch (FileHeader.Version) {
521 case 1:
522 InitialExpectation = FDRState::Token::NEW_BUFFER_RECORD_OR_EOF;
523 break;
524 case 2:
525 InitialExpectation = FDRState::Token::BUFFER_EXTENTS;
526 break;
527 default:
528 return make_error<StringError>(
529 Twine("Unsupported version '") + Twine(FileHeader.Version) + "'",
530 std::make_error_code(std::errc::executable_format_error));
531 }
532 FDRState State{0, 0, 0, InitialExpectation, BufferSize, 0};
533
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000534 // RecordSize will tell the loop how far to seek ahead based on the record
535 // type that we have just read.
536 size_t RecordSize = 0;
537 for (auto S = Data.drop_front(32); !S.empty(); S = S.drop_front(RecordSize)) {
538 DataExtractor RecordExtractor(S, true, 8);
539 uint32_t OffsetPtr = 0;
Dean Michael Berris60c24872017-03-29 06:10:12 +0000540 if (State.Expects == FDRState::Token::SCAN_TO_END_OF_THREAD_BUF) {
541 RecordSize = State.CurrentBufferSize - State.CurrentBufferConsumed;
Martin Pelikand78db152017-09-15 04:22:16 +0000542 if (S.size() < RecordSize) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000543 return make_error<StringError>(
Martin Pelikand78db152017-09-15 04:22:16 +0000544 Twine("Incomplete thread buffer. Expected at least ") +
545 Twine(RecordSize) + " bytes but found " + Twine(S.size()),
Dean Michael Berris60c24872017-03-29 06:10:12 +0000546 make_error_code(std::errc::invalid_argument));
547 }
548 State.CurrentBufferConsumed = 0;
549 State.Expects = FDRState::Token::NEW_BUFFER_RECORD_OR_EOF;
550 continue;
551 }
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000552 uint8_t BitField = RecordExtractor.getU8(&OffsetPtr);
553 bool isMetadataRecord = BitField & 0x01uL;
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000554 bool isBufferExtents =
555 (BitField >> 1) == 7; // BufferExtents record kind == 7
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000556 if (isMetadataRecord) {
557 RecordSize = 16;
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000558 if (auto E =
559 processFDRMetadataRecord(State, BitField, RecordExtractor,
560 RecordSize, Records, FileHeader.Version))
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000561 return E;
562 } else { // Process Function Record
563 RecordSize = 8;
564 if (auto E = processFDRFunctionRecord(State, BitField, RecordExtractor,
565 Records))
566 return E;
567 }
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000568
569 // The BufferExtents record is technically not part of the buffer, so we
570 // don't count the size of that record against the buffer's actual size.
571 if (!isBufferExtents)
572 State.CurrentBufferConsumed += RecordSize;
573 assert(State.CurrentBufferConsumed <= State.CurrentBufferSize);
574 if (FileHeader.Version == 2 &&
575 State.CurrentBufferSize == State.CurrentBufferConsumed) {
576 // In Version 2 of the log, we don't need to scan to the end of the thread
577 // buffer if we've already consumed all the bytes we need to.
578 State.Expects = FDRState::Token::BUFFER_EXTENTS;
579 State.CurrentBufferSize = BufferSize;
580 State.CurrentBufferConsumed = 0;
581 }
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000582 }
Martin Pelikand78db152017-09-15 04:22:16 +0000583
584 // Having iterated over everything we've been given, we've either consumed
585 // everything and ended up in the end state, or were told to skip the rest.
586 bool Finished = State.Expects == FDRState::Token::SCAN_TO_END_OF_THREAD_BUF &&
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000587 State.CurrentBufferSize == State.CurrentBufferConsumed;
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000588 if ((State.Expects != FDRState::Token::NEW_BUFFER_RECORD_OR_EOF &&
589 State.Expects != FDRState::Token::BUFFER_EXTENTS) &&
590 !Finished)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000591 return make_error<StringError>(
Dean Michael Berris60c24872017-03-29 06:10:12 +0000592 Twine("Encountered EOF with unexpected state expectation ") +
593 fdrStateToTwine(State.Expects) +
594 ". Remaining expected bytes in thread buffer total " +
595 Twine(State.CurrentBufferSize - State.CurrentBufferConsumed),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000596 std::make_error_code(std::errc::executable_format_error));
597
598 return Error::success();
599}
600
601Error loadYAMLLog(StringRef Data, XRayFileHeader &FileHeader,
602 std::vector<XRayRecord> &Records) {
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000603 YAMLXRayTrace Trace;
604 Input In(Data);
605 In >> Trace;
606 if (In.error())
607 return make_error<StringError>("Failed loading YAML Data.", In.error());
608
609 FileHeader.Version = Trace.Header.Version;
610 FileHeader.Type = Trace.Header.Type;
611 FileHeader.ConstantTSC = Trace.Header.ConstantTSC;
612 FileHeader.NonstopTSC = Trace.Header.NonstopTSC;
613 FileHeader.CycleFrequency = Trace.Header.CycleFrequency;
614
615 if (FileHeader.Version != 1)
616 return make_error<StringError>(
617 Twine("Unsupported XRay file version: ") + Twine(FileHeader.Version),
618 std::make_error_code(std::errc::invalid_argument));
619
620 Records.clear();
621 std::transform(Trace.Records.begin(), Trace.Records.end(),
622 std::back_inserter(Records), [&](const YAMLXRayRecord &R) {
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000623 return XRayRecord{R.RecordType, R.CPU, R.Type, R.FuncId,
624 R.TSC, R.TId, R.CallArgs};
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000625 });
626 return Error::success();
627}
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000628} // namespace
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000629
630Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
631 int Fd;
632 if (auto EC = sys::fs::openFileForRead(Filename, Fd)) {
633 return make_error<StringError>(
634 Twine("Cannot read log from '") + Filename + "'", EC);
635 }
636
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000637 uint64_t FileSize;
638 if (auto EC = sys::fs::file_size(Filename, FileSize)) {
639 return make_error<StringError>(
640 Twine("Cannot read log from '") + Filename + "'", EC);
641 }
642 if (FileSize < 4) {
643 return make_error<StringError>(
644 Twine("File '") + Filename + "' too small for XRay.",
Hans Wennborg84da6612017-01-12 18:33:14 +0000645 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000646 }
647
Martin Pelikand78db152017-09-15 04:22:16 +0000648 // Map the opened file into memory and use a StringRef to access it later.
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000649 std::error_code EC;
650 sys::fs::mapped_file_region MappedFile(
651 Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
652 if (EC) {
653 return make_error<StringError>(
654 Twine("Cannot read log from '") + Filename + "'", EC);
655 }
Martin Pelikand78db152017-09-15 04:22:16 +0000656 auto Data = StringRef(MappedFile.data(), MappedFile.size());
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000657
658 // Attempt to detect the file type using file magic. We have a slight bias
659 // towards the binary format, and we do this by making sure that the first 4
660 // bytes of the binary file is some combination of the following byte
Martin Pelikand78db152017-09-15 04:22:16 +0000661 // patterns: (observe the code loading them assumes they're little endian)
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000662 //
Martin Pelikand78db152017-09-15 04:22:16 +0000663 // 0x01 0x00 0x00 0x00 - version 1, "naive" format
664 // 0x01 0x00 0x01 0x00 - version 1, "flight data recorder" format
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000665 // 0x02 0x00 0x01 0x00 - version 2, "flight data recorder" format
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000666 //
Martin Pelikand78db152017-09-15 04:22:16 +0000667 // YAML files don't typically have those first four bytes as valid text so we
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000668 // try loading assuming YAML if we don't find these bytes.
669 //
670 // Only if we can't load either the binary or the YAML format will we yield an
671 // error.
672 StringRef Magic(MappedFile.data(), 4);
673 DataExtractor HeaderExtractor(Magic, true, 8);
674 uint32_t OffsetPtr = 0;
675 uint16_t Version = HeaderExtractor.getU16(&OffsetPtr);
676 uint16_t Type = HeaderExtractor.getU16(&OffsetPtr);
677
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000678 enum BinaryFormatType { NAIVE_FORMAT = 0, FLIGHT_DATA_RECORDER_FORMAT = 1 };
679
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000680 Trace T;
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000681 switch (Type) {
682 case NAIVE_FORMAT:
683 if (Version == 1 || Version == 2) {
684 if (auto E = loadNaiveFormatLog(Data, T.FileHeader, T.Records))
685 return std::move(E);
686 } else {
687 return make_error<StringError>(
688 Twine("Unsupported version for Basic/Naive Mode logging: ") +
689 Twine(Version),
690 std::make_error_code(std::errc::executable_format_error));
691 }
692 break;
693 case FLIGHT_DATA_RECORDER_FORMAT:
694 if (Version == 1 || Version == 2) {
695 if (auto E = loadFDRLog(Data, T.FileHeader, T.Records))
696 return std::move(E);
697 } else {
698 return make_error<StringError>(
699 Twine("Unsupported version for FDR Mode logging: ") + Twine(Version),
700 std::make_error_code(std::errc::executable_format_error));
701 }
702 break;
703 default:
Martin Pelikand78db152017-09-15 04:22:16 +0000704 if (auto E = loadYAMLLog(Data, T.FileHeader, T.Records))
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000705 return std::move(E);
706 }
707
708 if (Sort)
Mandeep Singh Grang28f3d5c2017-11-14 18:11:08 +0000709 std::stable_sort(T.Records.begin(), T.Records.end(),
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000710 [&](const XRayRecord &L, const XRayRecord &R) {
711 return L.TSC < R.TSC;
712 });
713
714 return std::move(T);
715}