LLVM  14.0.0git
InstrumentationMap.h
Go to the documentation of this file.
1 //===- InstrumentationMap.h - XRay Instrumentation Map ----------*- C++ -*-===//
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 //
9 // Defines the interface for extracting the instrumentation map from an
10 // XRay-instrumented binary.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_XRAY_INSTRUMENTATIONMAP_H
15 #define LLVM_XRAY_INSTRUMENTATIONMAP_H
16 
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/Error.h"
21 #include <cstdint>
22 #include <unordered_map>
23 #include <vector>
24 
25 namespace llvm {
26 
27 namespace xray {
28 
29 // Forward declare to make a friend.
30 class InstrumentationMap;
31 
32 /// Loads the instrumentation map from |Filename|. This auto-deduces the type of
33 /// the instrumentation map.
34 Expected<InstrumentationMap> loadInstrumentationMap(StringRef Filename);
35 
36 /// Represents an XRay instrumentation sled entry from an object file.
37 struct SledEntry {
38  /// Each entry here represents the kinds of supported instrumentation map
39  /// entries.
41 
42  /// The address of the sled.
44 
45  /// The address of the function.
47 
48  /// The kind of sled.
50 
51  /// Whether the sled was annotated to always be instrumented.
53 
54  unsigned char Version;
55 };
56 
58  int32_t FuncId;
59  yaml::Hex64 Address;
60  yaml::Hex64 Function;
63  std::string FunctionName;
64  unsigned char Version;
65 };
66 
67 /// The InstrumentationMap represents the computed function id's and indicated
68 /// function addresses from an object file (or a YAML file). This provides an
69 /// interface to just the mapping between the function id, and the function
70 /// address.
71 ///
72 /// We also provide raw access to the actual instrumentation map entries we find
73 /// associated with a particular object file.
74 ///
76 public:
77  using FunctionAddressMap = std::unordered_map<int32_t, uint64_t>;
78  using FunctionAddressReverseMap = std::unordered_map<uint64_t, int32_t>;
79  using SledContainer = std::vector<SledEntry>;
80 
81 private:
82  SledContainer Sleds;
83  FunctionAddressMap FunctionAddresses;
84  FunctionAddressReverseMap FunctionIds;
85 
87 
88 public:
89  /// Provides a raw accessor to the unordered map of function addresses.
90  const FunctionAddressMap &getFunctionAddresses() { return FunctionAddresses; }
91 
92  /// Returns an XRay computed function id, provided a function address.
94 
95  /// Returns the function address for a function id.
97 
98  /// Provide read-only access to the entries of the instrumentation map.
99  const SledContainer &sleds() const { return Sleds; };
100 };
101 
102 } // end namespace xray
103 
104 namespace yaml {
105 
106 template <> struct ScalarEnumerationTraits<xray::SledEntry::FunctionKinds> {
108  IO.enumCase(Kind, "function-enter", xray::SledEntry::FunctionKinds::ENTRY);
109  IO.enumCase(Kind, "function-exit", xray::SledEntry::FunctionKinds::EXIT);
110  IO.enumCase(Kind, "tail-exit", xray::SledEntry::FunctionKinds::TAIL);
111  IO.enumCase(Kind, "log-args-enter",
113  IO.enumCase(Kind, "custom-event",
115  }
116 };
117 
118 template <> struct MappingTraits<xray::YAMLXRaySledEntry> {
119  static void mapping(IO &IO, xray::YAMLXRaySledEntry &Entry) {
120  IO.mapRequired("id", Entry.FuncId);
121  IO.mapRequired("address", Entry.Address);
122  IO.mapRequired("function", Entry.Function);
123  IO.mapRequired("kind", Entry.Kind);
124  IO.mapRequired("always-instrument", Entry.AlwaysInstrument);
125  IO.mapOptional("function-name", Entry.FunctionName);
126  IO.mapOptional("version", Entry.Version, 0);
127  }
128 
129  static constexpr bool flow = true;
130 };
131 
132 } // end namespace yaml
133 
134 } // end namespace llvm
135 
136 LLVM_YAML_IS_SEQUENCE_VECTOR(xray::YAMLXRaySledEntry)
137 
138 #endif // LLVM_XRAY_INSTRUMENTATIONMAP_H
llvm::xray::YAMLXRaySledEntry::Version
unsigned char Version
Definition: InstrumentationMap.h:64
llvm::xray::YAMLXRaySledEntry::AlwaysInstrument
bool AlwaysInstrument
Definition: InstrumentationMap.h:62
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
Optional.h
llvm::xray::InstrumentationMap::getFunctionAddr
Optional< uint64_t > getFunctionAddr(int32_t FuncId) const
Returns the function address for a function id.
Definition: InstrumentationMap.cpp:44
llvm::xray::InstrumentationMap
The InstrumentationMap represents the computed function id's and indicated function addresses from an...
Definition: InstrumentationMap.h:75
llvm::yaml::MappingTraits< xray::YAMLXRaySledEntry >::mapping
static void mapping(IO &IO, xray::YAMLXRaySledEntry &Entry)
Definition: InstrumentationMap.h:119
StringRef.h
Error.h
LLVM_YAML_IS_SEQUENCE_VECTOR
#define LLVM_YAML_IS_SEQUENCE_VECTOR(type)
llvm::xray::InstrumentationMap::loadInstrumentationMap
friend Expected< InstrumentationMap > loadInstrumentationMap(StringRef)
Loads the instrumentation map from |Filename|.
llvm::xray::SledEntry::Kind
FunctionKinds Kind
The kind of sled.
Definition: InstrumentationMap.h:49
llvm::Optional< int32_t >
llvm::xray::SledEntry::FunctionKinds
FunctionKinds
Each entry here represents the kinds of supported instrumentation map entries.
Definition: InstrumentationMap.h:40
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::xray::YAMLXRaySledEntry::FunctionName
std::string FunctionName
Definition: InstrumentationMap.h:63
llvm::yaml::MappingTraits
Definition: ModuleSummaryIndex.h:52
llvm::xray::InstrumentationMap::getFunctionId
Optional< int32_t > getFunctionId(uint64_t Addr) const
Returns an XRay computed function id, provided a function address.
Definition: InstrumentationMap.cpp:37
llvm::xray::loadInstrumentationMap
Expected< InstrumentationMap > loadInstrumentationMap(StringRef Filename)
Loads the instrumentation map from |Filename|.
Definition: InstrumentationMap.cpp:254
llvm::xray::SledEntry::FunctionKinds::EXIT
@ EXIT
llvm::xray::SledEntry::Address
uint64_t Address
The address of the sled.
Definition: InstrumentationMap.h:43
llvm::xray::YAMLXRaySledEntry::Address
yaml::Hex64 Address
Definition: InstrumentationMap.h:59
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::xray::SledEntry::AlwaysInstrument
bool AlwaysInstrument
Whether the sled was annotated to always be instrumented.
Definition: InstrumentationMap.h:52
llvm::xray::InstrumentationMap::SledContainer
std::vector< SledEntry > SledContainer
Definition: InstrumentationMap.h:79
uint64_t
Addr
uint64_t Addr
Definition: ELFObjHandler.cpp:79
llvm::yaml::ScalarEnumerationTraits< xray::SledEntry::FunctionKinds >::enumeration
static void enumeration(IO &IO, xray::SledEntry::FunctionKinds &Kind)
Definition: InstrumentationMap.h:107
llvm::xray::SledEntry::Version
unsigned char Version
Definition: InstrumentationMap.h:54
llvm::xray::SledEntry::FunctionKinds::CUSTOM_EVENT
@ CUSTOM_EVENT
FuncId
Profile::FuncID FuncId
Definition: Profile.cpp:321
llvm::xray::InstrumentationMap::sleds
const SledContainer & sleds() const
Provide read-only access to the entries of the instrumentation map.
Definition: InstrumentationMap.h:99
llvm::xray::YAMLXRaySledEntry
Definition: InstrumentationMap.h:57
YAMLTraits.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::xray::SledEntry::FunctionKinds::ENTRY
@ ENTRY
llvm::xray::SledEntry::FunctionKinds::TAIL
@ TAIL
llvm::xray::YAMLXRaySledEntry::Kind
SledEntry::FunctionKinds Kind
Definition: InstrumentationMap.h:61
llvm::xray::SledEntry::FunctionKinds::LOG_ARGS_ENTER
@ LOG_ARGS_ENTER
llvm::xray::SledEntry
Represents an XRay instrumentation sled entry from an object file.
Definition: InstrumentationMap.h:37
llvm::xray::SledEntry::Function
uint64_t Function
The address of the function.
Definition: InstrumentationMap.h:46
llvm::xray::InstrumentationMap::getFunctionAddresses
const FunctionAddressMap & getFunctionAddresses()
Provides a raw accessor to the unordered map of function addresses.
Definition: InstrumentationMap.h:90
llvm::xray::YAMLXRaySledEntry::FuncId
int32_t FuncId
Definition: InstrumentationMap.h:58
llvm::xray::InstrumentationMap::FunctionAddressMap
std::unordered_map< int32_t, uint64_t > FunctionAddressMap
Definition: InstrumentationMap.h:77
llvm::xray::InstrumentationMap::FunctionAddressReverseMap
std::unordered_map< uint64_t, int32_t > FunctionAddressReverseMap
Definition: InstrumentationMap.h:78
llvm::xray::YAMLXRaySledEntry::Function
yaml::Hex64 Function
Definition: InstrumentationMap.h:60