1 //===-- sanitizer_symbolizer.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 AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_allocator_internal.h"
15 #include "sanitizer_platform.h"
16 #include "sanitizer_internal_defs.h"
17 #include "sanitizer_libc.h"
18 #include "sanitizer_placement_new.h"
19 #include "sanitizer_symbolizer_internal.h"
21 namespace __sanitizer {
23 AddressInfo::AddressInfo() {
24 internal_memset(this, 0, sizeof(AddressInfo));
25 function_offset = kUnknown;
28 void AddressInfo::Clear() {
30 InternalFree(function);
32 internal_memset(this, 0, sizeof(AddressInfo));
33 function_offset = kUnknown;
36 void AddressInfo::FillModuleInfo(const char *mod_name, uptr mod_offset) {
37 module = internal_strdup(mod_name);
38 module_offset = mod_offset;
41 SymbolizedStack::SymbolizedStack() : next(nullptr), info() {}
43 SymbolizedStack *SymbolizedStack::New(uptr addr) {
44 void *mem = InternalAlloc(sizeof(SymbolizedStack));
45 SymbolizedStack *res = new(mem) SymbolizedStack();
46 res->info.address = addr;
50 void SymbolizedStack::ClearAll() {
57 DataInfo::DataInfo() {
58 internal_memset(this, 0, sizeof(DataInfo));
61 void DataInfo::Clear() {
65 internal_memset(this, 0, sizeof(DataInfo));
68 Symbolizer *Symbolizer::symbolizer_;
69 StaticSpinMutex Symbolizer::init_mu_;
70 LowLevelAllocator Symbolizer::symbolizer_allocator_;
72 void Symbolizer::AddHooks(Symbolizer::StartSymbolizationHook start_hook,
73 Symbolizer::EndSymbolizationHook end_hook) {
74 CHECK(start_hook_ == 0 && end_hook_ == 0);
75 start_hook_ = start_hook;
79 const char *Symbolizer::ModuleNameOwner::GetOwnedCopy(const char *str) {
82 // 'str' will be the same string multiple times in a row, optimize this case.
83 if (last_match_ && !internal_strcmp(last_match_, str))
86 // FIXME: this is linear search.
87 // We should optimize this further if this turns out to be a bottleneck later.
88 for (uptr i = 0; i < storage_.size(); ++i) {
89 if (!internal_strcmp(storage_[i], str)) {
90 last_match_ = storage_[i];
94 last_match_ = internal_strdup(str);
95 storage_.push_back(last_match_);
99 Symbolizer::Symbolizer(IntrusiveList<SymbolizerTool> tools)
100 : module_names_(&mu_), modules_(), modules_fresh_(false), tools_(tools),
101 start_hook_(0), end_hook_(0) {}
103 Symbolizer::SymbolizerScope::SymbolizerScope(const Symbolizer *sym)
105 if (sym_->start_hook_)
109 Symbolizer::SymbolizerScope::~SymbolizerScope() {
114 } // namespace __sanitizer