Mirror of the gdb mailing list
 help / color / mirror / Atom feed
* GDB and LD_PRELOAD library-call interception
@ 2011-03-31  8:25 Kevin Pouget
  2011-03-31  8:47 ` Jan Kratochvil
  0 siblings, 1 reply; 9+ messages in thread
From: Kevin Pouget @ 2011-03-31  8:25 UTC (permalink / raw)
  To: gdb

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


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

end of thread, other threads:[~2011-04-04 13:35 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-31  8:25 GDB and LD_PRELOAD library-call interception Kevin Pouget
2011-03-31  8:47 ` Jan Kratochvil
2011-03-31  9:47   ` Kevin Pouget
2011-03-31 12:16     ` Jan Kratochvil
2011-03-31 15:07       ` Tom Tromey
2011-04-03 16:54       ` Xavier de Gaye
     [not found]         ` <BANLkTi=j6+-85R4B+N1Nd5kb9bkEZfsT9A@mail.gmail.com>
2011-04-04 13:35           ` Kevin Pouget
2011-03-31 15:06     ` Tom Tromey
2011-03-31 15:55       ` Kevin Pouget

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox