1 //===-- sanitizer_symbolizer.h ----------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
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
{
32 static const uptr kUnknown
= ~(uptr
)0;
41 internal_memset(this, 0, sizeof(AddressInfo
));
42 function_offset
= kUnknown
;
45 // Deletes all strings and resets all fields.
48 InternalFree(function
);
50 internal_memset(this, 0, sizeof(AddressInfo
));
51 function_offset
= kUnknown
;
54 void FillAddressAndModuleInfo(uptr addr
, const char *mod_name
,
57 module
= internal_strdup(mod_name
);
58 module_offset
= mod_offset
;
62 // For now, DataInfo is used to describe global variable.
71 internal_memset(this, 0, sizeof(DataInfo
));
77 internal_memset(this, 0, sizeof(DataInfo
));
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
) {
92 virtual bool SymbolizeData(uptr address
, DataInfo
*info
) {
95 virtual bool GetModuleNameAndOffsetForPC(uptr pc
, const char **module_name
,
96 uptr
*module_address
) {
99 virtual bool CanReturnFileLineInfo() {
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
) {
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
);
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_
;
132 static LowLevelAllocator symbolizer_allocator_
;
134 StartSymbolizationHook start_hook_
;
135 EndSymbolizationHook end_hook_
;
136 class SymbolizerScope
{
138 explicit SymbolizerScope(const Symbolizer
*sym
);
141 const Symbolizer
*sym_
;
145 } // namespace __sanitizer
147 #endif // SANITIZER_SYMBOLIZER_H