This is the mail archive of the gdb@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

GDB and LD_PRELOAD library-call interception


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]