LLVM 22.0.0git
FileHeaderReader.cpp
Go to the documentation of this file.
1//===- FileHeaderReader.cpp - XRay File Header Reader --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
9
10using namespace llvm;
11using namespace llvm::xray;
12
13// Populates the FileHeader reference by reading the first 32 bytes of the file.
16 uint64_t &OffsetPtr) {
17 // FIXME: Maybe deduce whether the data is little or big-endian using some
18 // magic bytes in the beginning of the file?
19
20 // First 32 bytes of the file will always be the header. We assume a certain
21 // format here:
22 //
23 // (2) uint16 : version
24 // (2) uint16 : type
25 // (4) uint32 : bitfield
26 // (8) uint64 : cycle frequency
27 // (16) - : padding
28 XRayFileHeader FileHeader;
29 auto PreReadOffset = OffsetPtr;
30 FileHeader.Version = HeaderExtractor.getU16(&OffsetPtr);
31 if (OffsetPtr == PreReadOffset)
32 return createStringError(
33 std::make_error_code(std::errc::invalid_argument),
34 "Failed reading version from file header at offset %" PRId64 ".",
35 OffsetPtr);
36
37 PreReadOffset = OffsetPtr;
38 FileHeader.Type = HeaderExtractor.getU16(&OffsetPtr);
39 if (OffsetPtr == PreReadOffset)
40 return createStringError(
41 std::make_error_code(std::errc::invalid_argument),
42 "Failed reading file type from file header at offset %" PRId64 ".",
43 OffsetPtr);
44
45 PreReadOffset = OffsetPtr;
46 uint32_t Bitfield = HeaderExtractor.getU32(&OffsetPtr);
47 if (OffsetPtr == PreReadOffset)
48 return createStringError(
49 std::make_error_code(std::errc::invalid_argument),
50 "Failed reading flag bits from file header at offset %" PRId64 ".",
51 OffsetPtr);
52
53 FileHeader.ConstantTSC = Bitfield & 1uL;
54 FileHeader.NonstopTSC = Bitfield & 1uL << 1;
55 PreReadOffset = OffsetPtr;
56 FileHeader.CycleFrequency = HeaderExtractor.getU64(&OffsetPtr);
57 if (OffsetPtr == PreReadOffset)
58 return createStringError(
59 std::make_error_code(std::errc::invalid_argument),
60 "Failed reading cycle frequency from file header at offset %" PRId64
61 ".",
62 OffsetPtr);
63
64 std::memcpy(&FileHeader.FreeFormData,
65 HeaderExtractor.getData().bytes_begin() + OffsetPtr, 16);
66
67 // Manually advance the offset pointer 16 bytes, after getting a raw memcpy
68 // from the underlying data.
69 OffsetPtr += 16;
70 return std::move(FileHeader);
71}
LLVM_ABI uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
StringRef getData() const
Get the data pointed to by this extractor.
LLVM_ABI uint16_t getU16(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint16_t value from *offset_ptr.
LLVM_ABI uint64_t getU64(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint64_t value from *offset_ptr.
Tagged union holding either a T or a Error.
Definition Error.h:485
const unsigned char * bytes_begin() const
Definition StringRef.h:124
LLVM_ABI Expected< XRayFileHeader > readBinaryFormatHeader(DataExtractor &HeaderExtractor, uint64_t &OffsetPtr)
Convenience function for loading the file header given a data extractor at a specified offset.
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1305
Holds functions to get, set or test bitfields.
Definition Bitfields.h:168
XRay traces all have a header providing some top-matter information useful to help tools determine ho...
Definition XRayRecord.h:26
bool ConstantTSC
Whether the CPU that produced the timestamp counters (TSC) move at a constant rate.
Definition XRayRecord.h:36
uint16_t Type
A numeric identifier for the type of file this is.
Definition XRayRecord.h:32
uint64_t CycleFrequency
The number of cycles per second for the CPU that produced the timestamp counter (TSC) values.
Definition XRayRecord.h:44
bool NonstopTSC
Whether the CPU that produced the timestamp counters (TSC) do not stop.
Definition XRayRecord.h:39
uint16_t Version
Version of the XRay implementation that produced this file.
Definition XRayRecord.h:28