2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 01:59:26 +00:00

Add the call function tracking to isc_mem API

As we already track __func__, __FILE__, __LINE__ triplet in most places,
add the function tracking to the isc_mem tracking API.
This commit is contained in:
Ondřej Surý 2024-09-09 13:59:43 +02:00
parent eab9fc22e7
commit 1fae6ccea1
No known key found for this signature in database
GPG Key ID: 2820F37E873DEA41
4 changed files with 49 additions and 44 deletions

View File

@ -114,14 +114,14 @@ unregister_algorithms(void) {
static void * static void *
isc__crypto_malloc_ex(size_t size, const char *file, int line) { isc__crypto_malloc_ex(size_t size, const char *file, int line) {
return isc__mem_allocate(isc__crypto_mctx, size, 0, file, return isc__mem_allocate(isc__crypto_mctx, size, 0, __func__, file,
(unsigned int)line); (unsigned int)line);
} }
static void * static void *
isc__crypto_realloc_ex(void *ptr, size_t size, const char *file, int line) { isc__crypto_realloc_ex(void *ptr, size_t size, const char *file, int line) {
return isc__mem_reallocate(isc__crypto_mctx, ptr, size, 0, file, return isc__mem_reallocate(isc__crypto_mctx, ptr, size, 0, __func__,
(unsigned int)line); file, (unsigned int)line);
} }
static void static void
@ -130,7 +130,7 @@ isc__crypto_free_ex(void *ptr, const char *file, int line) {
return; return;
} }
if (isc__crypto_mctx != NULL) { if (isc__crypto_mctx != NULL) {
isc__mem_free(isc__crypto_mctx, ptr, 0, file, isc__mem_free(isc__crypto_mctx, ptr, 0, __func__, file,
(unsigned int)line); (unsigned int)line);
} }
} }

View File

@ -65,8 +65,8 @@ extern unsigned int isc_mem_defaultflags;
/*@}*/ /*@}*/
#if ISC_MEM_TRACKLINES #if ISC_MEM_TRACKLINES
#define _ISC_MEM_FILELINE , __FILE__, __LINE__ #define _ISC_MEM_FILELINE , __func__, __FILE__, __LINE__
#define _ISC_MEM_FLARG , const char *, unsigned int #define _ISC_MEM_FLARG , const char *, const char *, unsigned int
#else /* if ISC_MEM_TRACKLINES */ #else /* if ISC_MEM_TRACKLINES */
#define _ISC_MEM_FILELINE #define _ISC_MEM_FILELINE
#define _ISC_MEM_FLARG #define _ISC_MEM_FLARG

View File

