blob: 1cd5b6ee24c017177a9217c30703126e79219192 [file] [log] [blame]
Peter Collingbourne791e65d2013-10-25 23:03:29 +00001//===-- sanitizer_symbolizer.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 AddressSanitizer and ThreadSanitizer
11// run-time libraries.
12//===----------------------------------------------------------------------===//
13
Alexey Samsonov0e906682014-12-02 19:48:40 +000014#include "sanitizer_allocator_internal.h"
Peter Collingbourne791e65d2013-10-25 23:03:29 +000015#include "sanitizer_platform.h"
16#include "sanitizer_internal_defs.h"
Alexey Samsonov0e906682014-12-02 19:48:40 +000017#include "sanitizer_libc.h"
Peter Collingbourne791e65d2013-10-25 23:03:29 +000018#include "sanitizer_placement_new.h"
Kuba Breckaae219d32015-03-09 18:36:28 +000019#include "sanitizer_symbolizer_internal.h"
Peter Collingbourne791e65d2013-10-25 23:03:29 +000020
21namespace __sanitizer {
22
Alexey Samsonov0e906682014-12-02 19:48:40 +000023AddressInfo::AddressInfo() {
24 internal_memset(this, 0, sizeof(AddressInfo));
25 function_offset = kUnknown;
26}
27
28void AddressInfo::Clear() {
29 InternalFree(module);
30 InternalFree(function);
31 InternalFree(file);
32 internal_memset(this, 0, sizeof(AddressInfo));
33 function_offset = kUnknown;
34}
35
Kuba Mracekb38f1ca2017-01-06 21:45:05 +000036void AddressInfo::FillModuleInfo(const char *mod_name, uptr mod_offset,
37 ModuleArch mod_arch) {
Alexey Samsonov0e906682014-12-02 19:48:40 +000038 module = internal_strdup(mod_name);
39 module_offset = mod_offset;
Kuba Mracekb38f1ca2017-01-06 21:45:05 +000040 module_arch = mod_arch;
Alexey Samsonov0e906682014-12-02 19:48:40 +000041}
42
43SymbolizedStack::SymbolizedStack() : next(nullptr), info() {}
44
45SymbolizedStack *SymbolizedStack::New(uptr addr) {
46 void *mem = InternalAlloc(sizeof(SymbolizedStack));
47 SymbolizedStack *res = new(mem) SymbolizedStack();
48 res->info.address = addr;
49 return res;
50}
51
52void SymbolizedStack::ClearAll() {
53 info.Clear();
54 if (next)
55 next->ClearAll();
56 InternalFree(this);
57}
58
59DataInfo::DataInfo() {
60 internal_memset(this, 0, sizeof(DataInfo));
61}
62
63void DataInfo::Clear() {
64 InternalFree(module);
Dmitry Vyukova029b792016-03-01 15:38:12 +000065 InternalFree(file);
Alexey Samsonov0e906682014-12-02 19:48:40 +000066 InternalFree(name);
67 internal_memset(this, 0, sizeof(DataInfo));
68}
69
Alexey Samsonov78928c12013-10-30 17:05:37 +000070Symbolizer *Symbolizer::symbolizer_;
71StaticSpinMutex Symbolizer::init_mu_;
Peter Collingbourne791e65d2013-10-25 23:03:29 +000072LowLevelAllocator Symbolizer::symbolizer_allocator_;
73
Alexey Samsonov627e2c02013-10-31 21:44:07 +000074void Symbolizer::AddHooks(Symbolizer::StartSymbolizationHook start_hook,
75 Symbolizer::EndSymbolizationHook end_hook) {
76 CHECK(start_hook_ == 0 && end_hook_ == 0);
77 start_hook_ = start_hook;
78 end_hook_ = end_hook;
79}
80
Timur Iskhodzhanov6c66ad02015-03-31 12:50:05 +000081const char *Symbolizer::ModuleNameOwner::GetOwnedCopy(const char *str) {
82 mu_->CheckLocked();
83
84 // 'str' will be the same string multiple times in a row, optimize this case.
85 if (last_match_ && !internal_strcmp(last_match_, str))
86 return last_match_;
87
88 // FIXME: this is linear search.
89 // We should optimize this further if this turns out to be a bottleneck later.
90 for (uptr i = 0; i < storage_.size(); ++i) {
91 if (!internal_strcmp(storage_[i], str)) {
92 last_match_ = storage_[i];
93 return last_match_;
94 }
95 }
96 last_match_ = internal_strdup(str);
97 storage_.push_back(last_match_);
98 return last_match_;
99}
100
Kuba Breckaae219d32015-03-09 18:36:28 +0000101Symbolizer::Symbolizer(IntrusiveList<SymbolizerTool> tools)
Alexey Samsonov8e3cbde2016-02-22 18:52:51 +0000102 : module_names_(&mu_), modules_(), modules_fresh_(false), tools_(tools),
Timur Iskhodzhanovb97bcc42015-04-06 12:49:30 +0000103 start_hook_(0), end_hook_(0) {}
Alexey Samsonov627e2c02013-10-31 21:44:07 +0000104
105Symbolizer::SymbolizerScope::SymbolizerScope(const Symbolizer *sym)
106 : sym_(sym) {
107 if (sym_->start_hook_)
108 sym_->start_hook_();
109}
110
111Symbolizer::SymbolizerScope::~SymbolizerScope() {
112 if (sym_->end_hook_)
113 sym_->end_hook_();
114}
115
Peter Collingbourne791e65d2013-10-25 23:03:29 +0000116} // namespace __sanitizer