c - An alternative for the deprecated __malloc_hook functionality of glibc -
i writing memory profiler c , intercepting calls malloc, realloc , free functions via malloc_hooks. unfortunately, these deprecated because of poor behaviour in multithreaded environments. not find document describing alternative best practice solution achieve same thing, can enlighten me?
i've read simple #define malloc(s) malloc_hook(s)
trick, not work system setup have in mind, because intrusive original code base suitable use in profiling / tracing tool. having manually change original application code killer decent profiler. optimally, solution looking should enabled or disabled linking optional shared library. example, current setup uses function declared __attribute__ ((constructor))
install intercepting malloc hooks.
thanks
after trying things, managed figure out how this.
first of all, in glibc
, malloc
defined weak symbol, means can overwritten application or shared library. hence, ld_preload
not needed. instead, implemented following function in shared library:
void* malloc (size_t size) { [ ... ] }
which gets called application instead of glibc
s malloc
.
now, equivalent __malloc_hook
s functionality, couple of things still missing.
1.) caller address
in addition original parameters malloc
, glibc
s __malloc_hook
s provide address of calling function, return address of malloc
return to. achieve same thing, can use __builtin_return_address
function available in gcc. have not looked other compilers, because limited gcc anyway, if happen know how such thing portably, please drop me comment :)
our malloc
function looks this:
void* malloc (size_t size) { void *caller = __builtin_return_address(0); [ ... ] }
2.) accessing glibc
s malloc within hook
as limited glibc in application, chose use __libc_malloc
access original malloc implementation. alternatively, dlsym(rtld_next, "malloc")
can used, @ possible pitfall function uses calloc
on first call, possibly resulting in infinite loop leading segfault.
complete malloc hook
my complete hooking function looks this:
extern void *__libc_malloc(size_t size); int malloc_hook_active = 0; void* malloc (size_t size) { void *caller = __builtin_return_address(0); if (malloc_hook_active) return my_malloc_hook(size, caller); return __libc_malloc(size); }
where my_malloc_hook
looks this:
void* my_malloc_hook (size_t size, void *caller) { void *result; // deactivate hooks logging malloc_hook_active = 0; result = malloc(size); // logging [ ... ] // reactivate hooks malloc_hook_active = 1; return result; }
of course, hooks calloc
, realloc
, free
work similarly.
dynamic , static linking
with these functions, dynamic linking works out of box. linking .so file containing malloc hook implementation result of calls malloc
application , library calls routed through hook. static linking problematic though. have not yet wrapped head around completely, in static linking malloc not weak symbol, resulting in multiple definition error @ link time.
if need static linking whatever reason, example translating function addresses in 3rd party libraries code lines via debug symbols, can link these 3rd party libs statically while still linking malloc hooks dynamically, avoiding multiple definition problem. have not yet found better workaround this, if know one,feel free leave me comment.
here short example:
gcc -o test test.c -lmalloc_hook_library -wl,-bstatic -l3rdparty -wl,-bdynamic
3rdparty
linked statically, while malloc_hook_library
linked dynamically, resulting in expected behaviour, , addresses of functions in 3rdparty
translatable via debug symbols in test
. pretty neat, huh?
conlusion
the techniques above describe non-deprecated, pretty equivalent approach __malloc_hook
s, couple of mean limitations:
__builtin_caller_address
works gcc
__libc_malloc
works glibc
dlsym(rtld_next, [...])
gnu extension in glibc
the linker flags -wl,-bstatic
, -wl,-bdynamic
specific gnu binutils.
in other words, solution utterly non-portable , alternative solutions have added if hooks library ported non-gnu operating system.
C - An Alternative For The Deprecated __Malloc_Hook Functionality Of
ReplyDeleteGlibc - >>>>> Download Now
>>>>> Download Full
C - An Alternative For The Deprecated __Malloc_Hook Functionality Of
Glibc - >>>>> Download LINK
>>>>> Download Now
C - An Alternative For The Deprecated __Malloc_Hook Functionality Of
Glibc - >>>>> Download Full
>>>>> Download LINK dI