Mirror of the gdb mailing list
 help / color / mirror / Atom feed
From: Kevin Pouget <kevin.pouget@gmail.com>
To: gdb@sourceware.org
Subject: GDB and LD_PRELOAD library-call interception
Date: Thu, 31 Mar 2011 08:25:00 -0000	[thread overview]
Message-ID: <AANLkTi=tESNe+Uj574gAQpiGXCX0ptM3ANsBWUOu+z+J@mail.gmail.com> (raw)

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


             reply	other threads:[~2011-03-31  8:25 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-31  8:25 Kevin Pouget [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='AANLkTi=tESNe+Uj574gAQpiGXCX0ptM3ANsBWUOu+z+J@mail.gmail.com' \
    --to=kevin.pouget@gmail.com \
    --cc=gdb@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox