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+
1522namespace ur_sanitizer_layer {
1623
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+
1759StackTrace GetCurrentBacktrace () {
1860 void *Frames[MAX_BACKTRACE_FRAMES];
1961 int FrameCount = backtrace (Frames, MAX_BACKTRACE_FRAMES);
@@ -25,8 +67,24 @@ StackTrace GetCurrentBacktrace() {
2567
2668 StackTrace Stack;
2769 for (int i = 0 ; i < FrameCount; i++) {
28- BacktraceInfo addr_info (Symbols[i]);
29- Stack.stack .emplace_back (std::move (addr_info));
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]);
3088 }
3189 free (Symbols);
3290
0 commit comments