blob: 7f8846cbd6f74aa6beb764b95d9f5891563d9886 [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;
85 Records.emplace_back();
86 auto &Record = Records.back();
87 Record.RecordType = RecordExtractor.getU16(&OffsetPtr);
88 Record.CPU = RecordExtractor.getU8(&OffsetPtr);
89 auto Type = RecordExtractor.getU8(&OffsetPtr);
90 switch (Type) {
91 case 0:
92 Record.Type = RecordTypes::ENTER;
93 break;
94 case 1:
95 Record.Type = RecordTypes::EXIT;
96 break;
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +000097 case 2:
98 Record.Type = RecordTypes::TAIL_EXIT;
99 break;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000100 default:
101 return make_error<StringError>(
102 Twine("Unknown record type '") + Twine(int{Type}) + "'",
NAKAMURA Takumib09bec22017-01-11 01:06:57 +0000103 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000104 }
105 Record.FuncId = RecordExtractor.getSigned(&OffsetPtr, sizeof(int32_t));
106 Record.TSC = RecordExtractor.getU64(&OffsetPtr);
107 Record.TId = RecordExtractor.getU32(&OffsetPtr);
108 }
109 return Error::success();
110}
111
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000112/// When reading from a Flight Data Recorder mode log, metadata records are
113/// sparse compared to packed function records, so we must maintain state as we
114/// read through the sequence of entries. This allows the reader to denormalize
115/// the CPUId and Thread Id onto each Function Record and transform delta
116/// encoded TSC values into absolute encodings on each record.
117struct FDRState {
118 uint16_t CPUId;
119 uint16_t ThreadId;
120 uint64_t BaseTSC;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000121
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000122 /// Encode some of the state transitions for the FDR log reader as explicit
123 /// checks. These are expectations for the next Record in the stream.
124 enum class Token {
125 NEW_BUFFER_RECORD_OR_EOF,
126 WALLCLOCK_RECORD,
127 NEW_CPU_ID_RECORD,
Dean Michael Berris60c24872017-03-29 06:10:12 +0000128 FUNCTION_SEQUENCE,
129 SCAN_TO_END_OF_THREAD_BUF,
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000130 CUSTOM_EVENT_DATA,
Martin Pelikan10c873f2017-09-27 04:48:03 +0000131 CALL_ARGUMENT,
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000132 };
133 Token Expects;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000134
Dean Michael Berris60c24872017-03-29 06:10:12 +0000135 // Each threads buffer may have trailing garbage to scan over, so we track our
136 // progress.
137 uint64_t CurrentBufferSize;
138 uint64_t CurrentBufferConsumed;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000139};
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000140
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000141const char *fdrStateToTwine(const FDRState::Token &state) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000142 switch (state) {
143 case FDRState::Token::NEW_BUFFER_RECORD_OR_EOF:
144 return "NEW_BUFFER_RECORD_OR_EOF";
145 case FDRState::Token::WALLCLOCK_RECORD:
146 return "WALLCLOCK_RECORD";
147 case FDRState::Token::NEW_CPU_ID_RECORD:
148 return "NEW_CPU_ID_RECORD";
149 case FDRState::Token::FUNCTION_SEQUENCE:
150 return "FUNCTION_SEQUENCE";
151 case FDRState::Token::SCAN_TO_END_OF_THREAD_BUF:
152 return "SCAN_TO_END_OF_THREAD_BUF";
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000153 case FDRState::Token::CUSTOM_EVENT_DATA:
154 return "CUSTOM_EVENT_DATA";
Martin Pelikan10c873f2017-09-27 04:48:03 +0000155 case FDRState::Token::CALL_ARGUMENT:
156 return "CALL_ARGUMENT";
Dean Michael Berris60c24872017-03-29 06:10:12 +0000157 }
158 return "UNKNOWN";
159}
160
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000161/// State transition when a NewBufferRecord is encountered.
162Error processFDRNewBufferRecord(FDRState &State, uint8_t RecordFirstByte,
163 DataExtractor &RecordExtractor) {
164
Dean Michael Berris60c24872017-03-29 06:10:12 +0000165 if (State.Expects != FDRState::Token::NEW_BUFFER_RECORD_OR_EOF)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000166 return make_error<StringError>(
167 "Malformed log. Read New Buffer record kind out of sequence",
168 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000169 uint32_t OffsetPtr = 1; // 1 byte into record.
170 State.ThreadId = RecordExtractor.getU16(&OffsetPtr);
171 State.Expects = FDRState::Token::WALLCLOCK_RECORD;
172 return Error::success();
173}
174
175/// State transition when an EndOfBufferRecord is encountered.
176Error processFDREndOfBufferRecord(FDRState &State, uint8_t RecordFirstByte,
177 DataExtractor &RecordExtractor) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000178 if (State.Expects == FDRState::Token::NEW_BUFFER_RECORD_OR_EOF)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000179 return make_error<StringError>(
180 "Malformed log. Received EOB message without current buffer.",
181 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris60c24872017-03-29 06:10:12 +0000182 State.Expects = FDRState::Token::SCAN_TO_END_OF_THREAD_BUF;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000183 return Error::success();
184}
185
186/// State transition when a NewCPUIdRecord is encountered.
187Error processFDRNewCPUIdRecord(FDRState &State, uint8_t RecordFirstByte,
188 DataExtractor &RecordExtractor) {
189 if (State.Expects != FDRState::Token::FUNCTION_SEQUENCE &&
Dean Michael Berris60c24872017-03-29 06:10:12 +0000190 State.Expects != FDRState::Token::NEW_CPU_ID_RECORD)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000191 return make_error<StringError>(
192 "Malformed log. Read NewCPUId record kind out of sequence",
193 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000194 uint32_t OffsetPtr = 1; // Read starting after the first byte.
195 State.CPUId = RecordExtractor.getU16(&OffsetPtr);
196 State.BaseTSC = RecordExtractor.getU64(&OffsetPtr);
197 State.Expects = FDRState::Token::FUNCTION_SEQUENCE;
198 return Error::success();
199}
200
201/// State transition when a TSCWrapRecord (overflow detection) is encountered.
202Error processFDRTSCWrapRecord(FDRState &State, uint8_t RecordFirstByte,
203 DataExtractor &RecordExtractor) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000204 if (State.Expects != FDRState::Token::FUNCTION_SEQUENCE)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000205 return make_error<StringError>(
206 "Malformed log. Read TSCWrap record kind out of sequence",
207 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000208 uint32_t OffsetPtr = 1; // Read starting after the first byte.
209 State.BaseTSC = RecordExtractor.getU64(&OffsetPtr);
210 return Error::success();
211}
212
213/// State transition when a WallTimeMarkerRecord is encountered.
214Error processFDRWallTimeRecord(FDRState &State, uint8_t RecordFirstByte,
215 DataExtractor &RecordExtractor) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000216 if (State.Expects != FDRState::Token::WALLCLOCK_RECORD)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000217 return make_error<StringError>(
218 "Malformed log. Read Wallclock record kind out of sequence",
219 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000220 // We don't encode the wall time into any of the records.
221 // XRayRecords are concerned with the TSC instead.
222 State.Expects = FDRState::Token::NEW_CPU_ID_RECORD;
223 return Error::success();
224}
225
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000226/// State transition when a CustomEventMarker is encountered.
227Error processCustomEventMarker(FDRState &State, uint8_t RecordFirstByte,
228 DataExtractor &RecordExtractor,
229 size_t &RecordSize) {
230 // We can encounter a CustomEventMarker anywhere in the log, so we can handle
Keith Wyss3d0bc9e2017-08-02 21:47:27 +0000231 // it regardless of the expectation. However, we do set the expectation to
232 // read a set number of fixed bytes, as described in the metadata.
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000233 uint32_t OffsetPtr = 1; // Read after the first byte.
234 uint32_t DataSize = RecordExtractor.getU32(&OffsetPtr);
235 uint64_t TSC = RecordExtractor.getU64(&OffsetPtr);
236
237 // FIXME: Actually represent the record through the API. For now we only skip
238 // through the data.
239 (void)TSC;
240 RecordSize = 16 + DataSize;
241 return Error::success();
242}
243
Martin Pelikan10c873f2017-09-27 04:48:03 +0000244/// State transition when a CallArgumentRecord is encountered.
245Error processFDRCallArgumentRecord(FDRState &State, uint8_t RecordFirstByte,
246 DataExtractor &RecordExtractor,
247 std::vector<XRayRecord> &Records) {
248 uint32_t OffsetPtr = 1; // Read starting after the first byte.
249 auto &Enter = Records.back();
250
251 if (Enter.Type != RecordTypes::ENTER)
252 return make_error<StringError>(
253 "CallArgument needs to be right after a function entry",
254 std::make_error_code(std::errc::executable_format_error));
255 Enter.Type = RecordTypes::ENTER_ARG;
256 Enter.CallArgs.emplace_back(RecordExtractor.getU64(&OffsetPtr));
257 return Error::success();
258}
259
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000260/// Advances the state machine for reading the FDR record type by reading one
Keith Wysse96152a2017-04-06 03:32:01 +0000261/// Metadata Record and updating the State appropriately based on the kind of
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000262/// record encountered. The RecordKind is encoded in the first byte of the
263/// Record, which the caller should pass in because they have already read it
264/// to determine that this is a metadata record as opposed to a function record.
265Error processFDRMetadataRecord(FDRState &State, uint8_t RecordFirstByte,
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000266 DataExtractor &RecordExtractor,
Martin Pelikan10c873f2017-09-27 04:48:03 +0000267 size_t &RecordSize,
268 std::vector<XRayRecord> &Records) {
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000269 // The remaining 7 bits are the RecordKind enum.
270 uint8_t RecordKind = RecordFirstByte >> 1;
271 switch (RecordKind) {
272 case 0: // NewBuffer
273 if (auto E =
274 processFDRNewBufferRecord(State, RecordFirstByte, RecordExtractor))
275 return E;
276 break;
277 case 1: // EndOfBuffer
278 if (auto E = processFDREndOfBufferRecord(State, RecordFirstByte,
279 RecordExtractor))
280 return E;
281 break;
282 case 2: // NewCPUId
283 if (auto E =
284 processFDRNewCPUIdRecord(State, RecordFirstByte, RecordExtractor))
285 return E;
286 break;
287 case 3: // TSCWrap
288 if (auto E =
289 processFDRTSCWrapRecord(State, RecordFirstByte, RecordExtractor))
290 return E;
291 break;
292 case 4: // WallTimeMarker
293 if (auto E =
294 processFDRWallTimeRecord(State, RecordFirstByte, RecordExtractor))
295 return E;
296 break;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000297 case 5: // CustomEventMarker
298 if (auto E = processCustomEventMarker(State, RecordFirstByte,
299 RecordExtractor, RecordSize))
300 return E;
301 break;
Martin Pelikan10c873f2017-09-27 04:48:03 +0000302 case 6: // CallArgument
303 if (auto E = processFDRCallArgumentRecord(State, RecordFirstByte,
304 RecordExtractor, Records))
305 return E;
306 break;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000307 default:
308 // Widen the record type to uint16_t to prevent conversion to char.
309 return make_error<StringError>(
310 Twine("Illegal metadata record type: ")
311 .concat(Twine(static_cast<unsigned>(RecordKind))),
312 std::make_error_code(std::errc::executable_format_error));
313 }
314 return Error::success();
315}
316
317/// Reads a function record from an FDR format log, appending a new XRayRecord
318/// to the vector being populated and updating the State with a new value
319/// reference value to interpret TSC deltas.
320///
321/// The XRayRecord constructed includes information from the function record
322/// processed here as well as Thread ID and CPU ID formerly extracted into
323/// State.
324Error processFDRFunctionRecord(FDRState &State, uint8_t RecordFirstByte,
325 DataExtractor &RecordExtractor,
326 std::vector<XRayRecord> &Records) {
327 switch (State.Expects) {
328 case FDRState::Token::NEW_BUFFER_RECORD_OR_EOF:
329 return make_error<StringError>(
330 "Malformed log. Received Function Record before new buffer setup.",
331 std::make_error_code(std::errc::executable_format_error));
332 case FDRState::Token::WALLCLOCK_RECORD:
333 return make_error<StringError>(
334 "Malformed log. Received Function Record when expecting wallclock.",
335 std::make_error_code(std::errc::executable_format_error));
336 case FDRState::Token::NEW_CPU_ID_RECORD:
337 return make_error<StringError>(
338 "Malformed log. Received Function Record before first CPU record.",
339 std::make_error_code(std::errc::executable_format_error));
340 default:
341 Records.emplace_back();
342 auto &Record = Records.back();
343 Record.RecordType = 0; // Record is type NORMAL.
344 // Strip off record type bit and use the next three bits.
345 uint8_t RecordType = (RecordFirstByte >> 1) & 0x07;
346 switch (RecordType) {
347 case static_cast<uint8_t>(RecordTypes::ENTER):
348 Record.Type = RecordTypes::ENTER;
349 break;
350 case static_cast<uint8_t>(RecordTypes::EXIT):
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000351 Record.Type = RecordTypes::EXIT;
352 break;
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000353 case static_cast<uint8_t>(RecordTypes::TAIL_EXIT):
354 Record.Type = RecordTypes::TAIL_EXIT;
355 break;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000356 default:
Martin Pelikand78db152017-09-15 04:22:16 +0000357 // Cast to an unsigned integer to not interpret the record type as a char.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000358 return make_error<StringError>(
359 Twine("Illegal function record type: ")
360 .concat(Twine(static_cast<unsigned>(RecordType))),
361 std::make_error_code(std::errc::executable_format_error));
362 }
363 Record.CPU = State.CPUId;
364 Record.TId = State.ThreadId;
Keith Wyss3d0bc9e2017-08-02 21:47:27 +0000365 // Back up to read first 32 bits, including the 4 we pulled RecordType
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000366 // and RecordKind out of. The remaining 28 are FunctionId.
367 uint32_t OffsetPtr = 0;
368 // Despite function Id being a signed int on XRayRecord,
369 // when it is written to an FDR format, the top bits are truncated,
370 // so it is effectively an unsigned value. When we shift off the
371 // top four bits, we want the shift to be logical, so we read as
372 // uint32_t.
373 uint32_t FuncIdBitField = RecordExtractor.getU32(&OffsetPtr);
374 Record.FuncId = FuncIdBitField >> 4;
375 // FunctionRecords have a 32 bit delta from the previous absolute TSC
376 // or TSC delta. If this would overflow, we should read a TSCWrap record
377 // with an absolute TSC reading.
Martin Pelikand78db152017-09-15 04:22:16 +0000378 uint64_t NewTSC = State.BaseTSC + RecordExtractor.getU32(&OffsetPtr);
379 State.BaseTSC = NewTSC;
380 Record.TSC = NewTSC;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000381 }
382 return Error::success();
383}
384
385/// Reads a log in FDR mode for version 1 of this binary format. FDR mode is
386/// defined as part of the compiler-rt project in xray_fdr_logging.h, and such
387/// a log consists of the familiar 32 bit XRayHeader, followed by sequences of
388/// of interspersed 16 byte Metadata Records and 8 byte Function Records.
389///
390/// The following is an attempt to document the grammar of the format, which is
391/// parsed by this function for little-endian machines. Since the format makes
Martin Pelikand78db152017-09-15 04:22:16 +0000392/// use of BitFields, when we support big-endian architectures, we will need to
Simon Pilgrim68168d12017-03-30 12:59:53 +0000393/// adjust not only the endianness parameter to llvm's RecordExtractor, but also
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000394/// the bit twiddling logic, which is consistent with the little-endian
395/// convention that BitFields within a struct will first be packed into the
396/// least significant bits the address they belong to.
397///
398/// We expect a format complying with the grammar in the following pseudo-EBNF.
399///
400/// FDRLog: XRayFileHeader ThreadBuffer*
Keith Wyss3d0bc9e2017-08-02 21:47:27 +0000401/// XRayFileHeader: 32 bytes to identify the log as FDR with machine metadata.
402/// Includes BufferSize
403/// ThreadBuffer: NewBuffer WallClockTime NewCPUId FunctionSequence EOB
Dean Michael Berris60c24872017-03-29 06:10:12 +0000404/// BufSize: 8 byte unsigned integer indicating how large the buffer is.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000405/// NewBuffer: 16 byte metadata record with Thread Id.
406/// WallClockTime: 16 byte metadata record with human readable time.
407/// NewCPUId: 16 byte metadata record with CPUId and a 64 bit TSC reading.
Dean Michael Berris60c24872017-03-29 06:10:12 +0000408/// EOB: 16 byte record in a thread buffer plus mem garbage to fill BufSize.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000409/// FunctionSequence: NewCPUId | TSCWrap | FunctionRecord
410/// TSCWrap: 16 byte metadata record with a full 64 bit TSC reading.
411/// FunctionRecord: 8 byte record with FunctionId, entry/exit, and TSC delta.
412Error loadFDRLog(StringRef Data, XRayFileHeader &FileHeader,
413 std::vector<XRayRecord> &Records) {
414 if (Data.size() < 32)
415 return make_error<StringError>(
416 "Not enough bytes for an XRay log.",
417 std::make_error_code(std::errc::invalid_argument));
418
419 // For an FDR log, there are records sized 16 and 8 bytes.
Dean Michael Berris60c24872017-03-29 06:10:12 +0000420 // There actually may be no records if no non-trivial functions are
421 // instrumented.
422 if (Data.size() % 8 != 0)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000423 return make_error<StringError>(
424 "Invalid-sized XRay data.",
425 std::make_error_code(std::errc::invalid_argument));
426
427 if (auto E = readBinaryFormatHeader(Data, FileHeader))
428 return E;
429
Dean Michael Berris60c24872017-03-29 06:10:12 +0000430 uint64_t BufferSize = 0;
431 {
432 StringRef ExtraDataRef(FileHeader.FreeFormData, 16);
433 DataExtractor ExtraDataExtractor(ExtraDataRef, true, 8);
434 uint32_t ExtraDataOffset = 0;
435 BufferSize = ExtraDataExtractor.getU64(&ExtraDataOffset);
436 }
437 FDRState State{0, 0, 0, FDRState::Token::NEW_BUFFER_RECORD_OR_EOF,
438 BufferSize, 0};
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000439 // RecordSize will tell the loop how far to seek ahead based on the record
440 // type that we have just read.
441 size_t RecordSize = 0;
442 for (auto S = Data.drop_front(32); !S.empty(); S = S.drop_front(RecordSize)) {
443 DataExtractor RecordExtractor(S, true, 8);
444 uint32_t OffsetPtr = 0;
Dean Michael Berris60c24872017-03-29 06:10:12 +0000445 if (State.Expects == FDRState::Token::SCAN_TO_END_OF_THREAD_BUF) {
446 RecordSize = State.CurrentBufferSize - State.CurrentBufferConsumed;
Martin Pelikand78db152017-09-15 04:22:16 +0000447 if (S.size() < RecordSize) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000448 return make_error<StringError>(
Martin Pelikand78db152017-09-15 04:22:16 +0000449 Twine("Incomplete thread buffer. Expected at least ") +
450 Twine(RecordSize) + " bytes but found " + Twine(S.size()),
Dean Michael Berris60c24872017-03-29 06:10:12 +0000451 make_error_code(std::errc::invalid_argument));
452 }
453 State.CurrentBufferConsumed = 0;
454 State.Expects = FDRState::Token::NEW_BUFFER_RECORD_OR_EOF;
455 continue;
456 }
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000457 uint8_t BitField = RecordExtractor.getU8(&OffsetPtr);
458 bool isMetadataRecord = BitField & 0x01uL;
459 if (isMetadataRecord) {
460 RecordSize = 16;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000461 if (auto E = processFDRMetadataRecord(State, BitField, RecordExtractor,
Martin Pelikan10c873f2017-09-27 04:48:03 +0000462 RecordSize, Records))
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000463 return E;
464 } else { // Process Function Record
465 RecordSize = 8;
466 if (auto E = processFDRFunctionRecord(State, BitField, RecordExtractor,
467 Records))
468 return E;
469 }
Martin Pelikand78db152017-09-15 04:22:16 +0000470 State.CurrentBufferConsumed += RecordSize;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000471 }
Martin Pelikand78db152017-09-15 04:22:16 +0000472
473 // Having iterated over everything we've been given, we've either consumed
474 // everything and ended up in the end state, or were told to skip the rest.
475 bool Finished = State.Expects == FDRState::Token::SCAN_TO_END_OF_THREAD_BUF &&
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000476 State.CurrentBufferSize == State.CurrentBufferConsumed;
Martin Pelikand78db152017-09-15 04:22:16 +0000477 if (State.Expects != FDRState::Token::NEW_BUFFER_RECORD_OR_EOF && !Finished)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000478 return make_error<StringError>(
Dean Michael Berris60c24872017-03-29 06:10:12 +0000479 Twine("Encountered EOF with unexpected state expectation ") +
480 fdrStateToTwine(State.Expects) +
481 ". Remaining expected bytes in thread buffer total " +
482 Twine(State.CurrentBufferSize - State.CurrentBufferConsumed),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000483 std::make_error_code(std::errc::executable_format_error));
484
485 return Error::success();
486}
487
488Error loadYAMLLog(StringRef Data, XRayFileHeader &FileHeader,
489 std::vector<XRayRecord> &Records) {
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000490 YAMLXRayTrace Trace;
491 Input In(Data);
492 In >> Trace;
493 if (In.error())
494 return make_error<StringError>("Failed loading YAML Data.", In.error());
495
496 FileHeader.Version = Trace.Header.Version;
497 FileHeader.Type = Trace.Header.Type;
498 FileHeader.ConstantTSC = Trace.Header.ConstantTSC;
499 FileHeader.NonstopTSC = Trace.Header.NonstopTSC;
500 FileHeader.CycleFrequency = Trace.Header.CycleFrequency;
501
502 if (FileHeader.Version != 1)
503 return make_error<StringError>(
504 Twine("Unsupported XRay file version: ") + Twine(FileHeader.Version),
505 std::make_error_code(std::errc::invalid_argument));
506
507 Records.clear();
508 std::transform(Trace.Records.begin(), Trace.Records.end(),
509 std::back_inserter(Records), [&](const YAMLXRayRecord &R) {
510 return XRayRecord{R.RecordType, R.CPU, R.Type,
511 R.FuncId, R.TSC, R.TId};
512 });
513 return Error::success();
514}
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000515} // namespace
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000516
517Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
518 int Fd;
519 if (auto EC = sys::fs::openFileForRead(Filename, Fd)) {
520 return make_error<StringError>(
521 Twine("Cannot read log from '") + Filename + "'", EC);
522 }
523
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000524 uint64_t FileSize;
525 if (auto EC = sys::fs::file_size(Filename, FileSize)) {
526 return make_error<StringError>(
527 Twine("Cannot read log from '") + Filename + "'", EC);
528 }
529 if (FileSize < 4) {
530 return make_error<StringError>(
531 Twine("File '") + Filename + "' too small for XRay.",
Hans Wennborg84da6612017-01-12 18:33:14 +0000532 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000533 }
534
Martin Pelikand78db152017-09-15 04:22:16 +0000535 // Map the opened file into memory and use a StringRef to access it later.
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000536 std::error_code EC;
537 sys::fs::mapped_file_region MappedFile(
538 Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
539 if (EC) {
540 return make_error<StringError>(
541 Twine("Cannot read log from '") + Filename + "'", EC);
542 }
Martin Pelikand78db152017-09-15 04:22:16 +0000543 auto Data = StringRef(MappedFile.data(), MappedFile.size());
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000544
545 // Attempt to detect the file type using file magic. We have a slight bias
546 // towards the binary format, and we do this by making sure that the first 4
547 // bytes of the binary file is some combination of the following byte
Martin Pelikand78db152017-09-15 04:22:16 +0000548 // patterns: (observe the code loading them assumes they're little endian)
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000549 //
Martin Pelikand78db152017-09-15 04:22:16 +0000550 // 0x01 0x00 0x00 0x00 - version 1, "naive" format
551 // 0x01 0x00 0x01 0x00 - version 1, "flight data recorder" format
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000552 //
Martin Pelikand78db152017-09-15 04:22:16 +0000553 // YAML files don't typically have those first four bytes as valid text so we
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000554 // try loading assuming YAML if we don't find these bytes.
555 //
556 // Only if we can't load either the binary or the YAML format will we yield an
557 // error.
558 StringRef Magic(MappedFile.data(), 4);
559 DataExtractor HeaderExtractor(Magic, true, 8);
560 uint32_t OffsetPtr = 0;
561 uint16_t Version = HeaderExtractor.getU16(&OffsetPtr);
562 uint16_t Type = HeaderExtractor.getU16(&OffsetPtr);
563
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000564 enum BinaryFormatType { NAIVE_FORMAT = 0, FLIGHT_DATA_RECORDER_FORMAT = 1 };
565
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000566 Trace T;
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000567 if (Type == NAIVE_FORMAT && (Version == 1 || Version == 2)) {
568 if (auto E = loadNaiveFormatLog(Data, T.FileHeader, T.Records))
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000569 return std::move(E);
570 } else if (Version == 1 && Type == FLIGHT_DATA_RECORDER_FORMAT) {
Martin Pelikand78db152017-09-15 04:22:16 +0000571 if (auto E = loadFDRLog(Data, T.FileHeader, T.Records))
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000572 return std::move(E);
573 } else {
Martin Pelikand78db152017-09-15 04:22:16 +0000574 if (auto E = loadYAMLLog(Data, T.FileHeader, T.Records))
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000575 return std::move(E);
576 }
577
578 if (Sort)
579 std::sort(T.Records.begin(), T.Records.end(),
580 [&](const XRayRecord &L, const XRayRecord &R) {
581 return L.TSC < R.TSC;
582 });
583
584 return std::move(T);
585}