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
|
//===-- sanitizer_symbolizer.cc -------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// 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.
//===----------------------------------------------------------------------===//
#include "sanitizer_allocator_internal.h"
#include "sanitizer_platform.h"
#include "sanitizer_internal_defs.h"
#include "sanitizer_libc.h"
#include "sanitizer_placement_new.h"
#include "sanitizer_symbolizer_internal.h"
namespace __sanitizer {
AddressInfo::AddressInfo() {
internal_memset(this, 0, sizeof(AddressInfo));
function_offset = kUnknown;
}
void AddressInfo::Clear() {
InternalFree(module);
InternalFree(function);
InternalFree(file);
internal_memset(this, 0, sizeof(AddressInfo));
function_offset = kUnknown;
}
void AddressInfo::FillModuleInfo(const char *mod_name, uptr mod_offset) {
module = internal_strdup(mod_name);
module_offset = mod_offset;
}
SymbolizedStack::SymbolizedStack() : next(nullptr), info() {}
SymbolizedStack *SymbolizedStack::New(uptr addr) {
void *mem = InternalAlloc(sizeof(SymbolizedStack));
SymbolizedStack *res = new(mem) SymbolizedStack();
res->info.address = addr;
return res;
}
void SymbolizedStack::ClearAll() {
info.Clear();
if (next)
next->ClearAll();
InternalFree(this);
}
DataInfo::DataInfo() {
internal_memset(this, 0, sizeof(DataInfo));
}
void DataInfo::Clear() {
InternalFree(module);
InternalFree(name);
internal_memset(this, 0, sizeof(DataInfo));
}
Symbolizer *Symbolizer::symbolizer_;
StaticSpinMutex Symbolizer::init_mu_;
LowLevelAllocator Symbolizer::symbolizer_allocator_;
void Symbolizer::AddHooks(Symbolizer::StartSymbolizationHook start_hook,
Symbolizer::EndSymbolizationHook end_hook) {
CHECK(start_hook_ == 0 && end_hook_ == 0);
start_hook_ = start_hook;
end_hook_ = end_hook;
}
const char *Symbolizer::ModuleNameOwner::GetOwnedCopy(const char *str) {
mu_->CheckLocked();
// 'str' will be the same string multiple times in a row, optimize this case.
if (last_match_ && !internal_strcmp(last_match_, str))
return last_match_;
// FIXME: this is linear search.
// We should optimize this further if this turns out to be a bottleneck later.
for (uptr i = 0; i < storage_.size(); ++i) {
if (!internal_strcmp(storage_[i], str)) {
last_match_ = storage_[i];
return last_match_;
}
}
last_match_ = internal_strdup(str);
storage_.push_back(last_match_);
return last_match_;
}
Symbolizer::Symbolizer(IntrusiveList<SymbolizerTool> tools)
: module_names_(&mu_), tools_(tools), start_hook_(0), end_hook_(0) {}
Symbolizer::SymbolizerScope::SymbolizerScope(const Symbolizer *sym)
: sym_(sym) {
if (sym_->start_hook_)
sym_->start_hook_();
}
Symbolizer::SymbolizerScope::~SymbolizerScope() {
if (sym_->end_hook_)
sym_->end_hook_();
}
SymbolizedStack *Symbolizer::SymbolizePC(uptr addr) {
BlockingMutexLock l(&mu_);
const char *module_name;
uptr module_offset;
SymbolizedStack *res = SymbolizedStack::New(addr);
if (!PlatformFindModuleNameAndOffsetForAddress(addr, &module_name,
&module_offset))
return res;
// Always fill data about module name and offset.
res->info.FillModuleInfo(module_name, module_offset);
for (auto iter = Iterator(&tools_); iter.hasNext();) {
auto *tool = iter.next();
SymbolizerScope sym_scope(this);
if (tool->SymbolizePC(addr, res)) {
return res;
}
}
return res;
}
bool Symbolizer::SymbolizeData(uptr addr, DataInfo *info) {
BlockingMutexLock l(&mu_);
const char *module_name;
uptr module_offset;
if (!PlatformFindModuleNameAndOffsetForAddress(addr, &module_name,
&module_offset))
return false;
info->Clear();
info->module = internal_strdup(module_name);
info->module_offset = module_offset;
for (auto iter = Iterator(&tools_); iter.hasNext();) {
auto *tool = iter.next();
SymbolizerScope sym_scope(this);
if (tool->SymbolizeData(addr, info)) {
return true;
}
}
return true;
}
bool Symbolizer::GetModuleNameAndOffsetForPC(uptr pc, const char **module_name,
uptr *module_address) {
BlockingMutexLock l(&mu_);
const char *internal_module_name = nullptr;
if (!PlatformFindModuleNameAndOffsetForAddress(pc, &internal_module_name,
module_address))
return false;
if (module_name)
*module_name = module_names_.GetOwnedCopy(internal_module_name);
return true;
}
void Symbolizer::Flush() {
BlockingMutexLock l(&mu_);
for (auto iter = Iterator(&tools_); iter.hasNext();) {
auto *tool = iter.next();
SymbolizerScope sym_scope(this);
tool->Flush();
}
}
const char *Symbolizer::Demangle(const char *name) {
BlockingMutexLock l(&mu_);
for (auto iter = Iterator(&tools_); iter.hasNext();) {
auto *tool = iter.next();
SymbolizerScope sym_scope(this);
if (const char *demangled = tool->Demangle(name))
return demangled;
}
return PlatformDemangle(name);
}
void Symbolizer::PrepareForSandboxing() {
BlockingMutexLock l(&mu_);
PlatformPrepareForSandboxing();
}
} // namespace __sanitizer
|