public inbox for crossgcc@sourceware.org
 help / color / mirror / Atom feed
From: "Yann E. MORIN" <yann.morin.1998@free.fr>
To: crossgcc@sourceware.org
Cc: Mirko Banchi <mk.banchi@gmail.com>, Zvi Vered <veredz72@gmail.com>
Subject: Re: How can I modify the source of new,malloc
Date: Tue, 26 Jun 2012 20:14:00 -0000	[thread overview]
Message-ID: <201206262214.37680.yann.morin.1998@free.fr> (raw)
In-Reply-To: <8347C635-3CB0-4967-8BCB-5AA28FC204B1@gmail.com>

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! ;-) )

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

  reply	other threads:[~2012-06-26 20:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-25 21:55 Zvi Vered
2012-06-25 22:28 ` Mirko Banchi
2012-06-26 20:14   ` Yann E. MORIN [this message]
2012-06-26 20:21     ` Joel Sherrill
2012-06-27  9:17     ` Eric Doenges
2012-06-27  9:51       ` Eric Doenges
2012-06-27 20:05         ` Yann E. MORIN

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201206262214.37680.yann.morin.1998@free.fr \
    --to=yann.morin.1998@free.fr \
    --cc=crossgcc@sourceware.org \
    --cc=mk.banchi@gmail.com \
    --cc=veredz72@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).