Skip to content

Commit b17145b

Browse files
committed
rt: Track backtraces of all allocations with RUSTRT_TRACK_ALLOCATIONS=3
1 parent 3ff0136 commit b17145b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/rt/memory_region.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include "rust_internal.h"
22
#include "memory_region.h"
33

4+
#if RUSTRT_TRACK_ALLOCATIONS >= 3
5+
#include <execinfo.h>
6+
#endif
7+
48
#if RUSTRT_TRACK_ALLOCATIONS >= 1
59
// For some platforms, 16 byte alignment is required.
610
# define PTR_SIZE 16
@@ -148,6 +152,13 @@ memory_region::~memory_region() {
148152
header->tag,
149153
(uintptr_t) get_data(header));
150154
++leak_count;
155+
156+
# if RUSTRT_TRACK_ALLOCATIONS >= 3
157+
if (_detailed_leaks) {
158+
backtrace_symbols_fd(header->bt + 1,
159+
header->btframes - 1, 2);
160+
}
161+
# endif
151162
}
152163
}
153164
assert(leak_count == _live_allocations);
@@ -199,6 +210,12 @@ memory_region::claim_alloc(void *mem) {
199210
if (_synchronized) { _lock.unlock(); }
200211
# endif
201212

213+
# if RUSTRT_TRACK_ALLOCATIONS >= 3
214+
if (_detailed_leaks) {
215+
alloc->btframes = ::backtrace(alloc->bt, 32);
216+
}
217+
# endif
218+
202219
add_alloc();
203220
}
204221

src/rt/memory_region.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// 0 --- no headers, no debugging support
1717
// 1 --- support poison, but do not track allocations
1818
// 2 --- track allocations in detail
19+
// 3 --- record backtraces of every allocation
1920
//
2021
// NB: please do not commit code with level 2. It's
2122
// hugely expensive and should only be used as a last resort.
@@ -31,6 +32,10 @@ class memory_region {
3132
int index;
3233
const char *tag;
3334
uint32_t size;
35+
# if RUSTRT_TRACK_ALLOCATIONS >= 3
36+
void *bt[32];
37+
int btframes;
38+
# endif
3439
# endif
3540
};
3641

0 commit comments

Comments
 (0)