1 //===-- stats_client.cc ---------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Sanitizer statistics gathering. Manages statistics for a module (executable
11 // or DSO) and registers statistics with the process.
13 // This is linked into each individual modle and cannot directly use functions
14 // declared in sanitizer_common.
16 //===----------------------------------------------------------------------===//
19 #define WIN32_LEAN_AND_MEAN
27 #include "sanitizer_common/sanitizer_internal_defs.h"
28 #include "stats/stats.h"
30 using namespace __sanitizer;
34 void *LookupSymbolFromMain(const char *name) {
36 return reinterpret_cast<void *>(GetProcAddress(GetModuleHandle(0), name));
38 return dlsym(RTLD_DEFAULT, name);
44 struct RegisterSanStats {
48 typedef unsigned (*reg_func_t)(StatModule **);
49 reg_func_t reg_func = reinterpret_cast<reg_func_t>(
50 LookupSymbolFromMain("__sanitizer_stats_register"));
52 module_id = reg_func(&list);
56 typedef void (*unreg_func_t)(unsigned);
57 unreg_func_t unreg_func = reinterpret_cast<unreg_func_t>(
58 LookupSymbolFromMain("__sanitizer_stats_unregister"));
60 unreg_func(module_id);
66 extern "C" void __sanitizer_stat_init(StatModule *mod) {
71 extern "C" void __sanitizer_stat_report(StatInfo *s) {
72 s->addr = GET_CALLER_PC();
73 #if defined(_WIN64) && !defined(__clang__)
74 uptr old_data = InterlockedIncrement64(reinterpret_cast<LONG64 *>(&s->data));
75 #elif defined(_WIN32) && !defined(__clang__)
76 uptr old_data = InterlockedIncrement(&s->data);
78 uptr old_data = __sync_fetch_and_add(&s->data, 1);
82 if (CountFromData(old_data + 1) == 0)