From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3840 invoked by alias); 27 Jun 2012 20:05:29 -0000 Received: (qmail 3813 invoked by uid 22791); 27 Jun 2012 20:05:27 -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 smtp10.smtpout.orange.fr (HELO smtp.smtpout.orange.fr) (80.12.242.132) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 27 Jun 2012 20:05:13 +0000 Received: from treguer.localnet ([90.32.122.212]) by mwinf5d19 with ME id TY5B1j0064b3ar603Y5BUu; Wed, 27 Jun 2012 22:05:12 +0200 From: "Yann E. MORIN" To: crossgcc@sourceware.org Subject: Re: How can I modify the source of new,malloc Date: Wed, 27 Jun 2012 20:05:00 -0000 User-Agent: KMail/1.13.5 (Linux/3.4.4-treguer; KDE/4.4.5; x86_64; ; ) Cc: Eric Doenges References: <4FEACF83.7010904@mvtec.com> <4FEAD792.8040200@mvtec.com> In-Reply-To: <4FEAD792.8040200@mvtec.com> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201206272205.10753.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/msg00054.txt.bz2 Eric, All, On Wednesday 27 June 2012 11:51:14 Eric Doenges wrote: > On 27.06.2012 11:16, Eric Doenges wrote: > > 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)). > > Just a short addendum: backtrace() calls malloc() internally (see bug > report from 2005 http://sourceware.org/bugzilla/show_bug.cgi?id=956), Hmmm. Baaaad... And, as usual, I appreciate very much the comments in that bug report! ;-p > so > unfortunately you can't use it in your malloc/free wrapper. You probably could, with some tricky tricks (this is not thread-safe, but no less than was my initial code-snippet): void* malloc( size_t size ) { static void* real_malloc; static int in_malloc; if( ! real_malloc ) { real_malloc = dlsym( RTLD_NEXT, "malloc" ); if( ! real_malloc ) { panic_and_exit(); } } if( ! in_malloc ) { in_malloc = 1; fprintf( stderr, "Allocating %d bytes\n", size ); backtrace( blablabla ); in_malloc = 0; } return real_malloc( size ); } You could even probably make it thread-safe by using pthread keys, though how to do it is left as an exercise for the reader. ;-) And of course, this would break in awfull ways if somehow malloc() becomes unavailable (because of corruption or whatever), but in this case you're deeply screwed anyway... :-/ So, if you need to debug memory allocation, I would highly suggest that you have a look at DUMA or dmalloc. Regards, 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