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.
76 class Symbolizer final
{
78 /// Initialize and return platform-specific implementation of symbolizer
79 /// (if it wasn't already initialized).
80 static Symbolizer
*GetOrInit();
81 // Returns a list of symbolized frames for a given address (containing
82 // all inlined functions, if necessary).
83 SymbolizedStack
*SymbolizePC(uptr address
);
84 bool SymbolizeData(uptr address
, DataInfo
*info
);
86 // The module names Symbolizer returns are stable and unique for every given
87 // module. It is safe to store and compare them as pointers.
88 bool GetModuleNameAndOffsetForPC(uptr pc
, const char **module_name
,
89 uptr
*module_address
);
90 const char *GetModuleNameForPc(uptr pc
) {
91 const char *module_name
= nullptr;
93 if (GetModuleNameAndOffsetForPC(pc
, &module_name
, &unused
))
98 // Release internal caches (if any).
100 // Attempts to demangle the provided C++ mangled name.
101 const char *Demangle(const char *name
);
102 void PrepareForSandboxing();
104 // Allow user to install hooks that would be called before/after Symbolizer
105 // does the actual file/line info fetching. Specific sanitizers may need this
106 // to distinguish system library calls made in user code from calls made
107 // during in-process symbolization.
108 typedef void (*StartSymbolizationHook
)();
109 typedef void (*EndSymbolizationHook
)();
110 // May be called at most once.
111 void AddHooks(StartSymbolizationHook start_hook
,
112 EndSymbolizationHook end_hook
);
115 // GetModuleNameAndOffsetForPC has to return a string to the caller.
116 // Since the corresponding module might get unloaded later, we should create
117 // our owned copies of the strings that we can safely return.
118 // ModuleNameOwner does not provide any synchronization, thus calls to
119 // its method should be protected by |mu_|.
120 class ModuleNameOwner
{
122 explicit ModuleNameOwner(BlockingMutex
*synchronized_by
)
123 : storage_(kInitialCapacity
), last_match_(nullptr),
124 mu_(synchronized_by
) {}
125 const char *GetOwnedCopy(const char *str
);
128 static const uptr kInitialCapacity
= 1000;
129 InternalMmapVector
<const char*> storage_
;
130 const char *last_match_
;
135 /// Platform-specific function for creating a Symbolizer object.
136 static Symbolizer
*PlatformInit();
138 bool FindModuleNameAndOffsetForAddress(uptr address
, const char **module_name
,
139 uptr
*module_offset
);
140 LoadedModule
*FindModuleForAddress(uptr address
);
141 LoadedModule modules_
[kMaxNumberOfModules
];
143 // If stale, need to reload the modules before looking up addresses.
146 // Platform-specific default demangler, must not return nullptr.
147 const char *PlatformDemangle(const char *name
);
148 void PlatformPrepareForSandboxing();
150 static Symbolizer
*symbolizer_
;
151 static StaticSpinMutex init_mu_
;
153 // Mutex locked from public methods of |Symbolizer|, so that the internals
154 // (including individual symbolizer tools and platform-specific methods) are
155 // always synchronized.
158 typedef IntrusiveList
<SymbolizerTool
>::Iterator Iterator
;
159 IntrusiveList
<SymbolizerTool
> tools_
;
161 explicit Symbolizer(IntrusiveList
<SymbolizerTool
> tools
);
163 static LowLevelAllocator symbolizer_allocator_
;
165 StartSymbolizationHook start_hook_
;
166 EndSymbolizationHook end_hook_
;
167 class SymbolizerScope
{
169 explicit SymbolizerScope(const Symbolizer
*sym
);
172 const Symbolizer
*sym_
;
176 } // namespace __sanitizer
178 #endif // SANITIZER_SYMBOLIZER_H