aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSiddhesh Poyarekar <siddhesh@sourceware.org>2021-07-22 18:38:06 +0530
committerSiddhesh Poyarekar <siddhesh@sourceware.org>2021-07-22 18:38:06 +0530
commit9dad716d4d2993f50b165747781244bd7c43bc95 (patch)
treebdd9025b5b9871f75239f7a3f6e86f4c0401eafb
parentcc35896ea3e4532919ec81b17f36299117debe79 (diff)
downloadglibc-9dad716d4d2993f50b165747781244bd7c43bc95.tar.xz
glibc-9dad716d4d2993f50b165747781244bd7c43bc95.zip
mtrace: Wean away from malloc hooks
Wean mtrace away from the malloc hooks and move them into the debug DSO. Split the API away from the implementation so that we can add the API to libc.so as well as libc_malloc_debug.so, with the libc implementations being empty. Update localplt data since memalign no longer has any callers after this change. Reviewed-by: Carlos O'Donell <carlos@redhat.com> Tested-by: Carlos O'Donell <carlos@redhat.com>
-rw-r--r--malloc/Versions2
-rw-r--r--malloc/malloc-debug.c13
-rw-r--r--malloc/mtrace-impl.c226
-rw-r--r--malloc/mtrace.c314
-rw-r--r--sysdeps/generic/localplt.data1
-rw-r--r--sysdeps/mach/hurd/i386/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/mach/hurd/i386/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/aarch64/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/aarch64/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/alpha/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/alpha/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/arc/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/arc/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/arm/be/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/arm/le/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/arm/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/csky/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/csky/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/hppa/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/hppa/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/i386/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/i386/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/ia64/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/ia64/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/m68k/coldfire/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/m68k/coldfire/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/m68k/m680x0/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/m68k/m680x0/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/microblaze/be/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/microblaze/le/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/microblaze/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/mips/mips32/fpu/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/mips/mips64/n32/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/mips/mips64/n64/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/nios2/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/nios2/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/powerpc/powerpc64/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/riscv/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/riscv/rv32/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/riscv/rv64/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/s390/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/s390/s390-32/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/s390/s390-64/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/sh/be/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/sh/le/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/sh/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/sparc/sparc32/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/sparc/sparc32/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/sparc/sparc64/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/sparc/sparc64/localplt.data1
-rw-r--r--sysdeps/unix/sysv/linux/x86_64/64/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/unix/sysv/linux/x86_64/x32/libc_malloc_debug.abilist2
-rw-r--r--sysdeps/x86_64/localplt.data1
60 files changed, 319 insertions, 325 deletions
diff --git a/malloc/Versions b/malloc/Versions
index 6548970419..71d933de19 100644
--- a/malloc/Versions
+++ b/malloc/Versions
@@ -117,6 +117,8 @@ libc_malloc_debug {
mcheck;
mprobe;
+ mtrace;
+ muntrace;
}
GLIBC_2.2 {
mcheck_check_all;
diff --git a/malloc/malloc-debug.c b/malloc/malloc-debug.c
index 7c3a1e26b5..9942124e02 100644
--- a/malloc/malloc-debug.c
+++ b/malloc/malloc-debug.c
@@ -49,6 +49,7 @@ enum malloc_debug_hooks
{
MALLOC_NONE_HOOK = 0,
MALLOC_MCHECK_HOOK = 1 << 0, /* mcheck() */
+ MALLOC_MTRACE_HOOK = 1 << 1, /* mtrace() */
};
static unsigned __malloc_debugging_hooks;
@@ -71,6 +72,7 @@ __malloc_debug_disable (enum malloc_debug_hooks flag)
}
#include "mcheck.c"
+#include "mtrace.c"
extern void (*__malloc_initialize_hook) (void);
compat_symbol_reference (libc, __malloc_initialize_hook,
@@ -154,6 +156,8 @@ __debug_malloc (size_t bytes)
}
if (__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK) && victim != NULL)
victim = malloc_mcheck_after (victim, orig_bytes);
+ if (__is_malloc_debug_enabled (MALLOC_MTRACE_HOOK))
+ malloc_mtrace_after (victim, orig_bytes, RETURN_ADDRESS (0));
return victim;
}
@@ -171,6 +175,8 @@ __debug_free (void *mem)
if (__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK))
mem = free_mcheck (mem);
+ if (__is_malloc_debug_enabled (MALLOC_MTRACE_HOOK))
+ free_mtrace (mem, RETURN_ADDRESS (0));
__libc_free (mem);
}
@@ -195,6 +201,8 @@ __debug_realloc (void *oldmem, size_t bytes)
if (__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK) && victim != NULL)
victim = realloc_mcheck_after (victim, oldmem, orig_bytes,
oldsize);
+ if (__is_malloc_debug_enabled (MALLOC_MTRACE_HOOK))
+ realloc_mtrace_after (victim, oldmem, orig_bytes, RETURN_ADDRESS (0));
return victim;
}
@@ -218,6 +226,8 @@ _debug_mid_memalign (size_t alignment, size_t bytes, const void *address)
}
if (__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK) && victim != NULL)
victim = memalign_mcheck_after (victim, alignment, orig_bytes);
+ if (__is_malloc_debug_enabled (MALLOC_MTRACE_HOOK))
+ memalign_mtrace_after (victim, orig_bytes, address);
return victim;
}
@@ -317,6 +327,9 @@ __debug_calloc (size_t nmemb, size_t size)
victim = malloc_mcheck_after (victim, orig_bytes);
memset (victim, 0, orig_bytes);
}
+ if (__is_malloc_debug_enabled (MALLOC_MTRACE_HOOK))
+ malloc_mtrace_after (victim, orig_bytes, RETURN_ADDRESS (0));
+
return victim;
}
strong_alias (__debug_calloc, calloc)
diff --git a/malloc/mtrace-impl.c b/malloc/mtrace-impl.c
new file mode 100644
index 0000000000..0e10ab7f60
--- /dev/null
+++ b/malloc/mtrace-impl.c
@@ -0,0 +1,226 @@
+/* mtrace implementation for `malloc'.
+ Copyright (C) 1991-2021 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Written April 2, 1991 by John Gilmore of Cygnus Support.
+ Based on mcheck.c by Mike Haertel.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+
+#include <malloc.h>
+#include <mcheck.h>
+
+#include <dlfcn.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <inttypes.h>
+
+#include <libc-internal.h>
+#include <dso_handle.h>
+
+#include <kernel-features.h>
+
+#define TRACE_BUFFER_SIZE 512
+
+static FILE *mallstream;
+static const char mallenv[] = "MALLOC_TRACE";
+static char *malloc_trace_buffer;
+
+static void
+tr_where (const void *caller, Dl_info *info)
+{
+ if (caller != NULL)
+ {
+ if (info != NULL)
+ {
+ char *buf = (char *) "";
+ if (info->dli_sname != NULL)
+ {
+ size_t len = strlen (info->dli_sname);
+ buf = alloca (len + 6 + 2 * sizeof (void *));
+ char sign;
+ ptrdiff_t offset =
+ (ptrdiff_t) info->dli_saddr - (ptrdiff_t) caller;
+
+ if (caller >= (const void *) info->dli_saddr)
+ {
+ sign = '+';
+ offset = -offset;
+ }
+ else
+ sign = '-';
+
+ sprintf (buf, "(%s%c%" PRIxPTR ")", info->dli_sname, sign,
+ offset);
+ }
+
+ fprintf (mallstream, "@ %s%s%s[%p] ", info->dli_fname ? : "",
+ info->dli_fname ? ":" : "",
+ buf, caller);
+ }
+ else
+ fprintf (mallstream, "@ [%p] ", caller);
+ }
+}
+
+static Dl_info *
+lock_and_info (const void *caller, Dl_info *mem)
+{
+ if (caller == NULL)
+ return NULL;
+
+ Dl_info *res = dladdr (caller, mem) ? mem : NULL;
+
+ flockfile (mallstream);
+
+ return res;
+}
+
+static void
+free_mtrace (void *ptr, const void *caller)
+{
+ if (ptr == NULL)
+ return;
+
+ Dl_info mem;
+ Dl_info *info = lock_and_info (caller, &mem);
+ tr_where (caller, info);
+ /* Be sure to print it first. */
+ fprintf (mallstream, "- %p\n", ptr);
+ funlockfile (mallstream);
+}
+
+static void
+malloc_mtrace_after (void *block, size_t size, const void *caller)
+{
+ Dl_info mem;
+ Dl_info *info = lock_and_info (caller, &mem);
+
+ tr_where (caller, info);
+ /* We could be printing a NULL here; that's OK. */
+ fprintf (mallstream, "+ %p %#lx\n", block, (unsigned long int) size);
+
+ funlockfile (mallstream);
+}
+
+static void
+realloc_mtrace_after (void *block, const void *oldptr, size_t size,
+ const void *caller)
+{
+ Dl_info mem;
+ Dl_info *info = lock_and_info (caller, &mem);
+
+ tr_where (caller, info);
+ if (block == NULL)
+ {
+ if (size != 0)
+ /* Failed realloc. */
+ fprintf (mallstream, "! %p %#lx\n", oldptr, (unsigned long int) size);
+ else
+ fprintf (mallstream, "- %p\n", oldptr);
+ }
+ else if (oldptr == NULL)
+ fprintf (mallstream, "+ %p %#lx\n", b