]> granicus.if.org Git - gc/commitdiff
Define GC_caller_func_offset only if HAVE_DLADDR and GC_RETURN_ADDR_PARENT
authorIvan Maidanski <ivmai@mail.ru>
Sat, 24 Aug 2013 14:43:29 +0000 (18:43 +0400)
committerIvan Maidanski <ivmai@mail.ru>
Sun, 25 Aug 2013 18:42:11 +0000 (22:42 +0400)
(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
include/private/gcconfig.h

index d8f44da4a49858c7c9012ab434d1e2d121d1a726..a0423e97e58849020b4921aa142520278ec4675c 100644 (file)
--- 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 <dlfcn.h>
-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 <dlfcn.h>
+
+    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);
index bd1f8cd10ad53745133fd5f46e66057d6c40fb39..83a4444956799490c6bcd3579c3750cd4aa97e2c 100644 (file)
 # 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