Daily bump.
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_stacktrace_printer.cpp
blob8db321051e1a0de051f26acd335af2d8d2b0a757
1 //===-- sanitizer_common.cpp ----------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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;
28 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)
38 return function;
39 if (!function)
40 return nullptr;
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;
45 return nullptr;
47 if (SANITIZER_APPLE) {
48 if (const char *s = try_strip("wrap_"))
49 return s;
50 } else if (SANITIZER_WINDOWS) {
51 if (const char *s = try_strip("__asan_wrap_"))
52 return s;
53 } else {
54 if (const char *s = try_strip("___interceptor_"))
55 return s;
56 if (const char *s = try_strip("__interceptor_"))
57 return s;
59 return function;
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)
67 return function;
68 if (!function)
69 return nullptr;
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.
74 #if SANITIZER_NETBSD
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";
139 #endif
141 return function;
144 static void MaybeBuildIdToBuffer(const AddressInfo &info, bool PrefixSpace,
145 InternalScopedString *buffer) {
146 if (info.uuid_size) {
147 if (PrefixSpace)
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,
161 uptr address,
162 const AddressInfo *info,
163 bool vs_style,
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
169 // should match.
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++) {
174 if (*p != '%') {
175 buffer->AppendF("%c", *p);
176 continue;
178 p++;
179 switch (*p) {
180 case '%':
181 buffer->Append("%");
182 break;
183 // Frame number and all fields of AddressInfo structure.
184 case 'n':
185 buffer->AppendF("%u", frame_no);
186 break;
187 case 'p':
188 buffer->AppendF("0x%zx", address);
189 break;
190 case 'm':
191 buffer->AppendF("%s", StripPathPrefix(info->module, strip_path_prefix));
192 break;
193 case 'o':
194 buffer->AppendF("0x%zx", info->module_offset);
195 break;
196 case 'b':
197 MaybeBuildIdToBuffer(*info, /*PrefixSpace=*/false, buffer);
198 break;
199 case 'f':
200 buffer->AppendF("%s",
201 DemangleFunctionName(StripFunctionName(info->function)));
202 break;
203 case 'q':
204 buffer->AppendF("0x%zx", info->function_offset != AddressInfo::kUnknown
205 ? info->function_offset
206 : 0x0);
207 break;
208 case 's':
209 buffer->AppendF("%s", StripPathPrefix(info->file, strip_path_prefix));
210 break;
211 case 'l':
212 buffer->AppendF("%d", info->line);
213 break;
214 case 'c':
215 buffer->AppendF("%d", info->column);
216 break;
217 // Smarter special cases.
218 case 'F':
219 // Function name and offset, if file is unknown.
220 if (info->function) {
221 buffer->AppendF(
222 "in %s", DemangleFunctionName(StripFunctionName(info->function)));
223 if (!info->file && info->function_offset != AddressInfo::kUnknown)
224 buffer->AppendF("+0x%zx", info->function_offset);
226 break;
227 case 'S':
228 // File/line information.
229 RenderSourceLocation(buffer, info->file, info->line, info->column,
230 vs_style, strip_path_prefix);
231 break;
232 case 'L':
233 // Source location, or module location.
234 if (info->file) {
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);
241 #if !SANITIZER_APPLE
242 MaybeBuildIdToBuffer(*info, /*PrefixSpace=*/true, buffer);
243 #endif
244 } else {
245 buffer->AppendF("(<unknown module>)");
247 break;
248 case 'M':
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, "");
256 #if !SANITIZER_APPLE
257 MaybeBuildIdToBuffer(*info, /*PrefixSpace=*/true, buffer);
258 #endif
259 } else {
260 buffer->AppendF("(%p)", (void *)address);
262 break;
263 default:
264 Report("Unsupported specifier in stack frame format: %c (%p)!\n", *p,
265 (void *)p);
266 Die();
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++) {
275 if (*p != '%')
276 continue;
277 p++;
278 switch (*p) {
279 case '%':
280 break;
281 case 'n':
282 // frame_no
283 break;
284 case 'p':
285 // address
286 break;
287 default:
288 return true;
291 return false;
294 void FormattedStackTracePrinter::RenderData(InternalScopedString *buffer,
295 const char *format,
296 const DataInfo *DI,
297 const char *strip_path_prefix) {
298 for (const char *p = format; *p != '\0'; p++) {
299 if (*p != '%') {
300 buffer->AppendF("%c", *p);
301 continue;
303 p++;
304 switch (*p) {
305 case '%':
306 buffer->Append("%");
307 break;
308 case 's':
309 buffer->AppendF("%s", StripPathPrefix(DI->file, strip_path_prefix));
310 break;
311 case 'l':
312 buffer->AppendF("%zu", DI->line);
313 break;
314 case 'g':
315 buffer->AppendF("%s", DI->name);
316 break;
317 default:
318 Report("Unsupported specifier in stack frame format: %c (%p)!\n", *p,
319 (void *)p);
320 Die();
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);
332 if (column > 0)
333 buffer->AppendF(",%d", column);
334 buffer->AppendF(")");
335 return;
338 buffer->AppendF("%s", StripPathPrefix(file, strip_path_prefix));
339 if (line > 0) {
340 buffer->AppendF(":%d", line);
341 if (column > 0)
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