LLVM  8.0.1
FileHeaderReader.cpp
Go to the documentation of this file.
1 //===- FileHeaderReader.cpp - XRay File Header Reader --------------------===//
2 //
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 //===----------------------------------------------------------------------===//
10 
11 namespace llvm {
12 namespace xray {
13 
14 // Populates the FileHeader reference by reading the first 32 bytes of the file.
16  uint32_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 %d.", OffsetPtr);
35 
36  PreReadOffset = OffsetPtr;
37  FileHeader.Type = HeaderExtractor.getU16(&OffsetPtr);
38  if (OffsetPtr == PreReadOffset)
39  return createStringError(
40  std::make_error_code(std::errc::invalid_argument),
41  "Failed reading file type from file header at offset %d.", OffsetPtr);
42 
43  PreReadOffset = OffsetPtr;
44  uint32_t Bitfield = HeaderExtractor.getU32(&OffsetPtr);
45  if (OffsetPtr == PreReadOffset)
46  return createStringError(
47  std::make_error_code(std::errc::invalid_argument),
48  "Failed reading flag bits from file header at offset %d.", OffsetPtr);
49 
50  FileHeader.ConstantTSC = Bitfield & 1uL;
51  FileHeader.NonstopTSC = Bitfield & 1uL << 1;
52  PreReadOffset = OffsetPtr;
53  FileHeader.CycleFrequency = HeaderExtractor.getU64(&OffsetPtr);
54  if (OffsetPtr == PreReadOffset)
55  return createStringError(
56  std::make_error_code(std::errc::invalid_argument),
57  "Failed reading cycle frequency from file header at offset %d.",
58  OffsetPtr);
59 
60  std::memcpy(&FileHeader.FreeFormData,
61  HeaderExtractor.getData().bytes_begin() + OffsetPtr, 16);
62 
63  // Manually advance the offset pointer 16 bytes, after getting a raw memcpy
64  // from the underlying data.
65  OffsetPtr += 16;
66  return std::move(FileHeader);
67 }
68 
69 } // namespace xray
70 } // namespace llvm
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Expected< XRayFileHeader > readBinaryFormatHeader(DataExtractor &HeaderExtractor, uint32_t &OffsetPtr)
Convenience function for loading the file header given a data extractor at a specified offset...
StringRef getData() const
Get the data pointed to by this extractor.
Definition: DataExtractor.h:55
uint16_t getU16(uint32_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
uint32_t getU32(uint32_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
std::error_code make_error_code(BitcodeError E)
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
uint64_t CycleFrequency
The number of cycles per second for the CPU that produced the timestamp counter (TSC) values...
Definition: XRayRecord.h:46
uint16_t Type
A numeric identifier for the type of file this is.
Definition: XRayRecord.h:34
bool NonstopTSC
Whether the CPU that produced the timestamp counters (TSC) do not stop.
Definition: XRayRecord.h:41
uint64_t getU64(uint32_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
const unsigned char * bytes_begin() const
Definition: StringRef.h:110
bool ConstantTSC
Whether the CPU that produced the timestamp counters (TSC) move at a constant rate.
Definition: XRayRecord.h:38
XRay traces all have a header providing some top-matter information useful to help tools determine ho...
Definition: XRayRecord.h:28
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1164
uint16_t Version
Version of the XRay implementation that produced this file.
Definition: XRayRecord.h:30