1 //===-- sanitizer_common.cc -----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between sanitizers' run-time libraries.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_stacktrace_printer.h"
15 #include "sanitizer_file.h"
16 #include "sanitizer_fuchsia.h"
18 namespace __sanitizer {
20 // sanitizer_symbolizer_markup.cc implements these differently.
21 #if !SANITIZER_SYMBOLIZER_MARKUP
23 static const char *StripFunctionName(const char *function, const char *prefix) {
24 if (!function) return nullptr;
25 if (!prefix) return function;
26 uptr prefix_len = internal_strlen(prefix);
27 if (0 == internal_strncmp(function, prefix, prefix_len))
28 return function + prefix_len;
32 static const char *DemangleFunctionName(const char *function) {
33 if (!function) return nullptr;
35 // NetBSD uses indirection for old threading functions for historical reasons
36 // The mangled names are internal implementation detail and should not be
37 // exposed even in backtraces.
39 if (!internal_strcmp(function, "__libc_mutex_init"))
40 return "pthread_mutex_init";
41 if (!internal_strcmp(function, "__libc_mutex_lock"))
42 return "pthread_mutex_lock";
43 if (!internal_strcmp(function, "__libc_mutex_trylock"))
44 return "pthread_mutex_trylock";
45 if (!internal_strcmp(function, "__libc_mutex_unlock"))
46 return "pthread_mutex_unlock";
47 if (!internal_strcmp(function, "__libc_mutex_destroy"))
48 return "pthread_mutex_destroy";
49 if (!internal_strcmp(function, "__libc_mutexattr_init"))
50 return "pthread_mutexattr_init";
51 if (!internal_strcmp(function, "__libc_mutexattr_settype"))
52 return "pthread_mutexattr_settype";
53 if (!internal_strcmp(function, "__libc_mutexattr_destroy"))
54 return "pthread_mutexattr_destroy";
55 if (!internal_strcmp(function, "__libc_cond_init"))
56 return "pthread_cond_init";
57 if (!internal_strcmp(function, "__libc_cond_signal"))
58 return "pthread_cond_signal";
59 if (!internal_strcmp(function, "__libc_cond_broadcast"))
60 return "pthread_cond_broadcast";
61 if (!internal_strcmp(function, "__libc_cond_wait"))
62 return "pthread_cond_wait";
63 if (!internal_strcmp(function, "__libc_cond_timedwait"))
64 return "pthread_cond_timedwait";
65 if (!internal_strcmp(function, "__libc_cond_destroy"))
66 return "pthread_cond_destroy";
67 if (!internal_strcmp(function, "__libc_rwlock_init"))
68 return "pthread_rwlock_init";
69 if (!internal_strcmp(function, "__libc_rwlock_rdlock"))
70 return "pthread_rwlock_rdlock";
71 if (!internal_strcmp(function, "__libc_rwlock_wrlock"))
72 return "pthread_rwlock_wrlock";
73 if (!internal_strcmp(function, "__libc_rwlock_tryrdlock"))
74 return "pthread_rwlock_tryrdlock";
75 if (!internal_strcmp(function, "__libc_rwlock_trywrlock"))
76 return "pthread_rwlock_trywrlock";
77 if (!internal_strcmp(function, "__libc_rwlock_unlock"))
78 return "pthread_rwlock_unlock";
79 if (!internal_strcmp(function, "__libc_rwlock_destroy"))
80 return "pthread_rwlock_destroy";
81 if (!internal_strcmp(function, "__libc_thr_keycreate"))
82 return "pthread_key_create";
83 if (!internal_strcmp(function, "__libc_thr_setspecific"))
84 return "pthread_setspecific";
85 if (!internal_strcmp(function, "__libc_thr_getspecific"))
86 return "pthread_getspecific";
87 if (!internal_strcmp(function, "__libc_thr_keydelete"))
88 return "pthread_key_delete";
89 if (!internal_strcmp(function, "__libc_thr_once"))
90 return "pthread_once";
91 if (!internal_strcmp(function, "__libc_thr_self"))
92 return "pthread_self";
93 if (!internal_strcmp(function, "__libc_thr_exit"))
94 return "pthread_exit";
95 if (!internal_strcmp(function, "__libc_thr_setcancelstate"))
96 return "pthread_setcancelstate";
97 if (!internal_strcmp(function, "__libc_thr_equal"))
98 return "pthread_equal";
99 if (!internal_strcmp(function, "__libc_thr_curcpu"))
100 return "pthread_curcpu_np";
101 if (!internal_strcmp(function, "__libc_thr_sigsetmask"))
102 return "pthread_sigmask";
108 static const char kDefaultFormat[] = " #%n %p %F %L";
110 void RenderFrame(InternalScopedString *buffer, const char *format, int frame_no,
111 const AddressInfo &info, bool vs_style,
112 const char *strip_path_prefix, const char *strip_func_prefix) {
113 if (0 == internal_strcmp(format, "DEFAULT"))
114 format = kDefaultFormat;
115 for (const char *p = format; *p != '\0'; p++) {
117 buffer->append("%c", *p);
123 buffer->append("%%");
125 // Frame number and all fields of AddressInfo structure.
127 buffer->append("%zu", frame_no);
130 buffer->append("0x%zx", info.address);
133 buffer->append("%s", StripPathPrefix(info.module, strip_path_prefix));
136 buffer->append("0x%zx", info.module_offset);
140 DemangleFunctionName(
141 StripFunctionName(info.function, strip_func_prefix)));
144 buffer->append("0x%zx", info.function_offset != AddressInfo::kUnknown
145 ? info.function_offset
149 buffer->append("%s", StripPathPrefix(info.file, strip_path_prefix));
152 buffer->append("%d", info.line);
155 buffer->append("%d", info.column);
157 // Smarter special cases.
159 // Function name and offset, if file is unknown.
161 buffer->append("in %s",
162 DemangleFunctionName(
163 StripFunctionName(info.function, strip_func_prefix)));
164 if (!info.file && info.function_offset != AddressInfo::kUnknown)
165 buffer->append("+0x%zx", info.function_offset);
169 // File/line information.
170 RenderSourceLocation(buffer, info.file, info.line, info.column, vs_style,
174 // Source location, or module location.
176 RenderSourceLocation(buffer, info.file, info.line, info.column,
177 vs_style, strip_path_prefix);
178 } else if (info.module) {
179 RenderModuleLocation(buffer, info.module, info.module_offset,
180 info.module_arch, strip_path_prefix);
182 buffer->append("(<unknown module>)");
186 // Module basename and offset, or PC.
187 if (info.address & kExternalPCBit)
188 {} // There PCs are not meaningful.
189 else if (info.module)
190 // Always strip the module name for %M.
191 RenderModuleLocation(buffer, StripModuleName(info.module),
192 info.module_offset, info.module_arch, "");
194 buffer->append("(%p)", (void *)info.address);
197 Report("Unsupported specifier in stack frame format: %c (0x%zx)!\n", *p,
204 void RenderData(InternalScopedString *buffer, const char *format,
205 const DataInfo *DI, const char *strip_path_prefix) {
206 for (const char *p = format; *p != '\0'; p++) {
208 buffer->append("%c", *p);
214 buffer->append("%%");
217 buffer->append("%s", StripPathPrefix(DI->file, strip_path_prefix));
220 buffer->append("%d", DI->line);
223 buffer->append("%s", DI->name);
226 Report("Unsupported specifier in stack frame format: %c (0x%zx)!\n", *p,
233 #endif // !SANITIZER_SYMBOLIZER_MARKUP
235 void RenderSourceLocation(InternalScopedString *buffer, const char *file,
236 int line, int column, bool vs_style,
237 const char *strip_path_prefix) {
238 if (vs_style && line > 0) {
239 buffer->append("%s(%d", StripPathPrefix(file, strip_path_prefix), line);
241 buffer->append(",%d", column);
246 buffer->append("%s", StripPathPrefix(file, strip_path_prefix));
248 buffer->append(":%d", line);
250 buffer->append(":%d", column);
254 void RenderModuleLocation(InternalScopedString *buffer, const char *module,
255 uptr offset, ModuleArch arch,
256 const char *strip_path_prefix) {
257 buffer->append("(%s", StripPathPrefix(module, strip_path_prefix));
258 if (arch != kModuleArchUnknown) {
259 buffer->append(":%s", ModuleArchToString(arch));
261 buffer->append("+0x%zx)", offset);
264 } // namespace __sanitizer