Back to index

glibc  2.9
Defines | Functions | Variables
mtrace.c File Reference
#include <malloc.h>
#include <mcheck.h>
#include <bits/libc-lock.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio-common/_itoa.h>
#include <libc-internal.h>
#include <libio/iolibio.h>
#include <kernel-features.h>

Go to the source code of this file.

Defines

#define _MALLOC_INTERNAL
#define setvbuf(s, b, f, l)   INTUSE(_IO_setvbuf) (s, b, f, l)
#define fwrite(buf, size, count, fp)   _IO_fwrite (buf, size, count, fp)
#define attribute_hidden
#define TRACE_BUFFER_SIZE   512

Functions

 __libc_lock_define_initialized (static, lock)
void tr_break (void)
 libc_hidden_def (tr_break)
static void tr_freehook (__ptr_t, const __ptr_t)
static __ptr_t tr_mallochook (__malloc_size_t, const __ptr_t)
static __ptr_t tr_reallochook (__ptr_t, __malloc_size_t, const __ptr_t)
static __ptr_t tr_memalignhook (__malloc_size_t, __malloc_size_t, const __ptr_t)
void mtrace ()
void muntrace ()

Variables

static FILEmallstream
static const char mallenv [] = "MALLOC_TRACE"
static char * malloc_trace_buffer
__ptr_t mallwatch
static void(* tr_old_free_hook )(__ptr_t ptr, const __ptr_t)
static __ptr_t(* tr_old_malloc_hook )(__malloc_size_t size, const __ptr_t)
static __ptr_t(* tr_old_realloc_hook )(__ptr_t ptr, __malloc_size_t size, const __ptr_t)
static __ptr_t(* tr_old_memalign_hook )(__malloc_size_t __alignment, __malloc_size_t __size, __const __ptr_t)

Define Documentation

Definition at line 23 of file mtrace.c.

Definition at line 46 of file mtrace.c.

#define fwrite (   buf,
  size,
  count,
  fp 
)    _IO_fwrite (buf, size, count, fp)

Definition at line 41 of file mtrace.c.

#define setvbuf (   s,
  b,
  f,
  l 
)    INTUSE(_IO_setvbuf) (s, b, f, l)

Definition at line 40 of file mtrace.c.

#define TRACE_BUFFER_SIZE   512

Definition at line 49 of file mtrace.c.


Function Documentation

__libc_lock_define_initialized ( static  ,
lock   
)

Definition at line 34 of file getnetgrent_r.c.

{
  /* Remember the first service_entry, it's always the same.  */
  static bool startp_initialized;
  static service_user *startp;
  int no_more;

  if (!startp_initialized)
    {
      /* Executing this more than once at the same time must yield the
        same result every time.  So we need no locking.  */
      no_more = __nss_netgroup_lookup (nipp, "setnetgrent", fctp);
      startp = no_more ? (service_user *) -1 : *nipp;
      PTR_MANGLE (startp);
      atomic_write_barrier ();
      startp_initialized = true;
    }
  else
    {
      service_user *nip = startp;
      PTR_DEMANGLE (nip);
      if (nip == (service_user *) -1)
       /* No services at all.  */
       return 1;

      /* Reset to the beginning of the service list.  */
      *nipp = nip;
      /* Look up the first function.  */
      no_more = __nss_lookup (nipp, "setnetgrent", NULL, fctp);
    }
  return no_more;
}

Definition at line 87 of file mtrace.c.

{
#ifdef USE_MTRACE_FILE
  if (_mtrace_file)
    {
      fprintf (mallstream, "@ %s:%d ", _mtrace_file, _mtrace_line);
      _mtrace_file = NULL;
    }
  else
#endif
    if (caller != NULL)
    {
#ifdef HAVE_ELF
      Dl_info info;
      if (_dl_addr (caller, &info, NULL, NULL))
       {
         char *buf = (char *) "";
         if (info.dli_sname != NULL)
           {
             size_t len = strlen (info.dli_sname);
             buf = alloca (len + 6 + 2 * sizeof (void *));

             buf[0] = '(';
             __stpcpy (_fitoa (caller >= (const __ptr_t) info.dli_saddr
                            ? caller - (const __ptr_t) info.dli_saddr
                            : (const __ptr_t) info.dli_saddr - caller,
                            __stpcpy (__mempcpy (buf + 1, info.dli_sname,
                                               len),
                                     caller >= (__ptr_t) info.dli_saddr
                                     ? "+0x" : "-0x"),
                            16, 0),
                     ")");
           }

         fprintf (mallstream, "@ %s%s%s[%p] ",
                 info.dli_fname ?: "", info.dli_fname ? ":" : "",
                 buf, caller);
       }
      else
#endif
       fprintf (mallstream, "@ [%p] ", caller);
    }
}

Here is the call graph for this function:

void mtrace ( void  )

Definition at line 293 of file mtrace.c.

