From b17145b4ae36f78ed9b53b8875b6822ae59da1ae Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 26 Mar 2012 18:58:40 -0700 Subject: [PATCH] rt: Track backtraces of all allocations with RUSTRT_TRACK_ALLOCATIONS=3 --- src/rt/memory_region.cpp | 17 +++++++++++++++++ src/rt/memory_region.h | 5 +++++ 2 files changed, 22 insertions(+) diff --git a/src/rt/memory_region.cpp b/src/rt/memory_region.cpp index e4130e80f5ac..770691e88eac 100644 --- a/src/rt/memory_region.cpp +++ b/src/rt/memory_region.cpp @@ -1,6 +1,10 @@ #include "rust_internal.h" #include "memory_region.h" +#if RUSTRT_TRACK_ALLOCATIONS >= 3 +#include +#endif + #if RUSTRT_TRACK_ALLOCATIONS >= 1 // For some platforms, 16 byte alignment is required. # define PTR_SIZE 16 @@ -148,6 +152,13 @@ memory_region::~memory_region() { header->tag, (uintptr_t) get_data(header)); ++leak_count; + +# if RUSTRT_TRACK_ALLOCATIONS >= 3 + if (_detailed_leaks) { + backtrace_symbols_fd(header->bt + 1, + header->btframes - 1, 2); + } +# endif } } assert(leak_count == _live_allocations); @@ -199,6 +210,12 @@ memory_region::claim_alloc(void *mem) { if (_synchronized) { _lock.unlock(); } # endif +# if RUSTRT_TRACK_ALLOCATIONS >= 3 + if (_detailed_leaks) { + alloc->btframes = ::backtrace(alloc->bt, 32); + } +# endif + add_alloc(); } diff --git a/src/rt/memory_region.h b/src/rt/memory_region.h index a72c87f9be94..57108ae58a8b 100644 --- a/src/rt/memory_region.h +++ b/src/rt/memory_region.h @@ -16,6 +16,7 @@ // 0 --- no headers, no debugging support // 1 --- support poison, but do not track allocations // 2 --- track allocations in detail +// 3 --- record backtraces of every allocation // // NB: please do not commit code with level 2. It's // hugely expensive and should only be used as a last resort. @@ -31,6 +32,10 @@ private: int index; const char *tag; uint32_t size; +# if RUSTRT_TRACK_ALLOCATIONS >= 3 + void *bt[32]; + int btframes; +# endif # endif };