1 //===- InstrumentationMap.cpp - XRay Instrumentation Map ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of the InstrumentationMap type for XRay sleds.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/XRay/InstrumentationMap.h"
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"
22 #include "llvm/Support/DataExtractor.h"
23 #include "llvm/Support/Error.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/YAMLTraits.h"
29 #include <system_error>
35 Optional<int32_t> InstrumentationMap::getFunctionId(uint64_t Addr) const {
36 auto I = FunctionIds.find(Addr);
37 if (I != FunctionIds.end())
42 Optional<uint64_t> InstrumentationMap::getFunctionAddr(int32_t FuncId) const {
43 auto I = FunctionAddresses.find(FuncId);
44 if (I != FunctionAddresses.end())
50 loadELF64(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile,
51 InstrumentationMap::SledContainer &Sleds,
52 InstrumentationMap::FunctionAddressMap &FunctionAddresses,
53 InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
54 InstrumentationMap Map;
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));
64 StringRef Contents = "";
65 const auto &Sections = ObjFile.getBinary()->sections();
66 auto I = llvm::find_if(Sections, [&](object::SectionRef Section) {
68 if (Section.getName(Name))
70 return Name == "xray_instr_map";
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));
78 if (I->getContents(Contents))
79 return errorCodeToError(
80 std::make_error_code(std::errc::executable_format_error));
82 // Copy the instrumentation map data into the Sleds data structure.
83 auto C = Contents.bytes_begin();
84 static constexpr size_t ELF64SledEntrySize = 32;
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));
94 for (; C != Contents.bytes_end(); C += ELF64SledEntrySize) {
95 DataExtractor Extractor(
96 StringRef(reinterpret_cast<const char *>(C), ELF64SledEntrySize), true,
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[] = {
105 SledEntry::FunctionKinds::ENTRY, SledEntry::FunctionKinds::EXIT,
106 SledEntry::FunctionKinds::TAIL,
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;
114 // We do replicate the function id generation scheme implemented in the
116 // FIXME: Figure out how to keep this consistent with the XRay runtime.
118 CurFn = Entry.Function;
119 FunctionAddresses[FuncId] = Entry.Function;
120 FunctionIds[Entry.Function] = FuncId;
122 if (Entry.Function != CurFn) {
124 CurFn = Entry.Function;
125 FunctionAddresses[FuncId] = Entry.Function;
126 FunctionIds[Entry.Function] = FuncId;
129 return Error::success();
133 loadYAML(int Fd, size_t FileSize, StringRef Filename,
134 InstrumentationMap::SledContainer &Sleds,
135 InstrumentationMap::FunctionAddressMap &FunctionAddresses,
136 InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
138 sys::fs::mapped_file_region MappedFile(
139 Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
141 return make_error<StringError>(
142 Twine("Failed memory-mapping file '") + Filename + "'.", EC);
144 std::vector<YAMLXRaySledEntry> YAMLSleds;
145 yaml::Input In(StringRef(MappedFile.data(), MappedFile.size()));
148 return make_error<StringError>(
149 Twine("Failed loading YAML document from '") + Filename + "'.",
152 Sleds.reserve(YAMLSleds.size());
153 for (const auto &Y : YAMLSleds) {
154 FunctionAddresses[Y.FuncId] = Y.Function;
155 FunctionIds[Y.Function] = Y.FuncId;
157 SledEntry{Y.Address, Y.Function, Y.Kind, Y.AlwaysInstrument});
159 return Error::success();
162 // FIXME: Create error types that encapsulate a bit more information than what
163 // StringError instances contain.
164 Expected<InstrumentationMap>
165 llvm::xray::loadInstrumentationMap(StringRef Filename) {
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.
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.
176 if (sys::fs::openFileForRead(Filename, Fd))
180 if (sys::fs::file_size(Filename, FileSize))
183 // If the file is empty, we return the original error.
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))
193 } else if (auto E = loadELF64(Filename, *ObjectFileOrError, Map.Sleds,
194 Map.FunctionAddresses, Map.FunctionIds)) {