]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace_printer.cc
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_stacktrace_printer.cc
1 //===-- sanitizer_common.cc -----------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is shared between sanitizers' run-time libraries.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "sanitizer_stacktrace_printer.h"
15 #include "sanitizer_file.h"
16 #include "sanitizer_fuchsia.h"
17
18 namespace __sanitizer {
19
20 // sanitizer_symbolizer_markup.cc implements these differently.
21 #if !SANITIZER_SYMBOLIZER_MARKUP
22
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;
29   return function;
30 }
31
32 static const char *DemangleFunctionName(const char *function) {
33   if (!function) return nullptr;
34
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.
38 #if SANITIZER_NETBSD
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 #endif
102
103   return function;
104 }
105
106 static const char kDefaultFormat[] = "    #%n %p %F %L";
107
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++) {
114     if (*p != '%') {
115       buffer->append("%c", *p);
116       continue;
117     }
118     p++;
119     switch (*p) {
120     case '%':
121       buffer->append("%%");
122       break;
123     // Frame number and all fields of AddressInfo structure.
124     case 'n':
125       buffer->append("%zu", frame_no);
126       break;
127     case 'p':
128       buffer->append("0x%zx", info.address);
129       break;
130     case 'm':
131       buffer->append("%s", StripPathPrefix(info.module, strip_path_prefix));
132       break;
133     case 'o':
134       buffer->append("0x%zx", info.module_offset);
135       break;
136     case 'f':
137       buffer->append("%s",
138         DemangleFunctionName(
139           StripFunctionName(info.function, strip_func_prefix)));
140       break;
141     case 'q':
142       buffer->append("0x%zx", info.function_offset != AddressInfo::kUnknown
143                                   ? info.function_offset
144                                   : 0x0);
145       break;
146     case 's':
147       buffer->append("%s", StripPathPrefix(info.file, strip_path_prefix));
148       break;
149     case 'l':
150       buffer->append("%d", info.line);
151       break;
152     case 'c':
153       buffer->append("%d", info.column);
154       break;
155     // Smarter special cases.
156     case 'F':
157       // Function name and offset, if file is unknown.
158       if (info.function) {
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);
164       }
165       break;
166     case 'S':
167       // File/line information.
168       RenderSourceLocation(buffer, info.file, info.line, info.column, vs_style,
169                            strip_path_prefix);
170       break;
171     case 'L':
172       // Source location, or module location.
173       if (info.file) {
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);
179       } else {
180         buffer->append("(<unknown module>)");
181       }
182       break;
183     case 'M':
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, "");
191       else
192         buffer->append("(%p)", (void *)info.address);
193       break;
194     default:
195       Report("Unsupported specifier in stack frame format: %c (0x%zx)!\n", *p,
196              *p);
197       Die();
198     }
199   }
200 }
201
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++) {
205     if (*p != '%') {
206       buffer->append("%c", *p);
207       continue;
208     }
209     p++;
210     switch (*p) {
211       case '%':
212         buffer->append("%%");
213         break;
214       case 's':
215         buffer->append("%s", StripPathPrefix(DI->file, strip_path_prefix));
216         break;
217       case 'l':
218         buffer->append("%d", DI->line);
219         break;
220       case 'g':
221         buffer->append("%s", DI->name);
222         break;
223       default:
224         Report("Unsupported specifier in stack frame format: %c (0x%zx)!\n", *p,
225                *p);
226         Die();
227     }
228   }
229 }
230
231 #endif  // !SANITIZER_SYMBOLIZER_MARKUP
232
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);
238     if (column > 0)
239       buffer->append(",%d", column);
240     buffer->append(")");
241     return;
242   }
243
244   buffer->append("%s", StripPathPrefix(file, strip_path_prefix));
245   if (line > 0) {
246     buffer->append(":%d", line);
247     if (column > 0)
248       buffer->append(":%d", column);
249   }
250 }
251
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));
258   }
259   buffer->append("+0x%zx)", offset);
260 }
261
262 } // namespace __sanitizer