From 5f4f005c4adb2079bc36f36ebce519cf0e9bc4a8 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Sat, 24 Aug 2013 18:43:29 +0400 Subject: [PATCH] Define GC_caller_func_offset only if HAVE_DLADDR and GC_RETURN_ADDR_PARENT (Apply commit 5d94f18 from 'release-7_2' branch.) * dbg_mlc.c (GC_caller_func_offset): Do not define (and do not include dlfcn.h) unless GC_ADD_CALLER; test HAVE_DLADDR (and GC_RETURN_ADDR_PARENT) instead of FREEBSD macro. * dbg_mlc.c (GC_caller_func_offset): Use STATIC; replace the K&R-style function definition with the ANSI C one; remove unnecessary "const" in type casts; do not call dladdr() if address is 0. * dbg_mlc.c (GC_debug_malloc, GC_debug_realloc): Move assignment of "s" argument to "unknown" to GC_caller_func_offset (if GC_ADD_CALLER). * dbg_mlc.c (GC_debug_realloc): Do not call GC_caller_func_offset if "p" argument is NULL (i.e., if redirected to GC_debug_malloc).. * include/private/gcconfig.h (HAVE_DLADDR): Define for FREEBSD. Conflicts: dbg_mlc.c --- dbg_mlc.c | 64 +++++++++++++++++++------------------- include/private/gcconfig.h | 6 ++++ 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/dbg_mlc.c b/dbg_mlc.c index d8f44da4..a0423e97 100644 --- a/dbg_mlc.c +++ b/dbg_mlc.c @@ -485,38 +485,40 @@ GC_API void GC_CALL GC_debug_register_displacement(size_t offset) UNLOCK(); } -#ifdef FREEBSD -#include -static void GC_caller_func_offset(ad, symp, offp) -const GC_word ad; -const char **symp; -int *offp; -{ - Dl_info caller; - if (dladdr((const void *)ad, &caller) && caller.dli_sname != NULL) { - *symp = caller.dli_sname; - *offp = (const char *)ad - (const char *)caller.dli_saddr; +#ifdef GC_ADD_CALLER +# if defined(HAVE_DLADDR) && defined(GC_RETURN_ADDR_PARENT) +# include + + STATIC void GC_caller_func_offset(word ad, const char **symp, int *offp) + { + Dl_info caller; + + if (ad && dladdr((void *)ad, &caller) && caller.dli_sname != NULL) { + *symp = caller.dli_sname; + *offp = (int)((char *)ad - (char *)caller.dli_saddr); + } + if (NULL == *symp) { + *symp = "unknown"; + } } -} -#else -# define GC_caller_func_offset(ad, symp, offp) (void)0 -#endif +# else +# define GC_caller_func_offset(ad, symp, offp) (void)(*(symp) = "unknown") +# endif +#endif /* GC_ADD_CALLER */ GC_API void * GC_CALL GC_debug_malloc(size_t lb, GC_EXTRA_PARAMS) { void * result; + /* Note that according to malloc() specification, if size is 0 then */ /* malloc() returns either NULL, or a unique pointer value that can */ /* later be successfully passed to free(). We always do the latter. */ result = GC_malloc(lb + DEBUG_BYTES); - -#ifdef GC_ADD_CALLER - if (s == NULL) { - GC_caller_func_offset(ra, &s, &i); - if (s == NULL) - s = "unknown"; - } -#endif +# ifdef GC_ADD_CALLER + if (s == NULL) { + GC_caller_func_offset(ra, &s, &i); + } +# endif if (result == 0) { GC_err_printf("GC_debug_malloc(%lu) returning NULL (%s:%d)\n", (unsigned long)lb, s, i); @@ -866,16 +868,14 @@ GC_API void * GC_CALL GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS) void * result; hdr * hhdr; -#ifdef GC_ADD_CALLER - if (s == NULL) { - GC_caller_func_offset(ra, &s, &i); - if (s == NULL) - s = "unknown"; + if (p == 0) { + return GC_debug_malloc(lb, OPT_RA s, i); } -#endif - if (p == 0) - return(GC_debug_malloc(lb, OPT_RA s, i)); - +# ifdef GC_ADD_CALLER + if (s == NULL) { + GC_caller_func_offset(ra, &s, &i); + } +# endif base = GC_base(p); if (base == 0) { ABORT_ARG1("Invalid pointer passed to realloc()", ": %p", p); diff --git a/include/private/gcconfig.h b/include/private/gcconfig.h index bd1f8cd1..83a44449 100644 --- a/include/private/gcconfig.h +++ b/include/private/gcconfig.h @@ -2814,6 +2814,12 @@ # define NEED_CALLINFO #endif +#if defined(FREEBSD) && !defined(HAVE_DLADDR) + /* TODO: Define for Darwin, Linux, Solaris. */ + /* TODO: Detect dladdr() presence by configure. */ +# define HAVE_DLADDR +#endif + #if defined(MAKE_BACK_GRAPH) && !defined(DBG_HDRS_ALL) # define DBG_HDRS_ALL #endif -- 2.40.0