FaultMapParser.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- FaultMapParser.h - Parser for the "FaultMaps" section --*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_OBJECT_FAULTMAPPARSER_H
  14. #define LLVM_OBJECT_FAULTMAPPARSER_H
  15. #include "llvm/Support/Endian.h"
  16. #include <cassert>
  17. #include <cstdint>
  18. namespace llvm {
  19. class raw_ostream;
  20. /// A parser for the __llvm_faultmaps section generated by the FaultMaps class
  21. /// declared in llvm/CodeGen/FaultMaps.h. This parser is version locked with
  22. /// with the __llvm_faultmaps section generated by the version of LLVM that
  23. /// includes it. No guarantees are made with respect to forward or backward
  24. /// compatibility.
  25. class FaultMapParser {
  26. using FaultMapVersionType = uint8_t;
  27. using Reserved0Type = uint8_t;
  28. using Reserved1Type = uint16_t;
  29. using NumFunctionsType = uint32_t;
  30. static const size_t FaultMapVersionOffset = 0;
  31. static const size_t Reserved0Offset =
  32. FaultMapVersionOffset + sizeof(FaultMapVersionType);
  33. static const size_t Reserved1Offset = Reserved0Offset + sizeof(Reserved0Type);
  34. static const size_t NumFunctionsOffset =
  35. Reserved1Offset + sizeof(Reserved1Type);
  36. static const size_t FunctionInfosOffset =
  37. NumFunctionsOffset + sizeof(NumFunctionsType);
  38. const uint8_t *P;
  39. const uint8_t *E;
  40. template <typename T> static T read(const uint8_t *P, const uint8_t *E) {
  41. assert(P + sizeof(T) <= E && "out of bounds read!");
  42. return support::endian::read<T, support::little, 1>(P);
  43. }
  44. public:
  45. enum FaultKind {
  46. FaultingLoad = 1,
  47. FaultingLoadStore,
  48. FaultingStore,
  49. FaultKindMax
  50. };
  51. class FunctionFaultInfoAccessor {
  52. using FaultKindType = uint32_t;
  53. using FaultingPCOffsetType = uint32_t;
  54. using HandlerPCOffsetType = uint32_t;
  55. static const size_t FaultKindOffset = 0;
  56. static const size_t FaultingPCOffsetOffset =
  57. FaultKindOffset + sizeof(FaultKindType);
  58. static const size_t HandlerPCOffsetOffset =
  59. FaultingPCOffsetOffset + sizeof(FaultingPCOffsetType);
  60. const uint8_t *P;
  61. const uint8_t *E;
  62. public:
  63. static const size_t Size =
  64. HandlerPCOffsetOffset + sizeof(HandlerPCOffsetType);
  65. explicit FunctionFaultInfoAccessor(const uint8_t *P, const uint8_t *E)
  66. : P(P), E(E) {}
  67. FaultKindType getFaultKind() const {
  68. return read<FaultKindType>(P + FaultKindOffset, E);
  69. }
  70. FaultingPCOffsetType getFaultingPCOffset() const {
  71. return read<FaultingPCOffsetType>(P + FaultingPCOffsetOffset, E);
  72. }
  73. HandlerPCOffsetType getHandlerPCOffset() const {
  74. return read<HandlerPCOffsetType>(P + HandlerPCOffsetOffset, E);
  75. }
  76. };
  77. class FunctionInfoAccessor {
  78. using FunctionAddrType = uint64_t;
  79. using NumFaultingPCsType = uint32_t;
  80. using ReservedType = uint32_t;
  81. static const size_t FunctionAddrOffset = 0;
  82. static const size_t NumFaultingPCsOffset =
  83. FunctionAddrOffset + sizeof(FunctionAddrType);
  84. static const size_t ReservedOffset =
  85. NumFaultingPCsOffset + sizeof(NumFaultingPCsType);
  86. static const size_t FunctionFaultInfosOffset =
  87. ReservedOffset + sizeof(ReservedType);
  88. static const size_t FunctionInfoHeaderSize = FunctionFaultInfosOffset;
  89. const uint8_t *P = nullptr;
  90. const uint8_t *E = nullptr;
  91. public:
  92. FunctionInfoAccessor() = default;
  93. explicit FunctionInfoAccessor(const uint8_t *P, const uint8_t *E)
  94. : P(P), E(E) {}
  95. FunctionAddrType getFunctionAddr() const {
  96. return read<FunctionAddrType>(P + FunctionAddrOffset, E);
  97. }
  98. NumFaultingPCsType getNumFaultingPCs() const {
  99. return read<NumFaultingPCsType>(P + NumFaultingPCsOffset, E);
  100. }
  101. FunctionFaultInfoAccessor getFunctionFaultInfoAt(uint32_t Index) const {
  102. assert(Index < getNumFaultingPCs() && "index out of bounds!");
  103. const uint8_t *Begin = P + FunctionFaultInfosOffset +
  104. FunctionFaultInfoAccessor::Size * Index;
  105. return FunctionFaultInfoAccessor(Begin, E);
  106. }
  107. FunctionInfoAccessor getNextFunctionInfo() const {
  108. size_t MySize = FunctionInfoHeaderSize +
  109. getNumFaultingPCs() * FunctionFaultInfoAccessor::Size;
  110. const uint8_t *Begin = P + MySize;
  111. assert(Begin < E && "out of bounds!");
  112. return FunctionInfoAccessor(Begin, E);
  113. }
  114. };
  115. explicit FaultMapParser(const uint8_t *Begin, const uint8_t *End)
  116. : P(Begin), E(End) {}
  117. FaultMapVersionType getFaultMapVersion() const {
  118. auto Version = read<FaultMapVersionType>(P + FaultMapVersionOffset, E);
  119. assert(Version == 1 && "only version 1 supported!");
  120. return Version;
  121. }
  122. NumFunctionsType getNumFunctions() const {
  123. return read<NumFunctionsType>(P + NumFunctionsOffset, E);
  124. }
  125. FunctionInfoAccessor getFirstFunctionInfo() const {
  126. const uint8_t *Begin = P + FunctionInfosOffset;
  127. return FunctionInfoAccessor(Begin, E);
  128. }
  129. };
  130. raw_ostream &operator<<(raw_ostream &OS,
  131. const FaultMapParser::FunctionFaultInfoAccessor &);
  132. raw_ostream &operator<<(raw_ostream &OS,
  133. const FaultMapParser::FunctionInfoAccessor &);
  134. raw_ostream &operator<<(raw_ostream &OS, const FaultMapParser &);
  135. } // namespace llvm
  136. #endif
  137. #ifdef __GNUC__
  138. #pragma GCC diagnostic pop
  139. #endif