Output to stderr when calling malloc / free

With Linux / GCC / C ++, I would like to write something to stderr whenever malloc / free / new / delete is called. I am trying to understand the memory allocations in a library, and therefore I would like to generate this output while doing unit tests. I use valgrind to detect mem leaks, but I cannot find a way to do this simply by distributing logs.

Any ideas? I am looking for the simplest possible solution. Recompiling the library is not an option.

+4
source share
4 answers

malloc_hook(3) allows you to globally insert your own malloc function. (There __realloc_hook __free_hook , etc., I just left them for simplicity.)

 #include <stdio.h> #include <malloc.h> static void *(*old_malloc_hook)(size_t, const void *); static void *new_malloc_hook(size_t size, const void *caller) { void *mem; __malloc_hook = old_malloc_hook; mem = malloc(size); fprintf(stderr, "%p: malloc(%zu) = %p\n", caller, size, mem); __malloc_hook = new_malloc_hook; return mem; } static void init_my_hooks(void) { old_malloc_hook = __malloc_hook; __malloc_hook = new_malloc_hook; } void (*__malloc_initialize_hook)(void) = init_my_hooks; 
  $ cat> mem.c << 'EOF'
 (the code above)
 Eof
 $ cc -fPIC -shared -o mem.so mem.c
 $ LD_PRELOAD =. / Mem.so ls
 0x7ffc14931adc: malloc (5) = 0xb40010
 0x7ffc1492c6b0: malloc (120) = 0xb40030
 0x7ffc1497f61a: malloc (12) = 0xb40010
 0x7ffc1492be38: malloc (776) = 0xb400b0
 ...

printf can call malloc , so we temporarily undo the hook. Be careful if you move malloc any way.

+3
source

You can trace calls in malloc / free with ltrace:

 #include <stdlib.h> int main (void) { void *ptr = malloc(10); free(ptr); return 0; } $ g++ test.cpp -o test $ ltrace -e malloc,free ./test malloc(10) = 0x804a008 free(0x804a008) = <void> +++ exited (status 0) +++ 

To track new / delete calls without recompiling, you probably need to use something like LD_PRELOAD to redefine calls with your own versions, this is exactly what LeakTracer can do what you want.

+15
source

This article (scroll down) gives a very clear and concise description of how to override global new and delete in C ++ (note that it does not provide an example for new[] , but it is similar in concept).

Regarding the redefinition of malloc and free, since you work with Linux and GCC, the easiest way is to use malloc_hook and free_hook . Here is a very good description of how these functions work.

+5
source

I have not tested this myself, but I am sure it will work:

  • Since you do not want to recompile the library, providing meaningful output (instead of just "new caused by 23 bytes") may require getting a stack trace. I remember how to use functions to navigate the stack, but I cannot find them right now. Perhaps calling system () and pstack (1) might do the trick.

  • You can override the operator new and delete and put this new definition in front of the std C ++ library. This may not reflect calls from containers and standard components that this library uses. This will require a relink.

  • Using LD_PRELOAD allows you to change the new operator and delete dynamically. This will not require re-linking if your application is dynamically linked.

Hope these pointers help, I'm sorry I don't have a recipe.

+1
source

All Articles