forked from desktop-app/cmake_helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux_allocation_tracer.cpp
171 lines (143 loc) · 4.53 KB
/
linux_allocation_tracer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/blob/master/LEGAL
//
#include "linux_allocation_tracer.h"
#include <atomic>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <malloc.h>
#include <dlfcn.h>
#include <stdio.h>
namespace {
std::atomic<void(*)(size_t, void *)> MallocLogger;
std::atomic<void(*)(size_t, void *)> VallocLogger;
std::atomic<void(*)(size_t, void *)> PVallocLogger;
std::atomic<void(*)(size_t, size_t, void *)> CallocLogger;
std::atomic<void(*)(void *, size_t, void *)> ReallocLogger;
std::atomic<void(*)(size_t, size_t, void *)> MemAlignLogger;
std::atomic<void(*)(size_t, size_t, void *)> AlignedAllocLogger;
std::atomic<void(*)(size_t, size_t, void *)> PosixMemAlignLogger;
std::atomic<void(*)(void *)> FreeLogger;
} // namespace
extern "C" {
//void *__real___malloc(size_t size);
void *__real___libc_malloc(size_t size);
void *__real_malloc(size_t size);
void *__real_valloc(size_t size);
void *__real_pvalloc(size_t size);
void *__real_calloc(size_t num, size_t size);
void *__real_realloc(void *ptr, size_t size);
void *__real_memalign(size_t alignment, size_t size);
void *__real_aligned_alloc(size_t alignment, size_t size);
int __real_posix_memalign(void **memptr, size_t alignment, size_t size);
void __real_free(void *ptr);
// void *__wrap___malloc(size_t size) {
// const auto result = __real___malloc(size);
// if (const auto logger = MallocLogger.load()) {
// logger(size, result);
// }
// return result;
// }
void *__wrap___libc_malloc(size_t size) {
const auto result = __real___libc_malloc(size);
if (const auto logger = MallocLogger.load()) {
logger(size, result);
}
return result;
}
void *__wrap_malloc(size_t size) {
const auto result = __real_malloc(size);
if (const auto logger = MallocLogger.load()) {
logger(size, result);
}
return result;
}
void *__wrap_valloc(size_t size) {
const auto result = __real_valloc(size);
if (const auto logger = VallocLogger.load()) {
logger(size, result);
}
return result;
}
void *__wrap_pvalloc(size_t size) {
const auto result = __real_pvalloc(size);
if (const auto logger = PVallocLogger.load()) {
logger(size, result);
}
return result;
}
void *__wrap_calloc(size_t num, size_t size) {
const auto result = __real_calloc(num, size);
if (const auto logger = CallocLogger.load()) {
logger(num, size, result);
}
return result;
}
void *__wrap_realloc(void *ptr, size_t size) {
const auto result = __real_realloc(ptr, size);
if (const auto logger = ReallocLogger.load()) {
logger(ptr, size, result);
}
return result;
}
void *__wrap_memalign(size_t alignment, size_t size) {
const auto result = __real_memalign(alignment, size);
if (const auto logger = MemAlignLogger.load()) {
logger(alignment, size, result);
}
return result;
}
void *__wrap_aligned_alloc(size_t alignment, size_t size) {
const auto result = __real_aligned_alloc(alignment, size);
if (const auto logger = AlignedAllocLogger.load()) {
logger(alignment, size, result);
}
return result;
}
int __wrap_posix_memalign(void **memptr, size_t alignment, size_t size) {
const auto result = __real_posix_memalign(memptr, alignment, size);
if (!result) {
if (const auto logger = PosixMemAlignLogger.load()) {
logger(alignment, size, *memptr);
}
}
return result;
}
void __wrap_free(void *ptr) {
if (const auto logger = FreeLogger.load()) {
logger(ptr);
}
__real_free(ptr);
}
} // extern "C"
void SetMallocLogger(void (*logger)(size_t, void *)) {
MallocLogger = logger;
}
void SetVallocLogger(void (*logger)(size_t, void *)) {
VallocLogger = logger;
}
void SetPVallocLogger(void (*logger)(size_t, void *)) {
PVallocLogger = logger;
}
void SetCallocLogger(void (*logger)(size_t, size_t, void *)) {
CallocLogger = logger;
}
void SetReallocLogger(void (*logger)(void *, size_t, void *)) {
ReallocLogger = logger;
}
void SetMemAlignLogger(void (*logger)(size_t, size_t, void *)) {
MemAlignLogger = logger;
}
void SetAlignedAllocLogger(void (*logger)(size_t, size_t, void *)) {
AlignedAllocLogger = logger;
}
void SetPosixMemAlignLogger(void (*logger)(size_t, size_t, void *)) {
PosixMemAlignLogger = logger;
}
void SetFreeLogger(void (*logger)(void *)) {
FreeLogger = logger;
}