LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-14-2009, 01:22 AM   #1
shashank929
LQ Newbie
 
Registered: Apr 2009
Posts: 10

Rep: Reputation: 0
problem with mtrace


I am using RHEL 5 and I am stuck at some memory allocation problem for which I used the mtrace utility.
But the problem is that it's output is not in human readable format and for converting it, it needs a perl script called mtrace.

So, anyone please tell me from where to install mtrace on RHEL 5

Last edited by shashank929; 04-14-2009 at 01:29 AM.
 
Old 04-14-2009, 07:23 AM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by shashank929 View Post
So, anyone please tell me from where to install mtrace on RHEL 5
I use the lowly Slackware, but I'm surprised your distribution doesn't already have it.

First, do this at the bash command line:
Code:
which mtrace
If you already have it, that will tell you where it is. But if you really don't have it, then my answer depends on what you mean by
Quote:
Originally Posted by shashank929 View Post
tell me from where to install mtrace on RHEL 5
If you mean where should it go when you install it, the answer is anywhere in your typical value for the PATH environment variable. If you mean where do you get it, I see no reason why you can't just copy and paste from this copy of what's in Slackware 12.1:
Code:
#! /usr/bin/perl
eval "exec /usr/bin/perl -S $0 $*"
    if 0;
# Copyright (C) 1997-2004, 2005, 2006, 2007 Free Software Foundation, Inc.
# This file is part of the GNU C Library.
# Contributed by Ulrich Drepper <drepper@gnu.org>, 1997.
# Based on the mtrace.awk script.

# The GNU C Library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# The GNU C Library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with the GNU C Library; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA.

$VERSION = "2.7";
$PACKAGE = "libc";
$progname = $0;

sub usage {
    print "Usage: mtrace [OPTION]... [Binary] MtraceData\n";
    print "  --help       print this help, then exit\n";
    print "  --version    print version number, then exit\n";
    print "\n";
    print "For bug reporting instructions, please see:\n";
    print "<http://www.gnu.org/software/libc/bugs.html>.\n";
    exit 0;
}

# We expect two arguments:
#   #1: the complete path to the binary
#   #2: the mtrace data filename
# The usual options are also recognized.

arglist: while (@ARGV) {
    if ($ARGV[0] eq "--v" || $ARGV[0] eq "--ve" || $ARGV[0] eq "--ver" ||
	$ARGV[0] eq "--vers" || $ARGV[0] eq "--versi" ||
	$ARGV[0] eq "--versio" || $ARGV[0] eq "--version") {
	print "mtrace (GNU $PACKAGE) $VERSION\n";
	print "Copyright (C) 2007 Free Software Foundation, Inc.\n";
	print "This is free software; see the source for copying conditions.  There is NO\n";
	print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
	print "Written by Ulrich Drepper <drepper\@gnu.org>\n";

	exit 0;
    } elsif ($ARGV[0] eq "--h" || $ARGV[0] eq "--he" || $ARGV[0] eq "--hel" ||
	     $ARGV[0] eq "--help") {
	&usage;
    } elsif ($ARGV[0] =~ /^-/) {
	print "$progname: unrecognized option `$ARGV[0]'\n";
	print "Try `$progname --help' for more information.\n";
	exit 1;
    } else {
	last arglist;
    }
}

if ($#ARGV == 0) {
    $binary="";
    $data=$ARGV[0];
} elsif ($#ARGV == 1) {
    $binary=$ARGV[0];
    $data=$ARGV[1];

    if ($binary =~ /^.*[\/].*$/) {
	$prog = $binary;
    } else {
	$prog = "./$binary";
    }
    if (open (LOCS, "env LD_TRACE_LOADED_OBJECTS=1 $prog |")) {
	while (<LOCS>) {
	    chop;
	    if (/^.*=> (.*) .(0x[0123456789abcdef]*).$/) {
		$locs{$1} = $2;
	    }
	}
	close (LOCS);
    }
} else {
    die "Wrong number of arguments, run $progname --help for help.";
}

sub location {
    my $str = pop(@_);
    return $str if ($str eq "");
    if ($str =~ /.*[[](0x[^]]*)]:(.)*/) {
	my $addr = $1;
	my $fct = $2;
	return $cache{$addr} if (exists $cache{$addr});
	if ($binary ne "" && open (ADDR, "addr2line -e $binary $addr|")) {
	    my $line = <ADDR>;
	    chomp $line;
	    close (ADDR);
	    if ($line ne '??:0') {
		$cache{$addr} = $line;
		return $cache{$addr};
	    }
	}
	$cache{$addr} = $str = "$fct @ $addr";
    } elsif ($str =~ /^(.*):.*[[](0x[^]]*)]$/) {
	my $prog = $1;
	my $addr = $2;
	my $searchaddr;
	return $cache{$addr} if (exists $cache{$addr});
	if ($locs{$prog} ne "") {
	    $searchaddr = sprintf "%#x", $addr - $locs{$prog};
	} else {
	    $searchaddr = $addr;
	    $prog = $binary;
	}
	if ($binary ne "" && open (ADDR, "addr2line -e $prog $searchaddr|")) {
	    my $line = <ADDR>;
	    chomp $line;
	    close (ADDR);
	    if ($line ne '??:0') {
		$cache{$addr} = $line;
		return $cache{$addr};
	    }
	}
	$cache{$addr} = $str = $addr;
    } elsif ($str =~ /^.*[[](0x[^]]*)]$/) {
	my $addr = $1;
	return $cache{$addr} if (exists $cache{$addr});
	if ($binary ne "" && open (ADDR, "addr2line -e $binary $addr|")) {
	    my $line = <ADDR>;
	    chomp $line;
	    close (ADDR);
	    if ($line ne '??:0') {
		$cache{$addr} = $line;
		return $cache{$addr};
	    }
	}
	$cache{$addr} = $str = $addr;
    }
    return $str;
}

$nr=0;
open(DATA, "<$data") || die "Cannot open mtrace data file";
while (<DATA>) {
    my @cols = split (' ');
    my $n, $where;
    if ($cols[0] eq "@") {
	# We have address and/or function name.
	$where=$cols[1];
	$n=2;
    } else {
	$where="";
	$n=0;
    }

    $allocaddr=$cols[$n + 1];
    $howmuch=hex($cols[$n + 2]);

    ++$nr;
    SWITCH: {
	if ($cols[$n] eq "+") {
	    if (defined $allocated{$allocaddr}) {
		printf ("+ %#010x Alloc %d duplicate: %s %s\n",
			hex($allocaddr), $nr, &location($addrwas{$allocaddr}),
			$where);
	    } else {
		$allocated{$allocaddr}=$howmuch;
		$addrwas{$allocaddr}=$where;
	    }
	    last SWITCH;
	}
	if ($cols[$n] eq "-") {
	    if (defined $allocated{$allocaddr}) {
		undef $allocated{$allocaddr};
		undef $addrwas{$allocaddr};
	    } else {
		printf ("- %#010x Free %d was never alloc'd %s\n",
			hex($allocaddr), $nr, &location($where));
	    }
	    last SWITCH;
	}
	if ($cols[$n] eq "<") {
	    if (defined $allocated{$allocaddr}) {
		undef $allocated{$allocaddr};
		undef $addrwas{$allocaddr};
	    } else {
		printf ("- %#010x Realloc %d was never alloc'd %s\n",
			hex($allocaddr), $nr, &location($where));
	    }
	    last SWITCH;
	}
	if ($cols[$n] eq ">") {
	    if (defined $allocated{$allocaddr}) {
		printf ("+ %#010x Realloc %d duplicate: %#010x %s %s\n",
			hex($allocaddr), $nr, $allocated{$allocaddr},
			&location($addrwas{$allocaddr}), &location($where));
	    } else {
		$allocated{$allocaddr}=$howmuch;
		$addrwas{$allocaddr}=$where;
	    }
	    last SWITCH;
	}
	if ($cols[$n] eq "=") {
	    # Ignore "= Start".
	    last SWITCH;
	}
	if ($cols[$n] eq "!") {
	    # Ignore failed realloc for now.
	    last SWITCH;
	}
    }
}
close (DATA);

# Now print all remaining entries.
@addrs= keys %allocated;
$anything=0;
if ($#addrs >= 0) {
    foreach $addr (sort @addrs) {
	if (defined $allocated{$addr}) {
	    if ($anything == 0) {
		print "\nMemory not freed:\n-----------------\n";
		print ' ' x (10 - 7), "Address     Size     Caller\n";
		$anything=1;
	    }
	    printf ("%#010x %#8x  at %s\n", hex($addr), $allocated{$addr},
		    &location($addrwas{$addr}));
	}
    }
}
print "No memory leaks.\n" if ($anything == 0);

exit $anything != 0;
 
Old 04-15-2009, 12:25 AM   #3
shashank929
LQ Newbie
 
Registered: Apr 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Smile Thanks

Thanks a lot... that solved my problem..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Multicast Traceroute (mtrace) install issues on Redhat runlikeanantelope Linux - Networking 2 06-08-2011 09:37 AM
Is glibc mtrace thread-safe? wielemaker Programming 0 12-19-2007 03:09 PM
mtrace / addr2line not resolving address to file + line number jph4599 Linux - Software 0 01-12-2007 03:54 PM
Sound Card problem(every time i install linux i have diffirent hardware problem) jacka1l Linux - Newbie 7 08-11-2005 06:10 AM
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration