/* More debugging hooks for `malloc'.
- Copyright (C) 1991, 92, 93, 94, 96, 97 Free Software Foundation, Inc.
+ Copyright (C) 1991, 92, 93, 94, 96, 97, 98 Free Software Foundation, Inc.
Written April 2, 1991 by John Gilmore of Cygnus Support.
Based on mcheck.c by Mike Haertel.
#include <bits/libc-lock.h>
#endif
-#ifdef HAVE_ELF
-#include <link.h>
-#endif
+#include <ldsodefs.h>
#include <stdio.h>
-
-#ifndef __GNU_LIBRARY__
-extern char *getenv ();
-#else
+#include <string.h>
#include <stdlib.h>
+
+#include <stdio-common/_itoa.h>
+
+#ifdef USE_IN_LIBIO
+# include <libio/iolibio.h>
+# define setvbuf(s, b, f, l) _IO_setvbuf (s, b, f, l)
#endif
+#define TRACE_BUFFER_SIZE 512
+
static FILE *mallstream;
static const char mallenv[]= "MALLOC_TRACE";
-static char mallbuf[BUFSIZ]; /* Buffer for the output. */
+static char malloc_trace_buffer[TRACE_BUFFER_SIZE];
__libc_lock_define_initialized (static, lock);
Dl_info info;
if (_dl_addr (caller, &info))
{
- fprintf (mallstream, "@ %s%s%s%s%s[%p]",
+ char *buf = (char *) "";
+ if (info.dli_sname && info.dli_sname[0])
+ {
+ 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 ? ":" : "",
- info.dli_sname ? "(" : "",
- info.dli_sname ?: "", info.dli_sname ? ") " : " ",
- caller);
+ buf, caller);
}
else
#endif
tr_where (caller);
/* We could be printing a NULL here; that's OK. */
- fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long)size);
+ fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long int) size);
if (hdr == mallwatch)
tr_break ();
tr_where (caller);
if (hdr == NULL)
/* Failed realloc. */
- fprintf (mallstream, "! %p %#lx\n", ptr, (unsigned long)size);
+ fprintf (mallstream, "! %p %#lx\n", ptr, (unsigned long int) size);
else if (ptr == NULL)
- fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long)size);
+ fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long int) size);
else
- fprintf (mallstream, "< %p\n> %p %#lx\n", ptr, hdr, (unsigned long)size);
+ {
+ fprintf (mallstream, "< %p\n", ptr);
+ tr_where (caller);
+ fprintf (mallstream, "> %p %#lx\n", hdr, (unsigned long int) size);
+ }
if (hdr == mallwatch)
tr_break ();
if (mallstream != NULL)
{
/* Be sure it doesn't malloc its buffer! */
- setbuf (mallstream, mallbuf);
+ setvbuf (mallstream, malloc_trace_buffer, _IOFBF, TRACE_BUFFER_SIZE);
fprintf (mallstream, "= Start\n");
tr_old_free_hook = __free_hook;
__free_hook = tr_freehook;