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_common.h"
21 #include "sanitizer_mutex.h"
23 namespace __sanitizer
{
26 // Owns all the string members. Storage for them is
27 // (de)allocated using sanitizer internal allocator.
32 ModuleArch module_arch
;
34 static const uptr kUnknown
= ~(uptr
)0;
43 // Deletes all strings and resets all fields.
45 void FillModuleInfo(const char *mod_name
, uptr mod_offset
, ModuleArch arch
);
48 // Linked list of symbolized frames (each frame is described by AddressInfo).
49 struct SymbolizedStack
{
50 SymbolizedStack
*next
;
52 static SymbolizedStack
*New(uptr addr
);
53 // Deletes current, and all subsequent frames in the linked list.
54 // The object cannot be accessed after the call to this function.
61 // For now, DataInfo is used to describe global variable.
63 // Owns all the string members. Storage for them is
64 // (de)allocated using sanitizer internal allocator.
67 ModuleArch module_arch
;
81 class Symbolizer final
{
83 /// Initialize and return platform-specific implementation of symbolizer
84 /// (if it wasn't already initialized).
85 static Symbolizer
*GetOrInit();
86 static void LateInitialize();
87 // Returns a list of symbolized frames for a given address (containing
88 // all inlined functions, if necessary).
89 SymbolizedStack
*SymbolizePC(uptr address
);
90 bool SymbolizeData(uptr address
, DataInfo
*info
);
92 // The module names Symbolizer returns are stable and unique for every given
93 // module. It is safe to store and compare them as pointers.
94 bool GetModuleNameAndOffsetForPC(uptr pc
, const char **module_name
,
95 uptr
*module_address
);
96 const char *GetModuleNameForPc(uptr pc
) {
97 const char *module_name
= nullptr;
99 if (GetModuleNameAndOffsetForPC(pc
, &module_name
, &unused
))
104 // Release internal caches (if any).
106 // Attempts to demangle the provided C++ mangled name.
107 const char *Demangle(const char *name
);
109 // Allow user to install hooks that would be called before/after Symbolizer
110 // does the actual file/line info fetching. Specific sanitizers may need this
111 // to distinguish system library calls made in user code from calls made
112 // during in-process symbolization.
113 typedef void (*StartSymbolizationHook
)();
114 typedef void (*EndSymbolizationHook
)();
115 // May be called at most once.
116 void AddHooks(StartSymbolizationHook start_hook
,
117 EndSymbolizationHook end_hook
);
119 void RefreshModules();
120 const LoadedModule
*FindModuleForAddress(uptr address
);
122 void InvalidateModuleList();
125 // GetModuleNameAndOffsetForPC has to return a string to the caller.
126 // Since the corresponding module might get unloaded later, we should create
127 // our owned copies of the strings that we can safely return.
128 // ModuleNameOwner does not provide any synchronization, thus calls to
129 // its method should be protected by |mu_|.
130 class ModuleNameOwner
{
132 explicit ModuleNameOwner(BlockingMutex
*synchronized_by
)
133 : last_match_(nullptr), mu_(synchronized_by
) {
134 storage_
.reserve(kInitialCapacity
);
136 const char *GetOwnedCopy(const char *str
);
139 static const uptr kInitialCapacity
= 1000;
140 InternalMmapVector
<const char*> storage_
;
141 const char *last_match_
;
146 /// Platform-specific function for creating a Symbolizer object.
147 static Symbolizer
*PlatformInit();
149 bool FindModuleNameAndOffsetForAddress(uptr address
, const char **module_name
,
151 ModuleArch
*module_arch
);
152 ListOfModules modules_
;
153 ListOfModules fallback_modules_
;
154 // If stale, need to reload the modules before looking up addresses.
157 // Platform-specific default demangler, must not return nullptr.
158 const char *PlatformDemangle(const char *name
);
160 static Symbolizer
*symbolizer_
;
161 static StaticSpinMutex init_mu_
;
163 // Mutex locked from public methods of |Symbolizer|, so that the internals
164 // (including individual symbolizer tools and platform-specific methods) are
165 // always synchronized.
168 IntrusiveList
<SymbolizerTool
> tools_
;
170 explicit Symbolizer(IntrusiveList
<SymbolizerTool
> tools
);
172 static LowLevelAllocator symbolizer_allocator_
;
174 StartSymbolizationHook start_hook_
;
175 EndSymbolizationHook end_hook_
;
176 class SymbolizerScope
{
178 explicit SymbolizerScope(const Symbolizer
*sym
);
181 const Symbolizer
*sym_
;
185 #ifdef SANITIZER_WINDOWS
186 void InitializeDbgHelpIfNeeded();
189 } // namespace __sanitizer
191 #endif // SANITIZER_SYMBOLIZER_H