Files
glibc/malloc/mtrace.c
T

349 lines
9.2 KiB
C
Raw Normal View History

1996-12-20 01:39:50 +00:00
/* More debugging hooks for `malloc'.
Copyright (C) 1991-2018 Free Software Foundation, Inc.
2001-07-06 04:58:11 +00:00
This file is part of the GNU C Library.
2014-01-02 09:38:18 +01:00
Written April 2, 1991 by John Gilmore of Cygnus Support.
Based on mcheck.c by Mike Haertel.
1996-12-20 01:39:50 +00:00
2001-07-06 04:58:11 +00:00
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.
1996-12-20 01:39:50 +00:00
2001-07-06 04:58:11 +00:00
The GNU C Library is distributed in the hope that it will be useful,
1996-12-20 01:39:50 +00:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2001-07-06 04:58:11 +00:00
Lesser General Public License for more details.
1996-12-20 01:39:50 +00:00
2001-07-06 04:58:11 +00:00
You should have received a copy of the GNU Lesser General Public
2012-02-09 23:18:22 +00:00
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
1996-12-20 01:39:50 +00:00
2014-01-02 09:38:18 +01:00
#ifndef _MALLOC_INTERNAL
# define _MALLOC_INTERNAL
# include <malloc.h>
# include <mcheck.h>
# include <libc-lock.h>
1996-12-20 01:39:50 +00:00
#endif
1999-06-28 15:52:36 +00:00
#include <dlfcn.h>
2000-09-18 22:41:47 +00:00
#include <fcntl.h>
1996-12-20 01:39:50 +00:00
#include <stdio.h>
1998-10-24 10:44:47 +00:00
#include <string.h>
1996-12-20 01:39:50 +00:00
#include <stdlib.h>
#include <_itoa.h>
1998-10-29 15:31:47 +00:00
#include <libc-internal.h>
2017-09-26 16:53:25 -07:00
#include <dso_handle.h>
2000-12-31 19:13:04 +00:00
#include <libio/iolibio.h>
2012-05-23 13:33:15 +02:00
#define setvbuf(s, b, f, l) _IO_setvbuf (s, b, f, l)
#define fwrite(buf, size, count, fp) _IO_fwrite (buf, size, count, fp)
#include <kernel-features.h>
1998-07-16 11:44:36 +00:00
1998-04-10 10:52:25 +00:00
#define TRACE_BUFFER_SIZE 512
1996-12-20 01:39:50 +00:00
static FILE *mallstream;
2014-01-02 09:38:18 +01:00
static const char mallenv[] = "MALLOC_TRACE";
static char *malloc_trace_buffer;
1996-12-20 01:39:50 +00:00
__libc_lock_define_initialized (static, lock);
/* Address to breakpoint on accesses to... */
2017-08-08 17:14:49 +00:00
void *mallwatch;
1996-12-20 01:39:50 +00:00
/* Old hook values. */
2017-08-08 17:14:49 +00:00
static void (*tr_old_free_hook) (void *ptr, const void *);
static void *(*tr_old_malloc_hook) (size_t size, const void *);
static void *(*tr_old_realloc_hook) (void *ptr, size_t size,
const void *);
static void *(*tr_old_memalign_hook) (size_t __alignment, size_t __size,
const void *);
1996-12-20 01:39:50 +00:00
/* This function is called when the block being alloc'd, realloc'd, or
freed has an address matching the variable "mallwatch". In a debugger,
set "mallwatch" to the address of interest, then put a breakpoint on
tr_break. */
2004-09-14 04:41:35 +00:00
extern void tr_break (void) __THROW;
2002-12-31 23:13:55 +00:00
libc_hidden_proto (tr_break)
1996-12-20 01:39:50 +00:00
void
tr_break (void)
1996-12-20 01:39:50 +00:00
{
}
2002-12-31 23:13:55 +00:00
libc_hidden_def (tr_break)
1996-12-20 01:39:50 +00:00
static void
2017-08-08 17:14:49 +00:00
tr_where (const void *caller, Dl_info *info)
1996-12-20 01:39:50 +00:00
{
2011-05-16 00:22:16 -04:00
if (caller != NULL)
1997-09-27 00:21:42 +00:00
{
2011-05-16 00:58:33 -04:00
if (info != NULL)
2014-01-02 09:38:18 +01:00
{
char *buf = (char *) "";
if (info->dli_sname != NULL)
{
size_t len = strlen (info->dli_sname);
buf = alloca (len + 6 + 2 * sizeof (void *));
1998-10-29 15:31:47 +00:00
2014-01-02 09:38:18 +01:00
buf[0] = '(';
2017-08-08 17:14:49 +00:00
__stpcpy (_fitoa (caller >= (const void *) info->dli_saddr
? caller - (const void *) info->dli_saddr
: (const void *) info->dli_saddr - caller,
2014-01-02 09:38:18 +01:00
__stpcpy (__mempcpy (buf + 1, info->dli_sname,
len),
2017-08-08 17:14:49 +00:00
caller >= (void *) info->dli_saddr
2014-01-02 09:38:18 +01:00
? "+0x" : "-0x"),
16, 0),
")");
}
1998-10-07 13:40:55 +00:00
2014-01-02 09:38:18 +01:00
fprintf (mallstream, "@ %s%s%s[%p] ",
info->dli_fname ? : "", info->dli_fname ? ":" : "",
buf, caller);
}
1997-09-27 00:21:42 +00:00
else
2014-01-02 09:38:18 +01:00
fprintf (mallstream, "@ [%p] ", caller);
1997-09-27 00:21:42 +00:00
}
1996-12-20 01:39:50 +00:00
}
2011-05-16 00:58:33 -04:00
static Dl_info *
2017-08-08 17:14:49 +00:00
lock_and_info (const void *caller, Dl_info *mem)
2011-05-16 00:58:33 -04:00
{
if (caller == NULL)
return NULL;
Dl_info *res = _dl_addr (caller, mem, NULL, NULL) ? mem : NULL;
__libc_lock_lock (lock);
return res;
}
2014-02-06 08:54:20 +00:00
static void
2017-08-08 17:14:49 +00:00
tr_freehook (void *ptr, const void *caller)
1996-12-20 01:39:50 +00:00
{
if (ptr == NULL)
return;
2011-05-16 00:58:33 -04:00
Dl_info mem;
Dl_info *info = lock_and_info (caller, &mem);
tr_where (caller, info);
1997-09-27 00:21:42 +00:00
/* Be sure to print it first. */
fprintf (mallstream, "- %p\n", ptr);
1996-12-20 01:39:50 +00:00
if (ptr == mallwatch)
2011-12-22 11:38:32 -05:00
{
__libc_lock_unlock (lock);
tr_break ();
__libc_lock_lock (lock);
}
1996-12-20 01:39:50 +00:00
__free_hook = tr_old_free_hook;
1997-09-27 00:21:42 +00:00
if (tr_old_free_hook != NULL)
2014-01-02 09:38:18 +01:00
(*tr_old_free_hook)(ptr, caller);
1997-09-27 00:21:42 +00:00
else
free (ptr);
1996-12-20 01:39:50 +00:00
__free_hook = tr_freehook;
__libc_lock_unlock (lock);
}
2017-08-08 17:14:49 +00:00
static void *
tr_mallochook (size_t size, const void *caller)
1996-12-20 01:39:50 +00:00
{
2017-08-08 17:14:49 +00:00
void *hdr;
1996-12-20 01:39:50 +00:00
2011-05-16 00:58:33 -04:00
Dl_info mem;
Dl_info *info = lock_and_info (caller, &mem);
1996-12-20 01:39:50 +00:00
__malloc_hook = tr_old_malloc_hook;
1997-09-27 00:21:42 +00:00
if (tr_old_malloc_hook != NULL)
2017-08-08 17:14:49 +00:00
hdr = (void *) (*tr_old_malloc_hook)(size, caller);
1997-09-27 00:21:42 +00:00
else
2017-08-08 17:14:49 +00:00
hdr = (void *) malloc (size);
1996-12-20 01:39:50 +00:00
__malloc_hook = tr_mallochook;
2011-05-16 00:58:33 -04:00
tr_where (caller, info);
1996-12-20 01:39:50 +00:00
/* We could be printing a NULL here; that's OK. */
1998-06-07 14:06:56 +00:00
fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long int) size);
1996-12-20 01:39:50 +00:00
1999-06-17 09:36:54 +00:00
__libc_lock_unlock (lock);
1996-12-20 01:39:50 +00:00
if (hdr == mallwatch)
tr_break ();
return hdr;
}
2017-08-08 17:14:49 +00:00
static void *
tr_reallochook (void *ptr, size_t size, const void *caller)
1996-12-20 01:39:50 +00:00
{
2017-08-08 17:14:49 +00:00
void *hdr;
1996-12-20 01:39:50 +00:00
if (ptr == mallwatch)
tr_break ();
2011-05-16 00:58:33 -04:00
Dl_info mem;
Dl_info *info = lock_and_info (caller, &mem);
1996-12-20 01:39:50 +00:00
__free_hook = tr_old_free_hook;
__malloc_hook = tr_old_malloc_hook;
__realloc_hook = tr_old_realloc_hook;
1997-09-27 00:21:42 +00:00
if (tr_old_realloc_hook != NULL)
2017-08-08 17:14:49 +00:00
hdr = (void *) (*tr_old_realloc_hook)(ptr, size, caller);
1997-09-27 00:21:42 +00:00
else
2017-08-08 17:14:49 +00:00
hdr = (void *) realloc (ptr, size);
1996-12-20 01:39:50 +00:00
__free_hook = tr_freehook;
__malloc_hook = tr_mallochook;
__realloc_hook = tr_reallochook;
2011-05-16 00:58:33 -04:00
tr_where (caller, info);
1996-12-20 01:39:50 +00:00
if (hdr == NULL)
2012-12-22 17:24:44 +01:00
{
if (size != 0)
2014-01-02 09:38:18 +01:00
/* Failed realloc. */
fprintf (mallstream, "! %p %#lx\n", ptr, (unsigned long int) size);
2012-12-22 17:24:44 +01:00
else
2014-01-02 09:38:18 +01:00
fprintf (mallstream, "- %p\n", ptr);
2012-12-22 17:24:44 +01:00
}
1996-12-20 01:39:50 +00:00
else if (ptr == NULL)
1998-06-07 14:06:56 +00:00
fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long int) size);
1996-12-20 01:39:50 +00:00
else
1998-10-08 17:00:12 +00:00
{
fprintf (mallstream, "< %p\n", ptr);
2011-05-16 00:58:33 -04:00
tr_where (caller, info);
1998-10-08 17:00:12 +00:00
fprintf (mallstream, "> %p %#lx\n", hdr, (unsigned long int) size);
}
1996-12-20 01:39:50 +00:00
1999-06-17 09:36:54 +00:00
__libc_lock_unlock (lock);
1996-12-20 01:39:50 +00:00
if (hdr == mallwatch)
tr_break ();
return hdr;
}
2017-08-08 17:14:49 +00:00
static void *
tr_memalignhook (size_t alignment, size_t size, const void *caller)
2003-12-17 23:52:56 +00:00
{
2017-08-08 17:14:49 +00:00
void *hdr;
2003-12-17 23:52:56 +00:00
2011-05-16 00:58:33 -04:00
Dl_info mem;
Dl_info *info = lock_and_info (caller, &mem);
2003-12-17 23:52:56 +00:00
__memalign_hook = tr_old_memalign_hook;
__malloc_hook = tr_old_malloc_hook;
if (tr_old_memalign_hook != NULL)
2017-08-08 17:14:49 +00:00
hdr = (void *) (*tr_old_memalign_hook)(alignment, size, caller);
2003-12-17 23:52:56 +00:00
else
2017-08-08 17:14:49 +00:00
hdr = (void *) memalign (alignment, size);
2003-12-17 23:52:56 +00:00
__memalign_hook = tr_memalignhook;
__malloc_hook = tr_mallochook;
2011-05-16 00:58:33 -04:00
tr_where (caller, info);
2003-12-17 23:52:56 +00:00
/* We could be printing a NULL here; that's OK. */
fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long int) size);
__libc_lock_unlock (lock);
if (hdr == mallwatch)
tr_break ();
return hdr;
}
1997-09-30 17:10:40 +00:00
#ifdef _LIBC
/* This function gets called to make sure all memory the library
allocates get freed and so does not irritate the user when studying
the mtrace output. */
static void __libc_freeres_fn_section
1997-09-30 17:10:40 +00:00
release_libc_mem (void)
{
/* Only call the free function if we still are running in mtrace mode. */
if (mallstream != NULL)
__libc_freeres ();
}
#endif
1996-12-20 01:39:50 +00:00
/* We enable tracing if either the environment variable MALLOC_TRACE
is set, or if the variable mallwatch has been patched to an address
that the debugging user wants us to stop on. When patching mallwatch,
don't forget to set a breakpoint on tr_break! */
void
mtrace (void)
1996-12-20 01:39:50 +00:00
{
1997-09-30 17:10:40 +00:00
#ifdef _LIBC
1999-04-28 23:13:52 +00:00
static int added_atexit_handler;
1997-09-30 17:10:40 +00:00
#endif
1996-12-20 01:39:50 +00:00
char *mallfile;
/* Don't panic if we're called more than once. */
if (mallstream != NULL)
return;
#ifdef _LIBC
/* When compiling the GNU libc we use the secure getenv function
which prevents the misuse in case of SUID or SGID enabled
programs. */
2012-07-25 19:46:22 +02:00
mallfile = __libc_secure_getenv (mallenv);
1996-12-20 01:39:50 +00:00
#else
mallfile = getenv (mallenv);
#endif
if (mallfile != NULL || mallwatch != NULL)
{
char *mtb = malloc (TRACE_BUFFER_SIZE);
if (mtb == NULL)
2014-01-02 09:38:18 +01:00
return;
mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "wce");
1996-12-20 01:39:50 +00:00
if (mallstream != NULL)
2014-01-02 09:38:18 +01:00
{
/* Be sure it doesn't malloc its buffer! */
malloc_trace_buffer = mtb;
setvbuf (mallstream, malloc_trace_buffer, _IOFBF, TRACE_BUFFER_SIZE);
fprintf (mallstream, "= Start\n");
tr_old_free_hook = __free_hook;
__free_hook = tr_freehook;
tr_old_malloc_hook = __malloc_hook;
__malloc_hook = tr_mallochook;
tr_old_realloc_hook = __realloc_hook;
__realloc_hook = tr_reallochook;
tr_old_memalign_hook = __memalign_hook;
__memalign_hook = tr_memalignhook;
1997-09-30 17:10:40 +00:00
#ifdef _LIBC
2014-01-02 09:38:18 +01:00
if (!added_atexit_handler)
{
added_atexit_handler = 1;
__cxa_atexit ((void (*)(void *))release_libc_mem, NULL,
2017-09-26 16:53:25 -07:00
__dso_handle);
2014-01-02 09:38:18 +01:00
}
1997-09-30 17:10:40 +00:00
#endif
2014-01-02 09:38:18 +01:00
}
else
2014-01-02 09:38:18 +01:00
free (mtb);
1996-12-20 01:39:50 +00:00
}
}
void
muntrace (void)
1996-12-20 01:39:50 +00:00
{
if (mallstream == NULL)
return;
/* Do the reverse of what done in mtrace: first reset the hooks and
MALLSTREAM, and only after that write the trailer and close the
file. */
FILE *f = mallstream;
1996-12-20 01:39:50 +00:00
mallstream = NULL;
__free_hook = tr_old_free_hook;
__malloc_hook = tr_old_malloc_hook;
__realloc_hook = tr_old_realloc_hook;
2003-12-17 23:52:56 +00:00
__memalign_hook = tr_old_memalign_hook;
fprintf (f, "= End\n");
fclose (f);
1996-12-20 01:39:50 +00:00
}