Skip to content

Commit dac2a0f

Browse files
zhaomaosuomarahmed1111
authored andcommitted
Do symolize when it's really needed
1 parent c066e6a commit dac2a0f

File tree

3 files changed

+78
-61
lines changed

3 files changed

+78
-61
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ option(UR_USE_MSAN "enable MemorySanitizer" OFF)
4242
option(UR_USE_TSAN "enable ThreadSanitizer" OFF)
4343
option(UR_ENABLE_TRACING "enable api tracing through xpti" OFF)
4444
option(UR_ENABLE_SANITIZER "enable device sanitizer" ON)
45-
option(UR_ENABLE_SYMBOLIZER "enable Symoblizer for Sanitizer" OFF)
45+
option(UR_ENABLE_SYMBOLIZER "enable symoblizer for sanitizer" OFF)
4646
option(UMF_BUILD_SHARED_LIBRARY "Build UMF as shared library" ON)
4747
option(UMF_ENABLE_POOL_TRACKING "Build UMF with pool tracking" ON)
4848
option(UR_BUILD_ADAPTER_L0 "Build the Level-Zero adapter" OFF)

source/loader/layers/sanitizer/linux/backtrace.cpp

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,8 @@
1212
#include <execinfo.h>
1313
#include <string>
1414

15-
extern "C" {
16-
17-
__attribute__((weak)) bool SymbolizeCode(const std::string ModuleName,
18-
uint64_t ModuleOffset,
19-
std::string &Result);
20-
}
21-
2215
namespace ur_sanitizer_layer {
2316

24-
std::string ExtractModuleName(const char *Symbol) {
25-
auto s1 = std::strrchr(Symbol, '(');
26-
return std::string(Symbol, s1 - Symbol);
27-
}
28-
29-
// Parse symbolizer output in the following formats:
30-
// <function_name>
31-
// <file_name>:<line_number>[:<column_number>]
32-
SourceInfo ParseSymbolizerOutput(std::string Output) {
33-
SourceInfo Info;
34-
// Parse function name
35-
size_t End = Output.find_first_of('\n');
36-
assert(End != std::string::npos);
37-
Info.function = Output.substr(0, End);
38-
// Parse file name
39-
size_t Start = End + 1;
40-
End = Output.find_first_of(':', Start);
41-
assert(End != std::string::npos);
42-
Info.file = Output.substr(Start, End - Start);
43-
// Parse line number
44-
Start = End + 1;
45-
End = Output.find_first_of(":\n", Start);
46-
assert(End != std::string::npos);
47-
Info.line = std::stoi(Output.substr(Start, End - Start));
48-
// Parse column number if exists
49-
if (Output[End] == ':') {
50-
Start = End + 1;
51-
End = Output.find_first_of("\n", Start);
52-
assert(End != std::string::npos);
53-
Info.column = std::stoi(Output.substr(Start, End - Start));
54-
}
55-
56-
return Info;
57-
}
58-
5917
StackTrace GetCurrentBacktrace() {
6018
void *Frames[MAX_BACKTRACE_FRAMES];
6119
int FrameCount = backtrace(Frames, MAX_BACKTRACE_FRAMES);
@@ -67,24 +25,8 @@ StackTrace GetCurrentBacktrace() {
6725

6826
StackTrace Stack;
6927
for (int i = 0; i < FrameCount; i++) {
70-
if (SymbolizeCode != nullptr) {
71-
std::string Result;
72-
std::string ModuleName = ExtractModuleName(Symbols[i]);
73-
if (SymbolizeCode(ModuleName, (uint64_t)Frames[i], Result)) {
74-
SourceInfo SrcInfo = ParseSymbolizerOutput(Result);
75-
std::ostringstream OS;
76-
if (SrcInfo.file != "??") {
77-
OS << "in " << SrcInfo.function << " " << SrcInfo.file
78-
<< ":" << SrcInfo.line << ":" << SrcInfo.column;
79-
} else {
80-
OS << "in " << SrcInfo.function << " (" << ModuleName << "+"
81-
<< Frames[i] << ")";
82-
}
83-
Stack.stack.emplace_back(OS.str());
84-
continue;
85-
}
86-
}
87-
Stack.stack.emplace_back(Symbols[i]);
28+
BacktraceInfo addr_info(Symbols[i]);
29+
Stack.stack.emplace_back(addr_info);
8830
}
8931
free(Symbols);
9032

source/loader/layers/sanitizer/stacktrace.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
#include "stacktrace.hpp"
1414
#include "ur_sanitizer_layer.hpp"
1515

16+
extern "C" {
17+
18+
__attribute__((weak)) bool SymbolizeCode(const std::string ModuleName,
19+
uint64_t ModuleOffset,
20+
std::string &Result);
21+
}
22+
1623
namespace ur_sanitizer_layer {
1724

1825
namespace {
@@ -21,6 +28,54 @@ bool Contains(const std::string &s, const char *p) {
2128
return s.find(p) != std::string::npos;
2229
}
2330

31+
// Parse back trace information in the following formats:
32+
// <module_name>([function_name]+function_offset) [offset]
33+
void ParseBacktraceInfo(BacktraceInfo BI, std::string &ModuleName,
34+
uptr &Offset) {
35+
// Parse module name
36+
size_t End = BI.find_first_of('(');
37+
assert(End != std::string::npos);
38+
ModuleName = BI.substr(0, End);
39+
// Parse offset
40+
size_t Start = BI.find_first_of('[');
41+
assert(Start != std::string::npos);
42+
Start = BI.substr(Start + 1, 2) == "0x" ? Start + 3 : Start + 1;
43+
End = BI.find_first_of(']');
44+
assert(End != std::string::npos);
45+
Offset = std::stoull(BI.substr(Start, End), nullptr, 16);
46+
return;
47+
}
48+
49+
// Parse symbolizer output in the following formats:
50+
// <function_name>
51+
// <file_name>:<line_number>[:<column_number>]
52+
SourceInfo ParseSymbolizerOutput(std::string Output) {
53+
SourceInfo Info;
54+
// Parse function name
55+
size_t End = Output.find_first_of('\n');
56+
assert(End != std::string::npos);
57+
Info.function = Output.substr(0, End);
58+
// Parse file name
59+
size_t Start = End + 1;
60+
End = Output.find_first_of(':', Start);
61+
assert(End != std::string::npos);
62+
Info.file = Output.substr(Start, End - Start);
63+
// Parse line number
64+
Start = End + 1;
65+
End = Output.find_first_of(":\n", Start);
66+
assert(End != std::string::npos);
67+
Info.line = std::stoi(Output.substr(Start, End - Start));
68+
// Parse column number if exists
69+
if (Output[End] == ':') {
70+
Start = End + 1;
71+
End = Output.find_first_of("\n", Start);
72+
assert(End != std::string::npos);
73+
Info.column = std::stoi(Output.substr(Start, End - Start));
74+
}
75+
76+
return Info;
77+
}
78+
2479
} // namespace
2580

2681
void StackTrace::print() const {
@@ -37,6 +92,26 @@ void StackTrace::print() const {
3792
Contains(BI, "libur_loader.so")) {
3893
continue;
3994
}
95+
96+
if (&SymbolizeCode != nullptr) {
97+
std::string Result;
98+
std::string ModuleName;
99+
uptr Offset;
100+
ParseBacktraceInfo(BI, ModuleName, Offset);
101+
if (SymbolizeCode(ModuleName, Offset, Result)) {
102+
SourceInfo SrcInfo = ParseSymbolizerOutput(Result);
103+
if (SrcInfo.file != "??") {
104+
getContext()->logger.always(" #{} in {} {}:{}:{}", index,
105+
SrcInfo.function, SrcInfo.file,
106+
SrcInfo.line, SrcInfo.column);
107+
} else {
108+
getContext()->logger.always(" #{} in {} ({}+{})", index,
109+
SrcInfo.function, ModuleName,
110+
(void *)Offset);
111+
}
112+
continue;
113+
}
114+
}
40115
getContext()->logger.always(" #{} {}", index, BI);
41116
++index;
42117
}

0 commit comments

Comments
 (0)