This is the mail archive of the crossgcc@sourceware.org mailing list for the crossgcc project.
See crosstool-NG for lots more information.
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
Other format: | [Raw text] |
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
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |