Home | History | Annotate | Download | only in sanitizer_common
      1 //===-- 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 
     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"
     20 
     21 namespace __sanitizer {
     22 
     23 AddressInfo::AddressInfo() {
     24   internal_memset(this, 0, sizeof(AddressInfo));
     25   function_offset = kUnknown;
     26 }
     27 
     28 void AddressInfo::Clear() {
     29   InternalFree(module);
     30   InternalFree(function);
     31   InternalFree(file);
     32   internal_memset(this, 0, sizeof(AddressInfo));
     33   function_offset = kUnknown;
     34 }
     35 
     36 void AddressInfo::FillModuleInfo(const char *mod_name, uptr mod_offset) {
     37   module = internal_strdup(mod_name);
     38   module_offset = mod_offset;
     39 }
     40 
     41 SymbolizedStack::SymbolizedStack() : next(nullptr), info() {}
     42 
     43 SymbolizedStack *SymbolizedStack::New(uptr addr) {
     44   void *mem = InternalAlloc(sizeof(SymbolizedStack));
     45   SymbolizedStack *res = new(mem) SymbolizedStack();
     46   res->info.address = addr;
     47   return res;
     48 }
     49 
     50 void SymbolizedStack::ClearAll() {
     51   info.Clear();
     52   if (next)
     53     next->ClearAll();
     54   InternalFree(this);
     55 }
     56 
     57 DataInfo::DataInfo() {
     58   internal_memset(this, 0, sizeof(DataInfo));
     59 }
     60 
     61 void DataInfo::Clear() {
     62   InternalFree(module);
     63   InternalFree(file);
     64   InternalFree(name);
     65   internal_memset(this, 0, sizeof(DataInfo));
     66 }
     67 
     68 Symbolizer *Symbolizer::symbolizer_;
     69 StaticSpinMutex Symbolizer::init_mu_;
     70 LowLevelAllocator Symbolizer::symbolizer_allocator_;
     71 
     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;
     76   end_hook_ = end_hook;
     77 }
     78 
     79 const char *Symbolizer::ModuleNameOwner::GetOwnedCopy(const char *str) {
     80   mu_->CheckLocked();
     81 
     82   // 'str' will be the same string multiple times in a row, optimize this case.
     83   if (last_match_ && !internal_strcmp(last_match_, str))
     84     return last_match_;
     85 
     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];
     91       return last_match_;
     92     }
     93   }
     94   last_match_ = internal_strdup(str);
     95   storage_.push_back(last_match_);
     96   return last_match_;
     97 }
     98 
     99 Symbolizer::Symbolizer(IntrusiveList<SymbolizerTool> tools)
    100     : module_names_(&mu_), modules_(), modules_fresh_(false), tools_(tools),
    101       start_hook_(0), end_hook_(0) {}
    102 
    103 Symbolizer::SymbolizerScope::SymbolizerScope(const Symbolizer *sym)
    104     : sym_(sym) {
    105   if (sym_->start_hook_)
    106     sym_->start_hook_();
    107 }
    108 
    109 Symbolizer::SymbolizerScope::~SymbolizerScope() {
    110   if (sym_->end_hook_)
    111     sym_->end_hook_();
    112 }
    113 
    114 }  // namespace __sanitizer
    115