blob: 559d6a95a11df3b922caae670d72a25c732616b2 [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 Berrisa9d477a2018-08-07 04:42:39 +000028// This is the number of bytes in the "body" of a MetadataRecord in FDR Mode.
29// This already excludes the first byte, which indicates the type of metadata
30// record it is.
31constexpr auto kFDRMetadataBodySize = 15;
32
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +000033// Populates the FileHeader reference by reading the first 32 bytes of the file.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +000034Error readBinaryFormatHeader(DataExtractor &HeaderExtractor,
35 uint32_t &OffsetPtr, XRayFileHeader &FileHeader) {
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000036 // FIXME: Maybe deduce whether the data is little or big-endian using some
37 // magic bytes in the beginning of the file?
38
39 // First 32 bytes of the file will always be the header. We assume a certain
40 // format here:
41 //
42 // (2) uint16 : version
43 // (2) uint16 : type
44 // (4) uint32 : bitfield
45 // (8) uint64 : cycle frequency
46 // (16) - : padding
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000047
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +000048 auto PreReadOffset = OffsetPtr;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000049 FileHeader.Version = HeaderExtractor.getU16(&OffsetPtr);
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +000050 if (OffsetPtr == PreReadOffset)
51 return createStringError(
52 std::make_error_code(std::errc::invalid_argument),
53 "Failed reading version from file header at offset %d.", OffsetPtr);
54
55 PreReadOffset = OffsetPtr;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000056 FileHeader.Type = HeaderExtractor.getU16(&OffsetPtr);
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +000057 if (OffsetPtr == PreReadOffset)
58 return createStringError(
59 std::make_error_code(std::errc::invalid_argument),
60 "Failed reading file type from file header at offset %d.", OffsetPtr);
61
62 PreReadOffset = OffsetPtr;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000063 uint32_t Bitfield = HeaderExtractor.getU32(&OffsetPtr);
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +000064 if (OffsetPtr == PreReadOffset)
65 return createStringError(
66 std::make_error_code(std::errc::invalid_argument),
67 "Failed reading flag bits from file header at offset %d.", OffsetPtr);
68
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000069 FileHeader.ConstantTSC = Bitfield & 1uL;
70 FileHeader.NonstopTSC = Bitfield & 1uL << 1;
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +000071 PreReadOffset = OffsetPtr;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000072 FileHeader.CycleFrequency = HeaderExtractor.getU64(&OffsetPtr);
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +000073 if (OffsetPtr == PreReadOffset)
74 return createStringError(
75 std::make_error_code(std::errc::invalid_argument),
76 "Failed reading cycle frequency from file header at offset %d.",
77 OffsetPtr);
78
79 std::memcpy(&FileHeader.FreeFormData,
80 HeaderExtractor.getData().bytes_begin() + OffsetPtr, 16);
81
82 // Manually advance the offset pointer 16 bytes, after getting a raw memcpy
83 // from the underlying data.
84 OffsetPtr += 16;
Dean Michael Berris10141262018-07-13 05:38:22 +000085 if (FileHeader.Version != 1 && FileHeader.Version != 2 &&
86 FileHeader.Version != 3)
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +000087 return createStringError(std::make_error_code(std::errc::invalid_argument),
88 "Unsupported XRay file version: %d at offset %d",
89 FileHeader.Version, OffsetPtr);
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +000090 return Error::success();
91}
92
93Error loadNaiveFormatLog(StringRef Data, XRayFileHeader &FileHeader,
94 std::vector<XRayRecord> &Records) {
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +000095 if (Data.size() < 32)
96 return make_error<StringError>(
97 "Not enough bytes for an XRay log.",
98 std::make_error_code(std::errc::invalid_argument));
99
100 if (Data.size() - 32 == 0 || Data.size() % 32 != 0)
101 return make_error<StringError>(
102 "Invalid-sized XRay data.",
103 std::make_error_code(std::errc::invalid_argument));
104
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000105 DataExtractor Reader(Data, true, 8);
106 uint32_t OffsetPtr = 0;
107 if (auto E = readBinaryFormatHeader(Reader, OffsetPtr, FileHeader))
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000108 return E;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000109
110 // Each record after the header will be 32 bytes, in the following format:
111 //
112 // (2) uint16 : record type
113 // (1) uint8 : cpu id
114 // (1) uint8 : type
115 // (4) sint32 : function id
116 // (8) uint64 : tsc
117 // (4) uint32 : thread id
Dean Michael Berris10141262018-07-13 05:38:22 +0000118 // (4) uint32 : process id
119 // (8) - : padding
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000120 while (Reader.isValidOffset(OffsetPtr)) {
121 if (!Reader.isValidOffsetForDataOfSize(OffsetPtr, 32))
122 return createStringError(
123 std::make_error_code(std::errc::executable_format_error),
124 "Not enough bytes to read a full record at offset %d.", OffsetPtr);
125 auto PreReadOffset = OffsetPtr;
126 auto RecordType = Reader.getU16(&OffsetPtr);
127 if (OffsetPtr == PreReadOffset)
128 return createStringError(
129 std::make_error_code(std::errc::executable_format_error),
130 "Failed reading record type at offset %d.", OffsetPtr);
131
132 switch (RecordType) {
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000133 case 0: { // Normal records.
134 Records.emplace_back();
135 auto &Record = Records.back();
136 Record.RecordType = RecordType;
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000137
138 PreReadOffset = OffsetPtr;
139 Record.CPU = Reader.getU8(&OffsetPtr);
140 if (OffsetPtr == PreReadOffset)
141 return createStringError(
142 std::make_error_code(std::errc::executable_format_error),
143 "Failed reading CPU field at offset %d.", OffsetPtr);
144
145 PreReadOffset = OffsetPtr;
146 auto Type = Reader.getU8(&OffsetPtr);
147 if (OffsetPtr == PreReadOffset)
148 return createStringError(
149 std::make_error_code(std::errc::executable_format_error),
150 "Failed reading record type field at offset %d.", OffsetPtr);
151
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000152 switch (Type) {
153 case 0:
154 Record.Type = RecordTypes::ENTER;
155 break;
156 case 1:
157 Record.Type = RecordTypes::EXIT;
158 break;
159 case 2:
160 Record.Type = RecordTypes::TAIL_EXIT;
161 break;
162 case 3:
163 Record.Type = RecordTypes::ENTER_ARG;
164 break;
165 default:
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000166 return createStringError(
167 std::make_error_code(std::errc::executable_format_error),
168 "Unknown record type '%d' at offset %d.", Type, OffsetPtr);
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000169 }
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000170
171 PreReadOffset = OffsetPtr;
172 Record.FuncId = Reader.getSigned(&OffsetPtr, sizeof(int32_t));
173 if (OffsetPtr == PreReadOffset)
174 return createStringError(
175 std::make_error_code(std::errc::executable_format_error),
176 "Failed reading function id field at offset %d.", OffsetPtr);
177
178 PreReadOffset = OffsetPtr;
179 Record.TSC = Reader.getU64(&OffsetPtr);
180 if (OffsetPtr == PreReadOffset)
181 return createStringError(
182 std::make_error_code(std::errc::executable_format_error),
183 "Failed reading TSC field at offset %d.", OffsetPtr);
184
185 PreReadOffset = OffsetPtr;
186 Record.TId = Reader.getU32(&OffsetPtr);
187 if (OffsetPtr == PreReadOffset)
188 return createStringError(
189 std::make_error_code(std::errc::executable_format_error),
190 "Failed reading thread id field at offset %d.", OffsetPtr);
191
192 PreReadOffset = OffsetPtr;
193 Record.PId = Reader.getU32(&OffsetPtr);
194 if (OffsetPtr == PreReadOffset)
195 return createStringError(
196 std::make_error_code(std::errc::executable_format_error),
197 "Failed reading process id at offset %d.", OffsetPtr);
198
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000199 break;
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000200 }
201 case 1: { // Arg payload record.
202 auto &Record = Records.back();
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000203
204 // We skip the next two bytes of the record, because we don't need the
205 // type and the CPU record for arg payloads.
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000206 OffsetPtr += 2;
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000207 PreReadOffset = OffsetPtr;
208 int32_t FuncId = Reader.getSigned(&OffsetPtr, sizeof(int32_t));
209 if (OffsetPtr == PreReadOffset)
210 return createStringError(
211 std::make_error_code(std::errc::executable_format_error),
212 "Failed reading function id field at offset %d.", OffsetPtr);
213
214 PreReadOffset = OffsetPtr;
215 auto TId = Reader.getU32(&OffsetPtr);
216 if (OffsetPtr == PreReadOffset)
217 return createStringError(
218 std::make_error_code(std::errc::executable_format_error),
219 "Failed reading thread id field at offset %d.", OffsetPtr);
220
221 PreReadOffset = OffsetPtr;
222 auto PId = Reader.getU32(&OffsetPtr);
223 if (OffsetPtr == PreReadOffset)
224 return createStringError(
225 std::make_error_code(std::errc::executable_format_error),
226 "Failed reading process id field at offset %d.", OffsetPtr);
Dean Michael Berris10141262018-07-13 05:38:22 +0000227
228 // Make a check for versions above 3 for the Pid field
229 if (Record.FuncId != FuncId || Record.TId != TId ||
230 (FileHeader.Version >= 3 ? Record.PId != PId : false))
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000231 return createStringError(
232 std::make_error_code(std::errc::executable_format_error),
233 "Corrupted log, found arg payload following non-matching "
234 "function+thread record. Record for function %d != %d at offset "
235 "%d",
236 Record.FuncId, FuncId, OffsetPtr);
Dean Michael Berris10141262018-07-13 05:38:22 +0000237
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000238 PreReadOffset = OffsetPtr;
239 auto Arg = Reader.getU64(&OffsetPtr);
240 if (OffsetPtr == PreReadOffset)
241 return createStringError(
242 std::make_error_code(std::errc::executable_format_error),
243 "Failed reading argument payload at offset %d.", OffsetPtr);
244
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000245 Record.CallArgs.push_back(Arg);
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000246 break;
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000247 }
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000248 default:
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000249 return createStringError(
250 std::make_error_code(std::errc::executable_format_error),
251 "Unknown record type '%d' at offset %d.", RecordType, OffsetPtr);
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000252 }
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000253 // Advance the offset pointer enough bytes to align to 32-byte records for
254 // basic mode logs.
255 OffsetPtr += 8;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000256 }
257 return Error::success();
258}
259
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000260/// When reading from a Flight Data Recorder mode log, metadata records are
261/// sparse compared to packed function records, so we must maintain state as we
262/// read through the sequence of entries. This allows the reader to denormalize
263/// the CPUId and Thread Id onto each Function Record and transform delta
264/// encoded TSC values into absolute encodings on each record.
265struct FDRState {
266 uint16_t CPUId;
267 uint16_t ThreadId;
Dean Michael Berris10141262018-07-13 05:38:22 +0000268 int32_t ProcessId;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000269 uint64_t BaseTSC;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000270
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000271 /// Encode some of the state transitions for the FDR log reader as explicit
272 /// checks. These are expectations for the next Record in the stream.
273 enum class Token {
274 NEW_BUFFER_RECORD_OR_EOF,
275 WALLCLOCK_RECORD,
276 NEW_CPU_ID_RECORD,
Dean Michael Berris60c24872017-03-29 06:10:12 +0000277 FUNCTION_SEQUENCE,
278 SCAN_TO_END_OF_THREAD_BUF,
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000279 CUSTOM_EVENT_DATA,
Martin Pelikan10c873f2017-09-27 04:48:03 +0000280 CALL_ARGUMENT,
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000281 BUFFER_EXTENTS,
Dean Michael Berris10141262018-07-13 05:38:22 +0000282 PID_RECORD,
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000283 };
284 Token Expects;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000285
Dean Michael Berris60c24872017-03-29 06:10:12 +0000286 // Each threads buffer may have trailing garbage to scan over, so we track our
287 // progress.
288 uint64_t CurrentBufferSize;
289 uint64_t CurrentBufferConsumed;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000290};
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000291
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000292const char *fdrStateToTwine(const FDRState::Token &state) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000293 switch (state) {
294 case FDRState::Token::NEW_BUFFER_RECORD_OR_EOF:
295 return "NEW_BUFFER_RECORD_OR_EOF";
296 case FDRState::Token::WALLCLOCK_RECORD:
297 return "WALLCLOCK_RECORD";
298 case FDRState::Token::NEW_CPU_ID_RECORD:
299 return "NEW_CPU_ID_RECORD";
300 case FDRState::Token::FUNCTION_SEQUENCE:
301 return "FUNCTION_SEQUENCE";
302 case FDRState::Token::SCAN_TO_END_OF_THREAD_BUF:
303 return "SCAN_TO_END_OF_THREAD_BUF";
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000304 case FDRState::Token::CUSTOM_EVENT_DATA:
305 return "CUSTOM_EVENT_DATA";
Martin Pelikan10c873f2017-09-27 04:48:03 +0000306 case FDRState::Token::CALL_ARGUMENT:
307 return "CALL_ARGUMENT";
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000308 case FDRState::Token::BUFFER_EXTENTS:
309 return "BUFFER_EXTENTS";
Dean Michael Berris10141262018-07-13 05:38:22 +0000310 case FDRState::Token::PID_RECORD:
311 return "PID_RECORD";
Dean Michael Berris60c24872017-03-29 06:10:12 +0000312 }
313 return "UNKNOWN";
314}
315
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000316/// State transition when a NewBufferRecord is encountered.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000317Error processFDRNewBufferRecord(FDRState &State, DataExtractor &RecordExtractor,
318 uint32_t &OffsetPtr) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000319 if (State.Expects != FDRState::Token::NEW_BUFFER_RECORD_OR_EOF)
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000320 return createStringError(
321 std::make_error_code(std::errc::executable_format_error),
322 "Malformed log: Read New Buffer record kind out of sequence; expected: "
323 "%s at offset %d.",
324 fdrStateToTwine(State.Expects), OffsetPtr);
325
326 auto PreReadOffset = OffsetPtr;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000327 State.ThreadId = RecordExtractor.getU16(&OffsetPtr);
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000328 if (OffsetPtr == PreReadOffset)
329 return createStringError(
330 std::make_error_code(std::errc::executable_format_error),
331 "Failed reading the thread id at offset %d.", OffsetPtr);
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000332 State.Expects = FDRState::Token::WALLCLOCK_RECORD;
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000333
334 // Advance the offset pointer by enough bytes representing the remaining
335 // padding in a metadata record.
336 OffsetPtr += kFDRMetadataBodySize - 2;
337 assert(OffsetPtr - PreReadOffset == kFDRMetadataBodySize);
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000338 return Error::success();
339}
340
341/// State transition when an EndOfBufferRecord is encountered.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000342Error processFDREndOfBufferRecord(FDRState &State, uint32_t &OffsetPtr) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000343 if (State.Expects == FDRState::Token::NEW_BUFFER_RECORD_OR_EOF)
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000344 return createStringError(
345 std::make_error_code(std::errc::executable_format_error),
346 "Malformed log: Received EOB message without current buffer; expected: "
347 "%s at offset %d.",
348 fdrStateToTwine(State.Expects), OffsetPtr);
349
Dean Michael Berris60c24872017-03-29 06:10:12 +0000350 State.Expects = FDRState::Token::SCAN_TO_END_OF_THREAD_BUF;
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000351
352 // Advance the offset pointer by enough bytes representing the remaining
353 // padding in a metadata record.
354 OffsetPtr += kFDRMetadataBodySize;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000355 return Error::success();
356}
357
358/// State transition when a NewCPUIdRecord is encountered.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000359Error processFDRNewCPUIdRecord(FDRState &State, DataExtractor &RecordExtractor,
360 uint32_t &OffsetPtr) {
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000361 if (State.Expects != FDRState::Token::FUNCTION_SEQUENCE &&
Dean Michael Berris60c24872017-03-29 06:10:12 +0000362 State.Expects != FDRState::Token::NEW_CPU_ID_RECORD)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000363 return make_error<StringError>(
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000364 Twine("Malformed log. Read NewCPUId record kind out of sequence; "
365 "expected: ") +
366 fdrStateToTwine(State.Expects),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000367 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000368 auto BeginOffset = OffsetPtr;
369 auto PreReadOffset = OffsetPtr;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000370 State.CPUId = RecordExtractor.getU16(&OffsetPtr);
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000371 if (OffsetPtr == PreReadOffset)
372 return createStringError(
373 std::make_error_code(std::errc::executable_format_error),
374 "Failed reading the CPU field at offset %d.", OffsetPtr);
375
376 PreReadOffset = OffsetPtr;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000377 State.BaseTSC = RecordExtractor.getU64(&OffsetPtr);
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000378 if (OffsetPtr == PreReadOffset)
379 return createStringError(
380 std::make_error_code(std::errc::executable_format_error),
381 "Failed reading the base TSC field at offset %d.", OffsetPtr);
382
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000383 State.Expects = FDRState::Token::FUNCTION_SEQUENCE;
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000384
385 // Advance the offset pointer by a few bytes, to account for the padding in
386 // CPU ID metadata records that we've already advanced through.
387 OffsetPtr += kFDRMetadataBodySize - (OffsetPtr - BeginOffset);
388 assert(OffsetPtr - BeginOffset == kFDRMetadataBodySize);
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000389 return Error::success();
390}
391
392/// State transition when a TSCWrapRecord (overflow detection) is encountered.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000393Error processFDRTSCWrapRecord(FDRState &State, DataExtractor &RecordExtractor,
394 uint32_t &OffsetPtr) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000395 if (State.Expects != FDRState::Token::FUNCTION_SEQUENCE)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000396 return make_error<StringError>(
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000397 Twine("Malformed log. Read TSCWrap record kind out of sequence; "
398 "expecting: ") +
399 fdrStateToTwine(State.Expects),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000400 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000401 auto PreReadOffset = OffsetPtr;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000402 State.BaseTSC = RecordExtractor.getU64(&OffsetPtr);
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000403 if (OffsetPtr == PreReadOffset)
404 return createStringError(
405 std::make_error_code(std::errc::executable_format_error),
406 "Failed reading the base TSC field at offset %d.", OffsetPtr);
407
408 // Advance the offset pointer by a few more bytes, accounting for the padding
409 // in the metadata record after reading the base TSC.
410 OffsetPtr += kFDRMetadataBodySize - 8;
411 assert(OffsetPtr - PreReadOffset == kFDRMetadataBodySize);
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000412 return Error::success();
413}
414
415/// State transition when a WallTimeMarkerRecord is encountered.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000416Error processFDRWallTimeRecord(FDRState &State, DataExtractor &RecordExtractor,
417 uint32_t &OffsetPtr) {
Dean Michael Berris60c24872017-03-29 06:10:12 +0000418 if (State.Expects != FDRState::Token::WALLCLOCK_RECORD)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000419 return make_error<StringError>(
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000420 Twine("Malformed log. Read Wallclock record kind out of sequence; "
421 "expecting: ") +
422 fdrStateToTwine(State.Expects),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000423 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000424
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000425 // Read in the data from the walltime record.
426 auto PreReadOffset = OffsetPtr;
427 auto WallTime = RecordExtractor.getU64(&OffsetPtr);
428 if (OffsetPtr == PreReadOffset)
429 return createStringError(
430 std::make_error_code(std::errc::executable_format_error),
431 "Failed reading the walltime record at offset %d.", OffsetPtr);
432
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000433 // TODO: Someday, reconcile the TSC ticks to wall clock time for presentation
434 // purposes. For now, we're ignoring these records.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000435 (void)WallTime;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000436 State.Expects = FDRState::Token::NEW_CPU_ID_RECORD;
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000437
438 // Advance the offset pointer by a few more bytes, accounting for the padding
439 // in the metadata record after reading in the walltime data.
440 OffsetPtr += kFDRMetadataBodySize - 8;
441 assert(OffsetPtr - PreReadOffset == kFDRMetadataBodySize);
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000442 return Error::success();
443}
444
Dean Michael Berris10141262018-07-13 05:38:22 +0000445/// State transition when a PidRecord is encountered.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000446Error processFDRPidRecord(FDRState &State, DataExtractor &RecordExtractor,
447 uint32_t &OffsetPtr) {
Dean Michael Berris10141262018-07-13 05:38:22 +0000448 if (State.Expects != FDRState::Token::PID_RECORD)
449 return make_error<StringError>(
450 Twine("Malformed log. Read Pid record kind out of sequence; "
451 "expected: ") +
452 fdrStateToTwine(State.Expects),
453 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000454 auto PreReadOffset = OffsetPtr;
Dean Michael Berris10141262018-07-13 05:38:22 +0000455 State.ProcessId = RecordExtractor.getU32(&OffsetPtr);
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000456 if (OffsetPtr == PreReadOffset)
457 return createStringError(
458 std::make_error_code(std::errc::executable_format_error),
459 "Failed reading the process ID at offset %d.", OffsetPtr);
Dean Michael Berris10141262018-07-13 05:38:22 +0000460 State.Expects = FDRState::Token::NEW_CPU_ID_RECORD;
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000461
462 // Advance the offset pointer by a few more bytes, accounting for the padding
463 // in the metadata record after reading in the PID.
464 OffsetPtr += kFDRMetadataBodySize - 4;
465 assert(OffsetPtr - PreReadOffset == kFDRMetadataBodySize);
Dean Michael Berris10141262018-07-13 05:38:22 +0000466 return Error::success();
467}
468
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000469/// State transition when a CustomEventMarker is encountered.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000470Error processCustomEventMarker(FDRState &State, DataExtractor &RecordExtractor,
471 uint32_t &OffsetPtr) {
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000472 // We can encounter a CustomEventMarker anywhere in the log, so we can handle
Keith Wyss3d0bc9e2017-08-02 21:47:27 +0000473 // it regardless of the expectation. However, we do set the expectation to
474 // read a set number of fixed bytes, as described in the metadata.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000475 auto BeginOffset = OffsetPtr;
476 auto PreReadOffset = OffsetPtr;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000477 uint32_t DataSize = RecordExtractor.getU32(&OffsetPtr);
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000478 if (OffsetPtr == PreReadOffset)
479 return createStringError(
480 std::make_error_code(std::errc::executable_format_error),
481 "Failed reading a custom event marker at offset %d.", OffsetPtr);
482
483 PreReadOffset = OffsetPtr;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000484 uint64_t TSC = RecordExtractor.getU64(&OffsetPtr);
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000485 if (OffsetPtr == PreReadOffset)
486 return createStringError(
487 std::make_error_code(std::errc::executable_format_error),
488 "Failed reading the TSC at offset %d.", OffsetPtr);
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000489
Dean Michael Berris0a465d72017-10-05 05:18:17 +0000490 // FIXME: Actually represent the record through the API. For now we only
491 // skip through the data.
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000492 (void)TSC;
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000493 // Advance the offset ptr by the size of the data associated with the custom
494 // event, as well as the padding associated with the remainder of the metadata
495 // record.
496 OffsetPtr += (kFDRMetadataBodySize - (OffsetPtr - BeginOffset)) + DataSize;
497 if (!RecordExtractor.isValidOffset(OffsetPtr))
498 return createStringError(
499 std::make_error_code(std::errc::executable_format_error),
500 "Reading custom event data moves past addressable trace data (starting "
501 "at offset %d, advancing to offset %d).",
502 BeginOffset, OffsetPtr);
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000503 return Error::success();
504}
505
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000506/// State transition when an BufferExtents record is encountered.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000507Error processBufferExtents(FDRState &State, DataExtractor &RecordExtractor,
508 uint32_t &OffsetPtr) {
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000509 if (State.Expects != FDRState::Token::BUFFER_EXTENTS)
510 return make_error<StringError>(
511 Twine("Malformed log. Buffer Extents unexpected; expected: ") +
512 fdrStateToTwine(State.Expects),
513 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000514
515 auto PreReadOffset = OffsetPtr;
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000516 State.CurrentBufferSize = RecordExtractor.getU64(&OffsetPtr);
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000517 if (OffsetPtr == PreReadOffset)
518 return createStringError(
519 std::make_error_code(std::errc::executable_format_error),
520 "Failed to read current buffer size at offset %d.", OffsetPtr);
521
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000522 State.Expects = FDRState::Token::NEW_BUFFER_RECORD_OR_EOF;
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000523
524 // Advance the offset pointer by enough bytes accounting for the padding in a
525 // metadata record, after we read in the buffer extents.
526 OffsetPtr += kFDRMetadataBodySize - 8;
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000527 return Error::success();
528}
529
Martin Pelikan10c873f2017-09-27 04:48:03 +0000530/// State transition when a CallArgumentRecord is encountered.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000531Error processFDRCallArgumentRecord(FDRState &State,
Martin Pelikan10c873f2017-09-27 04:48:03 +0000532 DataExtractor &RecordExtractor,
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000533 std::vector<XRayRecord> &Records,
534 uint32_t &OffsetPtr) {
Martin Pelikan10c873f2017-09-27 04:48:03 +0000535 auto &Enter = Records.back();
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000536 if (Enter.Type != RecordTypes::ENTER && Enter.Type != RecordTypes::ENTER_ARG)
Martin Pelikan10c873f2017-09-27 04:48:03 +0000537 return make_error<StringError>(
538 "CallArgument needs to be right after a function entry",
539 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000540
541 auto PreReadOffset = OffsetPtr;
542 auto Arg = RecordExtractor.getU64(&OffsetPtr);
543 if (OffsetPtr == PreReadOffset)
544 return createStringError(
545 std::make_error_code(std::errc::executable_format_error),
546 "Failed to read argument record at offset %d.", OffsetPtr);
547
Martin Pelikan10c873f2017-09-27 04:48:03 +0000548 Enter.Type = RecordTypes::ENTER_ARG;
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000549 Enter.CallArgs.emplace_back(Arg);
550
551 // Advance the offset pointer by enough bytes accounting for the padding in a
552 // metadata record, after reading the payload.
553 OffsetPtr += kFDRMetadataBodySize - 8;
Martin Pelikan10c873f2017-09-27 04:48:03 +0000554 return Error::success();
555}
556
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000557/// Advances the state machine for reading the FDR record type by reading one
Keith Wysse96152a2017-04-06 03:32:01 +0000558/// Metadata Record and updating the State appropriately based on the kind of
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000559/// record encountered. The RecordKind is encoded in the first byte of the
560/// Record, which the caller should pass in because they have already read it
561/// to determine that this is a metadata record as opposed to a function record.
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000562///
563/// Beginning with Version 2 of the FDR log, we do not depend on the size of the
564/// buffer, but rather use the extents to determine how far to read in the log
565/// for this particular buffer.
Dean Michael Berris10141262018-07-13 05:38:22 +0000566///
567/// In Version 3, FDR log now includes a pid metadata record after
568/// WallTimeMarker
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000569Error processFDRMetadataRecord(FDRState &State, DataExtractor &RecordExtractor,
570 uint32_t &OffsetPtr,
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000571 std::vector<XRayRecord> &Records,
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000572 uint16_t Version, uint8_t FirstByte) {
573 // The remaining 7 bits of the first byte are the RecordKind enum for each
574 // Metadata Record.
575 switch (FirstByte >> 1) {
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000576 case 0: // NewBuffer
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000577 if (auto E = processFDRNewBufferRecord(State, RecordExtractor, OffsetPtr))
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000578 return E;
579 break;
580 case 1: // EndOfBuffer
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000581 if (Version >= 2)
582 return make_error<StringError>(
583 "Since Version 2 of FDR logging, we no longer support EOB records.",
584 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000585 if (auto E = processFDREndOfBufferRecord(State, OffsetPtr))
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000586 return E;
587 break;
588 case 2: // NewCPUId
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000589 if (auto E = processFDRNewCPUIdRecord(State, RecordExtractor, OffsetPtr))
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000590 return E;
591 break;
592 case 3: // TSCWrap
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000593 if (auto E = processFDRTSCWrapRecord(State, RecordExtractor, OffsetPtr))
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000594 return E;
595 break;
596 case 4: // WallTimeMarker
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000597 if (auto E = processFDRWallTimeRecord(State, RecordExtractor, OffsetPtr))
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000598 return E;
Dean Michael Berris10141262018-07-13 05:38:22 +0000599 // In Version 3 and and above, a PidRecord is expected after WallTimeRecord
600 if (Version >= 3)
601 State.Expects = FDRState::Token::PID_RECORD;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000602 break;
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000603 case 5: // CustomEventMarker
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000604 if (auto E = processCustomEventMarker(State, RecordExtractor, OffsetPtr))
Dean Michael Berrisa7bbe442017-05-12 01:06:41 +0000605 return E;
606 break;
Martin Pelikan10c873f2017-09-27 04:48:03 +0000607 case 6: // CallArgument
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000608 if (auto E = processFDRCallArgumentRecord(State, RecordExtractor, Records,
609 OffsetPtr))
Martin Pelikan10c873f2017-09-27 04:48:03 +0000610 return E;
611 break;
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000612 case 7: // BufferExtents
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000613 if (auto E = processBufferExtents(State, RecordExtractor, OffsetPtr))
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000614 return E;
615 break;
Dean Michael Berris10141262018-07-13 05:38:22 +0000616 case 9: // Pid
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000617 if (auto E = processFDRPidRecord(State, RecordExtractor, OffsetPtr))
Dean Michael Berris10141262018-07-13 05:38:22 +0000618 return E;
619 break;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000620 default:
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000621 return createStringError(
622 std::make_error_code(std::errc::executable_format_error),
623 "Illegal metadata record type: '%d' at offset %d.", FirstByte >> 1,
624 OffsetPtr);
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000625 }
626 return Error::success();
627}
628
629/// Reads a function record from an FDR format log, appending a new XRayRecord
630/// to the vector being populated and updating the State with a new value
631/// reference value to interpret TSC deltas.
632///
633/// The XRayRecord constructed includes information from the function record
634/// processed here as well as Thread ID and CPU ID formerly extracted into
635/// State.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000636Error processFDRFunctionRecord(FDRState &State, DataExtractor &RecordExtractor,
637 uint32_t &OffsetPtr, uint8_t FirstByte,
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000638 std::vector<XRayRecord> &Records) {
639 switch (State.Expects) {
640 case FDRState::Token::NEW_BUFFER_RECORD_OR_EOF:
641 return make_error<StringError>(
642 "Malformed log. Received Function Record before new buffer setup.",
643 std::make_error_code(std::errc::executable_format_error));
644 case FDRState::Token::WALLCLOCK_RECORD:
645 return make_error<StringError>(
646 "Malformed log. Received Function Record when expecting wallclock.",
647 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris10141262018-07-13 05:38:22 +0000648 case FDRState::Token::PID_RECORD:
649 return make_error<StringError>(
650 "Malformed log. Received Function Record when expecting pid.",
651 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000652 case FDRState::Token::NEW_CPU_ID_RECORD:
653 return make_error<StringError>(
654 "Malformed log. Received Function Record before first CPU record.",
655 std::make_error_code(std::errc::executable_format_error));
656 default:
657 Records.emplace_back();
658 auto &Record = Records.back();
659 Record.RecordType = 0; // Record is type NORMAL.
660 // Strip off record type bit and use the next three bits.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000661 auto T = (FirstByte >> 1) & 0x07;
662 switch (T) {
663 case static_cast<decltype(T)>(RecordTypes::ENTER):
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000664 Record.Type = RecordTypes::ENTER;
665 break;
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000666 case static_cast<decltype(T)>(RecordTypes::EXIT):
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000667 Record.Type = RecordTypes::EXIT;
668 break;
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000669 case static_cast<decltype(T)>(RecordTypes::TAIL_EXIT):
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000670 Record.Type = RecordTypes::TAIL_EXIT;
671 break;
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000672 case static_cast<decltype(T)>(RecordTypes::ENTER_ARG):
673 Record.Type = RecordTypes::ENTER_ARG;
674 State.Expects = FDRState::Token::CALL_ARGUMENT;
675 break;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000676 default:
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000677 return createStringError(
678 std::make_error_code(std::errc::executable_format_error),
679 "Illegal function record type '%d' at offset %d.", T, OffsetPtr);
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000680 }
681 Record.CPU = State.CPUId;
682 Record.TId = State.ThreadId;
Dean Michael Berris10141262018-07-13 05:38:22 +0000683 Record.PId = State.ProcessId;
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000684
685 // Back up one byte to re-read the first byte, which is important for
686 // computing the function id for a record.
687 --OffsetPtr;
688
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000689 // Despite function Id being a signed int on XRayRecord,
690 // when it is written to an FDR format, the top bits are truncated,
691 // so it is effectively an unsigned value. When we shift off the
692 // top four bits, we want the shift to be logical, so we read as
693 // uint32_t.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000694 auto PreReadOffset = OffsetPtr;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000695 uint32_t FuncIdBitField = RecordExtractor.getU32(&OffsetPtr);
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000696 if (OffsetPtr == PreReadOffset)
697 return createStringError(
698 std::make_error_code(std::errc::executable_format_error),
699 "Failed reading truncated function id field at offset %d.",
700 OffsetPtr);
701
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000702 Record.FuncId = FuncIdBitField >> 4;
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000703
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000704 // FunctionRecords have a 32 bit delta from the previous absolute TSC
705 // or TSC delta. If this would overflow, we should read a TSCWrap record
706 // with an absolute TSC reading.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000707 PreReadOffset = OffsetPtr;
Martin Pelikand78db152017-09-15 04:22:16 +0000708 uint64_t NewTSC = State.BaseTSC + RecordExtractor.getU32(&OffsetPtr);
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000709 if (OffsetPtr == PreReadOffset)
710 return createStringError(
711 std::make_error_code(std::errc::executable_format_error),
712 "Failed reading TSC delta at offset %d.", OffsetPtr);
713
Martin Pelikand78db152017-09-15 04:22:16 +0000714 State.BaseTSC = NewTSC;
715 Record.TSC = NewTSC;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000716 }
717 return Error::success();
718}
719
720/// Reads a log in FDR mode for version 1 of this binary format. FDR mode is
721/// defined as part of the compiler-rt project in xray_fdr_logging.h, and such
722/// a log consists of the familiar 32 bit XRayHeader, followed by sequences of
723/// of interspersed 16 byte Metadata Records and 8 byte Function Records.
724///
725/// The following is an attempt to document the grammar of the format, which is
726/// parsed by this function for little-endian machines. Since the format makes
Martin Pelikand78db152017-09-15 04:22:16 +0000727/// use of BitFields, when we support big-endian architectures, we will need to
Simon Pilgrim68168d12017-03-30 12:59:53 +0000728/// adjust not only the endianness parameter to llvm's RecordExtractor, but also
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000729/// the bit twiddling logic, which is consistent with the little-endian
730/// convention that BitFields within a struct will first be packed into the
731/// least significant bits the address they belong to.
732///
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000733/// We expect a format complying with the grammar in the following pseudo-EBNF
734/// in Version 1 of the FDR log.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000735///
736/// FDRLog: XRayFileHeader ThreadBuffer*
Keith Wyss3d0bc9e2017-08-02 21:47:27 +0000737/// XRayFileHeader: 32 bytes to identify the log as FDR with machine metadata.
738/// Includes BufferSize
739/// ThreadBuffer: NewBuffer WallClockTime NewCPUId FunctionSequence EOB
Dean Michael Berris60c24872017-03-29 06:10:12 +0000740/// BufSize: 8 byte unsigned integer indicating how large the buffer is.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000741/// NewBuffer: 16 byte metadata record with Thread Id.
742/// WallClockTime: 16 byte metadata record with human readable time.
Dean Michael Berris10141262018-07-13 05:38:22 +0000743/// Pid: 16 byte metadata record with Pid
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000744/// NewCPUId: 16 byte metadata record with CPUId and a 64 bit TSC reading.
Dean Michael Berris60c24872017-03-29 06:10:12 +0000745/// EOB: 16 byte record in a thread buffer plus mem garbage to fill BufSize.
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000746/// FunctionSequence: NewCPUId | TSCWrap | FunctionRecord
747/// TSCWrap: 16 byte metadata record with a full 64 bit TSC reading.
748/// FunctionRecord: 8 byte record with FunctionId, entry/exit, and TSC delta.
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000749///
750/// In Version 2, we make the following changes:
751///
752/// ThreadBuffer: BufferExtents NewBuffer WallClockTime NewCPUId
753/// FunctionSequence
754/// BufferExtents: 16 byte metdata record describing how many usable bytes are
755/// in the buffer. This is measured from the start of the buffer
756/// and must always be at least 48 (bytes).
Dean Michael Berris10141262018-07-13 05:38:22 +0000757///
758/// In Version 3, we make the following changes:
759///
760/// ThreadBuffer: BufferExtents NewBuffer WallClockTime Pid NewCPUId
761/// FunctionSequence
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000762/// EOB: *deprecated*
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000763Error loadFDRLog(StringRef Data, XRayFileHeader &FileHeader,
764 std::vector<XRayRecord> &Records) {
765 if (Data.size() < 32)
766 return make_error<StringError>(
767 "Not enough bytes for an XRay log.",
768 std::make_error_code(std::errc::invalid_argument));
769
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000770 DataExtractor Reader(Data, true, 8);
771 uint32_t OffsetPtr = 0;
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000772
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000773 if (auto E = readBinaryFormatHeader(Reader, OffsetPtr, FileHeader))
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000774 return E;
775
Dean Michael Berris60c24872017-03-29 06:10:12 +0000776 uint64_t BufferSize = 0;
777 {
778 StringRef ExtraDataRef(FileHeader.FreeFormData, 16);
779 DataExtractor ExtraDataExtractor(ExtraDataRef, true, 8);
780 uint32_t ExtraDataOffset = 0;
781 BufferSize = ExtraDataExtractor.getU64(&ExtraDataOffset);
782 }
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000783
784 FDRState::Token InitialExpectation;
785 switch (FileHeader.Version) {
786 case 1:
787 InitialExpectation = FDRState::Token::NEW_BUFFER_RECORD_OR_EOF;
788 break;
789 case 2:
Dean Michael Berris10141262018-07-13 05:38:22 +0000790 case 3:
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000791 InitialExpectation = FDRState::Token::BUFFER_EXTENTS;
792 break;
793 default:
794 return make_error<StringError>(
795 Twine("Unsupported version '") + Twine(FileHeader.Version) + "'",
796 std::make_error_code(std::errc::executable_format_error));
797 }
Dean Michael Berris10141262018-07-13 05:38:22 +0000798 FDRState State{0, 0, 0, 0, InitialExpectation, BufferSize, 0};
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000799
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000800 // RecordSize will tell the loop how far to seek ahead based on the record
801 // type that we have just read.
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000802 while (Reader.isValidOffset(OffsetPtr)) {
803 auto BeginOffset = OffsetPtr;
Dean Michael Berris60c24872017-03-29 06:10:12 +0000804 if (State.Expects == FDRState::Token::SCAN_TO_END_OF_THREAD_BUF) {
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000805 OffsetPtr += State.CurrentBufferSize - State.CurrentBufferConsumed;
Dean Michael Berris60c24872017-03-29 06:10:12 +0000806 State.CurrentBufferConsumed = 0;
807 State.Expects = FDRState::Token::NEW_BUFFER_RECORD_OR_EOF;
808 continue;
809 }
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000810 auto PreReadOffset = OffsetPtr;
811 uint8_t BitField = Reader.getU8(&OffsetPtr);
812 if (OffsetPtr == PreReadOffset)
813 return createStringError(
814 std::make_error_code(std::errc::executable_format_error),
815 "Failed reading first byte of record at offset %d.", OffsetPtr);
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000816 bool isMetadataRecord = BitField & 0x01uL;
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000817 bool isBufferExtents =
818 (BitField >> 1) == 7; // BufferExtents record kind == 7
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000819 if (isMetadataRecord) {
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000820 if (auto E = processFDRMetadataRecord(State, Reader, OffsetPtr, Records,
821 FileHeader.Version, BitField))
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000822 return E;
823 } else { // Process Function Record
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000824 if (auto E = processFDRFunctionRecord(State, Reader, OffsetPtr, BitField,
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000825 Records))
826 return E;
827 }
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000828
829 // The BufferExtents record is technically not part of the buffer, so we
830 // don't count the size of that record against the buffer's actual size.
831 if (!isBufferExtents)
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000832 State.CurrentBufferConsumed += OffsetPtr - BeginOffset;
833
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000834 assert(State.CurrentBufferConsumed <= State.CurrentBufferSize);
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000835
Dean Michael Berris10141262018-07-13 05:38:22 +0000836 if ((FileHeader.Version == 2 || FileHeader.Version == 3) &&
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000837 State.CurrentBufferSize == State.CurrentBufferConsumed) {
838 // In Version 2 of the log, we don't need to scan to the end of the thread
839 // buffer if we've already consumed all the bytes we need to.
840 State.Expects = FDRState::Token::BUFFER_EXTENTS;
841 State.CurrentBufferSize = BufferSize;
842 State.CurrentBufferConsumed = 0;
843 }
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000844 }
Martin Pelikand78db152017-09-15 04:22:16 +0000845
846 // Having iterated over everything we've been given, we've either consumed
847 // everything and ended up in the end state, or were told to skip the rest.
848 bool Finished = State.Expects == FDRState::Token::SCAN_TO_END_OF_THREAD_BUF &&
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000849 State.CurrentBufferSize == State.CurrentBufferConsumed;
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000850 if ((State.Expects != FDRState::Token::NEW_BUFFER_RECORD_OR_EOF &&
851 State.Expects != FDRState::Token::BUFFER_EXTENTS) &&
852 !Finished)
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000853 return make_error<StringError>(
Dean Michael Berris60c24872017-03-29 06:10:12 +0000854 Twine("Encountered EOF with unexpected state expectation ") +
855 fdrStateToTwine(State.Expects) +
856 ". Remaining expected bytes in thread buffer total " +
857 Twine(State.CurrentBufferSize - State.CurrentBufferConsumed),
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000858 std::make_error_code(std::errc::executable_format_error));
859
860 return Error::success();
861}
862
863Error loadYAMLLog(StringRef Data, XRayFileHeader &FileHeader,
864 std::vector<XRayRecord> &Records) {
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000865 YAMLXRayTrace Trace;
866 Input In(Data);
867 In >> Trace;
868 if (In.error())
869 return make_error<StringError>("Failed loading YAML Data.", In.error());
870
871 FileHeader.Version = Trace.Header.Version;
872 FileHeader.Type = Trace.Header.Type;
873 FileHeader.ConstantTSC = Trace.Header.ConstantTSC;
874 FileHeader.NonstopTSC = Trace.Header.NonstopTSC;
875 FileHeader.CycleFrequency = Trace.Header.CycleFrequency;
876
877 if (FileHeader.Version != 1)
878 return make_error<StringError>(
879 Twine("Unsupported XRay file version: ") + Twine(FileHeader.Version),
880 std::make_error_code(std::errc::invalid_argument));
881
882 Records.clear();
883 std::transform(Trace.Records.begin(), Trace.Records.end(),
884 std::back_inserter(Records), [&](const YAMLXRayRecord &R) {
Dean Michael Berris10141262018-07-13 05:38:22 +0000885 return XRayRecord{R.RecordType, R.CPU, R.Type, R.FuncId,
886 R.TSC, R.TId, R.PId, R.CallArgs};
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000887 });
888 return Error::success();
889}
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000890} // namespace
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000891
892Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
893 int Fd;
894 if (auto EC = sys::fs::openFileForRead(Filename, Fd)) {
895 return make_error<StringError>(
896 Twine("Cannot read log from '") + Filename + "'", EC);
897 }
898
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000899 uint64_t FileSize;
900 if (auto EC = sys::fs::file_size(Filename, FileSize)) {
901 return make_error<StringError>(
902 Twine("Cannot read log from '") + Filename + "'", EC);
903 }
904 if (FileSize < 4) {
905 return make_error<StringError>(
906 Twine("File '") + Filename + "' too small for XRay.",
Hans Wennborg84da6612017-01-12 18:33:14 +0000907 std::make_error_code(std::errc::executable_format_error));
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000908 }
909
Martin Pelikand78db152017-09-15 04:22:16 +0000910 // Map the opened file into memory and use a StringRef to access it later.
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000911 std::error_code EC;
912 sys::fs::mapped_file_region MappedFile(
913 Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
914 if (EC) {
915 return make_error<StringError>(
916 Twine("Cannot read log from '") + Filename + "'", EC);
917 }
Martin Pelikand78db152017-09-15 04:22:16 +0000918 auto Data = StringRef(MappedFile.data(), MappedFile.size());
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000919
920 // Attempt to detect the file type using file magic. We have a slight bias
921 // towards the binary format, and we do this by making sure that the first 4
922 // bytes of the binary file is some combination of the following byte
Martin Pelikand78db152017-09-15 04:22:16 +0000923 // patterns: (observe the code loading them assumes they're little endian)
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000924 //
Martin Pelikand78db152017-09-15 04:22:16 +0000925 // 0x01 0x00 0x00 0x00 - version 1, "naive" format
926 // 0x01 0x00 0x01 0x00 - version 1, "flight data recorder" format
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000927 // 0x02 0x00 0x01 0x00 - version 2, "flight data recorder" format
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000928 //
Martin Pelikand78db152017-09-15 04:22:16 +0000929 // YAML files don't typically have those first four bytes as valid text so we
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000930 // try loading assuming YAML if we don't find these bytes.
931 //
932 // Only if we can't load either the binary or the YAML format will we yield an
933 // error.
934 StringRef Magic(MappedFile.data(), 4);
935 DataExtractor HeaderExtractor(Magic, true, 8);
936 uint32_t OffsetPtr = 0;
937 uint16_t Version = HeaderExtractor.getU16(&OffsetPtr);
938 uint16_t Type = HeaderExtractor.getU16(&OffsetPtr);
939
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000940 enum BinaryFormatType { NAIVE_FORMAT = 0, FLIGHT_DATA_RECORDER_FORMAT = 1 };
941
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000942 Trace T;
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000943 switch (Type) {
944 case NAIVE_FORMAT:
Dean Michael Berris10141262018-07-13 05:38:22 +0000945 if (Version == 1 || Version == 2 || Version == 3) {
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000946 if (auto E = loadNaiveFormatLog(Data, T.FileHeader, T.Records))
947 return std::move(E);
948 } else {
949 return make_error<StringError>(
950 Twine("Unsupported version for Basic/Naive Mode logging: ") +
951 Twine(Version),
952 std::make_error_code(std::errc::executable_format_error));
953 }
954 break;
955 case FLIGHT_DATA_RECORDER_FORMAT:
Dean Michael Berris10141262018-07-13 05:38:22 +0000956 if (Version == 1 || Version == 2 || Version == 3) {
Dean Michael Berris6ec72622017-11-21 07:16:57 +0000957 if (auto E = loadFDRLog(Data, T.FileHeader, T.Records))
958 return std::move(E);
959 } else {
960 return make_error<StringError>(
961 Twine("Unsupported version for FDR Mode logging: ") + Twine(Version),
962 std::make_error_code(std::errc::executable_format_error));
963 }
964 break;
965 default:
Martin Pelikand78db152017-09-15 04:22:16 +0000966 if (auto E = loadYAMLLog(Data, T.FileHeader, T.Records))
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000967 return std::move(E);
968 }
969
970 if (Sort)
Mandeep Singh Grang28f3d5c2017-11-14 18:11:08 +0000971 std::stable_sort(T.Records.begin(), T.Records.end(),
Dean Michael Berrisa9d477a2018-08-07 04:42:39 +0000972 [&](const XRayRecord &L, const XRayRecord &R) {
973 return L.TSC < R.TSC;
974 });
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000975
976 return std::move(T);
977}