]> git.ipfire.org Git - thirdparty/gcc.git/blob - libsanitizer/sanitizer_common/sanitizer_symbolizer.h
[libsanitizer merge from upstream r218156]
[thirdparty/gcc.git] / libsanitizer / sanitizer_common / sanitizer_symbolizer.h
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.
13 //
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
19
20 #include "sanitizer_allocator_internal.h"
21 #include "sanitizer_internal_defs.h"
22 #include "sanitizer_libc.h"
23
24 namespace __sanitizer {
25
26 struct AddressInfo {
27 uptr address;
28
29 char *module;
30 uptr module_offset;
31
32 static const uptr kUnknown = ~(uptr)0;
33 char *function;
34 uptr function_offset;
35
36 char *file;
37 int line;
38 int column;
39
40 AddressInfo() {
41 internal_memset(this, 0, sizeof(AddressInfo));
42 function_offset = kUnknown;
43 }
44
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;
52 }
53
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;
59 }
60 };
61
62 struct DataInfo {
63 uptr address;
64 char *module;
65 uptr module_offset;
66 char *name;
67 uptr start;
68 uptr size;
69 };
70
71 class Symbolizer {
72 public:
73 /// Initialize and return platform-specific implementation of symbolizer
74 /// (if it wasn't already initialized).
75 static Symbolizer *GetOrInit();
76 // Fills at most "max_frames" elements of "frames" with descriptions
77 // for a given address (in all inlined functions). Returns the number
78 // of descriptions actually filled.
79 virtual uptr SymbolizePC(uptr address, AddressInfo *frames, uptr max_frames) {
80 return 0;
81 }
82 virtual bool SymbolizeData(uptr address, DataInfo *info) {
83 return false;
84 }
85 virtual bool GetModuleNameAndOffsetForPC(uptr pc, const char **module_name,
86 uptr *module_address) {
87 return false;
88 }
89 virtual bool CanReturnFileLineInfo() {
90 return false;
91 }
92 // Release internal caches (if any).
93 virtual void Flush() {}
94 // Attempts to demangle the provided C++ mangled name.
95 virtual const char *Demangle(const char *name) {
96 return name;
97 }
98 virtual void PrepareForSandboxing() {}
99
100 // Allow user to install hooks that would be called before/after Symbolizer
101 // does the actual file/line info fetching. Specific sanitizers may need this
102 // to distinguish system library calls made in user code from calls made
103 // during in-process symbolization.
104 typedef void (*StartSymbolizationHook)();
105 typedef void (*EndSymbolizationHook)();
106 // May be called at most once.
107 void AddHooks(StartSymbolizationHook start_hook,
108 EndSymbolizationHook end_hook);
109
110 private:
111 /// Platform-specific function for creating a Symbolizer object.
112 static Symbolizer *PlatformInit();
113 /// Initialize the symbolizer in a disabled state. Not thread safe.
114 static Symbolizer *Disable();
115
116 static Symbolizer *symbolizer_;
117 static StaticSpinMutex init_mu_;
118
119 protected:
120 Symbolizer();
121
122 static LowLevelAllocator symbolizer_allocator_;
123
124 StartSymbolizationHook start_hook_;
125 EndSymbolizationHook end_hook_;
126 class SymbolizerScope {
127 public:
128 explicit SymbolizerScope(const Symbolizer *sym);
129 ~SymbolizerScope();
130 private:
131 const Symbolizer *sym_;
132 };
133 };
134
135 } // namespace __sanitizer
136
137 #endif // SANITIZER_SYMBOLIZER_H