Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / sanitizer_common / symbolizer / sanitizer_symbolize.cpp
blob4bdf75332bf33aa336a7f2591940bc20e226b6ef
1 //===-- sanitizer_symbolize.cpp ---------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implementation of weak hooks from sanitizer_symbolizer_posix_libcdep.cpp.
11 //===----------------------------------------------------------------------===//
13 #include <inttypes.h>
14 #include <stdio.h>
16 #include <string>
18 #include "llvm/DebugInfo/Symbolize/DIPrinter.h"
19 #include "llvm/DebugInfo/Symbolize/Symbolize.h"
20 #include "llvm/Demangle/Demangle.h"
22 static llvm::symbolize::LLVMSymbolizer *Symbolizer = nullptr;
23 static bool Demangle = true;
24 static bool InlineFrames = true;
26 static llvm::symbolize::LLVMSymbolizer *getDefaultSymbolizer() {
27 if (Symbolizer)
28 return Symbolizer;
29 llvm::symbolize::LLVMSymbolizer::Options Opts;
30 Opts.Demangle = Demangle;
31 Opts.UntagAddresses = true;
32 Symbolizer = new llvm::symbolize::LLVMSymbolizer(Opts);
33 return Symbolizer;
36 static llvm::symbolize::PrinterConfig getDefaultPrinterConfig() {
37 llvm::symbolize::PrinterConfig Config;
38 Config.Pretty = false;
39 Config.Verbose = false;
40 Config.PrintFunctions = true;
41 Config.PrintAddress = false;
42 Config.SourceContextLines = 0;
43 return Config;
46 static llvm::symbolize::ErrorHandler symbolize_error_handler(
47 llvm::raw_string_ostream &OS) {
48 return
49 [&](const llvm::ErrorInfoBase &ErrorInfo, llvm::StringRef ErrorBanner) {
50 OS << ErrorBanner;
51 ErrorInfo.log(OS);
52 OS << '\n';
56 namespace __sanitizer {
57 int internal_snprintf(char *buffer, uintptr_t length, const char *format, ...);
58 } // namespace __sanitizer
60 extern "C" {
62 typedef uint64_t u64;
64 bool __sanitizer_symbolize_code(const char *ModuleName, uint64_t ModuleOffset,
65 char *Buffer, int MaxLength) {
66 std::string Result;
68 llvm::raw_string_ostream OS(Result);
69 llvm::symbolize::PrinterConfig Config = getDefaultPrinterConfig();
70 llvm::symbolize::Request Request{ModuleName, ModuleOffset};
71 auto Printer = std::make_unique<llvm::symbolize::LLVMPrinter>(
72 OS, symbolize_error_handler(OS), Config);
74 // TODO: it is neccessary to set proper SectionIndex here.
75 // object::SectionedAddress::UndefSection works for only absolute addresses.
76 if (InlineFrames) {
77 auto ResOrErr = getDefaultSymbolizer()->symbolizeInlinedCode(
78 ModuleName,
79 {ModuleOffset, llvm::object::SectionedAddress::UndefSection});
80 if (!ResOrErr)
81 return false;
82 Printer->print(Request, ResOrErr.get());
83 } else {
84 auto ResOrErr = getDefaultSymbolizer()->symbolizeCode(
85 ModuleName,
86 {ModuleOffset, llvm::object::SectionedAddress::UndefSection});
87 if (!ResOrErr)
88 return false;
89 Printer->print(Request, ResOrErr.get());
92 return __sanitizer::internal_snprintf(Buffer, MaxLength, "%s",
93 Result.c_str()) < MaxLength;
96 bool __sanitizer_symbolize_data(const char *ModuleName, uint64_t ModuleOffset,
97 char *Buffer, int MaxLength) {
98 std::string Result;
100 llvm::symbolize::PrinterConfig Config = getDefaultPrinterConfig();
101 llvm::raw_string_ostream OS(Result);
102 llvm::symbolize::Request Request{ModuleName, ModuleOffset};
103 auto Printer = std::make_unique<llvm::symbolize::LLVMPrinter>(
104 OS, symbolize_error_handler(OS), Config);
106 // TODO: it is neccessary to set proper SectionIndex here.
107 // object::SectionedAddress::UndefSection works for only absolute addresses.
108 auto ResOrErr = getDefaultSymbolizer()->symbolizeData(
109 ModuleName,
110 {ModuleOffset, llvm::object::SectionedAddress::UndefSection});
111 if (!ResOrErr)
112 return false;
113 Printer->print(Request, ResOrErr.get());
115 return __sanitizer::internal_snprintf(Buffer, MaxLength, "%s",
116 Result.c_str()) < MaxLength;
119 bool __sanitizer_symbolize_frame(const char *ModuleName, uint64_t ModuleOffset,
120 char *Buffer, int MaxLength) {
121 std::string Result;
123 llvm::symbolize::PrinterConfig Config = getDefaultPrinterConfig();
124 llvm::raw_string_ostream OS(Result);
125 llvm::symbolize::Request Request{ModuleName, ModuleOffset};
126 auto Printer = std::make_unique<llvm::symbolize::LLVMPrinter>(
127 OS, symbolize_error_handler(OS), Config);
129 // TODO: it is neccessary to set proper SectionIndex here.
130 // object::SectionedAddress::UndefSection works for only absolute addresses.
131 auto ResOrErr = getDefaultSymbolizer()->symbolizeFrame(
132 ModuleName,
133 {ModuleOffset, llvm::object::SectionedAddress::UndefSection});
134 if (!ResOrErr)
135 return false;
136 Printer->print(Request, ResOrErr.get());
138 return __sanitizer::internal_snprintf(Buffer, MaxLength, "%s",
139 Result.c_str()) < MaxLength;
142 void __sanitizer_symbolize_flush() {
143 if (Symbolizer)
144 Symbolizer->flush();
147 bool __sanitizer_symbolize_demangle(const char *Name, char *Buffer,
148 int MaxLength) {
149 std::string Result;
150 if (!llvm::nonMicrosoftDemangle(Name, Result))
151 return false;
152 return __sanitizer::internal_snprintf(Buffer, MaxLength, "%s",
153 Result.c_str()) < MaxLength;
156 bool __sanitizer_symbolize_set_demangle(bool Value) {
157 // Must be called before LLVMSymbolizer created.
158 if (Symbolizer)
159 return false;
160 Demangle = Value;
161 return true;
164 bool __sanitizer_symbolize_set_inline_frames(bool Value) {
165 InlineFrames = Value;
166 return true;
169 // Override __cxa_atexit and ignore callbacks.
170 // This prevents crashes in a configuration when the symbolizer
171 // is built into sanitizer runtime and consequently into the test process.
172 // LLVM libraries have some global objects destroyed during exit,
173 // so if the test process triggers any bugs after that, the symbolizer crashes.
174 // An example stack trace of such crash:
176 // #1 __cxa_throw
177 // #2 std::__u::__throw_system_error
178 // #3 std::__u::recursive_mutex::lock
179 // #4 __sanitizer_llvm::ManagedStaticBase::RegisterManagedStatic
180 // #5 __sanitizer_llvm::errorToErrorCode
181 // #6 __sanitizer_llvm::getFileAux
182 // #7 __sanitizer_llvm::MemoryBuffer::getFileOrSTDIN
183 // #10 __sanitizer_llvm::symbolize::LLVMSymbolizer::getOrCreateModuleInfo
184 // #13 __sanitizer::Symbolizer::SymbolizeData
185 // #14 __tsan::SymbolizeData
186 // #16 __tsan::ReportRace
187 // #18 __tsan_write4
188 // #19 race() () at test/tsan/atexit4.cpp
189 // #20 cxa_at_exit_wrapper
190 // #21 __cxa_finalize
191 // #22 __do_fini
193 // For the standalone llvm-symbolizer this does not hurt,
194 // we just don't destroy few global objects on exit.
195 int __cxa_atexit(void (*f)(void *a), void *arg, void *dso) { return 0; }
197 } // extern "C"