blob: b3b482f0286302373c1d138e5961c6e3dd6d74fb [file] [log] [blame]
Dean Michael Berris0e8abab2017-02-01 00:05:29 +00001//===- 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
Eugene Zelenko44d95122017-02-09 01:09:54 +000014#include "llvm/ADT/None.h"
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/ADT/Triple.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/Object/Binary.h"
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000020#include "llvm/Object/ObjectFile.h"
21#include "llvm/Support/DataExtractor.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000022#include "llvm/Support/Error.h"
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000023#include "llvm/Support/FileSystem.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000024#include "llvm/Support/YAMLTraits.h"
25#include "llvm/XRay/InstrumentationMap.h"
26#include <algorithm>
27#include <cstddef>
28#include <cstdint>
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000029#include <system_error>
Eugene Zelenko44d95122017-02-09 01:09:54 +000030#include <vector>
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000031
Eugene Zelenko44d95122017-02-09 01:09:54 +000032using namespace llvm;
33using namespace xray;
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000034
35Optional<int32_t> InstrumentationMap::getFunctionId(uint64_t Addr) const {
36 auto I = FunctionIds.find(Addr);
37 if (I != FunctionIds.end())
38 return I->second;
39 return None;
40}
41
42Optional<uint64_t> InstrumentationMap::getFunctionAddr(int32_t FuncId) const {
43 auto I = FunctionAddresses.find(FuncId);
44 if (I != FunctionAddresses.end())
45 return I->second;
46 return None;
47}
48
Eugene Zelenko44d95122017-02-09 01:09:54 +000049static Error
50loadELF64(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile,
51 InstrumentationMap::SledContainer &Sleds,
52 InstrumentationMap::FunctionAddressMap &FunctionAddresses,
53 InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000054 InstrumentationMap Map;
55
56 // Find the section named "xray_instr_map".
57 if (!ObjFile.getBinary()->isELF() ||
58 ObjFile.getBinary()->getArch() != Triple::x86_64)
59 return make_error<StringError>(
60 "File format not supported (only does ELF little endian 64-bit).",
61 std::make_error_code(std::errc::not_supported));
62
63 StringRef Contents = "";
64 const auto &Sections = ObjFile.getBinary()->sections();
Eugene Zelenko44d95122017-02-09 01:09:54 +000065 auto I = llvm::find_if(Sections, [&](object::SectionRef Section) {
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000066 StringRef Name = "";
67 if (Section.getName(Name))
68 return false;
69 return Name == "xray_instr_map";
70 });
71
72 if (I == Sections.end())
73 return make_error<StringError>(
74 "Failed to find XRay instrumentation map.",
75 std::make_error_code(std::errc::executable_format_error));
76
77 if (I->getContents(Contents))
78 return errorCodeToError(
79 std::make_error_code(std::errc::executable_format_error));
80
81 // Copy the instrumentation map data into the Sleds data structure.
82 auto C = Contents.bytes_begin();
83 static constexpr size_t ELF64SledEntrySize = 32;
84
85 if ((C - Contents.bytes_end()) % ELF64SledEntrySize != 0)
86 return make_error<StringError>(
87 Twine("Instrumentation map entries not evenly divisible by size of "
88 "an XRay sled entry in ELF64."),
89 std::make_error_code(std::errc::executable_format_error));
90
91 int32_t FuncId = 1;
92 uint64_t CurFn = 0;
93 for (; C != Contents.bytes_end(); C += ELF64SledEntrySize) {
94 DataExtractor Extractor(
95 StringRef(reinterpret_cast<const char *>(C), ELF64SledEntrySize), true,
96 8);
97 Sleds.push_back({});
98 auto &Entry = Sleds.back();
99 uint32_t OffsetPtr = 0;
100 Entry.Address = Extractor.getU64(&OffsetPtr);
101 Entry.Function = Extractor.getU64(&OffsetPtr);
102 auto Kind = Extractor.getU8(&OffsetPtr);
103 static constexpr SledEntry::FunctionKinds Kinds[] = {
104 SledEntry::FunctionKinds::ENTRY, SledEntry::FunctionKinds::EXIT,
105 SledEntry::FunctionKinds::TAIL,
106 };
107 if (Kind >= sizeof(Kinds))
108 return errorCodeToError(
109 std::make_error_code(std::errc::executable_format_error));
110 Entry.Kind = Kinds[Kind];
111 Entry.AlwaysInstrument = Extractor.getU8(&OffsetPtr) != 0;
112
113 // We do replicate the function id generation scheme implemented in the
114 // XRay runtime.
115 // FIXME: Figure out how to keep this consistent with the XRay runtime.
116 if (CurFn == 0) {
117 CurFn = Entry.Function;
118 FunctionAddresses[FuncId] = Entry.Function;
119 FunctionIds[Entry.Function] = FuncId;
120 }
121 if (Entry.Function != CurFn) {
122 ++FuncId;
123 CurFn = Entry.Function;
124 FunctionAddresses[FuncId] = Entry.Function;
125 FunctionIds[Entry.Function] = FuncId;
126 }
127 }
128 return Error::success();
129}
130
Eugene Zelenko44d95122017-02-09 01:09:54 +0000131static Error
132loadYAML(int Fd, size_t FileSize, StringRef Filename,
133 InstrumentationMap::SledContainer &Sleds,
134 InstrumentationMap::FunctionAddressMap &FunctionAddresses,
135 InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000136 std::error_code EC;
137 sys::fs::mapped_file_region MappedFile(
138 Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
139 if (EC)
140 return make_error<StringError>(
141 Twine("Failed memory-mapping file '") + Filename + "'.", EC);
142
143 std::vector<YAMLXRaySledEntry> YAMLSleds;
144 yaml::Input In(StringRef(MappedFile.data(), MappedFile.size()));
145 In >> YAMLSleds;
146 if (In.error())
147 return make_error<StringError>(
148 Twine("Failed loading YAML document from '") + Filename + "'.",
149 In.error());
150
151 Sleds.reserve(YAMLSleds.size());
152 for (const auto &Y : YAMLSleds) {
153 FunctionAddresses[Y.FuncId] = Y.Function;
154 FunctionIds[Y.Function] = Y.FuncId;
155 Sleds.push_back(
156 SledEntry{Y.Address, Y.Function, Y.Kind, Y.AlwaysInstrument});
157 }
158 return Error::success();
159}
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000160
161// FIXME: Create error types that encapsulate a bit more information than what
162// StringError instances contain.
Eugene Zelenko44d95122017-02-09 01:09:54 +0000163Expected<InstrumentationMap>
164llvm::xray::loadInstrumentationMap(StringRef Filename) {
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000165 // At this point we assume the file is an object file -- and if that doesn't
166 // work, we treat it as YAML.
167 // FIXME: Extend to support non-ELF and non-x86_64 binaries.
168
169 InstrumentationMap Map;
170 auto ObjectFileOrError = object::ObjectFile::createObjectFile(Filename);
171 if (!ObjectFileOrError) {
172 auto E = ObjectFileOrError.takeError();
173 // We try to load it as YAML if the ELF load didn't work.
174 int Fd;
175 if (sys::fs::openFileForRead(Filename, Fd))
176 return std::move(E);
177
178 uint64_t FileSize;
179 if (sys::fs::file_size(Filename, FileSize))
180 return std::move(E);
181
182 // If the file is empty, we return the original error.
183 if (FileSize == 0)
184 return std::move(E);
185
186 // From this point on the errors will be only for the YAML parts, so we
187 // consume the errors at this point.
188 consumeError(std::move(E));
189 if (auto E = loadYAML(Fd, FileSize, Filename, Map.Sleds,
190 Map.FunctionAddresses, Map.FunctionIds))
191 return std::move(E);
192 } else if (auto E = loadELF64(Filename, *ObjectFileOrError, Map.Sleds,
193 Map.FunctionAddresses, Map.FunctionIds)) {
194 return std::move(E);
195 }
196 return Map;
197}