1 //===-- sanitizer_symbolizer.cpp ------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_allocator_internal.h"
14 #include "sanitizer_platform.h"
15 #include "sanitizer_internal_defs.h"
16 #include "sanitizer_libc.h"
17 #include "sanitizer_placement_new.h"
18 #include "sanitizer_symbolizer_internal.h"
20 namespace __sanitizer
{
22 AddressInfo::AddressInfo() {
23 internal_memset(this, 0, sizeof(AddressInfo
));
24 function_offset
= kUnknown
;
27 void AddressInfo::Clear() {
29 InternalFree(function
);
31 internal_memset(this, 0, sizeof(AddressInfo
));
32 function_offset
= kUnknown
;
35 void AddressInfo::FillModuleInfo(const char *mod_name
, uptr mod_offset
,
36 ModuleArch mod_arch
) {
37 module
= internal_strdup(mod_name
);
38 module_offset
= mod_offset
;
39 module_arch
= mod_arch
;
42 SymbolizedStack::SymbolizedStack() : next(nullptr), info() {}
44 SymbolizedStack
*SymbolizedStack::New(uptr addr
) {
45 void *mem
= InternalAlloc(sizeof(SymbolizedStack
));
46 SymbolizedStack
*res
= new(mem
) SymbolizedStack();
47 res
->info
.address
= addr
;
51 void SymbolizedStack::ClearAll() {
58 DataInfo::DataInfo() {
59 internal_memset(this, 0, sizeof(DataInfo
));
62 void DataInfo::Clear() {
66 internal_memset(this, 0, sizeof(DataInfo
));
69 void FrameInfo::Clear() {
71 for (LocalInfo
&local
: locals
) {
72 InternalFree(local
.function_name
);
73 InternalFree(local
.name
);
74 InternalFree(local
.decl_file
);
79 Symbolizer
*Symbolizer::symbolizer_
;
80 StaticSpinMutex
Symbolizer::init_mu_
;
81 LowLevelAllocator
Symbolizer::symbolizer_allocator_
;
83 void Symbolizer::InvalidateModuleList() {
84 modules_fresh_
= false;
87 void Symbolizer::AddHooks(Symbolizer::StartSymbolizationHook start_hook
,
88 Symbolizer::EndSymbolizationHook end_hook
) {
89 CHECK(start_hook_
== 0 && end_hook_
== 0);
90 start_hook_
= start_hook
;
94 const char *Symbolizer::ModuleNameOwner::GetOwnedCopy(const char *str
) {
97 // 'str' will be the same string multiple times in a row, optimize this case.
98 if (last_match_
&& !internal_strcmp(last_match_
, str
))
101 // FIXME: this is linear search.
102 // We should optimize this further if this turns out to be a bottleneck later.
103 for (uptr i
= 0; i
< storage_
.size(); ++i
) {
104 if (!internal_strcmp(storage_
[i
], str
)) {
105 last_match_
= storage_
[i
];
109 last_match_
= internal_strdup(str
);
110 storage_
.push_back(last_match_
);
114 Symbolizer::Symbolizer(IntrusiveList
<SymbolizerTool
> tools
)
115 : module_names_(&mu_
), modules_(), modules_fresh_(false), tools_(tools
),
116 start_hook_(0), end_hook_(0) {}
118 Symbolizer::SymbolizerScope::SymbolizerScope(const Symbolizer
*sym
)
120 if (sym_
->start_hook_
)
124 Symbolizer::SymbolizerScope::~SymbolizerScope() {
129 void Symbolizer::LateInitializeTools() {
130 for (auto &tool
: tools_
) {
131 tool
.LateInitialize();
135 } // namespace __sanitizer