1 //===-- sanitizer_symbolize.cc ----------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Implementation of weak hooks from sanitizer_symbolizer_posix_libcdep.cc.
11 //===----------------------------------------------------------------------===//
16 #include "llvm/DebugInfo/Symbolize/DIPrinter.h"
17 #include "llvm/DebugInfo/Symbolize/Symbolize.h"
19 static llvm::symbolize::LLVMSymbolizer *getDefaultSymbolizer() {
20 static llvm::symbolize::LLVMSymbolizer *DefaultSymbolizer =
21 new llvm::symbolize::LLVMSymbolizer();
22 return DefaultSymbolizer;
25 namespace __sanitizer {
26 int internal_snprintf(char *buffer, unsigned long length, const char *format,
28 } // namespace __sanitizer
34 bool __sanitizer_symbolize_code(const char *ModuleName, uint64_t ModuleOffset,
35 char *Buffer, int MaxLength) {
38 llvm::raw_string_ostream OS(Result);
39 llvm::symbolize::DIPrinter Printer(OS);
40 // TODO: it is neccessary to set proper SectionIndex here.
41 // object::SectionedAddress::UndefSection works for only absolute addresses.
42 auto ResOrErr = getDefaultSymbolizer()->symbolizeInlinedCode(
44 {ModuleOffset, llvm::object::SectionedAddress::UndefSection});
45 Printer << (ResOrErr ? ResOrErr.get() : llvm::DIInliningInfo());
47 return __sanitizer::internal_snprintf(Buffer, MaxLength, "%s",
48 Result.c_str()) < MaxLength;
51 bool __sanitizer_symbolize_data(const char *ModuleName, uint64_t ModuleOffset,
52 char *Buffer, int MaxLength) {
55 llvm::raw_string_ostream OS(Result);
56 llvm::symbolize::DIPrinter Printer(OS);
57 // TODO: it is neccessary to set proper SectionIndex here.
58 // object::SectionedAddress::UndefSection works for only absolute addresses.
59 auto ResOrErr = getDefaultSymbolizer()->symbolizeData(
61 {ModuleOffset, llvm::object::SectionedAddress::UndefSection});
62 Printer << (ResOrErr ? ResOrErr.get() : llvm::DIGlobal());
64 return __sanitizer::internal_snprintf(Buffer, MaxLength, "%s",
65 Result.c_str()) < MaxLength;
68 void __sanitizer_symbolize_flush() { getDefaultSymbolizer()->flush(); }
70 int __sanitizer_symbolize_demangle(const char *Name, char *Buffer,
73 llvm::symbolize::LLVMSymbolizer::DemangleName(Name, nullptr);
74 return __sanitizer::internal_snprintf(Buffer, MaxLength, "%s",
75 Result.c_str()) < MaxLength
76 ? static_cast<int>(Result.size() + 1)