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
{
36 internal_memset(this, 0, sizeof(AddressInfo
));
39 // Deletes all strings and sets all fields to zero.
42 InternalFree(function
);
44 internal_memset(this, 0, sizeof(AddressInfo
));
47 void FillAddressAndModuleInfo(uptr addr
, const char *mod_name
,
50 module
= internal_strdup(mod_name
);
51 module_offset
= mod_offset
;
66 /// Returns platform-specific implementation of Symbolizer. The symbolizer
67 /// must be initialized (with init or disable) before calling this function.
68 static Symbolizer
*Get();
69 /// Returns platform-specific implementation of Symbolizer, or null if not
71 static Symbolizer
*GetOrNull();
72 /// Returns platform-specific implementation of Symbolizer. Will
73 /// automatically initialize symbolizer as if by calling Init(0) if needed.
74 static Symbolizer
*GetOrInit();
75 /// Initialize and return the symbolizer, given an optional path to an
76 /// external symbolizer. The path argument is only required for legacy
77 /// reasons as this function will check $PATH for an external symbolizer. Not
79 static Symbolizer
*Init(const char* path_to_external
= 0);
80 /// Initialize the symbolizer in a disabled state. Not thread safe.
81 static Symbolizer
*Disable();
82 // Fills at most "max_frames" elements of "frames" with descriptions
83 // for a given address (in all inlined functions). Returns the number
84 // of descriptions actually filled.
85 virtual uptr
SymbolizeCode(uptr address
, AddressInfo
*frames
,
89 virtual bool SymbolizeData(uptr address
, DataInfo
*info
) {
92 virtual bool IsAvailable() {
95 virtual bool IsExternalAvailable() {
98 // Release internal caches (if any).
99 virtual void Flush() {}
100 // Attempts to demangle the provided C++ mangled name.
101 virtual const char *Demangle(const char *name
) {
104 virtual void PrepareForSandboxing() {}
106 // Allow user to install hooks that would be called before/after Symbolizer
107 // does the actual file/line info fetching. Specific sanitizers may need this
108 // to distinguish system library calls made in user code from calls made
109 // during in-process symbolization.
110 typedef void (*StartSymbolizationHook
)();
111 typedef void (*EndSymbolizationHook
)();
112 // May be called at most once.
113 void AddHooks(StartSymbolizationHook start_hook
,
114 EndSymbolizationHook end_hook
);
117 /// Platform-specific function for creating a Symbolizer object.
118 static Symbolizer
*PlatformInit(const char *path_to_external
);
119 /// Create a symbolizer and store it to symbolizer_ without checking if one
120 /// already exists. Not thread safe.
121 static Symbolizer
*CreateAndStore(const char *path_to_external
);
123 static Symbolizer
*symbolizer_
;
124 static StaticSpinMutex init_mu_
;
129 static LowLevelAllocator symbolizer_allocator_
;
131 StartSymbolizationHook start_hook_
;
132 EndSymbolizationHook end_hook_
;
133 class SymbolizerScope
{
135 explicit SymbolizerScope(const Symbolizer
*sym
);
138 const Symbolizer
*sym_
;
142 } // namespace __sanitizer
144 #endif // SANITIZER_SYMBOLIZER_H