public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Are the gcc memory hooks sometimes bypassed?
@ 2010-11-15 17:10 Fabian van der Werf
  2010-11-15 22:42 ` Ian Lance Taylor
  0 siblings, 1 reply; 2+ messages in thread
From: Fabian van der Werf @ 2010-11-15 17:10 UTC (permalink / raw)
  To: gcc-help

Hi All,


For a c++ arm application I need to trace the memory allocations. To do this I am using the gcc memory hooks. For now I am only printing the allocations and deallocations, see code below.

However, the malloc's and free's don't add up. Sometimes I see a free on a memory block that didn't the malloc hook before. Or the memory is freed twice. Of course this could be a bug in my code, although I don't get a segfault. But I also see that malloc sometimes returns a pointer that it has returned before and there has been no free in the meantime (at least my free hook wasn't called).

So my guess is that certain malloc's and free's are not passed through my hooks. Note that when I only trace the c++ allocations then things do add up nicely.

Does anyone have any ideas?


Regards,

Fabian van der Werf



#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <new>
#include <unistd.h>
#include <string.h>
#include <malloc.h>

pthread_mutex_t lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;

static void push_memhooks();
static void pop_memhooks();

static void *malloc_hook(size_t size, const void *ret)
{

    pthread_mutex_lock(&lock);

    pop_memhooks();

    void *mem = malloc(size);

    if (mem) {
        printf("malloc %p\n", mem);
    }

    push_memhooks();

    pthread_mutex_unlock(&lock);

    return mem;
}

static void *realloc_hook(void* ptr, size_t size, const void *ret)
{
    pthread_mutex_lock(&lock);

    pop_memhooks();

    void* mem = realloc(ptr, size);

    if (mem) {
        printf("realloc %p -> %p\n", ptr, mem);
    }

    push_memhooks();

    pthread_mutex_unlock(&lock);

    return mem;
}

static void* memalign_hook(size_t boundary, size_t size, const void *ret)
{
    pthread_mutex_lock(&lock);
    pop_memhooks();

    void* mem = memalign(boundary, size);

    if (mem) {
        printf("memalign %p\n", mem);
    }

    push_memhooks();

    pthread_mutex_unlock(&lock);

    return mem;
}

static void free_hook(void *mem, const void *ret)
{
    pthread_mutex_lock(&lock);

    pop_memhooks();

    free(mem);

    printf("free %p\n", mem);

    push_memhooks();

    pthread_mutex_unlock(&lock);
}

void *operator new(size_t size)
{
    void* mem = malloc(size);

    if (!mem) {
        throw std::bad_alloc();
    }

    return mem;
}

void operator delete(void* mem)
{
    free(mem);
}

void *operator new[](size_t size)
{      
    void* mem = malloc(size);

    if (!mem) {
        throw std::bad_alloc();
    }

    return mem;
}

void operator delete[](void* mem)
{
    free(mem);
}

static int memhooks = 0;

static void push_memhooks()
{
    if (++memhooks == 1) {
        __malloc_hook = malloc_hook;
        __realloc_hook = realloc_hook;
        __free_hook = free_hook;
        __memalign_hook = memalign_hook;
    }
}

static void pop_memhooks()
{
    if (--memhooks == 0) {
        __malloc_hook = NULL;
        __realloc_hook = NULL;
        __free_hook = NULL;
        __memalign_hook = NULL;
    }
}

static void install_memhooks ()
{
    push_memhooks();
}

void (*__malloc_initialize_hook)(void) = install_memhooks;

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Are the gcc memory hooks sometimes bypassed?
  2010-11-15 17:10 Are the gcc memory hooks sometimes bypassed? Fabian van der Werf
@ 2010-11-15 22:42 ` Ian Lance Taylor
  0 siblings, 0 replies; 2+ messages in thread
From: Ian Lance Taylor @ 2010-11-15 22:42 UTC (permalink / raw)
  To: fabian.van.der.werf; +Cc: gcc-help

"Fabian van der Werf" <fabian.van.der.werf@altenpts.nl> writes:

> For a c++ arm application I need to trace the memory allocations. To
> do this I am using the gcc memory hooks. For now I am only printing
> the allocations and deallocations, see code below.

Those hooks are actually not part of gcc.  They are part of glibc, which
is a separate project.  I don't know why the hooks aren't adding up for
you, but the place to ask would be on a glibc mailing list.  See
http://sourceware.org/glibc/ .

Ian

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-11-15 22:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-11-15 17:10 Are the gcc memory hooks sometimes bypassed? Fabian van der Werf
2010-11-15 22:42 ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).