How can I modify the source of new,malloc
Joel Sherrill
joel.sherrill@oarcorp.com
Tue Jun 26 20:21:00 GMT 2012
On 06/26/2012 03:14 PM, Yann E. MORIN wrote:
> 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<veredz72@gmail.com>
>> 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! ;-) )
You may also be able to use the "--wrap=symbol" option on ld. I have
used this to provide malloc and free wrappers.
>
> Cheers,
> Yann E. MORIN.
>
--
Joel Sherrill, Ph.D. Director of Research& Development
joel.sherrill@OARcorp.com On-Line Applications Research
Ask me about RTEMS: a free RTOS Huntsville AL 35805
Support Available (256) 722-9985
--
For unsubscribe information see http://sourceware.org/lists.html#faq
More information about the crossgcc
mailing list