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
;
73 /// Returns platform-specific implementation of Symbolizer. The symbolizer
74 /// must be initialized (with init or disable) before calling this function.
75 static Symbolizer
*Get();
76 /// Returns platform-specific implementation of Symbolizer, or null if not
78 static Symbolizer
*GetOrNull();
79 /// Returns platform-specific implementation of Symbolizer. Will
80 /// automatically initialize symbolizer as if by calling Init(0) if needed.
81 static Symbolizer
*GetOrInit();
82 /// Initialize and return the symbolizer, given an optional path to an
83 /// external symbolizer. The path argument is only required for legacy
84 /// reasons as this function will check $PATH for an external symbolizer. Not
86 static Symbolizer
*Init(const char* path_to_external
= 0);
87 // Fills at most "max_frames" elements of "frames" with descriptions
88 // for a given address (in all inlined functions). Returns the number
89 // of descriptions actually filled.
90 virtual uptr
SymbolizePC(uptr address
, AddressInfo
*frames
, uptr max_frames
) {
93 virtual bool SymbolizeData(uptr address
, DataInfo
*info
) {
96 virtual bool GetModuleNameAndOffsetForPC(uptr pc
, const char **module_name
,
97 uptr
*module_address
) {
100 virtual bool CanReturnFileLineInfo() {
103 // Release internal caches (if any).
104 virtual void Flush() {}
105 // Attempts to demangle the provided C++ mangled name.
106 virtual const char *Demangle(const char *name
) {
109 virtual void PrepareForSandboxing() {}
111 // Allow user to install hooks that would be called before/after Symbolizer
112 // does the actual file/line info fetching. Specific sanitizers may need this
113 // to distinguish system library calls made in user code from calls made
114 // during in-process symbolization.
115 typedef void (*StartSymbolizationHook
)();
116 typedef void (*EndSymbolizationHook
)();
117 // May be called at most once.
118 void AddHooks(StartSymbolizationHook start_hook
,
119 EndSymbolizationHook end_hook
);
122 /// Platform-specific function for creating a Symbolizer object.
123 static Symbolizer
*PlatformInit(const char *path_to_external
);
124 /// Create a symbolizer and store it to symbolizer_ without checking if one
125 /// already exists. Not thread safe.
126 static Symbolizer
*CreateAndStore(const char *path_to_external
);
127 /// Initialize the symbolizer in a disabled state. Not thread safe.
128 static Symbolizer
*Disable();
130 static Symbolizer
*symbolizer_
;
131 static StaticSpinMutex init_mu_
;
136 static LowLevelAllocator symbolizer_allocator_
;
138 StartSymbolizationHook start_hook_
;
139 EndSymbolizationHook end_hook_
;
140 class SymbolizerScope
{
142 explicit SymbolizerScope(const Symbolizer
*sym
);
145 const Symbolizer
*sym_
;
149 } // namespace __sanitizer
151 #endif // SANITIZER_SYMBOLIZER_H