From: Ondřej Surý Date: Tue, 11 May 2021 10:18:56 +0000 (+0200) Subject: Add debug tracing capability to isc_mem_create/isc_mem_destroy X-Git-Tag: v9.17.17~40^2~25 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=63924968d133115f0653d62518ecce31e3629137;p=thirdparty%2Fbind9.git Add debug tracing capability to isc_mem_create/isc_mem_destroy Previously, we only had capability to trace the memory gets and puts, but for debugging, it's sometimes also important to keep track how many and where do the memory contexts get created and destroyed. This commit adds such tracking capability. --- diff --git a/lib/isc/include/isc/mem.h b/lib/isc/include/isc/mem.h index 85973a755a2..a5f003bdced 100644 --- a/lib/isc/include/isc/mem.h +++ b/lib/isc/include/isc/mem.h @@ -175,8 +175,8 @@ extern unsigned int isc_mem_defaultflags; } while (0) /*@{*/ -void -isc_mem_create(isc_mem_t **mctxp); +#define isc_mem_create(cp) ISCMEMFUNC(create)((cp)_ISC_MEM_FILELINE) +void ISCMEMFUNC(create)(isc_mem_t **_ISC_MEM_FLARG); /*!< * \brief Create a memory context. @@ -188,8 +188,8 @@ isc_mem_create(isc_mem_t **mctxp); /*@{*/ void isc_mem_attach(isc_mem_t *, isc_mem_t **); -void -isc_mem_detach(isc_mem_t **); +#define isc_mem_detach(cp) ISCMEMFUNC(detach)((cp)_ISC_MEM_FILELINE) +void ISCMEMFUNC(detach)(isc_mem_t **_ISC_MEM_FLARG); /*!< * \brief Attach to / detach from a memory context. * @@ -204,8 +204,8 @@ isc_mem_detach(isc_mem_t **); */ /*@}*/ -void -isc_mem_destroy(isc_mem_t **); +#define isc_mem_destroy(cp) ISCMEMFUNC(destroy)((cp)_ISC_MEM_FILELINE) +void ISCMEMFUNC(destroy)(isc_mem_t **_ISC_MEM_FLARG); /*%< * Destroy a memory context. */ diff --git a/lib/isc/mem.c b/lib/isc/mem.c index 54d5411ef84..6adfd9d6716 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -604,7 +604,7 @@ isc_mem_attach(isc_mem_t *source, isc_mem_t **targetp) { } void -isc_mem_detach(isc_mem_t **ctxp) { +isc__mem_detach(isc_mem_t **ctxp FLARG) { REQUIRE(ctxp != NULL && VALID_CONTEXT(*ctxp)); isc_mem_t *ctx = *ctxp; @@ -612,6 +612,12 @@ isc_mem_detach(isc_mem_t **ctxp) { if (isc_refcount_decrement(&ctx->references) == 1) { isc_refcount_destroy(&ctx->references); +#if ISC_MEM_TRACKLINES + if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) { + fprintf(stderr, "destroy mctx %p file %s line %u\n", + ctx, file, line); + } +#endif destroy(ctx); } } @@ -663,7 +669,7 @@ destroy: } void -isc_mem_destroy(isc_mem_t **ctxp) { +isc__mem_destroy(isc_mem_t **ctxp FLARG) { /* * This routine provides legacy support for callers who use mctxs * without attaching/detaching. @@ -674,6 +680,11 @@ isc_mem_destroy(isc_mem_t **ctxp) { isc_mem_t *ctx = *ctxp; #if ISC_MEM_TRACKLINES + if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) { + fprintf(stderr, "destroy mctx %p file %s line %u\n", ctx, file, + line); + } + if (isc_refcount_decrement(&ctx->references) > 1) { print_active(ctx, stderr); } @@ -1343,7 +1354,6 @@ isc__mempool_put(isc_mempool_t *mpctx, void *mem FLARG) { void * isc__mempool_get(isc_mempool_t *mpctx FLARG) { element *item = NULL; - unsigned int i; REQUIRE(VALID_MEMPOOL(mpctx)); @@ -1366,7 +1376,7 @@ isc__mempool_get(isc_mempool_t *mpctx FLARG) { * We need to dip into the well. Lock the memory * context here and fill up our free list. */ - for (i = 0; i < fillcount; i++) { + for (size_t i = 0; i < fillcount; i++) { item = mem_get(mctx, mpctx->size); mem_getstats(mctx, mpctx->size); item->next = mpctx->items; @@ -1843,8 +1853,14 @@ error: #endif /* HAVE_JSON_C */ void -isc_mem_create(isc_mem_t **mctxp) { +isc__mem_create(isc_mem_t **mctxp FLARG) { mem_create(mctxp, isc_mem_defaultflags); +#if ISC_MEM_TRACKLINES + if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) { + fprintf(stderr, "create mctx %p file %s line %u\n", *mctxp, + file, line); + } +#endif /* ISC_MEM_TRACKLINES */ } void