From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7742 invoked by alias); 27 Jun 2012 09:17:18 -0000 Received: (qmail 7733 invoked by uid 22791); 27 Jun 2012 09:17:18 -0000 X-SWARE-Spam-Status: No, hits=-3.1 required=5.0 tests=AWL,BAYES_00,KHOP_THREADED,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mail.mvtec.com (HELO mail.mvtec.com) (62.245.183.138) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 27 Jun 2012 09:17:02 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.mvtec.com (8.14.4/8.14.4) with ESMTP id q5R9H0pi003622 for ; Wed, 27 Jun 2012 11:17:00 +0200 X-Spam-Score: -4.306 Received: from mail.mvtec.com ([127.0.0.1]) by localhost (mail.mvtec.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id pQLYgqD016iZ for ; Wed, 27 Jun 2012 11:16:52 +0200 (CEST) Received: from [192.168.32.114] (mercury.mvtec [192.168.32.114]) by mail.mvtec.com (8.14.4/8.14.4) with ESMTP id q5R9GpcL003611 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO) for ; Wed, 27 Jun 2012 11:16:51 +0200 Message-ID: <4FEACF83.7010904@mvtec.com> Date: Wed, 27 Jun 2012 09:17:00 -0000 From: Eric Doenges User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 MIME-Version: 1.0 To: crossgcc@sourceware.org Subject: Re: How can I modify the source of new,malloc References: <8347C635-3CB0-4967-8BCB-5AA28FC204B1@gmail.com> <201206262214.37680.yann.morin.1998@free.fr> In-Reply-To: <201206262214.37680.yann.morin.1998@free.fr> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit 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/msg00052.txt.bz2 On 26.06.2012 22:14, Yann E. MORIN wrote: > 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... Also note that if you wrap calloc() like malloc() above, you will run into the problem that dlsym calls calloc (at least on all the Linux systems I have tested on). What I do is to load the pointers to the real functions in a special initialization function that is marked with the "constructor" attribute, and call sbrk() instead of the real calloc if the pointer to the real calloc hasn't been defined yet. > 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/ If you really want to go down the wrapper route, I can send you my wrapper off-list as a starting point. It currently prints out how much memory was requested (and tracks all allocations, so it can tell you about possible memory leaks on exit), but since I didn't know about backtrace(3) and friends it doesn't show you where the call came from (yet). -- Dr. Eric Dönges doenges@mvtec.com MVTec Software GmbH | Neherstr. 1 | 81675 München | Germany www.mvtec.com | Tel: +49 89 457695-0 | Fax: +49 89 457695-55 Geschäftsführer: Dr. Wolfgang Eckstein, Dr. Olaf Munkelt Amtsgericht München HRB 114695 -- For unsubscribe information see http://sourceware.org/lists.html#faq