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 //===----------------------------------------------------------------------===//
26 #include "sanitizer_common/sanitizer_internal_defs.h"
27 #include "stats/stats.h"
29 using namespace __sanitizer;
33 void *LookupSymbolFromMain(const char *name) {
35 return reinterpret_cast<void *>(GetProcAddress(GetModuleHandle(0), name));
37 return dlsym(RTLD_DEFAULT, name);
43 struct RegisterSanStats {
47 typedef unsigned (*reg_func_t)(StatModule **);
48 reg_func_t reg_func = reinterpret_cast<reg_func_t>(
49 LookupSymbolFromMain("__sanitizer_stats_register"));
51 module_id = reg_func(&list);
55 typedef void (*unreg_func_t)(unsigned);
56 unreg_func_t unreg_func = reinterpret_cast<unreg_func_t>(
57 LookupSymbolFromMain("__sanitizer_stats_unregister"));
59 unreg_func(module_id);
65 extern "C" void __sanitizer_stat_init(StatModule *mod) {
70 extern "C" void __sanitizer_stat_report(StatInfo *s) {
71 s->addr = GET_CALLER_PC();
72 #if defined(_WIN64) && !defined(__clang__)
73 uptr old_data = InterlockedIncrement64(reinterpret_cast<LONG64 *>(&s->data));
74 #elif defined(_WIN32) && !defined(__clang__)
75 uptr old_data = InterlockedIncrement(&s->data);
77 uptr old_data = __sync_fetch_and_add(&s->data, 1);
81 if (CountFromData(old_data + 1) == 0)