LLVM: lib/XRay/InstrumentationMap.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

27#include

28#include

29#include <system_error>

30#include

31

32using namespace llvm;

33using namespace xray;

34

36 auto I = FunctionIds.find(Addr);

37 if (I != FunctionIds.end())

38 return I->second;

39 return std::nullopt;

40}

41

42std::optional<uint64_t>

44 auto I = FunctionAddresses.find(FuncId);

45 if (I != FunctionAddresses.end())

46 return I->second;

47 return std::nullopt;

48}

49

51

58

59

60 if ((!ObjFile.getBinary()->isELF() && !ObjFile.getBinary()->isMachO()) ||

68 "File format not supported (only does ELF and Mach-O little endian "

69 "64-bit).",

70 std::make_error_code(std::errc::not_supported));

71

73 const auto &Sections = ObjFile.getBinary()->sections();

77 if (NameOrErr) {

78 Address = Section.getAddress();

79 return *NameOrErr == "xray_instr_map";

80 }

82 return false;

83 });

84

85 if (I == Sections.end())

87 "Failed to find XRay instrumentation map.",

88 std::make_error_code(std::errc::executable_format_error));

89

90 if (Error E = I->getContents().moveInto(Contents))

91 return E;

92

94 if (ObjFile.getBinary()->isELF()) {

97 return ELFObj->getELFFile().getRelativeRelocationType();

98 else if (const auto *ELFObj =

100 return ELFObj->getELFFile().getRelativeRelocationType();

101 else if (const auto *ELFObj =

103 return ELFObj->getELFFile().getRelativeRelocationType();

104 else if (const auto *ELFObj =

106 return ELFObj->getELFFile().getRelativeRelocationType();

107 else

108 return static_cast<uint32_t>(0);

110

113 std::tie(Supports, Resolver) =

115

119 if (Supports && Supports(Reloc.getType())) {

121 if (!ValueOrErr)

124 {Reloc.getOffset(),

126 }

127 } else if (Supports && Supports(Reloc.getType())) {

129 auto A = AddendOrErr ? *AddendOrErr : 0;

131 if (!ValueOrErr)

132

135 {Reloc.getOffset(),

137 } else if (Reloc.getType() == RelativeRelocation) {

139 Relocs.insert({Reloc.getOffset(), *AddendOrErr});

140 }

141 }

142 }

143 }

144

145

147 bool Is32Bit = ObjFile.getBinary()->makeTriple().isArch32Bit();

148 size_t ELFSledEntrySize = Is32Bit ? 16 : 32;

149

150 if ((C - Contents.bytes_end()) % ELFSledEntrySize != 0)

152 Twine("Instrumentation map entries not evenly divisible by size of "

153 "an XRay sled entry."),

154 std::make_error_code(std::errc::executable_format_error));

155

157 if (!Address) {

160 if (R != Relocs.end())

161 return R->second;

162 }

163 return Address;

164 };

165

166 const int WordSize = Is32Bit ? 4 : 8;

167 int32_t FuncId = 1;

169 for (; C != Contents.bytes_end(); C += ELFSledEntrySize) {

171 StringRef(reinterpret_cast<const char *>(C), ELFSledEntrySize), true,

172 8);

173 Sleds.push_back({});

174 auto &Entry = Sleds.back();

176 uint64_t AddrOff = OffsetPtr;

177 if (Is32Bit)

178 Entry.Address = RelocateOrElse(AddrOff, Extractor.getU32(&OffsetPtr));

179 else

180 Entry.Address = RelocateOrElse(AddrOff, Extractor.getU64(&OffsetPtr));

181 uint64_t FuncOff = OffsetPtr;

182 if (Is32Bit)

183 Entry.Function = RelocateOrElse(FuncOff, Extractor.getU32(&OffsetPtr));

184 else

185 Entry.Function = RelocateOrElse(FuncOff, Extractor.getU64(&OffsetPtr));

186 auto Kind = Extractor.getU8(&OffsetPtr);

192 if (Kind >= std::size(Kinds))

194 std::make_error_code(std::errc::executable_format_error));

195 Entry.Kind = Kinds[Kind];

196 Entry.AlwaysInstrument = Extractor.getU8(&OffsetPtr) != 0;

197 Entry.Version = Extractor.getU8(&OffsetPtr);

198 if (Entry.Version >= 2) {

199 Entry.Address += C - Contents.bytes_begin() + Address;

200 Entry.Function += C - Contents.bytes_begin() + WordSize + Address;

201 }

202

203

204

205

206 if (CurFn == 0) {

207 CurFn = Entry.Function;

208 FunctionAddresses[FuncId] = Entry.Function;

209 FunctionIds[Entry.Function] = FuncId;

210 }

211 if (Entry.Function != CurFn) {

212 ++FuncId;

213 CurFn = Entry.Function;

214 FunctionAddresses[FuncId] = Entry.Function;

215 FunctionIds[Entry.Function] = FuncId;

216 }

217 }

219}

220

226 std::error_code EC;

