Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeviceSanitizer] Only try to get backtrace symbols when needed #2128

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 11 additions & 11 deletions source/loader/layers/sanitizer/linux/backtrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
namespace ur_sanitizer_layer {

StackTrace GetCurrentBacktrace() {
void *Frames[MAX_BACKTRACE_FRAMES];
BacktraceFrame 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 =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, we should be able to skip the stack buffer by writing to the vector directly:

StackTrace Stack;
Stack.stack.reserve(MAX_BACKTRACE_FRAMES);
int FrameCount = backtrace(Stack.stack.data(), MAX_BACKTRACE_FRAMES);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to delay merging because of this. If you want to change it, include it in another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'll do this later.

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
4 changes: 3 additions & 1 deletion source/loader/layers/sanitizer/stacktrace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ namespace ur_sanitizer_layer {
constexpr size_t MAX_BACKTRACE_FRAMES = 64;

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
Loading