X-Git-Url: http://git.csclub.uwaterloo.ca/?p=kopensolaris-gnu%2Fglibc.git;a=blobdiff_plain;f=malloc%2Fmtrace.c;h=d15569c9d6df06cb68fe2c26627fba1a1dcf394f;hp=df5b944b28ab224a82f2f09784c347c1e4b56344;hb=743471fe77464fefc691b203779939468f58e67d;hpb=dd5acff9c80abe072a372ad95512580b0e26efb6 diff --git a/malloc/mtrace.c b/malloc/mtrace.c index df5b944b28..d15569c9d6 100644 --- a/malloc/mtrace.c +++ b/malloc/mtrace.c @@ -28,21 +28,24 @@ #include #endif -#ifdef HAVE_ELF -#include -#endif +#include #include - -#ifndef __GNU_LIBRARY__ -extern char *getenv (); -#else +#include #include + +#include + +#ifdef USE_IN_LIBIO +# include +# 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); @@ -90,11 +93,27 @@ tr_where (caller) 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 @@ -144,7 +163,7 @@ tr_mallochook (size, caller) 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 (); @@ -182,11 +201,15 @@ tr_reallochook (ptr, size, caller) 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 (); @@ -242,7 +265,7 @@ mtrace () 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;