@ -86,14 +86,15 @@ struct debuglink {
ISC_LINK(debuglink_t) link; ISC_LINK(debuglink_t) link;
const void *ptr; const void *ptr;
size_t size; size_t size;
const char *func;
const char *file; const char *file;
unsigned int line; unsigned int line;
}; };
typedef ISC_LIST(debuglink_t) debuglist_t; typedef ISC_LIST(debuglink_t) debuglist_t;
#define FLARG_PASS , file, line #define FLARG_PASS , func, file, line
#define FLARG , const char *file, unsigned int line #define FLARG , const char *func, const char *file, unsigned int line
#else /* if ISC_MEM_TRACKLINES */ #else /* if ISC_MEM_TRACKLINES */
#define FLARG_PASS #define FLARG_PASS
#define FLARG #define FLARG
@ -164,8 +165,8 @@ struct isc_mempool {
*/ */
#if !ISC_MEM_TRACKLINES #if !ISC_MEM_TRACKLINES
#define ADD_TRACE(mctx, ptr, size, file, line) #define ADD_TRACE(mctx, ptr, size, func, file, line)
#define DELETE_TRACE(mctx, ptr, size, file, line) #define DELETE_TRACE(mctx, ptr, size, func, file, line)
#define ISC_MEMFUNC_SCOPE #define ISC_MEMFUNC_SCOPE
#else /* if !ISC_MEM_TRACKLINES */ #else /* if !ISC_MEM_TRACKLINES */
#define TRACE_OR_RECORD (ISC_MEM_DEBUGTRACE | ISC_MEM_DEBUGRECORD) #define TRACE_OR_RECORD (ISC_MEM_DEBUGTRACE | ISC_MEM_DEBUGRECORD)
@ -173,14 +174,14 @@ struct isc_mempool {
#define SHOULD_TRACE_OR_RECORD(mctx, ptr) \ #define SHOULD_TRACE_OR_RECORD(mctx, ptr) \
(((mctx)->debugging & TRACE_OR_RECORD) != 0 && ptr != NULL) (((mctx)->debugging & TRACE_OR_RECORD) != 0 && ptr != NULL)
#define ADD_TRACE(mctx, ptr, size, file, line) \ #define ADD_TRACE(mctx, ptr, size, func, file, line) \
if (SHOULD_TRACE_OR_RECORD(mctx, ptr)) { \ if (SHOULD_TRACE_OR_RECORD(mctx, ptr)) { \
add_trace_entry(mctx, ptr, size, file, line); \ add_trace_entry(mctx, ptr, size, func, file, line); \
} }
#define DELETE_TRACE(mctx, ptr, size, file, line) \ #define DELETE_TRACE(mctx, ptr, size, func, file, line) \
if (SHOULD_TRACE_OR_RECORD(mctx, ptr)) { \ if (SHOULD_TRACE_OR_RECORD(mctx, ptr)) { \
delete_trace_entry(mctx, ptr, size, file, line); \ delete_trace_entry(mctx, ptr, size, func, file, line); \
} }
static void static void
@ -200,8 +201,9 @@ add_trace_entry(isc_mem_t *mctx, const void *ptr, size_t size FLARG) {
MCTXLOCK(mctx); MCTXLOCK(mctx);
if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) { if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "add %p size %zu file %s line %u mctx %p\n", fprintf(stderr,
ptr, size, file, line, mctx); "add %p size %zu func %s file %s line %u mctx %p\n",
ptr, size, func, file, line, mctx);
} }
if (mctx->debuglist == NULL) { if (mctx->debuglist == NULL) {
@ -225,6 +227,7 @@ add_trace_entry(isc_mem_t *mctx, const void *ptr, size_t size FLARG) {
ISC_LINK_INIT(dl, link); ISC_LINK_INIT(dl, link);
dl->ptr = ptr; dl->ptr = ptr;
dl->size = size; dl->size = size;
dl->func = func;
dl->file = file; dl->file = file;
dl->line = line; dl->line = line;
@ -235,8 +238,7 @@ unlock:
} }
static void static void
delete_trace_entry(isc_mem_t *mctx, const void *ptr, size_t size, delete_trace_entry(isc_mem_t *mctx, const void *ptr, size_t size FLARG) {
const char *file, unsigned int line) {
debuglink_t *dl = NULL; debuglink_t *dl = NULL;
uint32_t hash; uint32_t hash;
uint32_t idx; uint32_t idx;
@ -244,8 +246,9 @@ delete_trace_entry(isc_mem_t *mctx, const void *ptr, size_t size,
MCTXLOCK(mctx); MCTXLOCK(mctx);
if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) { if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "del %p size %zu file %s line %u mctx %p\n", fprintf(stderr,
ptr, size, file, line, mctx); "del %p size %zu func %s file %s line %u mctx %p\n",
ptr, size, func, file, line, mctx);
} }
if (mctx->debuglist == NULL) { if (mctx->debuglist == NULL) {
@ -576,7 +579,7 @@ isc__mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size,
isc__mem_put(ctx, ptr, size, flags FLARG_PASS); isc__mem_put(ctx, ptr, size, flags FLARG_PASS);
#if ISC_MEM_TRACE #if ISC_MEM_TRACE
isc_mem__detach(&ctx, __func__, file, line); isc_mem__detach(&ctx, func, file, line);
#else #else
isc_mem_detach(&ctx); isc_mem_detach(&ctx);
#endif #endif
@ -591,7 +594,7 @@ isc__mem_get(isc_mem_t *ctx, size_t size, int flags FLARG) {
ptr = mem_get(ctx, size, flags); ptr = mem_get(ctx, size, flags);
mem_getstats(ctx, size); mem_getstats(ctx, size);
ADD_TRACE(ctx, ptr, size, file, line); ADD_TRACE(ctx, ptr, size, func, file, line);
return ptr; return ptr;
} }
@ -600,7 +603,7 @@ void
isc__mem_put(isc_mem_t *ctx, void *ptr, size_t size, int flags FLARG) { isc__mem_put(isc_mem_t *ctx, void *ptr, size_t size, int flags FLARG) {
REQUIRE(VALID_CONTEXT(ctx)); REQUIRE(VALID_CONTEXT(ctx));
DELETE_TRACE(ctx, ptr, size, file, line); DELETE_TRACE(ctx, ptr, size, func, file, line);
mem_putstats(ctx, size); mem_putstats(ctx, size);
mem_put(ctx, ptr, size, flags); mem_put(ctx, ptr, size, flags);
@ -697,7 +700,7 @@ isc__mem_allocate(isc_mem_t *ctx, size_t size, int flags FLARG) {
size = sallocx(ptr, flags | ctx->jemalloc_flags); size = sallocx(ptr, flags | ctx->jemalloc_flags);
mem_getstats(ctx, size); mem_getstats(ctx, size);
ADD_TRACE(ctx, ptr, size, file, line); ADD_TRACE(ctx, ptr, size, func, file, line);
return ptr; return ptr;
} }
@ -713,13 +716,13 @@ isc__mem_reget(isc_mem_t *ctx, void *old_ptr, size_t old_size, size_t new_size,
} else if (new_size == 0) { } else if (new_size == 0) {
isc__mem_put(ctx, old_ptr, old_size, flags FLARG_PASS); isc__mem_put(ctx, old_ptr, old_size, flags FLARG_PASS);
} else { } else {
DELETE_TRACE(ctx, old_ptr, old_size, file, line); DELETE_TRACE(ctx, old_ptr, old_size, func, file, line);
mem_putstats(ctx, old_size); mem_putstats(ctx, old_size);
new_ptr = mem_realloc(ctx, old_ptr, old_size, new_size, flags); new_ptr = mem_realloc(ctx, old_ptr, old_size, new_size, flags);
mem_getstats(ctx, new_size); mem_getstats(ctx, new_size);
ADD_TRACE(ctx, new_ptr, new_size, file, line); ADD_TRACE(ctx, new_ptr, new_size, func, file, line);
/* /*
* We want to postpone the call to water in edge case * We want to postpone the call to water in edge case
@ -745,7 +748,7 @@ isc__mem_reallocate(isc_mem_t *ctx, void *old_ptr, size_t new_size,
} else { } else {
size_t old_size = sallocx(old_ptr, flags | ctx->jemalloc_flags); size_t old_size = sallocx(old_ptr, flags | ctx->jemalloc_flags);
DELETE_TRACE(ctx, old_ptr, old_size, file, line); DELETE_TRACE(ctx, old_ptr, old_size, func, file, line);
mem_putstats(ctx, old_size); mem_putstats(ctx, old_size);
new_ptr = mem_realloc(ctx, old_ptr, old_size, new_size, flags); new_ptr = mem_realloc(ctx, old_ptr, old_size, new_size, flags);
@ -754,7 +757,7 @@ isc__mem_reallocate(isc_mem_t *ctx, void *old_ptr, size_t new_size,
new_size = sallocx(new_ptr, flags | ctx->jemalloc_flags); new_size = sallocx(new_ptr, flags | ctx->jemalloc_flags);
mem_getstats(ctx, new_size); mem_getstats(ctx, new_size);
ADD_TRACE(ctx, new_ptr, new_size, file, line); ADD_TRACE(ctx, new_ptr, new_size, func, file, line);
/* /*
* We want to postpone the call to water in edge case * We want to postpone the call to water in edge case
@ -775,7 +778,7 @@ isc__mem_free(isc_mem_t *ctx, void *ptr, int flags FLARG) {
size = sallocx(ptr, flags | ctx->jemalloc_flags); size = sallocx(ptr, flags | ctx->jemalloc_flags);
DELETE_TRACE(ctx, ptr, size, file, line); DELETE_TRACE(ctx, ptr, size, func, file, line);
mem_putstats(ctx, size); mem_putstats(ctx, size);
mem_put(ctx, ptr, size, flags); mem_put(ctx, ptr, size, flags);
@ -960,8 +963,9 @@ isc__mempool_create(isc_mem_t *restrict mctx, const size_t element_size,
#if ISC_MEM_TRACKLINES #if ISC_MEM_TRACKLINES
if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) { if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "create pool %p file %s line %u mctx %p\n", fprintf(stderr,
mpctx, file, line, mctx); "create pool %p func %s file %s line %u mctx %p\n",
mpctx, func, file, line, mctx);
} }
#endif /* ISC_MEM_TRACKLINES */ #endif /* ISC_MEM_TRACKLINES */
@ -1000,8 +1004,9 @@ isc__mempool_destroy(isc_mempool_t **restrict mpctxp FLARG) {
#if ISC_MEM_TRACKLINES #if ISC_MEM_TRACKLINES
if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) { if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "destroy pool %p file %s line %u mctx %p\n", fprintf(stderr,
mpctx, file, line, mctx); "destroy pool %p func %s file %s line %u mctx %p\n",
mpctx, func, file, line, mctx);
} }
#endif #endif
@ -1073,7 +1078,7 @@ isc__mempool_get(isc_mempool_t *restrict mpctx FLARG) {
mpctx->freecount--; mpctx->freecount--;
mpctx->gets++; mpctx->gets++;
ADD_TRACE(mpctx->mctx, item, mpctx->size, file, line); ADD_TRACE(mpctx->mctx, item, mpctx->size, func, file, line);
return item; return item;
} }
@ -1097,7 +1102,7 @@ isc__mempool_put(isc_mempool_t *restrict mpctx, void *mem FLARG) {
INSIST(mpctx->allocated > 0); INSIST(mpctx->allocated > 0);
mpctx->allocated--; mpctx->allocated--;
DELETE_TRACE(mctx, mem, mpctx->size, file, line); DELETE_TRACE(mctx, mem, mpctx->size, func, file, line);
/* /*
* If our free list is full, return this to the mctx directly. * If our free list is full, return this to the mctx directly.
@ -1432,8 +1437,8 @@ isc__mem_create(isc_mem_t **mctxp FLARG) {
mem_create(mctxp, isc_mem_debugging, isc_mem_defaultflags, 0); mem_create(mctxp, isc_mem_debugging, isc_mem_defaultflags, 0);
#if ISC_MEM_TRACKLINES #if ISC_MEM_TRACKLINES
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) { if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "create mctx %p file %s line %u\n", *mctxp, fprintf(stderr, "create mctx %p func %s file %s line %u\n",
file, line); *mctxp, func, file, line);
} }
#endif /* ISC_MEM_TRACKLINES */ #endif /* ISC_MEM_TRACKLINES */
} }
@ -1459,9 +1464,9 @@ isc__mem_create_arena(isc_mem_t **mctxp FLARG) {
#if ISC_MEM_TRACKLINES #if ISC_MEM_TRACKLINES
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) { if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, fprintf(stderr,
"create mctx %p file %s line %u for jemalloc arena " "create mctx %p func %s file %s line %u "
"%u\n", "for jemalloc arena %u\n",
*mctxp, file, line, arena_no); *mctxp, func, file, line, arena_no);
} }
#endif /* ISC_MEM_TRACKLINES */ #endif /* ISC_MEM_TRACKLINES */
} }

View File

@ -17,8 +17,8 @@
$mem_stats = ''; $mem_stats = '';
while (<>) { while (<>) {
$gets{$1.$2} = $_ if (/add (?:0x)?([0-9a-f]+) size (?:0x)?([0-9]+) file/); $gets{$1.$2} = $_ if (/add (?:0x)?([0-9a-f]+) size (?:0x)?([0-9]+) func/);
delete $gets{$1.$2} if /del (?:0x)?([0-9a-f]+) size (?:0x)?([0-9]+) file/; delete $gets{$1.$2} if /del (?:0x)?([0-9a-f]+) size (?:0x)?([0-9]+) func/;
$mem_stats .= $_ if /\d+ gets, +(\d+) rem/ && $1 > 0; $mem_stats .= $_ if /\d+ gets, +(\d+) rem/ && $1 > 0;
} }
print join('', values %gets); print join('', values %gets);