blob: 84317708d3f41aa422b3982fef68af52140ceebd [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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/XRay/InstrumentationMap.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000015#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"
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000021#include "llvm/Object/ObjectFile.h"
22#include "llvm/Support/DataExtractor.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000023#include "llvm/Support/Error.h"
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000024#include "llvm/Support/FileSystem.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000025#include "llvm/Support/YAMLTraits.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000026#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
David Carlier07cc5a82018-09-10 05:00:43 +000050loadObj(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile,
Eugene Zelenko44d95122017-02-09 01:09:54 +000051 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".
David Carlier07cc5a82018-09-10 05:00:43 +000057 if ((!ObjFile.getBinary()->isELF() && !ObjFile.getBinary()->isMachO()) ||
Tim Shen918ed872017-02-10 21:03:24 +000058 !(ObjFile.getBinary()->getArch() == Triple::x86_64 ||
59 ObjFile.getBinary()->getArch() == Triple::ppc64le))
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000060 return make_error<StringError>(
David Carlier07cc5a82018-09-10 05:00:43 +000061 "File format not supported (only does ELF and Mach-O little endian 64-bit).",
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000062 std::make_error_code(std::errc::not_supported));
63
64 StringRef Contents = "";
65 const auto &Sections = ObjFile.getBinary()->sections();
Eugene Zelenko44d95122017-02-09 01:09:54 +000066 auto I = llvm::find_if(Sections, [&](object::SectionRef Section) {
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000067 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[] = {
105 SledEntry::FunctionKinds::ENTRY, SledEntry::FunctionKinds::EXIT,
106 SledEntry::FunctionKinds::TAIL,
Dean Michael Berrisc5caf3e2017-08-21 00:14:06 +0000107 SledEntry::FunctionKinds::LOG_ARGS_ENTER,
108 SledEntry::FunctionKinds::CUSTOM_EVENT};
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000109 if (Kind >= sizeof(Kinds))
110 return errorCodeToError(
111 std::make_error_code(std::errc::executable_format_error));
112 Entry.Kind = Kinds[Kind];
113 Entry.AlwaysInstrument = Extractor.getU8(&OffsetPtr) != 0;
114
115 // We do replicate the function id generation scheme implemented in the
116 // XRay runtime.
117 // FIXME: Figure out how to keep this consistent with the XRay runtime.
118 if (CurFn == 0) {
119 CurFn = Entry.Function;
120 FunctionAddresses[FuncId] = Entry.Function;
121 FunctionIds[Entry.Function] = FuncId;
122 }
123 if (Entry.Function != CurFn) {
124 ++FuncId;
125 CurFn = Entry.Function;
126 FunctionAddresses[FuncId] = Entry.Function;
127 FunctionIds[Entry.Function] = FuncId;
128 }
129 }
130 return Error::success();
131}
132
Eugene Zelenko44d95122017-02-09 01:09:54 +0000133static Error
134loadYAML(int Fd, size_t FileSize, StringRef Filename,
135 InstrumentationMap::SledContainer &Sleds,
136 InstrumentationMap::FunctionAddressMap &FunctionAddresses,
137 InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000138 std::error_code EC;
139 sys::fs::mapped_file_region MappedFile(
140 Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
141 if (EC)
142 return make_error<StringError>(
143 Twine("Failed memory-mapping file '") + Filename + "'.", EC);
144
145 std::vector<YAMLXRaySledEntry> YAMLSleds;
146 yaml::Input In(StringRef(MappedFile.data(), MappedFile.size()));
147 In >> YAMLSleds;
148 if (In.error())
149 return make_error<StringError>(
150 Twine("Failed loading YAML document from '") + Filename + "'.",
151 In.error());
152
153 Sleds.reserve(YAMLSleds.size());
154 for (const auto &Y : YAMLSleds) {
155 FunctionAddresses[Y.FuncId] = Y.Function;
156 FunctionIds[Y.Function] = Y.FuncId;
157 Sleds.push_back(
158 SledEntry{Y.Address, Y.Function, Y.Kind, Y.AlwaysInstrument});
159 }
160 return Error::success();
161}
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000162
163// FIXME: Create error types that encapsulate a bit more information than what
164// StringError instances contain.
Eugene Zelenko44d95122017-02-09 01:09:54 +0000165Expected<InstrumentationMap>
166llvm::xray::loadInstrumentationMap(StringRef Filename) {
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000167 // At this point we assume the file is an object file -- and if that doesn't
168 // work, we treat it as YAML.
169 // FIXME: Extend to support non-ELF and non-x86_64 binaries.
170
171 InstrumentationMap Map;
172 auto ObjectFileOrError = object::ObjectFile::createObjectFile(Filename);
173 if (!ObjectFileOrError) {
174 auto E = ObjectFileOrError.takeError();
175 // We try to load it as YAML if the ELF load didn't work.
176 int Fd;
177 if (sys::fs::openFileForRead(Filename, Fd))
178 return std::move(E);
179
180 uint64_t FileSize;
181 if (sys::fs::file_size(Filename, FileSize))
182 return std::move(E);
183
184 // If the file is empty, we return the original error.
185 if (FileSize == 0)
186 return std::move(E);
187
188 // From this point on the errors will be only for the YAML parts, so we
189 // consume the errors at this point.
190 consumeError(std::move(E));
191 if (auto E = loadYAML(Fd, FileSize, Filename, Map.Sleds,
192 Map.FunctionAddresses, Map.FunctionIds))
193 return std::move(E);
David Carlier07cc5a82018-09-10 05:00:43 +0000194 } else if (auto E = loadObj(Filename, *ObjectFileOrError, Map.Sleds,
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000195 Map.FunctionAddresses, Map.FunctionIds)) {
196 return std::move(E);
197 }
198 return Map;
199}