Home | History | Annotate | Download | only in lsan
      1 //=-- lsan.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 a part of LeakSanitizer.
     11 // Standalone LSan RTL.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "lsan.h"
     16 
     17 #include "sanitizer_common/sanitizer_flags.h"
     18 #include "sanitizer_common/sanitizer_stacktrace.h"
     19 #include "lsan_allocator.h"
     20 #include "lsan_common.h"
     21 #include "lsan_thread.h"
     22 
     23 namespace __lsan {
     24 
     25 static void InitializeCommonFlags() {
     26   CommonFlags *cf = common_flags();
     27   cf->external_symbolizer_path = GetEnv("LSAN_SYMBOLIZER_PATH");
     28   cf->symbolize = (cf->external_symbolizer_path &&
     29       cf->external_symbolizer_path[0]);
     30   cf->strip_path_prefix = "";
     31   cf->fast_unwind_on_malloc = true;
     32   cf->malloc_context_size = 30;
     33   cf->detect_leaks = true;
     34   cf->leak_check_at_exit = true;
     35 
     36   ParseCommonFlagsFromString(GetEnv("LSAN_OPTIONS"));
     37 }
     38 
     39 void Init() {
     40   static bool inited;
     41   if (inited)
     42     return;
     43   inited = true;
     44   SanitizerToolName = "LeakSanitizer";
     45   InitializeCommonFlags();
     46   InitializeAllocator();
     47   InitTlsSize();
     48   InitializeInterceptors();
     49   InitializeThreadRegistry();
     50   u32 tid = ThreadCreate(0, 0, true);
     51   CHECK_EQ(tid, 0);
     52   ThreadStart(tid, GetTid());
     53   SetCurrentThread(tid);
     54 
     55   // Start symbolizer process if necessary.
     56   const char* external_symbolizer = common_flags()->external_symbolizer_path;
     57   if (common_flags()->symbolize && external_symbolizer &&
     58       external_symbolizer[0]) {
     59     InitializeExternalSymbolizer(external_symbolizer);
     60   }
     61 
     62   InitCommonLsan();
     63   if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit)
     64     Atexit(DoLeakCheck);
     65 }
     66 
     67 }  // namespace __lsan
     68