Blame malloc/mtrace.pl

Packit 6c4009
#! @PERL@
Packit 6c4009
eval "exec @PERL@ -S $0 $@"
Packit 6c4009
    if 0;
Packit 6c4009
# Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
# This file is part of the GNU C Library.
Packit 6c4009
# Contributed by Ulrich Drepper <drepper@gnu.org>, 1997.
Packit 6c4009
# Based on the mtrace.awk script.
Packit 6c4009
Packit 6c4009
# The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
# modify it under the terms of the GNU Lesser General Public
Packit 6c4009
# License as published by the Free Software Foundation; either
Packit 6c4009
# version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
# The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
# Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
# You should have received a copy of the GNU Lesser General Public
Packit 6c4009
# License along with the GNU C Library; if not, see
Packit 6c4009
# <http://www.gnu.org/licenses/>.
Packit 6c4009
Packit 6c4009
$VERSION = "@VERSION@";
Packit 6c4009
$PKGVERSION = "@PKGVERSION@";
Packit 6c4009
$REPORT_BUGS_TO = '@REPORT_BUGS_TO@';
Packit 6c4009
$progname = $0;
Packit 6c4009
Packit 6c4009
sub usage {
Packit 6c4009
    print "Usage: mtrace [OPTION]... [Binary] MtraceData\n";
Packit 6c4009
    print "  --help       print this help, then exit\n";
Packit 6c4009
    print "  --version    print version number, then exit\n";
Packit 6c4009
    print "\n";
Packit 6c4009
    print "For bug reporting instructions, please see:\n";
Packit 6c4009
    print "$REPORT_BUGS_TO.\n";
Packit 6c4009
    exit 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
# We expect two arguments:
Packit 6c4009
#   #1: the complete path to the binary
Packit 6c4009
#   #2: the mtrace data filename
Packit 6c4009
# The usual options are also recognized.
Packit 6c4009
Packit 6c4009
arglist: while (@ARGV) {
Packit 6c4009
    if ($ARGV[0] eq "--v" || $ARGV[0] eq "--ve" || $ARGV[0] eq "--ver" ||
Packit 6c4009
	$ARGV[0] eq "--vers" || $ARGV[0] eq "--versi" ||
Packit 6c4009
	$ARGV[0] eq "--versio" || $ARGV[0] eq "--version") {
Packit 6c4009
	print "mtrace $PKGVERSION$VERSION\n";
Packit 6c4009
	print "Copyright (C) 2018 Free Software Foundation, Inc.\n";
Packit 6c4009
	print "This is free software; see the source for copying conditions.  There is NO\n";
Packit 6c4009
	print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
Packit 6c4009
	print "Written by Ulrich Drepper <drepper\@gnu.org>\n";
Packit 6c4009
Packit 6c4009
	exit 0;
Packit 6c4009
    } elsif ($ARGV[0] eq "--h" || $ARGV[0] eq "--he" || $ARGV[0] eq "--hel" ||
Packit 6c4009
	     $ARGV[0] eq "--help") {
Packit 6c4009
	&usage;
Packit 6c4009
    } elsif ($ARGV[0] =~ /^-/) {
Packit 6c4009
	print "$progname: unrecognized option `$ARGV[0]'\n";
Packit 6c4009
	print "Try `$progname --help' for more information.\n";
Packit 6c4009
	exit 1;
Packit 6c4009
    } else {
Packit 6c4009
	last arglist;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
if ($#ARGV == 0) {
Packit 6c4009
    $binary="";
Packit 6c4009
    $data=$ARGV[0];
Packit 6c4009
} elsif ($#ARGV == 1) {
Packit 6c4009
    $binary=$ARGV[0];
Packit 6c4009
    $data=$ARGV[1];
Packit 6c4009
Packit 6c4009
    if ($binary =~ /^.*[\/].*$/) {
Packit 6c4009
	$prog = $binary;
Packit 6c4009
    } else {
Packit 6c4009
	$prog = "./$binary";
Packit 6c4009
    }
Packit 6c4009
    if (open (LOCS, "env LD_TRACE_LOADED_OBJECTS=1 $prog |")) {
Packit 6c4009
	while (<LOCS>) {
Packit 6c4009
	    chop;
Packit 6c4009
	    if (/^.*=> (.*) .(0x[0123456789abcdef]*).$/) {
Packit 6c4009
		$locs{$1} = $2;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
	close (LOCS);
Packit 6c4009
    }
Packit 6c4009
} else {
Packit 6c4009
    die "Wrong number of arguments, run $progname --help for help.";
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
sub location {
Packit 6c4009
    my $str = pop(@_);
Packit 6c4009
    return $str if ($str eq "");
Packit 6c4009
    if ($str =~ /.*[[](0x[^]]*)]:(.)*/) {
Packit 6c4009
	my $addr = $1;
Packit 6c4009
	my $fct = $2;
Packit 6c4009
	return $cache{$addr} if (exists $cache{$addr});
Packit 6c4009
	if ($binary ne "" && open (ADDR, "addr2line -e $binary $addr|")) {
Packit 6c4009
	    my $line = <ADDR>;
Packit 6c4009
	    chomp $line;
Packit 6c4009
	    close (ADDR);
Packit 6c4009
	    if ($line ne '??:0') {
Packit 6c4009
		$cache{$addr} = $line;
Packit 6c4009
		return $cache{$addr};
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
	$cache{$addr} = $str = "$fct @ $addr";
Packit 6c4009
    } elsif ($str =~ /^(.*):.*[[](0x[^]]*)]$/) {
Packit 6c4009
	my $prog = $1;
Packit 6c4009
	my $addr = $2;
Packit 6c4009
	my $searchaddr;
Packit 6c4009
	return $cache{$addr} if (exists $cache{$addr});
Packit 6c4009
	if ($locs{$prog} ne "") {
Packit 6c4009
	    $searchaddr = sprintf "%#x", $addr - $locs{$prog};
Packit 6c4009
	} else {
Packit 6c4009
	    $searchaddr = $addr;
Packit 6c4009
	    $prog = $binary;
Packit 6c4009
	}
Packit 6c4009
	if ($binary ne "" && open (ADDR, "addr2line -e $prog $searchaddr|")) {
Packit 6c4009
	    my $line = <ADDR>;
Packit 6c4009
	    chomp $line;
Packit 6c4009
	    close (ADDR);
Packit 6c4009
	    if ($line ne '??:0') {
Packit 6c4009
		$cache{$addr} = $line;
Packit 6c4009
		return $cache{$addr};
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
	$cache{$addr} = $str = $addr;
Packit 6c4009
    } elsif ($str =~ /^.*[[](0x[^]]*)]$/) {
Packit 6c4009
	my $addr = $1;
Packit 6c4009
	return $cache{$addr} if (exists $cache{$addr});
Packit 6c4009
	if ($binary ne "" && open (ADDR, "addr2line -e $binary $addr|")) {
Packit 6c4009
	    my $line = <ADDR>;
Packit 6c4009
	    chomp $line;
Packit 6c4009
	    close (ADDR);
Packit 6c4009
	    if ($line ne '??:0') {
Packit 6c4009
		$cache{$addr} = $line;
Packit 6c4009
		return $cache{$addr};
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
	$cache{$addr} = $str = $addr;
Packit 6c4009
    }
Packit 6c4009
    return $str;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
$nr=0;
Packit 6c4009
open(DATA, "<$data") || die "Cannot open mtrace data file";
Packit 6c4009
while (<DATA>) {
Packit 6c4009
    my @cols = split (' ');
Packit 6c4009
    my $n, $where;
Packit 6c4009
    if ($cols[0] eq "@") {
Packit 6c4009
	# We have address and/or function name.
Packit 6c4009
	$where=$cols[1];
Packit 6c4009
	$n=2;
Packit 6c4009
    } else {
Packit 6c4009
	$where="";
Packit 6c4009
	$n=0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
    $allocaddr=$cols[$n + 1];
Packit 6c4009
    $howmuch=hex($cols[$n + 2]);
Packit 6c4009
Packit 6c4009
    ++$nr;
Packit 6c4009
    SWITCH: {
Packit 6c4009
	if ($cols[$n] eq "+") {
Packit 6c4009
	    if (defined $allocated{$allocaddr}) {
Packit 6c4009
		printf ("+ %#0@XXX@x Alloc %d duplicate: %s %s\n",
Packit 6c4009
			hex($allocaddr), $nr, &location($addrwas{$allocaddr}),
Packit 6c4009
			$where);
Packit 6c4009
	    } elsif ($allocaddr =~ /^0x/) {
Packit 6c4009
		$allocated{$allocaddr}=$howmuch;
Packit 6c4009
		$addrwas{$allocaddr}=$where;
Packit 6c4009
	    }
Packit 6c4009
	    last SWITCH;
Packit 6c4009
	}
Packit 6c4009
	if ($cols[$n] eq "-") {
Packit 6c4009
	    if (defined $allocated{$allocaddr}) {
Packit 6c4009
		undef $allocated{$allocaddr};
Packit 6c4009
		undef $addrwas{$allocaddr};
Packit 6c4009
	    } else {
Packit 6c4009
		printf ("- %#0@XXX@x Free %d was never alloc'd %s\n",
Packit 6c4009
			hex($allocaddr), $nr, &location($where));
Packit 6c4009
	    }
Packit 6c4009
	    last SWITCH;
Packit 6c4009
	}
Packit 6c4009
	if ($cols[$n] eq "<") {
Packit 6c4009
	    if (defined $allocated{$allocaddr}) {
Packit 6c4009
		undef $allocated{$allocaddr};
Packit 6c4009
		undef $addrwas{$allocaddr};
Packit 6c4009
	    } else {
Packit 6c4009
		printf ("- %#0@XXX@x Realloc %d was never alloc'd %s\n",
Packit 6c4009
			hex($allocaddr), $nr, &location($where));
Packit 6c4009
	    }
Packit 6c4009
	    last SWITCH;
Packit 6c4009
	}
Packit 6c4009
	if ($cols[$n] eq ">") {
Packit 6c4009
	    if (defined $allocated{$allocaddr}) {
Packit 6c4009
		printf ("+ %#0@XXX@x Realloc %d duplicate: %#010x %s %s\n",
Packit 6c4009
			hex($allocaddr), $nr, $allocated{$allocaddr},
Packit 6c4009
			&location($addrwas{$allocaddr}), &location($where));
Packit 6c4009
	    } else {
Packit 6c4009
		$allocated{$allocaddr}=$howmuch;
Packit 6c4009
		$addrwas{$allocaddr}=$where;
Packit 6c4009
	    }
Packit 6c4009
	    last SWITCH;
Packit 6c4009
	}
Packit 6c4009
	if ($cols[$n] eq "=") {
Packit 6c4009
	    # Ignore "= Start".
Packit 6c4009
	    last SWITCH;
Packit 6c4009
	}
Packit 6c4009
	if ($cols[$n] eq "!") {
Packit 6c4009
	    # Ignore failed realloc for now.
Packit 6c4009
	    last SWITCH;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
close (DATA);
Packit 6c4009
Packit 6c4009
# Now print all remaining entries.
Packit 6c4009
@addrs= keys %allocated;
Packit 6c4009
$anything=0;
Packit 6c4009
if ($#addrs >= 0) {
Packit 6c4009
    foreach $addr (sort @addrs) {
Packit 6c4009
	if (defined $allocated{$addr}) {
Packit 6c4009
	    if ($anything == 0) {
Packit 6c4009
		print "\nMemory not freed:\n-----------------\n";
Packit 6c4009
		print ' ' x (@XXX@ - 7), "Address     Size     Caller\n";
Packit 6c4009
		$anything=1;
Packit 6c4009
	    }
Packit 6c4009
	    printf ("%#0@XXX@x %#8x  at %s\n", hex($addr), $allocated{$addr},
Packit 6c4009
		    &location($addrwas{$addr}));
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
print "No memory leaks.\n" if ($anything == 0);
Packit 6c4009
Packit 6c4009
exit $anything != 0;