InstrumentationMap.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- InstrumentationMap.h - XRay Instrumentation Map ----------*- 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. //
  14. // Defines the interface for extracting the instrumentation map from an
  15. // XRay-instrumented binary.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_XRAY_INSTRUMENTATION_MAP_H
  19. #define LLVM_XRAY_INSTRUMENTATION_MAP_H
  20. #include "llvm/ADT/Optional.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/Support/Error.h"
  23. #include "llvm/Support/YAMLTraits.h"
  24. #include <cstdint>
  25. #include <unordered_map>
  26. #include <vector>
  27. namespace llvm {
  28. namespace xray {
  29. // Forward declare to make a friend.
  30. class InstrumentationMap;
  31. /// Loads the instrumentation map from |Filename|. This auto-deduces the type of
  32. /// the instrumentation map.
  33. Expected<InstrumentationMap> loadInstrumentationMap(StringRef Filename);
  34. /// Represents an XRay instrumentation sled entry from an object file.
  35. struct SledEntry {
  36. /// Each entry here represents the kinds of supported instrumentation map
  37. /// entries.
  38. enum class FunctionKinds { ENTRY, EXIT, TAIL, LOG_ARGS_ENTER, CUSTOM_EVENT };
  39. /// The address of the sled.
  40. uint64_t Address;
  41. /// The address of the function.
  42. uint64_t Function;
  43. /// The kind of sled.
  44. FunctionKinds Kind;
  45. /// Whether the sled was annotated to always be instrumented.
  46. bool AlwaysInstrument;
  47. unsigned char Version;
  48. };
  49. struct YAMLXRaySledEntry {
  50. int32_t FuncId;
  51. yaml::Hex64 Address;
  52. yaml::Hex64 Function;
  53. SledEntry::FunctionKinds Kind;
  54. bool AlwaysInstrument;
  55. std::string FunctionName;
  56. unsigned char Version;
  57. };
  58. /// The InstrumentationMap represents the computed function id's and indicated
  59. /// function addresses from an object file (or a YAML file). This provides an
  60. /// interface to just the mapping between the function id, and the function
  61. /// address.
  62. ///
  63. /// We also provide raw access to the actual instrumentation map entries we find
  64. /// associated with a particular object file.
  65. ///
  66. class InstrumentationMap {
  67. public:
  68. using FunctionAddressMap = std::unordered_map<int32_t, uint64_t>;
  69. using FunctionAddressReverseMap = std::unordered_map<uint64_t, int32_t>;
  70. using SledContainer = std::vector<SledEntry>;
  71. private:
  72. SledContainer Sleds;
  73. FunctionAddressMap FunctionAddresses;
  74. FunctionAddressReverseMap FunctionIds;
  75. friend Expected<InstrumentationMap> loadInstrumentationMap(StringRef);
  76. public:
  77. /// Provides a raw accessor to the unordered map of function addresses.
  78. const FunctionAddressMap &getFunctionAddresses() { return FunctionAddresses; }
  79. /// Returns an XRay computed function id, provided a function address.
  80. Optional<int32_t> getFunctionId(uint64_t Addr) const;
  81. /// Returns the function address for a function id.
  82. Optional<uint64_t> getFunctionAddr(int32_t FuncId) const;
  83. /// Provide read-only access to the entries of the instrumentation map.
  84. const SledContainer &sleds() const { return Sleds; };
  85. };
  86. } // end namespace xray
  87. namespace yaml {
  88. template <> struct ScalarEnumerationTraits<xray::SledEntry::FunctionKinds> {
  89. static void enumeration(IO &IO, xray::SledEntry::FunctionKinds &Kind) {
  90. IO.enumCase(Kind, "function-enter", xray::SledEntry::FunctionKinds::ENTRY);
  91. IO.enumCase(Kind, "function-exit", xray::SledEntry::FunctionKinds::EXIT);
  92. IO.enumCase(Kind, "tail-exit", xray::SledEntry::FunctionKinds::TAIL);
  93. IO.enumCase(Kind, "log-args-enter",
  94. xray::SledEntry::FunctionKinds::LOG_ARGS_ENTER);
  95. IO.enumCase(Kind, "custom-event",
  96. xray::SledEntry::FunctionKinds::CUSTOM_EVENT);
  97. }
  98. };
  99. template <> struct MappingTraits<xray::YAMLXRaySledEntry> {
  100. static void mapping(IO &IO, xray::YAMLXRaySledEntry &Entry) {
  101. IO.mapRequired("id", Entry.FuncId);
  102. IO.mapRequired("address", Entry.Address);
  103. IO.mapRequired("function", Entry.Function);
  104. IO.mapRequired("kind", Entry.Kind);
  105. IO.mapRequired("always-instrument", Entry.AlwaysInstrument);
  106. IO.mapOptional("function-name", Entry.FunctionName);
  107. IO.mapOptional("version", Entry.Version, 0);
  108. }
  109. static constexpr bool flow = true;
  110. };
  111. } // end namespace yaml
  112. } // end namespace llvm
  113. LLVM_YAML_IS_SEQUENCE_VECTOR(xray::YAMLXRaySledEntry)
  114. #endif // LLVM_XRAY_INSTRUMENTATION_MAP_H
  115. #ifdef __GNUC__
  116. #pragma GCC diagnostic pop
  117. #endif