1 //===-- sanitizer_common.cpp ----------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is shared between sanitizers' run-time libraries.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_stacktrace_printer.h"
15 #include "sanitizer_file.h"
16 #include "sanitizer_flags.h"
17 #include "sanitizer_fuchsia.h"
19 namespace __sanitizer
{
21 StackTracePrinter
*StackTracePrinter::GetOrInit() {
22 static StackTracePrinter
*stacktrace_printer
;
23 static StaticSpinMutex init_mu
;
24 SpinMutexLock
l(&init_mu
);
25 if (stacktrace_printer
)
26 return stacktrace_printer
;
29 new (GetGlobalLowLevelAllocator()) FormattedStackTracePrinter();
31 CHECK(stacktrace_printer
);
32 return stacktrace_printer
;
35 const char *FormattedStackTracePrinter::StripFunctionName(
36 const char *function
) {
37 if (!common_flags()->demangle
)
41 auto try_strip
= [function
](const char *prefix
) -> const char * {
42 const uptr prefix_len
= internal_strlen(prefix
);
43 if (!internal_strncmp(function
, prefix
, prefix_len
))
44 return function
+ prefix_len
;
47 if (SANITIZER_APPLE
) {
48 if (const char *s
= try_strip("wrap_"))
50 } else if (SANITIZER_WINDOWS
) {
51 if (const char *s
= try_strip("__asan_wrap_"))
54 if (const char *s
= try_strip("___interceptor_"))
56 if (const char *s
= try_strip("__interceptor_"))
62 // sanitizer_symbolizer_markup.cpp implements these differently.
63 #if !SANITIZER_SYMBOLIZER_MARKUP
65 static const char *DemangleFunctionName(const char *function
) {
66 if (!common_flags()->demangle
)
71 // NetBSD uses indirection for old threading functions for historical reasons
72 // The mangled names are internal implementation detail and should not be
73 // exposed even in backtraces.
75 if (!internal_strcmp(function
, "__libc_mutex_init"))
76 return "pthread_mutex_init";
77 if (!internal_strcmp(function
, "__libc_mutex_lock"))
78 return "pthread_mutex_lock";
79 if (!internal_strcmp(function
, "__libc_mutex_trylock"))
80 return "pthread_mutex_trylock";
81 if (!internal_strcmp(function
, "__libc_mutex_unlock"))
82 return "pthread_mutex_unlock";
83 if (!internal_strcmp(function
, "__libc_mutex_destroy"))
84 return "pthread_mutex_destroy";
85 if (!internal_strcmp(function
, "__libc_mutexattr_init"))
86 return "pthread_mutexattr_init";
87 if (!internal_strcmp(function
, "__libc_mutexattr_settype"))
88 return "pthread_mutexattr_settype";
89 if (!internal_strcmp(function
, "__libc_mutexattr_destroy"))
90 return "pthread_mutexattr_destroy";
91 if (!internal_strcmp(function
, "__libc_cond_init"))
92 return "pthread_cond_init";
93 if (!internal_strcmp(function
, "__libc_cond_signal"))
94 return "pthread_cond_signal";
95 if (!internal_strcmp(function
, "__libc_cond_broadcast"))
96 return "pthread_cond_broadcast";
97 if (!internal_strcmp(function
, "__libc_cond_wait"))
98 return "pthread_cond_wait";
99 if (!internal_strcmp(function
, "__libc_cond_timedwait"))
100 return "pthread_cond_timedwait";
101 if (!internal_strcmp(function
, "__libc_cond_destroy"))
102 return "pthread_cond_destroy";
103 if (!internal_strcmp(function
, "__libc_rwlock_init"))
104 return "pthread_rwlock_init";
105 if (!internal_strcmp(function
, "__libc_rwlock_rdlock"))
106 return "pthread_rwlock_rdlock";
107 if (!internal_strcmp(function
, "__libc_rwlock_wrlock"))
108 return "pthread_rwlock_wrlock";
109 if (!internal_strcmp(function
, "__libc_rwlock_tryrdlock"))
110 return "pthread_rwlock_tryrdlock";
111 if (!internal_strcmp(function
, "__libc_rwlock_trywrlock"))
112 return "pthread_rwlock_trywrlock";
113 if (!internal_strcmp(function
, "__libc_rwlock_unlock"))
114 return "pthread_rwlock_unlock";
115 if (!internal_strcmp(function
, "__libc_rwlock_destroy"))
116 return "pthread_rwlock_destroy";
117 if (!internal_strcmp(function
, "__libc_thr_keycreate"))
118 return "pthread_key_create";
119 if (!internal_strcmp(function
, "__libc_thr_setspecific"))
120 return "pthread_setspecific";
121 if (!internal_strcmp(function
, "__libc_thr_getspecific"))
122 return "pthread_getspecific";
123 if (!internal_strcmp(function
, "__libc_thr_keydelete"))
124 return "pthread_key_delete";
125 if (!internal_strcmp(function
, "__libc_thr_once"))
126 return "pthread_once";
127 if (!internal_strcmp(function
, "__libc_thr_self"))
128 return "pthread_self";
129 if (!internal_strcmp(function
, "__libc_thr_exit"))
130 return "pthread_exit";
131 if (!internal_strcmp(function
, "__libc_thr_setcancelstate"))
132 return "pthread_setcancelstate";
133 if (!internal_strcmp(function
, "__libc_thr_equal"))
134 return "pthread_equal";
135 if (!internal_strcmp(function
, "__libc_thr_curcpu"))
136 return "pthread_curcpu_np";
137 if (!internal_strcmp(function
, "__libc_thr_sigsetmask"))
138 return "pthread_sigmask";
144 static void MaybeBuildIdToBuffer(const AddressInfo
&info
, bool PrefixSpace
,
145 InternalScopedString
*buffer
) {
146 if (info
.uuid_size
) {
148 buffer
->AppendF(" ");
149 buffer
->AppendF("(BuildId: ");
150 for (uptr i
= 0; i
< info
.uuid_size
; ++i
) {
151 buffer
->AppendF("%02x", info
.uuid
[i
]);
153 buffer
->AppendF(")");
157 static const char kDefaultFormat
[] = " #%n %p %F %L";
159 void FormattedStackTracePrinter::RenderFrame(InternalScopedString
*buffer
,
160 const char *format
, int frame_no
,
162 const AddressInfo
*info
,
164 const char *strip_path_prefix
) {
165 // info will be null in the case where symbolization is not needed for the
166 // given format. This ensures that the code below will get a hard failure
167 // rather than print incorrect information in case RenderNeedsSymbolization
168 // ever ends up out of sync with this function. If non-null, the addresses
170 CHECK(!info
|| address
== info
->address
);
171 if (0 == internal_strcmp(format
, "DEFAULT"))
172 format
= kDefaultFormat
;
173 for (const char *p
= format
; *p
!= '\0'; p
++) {
175 buffer
->AppendF("%c", *p
);
183 // Frame number and all fields of AddressInfo structure.
185 buffer
->AppendF("%u", frame_no
);
188 buffer
->AppendF("0x%zx", address
);
191 buffer
->AppendF("%s", StripPathPrefix(info
->module
, strip_path_prefix
));
194 buffer
->AppendF("0x%zx", info
->module_offset
);
197 MaybeBuildIdToBuffer(*info
, /*PrefixSpace=*/false, buffer
);
200 buffer
->AppendF("%s",
201 DemangleFunctionName(StripFunctionName(info
->function
)));
204 buffer
->AppendF("0x%zx", info
->function_offset
!= AddressInfo::kUnknown
205 ? info
->function_offset
209 buffer
->AppendF("%s", StripPathPrefix(info
->file
, strip_path_prefix
));
212 buffer
->AppendF("%d", info
->line
);
215 buffer
->AppendF("%d", info
->column
);
217 // Smarter special cases.
219 // Function name and offset, if file is unknown.
220 if (info
->function
) {
222 "in %s", DemangleFunctionName(StripFunctionName(info
->function
)));
223 if (!info
->file
&& info
->function_offset
!= AddressInfo::kUnknown
)
224 buffer
->AppendF("+0x%zx", info
->function_offset
);
228 // File/line information.
229 RenderSourceLocation(buffer
, info
->file
, info
->line
, info
->column
,
230 vs_style
, strip_path_prefix
);
233 // Source location, or module location.
235 RenderSourceLocation(buffer
, info
->file
, info
->line
, info
->column
,
236 vs_style
, strip_path_prefix
);
237 } else if (info
->module
) {
238 RenderModuleLocation(buffer
, info
->module
, info
->module_offset
,
239 info
->module_arch
, strip_path_prefix
);
242 MaybeBuildIdToBuffer(*info
, /*PrefixSpace=*/true, buffer
);
245 buffer
->AppendF("(<unknown module>)");
249 // Module basename and offset, or PC.
250 if (address
& kExternalPCBit
) {
251 // There PCs are not meaningful.
252 } else if (info
->module
) {
253 // Always strip the module name for %M.
254 RenderModuleLocation(buffer
, StripModuleName(info
->module
),
255 info
->module_offset
, info
->module_arch
, "");
257 MaybeBuildIdToBuffer(*info
, /*PrefixSpace=*/true, buffer
);
260 buffer
->AppendF("(%p)", (void *)address
);
264 Report("Unsupported specifier in stack frame format: %c (%p)!\n", *p
,
271 bool FormattedStackTracePrinter::RenderNeedsSymbolization(const char *format
) {
272 if (0 == internal_strcmp(format
, "DEFAULT"))
273 format
= kDefaultFormat
;
274 for (const char *p
= format
; *p
!= '\0'; p
++) {
294 void FormattedStackTracePrinter::RenderData(InternalScopedString
*buffer
,
297 const char *strip_path_prefix
) {
298 for (const char *p
= format
; *p
!= '\0'; p
++) {
300 buffer
->AppendF("%c", *p
);
309 buffer
->AppendF("%s", StripPathPrefix(DI
->file
, strip_path_prefix
));
312 buffer
->AppendF("%zu", DI
->line
);
315 buffer
->AppendF("%s", DI
->name
);
318 Report("Unsupported specifier in stack frame format: %c (%p)!\n", *p
,
325 #endif // !SANITIZER_SYMBOLIZER_MARKUP
327 void FormattedStackTracePrinter::RenderSourceLocation(
328 InternalScopedString
*buffer
, const char *file
, int line
, int column
,
329 bool vs_style
, const char *strip_path_prefix
) {
330 if (vs_style
&& line
> 0) {
331 buffer
->AppendF("%s(%d", StripPathPrefix(file
, strip_path_prefix
), line
);
333 buffer
->AppendF(",%d", column
);
334 buffer
->AppendF(")");
338 buffer
->AppendF("%s", StripPathPrefix(file
, strip_path_prefix
));
340 buffer
->AppendF(":%d", line
);
342 buffer
->AppendF(":%d", column
);
346 void FormattedStackTracePrinter::RenderModuleLocation(
347 InternalScopedString
*buffer
, const char *module
, uptr offset
,
348 ModuleArch arch
, const char *strip_path_prefix
) {
349 buffer
->AppendF("(%s", StripPathPrefix(module
, strip_path_prefix
));
350 if (arch
!= kModuleArchUnknown
) {
351 buffer
->AppendF(":%s", ModuleArchToString(arch
));
353 buffer
->AppendF("+0x%zx)", offset
);
356 } // namespace __sanitizer