PR target/37072
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_symbolizer.h
blob2ff99372da95214cfb64b10a68179698c2d4afda
1 //===-- sanitizer_symbolizer.h ----------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // Symbolizer is used by sanitizers to map instruction address to a location in
9 // source code at run-time. Symbolizer either uses __sanitizer_symbolize_*
10 // defined in the program, or (if they are missing) tries to find and
11 // launch "llvm-symbolizer" commandline tool in a separate process and
12 // communicate with it.
14 // Generally we should try to avoid calling system library functions during
15 // symbolization (and use their replacements from sanitizer_libc.h instead).
16 //===----------------------------------------------------------------------===//
17 #ifndef SANITIZER_SYMBOLIZER_H
18 #define SANITIZER_SYMBOLIZER_H
20 #include "sanitizer_allocator_internal.h"
21 #include "sanitizer_internal_defs.h"
22 #include "sanitizer_libc.h"
24 namespace __sanitizer {
26 struct AddressInfo {
27 uptr address;
29 char *module;
30 uptr module_offset;
32 static const uptr kUnknown = ~(uptr)0;
33 char *function;
34 uptr function_offset;
36 char *file;
37 int line;
38 int column;
40 AddressInfo() {
41 internal_memset(this, 0, sizeof(AddressInfo));
42 function_offset = kUnknown;
45 // Deletes all strings and resets all fields.
46 void Clear() {
47 InternalFree(module);
48 InternalFree(function);
49 InternalFree(file);
50 internal_memset(this, 0, sizeof(AddressInfo));
51 function_offset = kUnknown;
54 void FillAddressAndModuleInfo(uptr addr, const char *mod_name,
55 uptr mod_offset) {
56 address = addr;
57 module = internal_strdup(mod_name);
58 module_offset = mod_offset;
62 // For now, DataInfo is used to describe global variable.
63 struct DataInfo {
64 char *module;
65 uptr module_offset;
66 char *name;
67 uptr start;
68 uptr size;
70 DataInfo() {
71 internal_memset(this, 0, sizeof(DataInfo));
74 void Clear() {
75 InternalFree(module);
76 InternalFree(name);
77 internal_memset(this, 0, sizeof(DataInfo));
81 class Symbolizer {
82 public:
83 /// Initialize and return platform-specific implementation of symbolizer
84 /// (if it wasn't already initialized).
85 static Symbolizer *GetOrInit();
86 // Fills at most "max_frames" elements of "frames" with descriptions
87 // for a given address (in all inlined functions). Returns the number
88 // of descriptions actually filled.
89 virtual uptr SymbolizePC(uptr address, AddressInfo *frames, uptr max_frames) {
90 return 0;
92 virtual bool SymbolizeData(uptr address, DataInfo *info) {
93 return false;
95 virtual bool GetModuleNameAndOffsetForPC(uptr pc, const char **module_name,
96 uptr *module_address) {
97 return false;
99 virtual bool CanReturnFileLineInfo() {
100 return false;
102 // Release internal caches (if any).
103 virtual void Flush() {}
104 // Attempts to demangle the provided C++ mangled name.
105 virtual const char *Demangle(const char *name) {
106 return name;
108 virtual void PrepareForSandboxing() {}
110 // Allow user to install hooks that would be called before/after Symbolizer
111 // does the actual file/line info fetching. Specific sanitizers may need this
112 // to distinguish system library calls made in user code from calls made
113 // during in-process symbolization.
114 typedef void (*StartSymbolizationHook)();
115 typedef void (*EndSymbolizationHook)();
116 // May be called at most once.
117 void AddHooks(StartSymbolizationHook start_hook,
118 EndSymbolizationHook end_hook);
120 private:
121 /// Platform-specific function for creating a Symbolizer object.
122 static Symbolizer *PlatformInit();
123 /// Initialize the symbolizer in a disabled state. Not thread safe.
124 static Symbolizer *Disable();
126 static Symbolizer *symbolizer_;
127 static StaticSpinMutex init_mu_;
129 protected:
130 Symbolizer();
132 static LowLevelAllocator symbolizer_allocator_;
134 StartSymbolizationHook start_hook_;
135 EndSymbolizationHook end_hook_;
136 class SymbolizerScope {
137 public:
138 explicit SymbolizerScope(const Symbolizer *sym);
139 ~SymbolizerScope();
140 private:
141 const Symbolizer *sym_;
145 } // namespace __sanitizer
147 #endif // SANITIZER_SYMBOLIZER_H