LLVM 22.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/StringRef.h"
19#include "llvm/Support/Error.h"
21#include <cstdint>
22#include <optional>
23#include <unordered_map>
24#include <vector>
25
26namespace llvm::xray {
27
28// Forward declare to make a friend.
30
31/// Loads the instrumentation map from |Filename|. This auto-deduces the type of
32/// the instrumentation map.
33LLVM_ABI Expected<InstrumentationMap>
34loadInstrumentationMap(StringRef Filename);
35
36/// Represents an XRay instrumentation sled entry from an object file.
37struct 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///
76public:
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
81private:
82 SledContainer Sleds;
83 FunctionAddressMap FunctionAddresses;
84 FunctionAddressReverseMap FunctionIds;
85
88
89public:
90 /// Provides a raw accessor to the unordered map of function addresses.
91 const FunctionAddressMap &getFunctionAddresses() { return FunctionAddresses; }
92
93 /// Returns an XRay computed function id, provided a function address.
94 LLVM_ABI std::optional<int32_t> getFunctionId(uint64_t Addr) const;
95
96 /// Returns the function address for a function id.
97 LLVM_ABI std::optional<uint64_t> getFunctionAddr(int32_t FuncId) const;
98
99 /// Provide read-only access to the entries of the instrumentation map.
100 const SledContainer &sleds() const { return Sleds; };
101};
102
103} // end namespace llvm::xray
104
105namespace llvm {
106template <>
107struct yaml::ScalarEnumerationTraits<xray::SledEntry::FunctionKinds> {
109 IO.enumCase(Kind, "function-enter", xray::SledEntry::FunctionKinds::ENTRY);
110 IO.enumCase(Kind, "function-exit", xray::SledEntry::FunctionKinds::EXIT);
112 IO.enumCase(Kind, "log-args-enter",
114 IO.enumCase(Kind, "custom-event",
116 }
117};
118
119template <> struct yaml::MappingTraits<xray::YAMLXRaySledEntry> {
120 static void mapping(IO &IO, xray::YAMLXRaySledEntry &Entry) {
121 IO.mapRequired("id", Entry.FuncId);
122 IO.mapRequired("address", Entry.Address);
123 IO.mapRequired("function", Entry.Function);
124 IO.mapRequired("kind", Entry.Kind);
125 IO.mapRequired("always-instrument", Entry.AlwaysInstrument);
126 IO.mapOptional("function-name", Entry.FunctionName);
127 IO.mapOptional("version", Entry.Version, 0);
128 }
129
130 static constexpr bool flow = true;
131};
132} // namespace llvm
133
135
136#endif // LLVM_XRAY_INSTRUMENTATIONMAP_H
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_YAML_IS_SEQUENCE_VECTOR(type)
Utility for declaring that a std::vector of a particular type should be considered a YAML sequence.
Tagged union holding either a T or a Error.
Definition Error.h:485
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
The InstrumentationMap represents the computed function id's and indicated function addresses from an...
std::unordered_map< int32_t, uint64_t > FunctionAddressMap
std::unordered_map< uint64_t, int32_t > FunctionAddressReverseMap
LLVM_ABI friend Expected< InstrumentationMap > loadInstrumentationMap(StringRef)
Loads the instrumentation map from |Filename|.
const SledContainer & sleds() const
Provide read-only access to the entries of the instrumentation map.
std::vector< SledEntry > SledContainer
LLVM_ABI std::optional< int32_t > getFunctionId(uint64_t Addr) const
Returns an XRay computed function id, provided a function address.
const FunctionAddressMap & getFunctionAddresses()
Provides a raw accessor to the unordered map of function addresses.
LLVM_ABI std::optional< uint64_t > getFunctionAddr(int32_t FuncId) const
Returns the function address for a function id.
void enumCase(T &Val, StringRef Str, const T ConstVal)
Definition YAMLTraits.h:734
void mapOptional(StringRef Key, T &Val)
Definition YAMLTraits.h:799
void mapRequired(StringRef Key, T &Val)
Definition YAMLTraits.h:789
LLVM_ABI Expected< InstrumentationMap > loadInstrumentationMap(StringRef Filename)
Loads the instrumentation map from |Filename|.
This is an optimization pass for GlobalISel generic memory operations.
Represents an XRay instrumentation sled entry from an object file.
uint64_t Address
The address of the sled.
uint64_t Function
The address of the function.
FunctionKinds
Each entry here represents the kinds of supported instrumentation map entries.
bool AlwaysInstrument
Whether the sled was annotated to always be instrumented.
FunctionKinds Kind
The kind of sled.
SledEntry::FunctionKinds Kind
static void mapping(IO &IO, xray::YAMLXRaySledEntry &Entry)
This class should be specialized by any type that needs to be converted to/from a YAML mapping.
Definition YAMLTraits.h:62
static void enumeration(IO &IO, xray::SledEntry::FunctionKinds &Kind)
This class should be specialized by any integral type that converts to/from a YAML scalar where there...
Definition YAMLTraits.h:107