FileHeaderReader.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. //===----------------------------------------------------------------------===//
  8. #include "llvm/XRay/FileHeaderReader.h"
  9. namespace llvm {
  10. namespace xray {
  11. // Populates the FileHeader reference by reading the first 32 bytes of the file.
  12. Expected<XRayFileHeader> readBinaryFormatHeader(DataExtractor &HeaderExtractor,
  13. uint64_t &OffsetPtr) {
  14. // FIXME: Maybe deduce whether the data is little or big-endian using some
  15. // magic bytes in the beginning of the file?
  16. // First 32 bytes of the file will always be the header. We assume a certain
  17. // format here:
  18. //
  19. // (2) uint16 : version
  20. // (2) uint16 : type
  21. // (4) uint32 : bitfield
  22. // (8) uint64 : cycle frequency
  23. // (16) - : padding
  24. XRayFileHeader FileHeader;
  25. auto PreReadOffset = OffsetPtr;
  26. FileHeader.Version = HeaderExtractor.getU16(&OffsetPtr);
  27. if (OffsetPtr == PreReadOffset)
  28. return createStringError(
  29. std::make_error_code(std::errc::invalid_argument),
  30. "Failed reading version from file header at offset %" PRId64 ".",
  31. OffsetPtr);
  32. PreReadOffset = OffsetPtr;
  33. FileHeader.Type = HeaderExtractor.getU16(&OffsetPtr);
  34. if (OffsetPtr == PreReadOffset)
  35. return createStringError(
  36. std::make_error_code(std::errc::invalid_argument),
  37. "Failed reading file type from file header at offset %" PRId64 ".",
  38. OffsetPtr);
  39. PreReadOffset = OffsetPtr;
  40. uint32_t Bitfield = HeaderExtractor.getU32(&OffsetPtr);
  41. if (OffsetPtr == PreReadOffset)
  42. return createStringError(
  43. std::make_error_code(std::errc::invalid_argument),
  44. "Failed reading flag bits from file header at offset %" PRId64 ".",
  45. OffsetPtr);
  46. FileHeader.ConstantTSC = Bitfield & 1uL;
  47. FileHeader.NonstopTSC = Bitfield & 1uL << 1;
  48. PreReadOffset = OffsetPtr;
  49. FileHeader.CycleFrequency = HeaderExtractor.getU64(&OffsetPtr);
  50. if (OffsetPtr == PreReadOffset)
  51. return createStringError(
  52. std::make_error_code(std::errc::invalid_argument),
  53. "Failed reading cycle frequency from file header at offset %" PRId64
  54. ".",
  55. OffsetPtr);
  56. std::memcpy(&FileHeader.FreeFormData,
  57. HeaderExtractor.getData().bytes_begin() + OffsetPtr, 16);
  58. // Manually advance the offset pointer 16 bytes, after getting a raw memcpy
  59. // from the underlying data.
  60. OffsetPtr += 16;
  61. return std::move(FileHeader);
  62. }
  63. } // namespace xray
  64. } // namespace llvm