blob: 058bbdba39073798ebebc77338006de90a1529e0 [file] [log] [blame]
Sergey Matveeve96c62a2013-05-20 11:04:43 +00001//=-- 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
Sergey Matveev74c88792013-11-25 17:39:36 +000023bool lsan_inited;
24bool lsan_init_is_running;
25
Sergey Matveeve96c62a2013-05-20 11:04:43 +000026namespace __lsan {
27
28static void InitializeCommonFlags() {
29 CommonFlags *cf = common_flags();
Alexey Samsonov55c79472013-11-12 13:59:08 +000030 SetCommonFlagDefaults();
Sergey Matveeve96c62a2013-05-20 11:04:43 +000031 cf->external_symbolizer_path = GetEnv("LSAN_SYMBOLIZER_PATH");
Sergey Matveeve96c62a2013-05-20 11:04:43 +000032 cf->malloc_context_size = 30;
Sergey Matveev62519eb2013-07-22 12:38:17 +000033 cf->detect_leaks = true;
Sergey Matveeve96c62a2013-05-20 11:04:43 +000034
35 ParseCommonFlagsFromString(GetEnv("LSAN_OPTIONS"));
36}
37
Sergey Matveev74c88792013-11-25 17:39:36 +000038} // namespace __lsan
39
40using namespace __lsan; // NOLINT
41
42extern "C" void __lsan_init() {
43 CHECK(!lsan_init_is_running);
44 if (lsan_inited)
Sergey Matveeve96c62a2013-05-20 11:04:43 +000045 return;
Sergey Matveev74c88792013-11-25 17:39:36 +000046 lsan_init_is_running = true;
Sergey Matveeve96c62a2013-05-20 11:04:43 +000047 SanitizerToolName = "LeakSanitizer";
48 InitializeCommonFlags();
49 InitializeAllocator();
Sergey Matveeve96c62a2013-05-20 11:04:43 +000050 InitTlsSize();
51 InitializeInterceptors();
52 InitializeThreadRegistry();
53 u32 tid = ThreadCreate(0, 0, true);
54 CHECK_EQ(tid, 0);
55 ThreadStart(tid, GetTid());
Sergey Matveev02d7f322013-06-18 14:44:45 +000056 SetCurrentThread(tid);
Sergey Matveeve96c62a2013-05-20 11:04:43 +000057
58 // Start symbolizer process if necessary.
Alexey Samsonov68c016a2013-09-03 13:22:51 +000059 if (common_flags()->symbolize) {
Peter Collingbournec1a1ed62013-10-25 23:03:29 +000060 Symbolizer::Init(common_flags()->external_symbolizer_path);
61 } else {
62 Symbolizer::Disable();
Sergey Matveeve96c62a2013-05-20 11:04:43 +000063 }
Sergey Matveev4cacb4f2013-05-21 17:56:45 +000064
Sergey Matveevfc1a6122013-05-23 10:24:44 +000065 InitCommonLsan();
Sergey Matveev41f15402013-08-01 14:57:07 +000066 if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit)
67 Atexit(DoLeakCheck);
Sergey Matveev74c88792013-11-25 17:39:36 +000068 lsan_inited = true;
69 lsan_init_is_running = false;
Sergey Matveeve96c62a2013-05-20 11:04:43 +000070}
71