Skip to content

Commit 30e2d34

Browse files
Sebastian Kreutzersebastiankreutzer
authored andcommitted
fixup! [XRay] Add DSO support for XRay instrumentation on X86_64
1 parent 77cb964 commit 30e2d34

File tree

14 files changed

+176
-122
lines changed

14 files changed

+176
-122
lines changed

clang/include/clang/Driver/XRayArgs.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ class XRayArgs {
3636
llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const;
3737

3838
bool needsXRayRt() const { return XRayInstrument && XRayRT; }
39-
bool needsXRayDSORt() const {
40-
return XRayInstrument && XRayRT && XRayShared;
41-
}
39+
bool needsXRayDSORt() const { return XRayInstrument && XRayRT && XRayShared; }
4240
llvm::ArrayRef<std::string> modeList() const { return Modes; }
4341
XRayInstrSet instrumentationBundle() const { return InstrumentationBundle; }
4442
};

clang/lib/Driver/XRayArgs.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
6363
<< XRayInstrument->getSpelling() << Triple.str();
6464
}
6565

66-
if (Args.hasFlag(options::OPT_fxray_shared,
67-
options::OPT_fno_xray_shared, false)) {
66+
if (Args.hasFlag(options::OPT_fxray_shared, options::OPT_fno_xray_shared,
67+
false)) {
6868
XRayShared = true;
6969

7070
// DSO instrumentation is currently limited to x86_64
@@ -75,8 +75,8 @@ XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
7575

7676
unsigned PICLvl = std::get<1>(tools::ParsePICArgs(TC, Args));
7777
if (!PICLvl) {
78-
D.Diag(diag::err_opt_not_valid_without_opt)
79-
<< "-fxray-shared" << "-fPIC";
78+
D.Diag(diag::err_opt_not_valid_without_opt) << "-fxray-shared"
79+
<< "-fPIC";
8080
}
8181
}
8282

compiler-rt/include/xray/xray_interface.h

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ enum XRayPatchingStatus {
9393
FAILED = 3,
9494
};
9595

96-
/// This tells XRay to patch the instrumentation points in all currently loaded objects. See XRayPatchingStatus
97-
/// for possible result values.
96+
/// This tells XRay to patch the instrumentation points in all currently loaded
97+
/// objects. See XRayPatchingStatus for possible result values.
9898
extern XRayPatchingStatus __xray_patch();
9999

100100
/// This tells XRay to patch the instrumentation points in the given object.
@@ -105,17 +105,17 @@ extern XRayPatchingStatus __xray_patch_object(int32_t ObjId);
105105
/// result values.
106106
extern XRayPatchingStatus __xray_unpatch();
107107

108-
/// Reverses the effect of __xray_patch_object. See XRayPatchingStatus for possible
109-
/// result values.
108+
/// Reverses the effect of __xray_patch_object. See XRayPatchingStatus for
109+
/// possible result values.
110110
extern XRayPatchingStatus __xray_unpatch_object(int32_t ObjId);
111111

112112
/// This unpacks the given (packed) function id and patches
113113
/// the corresponding function. See XRayPatchingStatus for possible
114114
/// result values.
115115
extern XRayPatchingStatus __xray_patch_function(int32_t FuncId);
116116

117-
/// This patches a specific function in the given object. See XRayPatchingStatus for possible
118-
/// result values.
117+
/// This patches a specific function in the given object. See XRayPatchingStatus
118+
/// for possible result values.
119119
extern XRayPatchingStatus __xray_patch_function_in_object(int32_t FuncId,
120120
int32_t ObjId);
121121

@@ -129,26 +129,29 @@ extern XRayPatchingStatus __xray_unpatch_function(int32_t FuncId);
129129
extern XRayPatchingStatus __xray_unpatch_function_in_object(int32_t FuncId,
130130
int32_t ObjId);
131131

132-
/// This function unpacks the given (packed) function id and returns the address of the corresponding function. We return 0 if we encounter any error, even if 0 may be a valid function
133-
/// address.
132+
/// This function unpacks the given (packed) function id and returns the address
133+
/// of the corresponding function. We return 0 if we encounter any error, even
134+
/// if 0 may be a valid function address.
134135
extern uintptr_t __xray_function_address(int32_t FuncId);
135136

136-
/// This function returns the address of the function in the given object provided valid function and object
137-
/// ids. We return 0 if we encounter any error, even if 0 may be a valid function
138-
/// address.
137+
/// This function returns the address of the function in the given object
138+
/// provided valid function and object ids. We return 0 if we encounter any
139+
/// error, even if 0 may be a valid function address.
139140
extern uintptr_t __xray_function_address_in_object(int32_t FuncId,
140141
int32_t ObjId);
141142

142-
/// This function returns the maximum valid function id for the main executable (object id = 0). Returns 0 if we
143-
/// encounter errors (when there are no instrumented functions, etc.).
143+
/// This function returns the maximum valid function id for the main executable
144+
/// (object id = 0). Returns 0 if we encounter errors (when there are no
145+
/// instrumented functions, etc.).
144146
extern size_t __xray_max_function_id();
145147

146-
/// This function returns the maximum valid function id for the given object. Returns 0 if we
147-
/// encounter errors (when there are no instrumented functions, etc.).
148+
/// This function returns the maximum valid function id for the given object.
149+
/// Returns 0 if we encounter errors (when there are no instrumented functions,
150+
/// etc.).
148151
extern size_t __xray_max_function_id_in_object(int32_t ObjId);
149152

150-
/// This function returns the number of previously registered objects (executable + loaded DSOs).
151-
/// Returns 0 if XRay has not been initialized.
153+
/// This function returns the number of previously registered objects
154+
/// (executable + loaded DSOs). Returns 0 if XRay has not been initialized.
152155
extern size_t __xray_num_objects();
153156

154157
/// Unpacks the function id from the given packed id.
@@ -158,7 +161,8 @@ extern int32_t __xray_unpack_function_id(int32_t PackedId);
158161
extern int32_t __xray_unpack_object_id(int32_t PackedId);
159162

160163
/// Creates and returns a packed id from the given function and object ids.
161-
/// If the ids do not fit within the reserved number of bits for each part, the high bits are truncated.
164+
/// If the ids do not fit within the reserved number of bits for each part, the
165+
/// high bits are truncated.
162166
extern int32_t __xray_pack_id(int32_t FuncId, int32_t ObjId);
163167

164168
/// Initialize the required XRay data structures. This is useful in cases where

compiler-rt/lib/xray/xray_AArch64.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,23 @@ inline static bool patchSled(const bool Enable, const uint32_t FuncId,
8989

9090
bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
9191
const XRaySledEntry &Sled,
92-
void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
92+
const XRayTrampolines &Trampolines,
93+
bool LogArgs) XRAY_NEVER_INSTRUMENT {
94+
auto Trampoline =
95+
LogArgs ? Trampolines.LogArgsTrampoline : Trampolines.EntryTrampoline;
9396
return patchSled(Enable, FuncId, Sled, Trampoline);
9497
}
9598

96-
bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
97-
const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
98-
return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
99+
bool patchFunctionExit(
100+
const bool Enable, const uint32_t FuncId, const XRaySledEntry &Sled,
101+
const XRayTrampolines &Trampolines) XRAY_NEVER_INSTRUMENT {
102+
return patchSled(Enable, FuncId, Sled, Trampolines.ExitTrampoline);
99103
}
100104

101-
bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
102-
const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
103-
return patchSled(Enable, FuncId, Sled, __xray_FunctionTailExit);
105+
bool patchFunctionTailExit(
106+
const bool Enable, const uint32_t FuncId, const XRaySledEntry &Sled,
107+
const XRayTrampolines &Trampolines) XRAY_NEVER_INSTRUMENT {
108+
return patchSled(Enable, FuncId, Sled, Trampolines.TailExitTrampoline);
104109
}
105110

106111
// AArch64AsmPrinter::LowerPATCHABLE_EVENT_CALL generates this code sequence:

compiler-rt/lib/xray/xray_arm.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,23 @@ inline static bool patchSled(const bool Enable, const uint32_t FuncId,
128128

129129
bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
130130
const XRaySledEntry &Sled,
131-
void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
131+
const XRayTrampolines &Trampolines,
132+
bool LogArgs) XRAY_NEVER_INSTRUMENT {
133+
auto Trampoline =
134+
LogArgs ? Trampolines.LogArgsTrampoline : Trampolines.EntryTrampoline;
132135
return patchSled(Enable, FuncId, Sled, Trampoline);
133136
}
134137

135-
bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
136-
const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
137-
return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
138+
bool patchFunctionExit(
139+
const bool Enable, const uint32_t FuncId, const XRaySledEntry &Sled,
140+
const XRayTrampolines &Trampolines) XRAY_NEVER_INSTRUMENT {
141+
return patchSled(Enable, FuncId, Sled, Trampolines.ExitTrampoline);
138142
}
139143

140-
bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
141-
const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
142-
return patchSled(Enable, FuncId, Sled, __xray_FunctionTailExit);
144+
bool patchFunctionTailExit(
145+
const bool Enable, const uint32_t FuncId, const XRaySledEntry &Sled,
146+
const XRayTrampolines &Trampolines) XRAY_NEVER_INSTRUMENT {
147+
return patchSled(Enable, FuncId, Sled, Trampolines.TailExitTrampoline);
143148
}
144149

145150
bool patchCustomEvent(const bool Enable, const uint32_t FuncId,

compiler-rt/lib/xray/xray_hexagon.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,23 @@ inline static bool patchSled(const bool Enable, const uint32_t FuncId,
135135

136136
bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
137137
const XRaySledEntry &Sled,
138-
void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
138+
const XRayTrampolines &Trampolines,
139+
bool LogArgs) XRAY_NEVER_INSTRUMENT {
140+
auto Trampoline =
141+
LogArgs ? Trampolines.LogArgsTrampoline : Trampolines.EntryTrampoline;
139142
return patchSled(Enable, FuncId, Sled, Trampoline);
140143
}
141144

142-
bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
143-
const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
144-
return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
145+
bool patchFunctionExit(
146+
const bool Enable, const uint32_t FuncId, const XRaySledEntry &Sled,
147+
const XRayTrampolines &Trampolines) XRAY_NEVER_INSTRUMENT {
148+
return patchSled(Enable, FuncId, Sled, Trampolines.ExitTrampoline);
145149
}
146150

147-
bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
148-
const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
149-
return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
151+
bool patchFunctionTailExit(
152+
const bool Enable, const uint32_t FuncId, const XRaySledEntry &Sled,
153+
const XRayTrampolines &Trampolines) XRAY_NEVER_INSTRUMENT {
154+
return patchSled(Enable, FuncId, Sled, Trampolines.TailExitTrampoline);
150155
}
151156

152157
bool patchCustomEvent(const bool Enable, const uint32_t FuncId,

compiler-rt/lib/xray/xray_init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ __xray_deregister_dso(int32_t ObjId) XRAY_NEVER_INSTRUMENT {
210210
return false;
211211
}
212212

213-
if (ObjId <= 0 || ObjId >= __xray_num_objects()) {
213+
if (ObjId <= 0 || static_cast<uint32_t>(ObjId) >= __xray_num_objects()) {
214214
if (Verbosity())
215215
Report("Can't deregister object with ID %d: ID is invalid.\n", ObjId);
216216
return false;

compiler-rt/lib/xray/xray_interface.cpp

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
#include "xray_interface_internal.h"
16-
#include "llvm/Support/ErrorHandling.h"
1716

1817
#include <cinttypes>
1918
#include <cstdio>
@@ -154,8 +153,8 @@ namespace {
154153

155154
bool isObjectLoaded(int32_t ObjId) {
156155
SpinMutexLock Guard(&XRayInstrMapMutex);
157-
if (ObjId < 0 ||
158-
ObjId >= atomic_load(&XRayNumObjects, memory_order_acquire)) {
156+
if (ObjId < 0 || static_cast<uint32_t>(ObjId) >=
157+
atomic_load(&XRayNumObjects, memory_order_acquire)) {
159158
return false;
160159
}
161160
return XRayInstrMaps[ObjId].Loaded;
@@ -166,28 +165,24 @@ bool patchSled(const XRaySledEntry &Sled, bool Enable, int32_t FuncId,
166165
bool Success = false;
167166
switch (Sled.Kind) {
168167
case XRayEntryType::ENTRY:
169-
Success =
170-
patchFunctionEntry(Enable, FuncId, Sled, Trampolines.EntryTrampoline);
168+
Success = patchFunctionEntry(Enable, FuncId, Sled, Trampolines,
169+
/*LogArgs=*/false);
171170
break;
172171
case XRayEntryType::EXIT:
173-
Success =
174-
patchFunctionExit(Enable, FuncId, Sled, Trampolines.ExitTrampoline);
172+
Success = patchFunctionExit(Enable, FuncId, Sled, Trampolines);
175173
break;
176174
case XRayEntryType::TAIL:
177-
Success = patchFunctionTailExit(Enable, FuncId, Sled,
178-
Trampolines.TailExitTrampoline);
175+
Success = patchFunctionTailExit(Enable, FuncId, Sled, Trampolines);
179176
break;
180177
case XRayEntryType::LOG_ARGS_ENTRY:
181-
Success =
182-
patchFunctionEntry(Enable, FuncId, Sled, Trampolines.LogArgsTrampoline);
178+
Success = patchFunctionEntry(Enable, FuncId, Sled, Trampolines,
179+
/*LogArgs=*/true);
183180
break;
184181
case XRayEntryType::CUSTOM_EVENT:
185-
Success = patchCustomEvent(Enable, FuncId, Sled,
186-
Trampolines.CustomEventTrampoline);
182+
Success = patchCustomEvent(Enable, FuncId, Sled);
187183
break;
188184
case XRayEntryType::TYPED_EVENT:
189-
Success =
190-
patchTypedEvent(Enable, FuncId, Sled, Trampolines.TypedEventTrampoline);
185+
Success = patchTypedEvent(Enable, FuncId, Sled);
191186
break;
192187
default:
193188
Report("Unsupported sled kind '%" PRIu64 "' @%04x\n", Sled.Address,
@@ -236,8 +231,8 @@ XRayPatchingStatus patchFunction(int32_t FuncId, int32_t ObjId,
236231
XRaySledMap InstrMap;
237232
{
238233
SpinMutexLock Guard(&XRayInstrMapMutex);
239-
if (ObjId < 0 ||
240-
ObjId >= atomic_load(&XRayNumObjects, memory_order_acquire)) {
234+
if (ObjId < 0 || static_cast<uint32_t>(ObjId) >=
235+
atomic_load(&XRayNumObjects, memory_order_acquire)) {
241236
Report("Unable to patch function: invalid sled map index: %d", ObjId);
242237
return XRayPatchingStatus::FAILED;
243238
}
@@ -296,8 +291,8 @@ XRayPatchingStatus controlPatchingObjectUnchecked(bool Enable, int32_t ObjId) {
296291
XRaySledMap InstrMap;
297292
{
298293
SpinMutexLock Guard(&XRayInstrMapMutex);
299-
if (ObjId < 0 ||
300-
ObjId >= atomic_load(&XRayNumObjects, memory_order_acquire)) {
294+
if (ObjId < 0 || static_cast<uint32_t>(ObjId) >=
295+
atomic_load(&XRayNumObjects, memory_order_acquire)) {
301296
Report("Unable to patch functions: invalid sled map index: %d\n", ObjId);
302297
return XRayPatchingStatus::FAILED;
303298
}
@@ -411,9 +406,7 @@ XRayPatchingStatus controlPatching(bool Enable) XRAY_NEVER_INSTRUMENT {
411406
CombinedStatus = NOT_INITIALIZED;
412407
break;
413408
case ONGOING:
414-
llvm_unreachable("Status ONGOING should not appear at this point");
415-
default:
416-
llvm_unreachable("Unhandled patching status");
409+
UNREACHABLE("Status ONGOING should not appear at this point");
417410
}
418411
}
419412
return CombinedStatus;
@@ -442,8 +435,8 @@ XRayPatchingStatus mprotectAndPatchFunction(int32_t FuncId, int32_t ObjId,
442435
XRaySledMap InstrMap;
443436
{
444437
SpinMutexLock Guard(&XRayInstrMapMutex);
445-
if (ObjId < 0 ||
446-
ObjId >= atomic_load(&XRayNumObjects, memory_order_acquire)) {
438+
if (ObjId < 0 || static_cast<uint32_t>(ObjId) >=
439+
atomic_load(&XRayNumObjects, memory_order_acquire)) {
447440
Report("Unable to patch function: invalid sled map index: %d\n", ObjId);
448441
return XRayPatchingStatus::FAILED;
449442
}
@@ -637,7 +630,7 @@ uintptr_t __xray_function_address_in_object(int32_t FuncId, int32_t ObjId)
637630
{
638631
SpinMutexLock Guard(&XRayInstrMapMutex);
639632
auto count = atomic_load(&XRayNumObjects, memory_order_acquire);
640-
if (ObjId < 0 || ObjId >= count) {
633+
if (ObjId < 0 || static_cast<uint32_t>(ObjId) >= count) {
641634
Report("Unable to determine function address: invalid sled map index %d "
642635
"(size is %d)\n",
643636
ObjId, (int)count);
@@ -667,7 +660,8 @@ size_t __xray_max_function_id() XRAY_NEVER_INSTRUMENT {
667660

668661
size_t __xray_max_function_id_in_object(int32_t ObjId) XRAY_NEVER_INSTRUMENT {
669662
SpinMutexLock Guard(&XRayInstrMapMutex);
670-
if (ObjId < 0 || ObjId >= atomic_load(&XRayNumObjects, memory_order_acquire))
663+
if (ObjId < 0 || static_cast<uint32_t>(ObjId) >=
664+
atomic_load(&XRayNumObjects, memory_order_acquire))
671665
return 0;
672666
return XRayInstrMaps[ObjId].Functions;
673667
}

compiler-rt/lib/xray/xray_interface_internal.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,13 @@ struct XRayTrampolines {
8585
void (*ExitTrampoline)();
8686
void (*TailExitTrampoline)();
8787
void (*LogArgsTrampoline)();
88-
void (*CustomEventTrampoline)();
89-
void (*TypedEventTrampoline)();
9088

9189
XRayTrampolines() {
9290
// These resolve to the definitions in the respective executable or DSO.
9391
EntryTrampoline = __xray_FunctionEntry;
9492
ExitTrampoline = __xray_FunctionExit;
9593
TailExitTrampoline = __xray_FunctionTailExit;
9694
LogArgsTrampoline = __xray_ArgLoggerEntry;
97-
CustomEventTrampoline = __xray_CustomEvent;
98-
TypedEventTrampoline = __xray_TypedEvent;
9995
}
10096
};
10197

@@ -140,15 +136,14 @@ struct XRaySledMap {
140136
};
141137

142138
bool patchFunctionEntry(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled,
143-
void (*Trampoline)());
139+
const XRayTrampolines &Trampolines, bool LogArgs);
144140
bool patchFunctionExit(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled,
145-
void (*Trampoline)());
141+
const XRayTrampolines &Trampolines);
146142
bool patchFunctionTailExit(bool Enable, uint32_t FuncId,
147-
const XRaySledEntry &Sled, void (*Trampoline)());
148-
bool patchCustomEvent(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled,
149-
void (*Trampoline)());
150-
bool patchTypedEvent(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled,
151-
void (*Trampoline)());
143+
const XRaySledEntry &Sled,
144+
const XRayTrampolines &Trampolines);
145+
bool patchCustomEvent(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled);
146+
bool patchTypedEvent(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled);
152147

153148
} // namespace __xray
154149

0 commit comments

Comments
 (0)