LLVM  5.0.0
InstrumentationMap.cpp
Go to the documentation of this file.
1 //===- InstrumentationMap.cpp - XRay Instrumentation Map ------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implementation of the InstrumentationMap type for XRay sleds.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/None.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/Object/Binary.h"
21 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Support/Error.h"
26 #include <algorithm>
27 #include <cstddef>
28 #include <cstdint>
29 #include <system_error>
30 #include <vector>
31 
32 using namespace llvm;
33 using namespace xray;
34 
36  auto I = FunctionIds.find(Addr);
37  if (I != FunctionIds.end())
38  return I->second;
39  return None;
40 }
41 
43  auto I = FunctionAddresses.find(FuncId);
44  if (I != FunctionAddresses.end())
45  return I->second;
46  return None;
47 }
48 
49 static Error
52  InstrumentationMap::FunctionAddressMap &FunctionAddresses,
55 
56  // Find the section named "xray_instr_map".
57  if (!ObjFile.getBinary()->isELF() ||
58  !(ObjFile.getBinary()->getArch() == Triple::x86_64 ||
59  ObjFile.getBinary()->getArch() == Triple::ppc64le))
60  return make_error<StringError>(
61  "File format not supported (only does ELF little endian 64-bit).",
62  std::make_error_code(std::errc::not_supported));
63 
64  StringRef Contents = "";
65  const auto &Sections = ObjFile.getBinary()->sections();
66  auto I = llvm::find_if(Sections, [&](object::SectionRef Section) {
67  StringRef Name = "";
68  if (Section.getName(Name))
69  return false;
70  return Name == "xray_instr_map";
71  });
72 
73  if (I == Sections.end())
74  return make_error<StringError>(
75  "Failed to find XRay instrumentation map.",
76  std::make_error_code(std::errc::executable_format_error));
77 
78  if (I->getContents(Contents))
79  return errorCodeToError(
80  std::make_error_code(std::errc::executable_format_error));
81 
82  // Copy the instrumentation map data into the Sleds data structure.
83  auto C = Contents.bytes_begin();
84  static constexpr size_t ELF64SledEntrySize = 32;
85 
86  if ((C - Contents.bytes_end()) % ELF64SledEntrySize != 0)
87  return make_error<StringError>(
88  Twine("Instrumentation map entries not evenly divisible by size of "
89  "an XRay sled entry in ELF64."),
90  std::make_error_code(std::errc::executable_format_error));
91 
92  int32_t FuncId = 1;
93  uint64_t CurFn = 0;
94  for (; C != Contents.bytes_end(); C += ELF64SledEntrySize) {
95  DataExtractor Extractor(
96  StringRef(reinterpret_cast<const char *>(C), ELF64SledEntrySize), true,
97  8);
98  Sleds.push_back({});
99  auto &Entry = Sleds.back();
100  uint32_t OffsetPtr = 0;
101  Entry.Address = Extractor.getU64(&OffsetPtr);
102  Entry.Function = Extractor.getU64(&OffsetPtr);
103  auto Kind = Extractor.getU8(&OffsetPtr);
104  static constexpr SledEntry::FunctionKinds Kinds[] = {
107  };
108  if (Kind >= sizeof(Kinds))
109  return errorCodeToError(
110  std::make_error_code(std::errc::executable_format_error));
111  Entry.Kind = Kinds[Kind];
112  Entry.AlwaysInstrument = Extractor.getU8(&OffsetPtr) != 0;
113 
114  // We do replicate the function id generation scheme implemented in the
115  // XRay runtime.
116  // FIXME: Figure out how to keep this consistent with the XRay runtime.
117  if (CurFn == 0) {
118  CurFn = Entry.Function;
119  FunctionAddresses[FuncId] = Entry.Function;
120  FunctionIds[Entry.Function] = FuncId;
121  }
122  if (Entry.Function != CurFn) {
123  ++FuncId;
124  CurFn = Entry.Function;
125  FunctionAddresses[FuncId] = Entry.Function;
126  FunctionIds[Entry.Function] = FuncId;
127  }
128  }
129  return Error::success();
130 }
131 
132 static Error
133 loadYAML(int Fd, size_t FileSize, StringRef Filename,
135  InstrumentationMap::FunctionAddressMap &FunctionAddresses,
137  std::error_code EC;
138  sys::fs::mapped_file_region MappedFile(
139  Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
140  if (EC)
141  return make_error<StringError>(
142  Twine("Failed memory-mapping file '") + Filename + "'.", EC);
143 
144  std::vector<YAMLXRaySledEntry> YAMLSleds;
145  yaml::Input In(StringRef(MappedFile.data(), MappedFile.size()));
146  In >> YAMLSleds;
147  if (In.error())
148  return make_error<StringError>(
149  Twine("Failed loading YAML document from '") + Filename + "'.",
150  In.error());
151 
152  Sleds.reserve(YAMLSleds.size());
153  for (const auto &Y : YAMLSleds) {
154  FunctionAddresses[Y.FuncId] = Y.Function;
155  FunctionIds[Y.Function] = Y.FuncId;
156  Sleds.push_back(
157  SledEntry{Y.Address, Y.Function, Y.Kind, Y.AlwaysInstrument});
158  }
159  return Error::success();
160 }
161 
162 // FIXME: Create error types that encapsulate a bit more information than what
163 // StringError instances contain.
166  // At this point we assume the file is an object file -- and if that doesn't
167  // work, we treat it as YAML.
168  // FIXME: Extend to support non-ELF and non-x86_64 binaries.
169 
170  InstrumentationMap Map;
171  auto ObjectFileOrError = object::ObjectFile::createObjectFile(Filename);
172  if (!ObjectFileOrError) {
173  auto E = ObjectFileOrError.takeError();
174  // We try to load it as YAML if the ELF load didn't work.
175  int Fd;
176  if (sys::fs::openFileForRead(Filename, Fd))
177  return std::move(E);
178 
179  uint64_t FileSize;
180  if (sys::fs::file_size(Filename, FileSize))
181  return std::move(E);
182 
183  // If the file is empty, we return the original error.
184  if (FileSize == 0)
185  return std::move(E);
186 
187  // From this point on the errors will be only for the YAML parts, so we
188  // consume the errors at this point.
189  consumeError(std::move(E));
190  if (auto E = loadYAML(Fd, FileSize, Filename, Map.Sleds,
191  Map.FunctionAddresses, Map.FunctionIds))
192  return std::move(E);
193  } else if (auto E = loadELF64(Filename, *ObjectFileOrError, Map.Sleds,
194  Map.FunctionAddresses, Map.FunctionIds)) {
195  return std::move(E);
196  }
197  return Map;
198 }
const NoneType None
Definition: None.h:24
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Create ObjectFile from path.
Definition: ObjectFile.cpp:125
bool isELF() const
Definition: Binary.h:108
std::error_code error()
Definition: YAMLTraits.cpp:73
FunctionKinds
Each entry here represents the kinds of supported instrumentation map entries.
std::error_code openFileForRead(const Twine &Name, int &ResultFD, SmallVectorImpl< char > *RealPath=nullptr)
This class represents a memory mapped file.
Definition: FileSystem.h:744
std::unordered_map< uint64_t, int32_t > FunctionAddressReverseMap
virtual unsigned getArch() const =0
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
std::vector< SledEntry > SledContainer
std::error_code make_error_code(BitcodeError E)
std::error_code file_size(const Twine &Path, uint64_t &Result)
Get file size.
Definition: FileSystem.h:610
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
Expected< InstrumentationMap > loadInstrumentationMap(StringRef Filename)
Loads the instrumentation map from |Filename|.
The Input class is used to parse a yaml document into in-memory structs and vectors.
Definition: YAMLTraits.h:1108
std::error_code getName(StringRef &Result) const
Definition: ObjectFile.h:394
Represents an XRay instrumentation sled entry from an object file.
const char * Name
uint8_t getU8(uint32_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:78
static Error loadELF64(StringRef Filename, object::OwningBinary< object::ObjectFile > &ObjFile, InstrumentationMap::SledContainer &Sleds, InstrumentationMap::FunctionAddressMap &FunctionAddresses, InstrumentationMap::FunctionAddressReverseMap &FunctionIds)
section_iterator_range sections() const
Definition: ObjectFile.h:272
Optional< int32_t > getFunctionId(uint64_t Addr) const
Returns an XRay computed function id, provided a function address.
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:593
uint64_t getU64(uint32_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
static Error loadYAML(int Fd, size_t FileSize, StringRef Filename, InstrumentationMap::SledContainer &Sleds, InstrumentationMap::FunctionAddressMap &FunctionAddresses, InstrumentationMap::FunctionAddressReverseMap &FunctionIds)
static ErrorSuccess success()
Create a success value.
Definition: Error.h:311
#define E
Definition: LargeTest.cpp:27
Optional< uint64_t > getFunctionAddr(int32_t FuncId) const
Returns the function address for a function id.
#define C
Definition: LargeTest.cpp:25
#define I(x, y, z)
Definition: MD5.cpp:58
std::unordered_map< int32_t, uint64_t > FunctionAddressMap
const unsigned Kind
The InstrumentationMap represents the computed function id's and indicated function addresses from an...
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
auto find_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:846
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81