diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/talloc/talloc.c | 61 | ||||
-rw-r--r-- | lib/talloc/talloc.h | 2 |
2 files changed, 51 insertions, 12 deletions
diff --git a/lib/talloc/talloc.c b/lib/talloc/talloc.c index 64037b52cf..58776a9160 100644 --- a/lib/talloc/talloc.c +++ b/lib/talloc/talloc.c @@ -141,6 +141,41 @@ struct talloc_chunk { #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15) #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc)) +static void (*talloc_log_fn)(const char *message); + +void talloc_set_log_fn(void (*log_fn)(const char *message)) +{ + talloc_log_fn = log_fn; +} + +static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2); +static void talloc_log(const char *fmt, ...) +{ + va_list ap; + char *message; + + if (!talloc_log_fn) { + return; + } + + va_start(ap, fmt); + message = talloc_vasprintf(NULL, fmt, ap); + va_end(ap); + + talloc_log_fn(message); + talloc_free(message); +} + +static void talloc_log_stderr(const char *message) +{ + fprintf(stderr, "%s", message); +} + +void talloc_set_log_stderr(void) +{ + talloc_set_log_fn(talloc_log_stderr); +} + static void (*talloc_abort_fn)(const char *reason); void talloc_set_abort_fn(void (*abort_fn)(const char *reason)) @@ -150,6 +185,8 @@ void talloc_set_abort_fn(void (*abort_fn)(const char *reason)) static void talloc_abort(const char *reason) { + talloc_log("%s\n", reason); + if (!talloc_abort_fn) { TALLOC_ABORT(reason); } @@ -687,13 +724,13 @@ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *locati if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) { struct talloc_reference_handle *h; -#if DEVELOPER - fprintf(stderr, "WARNING: talloc_steal with references at %s\n", location); -#endif + + talloc_log("WARNING: talloc_steal with references at %s\n", + location); + for (h=tc->refs; h; h=h->next) { -#if DEVELOPER - fprintf(stderr, "\treference at %s\n", h->location); -#endif + talloc_log("\treference at %s\n", + h->location); } } @@ -1061,13 +1098,13 @@ int _talloc_free(void *ptr, const char *location) if (unlikely(tc->refs != NULL)) { struct talloc_reference_handle *h; -#if DEVELOPER - fprintf(stderr, "ERROR: talloc_free with references at %s\n", location); -#endif + + talloc_log("ERROR: talloc_free with references at %s\n", + location); + for (h=tc->refs; h; h=h->next) { -#if DEVELOPER - fprintf(stderr, "\treference at %s\n", h->location); -#endif + talloc_log("\treference at %s\n", + h->location); } return -1; } diff --git a/lib/talloc/talloc.h b/lib/talloc/talloc.h index 855f778f8c..234c0b442c 100644 --- a/lib/talloc/talloc.h +++ b/lib/talloc/talloc.h @@ -189,6 +189,8 @@ char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); void talloc_set_abort_fn(void (*abort_fn)(const char *reason)); +void talloc_set_log_fn(void (*log_fn)(const char *message)); +void talloc_set_log_stderr(void); /* for ABI compatibility, never use in new code */ void *_talloc_reference(const void *context, const void *ptr); |