//===- Trace.cpp - XRay Trace Loading implementation. ---------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // XRay log reader implementation. // //===----------------------------------------------------------------------===// #include "llvm/XRay/Trace.h" #include "llvm/ADT/STLExtras.h" #include "llvm/Support/DataExtractor.h" #include "llvm/Support/Error.h" #include "llvm/Support/FileSystem.h" #include "llvm/XRay/BlockIndexer.h" #include "llvm/XRay/BlockVerifier.h" #include "llvm/XRay/FDRRecordConsumer.h" #include "llvm/XRay/FDRRecordProducer.h" #include "llvm/XRay/FDRRecords.h" #include "llvm/XRay/FDRTraceExpander.h" #include "llvm/XRay/FileHeaderReader.h" #include "llvm/XRay/YAMLXRayRecord.h" #include <memory> #include <vector> usingnamespacellvm; usingnamespacellvm::xray; Input; namespace { Error loadNaiveFormatLog(StringRef Data, bool IsLittleEndian, XRayFileHeader &FileHeader, std::vector<XRayRecord> &Records) { … } /// Reads a log in FDR mode for version 1 of this binary format. FDR mode is /// defined as part of the compiler-rt project in xray_fdr_logging.h, and such /// a log consists of the familiar 32 bit XRayHeader, followed by sequences of /// of interspersed 16 byte Metadata Records and 8 byte Function Records. /// /// The following is an attempt to document the grammar of the format, which is /// parsed by this function for little-endian machines. Since the format makes /// use of BitFields, when we support big-endian architectures, we will need to /// adjust not only the endianness parameter to llvm's RecordExtractor, but also /// the bit twiddling logic, which is consistent with the little-endian /// convention that BitFields within a struct will first be packed into the /// least significant bits the address they belong to. /// /// We expect a format complying with the grammar in the following pseudo-EBNF /// in Version 1 of the FDR log. /// /// FDRLog: XRayFileHeader ThreadBuffer* /// XRayFileHeader: 32 bytes to identify the log as FDR with machine metadata. /// Includes BufferSize /// ThreadBuffer: NewBuffer WallClockTime NewCPUId FunctionSequence EOB /// BufSize: 8 byte unsigned integer indicating how large the buffer is. /// NewBuffer: 16 byte metadata record with Thread Id. /// WallClockTime: 16 byte metadata record with human readable time. /// Pid: 16 byte metadata record with Pid /// NewCPUId: 16 byte metadata record with CPUId and a 64 bit TSC reading. /// EOB: 16 byte record in a thread buffer plus mem garbage to fill BufSize. /// FunctionSequence: NewCPUId | TSCWrap | FunctionRecord /// TSCWrap: 16 byte metadata record with a full 64 bit TSC reading. /// FunctionRecord: 8 byte record with FunctionId, entry/exit, and TSC delta. /// /// In Version 2, we make the following changes: /// /// ThreadBuffer: BufferExtents NewBuffer WallClockTime NewCPUId /// FunctionSequence /// BufferExtents: 16 byte metdata record describing how many usable bytes are /// in the buffer. This is measured from the start of the buffer /// and must always be at least 48 (bytes). /// /// In Version 3, we make the following changes: /// /// ThreadBuffer: BufferExtents NewBuffer WallClockTime Pid NewCPUId /// FunctionSequence /// EOB: *deprecated* /// /// In Version 4, we make the following changes: /// /// CustomEventRecord now includes the CPU data. /// /// In Version 5, we make the following changes: /// /// CustomEventRecord and TypedEventRecord now use TSC delta encoding similar to /// what FunctionRecord instances use, and we no longer need to include the CPU /// id in the CustomEventRecord. /// Error loadFDRLog(StringRef Data, bool IsLittleEndian, XRayFileHeader &FileHeader, std::vector<XRayRecord> &Records) { … } Error loadYAMLLog(StringRef Data, XRayFileHeader &FileHeader, std::vector<XRayRecord> &Records) { … } } // namespace Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) { … } Expected<Trace> llvm::xray::loadTrace(const DataExtractor &DE, bool Sort) { … }