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
);
108 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 void RefreshModules();
121 const LoadedModule
*FindModuleForAddress(uptr address
);
123 void InvalidateModuleList();
126 // GetModuleNameAndOffsetForPC has to return a string to the caller.
127 // Since the corresponding module might get unloaded later, we should create
128 // our owned copies of the strings that we can safely return.
129 // ModuleNameOwner does not provide any synchronization, thus calls to
130 // its method should be protected by |mu_|.
131 class ModuleNameOwner
{
133 explicit ModuleNameOwner(BlockingMutex
*synchronized_by
)
134 : storage_(kInitialCapacity
), last_match_(nullptr),
135 mu_(synchronized_by
) {}
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
);
159 void PlatformPrepareForSandboxing();
161 static Symbolizer
*symbolizer_
;
162 static StaticSpinMutex init_mu_
;
164 // Mutex locked from public methods of |Symbolizer|, so that the internals
165 // (including individual symbolizer tools and platform-specific methods) are
166 // always synchronized.
169 IntrusiveList
<SymbolizerTool
> tools_
;
171 explicit Symbolizer(IntrusiveList
<SymbolizerTool
> tools
);
173 static LowLevelAllocator symbolizer_allocator_
;
175 StartSymbolizationHook start_hook_
;
176 EndSymbolizationHook end_hook_
;
177 class SymbolizerScope
{
179 explicit SymbolizerScope(const Symbolizer
*sym
);
182 const Symbolizer
*sym_
;
186 #ifdef SANITIZER_WINDOWS
187 void InitializeDbgHelpIfNeeded();
190 } // namespace __sanitizer
192 #endif // SANITIZER_SYMBOLIZER_H