Skip to content

Commit

Permalink
[DeviceSanitizer] Only try to get backtrace symbols when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaomaosu committed Sep 25, 2024
1 parent 7a2caca commit 6f67b1c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions source/loader/layers/sanitizer/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ inline uint64_t GetSizeAndRedzoneSizeForLocal(uint64_t Size,
return Result; \
}

using BacktraceFrame = void *;
using BacktraceInfo = std::string;

struct SourceInfo {
Expand Down
20 changes: 10 additions & 10 deletions source/loader/layers/sanitizer/linux/backtrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ namespace ur_sanitizer_layer {
StackTrace GetCurrentBacktrace() {
void *Frames[MAX_BACKTRACE_FRAMES];
int FrameCount = backtrace(Frames, MAX_BACKTRACE_FRAMES);
char **Symbols = backtrace_symbols(Frames, FrameCount);

if (Symbols == nullptr) {
return StackTrace();
}

StackTrace Stack;
for (int i = 0; i < FrameCount; i++) {
BacktraceInfo addr_info(Symbols[i]);
Stack.stack.emplace_back(addr_info);
}
free(Symbols);
Stack.stack =
std::vector<BacktraceFrame>(&Frames[0], &Frames[FrameCount - 1]);

return Stack;
}

char **GetBacktraceSymbols(const std::vector<BacktraceFrame> &BacktraceFrames) {
assert(!BacktraceFrames.empty());

char **BacktraceSymbols =
backtrace_symbols(&BacktraceFrames[0], BacktraceFrames.size());
return BacktraceSymbols;
}

} // namespace ur_sanitizer_layer
8 changes: 7 additions & 1 deletion source/loader/layers/sanitizer/stacktrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ void StackTrace::print() const {

unsigned index = 0;

for (auto &BI : stack) {
char **BacktraceSymbols = GetBacktraceSymbols(stack);

for (size_t i = 0; i < stack.size(); i++) {
BacktraceInfo BI = BacktraceSymbols[i];

// Skip runtime modules
if (Contains(BI, "libsycl.so") ||
Contains(BI, "libpi_unified_runtime.so") ||
Expand Down Expand Up @@ -123,6 +127,8 @@ void StackTrace::print() const {
++index;
}
getContext()->logger.always("");

free(BacktraceSymbols);
}

} // namespace ur_sanitizer_layer
6 changes: 4 additions & 2 deletions source/loader/layers/sanitizer/stacktrace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@

namespace ur_sanitizer_layer {

constexpr size_t MAX_BACKTRACE_FRAMES = 64;
constexpr size_t MAX_BACKTRACE_FRAMES = 48;

struct StackTrace {
std::vector<BacktraceInfo> stack;
std::vector<BacktraceFrame> stack;

void print() const;
};

StackTrace GetCurrentBacktrace();

char **GetBacktraceSymbols(const std::vector<BacktraceFrame> &BacktraceFrames);

} // namespace ur_sanitizer_layer

0 comments on commit 6f67b1c

Please sign in to comment.