1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
//===-- sanitizer_symbolizer_libbacktrace.cc ------------------------------===//
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is shared between AddressSanitizer and ThreadSanitizer
// run-time libraries.
// Libbacktrace implementation of symbolizer parts.
//===----------------------------------------------------------------------===//
#include "sanitizer_platform.h"
#include "sanitizer_internal_defs.h"
#include "sanitizer_symbolizer.h"
#include "sanitizer_symbolizer_libbacktrace.h"
#if SANITIZER_LIBBACKTRACE
# include "backtrace-supported.h"
# if SANITIZER_POSIX && BACKTRACE_SUPPORTED && !BACKTRACE_USES_MALLOC
# include "backtrace.h"
# if SANITIZER_CP_DEMANGLE
# undef ARRAY_SIZE
# include "demangle.h"
# endif
# else
# define SANITIZER_LIBBACKTRACE 0
# endif
#endif
namespace __sanitizer {
#if SANITIZER_LIBBACKTRACE
namespace {
# if SANITIZER_CP_DEMANGLE
struct CplusV3DemangleData {
char *buf;
uptr size, allocated;
};
extern "C" {
static void CplusV3DemangleCallback(const char *s, size_t l, void *vdata) {
CplusV3DemangleData *data = (CplusV3DemangleData *)vdata;
uptr needed = data->size + l + 1;
if (needed > data->allocated) {
data->allocated *= 2;
if (needed > data->allocated)
data->allocated = needed;
char *buf = (char *)InternalAlloc(data->allocated);
if (data->buf) {
internal_memcpy(buf, data->buf, data->size);
InternalFree(data->buf);
}
data->buf = buf;
}
internal_memcpy(data->buf + data->size, s, l);
data->buf[data->size + l] = '\0';
data->size += l;
}
} // extern "C"
char *CplusV3Demangle(const char *name) {
CplusV3DemangleData data;
data.buf = 0;
data.size = 0;
data.allocated = 0;
if (cplus_demangle_v3_callback(name, DMGL_PARAMS | DMGL_ANSI,
CplusV3DemangleCallback, &data)) {
if (data.size + 64 > data.allocated)
return data.buf;
char *buf = internal_strdup(data.buf);
InternalFree(data.buf);
return buf;
}
if (data.buf)
InternalFree(data.buf);
return 0;
}
# endif // SANITIZER_CP_DEMANGLE
struct SymbolizeCodeData {
AddressInfo *frames;
uptr n_frames;
uptr max_frames;
const char *module_name;
uptr module_offset;
};
extern "C" {
static int SymbolizeCodePCInfoCallback(void *vdata, uintptr_t addr,
const char *filename, int lineno,
const char *function) {
SymbolizeCodeData *cdata = (SymbolizeCodeData *)vdata;
if (function) {
AddressInfo *info = &cdata->frames[cdata->n_frames++];
info->Clear();
info->FillAddressAndModuleInfo(addr, cdata->module_name,
cdata->module_offset);
info->function = LibbacktraceSymbolizer::Demangle(function, true);
if (filename)
info->file = internal_strdup(filename);
info->line = lineno;
if (cdata->n_frames == cdata->max_frames)
return 1;
}
return 0;
}
static void SymbolizeCodeCallback(void *vdata, uintptr_t addr,
const char *symname, uintptr_t, uintptr_t) {
SymbolizeCodeData *cdata = (SymbolizeCodeData *)vdata;
if (symname) {
AddressInfo *info = &cdata->frames[0];
info->Clear();
info->FillAddressAndModuleInfo(addr, cdata->module_name,
cdata->module_offset);
info->function = LibbacktraceSymbolizer::Demangle(symname, true);
cdata->n_frames = 1;
}
}
static void SymbolizeDataCallback(void *vdata, uintptr_t, const char *symname,
uintptr_t symval, uintptr_t symsize) {
DataInfo *info = (DataInfo *)vdata;
if (symname && symval) {
info->name = LibbacktraceSymbolizer::Demangle(symname, true);
info->start = symval;
info->size = symsize;
}
}
static void ErrorCallback(void *, const char *, int) {}
} // extern "C"
} // namespace
LibbacktraceSymbolizer *LibbacktraceSymbolizer::get(LowLevelAllocator *alloc) {
// State created in backtrace_create_state is leaked.
void *state = (void *)(backtrace_create_state("/proc/self/exe", 0,
ErrorCallback, NULL));
if (!state)
return 0;
return new(*alloc) LibbacktraceSymbolizer(state);
}
uptr LibbacktraceSymbolizer::SymbolizeCode(uptr addr, AddressInfo *frames,
uptr max_frames,
const char *module_name,
uptr module_offset) {
SymbolizeCodeData data;
data.frames = frames;
data.n_frames = 0;
data.max_frames = max_frames;
data.module_name = module_name;
data.module_offset = module_offset;
backtrace_pcinfo((backtrace_state *)state_, addr, SymbolizeCodePCInfoCallback,
ErrorCallback, &data);
if (data.n_frames)
return data.n_frames;
backtrace_syminfo((backtrace_state *)state_, addr, SymbolizeCodeCallback,
ErrorCallback, &data);
return data.n_frames;
}
bool LibbacktraceSymbolizer::SymbolizeData(DataInfo *info) {
backtrace_syminfo((backtrace_state *)state_, info->start,
SymbolizeDataCallback, ErrorCallback, info);
return true;
}
#else // SANITIZER_LIBBACKTRACE
LibbacktraceSymbolizer *LibbacktraceSymbolizer::get(LowLevelAllocator *alloc) {
return 0;
}
uptr LibbacktraceSymbolizer::SymbolizeCode(uptr addr, AddressInfo *frames,
uptr max_frames,
const char *module_name,
uptr module_offset) {
(void)state_;
return 0;
}
bool LibbacktraceSymbolizer::SymbolizeData(DataInfo *info) {
return false;
}
#endif // SANITIZER_LIBBACKTRACE
char *LibbacktraceSymbolizer::Demangle(const char *name, bool always_alloc) {
#if SANITIZER_LIBBACKTRACE && SANITIZER_CP_DEMANGLE
if (char *demangled = CplusV3Demangle(name))
return demangled;
#endif
if (always_alloc)
return internal_strdup(name);
return 0;
}
} // namespace __sanitizer
|