From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9229 invoked by alias); 31 Mar 2011 08:25:44 -0000 Received: (qmail 9217 invoked by uid 22791); 31 Mar 2011 08:25:42 -0000 X-SWARE-Spam-Status: No, hits=-1.9 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: sourceware.org Received: from mail-vw0-f41.google.com (HELO mail-vw0-f41.google.com) (209.85.212.41) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 31 Mar 2011 08:25:37 +0000 Received: by vws4 with SMTP id 4so2127685vws.0 for ; Thu, 31 Mar 2011 01:25:37 -0700 (PDT) Received: by 10.52.0.198 with SMTP id 6mr3062486vdg.152.1301559937084; Thu, 31 Mar 2011 01:25:37 -0700 (PDT) MIME-Version: 1.0 Received: by 10.220.200.3 with HTTP; Thu, 31 Mar 2011 01:25:17 -0700 (PDT) From: Kevin Pouget Date: Thu, 31 Mar 2011 08:25:00 -0000 Message-ID: Subject: GDB and LD_PRELOAD library-call interception To: gdb@sourceware.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org X-SW-Source: 2011-03/txt/msg00209.txt.bz2 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 > > static void my_init (void) __attribute__ ((constructor)); > static void my_init (void) > { > =A0 printf("Hello world\n"); > } > > void * malloc(size_t size) > { > > =A0 void * ret; > > =A0 if(!malloc_func) { > =A0=A0=A0 printf("define malloc") ; > =A0=A0=A0 malloc_func =3D (void *(*)()) dlsym(RTLD_NEXT, "malloc"); > =A0 } > =A0 ret =3D malloc_func(size); > =A0 printf("malloc(%ld) =3D %p\n", size, ret); > =A0 return(ret); > } a standard execution: > > $ LD_PRELOAD=3D./libjit.so ./sleeper > Hello world > define malloc > malloc(64) =3D 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=3D./libjit.so > (gdb) start > Temporary breakpoint 1 at 0x400508: file sleeper.c, line 5. > Starting program: /home/kevin/travail/arm/perso/root/sample/debugger/slee= per > Hello world > define malloc > malloc(5) =3D 0x8e6010 > ... (repeated thousands of times, with different sizes) ... > Hello world > > Temporary breakpoint 1, main () at sleeper.c:8 > 8=A0=A0=A0 =A0=A0=A0 malloc(64) ; > (gdb) next > define malloc malloc > malloc(64) =3D 0x601010 > 9=A0=A0=A0 =A0=A0=A0=A0=A0=A0=A0 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