mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-09 00:39:32 +03:00
add tracing on linux and freebsd
This commit is contained in:
parent
e125c04081
commit
ea75c745e1
4 changed files with 63 additions and 7 deletions
|
@ -21,10 +21,14 @@ option(MI_BUILD_OBJECT "Build object library" ON)
|
|||
option(MI_BUILD_TESTS "Build test executables" ON)
|
||||
option(MI_DEBUG_TSAN "Build with thread sanitizer (needs clang)" OFF)
|
||||
option(MI_DEBUG_UBSAN "Build with undefined-behavior sanitizer (needs clang++)" OFF)
|
||||
option(MI_DEBUG_TRACE "Store allocation stack trace in each heap block to debug heap block overflows or corruption" OFF)
|
||||
option(MI_CHECK_FULL "Use full internal invariant checking in DEBUG mode (deprecated, use MI_DEBUG_FULL instead)" OFF)
|
||||
option(MI_INSTALL_TOPLEVEL "Install directly into $CMAKE_INSTALL_PREFIX instead of PREFIX/lib/mimalloc-version" OFF)
|
||||
option(MI_USE_LIBATOMIC "Explicitly link with -latomic (on older systems)" OFF)
|
||||
|
||||
set(MI_PADDING_EXTRA 0 CACHE STRING "Specify extra bytes for padding in each heap block (to debug heap block overflows)")
|
||||
|
||||
|
||||
include(GNUInstallDirs)
|
||||
include("cmake/mimalloc-config-version.cmake")
|
||||
|
||||
|
@ -120,6 +124,21 @@ if(MI_DEBUG_FULL)
|
|||
list(APPEND mi_defines MI_DEBUG=3) # full invariant checking
|
||||
endif()
|
||||
|
||||
if(MI_DEBUG_TRACE)
|
||||
if (APPLE)
|
||||
message(WARNING "Cannot enable MI_DEBUG_TRACE on Apple")
|
||||
set(MI_DEBUG_TRACE OFF)
|
||||
else()
|
||||
message(STATUS "Enable allocation trace in each heap block (MI_DEBUG_TRACE=ON)")
|
||||
list(APPEND mi_defines MI_DEBUG_TRACE=1)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(MI_PADDING_EXTRA)
|
||||
message(STATUS "Add extra debug padding to each heap block (MI_PADDING_EXTRA=${MI_PADDING_EXTRA})")
|
||||
list(APPEND mi_defines MI_PADDING_EXTRA=${MI_PADDING_EXTRA})
|
||||
endif()
|
||||
|
||||
if(NOT MI_PADDING)
|
||||
message(STATUS "Disable padding of heap blocks in debug mode (MI_PADDING=OFF)")
|
||||
list(APPEND mi_defines MI_PADDING=0)
|
||||
|
@ -212,9 +231,15 @@ if(WIN32)
|
|||
else()
|
||||
if(NOT ${CMAKE_C_COMPILER} MATCHES "android")
|
||||
list(APPEND mi_libraries pthread)
|
||||
find_library(LIBRT rt)
|
||||
if(LIBRT)
|
||||
list(APPEND mi_libraries ${LIBRT})
|
||||
find_library(MI_LIBRT rt)
|
||||
if(MI_LIBRT)
|
||||
list(APPEND mi_libraries ${MI_LIBRT})
|
||||
endif()
|
||||
if(MI_DEBUG_TRACE)
|
||||
find_library(MI_LIBEXECINFO NAMES execinfo)
|
||||
if (MI_LIBEXECINFO) # on freeBSD
|
||||
list(APPEND mi_libraries ${MI_LIBEXECINFO})
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
@ -270,6 +295,7 @@ else()
|
|||
message(STATUS "C Compiler : ${CMAKE_C_COMPILER}")
|
||||
endif()
|
||||
message(STATUS "Compiler flags : ${mi_cflags}")
|
||||
message(STATUS "Compiler defines : ${mi_defines}")
|
||||
message(STATUS "Build targets : ${mi_build_targets}")
|
||||
message(STATUS "")
|
||||
|
||||
|
|
|
@ -358,6 +358,7 @@ void _mi_stack_trace_capture(void** strace, size_t len, size_t skip) {
|
|||
#include <dbghelp.h>
|
||||
#pragma comment(lib,"dbghelp")
|
||||
void _mi_stack_trace_print(const void* const* strace, size_t len, const mi_block_t* block, size_t bsize, size_t avail) {
|
||||
_mi_fprintf(NULL, NULL, "trace for block %p of size %zu (%zu total in block), allocated at:\n", block, avail, bsize);
|
||||
HANDLE current_process = GetCurrentProcess();
|
||||
SymInitialize(current_process, NULL, TRUE);
|
||||
PSYMBOL_INFO info = (PSYMBOL_INFO)_malloca(sizeof(SYMBOL_INFO) + 256 * sizeof(TCHAR));
|
||||
|
@ -365,7 +366,6 @@ void _mi_stack_trace_print(const void* const* strace, size_t len, const mi_block
|
|||
memset(info, 0, sizeof(info));
|
||||
info->MaxNameLen = 255;
|
||||
info->SizeOfStruct = sizeof(SYMBOL_INFO);
|
||||
_mi_fprintf(NULL, NULL, "for block %p of %zu allocated bytes (%zu total in block), allocated at:\n", block, avail, bsize);
|
||||
for (size_t i = 0; i < len && strace[i] != NULL; i++) {
|
||||
if (SymFromAddr(current_process, (DWORD64)(strace[i]), 0, info)) {
|
||||
_mi_fprintf(NULL, NULL, " frame %2zu: %8p: %s\n", i, strace[i], info->Name);
|
||||
|
@ -375,10 +375,37 @@ void _mi_stack_trace_print(const void* const* strace, size_t len, const mi_block
|
|||
}
|
||||
}
|
||||
}
|
||||
#elif (MI_DEBUG_TRACE > 0) && (defined(__linux__) || defined(__FreeBSD__))
|
||||
#include <execinfo.h>
|
||||
#define MI_TRACE_LEN (64)
|
||||
void _mi_stack_trace_capture(void** strace, size_t len, size_t skip) {
|
||||
if (_mi_preloading()) return;
|
||||
if (!mi_recurse_enter()) return;
|
||||
void* trace[MI_TRACE_LEN];
|
||||
backtrace(trace, MI_TRACE_LEN);
|
||||
skip += 4;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
void* p = (i + skip < MI_TRACE_LEN ? trace[i+skip] : NULL);
|
||||
strace[i] = p;
|
||||
}
|
||||
mi_recurse_exit();
|
||||
}
|
||||
void _mi_stack_trace_print(const void* const* strace, size_t len, const mi_block_t* block, size_t bsize, size_t avail) {
|
||||
_mi_fprintf(NULL, NULL, "trace for block %p of size %zu (%zu total in block), allocated at:\n", block, avail, bsize);
|
||||
char** names = backtrace_symbols((void**)strace, len);
|
||||
for (size_t i = 0; i < len && strace[i] != NULL; i++) {
|
||||
_mi_fprintf(NULL, NULL, " frame %2zu: %8p: %s\n", i, strace[i], (names[i] == NULL ? "<unknown>" : names[i]));
|
||||
}
|
||||
}
|
||||
#else
|
||||
void _mi_capture_stack_trace(void** strace, size_t len, size_t skip) {
|
||||
void _mi_stack_trace_capture(void** strace, size_t len, size_t skip) {
|
||||
MI_UNUSED(strace); MI_UNUSED(len); MI_UNUSED(skip);
|
||||
}
|
||||
void _mi_stack_trace_print(const void* const* strace, size_t len, const mi_block_t* block, size_t bsize, size_t avail) {
|
||||
MI_UNUSED(strace); MI_UNUSED(len); MI_UNUSED(block);
|
||||
MI_UNUSED(bsize); MI_UNUSED(avail);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// --------------------------------------------------------
|
||||
|
|
|
@ -103,6 +103,9 @@ static void* alloc_items(size_t items, random_t r) {
|
|||
for (uintptr_t i = 0; i < items; i++) {
|
||||
p[i] = (items - i) ^ cookie;
|
||||
}
|
||||
if (pick(r)%1000 <= 1) {
|
||||
p[items+1] = 42; // overflow
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue