forked from KDE/heaptrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libheaptrack.cpp
641 lines (532 loc) · 17.6 KB
/
libheaptrack.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
* Copyright 2014 Milian Wolff <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* @file libheaptrack.cpp
*
* @brief Collect raw heaptrack data by overloading heap allocation functions.
*/
#include "libheaptrack.h"
#include <cstdio>
#include <cstdlib>
#include <stdio_ext.h>
#include <fcntl.h>
#include <link.h>
#include <atomic>
#include <string>
#include <memory>
#include <unordered_set>
#include <mutex>
#include <thread>
#include <cinttypes>
#include <boost/algorithm/string/replace.hpp>
#include "tracetree.h"
#include "config.h"
/**
* uncomment this to get extended debug code for known pointers
* there are still some malloc functions I'm missing apparently,
* related to TLS and such I guess
*/
// #define DEBUG_MALLOC_PTRS
using namespace std;
namespace {
enum DebugVerbosity
{
NoDebugOutput,
MinimalOutput,
VerboseOutput,
VeryVerboseOutput,
};
// change this to add more debug output to stderr
constexpr const DebugVerbosity s_debugVerbosity = NoDebugOutput;
/**
* Call this to optionally show debug information but give the compiler
* a hand in removing it all if debug output is disabled.
*/
template<DebugVerbosity debugLevel, typename... Args>
inline void debugLog(const char fmt[], Args... args)
{
if (debugLevel <= s_debugVerbosity) {
flockfile(stderr);
fprintf(stderr, "heaptrack debug [%d]: ", static_cast<int>(debugLevel));
fprintf(stderr, fmt, args...);
fputc('\n', stderr);
funlockfile(stderr);
}
}
/**
* Set to true in an atexit handler. In such conditions, the stop callback
* will not be called.
*/
atomic<bool> s_atexit{false};
/**
* A per-thread handle guard to prevent infinite recursion, which should be
* acquired before doing any special symbol handling.
*/
struct RecursionGuard
{
RecursionGuard()
: wasLocked(isActive)
{
isActive = true;
}
~RecursionGuard()
{
isActive = wasLocked;
}
const bool wasLocked;
static thread_local bool isActive;
};
thread_local bool RecursionGuard::isActive = false;
void writeVersion(FILE* out)
{
fprintf(out, "v %x\n", HEAPTRACK_VERSION);
}
void writeExe(FILE* out)
{
const int BUF_SIZE = 1023;
char buf[BUF_SIZE + 1];
ssize_t size = readlink("/proc/self/exe", buf, BUF_SIZE);
if (size > 0 && size < BUF_SIZE) {
buf[size] = 0;
fprintf(out, "x %s\n", buf);
}
}
void writeCommandLine(FILE* out)
{
fputc('X', out);
const int BUF_SIZE = 4096;
char buf[BUF_SIZE + 1];
auto fd = open("/proc/self/cmdline", O_RDONLY);
int bytesRead = read(fd, buf, BUF_SIZE);
char *end = buf + bytesRead;
for (char *p = buf; p < end;) {
fputc(' ', out);
fputs(p, out);
while (*p++); // skip until start of next 0-terminated section
}
close(fd);
fputc('\n', out);
}
void writeSystemInfo(FILE* out)
{
fprintf(out, "I %lx %lx\n",
sysconf(_SC_PAGESIZE),
sysconf(_SC_PHYS_PAGES));
}
FILE* createFile(const char* fileName)
{
string outputFileName;
if (fileName) {
outputFileName.assign(fileName);
}
if (outputFileName == "-" || outputFileName == "stdout") {
debugLog<VerboseOutput>("%s", "will write to stdout");
return stdout;
} else if (outputFileName == "stderr") {
debugLog<VerboseOutput>("%s", "will write to stderr");
return stderr;
}
if (outputFileName.empty()) {
// env var might not be set when linked directly into an executable
outputFileName = "heaptrack.$$";
}
boost::replace_all(outputFileName, "$$", to_string(getpid()));
auto out = fopen(outputFileName.c_str(), "w");
debugLog<VerboseOutput>("will write to %s/%p\n", outputFileName.c_str(), out);
// we do our own locking, this speeds up the writing significantly
__fsetlocking(out, FSETLOCKING_BYCALLER);
return out;
}
/**
* Thread-Safe heaptrack API
*
* The only critical section in libheaptrack is the output of the data, dl_iterate_phdr
* calls, as well as initialization and shutdown.
*
* This uses a spinlock, instead of a std::mutex, as the latter can lead to deadlocks
* on destruction. The spinlock is "simple", and OK to only guard the small sections.
*/
class HeapTrack
{
public:
HeapTrack(const RecursionGuard& /*recursionGuard*/)
: HeapTrack([] { return true; })
{
}
~HeapTrack()
{
debugLog<VeryVerboseOutput>("%s", "releasing lock");
s_locked.store(false, memory_order_release);
}
void initialize(const char* fileName,
heaptrack_callback_t initBeforeCallback,
heaptrack_callback_initialized_t initAfterCallback,
heaptrack_callback_t stopCallback)
{
debugLog<MinimalOutput>("initializing: %s", fileName);
if (s_data) {
debugLog<MinimalOutput>("%s", "already initialized");
return;
}
if (initBeforeCallback) {
debugLog<MinimalOutput>("%s", "calling initBeforeCallback");
initBeforeCallback();
debugLog<MinimalOutput>("%s", "done calling initBeforeCallback");
}
// do some once-only initializations
static once_flag once;
call_once(once, [] {
debugLog<MinimalOutput>("%s", "doing once-only initialization");
// configure libunwind for better speed
if (unw_set_caching_policy(unw_local_addr_space, UNW_CACHE_PER_THREAD)) {
fprintf(stderr, "WARNING: Failed to enable per-thread libunwind caching.\n");
}
#if HAVE_UNW_SET_CACHE_SIZE
if (unw_set_cache_size(unw_local_addr_space, 1024)) {
fprintf(stderr, "WARNING: Failed to set libunwind cache size.\n");
}
#endif
// do not trace forked child processes
// TODO: make this configurable
pthread_atfork(&prepare_fork, &parent_fork, &child_fork);
atexit([] () {
debugLog<MinimalOutput>("%s", "atexit()");
s_atexit.store(true);
heaptrack_stop();
});
});
FILE* out = createFile(fileName);
if (!out) {
fprintf(stderr, "ERROR: Failed to open heaptrack output file: %s\n", fileName);
if (stopCallback) {
stopCallback();
}
return;
}
writeVersion(out);
writeExe(out);
writeCommandLine(out);
writeSystemInfo(out);
s_data = new LockedData(out, stopCallback);
if (initAfterCallback) {
debugLog<MinimalOutput>("%s", "calling initAfterCallback");
initAfterCallback(out);
debugLog<MinimalOutput>("%s", "calling initAfterCallback done");
}
debugLog<MinimalOutput>("%s", "initialization done");
}
void shutdown()
{
if (!s_data) {
return;
}
debugLog<MinimalOutput>("%s", "shutdown()");
writeTimestamp();
writeRSS();
// NOTE: we leak heaptrack data on exit, intentionally
// This way, we can be sure to get all static deallocations.
if (!s_atexit) {
delete s_data;
s_data = nullptr;
}
debugLog<MinimalOutput>("%s", "shutdown() done");
}
void invalidateModuleCache()
{
if (!s_data) {
return;
}
s_data->moduleCacheDirty = true;
}
void writeTimestamp()
{
if (!s_data || !s_data->out) {
return;
}
auto elapsed = chrono::duration_cast<chrono::milliseconds>(clock::now() - s_data->start);
debugLog<VeryVerboseOutput>("writeTimestamp(%" PRIx64 ")", elapsed.count());
if (fprintf(s_data->out, "c %" PRIx64 "\n", elapsed.count()) < 0) {
writeError();
return;
}
}
void writeRSS()
{
if (!s_data || !s_data->out || !s_data->procStatm) {
return;
}
// read RSS in pages from statm, then rewind for next read
size_t rss = 0;
fscanf(s_data->procStatm, "%*x %zx", &rss);
rewind(s_data->procStatm);
// TODO: compare to rusage.ru_maxrss (getrusage) to find "real" peak?
// TODO: use custom allocators with known page sizes to prevent tainting
// the RSS numbers with heaptrack-internal data
if (fprintf(s_data->out, "R %zx\n", rss) < 0) {
writeError();
return;
}
}
void handleMalloc(void* ptr, size_t size, const Trace &trace)
{
if (!s_data || !s_data->out) {
return;
}
updateModuleCache();
const auto index = s_data->traceTree.index(trace, s_data->out);
#ifdef DEBUG_MALLOC_PTRS
auto it = s_data->known.find(ptr);
assert(it == s_data->known.end());
s_data->known.insert(ptr);
#endif
if (fprintf(s_data->out, "+ %zx %x %" PRIxPTR "\n", size, index, reinterpret_cast<uintptr_t>(ptr)) < 0) {
writeError();
return;
}
}
void handleFree(void* ptr)
{
if (!s_data || !s_data->out) {
return;
}
#ifdef DEBUG_MALLOC_PTRS
auto it = s_data->known.find(ptr);
assert(it != s_data->known.end());
s_data->known.erase(it);
#endif
if (fprintf(s_data->out, "- %" PRIxPTR "\n", reinterpret_cast<uintptr_t>(ptr)) < 0) {
writeError();
return;
}
}
private:
static int dl_iterate_phdr_callback(struct dl_phdr_info *info, size_t /*size*/, void *data)
{
auto heaptrack = reinterpret_cast<HeapTrack*>(data);
const char *fileName = info->dlpi_name;
if (!fileName || !fileName[0]) {
fileName = "x";
}
debugLog<VerboseOutput>("dlopen_notify_callback: %s %zx", fileName, info->dlpi_addr);
if (fprintf(heaptrack->s_data->out, "m %s %zx", fileName, info->dlpi_addr) < 0) {
heaptrack->writeError();
return 1;
}
for (int i = 0; i < info->dlpi_phnum; i++) {
const auto& phdr = info->dlpi_phdr[i];
if (phdr.p_type == PT_LOAD) {
if (fprintf(heaptrack->s_data->out, " %zx %zx", phdr.p_vaddr, phdr.p_memsz) < 0) {
heaptrack->writeError();
return 1;
}
}
}
if (fputc('\n', heaptrack->s_data->out) == EOF) {
heaptrack->writeError();
return 1;
}
return 0;
}
static void prepare_fork()
{
debugLog<MinimalOutput>("%s", "prepare_fork()");
// don't do any custom malloc handling while inside fork
RecursionGuard::isActive = true;
}
static void parent_fork()
{
debugLog<MinimalOutput>("%s", "parent_fork()");
// the parent process can now continue its custom malloc tracking
RecursionGuard::isActive = false;
}
static void child_fork()
{
debugLog<MinimalOutput>("%s", "child_fork()");
// but the forked child process cleans up itself
// this is important to prevent two processes writing to the same file
s_data = nullptr;
RecursionGuard::isActive = true;
}
void updateModuleCache()
{
if (!s_data || !s_data->out || !s_data->moduleCacheDirty) {
return;
}
debugLog<MinimalOutput>("%s", "updateModuleCache()");
if (fputs("m -\n", s_data->out) == EOF) {
writeError();
return;
}
dl_iterate_phdr(&dl_iterate_phdr_callback, this);
s_data->moduleCacheDirty = false;
}
void writeError()
{
debugLog<MinimalOutput>("write error %d/%s", errno, strerror(errno));
s_data->out = nullptr;
shutdown();
}
template<typename AdditionalLockCheck>
HeapTrack(AdditionalLockCheck lockCheck)
{
debugLog<VeryVerboseOutput>("%s", "acquiring lock");
while (s_locked.exchange(true, memory_order_acquire) && lockCheck()) {
this_thread::sleep_for(chrono::microseconds(1));
}
debugLog<VeryVerboseOutput>("%s", "lock acquired");
}
using clock = chrono::steady_clock;
struct LockedData
{
LockedData(FILE* out, heaptrack_callback_t stopCallback)
: out(out)
, stopCallback(stopCallback)
{
debugLog<MinimalOutput>("%s", "constructing LockedData");
procStatm = fopen("/proc/self/statm", "r");
if (!procStatm) {
fprintf(stderr, "WARNING: Failed to open /proc/self/statm for reading.\n");
}
timerThread = thread([&] () {
RecursionGuard::isActive = true;
debugLog<MinimalOutput>("%s", "timer thread started");
while (!stopTimerThread) {
// TODO: make interval customizable
this_thread::sleep_for(chrono::milliseconds(10));
HeapTrack heaptrack([&] { return !stopTimerThread.load(); });
if (!stopTimerThread) {
heaptrack.writeTimestamp();
heaptrack.writeRSS();
}
}
});
}
~LockedData()
{
debugLog<MinimalOutput>("%s", "destroying LockedData");
stopTimerThread = true;
if (timerThread.joinable()) {
try {
timerThread.join();
} catch(std::system_error) {}
}
if (out) {
fclose(out);
}
if (procStatm) {
fclose(procStatm);
}
if (stopCallback && !s_atexit) {
stopCallback();
}
debugLog<MinimalOutput>("%s", "done destroying LockedData");
}
/**
* Note: We use the C stdio API here for performance reasons.
* Esp. in multi-threaded environments this is much faster
* to produce non-per-line-interleaved output.
*/
FILE* out = nullptr;
/// /proc/self/statm file stream to read RSS value from
FILE* procStatm = nullptr;
/**
* Calls to dlopen/dlclose mark the cache as dirty.
* When this happened, all modules and their section addresses
* must be found again via dl_iterate_phdr before we output the
* next instruction pointer. Otherwise, heaptrack_interpret might
* encounter IPs of an unknown/invalid module.
*/
bool moduleCacheDirty = true;
TraceTree traceTree;
const chrono::time_point<clock> start = clock::now();
atomic<bool> stopTimerThread{false};
thread timerThread;
heaptrack_callback_t stopCallback = nullptr;
#ifdef DEBUG_MALLOC_PTRS
unordered_set<void*> known;
#endif
};
static atomic<bool> s_locked;
static LockedData* s_data;
};
atomic<bool> HeapTrack::s_locked{false};
HeapTrack::LockedData* HeapTrack::s_data{nullptr};
}
extern "C" {
void heaptrack_init(const char *outputFileName,
heaptrack_callback_t initBeforeCallback,
heaptrack_callback_initialized_t initAfterCallback,
heaptrack_callback_t stopCallback)
{
RecursionGuard guard;
debugLog<MinimalOutput>("heaptrack_init(%s)", outputFileName);
HeapTrack heaptrack(guard);
heaptrack.initialize(outputFileName,
initBeforeCallback, initAfterCallback,
stopCallback);
}
void heaptrack_stop()
{
RecursionGuard guard;
debugLog<MinimalOutput>("%s", "heaptrack_stop()");
HeapTrack heaptrack(guard);
heaptrack.shutdown();
}
void heaptrack_malloc(void* ptr, size_t size)
{
if (ptr && !RecursionGuard::isActive) {
RecursionGuard guard;
debugLog<VeryVerboseOutput>("heaptrack_malloc(%p, %zu)", ptr, size);
Trace trace;
trace.fill(2 + HEAPTRACK_DEBUG_BUILD);
HeapTrack heaptrack(guard);
heaptrack.handleMalloc(ptr, size, trace);
}
}
void heaptrack_free(void* ptr)
{
if (ptr && !RecursionGuard::isActive) {
RecursionGuard guard;
debugLog<VeryVerboseOutput>("heaptrack_free(%p)", ptr);
HeapTrack heaptrack(guard);
heaptrack.handleFree(ptr);
}
}
void heaptrack_realloc(void* ptr_in, size_t size, void* ptr_out)
{
if (ptr_out && !RecursionGuard::isActive) {
RecursionGuard guard;
debugLog<VeryVerboseOutput>("heaptrack_realloc(%p, %zu, %p)", ptr_in, size, ptr_out);
Trace trace;
trace.fill(2 + HEAPTRACK_DEBUG_BUILD);
HeapTrack heaptrack(guard);
if (ptr_in) {
heaptrack.handleFree(ptr_in);
}
heaptrack.handleMalloc(ptr_out, size, trace);
}
}
void heaptrack_invalidate_module_cache()
{
RecursionGuard guard;
debugLog<VerboseOutput>("%s", "heaptrack_invalidate_module_cache()");
HeapTrack heaptrack(guard);
heaptrack.invalidateModuleCache();
}
}