]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - malloc/mtrace.pl
Update copyright dates not handled by scripts/update-copyrights
[thirdparty/glibc.git] / malloc / mtrace.pl
index 46d8425bee62bdd6a661e904f7b3c8a641433066..075da0d9f11da15c32fb50dbe4a9d34b6e90e2cc 100644 (file)
@@ -1,34 +1,36 @@
 #! @PERL@
-eval "exec @PERL@ -S $0 $*"
+eval "exec @PERL@ -S $0 $@"
     if 0;
-# Copyright (C) 1997 Free Software Foundation, Inc.
+# Copyright (C) 1997-2024 Free Software Foundation, Inc.
 # This file is part of the GNU C Library.
-# Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 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 Library General Public License as
-# published by the Free Software Foundation; either version 2 of the
-# License, or (at your option) any later version.
+# 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
-# Library General Public License for more details.
+# Lesser General Public License for more details.
 
-# You should have received a copy of the GNU Library General Public
-# License along with the GNU C Library; see the file COPYING.LIB.  If not,
-# write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-# Boston, MA 02111-1307, USA.
+# You should have received a copy of the GNU Lesser General Public
+# License along with the GNU C Library; if not, see
+# <https://www.gnu.org/licenses/>.
 
 $VERSION = "@VERSION@";
-$PACKAGE = "libc";
+$PKGVERSION = "@PKGVERSION@";
+$REPORT_BUGS_TO = '@REPORT_BUGS_TO@';
 $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 "$REPORT_BUGS_TO.\n";
     exit 0;
 }
 
@@ -41,11 +43,11 @@ 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) 1997 Free Software Foundation, Inc.\n";
+       print "mtrace $PKGVERSION$VERSION\n";
+       print "Copyright (C) 2024 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.ai.mit.edu>\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" ||
@@ -66,35 +68,77 @@ if ($#ARGV == 0) {
 } elsif ($#ARGV == 1) {
     $binary=$ARGV[0];
     $data=$ARGV[1];
+
+    if ($binary =~ /^.*[\/].*$/) {
+       $prog = $binary;
+    } else {
+       $prog = "./$binary";
+    }
+    # Set the environment variable LD_TRACE_LOADED_OBJECTS to 2 so the
+    # executable is also printed.
+    if (open (locs, "env LD_TRACE_LOADED_OBJECTS=2 $prog |")) {
+       while (<locs>) {
+           chop;
+           if (/^.*=> (.*) .(0x[0123456789abcdef]*).$/) {
+               $locs{$1} = $2;
+               $rel{$1} = hex($2);
+           }
+       }
+       close (LOCS);
+    }
 } else {
-    die "Wrong number of arguments.";
+    die "Wrong number of arguments, run $progname --help for help.";
 }
 
+sub addr2line {
+    my $addr = pop(@_);
+    my $prog = pop(@_);
+    if (open (ADDR, "addr2line -e $prog $addr|")) {
+       my $line = <ADDR>;
+       chomp $line;
+       close (ADDR);
+       if ($line ne '??:0') {
+           return $line
+       }
+    }
+}
 sub location {
     my $str = pop(@_);
     return $str if ($str eq "");
-    if ($str =~ /[[](0x[^]]*)]:(.)*/) {
+    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') {
+       if ($binary ne "") {
+           my $line = &addr2line($binary, $addr);
+           if ($line) {
                $cache{$addr} = $line;
                return $cache{$addr};
            }
        }
        $cache{$addr} = $str = "$fct @ $addr";
-    } elsif ($str =~ /^[[](0x[^]]*)]$/) {
+    } elsif ($str =~ /^(.*):.*[[](0x[^]]*)]$/) {
+       my $prog = $1;
+       my $addr = $2;
+       my $searchaddr;
+       return $cache{$addr} if (exists $cache{$addr});
+       $searchaddr = sprintf "%#x", hex($addr) + $rel{$prog};
+       if ($binary ne "") {
+           for my $address ($searchaddr, $addr) {
+               my $line = &addr2line($prog, $address);
+               if ($line) {
+                   $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') {
+       if ($binary ne "") {
+           my $line = &addr2line($binary, $addr);
+           if ($line) {
                $cache{$addr} = $line;
                return $cache{$addr};
            }
@@ -125,20 +169,21 @@ while (<DATA>) {
     SWITCH: {
        if ($cols[$n] eq "+") {
            if (defined $allocated{$allocaddr}) {
-               printf ("+ %#010x Alloc %d duplicate: %s %s\n",
-                       hex($allocaddr), $nr, $wherewas{$allocaddr}, $where);
-           } else {
+               printf ("+ %#0@XXX@x Alloc %d duplicate: %s %s\n",
+                       hex($allocaddr), $nr, &location($addrwas{$allocaddr}),
+                       $where);
+           } elsif ($allocaddr =~ /^0x/) {
                $allocated{$allocaddr}=$howmuch;
-               $wherewas{$allocaddr}=&location($where);
+               $addrwas{$allocaddr}=$where;
            }
            last SWITCH;
        }
        if ($cols[$n] eq "-") {
            if (defined $allocated{$allocaddr}) {
                undef $allocated{$allocaddr};
-               undef $wherewas{$allocaddr};
+               undef $addrwas{$allocaddr};
            } else {
-               printf ("- %#010x Free %d was never alloc'd %s\n",
+               printf ("- %#0@XXX@x Free %d was never alloc'd %s\n",
                        hex($allocaddr), $nr, &location($where));
            }
            last SWITCH;
@@ -146,21 +191,21 @@ while (<DATA>) {
        if ($cols[$n] eq "<") {
            if (defined $allocated{$allocaddr}) {
                undef $allocated{$allocaddr};
-               undef $wherewas{$allocaddr};
+               undef $addrwas{$allocaddr};
            } else {
-               printf ("- %#010x Realloc %d was never alloc'd %s\n",
+               printf ("- %#0@XXX@x 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",
+               printf ("+ %#0@XXX@x Realloc %d duplicate: %#010x %s %s\n",
                        hex($allocaddr), $nr, $allocated{$allocaddr},
-                       $wherewas{$allocaddr}, &location($where));
+                       &location($addrwas{$allocaddr}), &location($where));
            } else {
                $allocated{$allocaddr}=$howmuch;
-               $wherewas{$allocaddr}=&location($where);
+               $addrwas{$allocaddr}=$where;
            }
            last SWITCH;
        }
@@ -178,15 +223,20 @@ close (DATA);
 
 # Now print all remaining entries.
 @addrs= keys %allocated;
+$anything=0;
 if ($#addrs >= 0) {
-    print "\nNot freed memory:\n-----------------\n";
-    print ' ' x (@XXX@ - 7), "Address     Size     Caller\n";
     foreach $addr (sort @addrs) {
        if (defined $allocated{$addr}) {
+           if ($anything == 0) {
+               print "\nMemory not freed:\n-----------------\n";
+               print ' ' x (@XXX@ - 7), "Address     Size     Caller\n";
+               $anything=1;
+           }
            printf ("%#0@XXX@x %#8x  at %s\n", hex($addr), $allocated{$addr},
-                   $wherewas{$addr});
+                   &location($addrwas{$addr}));
        }
     }
 }
+print "No memory leaks.\n" if ($anything == 0);
 
-exit 0;
+exit $anything != 0;