GDB and LD_PRELOAD library-call interception

Kevin Pouget kevin.pouget@gmail.com
Thu Mar 31 08:25:00 GMT 2011


Hello,

I'm playing with LD_PRELOAD to intercept some libC calls, and the
behavior I observe under seems a bit strange:

my shared library:
>
> #define __USE_GNU
> #include <dlfcn.h>
>
> static void my_init (void) __attribute__ ((constructor));
> static void my_init (void)
> {
>   printf("Hello world\n");
> }
>
> void * malloc(size_t size)
> {
>
>   void * ret;
>
>   if(!malloc_func) {
>     printf("define malloc") ;
>     malloc_func = (void *(*)()) dlsym(RTLD_NEXT, "malloc");
>   }
>   ret = malloc_func(size);
>   printf("malloc(%ld) = %p\n", size, ret);
>   return(ret);
> }

a standard execution:
>
> $ LD_PRELOAD=./libjit.so ./sleeper
> Hello world
> define malloc
> malloc(64) = 0x1158010


and a GDB execution:
>
> $ gdb-cvs ./sleeper
> GNU gdb (GDB) 7.2.50.20110321-cvs
> # (same with GNU gdb (GDB) Fedora (7.2-46.fc14))
> (gdb) set environment LD_PRELOAD=./libjit.so
> (gdb) start
> Temporary breakpoint 1 at 0x400508: file sleeper.c, line 5.
> Starting program: /home/kevin/travail/arm/perso/root/sample/debugger/sleeper
> Hello world
> define malloc
> malloc(5) = 0x8e6010
> ... (repeated thousands of times, with different sizes) ...
> Hello world
>
> Temporary breakpoint 1, main () at sleeper.c:8
> 8        malloc(64) ;
> (gdb) next
> define malloc malloc
> malloc(64) = 0x601010
> 9            return 0;


it looks like if the library is loaded twice, without any control on
the first load ('my_init' pending breakpoint is only resolved before
the second execution)

Is it a bug or a feature? (or did I do something wrong ?)


Thanks,

Kevin



More information about the Gdb mailing list