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";
106 static const char kDefaultFormat[] = " #%n %p %F %L";
108 void RenderFrame(InternalScopedString *buffer, const char *format, int frame_no,
109 const AddressInfo &info, bool vs_style,
110 const char *strip_path_prefix, const char *strip_func_prefix) {
111 if (0 == internal_strcmp(format, "DEFAULT"))
112 format = kDefaultFormat;
113 for (const char *p = format; *p != '\0'; p++) {
115 buffer->append("%c", *p);
121 buffer->append("%%");
123 // Frame number and all fields of AddressInfo structure.
125 buffer->append("%zu", frame_no);
128 buffer->append("0x%zx", info.address);
131 buffer->append("%s", StripPathPrefix(info.module, strip_path_prefix));
134 buffer->append("0x%zx", info.module_offset);
138 DemangleFunctionName(
139 StripFunctionName(info.function, strip_func_prefix)));
142 buffer->append("0x%zx", info.function_offset != AddressInfo::kUnknown
143 ? info.function_offset
147 buffer->append("%s", StripPathPrefix(info.file, strip_path_prefix));
150 buffer->append("%d", info.line);
153 buffer->append("%d", info.column);
155 // Smarter special cases.
157 // Function name and offset, if file is unknown.
159 buffer->append("in %s",
160 DemangleFunctionName(
161 StripFunctionName(info.function, strip_func_prefix)));
162 if (!info.file && info.function_offset != AddressInfo::kUnknown)
163 buffer->append("+0x%zx", info.function_offset);
167 // File/line information.
168 RenderSourceLocation(buffer, info.file, info.line, info.column, vs_style,
172 // Source location, or module location.
174 RenderSourceLocation(buffer, info.file, info.line, info.column,
175 vs_style, strip_path_prefix);
176 } else if (info.module) {
177 RenderModuleLocation(buffer, info.module, info.module_offset,
178 info.module_arch, strip_path_prefix);
180 buffer->append("(<unknown module>)");
184 // Module basename and offset, or PC.
185 if (info.address & kExternalPCBit)
186 {} // There PCs are not meaningful.
187 else if (info.module)
188 // Always strip the module name for %M.
189 RenderModuleLocation(buffer, StripModuleName(info.module),
190 info.module_offset, info.module_arch, "");
192 buffer->append("(%p)", (void *)info.address);
195 Report("Unsupported specifier in stack frame format: %c (0x%zx)!\n", *p,
202 void RenderData(InternalScopedString *buffer, const char *format,
203 const DataInfo *DI, const char *strip_path_prefix) {
204 for (const char *p = format; *p != '\0'; p++) {
206 buffer->append("%c", *p);
212 buffer->append("%%");
215 buffer->append("%s", StripPathPrefix(DI->file, strip_path_prefix));
218 buffer->append("%d", DI->line);
221 buffer->append("%s", DI->name);
224 Report("Unsupported specifier in stack frame format: %c (0x%zx)!\n", *p,
231 #endif // !SANITIZER_SYMBOLIZER_MARKUP
233 void RenderSourceLocation(InternalScopedString *buffer, const char *file,
234 int line, int column, bool vs_style,
235 const char *strip_path_prefix) {
236 if (vs_style && line > 0) {
237 buffer->append("%s(%d", StripPathPrefix(file, strip_path_prefix), line);
239 buffer->append(",%d", column);
244 buffer->append("%s", StripPathPrefix(file, strip_path_prefix));
246 buffer->append(":%d", line);
248 buffer->append(":%d", column);
252 void RenderModuleLocation(InternalScopedString *buffer, const char *module,
253 uptr offset, ModuleArch arch,
254 const char *strip_path_prefix) {
255 buffer->append("(%s", StripPathPrefix(module, strip_path_prefix));
256 if (arch != kModuleArchUnknown) {
257 buffer->append(":%s", ModuleArchToString(arch));
259 buffer->append("+0x%zx)", offset);
262 } // namespace __sanitizer