230 if (EC)

232 Twine("Failed memory-mapping file '") + Filename + "'.", EC);

233

234 std::vector YAMLSleds;

236 In >> YAMLSleds;

237 if (In.error())

239 Twine("Failed loading YAML document from '") + Filename + "'.",

240 In.error());

241

242 Sleds.reserve(YAMLSleds.size());

243 for (const auto &Y : YAMLSleds) {

244 FunctionAddresses[Y.FuncId] = Y.Function;

245 FunctionIds[Y.Function] = Y.FuncId;

246 Sleds.push_back(SledEntry{Y.Address, Y.Function, Y.Kind, Y.AlwaysInstrument,

247 Y.Version});

248 }

250}

251

252

253

254Expected

256

257

258

259

262 if (!ObjectFileOrError) {

263 auto E = ObjectFileOrError.takeError();

264

267 if (!FdOrErr) {

268

270 return std::move(E);

271 }

272

275 return std::move(E);

276

277

278 if (FileSize == 0)

279 return std::move(E);

280

281

282

284 if (auto E = loadYAML(*FdOrErr, FileSize, Filename, Map.Sleds,

285 Map.FunctionAddresses, Map.FunctionIds))

286 return std::move(E);

287 } else if (auto E = loadObj(Filename, *ObjectFileOrError, Map.Sleds,

288 Map.FunctionAddresses, Map.FunctionIds)) {

289 return std::move(E);

290 }

291 return Map;

292}

static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")

static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")

This file defines the DenseMap class.

static void getAddend(uint64_t &, const Elf_Rel_Impl< ELFT, false > &)

static Error loadObj(StringRef Filename, object::OwningBinary< object::ObjectFile > &ObjFile, InstrumentationMap::SledContainer &Sleds, InstrumentationMap::FunctionAddressMap &FunctionAddresses, InstrumentationMap::FunctionAddressReverseMap &FunctionIds)

Definition InstrumentationMap.cpp:53

static Error loadYAML(sys::fs::file_t Fd, size_t FileSize, StringRef Filename, InstrumentationMap::SledContainer &Sleds, InstrumentationMap::FunctionAddressMap &FunctionAddresses, InstrumentationMap::FunctionAddressReverseMap &FunctionIds)

Definition InstrumentationMap.cpp:222

DenseMap< uint64_t, uint64_t > RelocMap

Definition InstrumentationMap.cpp:50

static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")

iterator find(const_arg_type_t< KeyT > Val)

DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator

std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)

Lightweight error class with error context and mandatory checking.

static ErrorSuccess success()

Create a success value.

Tagged union holding either a T or a Error.

Error takeError()

Take ownership of the stored error.

Interface for looking up the initializer for a variable name, used by Init::resolveReferences.

StringRef - Represent a constant reference to a string, i.e.

const unsigned char * bytes_end() const

const unsigned char * bytes_begin() const

Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...

Expected< int64_t > getAddend() const

This class is the base class for all object file types.

static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)

This is a value type class that represents a single relocation in the list of relocations in the obje...

This is a value type class that represents a single section in the list of sections in the object fil...

This class represents a memory mapped file.

LLVM_ABI size_t size() const

@ readonly

May only access map via const_data as read only.

LLVM_ABI char * data() const

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

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.

Definition InstrumentationMap.cpp:35

LLVM_ABI std::optional< uint64_t > getFunctionAddr(int32_t FuncId) const

Returns the function address for a function id.

Definition InstrumentationMap.cpp:43

The Input class is used to parse a yaml document into in-memory structs and vectors.

@ C

The default llvm calling convention, compatible with C.

LLVM_ABI uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R, uint64_t S, uint64_t LocData)

uint64_t(*)(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t Addend) RelocationResolver

LLVM_ABI std::pair< SupportsRelocation, RelocationResolver > getRelocationResolver(const ObjectFile &Obj)

bool(*)(uint64_t) SupportsRelocation

LLVM_ABI std::error_code closeFile(file_t &F)

Close the file object.

LLVM_ABI Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)

Opens the file with the given name in a read-only mode, returning its open file descriptor.

std::error_code file_size(const Twine &Path, uint64_t &Result)

Get file size.

LLVM_ABI Expected< InstrumentationMap > loadInstrumentationMap(StringRef Filename)

Loads the instrumentation map from |Filename|.

Definition InstrumentationMap.cpp:255

This is an optimization pass for GlobalISel generic memory operations.

decltype(auto) dyn_cast(const From &Val)

dyn_cast - Return the argument parameter cast to the specified type.

Error make_error(ArgTs &&... Args)

Make a Error instance representing failure using the given error info type.

auto find_if(R &&Range, UnaryPredicate P)

Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.

LLVM_ABI Error errorCodeToError(std::error_code EC)

Helper for converting an std::error_code to a Error.

void consumeError(Error Err)

Consume a Error without doing anything.

Represents an XRay instrumentation sled entry from an object file.

FunctionKinds

Each entry here represents the kinds of supported instrumentation map entries.