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 //===----------------------------------------------------------------------===//
15 #include "sanitizer_allocator_internal.h"
16 #include "sanitizer_common.h"
17 #include "sanitizer_internal_defs.h"
18 #include "sanitizer_libc.h"
19 #include "sanitizer_placement_new.h"
20 #include "sanitizer_platform.h"
21 #include "sanitizer_symbolizer_internal.h"
23 namespace __sanitizer
{
25 AddressInfo::AddressInfo() {
26 internal_memset(this, 0, sizeof(AddressInfo
));
27 function_offset
= kUnknown
;
30 void AddressInfo::Clear() {
32 InternalFree(function
);
34 internal_memset(this, 0, sizeof(AddressInfo
));
35 function_offset
= kUnknown
;
39 void AddressInfo::FillModuleInfo(const char *mod_name
, uptr mod_offset
,
40 ModuleArch mod_arch
) {
41 module
= internal_strdup(mod_name
);
42 module_offset
= mod_offset
;
43 module_arch
= mod_arch
;
47 void AddressInfo::FillModuleInfo(const LoadedModule
&mod
) {
48 module
= internal_strdup(mod
.full_name());
49 module_offset
= address
- mod
.base_address();
50 module_arch
= mod
.arch();
52 internal_memcpy(uuid
, mod
.uuid(), mod
.uuid_size());
53 uuid_size
= mod
.uuid_size();
56 SymbolizedStack::SymbolizedStack() : next(nullptr), info() {}
58 SymbolizedStack
*SymbolizedStack::New(uptr addr
) {
59 void *mem
= InternalAlloc(sizeof(SymbolizedStack
));
60 SymbolizedStack
*res
= new(mem
) SymbolizedStack();
61 res
->info
.address
= addr
;
65 void SymbolizedStack::ClearAll() {
72 DataInfo::DataInfo() {
73 internal_memset(this, 0, sizeof(DataInfo
));
76 void DataInfo::Clear() {
80 internal_memset(this, 0, sizeof(DataInfo
));
83 void FrameInfo::Clear() {
85 for (LocalInfo
&local
: locals
) {
86 InternalFree(local
.function_name
);
87 InternalFree(local
.name
);
88 InternalFree(local
.decl_file
);
93 Symbolizer
*Symbolizer::symbolizer_
;
94 StaticSpinMutex
Symbolizer::init_mu_
;
95 LowLevelAllocator
Symbolizer::symbolizer_allocator_
;
97 void Symbolizer::InvalidateModuleList() {
98 modules_fresh_
= false;
101 void Symbolizer::AddHooks(Symbolizer::StartSymbolizationHook start_hook
,
102 Symbolizer::EndSymbolizationHook end_hook
) {
103 CHECK(start_hook_
== 0 && end_hook_
== 0);
104 start_hook_
= start_hook
;
105 end_hook_
= end_hook
;
108 const char *Symbolizer::ModuleNameOwner::GetOwnedCopy(const char *str
) {
111 // 'str' will be the same string multiple times in a row, optimize this case.
112 if (last_match_
&& !internal_strcmp(last_match_
, str
))
115 // FIXME: this is linear search.
116 // We should optimize this further if this turns out to be a bottleneck later.
117 for (uptr i
= 0; i
< storage_
.size(); ++i
) {
118 if (!internal_strcmp(storage_
[i
], str
)) {
119 last_match_
= storage_
[i
];
123 last_match_
= internal_strdup(str
);
124 storage_
.push_back(last_match_
);
128 Symbolizer::Symbolizer(IntrusiveList
<SymbolizerTool
> tools
)
129 : module_names_(&mu_
), modules_(), modules_fresh_(false), tools_(tools
),
130 start_hook_(0), end_hook_(0) {}
132 Symbolizer::SymbolizerScope::SymbolizerScope(const Symbolizer
*sym
)
133 : sym_(sym
), errno_(errno
) {
134 if (sym_
->start_hook_
)
138 Symbolizer::SymbolizerScope::~SymbolizerScope() {
144 } // namespace __sanitizer