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.
33 static const uptr kUnknown
= ~(uptr
)0;
42 // Deletes all strings and resets all fields.
44 void FillModuleInfo(const char *mod_name
, uptr mod_offset
);
47 // Linked list of symbolized frames (each frame is described by AddressInfo).
48 struct SymbolizedStack
{
49 SymbolizedStack
*next
;
51 static SymbolizedStack
*New(uptr addr
);
52 // Deletes current, and all subsequent frames in the linked list.
53 // The object cannot be accessed after the call to this function.
60 // For now, DataInfo is used to describe global variable.
62 // Owns all the string members. Storage for them is
63 // (de)allocated using sanitizer internal allocator.
78 class Symbolizer final
{
80 /// Initialize and return platform-specific implementation of symbolizer
81 /// (if it wasn't already initialized).
82 static Symbolizer
*GetOrInit();
83 static void LateInitialize();
84 // Returns a list of symbolized frames for a given address (containing
85 // all inlined functions, if necessary).
86 SymbolizedStack
*SymbolizePC(uptr address
);
87 bool SymbolizeData(uptr address
, DataInfo
*info
);
89 // The module names Symbolizer returns are stable and unique for every given
90 // module. It is safe to store and compare them as pointers.
91 bool GetModuleNameAndOffsetForPC(uptr pc
, const char **module_name
,
92 uptr
*module_address
);
93 const char *GetModuleNameForPc(uptr pc
) {
94 const char *module_name
= nullptr;
96 if (GetModuleNameAndOffsetForPC(pc
, &module_name
, &unused
))
101 // Release internal caches (if any).
103 // Attempts to demangle the provided C++ mangled name.
104 const char *Demangle(const char *name
);
105 void PrepareForSandboxing();
107 // Allow user to install hooks that would be called before/after Symbolizer
108 // does the actual file/line info fetching. Specific sanitizers may need this
109 // to distinguish system library calls made in user code from calls made
110 // during in-process symbolization.
111 typedef void (*StartSymbolizationHook
)();
112 typedef void (*EndSymbolizationHook
)();
113 // May be called at most once.
114 void AddHooks(StartSymbolizationHook start_hook
,
115 EndSymbolizationHook end_hook
);
117 const LoadedModule
*FindModuleForAddress(uptr address
);
120 // GetModuleNameAndOffsetForPC has to return a string to the caller.
121 // Since the corresponding module might get unloaded later, we should create
122 // our owned copies of the strings that we can safely return.
123 // ModuleNameOwner does not provide any synchronization, thus calls to
124 // its method should be protected by |mu_|.
125 class ModuleNameOwner
{
127 explicit ModuleNameOwner(BlockingMutex
*synchronized_by
)
128 : storage_(kInitialCapacity
), last_match_(nullptr),
129 mu_(synchronized_by
) {}
130 const char *GetOwnedCopy(const char *str
);
133 static const uptr kInitialCapacity
= 1000;
134 InternalMmapVector
<const char*> storage_
;
135 const char *last_match_
;
140 /// Platform-specific function for creating a Symbolizer object.
141 static Symbolizer
*PlatformInit();
143 bool FindModuleNameAndOffsetForAddress(uptr address
, const char **module_name
,
144 uptr
*module_offset
);
145 ListOfModules modules_
;
146 // If stale, need to reload the modules before looking up addresses.
149 // Platform-specific default demangler, must not return nullptr.
150 const char *PlatformDemangle(const char *name
);
151 void PlatformPrepareForSandboxing();
153 static Symbolizer
*symbolizer_
;
154 static StaticSpinMutex init_mu_
;
156 // Mutex locked from public methods of |Symbolizer|, so that the internals
157 // (including individual symbolizer tools and platform-specific methods) are
158 // always synchronized.
161 IntrusiveList
<SymbolizerTool
> tools_
;
163 explicit Symbolizer(IntrusiveList
<SymbolizerTool
> tools
);
165 static LowLevelAllocator symbolizer_allocator_
;
167 StartSymbolizationHook start_hook_
;
168 EndSymbolizationHook end_hook_
;
169 class SymbolizerScope
{
171 explicit SymbolizerScope(const Symbolizer
*sym
);
174 const Symbolizer
*sym_
;
178 #ifdef SANITIZER_WINDOWS
179 void InitializeDbgHelpIfNeeded();
182 } // namespace __sanitizer
184 #endif // SANITIZER_SYMBOLIZER_H