forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstrumentationRuntimeLibrary.cpp
333 lines (297 loc) · 13.6 KB
/
InstrumentationRuntimeLibrary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//===- bolt/RuntimeLibs/InstrumentationRuntimeLibrary.cpp -----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the InstrumentationRuntimeLibrary class.
//
//===----------------------------------------------------------------------===//
#include "bolt/RuntimeLibs/InstrumentationRuntimeLibrary.h"
#include "bolt/Core/BinaryFunction.h"
#include "bolt/Core/JumpTable.h"
#include "bolt/Utils/CommandLineOpts.h"
#include "llvm/ExecutionEngine/RuntimeDyld.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Support/CommandLine.h"
using namespace llvm;
using namespace bolt;
namespace opts {
cl::opt<std::string> RuntimeInstrumentationLib(
"runtime-instrumentation-lib",
cl::desc("specify file name of the runtime instrumentation library"),
cl::ZeroOrMore, cl::init("libbolt_rt_instr.a"), cl::cat(BoltOptCategory));
extern cl::opt<bool> InstrumentationFileAppendPID;
extern cl::opt<bool> ConservativeInstrumentation;
extern cl::opt<std::string> InstrumentationFilename;
extern cl::opt<std::string> InstrumentationBinpath;
extern cl::opt<uint32_t> InstrumentationSleepTime;
extern cl::opt<bool> InstrumentationNoCountersClear;
extern cl::opt<bool> InstrumentationWaitForks;
extern cl::opt<JumpTableSupportLevel> JumpTables;
} // namespace opts
void InstrumentationRuntimeLibrary::adjustCommandLineOptions(
const BinaryContext &BC) const {
if (!BC.HasRelocations) {
errs() << "BOLT-ERROR: instrumentation runtime libraries require "
"relocations\n";
exit(1);
}
if (opts::JumpTables != JTS_MOVE) {
opts::JumpTables = JTS_MOVE;
outs() << "BOLT-INFO: forcing -jump-tables=move for instrumentation\n";
}
if (!BC.StartFunctionAddress) {
errs() << "BOLT-ERROR: instrumentation runtime libraries require a known "
"entry point of "
"the input binary\n";
exit(1);
}
if (!BC.FiniFunctionAddress && !BC.IsStaticExecutable) {
errs() << "BOLT-ERROR: input binary lacks DT_FINI entry in the dynamic "
"section but instrumentation currently relies on patching "
"DT_FINI to write the profile\n";
exit(1);
}
}
void InstrumentationRuntimeLibrary::emitBinary(BinaryContext &BC,
MCStreamer &Streamer) {
MCSection *Section = BC.isELF()
? static_cast<MCSection *>(BC.Ctx->getELFSection(
".bolt.instr.counters", ELF::SHT_PROGBITS,
BinarySection::getFlags(/*IsReadOnly=*/false,
/*IsText=*/false,
/*IsAllocatable=*/true)
))
: static_cast<MCSection *>(BC.Ctx->getMachOSection(
"__BOLT", "__counters", MachO::S_REGULAR,
SectionKind::getData()));
if (BC.IsStaticExecutable && !opts::InstrumentationSleepTime) {
errs() << "BOLT-ERROR: instrumentation of static binary currently does not "
"support profile output on binary finalization, so it "
"requires -instrumentation-sleep-time=N (N>0) usage\n";
exit(1);
}
Section->setAlignment(llvm::Align(BC.RegularPageSize));
Streamer.SwitchSection(Section);
// EmitOffset is used to determine padding size for data alignment
uint64_t EmitOffset = 0;
auto emitLabel = [&Streamer](MCSymbol *Symbol, bool IsGlobal = true) {
Streamer.emitLabel(Symbol);
if (IsGlobal)
Streamer.emitSymbolAttribute(Symbol, MCSymbolAttr::MCSA_Global);
};
auto emitLabelByName = [&BC, emitLabel](StringRef Name,
bool IsGlobal = true) {
MCSymbol *Symbol = BC.Ctx->getOrCreateSymbol(Name);
emitLabel(Symbol, IsGlobal);
};
auto emitPadding = [&Streamer, &EmitOffset](unsigned Size) {
const uint64_t Padding = alignTo(EmitOffset, Size) - EmitOffset;
if (Padding) {
Streamer.emitFill(Padding, 0);
EmitOffset += Padding;
}
};
auto emitDataSize = [&EmitOffset](unsigned Size) { EmitOffset += Size; };
auto emitDataPadding = [emitPadding, emitDataSize](unsigned Size) {
emitPadding(Size);
emitDataSize(Size);
};
auto emitFill = [&Streamer, emitDataSize,
emitLabel](unsigned Size, MCSymbol *Symbol = nullptr,
uint8_t Byte = 0) {
emitDataSize(Size);
if (Symbol)
emitLabel(Symbol, /*IsGlobal*/ false);
Streamer.emitFill(Size, Byte);
};
auto emitValue = [&BC, &Streamer, emitDataPadding,
emitLabel](MCSymbol *Symbol, const MCExpr *Value) {
const unsigned Psize = BC.AsmInfo->getCodePointerSize();
emitDataPadding(Psize);
emitLabel(Symbol);
if (Value)
Streamer.emitValue(Value, Psize);
else
Streamer.emitFill(Psize, 0);
};
auto emitIntValue = [&Streamer, emitDataPadding, emitLabelByName](
StringRef Name, uint64_t Value, unsigned Size = 4) {
emitDataPadding(Size);
emitLabelByName(Name);
Streamer.emitIntValue(Value, Size);
};
auto emitString = [&Streamer, emitDataSize, emitLabelByName,
emitFill](StringRef Name, StringRef Contents) {
emitDataSize(Contents.size());
emitLabelByName(Name);
Streamer.emitBytes(Contents);
emitFill(1);
};
// All of the following symbols will be exported as globals to be used by the
// instrumentation runtime library to dump the instrumentation data to disk.
// Label marking start of the memory region containing instrumentation
// counters, total vector size is Counters.size() 8-byte counters
emitLabelByName("__bolt_instr_locations");
for (MCSymbol *const &Label : Summary->Counters)
emitFill(sizeof(uint64_t), Label);
emitPadding(BC.RegularPageSize);
emitIntValue("__bolt_instr_sleep_time", opts::InstrumentationSleepTime);
emitIntValue("__bolt_instr_no_counters_clear",
!!opts::InstrumentationNoCountersClear, 1);
emitIntValue("__bolt_instr_conservative", !!opts::ConservativeInstrumentation,
1);
emitIntValue("__bolt_instr_wait_forks", !!opts::InstrumentationWaitForks, 1);
emitIntValue("__bolt_num_counters", Summary->Counters.size());
emitValue(Summary->IndCallCounterFuncPtr, nullptr);
emitValue(Summary->IndTailCallCounterFuncPtr, nullptr);
emitIntValue("__bolt_instr_num_ind_calls",
Summary->IndCallDescriptions.size());
emitIntValue("__bolt_instr_num_ind_targets",
Summary->IndCallTargetDescriptions.size());
emitIntValue("__bolt_instr_num_funcs", Summary->FunctionDescriptions.size());
emitString("__bolt_instr_filename", opts::InstrumentationFilename);
emitString("__bolt_instr_binpath", opts::InstrumentationBinpath);
emitIntValue("__bolt_instr_use_pid", !!opts::InstrumentationFileAppendPID, 1);
if (BC.isMachO()) {
MCSection *TablesSection = BC.Ctx->getMachOSection(
"__BOLT", "__tables", MachO::S_REGULAR, SectionKind::getData());
TablesSection->setAlignment(llvm::Align(BC.RegularPageSize));
Streamer.SwitchSection(TablesSection);
emitString("__bolt_instr_tables", buildTables(BC));
}
}
void InstrumentationRuntimeLibrary::link(
BinaryContext &BC, StringRef ToolPath, RuntimeDyld &RTDyld,
std::function<void(RuntimeDyld &)> OnLoad) {
std::string LibPath = getLibPath(ToolPath, opts::RuntimeInstrumentationLib);
loadLibrary(LibPath, RTDyld);
OnLoad(RTDyld);
RTDyld.finalizeWithMemoryManagerLocking();
if (RTDyld.hasError()) {
outs() << "BOLT-ERROR: RTDyld failed: " << RTDyld.getErrorString() << "\n";
exit(1);
}
if (BC.isMachO())
return;
RuntimeFiniAddress = RTDyld.getSymbol("__bolt_instr_fini").getAddress();
if (!RuntimeFiniAddress) {
errs() << "BOLT-ERROR: instrumentation library does not define "
"__bolt_instr_fini: "
<< LibPath << "\n";
exit(1);
}
RuntimeStartAddress = RTDyld.getSymbol("__bolt_instr_start").getAddress();
if (!RuntimeStartAddress) {
errs() << "BOLT-ERROR: instrumentation library does not define "
"__bolt_instr_start: "
<< LibPath << "\n";
exit(1);
}
outs() << "BOLT-INFO: output linked against instrumentation runtime "
"library, lib entry point is 0x"
<< Twine::utohexstr(RuntimeFiniAddress) << "\n";
outs() << "BOLT-INFO: clear procedure is 0x"
<< Twine::utohexstr(
RTDyld.getSymbol("__bolt_instr_clear_counters").getAddress())
<< "\n";
emitTablesAsELFNote(BC);
}
std::string InstrumentationRuntimeLibrary::buildTables(BinaryContext &BC) {
std::string TablesStr;
raw_string_ostream OS(TablesStr);
// This is sync'ed with runtime/instr.cpp:readDescriptions()
auto getOutputAddress = [](const BinaryFunction &Func,
uint64_t Offset) -> uint64_t {
return Offset == 0
? Func.getOutputAddress()
: Func.translateInputToOutputAddress(Func.getAddress() + Offset);
};
// Indirect targets need to be sorted for fast lookup during runtime
std::sort(Summary->IndCallTargetDescriptions.begin(),
Summary->IndCallTargetDescriptions.end(),
[&](const IndCallTargetDescription &A,
const IndCallTargetDescription &B) {
return getOutputAddress(*A.Target, A.ToLoc.Offset) <
getOutputAddress(*B.Target, B.ToLoc.Offset);
});
// Start of the vector with descriptions (one CounterDescription for each
// counter), vector size is Counters.size() CounterDescription-sized elmts
const size_t IDSize =
Summary->IndCallDescriptions.size() * sizeof(IndCallDescription);
OS.write(reinterpret_cast<const char *>(&IDSize), 4);
for (const IndCallDescription &Desc : Summary->IndCallDescriptions) {
OS.write(reinterpret_cast<const char *>(&Desc.FromLoc.FuncString), 4);
OS.write(reinterpret_cast<const char *>(&Desc.FromLoc.Offset), 4);
}
const size_t ITDSize = Summary->IndCallTargetDescriptions.size() *
sizeof(IndCallTargetDescription);
OS.write(reinterpret_cast<const char *>(&ITDSize), 4);
for (const IndCallTargetDescription &Desc :
Summary->IndCallTargetDescriptions) {
OS.write(reinterpret_cast<const char *>(&Desc.ToLoc.FuncString), 4);
OS.write(reinterpret_cast<const char *>(&Desc.ToLoc.Offset), 4);
uint64_t TargetFuncAddress =
getOutputAddress(*Desc.Target, Desc.ToLoc.Offset);
OS.write(reinterpret_cast<const char *>(&TargetFuncAddress), 8);
}
uint32_t FuncDescSize = Summary->getFDSize();
OS.write(reinterpret_cast<const char *>(&FuncDescSize), 4);
for (const FunctionDescription &Desc : Summary->FunctionDescriptions) {
const size_t LeafNum = Desc.LeafNodes.size();
OS.write(reinterpret_cast<const char *>(&LeafNum), 4);
for (const InstrumentedNode &LeafNode : Desc.LeafNodes) {
OS.write(reinterpret_cast<const char *>(&LeafNode.Node), 4);
OS.write(reinterpret_cast<const char *>(&LeafNode.Counter), 4);
}
const size_t EdgesNum = Desc.Edges.size();
OS.write(reinterpret_cast<const char *>(&EdgesNum), 4);
for (const EdgeDescription &Edge : Desc.Edges) {
OS.write(reinterpret_cast<const char *>(&Edge.FromLoc.FuncString), 4);
OS.write(reinterpret_cast<const char *>(&Edge.FromLoc.Offset), 4);
OS.write(reinterpret_cast<const char *>(&Edge.FromNode), 4);
OS.write(reinterpret_cast<const char *>(&Edge.ToLoc.FuncString), 4);
OS.write(reinterpret_cast<const char *>(&Edge.ToLoc.Offset), 4);
OS.write(reinterpret_cast<const char *>(&Edge.ToNode), 4);
OS.write(reinterpret_cast<const char *>(&Edge.Counter), 4);
}
const size_t CallsNum = Desc.Calls.size();
OS.write(reinterpret_cast<const char *>(&CallsNum), 4);
for (const CallDescription &Call : Desc.Calls) {
OS.write(reinterpret_cast<const char *>(&Call.FromLoc.FuncString), 4);
OS.write(reinterpret_cast<const char *>(&Call.FromLoc.Offset), 4);
OS.write(reinterpret_cast<const char *>(&Call.FromNode), 4);
OS.write(reinterpret_cast<const char *>(&Call.ToLoc.FuncString), 4);
OS.write(reinterpret_cast<const char *>(&Call.ToLoc.Offset), 4);
OS.write(reinterpret_cast<const char *>(&Call.Counter), 4);
uint64_t TargetFuncAddress =
getOutputAddress(*Call.Target, Call.ToLoc.Offset);
OS.write(reinterpret_cast<const char *>(&TargetFuncAddress), 8);
}
const size_t EntryNum = Desc.EntryNodes.size();
OS.write(reinterpret_cast<const char *>(&EntryNum), 4);
for (const EntryNode &EntryNode : Desc.EntryNodes) {
OS.write(reinterpret_cast<const char *>(&EntryNode.Node), 8);
uint64_t TargetFuncAddress =
getOutputAddress(*Desc.Function, EntryNode.Address);
OS.write(reinterpret_cast<const char *>(&TargetFuncAddress), 8);
}
}
// Our string table lives immediately after descriptions vector
OS << Summary->StringTable;
OS.flush();
return TablesStr;
}
void InstrumentationRuntimeLibrary::emitTablesAsELFNote(BinaryContext &BC) {
std::string TablesStr = buildTables(BC);
const std::string BoltInfo = BinarySection::encodeELFNote(
"BOLT", TablesStr, BinarySection::NT_BOLT_INSTRUMENTATION_TABLES);
BC.registerOrUpdateNoteSection(".bolt.instr.tables", copyByteArray(BoltInfo),
BoltInfo.size(),
/*Alignment=*/1,
/*IsReadOnly=*/true, ELF::SHT_NOTE);
}