From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5166 invoked by alias); 26 Jun 2012 20:14:57 -0000 Received: (qmail 5152 invoked by uid 22791); 26 Jun 2012 20:14:55 -0000 X-SWARE-Spam-Status: No, hits=-2.4 required=5.0 tests=AWL,BAYES_00,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,KHOP_THREADED,RCVD_IN_DNSWL_NONE X-Spam-Check-By: sourceware.org Received: from smtp11.smtpout.orange.fr (HELO smtp.smtpout.orange.fr) (80.12.242.133) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 26 Jun 2012 20:14:39 +0000 Received: from treguer.localnet ([90.32.153.202]) by mwinf5d22 with ME id T8Ee1j0014NGFoi038Eebf; Tue, 26 Jun 2012 22:14:38 +0200 From: "Yann E. MORIN" To: crossgcc@sourceware.org Subject: Re: How can I modify the source of new,malloc Date: Tue, 26 Jun 2012 20:14:00 -0000 User-Agent: KMail/1.13.5 (Linux/3.4.4-treguer; KDE/4.4.5; x86_64; ; ) Cc: Mirko Banchi , Zvi Vered References: <8347C635-3CB0-4967-8BCB-5AA28FC204B1@gmail.com> In-Reply-To: <8347C635-3CB0-4967-8BCB-5AA28FC204B1@gmail.com> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201206262214.37680.yann.morin.1998@free.fr> X-IsSubscribed: yes Mailing-List: contact crossgcc-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: crossgcc-owner@sourceware.org X-SW-Source: 2012-06/txt/msg00050.txt.bz2 Zvi, Mirko, All, [Please, do not top-post...] On Tuesday 26 June 2012 00:28:09 Mirko Banchi wrote: > Il giorno 25/giu/2012, alle ore 23.55, Zvi Vered > ha scritto: > > I have a huge application (not written by me). > > I want to track all places where a dynamic allocation is done. > > Is there an alternative to my way ? > You could write your own shared object with your malloc implementation > and use LD_PRELOAD var. As Mirko suggested, I'd use a loader trick to pre-load a shared lib that overrides the malloc() and free() functions. Something like (untested!): void* malloc( size_t size ) { static void* real_malloc; if( ! real_malloc ) { real_malloc = dlsym( RTLD_NEXT, "malloc" ); if( ! real_malloc ) { panic_and_exit(); } } fprintf( stderr, "Allocating %d bytes\n", size ); return real_malloc( size ); } Then, replacing printf with calls to backtrace(3) and backtrace_symbols_fd(3), you can know the caller (function, offset), if you have the debug symbols in the binaries (libs and executable), in which case you can post-process that to find the actual file, function and line at which the call to malloc is made. (Note: do not use backtrace_symbols(3), as it calls malloc! So you have to use backtrace_symbols_fd(3)). Then, you can write such wrappers to free(3) and so on... Note however, that if you can recompile your application, you may want to link with a library like DUMA or dmalloc instead of using the above trick. http://duma.sourceforge.net/ http://dmalloc.com/ Which can be installed in the toolshain's sysroot by crosstool-NG ( I leave it up to you to find in which sub-menu these libs' options live! ;-) ) Cheers, Yann E. MORIN. -- .-----------------.--------------------.------------------.--------------------. | Yann E. MORIN | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: | | +33 662 376 056 | Software Designer | \ / CAMPAIGN | ___ | | +33 223 225 172 `------------.-------: X AGAINST | \e/ There is no | | http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL | v conspiracy. | '------------------------------^-------^------------------^--------------------' -- For unsubscribe information see http://sourceware.org/lists.html#faq