{
#ifdef _LIBC
  static int added_atexit_handler;
#endif
  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.  */
  mallfile = __secure_getenv (mallenv);
#else
  mallfile = getenv (mallenv);
#endif
  if (mallfile != NULL || mallwatch != NULL)
    {
      char *mtb = malloc (TRACE_BUFFER_SIZE);
      if (mtb == NULL)
       return;

      mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "wce");
      if (mallstream != NULL)
       {
#ifndef __ASSUME_O_CLOEXEC
         /* Make sure we close the file descriptor on exec.  */
         int flags = __fcntl (fileno (mallstream), F_GETFD, 0);
         if (flags >= 0)
           {
             flags |= FD_CLOEXEC;
             __fcntl (fileno (mallstream), F_SETFD, flags);
           }
#endif
         /* 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;
#ifdef _LIBC
         if (!added_atexit_handler)
           {
             extern void *__dso_handle __attribute__ ((__weak__));
             added_atexit_handler = 1;
             __cxa_atexit ((void (*) (void *)) release_libc_mem, NULL,
                          &__dso_handle ? __dso_handle : NULL);
           }
#endif
       }
      else
       free (mtb);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void muntrace ( void  )

Definition at line 358 of file mtrace.c.

Here is the call graph for this function:

Here is the caller graph for this function:

void tr_break ( void  )

Definition at line 81 of file mtrace.c.

{
}

Here is the caller graph for this function:

static void tr_freehook ( __ptr_t  ,
const __ptr_t   
) [static]

Definition at line 137 of file mtrace.c.

{
  if (ptr == NULL)
    return;
  __libc_lock_lock (lock);
  tr_where (caller);
  /* Be sure to print it first.  */
  fprintf (mallstream, "- %p\n", ptr);
  __libc_lock_unlock (lock);
  if (ptr == mallwatch)
    tr_break ();
  __libc_lock_lock (lock);
  __free_hook = tr_old_free_hook;
  if (tr_old_free_hook != NULL)
    (*tr_old_free_hook) (ptr, caller);
  else
    free (ptr);
  __free_hook = tr_freehook;
  __libc_lock_unlock (lock);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static __ptr_t tr_mallochook ( __malloc_size_t  ,
const __ptr_t   
) [static]

Definition at line 162 of file mtrace.c.

{
  __ptr_t hdr;

  __libc_lock_lock (lock);

  __malloc_hook = tr_old_malloc_hook;
  if (tr_old_malloc_hook != NULL)
    hdr = (__ptr_t) (*tr_old_malloc_hook) (size, caller);
  else
    hdr = (__ptr_t) malloc (size);
  __malloc_hook = tr_mallochook;

  tr_where (caller);
  /* 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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 238 of file mtrace.c.

{
  __ptr_t hdr;

  __libc_lock_lock (lock);

  __memalign_hook = tr_old_memalign_hook;
  __malloc_hook = tr_old_malloc_hook;
  if (tr_old_memalign_hook != NULL)
    hdr = (__ptr_t) (*tr_old_memalign_hook) (alignment, size, caller);
  else
    hdr = (__ptr_t) memalign (alignment, size);
  __memalign_hook = tr_memalignhook;
  __malloc_hook = tr_mallochook;

  tr_where (caller);
  /* 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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static __ptr_t tr_reallochook ( __ptr_t  ,
__malloc_size_t  ,
const __ptr_t   
) [static]

Definition at line 191 of file mtrace.c.

{
  __ptr_t hdr;

  if (ptr == mallwatch)
    tr_break ();

  __libc_lock_lock (lock);

  __free_hook = tr_old_free_hook;
  __malloc_hook = tr_old_malloc_hook;
  __realloc_hook = tr_old_realloc_hook;
  if (tr_old_realloc_hook != NULL)
    hdr = (__ptr_t) (*tr_old_realloc_hook) (ptr, size, caller);
  else
    hdr = (__ptr_t) realloc (ptr, size);
  __free_hook = tr_freehook;
  __malloc_hook = tr_mallochook;
  __realloc_hook = tr_reallochook;

  tr_where (caller);
  if (hdr == NULL)
    /* Failed realloc.  */
    fprintf (mallstream, "! %p %#lx\n", ptr, (unsigned long int) size);
  else if (ptr == NULL)
    fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long int) size);
  else
    {
      fprintf (mallstream, "< %p\n", ptr);
      tr_where (caller);
      fprintf (mallstream, "> %p %#lx\n", hdr, (unsigned long int) size);
    }

  __libc_lock_unlock (lock);

  if (hdr == mallwatch)
    tr_break ();

  return hdr;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

const char mallenv[] = "MALLOC_TRACE" [static]

Definition at line 52 of file mtrace.c.

char* malloc_trace_buffer [static]

Definition at line 53 of file mtrace.c.

FILE* mallstream [static]

Definition at line 51 of file mtrace.c.

Definition at line 58 of file mtrace.c.

void(* tr_old_free_hook)(__ptr_t ptr, const __ptr_t) [static]

Definition at line 68 of file mtrace.c.

Definition at line 69 of file mtrace.c.

Definition at line 72 of file mtrace.c.

Definition at line 70 of file mtrace.c.